plugin_url = $plugin_url; $this->version = $version; $this->init(); } /** * Initialize hooks. */ public function init() { if ( ! is_admin() ) { return; } add_action( 'current_screen', function () { if ( ! $this->is_plugins_screen() ) { return; } add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); } ); add_action( 'wp_ajax_adasprso_deactivate_feedback', array( $this, 'ajax_handle_feedback' ) ); } /** * Check if we are on the Plugins screen. * * @return bool */ private function is_plugins_screen() { return in_array( get_current_screen()->id, array( 'plugins', 'plugins-network' ), true ); } /** * Enqueue scripts and styles, and print the modal HTML in the footer. */ public function enqueue_scripts() { add_action( 'admin_footer', array( $this, 'print_modal_html' ) ); wp_register_script( 'adasprso-deactivation-feedback', $this->plugin_url . 'admin/js/deactivation-feedback.js', array( 'jquery' ), $this->version, true ); wp_localize_script( 'adasprso-deactivation-feedback', 'adasprsoDeactivationData', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'adasprso_deactivate_feedback_nonce' ), 'i18n' => array( 'select_reason' => esc_html__( 'Please select a reason before submitting.', 'add-as-preferred-source' ), 'provide_email' => esc_html__( 'Please enter a valid email address so we can reach you.', 'add-as-preferred-source' ), 'submitting' => esc_html__( 'Submitting…', 'add-as-preferred-source' ), 'submit_btn' => esc_html__( 'Submit & Deactivate', 'add-as-preferred-source' ), 'thank_you' => esc_html__( 'Thank you!', 'add-as-preferred-source' ), 'error' => esc_html__( 'Something went wrong. Please try again.', 'add-as-preferred-source' ), 'network_error' => esc_html__( 'Network error — please check your connection.', 'add-as-preferred-source' ), ), ) ); wp_enqueue_script( 'adasprso-deactivation-feedback' ); wp_register_style( 'adasprso-deactivation-feedback', $this->plugin_url . 'admin/css/deactivation-feedback.css', array(), $this->version ); wp_enqueue_style( 'adasprso-deactivation-feedback' ); } /** * Output the deactivation feedback modal HTML. */ public function print_modal_html() { $reasons = array( 'no_longer_needed' => array( 'label' => esc_html__( 'No longer needed', 'add-as-preferred-source' ), 'placeholder' => '', 'icon' => '', ), 'found_better_plugin' => array( 'label' => esc_html__( 'Found a better plugin', 'add-as-preferred-source' ), 'placeholder' => esc_attr__( 'Which plugin?', 'add-as-preferred-source' ), 'icon' => '', ), 'couldnt_get_to_work' => array( 'label' => esc_html__( "Couldn't get it to work", 'add-as-preferred-source' ), 'placeholder' => esc_attr__( 'What went wrong?', 'add-as-preferred-source' ), 'icon' => '', ), 'temporary_deactivation' => array( 'label' => esc_html__( 'Temporary deactivation', 'add-as-preferred-source' ), 'placeholder' => '', 'icon' => '', ), 'missing_feature' => array( 'label' => esc_html__( 'Missing a feature', 'add-as-preferred-source' ), 'placeholder' => esc_attr__( 'What feature would you like?', 'add-as-preferred-source' ), 'icon' => '', ), 'other' => array( 'label' => esc_html__( 'Other reason', 'add-as-preferred-source' ), 'placeholder' => esc_attr__( 'Tell us more…', 'add-as-preferred-source' ), 'icon' => '', ), ); ?> 'No longer needed', 'found_better_plugin' => 'Found a better plugin', 'couldnt_get_to_work' => "Couldn't get it to work", 'temporary_deactivation' => 'Temporary deactivation', 'missing_feature' => 'Missing a feature', 'other' => 'Other reason', ); if ( ! array_key_exists( $reason_key, $reason_labels ) ) { wp_send_json_error( esc_html__( 'Invalid reason selected.', 'add-as-preferred-source' ) ); wp_die(); } $reason_label = $reason_labels[ $reason_key ]; $reason_text = isset( $_POST[ 'reason_' . $reason_key ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ 'reason_' . $reason_key ] ) ) : ''; $payload = array( 'plugin_name' => 'Add as Preferred Source on Google', 'reason_key' => $reason_key, 'reason_label' => $reason_label, 'reason_text' => $reason_text, 'site_name' => get_bloginfo( 'name' ) ?: 'Unknown', 'site_url' => get_site_url() ?: 'Unknown', 'wp_version' => get_bloginfo( 'version' ) ?: 'Unknown', 'plugin_version' => $this->version ?: 'Unknown', 'contact_requested' => ( $wants_contact && $contact_email ) ? 'Yes' : 'No', 'contact_email' => $contact_email, ); wp_remote_post( 'https://startling-malasada-45aa0d.netlify.app/.netlify/functions/feedback', array( 'headers' => array( 'Content-Type' => 'application/json', 'X-Relay-Secret' => AAPS_RELAY_SECRET, ), 'body' => wp_json_encode( $payload ), 'timeout' => 15, ) ); wp_send_json_success( esc_html__( 'Thank you for your feedback!', 'add-as-preferred-source' ) ); wp_die(); } }