PluginProbe
Polylang / 2.7.4
Polylang v2.7.4
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.7.4, at include/crud-terms.php

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