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

91 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Parsely tweaks to WordPress admin bar
4 *
5 * @package Parsely
6 * @since 3.1.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\UI;
12
13 use WP_Admin_Bar;
14 use Parsely\Parsely;
15 use Parsely\Dashboard_Link;
16
17 /**
18 * Render Parse.ly related buttons in the WordPress administrator top bar.
19 *
20 * @since 3.1.0
21 */
22 final class Admin_Bar {
23 /**
24 * Instance of Parsely class.
25 *
26 * @var Parsely
27 */
28 private $parsely;
29
30 /**
31 * Constructor.
32 *
33 * @param Parsely $parsely Instance of Parsely class.
34 */
35 public function __construct( Parsely $parsely ) {
36 $this->parsely = $parsely;
37 }
38
39 /**
40 * Register admin bar buttons.
41 *
42 * @since 3.1.0
43 *
44 * @return void
45 */
46 public function run(): void {
47 /**
48 * Filter whether the Open on Parse.ly button is enabled or not on the admin bar menu.
49 *
50 * @since 3.1.2
51 *
52 * @param bool $enabled True if enabled, false if not.
53 */
54 if ( apply_filters( 'wp_parsely_enable_admin_bar', true ) ) {
55 // Priority 201 to load after Core's admin bar secondary groups (200).
56 add_action( 'admin_bar_menu', array( $this, 'admin_bar_parsely_stats_button' ), 201 );
57 }
58 }
59
60 /**
61 * Adds the `Parse.ly Stats` button on the admin bar when the current object is a post or a page.
62 *
63 * @param WP_Admin_Bar $admin_bar WP_Admin_Bar instance, passed by reference.
64 *
65 * @return void
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