| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages filters and actions related to default terms. |
| 8 |
* |
| 9 |
* @since 3.7 |
| 10 |
*/ |
| 11 |
class PLL_Default_Term { |
| 12 |
|
| 13 |
/** |
| 14 |
* A reference to the PLL_Model instance. |
| 15 |
* |
| 16 |
* @var PLL_Model |
| 17 |
*/ |
| 18 |
protected $model; |
| 19 |
|
| 20 |
/** |
| 21 |
* Preferred language to assign to new contents. |
| 22 |
* |
| 23 |
* @var PLL_Language|null |
| 24 |
*/ |
| 25 |
protected $curlang; |
| 26 |
|
| 27 |
/** |
| 28 |
* Array of registered taxonomy names for which Polylang manages languages and translations. |
| 29 |
* |
| 30 |
* @var string[] |
| 31 |
*/ |
| 32 |
protected $taxonomies; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor: setups properties. |
| 36 |
* |
| 37 |
* @since 3.1 |
| 38 |
* |
| 39 |
* @param object $polylang The Polylang object. |
| 40 |
*/ |
| 41 |
public function __construct( &$polylang ) { |
| 42 |
$this->model = &$polylang->model; |
| 43 |
$this->curlang = &$polylang->curlang; |
| 44 |
$this->taxonomies = $this->model->get_translated_taxonomies(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Setups filters and actions needed. |
| 49 |
* |
| 50 |
* @since 3.1 |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function add_hooks() { |
| 55 |
foreach ( $this->taxonomies as $taxonomy ) { |
| 56 |
if ( 'category' === $taxonomy ) { |
| 57 |
// Allows to get the default terms in all languages. |
| 58 |
add_filter( 'option_default_' . $taxonomy, array( $this, 'option_default_term' ) ); |
| 59 |
add_action( 'update_option_default_' . $taxonomy, array( $this, 'update_option_default_term' ), 10, 2 ); |
| 60 |
} |
| 61 |
} |
| 62 |
add_action( 'pll_add_language', array( $this, 'handle_default_term_on_create_language' ) ); |
| 63 |
|
| 64 |
// The default term should be in the default language. |
| 65 |
add_action( 'pll_update_default_lang', array( $this, 'update_default_term_language' ) ); |
| 66 |
|
| 67 |
// Prevents deleting all the translations of the default term. |
| 68 |
add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_term' ), 10, 4 ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Filters the default term to return the one in the right language. |
| 73 |
* |
| 74 |
* @since 1.2 |
| 75 |
* |
| 76 |
* @param int $taxonomy_term_id The taxonomy term id. |
| 77 |
* @return int A taxonomy term id. |
| 78 |
*/ |
| 79 |
public function option_default_term( $taxonomy_term_id ) { |
| 80 |
if ( isset( $this->curlang ) && $tr = $this->model->term->get( $taxonomy_term_id, $this->curlang ) ) { |
| 81 |
$taxonomy_term_id = $tr; |
| 82 |
} |
| 83 |
return $taxonomy_term_id; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Checks if the new default term is translated in all languages. |
| 88 |
* If not, create the translations. |
| 89 |
* |
| 90 |
* @since 1.7 |
| 91 |
* |
| 92 |
* @param int $old_value The old option value. |
| 93 |
* @param int $value The new option value. |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function update_option_default_term( $old_value, $value ) { |
| 97 |
$default_cat_lang = $this->model->term->get_language( $value ); |
| 98 |
|
| 99 |
// Assign a default language to default term. |
| 100 |
if ( ! $default_cat_lang ) { |
| 101 |
$default_cat_lang = $this->model->get_default_language(); |
| 102 |
$this->model->term->set_language( (int) $value, $default_cat_lang ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( empty( $default_cat_lang ) ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
$current_filter = current_filter(); |
| 110 |
if ( ! $current_filter ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$taxonomy = substr( $current_filter, 22 ); |
| 115 |
|
| 116 |
foreach ( $this->model->get_languages_list() as $language ) { |
| 117 |
if ( $language->slug != $default_cat_lang->slug && ! $this->model->term->get_translation( $value, $language ) ) { |
| 118 |
$this->create_default_term( $language, $taxonomy ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Creates a default term for a language. |
| 125 |
* |
| 126 |
* @since 1.2 |
| 127 |
* |
| 128 |
* @param PLL_Language|string|int $lang Language. |
| 129 |
* @param string $taxonomy The current taxonomy. |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function create_default_term( $lang, $taxonomy ) { |
| 133 |
$lang = $this->model->get_language( $lang ); |
| 134 |
|
| 135 |
// Create a new term. |
| 136 |
// FIXME this is translated in admin language when we would like it in $lang |
| 137 |
$cat_name = __( 'Uncategorized', 'polylang' ); |
| 138 |
$cat_slug = sanitize_title( $cat_name . '-' . $lang->slug ); |
| 139 |
$cat = wp_insert_term( $cat_name, $taxonomy, array( 'slug' => $cat_slug ) ); |
| 140 |
|
| 141 |
// Check that the term was not previously created (in case the language was deleted and recreated). |
| 142 |
$cat = $cat->error_data['term_exists'] ?? $cat['term_id']; |
| 143 |
|
| 144 |
// Set language. |
| 145 |
$this->model->term->set_language( (int) $cat, $lang ); |
| 146 |
|
| 147 |
// This is a translation of the default term. |
| 148 |
$default = (int) get_option( 'default_' . $taxonomy ); |
| 149 |
$translations = $this->model->term->get_translations( $default ); |
| 150 |
|
| 151 |
$this->model->term->save_translations( (int) $cat, $translations ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Manages the default term when new languages are created. |
| 156 |
* |
| 157 |
* @since 3.1 |
| 158 |
* |
| 159 |
* @param array $args Argument used to create the language. @see `Model\Languages::add()`. |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public function handle_default_term_on_create_language( $args ) { |
| 163 |
foreach ( $this->taxonomies as $taxonomy ) { |
| 164 |
if ( 'category' === $taxonomy ) { |
| 165 |
$default = (int) get_option( 'default_' . $taxonomy ); |
| 166 |
|
| 167 |
// Assign default language to default term. |
| 168 |
if ( ! $this->model->term->get_language( $default ) ) { |
| 169 |
$this->model->term->set_language( $default, $args['slug'] ); |
| 170 |
} elseif ( empty( $args['no_default_cat'] ) && ! $this->model->term->get( $default, $args['slug'] ) ) { |
| 171 |
$this->create_default_term( $args['slug'], $taxonomy ); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Prevents deleting all the translations of the default term. |
| 179 |
* |
| 180 |
* @since 2.1 |
| 181 |
* |
| 182 |
* @param array $caps The user's actual capabilities. |
| 183 |
* @param string $cap Capability name. |
| 184 |
* @param int $user_id The user ID. |
| 185 |
* @param array $args Adds the context to the cap. The term id. |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
public function fix_delete_default_term( $caps, $cap, $user_id, $args ) { |
| 189 |
if ( 'delete_term' === $cap && $this->is_default_term( reset( $args ) ) ) { |
| 190 |
$caps[] = 'do_not_allow'; |
| 191 |
} |
| 192 |
|
| 193 |
return $caps; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Checks if the term is the default term. |
| 198 |
* |
| 199 |
* @since 3.1 |
| 200 |
* |
| 201 |
* @param int $term_id The term ID. |
| 202 |
* @return bool True if the term is the default term, false otherwise. |
| 203 |
*/ |
| 204 |
public function is_default_term( $term_id ) { |
| 205 |
$term = get_term( $term_id ); |
| 206 |
if ( $term instanceof WP_Term ) { |
| 207 |
$default_term_id = get_option( 'default_' . $term->taxonomy ); |
| 208 |
return $default_term_id && in_array( $default_term_id, $this->model->term->get_translations( $term_id ) ); |
| 209 |
} |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Updates the default term language. |
| 215 |
* |
| 216 |
* @since 3.1 |
| 217 |
* |
| 218 |
* @param string $slug Language slug. |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
public function update_default_term_language( $slug ) { |
| 222 |
foreach ( $this->taxonomies as $taxonomy ) { |
| 223 |
if ( 'category' === $taxonomy ) { |
| 224 |
$default_cats = $this->model->term->get_translations( get_option( 'default_' . $taxonomy ) ); |
| 225 |
if ( isset( $default_cats[ $slug ] ) ) { |
| 226 |
update_option( 'default_' . $taxonomy, $default_cats[ $slug ] ); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|