| 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 |
|