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