403Webshell
Server IP : 193.146.211.90  /  Your IP : 10.85.6.55
Web Server : Apache
System : Linux wordpress-gti-69dcdb54f7-n4wdq 6.11.8-arch1-2 #1 SMP PREEMPT_DYNAMIC Fri, 15 Nov 2024 15:35:07 +0000 x86_64
User : 1001 ( 1001)
PHP Version : 8.2.25
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : ON  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /bitnami/wordpress/wp-content/plugins/google-pagespeed-insights/classes/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /bitnami/wordpress/wp-content/plugins/google-pagespeed-insights/classes/class-GPI-Admin.php
<?php
/**
 * =======================================
 * Google Pagespeed Insights Admin
 * =======================================
 * 
 * 
 * @author Matt Keys <https://profiles.wordpress.org/mattkeys>
 */

if ( ! defined( 'GPI_PLUGIN_FILE' ) ) {
	die();
}

class GPI_Admin
{
	var $gpi_options;
	var $gpi_ui_options;
	var $strategy;
	var $gpi_management_page;

	public function init()
	{
		$this->gpi_options		= get_option( 'gpagespeedi_options' );
		$this->gpi_ui_options	= get_option( 'gpagespeedi_ui_options' );
		$this->strategy			= ( isset( $_GET['strategy'] ) ) ? sanitize_text_field( $_GET['strategy'] ) : sanitize_text_field( $this->gpi_ui_options['view_preference'] );

		if ( 'desktop' != $this->strategy && 'mobile' != $this->strategy ) {
			$this->strategy = 'desktop';
		}

		add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 10, 2 );
		add_action( 'admin_init', array( $this, 'upgrade_check' ), 10 );
		add_action( 'pre_uninstall_plugin', array( $this, 'backup_addon_tables' ), 10, 1 );
		add_action( 'deleted_plugin', array( $this, 'restore_addon_tables' ), 10, 2 );
		add_action( 'admin_menu', array( $this, 'google_pageinsights_menu' ), 10 );
		add_action( 'admin_notices', array( $this, 'admin_notice' ) );
		add_action( 'plugins_loaded', array( $this, 'register_languages_dir' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'load_GPI_style' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'details_scripts' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'summary_scripts' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'snapshot_scripts' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'global_scripts' ) );
		add_action( 'admin_footer', array( $this, 'js_templates' ) );
		add_action( 'admin_init', array( $this, 'redirect' ), 9 );
		add_filter( 'heartbeat_settings', array( $this, 'heartbeat_interval' ), 10, 1 );
		add_filter( 'wpe_heartbeat_allowed_pages', array( $this, 'wpe_heartbeat' ), 10, 1 );
		add_filter( 'heartbeat_received', array( $this, 'progress_heartbeat' ), 10, 2 );
		add_filter( 'gpi_set_time_limit_disabled', array( $this, 'check_set_time_limit' ), 10, 1 );
		add_filter( 'gpi_error_logs', array( $this, 'get_error_logs' ), 10, 1 );
		add_filter( 'gpi_filter_options', array( $this, 'get_filter_options' ), 10, 2 );
		add_filter( 'gpi_custom_post_types', array( $this, 'get_custom_post_types' ), 10, 1 );
		add_filter( 'gpi_custom_urls_count', array( $this, 'get_custom_urls_count' ), 10, 1 );
		add_filter( 'gpi_custom_url_labels', array( $this, 'get_custom_url_labels' ), 10, 1 );
		add_filter( 'gpi_summary_stats', array( $this, 'get_summary_stats' ), 10, 1 );
		add_filter( 'gpi_summary_reports', array( $this, 'get_summary_reports' ), 10, 1 );
		add_filter( 'gpi_similar_snapshots', array( $this, 'get_similar_snapshots' ), 10, 2 );

		load_plugin_textdomain( 'gpagespeedi', false, 'google-pagespeed-insights/translations' );
	}

	public function add_settings_link( $links, $file )
	{
		if ( $file != GPI_BASENAME ) {
			return $links;
		}

		array_unshift( $links, '<a href="' . esc_url( admin_url( '/tools.php?page=google-pagespeed-insights&render=options' ) ) . '">' . esc_html__( 'Settings', 'acf-font-awesome' ) . '</a>' );

		return $links;
	}

	public function upgrade_check()
	{
		if ( ! isset( $this->gpi_options['version'] ) || version_compare( $this->gpi_options['version'], GPI_VERSION, '<' ) ) {
			GPI_Activation::upgrade( $this->gpi_options, $this->gpi_ui_options );
		}

		if ( defined( 'GPIA_PLUGIN_FILE' ) ) {
			deactivate_plugins( GPIA_PLUGIN_FILE );

			add_action( 'admin_notices', array( $this, 'notify_addon_deactivate' ) );
		}

		do_action( 'gpi_addon_upgrade_check' );
	}

	public function backup_addon_tables( $plugin_file )
	{
		if ( 'google-pagespeed-insights-addon/google-pagespeed-insights-addon.php' != $plugin_file ) {
			return;
		}

		global $wpdb;

		$gpi_summary_snapshots			= $wpdb->prefix . 'gpi_summary_snapshots';
		$gpi_custom_urls				= $wpdb->prefix . 'gpi_custom_urls';
		$gpi_summary_snapshots_backup	= $wpdb->prefix . 'gpi_summary_snapshots_backup';
		$gpi_custom_urls_backup			= $wpdb->prefix . 'gpi_custom_urls_backup';

		// Rename current tables
		$wpdb->query( "RENAME TABLE $gpi_summary_snapshots TO $gpi_summary_snapshots_backup" );
		$wpdb->query( "RENAME TABLE $gpi_custom_urls TO $gpi_custom_urls_backup" );

		// Create new blank tables so uninstall hook doesn't produce errors
		$wpdb->query( "CREATE TABLE $gpi_summary_snapshots (id INT(1) )" );
		$wpdb->query( "CREATE TABLE $gpi_custom_urls (id INT(1) )" );
	}

	public function restore_addon_tables( $plugin_file, $deleted )
	{
		if ( 'google-pagespeed-insights-addon/google-pagespeed-insights-addon.php' != $plugin_file ) {
			return;
		}

		global $wpdb;

		$gpi_summary_snapshots			= $wpdb->prefix . 'gpi_summary_snapshots';
		$gpi_custom_urls				= $wpdb->prefix . 'gpi_custom_urls';
		$gpi_summary_snapshots_backup	= $wpdb->prefix . 'gpi_summary_snapshots_backup';
		$gpi_custom_urls_backup			= $wpdb->prefix . 'gpi_custom_urls_backup';

		// Rename backed up tables to restore
		$wpdb->query( "RENAME TABLE $gpi_summary_snapshots_backup TO $gpi_summary_snapshots" );
		$wpdb->query( "RENAME TABLE $gpi_custom_urls_backup TO $gpi_custom_urls" );
	}

	public function notify_addon_deactivate()
	{
		?>
		<div class="notice notice-error is-dismissible">
			<p><?php esc_html_e( 'The plugin "Google Pagespeed Insights Addon" has automatically been deactivated. As of v3.0 Insights from Google PageSpeed now includes all "addon" functionality for free. The "Google Pagespeed Insights Addon" can be uninstalled from the plugins page.', 'gpagespeedi' ); ?></p>
		</div>
		<?php
	}

	public function google_pageinsights_menu()
	{
		$this->gpi_management_page = add_management_page( 'Insights from Google PageSpeed', 'Pagespeed Insights', 'manage_options', 'google-pagespeed-insights', array( $this, 'render_admin_page' ) );
	}

	public function render_admin_page()
	{
		$admin_page = ( isset( $_GET['render'] ) ) ? esc_attr( $_GET['render'] ) : 'report-list';
		?>
		<div class="wrap">
			<h2>
				<?php esc_html_e( 'Insights from Google PageSpeed', 'gpagespeedi' ); ?>
				<div class="global-actions">
					<?php
						if ( $worker_status = apply_filters( 'gpi_check_status', false ) ) :
							if ( ! get_option( 'gpi_abort_scan' ) ) :
								if ( isset( $_GET['page_id'] ) ) :
									?>
									<a href="<?php echo wp_nonce_url( '?page=google-pagespeed-insights&amp;render=' . $admin_page . '&amp;page_id=' . intval( $_GET['page_id'] ) . '&amp;action=abort-scan', 'gpi-abort-scan' ); ?>" class="button-gpi abort"><?php esc_html_e( 'Abort Current Scan', 'gpagespeedi' ); ?></a>
									<?php
								else :
									?>
									<a href="<?php echo wp_nonce_url( '?page=google-pagespeed-insights&amp;render=' . $admin_page . '&amp;action=abort-scan', 'gpi-abort-scan' ); ?>" class="button-gpi abort"><?php esc_html_e( 'Abort Current Scan', 'gpagespeedi' ); ?></a>
									<?php
								endif;
							else :
								?>
								<a href="<?php echo esc_url( '?page=google-pagespeed-insights&amp;render=' . $admin_page ); ?>" class="button-gpi abort" disabled><?php esc_html_e( 'Abort Current Scan', 'gpagespeedi' ); ?></a>
								<?php
							endif;
						elseif ( $this->gpi_options['google_developer_key'] ) :
							if ( isset( $_GET['page_id'] ) ) :
								?>
								<a id="start_scan" href="<?php echo wp_nonce_url( '?page=google-pagespeed-insights&amp;render=' . $admin_page . '&amp;page_id=' . intval( $_GET['page_id'] ) . '&amp;action=start-scan', 'gpi-start-scan' ); ?>" class="button-gpi run"><?php esc_html_e( 'Start Reporting', 'gpagespeedi' ); ?></a>
								<?php
							else :
								?>
								<a id="start_scan" href="<?php echo wp_nonce_url( '?page=google-pagespeed-insights&amp;render=' . $admin_page . '&amp;action=start-scan', 'gpi-start-scan' ); ?>" class="button-gpi run"><?php esc_html_e( 'Start Reporting', 'gpagespeedi' ); ?></a>
								<?php
							endif;
							?>
							<input type="checkbox" name="recheck_all_pages" id="recheck_all_pages" />
							<label for="recheck_all_pages"><?php esc_html_e( 'Recheck All', 'gpagespeedi' ); ?> <span class="tooltip" title="<?php esc_html_e( 'Ignore last checked date to generate new reports for all pages', 'gpagespeedi' ); ?>">(?)</span></label>
							<?php
						endif;
					?>
				</div>
			</h2>

			<?php include GPI_DIRECTORY . '/templates/parts/modes.php'; ?>
			<?php include GPI_DIRECTORY . '/templates/parts/messages.php'; ?>
			<?php include GPI_DIRECTORY . '/templates/parts/navigation.php'; ?>
			<?php
				$template_directory = apply_filters( 'gpi_template_directory', GPI_DIRECTORY, $admin_page );

				if ( in_array( $admin_page, [ 'add-custom-urls-bulk', 'add-custom-urls', 'custom-urls', 'details', 'ignored-urls', 'logs', 'options', 'report-list', 'snapshots', 'summary', 'view-snapshot' ] ) ) {
					include $template_directory . '/templates/' . $admin_page . '.php';
				}
			?>
		</div>
		<?php
	}

	public function admin_notice()
	{
		if ( ! (bool) $this->gpi_options['new_activation_message'] ) {
			return;
		}
		?>
		<div id="message" class="updated">
			<p>
				<?php esc_html_e( 'Insights from Google PageSpeed has been activated. It can be accessed via Tools', 'gpagespeedi' ); ?> -> <a href="<?php echo esc_url( admin_url('/tools.php?page=google-pagespeed-insights&render=options') ); ?>">Pagespeed Insights</a>
			</p>
		</div>
		<?php
		do_action( 'gpi_update_option', 'new_activation_message', false, 'gpagespeedi_options' );
	}

	public function register_languages_dir()
	{
		$lang_dir = dirname( plugin_basename( GPI_PLUGIN_FILE ) ) . '/languages';

		load_plugin_textdomain( 'gpagespeedi', false, $lang_dir );
	}

	public function load_GPI_style( $hook )
	{
		if ( $hook != $this->gpi_management_page ) {
			return;
		}

		wp_enqueue_style( 'gpagespeedi_css', plugins_url( '/assets/css/gpagespeedi_styles.css', GPI_PLUGIN_FILE ), false, esc_attr( GPI_VERSION ) );

		wp_register_script( 'gpagespeedi_google_charts', 'https://www.gstatic.com/charts/loader.js' );
	}

	public function details_scripts( $hook )
	{
		if ( $hook != $this->gpi_management_page ) {
			return;
		}

		if ( ! isset( $_GET['render'] ) || 'details' != $_GET['render'] ) {
			return;
		}

		$recheck_url = admin_url( '/tools.php?page=google-pagespeed-insights&render=details&page_id=' . intval( $_GET['page_id'] ) . '&action=single-recheck' );

		wp_enqueue_script( 'gpagespeedi_details_js', plugins_url( '/assets/js/details.js', GPI_PLUGIN_FILE ), array( 'jquery', 'jquery-ui-accordion', 'gpagespeedi_google_charts', 'wp-util' ), esc_attr( GPI_VERSION ), true );
		wp_localize_script( 'gpagespeedi_details_js', 'GPI_Details', array(
				'page_stats'	=> $this->get_page_stats( intval( $_GET['page_id'] ) ),
				'page_reports'	=> $this->get_page_reports( intval( $_GET['page_id'] ) ),
				'recheck_url'	=> wp_nonce_url( $recheck_url, 'gpi-single-recheck' ),
				'public_path'	=> esc_url( GPI_PUBLIC_PATH ),
				'strings'		=> array(
					'old_format_notice'			=> sprintf( esc_html__( 'Insights from Google PageSpeed has detected an outdated format in this report due to an update in version %s of this plugin. Some report features are unavailable. Please recheck results to resolve this problem.', 'gpagespeedi' ), '4.0' ),
					'insufficient_field_data'	=> wp_kses_post( 'The Chrome User Experience Report <a href="https://developers.google.com/speed/docs/insights/about#faq" target="_blank">does not have sufficient real-world speed data</a> for this page.', 'gpagespeedi' ),
					'FCP'						=> esc_html__( 'First Contentful Paint', 'gpagespeedi' ),
					'FID'						=> esc_html__( 'First Input Delay', 'gpagespeedi' ),
					'field_data_labels'			=> array(
						esc_html__( 'of loads for this page have a fast', 'gpagespeedi' ),
						esc_html__( 'of loads for this page have an average', 'gpagespeedi' ),
						esc_html__( 'of loads for this page have a slow', 'gpagespeedi' )
					),
					'shortpixel'				=> array(
						'title'			=> esc_html__( 'Auto-Optimize Images', 'gpagespeedi' ),
						'description'	=> wp_kses_data( 'Unoptimized images are often one of the <strong>biggest</strong> negative factors in pagespeed scores. Insights from Google PageSpeed has partnered with ShortPixel to provide an easy and affordable solution to <em>automatically</em> optimize all images.', 'gpagespeedi' ),
						'signup_desc'	=> wp_kses_data( 'Sign up using the button below and receive <strong>150 free image optimization credits</strong>.', 'gpagespeedi' ),
						'signup_btn'	=> esc_html__( 'Free Sign Up', 'gpagespeedi' )
					)
				)
			)
		);
	}

	public function summary_scripts( $hook )
	{
		if ( $hook != $this->gpi_management_page ) {
			return;
		}

		if ( ! isset( $_GET['render'] ) || 'summary' != $_GET['render'] ) {
			return;
		}

		wp_enqueue_script( 'gpagespeedi_summary_js', plugins_url( '/assets/js/summary.js', GPI_PLUGIN_FILE ), array( 'jquery', 'gpagespeedi_google_charts', 'wp-util' ), esc_attr( GPI_VERSION ) );
		wp_localize_script( 'gpagespeedi_summary_js', 'GPI_Summary', array(
				'summary_stats'		=> $this->get_summary_stats(),
				'summary_reports'	=> $this->get_summary_reports(),
				'strings'			=> array(
					'average_score'		=> esc_html__( 'Average Score', 'gpagespeedi' ),
					'best_performing'	=> esc_html__( 'View Best Performing', 'gpagespeedi' ),
					'worst_performing'	=> esc_html__( 'View Worst Performing', 'gpagespeedi' ),
					'old_format_notice'	=> esc_html__( 'Insights from Google PageSpeed has detected an outdated format in one or more reports due to an update in version 2.0 of this plugin. Some report features are unavailable. Please force recheck all reports from the options page to resolve this problem.', 'gpagespeedi' )
				)
			)
		);
	}

	public function snapshot_scripts( $hook )
	{
		if ( $hook != 'tools_page_google-pagespeed-insights' ) {
			return;
		}

		if ( ! isset( $_GET['render'] ) || 'view-snapshot' != $_GET['render'] ) {
			return;
		}

		$snapshot_data = $this->get_snapshot_data();
		$strings = array(
			'strings' => array(
				'comment'			=> esc_html__( 'Comment', 'gpagespeedi' ),
				'average_score'		=> esc_html__( 'Average Score', 'gpagespeedi' ),
				'best_performing'	=> esc_html__( 'View Best Performing', 'gpagespeedi' ),
				'worst_performing'	=> esc_html__( 'View Worst Performing', 'gpagespeedi' )
			),
			'comments'	=> array(
				'snapshot'	=> isset( $snapshot_data['snapshot']['comment'] ) ? esc_html( $snapshot_data['snapshot']['comment'] ) : false,
				'compare'	=> isset( $snapshot_data['compare']['comment'] ) ? esc_html( $snapshot_data['compare']['comment'] ) : false
			)
		);

		$localize_data = array_merge( $snapshot_data, $strings );

		wp_enqueue_script( 'gpagespeedi_view_snapshot_js', plugins_url( '/assets/js/view-snapshot.js', GPI_PLUGIN_FILE ), array( 'jquery', 'gpagespeedi_google_charts', 'wp-util' ), esc_attr( GPI_VERSION ) );
		wp_localize_script( 'gpagespeedi_view_snapshot_js', 'GPI_View_Snapshot', $localize_data );
	}

	public function global_scripts( $hook )
	{
		if ( $hook != $this->gpi_management_page ) {
			return;
		}

		wp_enqueue_script( 'gpagespeedi_global_js', plugins_url( '/assets/js/global.js', GPI_PLUGIN_FILE ), array( 'jquery', 'heartbeat', 'jquery-ui-tooltip' ), esc_attr( GPI_VERSION ) );
		wp_localize_script( 'gpagespeedi_global_js', 'GPI_Global', array(
				'heartbeat' => esc_attr( $this->gpi_options['heartbeat'] ),
				'progress'	=> esc_html( get_option( 'gpi_progress' ) )
			)
		);
	}

	public function js_templates()
	{
		if ( ! isset( $_GET['page'] ) || 'google-pagespeed-insights' != $_GET['page'] ) {
			return;
		}

		if ( ! isset( $_GET['render'] ) ) {
			return;
		}

		$render = sanitize_text_field( $_GET['render'] );

		switch ( $render ) {
			case 'details':
				include_once GPI_DIRECTORY . '/assets/js/templates/details/statistics.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/details/audits-opportunity.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/details/audits-diagnostic.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/details/audits-table.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/details/audits-criticalrequestchain.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/details/audits-filmstrip.php';
				break;

			case 'summary':
				include_once GPI_DIRECTORY . '/assets/js/templates/summary/areas_of_improvement.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/summary/scores.php';
				break;

			case 'view-snapshot':
				include_once GPI_DIRECTORY . '/assets/js/templates/summary/areas_of_improvement.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/summary/scores.php';
				include_once GPI_DIRECTORY . '/assets/js/templates/view-snapshot/comment.php';
				break;

			case apply_filters( 'gpi_custom_js_templates', $render ):
				do_action( 'gpi_load_custom_js_template', $render );
				break;
		}
	}

	public function redirect()
	{
		if ( ! isset( $_GET['page'] ) || 'google-pagespeed-insights' != $_GET['page'] ) {
			return;
		}

		if ( ! isset( $_GET['render'] ) ) {
			if ( empty( $this->gpi_options['google_developer_key'] ) ) {
				wp_redirect( '?page=google-pagespeed-insights&render=options' );
			} else {
				wp_redirect( '?page=google-pagespeed-insights&render=report-list' );
			}
			exit;
		}
	}

	public function heartbeat_interval( $settings )
	{
		if ( ! isset( $_GET['page'] ) || 'google-pagespeed-insights' != $_GET['page'] ) {
			return $settings;
		}

		switch ( $this->gpi_options['heartbeat'] ) {
			case 'slow':
				$settings['interval'] = 60;
				break;

			case 'disabled':
				break;

			default:
				$settings['interval'] = 15;
				break;
		}
		
		return $settings;
	}

	public function wpe_heartbeat( $allowed_pages )
	{
		if ( ! isset( $_GET['page'] ) || 'google-pagespeed-insights' != $_GET['page'] ) {
			return $allowed_pages;
		}

		$allowed_pages[] = 'tools.php';

		return $allowed_pages;
	}

	public function get_filter_options( $options, $flatlist = false )
	{
		if ( ! $flatlist ) {
			$options['all'] = esc_html__( 'All Reports', 'gpagespeedi' );
		}

		if ( (bool) $this->gpi_options['check_pages'] ) {
			$options['page'] = esc_html__( 'Pages', 'gpagespeedi' );
		}

		if ( (bool) $this->gpi_options['check_posts'] ) {
			$options['post'] = esc_html__( 'Posts', 'gpagespeedi' );
		}

		if ( (bool) $this->gpi_options['check_categories'] ) {
			$options['category'] = esc_html__( 'Categories', 'gpagespeedi' );
		}

		if ( ! empty( $this->gpi_options['cpt_whitelist'] ) ) {
			if ( ! $flatlist ) {
				$cpt_options = array( 'gpi_custom_posts' => esc_html__( 'All Custom Post Types', 'gpagespeedi' ) );
			}

			$cpt_whitelist = maybe_unserialize( $this->gpi_options['cpt_whitelist'] );
			foreach ( $cpt_whitelist as $cpt ) {
				$cpt_options[ $cpt ] = $cpt;
			}

			if ( ! $flatlist ) {
				$options['custom_post_types'] = array(
					'optgroup_label'	=> esc_html__( 'Custom Post Types', 'gpagespeedi' ),
					'options'			=> $cpt_options
				);
			} else {
				$options = array_merge( $options, $cpt_options );
			}
		}

		if ( (bool) $this->gpi_options['check_custom_urls'] ) {
			global $wpdb;

			$gpi_custom_urls = $wpdb->prefix . 'gpi_custom_urls';
			$custom_url_types = $wpdb->get_col(
				"
					SELECT DISTINCT type
					FROM $gpi_custom_urls
				"
			);

			if ( ! empty( $custom_url_types ) ) {
				if ( ! $flatlist ) {
					$custom_url_options = array( 'gpi_custom_urls' => esc_html__( 'All Custom URLs', 'gpagespeedi' ) );
				}

				foreach ( $custom_url_types as $custom_url_type ) {
					$custom_url_options[ $custom_url_type ] = $custom_url_type;
				}

				if ( ! $flatlist ) {
					$options['custom_urls'] = array(
						'optgroup_label'	=> esc_html__( 'Custom URLs', 'gpagespeedi' ),
						'options'			=> $custom_url_options
					);
				} else {
					if ( ! empty( $custom_url_options ) ) {
						$options = array_merge( $options, $custom_url_options );
					}
				}
			}
		}

		$options = apply_filters( 'gpi_custom_filter_options', $options, $flatlist, $this->gpi_options );

		if ( ! $flatlist ) {
			return $options;
		} else {
			return array_keys( $options );
		}
	}

	public function get_custom_post_types( $cpt )
	{
		$custom_post_types = get_post_types( array(
			'public' => true,
			'_builtin' => false
		) );

		$cpt_whitelist = maybe_unserialize( $this->gpi_options['cpt_whitelist'] );
		$cpt_whitelist = is_array( $cpt_whitelist ) ? $cpt_whitelist : array();

		if ( $custom_post_types ) {
			foreach ( $custom_post_types as $custom_post_type ) {
				$cpt[] = array(
					'value'		=> $custom_post_type,
					'count'		=> wp_count_posts( $custom_post_type )->publish,
					'checked'	=> in_array( $custom_post_type, $cpt_whitelist )
				);
			}
		}

		return $cpt;
	}

	public function get_custom_urls_count( $url_count )
	{
		global $wpdb;

		$gpi_custom_urls = $wpdb->prefix. 'gpi_custom_urls';
		
		return $wpdb->get_var(
			"
				SELECT COUNT( ID )
				FROM $gpi_custom_urls
			"
		);
	}

	public function get_custom_url_labels( $custom_url_labels = false )
	{
		global $wpdb;

		$gpi_custom_urls = $wpdb->prefix . 'gpi_custom_urls';

		$custom_url_labels = $wpdb->get_col(
			"
				SELECT DISTINCT type
				FROM $gpi_custom_urls
			"
		);

		if ( ! empty( $custom_url_labels ) ) {
			$custom_url_labels = implode( '|', $custom_url_labels );
		}

		return $custom_url_labels;
	}

	public function get_summary_stats( $summary_stats = array() )
	{
		global $wpdb;

		$all_types = $this->get_filter_options( array(), true );

		$gpi_page_stats = $wpdb->prefix . 'gpi_page_stats';
		$filter			= isset( $_GET['filter'] ) ? sanitize_text_field( $_GET['filter'] ) : 'all';
		$filter			= 'all' != $filter ? $filter : implode( '|', $all_types );

		if ( 'gpi_custom_urls' == $filter ) {
			$filter = apply_filters( 'gpi_custom_url_labels', false );
		}

		$all_page_stats = array();

		if ( $filter ) {
			$all_page_stats = $wpdb->get_results(
				$wpdb->prepare(
					"
						SELECT ID, URL, {$this->strategy}_score as score, {$this->strategy}_lab_data as labData
						FROM $gpi_page_stats
						WHERE type REGEXP %s
						AND {$this->strategy}_score IS NOT NULL
						ORDER BY score DESC
					",
					$filter
				), ARRAY_A
			);
		}

		if ( ! $all_page_stats ) {
			return array();
		}
		
		$pages = count( $all_page_stats );
		$score = 0;

		$page_scores = array();
		$old_format_detected = false;

		$avg_lab_data	= array();

		foreach ( $all_page_stats as $page_stats ) {
			$score			+= $page_stats['score'];
			$lab_data		= unserialize( $page_stats['labData'] );
			$report_url		= admin_url( '/tools.php?page=google-pagespeed-insights&render=details&page_id=' . $page_stats['ID'] );

			$page_scores[] = array(
				'score'			=> intval( $page_stats['score'] ),
				'report_url'	=> esc_url_raw( $report_url ),
				'page_url'		=> esc_url( $page_stats['URL'] )
			);

			// flag if pre 2.0 data structure is detected
			if ( ! $old_format_detected && isset( $resources['numberHosts'] ) ) {
				$old_format_detected = true;
			}

			if ( ! empty( $lab_data ) ) {
				foreach ( $lab_data as $index => $data ) {
					if ( ! isset( $avg_lab_data[ $data['title'] ] ) ) {
						$avg_lab_data[ $data['title'] ] = array( 'lowest' => array( 'value' => false, 'url' => false ), 'average' => false, 'highest' => array( 'value' => false, 'url' => false ) );
					}

					$avg_lab_data[ $data['title'] ]['average'] += floatval( $data['score'] );

					if ( false === $avg_lab_data[ $data['title'] ]['highest']['value'] || $data['score'] > $avg_lab_data[ $data['title'] ]['highest']['value'] ) {
						$avg_lab_data[ $data['title'] ]['highest'] = array(
							'value' => esc_html( $data['displayValue'] ),
							'url' => esc_url_raw( $report_url )
						);
					}

					if ( false === $avg_lab_data[ $data['title'] ]['lowest']['value'] || $data['score'] < $avg_lab_data[ $data['title'] ]['lowest']['value'] ) {
						$avg_lab_data[ $data['title'] ]['lowest'] = array(
							'value' => esc_html( $data['displayValue'] ),
							'url' => esc_url_raw( $report_url )
						);
					}
				}
			}
		}

		foreach ( $avg_lab_data as &$data ) {
			$data['average'] = floatval( $data['average'] / $pages );
		}

		$score = round( $score / $pages );

		$high_scores = $this->sort_array( $page_scores, 'score', 'desc' );
		$low_scores = array_reverse( $high_scores );

		return array(
			'page_scores'		=> array(
				'highest'	=> array_slice( $high_scores, 0, 5 ),
				'lowest'	=> array_slice( $low_scores, 0, 5 )
			),
			'labData'			=> $avg_lab_data,
			'score'				=> intval( $score ),
			'odometer_rotation'	=> esc_html( ( ( 3.38 * $score ) + 11 ) . 'deg' ),
			'data_format'		=> ( $old_format_detected ) ? '1.0' : esc_html( GPI_VERSION )
		);
	}

	public function get_summary_reports( $summary_reports = array() )
	{
		global $wpdb;

		$all_types = $this->get_filter_options( array(), true );

		$gpi_page_stats		= $wpdb->prefix . 'gpi_page_stats';
		$gpi_page_reports	= $wpdb->prefix . 'gpi_page_reports';
		$filter				= isset( $_GET['filter'] ) ? sanitize_text_field( $_GET['filter'] ) : 'all';
		$filter				= 'all' != $filter ? $filter : implode( '|', $all_types );

		if ( 'gpi_custom_urls' == $filter ) {
			$filter = apply_filters( 'gpi_custom_url_labels', false );
		}

		$all_page_reports = array();

		if ( $filter ) {
			$all_page_reports = $wpdb->get_results(
				$wpdb->prepare(
					"
						SELECT r.rule_key, r.rule_name, r.rule_score
						FROM $gpi_page_stats as s
						INNER JOIN $gpi_page_reports as r
							ON r.page_id = s.ID
							AND r.strategy = %s
							AND r.rule_type = %s
							AND r.rule_score < .9
						WHERE type REGEXP %s
						AND s.{$this->strategy}_score IS NOT NULL
						ORDER BY r.rule_score DESC
					",
					$this->strategy,
					'opportunity',
					$filter
				), ARRAY_A
			);
		}

		if ( ! $all_page_reports ) {
			return array();
		}

		foreach ( $all_page_reports as $page_report ) {
			if ( isset( $summary_reports[ $page_report['rule_key'] ] ) ) {
				$summary_reports[ $page_report['rule_key'] ]['avg_score'] += $page_report['rule_score'];
				$summary_reports[ $page_report['rule_key'] ]['occurances']++;
			} else {
				$summary_reports[ $page_report['rule_key'] ] = array(
					'rule_name'		=> ( 'uses-optimized-images' != $page_report['rule_key'] ) ? wp_kses_post( $page_report['rule_name'] ) : wp_kses_post( $page_report['rule_name'] . '<span class="shortpixel_blurb"> &ndash; <a href="https://shortpixel.com/h/af/PCFTWNN142247" target="_blank">' . __( 'Auto-Optimize images with ShortPixel. Sign up for 150 free credits!', 'gpagespeedi') . '</a></span>' ),
					'avg_score'		=> $page_report['rule_score'],
					'occurances'	=> 1
				);
			}
		}

		foreach ( $summary_reports as &$summary_report ) {
			$summary_report['avg_score'] = round( $summary_report['avg_score'] / $summary_report['occurances'], 2 );
			$summary_report['avg_score'] = intval( $summary_report['avg_score'] * 100 );
		}

		$summary_reports = $this->sort_array( $summary_reports, 'avg_score' );

		return array_slice( $summary_reports, 0, 5 );
	}

	public function check_set_time_limit( $disabled )
	{
		$disabled = explode( ',', ini_get( 'disable_functions' ) );

		return in_array( 'set_time_limit', $disabled );
	}

	public function progress_heartbeat( $response, $data )
	{
		if ( ! isset( $data['gpi_heartbeat'] ) || 'progress' != $data['gpi_heartbeat'] ) {
			return $response;
		}

		$progress = get_option( 'gpi_progress' );

		if ( 'abort' == $progress ) {
			$response['gpi_progress_tooltip'] = '';
			$response['gpi_progress'] = 'abort';
		} else if ( $progress != null ) {
			$response['gpi_progress_tooltip'] = $progress;
			$split_status = explode( ' / ', $progress );

			$percent_complete = $split_status[0] / $split_status[1];
			$percent_complete = round( $percent_complete * 100 );

			$response['gpi_progress'] = $percent_complete;
		} else {
			$response['gpi_progress_tooltip'] = '';
			$response['gpi_progress'] = 'done';
		}

		return $response;
	}

	public function get_error_logs( $error_logs )
	{
		global $wpdb;

		$gpi_api_error_logs = $wpdb->prefix . 'gpi_api_error_logs';

		$error_logs = $wpdb->get_results(
			"
				SELECT URL, strategy, is_update, type, timestamp, error
				FROM $gpi_api_error_logs
				ORDER BY timestamp DESC
			", ARRAY_A
		);

		return $error_logs;
	}

	private function get_page_stats( $page_id )
	{
		global $wpdb;

		$gpi_page_stats = $wpdb->prefix . 'gpi_page_stats';

		$page_stats = $wpdb->get_row(
			$wpdb->prepare(
				"
					SELECT URL, {$this->strategy}_lab_data as labData, {$this->strategy}_field_data as fieldData, {$this->strategy}_score as score, {$this->strategy}_last_modified as last_modified
					FROM $gpi_page_stats
					WHERE ID = %d
				",
				$page_id
			), ARRAY_A
		);

		if ( empty( $page_stats ) ) {
			return false;
		}

		// flag if pre 2.0 data structure is detected
		if ( strpos( $page_stats['labData'], 'numberHosts' ) !== false ) {
			$page_stats['data_format'] = '1.0';
		} else {
			$page_stats['data_format'] = esc_html( GPI_VERSION );
		}

		if ( ! empty( $page_stats['score'] ) ) {
			$page_stats['odometer_rotation'] = ( ( 3.38 * $page_stats['score'] ) + 11 ) . 'deg';
		} else if ( '0' == $page_stats['score'] ) {
			$page_stats['odometer_rotation'] = '12deg';
		} else {
			$page_stats['score'] = '-';
		}

		if ( ! empty( $page_stats['last_modified'] ) ) {
			$page_stats['last_modified'] = date_i18n( 'M d g:ia', $page_stats['last_modified'] );
		} else {
			$page_stats['last_modified'] = esc_html__( 'N/A', 'gpagespeedi' );
		}

		$page_stats = array_map( 'maybe_unserialize', $page_stats );

		if ( isset( $page_stats['fieldData'] ) && ! empty( $page_stats['fieldData'] ) ) {
			$page_stats['fieldData'] = $this->object_to_array( $page_stats['fieldData'] );
		}

		array_walk_recursive( $page_stats, [ $this, 'escape_and_format_recursive' ], 'page_stats' );

		return $page_stats;
	}

	private function get_page_reports( $page_id )
	{
		global $wpdb;

		$gpi_page_reports = $wpdb->prefix . 'gpi_page_reports';

		$page_reports = $wpdb->get_results(
			$wpdb->prepare(
				"
					SELECT rule_key, rule_name, rule_score, rule_blocks
					FROM $gpi_page_reports
					WHERE page_id = %d
					AND strategy = %s
					ORDER BY rule_score ASC
				",
				$page_id,
				$this->strategy
			), ARRAY_A
		);

		if ( $page_reports ) {
			foreach ( $page_reports as $index => &$page_report ) {
				$page_report['rule_blocks'] = unserialize( $page_report['rule_blocks'] );

				if ( isset( $page_report['rule_blocks'] ) && ! empty( $page_report['rule_blocks'] ) ) {
					$page_report['rule_blocks'] = $this->object_to_array( $page_report['rule_blocks'] );
				}

				array_walk_recursive( $page_report, [ $this, 'escape_and_format_recursive' ], 'page_reports' );

				if ( 'uses-optimized-images' == $page_report['rule_key'] ) {
					$page_report['rule_blocks'] = $this->shortpixel_image_rule_blocks( $page_report['rule_blocks'] );
				}
			}
		}

		return $page_reports;
	}

	private function get_snapshot_data()
	{
		global $wpdb;

		$gpi_summary_snapshots = $wpdb->prefix . 'gpi_summary_snapshots';

		if ( isset( $_GET['snapshot_id'] ) ) {
			$snapshot = $wpdb->get_row(
				$wpdb->prepare(
					"
					SELECT strategy, type, snaptime, comment, summary_stats, summary_reports
					FROM $gpi_summary_snapshots
					WHERE ID = %d
					",
					$_GET['snapshot_id']
				), ARRAY_A
			);
		} else {
			$snapshot = false;
		}

		if ( isset( $_GET['compare_id'] ) ) {
			$compare_snapshot = $wpdb->get_row(
				$wpdb->prepare(
					"
					SELECT strategy, type, snaptime, comment, summary_stats, summary_reports
					FROM $gpi_summary_snapshots
					WHERE ID = %d
					",
					$_GET['compare_id']
				), ARRAY_A
			);
		} else {
			$compare_snapshot = false;
		}

		if ( isset( $snapshot['summary_stats'] ) ) {
			$snapshot['summary_stats'] = json_decode( $snapshot['summary_stats'] );
		}
		if ( isset( $snapshot['summary_reports'] ) ) {
			$snapshot['summary_reports'] = json_decode( $snapshot['summary_reports'] );
		}

		$snapshot = $this->object_to_array( $snapshot );

		if ( ! $snapshot ) {
			wp_redirect( admin_url( 'tools.php?page=google-pagespeed-insights&render=snapshots' ) );
			exit;
		}

		array_walk_recursive( $snapshot, [ $this, 'escape_and_format_recursive' ], 'snapshot' );

		if ( $compare_snapshot ) {
			if ( isset( $compare_snapshot['summary_stats'] ) ) {
				$compare_snapshot['summary_stats'] = json_decode( $compare_snapshot['summary_stats'] );
			}
			if ( isset( $compare_snapshot['summary_reports'] ) ) {
				$compare_snapshot['summary_reports'] = json_decode( $compare_snapshot['summary_reports'] );
			}

			$compare_snapshot = $this->object_to_array( $compare_snapshot );

			array_walk_recursive( $compare_snapshot, [ $this, 'escape_and_format_recursive' ], 'snapshot' );
		} else if ( isset( $_GET['compare_id'] ) ) {
			if ( ! $compare_snapshot ) {
				wp_redirect( admin_url( 'tools.php?page=google-pagespeed-insights&render=snapshots' ) );
				exit;
			}
		}

		return array(
			'snapshot'	=> $snapshot,
			'compare'	=> $compare_snapshot
		);
	}

	public function get_similar_snapshots( $similar_snapshots, $current_snapshot_id )
	{
		if ( $current_snapshot_id ) {
			global $wpdb;

			$gpi_summary_snapshots = $wpdb->prefix . 'gpi_summary_snapshots';

			$current_snapshot = $wpdb->get_row(
				$wpdb->prepare(
					"
						SELECT strategy, type
						FROM $gpi_summary_snapshots
						WHERE ID = %d
					",
					$current_snapshot_id
				), ARRAY_A
			);

			$similar_snapshots = $wpdb->get_results(
				$wpdb->prepare(
					"
						SELECT ID, snaptime 
						FROM $gpi_summary_snapshots
						WHERE strategy = %s
						AND type = %s
					",
					$current_snapshot['strategy'],
					$current_snapshot['type']
				), ARRAY_A
			);
		}

		return $similar_snapshots;
	}

	private function shortpixel_image_rule_blocks( $rule_blocks )
	{
		if ( ! isset( $rule_blocks['details']->items ) || empty( $rule_blocks['details']->items ) ) {
			return $rule_blocks;
		}

		foreach ( $rule_blocks['details']->items as $index => $item ) {
			if ( isset( $item->url ) ) {
				$rule_blocks['details']->items[ $index ]->url = preg_replace(
					'/(?:href=")(.*?)(?:")/',
					'href="https://shortpixel.com/gpi/af/PCFTWNN142247?site-url=$1"',
					$item->url
				);
			}
		}

		return $rule_blocks;
	}

	private function sort_array( $array, $key, $direction = 'asc' )
	{
		usort( $array, function( $a, $b ) use ( $key, $direction ) {
			if ( abs( $a[ $key ] - $b[ $key ] ) < 0.00000001 ) {
				return 0; // almost equal
			} else if ( ( $a[ $key ] - $b[ $key ] ) < 0 ) {   
				return $direction == 'asc' ? -1 : 1;
			} else {
				return $direction == 'asc' ? 1 : -1;
			}
		});

		return $array;
	}

	private function object_to_array( $obj )
	{
		if ( is_object( $obj ) || is_array( $obj ) ) {
			$ret = (array) $obj;
			foreach ( $ret as &$item ) {
				$item = $this->object_to_array( $item );
			}
			return $ret;
		} else {
			return $obj;
		}
	}

	private function escape_and_format_recursive( &$value, $key, $type )
	{
		switch ( $key ) {
			case 'URL':
				$value = esc_url( $value );
				break;

			case 'url':
				if ( 'snapshot' == $type ) {
					$value = esc_url_raw( $value );
				} else {
					$value = wp_kses_post( $value );
				}
				break;

			case 'report_url':
				$value = esc_url_raw( $value );
				break;

			case 'groupLabel':
				$value =  esc_html( $value );
				$value = str_replace( ' &amp; ', ' & ', $value );
				break;

			case 'rule_name':
			case 'description':
				$value = wp_kses_post( $value );
				break;

			case 'min':
			case 'max':
			case 'percentile':
				$value = intval( $value );
				break;

			case 'proportion':
			case 'average':
				$value = floatval( $value );
				break;

			case 'score':
				$value = floatval( $value );
				$value = (float) number_format( $value, 3 );
				break;

			case 'blockingTime':
			case 'wastedMs':
			case 'startTime':
			case 'duration':
			case 'total':
			case 'scripting':
			case 'scriptParseCompile':
				$value = floatval( $value );
				$value = (float) number_format( $value, 3 );
				$value = $value . ' ' . 'ms';
				break;

			case 'wastedBytes':
			case 'totalBytes':
				$value = intval( $value );
				$value = number_format( $value / 1024, 2 );
				$value = $value . ' ' . 'KiB';
				break;

			case 'transferSize':
				$value = intval( $value );
				$value = number_format( $value / 1024, 2 );
				$value = $value . ' ' . 'KiB';
				break;

			default:
				$value = esc_html( $value );
				break;
		}
	}

}

add_action( 'plugins_loaded', array( new GPI_Admin, 'init' ) );

Youez - 2016 - github.com/yon3zu
LinuXploit