| @@ -29,18 +29,23 @@ | ||
| 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 | + } | |
| 35 | 36 | |
| 36 | 37 | return $locale; |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | function bogo_localizable_post_types() { |
| 40 | - $localizable = array( 'post', 'page' ); | |
| 41 | + $localizable = apply_filters( 'bogo_localizable_post_types', | |
| 42 | + array( 'post', 'page' ) ); | |
| 41 | 43 | |
| 42 | - return apply_filters( 'bogo_localizable_post_types', $localizable ); | |
| 44 | + $localizable = array_diff( $localizable, | |
| 45 | + array( 'attachment', 'revision', 'nav_menu_item' ) ); | |
| 46 | + | |
| 47 | + return $localizable; | |
| 43 | 48 | } |
| 44 | 49 | |
| 45 | 50 | function bogo_is_localizable_post_type( $post_type ) { |
| 46 | 51 | return ! empty( $post_type ) && in_array( $post_type, bogo_localizable_post_types() ); |
| @@ -48,17 +53,19 @@ | ||
| 48 | 53 | |
| 49 | 54 | function bogo_get_post_translations( $post_id = 0 ) { |
| 50 | 55 | $post = get_post( $post_id ); |
| 51 | 56 | |
| 52 | - if ( ! $post ) | |
| 57 | + if ( ! $post ) { | |
| 53 | 58 | return false; |
| 59 | + } | |
| 54 | 60 | |
| 55 | 61 | if ( 'auto-draft' == $post->post_status ) { |
| 56 | 62 | if ( ! empty( $_REQUEST['original_post'] ) ) { |
| 57 | 63 | $original = get_post_meta( $_REQUEST['original_post'], '_original_post', true ); |
| 58 | 64 | |
| 59 | - if ( empty( $original ) ) | |
| 65 | + if ( empty( $original ) ) { | |
| 60 | 66 | $original = (int) $_REQUEST['original_post']; |
| 67 | + } | |
| 61 | 68 | } else { |
| 62 | 69 | return false; |
| 63 | 70 | } |
| 64 | 71 | } else { |
| @@ -64,10 +71,11 @@ | ||
| 64 | 71 | } else { |
| 65 | 72 | $original = get_post_meta( $post->ID, '_original_post', true ); |
| 66 | 73 | } |
| 67 | 74 | |
| 68 | - if ( empty( $original ) ) | |
| 75 | + if ( empty( $original ) ) { | |
| 69 | 76 | $original = $post->ID; |
| 77 | + } | |
| 70 | 78 | |
| 71 | 79 | $args = array( |
| 72 | 80 | 'bogo_suppress_locale_query' => true, |
| 73 | 81 | 'posts_per_page' => -1, |
| @@ -88,28 +96,42 @@ | ||
| 88 | 96 | $translations[$locale] = get_post( $original ); |
| 89 | 97 | } |
| 90 | 98 | |
| 91 | 99 | foreach ( $posts as $p ) { |
| 92 | - if ( $p->ID == $post->ID ) | |
| 100 | + if ( $p->ID == $post->ID ) { | |
| 93 | 101 | continue; |
| 102 | + } | |
| 94 | 103 | |
| 95 | 104 | $locale = bogo_get_post_locale( $p->ID ); |
| 96 | 105 | |
| 97 | - if ( ! bogo_is_available_locale( $locale ) ) | |
| 106 | + if ( ! bogo_is_available_locale( $locale ) ) { | |
| 98 | 107 | continue; |
| 108 | + } | |
| 99 | 109 | |
| 100 | - if ( ! isset( $translations[$locale] ) ) | |
| 110 | + if ( ! isset( $translations[$locale] ) ) { | |
| 101 | 111 | $translations[$locale] = $p; |
| 112 | + } | |
| 102 | 113 | } |
| 103 | 114 | |
| 104 | 115 | return array_filter( $translations ); |
| 105 | 116 | } |
| 106 | 117 | |
| 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 | + | |
| 107 | 128 | function bogo_get_page_by_path( $page_path, $locale = null, $post_type = 'page' ) { |
| 108 | 129 | global $wpdb; |
| 109 | 130 | |
| 110 | - if ( ! bogo_is_available_locale( $locale ) ) | |
| 131 | + if ( ! bogo_is_available_locale( $locale ) ) { | |
| 111 | 132 | $locale = bogo_get_default_locale(); |
| 133 | + } | |
| 112 | 134 | |
| 113 | 135 | $page_path = rawurlencode( urldecode( $page_path ) ); |
| 114 | 136 | $page_path = str_replace( '%2F', '/', $page_path ); |
| 115 | 137 | $page_path = str_replace( '%20', ' ', $page_path ); |
| @@ -138,10 +160,11 @@ | ||
| 138 | 160 | |
| 139 | 161 | $foundid = 0; |
| 140 | 162 | |
| 141 | 163 | foreach ( (array) $pages as $page ) { |
| 142 | - if ( $page->post_name != $revparts[0] ) | |
| 164 | + if ( $page->post_name != $revparts[0] ) { | |
| 143 | 165 | continue; |
| 166 | + } | |
| 144 | 167 | |
| 145 | 168 | $count = 0; |
| 146 | 169 | $p = $page; |
| 147 | 170 | |
| @@ -148,10 +171,12 @@ | ||
| 148 | 171 | while ( $p->post_parent != 0 && isset( $pages[$p->post_parent] ) ) { |
| 149 | 172 | $count++; |
| 150 | 173 | $parent = $pages[$p->post_parent]; |
| 151 | 174 | |
| 152 | - if ( ! isset( $revparts[$count] ) || $parent->post_name != $revparts[$count] ) | |
| 175 | + if ( ! isset( $revparts[$count] ) | |
| 176 | + || $parent->post_name != $revparts[$count] ) { | |
| 153 | 177 | break; |
| 178 | + } | |
| 154 | 179 | |
| 155 | 180 | $p = $parent; |
| 156 | 181 | } |
| 157 | 182 | |
| @@ -162,11 +187,144 @@ | ||
| 162 | 187 | break; |
| 163 | 188 | } |
| 164 | 189 | } |
| 165 | 190 | |
| 166 | - if ( $foundid ) | |
| 191 | + if ( $foundid ) { | |
| 167 | 192 | return get_page( $foundid ); |
| 193 | + } | |
| 168 | 194 | |
| 169 | 195 | return null; |
| 170 | 196 | } |
| 171 | 197 | |
| 172 | -?> | |
| 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 | +} | |