| @@ -96,30 +96,47 @@ | ||
| 96 | 96 | * @return array Generated metadata |
| 97 | 97 | * @throws \Exception If generation fails |
| 98 | 98 | */ |
| 99 | 99 | public function generate_for_post(int $post_id, array $options = []): array { |
| 100 | + $metadata = $this->suggest_for_post($post_id, $options); | |
| 101 | + | |
| 102 | + // Save metadata to post meta | |
| 103 | + $this->save_post_metadata($post_id, $metadata); | |
| 104 | + | |
| 105 | + return $metadata; | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Generate metadata for a post without writing anything. | |
| 110 | + * | |
| 111 | + * The same generation as {@see self::generate_for_post()}, minus the save. | |
| 112 | + * Bulk Snippets shows the result for review and writes only when the user | |
| 113 | + * clicks Save, so it must never persist as a side effect (#727). | |
| 114 | + * | |
| 115 | + * @since 2.8.0 | |
| 116 | + * | |
| 117 | + * @param int $post_id Post ID. | |
| 118 | + * @param array $options Generation options. | |
| 119 | + * @return array Generated metadata. | |
| 120 | + * @throws \Exception If the post is missing or generation fails. | |
| 121 | + */ | |
| 122 | + public function suggest_for_post(int $post_id, array $options = []): array { | |
| 100 | 123 | $post = get_post($post_id); |
| 101 | - | |
| 124 | + | |
| 102 | 125 | if (!$post) { |
| 103 | 126 | throw new \Exception('Post not found'); |
| 104 | 127 | } |
| 105 | - | |
| 128 | + | |
| 106 | 129 | // Extract content from post |
| 107 | 130 | $content = $this->extract_post_content($post); |
| 108 | - | |
| 131 | + | |
| 109 | 132 | // Add post-specific options |
| 110 | 133 | $options = array_merge($options, [ |
| 111 | 134 | 'content_type' => $this->determine_content_type($post), |
| 112 | 135 | 'post_id' => $post_id, |
| 113 | 136 | ]); |
| 114 | - | |
| 115 | - // Generate metadata | |
| 116 | - $metadata = $this->generate_for_content($content, $options); | |
| 117 | - | |
| 118 | - // Save metadata to post meta | |
| 119 | - $this->save_post_metadata($post_id, $metadata); | |
| 120 | - | |
| 121 | - return $metadata; | |
| 137 | + | |
| 138 | + return $this->generate_for_content($content, $options); | |
| 122 | 139 | } |
| 123 | 140 | |
| 124 | 141 | /** |
| 125 | 142 | * Generate multiple variations of metadata |