PluginProbe
YayMail – WooCommerce Email Customizer / 3.3.6
YayMail – WooCommerce Email Customizer v3.3.6
4.4.6 4.4.5 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 All 57 releases
yaymail / includes / License / LicenseHandler.php

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

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