2021-09-21 15:01:08 +02:00
< ? php
2021-09-21 15:06:29 +02:00
/**
* @ package Polylang
*/
2021-09-21 15:01:08 +02:00
/**
2021-09-21 15:06:29 +02:00
* Setups the objects languages and translations model .
2021-09-21 15:01:08 +02:00
*
* @ since 1.8
*/
abstract class PLL_Translated_Object {
2021-09-21 15:06:29 +02:00
/**
* @ var PLL_Model
*/
2021-09-21 15:01:08 +02:00
public $model ;
/**
2021-09-21 15:06:29 +02:00
* Object type to use when registering the taxonomies .
* Left empty for posts .
*
* @ var string | null
*/
protected $object_type ;
/**
* Object type to use when checking capabilities .
*
* @ var string
*/
protected $type ;
/**
* Taxonomy name for the languages .
*
* @ var string
*/
protected $tax_language ;
/**
* Taxonomy name for the translation groups .
*
* @ var string
*/
protected $tax_translations ;
/**
* PLL_Language property name for the term_taxonomy id .
*
* @ var string
*/
protected $tax_tt ;
/**
* Constructor .
2021-09-21 15:01:08 +02:00
*
* @ since 1.8
*
2021-09-21 15:06:29 +02:00
* @ param PLL_Model $model Instance of PLL_Model .
2021-09-21 15:01:08 +02:00
*/
public function __construct ( & $model ) {
$this -> model = & $model ;
2021-09-21 15:06:29 +02:00
/*
* Register our taxonomies as soon as possible .
* This is early registration , not ready for rewrite rules as $wp_rewrite will be setup later .
*/
2021-09-21 15:01:08 +02:00
$args = array ( 'label' => false , 'public' => false , 'query_var' => false , 'rewrite' => false , '_pll' => true );
register_taxonomy ( $this -> tax_language , $this -> object_type , $args );
2021-09-21 15:06:29 +02:00
$args [ 'update_count_callback' ] = '_update_generic_term_count' ; // Count *all* objects to avoid deleting in clean_translations_terms.
2021-09-21 15:01:08 +02:00
register_taxonomy ( $this -> tax_translations , $this -> object_type , $args );
}
/**
2021-09-21 15:06:29 +02:00
* Stores the language in the database .
*
* @ since 0.6
*
* @ param int $id Object id .
* @ param int | string | PLL_Language $lang Language ( term_id or slug or object ) .
* @ return void
*/
abstract public function set_language ( $id , $lang );
/**
* Returns the language of an object .
*
* @ since 0.1
*
* @ param int $id Object id .
* @ return PLL_Language | false PLL_Language object , false if no language is associated to that object .
*/
abstract public function get_language ( $id );
/**
* Assigns a new language to an object , taking care of the translations group .
*
* @ since 3.1
*
* @ param int $id Object id .
* @ param PLL_Language $lang New language to assign to the object .
* @ return void
*/
public function update_language ( $id , $lang ) {
if ( $this -> get_language ( $id ) === $lang ) {
return ;
}
$this -> set_language ( $id , $lang );
$translations = $this -> get_translations ( $id );
if ( $translations ) {
// Remove the post's former language from the new translations group.
$translations = array_diff ( $translations , array ( $id ) );
$this -> save_translations ( $id , $translations );
}
}
/**
* Wrap wp_get_object_terms () to cache it and return only one object .
* inspired by the WordPress function get_the_terms () .
2021-09-21 15:01:08 +02:00
*
* @ since 1.2
*
2021-09-21 15:06:29 +02:00
* @ param int $object_id Object id ( typically a post_id or term_id ) .
* @ param string $taxonomy Polylang taxonomy depending if we are looking for a post ( or term ) language ( or translation ) .
* @ return WP_Term | false The term associated to the object in the requested taxonomy if it exists , false otherwise .
2021-09-21 15:01:08 +02:00
*/
public function get_object_term ( $object_id , $taxonomy ) {
if ( empty ( $object_id ) || is_wp_error ( $object_id ) ) {
return false ;
}
$object_id = ( int ) $object_id ;
2021-09-21 15:06:29 +02:00
if ( $object_id < 0 ) {
return false ;
}
2021-09-21 15:01:08 +02:00
$term = get_object_term_cache ( $object_id , $taxonomy );
if ( false === $term ) {
2021-09-21 15:06:29 +02:00
// Query language and translations at the same time.
2021-09-21 15:01:08 +02:00
$taxonomies = array ( $this -> tax_language , $this -> tax_translations );
2021-09-21 15:06:29 +02:00
// Query terms.
$terms = array ();
$object_terms = wp_get_object_terms ( $object_id , $taxonomies , array ( 'update_term_meta_cache' => false ) );
if ( is_array ( $object_terms ) ) {
foreach ( $object_terms as $t ) {
$terms [ $t -> taxonomy ] = $t ;
if ( $t -> taxonomy == $taxonomy ) {
$term = $t ;
}
2021-09-21 15:01:08 +02:00
}
}
2021-09-21 15:06:29 +02:00
// Stores it the way WP expects it. Set an empty cache if no term was found in the taxonomy.
2021-09-21 15:01:08 +02:00
foreach ( $taxonomies as $tax ) {
wp_cache_add ( $object_id , empty ( $terms [ $tax ] ) ? array () : array ( $terms [ $tax ] ), $tax . '_relationships' );
}
}
else {
$term = reset ( $term );
}
return empty ( $term ) ? false : $term ;
}
/**
2021-09-21 15:06:29 +02:00
* Tells whether a translation term must be updated .
2021-09-21 15:01:08 +02:00
*
* @ since 2.3
*
2021-09-21 15:06:29 +02:00
* @ param int $id Object id ( typically a post_id or term_id ) .
* @ param int [] $translations An associative array of translations with language code as key and translation id as value .
* @ return bool
2021-09-21 15:01:08 +02:00
*/
protected function should_update_translation_group ( $id , $translations ) {
2021-09-21 15:06:29 +02:00
// Don't do anything if no translations have been added to the group.
$old_translations = $this -> get_translations ( $id ); // Includes at least $id itself.
2021-09-21 15:01:08 +02:00
return count ( array_diff_assoc ( $translations , $old_translations ) ) > 0 ;
}
/**
2021-09-21 15:06:29 +02:00
* Saves translations for posts or terms .
2021-09-21 15:01:08 +02:00
*
* @ since 0.5
*
2021-09-21 15:06:29 +02:00
* @ param int $id Object id ( typically a post_id or term_id ) .
* @ param int [] $translations An associative array of translations with language code as key and translation id as value .
*
* @ return int [] An associative array with language codes as key and post ids as values .
2021-09-21 15:01:08 +02:00
*/
public function save_translations ( $id , $translations ) {
2021-09-21 15:06:29 +02:00
$translations = $this -> validate_translations ( $translations );
2021-09-21 15:01:08 +02:00
$id = ( int ) $id ;
2021-09-21 15:06:29 +02:00
if ( $lang = $this -> get_language ( $id ) ) {
// Sanitize the translations array.
2021-09-21 15:01:08 +02:00
$translations = array_map ( 'intval' , $translations );
2021-09-21 15:06:29 +02:00
$translations = array_merge ( array ( $lang -> slug => $id ), $translations ); // Make sure this object is in translations.
$translations = array_diff ( $translations , array ( 0 ) ); // Don't keep non translated languages.
$translations = array_intersect_key ( $translations , array_flip ( $this -> model -> get_languages_list ( array ( 'fields' => 'slug' ) ) ) ); // Keep only valid languages slugs as keys.
2021-09-21 15:01:08 +02:00
2021-09-21 15:06:29 +02:00
// Unlink removed translations.
2021-09-21 15:01:08 +02:00
$old_translations = $this -> get_translations ( $id );
foreach ( array_diff_assoc ( $old_translations , $translations ) as $object_id ) {
$this -> delete_translation ( $object_id );
}
2021-09-21 15:06:29 +02:00
// Check id we need to create or update the translation group.
2021-09-21 15:01:08 +02:00
if ( $this -> should_update_translation_group ( $id , $translations ) ) {
$terms = wp_get_object_terms ( $translations , $this -> tax_translations );
$term = reset ( $terms );
2021-09-21 15:06:29 +02:00
// Create a new term if necessary.
2021-09-21 15:01:08 +02:00
if ( empty ( $term ) ) {
2021-09-21 15:06:29 +02:00
wp_insert_term ( $group = uniqid ( 'pll_' ), $this -> tax_translations , array ( 'description' => maybe_serialize ( $translations ) ) );
} else {
// Take care not to overwrite extra data stored in the description field, if any.
$d = maybe_unserialize ( $term -> description );
$d = is_array ( $d ) ? array_diff_key ( $d , $old_translations ) : array (); // Remove old translations.
$d = array_merge ( $d , $translations ); // Add new one.
wp_update_term ( $group = ( int ) $term -> term_id , $this -> tax_translations , array ( 'description' => maybe_serialize ( $d ) ) );
2021-09-21 15:01:08 +02:00
}
2021-09-21 15:06:29 +02:00
// Link all translations to the new term.
2021-09-21 15:01:08 +02:00
foreach ( $translations as $p ) {
wp_set_object_terms ( $p , $group , $this -> tax_translations );
}
2021-09-21 15:06:29 +02:00
// Clean now unused translation groups.
2021-09-21 15:01:08 +02:00
foreach ( wp_list_pluck ( $terms , 'term_id' ) as $term_id ) {
$term = get_term ( $term_id , $this -> tax_translations );
if ( empty ( $term -> count ) ) {
wp_delete_term ( $term_id , $this -> tax_translations );
}
}
}
2021-09-21 15:06:29 +02:00
return $translations ;
} else {
return array ();
}
}
/**
* Returns translations after checking the translated post is in the right language
*
* @ since 3.1
*
* @ param int [] $translations An associative array of translations with language code as key and translation id as value .
*
* @ return int []
*/
public function validate_translations ( $translations ) {
$valid_translations = array ();
foreach ( $translations as $lang => $tr_id ) {
$valid_translations [ $lang ] = ( $tr_id && $this -> get_language ( ( int ) $tr_id ) -> slug == $lang ) ? ( int ) $tr_id : 0 ;
2021-09-21 15:01:08 +02:00
}
2021-09-21 15:06:29 +02:00
return $valid_translations ;
2021-09-21 15:01:08 +02:00
}
/**
2021-09-21 15:06:29 +02:00
* Deletes a translation of a post or term .
2021-09-21 15:01:08 +02:00
*
* @ since 0.5
*
2021-09-21 15:06:29 +02:00
* @ param int $id Object id ( typically a post_id or term_id ) .
* @ return void
2021-09-21 15:01:08 +02:00
*/
public function delete_translation ( $id ) {
$id = ( int ) $id ;
$term = $this -> get_object_term ( $id , $this -> tax_translations );
if ( ! empty ( $term ) ) {
2021-09-21 15:06:29 +02:00
$d = maybe_unserialize ( $term -> description );
if ( is_array ( $d ) ) {
$slug = array_search ( $id , $this -> get_translations ( $id ) ); // In case some plugin stores the same value with different key.
unset ( $d [ $slug ] );
}
2021-09-21 15:01:08 +02:00
if ( empty ( $d ) ) {
wp_delete_term ( ( int ) $term -> term_id , $this -> tax_translations );
2021-09-21 15:06:29 +02:00
} else {
wp_update_term ( ( int ) $term -> term_id , $this -> tax_translations , array ( 'description' => maybe_serialize ( $d ) ) );
2021-09-21 15:01:08 +02:00
}
}
}
/**
2021-09-21 15:06:29 +02:00
* Returns an array of translations of a post or term .
2021-09-21 15:01:08 +02:00
*
* @ since 0.5
*
2021-09-21 15:06:29 +02:00
* @ param int $id Object id ( typically a post_id or term_id ) .
* @ return int [] An associative array of translations with language code as key and translation id as value .
2021-09-21 15:01:08 +02:00
*/
public function get_translations ( $id ) {
$term = $this -> get_object_term ( $id , $this -> tax_translations );
2021-09-21 15:06:29 +02:00
$translations = empty ( $term ) ? array () : maybe_unserialize ( $term -> description );
2021-09-21 15:01:08 +02:00
2021-09-21 15:06:29 +02:00
// Make sure we return only translations ( thus we allow plugins to store other information in the array ).
2021-09-21 15:01:08 +02:00
if ( is_array ( $translations ) ) {
$translations = array_intersect_key ( $translations , array_flip ( $this -> model -> get_languages_list ( array ( 'fields' => 'slug' ) ) ) );
}
2021-09-21 15:06:29 +02:00
// Make sure to return at least the passed object in its translation array.
2021-09-21 15:01:08 +02:00
if ( empty ( $translations ) && $lang = $this -> get_language ( $id ) ) {
$translations = array ( $lang -> slug => $id );
}
return $translations ;
}
/**
2021-09-21 15:06:29 +02:00
* Returns the id of the translation of a post or term .
2021-09-21 15:01:08 +02:00
*
* @ since 0.5
*
2021-09-21 15:06:29 +02:00
* @ param int $id Object id ( typically a post_id or term_id ) .
* @ param PLL_Language | string $lang Language ( slug or object ) .
* @ return int | false Object id of the translation , false if there is none .
2021-09-21 15:01:08 +02:00
*/
public function get_translation ( $id , $lang ) {
if ( ! $lang = $this -> model -> get_language ( $lang ) ) {
return false ;
}
$translations = $this -> get_translations ( $id );
return isset ( $translations [ $lang -> slug ] ) ? $translations [ $lang -> slug ] : false ;
}
/**
* Among the object and its translations , returns the id of the object which is in $lang
*
* @ since 0.1
*
2021-09-21 15:06:29 +02:00
* @ param int $id Object id ( typically a post_id or term_id ) .
* @ param int | string | PLL_Language $lang Language ( term_id or slug or object ) .
* @ return int | false The translation object id if exists , otherwise the passed id , false if the passed object has no language .
2021-09-21 15:01:08 +02:00
*/
public function get ( $id , $lang ) {
2021-09-21 15:06:29 +02:00
$id = ( int ) $id ;
$lang = $this -> model -> get_language ( $lang );
$obj_lang = $this -> get_language ( $id );
if ( empty ( $lang ) || empty ( $obj_lang ) ) {
2021-09-21 15:01:08 +02:00
return false ;
}
return $obj_lang -> term_id == $lang -> term_id ? $id : $this -> get_translation ( $id , $lang );
}
/**
2021-09-21 15:06:29 +02:00
* A join clause to add to sql queries when filtering by language is needed directly in query .
*
* @ since 1.2
*
* @ param string $alias Optional alias for object table .
* @ return string Join clause .
*/
abstract public function join_clause ( $alias = '' );
/**
* A where clause to add to sql queries when filtering by language is needed directly in query .
2021-09-21 15:01:08 +02:00
*
* @ since 1.2
*
2021-09-21 15:06:29 +02:00
* @ param PLL_Language | string | string [] $lang PLL_Language object or a comma separated list of language slug or an array of language slugs .
* @ return string Where clause .
2021-09-21 15:01:08 +02:00
*/
public function where_clause ( $lang ) {
$tt_id = $this -> tax_tt ;
2021-09-21 15:06:29 +02:00
/*
* $lang is an object .
* This is generally the case if the query is coming from Polylang .
*/
2021-09-21 15:01:08 +02:00
if ( is_object ( $lang ) ) {
return ' AND pll_tr.term_taxonomy_id = ' . absint ( $lang -> $tt_id );
}
2021-09-21 15:06:29 +02:00
/*
* $lang is a comma separated list of slugs ( or an array of slugs ) .
* This is generally the case is the query is coming from outside with a 'lang' parameter .
*/
$slugs = is_array ( $lang ) ? $lang : explode ( ',' , $lang );
$languages = array ();
2021-09-21 15:01:08 +02:00
foreach ( $slugs as $slug ) {
$languages [] = absint ( $this -> model -> get_language ( $slug ) -> $tt_id );
}
return ' AND pll_tr.term_taxonomy_id IN ( ' . implode ( ',' , $languages ) . ' )' ;
}
/**
2021-09-21 15:06:29 +02:00
* Returns ids of objects in a language similarly to get_objects_in_term () for a taxonomy .
* It is faster than get_objects_in_term () as it avoids a JOIN .
2021-09-21 15:01:08 +02:00
*
* @ since 1.4
*
2021-09-21 15:06:29 +02:00
* @ param PLL_Language $lang PLL_Language object .
* @ return int [] Object ids .
2021-09-21 15:01:08 +02:00
*/
public function get_objects_in_language ( $lang ) {
global $wpdb ;
$tt_id = $this -> tax_tt ;
2021-09-21 15:06:29 +02:00
$last_changed = wp_cache_get_last_changed ( 'terms' );
$cache_key = " polylang:get_objects_in_language: { $lang -> $tt_id } : { $last_changed } " ;
$cache = wp_cache_get ( $cache_key , 'terms' );
if ( false === $cache ) {
$object_ids = $wpdb -> get_col ( $wpdb -> prepare ( " SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d " , $lang -> $tt_id ) );
wp_cache_set ( $cache_key , $object_ids , 'terms' );
} else {
$object_ids = ( array ) $cache ;
}
if ( ! $object_ids ) {
return array ();
}
return $object_ids ;
}
/**
* Check if a user can synchronize translations .
*
* @ since 2.6
*
* @ param int $id Object id .
* @ return bool
*/
public function current_user_can_synchronize ( $id ) {
/**
* Filters whether a synchronization capability check should take place .
*
* @ since 2.6
*
* @ param bool | null $check Null to enable the capability check ,
* true to always allow the synchronization ,
* false to always disallow the synchronization .
* Defaults to true .
* @ param int $id The synchronization source object id .
*/
$check = apply_filters ( " pll_pre_current_user_can_synchronize_ { $this -> type } " , true , $id );
if ( null !== $check ) {
return $check ;
}
if ( ! current_user_can ( " edit_ { $this -> type } " , $id ) ) {
return false ;
}
foreach ( $this -> get_translations ( $id ) as $tr_id ) {
if ( $tr_id !== $id && ! current_user_can ( " edit_ { $this -> type } " , $tr_id ) ) {
return false ;
}
}
return true ;
2021-09-21 15:01:08 +02:00
}
}