| @@ -1,345 +1,385 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /* Posts List Table */ |
| 4 | 4 | |
| 5 | -add_filter( 'manage_pages_columns', 'bogo_pages_columns', 10, 1 ); | |
| 5 | +add_filter( 'manage_pages_columns', 'bogo_pages_columns' ); | |
| 6 | 6 | |
| 7 | 7 | function bogo_pages_columns( $posts_columns ) { |
| 8 | 8 | return bogo_posts_columns( $posts_columns, 'page' ); |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | - | |
| 12 | 11 | add_filter( 'manage_posts_columns', 'bogo_posts_columns', 10, 2 ); |
| 13 | 12 | |
| 14 | 13 | function bogo_posts_columns( $posts_columns, $post_type ) { |
| 15 | - if ( ! bogo_is_localizable_post_type( $post_type ) ) { | |
| 14 | + if ( ! bogo_is_localizable_post_type( $post_type ) ) | |
| 16 | 15 | return $posts_columns; |
| 17 | - } | |
| 18 | 16 | |
| 19 | 17 | if ( ! isset( $posts_columns['locale'] ) ) { |
| 20 | 18 | $posts_columns = array_merge( |
| 21 | 19 | array_slice( $posts_columns, 0, 3 ), |
| 22 | 20 | array( 'locale' => __( 'Locale', 'bogo' ) ), |
| 23 | - array_slice( $posts_columns, 3 ) | |
| 24 | - ); | |
| 21 | + array_slice( $posts_columns, 3 ) ); | |
| 25 | 22 | } |
| 26 | 23 | |
| 27 | 24 | return $posts_columns; |
| 28 | 25 | } |
| 29 | 26 | |
| 27 | +add_action( 'manage_pages_custom_column', 'bogo_manage_posts_custom_column', 10, 2 ); | |
| 28 | +add_action( 'manage_posts_custom_column', 'bogo_manage_posts_custom_column', 10, 2 ); | |
| 30 | 29 | |
| 31 | -add_action( 'manage_pages_custom_column', | |
| 32 | - 'bogo_manage_posts_custom_column', 10, 2 | |
| 33 | -); | |
| 30 | +function bogo_manage_posts_custom_column( $column_name, $post_id ) { | |
| 31 | + $post_type = get_post_type( $post_id ); | |
| 34 | 32 | |
| 35 | -add_action( 'manage_posts_custom_column', | |
| 36 | - 'bogo_manage_posts_custom_column', 10, 2 | |
| 37 | -); | |
| 38 | - | |
| 39 | -function bogo_manage_posts_custom_column( $column_name, $post_id ) { | |
| 40 | - if ( 'locale' !== $column_name ) { | |
| 33 | + if ( ! bogo_is_localizable_post_type( $post_type ) ) | |
| 41 | 34 | return; |
| 42 | - } | |
| 43 | 35 | |
| 44 | - $post_type = get_post_type( $post_id ); | |
| 45 | - | |
| 46 | - if ( ! bogo_is_localizable_post_type( $post_type ) ) { | |
| 36 | + if ( 'locale' != $column_name ) | |
| 47 | 37 | return; |
| 48 | - } | |
| 49 | 38 | |
| 50 | 39 | $locale = get_post_meta( $post_id, '_locale', true ); |
| 51 | 40 | |
| 52 | - if ( empty( $locale ) ) { | |
| 41 | + if ( empty( $locale ) ) | |
| 53 | 42 | return; |
| 54 | - } | |
| 55 | 43 | |
| 56 | 44 | $language = bogo_get_language( $locale ); |
| 57 | 45 | |
| 58 | - if ( empty( $language ) ) { | |
| 46 | + if ( empty( $language ) ) | |
| 59 | 47 | $language = $locale; |
| 60 | - } | |
| 61 | 48 | |
| 62 | 49 | echo sprintf( '<a href="%1$s">%2$s</a>', |
| 63 | - esc_url( | |
| 64 | - add_query_arg( array( | |
| 65 | - 'post_type' => $post_type, | |
| 66 | - 'lang' => $locale, | |
| 67 | - ), 'edit.php' ) | |
| 68 | - ), | |
| 69 | - esc_html( $language ) | |
| 70 | - ); | |
| 50 | + esc_url( add_query_arg( | |
| 51 | + array( 'post_type' => $post_type, 'lang' => $locale ), | |
| 52 | + 'edit.php' ) ), | |
| 53 | + esc_html( $language ) ); | |
| 71 | 54 | } |
| 72 | 55 | |
| 56 | +add_action( 'restrict_manage_posts', 'bogo_restrict_manage_posts' ); | |
| 73 | 57 | |
| 74 | -add_action( 'restrict_manage_posts', 'bogo_restrict_manage_posts', 10, 2 ); | |
| 58 | +function bogo_restrict_manage_posts() { | |
| 59 | + global $post_type; | |
| 75 | 60 | |
| 76 | -function bogo_restrict_manage_posts( $post_type, $which ) { | |
| 77 | 61 | if ( ! bogo_is_localizable_post_type( $post_type ) ) { |
| 78 | 62 | return; |
| 79 | 63 | } |
| 80 | 64 | |
| 81 | 65 | $available_languages = bogo_available_languages(); |
| 82 | - $current_locale = $_GET['lang'] ?? ''; | |
| 66 | + $current_locale = empty( $_GET['lang'] ) ? '' : $_GET['lang']; | |
| 83 | 67 | |
| 84 | - $options = array( | |
| 85 | - sprintf( | |
| 86 | - '<option %1$s>%2$s</option>', | |
| 87 | - bogo_format_atts( array( | |
| 88 | - 'value' => '', | |
| 89 | - 'selected' => '' === $current_locale, | |
| 90 | - ) ), | |
| 91 | - __( 'Show all locales', 'bogo' ) | |
| 92 | - ), | |
| 93 | - ); | |
| 68 | + echo '<select name="lang">'; | |
| 94 | 69 | |
| 70 | + $selected = ( '' == $current_locale ) ? ' selected="selected"' : ''; | |
| 71 | + | |
| 72 | + echo '<option value=""' . $selected . '>' | |
| 73 | + . esc_html( __( 'Show all locales', 'bogo' ) ) . '</option>'; | |
| 74 | + | |
| 95 | 75 | foreach ( $available_languages as $locale => $lang ) { |
| 96 | - $options[] = sprintf( | |
| 97 | - '<option %1$s>%2$s</option>', | |
| 98 | - bogo_format_atts( array( | |
| 99 | - 'value' => $locale, | |
| 100 | - 'selected' => $locale === $current_locale, | |
| 101 | - ) ), | |
| 102 | - $lang | |
| 103 | - ); | |
| 76 | + $selected = ( $locale == $current_locale ) ? ' selected="selected"' : ''; | |
| 77 | + | |
| 78 | + echo '<option value="' . esc_attr( $locale ) . '"' . $selected . '>' | |
| 79 | + . esc_html( $lang ) . '</option>'; | |
| 104 | 80 | } |
| 105 | 81 | |
| 106 | - $dropdown = sprintf( | |
| 107 | - '<select name="lang">%s</select>', | |
| 108 | - implode( $options ) | |
| 109 | - ); | |
| 110 | - | |
| 111 | - echo wp_kses( $dropdown, 'bogo_form_inside' ); | |
| 82 | + echo '</select>' . "\n"; | |
| 112 | 83 | } |
| 113 | 84 | |
| 114 | - | |
| 115 | 85 | add_filter( 'post_row_actions', 'bogo_post_row_actions', 10, 2 ); |
| 116 | 86 | add_filter( 'page_row_actions', 'bogo_post_row_actions', 10, 2 ); |
| 117 | 87 | |
| 118 | 88 | function bogo_post_row_actions( $actions, $post ) { |
| 119 | - if ( | |
| 120 | - ! bogo_is_localizable_post_type( $post->post_type ) or | |
| 121 | - 'trash' === $post->post_status | |
| 122 | - ) { | |
| 89 | + if ( ! bogo_is_localizable_post_type( $post->post_type ) ) | |
| 123 | 90 | return $actions; |
| 124 | - } | |
| 125 | 91 | |
| 126 | 92 | $post_type_object = get_post_type_object( $post->post_type ); |
| 127 | 93 | |
| 128 | - if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) { | |
| 94 | + if ( ! current_user_can( $post_type_object->cap->edit_post, $post->ID ) | |
| 95 | + || 'trash' == $post->post_status ) | |
| 129 | 96 | return $actions; |
| 130 | - } | |
| 131 | 97 | |
| 132 | 98 | $user_locale = bogo_get_user_locale(); |
| 133 | 99 | $post_locale = bogo_get_post_locale( $post->ID ); |
| 134 | 100 | |
| 135 | - if ( $user_locale === $post_locale ) { | |
| 101 | + if ( $user_locale == $post_locale ) | |
| 136 | 102 | return $actions; |
| 137 | - } | |
| 138 | 103 | |
| 139 | - if ( $translation = bogo_get_post_translation( $post, $user_locale ) ) { | |
| 140 | - if ( empty( $translation->ID ) or $translation->ID === $post->ID ) { | |
| 104 | + $translations = bogo_get_post_translations( $post ); | |
| 105 | + | |
| 106 | + if ( ! empty( $translations[$user_locale] ) ) { | |
| 107 | + $translation = $translations[$user_locale]; | |
| 108 | + | |
| 109 | + if ( empty( $translation->ID ) || $translation->ID == $post->ID ) | |
| 141 | 110 | return $actions; |
| 142 | - } | |
| 143 | 111 | |
| 144 | - /* translators: %s: Language name */ | |
| 112 | + $title = __( 'Edit %s translation of this item', 'bogo' ); | |
| 145 | 113 | $text = __( 'Edit %s translation', 'bogo' ); |
| 146 | 114 | |
| 147 | 115 | $edit_link = get_edit_post_link( $translation->ID ); |
| 116 | + | |
| 148 | 117 | } else { |
| 149 | - /* translators: %s: Language name */ | |
| 118 | + $title = __( 'Translate this item into %s', 'bogo' ); | |
| 150 | 119 | $text = __( 'Translate into %s', 'bogo' ); |
| 151 | 120 | |
| 152 | 121 | $edit_link = admin_url( 'post-new.php?post_type=' . $post->post_type |
| 153 | - . '&action=bogo-add-translation' | |
| 154 | 122 | . '&locale=' . $user_locale |
| 155 | - . '&original_post=' . $post->ID | |
| 156 | - ); | |
| 157 | - | |
| 158 | - $edit_link = wp_nonce_url( $edit_link, 'bogo-add-translation' ); | |
| 123 | + . '&original_post=' . $post->ID ); | |
| 159 | 124 | } |
| 160 | 125 | |
| 161 | 126 | $language = bogo_get_language( $user_locale ); |
| 162 | 127 | |
| 163 | - if ( empty( $language ) ) { | |
| 128 | + if ( empty( $language ) ) | |
| 164 | 129 | $language = $user_locale; |
| 165 | - } | |
| 166 | 130 | |
| 167 | - $actions['translate'] = sprintf( | |
| 168 | - '<a href="%1$s">%2$s</a>', | |
| 169 | - $edit_link, | |
| 170 | - esc_html( sprintf( $text, $language ) ) | |
| 171 | - ); | |
| 131 | + $actions['translate'] = '<a title="' . esc_attr( sprintf( $title, $language ) ) . '" href="' . $edit_link . '">' . esc_html( sprintf( $text, $language ) ) . '</a>'; | |
| 172 | 132 | |
| 173 | 133 | return $actions; |
| 174 | 134 | } |
| 175 | 135 | |
| 136 | +/* Single Post */ | |
| 176 | 137 | |
| 177 | -add_action( 'admin_init', 'bogo_add_translation', 10, 0 ); | |
| 138 | +add_action( 'add_meta_boxes', 'bogo_add_l10n_meta_boxes', 10, 2 ); | |
| 178 | 139 | |
| 179 | -function bogo_add_translation() { | |
| 180 | - if ( | |
| 181 | - empty( $_REQUEST['action'] ) or | |
| 182 | - 'bogo-add-translation' !== $_REQUEST['action'] | |
| 183 | - ) { | |
| 140 | +function bogo_add_l10n_meta_boxes( $post_type, $post ) { | |
| 141 | + if ( ! bogo_is_localizable_post_type( $post_type ) ) | |
| 184 | 142 | return; |
| 143 | + | |
| 144 | + if ( in_array( $post_type, array( 'comment', 'link' ) ) ) | |
| 145 | + return; | |
| 146 | + | |
| 147 | + add_meta_box( 'bogol10ndiv', __( 'Language', 'bogo' ), | |
| 148 | + 'bogo_l10n_meta_box', null, 'side', 'high' ); | |
| 149 | +} | |
| 150 | + | |
| 151 | +function bogo_l10n_meta_box( $post ) { | |
| 152 | + $initial = ( 'auto-draft' == $post->post_status ); | |
| 153 | + | |
| 154 | + if ( $initial ) { | |
| 155 | + $locale = isset( $_REQUEST['locale'] ) ? $_REQUEST['locale'] : ''; | |
| 156 | + | |
| 157 | + if ( ! bogo_is_available_locale( $locale ) ) | |
| 158 | + $locale = bogo_get_user_locale(); | |
| 159 | + | |
| 160 | + $original_post = empty( $_REQUEST['original_post'] ) ? '' : $_REQUEST['original_post']; | |
| 161 | + } else { | |
| 162 | + $locale = bogo_get_post_locale( $post->ID ); | |
| 163 | + $original_post = get_post_meta( $post->ID, '_original_post', true ); | |
| 164 | + | |
| 165 | + if ( empty( $original_post ) ) | |
| 166 | + $original_post = $post->ID; | |
| 185 | 167 | } |
| 186 | 168 | |
| 187 | - check_admin_referer( 'bogo-add-translation' ); | |
| 169 | + $lang = bogo_get_language( $locale ); | |
| 188 | 170 | |
| 189 | - $locale = trim( $_REQUEST['locale'] ?? '' ); | |
| 190 | - $original_post = absint( $_REQUEST['original_post'] ?? 0 ); | |
| 171 | + if ( empty( $lang ) ) | |
| 172 | + $lang = $locale; | |
| 191 | 173 | |
| 192 | - if ( ! bogo_is_available_locale( $locale ) ) { | |
| 174 | +?> | |
| 175 | +<div class="hidden"> | |
| 176 | +<input type="hidden" name="locale" value="<?php echo esc_attr( $locale ); ?>" /> | |
| 177 | +<input type="hidden" name="original_post" value="<?php echo esc_attr( $original_post ); ?>" /> | |
| 178 | +</div> | |
| 179 | + | |
| 180 | +<div class="descriptions"> | |
| 181 | +<p><strong><?php echo esc_html( __( 'Language', 'bogo' ) ); ?>:</strong> | |
| 182 | + <?php echo esc_html( $lang ); ?></p> | |
| 183 | +</div> | |
| 184 | + | |
| 185 | +<?php | |
| 186 | + bogo_metabox_translations( $post ); | |
| 187 | + bogo_metabox_add_translation( $post ); | |
| 188 | +} | |
| 189 | + | |
| 190 | +function bogo_metabox_translations( $post ) { | |
| 191 | + $translations = bogo_get_post_translations( $post->ID ); | |
| 192 | + | |
| 193 | + if ( empty( $translations ) ) | |
| 193 | 194 | return; |
| 194 | - } | |
| 195 | 195 | |
| 196 | - if ( ! $original_post or ! $original_post = get_post( $original_post ) ) { | |
| 197 | - return; | |
| 198 | - } | |
| 196 | +?> | |
| 197 | +<p><strong><?php echo esc_html( __( 'Translations', 'bogo' ) ); ?>:</strong></p> | |
| 199 | 198 | |
| 200 | - $post_type_object = get_post_type_object( $original_post->post_type ); | |
| 199 | +<ul style="list-style: disc inside; margin-left: 1em;"> | |
| 200 | +<?php | |
| 201 | + foreach ( $translations as $locale => $translation ) { | |
| 202 | + $edit_link = get_edit_post_link( $translation->ID ); | |
| 201 | 203 | |
| 202 | - if ( | |
| 203 | - $post_type_object and | |
| 204 | - current_user_can( $post_type_object->cap->edit_posts ) | |
| 205 | - ) { | |
| 206 | - $new_post_id = bogo_duplicate_post( $original_post, $locale ); | |
| 204 | + echo '<li>'; | |
| 207 | 205 | |
| 208 | - if ( $new_post_id ) { | |
| 209 | - $redirect_to = get_edit_post_link( $new_post_id, 'raw' ); | |
| 210 | - wp_safe_redirect( $redirect_to ); | |
| 211 | - exit(); | |
| 212 | - } | |
| 206 | + if ( $edit_link ) | |
| 207 | + echo '<a href="' . esc_url( $edit_link ) . '" target="_blank">' . get_the_title( $translation->ID ) . '</a>'; | |
| 208 | + else | |
| 209 | + echo get_the_title( $translation->ID ); | |
| 210 | + | |
| 211 | + $lang = bogo_get_language( $locale ); | |
| 212 | + | |
| 213 | + if ( empty( $lang ) ) | |
| 214 | + $lang = $locale; | |
| 215 | + | |
| 216 | + echo ' [' . $lang . ']'; | |
| 217 | + echo '</li>'; | |
| 213 | 218 | } |
| 219 | +?> | |
| 220 | +</ul> | |
| 221 | +<?php | |
| 214 | 222 | } |
| 215 | 223 | |
| 224 | +function bogo_metabox_add_translation( $post ) { | |
| 225 | + if ( 'auto-draft' == $post->post_status ) | |
| 226 | + return; | |
| 216 | 227 | |
| 217 | -/* Single Post */ | |
| 228 | + $post_locale = bogo_get_post_locale( $post->ID ); | |
| 229 | + $user_locale = bogo_get_user_locale(); | |
| 218 | 230 | |
| 219 | -add_action( 'add_meta_boxes', 'bogo_add_l10n_meta_boxes', 10, 2 ); | |
| 231 | + if ( $post_locale == $user_locale ) | |
| 232 | + return; | |
| 220 | 233 | |
| 221 | -function bogo_add_l10n_meta_boxes( $post_type, $post ) { | |
| 222 | - if ( ! bogo_is_localizable_post_type( $post_type ) ) { | |
| 234 | + $locale = $user_locale; | |
| 235 | + | |
| 236 | + $translations = bogo_get_post_translations( $post->ID ); | |
| 237 | + | |
| 238 | + if ( isset( $translations[$locale] ) ) | |
| 223 | 239 | return; |
| 240 | + | |
| 241 | + $lang = bogo_get_language( $locale ); | |
| 242 | + | |
| 243 | + if ( empty( $lang ) ) | |
| 244 | + $lang = $locale; | |
| 245 | + | |
| 246 | + $edit_link = admin_url( 'post-new.php?post_type=' . $post->post_type | |
| 247 | + . '&locale=' . $locale | |
| 248 | + . '&original_post=' . $post->ID ); | |
| 249 | + | |
| 250 | +?> | |
| 251 | +<p class="textright"><a href="<?php echo $edit_link; ?>" target="_blank" class="button"><?php echo esc_html( sprintf( __( 'Add %s translation', 'bogo' ), $lang ) ) ?></a></p> | |
| 252 | +<?php | |
| 253 | +} | |
| 254 | + | |
| 255 | +add_filter( 'default_title', 'bogo_translation_default', 10, 2 ); | |
| 256 | +add_filter( 'default_content', 'bogo_translation_default', 10, 2 ); | |
| 257 | +add_filter( 'default_excerpt', 'bogo_translation_default', 10, 2 ); | |
| 258 | + | |
| 259 | +function bogo_translation_default( $value, $post ) { | |
| 260 | + if ( ! empty( $value ) | |
| 261 | + || empty( $_REQUEST['original_post'] ) | |
| 262 | + || ! $original = get_post( $_REQUEST['original_post'] ) ) { | |
| 263 | + return $value; | |
| 224 | 264 | } |
| 225 | 265 | |
| 226 | - if ( in_array( $post_type, array( 'comment', 'link' ) ) ) { | |
| 227 | - return; | |
| 266 | + if ( 'default_title' == current_filter() ) { | |
| 267 | + $value = $original->post_title; | |
| 268 | + } elseif ( 'default_content' == current_filter() ) { | |
| 269 | + $value = $original->post_content; | |
| 270 | + } elseif ( 'default_excerpt' == current_filter() ) { | |
| 271 | + $value = $original->post_excerpt; | |
| 228 | 272 | } |
| 229 | 273 | |
| 230 | - add_meta_box( 'bogol10ndiv', __( 'Language', 'bogo' ), | |
| 231 | - 'bogo_l10n_meta_box', null, 'side', 'high', | |
| 232 | - array( | |
| 233 | - '__back_compat_meta_box' => true, | |
| 234 | - ) | |
| 235 | - ); | |
| 274 | + return $value; | |
| 236 | 275 | } |
| 237 | 276 | |
| 277 | +add_action( 'save_post', 'bogo_save_post', 10, 2 ); | |
| 238 | 278 | |
| 239 | -function bogo_l10n_meta_box( $post ) { | |
| 240 | - $initial = ( 'auto-draft' === $post->post_status ); | |
| 279 | +function bogo_save_post( $post_id, $post ) { | |
| 280 | + if ( did_action( 'import_start' ) && ! did_action( 'import_end' ) ) // Importing | |
| 281 | + return; | |
| 241 | 282 | |
| 242 | - if ( $initial ) { | |
| 243 | - $post_locale = bogo_get_user_locale(); | |
| 244 | - } else { | |
| 245 | - $post_locale = bogo_get_post_locale( $post->ID ); | |
| 283 | + if ( ! bogo_is_localizable_post_type( $post->post_type ) ) | |
| 284 | + return; | |
| 285 | + | |
| 286 | + $old_locale = get_post_meta( $post_id, '_locale', true ); | |
| 287 | + | |
| 288 | + if ( empty( $old_locale ) ) { | |
| 289 | + if ( ! empty( $_REQUEST['locale'] ) && bogo_is_available_locale( $_REQUEST['locale'] ) ) | |
| 290 | + $locale = $_REQUEST['locale']; | |
| 291 | + elseif ( 'auto-draft' == get_post_status( $post_id ) ) | |
| 292 | + $locale = bogo_get_user_locale(); | |
| 293 | + else | |
| 294 | + $locale = bogo_get_default_locale(); | |
| 246 | 295 | } |
| 247 | 296 | |
| 248 | - $translations = bogo_get_post_translations( $post->ID ); | |
| 297 | + if ( ! empty( $locale ) && $locale != $old_locale ) | |
| 298 | + update_post_meta( $post_id, '_locale', $locale ); | |
| 299 | + else | |
| 300 | + $locale = $old_locale; | |
| 249 | 301 | |
| 250 | - $available_languages = bogo_available_languages( array( | |
| 251 | - 'current_user_can_access' => true, | |
| 252 | - ) ); | |
| 302 | + if ( $original = get_post_meta( $post_id, '_original_post', true ) ) | |
| 303 | + return; | |
| 253 | 304 | |
| 254 | - $post_language = $available_languages[$post_locale] ?? $post_locale; | |
| 305 | + if ( ! empty( $_REQUEST['original_post'] ) ) { | |
| 306 | + $original = get_post_meta( $_REQUEST['original_post'], '_original_post', true ); | |
| 255 | 307 | |
| 256 | - unset( $available_languages[$post_locale] ); | |
| 308 | + if ( empty( $original ) ) | |
| 309 | + $original = (int) $_REQUEST['original_post']; | |
| 257 | 310 | |
| 258 | -?> | |
| 311 | + update_post_meta( $post_id, '_original_post', $original ); | |
| 312 | + return; | |
| 313 | + } | |
| 259 | 314 | |
| 260 | -<div class="descriptions"> | |
| 261 | -<p><?php | |
| 315 | + $original = $post_id; | |
| 262 | 316 | |
| 263 | - echo wp_kses_data( sprintf( | |
| 264 | - '<strong>%1$s</strong> %2$s', | |
| 265 | - __( 'Language:', 'bogo' ), | |
| 266 | - $post_language | |
| 267 | - ) ); | |
| 317 | + while ( 1 ) { | |
| 318 | + $q = new WP_Query(); | |
| 268 | 319 | |
| 269 | -?></p> | |
| 270 | -</div> | |
| 320 | + $posts = $q->query( array( | |
| 321 | + 'bogo_suppress_locale_query' => true, | |
| 322 | + 'posts_per_page' => 1, | |
| 323 | + 'post_status' => 'any', | |
| 324 | + 'post_type' => $post->post_type, | |
| 325 | + 'meta_key' => '_original_post', | |
| 326 | + 'meta_value' => $original ) ); | |
| 271 | 327 | |
| 272 | -<div class="descriptions"> | |
| 273 | -<p> | |
| 274 | - <strong><?php echo wp_kses_data( __( 'Translations:', 'bogo' ) ); ?></strong> | |
| 275 | -</p> | |
| 328 | + if ( empty( $posts ) ) { | |
| 329 | + update_post_meta( $post_id, '_original_post', $original ); | |
| 330 | + return; | |
| 331 | + } | |
| 276 | 332 | |
| 277 | -<ul id="bogo-translations"> | |
| 278 | -<?php | |
| 333 | + $original += 1; | |
| 334 | + } | |
| 335 | +} | |
| 279 | 336 | |
| 280 | - foreach ( $translations as $locale => $translation ) { | |
| 281 | - $li_content = get_the_title( $translation->ID ); | |
| 337 | +add_filter( 'wp_unique_post_slug', 'bogo_unique_post_slug', 10, 6 ); | |
| 282 | 338 | |
| 283 | - if ( $edit_link = get_edit_post_link( $translation->ID ) ) { | |
| 284 | - $li_content = sprintf( | |
| 285 | - '<a %1$s>%2$s<span class="screen-reader-text">%3$s</span></a>', | |
| 286 | - bogo_format_atts( array( | |
| 287 | - 'href' => esc_url( $edit_link ), | |
| 288 | - 'target' => '_blank', | |
| 289 | - ) ), | |
| 290 | - $li_content, | |
| 291 | - /* translators: accessibility text */ | |
| 292 | - esc_html( __( '(opens in a new window)', 'bogo' ) ) | |
| 293 | - ); | |
| 294 | - } | |
| 339 | +function bogo_unique_post_slug( $slug, $post_id, $status, $type, $parent, $original ) { | |
| 340 | + global $wp_rewrite; | |
| 295 | 341 | |
| 296 | - $li_content .= sprintf( | |
| 297 | - ' [%s]', | |
| 298 | - $available_languages[$locale] ?? $locale | |
| 299 | - ); | |
| 300 | - | |
| 301 | - echo wp_kses_post( sprintf( '<li>%s</li>', $li_content ) ); | |
| 342 | + if ( ! bogo_is_localizable_post_type( $type ) ) { | |
| 343 | + return $slug; | |
| 302 | 344 | } |
| 303 | 345 | |
| 304 | -?> | |
| 305 | -</ul> | |
| 306 | -</div> | |
| 346 | + $feeds = is_array( $wp_rewrite->feeds ) ? $wp_rewrite->feeds : array(); | |
| 307 | 347 | |
| 308 | -<?php | |
| 348 | + if ( in_array( $original, $feeds ) ) { | |
| 349 | + return $slug; | |
| 350 | + } | |
| 309 | 351 | |
| 310 | - $available_languages = array_diff_key( $available_languages, $translations ); | |
| 352 | + $locale = bogo_get_post_locale( $post_id ); | |
| 311 | 353 | |
| 312 | - if ( ! $initial and $available_languages ) { | |
| 354 | + if ( empty( $locale ) ) { | |
| 355 | + return $slug; | |
| 356 | + } | |
| 313 | 357 | |
| 314 | -?> | |
| 358 | + $args = array( | |
| 359 | + 'posts_per_page' => 1, | |
| 360 | + 'post__not_in' => array( $post_id ), | |
| 361 | + 'post_type' => $type, | |
| 362 | + 'name' => $original, | |
| 363 | + 'lang' => $locale ); | |
| 315 | 364 | |
| 316 | -<div class="descriptions" id="bogo-add-translation-actions"> | |
| 317 | -<p> | |
| 318 | - <strong><?php echo wp_kses_data( __( 'Add Translation:', 'bogo' ) ); ?></strong> | |
| 319 | -</p> | |
| 365 | + $hierarchical = in_array( $type, get_post_types( array( 'hierarchical' => true ) ) ); | |
| 320 | 366 | |
| 321 | -<?php | |
| 367 | + if ( $hierarchical ) { | |
| 368 | + if ( preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $original ) ) { | |
| 369 | + return $slug; | |
| 370 | + } | |
| 322 | 371 | |
| 323 | - foreach ( $available_languages as $locale => $lang ) { | |
| 324 | - echo wp_kses_post( sprintf( | |
| 325 | - '<p><button %1$s>%2$s</button> <span class="spinner"></span></p>', | |
| 326 | - bogo_format_atts( array( | |
| 327 | - 'type' => 'button', | |
| 328 | - 'class' => 'button', | |
| 329 | - 'data-locale' => $locale, | |
| 330 | - ) ), | |
| 331 | - sprintf( | |
| 332 | - /* translators: %s: Language name. */ | |
| 333 | - __( 'Add %s translation', 'bogo' ), | |
| 334 | - $lang | |
| 335 | - ) | |
| 336 | - ) ); | |
| 337 | - } | |
| 372 | + $args['post_parent'] = $parent; | |
| 373 | + } | |
| 338 | 374 | |
| 339 | -?> | |
| 340 | -<div class="clear"></div> | |
| 341 | -</div> | |
| 342 | -<?php | |
| 375 | + $q = new WP_Query(); | |
| 376 | + $posts = $q->query( $args ); | |
| 343 | 377 | |
| 378 | + if ( empty( $posts ) ) { | |
| 379 | + $slug = $original; | |
| 344 | 380 | } |
| 381 | + | |
| 382 | + return $slug; | |
| 345 | 383 | } |
| 384 | + | |
| 385 | +?> | |