| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * Setups the posts languages and translations model |
| 5 | 8 | * |
| @@ -7,13 +10,13 @@ | ||
| 7 | 10 | */ |
| 8 | 11 | class PLL_Translated_Post extends PLL_Translated_Object { |
| 9 | 12 | |
| 10 | 13 | /** |
| 11 | - * Constructor | |
| 14 | + * Constructor. | |
| 12 | 15 | * |
| 13 | 16 | * @since 1.8 |
| 14 | 17 | * |
| 15 | - * @param object $model | |
| 18 | + * @param PLL_Model $model PLL_Model instance. | |
| 16 | 19 | */ |
| 17 | 20 | public function __construct( &$model ) { |
| 18 | 21 | // init properties |
| 19 | 22 | $this->object_type = null; // For taxonomies |
| @@ -34,20 +37,23 @@ | ||
| 34 | 37 | add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /** |
| 38 | - * Store the post language in the database | |
| 41 | + * Store the post language in the database. | |
| 39 | 42 | * |
| 40 | 43 | * @since 0.6 |
| 41 | 44 | * |
| 42 | - * @param int $post_id post id | |
| 43 | - * @param int|string|object $lang language ( term_id or slug or object ) | |
| 45 | + * @param int $post_id Post id. | |
| 46 | + * @param int|string|PLL_Language $lang Language (term_id or slug or object). | |
| 47 | + * @return void | |
| 44 | 48 | */ |
| 45 | 49 | public function set_language( $post_id, $lang ) { |
| 46 | 50 | $old_lang = $this->get_language( $post_id ); |
| 47 | 51 | $old_lang = $old_lang ? $old_lang->slug : ''; |
| 48 | - $lang = $lang ? $this->model->get_language( $lang )->slug : ''; | |
| 49 | 52 | |
| 53 | + $lang = $this->model->get_language( $lang ); | |
| 54 | + $lang = $lang ? $lang->slug : ''; | |
| 55 | + | |
| 50 | 56 | if ( $old_lang !== $lang ) { |
| 51 | 57 | wp_set_post_terms( (int) $post_id, $lang, 'language' ); |
| 52 | 58 | } |
| 53 | 59 | } |
| @@ -57,9 +63,9 @@ | ||
| 57 | 63 | * |
| 58 | 64 | * @since 0.1 |
| 59 | 65 | * |
| 60 | 66 | * @param int $post_id post id |
| 61 | - * @return bool|object PLL_Language object, false if no language is associated to that post | |
| 67 | + * @return PLL_Language|false PLL_Language object, false if no language is associated to that post | |
| 62 | 68 | */ |
| 63 | 69 | public function get_language( $post_id ) { |
| 64 | 70 | $lang = $this->get_object_term( $post_id, 'language' ); |
| 65 | 71 | return ( $lang ) ? $this->model->get_language( $lang ) : false; |
| @@ -65,17 +71,18 @@ | ||
| 65 | 71 | return ( $lang ) ? $this->model->get_language( $lang ) : false; |
| 66 | 72 | } |
| 67 | 73 | |
| 68 | 74 | /** |
| 69 | - * Deletes a translation | |
| 75 | + * Deletes a translation. | |
| 70 | 76 | * |
| 71 | 77 | * @since 0.5 |
| 72 | 78 | * |
| 73 | - * @param int $id post id | |
| 79 | + * @param int $id Post id. | |
| 80 | + * @return void | |
| 74 | 81 | */ |
| 75 | 82 | public function delete_translation( $id ) { |
| 76 | 83 | parent::delete_translation( $id ); |
| 77 | - wp_set_object_terms( $id, null, $this->tax_translations ); | |
| 84 | + wp_set_object_terms( $id, array(), $this->tax_translations ); | |
| 78 | 85 | } |
| 79 | 86 | |
| 80 | 87 | /** |
| 81 | 88 | * A join clause to add to sql queries when filtering by language is needed directly in query |
| @@ -96,8 +103,10 @@ | ||
| 96 | 103 | /** |
| 97 | 104 | * Register the language taxonomy |
| 98 | 105 | * |
| 99 | 106 | * @since 1.2 |
| 107 | + * | |
| 108 | + * @return void | |
| 100 | 109 | */ |
| 101 | 110 | public function register_taxonomy() { |
| 102 | 111 | register_taxonomy( |
| 103 | 112 | 'language', |
| @@ -124,8 +133,9 @@ | ||
| 124 | 133 | * |
| 125 | 134 | * @since 1.2 |
| 126 | 135 | * |
| 127 | 136 | * @param string $post_type post type name |
| 137 | + * @return void | |
| 128 | 138 | */ |
| 129 | 139 | public function registered_post_type( $post_type ) { |
| 130 | 140 | if ( $this->model->is_translated_post_type( $post_type ) ) { |
| 131 | 141 | register_taxonomy_for_object_type( 'language', $post_type ); |
| @@ -133,15 +143,16 @@ | ||
| 133 | 143 | } |
| 134 | 144 | } |
| 135 | 145 | |
| 136 | 146 | /** |
| 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 | |
| 147 | + * Forces calling 'update_object_term_cache' when querying posts or pages. | |
| 148 | + * This is especially useful for nav menus with a lot of pages as, without doing this, | |
| 149 | + * we would have one query per page in the menu to get the page language for the permalink. | |
| 140 | 150 | * |
| 141 | 151 | * @since 1.8 |
| 142 | 152 | * |
| 143 | - * @param object $query reference to the query object | |
| 153 | + * @param WP_Query $query Reference to the query object. | |
| 154 | + * @return void | |
| 144 | 155 | */ |
| 145 | 156 | public function pre_get_posts( $query ) { |
| 146 | 157 | if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) { |
| 147 | 158 | $query->query_vars['update_post_term_cache'] = true; |
| @@ -165,8 +176,12 @@ | ||
| 165 | 176 | } |
| 166 | 177 | |
| 167 | 178 | if ( 'inherit' === $post->post_status && $post->post_parent ) { |
| 168 | 179 | $post = get_post( $post->post_parent ); |
| 180 | + | |
| 181 | + if ( empty( $post ) ) { | |
| 182 | + return false; | |
| 183 | + } | |
| 169 | 184 | } |
| 170 | 185 | |
| 171 | 186 | if ( 'inherit' === $post->post_status || in_array( $post->post_status, get_post_stati( array( 'public' => true ) ) ) ) { |
| 172 | 187 | return true; |
| @@ -198,17 +213,17 @@ | ||
| 198 | 213 | } |
| 199 | 214 | |
| 200 | 215 | /** |
| 201 | 216 | * Returns a list of posts in a language ( $lang ) |
| 202 | - * not translated in another language ( $untranslated_in ) | |
| 217 | + * not translated in another language ( $untranslated_in ). | |
| 203 | 218 | * |
| 204 | 219 | * @since 2.6 |
| 205 | 220 | * |
| 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 | |
| 221 | + * @param string $type Post type. | |
| 222 | + * @param PLL_Language $untranslated_in The language the posts must not be translated in. | |
| 223 | + * @param PLL_Language $lang Language of the searched posts. | |
| 224 | + * @param string $search Limit the results to the posts matching this string. | |
| 225 | + * @return WP_Post[] Array of posts. | |
| 211 | 226 | */ |
| 212 | 227 | public function get_untranslated( $type, $untranslated_in, $lang, $search = '' ) { |
| 213 | 228 | $return = array(); |
| 214 | 229 | |
| @@ -242,9 +257,9 @@ | ||
| 242 | 257 | $args = apply_filters( 'pll_ajax_posts_not_translated_args', $args ); |
| 243 | 258 | $posts = get_posts( $args ); |
| 244 | 259 | |
| 245 | 260 | foreach ( $posts as $post ) { |
| 246 | - if ( ! $this->get_translation( $post->ID, $untranslated_in ) && $this->current_user_can_read( $post->ID, 'edit' ) ) { | |
| 261 | + if ( $post instanceof WP_Post && ! $this->get_translation( $post->ID, $untranslated_in ) && $this->current_user_can_read( $post->ID, 'edit' ) ) { | |
| 247 | 262 | $return[] = $post; |
| 248 | 263 | } |
| 249 | 264 | } |
| 250 | 265 | |
| @@ -249,5 +264,6 @@ | ||
| 249 | 264 | } |
| 250 | 265 | |
| 251 | 266 | return $return; |
| 252 | 267 | } |
| 268 | + | |
| 253 | 269 | } |