| @@ -15,10 +15,9 @@ | ||
| 15 | 15 | * @param object $model |
| 16 | 16 | */ |
| 17 | 17 | public function __construct( &$model ) { |
| 18 | 18 | // init properties |
| 19 | - $this->object_type = null; // For taxonomies | |
| 20 | - $this->type = 'post'; // For capabilities | |
| 19 | + $this->object_type = null; | |
| 21 | 20 | $this->tax_language = 'language'; |
| 22 | 21 | $this->tax_translations = 'post_translations'; |
| 23 | 22 | $this->tax_tt = 'term_taxonomy_id'; |
| 24 | 23 | |
| @@ -42,15 +41,9 @@ | ||
| 42 | 41 | * @param int $post_id post id |
| 43 | 42 | * @param int|string|object $lang language ( term_id or slug or object ) |
| 44 | 43 | */ |
| 45 | 44 | public function set_language( $post_id, $lang ) { |
| 46 | - $old_lang = $this->get_language( $post_id ); | |
| 47 | - $old_lang = $old_lang ? $old_lang->slug : ''; | |
| 48 | - $lang = $lang ? $this->model->get_language( $lang )->slug : ''; | |
| 49 | - | |
| 50 | - if ( $old_lang !== $lang ) { | |
| 51 | - wp_set_post_terms( (int) $post_id, $lang, 'language' ); | |
| 52 | - } | |
| 45 | + wp_set_post_terms( (int) $post_id, $lang ? $this->model->get_language( $lang )->slug : '', 'language' ); | |
| 53 | 46 | } |
| 54 | 47 | |
| 55 | 48 | /** |
| 56 | 49 | * Returns the language of a post |
| @@ -81,9 +74,8 @@ | ||
| 81 | 74 | * A join clause to add to sql queries when filtering by language is needed directly in query |
| 82 | 75 | * |
| 83 | 76 | * @since 1.2 |
| 84 | 77 | * |
| 85 | - * @param string $alias Alias for $wpdb->posts table | |
| 86 | 78 | * @return string join clause |
| 87 | 79 | */ |
| 88 | 80 | public function join_clause( $alias = '' ) { |
| 89 | 81 | global $wpdb; |
| @@ -98,26 +90,23 @@ | ||
| 98 | 90 | * |
| 99 | 91 | * @since 1.2 |
| 100 | 92 | */ |
| 101 | 93 | public function register_taxonomy() { |
| 102 | - register_taxonomy( | |
| 103 | - 'language', | |
| 104 | - $this->model->get_translated_post_types(), | |
| 105 | - array( | |
| 106 | - 'labels' => array( | |
| 107 | - 'name' => __( 'Languages', 'polylang' ), | |
| 108 | - 'singular_name' => __( 'Language', 'polylang' ), | |
| 109 | - 'all_items' => __( 'All languages', 'polylang' ), | |
| 110 | - ), | |
| 111 | - 'public' => false, | |
| 112 | - 'show_ui' => false, // hide the taxonomy on admin side, needed for WP 4.4.x | |
| 113 | - 'show_in_nav_menus' => false, // no metabox for nav menus, needed for WP 4.4.x | |
| 114 | - 'publicly_queryable' => true, // since WP 4.5 | |
| 115 | - 'query_var' => 'lang', | |
| 116 | - 'rewrite' => $this->model->options['force_lang'] < 2, // no rewrite for domains and sub-domains | |
| 117 | - '_pll' => true, // polylang taxonomy | |
| 118 | - ) | |
| 119 | - ); | |
| 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 | + ) ); | |
| 120 | 109 | } |
| 121 | 110 | |
| 122 | 111 | /** |
| 123 | 112 | * Check if registered post type must be translated |
| @@ -145,109 +134,6 @@ | ||
| 145 | 134 | public function pre_get_posts( $query ) { |
| 146 | 135 | if ( ! empty( $query->query['post_type'] ) && $this->model->is_translated_post_type( $query->query['post_type'] ) ) { |
| 147 | 136 | $query->query_vars['update_post_term_cache'] = true; |
| 148 | 137 | } |
| 149 | - } | |
| 150 | - | |
| 151 | - /** | |
| 152 | - * Checks if the current user can read the post | |
| 153 | - * | |
| 154 | - * @since 1.5 | |
| 155 | - * | |
| 156 | - * @param int $post_id Post ID | |
| 157 | - * @param string $context Optional, 'edit' or 'view', defaults to 'view'. | |
| 158 | - * @return bool | |
| 159 | - */ | |
| 160 | - public function current_user_can_read( $post_id, $context = 'view' ) { | |
| 161 | - $post = get_post( $post_id ); | |
| 162 | - | |
| 163 | - if ( empty( $post ) ) { | |
| 164 | - return false; | |
| 165 | - } | |
| 166 | - | |
| 167 | - if ( 'inherit' === $post->post_status && $post->post_parent ) { | |
| 168 | - $post = get_post( $post->post_parent ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - if ( 'inherit' === $post->post_status || in_array( $post->post_status, get_post_stati( array( 'public' => true ) ) ) ) { | |
| 172 | - return true; | |
| 173 | - } | |
| 174 | - | |
| 175 | - // Follow WP practices, which shows links to private posts ( when readable ), but not for draft posts ( ex: get_adjacent_post_link() ) | |
| 176 | - if ( in_array( $post->post_status, get_post_stati( array( 'private' => true ) ) ) ) { | |
| 177 | - $post_type_object = get_post_type_object( $post->post_type ); | |
| 178 | - $user = wp_get_current_user(); | |
| 179 | - 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! | |
| 180 | - } | |
| 181 | - | |
| 182 | - // In edit context, show draft and future posts. | |
| 183 | - if ( 'edit' === $context ) { | |
| 184 | - $states = get_post_stati( | |
| 185 | - array( | |
| 186 | - 'protected' => true, | |
| 187 | - 'show_in_admin_all_list' => true, | |
| 188 | - ) | |
| 189 | - ); | |
| 190 | - | |
| 191 | - if ( in_array( $post->post_status, $states ) ) { | |
| 192 | - $user = wp_get_current_user(); | |
| 193 | - return is_user_logged_in() && ( current_user_can( 'edit_posts' ) || $user->ID == $post->post_author ); // Comparison must not be strict! | |
| 194 | - } | |
| 195 | - } | |
| 196 | - | |
| 197 | - return false; | |
| 198 | - } | |
| 199 | - | |
| 200 | - /** | |
| 201 | - * Returns a list of posts in a language ( $lang ) | |
| 202 | - * not translated in another language ( $untranslated_in ) | |
| 203 | - * | |
| 204 | - * @since 2.6 | |
| 205 | - * | |
| 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 | |
| 211 | - */ | |
| 212 | - public function get_untranslated( $type, $untranslated_in, $lang, $search = '' ) { | |
| 213 | - $return = array(); | |
| 214 | - | |
| 215 | - // Don't order by title: see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough | |
| 216 | - $args = array( | |
| 217 | - 's' => $search, | |
| 218 | - 'suppress_filters' => 0, // To make the post_fields filter work | |
| 219 | - 'lang' => 0, // Avoid admin language filter | |
| 220 | - 'numberposts' => 20, // Limit to 20 posts | |
| 221 | - 'post_status' => 'any', | |
| 222 | - 'post_type' => $type, | |
| 223 | - 'tax_query' => array( | |
| 224 | - array( | |
| 225 | - 'taxonomy' => 'language', | |
| 226 | - 'field' => 'term_taxonomy_id', // WP 3.5+ | |
| 227 | - 'terms' => $lang->term_taxonomy_id, | |
| 228 | - ), | |
| 229 | - ), | |
| 230 | - ); | |
| 231 | - | |
| 232 | - /** | |
| 233 | - * Filter the query args when auto suggesting untranslated posts in the Languages metabox | |
| 234 | - * This should help plugins to fix some edge cases | |
| 235 | - * | |
| 236 | - * @see https://wordpress.org/support/topic/find-translated-post-when-10-is-not-enough | |
| 237 | - * | |
| 238 | - * @since 1.7 | |
| 239 | - * | |
| 240 | - * @param array $args WP_Query arguments | |
| 241 | - */ | |
| 242 | - $args = apply_filters( 'pll_ajax_posts_not_translated_args', $args ); | |
| 243 | - $posts = get_posts( $args ); | |
| 244 | - | |
| 245 | - foreach ( $posts as $post ) { | |
| 246 | - if ( ! $this->get_translation( $post->ID, $untranslated_in ) && $this->current_user_can_read( $post->ID, 'edit' ) ) { | |
| 247 | - $return[] = $post; | |
| 248 | - } | |
| 249 | - } | |
| 250 | - | |
| 251 | - return $return; | |
| 252 | 138 | } |
| 253 | 139 | } |