PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.2
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / class-wp-optimize-performance.php

class-wp-optimize-performance.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.2, at includes/class-wp-optimize-performance.php

169 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('No direct access allowed');
3
4 if (!class_exists('WP_Optimize_Performance')) :
5
6 class WP_Optimize_Performance {
7
8 /**
9 * WP_Optimize_404_Detector instance
10 *
11 * @var WP_Optimize_404_Detector
12 */
13 private $obj_404_detector;
14
15 /**
16 * Constructor
17 *
18 * @param WP_Optimize_404_Detector $detector
19 */
20 private function __construct($detector) {
21 $this->obj_404_detector = $detector;
22 }
23
24 /**
25 * Returns singleton instance
26 *
27 * @param WP_Optimize_404_Detector $detector Helper class to be able to call the 404 request detector
28 * @return WP_Optimize_Performance
29 */
30 public static function get_instance($detector) {
31 static $instance = null;
32 if (null === $instance) {
33 $instance = new WP_Optimize_Performance($detector);
34 }
35 return $instance;
36 }
37
38 /**
39 * Hook content generators
40 *
41 * @return void
42 */
43 public function init() {
44
45 add_filter('site_health_navigation_tabs', array($this, 'add_health_tab'));
46
47 add_action('site_health_tab_content', array($this, 'site_health_tab_content'));
48
49 add_action('wp_dashboard_setup', array($this, 'add_dashboard_widgets'));
50
51 // Network admin dashboard (multisite)
52 if (is_multisite()) {
53 add_action('wp_network_dashboard_setup', array($this, 'add_dashboard_widgets'));
54 }
55 }
56
57 /**
58 * Hook the handler class that will manage 404 requests
59 *
60 * @return void
61 */
62 public function hook_404_handler() {
63 add_action('template_redirect', array($this, 'maybe_handle_404_request'));
64 }
65
66 /**
67 * Detect if request is a 404 not found error, and handle it using WP_Optimize_404_Detector class
68 *
69 * @return void
70 */
71 public function maybe_handle_404_request() {
72 if (is_404()) {
73 $this->obj_404_detector->handle_request();
74 }
75 }
76
77 /**
78 * Add widget to admin dashboard
79 *
80 * @return void
81 */
82 public function add_dashboard_widgets() {
83 wp_add_dashboard_widget('wp_optimize_performance', __('Performance', 'wp-optimize'), array($this, 'print_dashboard_widget'));
84 }
85
86 /**
87 * Generate the admin widget content
88 *
89 * @return string
90 */
91 private function dashboard_widget() {
92 $html = $this->get_404_requests_summary();
93
94 if (empty($html)) {
95 $html = esc_html__('No performance issues found', 'wp-optimize');
96 }
97
98 return $html;
99 }
100
101 /**
102 * Build an HTML summary to be shown in a dashboard widget
103 *
104 * @return string
105 */
106 private function get_404_requests_summary() {
107 $detector = $this->obj_404_detector;
108
109 $counts = $detector->get_suspicious_requests_count();
110 if ($counts['total_requests'] > $detector->get_dashboard_alert_request_count_threshold()) {
111 $url_count = $counts['unique_urls'];
112 $total_count = $counts['total_requests'];
113
114 $admin_url = is_multisite() ? network_admin_url('admin.php') : admin_url('admin.php');
115
116 $performance_url = esc_url(add_query_arg(array('page' => 'wpo_performance'), $admin_url));
117
118 $url_label = _n('URL', 'URLs', $url_count, 'wp-optimize');
119
120 return sprintf(
121 // translators: %1$d is count of unique URLs, %2$s is "URL" or "URLs", %3$d is count of 404 requests, %4$s is the performance page URL.
122 __('<b>%1$d</b> %2$s generated <b>%3$d</b> 404 Not Found requests <a href="%4$s">Check</a>', 'wp-optimize'),
123 $url_count,
124 esc_html($url_label),
125 $total_count,
126 $performance_url
127 );
128 }
129
130 return "";
131 }
132
133 /**
134 * Actually print the widget contents
135 *
136 * @return void
137 */
138 public function print_dashboard_widget() {
139 echo $this->dashboard_widget(); // phpcs:ignore WordPress.Security.EscapeOutput -- Output is already escaped
140 }
141
142 /**
143 * Add a tab on the site health check
144 *
145 * @param array $tabs
146 * @return array
147 */
148 public function add_health_tab($tabs) {
149 $tabs['wpo_performance'] = 'WP-Optimize ' . esc_html__('Performance', 'wp-optimize');
150
151 return $tabs;
152 }
153
154 /**
155 * Display information in the site health report
156 *
157 * @return void
158 */
159 public function site_health_tab_content($tab) {
160 if ('wpo_performance' === $tab) {
161 WP_Optimize()->include_template('performance/site-health.php', false, array(
162 'alerts' => $this->dashboard_widget()
163 ));
164 }
165 }
166 }
167
168 endif;
169