| @@ -29,23 +29,18 @@ | ||
| 29 | 29 | |
| 30 | 30 | function bogo_get_post_locale( $post_id ) { |
| 31 | 31 | $locale = get_post_meta( $post_id, '_locale', true ); |
| 32 | 32 | |
| 33 | - if ( empty( $locale ) ) { | |
| 33 | + if ( empty( $locale ) ) | |
| 34 | 34 | $locale = bogo_get_default_locale(); |
| 35 | - } | |
| 36 | 35 | |
| 37 | 36 | return $locale; |
| 38 | 37 | } |
| 39 | 38 | |
| 40 | 39 | function bogo_localizable_post_types() { |
| 41 | - $localizable = apply_filters( 'bogo_localizable_post_types', | |
| 42 | - array( 'post', 'page' ) ); | |
| 40 | + $localizable = array( 'post', 'page' ); | |
| 43 | 41 | |
| 44 | - $localizable = array_diff( $localizable, | |
| 45 | - array( 'attachment', 'revision', 'nav_menu_item' ) ); | |
| 46 | - | |
| 47 | - return $localizable; | |
| 42 | + return apply_filters( 'bogo_localizable_post_types', $localizable ); | |
| 48 | 43 | } |
| 49 | 44 | |
| 50 | 45 | function bogo_is_localizable_post_type( $post_type ) { |
| 51 | 46 | return ! empty( $post_type ) && in_array( $post_type, bogo_localizable_post_types() ); |
| @@ -53,19 +48,17 @@ | ||
| 53 | 48 | |
| 54 | 49 | function bogo_get_post_translations( $post_id = 0 ) { |
| 55 | 50 | $post = get_post( $post_id ); |
| 56 | 51 | |
| 57 | - if ( ! $post ) { | |
| 52 | + if ( ! $post ) | |
| 58 | 53 | return false; |
| 59 | - } | |
| 60 | 54 | |
| 61 | 55 | if ( 'auto-draft' == $post->post_status ) { |
| 62 | 56 | if ( ! empty( $_REQUEST['original_post'] ) ) { |
| 63 | 57 | $original = get_post_meta( $_REQUEST['original_post'], '_original_post', true ); |
| 64 | 58 | |
| 65 | - if ( empty( $original ) ) { | |
| 59 | + if ( empty( $original ) ) | |
| 66 | 60 | $original = (int) $_REQUEST['original_post']; |
| 67 | - } | |
| 68 | 61 | } else { |
| 69 | 62 | return false; |
| 70 | 63 | } |
| 71 | 64 | } else { |
| @@ -71,11 +64,10 @@ | ||
| 71 | 64 | } else { |
| 72 | 65 | $original = get_post_meta( $post->ID, '_original_post', true ); |
| 73 | 66 | } |
| 74 | 67 | |
| 75 | - if ( empty( $original ) ) { | |
| 68 | + if ( empty( $original ) ) | |
| 76 | 69 | $original = $post->ID; |
| 77 | - } | |
| 78 | 70 | |
| 79 | 71 | $args = array( |
| 80 | 72 | 'bogo_suppress_locale_query' => true, |
| 81 | 73 | 'posts_per_page' => -1, |
| @@ -96,42 +88,28 @@ | ||
| 96 | 88 | $translations[$locale] = get_post( $original ); |
| 97 | 89 | } |
| 98 | 90 | |
| 99 | 91 | foreach ( $posts as $p ) { |
| 100 | - if ( $p->ID == $post->ID ) { | |
| 92 | + if ( $p->ID == $post->ID ) | |
| 101 | 93 | continue; |
| 102 | - } | |
| 103 | 94 | |
| 104 | 95 | $locale = bogo_get_post_locale( $p->ID ); |
| 105 | 96 | |
| 106 | - if ( ! bogo_is_available_locale( $locale ) ) { | |
| 97 | + if ( ! bogo_is_available_locale( $locale ) ) | |
| 107 | 98 | continue; |
| 108 | - } | |
| 109 | 99 | |
| 110 | - if ( ! isset( $translations[$locale] ) ) { | |
| 100 | + if ( ! isset( $translations[$locale] ) ) | |
| 111 | 101 | $translations[$locale] = $p; |
| 112 | - } | |
| 113 | 102 | } |
| 114 | 103 | |
| 115 | 104 | return array_filter( $translations ); |
| 116 | 105 | } |
| 117 | 106 | |
| 118 | -function bogo_get_post_translation( $post_id, $locale ) { | |
| 119 | - $translations = bogo_get_post_translations( $post_id ); | |
| 120 | - | |
| 121 | - if ( isset( $translations[$locale] ) ) { | |
| 122 | - return $translations[$locale]; | |
| 123 | - } | |
| 124 | - | |
| 125 | - return false; | |
| 126 | -} | |
| 127 | - | |
| 128 | 107 | function bogo_get_page_by_path( $page_path, $locale = null, $post_type = 'page' ) { |
| 129 | 108 | global $wpdb; |
| 130 | 109 | |
| 131 | - if ( ! bogo_is_available_locale( $locale ) ) { | |
| 110 | + if ( ! bogo_is_available_locale( $locale ) ) | |
| 132 | 111 | $locale = bogo_get_default_locale(); |
| 133 | - } | |
| 134 | 112 | |
| 135 | 113 | $page_path = rawurlencode( urldecode( $page_path ) ); |
| 136 | 114 | $page_path = str_replace( '%2F', '/', $page_path ); |
| 137 | 115 | $page_path = str_replace( '%20', ' ', $page_path ); |
| @@ -160,11 +138,10 @@ | ||
| 160 | 138 | |
| 161 | 139 | $foundid = 0; |
| 162 | 140 | |
| 163 | 141 | foreach ( (array) $pages as $page ) { |
| 164 | - if ( $page->post_name != $revparts[0] ) { | |
| 142 | + if ( $page->post_name != $revparts[0] ) | |
| 165 | 143 | continue; |
| 166 | - } | |
| 167 | 144 | |
| 168 | 145 | $count = 0; |
| 169 | 146 | $p = $page; |
| 170 | 147 | |
| @@ -171,12 +148,10 @@ | ||
| 171 | 148 | while ( $p->post_parent != 0 && isset( $pages[$p->post_parent] ) ) { |
| 172 | 149 | $count++; |
| 173 | 150 | $parent = $pages[$p->post_parent]; |
| 174 | 151 | |
| 175 | - if ( ! isset( $revparts[$count] ) | |
| 176 | - || $parent->post_name != $revparts[$count] ) { | |
| 152 | + if ( ! isset( $revparts[$count] ) || $parent->post_name != $revparts[$count] ) | |
| 177 | 153 | break; |
| 178 | - } | |
| 179 | 154 | |
| 180 | 155 | $p = $parent; |
| 181 | 156 | } |
| 182 | 157 | |
| @@ -187,144 +162,11 @@ | ||
| 187 | 162 | break; |
| 188 | 163 | } |
| 189 | 164 | } |
| 190 | 165 | |
| 191 | - if ( $foundid ) { | |
| 166 | + if ( $foundid ) | |
| 192 | 167 | return get_page( $foundid ); |
| 193 | - } | |
| 194 | 168 | |
| 195 | 169 | return null; |
| 196 | 170 | } |
| 197 | 171 | |
| 198 | -function bogo_duplicate_post( $original_post, $locale ) { | |
| 199 | - $original_post = get_post( $original_post ); | |
| 200 | - | |
| 201 | - if ( ! $original_post | |
| 202 | - || ! bogo_is_localizable_post_type( get_post_type( $original_post ) ) | |
| 203 | - || 'auto-draft' == get_post_status( $original_post ) ) { | |
| 204 | - return false; | |
| 205 | - } | |
| 206 | - | |
| 207 | - if ( ! bogo_is_available_locale( $locale ) | |
| 208 | - || bogo_get_post_locale( $original_post->ID ) == $locale ) { | |
| 209 | - return false; | |
| 210 | - } | |
| 211 | - | |
| 212 | - if ( bogo_get_post_translation( $original_post->ID, $locale ) ) { | |
| 213 | - return false; | |
| 214 | - } | |
| 215 | - | |
| 216 | - $postarr = array( | |
| 217 | - 'post_content' => $original_post->post_content, | |
| 218 | - 'post_title' => $original_post->post_title, | |
| 219 | - 'post_name' => $original_post->post_name, | |
| 220 | - 'post_excerpt' => $original_post->post_excerpt, | |
| 221 | - 'post_status' => 'draft', | |
| 222 | - 'post_type' => $original_post->post_type, | |
| 223 | - 'comment_status' => $original_post->comment_status, | |
| 224 | - 'ping_status' => $original_post->ping_status, | |
| 225 | - 'post_password' => $original_post->post_password, | |
| 226 | - 'post_content_filtered' => $original_post->post_content_filtered, | |
| 227 | - 'post_parent' => $original_post->post_parent, | |
| 228 | - 'menu_order' => $original_post->menu_order, | |
| 229 | - 'post_mime_type' => $original_post->post_mime_type, | |
| 230 | - 'tax_input' => array(), | |
| 231 | - 'meta_input' => array() ); | |
| 232 | - | |
| 233 | - if ( ! empty( $original_post->post_parent ) ) { | |
| 234 | - $parent_translation = bogo_get_post_translation( | |
| 235 | - $original_post->post_parent, $locale ); | |
| 236 | - | |
| 237 | - if ( $parent_translation ) { | |
| 238 | - $postarr['post_parent'] = $parent_translation->ID; | |
| 239 | - } | |
| 240 | - } | |
| 241 | - | |
| 242 | - if ( $taxonomies = get_object_taxonomies( $original_post ) ) { | |
| 243 | - foreach ( $taxonomies as $taxonomy ) { | |
| 244 | - $terms = wp_get_post_terms( $original_post->ID, | |
| 245 | - $taxonomy, array( 'fields' => 'ids' ) ); | |
| 246 | - | |
| 247 | - if ( $terms && ! is_wp_error( $terms ) ) { | |
| 248 | - $postarr['tax_input'][$taxonomy] = $terms; | |
| 249 | - } | |
| 250 | - } | |
| 251 | - } | |
| 252 | - | |
| 253 | - if ( $post_metas = get_post_meta( $original_post->ID ) ) { | |
| 254 | - $postarr['meta_input'] = $post_metas; | |
| 255 | - } | |
| 256 | - | |
| 257 | - $postarr = apply_filters( 'bogo_duplicate_post', | |
| 258 | - $postarr, $original_post, $locale ); | |
| 259 | - | |
| 260 | - if ( $taxonomies = $postarr['tax_input'] ) { | |
| 261 | - $postarr['tax_input'] = array(); | |
| 262 | - } | |
| 263 | - | |
| 264 | - if ( $post_metas = $postarr['meta_input'] ) { | |
| 265 | - $postarr['meta_input'] = array(); | |
| 266 | - } | |
| 267 | - | |
| 268 | - $new_post_id = wp_insert_post( $postarr ); | |
| 269 | - | |
| 270 | - if ( $new_post_id ) { | |
| 271 | - if ( $taxonomies ) { | |
| 272 | - foreach ( $taxonomies as $taxonomy => $terms ) { | |
| 273 | - wp_set_post_terms( $new_post_id, $terms, $taxonomy ); | |
| 274 | - } | |
| 275 | - } | |
| 276 | - | |
| 277 | - if ( $post_metas ) { | |
| 278 | - foreach ( $post_metas as $meta_key => $meta_values ) { | |
| 279 | - if ( in_array( $meta_key, array( '_locale', '_original_post' ) ) ) { | |
| 280 | - continue; | |
| 281 | - } | |
| 282 | - | |
| 283 | - foreach ( (array) $meta_values as $meta_value ) { | |
| 284 | - add_post_meta( $new_post_id, $meta_key, $meta_value ); | |
| 285 | - } | |
| 286 | - } | |
| 287 | - } | |
| 288 | - | |
| 289 | - update_post_meta( $new_post_id, '_locale', $locale ); | |
| 290 | - | |
| 291 | - $meta_original_post = get_post_meta( $original_post->ID, | |
| 292 | - '_original_post', true ); | |
| 293 | - | |
| 294 | - if ( $meta_original_post ) { | |
| 295 | - update_post_meta( $new_post_id, '_original_post', $meta_original_post ); | |
| 296 | - } else { | |
| 297 | - update_post_meta( $new_post_id, '_original_post', $original_post->ID ); | |
| 298 | - } | |
| 299 | - } | |
| 300 | - | |
| 301 | - return $new_post_id; | |
| 302 | -} | |
| 303 | - | |
| 304 | -add_filter( 'get_pages', 'bogo_get_pages', 10, 2 ); | |
| 305 | - | |
| 306 | -function bogo_get_pages( $pages, $args ) { | |
| 307 | - if ( is_admin() | |
| 308 | - || ! bogo_is_localizable_post_type( $args['post_type'] ) | |
| 309 | - || ! empty( $args['bogo_suppress_locale_query'] ) ) { | |
| 310 | - return $pages; | |
| 311 | - } | |
| 312 | - | |
| 313 | - $locale = isset( $args['lang'] ) ? $args['lang'] : get_locale(); | |
| 314 | - | |
| 315 | - if ( ! bogo_is_available_locale( $locale ) ) { | |
| 316 | - return $pages; | |
| 317 | - } | |
| 318 | - | |
| 319 | - $new_pages = array(); | |
| 320 | - | |
| 321 | - foreach ( (array) $pages as $page ) { | |
| 322 | - $post_locale = bogo_get_post_locale( $page->ID ); | |
| 323 | - | |
| 324 | - if ( $post_locale == $locale ) { | |
| 325 | - $new_pages[] = $page; | |
| 326 | - } | |
| 327 | - } | |
| 328 | - | |
| 329 | - return $new_pages; | |
| 330 | -} | |
| 172 | +?> | |