PluginProbe ʕ •ᴥ•ʔ
WPCode – Insert Headers and Footers + Custom Code Snippets – WordPress Code Manager / 2.2.4
WPCode – Insert Headers and Footers + Custom Code Snippets – WordPress Code Manager v2.2.4
2.3.6 trunk 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.5.0 1.6.0 1.6.1 1.6.2 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.13.1 2.0.2 2.0.3 2.0.4 2.0.4.1 2.0.4.2 2.0.4.3 2.0.4.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.2 2.1.3 2.1.3.1 2.1.4 2.1.4.1 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.2.4 2.2.4.1 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.2.1 2.3.3 2.3.4 2.3.5
insert-headers-and-footers / includes / class-wpcode-admin-bar-info.php
insert-headers-and-footers / includes Last commit date
admin 1 year ago auto-insert 1 year ago conditional-logic 1 year ago execute 1 year ago generator 2 years ago lite 1 year ago capabilities.php 2 years ago class-wpcode-admin-bar-info.php 2 years ago class-wpcode-auto-insert.php 1 year ago class-wpcode-capabilities.php 3 years ago class-wpcode-conditional-logic.php 1 year ago class-wpcode-error.php 2 years ago class-wpcode-file-cache.php 1 year ago class-wpcode-file-logger.php 3 years ago class-wpcode-generator.php 3 years ago class-wpcode-install.php 2 years ago class-wpcode-library-auth.php 1 year ago class-wpcode-library.php 1 year ago class-wpcode-settings.php 2 years ago class-wpcode-smart-tags.php 2 years ago class-wpcode-snippet-cache.php 2 years ago class-wpcode-snippet-execute.php 1 year ago class-wpcode-snippet.php 1 year ago compat.php 2 years ago global-output.php 2 years ago helpers.php 1 year ago icons.php 1 year ago ihaf.php 3 years ago legacy.php 3 years ago pluggable.php 2 years ago post-type.php 1 year ago safe-mode.php 2 years ago shortcode.php 2 years ago
class-wpcode-admin-bar-info.php
500 lines
1 <?php
2 /**
3 * This class adds the WPCode info to the admin bar for logged-in administrator users
4 * that can manage snippets (wpcode_activate_snippets capability).
5 * The class will gather info about the header & footer items added and all the active snippets
6 * and display a count of scripts/snippets added by wpcode after the page is fully loaded.
7 */
8
9 /**
10 * Class WPCode_Admin_Bar_Info
11 */
12 abstract class WPCode_Admin_Bar_Info {
13
14 /**
15 * The global locations disabled for the current page through the Page Scripts settings.
16 *
17 * @var array
18 */
19 protected $global_disabled = array();
20
21 /**
22 * The snippets loaded on this page.
23 *
24 * @var array
25 */
26 public $loaded_snippets = array();
27
28 /**
29 * The WPCode_Admin_Bar_Info constructor.
30 */
31 public function __construct() {
32 add_action( 'plugins_loaded', array( $this, 'maybe_init' ), 1 );
33 }
34
35 /**
36 * Check if we should be tracking for the current session.
37 *
38 * @return bool
39 */
40 public function should_track() {
41 if ( ! is_user_logged_in() ) {
42 return false;
43 }
44
45 if ( ! current_user_can( 'wpcode_activate_snippets' ) ) {
46 return false;
47 }
48
49 // Don't load the admin bar menu in headers & footers mode.
50 if ( wpcode()->settings->get_option( 'headers_footers_mode' ) ) {
51 return false;
52 }
53
54 $show_menu = wpcode()->settings->get_option( 'admin_bar_info', true );
55
56 if ( ! $show_menu ) {
57 return false;
58 }
59
60 return true;
61 }
62
63 /**
64 * Early on the plugins_loaded hook we check if we should be tracking for the current session.
65 *
66 * @return void
67 */
68 public function maybe_init() {
69 if ( $this->should_track() ) {
70 $this->hooks();
71 }
72 }
73
74 /**
75 * Add hooks needed to track the way WPCode loads scripts and snippets.
76 */
77 public function hooks() {
78 // Use the snippet output to more accurately track the snippets loaded.
79 add_filter( 'wpcode_snippet_output', array( $this, 'track_snippet_output' ), 999, 2 );
80
81 // Add an admin menu item to display the results.
82 add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_info' ), 999 );
83
84 add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_quick_links' ), 1200 );
85
86 // Output results at the end of the page and use JS to populate the admin menu item.
87 add_action( 'wp_footer', array( $this, 'add_footer_info' ), 15 );
88 add_action( 'admin_footer', array( $this, 'add_footer_info' ), 999999 );
89
90 add_action( 'admin_init', array( $this, 'enqueue_scripts' ), - 5 );
91 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 15 );
92 add_action( 'template_redirect', array( $this, 'enqueue_scripts' ), - 5 );
93 }
94
95 /**
96 * Load the scripts used by the admin bar.
97 *
98 * @return void
99 */
100 public function enqueue_scripts() {
101 if ( ! is_admin_bar_showing() ) {
102 return;
103 }
104 // Allow other plugins to modify the screens where the admin bar scripts are loaded.
105 if ( apply_filters( 'wpcode_load_admin_bar_scripts', false ) ) {
106 return;
107 }
108
109 $admin_asset_file = WPCODE_PLUGIN_PATH . 'build/admin-bar.asset.php';
110
111 if ( ! file_exists( $admin_asset_file ) ) {
112 return;
113 }
114
115 $asset = require $admin_asset_file;
116
117 wp_enqueue_style( 'wpcode-admin-bar-css', WPCODE_PLUGIN_URL . 'build/admin-bar.css', null, $asset['version'] );
118
119 wp_enqueue_script( 'wpcode-admin-bar-js', WPCODE_PLUGIN_URL . 'build/admin-bar.js', null, $asset['version'], true );
120
121 }
122
123 /**
124 * Add the WPCode info to the admin bar.
125 *
126 * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
127 */
128 public function add_admin_bar_info( $wp_admin_bar ) {
129 // Let's see if we have any errors.
130 $error_count = wpcode()->error->get_error_count();
131 $indicator = '';
132
133 if ( $error_count > 0 ) {
134 $indicator = ' <div class="wp-core-ui wp-ui-notification wpcode-menu-notification-counter">' . $error_count . '</div>';
135 }
136
137 // Add an admin menu item to append our count to.
138 $wp_admin_bar->add_menu(
139 array(
140 'id' => 'wpcode-admin-bar-info',
141 'title' => 'WPCode' . $indicator,
142 'meta' => array(
143 'class' => 'wpcode-admin-bar-info menupop',
144 ),
145 'href' => add_query_arg( 'page', 'wpcode', admin_url( 'admin.php' ) ),
146 )
147 );
148
149 do_action( 'wpcode_admin_bar_info_top', $wp_admin_bar );
150
151 if ( ! empty( $error_count ) ) {
152 $wp_admin_bar->add_menu(
153 array(
154 'id' => 'wpcode-error-count',
155 'parent' => 'wpcode-admin-bar-info',
156 'title' => esc_html__( 'Snippets With Errors', 'insert-headers-and-footers' ) . $indicator,
157 'meta' => array(
158 'class' => 'wpcode-admin-bar-info-submenu',
159 ),
160 'href' => add_query_arg(
161 array(
162 'page' => 'wpcode',
163 'view' => 'has_error',
164 ),
165 admin_url( 'admin.php' )
166 ),
167 )
168 );
169 }
170
171 $wp_admin_bar->add_menu(
172 array(
173 'id' => 'wpcode-description',
174 'parent' => 'wpcode-admin-bar-info',
175 'title' => esc_html__( 'Loaded on this page', 'insert-headers-and-footers' ),
176 'meta' => array(
177 'class' => 'wpcode-admin-bar-info-submenu wpcode-admin-bar-description',
178 ),
179 )
180 );
181
182 $global_scripts_data = $this->get_global_scripts_data();
183
184 if ( ! empty( $global_scripts_data ) ) {
185
186 // Calculate total by adding up the "count" property of each item.
187 $total = array_sum( wp_list_pluck( $global_scripts_data, 'count' ) );
188
189 $wp_admin_bar->add_menu(
190 array(
191 'id' => 'wpcode-global-scripts',
192 'parent' => 'wpcode-admin-bar-info',
193 'title' => esc_html(
194 sprintf(
195 // translators: %d is the total number of global scripts.
196 __( 'Global Scripts (%d)', 'insert-headers-and-footers' ),
197 $total
198 )
199 ),
200 'meta' => array(
201 'class' => 'wpcode-admin-bar-info-submenu',
202 ),
203 'href' => add_query_arg( 'page', 'wpcode-headers-footers', admin_url( 'admin.php' ) ),
204 )
205 );
206
207 foreach ( $global_scripts_data as $id => $global_scripts_area ) {
208 $wp_admin_bar->add_menu(
209 array(
210 'id' => 'wpcode-global-' . $id,
211 'parent' => 'wpcode-global-scripts',
212 'title' => esc_html( $global_scripts_area['label'] ),
213 'meta' => array(
214 'class' => 'wpcode-admin-bar-info-submenu',
215 ),
216 'href' => $global_scripts_area['href'],
217 )
218 );
219 }
220 }
221
222 $wp_admin_bar->add_menu(
223 array(
224 'id' => 'wpcode-loaded-on-this-page',
225 'parent' => 'wpcode-admin-bar-info',
226 'title' => esc_html__( 'Code Snippets', 'insert-headers-and-footers' ),
227 'meta' => array(
228 'class' => 'wpcode-admin-bar-info-submenu',
229 ),
230 'href' => add_query_arg( 'page', 'wpcode', admin_url( 'admin.php' ) ),
231 )
232 );
233
234 $wp_admin_bar->add_menu(
235 array(
236 'id' => 'wpcode-admin-bar-info-replace',
237 'parent' => 'wpcode-loaded-on-this-page',
238 'title' => '',
239 'meta' => array(
240 'class' => 'wpcode-admin-bar-info-submenu',
241 ),
242 )
243 );
244 }
245
246 /**
247 * Use the filter called in each location to keep tabs of which snippets were actually loaded in each location.
248 *
249 * @param WPCode_Snippet[] $snippets Array of snippets.
250 * @param string $location The location.
251 *
252 * @return mixed
253 */
254 public function track_used_snippets( $snippets, $location ) {
255
256 foreach ( $snippets as $snippet ) {
257 if ( ! isset( $this->loaded_snippets[ $location ] ) ) {
258 $this->loaded_snippets[ $location ] = array();
259 }
260 $this->loaded_snippets[ $location ][] = $snippet;
261 }
262
263 return $snippets;
264 }
265
266 /**
267 * Output a script that builds the admin bar menu with the snippet/scripts info using JS after the page has loaded.
268 *
269 * @return void
270 */
271 public function add_footer_info() {
272
273 if ( ! is_admin_bar_showing() ) {
274 return;
275 }
276
277 $footer_info = array();
278
279 foreach ( $this->loaded_snippets as $location => $snippets ) {
280
281 $location_label = wpcode()->auto_insert->get_location_label( $location );
282 if ( 'shortcode' === $location ) {
283 $location_label = esc_html__( 'Shortcode', 'insert-headers-and-footers' );
284 }
285 if ( 'block' === $location ) {
286 $location_label = esc_html__( 'Gutenberg Block', 'insert-headers-and-footers' );
287 }
288 if ( 0 === strpos( $location, 'shortcode-' ) ) {
289 $location_label = esc_html__( 'Custom Shortcode', 'insert-headers-and-footers' );
290 }
291 $location_info = array(
292 'label' => $location_label . ' (' . count( $snippets ) . ')',
293 'location_id' => $location,
294 'snippets' => array(),
295 'href' => $this->get_location_filter_link( $location ),
296 );
297 foreach ( $snippets as $snippet ) {
298 $location_info['snippets'][] = array(
299 'id' => $snippet->get_id(),
300 'title' => esc_html( $snippet->get_title() ),
301 'edit_link' => $this->get_snippet_edit_link( $snippet ),
302 );
303 }
304 $location_info['count'] = count( $snippets );
305
306 $footer_info[] = $location_info;
307 }
308
309 $total_count = 0;
310 foreach ( $footer_info as $location ) {
311 $total_count += $location['count'];
312 }
313
314 // Output $footer_info as a JSON in a JS variable to be used in the script to populate the admin bar.
315 ?>
316 <script>
317 var wpcode_admin_bar_info = <?php echo wp_json_encode( $footer_info ); ?>;
318 var wpcode_admin_bar_info_count = <?php echo absint( $total_count ); ?>;
319 </script>
320 <?php
321 }
322
323 /**
324 * Get the snippet edit link based on the snippet id.
325 *
326 * @param WPCode_Snippet $snippet The snippet object.
327 *
328 * @return string
329 */
330 public function get_snippet_edit_link( $snippet ) {
331 return apply_filters( 'wpcode_admin_bar_edit_snippet_link', admin_url( 'admin.php?page=wpcode-snippet-manager&snippet_id=' . $snippet->get_id() ), $snippet );
332 }
333
334 /**
335 * Get data related to the global Header & Footer scripts.
336 *
337 * @return array
338 */
339 public function get_global_scripts_data() {
340 if ( is_admin() ) {
341 // Global scripts are never loaded in the admin.
342 return array();
343 }
344 $data = array();
345
346 $global_locations = array(
347 'header' => array(
348 'label' => __( 'Global Header', 'insert-headers-and-footers' ),
349 ),
350 );
351 if ( function_exists( 'wp_body_open' ) && version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ) {
352 $global_locations['body'] = array(
353 'label' => __( 'Global Body', 'insert-headers-and-footers' ),
354 );
355 }
356 $global_locations['footer'] = array(
357 'label' => __( 'Global Footer', 'insert-headers-and-footers' ),
358 );
359
360 $disabled_label = esc_html__( 'Disabled via Page Scripts', 'insert-headers-and-footers' );
361
362 foreach ( $global_locations as $location => $location_texts ) {
363 $scripts = get_option( 'ihaf_insert_' . $location );
364
365 $count = ! empty( $scripts ) ? 1 : 0;
366
367 if ( in_array( $location, $this->global_disabled, true ) ) {
368 $count = 0;
369
370 $location_texts['label'] .= ' (' . $disabled_label . ')';
371 } else {
372 $location_texts['label'] .= ' (' . $count . ')';
373 }
374 $data[ $location ] = array(
375 'label' => $location_texts['label'],
376 'href' => admin_url( 'admin.php?page=wpcode-headers-footers#wpcode-global-' . $location ),
377 'count' => $count,
378 );
379 }
380
381 return $data;
382 }
383
384 /**
385 * Add the WPCode info to the admin bar.
386 *
387 * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
388 */
389 public function add_admin_bar_quick_links( $wp_admin_bar ) {
390
391
392 $wp_admin_bar->add_menu(
393 array(
394 'id' => 'wpcode-admin-bar-info-add-new',
395 'parent' => 'wpcode-admin-bar-info',
396 'title' => esc_html__( '+ Add Snippet', 'insert-headers-and-footers' ),
397 'href' => admin_url( 'admin.php?page=wpcode-snippet-manager' ),
398 'meta' => array(
399 'class' => 'wpcode-admin-bar-info-submenu wpcode-admin-bar-info-separator-top',
400 ),
401 )
402 );
403
404 $wp_admin_bar->add_menu(
405 array(
406 'id' => 'wpcode-admin-bar-info-settings',
407 'parent' => 'wpcode-admin-bar-info',
408 'title' => esc_html__( 'Settings', 'insert-headers-and-footers' ),
409 'href' => add_query_arg( 'page', 'wpcode-settings', admin_url( 'admin.php' ) ),
410 'meta' => array(
411 'class' => 'wpcode-admin-bar-info-submenu',
412 ),
413 )
414 );
415
416 // If error logging is enabled add a direct link here.
417 if ( wpcode()->settings->get_option( 'error_logging' ) ) {
418 $wp_admin_bar->add_menu(
419 array(
420 'id' => 'wpcode-admin-bar-info-error-logs',
421 'parent' => 'wpcode-admin-bar-info',
422 'title' => esc_html__( 'Logs', 'insert-headers-and-footers' ),
423 'href' => add_query_arg(
424 array(
425 'page' => 'wpcode-tools',
426 'view' => 'logs',
427
428 ),
429 admin_url( 'admin.php' )
430 ),
431 'meta' => array(
432 'class' => 'wpcode-admin-bar-info-submenu',
433 ),
434 )
435 );
436 }
437
438 $wp_admin_bar->add_menu(
439 array(
440 'id' => 'wpcode-admin-bar-info-help',
441 'parent' => 'wpcode-admin-bar-info',
442 'title' => esc_html__( 'Help Docs', 'insert-headers-and-footers' ),
443 'href' => wpcode_utm_url( 'https://wpcode.com/docs/', 'admin-bar', 'help' ),
444 'meta' => array(
445 'class' => 'wpcode-admin-bar-info-submenu',
446 'target' => '_blank',
447 'rel' => 'noopener noreferrer',
448 ),
449 )
450 );
451 }
452
453 /**
454 * Track a snippet output for the admin bar.
455 *
456 * @param string $output The snippet output (we don't use this).
457 * @param WPCode_Snippet $snippet The snippet object.
458 *
459 * @return string
460 */
461 public function track_snippet_output( $output, $snippet ) {
462
463 $location = $snippet->get_location();
464
465 if ( ! isset( $this->loaded_snippets[ $location ] ) ) {
466 $this->loaded_snippets[ $location ] = array();
467 }
468 $this->loaded_snippets[ $location ][] = $snippet;
469
470 return $output;
471 }
472
473 /**
474 * Method for the location link.
475 *
476 * @param string $location The location slug.
477 *
478 * @return string
479 */
480 public function get_location_filter_link( $location ) {
481
482 if ( in_array( $location, array( 'shortcode', 'block' ), true ) ) {
483 // If this is for a shortcode or a block let's try to link to the edit page of the currently loaded post.
484 return get_edit_post_link();
485 }
486
487 return esc_url(
488 add_query_arg(
489 array(
490 'page' => 'wpcode',
491 'location' => $location,
492 'filter_action' => 'filter',
493 ),
494 admin_url( 'admin.php' )
495 )
496 );
497 }
498
499 }
500