option_monitoring = 'awpp_monitoring'; $this->option_psikey = 'awpp_monitoring_psikey'; $this->ajax_set_psikey = 'awpp_monitoring_set_psikey'; $this->action_remove_psikey = 'awpp_monitoring_remove_psikey'; $this->ajax_save_settings = 'awpp_monitoring_save_settings'; $this->dir = trailingslashit( wp_upload_dir()['basedir'] ) . 'awpp-monitoring/'; if ( ! is_dir( $this->dir ) ) { mkdir( $this->dir ); } } public function run() { add_action( 'awpp_basics_section', [ $this, 'speed_test_monitoring' ] ); add_action( 'wp_ajax_' . $this->ajax_set_psikey, [ $this, 'set_psikey' ] ); add_action( 'admin_action_' . $this->action_remove_psikey, [ $this, 'remove_psikey' ] ); add_filter( 'cron_schedules', [ $this, 'cron_schedules' ] ); add_action( 'wp_ajax_' . $this->ajax_save_settings, [ $this, 'ajax_save_settings' ] ); } public function speed_test_monitoring() { $urls = apply_filters( 'awpp_monitoring_urls', [ get_home_url() ] ); if ( ! is_array( $urls ) ) { $urls = [ get_home_url() ]; } natsort( $urls ); //$colors = [ '#ff0000', '#00ff00', '#0000ff' ]; $psi_apikey = get_option( $this->option_psikey ); $psi_apikey_set = ( '' != $psi_apikey ); add_thickbox(); /** * Settings */ $settings = [ 'frequency' => [], 'email' => __( 'Email', 'awpp' ), ]; foreach ( wp_get_schedules() as $key => $shedule ) { $settings['frequency'][ $key ] = $shedule['display']; } echo '
'; /** * Page */ echo '| ' . __( 'Link', 'awpp' ) . ' | '; echo '' . __( 'lowest', 'awpp' ) . ' | '; echo '' . __( 'highest', 'awpp' ) . ' | '; echo '' . __( 'average', 'awpp' ) . ' | '; echo ''; echo ''; echo ''; foreach ( $urls as $index => $url ) { $key = $this->filter_monitoring_url_key( $url ); $file = $this->dir . $key . '.json'; if ( ! file_exists( $file ) ) { fopen( $file, 'w' ); } $data = json_decode( file_get_contents( $file ), true ); if ( is_null( $data ) ) { continue; } //echo ' |
|---|---|---|---|---|
| {$url} | "; echo "$min | "; echo "$max | "; echo "$average | "; echo ""; echo ' |
' . __( 'remove API Key', 'awpp' ) . '
'; } else { echo '' . __( 'Google Pagespeed Insights API Key', 'awpp' ) . '
'; echo '' . __( 'Get an API Key', 'awpp' ) . '
'; echo ''; //echo 'AIzaSyD1DEAkkZIGqitAhOTn1BbqctWP6f_tAoI'; echo ''; wp_nonce_field( $this->option_psikey . '_nonce', 'nonce' ); echo '' . sht_error( 'nonce error' ) . '
' ); } $return = $this->do_psi_request( get_home_url(), $_POST['apikey'] ); if ( isset( $return['error'] ) && is_array( $return['error'] ) ) { $error = "Error {$return['error']['code']}: {$return['error']['message']}"; awpp_exit_ajax( 'error', $error, $return ); } update_option( $this->option_psikey, $_POST['apikey'] ); awpp_exit_ajax( 'success', 'test', $return ); } public function remove_psikey() { if ( false === current_user_can( awpp_settings()->capability ) ) { wp_die( esc_html__( 'Access denied.', 'awpp' ) ); } update_option( $this->option_psikey, '' ); $sendback = wp_get_referer(); wp_redirect( esc_url_raw( $sendback ) ); exit; } public function do_psi_request( $url, $key = '' ) { if ( '' == $key ) { $key = get_option( $this->option_psikey ); } $url = "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$url&key=$key"; $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); $content = curl_exec( $ch ); curl_close( $ch ); return json_decode( $content, true ); } public function cron_schedules( $schedules ) { $schedules['awpp_hourly'] = [ 'interval' => ( 60 * 60 ), 'display' => __( 'Every Hour', 'awpp' ), ]; $schedules['awpp_twicedaily'] = [ 'interval' => ( 60 * 60 * 12 ), 'display' => __( 'Twice Daily', 'awpp' ), ]; $schedules['awpp_daily'] = [ 'interval' => ( 60 * 60 * 24 ), 'display' => __( 'Daily', 'awpp' ), ]; $schedules['awpp_twiceweekly'] = [ 'interval' => ( 60 * 60 * 24 * 3.5 ), 'display' => __( 'Twice Weekly', 'awpp' ), ]; $schedules['awpp_weekly'] = [ 'interval' => ( 60 * 60 * 24 * 7 ), 'display' => __( 'Weekly', 'awpp' ), ]; return $schedules; } public function ajax_save_settings() { $data = [ 'frequency' => 'never', 'minindex' => 1, 'email' => '', ]; foreach ( $data as $key => $value ) { if ( ! array_key_exists( $key, $_POST ) ) { continue; } if ( 'minindex' == $key ) { $data[ $key ] = intval( $_POST[ $key ] ); } elseif ( 'email' == $key ) { $data[ $key ] = sanitize_email( $_POST[ $key ] ); } else { $data[ $key ] = $_POST[ $key ]; } } update_option( $this->option_monitoring, $data ); wp_clear_scheduled_hook( 'awpp_monitoring_sheduled_psi_request' ); if ( 'never' != $data['frequency'] ) { $this->sheduled_psi_request(); wp_schedule_event( time(), $data['frequency'], 'awpp_monitoring_sheduled_psi_request' ); } add_action( 'awpp_monitoring_sheduled_psi_request', [ $this, 'sheduled_psi_request' ] ); awpp_exit_ajax( 'success', 'test' ); } public function get_setting( $key, $default = '' ) { $option = get_option( $this->option_monitoring ); if ( ! is_array( $option ) ) { return $default; } if ( array_key_exists( $key, $option ) ) { return $option[ $key ]; } return $default; } public function sheduled_psi_request() { $urls = apply_filters( 'awpp_monitoring_urls', [ get_home_url() ] ); $settings = get_option( $this->option_monitoring ); foreach ( $urls as $url ) { $key = $this->filter_monitoring_url_key( $url ); $file = $this->dir . $key . '.json'; if ( ! file_exists( $file ) ) { fopen( $file, 'w' ); } $old_data = json_decode( file_get_contents( $file ), true ); if ( is_null( $old_data ) ) { $old_data = []; } $return_value = $this->do_psi_request( $url ); if ( ! isset( $return['error'] ) ) { $old_data[ time() ] = $return_value; $parsed_value = $this->parse_psi( $return_value ); if ( intval( $settings['minindex'] ) > $parsed_value['score'] ) { // translators: Sheduled Pagespeed insights returned a score lower than {{score}}. $content = sprintf( __( 'Sheduled Pagespeed insights returned a score lower than %s.', 'awpp' ), $settings['minindex'] ); $content .= '| ' . __( 'Time:', 'awpp' ) . ' | ' . awpp_convert_date() . ' |
| ' . __( 'Score:', 'awpp' ) . ' | ' . $parsed_value['score'] . ' |
| ' . __( 'URL:', 'awpp' ) . ' | ' . $url . ' |
| ' . __( 'Answer:', 'awpp' ) . ' | ' . print_r( $parsed_value, true ) . ' |