| 1 |
<?php |
| 2 |
namespace Depicter\WordPress; |
| 3 |
|
| 4 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 5 |
|
| 6 |
class WPCronServiceProvider implements ServiceProviderInterface |
| 7 |
{ |
| 8 |
|
| 9 |
/** |
| 10 |
* Register all dependencies in the IoC container. |
| 11 |
* |
| 12 |
* @param Container $container |
| 13 |
* |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public function register($container) {} |
| 17 |
|
| 18 |
/** |
| 19 |
* Bootstrap any services if needed. |
| 20 |
* |
| 21 |
* @param Container $container |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function bootstrap($container) |
| 26 |
{ |
| 27 |
|
| 28 |
register_deactivation_hook(DEPICTER_PLUGIN_FILE, [ $this, 'deactivate_cron_jobs'] ); |
| 29 |
|
| 30 |
add_action( 'init', [ $this, 'set_cron_jobs' ] ); |
| 31 |
add_action( 'depicter_check_authorize', [ $this, 'check_user_authorize' ] ); |
| 32 |
add_action( 'depicter_validate_activation', [ $this, 'validateActivation' ], 10 ); |
| 33 |
add_action( 'depicter_collect_usage_data', [ $this, 'collectData' ], 10, 1 ); |
| 34 |
|
| 35 |
add_action('wp_login', [$this, 'on_admin_login'], 10, 2); |
| 36 |
} |
| 37 |
|
| 38 |
public function on_admin_login($user_login, $user){ |
| 39 |
if (in_array('administrator', $user->roles)) { |
| 40 |
|
| 41 |
$this->validateActivation(); |
| 42 |
$this->reschedule_hook('depicter_validate_activation'); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Set cron jobs |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function set_cron_jobs() { |
| 52 |
if ( ! wp_next_scheduled( 'depicter_check_authorize' ) ) { |
| 53 |
wp_schedule_event( time(), 'twicedaily', 'depicter_check_authorize' ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! wp_next_scheduled( 'depicter_validate_activation' ) ) { |
| 57 |
wp_schedule_event( time(), 'twicedaily', 'depicter_validate_activation' ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! wp_next_scheduled( 'depicter_collect_usage_data' ) ) { |
| 61 |
wp_schedule_event( time(), 'daily', 'depicter_collect_usage_data' ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* check if user has purchased a license or not |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function check_user_authorize() { |
| 71 |
\Depicter::client()->authorize(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Validate plugin activation |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function validateActivation() { |
| 80 |
\Depicter::client()->validateActivation(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Remove cron jobs on plugin deactivation |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function deactivate_cron_jobs() { |
| 89 |
$timestamp = wp_next_scheduled( 'depicter_check_authorize' ); |
| 90 |
wp_unschedule_event( $timestamp, 'depicter_check_authorize' ); |
| 91 |
|
| 92 |
$timestamp = wp_next_scheduled( 'depicter_validate_activation' ); |
| 93 |
wp_unschedule_event( $timestamp, 'depicter_validate_activation' ); |
| 94 |
|
| 95 |
$timestamp = wp_next_scheduled( 'depicter_collect_usage_data' ); |
| 96 |
wp_unschedule_event( $timestamp, 'depicter_collect_usage_data' ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Collect usage date if user accepts collecting data consent |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function collectData() { |
| 104 |
\Depicter::usageService()->collect(); |
| 105 |
\Depicter::usageService()->send(); |
| 106 |
} |
| 107 |
|
| 108 |
private function reschedule_hook($hook, $recurrence = 'twicedaily') { |
| 109 |
wp_clear_scheduled_hook($hook); |
| 110 |
wp_schedule_event(time(), $recurrence, $hook); |
| 111 |
} |
| 112 |
} |
| 113 |
|