| 1 |
<?php |
| 2 |
/* |
| 3 |
+=====================================================================+ |
| 4 |
| ____ _ ____ __ _ _ | |
| 5 |
| / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ | |
| 6 |
| | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 |
| | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 |
| \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 |
| | |
| 10 |
| (c) Jerome Bruandet ~ https://code-profiler.com/ | |
| 11 |
+=====================================================================+ |
| 12 |
*/ |
| 13 |
|
| 14 |
if (! defined('ABSPATH') ) { |
| 15 |
die('Forbidden'); |
| 16 |
} |
| 17 |
|
| 18 |
// ===================================================================== 2023-11-17 |
| 19 |
// Display Plugins & Theme stats. |
| 20 |
|
| 21 |
$buffer = code_profiler_get_profile_data( $profile_path, 'slugs'); |
| 22 |
if ( isset( $buffer['error'] ) ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
// Fetch options |
| 27 |
$cp_options = get_option('code-profiler'); |
| 28 |
|
| 29 |
// Name vs slug |
| 30 |
if ( empty( $cp_options['display_name'] ) || |
| 31 |
! in_array( $cp_options['display_name'], ['full', 'slug' ] ) ) { |
| 32 |
|
| 33 |
$cp_options['display_name'] = 'full'; |
| 34 |
} |
| 35 |
// Truncate name |
| 36 |
if ( empty( $cp_options['truncate_name'] ) || |
| 37 |
! preg_match('/^\d+$/', ( $cp_options['truncate_name'] ) ) ) { |
| 38 |
|
| 39 |
$cp_options['truncate_name'] = 30; |
| 40 |
} |
| 41 |
// Horizontal vs vertical chart |
| 42 |
if ( empty( $cp_options['chart_type'] ) || |
| 43 |
! in_array( $cp_options['chart_type'], ['x', 'y' ] ) ) { |
| 44 |
|
| 45 |
$axis = 'x'; |
| 46 |
} else { |
| 47 |
$axis = $cp_options['chart_type']; |
| 48 |
} |
| 49 |
// Max plugins to display |
| 50 |
if ( empty( $cp_options['chart_max_plugins'] ) || |
| 51 |
! preg_match('/^\d+$/', ( $cp_options['chart_max_plugins'] ) ) ) { |
| 52 |
|
| 53 |
$chart_max_plugins = 25; |
| 54 |
} else { |
| 55 |
$chart_max_plugins = $cp_options['chart_max_plugins']; |
| 56 |
} |
| 57 |
|
| 58 |
// Check if we should warn about composer |
| 59 |
if (! empty( $cp_options['warn_composer'] ) ) { |
| 60 |
$composer_warning = code_profiler_composer_warning( $profile_path, $cp_options['display_name'] ); |
| 61 |
} |
| 62 |
|
| 63 |
$label = []; |
| 64 |
$data = ''; |
| 65 |
$theme = esc_attr__('theme', 'code-profiler'); |
| 66 |
$total_time = 0; |
| 67 |
$hidden = 0; |
| 68 |
$count = 0; |
| 69 |
// Sort by value |
| 70 |
usort( $buffer, function( $a, $b ) { |
| 71 |
return $b[1] <=> $a[1]; |
| 72 |
} ); |
| 73 |
foreach( $buffer as $k => $slugs ) { |
| 74 |
|
| 75 |
if ( empty( $slugs[1] ) && ! empty( $cp_options['hide_empty_value'] ) ) { |
| 76 |
$hidden++; |
| 77 |
$hidden_empty = 1; |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
$count++; |
| 82 |
if ( $count > $chart_max_plugins ) { |
| 83 |
$hidden++; |
| 84 |
$hidden_max_plugins = 1; |
| 85 |
continue; |
| 86 |
} |
| 87 |
|
| 88 |
// Check whether we should use the name or the slug |
| 89 |
if ( $cp_options['display_name'] == 'slug') { |
| 90 |
$n = $slugs[0]; |
| 91 |
} else { |
| 92 |
$n = $slugs[2]; |
| 93 |
} |
| 94 |
|
| 95 |
// Truncate and sanitise the name |
| 96 |
if ( strlen( $n ) > $cp_options['truncate_name'] ) { |
| 97 |
$n = mb_substr( $n, 0, $cp_options['truncate_name'] , 'utf-8') .'...'; |
| 98 |
} |
| 99 |
|
| 100 |
// Mark theme as such |
| 101 |
if ( $slugs[3] == 'theme') { |
| 102 |
$label[] = "{$n} ($theme)"; |
| 103 |
// ...and MU plugins |
| 104 |
} elseif ( $slugs[3] == 'mu-plugin') { |
| 105 |
$label[] = "{$n} (MU)"; |
| 106 |
} else { |
| 107 |
$label[] = $n; |
| 108 |
} |
| 109 |
|
| 110 |
$data .= "{$slugs[1]},"; |
| 111 |
$total_time += $slugs[1]; |
| 112 |
} |
| 113 |
$data = rtrim( $data, ','); |
| 114 |
$count = count( $label ) - 1; |
| 115 |
?> |
| 116 |
<div style="width:80vw;margin:auto" aria-hidden="true"> |
| 117 |
<canvas id="myChart"></canvas> |
| 118 |
</div> |
| 119 |
<script> |
| 120 |
jQuery(document).ready(function() { |
| 121 |
'use strict'; |
| 122 |
cpjs_plugins_chart( |
| 123 |
'<?php echo esc_js( $axis ) ?>', [<?php |
| 124 |
for( $i = 0; $i < $count; $i++ ) { |
| 125 |
echo "'". addslashes( $label[ $i ] ) ."',"; |
| 126 |
} |
| 127 |
echo "'". esc_js( $label[ $i ] ) ."'"; |
| 128 |
?>], |
| 129 |
[<?php echo esc_js( $data ) ?>], <?php echo esc_js( number_format( $total_time, 4 ) ) ?> |
| 130 |
); |
| 131 |
}); |
| 132 |
</script> |
| 133 |
|
| 134 |
<!-- Screen-reader only --> |
| 135 |
<div class="visually-hidden"> |
| 136 |
<?php |
| 137 |
$sr_data = explode(',', $data ); |
| 138 |
echo "<table>\n". |
| 139 |
"\t<tr>\n". |
| 140 |
"\t\t<td>". esc_html__('Component name', 'code-profiler') ."</td>". |
| 141 |
"<td>". esc_html__('Time in seconds', 'code-profiler') ."</td>". |
| 142 |
"<td>". esc_html__('Time in percentage', 'code-profiler') ."</td>\n"; |
| 143 |
for( $i = 0; $i <= $count; $i++ ) { |
| 144 |
if ( empty( $sr_data[ $i ] ) ) { |
| 145 |
$sr_data[ $i ] = 0; |
| 146 |
} |
| 147 |
echo "\t<tr>\n\t\t<td>". esc_html( $label[ $i ] ) ."</td>". |
| 148 |
"<td>". esc_html( number_format( $sr_data[ $i ], 3 ) ) ."</td>". |
| 149 |
"<td>". esc_html( number_format( ( $sr_data[ $i ] / $total_time ) * 100 ) ) . |
| 150 |
"%</td>\n\t</tr>\n"; |
| 151 |
} |
| 152 |
echo "</table>\n"; |
| 153 |
?> |
| 154 |
</div> |
| 155 |
<!-- End of Screen-reader only --> |
| 156 |
|
| 157 |
<?php |
| 158 |
// Actions below stats |
| 159 |
$save_png = 1; |
| 160 |
$rotate_img = 1; |
| 161 |
$type = 'slugs'; |
| 162 |
|
| 163 |
// ===================================================================== 2023-11-17 |
| 164 |
// Warn if multiple plugins are using composer |
| 165 |
|
| 166 |
function code_profiler_composer_warning( $profile_path, $display_name ) { |
| 167 |
|
| 168 |
if (! file_exists( "$profile_path.composer.profile" ) ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
$res = json_decode( file_get_contents( "$profile_path.composer.profile" ), true ); |
| 172 |
// Make sure there are at least two items (free version only) |
| 173 |
if ( $res === false || count( $res ) < 2 ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$list = ''; |
| 178 |
$first = ''; |
| 179 |
$count = 1; |
| 180 |
foreach( $res as $slug => $name ) { |
| 181 |
if ( $display_name == 'full') { |
| 182 |
$list .= "$count: <strong>". esc_html( $name ) .'</strong>, '; |
| 183 |
if ( empty( $first ) ) { |
| 184 |
$first = '<strong>'. esc_html( $name ) .'</strong>'; |
| 185 |
} |
| 186 |
} else { |
| 187 |
$list .= "$count: <strong>". esc_html( $slug ) .'</strong>, '; |
| 188 |
if ( empty( $first ) ) { |
| 189 |
$first = '<strong>'. esc_html( $slug ) .'</strong>'; |
| 190 |
} |
| 191 |
} |
| 192 |
$count++; |
| 193 |
} |
| 194 |
$list = rtrim( $list, ', ') .'.'; |
| 195 |
|
| 196 |
$msg = esc_html__('Code Profiler has detected that the following components, sorted by execution'. |
| 197 |
' order, are using Composer dependency manager:', 'code-profiler') . "<br />$list<br />". |
| 198 |
sprintf( |
| 199 |
esc_html__( |
| 200 |
'As that may increase the execution time of %s, make sure to consult the following FAQ: '. |
| 201 |
'%s%s%s', 'code-profiler' |
| 202 |
), |
| 203 |
$first, |
| 204 |
'<a href="?page=code-profiler&cptab=faq#composerwarning" target="_blank" rel="noopener '. |
| 205 |
'noreferrer">', |
| 206 |
esc_html__( |
| 207 |
'Why does Code Profiler warn me that I have multiple plugins using Composer?', |
| 208 |
'code-profiler' |
| 209 |
), |
| 210 |
'</a>' |
| 211 |
); |
| 212 |
|
| 213 |
return '<div class="cp-notice cp-notice-orange"><p>'. $msg .'</p></div>'; |
| 214 |
|
| 215 |
} |
| 216 |
// ===================================================================== |
| 217 |
// EOF |
| 218 |
|