PluginProbe
Parse.ly / 3.3.2
Parse.ly v3.3.2
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / UI / class-admin-bar.php

class-admin-bar.php in Parse.ly 3.3.2, at src/UI/class-admin-bar.php

91 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UI: Admin bar class
4 *
5 * Enhances the WordPress admin bar with Parse.ly tweaks.
6 *
7 * @package Parsely
8 * @since 3.1.0
9 */
10
11 declare(strict_types=1);
12
13 namespace Parsely\UI;
14
15 use WP_Admin_Bar;
16 use Parsely\Parsely;
17 use Parsely\Dashboard_Link;
18
19 /**
20 * Renders Parse.ly related buttons in the WordPress administrator top bar.
21 *
22 * @since 3.1.0
23 */
24 final class Admin_Bar {
25 /**
26 * Instance of Parsely class.
27 *
28 * @var Parsely
29 */
30 private $parsely;
31
32 /**
33 * Constructor.
34 *
35 * @param Parsely $parsely Instance of Parsely class.
36 */
37 public function __construct( Parsely $parsely ) {
38 $this->parsely = $parsely;
39 }
40
41 /**
42 * Registers admin bar buttons.
43 *
44 * @since 3.1.0
45 */
46 public function run(): void {
47 /**
48 * Filter whether the Open on Parse.ly button is enabled or not on the
49 * admin bar menu.
50 *
51 * @since 3.1.2
52 *
53 * @param bool $enabled True if enabled, false if not.
54 */
55 if ( apply_filters( 'wp_parsely_enable_admin_bar', true ) ) {
56 // Priority 201 to load after Core's admin bar secondary groups (200).
57 add_action( 'admin_bar_menu', array( $this, 'admin_bar_parsely_stats_button' ), 201 );
58 }
59 }
60
61 /**
62 * Adds the `Parse.ly Stats` button on the admin bar when the current object
63 * is a post or a page.
64 *
65 * @param WP_Admin_Bar $admin_bar WP_Admin_Bar instance, passed by reference.
66 */
67 public function admin_bar_parsely_stats_button( WP_Admin_Bar $admin_bar ): void {
68 $current_object = $GLOBALS['wp_the_query']->get_queried_object();
69
70 if ( null === $current_object || empty( $current_object->post_type ) ) {
71 return;
72 }
73
74 $post_type_object = get_post_type_object( $current_object->post_type );
75 if ( null !== $post_type_object && $post_type_object->show_in_admin_bar && Dashboard_Link::can_show_link( $current_object, $this->parsely ) ) {
76 $href = Dashboard_Link::generate_url( $current_object, $this->parsely->get_api_key(), 'wp-page-single', 'admin-bar' );
77
78 // Not adding the link if there were issues generating the URL.
79 if ( '' !== $href ) {
80 $admin_bar->add_node(
81 array(
82 'id' => 'parsely-stats',
83 'title' => __( 'Parse.ly Stats', 'wp-parsely' ),
84 'href' => $href,
85 )
86 );
87 }
88 }
89 }
90 }
91