PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.8
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.8
1.9.5 1.9.4 1.9.3 trunk 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 All 40 releases
code-profiler / index.php

index.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy 1.8, at index.php

302 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 Plugin Name: Code Profiler
4 Plugin URI: https://nintechnet.com/codeprofiler/
5 Description: A profiler to measure the performance of your WordPress plugins and themes.
6 Author: Jerome Bruandet ~ NinTechNet Ltd.
7 Author URI: https://nintechnet.com/
8 Version: 1.8
9 Network: true
10 License: GPLv3 or later
11 Text Domain: code-profiler
12 Domain Path: /languages
13 */
14
15 define('CODE_PROFILER_VERSION', '1.8');
16 /**
17 +=====================================================================+
18 | ____ _ ____ __ _ _ |
19 | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ |
20 | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| |
21 | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | |
22 | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| |
23 | |
24 | (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ |
25 +=====================================================================+
26 */
27
28 if (! defined('ABSPATH') ) {
29 die('Forbidden');
30 }
31
32 // =====================================================================
33 // Menu functions
34 require __DIR__ .'/lib/menu.php';
35 // Helper (can be already loaded by the MU plugin)
36 require_once __DIR__ .'/lib/helper.php';
37 // AJAX calls
38 require __DIR__ .'/lib/ajax.php';
39 // Scheduled tasks
40 require_once __DIR__ .'/lib/class-wp-cron.php';
41 // =====================================================================
42 // Activation: make sure the blog meets the requirements.
43
44 function code_profiler_activate() {
45
46 if (! defined('WP_CLI') && ! current_user_can('activate_plugins') ) {
47 exit( esc_html__('Your are not allowed to activate plugins.', 'code-profiler') );
48 }
49
50 global $wp_version;
51 if ( version_compare( $wp_version, '5.0', '<') ) {
52 exit( sprintf(
53 esc_html__('Code Profiler requires WordPress %s or greater but your current version is %s.',
54 'code-profiler'),
55 '5.0',
56 esc_html( $wp_version )
57 ) );
58 }
59
60 if ( version_compare( PHP_VERSION, '7.1', '<') ) {
61 exit( sprintf(
62 esc_html__('Code Profiler requires PHP 7.1 or greater but your current version is %s.',
63 'code-profiler'),
64 esc_html( PHP_VERSION )
65 ) );
66 }
67
68 // If the pro version is active, we refuse to run and throw an error message
69 if ( is_plugin_active('code-profiler-pro/index.php') ) {
70 exit( esc_html__('Code Profiler Pro is active on this site, please disable it first if you '.
71 'want to run the free version.', 'code-profiler')
72 );
73 }
74
75 // Verify/create our storage folder
76 code_profiler_check_uploadsdir();
77
78 // If we don't have any options yet, create them
79 $cp_options = get_option('code-profiler');
80 if ( $cp_options === false ) {
81 code_profiler_default_options();
82 }
83
84 /**
85 * Install scheduled tasks on the main site only.
86 */
87 if ( is_main_site() ) {
88 CodeProfiler_WPCron::install();
89 }
90 }
91
92 register_activation_hook( __FILE__, 'code_profiler_activate');
93
94 // ===================================================================== 2023-06-17
95 // Deactivation.
96
97 function code_profiler_deactivate() {
98
99 if (! defined('WP_CLI') && ! current_user_can('activate_plugins') ) {
100 exit( esc_html__('Your are not allowed to deactivate plugins.', 'code-profiler') );
101 }
102
103 // Remove the MU plugin
104 if ( file_exists( WPMU_PLUGIN_DIR .'/'. CODE_PROFILER_MUPLUGIN ) ) {
105 unlink( WPMU_PLUGIN_DIR .'/'. CODE_PROFILER_MUPLUGIN );
106 }
107
108 /**
109 * Uninstall scheduled tasks.
110 */
111 CodeProfiler_WPCron::uninstall();
112 }
113
114 register_deactivation_hook( __FILE__, 'code_profiler_deactivate');
115
116 // ===================================================================== 2023-06-14
117 // Create Profiler's menu.
118
119 function code_profiler_admin_menu() {
120
121 // In a MU environment, only the superadmin can run Code Profiler
122 if (! is_super_admin() ) {
123 return;
124 }
125
126 add_menu_page(
127 'Code Profiler',
128 'Code Profiler',
129 'manage_options',
130 'code-profiler',
131 'code_profiler_main_menu',
132 plugins_url('/static/dashicon-16x16.png', __FILE__ )
133 );
134 }
135
136 add_action('admin_menu', 'code_profiler_admin_menu');
137
138 // ===================================================================== 2023-08-20
139 // Register scripts and styles.
140
141 function code_profiler_enqueue( $hook ) {
142
143 if (! is_super_admin() ) {
144 return;
145 }
146
147 // Load files only if we're in Code Profiler's main page
148 if ( $hook != 'toplevel_page_code-profiler') {
149 return;
150 }
151
152 // We load Thickbox if we're viewing section 4 only
153 if ( isset( $_REQUEST['section'] ) && $_REQUEST['section'] == 4 ) {
154 $extra_js = ['jquery', 'thickbox'];
155 $extra_css = ['thickbox'];
156 } else {
157 $extra_js = ['jquery'];
158 $extra_css = null;
159 }
160
161 wp_enqueue_script(
162 'code_profiler_javascript',
163 plugin_dir_url( __FILE__ ) .'static/code-profiler.js',
164 $extra_js,
165 CODE_PROFILER_VERSION
166 );
167
168 wp_enqueue_script(
169 'code-profiler_tiptip',
170 plugin_dir_url( __FILE__ ) .'static/vendor/jquery.tipTip.js',
171 ['jquery'],
172 CODE_PROFILER_VERSION
173 );
174
175 wp_enqueue_style(
176 'code-profiler_style',
177 plugin_dir_url( __FILE__ ) .'static/code-profiler.css',
178 $extra_css,
179 CODE_PROFILER_VERSION
180 );
181
182 // Enqueue Chart.js if we're viewing a profile
183 if ( isset( $_REQUEST['action'] ) &&
184 $_REQUEST['action'] == 'view_profile') {
185
186 wp_enqueue_script(
187 'code_profiler_charts',
188 plugin_dir_url( __FILE__ ) .'static/vendor/chart.min.js',
189 ['jquery'],
190 CODE_PROFILER_VERSION,
191 // We load it in the footer, because some plugins loads it too
192 // on every pages and that could mess with our pages
193 true
194 );
195 }
196
197 // JS i18n
198 $code_profiler_i18n = [
199 'missing_nonce' =>
200 esc_attr__('Security nonce is missing, try to reload the page.', 'code-profiler'),
201 'missing_frontbackend' =>
202 esc_attr__('Please select either the fontend or backend option.', 'code-profiler'),
203 'missing_profileid' =>
204 esc_attr__('Missing profile identifier.', 'code-profiler'),
205 'missing_profilename' =>
206 esc_attr__('Please enter a name for this profile.', 'code-profiler'),
207 'missing_userauth' =>
208 esc_attr__('Please select whether the profiler should run as an authenticated user or not.',
209 'code-profiler'),
210 'missing_username' =>
211 esc_attr__('Please enter the name of the user.', 'code-profiler'),
212 'missing_ajax' =>
213 esc_attr__('Please enter a valid JSON-encoded payload, or change the content-type option. '.
214 'If you want to send an empty value, enter: {}', 'code-profiler'),
215 'multiline_raw' =>
216 esc_attr__('The raw POST payload should be on one single line only.', 'code-profiler'),
217 'missing_post' =>
218 esc_attr__('Please select a page to profile.', 'code-profiler'),
219 'unknown_error' =>
220 esc_attr__('Unknown error returned by AJAX', 'code-profiler'),
221 'http_error' =>
222 esc_attr__('The HTTP server returned the following error:', 'code-profiler'),
223 'timeout_error' =>
224 esc_attr__('You can try to select a lower Accuracy and Precision level in the Settings page',
225 'code-profiler'),
226 'notfound_error' =>
227 esc_attr__('The requested page does not seem to exist. Please check the syntax of the URL '.
228 'you want to profile',
229 'code-profiler'),
230 'forbidden_error' =>
231 esc_attr__('The server rejected and blocked the requested page. Make sure you are allowed '.
232 'to access that page or that there is no security plugin or application blocking it',
233 'code-profiler'),
234 'internal_error' =>
235 esc_attr__('This is an internal server error. Please check your server, PHP and Code '.
236 'Profiler logs as they may contain more details about the error', 'code-profiler'),
237 'unknown_error' =>
238 esc_attr__('An unknown error occurred:', 'code-profiler'),
239 'preparing_report' =>
240 esc_attr__('Preparing the report', 'code-profiler') .'...',
241 'empty_log' =>
242 esc_attr__('No records were found that match the specified search criteria.',
243 'code-profiler'),
244 'delete_log' =>
245 esc_attr__('Delete log?', 'code-profiler'),
246 'delete_profile' =>
247 esc_attr__('Delete this profile?', 'code-profiler'),
248 // Charts
249 'exec_sec_plugins' =>
250 esc_attr__('Execution time in seconds', 'code-profiler'),
251 'pc_plugins' =>
252 /* Translators: xx% of all plugins and the theme */
253 esc_attr__('% of all plugins and the theme', 'code-profiler'),
254 'exec_tot_plugins_1' =>
255 esc_attr__('Plugins and theme execution time, in seconds', 'code-profiler'),
256 'chart_total' =>
257 esc_attr__('total:', 'code-profiler'),
258 'iolist_total_calls' =>
259 esc_attr__('Total calls', 'code-profiler'),
260 'io_calls' =>
261 esc_attr__('File I/O operations', 'code-profiler'),
262 'disk_io_bytes' =>
263 esc_attr__('Total bytes', 'code-profiler'),
264 'disk_io_title' =>
265 esc_attr__('Total Disk I/O read and write, in bytes', 'code-profiler'),
266 'text_copied' =>
267 esc_attr__('The text was successfully copied to the clipboard.', 'code-profiler')
268
269 ];
270 wp_localize_script(
271 'code_profiler_javascript',
272 'cpi18n',
273 $code_profiler_i18n
274 );
275 }
276
277 add_action('admin_enqueue_scripts', 'code_profiler_enqueue');
278
279 // ===================================================================== 2023-08-20
280 // Display the settings link in the "Plugins" page.
281
282 function code_profiler_settings_link( $links ) {
283
284 $links[] = '<a href="'. get_admin_url( null, 'admin.php?page=code-profiler') .'">'.
285 esc_html__('Start Profiling', 'code-profiler'). '</a>';
286 $links[] = '<a style="font-weight:700;color:#006393;" href="https://nintechnet.com/codeprofiler/" '.
287 'target="_blank" rel="noopener noreferrer">'.
288 esc_html__('Go Pro', 'code-profiler'). '</a>';
289 return $links;
290 }
291
292 add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'code_profiler_settings_link');
293
294 // ===================================================================== 2023-08-20
295 // WP CLI commands.
296
297 if ( defined('WP_CLI') && WP_CLI ) {
298 require_once __DIR__ . '/lib/class-cli.php';
299 }
300 // =====================================================================
301 // EOF
302