| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable |
| 2 |
/** |
| 3 |
* Plugin Lifecycle Component for WP Analytify |
| 4 |
* |
| 5 |
* This file contains all plugin activation, deactivation, and uninstall functions |
| 6 |
* that were previously in the main plugin file. |
| 7 |
* |
| 8 |
* @package WP_Analytify |
| 9 |
* @since 8.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Plugin Lifecycle Component Class |
| 18 |
*/ |
| 19 |
class Analytify_Plugin_Lifecycle { |
| 20 |
|
| 21 |
/** |
| 22 |
* Main plugin instance |
| 23 |
* |
| 24 |
* @var WP_Analytify |
| 25 |
*/ |
| 26 |
private $analytify; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor |
| 30 |
* |
| 31 |
* @param WP_Analytify $analytify Main plugin instance. |
| 32 |
*/ |
| 33 |
public function __construct( $analytify ) { |
| 34 |
$this->analytify = $analytify; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Run on plugin activation. |
| 39 |
* |
| 40 |
* @since 1.2.2 |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function activate() { |
| 44 |
|
| 45 |
// update version. |
| 46 |
if ( ! get_option( 'pa_google_token' ) ) { |
| 47 |
update_option( 'wpa_current_version', '2.1.2' ); |
| 48 |
} |
| 49 |
|
| 50 |
// Return if settings already added in DB. |
| 51 |
$_admin_settings = get_option( 'wp-analytify-admin' ); |
| 52 |
if ( $_admin_settings && 'on' === $_admin_settings['enable_back_end'] && ! empty( $_admin_settings['show_analytics_roles_back_end'] ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Load default settings on new install. |
| 57 |
if ( ! get_option( 'analytify_default_settings' ) ) { |
| 58 |
$profile_tab_settings = array( |
| 59 |
'exclude_users_tracking' => array( 'administrator' ), |
| 60 |
); |
| 61 |
|
| 62 |
update_option( 'wp-analytify-profile', $profile_tab_settings ); |
| 63 |
|
| 64 |
$admin_tab_settings = array( |
| 65 |
'enable_back_end' => 'on', |
| 66 |
'show_analytics_roles_back_end' => array( 'administrator', 'editor' ), |
| 67 |
'show_analytics_post_types_back_end' => array( 'post', 'page' ), |
| 68 |
'show_panels_back_end' => array( 'show-overall-dashboard', 'show-social-dashboard', 'show-geographic-dashboard', 'show-system-stats', 'show-keywords-dashboard', 'show-referrer-dashboard' ), |
| 69 |
); |
| 70 |
|
| 71 |
update_option( 'wp-analytify-admin', $admin_tab_settings ); |
| 72 |
|
| 73 |
$dashboard_tab_settings['show_analytics_panels_dashboard'] = array( |
| 74 |
'show-real-time', |
| 75 |
'show-compare-stats', |
| 76 |
'show-overall-dashboard', |
| 77 |
'show-top-pages-dashboard', |
| 78 |
'show-geographic-dashboard', |
| 79 |
'show-system-stats', |
| 80 |
'show-keywords-dashboard', |
| 81 |
'show-social-dashboard', |
| 82 |
'show-referrer-dashboard', |
| 83 |
'show-page-stats-dashboard', |
| 84 |
); |
| 85 |
|
| 86 |
$dashboard_tab_settings['show_analytics_roles_dashboard'] = array( 'administrator' ); |
| 87 |
|
| 88 |
update_option( 'wp-analytify-dashboard', $dashboard_tab_settings ); |
| 89 |
|
| 90 |
$advanced_tab_settings = array( |
| 91 |
'gtag_tracking_mode' => 'gtag', |
| 92 |
'google_analytics_version' => 'ga4', |
| 93 |
); |
| 94 |
|
| 95 |
update_option( 'wp-analytify-advanced', $advanced_tab_settings ); |
| 96 |
|
| 97 |
// Update meta so default settings load only one time. |
| 98 |
update_option( 'analytify_default_settings', 'done' ); |
| 99 |
|
| 100 |
update_option( 'analytify_active_date', gmdate( 'l jS F Y h:i:s A' ) . date_default_timezone_get() ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Delete option values on plugin deactivation. |
| 106 |
* |
| 107 |
* @since 1.2.2 |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function deactivate() { |
| 111 |
// Delete welcome page check on de-activate. |
| 112 |
delete_option( 'show_welcome_page' ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Delete plugin settings meta on deleting the plugin |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function uninstall() { |
| 121 |
|
| 122 |
require_once defined( 'WP_ANALYTIFY_PLUGIN_DIR' ) ? WP_ANALYTIFY_PLUGIN_DIR : dirname( __DIR__ ) . '/classes/analytify-utils.php'; |
| 123 |
|
| 124 |
$remove_settings_on_uninstall = WPANALYTIFY_Utils::get_option( 'uninstall_analytify_settings', 'wp-analytify-advanced', false ); |
| 125 |
|
| 126 |
if ( $remove_settings_on_uninstall && 'on' === $remove_settings_on_uninstall ) { |
| 127 |
|
| 128 |
require_once defined( 'WP_ANALYTIFY_PLUGIN_DIR' ) ? WP_ANALYTIFY_PLUGIN_DIR : dirname( __DIR__ ) . '/classes/analytify-factory-reset.php'; |
| 129 |
|
| 130 |
// Remove all the settings on uninstall. |
| 131 |
( new Analytify_Factory_Reset() )->remove_settings(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Send status of subscriber who opt-in for improving the product. |
| 137 |
* |
| 138 |
* @param string $email Users email. |
| 139 |
* @param string $status Plugin status. |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function send_status( $email, $status ) { |
| 143 |
$url = 'https://analytify.io/plugin-manager/'; |
| 144 |
|
| 145 |
if ( '' === $email ) { |
| 146 |
$email = 'track@analytify.io'; |
| 147 |
} |
| 148 |
|
| 149 |
$fields = array( |
| 150 |
'email' => $email, |
| 151 |
'site' => get_site_url(), |
| 152 |
'status' => $status, |
| 153 |
'type' => 'FREE', |
| 154 |
); |
| 155 |
|
| 156 |
wp_remote_post( |
| 157 |
$url, |
| 158 |
array( |
| 159 |
'method' => 'POST', |
| 160 |
'timeout' => 5, |
| 161 |
'httpversion' => '1.0', |
| 162 |
'blocking' => false, |
| 163 |
'headers' => array(), |
| 164 |
'body' => $fields, |
| 165 |
) |
| 166 |
); |
| 167 |
} |
| 168 |
} |
| 169 |
|