| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -add_action( 'parse_query', 'bogo_parse_query', 10, 1 ); | |
| 3 | +add_action( 'parse_query', 'bogo_parse_query' ); | |
| 4 | 4 | |
| 5 | 5 | function bogo_parse_query( $query ) { |
| 6 | 6 | $qv = &$query->query_vars; |
| 7 | 7 | |
| @@ -8,19 +8,11 @@ | ||
| 8 | 8 | if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) { |
| 9 | 9 | return; |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | - if ( $query->is_preview() and ( $qv['page_id'] or $qv['p'] ) ) { | |
| 13 | - $qv['bogo_suppress_locale_query'] = true; | |
| 14 | - return; | |
| 15 | - } | |
| 12 | + if ( isset( $qv['post_type'] ) && 'any' != $qv['post_type'] ) { | |
| 13 | + $localizable = array_filter( (array) $qv['post_type'], 'bogo_is_localizable_post_type' ); | |
| 16 | 14 | |
| 17 | - if ( isset( $qv['post_type'] ) and 'any' !== $qv['post_type'] ) { | |
| 18 | - $localizable = array_filter( | |
| 19 | - (array) $qv['post_type'], | |
| 20 | - 'bogo_is_localizable_post_type' | |
| 21 | - ); | |
| 22 | - | |
| 23 | 15 | if ( empty( $localizable ) ) { |
| 24 | 16 | $qv['bogo_suppress_locale_query'] = true; |
| 25 | 17 | return; |
| 26 | 18 | } |
| @@ -25,9 +17,9 @@ | ||
| 25 | 17 | return; |
| 26 | 18 | } |
| 27 | 19 | } |
| 28 | 20 | |
| 29 | - $lang = $qv['lang'] ?? ''; | |
| 21 | + $lang = isset( $qv['lang'] ) ? $qv['lang'] : ''; | |
| 30 | 22 | |
| 31 | 23 | if ( is_admin() ) { |
| 32 | 24 | $locale = $lang; |
| 33 | 25 | } else { |
| @@ -41,9 +33,9 @@ | ||
| 41 | 33 | $locale = bogo_get_default_locale(); |
| 42 | 34 | } |
| 43 | 35 | } |
| 44 | 36 | |
| 45 | - if ( empty( $locale ) or ! bogo_is_available_locale( $locale ) ) { | |
| 37 | + if ( empty( $locale ) || ! bogo_is_available_locale( $locale ) ) { | |
| 46 | 38 | $qv['bogo_suppress_locale_query'] = true; |
| 47 | 39 | return; |
| 48 | 40 | } |
| 49 | 41 | |
| @@ -52,21 +44,16 @@ | ||
| 52 | 44 | if ( is_admin() ) { |
| 53 | 45 | return; |
| 54 | 46 | } |
| 55 | 47 | |
| 56 | - if ( | |
| 57 | - $query->is_home and | |
| 58 | - 'page' === get_option( 'show_on_front' ) and | |
| 59 | - get_option( 'page_on_front' ) | |
| 60 | - ) { | |
| 48 | + if ( $query->is_home && 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ) { | |
| 61 | 49 | $query_keys = array_keys( wp_parse_args( $query->query ) ); |
| 62 | - $query_keys = array_diff( | |
| 63 | - $query_keys, | |
| 64 | - array( 'preview', 'page', 'paged', 'cpage', 'lang' ) | |
| 65 | - ); | |
| 50 | + $query_keys = array_diff( $query_keys, | |
| 51 | + array( 'preview', 'page', 'paged', 'cpage', 'lang' ) ); | |
| 66 | 52 | |
| 67 | 53 | if ( empty( $query_keys ) ) { |
| 68 | 54 | $query->is_page = true; |
| 55 | + $query->is_singular = true; | |
| 69 | 56 | $query->is_home = false; |
| 70 | 57 | $qv['page_id'] = get_option( 'page_on_front' ); |
| 71 | 58 | |
| 72 | 59 | if ( ! empty( $qv['paged'] ) ) { |
| @@ -82,16 +69,13 @@ | ||
| 82 | 69 | if ( ! empty( $query->queried_object ) ) { |
| 83 | 70 | $query->queried_object_id = (int) $query->queried_object->ID; |
| 84 | 71 | } else { |
| 85 | 72 | unset( $query->queried_object ); |
| 86 | - unset( $query->queried_object_id ); | |
| 87 | 73 | } |
| 88 | 74 | |
| 89 | - if ( | |
| 90 | - 'page' === get_option( 'show_on_front' ) and | |
| 91 | - isset( $query->queried_object_id ) and | |
| 92 | - $query->queried_object_id == get_option( 'page_for_posts' ) | |
| 93 | - ) { | |
| 75 | + if ( 'page' == get_option( 'show_on_front' ) | |
| 76 | + && isset( $query->queried_object_id ) | |
| 77 | + && $query->queried_object_id == get_option( 'page_for_posts' ) ) { | |
| 94 | 78 | $query->is_page = false; |
| 95 | 79 | $query->is_home = true; |
| 96 | 80 | $query->is_posts_page = true; |
| 97 | 81 | } |
| @@ -96,41 +80,23 @@ | ||
| 96 | 80 | $query->is_posts_page = true; |
| 97 | 81 | } |
| 98 | 82 | } |
| 99 | 83 | |
| 100 | - if ( | |
| 101 | - isset( $qv['post_type'] ) and | |
| 102 | - 'any' !== $qv['post_type'] and | |
| 103 | - ! is_array( $qv['post_type'] ) and | |
| 104 | - ! empty( $qv['name'] ) | |
| 105 | - ) { | |
| 84 | + if ( isset( $qv['post_type'] ) | |
| 85 | + && 'any' != $qv['post_type'] | |
| 86 | + && ! is_array( $qv['post_type'] ) | |
| 87 | + && '' != $qv['name'] ) { | |
| 106 | 88 | $query->queried_object = bogo_get_page_by_path( |
| 107 | - $qv['name'], $locale, $qv['post_type'] | |
| 108 | - ); | |
| 89 | + $qv['name'], $locale, $qv['post_type'] ); | |
| 109 | 90 | |
| 110 | 91 | if ( ! empty( $query->queried_object ) ) { |
| 111 | 92 | $query->queried_object_id = (int) $query->queried_object->ID; |
| 112 | 93 | } else { |
| 113 | 94 | unset( $query->queried_object ); |
| 114 | - unset( $query->queried_object_id ); | |
| 115 | 95 | } |
| 116 | 96 | } |
| 117 | - | |
| 118 | - if ( | |
| 119 | - $query->is_posts_page and | |
| 120 | - ( ! isset( $qv['withcomments'] ) or ! $qv['withcomments'] ) | |
| 121 | - ) { | |
| 122 | - $query->is_comment_feed = false; | |
| 123 | - } | |
| 124 | - | |
| 125 | - $query->is_singular = | |
| 126 | - ( $query->is_single || $query->is_page || $query->is_attachment ); | |
| 127 | - | |
| 128 | - $query->is_embed = | |
| 129 | - $query->is_embed && ( $query->is_singular || $query->is_404 ); | |
| 130 | 97 | } |
| 131 | 98 | |
| 132 | - | |
| 133 | 99 | add_filter( 'posts_join', 'bogo_posts_join', 10, 2 ); |
| 134 | 100 | |
| 135 | 101 | function bogo_posts_join( $join, $query ) { |
| 136 | 102 | global $wpdb; |
| @@ -140,9 +106,9 @@ | ||
| 140 | 106 | if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) { |
| 141 | 107 | return $join; |
| 142 | 108 | } |
| 143 | 109 | |
| 144 | - $locale = $qv['lang'] ?? ''; | |
| 110 | + $locale = empty( $qv['lang'] ) ? '' : $qv['lang']; | |
| 145 | 111 | |
| 146 | 112 | if ( ! bogo_is_available_locale( $locale ) ) { |
| 147 | 113 | return $join; |
| 148 | 114 | } |
| @@ -155,9 +121,8 @@ | ||
| 155 | 121 | |
| 156 | 122 | return $join; |
| 157 | 123 | } |
| 158 | 124 | |
| 159 | - | |
| 160 | 125 | add_filter( 'posts_where', 'bogo_posts_where', 10, 2 ); |
| 161 | 126 | |
| 162 | 127 | function bogo_posts_where( $where, $query ) { |
| 163 | 128 | global $wpdb; |
| @@ -167,9 +132,9 @@ | ||
| 167 | 132 | if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) { |
| 168 | 133 | return $where; |
| 169 | 134 | } |
| 170 | 135 | |
| 171 | - $locale = $qv['lang'] ?? ''; | |
| 136 | + $locale = empty( $qv['lang'] ) ? '' : $qv['lang']; | |
| 172 | 137 | |
| 173 | 138 | if ( ! bogo_is_available_locale( $locale ) ) { |
| 174 | 139 | return $where; |
| 175 | 140 | } |
| @@ -190,93 +155,70 @@ | ||
| 190 | 155 | |
| 191 | 156 | return $where; |
| 192 | 157 | } |
| 193 | 158 | |
| 159 | +add_filter( 'option_sticky_posts', 'bogo_option_sticky_posts' ); | |
| 194 | 160 | |
| 195 | -add_filter( 'option_sticky_posts', 'bogo_option_sticky_posts', 10, 1 ); | |
| 161 | +function bogo_option_sticky_posts( $posts ) { | |
| 162 | + if ( is_admin() ) { | |
| 163 | + return $posts; | |
| 164 | + } | |
| 196 | 165 | |
| 197 | -function bogo_option_sticky_posts( $posts ) { | |
| 198 | - if ( is_home() ) { | |
| 199 | - $locale = get_locale(); | |
| 166 | + $locale = get_locale(); | |
| 200 | 167 | |
| 201 | - $posts = array_filter( $posts, | |
| 202 | - static function ( $post_id ) use ( $locale ) { | |
| 203 | - return bogo_get_post_locale( $post_id ) === $locale; | |
| 204 | - } | |
| 205 | - ); | |
| 168 | + foreach ( $posts as $key => $post_id ) { | |
| 169 | + if ( $locale != bogo_get_post_locale( $post_id ) ) | |
| 170 | + unset( $posts[$key] ); | |
| 206 | 171 | } |
| 207 | 172 | |
| 208 | 173 | return $posts; |
| 209 | 174 | } |
| 210 | 175 | |
| 176 | +add_filter( 'option_page_on_front', 'bogo_get_local_post' ); | |
| 177 | +add_filter( 'option_page_for_posts', 'bogo_get_local_post' ); | |
| 211 | 178 | |
| 212 | -add_filter( 'option_page_on_front', 'bogo_get_local_post', 10, 1 ); | |
| 213 | -add_filter( 'option_page_for_posts', 'bogo_get_local_post', 10, 1 ); | |
| 214 | - | |
| 215 | 179 | function bogo_get_local_post( $post_id ) { |
| 216 | 180 | global $wpdb; |
| 217 | 181 | |
| 218 | - if ( is_admin() or empty( $post_id ) ) { | |
| 182 | + if ( is_admin() || empty( $post_id ) ) { | |
| 219 | 183 | return $post_id; |
| 220 | 184 | } |
| 221 | 185 | |
| 222 | 186 | $post_type = get_post_type( $post_id ); |
| 223 | 187 | |
| 224 | - if ( | |
| 225 | - ! post_type_exists( $post_type ) or | |
| 226 | - ! bogo_is_localizable_post_type( $post_type ) | |
| 227 | - ) { | |
| 188 | + if ( ! post_type_exists( $post_type ) | |
| 189 | + || ! bogo_is_localizable_post_type( $post_type ) ) { | |
| 228 | 190 | return $post_id; |
| 229 | 191 | } |
| 230 | 192 | |
| 231 | 193 | $locale = get_locale(); |
| 232 | 194 | |
| 233 | - if ( bogo_get_post_locale( $post_id ) === $locale ) { | |
| 195 | + if ( bogo_get_post_locale( $post_id ) == $locale ) { | |
| 234 | 196 | return $post_id; |
| 235 | 197 | } |
| 236 | 198 | |
| 237 | - $locale_query = $wpdb->prepare( | |
| 238 | - "pm2.meta_value LIKE %s", | |
| 239 | - $locale | |
| 240 | - ); | |
| 199 | + $original = get_post_meta( $post_id, '_original_post', true ); | |
| 241 | 200 | |
| 242 | - if ( bogo_is_default_locale( $locale ) ) { | |
| 243 | - $locale_query = $wpdb->prepare( | |
| 244 | - "(pm2.meta_value LIKE %s OR pm2.meta_id IS NULL)", | |
| 245 | - $locale | |
| 246 | - ); | |
| 201 | + if ( empty( $original ) ) { | |
| 202 | + $original = $post_id; | |
| 247 | 203 | } |
| 248 | 204 | |
| 249 | - $original_post = get_post_meta( $post_id, '_original_post', true ); | |
| 205 | + $q = "SELECT ID FROM $wpdb->posts AS posts"; | |
| 206 | + $q .= " LEFT JOIN $wpdb->postmeta AS pm1"; | |
| 207 | + $q .= " ON posts.ID = pm1.post_id AND pm1.meta_key = '_original_post'"; | |
| 208 | + $q .= " LEFT JOIN $wpdb->postmeta AS pm2"; | |
| 209 | + $q .= " ON posts.ID = pm2.post_id AND pm2.meta_key = '_locale'"; | |
| 210 | + $q .= " WHERE 1=1"; | |
| 211 | + $q .= " AND post_status = 'publish'"; | |
| 212 | + $q .= $wpdb->prepare( " AND post_type = %s", $post_type ); | |
| 213 | + $q .= $wpdb->prepare( " AND (ID = %d OR pm1.meta_value = %d)", | |
| 214 | + $original, $original ); | |
| 215 | + $q .= " AND (1=0"; | |
| 216 | + $q .= $wpdb->prepare( " OR pm2.meta_value LIKE %s", $locale ); | |
| 217 | + $q .= bogo_is_default_locale( $locale ) ? " OR pm2.meta_id IS NULL" : ""; | |
| 218 | + $q .= ")"; | |
| 250 | 219 | |
| 251 | - // For back-compat | |
| 252 | - if ( empty( $original_post ) ) { | |
| 253 | - $original_post = $post_id; | |
| 254 | - } | |
| 255 | - | |
| 256 | - $original_post_query = $wpdb->prepare( | |
| 257 | - "pm1.meta_value = %s", | |
| 258 | - $original_post | |
| 259 | - ); | |
| 260 | - | |
| 261 | - // For back-compat | |
| 262 | - if ( is_int( $original_post ) ) { | |
| 263 | - $original_post_query = $wpdb->prepare( | |
| 264 | - "(ID = %d OR pm1.meta_value = %d)", | |
| 265 | - $original_post, | |
| 266 | - $original_post | |
| 267 | - ); | |
| 268 | - } | |
| 269 | - | |
| 270 | - $translation = $wpdb->get_var( $wpdb->prepare( | |
| 271 | - "SELECT ID FROM %i AS posts LEFT JOIN %i AS pm1 ON posts.ID = pm1.post_id AND pm1.meta_key = '_original_post' LEFT JOIN %i AS pm2 ON posts.ID = pm2.post_id AND pm2.meta_key = '_locale' WHERE post_status = 'publish' AND post_type = %s AND $locale_query AND $original_post_query", | |
| 272 | - $wpdb->posts, | |
| 273 | - $wpdb->postmeta, | |
| 274 | - $wpdb->postmeta, | |
| 275 | - $post_type, | |
| 276 | - ) ); | |
| 277 | - | |
| 278 | - $translation = absint( $translation ); | |
| 220 | + $translation = absint( $wpdb->get_var( $q ) ); | |
| 279 | 221 | |
| 280 | 222 | if ( $translation ) { |
| 281 | 223 | return $translation; |
| 282 | 224 | } |