Changeset 3764005 for langforge/trunk/includes/class-cli.php
- Timestamp:
- 10/25/2026 06:29:16 AM (22 hours ago)
- File:
-
- 1 edited
-
langforge/trunk/includes/class-cli.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
langforge/trunk/includes/class-cli.php
r3742801 r3764005 16 16 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- LANGFORGE_ is the established plugin class prefix 17 17 class LANGFORGE_CLI extends WP_CLI_Command { 18 19 /** 20 * The language code as the site stores it, or an error that names the languages the site has. 21 * 22 * Same gate as the REST routes and the metabox (LANGFORGE_Post_Translation::require_language_available): 23 * the code, cleaned, must be one of the active languages. A command that skipped it left a post in a language 24 * the site does not have, or made a draft in it. 25 * 26 * @param mixed $language Language code as typed. 27 * @return string The cleaned code. 28 */ 29 private static function require_site_language($language) { 30 $language = sanitize_text_field((string) $language); 31 if (!LANGFORGE_Post_Translation::instance()->is_language_available($language)) { 32 $active = array_keys((array) LANGFORGE_Core::instance()->get_active_languages()); 33 WP_CLI::error(sprintf( 34 "Language '%s' is not active on this site. Active languages: %s.", 35 $language, 36 $active ? implode(', ', $active) : '(none)' 37 )); 38 } 39 return $language; 40 } 18 41 19 42 /** … … 102 125 } 103 126 127 // The REST route and the metabox refuse a language the site does not have; the command asks the same. 128 $language = self::require_site_language($language); 129 104 130 $existing = LANGFORGE_Post_Translation::instance()->get_translation($post_id, $language); 105 131 if ($existing) { … … 158 184 } 159 185 186 $language = self::require_site_language($language); 187 160 188 LANGFORGE_Post_Translation::instance()->set_post_language((int) $post_id, $language); 161 189 WP_CLI::success("Set language '$language' for post #$post_id."); … … 176 204 public function link($args, $assoc_args) { 177 205 list($id1, $id2) = $args; 178 LANGFORGE_Post_Translation::instance()->link_translations((int) $id1, (int) $id2); 206 $id1 = (int) $id1; 207 $id2 = (int) $id2; 208 209 // A row for a post that is not there is an orphan in the translations table: both posts are looked up 210 // first, as translate and set-language do, and nothing is written for a missing one. 211 foreach ([$id1, $id2] as $id) { 212 if ($id <= 0 || !get_post($id)) { 213 WP_CLI::error("Post $id not found."); 214 } 215 } 216 217 LANGFORGE_Post_Translation::instance()->link_translations($id1, $id2); 179 218 WP_CLI::success("Linked posts #$id1 and #$id2 as translations."); 180 219 } … … 194 233 public function unlink($args, $assoc_args) { 195 234 list($post_id, $language) = $args; 196 LANGFORGE_Post_Translation::instance()->unlink_translation((int) $post_id, $language); 235 $post_id = (int) $post_id; 236 237 // As link does: a post that is not there is an error, not a "Success" for nothing. 238 if ($post_id <= 0 || !get_post($post_id)) { 239 WP_CLI::error("Post $post_id not found."); 240 } 241 242 LANGFORGE_Post_Translation::instance()->unlink_translation($post_id, $language); 197 243 WP_CLI::success("Unlinked $language translation from post #$post_id."); 198 244 } … … 221 267 * --lang=<language> 222 268 * : Target language code (letters, digits, "-" and "_" only, for example 223 * de or pt-BR). 269 * de or pt-BR). It has to be an active language of the site. 224 270 * 225 271 * [--format=<format>] … … 258 304 )); 259 305 } 306 307 // A language the site does not have gets the same refusal as set-language: the code is an active language 308 // of the site (the one gate of the REST route and the metabox), and the error names the languages it has. 309 $target = self::require_site_language($target); 260 310 261 311 if (!class_exists('LANGFORGE_Import_Export')) {
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)