| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plausible Analytics | Setup. |
| 4 |
* |
| 5 |
* @since 1.3.0 |
| 6 |
* @package WordPress |
| 7 |
* @subpackage Plausible Analytics |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Plausible\Analytics\WP; |
| 11 |
|
| 12 |
class Setup { |
| 13 |
/** |
| 14 |
* Filters and Hooks. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
register_activation_hook( PLAUSIBLE_ANALYTICS_PLUGIN_FILE, [ $this, 'create_cache_dir' ] ); |
| 20 |
register_activation_hook( PLAUSIBLE_ANALYTICS_PLUGIN_FILE, [ $this, 'activate_cron' ] ); |
| 21 |
register_deactivation_hook( PLAUSIBLE_ANALYTICS_PLUGIN_FILE, [ $this, 'deactivate_cron' ] ); |
| 22 |
|
| 23 |
// Attach the cron script to the cron action. |
| 24 |
add_action( Cron::TASK_NAME, [ $this, 'load_cron_script' ] ); |
| 25 |
|
| 26 |
// This assures that the local file is downloaded/updated when settings are saved. |
| 27 |
add_action( 'plausible_analytics_settings_saved', [ $this, 'load_cron_script' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Create Cache-dir upon (re)activation. |
| 32 |
*/ |
| 33 |
public function create_cache_dir() { |
| 34 |
$upload_dir = Helpers::get_proxy_resource( 'cache_dir' ); |
| 35 |
|
| 36 |
if ( ! is_dir( $upload_dir ) ) { |
| 37 |
wp_mkdir_p( $upload_dir ); // @codeCoverageIgnore |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Register hook to schedule script in wp_cron() |
| 43 |
* |
| 44 |
* @codeCoverageIgnore |
| 45 |
*/ |
| 46 |
public function activate_cron() { |
| 47 |
if ( ! wp_next_scheduled( Cron::TASK_NAME ) ) { |
| 48 |
wp_schedule_event( time(), 'daily', Cron::TASK_NAME ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Deactivate cron when plugin is deactivated. |
| 54 |
* |
| 55 |
* @codeCoverageIgnore |
| 56 |
*/ |
| 57 |
public function deactivate_cron() { |
| 58 |
if ( wp_next_scheduled( Cron::TASK_NAME ) ) { |
| 59 |
wp_clear_scheduled_hook( Cron::TASK_NAME ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Triggers the cron script. |
| 65 |
* |
| 66 |
* @codeCoverageIgnore |
| 67 |
*/ |
| 68 |
public function load_cron_script() { |
| 69 |
new Cron(); |
| 70 |
} |
| 71 |
} |
| 72 |
|