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