2015-10-15 22:52:37 +10:00
< ? php
2020-04-17 14:36:36 +02:00
/**
2019-02-19 23:52:21 +07:00
*
* Credits
*
2020-04-17 14:36:36 +02:00
* Plugin Name : Google Pagespeed Insights
2019-02-19 23:52:21 +07:00
* Plugin URI : http :// mattkeys . me
* Author : Matt Keys
* Author URI : http :// mattkeys . me
*
* The code is used for the MainWP Page Speed Extension
* Extension URL : https :// mainwp . com / extension / page - speed /
2020-04-17 14:36:36 +02:00
*/
2019-02-19 23:52:21 +07:00
2020-05-14 19:43:10 +07:00
// phpcs:disable PSR1.Classes.ClassDeclaration, WordPress.WP.AlternativeFunctions -- root namespace to use external code.
2020-05-05 20:13:38 +07:00
2015-10-15 22:52:37 +10:00
class MainWP_Child_Pagespeed {
2020-03-26 19:45:07 +00:00
public static $instance = null ;
2017-07-11 14:10:22 +02:00
public $is_plugin_installed = false ;
2015-10-15 22:52:37 +10:00
2020-05-06 20:22:11 +07:00
public static function instance () {
2020-03-26 14:05:04 +00:00
if ( null === self :: $instance ) {
2020-05-07 01:03:56 +07:00
self :: $instance = new self ();
2015-10-15 22:52:37 +10:00
}
2020-03-26 14:05:04 +00:00
return self :: $instance ;
2015-10-15 22:52:37 +10:00
}
public function __construct () {
2020-03-26 14:11:33 +00:00
require_once ABSPATH . 'wp-admin/includes/plugin.php' ;
2017-01-14 13:16:52 +01:00
if ( is_plugin_active ( 'google-pagespeed-insights/google-pagespeed-insights.php' ) ) {
2017-07-11 14:10:22 +02:00
$this -> is_plugin_installed = true ;
2017-01-14 13:16:52 +01:00
}
2018-11-14 21:33:36 +07:00
2020-03-27 15:13:11 +00:00
if ( ! $this -> is_plugin_installed ) {
return ;
}
2018-11-14 21:33:36 +07:00
2020-05-07 19:34:36 +07:00
add_filter ( 'mainwp_site_sync_others_data' , array ( $this , 'sync_others_data' ), 10 , 2 );
2018-11-14 21:33:36 +07:00
2015-10-15 22:52:37 +10:00
add_action ( 'mainwp_child_deactivation' , array ( $this , 'child_deactivation' ) );
}
public function action () {
$information = array ();
2017-07-11 14:10:22 +02:00
if ( ! defined ( 'GPI_DIRECTORY' ) ) {
$information [ 'error' ] = 'Please install Google Pagespeed Insights plugin on child website' ;
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( $information );
2015-10-15 22:52:37 +10:00
}
if ( isset ( $_POST [ 'mwp_action' ] ) ) {
2018-11-14 21:33:36 +07:00
2015-10-15 22:52:37 +10:00
switch ( $_POST [ 'mwp_action' ] ) {
case 'save_settings' :
$information = $this -> save_settings ();
break ;
case 'set_showhide' :
$information = $this -> set_showhide ();
break ;
case 'sync_data' :
2018-06-26 19:52:53 +02:00
$information = $this -> get_sync_data ();
2015-10-15 22:52:37 +10:00
break ;
2020-03-26 14:05:04 +00:00
case 'check_pages' :
2015-12-05 17:38:52 +01:00
$information = $this -> check_pages ();
break ;
2015-10-15 22:52:37 +10:00
}
}
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( $information );
2015-10-15 22:52:37 +10:00
}
public function child_deactivation () {
2020-04-17 14:36:36 +02:00
$sched = wp_next_scheduled ( 'mainwp_child_pagespeed_cron_check' );
if ( $sched ) {
2015-10-15 22:52:37 +10:00
wp_unschedule_event ( $sched , 'mainwp_child_pagespeed_cron_check' );
}
}
public function init () {
2020-03-27 15:13:11 +00:00
if ( ! $this -> is_plugin_installed ) {
return ;
}
2019-02-14 22:38:14 +07:00
2020-04-17 14:36:36 +02:00
if ( 'hide' === get_option ( 'mainwp_pagespeed_hide_plugin' ) ) {
2015-10-15 22:52:37 +10:00
add_filter ( 'all_plugins' , array ( $this , 'hide_plugin' ) );
2020-04-17 14:36:36 +02:00
add_action ( 'admin_menu' , array ( $this , 'hide_menu' ), 999 );
2015-10-15 22:52:37 +10:00
}
$this -> init_cron ();
}
public function init_cron () {
2020-05-07 23:22:05 +07:00
add_action ( 'mainwp_child_pagespeed_cron_check' , array ( '\MainWP_Child_Pagespeed' , 'pagespeed_cron_check' ) );
2020-04-17 14:36:36 +02:00
$sched = wp_next_scheduled ( 'mainwp_child_pagespeed_cron_check' );
if ( false === $sched ) {
2015-10-15 22:52:37 +10:00
wp_schedule_event ( time (), 'daily' , 'mainwp_child_pagespeed_cron_check' );
}
}
public static function pagespeed_cron_check () {
$count = get_option ( 'mainwp_child_pagespeed_count_checking' );
if ( $count >= 7 ) {
$recheck = true ;
$count = 0 ;
} else {
$recheck = false ;
$count ++ ;
}
update_option ( 'mainwp_child_pagespeed_count_checking' , $count );
$worker_args = array (
array (),
false ,
$recheck ,
);
wp_schedule_single_event ( time (), 'googlepagespeedinsightschecknow' , $worker_args );
}
public function hide_plugin ( $plugins ) {
foreach ( $plugins as $key => $value ) {
$plugin_slug = basename ( $key , '.php' );
if ( 'google-pagespeed-insights' === $plugin_slug ) {
unset ( $plugins [ $key ] );
}
}
return $plugins ;
}
2017-07-11 14:10:22 +02:00
public function hide_menu () {
global $submenu ;
2020-04-17 14:36:36 +02:00
if ( isset ( $submenu [ 'tools.php' ] ) ) {
2020-03-27 14:11:21 +00:00
foreach ( $submenu [ 'tools.php' ] as $key => $menu ) {
2020-04-17 14:36:36 +02:00
if ( 'google-pagespeed-insights' == $menu [ 2 ] ) {
unset ( $submenu [ 'tools.php' ][ $key ] );
2017-07-11 14:10:22 +02:00
break ;
}
}
}
}
2015-10-15 22:52:37 +10:00
2020-04-17 14:36:36 +02:00
public function update_footer ( $text ) {
2015-10-15 22:52:37 +10:00
?>
< script >
jQuery ( document ) . ready ( function () {
jQuery ( '#menu-tools a[href="tools.php?page=google-pagespeed-insights"]' ) . closest ( 'li' ) . remove ();
} );
</ script >
< ? php
return $text ;
}
2020-04-17 14:36:36 +02:00
public function set_showhide () {
2015-10-15 22:52:37 +10:00
$hide = isset ( $_POST [ 'showhide' ] ) && ( 'hide' === $_POST [ 'showhide' ] ) ? 'hide' : '' ;
MainWP_Helper :: update_option ( 'mainwp_pagespeed_hide_plugin' , $hide );
$information [ 'result' ] = 'SUCCESS' ;
return $information ;
}
2020-05-19 20:08:55 +07:00
public function save_settings () { // phpcs:ignore -- ignore complex method notice.
2015-10-15 22:52:37 +10:00
$current_values = get_option ( 'gpagespeedi_options' );
2020-03-26 19:45:07 +00:00
$checkstatus = apply_filters ( 'gpi_check_status' , false );
2017-07-11 14:10:22 +02:00
if ( $checkstatus ) {
2015-10-15 22:52:37 +10:00
return array ( 'result' => 'RUNNING' );
}
2020-03-27 15:13:11 +00:00
$information = array ();
2019-08-26 23:29:16 +07:00
2015-10-15 22:52:37 +10:00
$settings = $_POST [ 'settings' ];
2020-05-08 16:44:48 +07:00
$settings = maybe_unserialize ( base64_decode ( $settings ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
2015-10-15 22:52:37 +10:00
if ( is_array ( $settings ) ) {
if ( isset ( $settings [ 'api_key' ] ) && ! empty ( $settings [ 'api_key' ] ) ) {
$current_values [ 'google_developer_key' ] = $settings [ 'api_key' ];
}
if ( isset ( $settings [ 'response_language' ] ) ) {
$current_values [ 'response_language' ] = $settings [ 'response_language' ];
}
if ( isset ( $_POST [ 'strategy' ] ) ) {
$current_values [ 'strategy' ] = $_POST [ 'strategy' ];
}
2020-03-27 15:13:11 +00:00
if ( isset ( $settings [ 'store_screenshots' ] ) ) {
2019-02-14 22:38:14 +07:00
$current_values [ 'store_screenshots' ] = $settings [ 'store_screenshots' ];
}
2020-03-27 15:13:11 +00:00
if ( isset ( $settings [ 'use_schedule' ] ) ) {
2019-02-14 22:38:14 +07:00
$current_values [ 'use_schedule' ] = $settings [ 'use_schedule' ];
}
2015-10-15 22:52:37 +10:00
if ( isset ( $settings [ 'max_execution_time' ] ) ) {
$current_values [ 'max_execution_time' ] = $settings [ 'max_execution_time' ];
}
2017-07-11 14:10:22 +02:00
if ( isset ( $settings [ 'max_run_time' ] ) ) {
$current_values [ 'max_run_time' ] = $settings [ 'max_run_time' ];
}
if ( isset ( $settings [ 'heartbeat' ] ) ) {
$current_values [ 'heartbeat' ] = $settings [ 'heartbeat' ];
}
2015-10-15 22:52:37 +10:00
if ( isset ( $settings [ 'delay_time' ] ) ) {
$current_values [ 'sleep_time' ] = $settings [ 'delay_time' ];
}
if ( isset ( $settings [ 'log_exception' ] ) ) {
$current_values [ 'log_api_errors' ] = ( $settings [ 'log_exception' ] ) ? true : false ;
}
if ( isset ( $settings [ 'report_expiration' ] ) ) {
$current_values [ 'recheck_interval' ] = $settings [ 'report_expiration' ];
}
if ( isset ( $settings [ 'check_report' ] ) ) {
if ( is_array ( $settings [ 'check_report' ] ) ) {
2020-03-26 19:45:07 +00:00
$current_values [ 'check_pages' ] = in_array ( 'page' , $settings [ 'check_report' ] ) ? true : false ;
$current_values [ 'check_posts' ] = in_array ( 'post' , $settings [ 'check_report' ] ) ? true : false ;
$current_values [ 'check_categories' ] = in_array ( 'category' , $settings [ 'check_report' ] ) ? true : false ;
2020-03-27 15:13:11 +00:00
$current_values [ 'check_custom_urls' ] = in_array ( 'custom_urls' , $settings [ 'check_report' ] ) ? true : false ;
2015-10-15 22:52:37 +10:00
} else {
2020-04-17 14:36:36 +02:00
$current_values [ 'check_pages' ] = false ;
$current_values [ 'check_posts' ] = false ;
$current_values [ 'check_categories' ] = false ;
$current_values [ 'check_custom_urls' ] = false ;
2015-10-15 22:52:37 +10:00
}
}
if ( isset ( $settings [ 'delete_data' ] ) && ! empty ( $settings [ 'delete_data' ] ) ) {
$this -> delete_data ( $settings [ 'delete_data' ] );
}
if ( update_option ( 'gpagespeedi_options' , $current_values ) ) {
$information [ 'result' ] = 'SUCCESS' ;
} else {
$information [ 'result' ] = 'NOTCHANGE' ;
}
}
$strategy = $current_values [ 'strategy' ];
2018-06-26 19:52:53 +02:00
$result = $this -> get_sync_data ( $strategy );
2015-10-15 22:52:37 +10:00
2015-12-05 17:38:52 +01:00
$information [ 'data' ] = $result [ 'data' ];
return $information ;
}
2020-04-17 14:36:36 +02:00
public function check_pages () {
if ( isset ( $_POST [ 'force_recheck' ] ) && ! empty ( $_POST [ 'force_recheck' ] ) ) {
2015-12-05 17:38:52 +01:00
$recheck = true ;
} else {
$recheck = false ;
}
2020-04-17 14:36:36 +02:00
$information = $this -> do_check_pages ( $recheck );
if ( isset ( $information [ 'checked_pages' ] ) && $information [ 'checked_pages' ] ) {
2017-07-11 14:10:22 +02:00
$information [ 'result' ] = 'SUCCESS' ;
}
2015-12-05 17:38:52 +01:00
return $information ;
}
2020-04-17 14:36:36 +02:00
public function do_check_pages ( $forceRecheck = false ) {
2017-07-11 14:10:22 +02:00
$information = array ();
2015-12-05 17:38:52 +01:00
if ( defined ( 'GPI_DIRECTORY' ) ) {
2017-07-11 14:10:22 +02:00
$checkstatus = apply_filters ( 'gpi_check_status' , false );
if ( $checkstatus ) {
$information [ 'error' ] = __ ( 'The API is busy checking other pages, please try again later.' , 'gpagespeedi' );
} else {
2020-04-17 14:36:36 +02:00
do_action ( 'run_gpi' , $forceRecheck );
2017-07-11 14:10:22 +02:00
$information [ 'checked_pages' ] = 1 ;
2016-04-27 21:54:28 +02:00
}
2015-10-15 22:52:37 +10:00
}
2017-07-11 14:10:22 +02:00
return $information ;
2015-10-15 22:52:37 +10:00
}
2020-05-06 00:47:59 +07:00
public function sync_others_data ( $information , $data = array () ) {
2020-03-27 15:13:11 +00:00
if ( isset ( $data [ 'syncPageSpeedData' ] ) && $data [ 'syncPageSpeedData' ] ) {
try {
$information [ 'syncPageSpeedData' ] = $this -> get_sync_data ();
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2020-04-17 14:36:36 +02:00
// ok!
2020-03-27 15:13:11 +00:00
}
}
2018-06-26 19:52:53 +02:00
return $information ;
}
2020-04-17 14:36:36 +02:00
2018-06-26 19:52:53 +02:00
public function get_sync_data ( $strategy = '' ) {
2015-10-15 22:52:37 +10:00
if ( empty ( $strategy ) ) {
$strategy = 'both' ;
}
2016-04-27 21:54:28 +02:00
$current_values = get_option ( 'gpagespeedi_options' );
2020-03-26 19:45:07 +00:00
$checkstatus = apply_filters ( 'gpi_check_status' , false );
2017-07-11 14:10:22 +02:00
if ( $checkstatus ) {
2015-10-15 22:52:37 +10:00
return array ( 'result' => 'RUNNING' );
}
$information = array ();
$bad_key = ( $current_values [ 'bad_api_key' ] || empty ( $current_values [ 'google_developer_key' ] ) );
$data = array ( 'bad_api_key' => $bad_key );
if ( 'both' === $strategy || 'desktop' === $strategy ) {
2020-03-26 19:45:07 +00:00
$result = self :: cal_pagespeed_data ( 'desktop' );
2020-04-17 14:36:36 +02:00
if ( ! empty ( $result ) && is_array ( $result ) ) {
2020-03-27 15:13:11 +00:00
$data [ 'desktop_score' ] = $result [ 'average_score' ];
$data [ 'desktop_total_pages' ] = $result [ 'total_pages' ];
$data [ 'desktop_last_modified' ] = $result [ 'last_modified' ];
}
2015-10-15 22:52:37 +10:00
}
if ( 'both' === $strategy || 'mobile' === $strategy ) {
2020-03-26 19:45:07 +00:00
$result = self :: cal_pagespeed_data ( 'mobile' );
2020-04-17 14:36:36 +02:00
if ( ! empty ( $result ) && is_array ( $result ) ) {
2020-03-27 15:13:11 +00:00
$data [ 'mobile_score' ] = $result [ 'average_score' ];
$data [ 'mobile_total_pages' ] = $result [ 'total_pages' ];
$data [ 'mobile_last_modified' ] = $result [ 'last_modified' ];
}
2015-10-15 22:52:37 +10:00
}
$information [ 'data' ] = $data ;
return $information ;
}
2020-04-17 14:36:36 +02:00
public static function cal_pagespeed_data ( $strategy ) {
2015-10-15 22:52:37 +10:00
global $wpdb ;
if ( ! defined ( 'GPI_DIRECTORY' ) ) {
2019-02-14 22:38:14 +07:00
return false ;
2015-10-15 22:52:37 +10:00
}
if ( 'desktop' !== $strategy && 'mobile' !== $strategy ) {
2019-02-14 22:38:14 +07:00
return false ;
2015-10-15 22:52:37 +10:00
}
2020-03-26 19:45:07 +00:00
$score_column = $strategy . '_score' ;
2015-10-15 22:52:37 +10:00
2019-05-06 22:42:18 +07:00
$data_typestocheck = self :: get_filter_options ( 'all' );
2015-10-15 22:52:37 +10:00
$gpi_page_stats = $wpdb -> prefix . 'gpi_page_stats' ;
if ( ! empty ( $data_typestocheck ) ) {
$allpagedata = $wpdb -> get_results (
$wpdb -> prepare (
2020-05-08 00:51:43 +07:00
" SELECT ID, URL, $score_column FROM $gpi_page_stats WHERE ( $data_typestocheck[0] ) " , // phpcs:ignore -- safe query.
2015-10-15 22:52:37 +10:00
$data_typestocheck [ 1 ]
),
ARRAY_A
);
} else {
$allpagedata = array ();
}
2019-05-06 22:42:18 +07:00
$reports_typestocheck = self :: get_filter_options ( 'all' );
2015-10-15 22:52:37 +10:00
$gpi_page_reports = $wpdb -> prefix . 'gpi_page_reports' ;
if ( ! empty ( $reports_typestocheck ) ) {
$allpagereports = $wpdb -> get_results (
2020-05-07 17:53:28 +00:00
$wpdb -> prepare (
2020-05-08 00:51:43 +07:00
" SELECT r.rule_key, r.rule_name FROM $gpi_page_stats d INNER JOIN $gpi_page_reports r ON r.page_id = d.ID AND r.strategy = ' $strategy ' WHERE ( $reports_typestocheck[0] ) " , // phpcs:ignore -- safe query.
2015-10-15 22:52:37 +10:00
$reports_typestocheck [ 1 ]
),
ARRAY_A
);
} else {
$allpagereports = array ();
}
$total_pages = count ( $allpagedata );
$total_scores = 0 ;
$average_score = 0 ;
if ( ! empty ( $total_pages ) && ! empty ( $allpagereports ) ) {
foreach ( $allpagedata as $key => $pagedata ) {
$total_scores = $total_scores + $pagedata [ $score_column ];
}
$average_score = number_format ( $total_scores / $total_pages );
}
switch ( $strategy ) {
case 'mobile' :
$nullcheck = 'mobile_score IS NOT NULL' ;
$_select = ' max(mobile_last_modified) as last_modified ' ;
break ;
case 'desktop' :
$nullcheck = 'desktop_score IS NOT NULL' ;
$_select = ' max(desktop_last_modified) as last_modified ' ;
break ;
}
if ( ! is_null ( $reports_typestocheck ) ) {
$gpi_page_stats = $wpdb -> prefix . 'gpi_page_stats' ;
2020-05-08 00:51:43 +07:00
$data = $wpdb -> get_results ( $wpdb -> prepare ( " SELECT $_select FROM $gpi_page_stats WHERE ( $reports_typestocheck[0] ) AND $nullcheck " , $reports_typestocheck [ 1 ] ), ARRAY_A ); // phpcs:ignore -- safe query.
2015-10-15 22:52:37 +10:00
}
return array (
'last_modified' => is_array ( $data [ 0 ] ) && isset ( $data [ 0 ][ 'last_modified' ] ) ? $data [ 0 ][ 'last_modified' ] : 0 ,
'average_score' => $average_score ,
'total_pages' => $total_pages ,
);
}
2020-05-19 20:08:55 +07:00
public static function get_filter_options ( $restrict_type = 'all' ) { // phpcs:ignore -- ignore complex method notice, third party credit.
2017-07-11 14:10:22 +02:00
2020-03-26 19:45:07 +00:00
$types = array ();
2020-04-17 14:36:36 +02:00
$gpi_options = get_option ( 'gpagespeedi_options' );
2017-07-11 14:10:22 +02:00
$typestocheck = array ();
2020-03-27 14:11:21 +00:00
if ( $gpi_options [ 'check_pages' ] ) {
2020-04-17 14:36:36 +02:00
if ( 'all' == $restrict_type || 'ignored' == $restrict_type || 'pages' == $restrict_type ) {
2017-07-11 14:10:22 +02:00
$typestocheck [] = 'type = %s' ;
2020-03-26 19:45:07 +00:00
$types [ 1 ][] = 'page' ;
2017-07-11 14:10:22 +02:00
}
}
2020-03-27 14:11:21 +00:00
if ( $gpi_options [ 'check_posts' ] ) {
2020-04-17 14:38:10 +02:00
if ( 'all' == $restrict_type || 'ignored' == $restrict_type || 'posts' == $restrict_type ) {
2017-07-11 14:10:22 +02:00
$typestocheck [] = 'type = %s' ;
2020-03-26 19:45:07 +00:00
$types [ 1 ][] = 'post' ;
2017-07-11 14:10:22 +02:00
}
}
2020-03-27 14:11:21 +00:00
if ( $gpi_options [ 'check_categories' ] ) {
2020-04-17 14:36:36 +02:00
if ( 'all' == $restrict_type || 'ignored' == $restrict_type || 'categories' == $restrict_type ) {
2017-07-11 14:10:22 +02:00
$typestocheck [] = 'type = %s' ;
2020-03-26 19:45:07 +00:00
$types [ 1 ][] = 'category' ;
2017-07-11 14:10:22 +02:00
}
}
2020-03-27 14:11:21 +00:00
if ( $gpi_options [ 'cpt_whitelist' ] ) {
2020-04-17 14:36:36 +02:00
if ( 'all' == $restrict_type || 'ignored' == $restrict_type || stristr ( $restrict_type , 'gpi_custom_posts' ) ) {
2017-07-11 14:10:22 +02:00
$cpt_whitelist_arr = false ;
2020-04-17 14:36:36 +02:00
if ( ! empty ( $gpi_options [ 'cpt_whitelist' ] ) ) {
$cpt_whitelist_arr = unserialize ( $gpi_options [ 'cpt_whitelist' ] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- unserialize required.
2017-07-11 14:10:22 +02:00
}
2020-03-26 19:45:07 +00:00
$args = array (
2017-07-11 14:10:22 +02:00
'public' => true ,
2020-03-26 14:05:04 +00:00
'_builtin' => false ,
2017-07-11 14:10:22 +02:00
);
2020-04-17 14:36:36 +02:00
$custom_post_types = get_post_types ( $args , 'names' , 'and' );
if ( 'gpi_custom_posts' != $restrict_type && 'all' != $restrict_type && 'ignored' != $restrict_type ) {
$restrict_type = str_replace ( 'gpi_custom_posts-' , '' , $restrict_type );
2020-03-27 14:11:21 +00:00
foreach ( $custom_post_types as $post_type ) {
2020-04-17 14:36:36 +02:00
if ( $cpt_whitelist_arr && in_array ( $post_type , $cpt_whitelist_arr ) ) {
2020-03-27 14:11:21 +00:00
if ( $post_type == $restrict_type ) {
2017-07-11 14:10:22 +02:00
$typestocheck [] = 'type = %s' ;
2020-03-26 19:45:07 +00:00
$types [ 1 ][] = $custom_post_types [ $post_type ];
2017-07-11 14:10:22 +02:00
}
}
}
} else {
2020-03-27 14:11:21 +00:00
foreach ( $custom_post_types as $post_type ) {
2020-04-17 14:36:36 +02:00
if ( $cpt_whitelist_arr && in_array ( $post_type , $cpt_whitelist_arr ) ) {
2017-07-11 14:10:22 +02:00
$typestocheck [] = 'type = %s' ;
2020-03-26 19:45:07 +00:00
$types [ 1 ][] = $custom_post_types [ $post_type ];
2017-07-11 14:10:22 +02:00
}
}
}
}
}
2020-03-27 15:13:11 +00:00
if ( $gpi_options [ 'check_custom_urls' ] ) {
2019-05-06 22:42:18 +07:00
global $wpdb ;
2020-05-07 17:53:28 +00:00
$custom_url_types = $wpdb -> get_col ( 'SELECT DISTINCT type FROM ' . $wpdb -> prefix . 'gpi_custom_urls ' );
2019-05-06 22:42:18 +07:00
if ( ! empty ( $custom_url_types ) ) {
foreach ( $custom_url_types as $custom_url_type ) {
2020-03-27 15:13:11 +00:00
$typestocheck [] = 'type = %s' ;
$types [ 1 ][] = $custom_url_type ;
2019-05-06 22:42:18 +07:00
}
}
}
2020-04-17 14:36:36 +02:00
if ( ! empty ( $typestocheck ) ) {
2017-07-11 14:10:22 +02:00
$types [ 0 ] = '' ;
2020-03-27 14:11:21 +00:00
foreach ( $typestocheck as $type ) {
2020-04-17 14:36:36 +02:00
if ( ! is_array ( $type ) ) {
2017-07-11 14:10:22 +02:00
$types [ 0 ] .= $type . ' OR ' ;
} else {
2020-03-27 14:11:21 +00:00
foreach ( $type as $custom_post_type ) {
2020-03-26 19:45:07 +00:00
$types [ 0 ] .= 'type = %s OR ' ;
2017-07-11 14:10:22 +02:00
$types [ 1 ][] = $custom_post_type ;
}
}
}
2020-04-17 14:36:36 +02:00
$types [ 0 ] = rtrim ( $types [ 0 ], ' OR ' );
2017-07-11 14:10:22 +02:00
return $types ;
}
return null ;
}
2020-04-17 14:36:36 +02:00
public function delete_data ( $what ) {
2015-10-15 22:52:37 +10:00
global $wpdb ;
$gpi_page_stats = $wpdb -> prefix . 'gpi_page_stats' ;
$gpi_page_reports = $wpdb -> prefix . 'gpi_page_reports' ;
$gpi_page_blacklist = $wpdb -> prefix . 'gpi_page_blacklist' ;
2020-05-08 00:51:43 +07:00
// phpcs:disable -- safe queries.
2015-10-15 22:52:37 +10:00
if ( 'purge_reports' === $what ) {
$wpdb -> query ( " TRUNCATE TABLE $gpi_page_stats " );
$wpdb -> query ( " TRUNCATE TABLE $gpi_page_reports " );
} elseif ( 'purge_everything' === $what ) {
$wpdb -> query ( " TRUNCATE TABLE $gpi_page_stats " );
$wpdb -> query ( " TRUNCATE TABLE $gpi_page_reports " );
$wpdb -> query ( " TRUNCATE TABLE $gpi_page_blacklist " );
}
}
}