PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
3.24.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 All 106 releases
wp-parsely / src / content-helper / dashboard-widget / class-dashboard-widget.php

class-dashboard-widget.php in Parse.ly 3.24.2, at src/content-helper/dashboard-widget/class-dashboard-widget.php

148 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PCH Dashboard Widget class
4 *
5 * @package Parsely
6 * @since 3.7.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Content_Helper;
12
13 use Parsely\Parsely;
14 use Parsely\Utils\Utils;
15 use Parsely\REST_API\Settings\Endpoint_Dashboard_Widget_Settings;
16
17 use const Parsely\PARSELY_FILE;
18
19 /**
20 * Class that generates and manages the PCH Dashboard Widget.
21 *
22 * @since 3.7.0
23 */
24 class Dashboard_Widget extends Content_Helper_Feature {
25 /**
26 * Constructor.
27 *
28 * @since 3.9.0
29 *
30 * @param Parsely $parsely Instance of Parsely class.
31 */
32 public function __construct( Parsely $parsely ) {
33 $this->parsely = $parsely;
34 }
35
36 /**
37 * Returns the feature's filter name.
38 *
39 * @since 3.9.0
40 *
41 * @return string The filter name.
42 */
43 public static function get_feature_filter_name(): string {
44 return self::get_global_filter_name() . '_dashboard_widget';
45 }
46
47 /**
48 * Returns the feature's script ID.
49 *
50 * @since 3.9.0
51 *
52 * @return string The script ID.
53 */
54 public static function get_script_id(): string {
55 return 'wp-parsely-dashboard-widget';
56 }
57
58 /**
59 * Returns the feature's style ID.
60 *
61 * @since 3.9.0
62 *
63 * @return string The style ID.
64 */
65 public static function get_style_id(): string {
66 return static::get_script_id();
67 }
68
69 /**
70 * Sets all the hooks that are needed in order to add the Dashboard Widget
71 * to the WordPress Dashboard.
72 *
73 * @since 3.7.0
74 */
75 public function run(): void {
76 // The can_enable_feature() function is not being used here, as
77 // get_current_screen() is still null when this function is called.
78 add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
79 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
80 }
81
82 /**
83 * Returns whether the Dashboard Widget can be enabled.
84 *
85 * @since 3.9.0
86 *
87 * @return bool Whether the Dashboard Widget can be enabled.
88 */
89 public function can_enable_widget(): bool {
90 $screen = get_current_screen();
91
92 return $this->can_enable_feature(
93 null !== $screen && 'dashboard' === $screen->id,
94 $this->parsely->get_rest_api_controller()->is_available_to_current_user(
95 '/stats/posts'
96 )
97 );
98 }
99
100 /**
101 * Adds the Widget and its contents to the WordPress Dashboard.
102 *
103 * @since 3.7.0
104 */
105 public function add_dashboard_widget(): void {
106 if ( ! $this->can_enable_widget() ) {
107 return;
108 }
109
110 wp_add_dashboard_widget(
111 static::get_script_id(),
112 __( 'Parse.ly Top Posts', 'wp-parsely' ),
113 '__return_empty_string' // Content will be populated by JavaScript.
114 );
115 }
116
117 /**
118 * Enqueues the Dashboard Widget's assets.
119 *
120 * @since 3.7.0
121 */
122 public function enqueue_assets(): void {
123 if ( ! $this->can_enable_widget() ) {
124 return;
125 }
126
127 $asset_php = Utils::get_asset_info( 'build/content-helper/dashboard-widget.asset.php' );
128 $built_assets_url = plugin_dir_url( PARSELY_FILE ) . 'build/content-helper/';
129
130 wp_enqueue_script(
131 static::get_script_id(),
132 $built_assets_url . 'dashboard-widget.js',
133 $asset_php['dependencies'],
134 $asset_php['version'],
135 true
136 );
137
138 $this->inject_inline_scripts( Endpoint_Dashboard_Widget_Settings::get_endpoint_name() );
139
140 wp_enqueue_style(
141 static::get_style_id(),
142 $built_assets_url . 'dashboard-widget.css',
143 array(),
144 $asset_php['version']
145 );
146 }
147 }
148