PluginProbe
Parse.ly / 3.24.1
Parse.ly v3.24.1
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.24.1, at src/UI/class-admin-bar.php

107 lines 2.4 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_Post;
16 use WP_Admin_Bar;
17 use Parsely\Parsely;
18 use Parsely\Dashboard_Link;
19
20 /**
21 * Renders Parse.ly related buttons in the WordPress administrator top bar.
22 *
23 * @since 3.1.0
24 */
25 final class Admin_Bar {
26 /**
27 * Instance of Parsely class.
28 *
29 * @var Parsely
30 */
31 private $parsely;
32
33 /**
34 * Constructor.
35 *
36 * @param Parsely $parsely Instance of Parsely class.
37 */
38 public function __construct( Parsely $parsely ) {
39 $this->parsely = $parsely;
40 }
41
42 /**
43 * Registers admin bar buttons.
44 *
45 * @since 3.1.0
46 */
47 public function run(): void {
48 /**
49 * Filter whether the Open on Parse.ly button is enabled or not on the
50 * admin bar menu.
51 *
52 * @since 3.1.2
53 *
54 * @param bool $enabled True if enabled, false if not.
55 */
56 if ( apply_filters( 'wp_parsely_enable_admin_bar', true ) ) {
57 // Priority 201 to load after Core's admin bar secondary groups (200).
58 add_action( 'admin_bar_menu', array( $this, 'admin_bar_parsely_stats_button' ), 201 );
59 }
60 }
61
62 /**
63 * Adds the "Parse.ly Stats" button on the admin bar when the current object
64 * is a post or a page.
65 *
66 * @param WP_Admin_Bar $admin_bar WP_Admin_Bar instance, passed by reference.
67 */
68 public function admin_bar_parsely_stats_button( WP_Admin_Bar $admin_bar ): void {
69 /**
70 * The result of calling get_queried_object().
71 *
72 * Although it shouldn't return false per the documentation, it can
73 * happen in practice.
74 *
75 * @var \WP_Term|\WP_Post_Type|WP_Post|\WP_User|null|false
76 */
77 $queried_object = $GLOBALS['wp_the_query']->get_queried_object();
78
79 if ( ! $queried_object instanceof WP_Post ) {
80 return;
81 }
82
83 /**
84 * Variable.
85 *
86 * @var WP_Post
87 */
88 $current_post = $queried_object;
89
90 $post_type_object = get_post_type_object( $current_post->post_type );
91 if ( null !== $post_type_object && $post_type_object->show_in_admin_bar && Dashboard_Link::can_show_link( $current_post, $this->parsely ) ) {
92 $href = Dashboard_Link::generate_url( $current_post, $this->parsely->get_site_id(), 'wp-page-single', 'admin-bar' );
93
94 // Not adding the link if there were issues generating the URL.
95 if ( '' !== $href ) {
96 $admin_bar->add_node(
97 array(
98 'id' => 'parsely-stats',
99 'title' => __( 'Parse.ly Stats', 'wp-parsely' ),
100 'href' => $href,
101 )
102 );
103 }
104 }
105 }
106 }
107