PluginProbe
Parse.ly / 3.14.1
Parse.ly v3.14.1
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.14.1, at src/content-helper/dashboard-widget/class-dashboard-widget.php

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