| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Setups the posts languages and translations model |
| 8 | 5 | * |
| @@ -10,18 +7,17 @@ | ||
| 10 | 7 | */ |
| 11 | 8 | class PLL_Translated_Post extends PLL_Translated_Object { |
| 12 | 9 | |
| 13 | 10 | /** |
| 14 | - * Constructor. | |
| 11 | + * Constructor | |
| 15 | 12 | * |
| 16 | 13 | * @since 1.8 |
| 17 | 14 | * |
| 18 | - * @param PLL_Model $model PLL_Model instance. | |
| 15 | + * @param object $model | |
| 19 | 16 | */ |
| 20 | 17 | public function __construct( &$model ) { |
| 21 | 18 | // init properties |
| 22 | - $this->object_type = null; // For taxonomies | |
| 23 | - $this->type = 'post'; // For capabilities | |
| 19 | + $this->object_type = null; | |
| 24 | 20 | $this->tax_language = 'language'; |
| 25 | 21 | $this->tax_translations = 'post_translations'; |
| 26 | 22 | $this->tax_tt = 'term_taxonomy_id'; |
| 27 | 23 | |
| @@ -37,23 +33,20 @@ | ||
| 37 | 33 | add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 38 | 34 | } |
| 39 | 35 | |
| 40 | 36 | /** |
| 41 | - * Store the post language in the database. | |
| 37 | + * Store the post language in the database | |
| 42 | 38 | * |
| 43 | 39 | * @since 0.6 |
| 44 | 40 | * |
| 45 | - * @param int $post_id Post id. | |
| 46 | - * @param int|string|PLL_Language $lang Language (term_id or slug or object). | |
| 47 | - * @return void | |
| 41 | + * @param int $post_id post id | |
| 42 | + * @param int|string|object $lang language ( term_id or slug or object ) | |
| 48 | 43 | */ |
| 49 | 44 | public function set_language( $post_id, $lang ) { |
| 50 | 45 | $old_lang = $this->get_language( $post_id ); |
| 51 | 46 | $old_lang = $old_lang ? $old_lang->slug : ''; |
| 47 | + $lang = $lang ? $this->model->get_language( $lang )->slug : ''; | |
| 52 | 48 | |
| 53 | - $lang = $this->model->get_language( $lang ); | |
| 54 | - $lang = $lang ? $lang->slug : ''; | |
| 55 | - | |
| 56 | 49 | if ( $old_lang !== $lang ) { |
| 57 | 50 | wp_set_post_terms( (int) $post_id, $lang, 'language' ); |
| 58 | 51 | } |
| 59 | 52 | } |
| @@ -63,9 +56,9 @@ | ||
| 63 | 56 | * |
| 64 | 57 | * @since 0.1 |
| 65 | 58 | * |
| 66 | 59 | * @param int $post_id post id |
| 67 | - * @return PLL_Language|false PLL_Language object, false if no language is associated to that post | |
| 60 | + * @return bool|object PLL_Language object, false if no language is associated to that post | |
| 68 | 61 | */ |
| 69 | 62 | public function get_language( $post_id ) { |
| 70 | 63 | $lang = $this->get_object_term( $post_id, 'language' ); |
| 71 | 64 | return ( $lang ) ? $this->model->get_language( $lang ) : false; |
| @@ -71,18 +64,17 @@ | ||
| 71 | 64 | return ( $lang ) ? $this->model->get_language( $lang ) : false; |
| 72 | 65 | } |
| 73 | 66 | |
| 74 | 67 | /** |
| 75 | - * Deletes a translation. | |
| 68 | + * Deletes a translation | |
| 76 | 69 | * |
| 77 | 70 | * @since 0.5 |
| 78 | 71 | * |
| 79 | - * @param int $id Post id. | |
| 80 | - * @return void | |
| 72 | + * @param int $id post id | |
| 81 | 73 | */ |
| 82 | 74 | public function delete_translation( $id ) { |
| 83 | 75 | parent::delete_translation( $id ); |
| 84 | - wp_set_object_terms( $id, array(), $this->tax_translations ); | |
| 76 | + wp_set_object_terms( $id, null, $this->tax_translations ); | |
| 85 | 77 | } |
| 86 | 78 | |
| 87 | 79 | /** |
| 88 | 80 | * A join clause to add to sql queries when filtering by language is needed directly in query |
| @@ -103,10 +95,8 @@ | ||
| 103 | 95 | /** |
| 104 | 96 | * Register the language taxonomy |
| 105 | 97 | * |
| 106 | 98 | * @since 1.2 |
| 107 | - * | |
| 108 | - * @return void | |
| 109 | 99 | */ |
| 110 | 100 | public function register_taxonomy() { |
| 111 | 101 | register_taxonomy( |
| 112 | 102 | 'language', |
| @@ -133,9 +123,8 @@ | ||
| 133 | 123 | * |
| 134 | 124 | * @since 1.2 |
| 135 | 125 | * |
| 136 | 126 | * @param string $post_type post type name |
| 137 | - * @return void | |
| 138 | 127 | */ |
| 139 | 128 | public function registered_post_type( $post_type ) { |
| 140 | 129 | if ( $this->model->is_translated_post_type( $post_type ) ) { |
| 141 | 130 | register_taxonomy_for_object_type( 'language', $post_type ); |
| @@ -143,126 +132,18 @@ | ||
| 143 | 132 | } |
| 144 | 133 | } |
| 145 | 134 | |
| 146 | 135 | /** |
| 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. | |
| 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 | |
| 150 | 139 | * |
| 151 | 140 | * @since 1.8 |
| 152 | 141 | * |
| 153 | - * @param WP_Query $query Reference to the query object. | |
| 154 | - * @return void | |
| 142 | + * @param object $query reference to the query object | |
| 155 | 143 | */ |
| 156 | 144 | public function pre_get_posts( $query ) { |
| 157 | 145 | if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) { |
| 158 | 146 | $query->query_vars['update_post_term_cache'] = true; |
| 159 | 147 | } |
| 160 | - } | |
| 161 | - | |
| 162 | - /** | |
| 163 | - * Checks if the current user can read the post | |
| 164 | - * | |
| 165 | - * @since 1.5 | |
| 166 | - * | |
| 167 | - * @param int $post_id Post ID | |
| 168 | - * @param string $context Optional, 'edit' or 'view', defaults to 'view'. | |
| 169 | - * @return bool | |
| 170 | - */ | |
| 171 | - public function current_user_can_read( $post_id, $context = 'view' ) { | |
| 172 | - $post = get_post( $post_id ); | |
| 173 | - | |
| 174 | - if ( empty( $post ) ) { | |
| 175 | - return false; | |
| 176 | - } | |
| 177 | - | |
| 178 | - if ( 'inherit' === $post->post_status && $post->post_parent ) { | |
| 179 | - $post = get_post( $post->post_parent ); | |
| 180 | - | |
| 181 | - if ( empty( $post ) ) { | |
| 182 | - return false; | |
| 183 | - } | |
| 184 | - } | |
| 185 | - | |
| 186 | - if ( 'inherit' === $post->post_status || in_array( $post->post_status, get_post_stati( array( 'public' => true ) ) ) ) { | |
| 187 | - return true; | |
| 188 | - } | |
| 189 | - | |
| 190 | - // Follow WP practices, which shows links to private posts ( when readable ), but not for draft posts ( ex: get_adjacent_post_link() ) | |
| 191 | - if ( in_array( $post->post_status, get_post_stati( array( 'private' => true ) ) ) ) { | |
| 192 | - $post_type_object = get_post_type_object( $post->post_type ); | |
| 193 | - $user = wp_get_current_user(); | |
| 194 | - return is_user_logged_in() && ( current_user_can( $post_type_object->cap->read_private_posts ) || $user->ID == $post->post_author ); // Comparison must not be strict! | |
| 195 | - } | |
| 196 | - | |
| 197 | - // In edit context, show draft and future posts. | |
| 198 | - if ( 'edit' === $context ) { | |
| 199 | - $states = get_post_stati( | |
| 200 | - array( | |
| 201 | - 'protected' => true, | |
| 202 | - 'show_in_admin_all_list' => true, | |
| 203 | - ) | |
| 204 | - ); | |
| 205 | - | |
| 206 | - if ( in_array( $post->post_status, $states ) ) { | |
| 207 | - $user = wp_get_current_user(); | |
| 208 | - return is_user_logged_in() && ( current_user_can( 'edit_posts' ) || $user->ID == $post->post_author ); // Comparison must not be strict! | |
| 209 | - } | |
| 210 | - } | |
| 211 | - | |
| 212 | - return false; | |
| 213 | - } | |
| 214 | - | |
| 215 | - /** | |
| 216 | - * Returns a list of posts in a language ( $lang ) | |
| 217 | - * not translated in another language ( $untranslated_in ). | |
| 218 | - * | |
| 219 | - * @since 2.6 | |
| 220 | - * | |
| 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. | |
| 226 | - */ | |
| 227 | - public function get_untranslated( $type, $untranslated_in, $lang, $search = '' ) { | |
| 228 | - $return = array(); | |
| 229 | - | |
| 230 | - // Don't order by title: see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough | |
| 231 | - $args = array( | |
| 232 | - 's' => $search, | |
| 233 | - 'suppress_filters' => 0, // To make the post_fields filter work | |
| 234 | - 'lang' => 0, // Avoid admin language filter | |
| 235 | - 'numberposts' => 20, // Limit to 20 posts | |
| 236 | - 'post_status' => 'any', | |
| 237 | - 'post_type' => $type, | |
| 238 | - 'tax_query' => array( | |
| 239 | - array( | |
| 240 | - 'taxonomy' => 'language', | |
| 241 | - 'field' => 'term_taxonomy_id', // WP 3.5+ | |
| 242 | - 'terms' => $lang->term_taxonomy_id, | |
| 243 | - ), | |
| 244 | - ), | |
| 245 | - ); | |
| 246 | - | |
| 247 | - /** | |
| 248 | - * Filter the query args when auto suggesting untranslated posts in the Languages metabox | |
| 249 | - * This should help plugins to fix some edge cases | |
| 250 | - * | |
| 251 | - * @see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough | |
| 252 | - * | |
| 253 | - * @since 1.7 | |
| 254 | - * | |
| 255 | - * @param array $args WP_Query arguments | |
| 256 | - */ | |
| 257 | - $args = apply_filters( 'pll_ajax_posts_not_translated_args', $args ); | |
| 258 | - $posts = get_posts( $args ); | |
| 259 | - | |
| 260 | - foreach ( $posts as $post ) { | |
| 261 | - if ( $post instanceof WP_Post && ! $this->get_translation( $post->ID, $untranslated_in ) && $this->current_user_can_read( $post->ID, 'edit' ) ) { | |
| 262 | - $return[] = $post; | |
| 263 | - } | |
| 264 | - } | |
| 265 | - | |
| 266 | - return $return; | |
| 267 | 148 | } |
| 268 | 149 | } |