PluginProbe
Polylang / 2.4
Polylang v2.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 / translated-post.php

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

150 lines 4.1 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(
102 'language',
103 $this->model->get_translated_post_types(),
104 array(
105 'labels' => array(
106 'name' => __( 'Languages', 'polylang' ),
107 'singular_name' => __( 'Language', 'polylang' ),
108 'all_items' => __( 'All languages', 'polylang' ),
109 ),
110 'public' => false,
111 'show_ui' => false, // hide the taxonomy on admin side, needed for WP 4.4.x
112 'show_in_nav_menus' => false, // no metabox for nav menus, needed for WP 4.4.x
113 'publicly_queryable' => true, // since WP 4.5
114 'query_var' => 'lang',
115 'rewrite' => $this->model->options['force_lang'] < 2, // no rewrite for domains and sub-domains
116 '_pll' => true, // polylang taxonomy
117 )
118 );
119 }
120
121 /**
122 * Check if registered post type must be translated
123 *
124 * @since 1.2
125 *
126 * @param string $post_type post type name
127 */
128 public function registered_post_type( $post_type ) {
129 if ( $this->model->is_translated_post_type( $post_type ) ) {
130 register_taxonomy_for_object_type( 'language', $post_type );
131 register_taxonomy_for_object_type( 'post_translations', $post_type );
132 }
133 }
134
135 /**
136 * Forces calling 'update_object_term_cache' when querying posts or pages
137 * this is especially useful for nav menus with a lot of pages
138 * without doing this, we would have one query per page in the menu to get the page language for the permalink
139 *
140 * @since 1.8
141 *
142 * @param object $query reference to the query object
143 */
144 public function pre_get_posts( $query ) {
145 if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) {
146 $query->query_vars['update_post_term_cache'] = true;
147 }
148 }
149 }
150