PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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 +446 -123 2.7.33.7.7 View file →
@@ -1,166 +1,267 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
6 +use WP_Syntex\Polylang\Options\Options;
7 +
8 +defined( 'ABSPATH' ) || exit;
9 +
3 10 /**
4 - * Setups the posts languages and translations model
11 + * Sets the posts languages and translations model up.
5 12 *
6 13 * @since 1.8
14 + *
15 + * @phpstan-import-type DBInfoWithType from PLL_Translatable_Object_With_Types_Interface
7 16 */
8 -class PLL_Translated_Post extends PLL_Translated_Object {
17 +class PLL_Translated_Post extends PLL_Translated_Object implements PLL_Translatable_Object_With_Types_Interface {
18 + use PLL_Translatable_Object_With_Types_Trait;
9 19
10 20 /**
11 - * Constructor
21 + * Taxonomy name for the languages.
12 22 *
23 + * @var string
24 + *
25 + * @phpstan-var non-empty-string
26 + */
27 + protected $tax_language = 'language';
28 +
29 + /**
30 + * Identifier that must be unique for each type of content.
31 + * Also used when checking capabilities.
32 + *
33 + * @var string
34 + *
35 + * @phpstan-var non-empty-string
36 + */
37 + protected $type = 'post';
38 +
39 + /**
40 + * Identifier for each type of content to used for cache type.
41 + *
42 + * @var string
43 + *
44 + * @phpstan-var non-empty-string
45 + */
46 + protected $cache_type = 'posts';
47 +
48 + /**
49 + * Taxonomy name for the translation groups.
50 + *
51 + * @var string
52 + *
53 + * @phpstan-var non-empty-string
54 + */
55 + protected $tax_translations = 'post_translations';
56 +
57 + /**
58 + * Constructor.
59 + *
13 60 * @since 1.8
14 61 *
15 - * @param object $model
62 + * @param PLL_Model $model Instance of `PLL_Model`.
16 63 */
17 - public function __construct( &$model ) {
18 - // init properties
19 - $this->object_type = null; // For taxonomies
20 - $this->type = 'post'; // For capabilities
21 - $this->tax_language = 'language';
22 - $this->tax_translations = 'post_translations';
23 - $this->tax_tt = 'term_taxonomy_id';
24 -
64 + public function __construct( PLL_Model $model ) {
25 65 parent::__construct( $model );
26 66
27 - // registers completely the language taxonomy
28 - add_action( 'setup_theme', array( $this, 'register_taxonomy' ), 1 );
67 + // Keep hooks in constructor for backward compatibility.
68 + $this->init();
69 + }
29 70
30 - // setups post types to translate
31 - add_action( 'registered_post_type', array( $this, 'registered_post_type' ) );
71 + /**
72 + * Registers the language taxonomy.
73 + *
74 + * @since 1.2
75 + * @since 3.7 Protected and renamed from `register_taxonomy()`.
76 + *
77 + * @return void
78 + */
79 + protected function register_language_taxonomy(): void {
80 + register_taxonomy(
81 + $this->tax_language,
82 + (array) $this->get_translated_object_types(),
83 + array(
84 + 'public' => false,
85 + 'show_ui' => false, // Hide the taxonomy on admin side, needed for WP 4.4.x.
86 + 'show_in_nav_menus' => false, // No metabox for nav menus, needed for WP 4.4.x.
87 + 'publicly_queryable' => true, // Since WP 4.5.
88 + 'query_var' => 'lang', // See `add_language_taxonomy_query_var()`.
89 + 'rewrite' => false, // Rewrite rules are added through filters when needed.
90 + '_pll' => true, // Polylang taxonomy.
91 + )
92 + );
32 93
33 - // forces updating posts cache
34 - add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
94 + add_action( 'setup_theme', array( $this, 'add_language_taxonomy_query_var' ) );
35 95 }
36 96
37 97 /**
38 - * Store the post language in the database
98 + * Adds the language query var once the global `$wp` is available.
39 99 *
40 - * @since 0.6
100 + * @since 3.7
101 + * @see WP_Taxonomy::add_rewrite_rules()
41 102 *
42 - * @param int $post_id post id
43 - * @param int|string|object $lang language ( term_id or slug or object )
103 + * @return void
44 104 */
45 - public function set_language( $post_id, $lang ) {
46 - $old_lang = $this->get_language( $post_id );
47 - $old_lang = $old_lang ? $old_lang->slug : '';
48 - $lang = $lang ? $this->model->get_language( $lang )->slug : '';
49 -
50 - if ( $old_lang !== $lang ) {
51 - wp_set_post_terms( (int) $post_id, $lang, 'language' );
52 - }
105 + public function add_language_taxonomy_query_var(): void {
106 + $GLOBALS['wp']->add_query_var( 'lang' );
53 107 }
54 108
55 109 /**
56 - * Returns the language of a post
110 + * Adds hooks.
57 111 *
58 - * @since 0.1
112 + * @since 3.4
59 113 *
60 - * @param int $post_id post id
61 - * @return bool|object PLL_Language object, false if no language is associated to that post
114 + * @return static
62 115 */
63 - public function get_language( $post_id ) {
64 - $lang = $this->get_object_term( $post_id, 'language' );
65 - return ( $lang ) ? $this->model->get_language( $lang ) : false;
116 + public function init() {
117 + // Setups post types to translate.
118 + add_action( 'registered_post_type', array( $this, 'registered_post_type' ) );
119 +
120 + // Forces updating posts cache.
121 + add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
122 + return parent::init();
66 123 }
67 124
68 125 /**
69 - * Deletes a translation
126 + * Deletes a translation of a post.
70 127 *
71 128 * @since 0.5
72 129 *
73 - * @param int $id post id
130 + * @param int $id Post ID.
131 + * @return void
74 132 */
75 133 public function delete_translation( $id ) {
134 + $id = $this->sanitize_int_id( $id );
135 +
136 + if ( empty( $id ) ) {
137 + return;
138 + }
139 +
76 140 parent::delete_translation( $id );
77 - wp_set_object_terms( $id, null, $this->tax_translations );
141 + wp_set_object_terms( $id, array(), $this->tax_translations );
78 142 }
79 143
80 144 /**
81 - * A join clause to add to sql queries when filtering by language is needed directly in query
145 + * Returns object types (post types) that need to be translated.
146 + * The post types list is cached for better performance.
147 + * The method waits for 'after_setup_theme' to apply the cache to allow themes adding the filter in functions.php.
82 148 *
83 - * @since 1.2
149 + * @since 3.4
84 150 *
85 - * @param string $alias Alias for $wpdb->posts table
86 - * @return string join clause
151 + * @param bool $filter True if we should return only valid registered object types.
152 + * @return string[] Object type names for which Polylang manages languages.
153 + *
154 + * @phpstan-return array<non-empty-string, non-empty-string>
87 155 */
88 - public function join_clause( $alias = '' ) {
89 - global $wpdb;
90 - if ( empty( $alias ) ) {
91 - $alias = $wpdb->posts;
156 + public function get_translated_object_types( $filter = true ) {
157 + $post_types = $this->cache->get( 'post_types' );
158 +
159 + if ( false === $post_types ) {
160 + $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
161 +
162 + if ( ! empty( $this->options['post_types'] ) ) {
163 + $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
164 + }
165 +
166 + if ( empty( $this->options['media_support'] ) ) {
167 + // In case the post type attachment is stored in the option.
168 + unset( $post_types['attachment'] );
169 + } else {
170 + $post_types['attachment'] = 'attachment';
171 + }
172 +
173 + /**
174 + * Filters the list of post types available for translation.
175 + * The default are post types which have the parameter ‘public’ set to true.
176 + * The filter must be added soon in the WordPress loading process:
177 + * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
178 + *
179 + * @since 0.8
180 + *
181 + * @param string[] $post_types List of post type names (as array keys and values).
182 + * @param bool $is_settings True when displaying the list of custom post types in Polylang settings.
183 + */
184 + $post_types = (array) apply_filters( 'pll_get_post_types', $post_types, false );
185 +
186 + if ( did_action( 'after_setup_theme' ) && ! doing_action( 'switch_blog' ) ) {
187 + $this->cache->set( 'post_types', $post_types );
188 + }
92 189 }
93 - return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.ID";
190 +
191 + /** @var array<non-empty-string, non-empty-string> $post_types */
192 + return $filter ? array_intersect( $post_types, get_post_types() ) : $post_types;
94 193 }
95 194
96 195 /**
97 - * Register the language taxonomy
196 + * Returns true if Polylang manages languages for this object type.
98 197 *
99 - * @since 1.2
198 + * @since 3.4
199 + *
200 + * @param string|string[] $object_type Object type (post type) name or array of object type names.
201 + * @return bool
202 + *
203 + * @phpstan-param non-empty-string|non-empty-string[] $object_type
100 204 */
101 - public function register_taxonomy() {
102 - register_taxonomy(
103 - 'language',
104 - $this->model->get_translated_post_types(),
105 - array(
106 - 'labels' => array(
107 - 'name' => __( 'Languages', 'polylang' ),
108 - 'singular_name' => __( 'Language', 'polylang' ),
109 - 'all_items' => __( 'All languages', 'polylang' ),
110 - ),
111 - 'public' => false,
112 - 'show_ui' => false, // hide the taxonomy on admin side, needed for WP 4.4.x
113 - 'show_in_nav_menus' => false, // no metabox for nav menus, needed for WP 4.4.x
114 - 'publicly_queryable' => true, // since WP 4.5
115 - 'query_var' => 'lang',
116 - 'rewrite' => $this->model->options['force_lang'] < 2, // no rewrite for domains and sub-domains
117 - '_pll' => true, // polylang taxonomy
118 - )
119 - );
205 + public function is_translated_object_type( $object_type ) {
206 + $post_types = $this->get_translated_object_types( false );
207 + return ( is_array( $object_type ) && array_intersect( $object_type, $post_types ) ) || in_array( $object_type, $post_types ) || ( 'any' === $object_type && ! empty( $post_types ) );
120 208 }
121 209
122 210 /**
123 - * Check if registered post type must be translated
211 + * Checks if registered post type must be translated.
124 212 *
125 213 * @since 1.2
126 214 *
127 - * @param string $post_type post type name
215 + * @param string $post_type Post type name.
216 + * @return void
217 + *
218 + * @phpstan-param non-empty-string $post_type
128 219 */
129 220 public function registered_post_type( $post_type ) {
130 - if ( $this->model->is_translated_post_type( $post_type ) ) {
131 - register_taxonomy_for_object_type( 'language', $post_type );
132 - register_taxonomy_for_object_type( 'post_translations', $post_type );
221 + if ( $this->is_translated_object_type( $post_type ) ) {
222 + register_taxonomy_for_object_type( $this->tax_language, $post_type );
223 + register_taxonomy_for_object_type( $this->tax_translations, $post_type );
133 224 }
134 225 }
135 226
136 227 /**
137 - * Forces calling 'update_object_term_cache' when querying posts or pages
138 - * this is especially useful for nav menus with a lot of pages
139 - * without doing this, we would have one query per page in the menu to get the page language for the permalink
228 + * Forces calling 'update_object_term_cache' when querying posts or pages.
229 + * This is especially useful for nav menus with a lot of pages as, without doing this,
230 + * we would have one query per page in the menu to get the page language for the permalink.
140 231 *
141 232 * @since 1.8
142 233 *
143 - * @param object $query reference to the query object
234 + * @param WP_Query $query Reference to the query object.
235 + * @return void
144 236 */
145 237 public function pre_get_posts( $query ) {
146 - if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) {
238 + if ( ! empty( $query->query['post_type'] ) && $this->is_translated_object_type( $query->query['post_type'] ) ) {
147 239 $query->query_vars['update_post_term_cache'] = true;
148 240 }
149 241 }
150 242
151 243 /**
152 - * Checks if the current user can read the post
244 + * Checks if the current user can read the post.
153 245 *
154 246 * @since 1.5
247 + * @since 3.4 Renamed the parameter $post_id into $id.
155 248 *
156 - * @param int $post_id Post ID
157 - * @param string $context Optional, 'edit' or 'view', defaults to 'view'.
249 + * @param int $id Post ID
250 + * @param string $context Optional, 'edit' or 'view'. Defaults to 'view'.
158 251 * @return bool
252 + *
253 + * @phpstan-param non-empty-string $context
159 254 */
160 - public function current_user_can_read( $post_id, $context = 'view' ) {
161 - $post = get_post( $post_id );
255 + public function current_user_can_read( $id, $context = 'view' ) {
256 + $id = $this->sanitize_int_id( $id );
162 257
258 + if ( empty( $id ) ) {
259 + return false;
260 + }
261 +
262 + $post = get_post( $id );
263 +
163 264 if ( empty( $post ) ) {
164 265 return false;
165 266 }
166 267
@@ -165,8 +266,12 @@
165 266 }
166 267
167 268 if ( 'inherit' === $post->post_status && $post->post_parent ) {
168 269 $post = get_post( $post->post_parent );
270 +
271 + if ( empty( $post ) ) {
272 + return false;
273 + }
169 274 }
170 275
171 276 if ( 'inherit' === $post->post_status || in_array( $post->post_status, get_post_stati( array( 'public' => true ) ) ) ) {
172 277 return true;
@@ -173,11 +278,21 @@
173 278 }
174 279
175 280 // Follow WP practices, which shows links to private posts ( when readable ), but not for draft posts ( ex: get_adjacent_post_link() )
176 281 if ( in_array( $post->post_status, get_post_stati( array( 'private' => true ) ) ) ) {
282 + if ( ! is_user_logged_in() ) {
283 + return false;
284 + }
285 +
286 + $user = wp_get_current_user();
287 +
288 + if ( (int) $user->ID === (int) $post->post_author ) {
289 + return true;
290 + }
291 +
177 292 $post_type_object = get_post_type_object( $post->post_type );
178 - $user = wp_get_current_user();
179 - 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!
293 +
294 + return ! empty( $post_type_object ) && current_user_can( $post_type_object->cap->read_private_posts );
180 295 }
181 296
182 297 // In edit context, show draft and future posts.
183 298 if ( 'edit' === $context ) {
@@ -189,9 +304,9 @@
189 304 );
190 305
191 306 if ( in_array( $post->post_status, $states ) ) {
192 307 $user = wp_get_current_user();
193 - return is_user_logged_in() && ( current_user_can( 'edit_posts' ) || $user->ID == $post->post_author ); // Comparison must not be strict!
308 + return is_user_logged_in() && ( current_user_can( 'edit_posts' ) || (int) $user->ID === (int) $post->post_author );
194 309 }
195 310 }
196 311
197 312 return false;
@@ -197,57 +312,265 @@
197 312 return false;
198 313 }
199 314
200 315 /**
201 - * Returns a list of posts in a language ( $lang )
202 - * not translated in another language ( $untranslated_in )
316 + * Creates a media translation
203 317 *
204 - * @since 2.6
318 + * @since 1.8
319 + * @since 3.7 Moved from PLL_CRUD_Posts.
205 320 *
206 - * @param string $type Post type
207 - * @param string $untranslated_in The posts must not be translated in this language
208 - * @param string $lang Language of the search posts
209 - * @param string $search Limit results to posts matching this string
210 - * @return array Array of posts
321 + * @param int $post_id Original attachment id.
322 + * @param string|PLL_Language $lang New translation language.
323 + * @return int Attachment id of the translated media.
211 324 */
212 - public function get_untranslated( $type, $untranslated_in, $lang, $search = '' ) {
213 - $return = array();
325 + public function create_media_translation( $post_id, $lang ) {
326 + if ( empty( $post_id ) ) {
327 + return 0;
328 + }
214 329
215 - // Don't order by title: see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough
216 - $args = array(
217 - 's' => $search,
218 - 'suppress_filters' => 0, // To make the post_fields filter work
219 - 'lang' => 0, // Avoid admin language filter
220 - 'numberposts' => 20, // Limit to 20 posts
221 - 'post_status' => 'any',
222 - 'post_type' => $type,
223 - 'tax_query' => array(
224 - array(
225 - 'taxonomy' => 'language',
226 - 'field' => 'term_taxonomy_id', // WP 3.5+
227 - 'terms' => $lang->term_taxonomy_id,
228 - ),
229 - ),
230 - );
330 + $post = get_post( $post_id, ARRAY_A );
231 331
332 + if ( empty( $post ) ) {
333 + return 0;
334 + }
335 +
336 + $lang = $this->languages->get( $lang ); // Make sure we get a valid language slug.
337 +
338 + if ( empty( $lang ) ) {
339 + return 0;
340 + }
341 +
342 + // Create a new attachment ( translate attachment parent if exists ).
343 + add_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Avoid a conflict with automatic duplicate at upload.
344 + unset( $post['ID'] ); // Will force the creation.
345 + if ( ! empty( $post['post_parent'] ) ) {
346 + $post['post_parent'] = (int) $this->get_translation( $post['post_parent'], $lang->slug );
347 + }
348 + $post['tax_input'] = array( 'language' => array( $lang->slug ) ); // Assigns the language.
349 +
350 + // Loads the strings translations with the attachment's target language.
351 + PLL()->load_strings_translations( $lang->slug );
352 +
353 + $tr_id = wp_insert_attachment( wp_slash( $post ) );
354 + remove_filter( 'pll_enable_duplicate_media', '__return_false', 99 ); // Restore automatic duplicate at upload.
355 +
356 + // Copy metadata.
357 + $data = wp_get_attachment_metadata( $post_id, true ); // Unfiltered.
358 + if ( is_array( $data ) ) {
359 + wp_update_attachment_metadata( $tr_id, wp_slash( $data ) ); // Directly uses update_post_meta, so expects slashed.
360 + }
361 +
362 + // Copy attached file.
363 + if ( $file = get_attached_file( $post_id, true ) ) { // Unfiltered.
364 + update_attached_file( $tr_id, wp_slash( $file ) ); // Directly uses update_post_meta, so expects slashed.
365 + }
366 +
367 + // Copy alternative text. Direct use of the meta as there is no filtered wrapper to manipulate it.
368 + if ( $text = get_post_meta( $post_id, '_wp_attachment_image_alt', true ) ) {
369 + add_post_meta( $tr_id, '_wp_attachment_image_alt', wp_slash( $text ) );
370 + }
371 +
372 + $this->set_language( $tr_id, $lang );
373 +
374 + $translations = $this->get_translations( $post_id );
375 + $translations[ $lang->slug ] = $tr_id;
376 + $this->save_translations( $tr_id, $translations );
377 +
232 378 /**
233 - * Filter the query args when auto suggesting untranslated posts in the Languages metabox
234 - * This should help plugins to fix some edge cases
379 + * Fires after a media translation is created
235 380 *
236 - * @see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough
381 + * @since 1.6.4
237 382 *
383 + * @param int $post_id Post id of the source media.
384 + * @param int $tr_id Post id of the new media translation.
385 + * @param string $slug Language code of the new translation.
386 + */
387 + do_action( 'pll_translate_media', $post_id, $tr_id, $lang->slug );
388 +
389 + // Restores the strings translations with the current language.
390 + if ( PLL()->curlang instanceof PLL_Language ) {
391 + PLL()->load_strings_translations( PLL()->curlang->slug );
392 + }
393 +
394 + return $tr_id;
395 + }
396 +
397 + /**
398 + * Returns a list of posts in a language ($lang) not translated in another language ($untranslated_in).
399 + *
400 + * @since 2.6
401 + *
402 + * @param string $type Post type.
403 + * @param PLL_Language $untranslated_in The language the posts must not be translated in.
404 + * @param PLL_Language $lang Language of the searched posts.
405 + * @param string $search Limit the results to the posts matching this string.
406 + * @return WP_Post[] Array of posts.
407 + */
408 + public function get_untranslated( $type, PLL_Language $untranslated_in, PLL_Language $lang, $search = '' ) {
409 + global $wpdb;
410 +
411 + $args = array( 'numberposts' => 20 ); // Limit to 20 posts by default.
412 + /**
413 + * Filters the query args when auto suggesting untranslated posts in the Languages metabox.
414 + *
238 415 * @since 1.7
416 + * @since 3.4 Handled arguments restricted to `numberposts` to limit queried posts.
417 + * No `WP_Query` is made anymore, a custom one is used instead.
239 418 *
240 419 * @param array $args WP_Query arguments
241 420 */
242 - $args = apply_filters( 'pll_ajax_posts_not_translated_args', $args );
243 - $posts = get_posts( $args );
421 + $args = apply_filters( 'pll_ajax_posts_not_translated_args', $args );
244 422
245 - foreach ( $posts as $post ) {
246 - if ( ! $this->get_translation( $post->ID, $untranslated_in ) && $this->current_user_can_read( $post->ID, 'edit' ) ) {
247 - $return[] = $post;
423 + $limit = $args['numberposts'];
424 + $search_like = '%' . $wpdb->esc_like( $search ) . '%';
425 + $untranslated_like = '%' . $wpdb->esc_like( $untranslated_in->slug ) . '%';
426 + $posts = $wpdb->get_results(
427 + $wpdb->prepare(
428 + "SELECT * FROM {$wpdb->posts}
429 + INNER JOIN {$wpdb->term_relationships} AS tr1 ON ({$wpdb->posts}.ID = tr1.object_id)
430 + AND tr1.term_taxonomy_id IN (%d)
431 + AND (({$wpdb->posts}.post_title LIKE %s)
432 + OR ({$wpdb->posts}.post_excerpt LIKE %s)
433 + OR ({$wpdb->posts}.post_content LIKE %s)
434 + )
435 + AND {$wpdb->posts}.post_type = %s
436 + AND {$wpdb->posts}.post_status NOT IN ('trash', 'auto-draft')
437 + WHERE {$wpdb->posts}.ID NOT IN (
438 + SELECT {$wpdb->posts}.ID FROM {$wpdb->posts}
439 + LEFT JOIN {$wpdb->term_relationships} AS tr2 ON ({$wpdb->posts}.ID = tr2.object_id)
440 + INNER JOIN {$wpdb->term_taxonomy} AS tt ON (tt.term_taxonomy_id = tr2.term_taxonomy_id)
441 + AND (tt.taxonomy = 'post_translations')
442 + AND tt.description LIKE %s
443 + )
444 + LIMIT 0, %d",
445 + $lang->get_tax_prop( $this->tax_language, 'term_taxonomy_id' ),
446 + $search_like,
447 + $search_like,
448 + $search_like,
449 + $type,
450 + $untranslated_like,
451 + $limit
452 + )
453 + );
454 +
455 + foreach ( $posts as $i => $post ) {
456 + if ( ! $this->current_user_can_read( $post->ID, 'edit' ) ) {
457 + unset( $posts[ $i ] );
458 + continue;
248 459 }
460 +
461 + $posts[ $i ] = new WP_Post( $post );
249 462 }
250 463
251 - return $return;
464 + return $posts;
465 + }
466 +
467 + /**
468 + * Returns the description to use for the "language properties" in the REST API.
469 + *
470 + * @since 3.7
471 + * @see WP_Syntex\Polylang\REST\V2\Languages::get_item_schema()
472 + *
473 + * @return string
474 + */
475 + public function get_rest_description(): string {
476 + return __( 'Language taxonomy properties for post types.', 'polylang' );
477 + }
478 +
479 + /**
480 + * Returns database-related information that can be used in some of this class methods.
481 + * These are specific to the table containing the objects.
482 + *
483 + * @see PLL_Translatable_Object::join_clause()
484 + * @see PLL_Translatable_Object::get_raw_objects_with_no_lang()
485 + *
486 + * @since 3.4.3
487 + *
488 + * @return string[] {
489 + * @type string $table Name of the table.
490 + * @type string $id_column Name of the column containing the object's ID.
491 + * @type string $type_column Name of the column containing the object's type.
492 + * @type string $default_alias Default alias corresponding to the object's table.
493 + * }
494 + * @phpstan-return DBInfoWithType
495 + */
496 + protected function get_db_infos() {
497 + return array(
498 + 'table' => $GLOBALS['wpdb']->posts,
499 + 'id_column' => 'ID',
500 + 'type_column' => 'post_type',
501 + 'default_alias' => $GLOBALS['wpdb']->posts,
502 + );
503 + }
504 +
505 + /**
506 + * Wraps `wp_insert_post` with language feature.
507 + *
508 + * @since 3.7
509 + *
510 + * @param array $postarr {
511 + * Optional. An array of elements that make up a post to insert.
512 + * @See wp_insert_post() for accepted arguments.
513 + *
514 + * @type string[] $translations The translation group to assign to the post with language slug as keys and post ID as values.
515 + * }
516 + * @param PLL_Language $language The post language object.
517 + * @return int|WP_Error The post ID on success. The value `WP_Error` on failure.
518 + */
519 + public function insert( array $postarr, PLL_Language $language ) {
520 + $post_id = wp_insert_post( $postarr, true );
521 + if ( is_wp_error( $post_id ) ) {
522 + // Something went wrong!
523 + return $post_id;
524 + }
525 +
526 + $this->set_language( $post_id, $language );
527 +
528 + if ( ! empty( $postarr['translations'] ) ) {
529 + $this->save_translations( $post_id, $postarr['translations'] );
530 + }
531 +
532 + return $post_id;
533 + }
534 +
535 + /**
536 + * Wraps `wp_update_post` with language feature.
537 + *
538 + * @since 3.7
539 + *
540 + * @param array $postarr {
541 + * Optional. An array of elements that make up a post to update.
542 + * @See wp_insert_post() for accepted arguments.
543 + *
544 + * @type PLL_Language|string $lang The post language object or slug.
545 + * @type string[] $translations The translation group to assign to the post with language slug as keys and post ID as values.
546 + * }
547 + * @return int|WP_Error The post ID on success. The value `WP_Error` on failure.
548 + */
549 + public function update( array $postarr ) {
550 + if ( ! empty( $postarr['lang'] ) ) {
551 + $post = get_post( $postarr['ID'] );
552 + if ( ! $post instanceof WP_Post ) {
553 + return new WP_Error( 'invalid_post', __( 'Invalid post ID.', 'polylang' ) );
554 + }
555 +
556 + $language = $this->languages->get( $postarr['lang'] );
557 + if ( ! $language instanceof PLL_Language ) {
558 + return new WP_Error( 'invalid_language', __( 'Please provide a valid language.', 'polylang' ) );
559 + }
560 +
561 + $this->set_language( $postarr['ID'], $language );
562 + }
563 +
564 + $post_id = wp_update_post( $postarr, true );
565 + if ( is_wp_error( $post_id ) ) {
566 + // Something went wrong!
567 + return $post_id;
568 + }
569 +
570 + if ( ! empty( $postarr['translations'] ) ) {
571 + $this->save_translations( $postarr['ID'], $postarr['translations'] );
572 + }
573 +
574 + return $post_id;
252 575 }
253 576 }