PluginProbe
YayMail – WooCommerce Email Customizer / 3.2.2
YayMail – WooCommerce Email Customizer v3.2.2
4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 3.4.2 3.4.3 All 55 releases
yaymail / includes / License / LicenseHandler.php

LicenseHandler.php in YayMail – WooCommerce Email Customizer 3.2.2, at includes/License/LicenseHandler.php

374 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\License;
4
5 use YayMail\License\EDD_SL_Plugin_Updater;
6 use YayMail\License\RestAPI;
7 use YayMail\License\CorePlugin;
8
9 defined( 'ABSPATH' ) || exit;
10
11 if ( ! defined( 'YAYCOMMERCE_SELLER_SITE_URL' ) ) {
12 define( 'YAYCOMMERCE_SELLER_SITE_URL', 'https://yaycommerce.com/' );
13 }
14
15 class LicenseHandler {
16 protected static $instance = null;
17
18 public static function get_instance() {
19 if ( null === self::$instance ) {
20 self::$instance = new self();
21 }
22 return self::$instance;
23 }
24
25 protected function __construct() {
26 new RestAPI();
27 $this->do_hooks();
28 $this->do_cron_job();
29 $this->do_post_requests();
30 $this->show_plugin_page_notification();
31 $this->license_set_site_transient();
32 }
33
34 public function do_hooks() {
35 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_license_scripts' ) );
36 add_filter( 'woocommerce_settings_tabs_array', array( $this, 'yaycommerce_licenses_setting_tab' ), 100, 1 );
37 add_action( 'woocommerce_settings_yaycommerce_licenses', array( $this, 'yaycommerce_licenses_settings' ), 100 );
38
39 /** Expired license admin notice */
40 add_action( 'admin_notices', array( $this, 'license_expired_admin_notice' ) );
41
42 $licensing_plugins = $this->get_licensing_plugins();
43 foreach ( $licensing_plugins as $plugin ) {
44 $license = new License( $plugin['slug'] );
45 if ( ! $license->is_active() || $license->is_expired() ) {
46 /** Add plugin action when expired license */
47 add_filter( 'plugin_action_links_' . $plugin['basename'], array( $this, 'edit_action_links' ) );
48 }
49 }
50
51 /** Filter for view changelog */
52 add_filter( 'plugins_api', array( $this, 'change_plugin_data' ), 21, 3 );
53
54 /** Auto update */
55 add_action( 'admin_init', array( $this, 'auto_update' ) );
56 add_filter( 'auto_update_plugin', array( $this, 'add_disabled_auto_update_text' ), 100, 2 );
57 }
58
59 public function do_cron_job() {
60 add_filter( 'cron_schedules', array( $this, 'custom_schedules' ) );
61 add_action( 'check_license_cron', array( $this, 'check_license_cron_run' ) );
62 if ( ! wp_next_scheduled( 'check_license_cron' ) ) {
63 wp_schedule_event( time(), 'daily', 'check_license_cron' );
64 };
65
66 add_action( 'get_version_cron', array( $this, 'get_version_cron_run' ) );
67 if ( ! wp_next_scheduled( 'get_version_cron' ) ) {
68 wp_schedule_event( time(), '3hours', 'get_version_cron' );
69 };
70 }
71
72 public function custom_schedules( $schedules ) {
73 $schedules['3hours'] = array(
74 'interval' => 60 * 60 * 3,
75 'display' => 'Three Hours',
76 );
77 return $schedules;
78 }
79
80 public function check_license_cron_run() {
81 $licensing_plugins = $this->get_licensing_plugins();
82 foreach ( $licensing_plugins as $plugin ) {
83 $license = new License( $plugin['slug'] );
84 $license->update();
85 }
86 }
87
88 public function get_version_cron_run() {
89 $licensing_plugins = $this->get_licensing_plugins();
90 foreach ( $licensing_plugins as $plugin ) {
91 $licensing_plugin = new LicensingPlugin( $plugin['slug'] );
92 $licensing_plugin->update_version_info();
93 }
94 }
95
96 public function enqueue_license_scripts() {
97 if ( ! isset( $_GET['page'] ) || ! 'wc-settings' === $_GET['page'] || ! isset( $_GET['tab'] ) || ! 'yaycommerce_licenses' === $_GET['tab'] ) {
98 return;
99 }
100 wp_enqueue_style( CorePlugin::get( 'slug' ) . '-license-style', CorePlugin::get( 'url' ) . 'assets/admin/css/license.css', array(), CorePlugin::get( 'version' ) );
101 wp_enqueue_script( CorePlugin::get( 'slug' ) . '-license-script', CorePlugin::get( 'url' ) . 'assets/admin/js/license.js', array(), CorePlugin::get( 'version' ), true );
102 wp_localize_script(
103 CorePlugin::get( 'slug' ) . '-license-script',
104 CorePlugin::get( 'slug' ) . 'LicenseData',
105 array(
106 'apiSettings' => array(
107 'restNonce' => wp_create_nonce( 'wp_rest' ),
108 'restUrl' => esc_url_raw( rest_url( CorePlugin::get( 'slug' ) . '-license/v1' ) ),
109 'adminUrl' => admin_url(),
110 ),
111 )
112 );
113 }
114
115 public function yaycommerce_licenses_setting_tab( $settings_array ) {
116 $licensing_plugins = $this->get_licensing_plugins();
117 if ( count( $licensing_plugins ) > 0 ) {
118 $settings_array = array_merge( $settings_array, array( 'yaycommerce_licenses' => 'YayCommerce Licenses' ) );
119 }
120 return $settings_array;
121 }
122
123 public function yaycommerce_licenses_settings() {
124 $GLOBALS['hide_save_button'] = true;
125 add_action(
126 'woocommerce_after_settings_yaycommerce_licenses',
127 function() {
128 $GLOBALS['hide_save_button'] = '';
129 }
130 );
131 require CorePlugin::get( 'path' ) . 'views/license/setting-page.php';
132 }
133
134 public static function get_plugin_data( $plugin_info ) {
135 if ( ! function_exists( 'get_plugin_data' ) ) {
136 require_once ABSPATH . 'wp-admin/includes/plugin.php';
137 }
138 $plugin_data = get_plugin_data( $plugin_info['dir_path'] . basename( $plugin_info['basename'] ) );
139 return $plugin_data;
140 }
141
142 public static function get_licensing_plugins() {
143 $plugins = array();
144 return apply_filters( CorePlugin::get( 'slug' ) . '_available_licensing_plugins', $plugins );
145 }
146
147 public function do_remove_license_request() {
148 if ( isset( $_POST['nonce'] ) ) {
149 wp_verify_nonce( $_POST['nonce'] );
150 }
151 $licensing_plugin_slug = isset( $_POST[ CorePlugin::get( 'slug' ) . '_licensing_plugin' ] ) ? sanitize_text_field( $_POST[ CorePlugin::get( 'slug' ) . '_licensing_plugin' ] ) : '';
152 $license = new License( $licensing_plugin_slug );
153 $license->remove_license_key();
154 $license->remove_license_info();
155 }
156
157 public function do_post_requests() {
158 if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {
159 if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['_wpnonce'] ), 'woocommerce-settings' ) ) {
160 if ( isset( $_POST[ CorePlugin::get( 'slug' ) . '_remove_license' ] ) ) {
161 $this->do_remove_license_request();
162 }
163 }
164 }
165 }
166
167 /** NOTIFICATION */
168 public function license_expired_admin_notice() {
169 require CorePlugin::get( 'path' ) . 'views/license/expired-admin-notice.php';
170 }
171
172 public function show_plugin_page_notification() {
173 $licensing_plugins = $this->get_licensing_plugins();
174 foreach ( $licensing_plugins as $plugin ) {
175 add_action( 'after_plugin_row_' . $plugin['basename'], array( $this, 'plugin_notifications' ), 10, 2 );
176 }
177 }
178
179 public function plugin_notifications( $file, $plugin_info ) {
180 $wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' );
181 $licensing_plugins = $this->get_licensing_plugins();
182 $match_position = array_search( $file, array_column( $licensing_plugins, 'basename' ) );
183 if ( false === $match_position ) {
184 return;
185 }
186 $plugin = $licensing_plugins[ $match_position ];
187 $licensing_plugin = new LicensingPlugin( $plugin['slug'] );
188 $license = $licensing_plugin->get_license();
189 $plugin_version_info = $licensing_plugin->get_version_info();
190 if ( ! $license->is_active() || $license->is_expired() ) {
191 $new_version = isset( $plugin_version_info['new_version'] ) ? $plugin_version_info['new_version'] : '0';
192 ?>
193 <script>
194 var plugin_row_element = document.querySelector('tr[data-plugin="<?php echo esc_js( plugin_basename( $file ) ); ?>"]');
195 plugin_row_element.classList.add('update');
196 </script>
197 <tr class="plugin-update-tr <?php echo esc_attr( is_plugin_active( $file ) ? 'active' : '' ); ?>">
198 <td colspan="<?php echo esc_attr( $wp_list_table->get_column_count() ); ?>" class="plugin-update colspanchange">
199 <?php
200 if ( $plugin_info['Version'] < $new_version ) {
201 require CorePlugin::get( 'path' ) . 'views/license/update-plugin-notification.php';
202 }
203 if ( ! $license->is_active() ) {
204 require CorePlugin::get( 'path' ) . 'views/license/not-activate-license-notification.php';
205 }
206 if ( $license->is_expired() ) {
207 require CorePlugin::get( 'path' ) . 'views/license/expired-license-notification.php';
208 }
209 }
210 }
211
212 /** Plugin action link */
213 public function edit_action_links( $action_links ) {
214 $new_action_links = array(
215 'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=yaycommerce_licenses' ) . '" aria-label="' . esc_attr( 'View YayCommerce license settings' ) . '">' . esc_html( 'Enter license key' ) . '</a>',
216 );
217 return array_merge( $new_action_links, $action_links );
218 }
219
220 public function change_plugin_data( $_data, $_action = '', $_args = null ) {
221 if ( ! isset( $_args->slug ) ) {
222 return $_data;
223 }
224 $licensing_plugins = $this->get_licensing_plugins();
225 $match_position = array_search( $_args->slug, array_column( $licensing_plugins, 'basename' ) );
226 if ( false === $match_position ) {
227 return $_data;
228 }
229
230 $plugin = $licensing_plugins[ $match_position ];
231 $licensing_plugin = new LicensingPlugin( $plugin['slug'] );
232 $license = $licensing_plugin->get_license();
233
234 if ( $license->is_active() && ! $license->is_expired() ) {
235 return $_data;
236 }
237
238 $plugin_data = self::get_plugin_data( $plugin );
239
240 $plugin_version_info = $licensing_plugin->get_version_info();
241
242 $_data = (object) $plugin_version_info;
243 $sites = get_site_transient( 'update_plugins' );
244 if ( ! isset( $sites->response[ $plugin['basename'] ] ) ) {
245 $sites->response[ $plugin['basename'] ] = $_data;
246 set_site_transient( 'update_plugins', $sites );
247 }
248
249 $_data->version = $_data->new_version;
250 $_data->author = $plugin_data['AuthorName'];
251 $_data->author_profile = $plugin_data['AuthorURI'];
252 $_data->download_link = '#';
253
254 add_action(
255 'admin_head',
256 function() use ( $plugin ) {
257 ?>
258 <script>
259 jQuery(document).ready(function(){
260 var update_a_tag = document.querySelector('a[data-plugin="<?php echo esc_js( $plugin['basename'] ); ?>"]#plugin_update_from_iframe');
261 update_a_tag.innerHTML = '<?php echo esc_html( 'Active your license to update' ); ?>';
262 update_a_tag.href = '<?php echo esc_url( admin_url() ) . 'admin.php?page=wc-settings&tab=yaycommerce_licenses'; ?>';
263 update_a_tag.id = '<?php echo esc_attr( CorePlugin::get( 'slug' ) ); ?>-activate-license';
264 update_a_tag.target = '_blank';
265 })
266 </script>
267 <?php
268 },
269 100
270 );
271
272 if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
273 $_data->sections = maybe_unserialize( $_data->sections );
274 }
275
276 if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
277 $_data->banners = maybe_unserialize( $_data->banners );
278 }
279
280 if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
281 $_data->icons = maybe_unserialize( $_data->icons );
282 }
283
284 if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) {
285 $_data->contributors = $this->convert_object_to_array( $_data->contributors );
286 }
287
288 if ( ! isset( $_data->plugin ) ) {
289 $_data->plugin = $plugin['basename'];
290 }
291
292 return $_data;
293 }
294
295 public function convert_object_to_array( $data ) {
296 $new_data = array();
297 foreach ( $data as $key => $value ) {
298 $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value;
299 }
300 return $new_data;
301 }
302
303 /** Auto update */
304 public function auto_update() {
305 $doing_cron = defined( 'DOING_CRON' ) && DOING_CRON;
306 if ( ! current_user_can( 'manage_options' ) && ! $doing_cron ) {
307 return;
308 }
309 $licensing_plugins = $this->get_licensing_plugins();
310 foreach ( $licensing_plugins as $plugin ) {
311 $license = new License( $plugin['slug'] );
312 if ( $license->is_active() && ! $license->is_expired() ) {
313 $_file = $plugin['dir_path'] . basename( $plugin['basename'] );
314
315 $plugin_data = self::get_plugin_data( $plugin );
316 $license_key = $license->get_license_key();
317 $args = array(
318 'version' => $plugin_data['Version'],
319 'license' => $license_key,
320 'author' => $plugin_data['AuthorName'],
321 'item_id' => $plugin['item_id'],
322 );
323 $edd_updater = new EDD_SL_Plugin_Updater(
324 YAYCOMMERCE_SELLER_SITE_URL,
325 $_file,
326 $args
327 );
328 }
329 }
330 }
331
332 public function license_set_site_transient() {
333 global $pagenow;
334 if ( 'plugin-install.php' === $pagenow ) {
335 return;
336 }
337 if ( ! function_exists( 'get_plugin_data' ) ) {
338 require_once ABSPATH . 'wp-admin/includes/plugin.php';
339 }
340 $licensing_plugins = $this->get_licensing_plugins();
341 $site_transient_update_plugins = get_site_transient( 'update_plugins' );
342 $check = false;
343
344 foreach ( $licensing_plugins as $plugin ) {
345 $license = new License( $plugin['slug'] );
346 if ( ! $license->is_active() || $license->is_expired() ) {
347 if ( isset( $site_transient_update_plugins->response[ $plugin['basename'] ] )
348 || isset( $site_transient_update_plugins->no_update[ $plugin['basename'] ] )
349 ) {
350 $check = true;
351 unset( $site_transient_update_plugins->response[ $plugin['basename'] ] );
352 unset( $site_transient_update_plugins->no_update[ $plugin['basename'] ] );
353 }
354 }
355 }
356 if ( false !== $site_transient_update_plugins && $check ) {
357 set_site_transient( 'update_plugins', $site_transient_update_plugins );
358 }
359 }
360
361 public function add_disabled_auto_update_text( $value, $plugin_info ) {
362 $licensing_plugins = $this->get_licensing_plugins();
363 foreach ( $licensing_plugins as $plugin ) {
364 if ( $plugin['basename'] === $plugin_info->plugin ) {
365 $license = new License( $plugin['slug'] );
366 if ( ! $license->is_active() || $license->is_expired() ) {
367 return false;
368 }
369 }
370 }
371 return $value;
372 }
373 }
374