500 ) ); } $internal = new WP_REST_Request( 'POST', '/aipt/v1/automation-jobs' ); if ( '' !== $nonce ) { $internal->set_header( 'X-WP-Nonce', $nonce ); } $internal->set_header( 'content-type', 'application/json' ); $internal->set_body( (string) wp_json_encode( $payload ) ); return rest_do_request( $internal ); } /** * Fallback reason: the generator has no Automation job type. */ public const FALLBACK_NO_JOB_TYPE = 'no_job_type'; /** * Fallback reason: the job could not be created (refused, failed or no id). */ public const FALLBACK_JOB_FAILED = 'job_failed'; /** * Longest merchant-facing refusal sentence kept in a payload. */ private const JOB_ERROR_MAX_LENGTH = 200; /** * The merchant-facing refusal sentence of a failed job creation: the * WP_Error message, or the `message` of the REST error response data. * Tags are stripped and the length is capped; empty when there is none * or when the refusal is technical (5xx, core REST validation codes). * * @param WP_REST_Response|WP_Error|mixed $job_response What createJob() returned. * @return string */ public static function refusalMessage( $job_response ): string { $message = ''; $code = ''; $status = 0; if ( is_wp_error( $job_response ) ) { $message = (string) $job_response->get_error_message(); $code = (string) $job_response->get_error_code(); $error_data = $job_response->get_error_data(); $status = is_array( $error_data ) ? (int) ( $error_data['status'] ?? 0 ) : 0; } elseif ( $job_response instanceof WP_REST_Response ) { $status = (int) $job_response->get_status(); $data = $job_response->get_data(); if ( is_array( $data ) && isset( $data['message'] ) && is_string( $data['message'] ) ) { $message = $data['message']; $code = isset( $data['code'] ) && is_string( $data['code'] ) ? $data['code'] : ''; } } if ( '' === $message || $status < 400 || $status >= 500 || 0 === strpos( $code, 'rest_' ) ) { return ''; } $message = trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( $message ) ) ); if ( mb_strlen( $message, 'UTF-8' ) > self::JOB_ERROR_MAX_LENGTH ) { $message = rtrim( mb_substr( $message, 0, self::JOB_ERROR_MAX_LENGTH - 3, 'UTF-8' ) ) . '...'; } return $message; } /** * @param string $generator Generator key. * @param array $pin RunPinService::createRunPin()'s result. * @param int[] $product_ids Resolved product/term ids. * @param int $product_count Total matched count. * @param int $revision Blueprint revision used (0 when the run spans more than one Blueprint, e.g. a pin with no single manual pick). * @param bool $approximate True when the scope hit its cap, so `$product_count` is a lower bound of the governed products. * @param string $fallback_reason FALLBACK_* constant saying why no job was queued; empty when the caller does not say. * @param string $job_error Merchant-facing refusal sentence of the failed job creation; omitted from the payload when empty. * @return array{deepLink:string,productCount:int,approximate:bool,revision:int,fallbackReason?:string,jobError?:string} */ public static function deepLinkPayload( string $generator, array $pin, array $product_ids, int $product_count, int $revision, bool $approximate = false, string $fallback_reason = '', string $job_error = '' ): array { $slug = Generators::adminSlug( $generator ); $bounded = array_slice( $product_ids, 0, 200 ); $query = 'page=' . $slug . '&blueprintRun=' . (int) $pin['id'] . '&products=' . implode( ',', $bounded ); $payload = array( 'deepLink' => function_exists( 'admin_url' ) && '' !== $slug ? admin_url( 'admin.php?' . $query ) : '', 'productCount' => $product_count, 'approximate' => $approximate, 'revision' => $revision, ); if ( '' !== $fallback_reason ) { $payload['fallbackReason'] = $fallback_reason; } if ( '' !== $job_error ) { $payload['jobError'] = $job_error; } return $payload; } }