PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.6.3
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.6.3
1.9.5 1.9.4 1.9.3 trunk 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 All 40 releases
code-profiler / lib / menu_view_plugins.php

menu_view_plugins.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy 1.6.3, at lib/menu_view_plugins.php

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