Plugin Directory


Ignore:
Timestamp:
10/25/2026 06:29:16 AM (22 hours ago)
Author:
avakodeforge
Message:

Release 1.3.0 (trunk + tag; see readme changelog)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • langforge/trunk/includes/class-cli.php

    r3742801 r3764005  
    1616// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- LANGFORGE_ is the established plugin class prefix
    1717class 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    }
    1841
    1942    /**
    … …  
    102125        }
    103126
     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
    104130        $existing = LANGFORGE_Post_Translation::instance()->get_translation($post_id, $language);
    105131        if ($existing) {
    … …  
    158184        }
    159185
     186        $language = self::require_site_language($language);
     187
    160188        LANGFORGE_Post_Translation::instance()->set_post_language((int) $post_id, $language);
    161189        WP_CLI::success("Set language '$language' for post #$post_id.");
    … …  
    176204    public function link($args, $assoc_args) {
    177205        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);
    179218        WP_CLI::success("Linked posts #$id1 and #$id2 as translations.");
    180219    }
    … …  
    194233    public function unlink($args, $assoc_args) {
    195234        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);
    197243        WP_CLI::success("Unlinked $language translation from post #$post_id.");
    198244    }
    … …  
    221267 * --lang=<language>
    222268 * : 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.
    224270 *
    225271 * [--format=<format>]
    … …  
    258304            ));
    259305        }
     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);
    260310
    261311        if (!class_exists('LANGFORGE_Import_Export')) {
Note: See TracChangeset for help on using the changeset viewer.