| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Modules\GooglePageSpeed; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Modules\PageSpeed_Insight\PageSpeed_Insight; |
| 6 |
use WPAdminify\Inc\Admin\AdminSettings; |
| 7 |
use WPAdminify\Inc\Utils; |
| 8 |
|
| 9 |
// no direct access allowed |
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* WP Adminify |
| 16 |
* Module: Google Pagespeed |
| 17 |
* |
| 18 |
* @author Jewel Theme <support@jeweltheme.com> |
| 19 |
*/ |
| 20 |
|
| 21 |
if ( ! class_exists( 'GooglePageSpeed' ) ) { |
| 22 |
|
| 23 |
class GooglePageSpeed { |
| 24 |
|
| 25 |
|
| 26 |
public $url; |
| 27 |
|
| 28 |
public $insight; |
| 29 |
|
| 30 |
public $options; |
| 31 |
|
| 32 |
public function __construct() { |
| 33 |
$needed_keys = [ 'google_pagepseed_user_roles', 'google_pagepseed_api_key' ]; |
| 34 |
|
| 35 |
$this->options = (array) array_intersect_key( AdminSettings::get_instance()->get(), array_flip( $needed_keys ) ); |
| 36 |
|
| 37 |
// $this->url = WP_ADMINIFY_URL . 'Inc/Modules/GooglePageSpeed'; |
| 38 |
|
| 39 |
add_action( 'admin_menu', [ $this, 'GooglePageSpeed' ], 47 ); |
| 40 |
|
| 41 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ], 100 ); |
| 42 |
|
| 43 |
add_action( 'wp_ajax_adminify_page_speed', [ $this, 'handle_adminify_page_speed' ] ); |
| 44 |
|
| 45 |
add_action( 'init', [ $this, 'maybe_create_tables' ], 0 ); |
| 46 |
} |
| 47 |
|
| 48 |
public function is_module_active() { |
| 49 |
$mopdule_roles = array_filter( (array) $this->options['google_pagepseed_user_roles'] ); |
| 50 |
|
| 51 |
if ( empty( $mopdule_roles ) ) { |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
$user = wp_get_current_user(); |
| 56 |
|
| 57 |
return ! empty( array_intersect( $mopdule_roles, $user->roles ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Google Pagespeed Menu |
| 62 |
*/ |
| 63 |
public function GooglePageSpeed() { |
| 64 |
if ( ! $this->is_module_active() ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
add_submenu_page( |
| 69 |
'wp-adminify-settings', |
| 70 |
esc_html__( 'Google Pagespeed Insights by WP Adminify', 'adminify' ), |
| 71 |
esc_html__( 'Pagespeed Insights', 'adminify' ), |
| 72 |
apply_filters( 'jltwp_adminify_capability', 'manage_options' ), |
| 73 |
'adminify-pagespeed-insights', // Page slug, will be displayed in URL |
| 74 |
[ $this, 'jltwp_adminify_menu_editor_contents' ] |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
public function jltwp_adminify_menu_editor_contents() { |
| 79 |
echo '<div class="wrap"><div id="wp-adminify--page-speed-app"></div></div>'; |
| 80 |
} |
| 81 |
|
| 82 |
public function load_insight_class() { |
| 83 |
require_once 'class-pagespeed-insights.php'; |
| 84 |
|
| 85 |
if ( ! ( $this->insight instanceof PageSpeed_Insight ) ) { |
| 86 |
$this->insight = new PageSpeed_Insight( $this->options['google_pagepseed_api_key'] ); |
| 87 |
} |
| 88 |
|
| 89 |
return $this->insight; |
| 90 |
} |
| 91 |
|
| 92 |
public function handle_adminify_page_speed() { |
| 93 |
check_ajax_referer( '__adminify-page_speed-secirity__' ); |
| 94 |
|
| 95 |
if ( ! empty( $_POST['route'] ) ) { |
| 96 |
$route_handler = 'handle_' . sanitize_text_field( wp_unslash( $_POST['route'] ) ); |
| 97 |
if ( is_callable( get_class( $this ), $route_handler ) ) { |
| 98 |
$this->$route_handler( $_POST ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
wp_send_json_error( [ 'message' => __( 'Something is wrong, no route found' ) ], 400 ); |
| 103 |
} |
| 104 |
|
| 105 |
private function _get_analyze_data( $url ) { |
| 106 |
$data = [ |
| 107 |
'desktop' => null, |
| 108 |
'mobile' => null, |
| 109 |
]; |
| 110 |
|
| 111 |
$result = $this->insight->run_insight( $url, [ 'strategy' => 'desktop' ] ); |
| 112 |
if ( ! empty( $result ) && $result['responseCode'] == 200 && ! empty( $result['data'] ) ) { |
| 113 |
$data['desktop'] = $result['data']; |
| 114 |
|
| 115 |
$result_mobile = $this->insight->run_insight( $url, [ 'strategy' => 'mobile' ] ); |
| 116 |
if ( ! empty( $result_mobile ) && $result_mobile['responseCode'] == 200 && ! empty( $result_mobile['data'] ) ) { |
| 117 |
$data['mobile'] = $result_mobile['data']; |
| 118 |
return $data; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
public function handle_new_analyze( $data ) { |
| 126 |
check_ajax_referer( '__adminify-page_speed-secirity__' ); |
| 127 |
|
| 128 |
if ( empty( $data['url'] ) || ! wp_http_validate_url( $data['url'] ) ) { |
| 129 |
wp_send_json_error( [ 'message' => __( 'Something is wrong, URL is missing or wrong formatted' ) ], 400 ); |
| 130 |
} |
| 131 |
|
| 132 |
set_time_limit( 300 ); |
| 133 |
|
| 134 |
$this->load_insight_class(); |
| 135 |
|
| 136 |
$analyze_data = $this->_get_analyze_data( $data['url'] ); |
| 137 |
|
| 138 |
if ( $analyze_data === false ) { |
| 139 |
wp_send_json_error( [ 'message' => __( 'Couldn\'t fetch the result, Please try again later' ) ], 503 ); |
| 140 |
} |
| 141 |
|
| 142 |
$saved = $this->save_analyze( $analyze_data ); |
| 143 |
|
| 144 |
if ( empty( $saved ) ) { |
| 145 |
wp_send_json_error( [ 'message' => __( 'Couldn\'t save the result, Please try again later' ) ], 500 ); |
| 146 |
} |
| 147 |
|
| 148 |
wp_send_json_success( $saved ); |
| 149 |
} |
| 150 |
|
| 151 |
public function save_analyze( $analyze_data ) { |
| 152 |
$data_desktop = $analyze_data['desktop']; |
| 153 |
$data_mobile = $analyze_data['mobile']; |
| 154 |
$url = $data_desktop->id; |
| 155 |
$score_desktop = $data_desktop->lighthouseResult->categories->performance->score * 100; |
| 156 |
$score_mobile = $data_mobile->lighthouseResult->categories->performance->score * 100; |
| 157 |
$screenshot = $data_desktop->lighthouseResult->audits->{'final-screenshot'}->details->data; |
| 158 |
|
| 159 |
// unset( $data_desktop->lighthouseResult->audits->{'final-screenshot'} ); |
| 160 |
// unset( $data_mobile->lighthouseResult->audits->{'final-screenshot'} ); |
| 161 |
|
| 162 |
$data = [ |
| 163 |
'url' => $url, |
| 164 |
'score_desktop' => (int) $score_desktop, |
| 165 |
'score_mobile' => (int) $score_mobile, |
| 166 |
'data_desktop' => json_encode( $data_desktop ), |
| 167 |
'data_mobile' => json_encode( $data_mobile ), |
| 168 |
'screenshot' => $screenshot, |
| 169 |
'time' => current_time( 'mysql' ), |
| 170 |
]; |
| 171 |
|
| 172 |
global $wpdb; |
| 173 |
|
| 174 |
$result = $wpdb->insert( |
| 175 |
"{$wpdb->prefix}adminify_page_speed", |
| 176 |
$data, |
| 177 |
[ |
| 178 |
'url' => '%s', |
| 179 |
'score_desktop' => '%d', |
| 180 |
'score_mobile' => '%d', |
| 181 |
'data_desktop' => '%s', |
| 182 |
'data_mobile' => '%s', |
| 183 |
'screenshot' => '%s', |
| 184 |
'time' => '%s', |
| 185 |
] |
| 186 |
); |
| 187 |
|
| 188 |
if ( empty( $result ) ) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
return $wpdb->insert_id; |
| 193 |
} |
| 194 |
|
| 195 |
public function handle_count_total( $data ) { |
| 196 |
check_ajax_referer( '__adminify-page_speed-secirity__' ); |
| 197 |
|
| 198 |
global $wpdb; |
| 199 |
|
| 200 |
$table_name = $wpdb->prefix . 'adminify_page_speed'; |
| 201 |
|
| 202 |
$total_query = "SELECT COUNT(*) FROM $table_name"; |
| 203 |
$total = $wpdb->get_var( $total_query ); |
| 204 |
|
| 205 |
wp_send_json_success( $total ); |
| 206 |
} |
| 207 |
|
| 208 |
public function handle_fetch_histories( $data ) { |
| 209 |
check_ajax_referer( '__adminify-page_speed-secirity__' ); |
| 210 |
|
| 211 |
global $wpdb; |
| 212 |
|
| 213 |
$table_name = $wpdb->prefix . 'adminify_page_speed'; |
| 214 |
$fields = 'id, url, score_desktop, score_mobile, time, screenshot'; |
| 215 |
$items_per_page = empty( $data['items_per_page'] ) ? 1 : abs( (int) $data['items_per_page'] ); |
| 216 |
$page = empty( $data['page'] ) ? 1 : abs( (int) $data['page'] ); |
| 217 |
$offset = ( $page * $items_per_page ) - $items_per_page; |
| 218 |
|
| 219 |
$query = "SELECT {$fields} FROM $table_name"; |
| 220 |
|
| 221 |
$total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table"; |
| 222 |
$total = $wpdb->get_var( $total_query ); |
| 223 |
|
| 224 |
$histories = $wpdb->get_results( $query . ' ORDER BY time DESC LIMIT ' . $offset . ', ' . $items_per_page, ARRAY_A ); |
| 225 |
|
| 226 |
foreach ( $histories as &$history ) { |
| 227 |
$history['formated_date'] = date( 'Y-m-d', strtotime( $history['time'] ) ); |
| 228 |
$history['formated_time'] = date( 'H:i:s', strtotime( $history['time'] ) ); |
| 229 |
} |
| 230 |
|
| 231 |
wp_send_json_success( |
| 232 |
[ |
| 233 |
'total' => (int) $total, |
| 234 |
'histories' => (array) $histories, |
| 235 |
] |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
public function handle_fetch_history( $data ) { |
| 240 |
check_ajax_referer( '__adminify-page_speed-secirity__' ); |
| 241 |
|
| 242 |
if ( empty( $data['id'] ) ) { |
| 243 |
wp_send_json_error( 'Something is wrong, Try again later', 400 ); |
| 244 |
} |
| 245 |
|
| 246 |
global $wpdb; |
| 247 |
|
| 248 |
$table_name = $wpdb->prefix . 'adminify_page_speed'; |
| 249 |
$history_id = $data['id']; |
| 250 |
$fields = 'id, url, score_desktop, score_mobile, data_desktop, data_mobile, time'; |
| 251 |
|
| 252 |
$history = $wpdb->get_row( $wpdb->prepare( "SELECT {$fields} FROM $table_name WHERE id = %d", $history_id ), ARRAY_A ); |
| 253 |
|
| 254 |
if ( empty( $history ) || $wpdb->last_error !== '' ) { |
| 255 |
wp_send_json_error( $wpdb->last_error, 400 ); |
| 256 |
} |
| 257 |
|
| 258 |
$history['data_desktop'] = json_decode( $history['data_desktop'], true ); |
| 259 |
$history['data_mobile'] = json_decode( $history['data_mobile'], true ); |
| 260 |
|
| 261 |
wp_send_json_success( |
| 262 |
[ |
| 263 |
'history' => $history, |
| 264 |
] |
| 265 |
); |
| 266 |
} |
| 267 |
|
| 268 |
public function handle_delete_history() { |
| 269 |
check_ajax_referer( '__adminify-page_speed-secirity__' ); |
| 270 |
|
| 271 |
if ( empty( $_POST['ids'] ) ) { |
| 272 |
wp_send_json_error( 'Something is wrong, Try again later', 400 ); |
| 273 |
} |
| 274 |
|
| 275 |
global $wpdb; |
| 276 |
|
| 277 |
$ids = implode( ',', array_map( 'absint', $_POST['ids'] ) ); |
| 278 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}adminify_page_speed WHERE ID IN($ids)" ); |
| 279 |
|
| 280 |
if ( $wpdb->last_error !== '' ) { |
| 281 |
wp_send_json_error( $wpdb->last_error, 400 ); |
| 282 |
} |
| 283 |
|
| 284 |
wp_send_json_success( |
| 285 |
[ |
| 286 |
'message' => 'History has been deleted', |
| 287 |
] |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
public function enqueue_scripts() { |
| 292 |
wp_enqueue_style( 'wp-adminify--page-speed', WP_ADMINIFY_URL . 'assets/admin/css/wp-adminify--page-speed.css', [], WP_ADMINIFY_VER ); |
| 293 |
|
| 294 |
$data = [ |
| 295 |
'adminurl' => admin_url(), |
| 296 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 297 |
'nonce' => wp_create_nonce( '__adminify-page_speed-secirity__' ), |
| 298 |
'is_pro' => wp_validate_boolean( jltwp_adminify()->can_use_premium_code__premium_only() ), |
| 299 |
'pro_notice' => Utils::adminify_upgrade_pro(), |
| 300 |
]; |
| 301 |
|
| 302 |
wp_localize_script( 'wp-adminify--page-speed', 'wp_adminify__pagespeed_data', $data ); |
| 303 |
|
| 304 |
wp_enqueue_script( 'wp-adminify--page-speed' ); |
| 305 |
} |
| 306 |
|
| 307 |
public function maybe_create_tables() { |
| 308 |
global $wpdb; |
| 309 |
|
| 310 |
// delete_option( "{$wpdb->prefix}adminify_page_speed_db_version" ); |
| 311 |
|
| 312 |
$db_version = '1.0'; |
| 313 |
|
| 314 |
if ( get_option( "{$wpdb->prefix}adminify_page_speed_db_version" ) == $db_version ) { |
| 315 |
return; // vail early |
| 316 |
} |
| 317 |
|
| 318 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 319 |
|
| 320 |
$sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}adminify_page_speed ( |
| 321 |
id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT, |
| 322 |
url VARCHAR(255) NOT NULL, |
| 323 |
score_desktop VARCHAR(3) NOT NULL, |
| 324 |
score_mobile VARCHAR(3) NOT NULL, |
| 325 |
data_desktop MEDIUMTEXT NOT NULL, |
| 326 |
data_mobile MEDIUMTEXT NOT NULL, |
| 327 |
screenshot MEDIUMTEXT NOT NULL, |
| 328 |
time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 329 |
PRIMARY KEY (id) |
| 330 |
)" . $wpdb->get_charset_collate() . ';'; |
| 331 |
|
| 332 |
if ( get_option( "{$wpdb->prefix}adminify_page_speed_db_version" ) < $db_version ) { |
| 333 |
dbDelta( $sql ); |
| 334 |
} |
| 335 |
|
| 336 |
update_option( "{$wpdb->prefix}adminify_page_speed_db_version", $db_version ); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|