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