PluginProbe
Polylang / 2.1
Polylang v2.1
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.1, at include/translated-post.php

140 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 wp_set_post_terms( (int) $post_id, $lang ? $this->model->get_language( $lang )->slug : '', 'language' );
46 }
47
48 /**
49 * Returns the language of a post
50 *
51 * @since 0.1
52 *
53 * @param int $post_id post id
54 * @return bool|object PLL_Language object, false if no language is associated to that post
55 */
56 public function get_language( $post_id ) {
57 $lang = $this->get_object_term( $post_id, 'language' );
58 return ( $lang ) ? $this->model->get_language( $lang ) : false;
59 }
60
61 /**
62 * Deletes a translation
63 *
64 * @since 0.5
65 *
66 * @param int $id post id
67 */
68 public function delete_translation( $id ) {
69 parent::delete_translation( $id );
70 wp_set_object_terms( $id, null, $this->tax_translations );
71 }
72
73 /**
74 * A join clause to add to sql queries when filtering by language is needed directly in query
75 *
76 * @since 1.2
77 *
78 * @return string join clause
79 */
80 public function join_clause( $alias = '' ) {
81 global $wpdb;
82 if ( empty( $alias ) ) {
83 $alias = $wpdb->posts;
84 }
85 return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = $alias.ID";
86 }
87
88 /**
89 * Register the language taxonomy
90 *
91 * @since 1.2
92 */
93 public function register_taxonomy() {
94 register_taxonomy( 'language', $this->model->get_translated_post_types(), array(
95 'labels' => array(
96 'name' => __( 'Languages', 'polylang' ),
97 'singular_name' => __( 'Language', 'polylang' ),
98 'all_items' => __( 'All languages', 'polylang' ),
99 ),
100 // FIXME backward compatibility with WP 4.4.x: we must keep public to true for WP to accept our query var
101 'public' => version_compare( $GLOBALS['wp_version'], '4.4', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.5', '<' ),
102 'show_ui' => false, // hide the taxonomy on admin side, needed for WP 4.4.x
103 'show_in_nav_menus' => false, // no metabox for nav menus, needed for WP 4.4.x
104 'publicly_queryable' => true, // since WP 4.5
105 'query_var' => 'lang',
106 'rewrite' => $this->model->options['force_lang'] < 2, // no rewrite for domains and sub-domains
107 '_pll' => true, // polylang taxonomy
108 ) );
109 }
110
111 /**
112 * Check if registered post type must be translated
113 *
114 * @since 1.2
115 *
116 * @param string $post_type post type name
117 */
118 public function registered_post_type( $post_type ) {
119 if ( $this->model->is_translated_post_type( $post_type ) ) {
120 register_taxonomy_for_object_type( 'language', $post_type );
121 register_taxonomy_for_object_type( 'post_translations', $post_type );
122 }
123 }
124
125 /**
126 * Forces calling 'update_object_term_cache' when querying posts or pages
127 * this is especially useful for nav menus with a lot of pages
128 * without doing this, we would have one query per page in the menu to get the page language for the permalink
129 *
130 * @since 1.8
131 *
132 * @param object $query reference to the query object
133 */
134 public function pre_get_posts( $query ) {
135 if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) {
136 $query->query_vars['update_post_term_cache'] = true;
137 }
138 }
139 }
140