PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / trunk
Code Profiler – WordPress Performance Profiling and Debugging Made Easy vtrunk
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 trunk, at index.php

305 lines 9.7 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.9.5
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.9.5');
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/class-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 // =====================================================================
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 // =====================================================================
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 'missing_wpcron' =>
216 esc_attr__('There are no cron events available, please select another page to profile.',
217 'code-profiler'),
218 'multiline_raw' =>
219 esc_attr__('The raw POST payload should be on one single line only.', 'code-profiler'),
220 'missing_post' =>
221 esc_attr__('Please select a page to profile.', 'code-profiler'),
222 'unknown_error' =>
223 esc_attr__('Unknown error returned by AJAX', 'code-profiler'),
224 'http_error' =>
225 esc_attr__('The HTTP server returned the following error:', 'code-profiler'),
226 'timeout_error' =>
227 esc_attr__('You can try to select a lower Accuracy and Precision level in the Settings page',
228 'code-profiler'),
229 'notfound_error' =>
230 esc_attr__('The requested page does not seem to exist. Please check the syntax of the URL '.
231 'you want to profile',
232 'code-profiler'),
233 'forbidden_error' =>
234 esc_attr__('The server rejected and blocked the requested page. Make sure you are allowed '.
235 'to access that page or that there is no security plugin or application blocking it',
236 'code-profiler'),
237 'internal_error' =>
238 esc_attr__('This is an internal server error. Please check your server, PHP and Code '.
239 'Profiler logs as they may contain more details about the error', 'code-profiler'),
240 'unknown_error' =>
241 esc_attr__('An unknown error occurred:', 'code-profiler'),
242 'preparing_report' =>
243 esc_attr__('Preparing the report', 'code-profiler') .'...',
244 'empty_log' =>
245 esc_attr__('No records were found that match the specified search criteria.',
246 'code-profiler'),
247 'delete_log' =>
248 esc_attr__('Delete log?', 'code-profiler'),
249 'delete_profile' =>
250 esc_attr__('Delete this profile?', 'code-profiler'),
251 // Charts
252 'exec_sec_plugins' =>
253 esc_attr__('Execution time in seconds', 'code-profiler'),
254 'pc_plugins' =>
255 /* Translators: xx% of all plugins and the theme */
256 esc_attr__('% of all plugins and the theme', 'code-profiler'),
257 'exec_tot_plugins_1' =>
258 esc_attr__('Plugins and theme execution time, in seconds', 'code-profiler'),
259 'chart_total' =>
260 esc_attr__('total:', 'code-profiler'),
261 'iolist_total_calls' =>
262 esc_attr__('Total calls', 'code-profiler'),
263 'io_calls' =>
264 esc_attr__('File I/O operations', 'code-profiler'),
265 'disk_io_bytes' =>
266 esc_attr__('Total bytes', 'code-profiler'),
267 'disk_io_title' =>
268 esc_attr__('Total Disk I/O read and write, in bytes', 'code-profiler'),
269 'text_copied' =>
270 esc_attr__('The text was successfully copied to the clipboard.', 'code-profiler')
271
272 ];
273 wp_localize_script(
274 'code_profiler_javascript',
275 'cpi18n',
276 $code_profiler_i18n
277 );
278 }
279
280 add_action('admin_enqueue_scripts', 'code_profiler_enqueue');
281
282 // ===================================================================== 2023-08-20
283 // Display the settings link in the "Plugins" page.
284
285 function code_profiler_settings_link( $links ) {
286
287 $links[] = '<a href="'. get_admin_url( null, 'admin.php?page=code-profiler') .'">'.
288 esc_html__('Start Profiling', 'code-profiler'). '</a>';
289 $links[] = '<a style="font-weight:700;color:#006393;" href="https://nintechnet.com/codeprofiler/" '.
290 'target="_blank" rel="noopener noreferrer">'.
291 esc_html__('Go Pro', 'code-profiler'). '</a>';
292 return $links;
293 }
294
295 add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'code_profiler_settings_link');
296
297 // ===================================================================== 2023-08-20
298 // WP CLI commands.
299
300 if ( defined('WP_CLI') && WP_CLI ) {
301 require_once __DIR__ . '/lib/class-cli.php';
302 }
303 // =====================================================================
304 // EOF
305