| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Domain\Updates; |
| 5 |
|
| 6 |
/** |
| 7 |
* This class describes the outcome of a single post update. |
| 8 |
*/ |
| 9 |
class Update_Result { |
| 10 |
|
| 11 |
/** |
| 12 |
* The ID of the post the result is for. |
| 13 |
* |
| 14 |
* @var int |
| 15 |
*/ |
| 16 |
private $post_id; |
| 17 |
|
| 18 |
/** |
| 19 |
* Whether the update succeeded. |
| 20 |
* |
| 21 |
* @var bool |
| 22 |
*/ |
| 23 |
private $success; |
| 24 |
|
| 25 |
/** |
| 26 |
* The error code, or null when the update succeeded. |
| 27 |
* |
| 28 |
* @var string|null |
| 29 |
*/ |
| 30 |
private $error_code; |
| 31 |
|
| 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 |
/** |
| 53 |
* The constructor. Use the named constructors to create instances. |
| 54 |
* |
| 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. |
| 60 |
*/ |
| 61 |
private function __construct( int $post_id, bool $success, ?string $error_code, array $rendered = [], array $sanitized = [] ) { |
| 62 |
$this->post_id = $post_id; |
| 63 |
$this->success = $success; |
| 64 |
$this->error_code = $error_code; |
| 65 |
$this->rendered = $rendered; |
| 66 |
$this->sanitized = $sanitized; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Creates a successful result. |
| 71 |
* |
| 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. |
| 75 |
* |
| 76 |
* @return self The successful result. |
| 77 |
*/ |
| 78 |
public static function for_success( int $post_id, array $rendered = [], array $sanitized = [] ): self { |
| 79 |
return new self( $post_id, true, null, $rendered, $sanitized ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Creates a failed result. |
| 84 |
* |
| 85 |
* @param int $post_id The ID of the post the result is for. |
| 86 |
* @param string $error_code The error code describing the failure. |
| 87 |
* |
| 88 |
* @return self The failed result. |
| 89 |
*/ |
| 90 |
public static function for_failure( int $post_id, string $error_code ): self { |
| 91 |
return new self( $post_id, false, $error_code ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Gets the ID of the post the result is for. |
| 96 |
* |
| 97 |
* @return int The ID of the post the result is for. |
| 98 |
*/ |
| 99 |
public function get_post_id(): int { |
| 100 |
return $this->post_id; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Whether the update succeeded. |
| 105 |
* |
| 106 |
* @return bool Whether the update succeeded. |
| 107 |
*/ |
| 108 |
public function is_success(): bool { |
| 109 |
return $this->success; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Gets the error code. |
| 114 |
* |
| 115 |
* @return string|null The error code, or null when the update succeeded. |
| 116 |
*/ |
| 117 |
public function get_error_code(): ?string { |
| 118 |
return $this->error_code; |
| 119 |
} |
| 120 |
|
| 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 |
/** |
| 140 |
* Parses the result to the expected key value representation. |
| 141 |
* |
| 142 |
* @return array<string, int|bool|string|array<string, string>> The result presented as the expected key value representation. |
| 143 |
*/ |
| 144 |
public function to_array(): array { |
| 145 |
$array = [ |
| 146 |
'id' => $this->post_id, |
| 147 |
'success' => $this->success, |
| 148 |
]; |
| 149 |
|
| 150 |
if ( ! $this->success ) { |
| 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; |
| 160 |
} |
| 161 |
|
| 162 |
return $array; |
| 163 |
} |
| 164 |
} |
| 165 |
|