name = $name; $this->text_domain = $text_domain; $this->plugin_abbr = $plugin_abbr; $this->product_id = $product_id; $this->plugin_plan = $plugin_plan; $this->tracker_class = $tracker_class; $this->allowed_by_default = $allowed_by_default; // Don't run usage tracker on dev environment if not enabled. if ( $tracker_class::is_dev_environment() && ! $enable_on_dev ) { return; } register_activation_hook( $plugin_file_path, array( $this, 'do_activation_setup' ) ); register_deactivation_hook( $plugin_file_path, array( $this, 'do_deactivation_cleanup' ) ); $tracking_option_name = $this->get_tracking_option_name(); /** * Tracking consent add/update handler function. * These action hooks are triggered by WordPress when we add/update tracking consent option in DB. */ add_action( 'add_option_' . $tracking_option_name, array( $this, 'handle_optin_add' ), 10, 2 ); add_action( 'update_option_' . $tracking_option_name, array( $this, 'handle_optin_update' ), 10, 3 ); add_action( 'admin_notices', array( $this, 'show_tracker_notice' ) ); add_action( 'admin_init', array( $this, 'handle_tracker_notice_actions' ) ); add_filter( 'cron_schedules', array( $this, 'add_weekly_schedule' ) ); add_action( $this->plugin_abbr . '_send_tracking_data', array( $this, 'send_tracking_data' ) ); add_filter( $this->plugin_abbr . '_allow_tracking', array( $this, 'is_tracking_allowed' ) ); add_filter( $this->plugin_abbr . '_tracking_data', array( $this, 'get_tracking_data' ) ); } /** * Handles when optin option is added in db * * @param string $option_name * @param string $value */ public function handle_optin_add( $option_name, $value ) { $this->handle_optin_change( $value ); } /** * Handles when optin option is updated in db * * @param string $old_value * @param string $new_value * @param string $option_name */ public function handle_optin_update( $old_value, $new_value, $option_name ) { $this->handle_optin_change( $new_value ); } /** * Common method to handle optin option add/update * * @param string $value */ public function handle_optin_change( $opted_in ) { if ( 'yes' === $opted_in ) { $this->schedule_cron(); $this->send_tracking_data( true ); } else { $this->clear_scheduled_cron(); } } /** * Perform plugin activation related tasks. */ public function do_activation_setup() { add_option( $this->plugin_abbr . '_installed_on', gmdate( 'Y-m-d H:i:s' ), '', false ); $this->schedule_cron(); $this->send_tracking_data( true ); } /** * Do deactivation cleanup */ public function do_deactivation_cleanup() { $this->clear_scheduled_cron(); $survey_status = ig_es_get_request_data( 'survey_status', '' ); if ( ! empty( $survey_status ) && 'skipped' === $survey_status ) { $extra_params = array( 'is_deactivated' => 1, ); $this->send_tracking_data( true, $extra_params ); } } /** * Add weekly cron schedule * * @param array $schedules * * @return array $schedules */ public function add_weekly_schedule( $schedules = array() ) { // Add weekly schedule if not exists already. From WP 5.4, it is added by default. if ( empty( $schedules['weekly'] ) ) { $schedules['weekly'] = array( 'interval' => DAY_IN_SECONDS * 7, 'display' => __( 'Once Weekly', $this->text_domain ), ); } return $schedules; } /** * Schedule cron */ public function schedule_cron() { // Schedule a weekly cron to send usage data. $hook_name = $this->plugin_abbr . '_send_tracking_data'; if ( ! wp_next_scheduled( $hook_name ) ) { wp_schedule_event( time(), 'weekly', $hook_name ); } } /** * Clear any scheduled cron */ public function clear_scheduled_cron() { wp_clear_scheduled_hook( $this->plugin_abbr . '_send_tracking_data' ); } /** * Show tracking notice */ public function show_tracker_notice() { if ( ! current_user_can( 'manage_options' ) ) { return; } $show_tracking_notice = apply_filters( $this->plugin_abbr . '_show_plugin_usage_tracking_notice', false ); if ( false === $show_tracking_notice ) { return; } $tracking_allowed = $this->is_tracking_allowed(); // Check if tracking option is set. Can be yes or no if set else empty. // yes/no indicates the notice has already been shown and user has opted in/out. Don't show the notice in this case. if ( ! empty( $tracking_allowed ) ) { return; } $optin_url = wp_nonce_url( add_query_arg( $this->plugin_abbr . '_tracker', 'opt_into' ), $this->plugin_abbr . '_tracker_action' ); $optout_url = wp_nonce_url( add_query_arg( $this->plugin_abbr . '_tracker', 'opt_out' ), $this->plugin_abbr . '_tracker_action' ); ?>
text_domain ), '' . esc_html( $this->name ) . '' ); ?> text_domain ); ?>