is_promptable() ) { return; } if ( ! $this->is_revenue_eligible() ) { return; } $this->record_shown(); ?>
state(); if ( empty( $state ) ) { return true; } if ( ! empty( $state['closed'] ) ) { return false; } if ( 'snoozed' !== $state['outcome'] ) { return false; } if ( (int) $state['times_shown'] >= self::MAX_PROMPTS ) { return false; } return (int) $state['snoozed_until'] <= time(); } /** * Read and normalize local state. * * @return array */ private function state() { $state = get_option( self::OPTION_STATE, array() ); if ( ! is_array( $state ) || empty( $state ) ) { return array(); } return array( 'times_shown' => isset( $state['times_shown'] ) ? (int) $state['times_shown'] : 0, 'outcome' => isset( $state['outcome'] ) ? sanitize_key( $state['outcome'] ) : '', 'snoozed_until' => isset( $state['snoozed_until'] ) ? (int) $state['snoozed_until'] : 0, 'closed' => ! empty( $state['closed'] ), ); } /** * Ask the brain whether attributed revenue is above zero. * * @return bool */ private function is_revenue_eligible() { $cached = get_transient( self::TRANSIENT_ELIGIBILITY ); if ( false !== $cached ) { return 'yes' === $cached; } $response = wp_remote_get( Plugin::brain_url() . '/api/storefront/review-eligibility', array( 'timeout' => 10, 'headers' => array( 'X-Aisthetix-Secret' => Plugin::secret_key(), 'Accept' => 'application/json', ), ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { set_transient( self::TRANSIENT_ELIGIBILITY, 'no', 15 * MINUTE_IN_SECONDS ); return false; } $data = json_decode( wp_remote_retrieve_body( $response ), true ); $eligible = is_array( $data ) && ! empty( $data['eligible'] ); set_transient( self::TRANSIENT_ELIGIBILITY, $eligible ? 'yes' : 'no', HOUR_IN_SECONDS ); return $eligible; } /** * Showing the notice spends an ask. * * @return void */ private function record_shown() { $state = $this->state(); update_option( self::OPTION_STATE, array( 'times_shown' => ( isset( $state['times_shown'] ) ? (int) $state['times_shown'] : 0 ) + 1, 'outcome' => '', 'snoozed_until' => 0, 'closed' => false, ) ); } /** * Build a nonce-protected admin-post URL. * * @param string $action Action name. * @return string */ private function action_url( $action ) { return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action ), $action, '_aisthetix_nonce' ); } /** Mark requested and continue to WordPress.org. */ public function handle_review() { $this->guard_action( self::ACTION_REVIEW ); $this->close( 'requested' ); wp_redirect( self::REVIEW_URL ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- fixed trusted URL. exit; } /** Snooze for exactly 60 days. */ public function handle_snooze() { $this->guard_action( self::ACTION_SNOOZE ); $state = $this->state(); update_option( self::OPTION_STATE, array( 'times_shown' => isset( $state['times_shown'] ) ? (int) $state['times_shown'] : 1, 'outcome' => 'snoozed', 'snoozed_until' => time() + self::SNOOZE_DAYS * DAY_IN_SECONDS, 'closed' => false, ) ); $this->redirect_back(); } /** Permanently dismiss the ask. */ public function handle_dismiss() { $this->guard_action( self::ACTION_DISMISS ); $this->close( 'dismissed' ); $this->redirect_back(); } /** * Validate capability and nonce. * * @param string $action Action name. * @return void */ private function guard_action( $action ) { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'aisthetix-virtual-try-on' ) ); } check_admin_referer( $action, '_aisthetix_nonce' ); } /** * Persist a terminal outcome. * * @param string $outcome Outcome name. * @return void */ private function close( $outcome ) { $state = $this->state(); update_option( self::OPTION_STATE, array( 'times_shown' => isset( $state['times_shown'] ) ? (int) $state['times_shown'] : 1, 'outcome' => sanitize_key( $outcome ), 'snoozed_until' => 0, 'closed' => true, ) ); } /** Redirect to the plugin settings page. */ private function redirect_back() { wp_safe_redirect( admin_url( 'admin.php?page=' . Settings::PAGE_SLUG ) ); exit; } }