| @@ -29,29 +29,55 @@ | ||
| 29 | 29 | */ |
| 30 | 30 | private $error_code; |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | + * The rendered values of the updated fields, keyed by field, with replacement variables resolved. | |
| 34 | + * | |
| 35 | + * Used for fields that feed the per-field scores (seo_title, meta_description). | |
| 36 | + * | |
| 37 | + * @var array<string, string> | |
| 38 | + */ | |
| 39 | + private $rendered; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * The sanitized literals that were actually persisted, keyed by field. | |
| 43 | + * | |
| 44 | + * Used for fields whose submitted value may have been silently altered by sanitization | |
| 45 | + * (e.g. focus_keyphrase with HTML stripped), so the frontend can reflect what was stored | |
| 46 | + * rather than what was submitted. | |
| 47 | + * | |
| 48 | + * @var array<string, string> | |
| 49 | + */ | |
| 50 | + private $sanitized; | |
| 51 | + | |
| 52 | + /** | |
| 33 | 53 | * The constructor. Use the named constructors to create instances. |
| 34 | 54 | * |
| 35 | - * @param int $post_id The ID of the post the result is for. | |
| 36 | - * @param bool $success Whether the update succeeded. | |
| 37 | - * @param string|null $error_code The error code, or null when the update succeeded. | |
| 55 | + * @param int $post_id The ID of the post the result is for. | |
| 56 | + * @param bool $success Whether the update succeeded. | |
| 57 | + * @param string|null $error_code The error code, or null when the update succeeded. | |
| 58 | + * @param array<string, string> $rendered The rendered values of the updated fields. | |
| 59 | + * @param array<string, string> $sanitized The sanitized literals that were persisted. | |
| 38 | 60 | */ |
| 39 | - private function __construct( int $post_id, bool $success, ?string $error_code ) { | |
| 61 | + private function __construct( int $post_id, bool $success, ?string $error_code, array $rendered = [], array $sanitized = [] ) { | |
| 40 | 62 | $this->post_id = $post_id; |
| 41 | 63 | $this->success = $success; |
| 42 | 64 | $this->error_code = $error_code; |
| 65 | + $this->rendered = $rendered; | |
| 66 | + $this->sanitized = $sanitized; | |
| 43 | 67 | } |
| 44 | 68 | |
| 45 | 69 | /** |
| 46 | 70 | * Creates a successful result. |
| 47 | 71 | * |
| 48 | - * @param int $post_id The ID of the post the result is for. | |
| 72 | + * @param int $post_id The ID of the post the result is for. | |
| 73 | + * @param array<string, string> $rendered The rendered values of the updated fields, keyed by field. | |
| 74 | + * @param array<string, string> $sanitized The sanitized literals that were persisted, keyed by field. | |
| 49 | 75 | * |
| 50 | 76 | * @return self The successful result. |
| 51 | 77 | */ |
| 52 | - public static function for_success( int $post_id ): self { | |
| 53 | - return new self( $post_id, true, null ); | |
| 78 | + public static function for_success( int $post_id, array $rendered = [], array $sanitized = [] ): self { | |
| 79 | + return new self( $post_id, true, null, $rendered, $sanitized ); | |
| 54 | 80 | } |
| 55 | 81 | |
| 56 | 82 | /** |
| 57 | 83 | * Creates a failed result. |
| @@ -92,11 +118,29 @@ | ||
| 92 | 118 | return $this->error_code; |
| 93 | 119 | } |
| 94 | 120 | |
| 95 | 121 | /** |
| 122 | + * Gets the rendered values of the updated fields. | |
| 123 | + * | |
| 124 | + * @return array<string, string> The rendered values, keyed by field. | |
| 125 | + */ | |
| 126 | + public function get_rendered(): array { | |
| 127 | + return $this->rendered; | |
| 128 | + } | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * Gets the sanitized literals that were persisted. | |
| 132 | + * | |
| 133 | + * @return array<string, string> The sanitized values, keyed by field. | |
| 134 | + */ | |
| 135 | + public function get_sanitized(): array { | |
| 136 | + return $this->sanitized; | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 96 | 140 | * Parses the result to the expected key value representation. |
| 97 | 141 | * |
| 98 | - * @return array<string, int|bool|string> The result presented as the expected key value representation. | |
| 142 | + * @return array<string, int|bool|string|array<string, string>> The result presented as the expected key value representation. | |
| 99 | 143 | */ |
| 100 | 144 | public function to_array(): array { |
| 101 | 145 | $array = [ |
| 102 | 146 | 'id' => $this->post_id, |
| @@ -104,8 +148,16 @@ | ||
| 104 | 148 | ]; |
| 105 | 149 | |
| 106 | 150 | if ( ! $this->success ) { |
| 107 | 151 | $array['error'] = $this->error_code; |
| 152 | + } | |
| 153 | + | |
| 154 | + if ( $this->rendered !== [] ) { | |
| 155 | + $array['rendered'] = $this->rendered; | |
| 156 | + } | |
| 157 | + | |
| 158 | + if ( $this->sanitized !== [] ) { | |
| 159 | + $array['sanitized'] = $this->sanitized; | |
| 108 | 160 | } |
| 109 | 161 | |
| 110 | 162 | return $array; |
| 111 | 163 | } |