| 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')) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* WP Adminify |
| 14 |
* Module: Google Pagespeed |
| 15 |
* |
| 16 |
* @author Jewel Theme <support@jeweltheme.com> |
| 17 |
*/ |
| 18 |
|
| 19 |
if (!class_exists('GooglePageSpeed')) { |
| 20 |
|
| 21 |
class GooglePageSpeed |
| 22 |
{ |
| 23 |
|
| 24 |
public $url; |
| 25 |
|
| 26 |
public $insight; |
| 27 |
|
| 28 |
public $options; |
| 29 |
|
| 30 |
public function __construct() |
| 31 |
{ |
| 32 |
|
| 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']); |
| 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 |
{ |
| 50 |
|
| 51 |
$mopdule_roles = array_filter((array) $this->options['google_pagepseed_user_roles']); |
| 52 |
|
| 53 |
if (empty($mopdule_roles)) return true; |
| 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 |
{ |
| 65 |
|
| 66 |
if (!$this->is_module_active()) return; |
| 67 |
|
| 68 |
add_submenu_page( |
| 69 |
'wp-adminify-settings', |
| 70 |
esc_html__('Google Pagespeed Insights by WP Adminify', WP_ADMINIFY_TD), |
| 71 |
esc_html__('Pagespeed Insights', WP_ADMINIFY_TD), |
| 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 |
{ |
| 80 |
echo '<div class="wrap"><div id="wp-adminify--page-speed-app"></div></div>'; |
| 81 |
} |
| 82 |
|
| 83 |
public function load_insight_class() |
| 84 |
{ |
| 85 |
|
| 86 |
require_once 'class-pagespeed-insights.php'; |
| 87 |
|
| 88 |
if (!($this->insight instanceof PageSpeed_Insight)) { |
| 89 |
$this->insight = new PageSpeed_Insight($this->options['google_pagepseed_api_key']); |
| 90 |
} |
| 91 |
|
| 92 |
return $this->insight; |
| 93 |
} |
| 94 |
|
| 95 |
public function handle_adminify_page_speed() |
| 96 |
{ |
| 97 |
|
| 98 |
check_ajax_referer('__adminify-page_speed-secirity__'); |
| 99 |
|
| 100 |
if (!empty($_POST['route'])) { |
| 101 |
$route_handler = 'handle_' . $_POST['route']; |
| 102 |
if (is_callable(get_class($this), $route_handler)) $this->$route_handler($_POST); |
| 103 |
} |
| 104 |
|
| 105 |
wp_send_json_error(['message' => __('Something is wrong, no route found')], 400); |
| 106 |
} |
| 107 |
|
| 108 |
private function _get_analyze_data($url) |
| 109 |
{ |
| 110 |
|
| 111 |
$data = [ |
| 112 |
'desktop' => null, |
| 113 |
'mobile' => null |
| 114 |
]; |
| 115 |
|
| 116 |
$result = $this->insight->run_insight($url, ['strategy' => 'desktop']); |
| 117 |
if (!empty($result) && $result['responseCode'] == 200 && !empty($result['data'])) { |
| 118 |
$data['desktop'] = $result['data']; |
| 119 |
|
| 120 |
$result_mobile = $this->insight->run_insight($url, ['strategy' => 'mobile']); |
| 121 |
if (!empty($result_mobile) && $result_mobile['responseCode'] == 200 && !empty($result_mobile['data'])) { |
| 122 |
$data['mobile'] = $result_mobile['data']; |
| 123 |
return $data; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
public function handle_new_analyze($data) |
| 131 |
{ |
| 132 |
|
| 133 |
check_ajax_referer('__adminify-page_speed-secirity__'); |
| 134 |
|
| 135 |
if (empty($data['url']) || !wp_http_validate_url($data['url'])) wp_send_json_error(['message' => __('Something is wrong, URL is missing or wrong formatted')], 400); |
| 136 |
|
| 137 |
set_time_limit(300); |
| 138 |
|
| 139 |
$this->load_insight_class(); |
| 140 |
|
| 141 |
$analyze_data = $this->_get_analyze_data($data['url']); |
| 142 |
|
| 143 |
if ($analyze_data === false) wp_send_json_error(['message' => __('Couldn\'t fetch the result, Please try again later')], 503); |
| 144 |
|
| 145 |
$saved = $this->save_analyze($analyze_data); |
| 146 |
|
| 147 |
if (empty($saved)) wp_send_json_error(['message' => __('Couldn\'t save the result, Please try again later')], 500); |
| 148 |
|
| 149 |
wp_send_json_success($saved); |
| 150 |
} |
| 151 |
|
| 152 |
public function save_analyze($analyze_data) |
| 153 |
{ |
| 154 |
|
| 155 |
$data_desktop = $analyze_data['desktop']; |
| 156 |
$data_mobile = $analyze_data['mobile']; |
| 157 |
$url = $data_desktop->id; |
| 158 |
$score_desktop = $data_desktop->lighthouseResult->categories->performance->score * 100; |
| 159 |
$score_mobile = $data_mobile->lighthouseResult->categories->performance->score * 100; |
| 160 |
$screenshot = $data_desktop->lighthouseResult->audits->{'final-screenshot'}->details->data; |
| 161 |
|
| 162 |
// unset( $data_desktop->lighthouseResult->audits->{'final-screenshot'} ); |
| 163 |
// unset( $data_mobile->lighthouseResult->audits->{'final-screenshot'} ); |
| 164 |
|
| 165 |
$data = array( |
| 166 |
"url" => $url, |
| 167 |
"score_desktop" => (int) $score_desktop, |
| 168 |
"score_mobile" => (int) $score_mobile, |
| 169 |
"data_desktop" => json_encode($data_desktop), |
| 170 |
"data_mobile" => json_encode($data_mobile), |
| 171 |
"screenshot" => $screenshot, |
| 172 |
"time" => current_time('mysql'), |
| 173 |
); |
| 174 |
|
| 175 |
global $wpdb; |
| 176 |
|
| 177 |
$result = $wpdb->insert("{$wpdb->prefix}adminify_page_speed", $data, array( |
| 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 |
if (empty($result)) return false; |
| 188 |
|
| 189 |
return $wpdb->insert_id; |
| 190 |
} |
| 191 |
|
| 192 |
public function handle_count_total($data) |
| 193 |
{ |
| 194 |
check_ajax_referer('__adminify-page_speed-secirity__'); |
| 195 |
|
| 196 |
global $wpdb; |
| 197 |
|
| 198 |
$table_name = $wpdb->prefix . 'adminify_page_speed'; |
| 199 |
|
| 200 |
$total_query = "SELECT COUNT(*) FROM $table_name"; |
| 201 |
$total = $wpdb->get_var($total_query); |
| 202 |
|
| 203 |
wp_send_json_success($total); |
| 204 |
} |
| 205 |
|
| 206 |
public function handle_fetch_histories($data) |
| 207 |
{ |
| 208 |
|
| 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 |
'total' => (int) $total, |
| 233 |
'histories' => (array) $histories |
| 234 |
]); |
| 235 |
} |
| 236 |
|
| 237 |
public function handle_fetch_history($data) |
| 238 |
{ |
| 239 |
|
| 240 |
check_ajax_referer('__adminify-page_speed-secirity__'); |
| 241 |
|
| 242 |
if (empty($data['id'])) wp_send_json_error('Something is wrong, Try again later', 400); |
| 243 |
|
| 244 |
global $wpdb; |
| 245 |
|
| 246 |
$table_name = $wpdb->prefix . 'adminify_page_speed'; |
| 247 |
$history_id = $data['id']; |
| 248 |
$fields = 'id, url, score_desktop, score_mobile, data_desktop, data_mobile, time'; |
| 249 |
|
| 250 |
$history = $wpdb->get_row($wpdb->prepare("SELECT {$fields} FROM $table_name WHERE id = %d", $history_id), ARRAY_A); |
| 251 |
|
| 252 |
if (empty($history) || $wpdb->last_error !== '') wp_send_json_error($wpdb->last_error, 400); |
| 253 |
|
| 254 |
$history["data_desktop"] = json_decode($history["data_desktop"], true); |
| 255 |
$history["data_mobile"] = json_decode($history["data_mobile"], true); |
| 256 |
|
| 257 |
wp_send_json_success([ |
| 258 |
'history' => $history |
| 259 |
]); |
| 260 |
} |
| 261 |
|
| 262 |
public function handle_delete_history() |
| 263 |
{ |
| 264 |
|
| 265 |
check_ajax_referer('__adminify-page_speed-secirity__'); |
| 266 |
|
| 267 |
if (empty($_POST['ids'])) wp_send_json_error('Something is wrong, Try again later', 400); |
| 268 |
|
| 269 |
global $wpdb; |
| 270 |
|
| 271 |
$ids = implode(',', array_map('absint', $_POST['ids'])); |
| 272 |
$wpdb->query("DELETE FROM {$wpdb->prefix}adminify_page_speed WHERE ID IN($ids)"); |
| 273 |
|
| 274 |
if ($wpdb->last_error !== '') wp_send_json_error($wpdb->last_error, 400); |
| 275 |
|
| 276 |
wp_send_json_success([ |
| 277 |
'message' => 'History has been deleted' |
| 278 |
]); |
| 279 |
} |
| 280 |
|
| 281 |
public function enqueue_scripts() |
| 282 |
{ |
| 283 |
|
| 284 |
wp_enqueue_style('wp-adminify--page-speed', WP_ADMINIFY_URL . 'assets/admin/css/wp-adminify--page-speed.css', [], WP_ADMINIFY_VER); |
| 285 |
|
| 286 |
$data = [ |
| 287 |
'adminurl' => admin_url(), |
| 288 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 289 |
'nonce' => wp_create_nonce('__adminify-page_speed-secirity__'), |
| 290 |
'is_pro' => wp_validate_boolean(jltwp_adminify()->can_use_premium_code__premium_only()), |
| 291 |
'pro_notice' => Utils::adminify_upgrade_pro() |
| 292 |
]; |
| 293 |
|
| 294 |
wp_localize_script('wp-adminify--page-speed', 'wp_adminify__pagespeed_data', $data); |
| 295 |
|
| 296 |
wp_enqueue_script('wp-adminify--page-speed'); |
| 297 |
} |
| 298 |
|
| 299 |
public function maybe_create_tables() |
| 300 |
{ |
| 301 |
|
| 302 |
global $wpdb; |
| 303 |
|
| 304 |
// delete_option( "{$wpdb->prefix}adminify_page_speed_db_version" ); |
| 305 |
|
| 306 |
$db_version = '1.0'; |
| 307 |
|
| 308 |
if (get_option("{$wpdb->prefix}adminify_page_speed_db_version") == $db_version) return; // vail early |
| 309 |
|
| 310 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 311 |
|
| 312 |
$sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}adminify_page_speed ( |
| 313 |
id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT, |
| 314 |
url VARCHAR(255) NOT NULL, |
| 315 |
score_desktop VARCHAR(3) NOT NULL, |
| 316 |
score_mobile VARCHAR(3) NOT NULL, |
| 317 |
data_desktop MEDIUMTEXT NOT NULL, |
| 318 |
data_mobile MEDIUMTEXT NOT NULL, |
| 319 |
screenshot MEDIUMTEXT NOT NULL, |
| 320 |
time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 321 |
PRIMARY KEY (id) |
| 322 |
)" . $wpdb->get_charset_collate() . ";"; |
| 323 |
|
| 324 |
if (get_option("{$wpdb->prefix}adminify_page_speed_db_version") < $db_version) { |
| 325 |
dbDelta($sql); |
| 326 |
} |
| 327 |
|
| 328 |
update_option("{$wpdb->prefix}adminify_page_speed_db_version", $db_version); |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|