PluginProbe
Polylang / 2.8.3
Polylang v2.8.3
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / crud-terms.php

crud-terms.php in Polylang 2.8.3, at include/crud-terms.php

210 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Adds actions and filters related to languages when creating, reading, updating or deleting posts
8 * Acts both on frontend and backend
9 *
10 * @since 2.4
11 */
12 class PLL_CRUD_Terms {
13 public $model, $curlang, $filter_lang, $pref_lang;
14 private $tax_query_lang;
15
16 /**
17 * Constructor
18 *
19 * @since 2.4
20 *
21 * @param object $polylang
22 */
23 public function __construct( &$polylang ) {
24 $this->model = &$polylang->model;
25 $this->curlang = &$polylang->curlang;
26 $this->filter_lang = &$polylang->filter_lang;
27 $this->pref_lang = &$polylang->pref_lang;
28
29 // Saving terms
30 add_action( 'create_term', array( $this, 'save_term' ), 999, 3 );
31 add_action( 'edit_term', array( $this, 'save_term' ), 999, 3 ); // After PLL_Admin_Filters_Term
32
33 // Adds cache domain when querying terms
34 add_filter( 'get_terms_args', array( $this, 'get_terms_args' ), 10, 2 );
35
36 // Filters terms by language
37 add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 3 );
38 add_action( 'pre_get_posts', array( $this, 'set_tax_query_lang' ), 999 );
39 add_action( 'posts_selection', array( $this, 'unset_tax_query_lang' ), 0 );
40
41 // Deleting terms
42 add_action( 'pre_delete_term', array( $this, 'delete_term' ) );
43 }
44
45 /**
46 * Allows to set a language by default for terms if it has no language yet
47 *
48 * @since 1.5.4
49 *
50 * @param int $term_id
51 * @param string $taxonomy
52 */
53 protected function set_default_language( $term_id, $taxonomy ) {
54 if ( ! $this->model->term->get_language( $term_id ) ) {
55 if ( ! isset( $this->pref_lang ) && ! empty( $_REQUEST['lang'] ) && $lang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
56 // Testing $this->pref_lang makes this test pass only on frontend.
57 $this->model->term->set_language( $term_id, $lang );
58 } elseif ( ( $term = get_term( $term_id, $taxonomy ) ) && ! empty( $term->parent ) && $parent_lang = $this->model->term->get_language( $term->parent ) ) {
59 // Sets language from term parent if exists thanks to Scott Kingsley Clark
60 $this->model->term->set_language( $term_id, $parent_lang );
61 } elseif ( isset( $this->pref_lang ) ) {
62 // Always defined on admin, never defined on frontend
63 $this->model->term->set_language( $term_id, $this->pref_lang );
64 } else {
65 // Only on frontend due to the previous test always true on admin
66 $this->model->term->set_language( $term_id, $this->curlang );
67 }
68 }
69 }
70
71 /**
72 * Called when a category or post tag is created or edited
73 * Does nothing except on taxonomies which are filterable
74 *
75 * @since 0.1
76 *
77 * @param int $term_id
78 * @param int $tt_id Term taxonomy id
79 * @param string $taxonomy
80 */
81 public function save_term( $term_id, $tt_id, $taxonomy ) {
82 if ( $this->model->is_translated_taxonomy( $taxonomy ) ) {
83
84 $lang = $this->model->term->get_language( $term_id );
85
86 if ( empty( $lang ) ) {
87 $this->set_default_language( $term_id, $taxonomy );
88 }
89
90 /**
91 * Fires after the term language and translations are saved
92 *
93 * @since 1.2
94 *
95 * @param int $term_id term id
96 * @param string $taxonomy taxonomy name
97 * @param array $translations the list of translations term ids
98 */
99 do_action( 'pll_save_term', $term_id, $taxonomy, $this->model->term->get_translations( $term_id ) );
100 }
101 }
102
103 /**
104 * Get the language(s) to filter get_terms
105 *
106 * @since 1.7.6
107 *
108 * @param array $taxonomies queried taxonomies
109 * @param array $args get_terms arguments
110 * @return object|string|bool the language(s) to use in the filter, false otherwise
111 */
112 protected function get_queried_language( $taxonomies, $args ) {
113 // Does nothing except on taxonomies which are filterable
114 // Since WP 4.7, make sure not to filter wp_get_object_terms()
115 if ( ! $this->model->is_translated_taxonomy( $taxonomies ) || ! empty( $args['object_ids'] ) ) {
116 return false;
117 }
118
119 // If get_terms is queried with a 'lang' parameter
120 if ( isset( $args['lang'] ) ) {
121 return $args['lang'];
122 }
123
124 // On tags page, everything should be filtered according to the admin language filter except the parent dropdown
125 if ( 'edit-tags.php' === $GLOBALS['pagenow'] && empty( $args['class'] ) ) {
126 return $this->filter_lang;
127 }
128
129 return $this->curlang;
130 }
131
132 /**
133 * Adds language dependent cache domain when querying terms
134 * Useful as the 'lang' parameter is not included in cache key by WordPress
135 *
136 * @since 1.3
137 *
138 * @param array $args
139 * @param array $taxonomies
140 * @return array modified arguments
141 */
142 public function get_terms_args( $args, $taxonomies ) {
143 // Don't break _get_term_hierarchy()
144 if ( 'all' === $args['get'] && 'id' === $args['orderby'] && 'id=>parent' === $args['fields'] ) {
145 $args['lang'] = '';
146 }
147
148 if ( isset( $this->tax_query_lang ) ) {
149 $args['lang'] = empty( $this->tax_query_lang ) && ! empty( $this->curlang ) && ! empty( $args['slug'] ) ? $this->curlang->slug : $this->tax_query_lang;
150 }
151
152 if ( $lang = $this->get_queried_language( $taxonomies, $args ) ) {
153 $lang = is_string( $lang ) && strpos( $lang, ',' ) ? explode( ',', $lang ) : $lang;
154 $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug );
155 $args['cache_domain'] = empty( $args['cache_domain'] ) ? 'pll' . $key : $args['cache_domain'] . $key;
156 }
157 return $args;
158 }
159
160 /**
161 * Filters categories and post tags by language(s) when needed on admin side
162 *
163 * @since 0.2
164 *
165 * @param array $clauses list of sql clauses
166 * @param array $taxonomies list of taxonomies
167 * @param array $args get_terms arguments
168 * @return array modified sql clauses
169 */
170 public function terms_clauses( $clauses, $taxonomies, $args ) {
171 $lang = $this->get_queried_language( $taxonomies, $args );
172 return $this->model->terms_clauses( $clauses, $lang );
173 }
174
175 /**
176 * Sets the WP_Term_Query language when doing a WP_Query
177 * Needed since WP 4.9
178 *
179 * @since 2.3.2
180 *
181 * @param object $query WP_Query object
182 */
183 public function set_tax_query_lang( $query ) {
184 $this->tax_query_lang = isset( $query->query_vars['lang'] ) ? $query->query_vars['lang'] : '';
185 }
186
187 /**
188 * Removes the WP_Term_Query language filter for WP_Query
189 * Needed since WP 4.9
190 *
191 * @since 2.3.2
192 */
193 public function unset_tax_query_lang() {
194 unset( $this->tax_query_lang );
195 }
196
197 /**
198 * Called when a category or post tag is deleted
199 * Deletes language and translations
200 *
201 * @since 0.1
202 *
203 * @param int $term_id
204 */
205 public function delete_term( $term_id ) {
206 $this->model->term->delete_translation( $term_id );
207 $this->model->term->delete_language( $term_id );
208 }
209 }
210