PluginProbe
Plausible Analytics / trunk
Plausible Analytics vtrunk
2.6.1 2.6.0 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 52 releases
plausible-analytics / src / Setup.php

Setup.php in Plausible Analytics trunk, at src/Setup.php

72 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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