Skip to content

Commit e3e79b6

Browse files
author
Woo
committed
Updates to 4.23.1.1.23.1
1 parent 34ddb38 commit e3e79b6

File tree

2,793 files changed

+352996
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,793 files changed

+352996
-0
lines changed

LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

changelog.txt

Lines changed: 786 additions & 0 deletions
Large diffs are not rendered by default.

class-sensei-compat-admin.php

Lines changed: 378 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
1+
<?php
2+
/**
3+
* File containing the class Sensei_Compat_Admin.
4+
*
5+
* @package sensei-compat
6+
* @since 1.0.0
7+
*/
8+
9+
/**
10+
* Sensei_Compat_Admin class.
11+
*/
12+
class Sensei_Compat_Admin {
13+
const WOO_PRODUCT_ID = 152116;
14+
15+
/**
16+
* Initialize admin actions and filters.
17+
*/
18+
public static function init() {
19+
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 3 );
20+
add_filter( 'install_plugins_search', array( __CLASS__, 'load_plugin_information' ) );
21+
add_filter( 'sensei_admin_notices', [ __CLASS__, 'wccom_connect_notice' ] );
22+
add_filter( 'sensei_admin_notices', [ __CLASS__, 'woocommerce_notice' ] );
23+
add_filter( 'sensei_home_is_plugin_licensed_woothemes-sensei', [ __CLASS__, 'is_plugin_licensed' ] );
24+
25+
if ( SENSEI_COMPAT_LOADING_SENSEI ) {
26+
add_filter( 'site_transient_update_plugins', array( __CLASS__, 'add_sensei_translations' ) );
27+
add_action( 'set_site_transient_update_plugins', array( __CLASS__, 'clear_sensei_translations' ) );
28+
}
29+
}
30+
31+
/**
32+
* Adds Sensei's language pack updates to the `update_plugins` transient.
33+
*
34+
* @access private
35+
*
36+
* @param \stdClass $value Current value of `update_plugins` transient.
37+
* @return \stdClass
38+
*/
39+
public static function add_sensei_translations( $value ) {
40+
if ( empty( $value ) ) {
41+
return $value;
42+
}
43+
44+
$language_pack_transient_key = self::get_translation_update_cache_key();
45+
46+
$translations_available = get_site_transient( $language_pack_transient_key );
47+
if ( ! $translations_available ) {
48+
$translations_available = self::get_sensei_language_pack_updates();
49+
set_site_transient( $language_pack_transient_key, $translations_available, DAY_IN_SECONDS );
50+
}
51+
52+
foreach ( $translations_available as $locale => $package ) {
53+
$value->translations[] = $package;
54+
}
55+
56+
return $value;
57+
}
58+
59+
/**
60+
* Clear's Sensei language pack update cache.
61+
*
62+
* @access private
63+
*/
64+
public static function clear_sensei_translations() {
65+
delete_site_transient( self::get_translation_update_cache_key() );
66+
}
67+
68+
/**
69+
* Gets the cache key for the current Sensei translation update cache.
70+
*
71+
* @return string
72+
*/
73+
private static function get_translation_update_cache_key() {
74+
$cache_key_parts = array_values( get_available_languages() );
75+
$cache_key_parts[] = Sensei()->version;
76+
77+
return 'sensei_language_packs_' . md5( implode( ',', $cache_key_parts ) );
78+
}
79+
80+
/**
81+
* Gets the available language package updates.
82+
*
83+
* @return array
84+
*/
85+
private static function get_sensei_language_pack_updates() {
86+
global $wp_version;
87+
88+
static $plugin_translations;
89+
90+
if ( isset( $plugin_translations ) ) {
91+
return $plugin_translations;
92+
}
93+
94+
$plugin_translations = array();
95+
$installed_translations_raw = wp_get_installed_translations( 'plugins' );
96+
$installed_translations = array();
97+
98+
// Only pass translations installed for Sensei.
99+
if ( isset( $installed_translations_raw['sensei-lms'] ) ) {
100+
$installed_translations['sensei-lms'] = $installed_translations_raw['sensei-lms'];
101+
}
102+
103+
$to_send = array();
104+
$to_send['plugins'] = array(
105+
'sensei-lms/sensei-lms.php' => array(
106+
'Name' => 'Sensei LMS',
107+
'Title' => 'Sensei LMS',
108+
'Version' => Sensei()->version,
109+
'TextDomain' => 'sensei-lms',
110+
),
111+
);
112+
$to_send['active'] = array( 'sensei-lms/sensei-lms.php' );
113+
114+
$locales = array_values( get_available_languages() );
115+
116+
/** This action is documented in WordPress core's wp-includes/update.php */
117+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
118+
$locales = apply_filters( 'plugins_update_check_locales', $locales );
119+
$locales = array_unique( $locales );
120+
121+
$options = array(
122+
'timeout' => 10,
123+
'body' => array(
124+
'plugins' => wp_json_encode( $to_send ),
125+
'translations' => wp_json_encode( $installed_translations ),
126+
'locale' => wp_json_encode( $locales ),
127+
'all' => wp_json_encode( true ),
128+
),
129+
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
130+
);
131+
132+
$url = 'http://api.wordpress.org/plugins/update-check/1.1/';
133+
$ssl = wp_http_supports( array( 'ssl' ) );
134+
if ( $ssl ) {
135+
$url = set_url_scheme( $url, 'https' );
136+
}
137+
$raw_response = wp_remote_post( $url, $options );
138+
if ( is_wp_error( $raw_response ) || 200 !== intval( wp_remote_retrieve_response_code( $raw_response ) ) ) {
139+
return array();
140+
}
141+
142+
$response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
143+
if ( is_array( $response ) && ! empty( $response['translations'] ) ) {
144+
$plugin_translations = $response['translations'];
145+
}
146+
147+
return $plugin_translations;
148+
}
149+
150+
/**
151+
* Adds details about the plugins packaged within this compatibility plugin.
152+
*
153+
* @param string[] $plugin_meta An array of the plugin's metadata,
154+
* including the version, author,
155+
* author URI, and plugin URI.
156+
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
157+
* @param array $plugin_data An array of plugin data.
158+
*
159+
* @return string[]
160+
*/
161+
public static function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data ) {
162+
if ( ! in_array( $plugin_file, [ 'woothemes-sensei/woothemes-sensei.php', 'sensei/woothemes-sensei.php' ], true ) ) {
163+
return $plugin_meta;
164+
}
165+
166+
if ( 'sensei-compat' !== $plugin_data['TextDomain'] ) {
167+
return $plugin_meta;
168+
}
169+
170+
unset( $plugin_meta[0] );
171+
172+
if ( SENSEI_COMPAT_LOADING_SENSEI_PRO && defined( 'SENSEI_PRO_VERSION' ) ) {
173+
// translators: placeholder is current version of Sensei Pro.
174+
array_unshift( $plugin_meta, esc_html( sprintf( __( 'Sensei Pro Version: %s', 'sensei-compat' ), SENSEI_PRO_VERSION ) ) );
175+
}
176+
177+
if ( SENSEI_COMPAT_LOADING_SENSEI && function_exists( 'Sensei' ) ) {
178+
// translators: placeholder is current version of Sensei.
179+
array_unshift( $plugin_meta, esc_html( sprintf( __( 'Sensei LMS Version: %s', 'sensei-compat' ), Sensei()->version ) ) );
180+
}
181+
182+
return $plugin_meta;
183+
}
184+
185+
/**
186+
* Manually load the plugin information on `plugin-install.php` page load.
187+
*/
188+
public static function load_plugin_information() {
189+
$plugins_handled = [
190+
'sensei-lms' => 'Sensei LMS',
191+
'woocommerce' => 'WooCommerce',
192+
];
193+
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
194+
if ( empty( $_GET['plugin_details'] ) || ! isset( $plugins_handled[ $_GET['plugin_details'] ] ) ) {
195+
return;
196+
}
197+
198+
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
199+
$plugin_slug = sanitize_title( wp_unslash( $_GET['plugin_details'] ) );
200+
$plugin_name = $plugins_handled[ $plugin_slug ];
201+
202+
$details_link = self_admin_url(
203+
'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin_slug .
204+
'&amp;TB_iframe=true&amp;width=600&amp;height=550'
205+
);
206+
207+
printf(
208+
'<a href="%1$s" style="display: none;" id="plugin-information-onload" class="thickbox open-plugin-details-modal" data-title="%2$s">%2$s</a>',
209+
esc_url( $details_link ),
210+
esc_attr( $plugin_name )
211+
);
212+
213+
?>
214+
<script type="text/javascript">
215+
jQuery( document ).ready( function( $ ) {
216+
setTimeout( function () {
217+
$('#plugin-information-onload').click();
218+
} );
219+
} );
220+
</script>
221+
<?php
222+
}
223+
224+
/**
225+
* Check if WooCommerce.com connection has been made.
226+
*
227+
* @return bool
228+
*/
229+
private static function is_wccom_connected() {
230+
if ( ! class_exists( 'WC_Helper_Options' ) ) {
231+
return false;
232+
}
233+
234+
$auth = WC_Helper_Options::get( 'auth' );
235+
236+
return ! empty( $auth['access_token'] );
237+
}
238+
239+
/**
240+
* Tells if the current site is hosted in wordpress.com and the
241+
* plan includes an active woothemes-sensei paid plan.
242+
*/
243+
public static function has_wpcom_subscription(): bool {
244+
$subscriptions = get_option( 'wpcom_active_subscriptions', [] );
245+
246+
return isset( $subscriptions['woothemes-sensei'] );
247+
}
248+
249+
/**
250+
* Displays a notice for users that needs to install/activate WooCommerce.
251+
*
252+
* @internal
253+
*
254+
* @param array $notices Notices list.
255+
*
256+
* @return array
257+
*/
258+
public static function woocommerce_notice( $notices ) {
259+
if ( Sensei_Utils::is_woocommerce_active() ) {
260+
return $notices;
261+
}
262+
263+
$action = [
264+
'label' => __( 'Activate', 'sensei-compat' ),
265+
'url' => add_query_arg(
266+
'_wpnonce',
267+
wp_create_nonce( 'activate-plugin_woocommerce/woocommerce.php' ),
268+
self_admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php' )
269+
),
270+
];
271+
272+
if ( ! Sensei_Utils::is_woocommerce_installed() ) {
273+
$action = [
274+
'label' => __( 'Install', 'sensei-compat' ),
275+
'url' => add_query_arg(
276+
'_wpnonce',
277+
wp_create_nonce( 'install-plugin_woocommerce' ),
278+
self_admin_url( 'update.php?action=install-plugin&plugin=woocommerce' )
279+
),
280+
];
281+
}
282+
283+
$notices['wcpc-woo-active'] = [
284+
'type' => 'site-wide',
285+
'icon' => 'sensei',
286+
'heading' => __( 'Sensei Pro (WC Paid Courses)', 'sensei-compat' ),
287+
'message' => __( 'WooCommerce must be installed and connected to WooCommerce.com in order to receive plugin updates.', 'sensei-compat' ),
288+
'actions' => [ $action ],
289+
'conditions' => [
290+
[
291+
'type' => 'screens',
292+
'screens' => [ 'sensei*', 'plugins', 'plugins-network' ],
293+
],
294+
[
295+
'type' => 'user_cap',
296+
'capabilities' => [ 'install_plugins', 'activate_plugins' ],
297+
],
298+
],
299+
];
300+
301+
return $notices;
302+
}
303+
/**
304+
* Check if the plugin is licensed .
305+
*
306+
* @internal
307+
*
308+
* @return bool
309+
*/
310+
public static function is_plugin_licensed() {
311+
return self::has_wccom_subscription() || self::has_wpcom_subscription();
312+
}
313+
314+
/**
315+
* Tells if user has a woocommerce.com subscription.
316+
*/
317+
public static function has_wccom_subscription(): bool {
318+
if ( ! self::is_wccom_connected() ) {
319+
return false;
320+
}
321+
322+
if ( ! class_exists( 'WC_Helper' ) ) {
323+
return false;
324+
}
325+
326+
return WC_Helper::has_product_subscription( self::WOO_PRODUCT_ID );
327+
}
328+
329+
/**
330+
* Displays a notice for users that needs to connect the WCCOM.
331+
*
332+
* @internal
333+
*
334+
* @param array $notices Notices list.
335+
*
336+
* @return array Notices including the WCCOM connect notice.
337+
*/
338+
public static function wccom_connect_notice( $notices ) {
339+
if ( ! Sensei_Utils::is_woocommerce_active() || self::is_wccom_connected() || self::has_wpcom_subscription() ) {
340+
return $notices;
341+
}
342+
343+
$connect_url = add_query_arg(
344+
[
345+
'page' => 'wc-addons',
346+
'section' => 'helper',
347+
'wc-helper-connect' => 1,
348+
'wc-helper-nonce' => wp_create_nonce( 'connect' ),
349+
],
350+
admin_url( 'admin.php' )
351+
);
352+
353+
$notices['wcpc-wccom-connect-notice'] = [
354+
'type' => 'site-wide',
355+
'icon' => 'sensei',
356+
'heading' => __( 'Sensei Pro (WC Paid Courses)', 'sensei-compat' ),
357+
'message' => __( 'WooCommerce must be connected to WooCommerce.com in order to receive plugin updates.', 'sensei-compat' ),
358+
'actions' => [
359+
[
360+
'label' => __( 'Connect account', 'sensei-compat' ),
361+
'url' => $connect_url,
362+
],
363+
],
364+
'conditions' => [
365+
[
366+
'type' => 'screens',
367+
'screens' => [ 'sensei*', 'plugins', 'plugins-network' ],
368+
],
369+
[
370+
'type' => 'user_cap',
371+
'capabilities' => [ 'activate_plugins' ],
372+
],
373+
],
374+
];
375+
376+
return $notices;
377+
}
378+
}

0 commit comments

Comments
 (0)