forked from Automattic/jetpack-production
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-jetpack-newsletter-dashboard-widget.php
204 lines (181 loc) · 4.83 KB
/
class-jetpack-newsletter-dashboard-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Jetpack Newsletter Dashboard Widget.
*
* @package jetpack
*/
/**
* Adds the Jetpack Newsletter widget to the WordPress admin dashboard.
*
* @package jetpack
*/
use Automattic\Jetpack\Connection\Client;
/**
* Class that adds the Jetpack Newsletter Dashboard Widget to the WordPress admin dashboard.
*/
class Jetpack_Newsletter_Dashboard_Widget {
/**
* Indicates whether the class initialized or not.
*
* @var bool
*/
private static $initialized = false;
/**
* The Widget ID.
*
* @var string
*/
private static $widget_id = 'jetpack_newsletter_dashboard_widget';
/**
* Initialize the class by calling the setup static function.
*
* @return void
*/
public static function init() {
if ( ! self::$initialized ) {
self::$initialized = true;
self::wp_dashboard_setup();
}
}
/**
* Get the config data for the Jetpack Newsletter widget.
*
* @return array
*/
public static function get_config_data() {
$subscriber_counts = array();
$config_data = array();
if ( Jetpack::is_connection_ready() ) {
$site_id = Jetpack_Options::get_option( 'id' );
$api_path = sprintf( '/sites/%d/subscribers/counts', $site_id );
$response = Client::wpcom_json_api_request_as_blog(
$api_path,
'2',
array(),
null,
'wpcom'
);
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
$subscriber_counts = json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $subscriber_counts['counts']['email_subscribers'] ) ) {
$config_data['emailSubscribers'] = (int) $subscriber_counts['counts']['email_subscribers'];
}
if ( isset( $subscriber_counts['counts']['paid_subscribers'] ) ) {
$config_data['paidSubscribers'] = (int) $subscriber_counts['counts']['paid_subscribers'];
}
}
}
return $config_data;
}
/**
* Sets up the Jetpack Newsletter widget in the WordPress admin dashboard.
*/
public static function wp_dashboard_setup() {
if ( Jetpack::is_connection_ready() ) {
static::load_admin_scripts(
'jp-newsletter-widget',
'newsletter-widget',
array(
'config_variable_name' => 'jetpackNewsletterWidgetConfigData',
'config_data' => static::get_config_data(),
)
);
$widget_title = sprintf(
__( 'Newsletter', 'jetpack' )
);
wp_add_dashboard_widget(
self::$widget_id,
$widget_title,
array( static::class, 'render' )
);
}
}
/**
* Render the Jetpack Newsletter widget.
*
* @return void
*/
public static function render() {
?>
<div id="wpcom">
<div id="newsletter-widget-app"></div>
</div>
<?php
}
/**
* Load the admin scripts for the Jetpack Newsletter widget.
*
* @return void
*/
public static function admin_init() {
static::load_admin_scripts(
'jp-newsletter-widget',
'newsletter-widget',
array(
'config_variable_name' => 'jetpackNewsletterWidgetConfigData',
'config_data' => static::get_config_data(),
)
);
}
/**
* Load the admin scripts for the Jetpack Newsletter widget.
*
* @param string $asset_handle The handle of the asset.
* @param string $asset_name The name of the asset.
* @param array $options The options for the asset.
* @return void
*/
public static function load_admin_scripts( $asset_handle, $asset_name, $options = array() ) {
$default_options = array(
'config_data' => array(),
'config_variable_name' => 'configData',
'enqueue_css' => true,
);
$options = wp_parse_args( $options, $default_options );
// Get the asset file path
$asset_path = JETPACK__PLUGIN_DIR . '_inc/build/' . $asset_name . '.min.asset.php';
// Get dependencies and version from asset file
$dependencies = array();
$version = JETPACK__VERSION;
if ( file_exists( $asset_path ) ) {
$asset = require $asset_path;
$dependencies = $asset['dependencies'];
$version = $asset['version'];
}
// Register and enqueue the script
wp_register_script(
$asset_handle,
plugins_url( '_inc/build/' . $asset_name . '.min.js', JETPACK__PLUGIN_FILE ),
$dependencies,
$version,
true
);
wp_enqueue_script( $asset_handle );
// Enqueue the CSS if enabled
if ( $options['enqueue_css'] ) {
wp_enqueue_style(
$asset_handle,
plugins_url( '_inc/build/' . $asset_name . '.css', JETPACK__PLUGIN_FILE ),
array(),
$version
);
// Enqueue RTL stylesheet if needed
if ( is_rtl() ) {
wp_enqueue_style(
$asset_handle . '-rtl',
plugins_url( '_inc/build/' . $asset_name . '.rtl.css', JETPACK__PLUGIN_FILE ),
array( $asset_handle ),
$version
);
}
}
// Add any configuration data if needed
if ( ! empty( $options['config_data'] ) ) {
wp_add_inline_script(
$asset_handle,
"window.{$options['config_variable_name']} = " . wp_json_encode( $options['config_data'] ) . ';',
'before'
);
}
}
}