array( 'pagespeed_widget_data_error' => __( 'Error : ', 'w3-total-cache' ), ), ) ); wp_enqueue_script( 'w3tc-widget-pagespeed' ); wp_enqueue_style( 'w3tc-widget-pagespeed', esc_url( plugins_url( 'PageSpeed_Widget_View.css', W3TC_FILE ) ), array(), W3TC_VERSION ); } /** * Dashboard setup action. * * @since 2.3.0 * * @return void */ public function wp_dashboard_setup() { Util_Widget::add( 'w3tc_pagespeed', '
' . ' ', array( $this, 'widget_pagespeed' ), Util_Ui::admin_url( 'admin.php?page=w3tc_general#google_page_speed' ), 'normal' ); } /** * PageSpeed widget. * * @since 2.3.0 * * @return void */ public function widget_pagespeed() { $config = Dispatcher::config(); $access_token = $config->get_string( 'widget.pagespeed.access_token' ); include W3TC_DIR . '/PageSpeed_Widget_View.php'; } /** * PageSpeed widget AJAX fetch data. * * @since 2.3.0 * * @return JSON */ public function w3tc_ajax_pagespeed_widgetdata() { $home_url = get_home_url(); $api_response = null; $api_response_error = null; if ( Util_Request::get( 'cache' ) !== 'no' ) { $cache = get_option( 'w3tc_pagespeed_data_' . $home_url ); $cache = json_decode( $cache, true ); if ( is_array( $cache ) && isset( $cache['time'] ) && $cache['time'] >= time() - Util_PageSpeed::get_cache_life() ) { $api_response = $cache; } } if ( is_null( $api_response ) ) { $config = Dispatcher::config(); $access_token = ! empty( $config->get_string( 'widget.pagespeed.access_token' ) ) ? $config->get_string( 'widget.pagespeed.access_token' ) : null; if ( empty( $access_token ) ) { echo wp_json_encode( array( 'missing_token' => sprintf( // translators: 1 HTML a tag to W3TC settings page Google PageSpeed meta box. __( 'Before you can get started using the Google PageSpeed tool, you’ll first need to authorize access. Please click %1$s.', 'w3-total-cache' ), '' . esc_html__( 'here', 'w3-total-cache' ) . '' ), ) ); return; } $w3_pagespeed = new PageSpeed_Api( $access_token ); $api_response = $w3_pagespeed->analyze( $home_url ); if ( ! $api_response ) { $api_response_error = array( 'error' => '' . esc_html__( 'API request failed!', 'w3-total-cache' ) . '
' . esc_html__( 'Analyze URL : ', 'w3-total-cache' ) . $home_url . '
', ); delete_option( 'w3tc_pagespeed_data_' . $home_url ); } elseif ( ! empty( $api_response['error'] ) ) { $error_code = ! empty( $api_response['error']['code'] ) ? $api_response['error']['code'] : 'N/A'; $error_message = ! empty( $api_response['error']['message'] ) ? $api_response['error']['message'] : 'N/A'; $api_response_error = array( 'error' => '' . esc_html__( 'API request error!', 'w3-total-cache' ) . '
' . esc_html__( 'Analyze URL : ', 'w3-total-cache' ) . $home_url . '
' . esc_html__( 'Response Code : ', 'w3-total-cache' ) . $error_code . '
' . esc_html__( 'Response Message : ', 'w3-total-cache' ) . $error_message . '
', ); delete_option( 'w3tc_pagespeed_data_' . $home_url ); } elseif ( ! empty( $api_response['mobile']['error'] ) && ! empty( $api_response['desktop']['error'] ) ) { $mobile_error_code = ! empty( $api_response['mobile']['error']['code'] ) ? $api_response['mobile']['error']['code'] : 'N/A'; $mobile_error_message = ! empty( $api_response['mobile']['error']['message'] ) ? $api_response['mobile']['error']['message'] : 'N/A'; $desktop_error_code = ! empty( $api_response['desktop']['error']['code'] ) ? $api_response['desktop']['error']['code'] : 'N/A'; $desktop_error_message = ! empty( $api_response['desktop']['error']['message'] ) ? $api_response['desktop']['error']['message'] : 'N/A'; $api_response_error = array( 'error' => '' . esc_html__( 'API request error!', 'w3-total-cache' ) . '
' . esc_html__( 'Analyze URL : ', 'w3-total-cache' ) . $home_url . '
' . esc_html__( 'Mobile response Code : ', 'w3-total-cache' ) . $mobile_error_code . '
' . esc_html__( 'Mobile response Message : ', 'w3-total-cache' ) . $mobile_error_message . '
' . esc_html__( 'Desktop response Code : ', 'w3-total-cache' ) . $desktop_error_code . '
' . esc_html__( 'Desktop response Message : ', 'w3-total-cache' ) . $desktop_error_message . '
', ); delete_option( 'w3tc_pagespeed_data_' . $home_url ); } else { $api_response['time'] = time(); $api_response['display_time'] = \current_time( 'M jS, Y g:ia', false ); update_option( 'w3tc_pagespeed_data_' . $home_url, wp_json_encode( $api_response ), 'yes' ); } } ob_start(); include __DIR__ . '/PageSpeed_Widget_View_FromApi.php'; $content = ob_get_contents(); ob_end_clean(); echo wp_json_encode( array( 'w3tcps_widget' => $content, 'w3tcps_timestamp' => ! empty( $api_response['display_time'] ) ? $api_response['display_time'] : '', ) ); } }