| 1 |
<?php |
| 2 |
/* |
| 3 |
+=====================================================================+ |
| 4 |
| ____ _ ____ __ _ _ | |
| 5 |
| / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ | |
| 6 |
| | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 |
| | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 |
| \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 |
| | |
| 10 |
| (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ | |
| 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 |
$label = []; |
| 59 |
$data = ''; |
| 60 |
$theme = esc_attr__('theme', 'code-profiler'); |
| 61 |
$total_time = 0; |
| 62 |
$hidden = 0; |
| 63 |
$count = 0; |
| 64 |
// Sort by value |
| 65 |
usort( $buffer, function( $a, $b ) { |
| 66 |
return $b[1] <=> $a[1]; |
| 67 |
} ); |
| 68 |
foreach( $buffer as $k => $slugs ) { |
| 69 |
|
| 70 |
if ( empty( $slugs[1] ) && ! empty( $cp_options['hide_empty_value'] ) ) { |
| 71 |
$hidden++; |
| 72 |
$hidden_empty = 1; |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
$count++; |
| 77 |
if ( $count > $chart_max_plugins ) { |
| 78 |
$hidden++; |
| 79 |
$hidden_max_plugins = 1; |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
// Check whether we should use the name or the slug |
| 84 |
if ( $cp_options['display_name'] == 'slug') { |
| 85 |
$n = $slugs[0]; |
| 86 |
} else { |
| 87 |
$n = $slugs[2]; |
| 88 |
} |
| 89 |
|
| 90 |
// Truncate and sanitise the name |
| 91 |
if ( strlen( $n ) > $cp_options['truncate_name'] ) { |
| 92 |
$n = mb_substr( $n, 0, $cp_options['truncate_name'] , 'utf-8') .'...'; |
| 93 |
} |
| 94 |
|
| 95 |
// Mark theme as such |
| 96 |
if ( $slugs[3] == 'theme') { |
| 97 |
$label[] = "{$n} ($theme)"; |
| 98 |
// ...and MU plugins |
| 99 |
} elseif ( $slugs[3] == 'mu-plugin') { |
| 100 |
$label[] = "{$n} (MU)"; |
| 101 |
} else { |
| 102 |
$label[] = $n; |
| 103 |
} |
| 104 |
|
| 105 |
$data .= "{$slugs[1]},"; |
| 106 |
$total_time += $slugs[1]; |
| 107 |
} |
| 108 |
$data = rtrim( $data, ','); |
| 109 |
$count = count( $label ) - 1; |
| 110 |
?> |
| 111 |
<div style="width:80vw;margin:auto" aria-hidden="true"> |
| 112 |
<canvas id="myChart"></canvas> |
| 113 |
</div> |
| 114 |
<script> |
| 115 |
jQuery(document).ready(function() { |
| 116 |
'use strict'; |
| 117 |
cpjs_plugins_chart( |
| 118 |
'<?php echo esc_js( $axis ) ?>', [<?php |
| 119 |
for( $i = 0; $i < $count; $i++ ) { |
| 120 |
echo "'". addslashes( $label[ $i ] ) ."',"; |
| 121 |
} |
| 122 |
echo "'". esc_js( $label[ $i ] ) ."'"; |
| 123 |
?>], |
| 124 |
[<?php echo esc_js( $data ) ?>], <?php echo esc_js( number_format( $total_time, 4 ) ) ?> |
| 125 |
); |
| 126 |
}); |
| 127 |
</script> |
| 128 |
|
| 129 |
<!-- Screen-reader only --> |
| 130 |
<div class="visually-hidden"> |
| 131 |
<?php |
| 132 |
$sr_data = explode(',', $data ); |
| 133 |
echo "<table>\n". |
| 134 |
"\t<tr>\n". |
| 135 |
"\t\t<td>". esc_html__('Component name', 'code-profiler') ."</td>". |
| 136 |
"<td>". esc_html__('Time in seconds', 'code-profiler') ."</td>". |
| 137 |
"<td>". esc_html__('Time in percentage', 'code-profiler') ."</td>\n"; |
| 138 |
for( $i = 0; $i <= $count; $i++ ) { |
| 139 |
if ( empty( $sr_data[ $i ] ) ) { |
| 140 |
$sr_data[ $i ] = 0; |
| 141 |
} |
| 142 |
echo "\t<tr>\n\t\t<td>". esc_html( $label[ $i ] ) ."</td>". |
| 143 |
"<td>". esc_html( number_format( $sr_data[ $i ], 3 ) ) ."</td>". |
| 144 |
"<td>". esc_html( number_format( ( $sr_data[ $i ] / $total_time ) * 100 ) ) . |
| 145 |
"%</td>\n\t</tr>\n"; |
| 146 |
} |
| 147 |
echo "</table>\n"; |
| 148 |
?> |
| 149 |
</div> |
| 150 |
<!-- End of Screen-reader only --> |
| 151 |
|
| 152 |
<?php |
| 153 |
// Actions below stats |
| 154 |
$save_png = 1; |
| 155 |
$rotate_img = 1; |
| 156 |
$type = 'slugs'; |
| 157 |
|
| 158 |
// ===================================================================== |
| 159 |
// EOF |
| 160 |
|