Fixed: CodeFactor issues

This commit is contained in:
Bogdan Rapaić 2020-04-17 14:36:36 +02:00 committed by GitHub
parent 758b5ec710
commit 596c878c6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,26 +1,23 @@
<?php <?php
/**
/*
* *
* Credits * Credits
* *
* Plugin-Name: Google Pagespeed Insights * Plugin Name: Google Pagespeed Insights
* Plugin URI: http://mattkeys.me * Plugin URI: http://mattkeys.me
* Author: Matt Keys * Author: Matt Keys
* Author URI: http://mattkeys.me * Author URI: http://mattkeys.me
* *
* The code is used for the MainWP Page Speed Extension * The code is used for the MainWP Page Speed Extension
* Extension URL: https://mainwp.com/extension/page-speed/ * Extension URL: https://mainwp.com/extension/page-speed/
* */
*/
class MainWP_Child_Pagespeed { class MainWP_Child_Pagespeed {
public static $instance = null; public static $instance = null;
public $is_plugin_installed = false; public $is_plugin_installed = false;
static function Instance() { public static function Instance() {
if ( null === self::$instance ) { if ( null === self::$instance ) {
self::$instance = new MainWP_Child_Pagespeed(); self::$instance = new MainWP_Child_Pagespeed();
} }
@ -70,7 +67,8 @@ class MainWP_Child_Pagespeed {
} }
public function child_deactivation() { public function child_deactivation() {
if ( $sched = wp_next_scheduled( 'mainwp_child_pagespeed_cron_check' ) ) { $sched = wp_next_scheduled( 'mainwp_child_pagespeed_cron_check' );
if ( $sched ) {
wp_unschedule_event( $sched, 'mainwp_child_pagespeed_cron_check' ); wp_unschedule_event( $sched, 'mainwp_child_pagespeed_cron_check' );
} }
} }
@ -80,16 +78,17 @@ class MainWP_Child_Pagespeed {
return; return;
} }
if ( get_option( 'mainwp_pagespeed_hide_plugin' ) === 'hide' ) { if ( 'hide' === get_option( 'mainwp_pagespeed_hide_plugin' ) ) {
add_filter( 'all_plugins', array( $this, 'hide_plugin' ) ); add_filter( 'all_plugins', array( $this, 'hide_plugin' ) );
add_action('admin_menu', array( $this, 'hide_menu' ), 999); add_action( 'admin_menu', array( $this, 'hide_menu' ), 999 );
} }
$this->init_cron(); $this->init_cron();
} }
public function init_cron() { public function init_cron() {
add_action( 'mainwp_child_pagespeed_cron_check', array( 'MainWP_Child_Pagespeed', 'pagespeed_cron_check' ) ); add_action( 'mainwp_child_pagespeed_cron_check', array( 'MainWP_Child_Pagespeed', 'pagespeed_cron_check' ) );
if ( false === ( $sched = wp_next_scheduled( 'mainwp_child_pagespeed_cron_check' ) ) ) { $sched = wp_next_scheduled( 'mainwp_child_pagespeed_cron_check' );
if ( false === $sched ) {
wp_schedule_event( time(), 'daily', 'mainwp_child_pagespeed_cron_check' ); wp_schedule_event( time(), 'daily', 'mainwp_child_pagespeed_cron_check' );
} }
} }
@ -127,17 +126,17 @@ class MainWP_Child_Pagespeed {
public function hide_menu() { public function hide_menu() {
global $submenu; global $submenu;
if ( isset($submenu['tools.php']) ) { if ( isset( $submenu['tools.php'] ) ) {
foreach ( $submenu['tools.php'] as $key => $menu ) { foreach ( $submenu['tools.php'] as $key => $menu ) {
if ( $menu[2] == 'google-pagespeed-insights' ) { if ( 'google-pagespeed-insights' == $menu[2] ) {
unset($submenu['tools.php'][ $key ]); unset( $submenu['tools.php'][ $key ] );
break; break;
} }
} }
} }
} }
function update_footer( $text ) { public function update_footer( $text ) {
?> ?>
<script> <script>
jQuery( document ).ready( function () { jQuery( document ).ready( function () {
@ -149,7 +148,7 @@ class MainWP_Child_Pagespeed {
} }
function set_showhide() { public function set_showhide() {
$hide = isset( $_POST['showhide'] ) && ( 'hide' === $_POST['showhide'] ) ? 'hide' : ''; $hide = isset( $_POST['showhide'] ) && ( 'hide' === $_POST['showhide'] ) ? 'hide' : '';
MainWP_Helper::update_option( 'mainwp_pagespeed_hide_plugin', $hide ); MainWP_Helper::update_option( 'mainwp_pagespeed_hide_plugin', $hide );
$information['result'] = 'SUCCESS'; $information['result'] = 'SUCCESS';
@ -157,7 +156,7 @@ class MainWP_Child_Pagespeed {
return $information; return $information;
} }
function save_settings() { public function save_settings() {
$current_values = get_option( 'gpagespeedi_options' ); $current_values = get_option( 'gpagespeedi_options' );
$checkstatus = apply_filters( 'gpi_check_status', false ); $checkstatus = apply_filters( 'gpi_check_status', false );
if ( $checkstatus ) { if ( $checkstatus ) {
@ -167,7 +166,7 @@ class MainWP_Child_Pagespeed {
$information = array(); $information = array();
$settings = $_POST['settings']; $settings = $_POST['settings'];
$settings = maybe_unserialize( base64_decode( $settings ) ); $settings = maybe_unserialize( base64_decode( $settings ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for benign reasons.
if ( is_array( $settings ) ) { if ( is_array( $settings ) ) {
@ -221,7 +220,10 @@ class MainWP_Child_Pagespeed {
$current_values['check_categories'] = in_array( 'category', $settings['check_report'] ) ? true : false; $current_values['check_categories'] = in_array( 'category', $settings['check_report'] ) ? true : false;
$current_values['check_custom_urls'] = in_array( 'custom_urls', $settings['check_report'] ) ? true : false; $current_values['check_custom_urls'] = in_array( 'custom_urls', $settings['check_report'] ) ? true : false;
} else { } else {
$current_values['check_pages'] = $current_values['check_posts'] = $current_values['check_categories'] = $current_values['check_custom_urls'] = false; $current_values['check_pages'] = false;
$current_values['check_posts'] = false;
$current_values['check_categories'] = false;
$current_values['check_custom_urls'] = false;
} }
} }
@ -240,44 +242,31 @@ class MainWP_Child_Pagespeed {
$result = $this->get_sync_data( $strategy ); $result = $this->get_sync_data( $strategy );
// if ( isset( $_POST['doaction'] ) && ( 'check_new_pages' === $_POST['doaction'] || 'recheck_all_pages' === $_POST['doaction'] ) ) {
// if ( 'recheck_all_pages' === $_POST['doaction'] ) {
// $recheck = true;
// } else {
// $recheck = false;
// }
//
//
// if ($this->do_check_pages($recheck))
// $information['checked_pages'] = 1;
// }
$information['data'] = $result['data']; $information['data'] = $result['data'];
return $information; return $information;
} }
public function check_pages() {
function check_pages() { if ( isset( $_POST['force_recheck'] ) && ! empty( $_POST['force_recheck'] ) ) {
if ( isset($_POST['force_recheck']) && ! empty($_POST['force_recheck']) ) {
$recheck = true; $recheck = true;
} else { } else {
$recheck = false; $recheck = false;
} }
$information = $this->do_check_pages($recheck); $information = $this->do_check_pages( $recheck );
if ( isset($information['checked_pages']) && $information['checked_pages'] ) { if ( isset( $information['checked_pages'] ) && $information['checked_pages'] ) {
$information['result'] = 'SUCCESS'; $information['result'] = 'SUCCESS';
} }
return $information; return $information;
} }
function do_check_pages( $forceRecheck = false ) { public function do_check_pages( $forceRecheck = false ) {
$information = array(); $information = array();
if ( defined( 'GPI_DIRECTORY' ) ) { if ( defined( 'GPI_DIRECTORY' ) ) {
$checkstatus = apply_filters( 'gpi_check_status', false ); $checkstatus = apply_filters( 'gpi_check_status', false );
if ( $checkstatus ) { if ( $checkstatus ) {
$information['error'] = __( 'The API is busy checking other pages, please try again later.', 'gpagespeedi' ); $information['error'] = __( 'The API is busy checking other pages, please try again later.', 'gpagespeedi' );
} else { } else {
// do_action( 'googlepagespeedinsightsworker', array(), $forceRecheck ); do_action( 'run_gpi', $forceRecheck );
do_action( 'run_gpi', $forceRecheck ); // to fix
$information['checked_pages'] = 1; $information['checked_pages'] = 1;
} }
} }
@ -289,12 +278,12 @@ class MainWP_Child_Pagespeed {
try { try {
$information['syncPageSpeedData'] = $this->get_sync_data(); $information['syncPageSpeedData'] = $this->get_sync_data();
} catch ( Exception $e ) { } catch ( Exception $e ) {
// ok!
} }
} }
return $information; return $information;
} }
// ok
public function get_sync_data( $strategy = '' ) { public function get_sync_data( $strategy = '' ) {
if ( empty( $strategy ) ) { if ( empty( $strategy ) ) {
$strategy = 'both'; $strategy = 'both';
@ -312,7 +301,7 @@ class MainWP_Child_Pagespeed {
if ( 'both' === $strategy || 'desktop' === $strategy ) { if ( 'both' === $strategy || 'desktop' === $strategy ) {
$result = self::cal_pagespeed_data( 'desktop' ); $result = self::cal_pagespeed_data( 'desktop' );
if ( ! empty($result) && is_array($result) ) { if ( ! empty( $result ) && is_array( $result ) ) {
$data['desktop_score'] = $result['average_score']; $data['desktop_score'] = $result['average_score'];
$data['desktop_total_pages'] = $result['total_pages']; $data['desktop_total_pages'] = $result['total_pages'];
$data['desktop_last_modified'] = $result['last_modified']; $data['desktop_last_modified'] = $result['last_modified'];
@ -320,7 +309,7 @@ class MainWP_Child_Pagespeed {
} }
if ( 'both' === $strategy || 'mobile' === $strategy ) { if ( 'both' === $strategy || 'mobile' === $strategy ) {
$result = self::cal_pagespeed_data( 'mobile' ); $result = self::cal_pagespeed_data( 'mobile' );
if ( ! empty($result) && is_array($result) ) { if ( ! empty( $result ) && is_array( $result ) ) {
$data['mobile_score'] = $result['average_score']; $data['mobile_score'] = $result['average_score'];
$data['mobile_total_pages'] = $result['total_pages']; $data['mobile_total_pages'] = $result['total_pages'];
$data['mobile_last_modified'] = $result['last_modified']; $data['mobile_last_modified'] = $result['last_modified'];
@ -332,7 +321,7 @@ class MainWP_Child_Pagespeed {
return $information; return $information;
} }
static function cal_pagespeed_data( $strategy ) { public static function cal_pagespeed_data( $strategy ) {
global $wpdb; global $wpdb;
if ( ! defined( 'GPI_DIRECTORY' ) ) { if ( ! defined( 'GPI_DIRECTORY' ) ) {
return false; return false;
@ -343,7 +332,6 @@ class MainWP_Child_Pagespeed {
} }
$score_column = $strategy . '_score'; $score_column = $strategy . '_score';
// $page_stats_column = $strategy . '_page_stats';
$data_typestocheck = self::get_filter_options( 'all' ); $data_typestocheck = self::get_filter_options( 'all' );
@ -352,9 +340,7 @@ class MainWP_Child_Pagespeed {
$allpagedata = $wpdb->get_results( $allpagedata = $wpdb->get_results(
$wpdb->prepare( $wpdb->prepare(
"SELECT ID, URL, $score_column "SELECT ID, URL, $score_column FROM $gpi_page_stats WHERE ( $data_typestocheck[0] )",
FROM $gpi_page_stats
WHERE ($data_typestocheck[0])",
$data_typestocheck[1] $data_typestocheck[1]
), ),
ARRAY_A ARRAY_A
@ -370,12 +356,7 @@ class MainWP_Child_Pagespeed {
$allpagereports = $wpdb->get_results( $allpagereports = $wpdb->get_results(
$wpdb->prepare( $wpdb->prepare(
"SELECT r.rule_key, r.rule_name "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] )",
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])",
$reports_typestocheck[1] $reports_typestocheck[1]
), ),
ARRAY_A ARRAY_A
@ -397,34 +378,22 @@ class MainWP_Child_Pagespeed {
$average_score = number_format( $total_scores / $total_pages ); $average_score = number_format( $total_scores / $total_pages );
} }
// Not Null check for Report List scores
switch ( $strategy ) { switch ( $strategy ) {
// case 'both':
// $nullcheck = 'desktop_score IS NOT NULL AND mobile_score IS NOT NULL';
// break;
case 'mobile': case 'mobile':
$nullcheck = 'mobile_score IS NOT NULL'; $nullcheck = 'mobile_score IS NOT NULL';
$_select = ' max(mobile_last_modified) as last_modified '; $_select = ' max(mobile_last_modified) as last_modified ';
break; break;
case 'desktop': case 'desktop':
$nullcheck = 'desktop_score IS NOT NULL'; $nullcheck = 'desktop_score IS NOT NULL';
$_select = ' max(desktop_last_modified) as last_modified '; $_select = ' max(desktop_last_modified) as last_modified ';
break; break;
} }
// Get our Data
if ( ! is_null( $reports_typestocheck ) ) { if ( ! is_null( $reports_typestocheck ) ) {
$gpi_page_stats = $wpdb->prefix . 'gpi_page_stats'; $gpi_page_stats = $wpdb->prefix . 'gpi_page_stats';
$data = $wpdb->get_results( $data = $wpdb->get_results(
$wpdb->prepare( $wpdb->prepare(
"SELECT $_select "SELECT $_select FROM $gpi_page_stats WHERE ( $reports_typestocheck[0] ) AND $nullcheck",
FROM $gpi_page_stats
WHERE ($reports_typestocheck[0])
AND $nullcheck",
$reports_typestocheck[1] $reports_typestocheck[1]
), ),
ARRAY_A ARRAY_A
@ -438,14 +407,14 @@ class MainWP_Child_Pagespeed {
); );
} }
static function get_filter_options( $restrict_type = 'all' ) { public static function get_filter_options( $restrict_type = 'all' ) {
$types = array(); $types = array();
$gpi_options = get_option('gpagespeedi_options'); $gpi_options = get_option( 'gpagespeedi_options' );
$typestocheck = array(); $typestocheck = array();
if ( $gpi_options['check_pages'] ) { if ( $gpi_options['check_pages'] ) {
if ( $restrict_type == 'all' || $restrict_type == 'ignored' || $restrict_type == 'pages' ) { if ( 'all' == $restrict_type || 'ignored' == $restrict_type || 'pages' == $restrict_type ) {
$typestocheck[] = 'type = %s'; $typestocheck[] = 'type = %s';
$types[1][] = 'page'; $types[1][] = 'page';
} }
@ -459,27 +428,27 @@ class MainWP_Child_Pagespeed {
} }
if ( $gpi_options['check_categories'] ) { if ( $gpi_options['check_categories'] ) {
if ( $restrict_type == 'all' || $restrict_type == 'ignored' || $restrict_type == 'categories' ) { if ( 'all' == $restrict_type || 'ignored' == $restrict_type || 'categories' == $restrict_type ) {
$typestocheck[] = 'type = %s'; $typestocheck[] = 'type = %s';
$types[1][] = 'category'; $types[1][] = 'category';
} }
} }
if ( $gpi_options['cpt_whitelist'] ) { if ( $gpi_options['cpt_whitelist'] ) {
if ( $restrict_type == 'all' || $restrict_type == 'ignored' || stristr($restrict_type, 'gpi_custom_posts') ) { if ( 'all' == $restrict_type || 'ignored' == $restrict_type || stristr( $restrict_type, 'gpi_custom_posts' ) ) {
$cpt_whitelist_arr = false; $cpt_whitelist_arr = false;
if ( ! empty($gpi_options['cpt_whitelist']) ) { if ( ! empty( $gpi_options['cpt_whitelist'] ) ) {
$cpt_whitelist_arr = unserialize($gpi_options['cpt_whitelist']); $cpt_whitelist_arr = unserialize( $gpi_options['cpt_whitelist'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- unserialize required.
} }
$args = array( $args = array(
'public' => true, 'public' => true,
'_builtin' => false, '_builtin' => false,
); );
$custom_post_types = get_post_types($args, 'names', 'and'); $custom_post_types = get_post_types( $args, 'names', 'and' );
if ( $restrict_type != 'gpi_custom_posts' && $restrict_type != 'all' && $restrict_type != 'ignored' ) { if ( 'gpi_custom_posts' != $restrict_type && 'all' != $restrict_type && 'ignored' != $restrict_type ) {
$restrict_type = str_replace('gpi_custom_posts-', '', $restrict_type); $restrict_type = str_replace( 'gpi_custom_posts-', '', $restrict_type );
foreach ( $custom_post_types as $post_type ) { foreach ( $custom_post_types as $post_type ) {
if ( $cpt_whitelist_arr && in_array($post_type, $cpt_whitelist_arr) ) { if ( $cpt_whitelist_arr && in_array( $post_type, $cpt_whitelist_arr ) ) {
if ( $post_type == $restrict_type ) { if ( $post_type == $restrict_type ) {
$typestocheck[] = 'type = %s'; $typestocheck[] = 'type = %s';
$types[1][] = $custom_post_types[ $post_type ]; $types[1][] = $custom_post_types[ $post_type ];
@ -488,7 +457,7 @@ class MainWP_Child_Pagespeed {
} }
} else { } else {
foreach ( $custom_post_types as $post_type ) { foreach ( $custom_post_types as $post_type ) {
if ( $cpt_whitelist_arr && in_array($post_type, $cpt_whitelist_arr) ) { if ( $cpt_whitelist_arr && in_array( $post_type, $cpt_whitelist_arr ) ) {
$typestocheck[] = 'type = %s'; $typestocheck[] = 'type = %s';
$types[1][] = $custom_post_types[ $post_type ]; $types[1][] = $custom_post_types[ $post_type ];
} }
@ -516,10 +485,10 @@ class MainWP_Child_Pagespeed {
} }
} }
if ( ! empty($typestocheck) ) { if ( ! empty( $typestocheck ) ) {
$types[0] = ''; $types[0] = '';
foreach ( $typestocheck as $type ) { foreach ( $typestocheck as $type ) {
if ( ! is_array($type) ) { if ( ! is_array( $type ) ) {
$types[0] .= $type . ' OR '; $types[0] .= $type . ' OR ';
} else { } else {
foreach ( $type as $custom_post_type ) { foreach ( $type as $custom_post_type ) {
@ -528,13 +497,13 @@ class MainWP_Child_Pagespeed {
} }
} }
} }
$types[0] = rtrim($types[0], ' OR '); $types[0] = rtrim( $types[0], ' OR ' );
return $types; return $types;
} }
return null; return null;
} }
function delete_data( $what ) { public function delete_data( $what ) {
global $wpdb; global $wpdb;
$gpi_page_stats = $wpdb->prefix . 'gpi_page_stats'; $gpi_page_stats = $wpdb->prefix . 'gpi_page_stats';
$gpi_page_reports = $wpdb->prefix . 'gpi_page_reports'; $gpi_page_reports = $wpdb->prefix . 'gpi_page_reports';
@ -550,4 +519,3 @@ class MainWP_Child_Pagespeed {
} }
} }
} }