-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-sensei-compat-admin.php
378 lines (327 loc) · 10.9 KB
/
class-sensei-compat-admin.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* File containing the class Sensei_Compat_Admin.
*
* @package sensei-compat
* @since 1.0.0
*/
/**
* Sensei_Compat_Admin class.
*/
class Sensei_Compat_Admin {
const WOO_PRODUCT_ID = 152116;
/**
* Initialize admin actions and filters.
*/
public static function init() {
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 3 );
add_filter( 'install_plugins_search', array( __CLASS__, 'load_plugin_information' ) );
add_filter( 'sensei_admin_notices', [ __CLASS__, 'wccom_connect_notice' ] );
add_filter( 'sensei_admin_notices', [ __CLASS__, 'woocommerce_notice' ] );
add_filter( 'sensei_home_is_plugin_licensed_woothemes-sensei', [ __CLASS__, 'is_plugin_licensed' ] );
if ( SENSEI_COMPAT_LOADING_SENSEI ) {
add_filter( 'site_transient_update_plugins', array( __CLASS__, 'add_sensei_translations' ) );
add_action( 'set_site_transient_update_plugins', array( __CLASS__, 'clear_sensei_translations' ) );
}
}
/**
* Adds Sensei's language pack updates to the `update_plugins` transient.
*
* @access private
*
* @param \stdClass $value Current value of `update_plugins` transient.
* @return \stdClass
*/
public static function add_sensei_translations( $value ) {
if ( empty( $value ) ) {
return $value;
}
$language_pack_transient_key = self::get_translation_update_cache_key();
$translations_available = get_site_transient( $language_pack_transient_key );
if ( ! $translations_available ) {
$translations_available = self::get_sensei_language_pack_updates();
set_site_transient( $language_pack_transient_key, $translations_available, DAY_IN_SECONDS );
}
foreach ( $translations_available as $locale => $package ) {
$value->translations[] = $package;
}
return $value;
}
/**
* Clear's Sensei language pack update cache.
*
* @access private
*/
public static function clear_sensei_translations() {
delete_site_transient( self::get_translation_update_cache_key() );
}
/**
* Gets the cache key for the current Sensei translation update cache.
*
* @return string
*/
private static function get_translation_update_cache_key() {
$cache_key_parts = array_values( get_available_languages() );
$cache_key_parts[] = Sensei()->version;
return 'sensei_language_packs_' . md5( implode( ',', $cache_key_parts ) );
}
/**
* Gets the available language package updates.
*
* @return array
*/
private static function get_sensei_language_pack_updates() {
global $wp_version;
static $plugin_translations;
if ( isset( $plugin_translations ) ) {
return $plugin_translations;
}
$plugin_translations = array();
$installed_translations_raw = wp_get_installed_translations( 'plugins' );
$installed_translations = array();
// Only pass translations installed for Sensei.
if ( isset( $installed_translations_raw['sensei-lms'] ) ) {
$installed_translations['sensei-lms'] = $installed_translations_raw['sensei-lms'];
}
$to_send = array();
$to_send['plugins'] = array(
'sensei-lms/sensei-lms.php' => array(
'Name' => 'Sensei LMS',
'Title' => 'Sensei LMS',
'Version' => Sensei()->version,
'TextDomain' => 'sensei-lms',
),
);
$to_send['active'] = array( 'sensei-lms/sensei-lms.php' );
$locales = array_values( get_available_languages() );
/** This action is documented in WordPress core's wp-includes/update.php */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$locales = apply_filters( 'plugins_update_check_locales', $locales );
$locales = array_unique( $locales );
$options = array(
'timeout' => 10,
'body' => array(
'plugins' => wp_json_encode( $to_send ),
'translations' => wp_json_encode( $installed_translations ),
'locale' => wp_json_encode( $locales ),
'all' => wp_json_encode( true ),
),
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
);
$url = 'http://api.wordpress.org/plugins/update-check/1.1/';
$ssl = wp_http_supports( array( 'ssl' ) );
if ( $ssl ) {
$url = set_url_scheme( $url, 'https' );
}
$raw_response = wp_remote_post( $url, $options );
if ( is_wp_error( $raw_response ) || 200 !== intval( wp_remote_retrieve_response_code( $raw_response ) ) ) {
return array();
}
$response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
if ( is_array( $response ) && ! empty( $response['translations'] ) ) {
$plugin_translations = $response['translations'];
}
return $plugin_translations;
}
/**
* Adds details about the plugins packaged within this compatibility plugin.
*
* @param string[] $plugin_meta An array of the plugin's metadata,
* including the version, author,
* author URI, and plugin URI.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @param array $plugin_data An array of plugin data.
*
* @return string[]
*/
public static function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data ) {
if ( ! in_array( $plugin_file, [ 'woothemes-sensei/woothemes-sensei.php', 'sensei/woothemes-sensei.php' ], true ) ) {
return $plugin_meta;
}
if ( 'sensei-compat' !== $plugin_data['TextDomain'] ) {
return $plugin_meta;
}
unset( $plugin_meta[0] );
if ( SENSEI_COMPAT_LOADING_SENSEI_PRO && defined( 'SENSEI_PRO_VERSION' ) ) {
// translators: placeholder is current version of Sensei Pro.
array_unshift( $plugin_meta, esc_html( sprintf( __( 'Sensei Pro Version: %s', 'sensei-compat' ), SENSEI_PRO_VERSION ) ) );
}
if ( SENSEI_COMPAT_LOADING_SENSEI && function_exists( 'Sensei' ) ) {
// translators: placeholder is current version of Sensei.
array_unshift( $plugin_meta, esc_html( sprintf( __( 'Sensei LMS Version: %s', 'sensei-compat' ), Sensei()->version ) ) );
}
return $plugin_meta;
}
/**
* Manually load the plugin information on `plugin-install.php` page load.
*/
public static function load_plugin_information() {
$plugins_handled = [
'sensei-lms' => 'Sensei LMS',
'woocommerce' => 'WooCommerce',
];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( empty( $_GET['plugin_details'] ) || ! isset( $plugins_handled[ $_GET['plugin_details'] ] ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$plugin_slug = sanitize_title( wp_unslash( $_GET['plugin_details'] ) );
$plugin_name = $plugins_handled[ $plugin_slug ];
$details_link = self_admin_url(
'plugin-install.php?tab=plugin-information&plugin=' . $plugin_slug .
'&TB_iframe=true&width=600&height=550'
);
printf(
'<a href="%1$s" style="display: none;" id="plugin-information-onload" class="thickbox open-plugin-details-modal" data-title="%2$s">%2$s</a>',
esc_url( $details_link ),
esc_attr( $plugin_name )
);
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
setTimeout( function () {
$('#plugin-information-onload').click();
} );
} );
</script>
<?php
}
/**
* Check if WooCommerce.com connection has been made.
*
* @return bool
*/
private static function is_wccom_connected() {
if ( ! class_exists( 'WC_Helper_Options' ) ) {
return false;
}
$auth = WC_Helper_Options::get( 'auth' );
return ! empty( $auth['access_token'] );
}
/**
* Tells if the current site is hosted in wordpress.com and the
* plan includes an active woothemes-sensei paid plan.
*/
public static function has_wpcom_subscription(): bool {
$subscriptions = get_option( 'wpcom_active_subscriptions', [] );
return isset( $subscriptions['woothemes-sensei'] );
}
/**
* Displays a notice for users that needs to install/activate WooCommerce.
*
* @internal
*
* @param array $notices Notices list.
*
* @return array
*/
public static function woocommerce_notice( $notices ) {
if ( Sensei_Utils::is_woocommerce_active() ) {
return $notices;
}
$action = [
'label' => __( 'Activate', 'sensei-compat' ),
'url' => add_query_arg(
'_wpnonce',
wp_create_nonce( 'activate-plugin_woocommerce/woocommerce.php' ),
self_admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php' )
),
];
if ( ! Sensei_Utils::is_woocommerce_installed() ) {
$action = [
'label' => __( 'Install', 'sensei-compat' ),
'url' => add_query_arg(
'_wpnonce',
wp_create_nonce( 'install-plugin_woocommerce' ),
self_admin_url( 'update.php?action=install-plugin&plugin=woocommerce' )
),
];
}
$notices['wcpc-woo-active'] = [
'type' => 'site-wide',
'icon' => 'sensei',
'heading' => __( 'Sensei Pro (WC Paid Courses)', 'sensei-compat' ),
'message' => __( 'WooCommerce must be installed and connected to WooCommerce.com in order to receive plugin updates.', 'sensei-compat' ),
'actions' => [ $action ],
'conditions' => [
[
'type' => 'screens',
'screens' => [ 'sensei*', 'plugins', 'plugins-network' ],
],
[
'type' => 'user_cap',
'capabilities' => [ 'install_plugins', 'activate_plugins' ],
],
],
];
return $notices;
}
/**
* Check if the plugin is licensed .
*
* @internal
*
* @return bool
*/
public static function is_plugin_licensed() {
return self::has_wccom_subscription() || self::has_wpcom_subscription();
}
/**
* Tells if user has a woocommerce.com subscription.
*/
public static function has_wccom_subscription(): bool {
if ( ! self::is_wccom_connected() ) {
return false;
}
if ( ! class_exists( 'WC_Helper' ) ) {
return false;
}
return WC_Helper::has_product_subscription( self::WOO_PRODUCT_ID );
}
/**
* Displays a notice for users that needs to connect the WCCOM.
*
* @internal
*
* @param array $notices Notices list.
*
* @return array Notices including the WCCOM connect notice.
*/
public static function wccom_connect_notice( $notices ) {
if ( ! Sensei_Utils::is_woocommerce_active() || self::is_wccom_connected() || self::has_wpcom_subscription() ) {
return $notices;
}
$connect_url = add_query_arg(
[
'page' => 'wc-addons',
'section' => 'helper',
'wc-helper-connect' => 1,
'wc-helper-nonce' => wp_create_nonce( 'connect' ),
],
admin_url( 'admin.php' )
);
$notices['wcpc-wccom-connect-notice'] = [
'type' => 'site-wide',
'icon' => 'sensei',
'heading' => __( 'Sensei Pro (WC Paid Courses)', 'sensei-compat' ),
'message' => __( 'WooCommerce must be connected to WooCommerce.com in order to receive plugin updates.', 'sensei-compat' ),
'actions' => [
[
'label' => __( 'Connect account', 'sensei-compat' ),
'url' => $connect_url,
],
],
'conditions' => [
[
'type' => 'screens',
'screens' => [ 'sensei*', 'plugins', 'plugins-network' ],
],
[
'type' => 'user_cap',
'capabilities' => [ 'activate_plugins' ],
],
],
];
return $notices;
}
}