id; } public function enqueue( $hook ) { if ( self::SCREEN_HOOK !== $hook || ! $this->should_show() ) { return; } wp_enqueue_style( 'acosmaia-feedback-ui', ACOSMAIA_URL . 'assets/css/acowebs-feedback-ui.css', [], ACOSMAIA_VERSION ); wp_enqueue_script( 'acosmaia-newsletter', ACOSMAIA_URL . 'assets/js/newsletter-notice.js', [], ACOSMAIA_VERSION, true ); wp_localize_script( 'acosmaia-newsletter', 'acosmaiaNewsletter', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE_ACTION ), 'noticeId' => 'acofb-' . self::PREFIX . '-newsletter', 'subscribing' => __( 'Subscribing…', 'aco-smart-ai-assistant' ), 'genericError' => __( 'Something went wrong. Please try again.', 'aco-smart-ai-assistant' ), ] ); } private function verify_request() { check_ajax_referer( self::NONCE_ACTION, 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => 'forbidden' ], 403 ); } } public function ajax_dismiss() { $this->verify_request(); update_option( self::HIDDEN_OPTION, 1, false ); wp_send_json_success(); } public function ajax_subscribe() { $this->verify_request(); // phpcs:disable WordPress.Security.NonceVerification.Missing -- verified in verify_request(). $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; $phone = isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : ''; $hp = isset( $_POST['website_hp'] ) ? sanitize_text_field( wp_unslash( $_POST['website_hp'] ) ) : ''; // phpcs:enable if ( ! is_email( $email ) ) { wp_send_json_error( [ 'message' => __( 'Please enter a valid email address.', 'aco-smart-ai-assistant' ) ], 400 ); } // Clicking "Subscribe" with the privacy line visible is the consent. $payload = [ 'email' => $email, 'consent' => true, 'list' => self::LIST_NAME, 'source' => self::SOURCE, 'plugin' => self::PLUGIN_NAME, 'edition' => self::EDITION, 'site_url' => home_url(), 'site_name' => get_bloginfo( 'name' ), 'website_hp' => $hp, ]; if ( '' !== $name ) { $payload['name'] = $name; } if ( '' !== $phone ) { $payload['phone'] = $phone; } $api_base = class_exists( 'Acowebs_Feedback_Client' ) ? Acowebs_Feedback_Client::api_url() : 'https://audience.acowebs.com'; $response = wp_remote_post( $api_base . '/v1/subscribe', [ 'timeout' => 10, 'redirection' => 0, 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => wp_json_encode( $payload ), ] ); if ( is_wp_error( $response ) ) { wp_send_json_error( [ 'message' => __( 'Could not reach the newsletter service. Please try again later.', 'aco-smart-ai-assistant' ) ], 502 ); } $status = (int) wp_remote_retrieve_response_code( $response ); $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( $status >= 200 && $status < 300 && ( ! is_array( $body ) || ! isset( $body['ok'] ) || $body['ok'] ) ) { update_option( self::HIDDEN_OPTION, 1, false ); wp_send_json_success(); } $message = ( is_array( $body ) && 'validation_failed' === ( $body['error'] ?? '' ) ) ? __( 'Please check your email and phone number and try again.', 'aco-smart-ai-assistant' ) : __( 'Something went wrong. Please try again.', 'aco-smart-ai-assistant' ); wp_send_json_error( [ 'message' => $message ], 400 ); } public function render() { if ( ! $this->should_show() ) { return; } $p = self::PREFIX; $user = wp_get_current_user(); ?>