PluginProbe
Polylang / 2.3.7
Polylang v2.3.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
polylang / include / translated-post.php

translated-post.php in Polylang 2.3.7, at include/translated-post.php

147 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Setups the posts languages and translations model
5 *
6 * @since 1.8
7 */
8 class PLL_Translated_Post extends PLL_Translated_Object {
9
10 /**
11 * Constructor
12 *
13 * @since 1.8
14 *
15 * @param object $model
16 */
17 public function __construct( &$model ) {
18 // init properties
19 $this->object_type = null;
20 $this->tax_language = 'language';
21 $this->tax_translations = 'post_translations';
22 $this->tax_tt = 'term_taxonomy_id';
23
24 parent::__construct( $model );
25
26 // registers completely the language taxonomy
27 add_action( 'setup_theme', array( $this, 'register_taxonomy' ), 1 );
28
29 // setups post types to translate
30 add_action( 'registered_post_type', array( $this, 'registered_post_type' ) );
31
32 // forces updating posts cache
33 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
34 }
35
36 /**
37 * Store the post language in the database
38 *
39 * @since 0.6
40 *
41 * @param int $post_id post id
42 * @param int|string|object $lang language ( term_id or slug or object )
43 */
44 public function set_language( $post_id, $lang ) {
45 $old_lang = $this->get_language( $post_id );
46 $old_lang = $old_lang ? $old_lang->slug : '';
47 $lang = $lang ? $this->model->get_language( $lang )->slug : '';
48
49 if ( $old_lang !== $lang ) {
50 wp_set_post_terms( (int) $post_id, $lang, 'language' );
51 }
52 }
53
54 /**
55 * Returns the language of a post
56 *
57 * @since 0.1
58 *
59 * @param int $post_id post id
60 * @return bool|object PLL_Language object, false if no language is associated to that post
61 */
62 public function get_language( $post_id ) {
63 $lang = $this->get_object_term( $post_id, 'language' );
64 return ( $lang ) ? $this->model->get_language( $lang ) : false;
65 }
66
67 /**
68 * Deletes a translation
69 *
70 * @since 0.5
71 *
72 * @param int $id post id
73 */
74 public function delete_translation( $id ) {
75 parent::delete_translation( $id );
76 wp_set_object_terms( $id, null, $this->tax_translations );
77 }
78
79 /**
80 * A join clause to add to sql queries when filtering by language is needed directly in query
81 *
82 * @since 1.2
83 *
84 * @param string $alias Alias for $wpdb->posts table
85 * @return string join clause
86 */
87 public function join_clause( $alias = '' ) {
88 global $wpdb;
89 if ( empty( $alias ) ) {
90 $alias = $wpdb->posts;
91 }
92 return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.ID";
93 }
94
95 /**
96 * Register the language taxonomy
97 *
98 * @since 1.2
99 */
100 public function register_taxonomy() {
101 register_taxonomy( 'language', $this->model->get_translated_post_types(), array(
102 'labels' => array(
103 'name' => __( 'Languages', 'polylang' ),
104 'singular_name' => __( 'Language', 'polylang' ),
105 'all_items' => __( 'All languages', 'polylang' ),
106 ),
107 // FIXME backward compatibility with WP 4.4.x: we must keep public to true for WP to accept our query var
108 'public' => version_compare( $GLOBALS['wp_version'], '4.4', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.5', '<' ),
109 'show_ui' => false, // hide the taxonomy on admin side, needed for WP 4.4.x
110 'show_in_nav_menus' => false, // no metabox for nav menus, needed for WP 4.4.x
111 'publicly_queryable' => true, // since WP 4.5
112 'query_var' => 'lang',
113 'rewrite' => $this->model->options['force_lang'] < 2, // no rewrite for domains and sub-domains
114 '_pll' => true, // polylang taxonomy
115 ) );
116 }
117
118 /**
119 * Check if registered post type must be translated
120 *
121 * @since 1.2
122 *
123 * @param string $post_type post type name
124 */
125 public function registered_post_type( $post_type ) {
126 if ( $this->model->is_translated_post_type( $post_type ) ) {
127 register_taxonomy_for_object_type( 'language', $post_type );
128 register_taxonomy_for_object_type( 'post_translations', $post_type );
129 }
130 }
131
132 /**
133 * Forces calling 'update_object_term_cache' when querying posts or pages
134 * this is especially useful for nav menus with a lot of pages
135 * without doing this, we would have one query per page in the menu to get the page language for the permalink
136 *
137 * @since 1.8
138 *
139 * @param object $query reference to the query object
140 */
141 public function pre_get_posts( $query ) {
142 if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) {
143 $query->query_vars['update_post_term_cache'] = true;
144 }
145 }
146 }
147