This commit is contained in:
2026-03-11 15:57:27 +01:00
parent 481271c972
commit b4b460fd21
10775 changed files with 2071579 additions and 26409 deletions

View File

@@ -109,20 +109,20 @@ function pll_default_language( $field = 'slug' ) {
*
* @api
* @since 0.5
* @since 3.4 Returns 0 instead of false.
* @since 3.4 $lang accepts PLL_Language or string.
* @since 3.4 Returns `0` instead of `false` if not translated or if the post has no language.
* @since 3.4 $lang accepts `PLL_Language` or string.
*
* @param int $post_id Post ID.
* @param PLL_Language|string $lang Optional language (object or slug), defaults to the current language.
* @return int|false The translation post ID if exists, otherwise the passed ID. False if the passed object has no language or if the language doesn't exist.
* @return int The translation post ID if exists. 0 if not translated, the post has no language or if the language doesn't exist.
*
* @phpstan-return int<0, max>|false
* @phpstan-return int<0, max>
*/
function pll_get_post( $post_id, $lang = '' ) {
$lang = $lang ? $lang : pll_current_language();
$lang = $lang ?: pll_current_language();
if ( empty( $lang ) ) {
return false;
return 0;
}
return PLL()->model->post->get( $post_id, $lang );
@@ -133,20 +133,20 @@ function pll_get_post( $post_id, $lang = '' ) {
*
* @api
* @since 0.5
* @since 3.4 Returns 0 instead of false.
* @since 3.4 Returns `0` instead of `false` if not translated or if the term has no language.
* @since 3.4 $lang accepts PLL_Language or string.
*
* @param int $term_id Term ID.
* @param PLL_Language|string $lang Optional language (object or slug), defaults to the current language.
* @return int|false The translation term ID if exists, otherwise the passed ID. False if the passed object has no language or if the language doesn't exist.
* @return int The translation term ID if exists. 0 if not translated, the term has no language or if the language doesn't exist.
*
* @phpstan-return int<0, max>|false
* @phpstan-return int<0, max>
*/
function pll_get_term( $term_id, $lang = null ) {
$lang = $lang ? $lang : pll_current_language();
function pll_get_term( $term_id, $lang = '' ) {
$lang = $lang ?: pll_current_language();
if ( empty( $lang ) ) {
return false;
return 0;
}
return PLL()->model->term->get( $term_id, $lang );
@@ -300,19 +300,8 @@ function pll_translate_string( $string, $lang ) {
return $string;
}
static $cache; // Cache object to avoid loading the same translations object several times.
if ( empty( $cache ) ) {
$cache = new PLL_Cache();
}
$mo = $cache->get( $lang->slug );
if ( ! $mo instanceof PLL_MO ) {
$mo = new PLL_MO();
$mo->import_from_db( $lang );
$cache->set( $lang->slug, $mo );
}
$mo = new PLL_MO();
$mo->import_from_db( $lang );
return $mo->translate( $string );
}
@@ -560,6 +549,112 @@ function pll_count_posts( $lang, $args = array() ) {
return PLL()->model->count_posts( $lang, $args );
}
/**
* Wraps `wp_insert_post` with language feature.
*
* @since 3.7
*
* @param array $postarr {
* An array of elements that make up a post to insert.
* @See https://developer.wordpress.org/reference/functions/wp_insert_post/ wp_insert_post() for accepted arguments.
*
* @type string[] $translations The translation group to assign to the post with language slug as keys and post ID as values.
* }
* @param PLL_Language|string $language The post language object or slug.
* @return int|WP_Error The post ID on success. The value `WP_Error` on failure.
*/
function pll_insert_post( array $postarr, $language ) {
$language = PLL()->model->get_language( $language );
if ( ! $language instanceof PLL_Language ) {
return new WP_Error( 'invalid_language', __( 'Please provide a valid language.', 'polylang' ) );
}
return PLL()->model->post->insert( $postarr, $language );
}
/**
* Wraps `wp_insert_term` with language feature.
*
* @since 3.7
*
* @param string $term The term name to add.
* @param string $taxonomy The taxonomy to which to add the term.
* @param PLL_Language|string $language The term language object or slug.
* @param array $args {
* Optional. Array of arguments for inserting a term.
*
* @type string $alias_of Slug of the term to make this term an alias of.
* Default empty string. Accepts a term slug.
* @type string $description The term description. Default empty string.
* @type int $parent The id of the parent term. Default 0.
* @type string $slug The term slug to use. Default empty string.
* @type string[] $translations The translation group to assign to the term with language slug as keys and `term_id` as values.
* }
* @return array|WP_Error {
* An array of the new term data, `WP_Error` otherwise.
*
* @type int $term_id The new term ID.
* @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string.
* }
*/
function pll_insert_term( string $term, string $taxonomy, $language, array $args = array() ) {
$language = PLL()->model->get_language( $language );
if ( ! $language instanceof PLL_Language ) {
return new WP_Error( 'invalid_language', __( 'Please provide a valid language.', 'polylang' ) );
}
return PLL()->model->term->insert( $term, $taxonomy, $language, $args );
}
/**
* Wraps `wp_update_post` with language feature.
*
* @since 3.7
*
* @param array $postarr {
* Optional. An array of elements that make up a post to update.
* @See https://developer.wordpress.org/reference/functions/wp_insert_post/ wp_insert_post() for accepted arguments.
*
* @type PLL_Language|string $lang The post language object or slug.
* @type string[] $translations The translation group to assign to the post with language slug as keys and post ID as values.
* }
* @return int|WP_Error The post ID on success. The value `WP_Error` on failure.
*/
function pll_update_post( array $postarr ) {
return PLL()->model->post->update( $postarr );
}
/**
* Wraps `wp_update_term` with language feature.
*
* @since 3.7
*
* @param int $term_id The ID of the term.
* @param array $args {
* Optional. Array of arguments for updating a term.
*
* @type string $alias_of Slug of the term to make this term an alias of.
* Default empty string. Accepts a term slug.
* @type string $description The term description. Default empty string.
* @type int $parent The id of the parent term. Default 0.
* @type string $slug The term slug to use. Default empty string.
* @type string $name The term name.
* @type PLL_Language|string $lang The term language object or slug.
* @type string[] $translations The translation group to assign to the term with language slug as keys and `term_id` as values.
* }
* @return array|WP_Error {
* An array containing the `term_id` and `term_taxonomy_id`, `WP_Error` otherwise.
*
* @type int $term_id The new term ID.
* @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string.
* }
*/
function pll_update_term( int $term_id, array $args = array() ) {
return PLL()->model->term->update( $term_id, $args );
}
/**
* Allows to access the Polylang instance.
* However, it is always preferable to use API functions