| @@ -1,74 +1,33 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -/* Post Meta */ | |
| 4 | - | |
| 5 | -add_action( 'init', | |
| 6 | - | |
| 7 | - function() { | |
| 8 | - $post_types = bogo_localizable_post_types(); | |
| 9 | - | |
| 10 | - $auth_callback = function ( $allowed, $meta_key, $object_id, $user_id ) { | |
| 11 | - return user_can( $user_id, 'edit_post', $object_id ); | |
| 12 | - }; | |
| 13 | - | |
| 14 | - foreach ( $post_types as $post_type ) { | |
| 15 | - register_post_meta( $post_type, | |
| 16 | - '_locale', | |
| 17 | - array( | |
| 18 | - 'type' => 'string', | |
| 19 | - 'single' => true, | |
| 20 | - 'auth_callback' => $auth_callback, | |
| 21 | - 'show_in_rest' => true, | |
| 22 | - ) | |
| 23 | - ); | |
| 24 | - | |
| 25 | - register_post_meta( $post_type, | |
| 26 | - '_original_post', | |
| 27 | - array( | |
| 28 | - 'type' => 'string', | |
| 29 | - 'single' => true, | |
| 30 | - 'auth_callback' => $auth_callback, | |
| 31 | - 'show_in_rest' => true, | |
| 32 | - ) | |
| 33 | - ); | |
| 34 | - } | |
| 35 | - }, | |
| 36 | - | |
| 37 | - 10, 0 | |
| 38 | -); | |
| 39 | - | |
| 40 | 3 | /* Post Template */ |
| 41 | 4 | |
| 42 | 5 | add_filter( 'body_class', 'bogo_body_class', 10, 2 ); |
| 43 | 6 | |
| 44 | -function bogo_body_class( $classes, $classes_to_add ) { | |
| 7 | +function bogo_body_class( $classes, $class ) { | |
| 45 | 8 | $locale = bogo_language_tag( get_locale() ); |
| 46 | 9 | $locale = esc_attr( $locale ); |
| 47 | 10 | |
| 48 | - if ( $locale and ! in_array( $locale, $classes ) ) { | |
| 11 | + if ( $locale && ! in_array( $locale, $classes ) ) | |
| 49 | 12 | $classes[] = $locale; |
| 50 | - } | |
| 51 | 13 | |
| 52 | 14 | return $classes; |
| 53 | 15 | } |
| 54 | 16 | |
| 55 | - | |
| 56 | 17 | add_filter( 'post_class', 'bogo_post_class', 10, 3 ); |
| 57 | 18 | |
| 58 | -function bogo_post_class( $classes, $classes_to_add, $post_id ) { | |
| 19 | +function bogo_post_class( $classes, $class, $post_id ) { | |
| 59 | 20 | $locale = bogo_get_post_locale( $post_id ); |
| 60 | 21 | $locale = bogo_language_tag( $locale ); |
| 61 | 22 | $locale = esc_attr( $locale ); |
| 62 | 23 | |
| 63 | - if ( $locale and ! in_array( $locale, $classes ) ) { | |
| 24 | + if ( $locale && ! in_array( $locale, $classes ) ) | |
| 64 | 25 | $classes[] = $locale; |
| 65 | - } | |
| 66 | 26 | |
| 67 | 27 | return $classes; |
| 68 | 28 | } |
| 69 | 29 | |
| 70 | - | |
| 71 | 30 | function bogo_get_post_locale( $post_id ) { |
| 72 | 31 | $locale = get_post_meta( $post_id, '_locale', true ); |
| 73 | 32 | |
| 74 | 33 | if ( empty( $locale ) ) { |
| @@ -77,60 +36,22 @@ | ||
| 77 | 36 | |
| 78 | 37 | return $locale; |
| 79 | 38 | } |
| 80 | 39 | |
| 81 | - | |
| 82 | 40 | function bogo_localizable_post_types() { |
| 83 | 41 | $localizable = apply_filters( 'bogo_localizable_post_types', |
| 84 | - array( 'post', 'page' ) | |
| 85 | - ); | |
| 42 | + array( 'post', 'page' ) ); | |
| 86 | 43 | |
| 87 | - $localizable = array_diff( | |
| 88 | - $localizable, | |
| 89 | - array( 'attachment', 'revision', 'nav_menu_item' ) | |
| 90 | - ); | |
| 44 | + $localizable = array_diff( $localizable, | |
| 45 | + array( 'attachment', 'revision', 'nav_menu_item' ) ); | |
| 91 | 46 | |
| 92 | 47 | return $localizable; |
| 93 | 48 | } |
| 94 | 49 | |
| 95 | - | |
| 96 | 50 | function bogo_is_localizable_post_type( $post_type ) { |
| 97 | - return in_array( $post_type, bogo_localizable_post_types(), true ); | |
| 51 | + return ! empty( $post_type ) && in_array( $post_type, bogo_localizable_post_types() ); | |
| 98 | 52 | } |
| 99 | 53 | |
| 100 | - | |
| 101 | -function bogo_count_posts( $locale, $post_type = 'post' ) { | |
| 102 | - global $wpdb; | |
| 103 | - | |
| 104 | - if ( | |
| 105 | - ! bogo_is_available_locale( $locale ) or | |
| 106 | - ! bogo_is_localizable_post_type( $post_type ) | |
| 107 | - ) { | |
| 108 | - return false; | |
| 109 | - } | |
| 110 | - | |
| 111 | - if ( bogo_is_default_locale( $locale ) ) { | |
| 112 | - $count = $wpdb->get_var( $wpdb->prepare( | |
| 113 | - "SELECT COUNT(1) FROM %i LEFT JOIN %i AS postmeta ON ID = postmeta.post_id AND meta_key = '_locale' WHERE post_type = %s AND post_status = 'publish' AND (meta_value LIKE %s OR meta_id IS NULL)", | |
| 114 | - $wpdb->posts, | |
| 115 | - $wpdb->postmeta, | |
| 116 | - $post_type, | |
| 117 | - $locale | |
| 118 | - ) ); | |
| 119 | - } else { | |
| 120 | - $count = $wpdb->get_var( $wpdb->prepare( | |
| 121 | - "SELECT COUNT(1) FROM %i LEFT JOIN %i AS postmeta ON ID = postmeta.post_id AND meta_key = '_locale' WHERE post_type = %s AND post_status = 'publish' AND meta_value LIKE %s", | |
| 122 | - $wpdb->posts, | |
| 123 | - $wpdb->postmeta, | |
| 124 | - $post_type, | |
| 125 | - $locale | |
| 126 | - ) ); | |
| 127 | - } | |
| 128 | - | |
| 129 | - return (int) $count; | |
| 130 | -} | |
| 131 | - | |
| 132 | - | |
| 133 | 54 | function bogo_get_post_translations( $post_id = 0 ) { |
| 134 | 55 | $post = get_post( $post_id ); |
| 135 | 56 | |
| 136 | 57 | if ( ! $post ) { |
| @@ -136,19 +57,24 @@ | ||
| 136 | 57 | if ( ! $post ) { |
| 137 | 58 | return false; |
| 138 | 59 | } |
| 139 | 60 | |
| 140 | - static $translations = array(); | |
| 61 | + if ( 'auto-draft' == $post->post_status ) { | |
| 62 | + if ( ! empty( $_REQUEST['original_post'] ) ) { | |
| 63 | + $original = get_post_meta( $_REQUEST['original_post'], '_original_post', true ); | |
| 141 | 64 | |
| 142 | - if ( isset( $translations[$post->ID] ) ) { | |
| 143 | - return $translations[$post->ID]; | |
| 65 | + if ( empty( $original ) ) { | |
| 66 | + $original = (int) $_REQUEST['original_post']; | |
| 67 | + } | |
| 68 | + } else { | |
| 69 | + return false; | |
| 70 | + } | |
| 71 | + } else { | |
| 72 | + $original = get_post_meta( $post->ID, '_original_post', true ); | |
| 144 | 73 | } |
| 145 | 74 | |
| 146 | - $original_post = get_post_meta( $post->ID, '_original_post', true ); | |
| 147 | - | |
| 148 | - // For back-compat | |
| 149 | - if ( empty( $original_post ) ) { | |
| 150 | - $original_post = $post->ID; | |
| 75 | + if ( empty( $original ) ) { | |
| 76 | + $original = $post->ID; | |
| 151 | 77 | } |
| 152 | 78 | |
| 153 | 79 | $args = array( |
| 154 | 80 | 'bogo_suppress_locale_query' => true, |
| @@ -155,27 +81,24 @@ | ||
| 155 | 81 | 'posts_per_page' => -1, |
| 156 | 82 | 'post_status' => 'any', |
| 157 | 83 | 'post_type' => $post->post_type, |
| 158 | 84 | 'meta_key' => '_original_post', |
| 159 | - 'meta_value' => $original_post, | |
| 160 | - ); | |
| 85 | + 'meta_value' => $original ); | |
| 161 | 86 | |
| 162 | 87 | $q = new WP_Query(); |
| 163 | 88 | $posts = $q->query( $args ); |
| 164 | 89 | |
| 165 | - // For back-compat | |
| 166 | - if ( | |
| 167 | - is_int( $original_post ) and | |
| 168 | - $p = get_post( $original_post ) and | |
| 169 | - 'trash' !== get_post_status( $p ) | |
| 170 | - ) { | |
| 171 | - array_unshift( $posts, $p ); | |
| 90 | + $translations = array(); | |
| 91 | + | |
| 92 | + $original_post_status = get_post_status( $original ); | |
| 93 | + | |
| 94 | + if ( $original != $post->ID && $original_post_status && 'trash' != $original_post_status ) { | |
| 95 | + $locale = bogo_get_post_locale( $original ); | |
| 96 | + $translations[$locale] = get_post( $original ); | |
| 172 | 97 | } |
| 173 | 98 | |
| 174 | - $translations[$post->ID] = array(); | |
| 175 | - | |
| 176 | 99 | foreach ( $posts as $p ) { |
| 177 | - if ( $p->ID === $post->ID ) { | |
| 100 | + if ( $p->ID == $post->ID ) { | |
| 178 | 101 | continue; |
| 179 | 102 | } |
| 180 | 103 | |
| 181 | 104 | $locale = bogo_get_post_locale( $p->ID ); |
| @@ -183,19 +106,16 @@ | ||
| 183 | 106 | if ( ! bogo_is_available_locale( $locale ) ) { |
| 184 | 107 | continue; |
| 185 | 108 | } |
| 186 | 109 | |
| 187 | - if ( ! isset( $translations[$post->ID][$locale] ) ) { | |
| 188 | - $translations[$post->ID][$locale] = $p; | |
| 110 | + if ( ! isset( $translations[$locale] ) ) { | |
| 111 | + $translations[$locale] = $p; | |
| 189 | 112 | } |
| 190 | 113 | } |
| 191 | 114 | |
| 192 | - $translations[$post->ID] = array_filter( $translations[$post->ID] ); | |
| 193 | - | |
| 194 | - return $translations[$post->ID]; | |
| 115 | + return array_filter( $translations ); | |
| 195 | 116 | } |
| 196 | 117 | |
| 197 | - | |
| 198 | 118 | function bogo_get_post_translation( $post_id, $locale ) { |
| 199 | 119 | $translations = bogo_get_post_translations( $post_id ); |
| 200 | 120 | |
| 201 | 121 | if ( isset( $translations[$locale] ) ) { |
| @@ -204,9 +124,8 @@ | ||
| 204 | 124 | |
| 205 | 125 | return false; |
| 206 | 126 | } |
| 207 | 127 | |
| 208 | - | |
| 209 | 128 | function bogo_get_page_by_path( $page_path, $locale = null, $post_type = 'page' ) { |
| 210 | 129 | global $wpdb; |
| 211 | 130 | |
| 212 | 131 | if ( ! bogo_is_available_locale( $locale ) ) { |
| @@ -220,39 +139,30 @@ | ||
| 220 | 139 | $parts = explode( '/', trim( $page_path, '/' ) ); |
| 221 | 140 | $parts = array_map( 'esc_sql', $parts ); |
| 222 | 141 | $parts = array_map( 'sanitize_title_for_query', $parts ); |
| 223 | 142 | |
| 224 | - $in_string = implode( ',', array_map( | |
| 225 | - static function ( $p ) use ( $wpdb ) { | |
| 226 | - return $wpdb->prepare( '%s', $p ); | |
| 227 | - }, | |
| 228 | - $parts | |
| 229 | - ) ); | |
| 143 | + $in_string = "'" . implode( "','", $parts ) . "'"; | |
| 144 | + $post_type_sql = $post_type; | |
| 145 | + $wpdb->escape_by_ref( $post_type_sql ); | |
| 230 | 146 | |
| 231 | - if ( bogo_is_default_locale( $locale ) ) { | |
| 232 | - $pages = $wpdb->get_results( $wpdb->prepare( | |
| 233 | - "SELECT ID, post_name, post_parent FROM %i LEFT JOIN %i AS postmeta ON ID = postmeta.post_id AND meta_key = '_locale' WHERE post_name IN ($in_string) AND (post_type = %s OR post_type = 'attachment') AND (meta_value LIKE %s OR meta_id IS NULL)", | |
| 234 | - $wpdb->posts, | |
| 235 | - $wpdb->postmeta, | |
| 236 | - $post_type, | |
| 237 | - $locale | |
| 238 | - ), OBJECT_K ); | |
| 239 | - } else { | |
| 240 | - $pages = $wpdb->get_results( $wpdb->prepare( | |
| 241 | - "SELECT ID, post_name, post_parent FROM %i LEFT JOIN %i AS postmeta ON ID = postmeta.post_id AND meta_key = '_locale' WHERE post_name IN ($in_string) AND (post_type = %s OR post_type = 'attachment') AND (meta_value LIKE %s)", | |
| 242 | - $wpdb->posts, | |
| 243 | - $wpdb->postmeta, | |
| 244 | - $post_type, | |
| 245 | - $locale | |
| 246 | - ), OBJECT_K ); | |
| 247 | - } | |
| 147 | + $q = "SELECT ID, post_name, post_parent FROM $wpdb->posts"; | |
| 148 | + $q .= " LEFT JOIN $wpdb->postmeta ON ID = $wpdb->postmeta.post_id AND meta_key = '_locale'"; | |
| 149 | + $q .= " WHERE 1=1"; | |
| 150 | + $q .= " AND post_name IN ($in_string)"; | |
| 151 | + $q .= " AND (post_type = '$post_type_sql' OR post_type = 'attachment')"; | |
| 152 | + $q .= " AND (1=0"; | |
| 153 | + $q .= $wpdb->prepare( " OR meta_value LIKE %s", $locale ); | |
| 154 | + $q .= bogo_is_default_locale( $locale ) ? " OR meta_id IS NULL" : ""; | |
| 155 | + $q .= ")"; | |
| 248 | 156 | |
| 157 | + $pages = $wpdb->get_results( $q, OBJECT_K ); | |
| 158 | + | |
| 249 | 159 | $revparts = array_reverse( $parts ); |
| 250 | 160 | |
| 251 | 161 | $foundid = 0; |
| 252 | 162 | |
| 253 | 163 | foreach ( (array) $pages as $page ) { |
| 254 | - if ( $page->post_name !== $revparts[0] ) { | |
| 164 | + if ( $page->post_name != $revparts[0] ) { | |
| 255 | 165 | continue; |
| 256 | 166 | } |
| 257 | 167 | |
| 258 | 168 | $count = 0; |
| @@ -257,16 +167,14 @@ | ||
| 257 | 167 | |
| 258 | 168 | $count = 0; |
| 259 | 169 | $p = $page; |
| 260 | 170 | |
| 261 | - while ( ! empty( $p->post_parent ) and isset( $pages[$p->post_parent] ) ) { | |
| 171 | + while ( $p->post_parent != 0 && isset( $pages[$p->post_parent] ) ) { | |
| 262 | 172 | $count++; |
| 263 | 173 | $parent = $pages[$p->post_parent]; |
| 264 | 174 | |
| 265 | - if ( | |
| 266 | - ! isset( $revparts[$count] ) or | |
| 267 | - $parent->post_name !== $revparts[$count] | |
| 268 | - ) { | |
| 175 | + if ( ! isset( $revparts[$count] ) | |
| 176 | + || $parent->post_name != $revparts[$count] ) { | |
| 269 | 177 | break; |
| 270 | 178 | } |
| 271 | 179 | |
| 272 | 180 | $p = $parent; |
| @@ -271,13 +179,11 @@ | ||
| 271 | 179 | |
| 272 | 180 | $p = $parent; |
| 273 | 181 | } |
| 274 | 182 | |
| 275 | - if ( | |
| 276 | - empty( $p->post_parent ) and | |
| 277 | - $count + 1 === count( $revparts ) and | |
| 278 | - $p->post_name === $revparts[$count] | |
| 279 | - ) { | |
| 183 | + if ( $p->post_parent == 0 | |
| 184 | + && $count + 1 == count( $revparts ) | |
| 185 | + && $p->post_name == $revparts[$count] ) { | |
| 280 | 186 | $foundid = $page->ID; |
| 281 | 187 | break; |
| 282 | 188 | } |
| 283 | 189 | } |
| @@ -288,24 +194,19 @@ | ||
| 288 | 194 | |
| 289 | 195 | return null; |
| 290 | 196 | } |
| 291 | 197 | |
| 292 | - | |
| 293 | 198 | function bogo_duplicate_post( $original_post, $locale ) { |
| 294 | 199 | $original_post = get_post( $original_post ); |
| 295 | 200 | |
| 296 | - if ( | |
| 297 | - ! $original_post or | |
| 298 | - ! bogo_is_localizable_post_type( get_post_type( $original_post ) ) or | |
| 299 | - 'auto-draft' === get_post_status( $original_post ) | |
| 300 | - ) { | |
| 201 | + if ( ! $original_post | |
| 202 | + || ! bogo_is_localizable_post_type( get_post_type( $original_post ) ) | |
| 203 | + || 'auto-draft' == get_post_status( $original_post ) ) { | |
| 301 | 204 | return false; |
| 302 | 205 | } |
| 303 | 206 | |
| 304 | - if ( | |
| 305 | - ! bogo_is_available_locale( $locale ) or | |
| 306 | - bogo_get_post_locale( $original_post->ID ) === $locale | |
| 307 | - ) { | |
| 207 | + if ( ! bogo_is_available_locale( $locale ) | |
| 208 | + || bogo_get_post_locale( $original_post->ID ) == $locale ) { | |
| 308 | 209 | return false; |
| 309 | 210 | } |
| 310 | 211 | |
| 311 | 212 | if ( bogo_get_post_translation( $original_post->ID, $locale ) ) { |
| @@ -326,15 +227,13 @@ | ||
| 326 | 227 | 'post_parent' => $original_post->post_parent, |
| 327 | 228 | 'menu_order' => $original_post->menu_order, |
| 328 | 229 | 'post_mime_type' => $original_post->post_mime_type, |
| 329 | 230 | 'tax_input' => array(), |
| 330 | - 'meta_input' => array(), | |
| 331 | - ); | |
| 231 | + 'meta_input' => array() ); | |
| 332 | 232 | |
| 333 | 233 | if ( ! empty( $original_post->post_parent ) ) { |
| 334 | 234 | $parent_translation = bogo_get_post_translation( |
| 335 | - $original_post->post_parent, $locale | |
| 336 | - ); | |
| 235 | + $original_post->post_parent, $locale ); | |
| 337 | 236 | |
| 338 | 237 | if ( $parent_translation ) { |
| 339 | 238 | $postarr['post_parent'] = $parent_translation->ID; |
| 340 | 239 | } |
| @@ -342,12 +241,11 @@ | ||
| 342 | 241 | |
| 343 | 242 | if ( $taxonomies = get_object_taxonomies( $original_post ) ) { |
| 344 | 243 | foreach ( $taxonomies as $taxonomy ) { |
| 345 | 244 | $terms = wp_get_post_terms( $original_post->ID, |
| 346 | - $taxonomy, array( 'fields' => 'ids' ) | |
| 347 | - ); | |
| 245 | + $taxonomy, array( 'fields' => 'ids' ) ); | |
| 348 | 246 | |
| 349 | - if ( $terms and ! is_wp_error( $terms ) ) { | |
| 247 | + if ( $terms && ! is_wp_error( $terms ) ) { | |
| 350 | 248 | $postarr['tax_input'][$taxonomy] = $terms; |
| 351 | 249 | } |
| 352 | 250 | } |
| 353 | 251 | } |
| @@ -352,19 +250,13 @@ | ||
| 352 | 250 | } |
| 353 | 251 | } |
| 354 | 252 | |
| 355 | 253 | if ( $post_metas = get_post_meta( $original_post->ID ) ) { |
| 356 | - | |
| 357 | - $post_metas = array_map( function ( $post_meta ) { | |
| 358 | - return array_map( 'maybe_unserialize', (array) $post_meta ); | |
| 359 | - }, $post_metas ); | |
| 360 | - | |
| 361 | 254 | $postarr['meta_input'] = $post_metas; |
| 362 | 255 | } |
| 363 | 256 | |
| 364 | - $postarr = apply_filters( 'bogo_duplicate_post', $postarr, | |
| 365 | - $original_post, $locale | |
| 366 | - ); | |
| 257 | + $postarr = apply_filters( 'bogo_duplicate_post', | |
| 258 | + $postarr, $original_post, $locale ); | |
| 367 | 259 | |
| 368 | 260 | if ( $taxonomies = $postarr['tax_input'] ) { |
| 369 | 261 | $postarr['tax_input'] = array(); |
| 370 | 262 | } |
| @@ -396,39 +288,14 @@ | ||
| 396 | 288 | |
| 397 | 289 | update_post_meta( $new_post_id, '_locale', $locale ); |
| 398 | 290 | |
| 399 | 291 | $meta_original_post = get_post_meta( $original_post->ID, |
| 400 | - '_original_post', true | |
| 401 | - ); | |
| 292 | + '_original_post', true ); | |
| 402 | 293 | |
| 403 | 294 | if ( $meta_original_post ) { |
| 404 | - update_post_meta( $new_post_id, | |
| 405 | - '_original_post', $meta_original_post | |
| 406 | - ); | |
| 295 | + update_post_meta( $new_post_id, '_original_post', $meta_original_post ); | |
| 407 | 296 | } else { |
| 408 | - $original_post_guid = get_the_guid( $original_post ); | |
| 409 | - | |
| 410 | - if ( empty( $original_post_guid ) ) { | |
| 411 | - $original_post_guid = $original_post->ID; | |
| 412 | - } | |
| 413 | - | |
| 414 | - $translations = bogo_get_post_translations( $original_post ); | |
| 415 | - | |
| 416 | - update_post_meta( $original_post->ID, | |
| 417 | - '_original_post', $original_post_guid | |
| 418 | - ); | |
| 419 | - | |
| 420 | - if ( $translations ) { | |
| 421 | - foreach ( $translations as $tr_locale => $tr_post ) { | |
| 422 | - update_post_meta( $tr_post->ID, | |
| 423 | - '_original_post', $original_post_guid | |
| 424 | - ); | |
| 425 | - } | |
| 426 | - } | |
| 427 | - | |
| 428 | - update_post_meta( $new_post_id, | |
| 429 | - '_original_post', $original_post_guid | |
| 430 | - ); | |
| 297 | + update_post_meta( $new_post_id, '_original_post', $original_post->ID ); | |
| 431 | 298 | } |
| 432 | 299 | } |
| 433 | 300 | |
| 434 | 301 | return $new_post_id; |
| @@ -433,17 +300,14 @@ | ||
| 433 | 300 | |
| 434 | 301 | return $new_post_id; |
| 435 | 302 | } |
| 436 | 303 | |
| 437 | - | |
| 438 | 304 | add_filter( 'get_pages', 'bogo_get_pages', 10, 2 ); |
| 439 | 305 | |
| 440 | 306 | function bogo_get_pages( $pages, $args ) { |
| 441 | - if ( | |
| 442 | - is_admin() or | |
| 443 | - ! bogo_is_localizable_post_type( $args['post_type'] ) or | |
| 444 | - ! empty( $args['bogo_suppress_locale_query'] ) | |
| 445 | - ) { | |
| 307 | + if ( is_admin() | |
| 308 | + || ! bogo_is_localizable_post_type( $args['post_type'] ) | |
| 309 | + || ! empty( $args['bogo_suppress_locale_query'] ) ) { | |
| 446 | 310 | return $pages; |
| 447 | 311 | } |
| 448 | 312 | |
| 449 | 313 | $locale = isset( $args['lang'] ) ? $args['lang'] : get_locale(); |
| @@ -456,253 +320,11 @@ | ||
| 456 | 320 | |
| 457 | 321 | foreach ( (array) $pages as $page ) { |
| 458 | 322 | $post_locale = bogo_get_post_locale( $page->ID ); |
| 459 | 323 | |
| 460 | - if ( $post_locale === $locale ) { | |
| 324 | + if ( $post_locale == $locale ) { | |
| 461 | 325 | $new_pages[] = $page; |
| 462 | 326 | } |
| 463 | 327 | } |
| 464 | 328 | |
| 465 | 329 | return $new_pages; |
| 466 | -} | |
| 467 | - | |
| 468 | - | |
| 469 | -add_action( 'save_post', 'bogo_save_post', 10, 2 ); | |
| 470 | - | |
| 471 | -function bogo_save_post( $post_id, $post ) { | |
| 472 | - if ( did_action( 'import_start' ) and ! did_action( 'import_end' ) ) { | |
| 473 | - // Importing | |
| 474 | - return; | |
| 475 | - } | |
| 476 | - | |
| 477 | - if ( ! bogo_is_localizable_post_type( $post->post_type ) ) { | |
| 478 | - return; | |
| 479 | - } | |
| 480 | - | |
| 481 | - $current_locales = get_post_meta( $post_id, '_locale' ); | |
| 482 | - $locale = null; | |
| 483 | - | |
| 484 | - if ( ! empty( $current_locales ) ) { | |
| 485 | - foreach ( $current_locales as $current_locale ) { | |
| 486 | - if ( bogo_is_available_locale( $current_locale ) ) { | |
| 487 | - $locale = $current_locale; | |
| 488 | - break; | |
| 489 | - } | |
| 490 | - } | |
| 491 | - | |
| 492 | - if ( empty( $locale ) or 1 < count( $current_locales ) ) { | |
| 493 | - delete_post_meta( $post_id, '_locale' ); | |
| 494 | - $current_locales = array(); | |
| 495 | - } | |
| 496 | - } | |
| 497 | - | |
| 498 | - if ( empty( $current_locales ) ) { | |
| 499 | - if ( bogo_is_available_locale( $locale ) ) { | |
| 500 | - // $locale = $locale; | |
| 501 | - } elseif ( | |
| 502 | - ! empty( $_REQUEST['locale'] ) and | |
| 503 | - bogo_is_available_locale( $_REQUEST['locale'] ) | |
| 504 | - ) { | |
| 505 | - $locale = $_REQUEST['locale']; | |
| 506 | - } elseif ( 'auto-draft' === get_post_status( $post_id ) ) { | |
| 507 | - $locale = bogo_get_user_locale(); | |
| 508 | - } else { | |
| 509 | - $locale = bogo_get_default_locale(); | |
| 510 | - } | |
| 511 | - | |
| 512 | - add_post_meta( $post_id, '_locale', $locale, true ); | |
| 513 | - } | |
| 514 | - | |
| 515 | - $original_post = get_post_meta( $post_id, '_original_post', true ); | |
| 516 | - | |
| 517 | - if ( empty( $original_post ) ) { | |
| 518 | - $post_guid = get_the_guid( $post_id ); | |
| 519 | - | |
| 520 | - if ( empty( $post_guid ) ) { | |
| 521 | - $post_guid = $post_id; | |
| 522 | - } | |
| 523 | - | |
| 524 | - $translations = bogo_get_post_translations( $post_id ); | |
| 525 | - | |
| 526 | - update_post_meta( $post_id, '_original_post', $post_guid ); | |
| 527 | - | |
| 528 | - if ( $translations ) { | |
| 529 | - foreach ( $translations as $tr_locale => $tr_post ) { | |
| 530 | - update_post_meta( $tr_post->ID, '_original_post', $post_guid ); | |
| 531 | - } | |
| 532 | - } | |
| 533 | - } | |
| 534 | -} | |
| 535 | - | |
| 536 | - | |
| 537 | -add_filter( 'pre_wp_unique_post_slug', 'bogo_unique_post_slug', 10, 6 ); | |
| 538 | - | |
| 539 | -function bogo_unique_post_slug( $override_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) { | |
| 540 | - | |
| 541 | - if ( ! bogo_is_localizable_post_type( $post_type ) ) { | |
| 542 | - return $override_slug; | |
| 543 | - } | |
| 544 | - | |
| 545 | - $locale = bogo_get_post_locale( $post_id ); | |
| 546 | - | |
| 547 | - if ( ! bogo_is_available_locale( $locale ) ) { | |
| 548 | - return $override_slug; | |
| 549 | - } | |
| 550 | - | |
| 551 | - $override_slug = $slug; | |
| 552 | - | |
| 553 | - $q = new WP_Query(); | |
| 554 | - | |
| 555 | - global $wp_rewrite; | |
| 556 | - | |
| 557 | - $feeds = is_array( $wp_rewrite->feeds ) ? $wp_rewrite->feeds : array(); | |
| 558 | - | |
| 559 | - if ( is_post_type_hierarchical( $post_type ) ) { | |
| 560 | - $q_args = array( | |
| 561 | - 'name' => $slug, | |
| 562 | - 'lang' => $locale, | |
| 563 | - 'post_type' => array( $post_type, 'attachment' ), | |
| 564 | - 'post_parent' => $post_parent, | |
| 565 | - 'post__not_in' => array( $post_id ), | |
| 566 | - 'posts_per_page' => 1, | |
| 567 | - ); | |
| 568 | - | |
| 569 | - $is_bad_slug = in_array( $slug, $feeds ) || | |
| 570 | - 'embed' === $slug || | |
| 571 | - preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || | |
| 572 | - apply_filters( | |
| 573 | - 'wp_unique_post_slug_is_bad_hierarchical_slug', false, | |
| 574 | - $slug, $post_type, $post_parent | |
| 575 | - ); | |
| 576 | - | |
| 577 | - if ( ! $is_bad_slug ) { | |
| 578 | - $q_results = $q->query( $q_args ); | |
| 579 | - $is_bad_slug = ! empty( $q_results ); | |
| 580 | - } | |
| 581 | - | |
| 582 | - if ( $is_bad_slug ) { | |
| 583 | - $suffix = 1; | |
| 584 | - | |
| 585 | - while ( $is_bad_slug ) { | |
| 586 | - $suffix += 1; | |
| 587 | - $alt_slug = sprintf( '%s-%s', | |
| 588 | - bogo_truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ), | |
| 589 | - $suffix | |
| 590 | - ); | |
| 591 | - | |
| 592 | - $q_results = $q->query( array_merge( | |
| 593 | - $q_args, | |
| 594 | - array( 'name' => $alt_slug ) | |
| 595 | - ) ); | |
| 596 | - | |
| 597 | - $is_bad_slug = ! empty( $q_results ); | |
| 598 | - } | |
| 599 | - | |
| 600 | - $override_slug = $alt_slug; | |
| 601 | - } | |
| 602 | - | |
| 603 | - } else { | |
| 604 | - $q_args = array( | |
| 605 | - 'name' => $slug, | |
| 606 | - 'lang' => $locale, | |
| 607 | - 'post_type' => $post_type, | |
| 608 | - 'post__not_in' => array( $post_id ), | |
| 609 | - 'posts_per_page' => 1, | |
| 610 | - ); | |
| 611 | - | |
| 612 | - $is_bad_slug = in_array( $slug, $feeds ) || | |
| 613 | - 'embed' === $slug || | |
| 614 | - apply_filters( | |
| 615 | - 'wp_unique_post_slug_is_bad_flat_slug', false, | |
| 616 | - $slug, $post_type | |
| 617 | - ); | |
| 618 | - | |
| 619 | - if ( ! $is_bad_slug ) { | |
| 620 | - $post = get_post( $post_id ); | |
| 621 | - | |
| 622 | - if ( | |
| 623 | - 'post' === $post_type and | |
| 624 | - ( ! $post or $post->post_name !== $slug ) and | |
| 625 | - preg_match( '/^[0-9]+$/', $slug ) | |
| 626 | - ) { | |
| 627 | - $slug_num = intval( $slug ); | |
| 628 | - | |
| 629 | - if ( $slug_num ) { | |
| 630 | - $permastructs = array_values( array_filter( | |
| 631 | - explode( '/', get_option( 'permalink_structure' ) ) | |
| 632 | - ) ); | |
| 633 | - $postname_index = array_search( '%postname%', $permastructs ); | |
| 634 | - | |
| 635 | - $is_bad_slug = 0 === $postname_index || | |
| 636 | - ( | |
| 637 | - $postname_index && | |
| 638 | - '%year%' === $permastructs[$postname_index - 1] && | |
| 639 | - 13 > $slug_num | |
| 640 | - ) || | |
| 641 | - ( | |
| 642 | - $postname_index && | |
| 643 | - '%monthnum%' === $permastructs[$postname_index - 1] && | |
| 644 | - 32 > $slug_num | |
| 645 | - ); | |
| 646 | - } | |
| 647 | - } | |
| 648 | - } | |
| 649 | - | |
| 650 | - if ( ! $is_bad_slug ) { | |
| 651 | - $q_results = $q->query( $q_args ); | |
| 652 | - $is_bad_slug = ! empty( $q_results ); | |
| 653 | - } | |
| 654 | - | |
| 655 | - if ( $is_bad_slug ) { | |
| 656 | - $suffix = 1; | |
| 657 | - | |
| 658 | - while ( $is_bad_slug ) { | |
| 659 | - $suffix += 1; | |
| 660 | - $alt_slug = sprintf( '%s-%s', | |
| 661 | - bogo_truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ), | |
| 662 | - $suffix | |
| 663 | - ); | |
| 664 | - | |
| 665 | - $q_results = $q->query( array_merge( | |
| 666 | - $q_args, | |
| 667 | - array( 'name' => $alt_slug ) | |
| 668 | - ) ); | |
| 669 | - | |
| 670 | - $is_bad_slug = ! empty( $q_results ); | |
| 671 | - } | |
| 672 | - | |
| 673 | - $override_slug = $alt_slug; | |
| 674 | - } | |
| 675 | - } | |
| 676 | - | |
| 677 | - return $override_slug; | |
| 678 | -} | |
| 679 | - | |
| 680 | - | |
| 681 | -function bogo_truncate_post_slug( $slug, $length = 200 ) { | |
| 682 | - if ( strlen( $slug ) > $length ) { | |
| 683 | - $decoded_slug = urldecode( $slug ); | |
| 684 | - | |
| 685 | - if ( $decoded_slug === $slug ) { | |
| 686 | - $slug = substr( $slug, 0, $length ); | |
| 687 | - } else { | |
| 688 | - $slug = utf8_uri_encode( $decoded_slug, $length ); | |
| 689 | - } | |
| 690 | - } | |
| 691 | - | |
| 692 | - return rtrim( $slug, '-' ); | |
| 693 | -} | |
| 694 | - | |
| 695 | - | |
| 696 | -add_filter( | |
| 697 | - 'wp_sitemaps_posts_query_args', | |
| 698 | - 'bogo_sitemaps_posts_query_args', | |
| 699 | - 10, 2 | |
| 700 | -); | |
| 701 | - | |
| 702 | -function bogo_sitemaps_posts_query_args( $args, $post_type ) { | |
| 703 | - if ( bogo_is_localizable_post_type( $post_type ) ) { | |
| 704 | - $args['bogo_suppress_locale_query'] = true; | |
| 705 | - } | |
| 706 | - | |
| 707 | - return $args; | |
| 708 | 330 | } |