403 ) ); } $operation = isset( $_POST['operation'] ) && is_string( $_POST['operation'] ) ? sanitize_key( wp_unslash( $_POST['operation'] ) ) : ''; $user = get_current_user_id(); $destination = admin_url( 'admin.php?page=affiai#affiai-first-success' ); if ( in_array( $operation, array( 'dismiss', 'reopen' ), true ) ) { update_user_meta( $user, 'affiai_first_success_dismissed_v1', 'dismiss' === $operation ? 1 : 0 ); } elseif ( 'start' === $operation ) { $post_input = isset( $_POST['post_id'] ) && is_string( $_POST['post_id'] ) ? sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) : ''; $post_id = ctype_digit( $post_input ) ? absint( $post_input ) : 0; $asin = isset( $_POST['asin'] ) && is_string( $_POST['asin'] ) ? strtoupper( trim( sanitize_text_field( wp_unslash( $_POST['asin'] ) ) ) ) : ''; $post = get_post( $post_id ); if ( ! $post || 'publish' !== $post->post_status || ! current_user_can( 'edit_post', $post_id ) || ! preg_match( '/^[A-Z0-9]{10}$/D', $asin ) ) { wp_die( esc_html__( 'Choose an existing published post/page and its ten-character Amazon product ID.', 'affiliate-ai-lite' ), '', array( 'response' => 400 ) ); } update_option( 'affiai_first_success_attempt_v1', array( 'attempt_id' => wp_generate_uuid4(), 'user_id' => $user, 'post_id' => $post_id, 'asin' => $asin, 'context' => AffiAI::integrity_context(), 'expires_at' => time() + 15 * MINUTE_IN_SECONDS ), false ); $destination = get_permalink( $post_id ); } else { wp_die( esc_html__( 'Unknown checklist action.', 'affiliate-ai-lite' ), '', array( 'response' => 400 ) ); } wp_safe_redirect( $destination ); exit; } add_action( 'admin_post_affiai_first_success', 'affiai_first_success_action' ); /** No visitor can create success evidence; the post, ASIN, admin and configuration must agree. */ function affiai_first_success_observe( $post_id, $placement, $atts, $products, $output, $context, $source ) { if ( is_admin() || is_preview() || ! is_singular() || ! current_user_can( 'manage_options' ) ) { return; } $scope = get_option( 'affiai_first_success_attempt_v1', array() ); if ( ! is_array( $scope ) || empty( $scope['attempt_id'] ) || isset( $scope['result'] ) || (int) ( $scope['expires_at'] ?? 0 ) < time() || (int) ( $scope['user_id'] ?? 0 ) !== get_current_user_id() || (int) ( $scope['post_id'] ?? 0 ) !== (int) $post_id || ( $scope['context'] ?? null ) !== $context || ( $scope['asin'] ?? null ) !== ( is_string( $atts['asin'] ?? null ) ? strtoupper( trim( $atts['asin'] ) ) : null ) || ! empty( $atts['keyword'] ) || ! in_array( $atts['template'] ?? '', array( 'grid', 'carousel', 'box', 'review', 'list', 'vertical' ), true ) ) { return; } $usable = Affiai_Offer_Integrity::usable_products( $products ); $market = AffiAI::integrity_market(); $selected = 1 === count( $usable ) ? reset( $usable ) : array(); $ready = ( $selected['sku'] ?? null ) === $scope['asin'] && ( $selected['_affiai_integrity']['source_market'] ?? null ) === ( $market['country'] ?? null ) && null !== $market && is_string( $output ) && '' !== trim( $output ); $code = $ready ? 'placement_rendered' : affiai_first_success_failure_code( $market ); $record = array( 'attempt_id' => $scope['attempt_id'], 'post_id' => (int) $post_id, 'placement' => $placement, 'context' => $context, 'source_market' => $market['country'] ?? null, 'state' => $ready ? 'ready' : 'action_required', 'code' => $code, 'source' => $source, 'checked_at' => time(), 'source_fetched_at' => null ); foreach ( $usable as $product ) { foreach ( $product['merchant'] as $offer ) { if ( ! empty( $offer['source_fetched_at'] ) ) { $record['source_fetched_at'] = (int) $offer['source_fetched_at']; } } } // Store the result inside its scope in one compare-and-swap. A newer start // replaces this scope, so an older render cannot publish or consume its result. global $wpdb; $completed = $scope; $completed['result'] = $record; $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND option_value = %s", maybe_serialize( $completed ), 'affiai_first_success_attempt_v1', maybe_serialize( $scope ) ) ); wp_cache_delete( 'affiai_first_success_attempt_v1', 'options' ); } /** Only the current attempt's result is displayed; legacy history is used before the first new check. */ function affiai_first_success_result() { $scope = get_option( 'affiai_first_success_attempt_v1', array() ); if ( is_array( $scope ) && $scope ) { $result = $scope['result'] ?? array(); return is_array( $result ) && ! empty( $scope['attempt_id'] ) && ( $result['attempt_id'] ?? null ) === $scope['attempt_id'] ? $result : array(); } return get_option( 'affiai_first_success_v1', array() ); } function affiai_first_success_failure_code( $market ) { if ( ! $market ) { return 'unsupported_market'; } $prefix = AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_'; $keys = ( get_option( $prefix . 'amazon_creators_key' ) && get_option( $prefix . 'amazon_creators_secret' ) ) || ( get_option( $prefix . 'amazon_api_key' ) && get_option( $prefix . 'amazon_api_secret' ) ); if ( ! get_option( $prefix . 'amazon_tag' ) || ! $keys ) { return 'configuration_missing'; } global $affiai_request_observation; return $affiai_request_observation ?: 'no_usable_offer_or_unknown'; } /** Safe operational response code; no upstream body or credentials are retained. */ function affiai_lite_provider_observation( $code ) { global $affiai_request_observation; $allowed = array( 'transport_error', 'request_failed', 'malformed_response', 'no_usable_offer_or_unknown', 'provider_http_400', 'provider_http_401', 'provider_http_403', 'provider_http_429', 'provider_http_500', 'provider_http_502', 'provider_http_503' ); $affiai_request_observation = in_array( $code, $allowed, true ) ? $code : 'request_failed'; } function affiai_first_success_panel() { if ( ! current_user_can( 'manage_options' ) ) { return; } $dismissed = (bool) get_user_meta( get_current_user_id(), 'affiai_first_success_dismissed_v1', true ); $record = affiai_first_success_result(); $actions = array( 'placement_rendered' => __( 'This public placement rendered a usable offer in the recorded source market. This is not a credential or stock guarantee.', 'affiliate-ai-lite' ), 'unsupported_market' => __( 'Select a supported marketplace in Amazon settings, then retry this placement.', 'affiliate-ai-lite' ), 'configuration_missing' => __( 'Save the marketplace, its tracking ID, and your Amazon API credentials. Lite needs API access; a paid No-API option does not enable it here.', 'affiliate-ai-lite' ), 'provider_http_429' => __( 'Amazon limited this request. Wait before using the existing refresh action.', 'affiliate-ai-lite' ), 'provider_http_401' => __( 'Amazon returned HTTP 401. Check the saved credentials for this marketplace.', 'affiliate-ai-lite' ), 'provider_http_403' => __( 'Amazon returned HTTP 403. Check account access and the selected marketplace tracking ID.', 'affiliate-ai-lite' ), 'transport_error' => __( 'The request could not reach Amazon. Check connectivity and try again later.', 'affiliate-ai-lite' ), ); ?>