register(); } private function register(){ $this->status = get_option("$this->prefix-opt_in", false); $this->last_check = get_option("$this->prefix-info-check", time()-1); $this->marketing_allowed = get_option("$this->prefix-marketing-allowed", false); add_filter("plugin_action_links_$this->base_name", [$this, 'opt_in_button'] ); add_action("wp_ajax_$this->prefix-opt-in-update", [$this, 'opt_in_update']); add_action('admin_init', [$this, 'admin_init']); add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']); if(!$this->status){ add_action('admin_menu', [$this, 'add_opt_in_menu']); } if($this->status == 'agreed'){ register_deactivation_hook( $this->__FILE__, [$this, 'deactivate'] ); } add_action('admin_footer', [$this, 'opt_in_modal']); add_action('admin_footer', [$this, 'initialize_opt_in']); } function initialize_opt_in(){ ?> prefix-redirect", false); if(!$redirect && !str_contains($_SERVER['REQUEST_URI'], 'post.php') && !str_contains( $_SERVER['REQUEST_URI'], 'post-new.php' )){ wp_redirect("admin.php?page=".dirname($this->base_name)); update_option("$this->prefix-redirect", true); } } public function opt_in_update(){ $nonce = $_POST['nonce'] ?? ''; $actionType = $_POST['actionType'] ?? ''; if(!wp_verify_nonce( $nonce, 'wp_ajax' )){ echo wp_json_encode(['success' => false, 'message' => 'invalid nonce']); wp_die(); } if($actionType === 'agree'){ update_option("$this->prefix-opt_in", 'agreed'); update_option("$this->prefix-marketing-allowed", true); }else if($actionType === 'skip'){ update_option("$this->prefix-opt_in", 'skipped'); }else if ($actionType === 'opt_out'){ update_option("$this->prefix-marketing-allowed", false); } echo wp_json_encode(new \WP_REST_Response(['success' => true])); wp_die(); } public function opt_in_button($links){ $settings_link = ''.esc_html($this->marketing_allowed ? 'Opt out' : 'Opt In').''; array_unshift($links, $settings_link); return $links; } // add a temp page to show opt-in form public function add_opt_in_menu(){ add_submenu_page( '', $this->plugin_name, $this->plugin_name, 'manage_options', dirname($this->base_name), [$this, 'opt_in_form'] ); } // enqueue assets public function enqueue_assets($hook){ wp_register_script("bsdk-opt-in", plugin_dir_url(plugin_dir_path( __DIR__ )).'dist/opt-in-form.js', ['react', 'react-dom'], $this->version); wp_register_style("bsdk-opt-in", plugin_dir_url(plugin_dir_path( __DIR__ )).'dist/opt-in-form.css', [], $this->version); if($hook === 'plugins.php' || $hook === "admin_page_".dirname($this->base_name)){ wp_enqueue_script("bsdk-opt-in"); wp_enqueue_style("bsdk-opt-in"); } } // show the opt-in form public function opt_in_form(){ update_option("$this->prefix-redirect", true); ?>