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