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

104 lines 2.3 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 * Variable.
71 *
72 * @var \WP_Term|\WP_Post_Type|WP_Post|\WP_User|null
73 */
74 $queried_object = $GLOBALS['wp_the_query']->get_queried_object();
75
76 if ( null === $queried_object || WP_Post::class !== get_class( $queried_object ) ) {
77 return;
78 }
79
80 /**
81 * Variable.
82 *
83 * @var WP_Post
84 */
85 $current_post = $queried_object;
86
87 $post_type_object = get_post_type_object( $current_post->post_type );
88 if ( null !== $post_type_object && $post_type_object->show_in_admin_bar && Dashboard_Link::can_show_link( $current_post, $this->parsely ) ) {
89 $href = Dashboard_Link::generate_url( $current_post, $this->parsely->get_site_id(), 'wp-page-single', 'admin-bar' );
90
91 // Not adding the link if there were issues generating the URL.
92 if ( '' !== $href ) {
93 $admin_bar->add_node(
94 array(
95 'id' => 'parsely-stats',
96 'title' => __( 'Parse.ly Stats', 'wp-parsely' ),
97 'href' => $href,
98 )
99 );
100 }
101 }
102 }
103 }
104