| 1 |
<?php |
| 2 |
|
| 3 |
/* Post Template */ |
| 4 |
|
| 5 |
add_filter( 'body_class', 'bogo_body_class', 10, 2 ); |
| 6 |
|
| 7 |
function bogo_body_class( $classes, $class ) { |
| 8 |
$locale = bogo_language_tag( get_locale() ); |
| 9 |
$locale = esc_attr( $locale ); |
| 10 |
|
| 11 |
if ( $locale && ! in_array( $locale, $classes ) ) |
| 12 |
$classes[] = $locale; |
| 13 |
|
| 14 |
return $classes; |
| 15 |
} |
| 16 |
|
| 17 |
add_filter( 'post_class', 'bogo_post_class', 10, 3 ); |
| 18 |
|
| 19 |
function bogo_post_class( $classes, $class, $post_id ) { |
| 20 |
$locale = bogo_get_post_locale( $post_id ); |
| 21 |
$locale = bogo_language_tag( $locale ); |
| 22 |
$locale = esc_attr( $locale ); |
| 23 |
|
| 24 |
if ( $locale && ! in_array( $locale, $classes ) ) |
| 25 |
$classes[] = $locale; |
| 26 |
|
| 27 |
return $classes; |
| 28 |
} |
| 29 |
|
| 30 |
function bogo_get_post_locale( $post_id ) { |
| 31 |
$locale = get_post_meta( $post_id, '_locale', true ); |
| 32 |
|
| 33 |
if ( empty( $locale ) ) |
| 34 |
$locale = bogo_get_default_locale(); |
| 35 |
|
| 36 |
return $locale; |
| 37 |
} |
| 38 |
|
| 39 |
function bogo_localizable_post_types() { |
| 40 |
$localizable = array( 'post', 'page' ); |
| 41 |
|
| 42 |
return apply_filters( 'bogo_localizable_post_types', $localizable ); |
| 43 |
} |
| 44 |
|
| 45 |
function bogo_is_localizable_post_type( $post_type ) { |
| 46 |
return ! empty( $post_type ) && in_array( $post_type, bogo_localizable_post_types() ); |
| 47 |
} |
| 48 |
|
| 49 |
function bogo_get_post_translations( $post_id = 0 ) { |
| 50 |
$post = get_post( $post_id ); |
| 51 |
|
| 52 |
if ( ! $post ) |
| 53 |
return false; |
| 54 |
|
| 55 |
if ( 'auto-draft' == $post->post_status ) { |
| 56 |
if ( ! empty( $_REQUEST['original_post'] ) ) { |
| 57 |
$original = get_post_meta( $_REQUEST['original_post'], '_original_post', true ); |
| 58 |
|
| 59 |
if ( empty( $original ) ) |
| 60 |
$original = (int) $_REQUEST['original_post']; |
| 61 |
} else { |
| 62 |
return false; |
| 63 |
} |
| 64 |
} else { |
| 65 |
$original = get_post_meta( $post->ID, '_original_post', true ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( empty( $original ) ) |
| 69 |
$original = $post->ID; |
| 70 |
|
| 71 |
$args = array( |
| 72 |
'bogo_suppress_locale_query' => true, |
| 73 |
'posts_per_page' => -1, |
| 74 |
'post_status' => 'any', |
| 75 |
'post_type' => $post->post_type, |
| 76 |
'meta_key' => '_original_post', |
| 77 |
'meta_value' => $original ); |
| 78 |
|
| 79 |
$q = new WP_Query(); |
| 80 |
$posts = $q->query( $args ); |
| 81 |
|
| 82 |
$translations = array(); |
| 83 |
|
| 84 |
$original_post_status = get_post_status( $original ); |
| 85 |
|
| 86 |
if ( $original != $post->ID && $original_post_status && 'trash' != $original_post_status ) { |
| 87 |
$locale = bogo_get_post_locale( $original ); |
| 88 |
$translations[$locale] = get_post( $original ); |
| 89 |
} |
| 90 |
|
| 91 |
foreach ( $posts as $p ) { |
| 92 |
if ( $p->ID == $post->ID ) |
| 93 |
continue; |
| 94 |
|
| 95 |
$locale = bogo_get_post_locale( $p->ID ); |
| 96 |
|
| 97 |
if ( ! bogo_is_available_locale( $locale ) ) |
| 98 |
continue; |
| 99 |
|
| 100 |
if ( ! isset( $translations[$locale] ) ) |
| 101 |
$translations[$locale] = $p; |
| 102 |
} |
| 103 |
|
| 104 |
return array_filter( $translations ); |
| 105 |
} |
| 106 |
|
| 107 |
function bogo_get_page_by_path( $page_path, $locale = null, $post_type = 'page' ) { |
| 108 |
global $wpdb; |
| 109 |
|
| 110 |
if ( ! bogo_is_available_locale( $locale ) ) |
| 111 |
$locale = bogo_get_default_locale(); |
| 112 |
|
| 113 |
$page_path = rawurlencode( urldecode( $page_path ) ); |
| 114 |
$page_path = str_replace( '%2F', '/', $page_path ); |
| 115 |
$page_path = str_replace( '%20', ' ', $page_path ); |
| 116 |
|
| 117 |
$parts = explode( '/', trim( $page_path, '/' ) ); |
| 118 |
$parts = array_map( 'esc_sql', $parts ); |
| 119 |
$parts = array_map( 'sanitize_title_for_query', $parts ); |
| 120 |
|
| 121 |
$in_string = "'" . implode( "','", $parts ) . "'"; |
| 122 |
$post_type_sql = $post_type; |
| 123 |
$wpdb->escape_by_ref( $post_type_sql ); |
| 124 |
|
| 125 |
$q = "SELECT ID, post_name, post_parent FROM $wpdb->posts"; |
| 126 |
$q .= " LEFT JOIN $wpdb->postmeta ON ID = $wpdb->postmeta.post_id AND meta_key = '_locale'"; |
| 127 |
$q .= " WHERE 1=1"; |
| 128 |
$q .= " AND post_name IN ($in_string)"; |
| 129 |
$q .= " AND (post_type = '$post_type_sql' OR post_type = 'attachment')"; |
| 130 |
$q .= " AND (1=0"; |
| 131 |
$q .= $wpdb->prepare( " OR meta_value LIKE %s", $locale ); |
| 132 |
$q .= bogo_is_default_locale( $locale ) ? " OR meta_id IS NULL" : ""; |
| 133 |
$q .= ")"; |
| 134 |
|
| 135 |
$pages = $wpdb->get_results( $q, OBJECT_K ); |
| 136 |
|
| 137 |
$revparts = array_reverse( $parts ); |
| 138 |
|
| 139 |
$foundid = 0; |
| 140 |
|
| 141 |
foreach ( (array) $pages as $page ) { |
| 142 |
if ( $page->post_name != $revparts[0] ) |
| 143 |
continue; |
| 144 |
|
| 145 |
$count = 0; |
| 146 |
$p = $page; |
| 147 |
|
| 148 |
while ( $p->post_parent != 0 && isset( $pages[$p->post_parent] ) ) { |
| 149 |
$count++; |
| 150 |
$parent = $pages[$p->post_parent]; |
| 151 |
|
| 152 |
if ( ! isset( $revparts[$count] ) || $parent->post_name != $revparts[$count] ) |
| 153 |
break; |
| 154 |
|
| 155 |
$p = $parent; |
| 156 |
} |
| 157 |
|
| 158 |
if ( $p->post_parent == 0 |
| 159 |
&& $count + 1 == count( $revparts ) |
| 160 |
&& $p->post_name == $revparts[$count] ) { |
| 161 |
$foundid = $page->ID; |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if ( $foundid ) |
| 167 |
return get_page( $foundid ); |
| 168 |
|
| 169 |
return null; |
| 170 |
} |
| 171 |
|
| 172 |
?> |