PluginProbe
Polylang / 3.3
Polylang v3.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
← All changes | include/translated-post.php +77 -30 2.83.3 View file →
@@ -10,13 +10,13 @@
10 10 */
11 11 class PLL_Translated_Post extends PLL_Translated_Object {
12 12
13 13 /**
14 - * Constructor
14 + * Constructor.
15 15 *
16 16 * @since 1.8
17 17 *
18 - * @param object $model
18 + * @param PLL_Model $model PLL_Model instance.
19 19 */
20 20 public function __construct( &$model ) {
21 21 // init properties
22 22 $this->object_type = null; // For taxonomies
@@ -37,22 +37,31 @@
37 37 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
38 38 }
39 39
40 40 /**
41 - * Store the post language in the database
41 + * Store the post language in the database.
42 42 *
43 43 * @since 0.6
44 44 *
45 - * @param int $post_id post id
46 - * @param int|string|object $lang language ( term_id or slug or object )
45 + * @param int $post_id Post id.
46 + * @param int|string|PLL_Language $lang Language (term_id or slug or object).
47 + * @return void
47 48 */
48 49 public function set_language( $post_id, $lang ) {
50 + $post_id = $this->sanitize_int_id( $post_id );
51 +
52 + if ( empty( $post_id ) ) {
53 + return;
54 + }
55 +
49 56 $old_lang = $this->get_language( $post_id );
50 57 $old_lang = $old_lang ? $old_lang->slug : '';
51 - $lang = $lang ? $this->model->get_language( $lang )->slug : '';
52 58
59 + $lang = $this->model->get_language( $lang );
60 + $lang = $lang ? $lang->slug : '';
61 +
53 62 if ( $old_lang !== $lang ) {
54 - wp_set_post_terms( (int) $post_id, $lang, 'language' );
63 + wp_set_post_terms( $post_id, $lang, $this->tax_language );
55 64 }
56 65 }
57 66
58 67 /**
@@ -60,25 +69,38 @@
60 69 *
61 70 * @since 0.1
62 71 *
63 72 * @param int $post_id post id
64 - * @return bool|object PLL_Language object, false if no language is associated to that post
73 + * @return PLL_Language|false PLL_Language object, false if no language is associated to that post
65 74 */
66 75 public function get_language( $post_id ) {
67 - $lang = $this->get_object_term( $post_id, 'language' );
68 - return ( $lang ) ? $this->model->get_language( $lang ) : false;
76 + $post_id = $this->sanitize_int_id( $post_id );
77 +
78 + if ( empty( $post_id ) ) {
79 + return false;
80 + }
81 +
82 + $lang = $this->get_object_term( $post_id, $this->tax_language );
83 + return ! empty( $lang ) ? $this->model->get_language( $lang ) : false;
69 84 }
70 85
71 86 /**
72 - * Deletes a translation
87 + * Deletes a translation.
73 88 *
74 89 * @since 0.5
75 90 *
76 - * @param int $id post id
91 + * @param int $id Post id.
92 + * @return void
77 93 */
78 94 public function delete_translation( $id ) {
95 + $id = $this->sanitize_int_id( $id );
96 +
97 + if ( empty( $id ) ) {
98 + return;
99 + }
100 +
79 101 parent::delete_translation( $id );
80 - wp_set_object_terms( $id, null, $this->tax_translations );
102 + wp_set_object_terms( $id, array(), $this->tax_translations );
81 103 }
82 104
83 105 /**
84 106 * A join clause to add to sql queries when filtering by language is needed directly in query
@@ -99,12 +121,14 @@
99 121 /**
100 122 * Register the language taxonomy
101 123 *
102 124 * @since 1.2
125 + *
126 + * @return void
103 127 */
104 128 public function register_taxonomy() {
105 129 register_taxonomy(
106 - 'language',
130 + $this->tax_language,
107 131 $this->model->get_translated_post_types(),
108 132 array(
109 133 'labels' => array(
110 134 'name' => __( 'Languages', 'polylang' ),
@@ -127,24 +151,26 @@
127 151 *
128 152 * @since 1.2
129 153 *
130 154 * @param string $post_type post type name
155 + * @return void
131 156 */
132 157 public function registered_post_type( $post_type ) {
133 158 if ( $this->model->is_translated_post_type( $post_type ) ) {
134 - register_taxonomy_for_object_type( 'language', $post_type );
135 - register_taxonomy_for_object_type( 'post_translations', $post_type );
159 + register_taxonomy_for_object_type( $this->tax_language, $post_type );
160 + register_taxonomy_for_object_type( $this->tax_translations, $post_type );
136 161 }
137 162 }
138 163
139 164 /**
140 - * Forces calling 'update_object_term_cache' when querying posts or pages
141 - * this is especially useful for nav menus with a lot of pages
142 - * without doing this, we would have one query per page in the menu to get the page language for the permalink
165 + * Forces calling 'update_object_term_cache' when querying posts or pages.
166 + * This is especially useful for nav menus with a lot of pages as, without doing this,
167 + * we would have one query per page in the menu to get the page language for the permalink.
143 168 *
144 169 * @since 1.8
145 170 *
146 - * @param object $query reference to the query object
171 + * @param WP_Query $query Reference to the query object.
172 + * @return void
147 173 */
148 174 public function pre_get_posts( $query ) {
149 175 if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) {
150 176 $query->query_vars['update_post_term_cache'] = true;
@@ -160,8 +186,14 @@
160 186 * @param string $context Optional, 'edit' or 'view', defaults to 'view'.
161 187 * @return bool
162 188 */
163 189 public function current_user_can_read( $post_id, $context = 'view' ) {
190 + $post_id = $this->sanitize_int_id( $post_id );
191 +
192 + if ( empty( $post_id ) ) {
193 + return false;
194 + }
195 +
164 196 $post = get_post( $post_id );
165 197
166 198 if ( empty( $post ) ) {
167 199 return false;
@@ -168,8 +200,12 @@
168 200 }
169 201
170 202 if ( 'inherit' === $post->post_status && $post->post_parent ) {
171 203 $post = get_post( $post->post_parent );
204 +
205 + if ( empty( $post ) ) {
206 + return false;
207 + }
172 208 }
173 209
174 210 if ( 'inherit' === $post->post_status || in_array( $post->post_status, get_post_stati( array( 'public' => true ) ) ) ) {
175 211 return true;
@@ -176,11 +212,21 @@
176 212 }
177 213
178 214 // Follow WP practices, which shows links to private posts ( when readable ), but not for draft posts ( ex: get_adjacent_post_link() )
179 215 if ( in_array( $post->post_status, get_post_stati( array( 'private' => true ) ) ) ) {
216 + if ( ! is_user_logged_in() ) {
217 + return false;
218 + }
219 +
220 + $user = wp_get_current_user();
221 +
222 + if ( (int) $user->ID === (int) $post->post_author ) {
223 + return true;
224 + }
225 +
180 226 $post_type_object = get_post_type_object( $post->post_type );
181 - $user = wp_get_current_user();
182 - return is_user_logged_in() && ( current_user_can( $post_type_object->cap->read_private_posts ) || $user->ID == $post->post_author ); // Comparison must not be strict!
227 +
228 + return ! empty( $post_type_object ) && current_user_can( $post_type_object->cap->read_private_posts );
183 229 }
184 230
185 231 // In edit context, show draft and future posts.
186 232 if ( 'edit' === $context ) {
@@ -201,17 +247,17 @@
201 247 }
202 248
203 249 /**
204 250 * Returns a list of posts in a language ( $lang )
205 - * not translated in another language ( $untranslated_in )
251 + * not translated in another language ( $untranslated_in ).
206 252 *
207 253 * @since 2.6
208 254 *
209 - * @param string $type Post type
210 - * @param string $untranslated_in The posts must not be translated in this language
211 - * @param string $lang Language of the search posts
212 - * @param string $search Limit results to posts matching this string
213 - * @return array Array of posts
255 + * @param string $type Post type.
256 + * @param PLL_Language $untranslated_in The language the posts must not be translated in.
257 + * @param PLL_Language $lang Language of the searched posts.
258 + * @param string $search Limit the results to the posts matching this string.
259 + * @return WP_Post[] Array of posts.
214 260 */
215 261 public function get_untranslated( $type, $untranslated_in, $lang, $search = '' ) {
216 262 $return = array();
217 263
@@ -224,9 +270,9 @@
224 270 'post_status' => 'any',
225 271 'post_type' => $type,
226 272 'tax_query' => array(
227 273 array(
228 - 'taxonomy' => 'language',
274 + 'taxonomy' => $this->tax_language,
229 275 'field' => 'term_taxonomy_id', // WP 3.5+
230 276 'terms' => $lang->term_taxonomy_id,
231 277 ),
232 278 ),
@@ -245,9 +291,9 @@
245 291 $args = apply_filters( 'pll_ajax_posts_not_translated_args', $args );
246 292 $posts = get_posts( $args );
247 293
248 294 foreach ( $posts as $post ) {
249 - if ( ! $this->get_translation( $post->ID, $untranslated_in ) && $this->current_user_can_read( $post->ID, 'edit' ) ) {
295 + if ( $post instanceof WP_Post && ! $this->get_translation( $post->ID, $untranslated_in ) && $this->current_user_can_read( $post->ID, 'edit' ) ) {
250 296 $return[] = $post;
251 297 }
252 298 }
253 299
@@ -252,5 +298,6 @@
252 298 }
253 299
254 300 return $return;
255 301 }
302 +
256 303 }