/core/admin.php

  1. <?php 
  2.  
  3. /*************************** LOAD THE BASE CLASS ******************************* 
  4. *******************************************************************************/ 
  5.  
  6. if(!class_exists('WP_List_Table')) { 
  7. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); 
  8.  
  9. /************************ EXTEND WP_List_Table CLASS *************************** 
  10. *******************************************************************************/ 
  11.  
  12. class GPI_List_Table extends WP_List_Table { 
  13.  
  14. function __construct() { 
  15. global $status, $page, $hook_suffix; 
  16.  
  17. //Set parent defaults 
  18. parent::__construct( array( 
  19. 'singular' => 'gpi_page_report',  
  20. 'plural' => 'gpi_page_reports',  
  21. 'ajax' => false 
  22. ) ); 
  23.  
  24.  
  25. // here for compatibility with 4.3 
  26. function get_columns() 
  27. // Get options 
  28. $gpi_options = $this->getOptions(); 
  29.  
  30. return $this->gpi_get_columns(false, false, false, $gpi_options['strategy']); 
  31.  
  32. // humanTiming used to calculate time since last report check 
  33. function humanTiming($time) 
  34. if(empty($time)) return 'N/A'; 
  35. $time = time() - $time; 
  36.  
  37. $tokens = array ( 
  38. 31536000 => 'year',  
  39. 2592000 => 'month',  
  40. 604800 => 'week',  
  41. 86400 => 'day',  
  42. 3600 => 'hour',  
  43. 60 => 'minute',  
  44. 1 => 'second' 
  45. ); 
  46.  
  47. foreach ($tokens as $unit => $text) { 
  48. if ($time < $unit) continue; 
  49. $numberOfUnits = floor($time / $unit); 
  50. return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s ago':' ago'); 
  51.  
  52.  
  53. function no_items() { 
  54. $pagetype = isset( $_GET['render'] ) ? $_GET['render'] : 'list'; 
  55.  
  56. switch($pagetype) 
  57. case 'list': 
  58. _e( 'No Pagespeed Reports Found. Google Pagespeed may still be checking your pages. If problems persist, see the following possible solutions:', 'gpagespeedi' ); 
  59. ?> 
  60. <ul class="no-items"> 
  61. <li><?php _e( 'Make sure that you have entered your Google API key on the ', 'gpagespeedi' );?><a href="?page=<?php echo $_REQUEST['page']; ?>&render=options">Options</a> page.</li> 
  62. <li><?php _e( 'Make sure that you have enabled "PageSpeed Insights API" from the Services page of the ', 'gpagespeedi' );?><a href="https://code.google.com/apis/console/">Google Console</a>.</li> 
  63. <li><?php _e( 'The Google Pagespeed API may be temporarily unavailable.', 'gpagespeedi' );?></li> 
  64. </ul> 
  65. <?php 
  66. break; 
  67.  
  68. case 'ignored-urls': 
  69. _e( 'No Ignored URLs found. A URL can be ignored from the <a href="?page=' . $_REQUEST['page'] . '&render=list">Report List</a> page if you would like to remove it from report pages', 'gpagespeedi' ); 
  70. break; 
  71.  
  72.  
  73. // Get our plugin options 
  74. function getOptions($option_name = 'gpagespeedi_options') { 
  75. $gpi_options = get_option($option_name); 
  76.  
  77. return $gpi_options; 
  78.  
  79. // Figure out which types to query for based on the plugin options 
  80. function getTypesToCheck($restrict_type = 'all') { 
  81.  
  82. $types = array(); 
  83. $gpi_options = $this->getOptions(); 
  84. $typestocheck = array(); 
  85.  
  86. if($gpi_options['check_pages']) { 
  87. if($restrict_type == 'all' || $restrict_type == 'ignored' || $restrict_type == 'pages') { 
  88. $typestocheck[] = 'type = %s'; 
  89. $types[1][] = "page"; 
  90.  
  91. if($gpi_options['check_posts']) { 
  92. if($restrict_type == 'all' || $restrict_type == 'ignored' || $restrict_type == 'posts') { 
  93. $typestocheck[] = 'type = %s'; 
  94. $types[1][] = "post"; 
  95.  
  96. if($gpi_options['check_categories']) { 
  97. if($restrict_type == 'all' || $restrict_type == 'ignored' || $restrict_type == 'categories') { 
  98. $typestocheck[] = 'type = %s'; 
  99. $types[1][] = "category"; 
  100. if($gpi_options['cpt_whitelist']) { 
  101. if($restrict_type == 'all' || $restrict_type == 'ignored' || stristr($restrict_type, 'gpi_custom_posts')) { 
  102.  
  103. $cpt_whitelist_arr = false; 
  104. if(!empty($gpi_options['cpt_whitelist'])) { 
  105. $cpt_whitelist_arr = unserialize($gpi_options['cpt_whitelist']); 
  106. $args=array( 
  107. 'public' => true,  
  108. '_builtin' => false 
  109. );  
  110. $custom_post_types = get_post_types($args, 'names', 'and'); 
  111. if($restrict_type != 'gpi_custom_posts' && $restrict_type != 'all' && $restrict_type != 'ignored') { 
  112. $restrict_type = str_replace('gpi_custom_posts-', '', $restrict_type); 
  113. foreach($custom_post_types as $post_type) 
  114. if($cpt_whitelist_arr && in_array($post_type, $cpt_whitelist_arr)) { 
  115. if($post_type == $restrict_type) { 
  116. $typestocheck[] = 'type = %s'; 
  117. $types[1][] = $custom_post_types[$post_type]; 
  118. } else { 
  119. foreach($custom_post_types as $post_type) 
  120. if($cpt_whitelist_arr && in_array($post_type, $cpt_whitelist_arr)) { 
  121. $typestocheck[] = 'type = %s'; 
  122. $types[1][] = $custom_post_types[$post_type]; 
  123.  
  124. if(!empty($typestocheck)) { 
  125. $types[0] = ''; 
  126. foreach($typestocheck as $type) 
  127. if(!is_array($type)) { 
  128. $types[0] .= $type . ' OR '; 
  129. } else { 
  130. foreach($type as $custom_post_type) 
  131. $types[0] .= 'type = %s OR '; 
  132. $types[1][] = $custom_post_type; 
  133. $types[0] = rtrim($types[0], ' OR '); 
  134. return $types; 
  135. return null; 
  136.  
  137. // Default Column handling behaviors 
  138. function column_default($item, $column_name) { 
  139. switch($column_name) { 
  140. case 'desktop_last_modified': 
  141. $formatted_time = $this->humanTiming($item['desktop_last_modified']); 
  142. return $formatted_time; 
  143. case 'mobile_last_modified': 
  144. $formatted_time = $this->humanTiming($item['mobile_last_modified']); 
  145. return $formatted_time; 
  146. case 'type': 
  147. return $item['type']; 
  148. default: 
  149. return print_r($item, true); //Show the whole array for troubleshooting purposes 
  150.  
  151. // URL column handling behavior 
  152. function column_URL($item) { 
  153.  
  154. //Strip domain from URL 
  155. $siteurl = get_site_url(); 
  156. $siteurl_ssl = get_site_url('', '', 'https'); 
  157.  
  158. $search_urls = array($siteurl, $siteurl_ssl); 
  159.  
  160. $cleaned_url = str_replace($search_urls, '', $item['URL']); 
  161.  
  162. //Build row actions 
  163. $actions = array( 
  164. 'view_details' => sprintf('<a href="?page=%s&render=%s&page_id=%s">%s</a>', $_REQUEST['page'], 'details', $item['ID'], __('Details', 'gpagespeedi')),  
  165. 'ignore' => sprintf('?page=%s&render=%s&action=%s&id=%s', $_REQUEST['page'], 'list', 'ignore', $item['ID']),  
  166. 'visit' => sprintf('<a href="%s" target="_blank">%s</a>', $item['URL'], __('View URL', 'gpagespeedi')),  
  167. ); 
  168.  
  169. $url_to_nonce = $actions['ignore']; 
  170. $nonced_url = ( function_exists('wp_nonce_url') ) ? wp_nonce_url($url_to_nonce, 'bulk-gpi_page_reports') : $url_to_nonce; 
  171. $actions['ignore'] = '<a href="'.$nonced_url.'">'.__('Ignore', 'gpagespeedi').'</a>'; 
  172.  
  173. //Return the url contents 
  174. return sprintf('<a href="?page=%3$s&render=%4$s&page_id=%5$s">%1$s</a> %2$s',  
  175. /**$1%s*/ $cleaned_url,  
  176. /**$2%s*/ $this->row_actions($actions),  
  177. /**$3%s*/ $_REQUEST['page'],  
  178. /**$4%s*/ 'details',  
  179. /**$5%s*/ $item['ID'] 
  180. ); 
  181.  
  182. // Ignored URL column handling behavior 
  183. function column_Ignored_URL($item) { 
  184.  
  185. //Strip domain from URL 
  186. $siteurl = get_site_url(); 
  187. $siteurl_ssl = get_site_url('', '', 'https'); 
  188.  
  189. $search_urls = array($siteurl, $siteurl_ssl); 
  190.  
  191. $cleaned_url = str_replace($search_urls, '', $item['URL']); 
  192.  
  193. //Build row actions 
  194. $actions = array( 
  195. 'activate' => sprintf('?page=%s&render=%s&action=%s&id=%s', $_REQUEST['page'], 'ignored-urls', 'activate', $item['ID']),  
  196. 'visit' => sprintf('<a href="%s" target="_blank">%s</a>', $item['URL'], __('View URL', 'gpagespeedi')),  
  197. ); 
  198.  
  199. $url_to_nonce = $actions['activate']; 
  200. $nonced_url = ( function_exists('wp_nonce_url') ) ? wp_nonce_url($url_to_nonce, 'bulk-gpi_page_reports') : $url_to_nonce; 
  201. $actions['activate'] = '<a href="'.$nonced_url.'">'.__('Reactivate', 'gpagespeedi').'</a>'; 
  202.  
  203. //Return the url contents 
  204. return sprintf('%1$s %2$s',  
  205. /**$1%s*/ $cleaned_url,  
  206. /**$2%s*/ $this->row_actions($actions) 
  207. ); 
  208.  
  209. // Mobile Score column handling behavior 
  210. function column_mobile_score($item) { 
  211.  
  212. if(empty($item['mobile_score'])) return 'N/A'; 
  213.  
  214. $gradient = array("", "FF0000", "FE0500", "FE0A00", "FE0F00", "FE1400", "FE1900", "FE1E00", "FE2300", "FE2800", "FE2D00", "FE3300", "FE3800", "FE3D00", "FE4200", "FE4700", "FE4C00", "FE5100", "FD5600", "FD5B00", "FD6000", "FD6600", "FD6B00", "FD7000", "FD7500", "FD7A00", "FD7F00", "FD8400", "FD8900", "FD8E00", "FD9300", "FD9900", "FD9E00", "FDA300", "FDA800", "FCAD00", "FCB200", "FCB700", "FCBC00", "FCC100", "FCC600", "FCCC00", "FCD100", "FCD600", "FCDB00", "FCE000", "FCE500", "FCEA00", "FCEF00", "FCF400", "FCF900", "FCFF00", "F7FF00", "F2FF00", "EEFF00", "E9FF00", "E4FF00", "E0FF00", "DBFF00", "D6FF00", "D2FF00", "CDFF00", "C8FF00", "C4FF00", "BFFF00", "BAFF00", "B6FF00", "B1FF00", "ACFF00", "A8FF00", "A3FF00", "9EFF00", "9AFF00", "95FF00", "90FF00", "8CFF00", "87FF00", "83FF00", "7EFF00", "79FF00", "75FF00", "70FF00", "6BFF00", "67FF00", "62FF00", "5DFF00", "59FF00", "54FF00", "4FFF00", "4BFF00", "46FF00", "41FF00", "3DFF00", "38FF00", "33FF00", "2FFF00", "2AFF00", "25FF00", "21FF00", "1CFF00", "18FF00"); 
  215. $barcolor = $gradient[$item['mobile_score']]; 
  216. $innerdiv_css = "background-color:#".$barcolor.";width:".$item['mobile_score']."%"; 
  217.  
  218. //Return the score contents 
  219. return sprintf('<span class="scorenum">%1$s</span><div class="reportscore_outter_bar"><div class="reportscore_inner_bar" style="%2$s"></div></div>', $item['mobile_score'], $innerdiv_css); 
  220.  
  221. // Desktop Score column handling behavior 
  222. function column_desktop_score($item) { 
  223.  
  224. if(empty($item['desktop_score'])) return 'N/A'; 
  225.  
  226. $gradient = array("", "FF0000", "FE0500", "FE0A00", "FE0F00", "FE1400", "FE1900", "FE1E00", "FE2300", "FE2800", "FE2D00", "FE3300", "FE3800", "FE3D00", "FE4200", "FE4700", "FE4C00", "FE5100", "FD5600", "FD5B00", "FD6000", "FD6600", "FD6B00", "FD7000", "FD7500", "FD7A00", "FD7F00", "FD8400", "FD8900", "FD8E00", "FD9300", "FD9900", "FD9E00", "FDA300", "FDA800", "FCAD00", "FCB200", "FCB700", "FCBC00", "FCC100", "FCC600", "FCCC00", "FCD100", "FCD600", "FCDB00", "FCE000", "FCE500", "FCEA00", "FCEF00", "FCF400", "FCF900", "FCFF00", "F7FF00", "F2FF00", "EEFF00", "E9FF00", "E4FF00", "E0FF00", "DBFF00", "D6FF00", "D2FF00", "CDFF00", "C8FF00", "C4FF00", "BFFF00", "BAFF00", "B6FF00", "B1FF00", "ACFF00", "A8FF00", "A3FF00", "9EFF00", "9AFF00", "95FF00", "90FF00", "8CFF00", "87FF00", "83FF00", "7EFF00", "79FF00", "75FF00", "70FF00", "6BFF00", "67FF00", "62FF00", "5DFF00", "59FF00", "54FF00", "4FFF00", "4BFF00", "46FF00", "41FF00", "3DFF00", "38FF00", "33FF00", "2FFF00", "2AFF00", "25FF00", "21FF00", "1CFF00", "18FF00"); 
  227. $barcolor = $gradient[$item['desktop_score']]; 
  228. $innerdiv_css = "background-color:#".$barcolor.";width:".$item['desktop_score']."%"; 
  229.  
  230. //Return the score contents 
  231. return sprintf('<span class="scorenum">%1$s</span><div class="reportscore_outter_bar"><div class="reportscore_inner_bar" style="%2$s"></div></div>', $item['desktop_score'], $innerdiv_css); 
  232.  
  233. // Checkboxes column handling 
  234. function column_cb($item) { 
  235. return sprintf( 
  236. '<input type="checkbox" name="%1$s[]" value="%2$s" />',  
  237. /**$1%s*/ $this->_args['singular'],  
  238. /**$2%s*/ $item['ID'] 
  239. ); 
  240.  
  241. // Setup available column types 
  242. function gpi_get_columns($ignored = false, $strategy) { 
  243. if($ignored) { 
  244. $columns = array( 
  245. 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text 
  246. 'ignored_url' => __('Ignored URL', 'gpagespeedi'),  
  247. 'type' => __('Page Type', 'gpagespeedi'),  
  248. ); 
  249. } elseif($strategy == "desktop") { 
  250. $columns = array( 
  251. 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text 
  252. 'url' => __('URL', 'gpagespeedi'),  
  253. 'desktop_score' => __('Score', 'gpagespeedi'),  
  254. 'type' => __('Page Type', 'gpagespeedi'),  
  255. 'desktop_last_modified' => __('Last Checked', 'gpagespeedi'),  
  256. ); 
  257. } elseif($strategy == "mobile") { 
  258. $columns = array( 
  259. 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text 
  260. 'url' => __('URL', 'gpagespeedi'),  
  261. 'mobile_score' => __('Score', 'gpagespeedi'),  
  262. 'type' => __('Page Type', 'gpagespeedi'),  
  263. 'mobile_last_modified' => __('Last Checked', 'gpagespeedi') 
  264. ); 
  265. } else { 
  266. $columns = array( 
  267. 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text 
  268. 'url' => __('URL', 'gpagespeedi'),  
  269. 'desktop_score' => __('Score (Desktop)', 'gpagespeedi'),  
  270. 'mobile_score' => __('Score (Mobile)', 'gpagespeedi'),  
  271. 'type' => __('Page Type', 'gpagespeedi'),  
  272. 'desktop_last_modified' => __('Last Checked (Desktop)', 'gpagespeedi'),  
  273. 'mobile_last_modified' => __('Last Checked (Mobile)', 'gpagespeedi') 
  274. ); 
  275. return $columns; 
  276.  
  277. // Setup sortable columns 
  278. function get_sortable_columns($ignored_urls = false) { 
  279. $filter = (isset($_GET['filter'])) ? $_GET['filter'] : 'all'; 
  280. if($ignored_urls) { 
  281. $sortable_columns = array( 
  282. 'type' => array('type', false) 
  283. ); 
  284. } elseif($filter == "all" || $filter == "custom_posts") { 
  285. $sortable_columns = array( 
  286. 'desktop_score' => array('desktop_score', false),  
  287. 'mobile_score' => array('mobile_score', false),  
  288. 'type' => array('type', false) 
  289. ); 
  290. } else { 
  291. $sortable_columns = array( 
  292. 'desktop_score' => array('desktop_score', false),  
  293. 'mobile_score' => array('mobile_score', false) 
  294. ); 
  295. return $sortable_columns; 
  296.  
  297. // Setup bulk actions 
  298. function get_bulk_actions() { 
  299. $filter = (isset($_GET['filter'])) ? $_GET['filter'] : ''; 
  300. $render = (isset($_GET['render'])) ? $_GET['render'] : ''; 
  301. if($render == "ignored-urls") { 
  302. $actions = array( 
  303. 'activate' => __('Reactivate', 'gpagespeedi') 
  304. ); 
  305. } else { 
  306. $actions = array( 
  307. 'ignore' => __('Ignore', 'gpagespeedi') 
  308. //'recheck' => __('Recheck', 'gpagespeedi') 
  309. ); 
  310. return $actions; 
  311.  
  312. function extra_tablenav( $which ) { 
  313.  
  314. $gpi_options = $this->getOptions(); 
  315.  
  316. // Filter report type 
  317. $report_filter = 'all'; 
  318. if(isset($_GET['filter'])) { 
  319. $report_filter = $_GET['filter']; 
  320.  
  321. // Custom Posts Filter 
  322. $args=array( 
  323. 'public' => true,  
  324. '_builtin' => false 
  325. );  
  326. $custom_post_types = get_post_types($args, 'names', 'and'); 
  327. $subfilters['custom_posts'] = $custom_post_types; 
  328.  
  329. // Custom URLs Filter 
  330. global $wpdb; 
  331.  
  332. $post_per_page = ( isset($_GET['post-per-page']) ) ? $_GET['post-per-page'] : 25 ; 
  333.  
  334. if ( 'top' == $which ) { 
  335. ?> 
  336.  
  337. <div class="alignleft actions"> 
  338. <?php if( isset( $_GET['render'] ) && ( $_GET['render'] == "list" || $_GET['render'] == "summary") ) { ?> 
  339. <select name="filter" id="filter"> 
  340. <option value="all"><?php _e('All Reports', 'gpagespeedi'); ?></option> 
  341. <?php if($gpi_options['check_pages']) { ?> 
  342. <option <?php if($report_filter == 'pages') { echo 'selected="selected"'; } ?> value="pages"><?php _e('Pages', 'gpagespeedi'); ?></option> 
  343. <?php } ?> 
  344. <?php if($gpi_options['check_posts']) { ?> 
  345. <option <?php if($report_filter == 'posts') { echo 'selected="selected"'; } ?> value="posts"><?php _e('Posts', 'gpagespeedi'); ?></option> 
  346. <?php } ?> 
  347. <?php if($gpi_options['check_categories']) { ?> 
  348. <option <?php if($report_filter == 'categories') { echo 'selected="selected"'; } ?> value="categories"><?php _e('Categories', 'gpagespeedi'); ?></option> 
  349. <?php } ?> 
  350. <?php if($gpi_options['cpt_whitelist']) { 
  351.  
  352. $cpt_whitelist_arr = false; 
  353. if(!empty($gpi_options['cpt_whitelist'])) { 
  354. $cpt_whitelist_arr = unserialize($gpi_options['cpt_whitelist']); 
  355.  
  356. if($cpt_whitelist_arr) { 
  357. ?> 
  358. <optgroup label="<?php _e('Custom Post Types', 'gpagespeedi'); ?>"> 
  359. <option <?php if($report_filter == 'gpi_custom_posts') { echo 'selected="selected"'; } ?> value="gpi_custom_posts"><?php _e('All Custom Post Types', 'gpagespeedi'); ?></option> 
  360. <?php 
  361. $check_filter = str_replace('gpi_custom_posts-', '', $report_filter); 
  362. foreach($subfilters['custom_posts'] as $filter) 
  363. if(in_array($filter, $cpt_whitelist_arr)) { 
  364. ?> 
  365. <option <?php if($check_filter == $filter) { echo 'selected="selected"'; } ?> value='gpi_custom_posts-<?php echo $filter; ?>'><?php echo $filter; ?></option> 
  366. <?php 
  367. ?> 
  368. </optgroup> 
  369. <?php 
  370. } ?> 
  371. </select> 
  372. <?php } ?> 
  373. <?php if( isset( $_GET['render'] ) && $_GET['render'] != "summary") { ?> 
  374. <select name="post-per-page" id="post-per-page"> 
  375. <option value="25" <?php if($post_per_page == 25) {echo 'selected="selected"';} ?>><?php _e('25 Results/Page', 'gpagespeedi'); ?></option> 
  376. <option value="50" <?php if($post_per_page == 50) {echo 'selected="selected"';} ?>><?php _e('50 Results/Page', 'gpagespeedi'); ?></option> 
  377. <option value="100" <?php if($post_per_page == 100) {echo 'selected="selected"';} ?>><?php _e('100 Results/Page', 'gpagespeedi'); ?></option> 
  378. <option value="500" <?php if($post_per_page == 500) {echo 'selected="selected"';} ?>><?php _e('500 Results/Page', 'gpagespeedi'); ?></option> 
  379. <option value="1000" <?php if($post_per_page == 1000) {echo 'selected="selected"';} ?>><?php _e('1000 Results/Page', 'gpagespeedi'); ?></option> 
  380. </select> 
  381. <?php } ?> 
  382. <?php 
  383. submit_button( __( 'Filter', 'gpagespeedi' ), 'button', false, false, array( 'id' => 'post-query-submit' ) ); 
  384. ?> 
  385. </div> 
  386. <?php 
  387.  
  388. // Prepare our data for display - Active items 
  389. function prepare_items($ignored_query = false, $type = '', $per_page = 25) { 
  390. global $wpdb; //This is used only if making any database queries 
  391.  
  392. // Get options 
  393. $gpi_options = $this->getOptions(); 
  394.  
  395. // Setup Columns 
  396. $columns = $this->gpi_get_columns($ignored_query, $gpi_options['strategy']); 
  397. $hidden = array(); 
  398. $sortable = $this->get_sortable_columns($ignored_query); 
  399. $this->_column_headers = array($columns, $hidden, $sortable);  
  400.  
  401. // Figure out which page types we are checking (set in options) 
  402. $typestocheck = $this->getTypesToCheck($type); 
  403.  
  404. // Not Null check for Report List scores 
  405. switch($gpi_options['strategy']) { 
  406.  
  407. case 'both': 
  408. $nullcheck = 'desktop_score IS NOT NULL AND mobile_score IS NOT NULL'; 
  409. break; 
  410.  
  411. case 'mobile': 
  412. $nullcheck = 'mobile_score IS NOT NULL'; 
  413. break; 
  414.  
  415. case 'desktop': 
  416. $nullcheck = 'desktop_score IS NOT NULL'; 
  417. break; 
  418.  
  419.  
  420. // Get our Data 
  421. if(!is_null($typestocheck) && !$ignored_query) { 
  422. $gpi_page_stats = $wpdb->prefix . 'gpi_page_stats'; 
  423. if(isset($_GET['orderby'])) $orderby = $_GET['orderby']; 
  424. if(isset($_GET['order'])) $order = $_GET['order']; 
  425.  
  426. if(isset($orderby)) { 
  427.  
  428. $data = $wpdb->get_results( 
  429. $wpdb->prepare( 
  430. "SELECT ID, URL, desktop_score, mobile_score, desktop_last_modified, mobile_last_modified, type 
  431. FROM $gpi_page_stats 
  432. WHERE ($typestocheck[0]) 
  433. AND $nullcheck 
  434. ORDER BY $orderby $order",  
  435. $typestocheck[1] 
  436. ),  
  437. ARRAY_A  
  438. ); 
  439.  
  440. } else { 
  441.  
  442. $data = $wpdb->get_results( 
  443. $wpdb->prepare( 
  444. "SELECT ID, URL, desktop_score, mobile_score, desktop_last_modified, mobile_last_modified, type 
  445. FROM $gpi_page_stats 
  446. WHERE ($typestocheck[0]) 
  447. AND $nullcheck",  
  448. $typestocheck[1] 
  449. ),  
  450. ARRAY_A  
  451. ); 
  452.  
  453.  
  454. } elseif($ignored_query) { 
  455. $gpi_page_blacklist = $wpdb->prefix . 'gpi_page_blacklist'; 
  456. if(isset($_GET['orderby'])) $orderby = $_GET['orderby']; 
  457. if(isset($_GET['order'])) $order = $_GET['order']; 
  458. if(isset($orderby)) { 
  459.  
  460. $data = $wpdb->get_results(  
  461. "SELECT ID, URL, type 
  462. FROM $gpi_page_blacklist 
  463. ORDER BY $orderby $order",  
  464. ARRAY_A  
  465. ); 
  466.  
  467. } else { 
  468.  
  469. $data = $wpdb->get_results( 
  470. "SELECT ID, URL, type 
  471. FROM $gpi_page_blacklist 
  472. ORDER BY ID DESC",  
  473. ARRAY_A 
  474. ); 
  475.  
  476.  
  477. } else { 
  478. $data = array(); 
  479.  
  480. // Figure out which page of results we are on 
  481. $current_page = $this->get_pagenum(); 
  482.  
  483. // Total number of results 
  484. $total_items = count($data); 
  485.  
  486. // Slice up our data for Pagination 
  487. $data = array_slice($data, (($current_page-1)*$per_page), $per_page); 
  488.  
  489. // Return sorted data to be used 
  490. $this->items = $data; 
  491.  
  492. // Register pagination 
  493. $this->set_pagination_args( array( 
  494. 'total_items' => $total_items,  
  495. 'per_page' => $per_page,  
  496. 'total_pages' => ceil($total_items/$per_page) 
  497. ) ); 
  498.  
  499.  
  500. /********************************** DO ACTIONS ********************************** 
  501. ********************************************************************************/ 
  502.  
  503. function do_gpi_actions() { 
  504.  
  505. if(!isset($_GET['page']) || $_GET['page'] != 'google-pagespeed-insights') { 
  506. return; 
  507.  
  508. $gpi_options = get_option('gpagespeedi_options'); 
  509.  
  510. if(!isset($_GET['render'])) { 
  511.  
  512. if($gpi_options['google_developer_key'] == '') { 
  513. wp_redirect( '?page=' . $_REQUEST['page'] . '&render=options' ); 
  514. } else { 
  515. wp_redirect( '?page=' . $_REQUEST['page'] . '&render=list' ); 
  516.  
  517.  
  518. $doaction = ''; 
  519. if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] ) 
  520. $doaction = $_REQUEST['action']; 
  521.  
  522. if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] ) 
  523. $doaction = $_REQUEST['action2']; 
  524.  
  525. if($doaction != '') { 
  526.  
  527. check_admin_referer('bulk-gpi_page_reports'); 
  528.  
  529. $page_id = (isset($_GET['id'])) ? $_GET['id'] : ''; 
  530. $page_report = (isset($_GET['gpi_page_report'])) ? $_GET['gpi_page_report'] : ''; 
  531.  
  532. switch($doaction) 
  533. case 'recheck': 
  534. require_once(GPI_DIRECTORY . '/includes/actions/recheck.php'); 
  535. $recheck_urls = gpi_action_recheck_page($page_id, $page_report); 
  536. $action_message = $recheck_urls . ' ' . __('URLs have been scheduled for a recheck. Depending on the number of URLs to check, this may take a while to complete.', 'gpagespeedi'); 
  537. break; 
  538. case 'single-recheck': 
  539. require_once(GPI_DIRECTORY . '/includes/actions/single-recheck.php'); 
  540. $page_id = (isset($_GET['page_id'])) ? $_GET['page_id'] : ''; 
  541. $recheck_url = gpi_action_single_recheck_page($page_id); 
  542. $action_message = $recheck_url; 
  543. break; 
  544. case 'activate': 
  545. require_once(GPI_DIRECTORY . '/includes/actions/activate.php'); 
  546. $activated_pages = gpi_action_activate_page($page_id, $page_report); 
  547. $action_message = $activated_pages . ' ' . __('URLs have been reactivated.', 'gpagespeedi'); 
  548. break; 
  549. case 'ignore': 
  550. require_once(GPI_DIRECTORY . '/includes/actions/ignore.php'); 
  551. $ignored_pages = gpi_action_ignore_page($page_id, $page_report); 
  552. $action_message = $ignored_pages . ' ' . __('URLs have been ignored.', 'gpagespeedi'); 
  553. break; 
  554.  
  555. if(isset($action_message)) { 
  556. require_once GPI_DIRECTORY . '/core/core.php'; 
  557. $googlePagespeedInsights = new googlePagespeedInsights($gpi_options); 
  558. $googlePagespeedInsights->google_pagespeed_insights_Update_Options('action_message', $action_message, 'gpagespeedi_ui_options');  
  559.  
  560. $default_strategy = ( isset($_GET['strategy']) ) ? $_GET['strategy'] : false; 
  561. if(!empty($default_strategy)) { 
  562. if($default_strategy == 'mobile' || $default_strategy == 'desktop') { 
  563. require_once GPI_DIRECTORY . '/core/core.php'; 
  564. $googlePagespeedInsights = new googlePagespeedInsights($gpi_options); 
  565. $googlePagespeedInsights->google_pagespeed_insights_Update_Options('view_preference', $default_strategy, 'gpagespeedi_ui_options'); 
  566. }  
  567.  
  568. if ( !empty($_GET['_wp_http_referer']) || !empty($_GET['action']) || !empty($_GET['action2']) || !empty($_GET['strategy']) ) { 
  569. wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'action', 'action2', 'id', 'gpi_page_report', 'single-recheck', 'strategy' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) ); 
  570. exit; 
  571. add_action( 'admin_init', 'do_gpi_actions', 9 ); 
  572.  
  573. /** ************************ REGISTER THE ADMIN PAGE **************************** 
  574. ********************************************************************************/ 
  575.  
  576. function google_pagespeed_insights_menu() { 
  577. global $gpi_management_page; 
  578. $gpi_management_page = add_management_page( 'Google Pagespeed Insights', 'Pagespeed Insights', 'manage_options', 'google-pagespeed-insights', 'gpi_render_admin_page' ); 
  579. add_action( 'admin_menu', 'google_pagespeed_insights_menu', 10 ); 
  580.  
  581. function load_GPI_style($hook) { 
  582.  
  583. global $gpi_management_page; 
  584. if($hook != $gpi_management_page) return; 
  585.  
  586. wp_register_style( 'gpagespeedi_css', plugins_url('/css/gpagespeedi_styles.css', GPI_PLUGIN_FILE), false, '1.0.0' ); 
  587. wp_enqueue_style( 'gpagespeedi_css' ); 
  588.  
  589. wp_register_script( 'gpagespeedi_javascript', plugins_url('/js/gpagespeedi_javascript.js', GPI_PLUGIN_FILE), array('jquery'), '1.0.0' ); 
  590. wp_register_script( 'jquery-rotate', plugins_url('/js/jQueryRotateCompressed.js', GPI_PLUGIN_FILE), array('jquery'), '1.0.0' ); 
  591. wp_enqueue_script( 'gpagespeedi_javascript' ); 
  592. wp_enqueue_script( 'jquery-rotate' ); 
  593. add_action( 'admin_enqueue_scripts', 'load_GPI_style' ); 
  594.  
  595. function gpi_register_scripts($hook) { 
  596.  
  597. global $gpi_management_page; 
  598. if($hook != $gpi_management_page) return; 
  599.  
  600. wp_enqueue_script( 'gpi-status-ajax', GPI_PUBLIC_PATH . '/js/ajax.js', array( 'jquery' ));  
  601. wp_localize_script( 'gpi-status-ajax', 'GPI_Ajax', array( 
  602. 'ajaxurl' => admin_url( 'admin-ajax.php' ),  
  603. 'gpiNonce' => wp_create_nonce( 'gpiNonce' ),  
  604. 'report_page' => '?page=' . $_REQUEST['page'] . '&render=list' 
  605. ); 
  606.  
  607. $gpi_options = get_option('gpagespeedi_options'); 
  608.  
  609. if($gpi_options['scan_method'] == "ajax") { 
  610. if(!$gpi_options['first_run_complete'] && $gpi_options['google_developer_key'] != '' || isset($_POST['check_new_pages']) || isset($_POST['recheck_all_pages'])) { 
  611. if( isset( $_POST['recheck_all_pages'] ) ) { 
  612. $recheck = 'true'; 
  613. } else { 
  614. $recheck = 'false'; 
  615.  
  616. wp_enqueue_script( 'gpi-worker-ajax', GPI_PUBLIC_PATH . '/js/run_worker.js', array( 'jquery' ));  
  617. wp_localize_script( 'gpi-worker-ajax', 'GPI_WorkerAjax', array( 
  618. 'ajaxurl' => admin_url( 'admin-ajax.php' ),  
  619. 'gpiNonce' => wp_create_nonce( 'gpiNonce' ),  
  620. 'recheck' => $recheck 
  621. ); 
  622.  
  623. add_action( 'admin_enqueue_scripts', 'gpi_register_scripts' ); 
  624.  
  625. function gpi_check_status_callback() { 
  626.  
  627. if(!wp_verify_nonce($_POST['gpiNonce'], 'gpiNonce')) { 
  628. echo 'nonce_failure'; 
  629. exit; 
  630.  
  631. $options = get_option('gpagespeedi_options'); 
  632.  
  633. $current_status = $options['progress']; 
  634. if($current_status != null) { 
  635.  
  636. $split_status = explode(':', $current_status); 
  637.  
  638. $percent_complete = $split_status[0] / $split_status[1]; 
  639. $percent_complete = round($percent_complete * 100); 
  640.  
  641. echo $percent_complete; 
  642. } else { 
  643. echo 'done'; 
  644. exit; 
  645. add_action('wp_ajax_gpi_check_status', 'gpi_check_status_callback'); 
  646.  
  647. function gpi_run_worker_service() { 
  648.  
  649. if(!wp_verify_nonce($_POST['gpiNonce'], 'gpiNonce')) { 
  650. echo 'nonce_failure'; 
  651. exit; 
  652.  
  653. if($_POST['recheck'] == 'true') { 
  654. $recheck = true; 
  655. } else { 
  656. $recheck = false; 
  657.  
  658. $gpi_options = get_option('gpagespeedi_options'); 
  659.  
  660. require_once GPI_DIRECTORY . '/core/core.php'; 
  661. $googlePagespeedInsights = new googlePagespeedInsights($gpi_options); 
  662.  
  663. $googlePagespeedInsights->googlepagespeedinsightsworker( array(), true, $recheck ); 
  664.  
  665. exit; 
  666. add_action('wp_ajax_gpi_run_worker_service', 'gpi_run_worker_service'); 
  667.  
  668. /***************************** RENDER ADMIN PAGES ******************************** 
  669. *********************************************************************************/ 
  670.  
  671. // This is our page type selector 
  672. function gpi_render_admin_page() { 
  673.  
  674. $admin_page = (isset($_GET['render'])) ? $_GET['render'] : ''; 
  675. if ( isset( $_REQUEST['render1'] ) && -1 != $_REQUEST['render1'] ) 
  676. $admin_page = $_REQUEST['render1']; 
  677.  
  678. if ( isset( $_REQUEST['render2'] ) && -1 != $_REQUEST['render2'] ) 
  679. $admin_page = $_REQUEST['render2']; 
  680.  
  681. $GPI_ListTable = new GPI_List_Table(); 
  682. $gpi_options = $GPI_ListTable->getOptions(); 
  683. $gpi_ui_options = $GPI_ListTable->getOptions('gpagespeedi_ui_options'); 
  684.  
  685. ?> 
  686. <div class="wrap"> 
  687.  
  688. <div id="icon-gpi" class="icon32"><br/></div> 
  689. <h2>Google Pagespeed Insights</h2> 
  690. <div class="reportmodes"> 
  691. <?php if($gpi_options['strategy'] == 'both' || $gpi_options['strategy'] == 'desktop') { ?> 
  692. <a href="<?php echo $_SERVER['REQUEST_URI']; ?>&strategy=desktop" class="button-gpi desktop<?php if($gpi_ui_options['view_preference'] == "desktop") { echo ' active'; } ?>"><?php _e('Desktop Mode', 'gpagespeedi'); ?></a> 
  693. <?php } ?> 
  694. <?php if($gpi_options['strategy'] == 'both' || $gpi_options['strategy'] == 'mobile') { ?> 
  695. <a href="<?php echo $_SERVER['REQUEST_URI']; ?>&strategy=mobile" class="button-gpi mobile<?php if($gpi_ui_options['view_preference'] == "mobile") { echo ' active'; } ?>"><?php _e('Mobile Mode', 'gpagespeedi'); ?></a> 
  696. <?php } ?> 
  697. </div> 
  698. <h3 class="nav-tab-wrapper"> 
  699. <a href="?page=<?php echo $_REQUEST['page'];?>&render=list" class="nav-tab <?php if($admin_page == '' || $admin_page == 'list' || $admin_page == 'ignore' || $admin_page == 'recheck') {echo 'nav-tab-active';} ?>"><?php _e('Report List', 'gpagespeedi'); ?></a> 
  700. <?php if($admin_page == 'details') { ?> 
  701. <a href="?page=<?php echo $_REQUEST['page'];?>&render=details&page_id=<?php echo $_GET['page_id']; ?>" class="nav-tab nav-tab-active nav-tab-temp"><?php _e('Report Details', 'gpagespeedi'); ?></a> 
  702. <?php } ?> 
  703. <a href="?page=<?php echo $_REQUEST['page'];?>&render=summary" class="nav-tab <?php if($admin_page == 'summary') {echo 'nav-tab-active';} ?>"><?php _e('Report Summary', 'gpagespeedi'); ?></a> 
  704. <a href="?page=<?php echo $_REQUEST['page'];?>&render=ignored-urls" class="nav-tab <?php if($admin_page == 'ignored-urls' || $admin_page == 'activate') {echo 'nav-tab-active';} ?>"><?php _e('Ignored URLs', 'gpagespeedi'); ?></a> 
  705. <a href="?page=<?php echo $_REQUEST['page'];?>&render=options" class="nav-tab <?php if($admin_page == 'options') {echo 'nav-tab-active';} ?>"><?php _e('Options', 'gpagespeedi'); ?></a> 
  706. <a href="?page=<?php echo $_REQUEST['page'];?>&render=about" class="nav-tab <?php if($admin_page == 'about') {echo 'nav-tab-active';} ?>"><?php _e('About', 'gpagespeedi'); ?></a> 
  707. </h3> 
  708.  
  709. <?php if($gpi_options['google_developer_key'] == '' && $admin_page != 'options') { ?> 
  710. <div id="message" class="error"> 
  711. <p><strong><?php _e('You must enter your Google API key to use this plugin! Enter your API key in the', 'gpagespeedi'); ?> <a href="?page=<?php echo $_REQUEST['page'];?>&render=options"><?php _e('Options', 'gpagespeedi'); ?></a></strong>.</p> 
  712. </div> 
  713. <?php } ?> 
  714. <?php if($gpi_options['bad_api_key'] && $admin_page != 'options') { ?> 
  715. <div id="message" class="error"> 
  716. <p><strong><?php _e('The Google Pagespeed API Key you entered appears to be invalid. Please update your API key in the', 'gpagespeedi'); ?> <a href="?page=<?php echo $_REQUEST['page'];?>&render=options"><?php _e('Options', 'gpagespeedi'); ?></a></strong>.</p> 
  717. </div> 
  718. <?php } ?> 
  719. <?php if($gpi_options['pagespeed_disabled'] && $admin_page != 'options') { ?> 
  720. <div id="message" class="error"> 
  721. <p><strong><?php _e('The "PageSpeed Insights API" service is not enabled. To enable it, please visit the "Services" page from your ', 'gpagespeedi'); ?> <a href="https://code.google.com/apis/console/" target="_blank"><?php _e('Google API Console', 'gpagespeedi'); ?></a></strong>.</p> 
  722. </div> 
  723. <?php } ?> 
  724. <?php if($gpi_ui_options['action_message']) { ?> 
  725. <div id="message" class="updated"> 
  726. <p><?php echo $gpi_ui_options['action_message']; ?></p> 
  727. </div> 
  728. <?php } ?> 
  729. <?php if($gpi_options['new_ignored_items']) { ?> 
  730. <div id="message" class="error"> 
  731. <p><strong><?php _e('One or more URLs could not be reached by Google Pagespeed Insights and have automatically been added to the', 'gpagespeedi'); ?> <a href="?page=<?php echo $_REQUEST['page'];?>&render=ignored-urls"><?php _e('Ignored URLs', 'gpagespeedi'); ?></a></strong>.</p> 
  732. </div> 
  733. <?php } ?> 
  734. <?php if($gpi_options['backend_error']) { ?> 
  735. <div id="message" class="error"> 
  736. <p><strong><?php _e('An error has been encountered while checking one or more URLs. Possible causes: <br /><br />Daily API Limit Exceeded <a href="https://code.google.com/apis/console" target="_blank">Check API Usage</a> <br />API Key user limit exceeded <a href="https://code.google.com/apis/console" target="_blank">Check API Usage</a> <br />the URL is not publicly accessible or is bad. <br /><br />The URL(s) have been added to the', 'gpagespeedi'); ?> <a href="?page=<?php echo $_REQUEST['page'];?>&render=ignored-urls"><?php _e('Ignored URLs', 'gpagespeedi'); ?></a></strong></p> 
  737. </div> 
  738. <?php } ?> 
  739. <?php 
  740.  
  741. require_once GPI_DIRECTORY . '/core/core.php'; 
  742. $googlePagespeedInsights = new googlePagespeedInsights($gpi_options); 
  743.  
  744. //Show currently working status on admin pages if GPI is working in the background 
  745. $worker_status = $googlePagespeedInsights->google_pagespeed_insights_Check_Status(); 
  746. if($worker_status) { ?> 
  747. <div id="message" class="updated"> 
  748. <span><p id="gpi_status_finished" style="font-size: 13px; display: none;"><?php _e('Google Pagespeed has finished checking pagespeed scores. <a href="javascript:location.reload(true);">Refresh to see new results.</a>', 'gpagespeedi'); ?></p><p id="gpi_status_ajax" style="font-size: 13px;"><?php _e('Google Pagespeed is running in the background. Progress...', 'gpagespeedi'); ?></p></span> 
  749. </div> 
  750. <?php } 
  751.  
  752. //Clear any one-time messages from above 
  753. $googlePagespeedInsights->google_pagespeed_insights_Update_Options('backend_error', false, 'gpagespeedi_options'); 
  754. $googlePagespeedInsights->google_pagespeed_insights_Update_Options('action_message', false, 'gpagespeedi_ui_options'); 
  755. $googlePagespeedInsights->google_pagespeed_insights_Update_Options('new_ignored_items', false, 'gpagespeedi_options'); 
  756.  
  757. $default_strategy = $gpi_ui_options['view_preference']; 
  758.  
  759. switch($admin_page) 
  760. case 'list': 
  761. require_once(GPI_DIRECTORY . '/includes/admin/list.php'); 
  762. gpi_render_list_page(); 
  763. break; 
  764. case 'ignored-urls': 
  765. require_once(GPI_DIRECTORY . '/includes/admin/ignored-urls.php'); 
  766. gpi_render_ignored_urls_page(); 
  767. break; 
  768. case 'options': 
  769. require_once(GPI_DIRECTORY . '/includes/admin/options.php'); 
  770. gpi_render_options_page(); 
  771. break; 
  772. case 'details': 
  773. $page_id = (isset($_GET['page_id'])) ? $_GET['page_id'] : ''; 
  774. require_once(GPI_DIRECTORY . '/includes/admin/details.php'); 
  775. gpi_render_details_page($default_strategy, $page_id); 
  776. break; 
  777. case 'summary': 
  778. require_once(GPI_DIRECTORY . '/includes/admin/summary.php'); 
  779. gpi_render_summary_page($default_strategy); 
  780. break; 
  781. case 'about': 
  782. require_once(GPI_DIRECTORY . '/includes/admin/about.php'); 
  783. gpi_render_about(); 
  784. break; 
  785. default: 
  786. require_once(GPI_DIRECTORY . '/includes/admin/list.php'); 
  787. gpi_render_list_page(); 
  788. break; 
  789. ?> 
  790. </div> 
  791. <?php 
  792.  
.