| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Manages filters and actions related to terms on admin side |
| 8 | 5 | * |
| @@ -8,9 +5,9 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 1.2 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Admin_Filters_Term { |
| 12 | - public $links, $model, $options, $pref_lang; | |
| 9 | + public $links, $model, $options, $curlang, $filter_lang, $pref_lang; | |
| 13 | 10 | protected $pre_term_name; // Used to store the term name before creating a slug if needed |
| 14 | 11 | protected $post_id; // Used to store the current post_id when bulk editing posts |
| 15 | 12 | |
| 16 | 13 | /** |
| @@ -21,8 +18,10 @@ | ||
| 21 | 18 | public function __construct( &$polylang ) { |
| 22 | 19 | $this->links = &$polylang->links; |
| 23 | 20 | $this->model = &$polylang->model; |
| 24 | 21 | $this->options = &$polylang->options; |
| 22 | + $this->curlang = &$polylang->curlang; | |
| 23 | + $this->filter_lang = &$polylang->filter_lang; | |
| 25 | 24 | $this->pref_lang = &$polylang->pref_lang; |
| 26 | 25 | |
| 27 | 26 | foreach ( $this->model->get_translated_taxonomies() as $tax ) { |
| 28 | 27 | // Adds the language field in the 'Categories' and 'Post Tags' panels |
| @@ -29,14 +28,17 @@ | ||
| 29 | 28 | add_action( $tax . '_add_form_fields', array( $this, 'add_term_form' ) ); |
| 30 | 29 | |
| 31 | 30 | // Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels |
| 32 | 31 | add_action( $tax . '_edit_form_fields', array( $this, 'edit_term_form' ) ); |
| 32 | + | |
| 33 | + // Adds action related to languages when deleting categories and post tags | |
| 34 | + add_action( 'delete_' . $tax, array( $this, 'delete_term' ) ); | |
| 33 | 35 | } |
| 34 | 36 | |
| 35 | 37 | // Adds actions related to languages when creating or saving categories and post tags |
| 36 | 38 | add_filter( 'wp_dropdown_cats', array( $this, 'wp_dropdown_cats' ) ); |
| 37 | - add_action( 'create_term', array( $this, 'save_term' ), 900, 3 ); | |
| 38 | - add_action( 'edit_term', array( $this, 'save_term' ), 900, 3 ); // Late as it may conflict with other plugins, see http://wordpress.org/support/topic/polylang-and-wordpress-seo-by-yoast | |
| 39 | + add_action( 'create_term', array( $this, 'save_term' ), 999, 3 ); | |
| 40 | + add_action( 'edit_term', array( $this, 'save_term' ), 999, 3 ); // late as it may conflict with other plugins, see http://wordpress.org/support/topic/polylang-and-wordpress-seo-by-yoast | |
| 39 | 41 | add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 40 | 42 | add_filter( 'pre_term_name', array( $this, 'pre_term_name' ) ); |
| 41 | 43 | add_filter( 'pre_term_slug', array( $this, 'pre_term_slug' ), 10, 2 ); |
| 42 | 44 | |
| @@ -43,8 +45,14 @@ | ||
| 43 | 45 | // Ajax response for edit term form |
| 44 | 46 | add_action( 'wp_ajax_term_lang_choice', array( $this, 'term_lang_choice' ) ); |
| 45 | 47 | add_action( 'wp_ajax_pll_terms_not_translated', array( $this, 'ajax_terms_not_translated' ) ); |
| 46 | 48 | |
| 49 | + // Adds cache domain when querying terms | |
| 50 | + add_filter( 'get_terms_args', array( $this, 'get_terms_args' ), 10, 2 ); | |
| 51 | + | |
| 52 | + // Filters categories and post tags by language | |
| 53 | + add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 3 ); | |
| 54 | + | |
| 47 | 55 | // Allows to get the default categories in all languages |
| 48 | 56 | add_filter( 'option_default_category', array( $this, 'option_default_category' ) ); |
| 49 | 57 | add_action( 'update_option_default_category', array( $this, 'update_option_default_category' ), 10, 2 ); |
| 50 | 58 | |
| @@ -57,62 +65,44 @@ | ||
| 57 | 65 | * |
| 58 | 66 | * @since 0.1 |
| 59 | 67 | */ |
| 60 | 68 | public function add_term_form() { |
| 61 | - if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 62 | - $taxonomy = sanitize_key( $_GET['taxonomy'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 63 | - } | |
| 69 | + $taxonomy = $_GET['taxonomy']; | |
| 70 | + $post_type = isset( $GLOBALS['post_type'] ) ? $GLOBALS['post_type'] : $_REQUEST['post_type']; | |
| 64 | 71 | |
| 65 | - if ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 66 | - $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 67 | - } | |
| 68 | - | |
| 69 | - if ( isset( $GLOBALS['post_type'] ) ) { | |
| 70 | - $post_type = $GLOBALS['post_type']; | |
| 71 | - } | |
| 72 | - | |
| 73 | - if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) { | |
| 72 | + if ( ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) { | |
| 74 | 73 | return; |
| 75 | 74 | } |
| 76 | 75 | |
| 77 | - $from_term_id = isset( $_GET['from_tag'] ) ? (int) $_GET['from_tag'] : 0; // phpcs:ignore WordPress.Security.NonceVerification | |
| 78 | - | |
| 79 | - $lang = isset( $_GET['new_lang'] ) ? $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) : $this->pref_lang; // phpcs:ignore WordPress.Security.NonceVerification | |
| 80 | - | |
| 76 | + $lang = isset( $_GET['new_lang'] ) ? $this->model->get_language( $_GET['new_lang'] ) : $this->pref_lang; | |
| 81 | 77 | $dropdown = new PLL_Walker_Dropdown(); |
| 82 | 78 | |
| 83 | - $dropdown_html = $dropdown->walk( | |
| 84 | - $this->model->get_languages_list(), | |
| 85 | - -1, | |
| 86 | - array( | |
| 87 | - 'name' => 'term_lang_choice', | |
| 88 | - 'value' => 'term_id', | |
| 89 | - 'selected' => $lang ? $lang->term_id : '', | |
| 90 | - 'flag' => true, | |
| 91 | - ) | |
| 92 | - ); | |
| 93 | - | |
| 94 | 79 | wp_nonce_field( 'pll_language', '_pll_nonce' ); |
| 95 | 80 | |
| 96 | - printf( | |
| 97 | - '<div class="form-field"> | |
| 81 | + printf( ' | |
| 82 | + <div class="form-field"> | |
| 98 | 83 | <label for="term_lang_choice">%s</label> |
| 99 | 84 | <div id="select-add-term-language">%s</div> |
| 100 | 85 | <p>%s</p> |
| 101 | 86 | </div>', |
| 102 | 87 | esc_html__( 'Language', 'polylang' ), |
| 103 | - $dropdown_html, // phpcs:ignore | |
| 88 | + $dropdown->walk( $this->model->get_languages_list(), array( | |
| 89 | + 'name' => 'term_lang_choice', | |
| 90 | + 'value' => 'term_id', | |
| 91 | + 'selected' => $lang ? $lang->term_id : '', | |
| 92 | + 'flag' => true, | |
| 93 | + ) ), | |
| 104 | 94 | esc_html__( 'Sets the language', 'polylang' ) |
| 105 | 95 | ); |
| 106 | 96 | |
| 107 | - if ( ! empty( $from_term_id ) ) { | |
| 108 | - printf( '<input type="hidden" name="from_tag" value="%d" />', (int) $from_term_id ); | |
| 97 | + if ( ! empty( $_GET['from_tag'] ) ) { | |
| 98 | + printf( '<input type="hidden" name="from_tag" value="%d" />', (int) $_GET['from_tag'] ); | |
| 109 | 99 | } |
| 110 | 100 | |
| 111 | 101 | // Adds translation fields |
| 112 | 102 | echo '<div id="term-translations" class="form-field">'; |
| 113 | 103 | if ( $lang ) { |
| 114 | - include __DIR__ . '/view-translations-term.php'; | |
| 104 | + include PLL_ADMIN_INC . '/view-translations-term.php'; | |
| 115 | 105 | } |
| 116 | 106 | echo '</div>' . "\n"; |
| 117 | 107 | } |
| 118 | 108 | |
| @@ -123,48 +113,29 @@ | ||
| 123 | 113 | * |
| 124 | 114 | * @param object $tag |
| 125 | 115 | */ |
| 126 | 116 | public function edit_term_form( $tag ) { |
| 127 | - if ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 128 | - $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 129 | - } | |
| 117 | + $post_type = isset( $GLOBALS['post_type'] ) ? $GLOBALS['post_type'] : $_REQUEST['post_type']; | |
| 130 | 118 | |
| 131 | - if ( isset( $GLOBALS['post_type'] ) ) { | |
| 132 | - $post_type = $GLOBALS['post_type']; | |
| 133 | - } | |
| 134 | - | |
| 135 | 119 | if ( ! post_type_exists( $post_type ) ) { |
| 136 | 120 | return; |
| 137 | 121 | } |
| 138 | 122 | |
| 139 | - $term_id = $tag->term_id; | |
| 140 | - $taxonomy = $tag->taxonomy; // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable | |
| 123 | + $term_id = $tag->term_id; | |
| 124 | + $taxonomy = $tag->taxonomy; | |
| 141 | 125 | |
| 142 | 126 | $lang = $this->model->term->get_language( $term_id ); |
| 143 | 127 | $lang = empty( $lang ) ? $this->pref_lang : $lang; |
| 144 | 128 | |
| 129 | + $dropdown = new PLL_Walker_Dropdown(); | |
| 130 | + | |
| 145 | 131 | // Disable the language dropdown and the translations input fields for default categories to prevent removal |
| 146 | 132 | $disabled = in_array( get_option( 'default_category' ), $this->model->term->get_translations( $term_id ) ); |
| 147 | 133 | |
| 148 | - $dropdown = new PLL_Walker_Dropdown(); | |
| 149 | - | |
| 150 | - $dropdown_html = $dropdown->walk( | |
| 151 | - $this->model->get_languages_list(), | |
| 152 | - -1, | |
| 153 | - array( | |
| 154 | - 'name' => 'term_lang_choice', | |
| 155 | - 'value' => 'term_id', | |
| 156 | - 'selected' => $lang ? $lang->term_id : '', | |
| 157 | - 'disabled' => $disabled, | |
| 158 | - 'flag' => true, | |
| 159 | - ) | |
| 160 | - ); | |
| 161 | - | |
| 162 | - wp_nonce_field( 'pll_language', '_pll_nonce' ); | |
| 163 | - | |
| 164 | - printf( | |
| 165 | - '<tr class="form-field"> | |
| 134 | + printf( ' | |
| 135 | + <tr class="form-field"> | |
| 166 | 136 | <th scope="row"> |
| 137 | + %s | |
| 167 | 138 | <label for="term_lang_choice">%s</label> |
| 168 | 139 | </th> |
| 169 | 140 | <td id="select-edit-term-language"> |
| 170 | 141 | %s<br /> |
| @@ -170,16 +141,23 @@ | ||
| 170 | 141 | %s<br /> |
| 171 | 142 | <p class="description">%s</p> |
| 172 | 143 | </td> |
| 173 | 144 | </tr>', |
| 145 | + wp_nonce_field( 'pll_language', '_pll_nonce', true, false ), | |
| 174 | 146 | esc_html__( 'Language', 'polylang' ), |
| 175 | - $dropdown_html, // phpcs:ignore | |
| 147 | + $dropdown->walk( $this->model->get_languages_list(), array( | |
| 148 | + 'name' => 'term_lang_choice', | |
| 149 | + 'value' => 'term_id', | |
| 150 | + 'selected' => $lang ? $lang->term_id : '', | |
| 151 | + 'disabled' => $disabled, | |
| 152 | + 'flag' => true, | |
| 153 | + ) ), | |
| 176 | 154 | esc_html__( 'Sets the language', 'polylang' ) |
| 177 | 155 | ); |
| 178 | 156 | |
| 179 | 157 | echo '<tr id="term-translations" class="form-field">'; |
| 180 | 158 | if ( $lang ) { |
| 181 | - include __DIR__ . '/view-translations-term.php'; | |
| 159 | + include PLL_ADMIN_INC . '/view-translations-term.php'; | |
| 182 | 160 | } |
| 183 | 161 | echo '</tr>' . "\n"; |
| 184 | 162 | } |
| 185 | 163 | |
| @@ -191,17 +169,12 @@ | ||
| 191 | 169 | * @param string $output html markup for dropdown list of categories |
| 192 | 170 | * @return string modified html |
| 193 | 171 | */ |
| 194 | 172 | public function wp_dropdown_cats( $output ) { |
| 195 | - if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 196 | - $taxonomy = sanitize_key( $_GET['taxonomy'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 197 | - } | |
| 198 | - | |
| 199 | - if ( isset( $taxonomy, $_GET['from_tag'], $_GET['new_lang'] ) && taxonomy_exists( $taxonomy ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 200 | - $term = get_term( (int) $_GET['from_tag'], $taxonomy ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 201 | - | |
| 173 | + if ( isset( $_GET['taxonomy'], $_GET['from_tag'], $_GET['new_lang'] ) && taxonomy_exists( $_GET['taxonomy'] ) ) { | |
| 174 | + $term = get_term( (int) $_GET['from_tag'], $_GET['taxonomy'] ); | |
| 202 | 175 | if ( $term && $id = $term->parent ) { |
| 203 | - $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 176 | + $lang = $this->model->get_language( $_GET['new_lang'] ); | |
| 204 | 177 | if ( $parent = $this->model->term->get_translation( $id, $lang ) ) { |
| 205 | 178 | return str_replace( '"' . $parent . '"', '"' . $parent . '" selected="selected"', $output ); |
| 206 | 179 | } |
| 207 | 180 | } |
| @@ -209,9 +182,9 @@ | ||
| 209 | 182 | return $output; |
| 210 | 183 | } |
| 211 | 184 | |
| 212 | 185 | /** |
| 213 | - * Stores the current post_id when bulk editing posts for use in save_language and pre_term_slug | |
| 186 | + * stores the current post_id when bulk editing posts for use in save_language and pre_term_slug | |
| 214 | 187 | * |
| 215 | 188 | * @since 1.7 |
| 216 | 189 | * |
| 217 | 190 | * @param int $post_id |
| @@ -216,14 +189,34 @@ | ||
| 216 | 189 | * |
| 217 | 190 | * @param int $post_id |
| 218 | 191 | */ |
| 219 | 192 | public function pre_post_update( $post_id ) { |
| 220 | - if ( isset( $_GET['bulk_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 193 | + if ( isset( $_GET['bulk_edit'] ) ) { | |
| 221 | 194 | $this->post_id = $post_id; |
| 222 | 195 | } |
| 223 | 196 | } |
| 224 | 197 | |
| 225 | 198 | /** |
| 199 | + * Allows to set a language by default for terms if it has no language yet | |
| 200 | + * | |
| 201 | + * @since 1.5.4 | |
| 202 | + * | |
| 203 | + * @param int $term_id | |
| 204 | + * @param string $taxonomy | |
| 205 | + */ | |
| 206 | + protected function set_default_language( $term_id, $taxonomy ) { | |
| 207 | + if ( ! $this->model->term->get_language( $term_id ) ) { | |
| 208 | + // Sets language from term parent if exists thanks to Scott Kingsley Clark | |
| 209 | + if ( ( $term = get_term( $term_id, $taxonomy ) ) && ! empty( $term->parent ) && $parent_lang = $this->model->term->get_language( $term->parent ) ) { | |
| 210 | + $this->model->term->set_language( $term_id, $parent_lang ); | |
| 211 | + } | |
| 212 | + else { | |
| 213 | + $this->model->term->set_language( $term_id, $this->pref_lang ); | |
| 214 | + } | |
| 215 | + } | |
| 216 | + } | |
| 217 | + | |
| 218 | + /** | |
| 226 | 219 | * Saves language |
| 227 | 220 | * |
| 228 | 221 | * @since 1.5 |
| 229 | 222 | * |
| @@ -236,15 +229,16 @@ | ||
| 236 | 229 | // as 'wp_update_term' can be called from outside WP admin |
| 237 | 230 | |
| 238 | 231 | // Edit tags |
| 239 | 232 | if ( isset( $_POST['term_lang_choice'] ) ) { |
| 240 | - if ( isset( $_POST['action'] ) && sanitize_key( $_POST['action'] ) === 'add-' . $taxonomy ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 241 | - check_ajax_referer( 'add-' . $taxonomy, '_ajax_nonce-add-' . $taxonomy ); // Category metabox | |
| 242 | - } else { | |
| 243 | - check_admin_referer( 'pll_language', '_pll_nonce' ); // Edit tags or tags metabox | |
| 233 | + if ( 'add-' . $taxonomy == $_POST['action'] ) { | |
| 234 | + check_ajax_referer( $_POST['action'], '_ajax_nonce-add-' . $taxonomy ); // category metabox | |
| 244 | 235 | } |
| 236 | + else { | |
| 237 | + check_admin_referer( 'pll_language', '_pll_nonce' ); // edit tags or tags metabox | |
| 238 | + } | |
| 245 | 239 | |
| 246 | - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) ); | |
| 240 | + $this->model->term->set_language( $term_id, $this->model->get_language( $_POST['term_lang_choice'] ) ); | |
| 247 | 241 | } |
| 248 | 242 | |
| 249 | 243 | // *Post* bulk edit, in case a new term is created |
| 250 | 244 | elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { |
| @@ -261,24 +255,19 @@ | ||
| 261 | 255 | // FIXME backward compatibility WP < 4.2 |
| 262 | 256 | // No WP function to get all terms with the exact same name so let's use a custom query |
| 263 | 257 | // $terms = get_terms( $taxonomy, array( 'name' => $term->name, 'hide_empty' => false, 'fields' => 'ids' ) ); should be OK in 4.2 |
| 264 | 258 | // I may need to rework the loop below |
| 265 | - $terms = $wpdb->get_results( | |
| 266 | - $wpdb->prepare( | |
| 267 | - "SELECT t.term_id FROM $wpdb->terms AS t | |
| 268 | - INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id | |
| 269 | - WHERE tt.taxonomy = %s AND t.name = %s", | |
| 270 | - $taxonomy, | |
| 271 | - $term->name | |
| 272 | - ) | |
| 273 | - ); | |
| 259 | + $terms = $wpdb->get_results( $wpdb->prepare( " | |
| 260 | + SELECT t.term_id FROM $wpdb->terms AS t | |
| 261 | + INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id | |
| 262 | + WHERE tt.taxonomy = %s AND t.name = %s", | |
| 263 | + $taxonomy, $term->name | |
| 264 | + ) ); | |
| 274 | 265 | |
| 275 | 266 | // If we have several terms with the same name, they are translations of each other |
| 276 | 267 | if ( count( $terms ) > 1 ) { |
| 277 | - $translations = array(); | |
| 278 | - | |
| 279 | 268 | foreach ( $terms as $term ) { |
| 280 | - $translations[ $this->model->term->get_language( $term->term_id )->slug ] = $term->term_id; | |
| 269 | + $translations[ $this->model->term->get_language( $term->term_id )->slug ] = $term->term_id; | |
| 281 | 270 | } |
| 282 | 271 | |
| 283 | 272 | $this->model->term->save_translations( $term_id, $translations ); |
| 284 | 273 | } |
| @@ -284,9 +273,9 @@ | ||
| 284 | 273 | } |
| 285 | 274 | } |
| 286 | 275 | |
| 287 | 276 | else { |
| 288 | - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ) ); | |
| 277 | + $this->model->term->set_language( $term_id, $this->model->get_language( $_GET['inline_lang_choice'] ) ); | |
| 289 | 278 | } |
| 290 | 279 | } |
| 291 | 280 | |
| 292 | 281 | // Quick edit |
| @@ -296,9 +285,9 @@ | ||
| 296 | 285 | '_inline_edit' |
| 297 | 286 | ); |
| 298 | 287 | |
| 299 | 288 | $old_lang = $this->model->term->get_language( $term_id ); // Stores the old language |
| 300 | - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // New language | |
| 289 | + $lang = $this->model->get_language( $_POST['inline_lang_choice'] ); // New language | |
| 301 | 290 | $translations = $this->model->term->get_translations( $term_id ); |
| 302 | 291 | |
| 303 | 292 | // Checks if the new language already exists in the translation group |
| 304 | 293 | if ( $old_lang && $old_lang->slug != $lang->slug ) { |
| @@ -317,10 +306,14 @@ | ||
| 317 | 306 | |
| 318 | 307 | // Edit post |
| 319 | 308 | elseif ( isset( $_POST['post_lang_choice'] ) ) { // FIXME should be useless now |
| 320 | 309 | check_admin_referer( 'pll_language', '_pll_nonce' ); |
| 321 | - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) ); | |
| 310 | + $this->model->term->set_language( $term_id, $this->model->get_language( $_POST['post_lang_choice'] ) ); | |
| 322 | 311 | } |
| 312 | + | |
| 313 | + else { | |
| 314 | + $this->set_default_language( $term_id, $taxonomy ); | |
| 315 | + } | |
| 323 | 316 | } |
| 324 | 317 | |
| 325 | 318 | /** |
| 326 | 319 | * Save translations from our form |
| @@ -333,16 +326,12 @@ | ||
| 333 | 326 | protected function save_translations( $term_id ) { |
| 334 | 327 | // Security check as 'wp_update_term' can be called from outside WP admin |
| 335 | 328 | check_admin_referer( 'pll_language', '_pll_nonce' ); |
| 336 | 329 | |
| 337 | - $translations = array(); | |
| 338 | - | |
| 339 | 330 | // Save translations after checking the translated term is in the right language ( as well as cast id to int ) |
| 340 | - if ( isset( $_POST['term_tr_lang'] ) ) { | |
| 341 | - foreach ( array_map( 'absint', $_POST['term_tr_lang'] ) as $lang => $tr_id ) { | |
| 342 | - $tr_lang = $this->model->term->get_language( $tr_id ); | |
| 343 | - $translations[ $lang ] = $tr_lang && $tr_lang->slug == $lang ? $tr_id : 0; | |
| 344 | - } | |
| 331 | + foreach ( $_POST['term_tr_lang'] as $lang => $tr_id ) { | |
| 332 | + $tr_lang = $this->model->term->get_language( (int) $tr_id ); | |
| 333 | + $translations[ $lang ] = $tr_lang && $tr_lang->slug == $lang ? (int) $tr_id : 0; | |
| 345 | 334 | } |
| 346 | 335 | |
| 347 | 336 | $this->model->term->save_translations( $term_id, $translations ); |
| 348 | 337 | |
| @@ -368,15 +357,31 @@ | ||
| 368 | 357 | // Capability check |
| 369 | 358 | // As 'wp_update_term' can be called from outside WP admin |
| 370 | 359 | // 2nd test for creating tags when creating / editing a post |
| 371 | 360 | $tax = get_taxonomy( $taxonomy ); |
| 372 | - if ( current_user_can( $tax->cap->edit_terms ) || ( isset( $_POST['tax_input'][ $taxonomy ] ) && current_user_can( $tax->cap->assign_terms ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 361 | + if ( current_user_can( $tax->cap->edit_terms ) || ( isset( $_POST['tax_input'][ $taxonomy ] ) && current_user_can( $tax->cap->assign_terms ) ) ) { | |
| 373 | 362 | $this->save_language( $term_id, $taxonomy ); |
| 374 | 363 | |
| 375 | - if ( isset( $_POST['term_tr_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 376 | - $this->save_translations( $term_id ); | |
| 364 | + if ( isset( $_POST['term_tr_lang'] ) ) { | |
| 365 | + $translations = $this->save_translations( $term_id ); | |
| 377 | 366 | } |
| 367 | + | |
| 368 | + /** | |
| 369 | + * Fires after the term language and translations are saved | |
| 370 | + * | |
| 371 | + * @since 1.2 | |
| 372 | + * | |
| 373 | + * @param int $term_id term id | |
| 374 | + * @param string $taxonomy taxonomy name | |
| 375 | + * @param array $translations the list of translations term ids | |
| 376 | + */ | |
| 377 | + do_action( 'pll_save_term', $term_id, $taxonomy, empty( $translations ) ? $this->model->term->get_translations( $term_id ) : $translations ); | |
| 378 | 378 | } |
| 379 | + | |
| 380 | + // Attempts to set a default language even if no capability | |
| 381 | + else { | |
| 382 | + $this->set_default_language( $term_id, $taxonomy ); | |
| 383 | + } | |
| 379 | 384 | } |
| 380 | 385 | |
| 381 | 386 | /** |
| 382 | 387 | * Stores the term name for use in pre_term_slug |
| @@ -403,29 +408,25 @@ | ||
| 403 | 408 | $name = sanitize_title( $this->pre_term_name ); |
| 404 | 409 | |
| 405 | 410 | // If the term already exists in another language |
| 406 | 411 | if ( ! $slug && $this->model->is_translated_taxonomy( $taxonomy ) && term_exists( $name, $taxonomy ) ) { |
| 407 | - if ( isset( $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 408 | - $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 412 | + if ( isset( $_POST['term_lang_choice'] ) ) { | |
| 413 | + $slug = $name . '-' . $this->model->get_language( $_POST['term_lang_choice'] )->slug; | |
| 409 | 414 | } |
| 410 | 415 | |
| 411 | - elseif ( isset( $_POST['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 412 | - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 416 | + elseif ( isset( $_POST['inline_lang_choice'] ) ) { | |
| 417 | + $slug = $name . '-' . $this->model->get_language( $_POST['inline_lang_choice'] )->slug; | |
| 413 | 418 | } |
| 414 | 419 | |
| 415 | 420 | // *Post* bulk edit, in case a new term is created |
| 416 | - elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 421 | + elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { | |
| 417 | 422 | // Bulk edit does not modify the language |
| 418 | - if ( -1 == $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 419 | - $lang = $this->model->post->get_language( $this->post_id ); | |
| 423 | + if ( -1 == $_GET['inline_lang_choice'] ) { | |
| 424 | + $slug = $name . '-' . $this->model->post->get_language( $this->post_id )->slug; | |
| 420 | 425 | } else { |
| 421 | - $lang = $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 426 | + $slug = $name . '-' . $this->model->get_language( $_GET['inline_lang_choice'] )->slug; | |
| 422 | 427 | } |
| 423 | 428 | } |
| 424 | - | |
| 425 | - if ( ! empty( $lang ) && ! $this->model->term_exists_by_slug( $name, $lang, $taxonomy ) ) { | |
| 426 | - $slug = $name . '-' . $lang->slug; | |
| 427 | - } | |
| 428 | 429 | } |
| 429 | 430 | |
| 430 | 431 | return $slug; |
| 431 | 432 | } |
| @@ -430,8 +431,21 @@ | ||
| 430 | 431 | return $slug; |
| 431 | 432 | } |
| 432 | 433 | |
| 433 | 434 | /** |
| 435 | + * Called when a category or post tag is deleted | |
| 436 | + * Deletes language and translations | |
| 437 | + * | |
| 438 | + * @since 0.1 | |
| 439 | + * | |
| 440 | + * @param int $term_id | |
| 441 | + */ | |
| 442 | + public function delete_term( $term_id ) { | |
| 443 | + $this->model->term->delete_translation( $term_id ); | |
| 444 | + $this->model->term->delete_language( $term_id ); | |
| 445 | + } | |
| 446 | + | |
| 447 | + /** | |
| 434 | 448 | * Ajax response for edit term form |
| 435 | 449 | * |
| 436 | 450 | * @since 0.2 |
| 437 | 451 | */ |
| @@ -437,24 +451,20 @@ | ||
| 437 | 451 | */ |
| 438 | 452 | public function term_lang_choice() { |
| 439 | 453 | check_ajax_referer( 'pll_language', '_pll_nonce' ); |
| 440 | 454 | |
| 441 | - if ( ! isset( $_POST['taxonomy'], $_POST['post_type'], $_POST['lang'] ) ) { | |
| 442 | - wp_die( 0 ); | |
| 443 | - } | |
| 455 | + $lang = $this->model->get_language( $_POST['lang'] ); | |
| 456 | + $term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : null; | |
| 457 | + $taxonomy = $_POST['taxonomy']; | |
| 458 | + $post_type = $_POST['post_type']; | |
| 444 | 459 | |
| 445 | - $lang = $this->model->get_language( sanitize_key( $_POST['lang'] ) ); | |
| 446 | - $term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : null; // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable | |
| 447 | - $taxonomy = sanitize_key( $_POST['taxonomy'] ); // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable | |
| 448 | - $post_type = sanitize_key( $_POST['post_type'] ); | |
| 449 | - | |
| 450 | 460 | if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) { |
| 451 | - wp_die( 0 ); | |
| 461 | + die( 0 ); | |
| 452 | 462 | } |
| 453 | 463 | |
| 454 | 464 | ob_start(); |
| 455 | 465 | if ( $lang ) { |
| 456 | - include __DIR__ . '/view-translations-term.php'; | |
| 466 | + include PLL_ADMIN_INC . '/view-translations-term.php'; | |
| 457 | 467 | } |
| 458 | 468 | $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) ); |
| 459 | 469 | ob_end_clean(); |
| 460 | 470 | |
| @@ -461,16 +471,16 @@ | ||
| 461 | 471 | // Parent dropdown list ( only for hierarchical taxonomies ) |
| 462 | 472 | // $args copied from edit_tags.php except echo |
| 463 | 473 | if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 464 | 474 | $args = array( |
| 465 | - 'hide_empty' => 0, | |
| 466 | - 'hide_if_empty' => false, | |
| 467 | - 'taxonomy' => $taxonomy, | |
| 468 | - 'name' => 'parent', | |
| 469 | - 'orderby' => 'name', | |
| 470 | - 'hierarchical' => true, | |
| 471 | - 'show_option_none' => __( 'None', 'polylang' ), | |
| 472 | - 'echo' => 0, | |
| 475 | + 'hide_empty' => 0, | |
| 476 | + 'hide_if_empty' => false, | |
| 477 | + 'taxonomy' => $taxonomy, | |
| 478 | + 'name' => 'parent', | |
| 479 | + 'orderby' => 'name', | |
| 480 | + 'hierarchical' => true, | |
| 481 | + 'show_option_none' => __( 'None' ), | |
| 482 | + 'echo' => 0, | |
| 473 | 483 | ); |
| 474 | 484 | $x->Add( array( 'what' => 'parent', 'data' => wp_dropdown_categories( $args ) ) ); |
| 475 | 485 | } |
| 476 | 486 | |
| @@ -504,66 +514,120 @@ | ||
| 504 | 514 | */ |
| 505 | 515 | public function ajax_terms_not_translated() { |
| 506 | 516 | check_ajax_referer( 'pll_language', '_pll_nonce' ); |
| 507 | 517 | |
| 508 | - if ( ! isset( $_GET['term'], $_GET['post_type'], $_GET['taxonomy'], $_GET['term_language'], $_GET['translation_language'] ) ) { | |
| 509 | - wp_die( 0 ); | |
| 510 | - } | |
| 518 | + $s = wp_unslash( $_GET['term'] ); | |
| 519 | + $post_type = $_GET['post_type']; | |
| 520 | + $taxonomy = $_GET['taxonomy']; | |
| 511 | 521 | |
| 512 | - $s = wp_unslash( $_GET['term'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput | |
| 513 | - $post_type = sanitize_key( $_GET['post_type'] ); | |
| 514 | - $taxonomy = sanitize_key( $_GET['taxonomy'] ); | |
| 515 | - | |
| 516 | 522 | if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) { |
| 517 | - wp_die( 0 ); | |
| 523 | + die( 0 ); | |
| 518 | 524 | } |
| 519 | 525 | |
| 520 | - $term_language = $this->model->get_language( sanitize_key( $_GET['term_language'] ) ); | |
| 521 | - $translation_language = $this->model->get_language( sanitize_key( $_GET['translation_language'] ) ); | |
| 526 | + $term_language = $this->model->get_language( $_GET['term_language'] ); | |
| 527 | + $translation_language = $this->model->get_language( $_GET['translation_language'] ); | |
| 522 | 528 | |
| 523 | - $terms = array(); | |
| 524 | 529 | $return = array(); |
| 525 | 530 | |
| 526 | - // Add current translation in list. | |
| 527 | - // Not in add term as term_id is not set. | |
| 528 | - if ( isset( $_GET['term_id'] ) && 'undefined' !== $_GET['term_id'] && $term_id = $this->model->term->get_translation( (int) $_GET['term_id'], $translation_language ) ) { | |
| 529 | - $terms = array( get_term( $term_id, $taxonomy ) ); | |
| 530 | - } | |
| 531 | - | |
| 532 | - // It is more efficient to use one common query for all languages as soon as there are more than 2. | |
| 531 | + // It is more efficient to use one common query for all languages as soon as there are more than 2 | |
| 533 | 532 | foreach ( get_terms( $taxonomy, 'hide_empty=0&lang=0&name__like=' . $s ) as $term ) { |
| 534 | 533 | $lang = $this->model->term->get_language( $term->term_id ); |
| 535 | 534 | |
| 536 | 535 | if ( $lang && $lang->slug == $translation_language->slug && ! $this->model->term->get_translation( $term->term_id, $term_language ) ) { |
| 537 | - $terms[] = $term; | |
| 536 | + $return[] = array( | |
| 537 | + 'id' => $term->term_id, | |
| 538 | + 'value' => $term->name, | |
| 539 | + 'link' => $this->links->edit_term_translation_link( $term->term_id, $taxonomy, $post_type ), | |
| 540 | + ); | |
| 538 | 541 | } |
| 539 | 542 | } |
| 540 | 543 | |
| 541 | - // Format the ajax response. | |
| 542 | - foreach ( $terms as $term ) { | |
| 543 | - $return[] = array( | |
| 544 | - 'id' => $term->term_id, | |
| 545 | - 'value' => rtrim( // Trim the seperator added at the end by WP. | |
| 546 | - get_term_parents_list( | |
| 547 | - $term->term_id, | |
| 548 | - $term->taxonomy, | |
| 549 | - array( | |
| 550 | - 'separator' => ' > ', | |
| 551 | - 'link' => false, | |
| 552 | - ) | |
| 553 | - ), | |
| 554 | - ' >' | |
| 555 | - ), | |
| 556 | - 'link' => $this->links->edit_term_translation_link( $term->term_id, $term->taxonomy, $post_type ), | |
| 557 | - ); | |
| 544 | + // Add current translation in list | |
| 545 | + // Not in add term for as term_id is not set | |
| 546 | + if ( 'undefined' !== $_GET['term_id'] && $term_id = $this->model->term->get_translation( (int) $_GET['term_id'], $translation_language ) ) { | |
| 547 | + $term = get_term( $term_id, $taxonomy ); | |
| 548 | + array_unshift( $return, array( | |
| 549 | + 'id' => $term_id, | |
| 550 | + 'value' => $term->name, | |
| 551 | + 'link' => $this->links->edit_term_translation_link( $term->term_id, $taxonomy, $post_type ), | |
| 552 | + ) ); | |
| 558 | 553 | } |
| 559 | 554 | |
| 560 | - wp_die( wp_json_encode( $return ) ); | |
| 555 | + wp_die( json_encode( $return ) ); | |
| 561 | 556 | } |
| 562 | 557 | |
| 563 | 558 | /** |
| 564 | - * Filters the default category in note below the category list table and in settings->writing dropdown | |
| 559 | + * Get the language(s) to filter get_terms | |
| 565 | 560 | * |
| 561 | + * @since 1.7.6 | |
| 562 | + * | |
| 563 | + * @param array $taxonomies queried taxonomies | |
| 564 | + * @param array $args get_terms arguments | |
| 565 | + * @return object|string|bool the language(s) to use in the filter, false otherwise | |
| 566 | + */ | |
| 567 | + protected function get_queried_language( $taxonomies, $args ) { | |
| 568 | + // Does nothing except on taxonomies which are filterable | |
| 569 | + // Since WP 4.7, make sure not to filter wp_get_object_terms() | |
| 570 | + if ( ! $this->model->is_translated_taxonomy( $taxonomies ) || ! empty( $args['object_ids'] ) ) { | |
| 571 | + return false; | |
| 572 | + } | |
| 573 | + | |
| 574 | + // If get_terms is queried with a 'lang' parameter | |
| 575 | + if ( isset( $args['lang'] ) ) { | |
| 576 | + return $args['lang']; | |
| 577 | + } | |
| 578 | + | |
| 579 | + // On tags page, everything should be filtered according to the admin language filter except the parent dropdown | |
| 580 | + if ( 'edit-tags.php' === $GLOBALS['pagenow'] && empty( $args['class'] ) ) { | |
| 581 | + return $this->filter_lang; | |
| 582 | + } | |
| 583 | + | |
| 584 | + return $this->curlang; | |
| 585 | + } | |
| 586 | + | |
| 587 | + /** | |
| 588 | + * Adds language dependent cache domain when querying terms | |
| 589 | + * Useful as the 'lang' parameter is not included in cache key by WordPress | |
| 590 | + * | |
| 591 | + * @since 1.3 | |
| 592 | + * | |
| 593 | + * @param array $args | |
| 594 | + * @param array $taxonomies | |
| 595 | + * @return array modified arguments | |
| 596 | + */ | |
| 597 | + public function get_terms_args( $args, $taxonomies ) { | |
| 598 | + // don't break _get_term_hierarchy() | |
| 599 | + if ( 'all' === $args['get'] && 'id' === $args['orderby'] && 'id=>parent' === $args['fields'] ) { | |
| 600 | + $args['lang'] = ''; | |
| 601 | + } | |
| 602 | + | |
| 603 | + if ( $lang = $this->get_queried_language( $taxonomies, $args ) ) { | |
| 604 | + $lang = is_string( $lang ) && strpos( $lang, ',' ) ? explode( ',', $lang ) : $lang; | |
| 605 | + $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug ); | |
| 606 | + $args['cache_domain'] = empty( $args['cache_domain'] ) ? 'pll' . $key : $args['cache_domain'] . $key; | |
| 607 | + } | |
| 608 | + return $args; | |
| 609 | + } | |
| 610 | + | |
| 611 | + /** | |
| 612 | + * Filters categories and post tags by language(s) when needed on admin side | |
| 613 | + * | |
| 614 | + * @since 0.5 | |
| 615 | + * | |
| 616 | + * @param array $clauses list of sql clauses | |
| 617 | + * @param array $taxonomies list of taxonomies | |
| 618 | + * @param array $args get_terms arguments | |
| 619 | + * @return array modified sql clauses | |
| 620 | + */ | |
| 621 | + public function terms_clauses( $clauses, $taxonomies, $args ) { | |
| 622 | + $lang = $this->get_queried_language( $taxonomies, $args ); | |
| 623 | + return ! empty( $lang ) ? $this->model->terms_clauses( $clauses, $lang ) : $clauses; // adds our clauses to filter by current language | |
| 624 | + } | |
| 625 | + | |
| 626 | + /** | |
| 627 | + * Hack to avoid displaying delete link for the default category in all languages | |
| 628 | + * Also returns the default category in the right language when called from wp_delete_term | |
| 629 | + * | |
| 566 | 630 | * @since 1.2 |
| 567 | 631 | * |
| 568 | 632 | * @param int $value |
| 569 | 633 | * @return int |
| @@ -568,11 +632,30 @@ | ||
| 568 | 632 | * @param int $value |
| 569 | 633 | * @return int |
| 570 | 634 | */ |
| 571 | 635 | public function option_default_category( $value ) { |
| 636 | + // Filters the default category in note below the category list table and in settings->writing dropdown | |
| 572 | 637 | if ( isset( $this->pref_lang ) && $tr = $this->model->term->get( $value, $this->pref_lang ) ) { |
| 573 | 638 | $value = $tr; |
| 574 | 639 | } |
| 640 | + | |
| 641 | + // FIXME backward compatibility with WP < 4.7 | |
| 642 | + if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) { | |
| 643 | + $traces = debug_backtrace(); | |
| 644 | + $n = version_compare( PHP_VERSION, '7', '>=' ) ? 3 : 4; // PHP 7 does not include call_user_func_array | |
| 645 | + | |
| 646 | + if ( isset( $traces[ $n ] ) ) { | |
| 647 | + // FIXME 'column_name' for backward compatibility with WP < 4.3 | |
| 648 | + if ( in_array( $traces[ $n ]['function'], array( 'column_cb', 'column_name', 'handle_row_actions' ) ) && in_array( $traces[ $n ]['args'][0]->term_id, $this->model->term->get_translations( $value ) ) ) { | |
| 649 | + return $traces[ $n ]['args'][0]->term_id; | |
| 650 | + } | |
| 651 | + | |
| 652 | + if ( 'wp_delete_term' == $traces[ $n ]['function'] ) { | |
| 653 | + return $this->model->term->get( $value, $this->model->term->get_language( $traces[ $n ]['args'][0] ) ); | |
| 654 | + } | |
| 655 | + } | |
| 656 | + } | |
| 657 | + | |
| 575 | 658 | return $value; |
| 576 | 659 | } |
| 577 | 660 | |
| 578 | 661 | /** |
| @@ -623,9 +706,8 @@ | ||
| 623 | 706 | } |
| 624 | 707 | |
| 625 | 708 | $avoid_recursion = true; |
| 626 | 709 | $lang = $this->model->term->get_language( $term_id ); |
| 627 | - $translations = array(); | |
| 628 | 710 | |
| 629 | 711 | foreach ( $this->model->term->get_translations( $term_id ) as $key => $tr_id ) { |
| 630 | 712 | if ( $lang->slug == $key ) { |
| 631 | 713 | $translations[ $key ] = $new_term_id; |
| @@ -634,9 +716,9 @@ | ||
| 634 | 716 | $tr_term = get_term( $tr_id, $taxonomy ); |
| 635 | 717 | $translations[ $key ] = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id ); |
| 636 | 718 | |
| 637 | 719 | // Hack translation ids sent by the form to avoid overwrite in PLL_Admin_Filters_Term::save_translations |
| 638 | - if ( isset( $_POST['term_tr_lang'][ $key ] ) && $_POST['term_tr_lang'][ $key ] == $tr_id ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 720 | + if ( isset( $_POST['term_tr_lang'][ $key ] ) && $_POST['term_tr_lang'][ $key ] == $tr_id ) { | |
| 639 | 721 | $_POST['term_tr_lang'][ $key ] = $translations[ $key ]; |
| 640 | 722 | } |
| 641 | 723 | } |
| 642 | 724 | $this->model->term->set_language( $translations[ $key ], $key ); |