PluginProbe
Polylang / 3.2.2
Polylang v3.2.2
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 3.2.2, at include/crud-terms.php

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