PluginProbe
Polylang / 1.9.2
Polylang v1.9.2
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 1.9.2, at include/translated-post.php

137 lines 4.0 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() {
81 global $wpdb;
82 return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = ID";
83 }
84
85 /**
86 * Register the language taxonomy
87 *
88 * @since 1.2
89 */
90 public function register_taxonomy() {
91 register_taxonomy( 'language', $this->model->get_translated_post_types(), array(
92 'labels' => array(
93 'name' => __( 'Languages', 'polylang' ),
94 'singular_name' => __( 'Language', 'polylang' ),
95 'all_items' => __( 'All languages', 'polylang' ),
96 ),
97 // FIXME backward compatibility with WP 4.4.x: we must keep public to true for WP to accept our query var
98 'public' => version_compare( $GLOBALS['wp_version'], '4.4', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.5', '<' ),
99 'show_ui' => false, // hide the taxonomy on admin side, needed for WP 4.4.x
100 'show_in_nav_menus' => false, // no metabox for nav menus, needed for WP 4.4.x
101 'publicly_queryable' => true, // since WP 4.5
102 'query_var' => 'lang',
103 'rewrite' => $this->model->options['force_lang'] < 2, // no rewrite for domains and sub-domains
104 '_pll' => true, // polylang taxonomy
105 ) );
106 }
107
108 /**
109 * Check if registered post type must be translated
110 *
111 * @since 1.2
112 *
113 * @param string $post_type post type name
114 */
115 public function registered_post_type( $post_type ) {
116 if ( $this->model->is_translated_post_type( $post_type ) ) {
117 register_taxonomy_for_object_type( 'language', $post_type );
118 register_taxonomy_for_object_type( 'post_translations', $post_type );
119 }
120 }
121
122 /**
123 * Forces calling 'update_object_term_cache' when querying posts or pages
124 * this is especially useful for nav menus with a lot of pages
125 * without doing this, we would have one query per page in the menu to get the page language for the permalink
126 *
127 * @since 1.8
128 *
129 * @param object $query reference to the query object
130 */
131 public function pre_get_posts( $query ) {
132 if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) {
133 $query->query_vars['update_post_term_cache'] = true;
134 }
135 }
136 }
137