PLL_Admin_Filters_Term

Manages filters and actions related to terms on admin side.

Defined (1)

The class is defined in the following location(s).

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