PluginProbe
Bogo / 2.9
Bogo v2.9
3.9.3 trunk 1.0 1.1 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.8 2.8.1 2.9 3.0 3.0.1 All 52 releases
← All changes | admin/includes/post.php +249 -155 trunk2.9 View file →
@@ -1,15 +1,14 @@
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 14 if ( ! bogo_is_localizable_post_type( $post_type ) ) {
@@ -19,26 +18,21 @@
19 18 if ( ! isset( $posts_columns['locale'] ) ) {
20 19 $posts_columns = array_merge(
21 20 array_slice( $posts_columns, 0, 3 ),
22 21 array( 'locale' => __( 'Locale', 'bogo' ) ),
23 - array_slice( $posts_columns, 3 )
24 - );
22 + array_slice( $posts_columns, 3 ) );
25 23 }
26 24
27 25 return $posts_columns;
28 26 }
29 27
30 -
31 28 add_action( 'manage_pages_custom_column',
32 - 'bogo_manage_posts_custom_column', 10, 2
33 -);
34 -
29 + 'bogo_manage_posts_custom_column', 10, 2 );
35 30 add_action( 'manage_posts_custom_column',
36 - 'bogo_manage_posts_custom_column', 10, 2
37 -);
31 + 'bogo_manage_posts_custom_column', 10, 2 );
38 32
39 33 function bogo_manage_posts_custom_column( $column_name, $post_id ) {
40 - if ( 'locale' !== $column_name ) {
34 + if ( 'locale' != $column_name ) {
41 35 return;
42 36 }
43 37
44 38 $post_type = get_post_type( $post_id );
@@ -59,74 +53,55 @@
59 53 $language = $locale;
60 54 }
61 55
62 56 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 - );
57 + esc_url( add_query_arg(
58 + array( 'post_type' => $post_type, 'lang' => $locale ),
59 + 'edit.php' ) ),
60 + esc_html( $language ) );
71 61 }
72 62
63 +add_action( 'restrict_manage_posts', 'bogo_restrict_manage_posts' );
73 64
74 -add_action( 'restrict_manage_posts', 'bogo_restrict_manage_posts', 10, 2 );
65 +function bogo_restrict_manage_posts() {
66 + global $post_type;
75 67
76 -function bogo_restrict_manage_posts( $post_type, $which ) {
77 68 if ( ! bogo_is_localizable_post_type( $post_type ) ) {
78 69 return;
79 70 }
80 71
81 72 $available_languages = bogo_available_languages();
82 - $current_locale = $_GET['lang'] ?? '';
73 + $current_locale = empty( $_GET['lang'] ) ? '' : $_GET['lang'];
83 74
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 - );
75 + echo '<select name="lang">';
94 76
77 + $selected = ( '' == $current_locale ) ? ' selected="selected"' : '';
78 +
79 + echo '<option value=""' . $selected . '>'
80 + . esc_html( __( 'Show all locales', 'bogo' ) ) . '</option>';
81 +
95 82 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 - );
83 + $selected = ( $locale == $current_locale ) ? ' selected="selected"' : '';
84 +
85 + echo '<option value="' . esc_attr( $locale ) . '"' . $selected . '>'
86 + . esc_html( $lang ) . '</option>';
104 87 }
105 88
106 - $dropdown = sprintf(
107 - '<select name="lang">%s</select>',
108 - implode( $options )
109 - );
110 -
111 - echo wp_kses( $dropdown, 'bogo_form_inside' );
89 + echo '</select>' . "\n";
112 90 }
113 91
114 -
115 92 add_filter( 'post_row_actions', 'bogo_post_row_actions', 10, 2 );
116 93 add_filter( 'page_row_actions', 'bogo_post_row_actions', 10, 2 );
117 94
118 95 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 - ) {
96 + if ( ! bogo_is_localizable_post_type( $post->post_type ) ) {
123 97 return $actions;
124 98 }
125 99
126 100 $post_type_object = get_post_type_object( $post->post_type );
127 101
128 - if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
102 + if ( ! current_user_can( $post_type_object->cap->edit_post, $post->ID )
103 + || 'trash' == $post->post_status ) {
129 104 return $actions;
130 105 }
131 106
132 107 $user_locale = bogo_get_user_locale();
@@ -131,31 +106,25 @@
131 106
132 107 $user_locale = bogo_get_user_locale();
133 108 $post_locale = bogo_get_post_locale( $post->ID );
134 109
135 - if ( $user_locale === $post_locale ) {
110 + if ( $user_locale == $post_locale ) {
136 111 return $actions;
137 112 }
138 113
139 114 if ( $translation = bogo_get_post_translation( $post, $user_locale ) ) {
140 - if ( empty( $translation->ID ) or $translation->ID === $post->ID ) {
115 + if ( empty( $translation->ID ) || $translation->ID == $post->ID ) {
141 116 return $actions;
142 117 }
143 118
144 - /* translators: %s: Language name */
145 119 $text = __( 'Edit %s translation', 'bogo' );
146 -
147 120 $edit_link = get_edit_post_link( $translation->ID );
148 121 } else {
149 - /* translators: %s: Language name */
150 122 $text = __( 'Translate into %s', 'bogo' );
151 -
152 123 $edit_link = admin_url( 'post-new.php?post_type=' . $post->post_type
153 124 . '&action=bogo-add-translation'
154 125 . '&locale=' . $user_locale
155 - . '&original_post=' . $post->ID
156 - );
157 -
126 + . '&original_post=' . $post->ID );
158 127 $edit_link = wp_nonce_url( $edit_link, 'bogo-add-translation' );
159 128 }
160 129
161 130 $language = bogo_get_language( $user_locale );
@@ -166,44 +135,40 @@
166 135
167 136 $actions['translate'] = sprintf(
168 137 '<a href="%1$s">%2$s</a>',
169 138 $edit_link,
170 - esc_html( sprintf( $text, $language ) )
171 - );
139 + esc_html( sprintf( $text, $language ) ) );
172 140
173 141 return $actions;
174 142 }
175 143
144 +add_action( 'admin_init', 'bogo_add_translation' );
176 145
177 -add_action( 'admin_init', 'bogo_add_translation', 10, 0 );
178 -
179 146 function bogo_add_translation() {
180 - if (
181 - empty( $_REQUEST['action'] ) or
182 - 'bogo-add-translation' !== $_REQUEST['action']
183 - ) {
147 + if ( empty( $_REQUEST['action'] )
148 + || 'bogo-add-translation' != $_REQUEST['action'] ) {
184 149 return;
185 150 }
186 151
187 152 check_admin_referer( 'bogo-add-translation' );
188 153
189 - $locale = trim( $_REQUEST['locale'] ?? '' );
190 - $original_post = absint( $_REQUEST['original_post'] ?? 0 );
154 + $locale = isset( $_REQUEST['locale'] ) ? $_REQUEST['locale'] : '';
155 + $original_post = isset( $_REQUEST['original_post'] )
156 + ? absint( $_REQUEST['original_post'] ) : 0;
191 157
192 - if ( ! bogo_is_available_locale( $locale ) ) {
158 + if ( ! bogo_is_available_locale( $locale )
159 + || ! current_user_can( 'bogo_access_locale', $locale ) ) {
193 160 return;
194 161 }
195 162
196 - if ( ! $original_post or ! $original_post = get_post( $original_post ) ) {
163 + if ( ! $original_post || ! $original_post = get_post( $original_post ) ) {
197 164 return;
198 165 }
199 166
200 167 $post_type_object = get_post_type_object( $original_post->post_type );
201 168
202 - if (
203 - $post_type_object and
204 - current_user_can( $post_type_object->cap->edit_posts )
205 - ) {
169 + if ( $post_type_object
170 + && current_user_can( $post_type_object->cap->edit_posts ) ) {
206 171 $new_post_id = bogo_duplicate_post( $original_post, $locale );
207 172
208 173 if ( $new_post_id ) {
209 174 $redirect_to = get_edit_post_link( $new_post_id, 'raw' );
@@ -212,9 +177,8 @@
212 177 }
213 178 }
214 179 }
215 180
216 -
217 181 /* Single Post */
218 182
219 183 add_action( 'add_meta_boxes', 'bogo_add_l10n_meta_boxes', 10, 2 );
220 184
@@ -227,119 +191,249 @@
227 191 return;
228 192 }
229 193
230 194 add_meta_box( 'bogol10ndiv', __( 'Language', 'bogo' ),
231 - 'bogo_l10n_meta_box', null, 'side', 'high',
232 - array(
233 - '__back_compat_meta_box' => true,
234 - )
235 - );
195 + 'bogo_l10n_meta_box', null, 'side', 'high' );
236 196 }
237 197
238 -
239 198 function bogo_l10n_meta_box( $post ) {
240 - $initial = ( 'auto-draft' === $post->post_status );
199 + $initial = ( 'auto-draft' == $post->post_status );
241 200
242 201 if ( $initial ) {
243 - $post_locale = bogo_get_user_locale();
202 + $post_locale = isset( $_REQUEST['locale'] ) ? $_REQUEST['locale'] : '';
203 +
204 + if ( ! bogo_is_available_locale( $post_locale ) ) {
205 + $post_locale = bogo_get_user_locale();
206 + }
207 +
208 + $original_post = empty( $_REQUEST['original_post'] )
209 + ? '' : $_REQUEST['original_post'];
244 210 } else {
245 211 $post_locale = bogo_get_post_locale( $post->ID );
212 + $original_post = get_post_meta( $post->ID, '_original_post', true );
213 +
214 + if ( empty( $original_post ) ) {
215 + $original_post = $post->ID;
216 + }
246 217 }
247 218
248 219 $translations = bogo_get_post_translations( $post->ID );
220 + $available_locales = bogo_available_locales( array(
221 + 'exclude' => array_merge(
222 + array( $post_locale ),
223 + array_keys( (array) $translations ) ),
224 + 'exclude_enus_if_inactive' => true,
225 + 'current_user_can_access' => true ) );
226 +?>
227 +<div class="hidden">
228 +<input type="hidden" name="locale" value="<?php echo esc_attr( $post_locale ); ?>" />
229 +<input type="hidden" name="original_post" value="<?php echo esc_attr( $original_post ); ?>" />
230 +</div>
249 231
250 - $available_languages = bogo_available_languages( array(
251 - 'current_user_can_access' => true,
252 - ) );
232 +<div class="descriptions">
233 +<?php
234 + $lang = bogo_get_language( $post_locale );
235 + $lang = empty( $lang ) ? $post_locale : $lang;
236 +?>
237 +<p><strong><?php echo esc_html( __( 'Language', 'bogo' ) ); ?>:</strong>
238 + <?php echo esc_html( $lang ); ?></p>
239 +</div>
253 240
254 - $post_language = $available_languages[$post_locale] ?? $post_locale;
241 +<?php
242 + do {
243 + if ( ! $translations && ( $initial || empty( $available_locales ) ) ) {
244 + break;
245 + }
255 246
256 - unset( $available_languages[$post_locale] );
247 + echo '<div class="descriptions">';
248 + echo sprintf( '<p><strong>%s:</strong></p>',
249 + esc_html( __( 'Translations', 'bogo' ) ) );
250 + echo '<ul id="bogo-translations">';
257 251
258 -?>
252 + if ( $translations ) {
253 + foreach ( $translations as $locale => $translation ) {
254 + $edit_link = get_edit_post_link( $translation->ID );
255 + echo '<li>';
259 256
260 -<div class="descriptions">
261 -<p><?php
257 + if ( $edit_link ) {
258 + echo sprintf( '<a href="%1$s" target="_blank">%2$s</a>',
259 + esc_url( $edit_link ), get_the_title( $translation->ID ) );
260 + } else {
261 + echo get_the_title( $translation->ID );
262 + }
262 263
263 - echo wp_kses_data( sprintf(
264 - '<strong>%1$s</strong> %2$s',
265 - __( 'Language:', 'bogo' ),
266 - $post_language
267 - ) );
264 + $lang = bogo_get_language( $locale );
265 + $lang = empty( $lang ) ? $locale : $lang;
266 + echo ' [' . $lang . ']';
267 + echo '</li>';
268 + }
269 + }
268 270
269 -?></p>
270 -</div>
271 + echo '</ul>';
272 + echo '</div>';
273 + } while (0);
271 274
272 -<div class="descriptions">
273 -<p>
274 - <strong><?php echo wp_kses_data( __( 'Translations:', 'bogo' ) ); ?></strong>
275 -</p>
275 + do {
276 + if ( $initial || empty( $available_locales ) ) {
277 + break;
278 + }
276 279
277 -<ul id="bogo-translations">
278 -<?php
280 + echo '<div id="bogo-add-translation-actions" class="descriptions">';
281 + echo sprintf( '<p><strong>%s:</strong></p>',
282 + esc_html( __( 'Add Translation', 'bogo' ) ) );
283 + echo '<select id="bogo-translations-to-add">';
279 284
280 - foreach ( $translations as $locale => $translation ) {
281 - $li_content = get_the_title( $translation->ID );
285 + foreach ( $available_locales as $locale ) {
286 + $lang = bogo_get_language( $locale );
287 + $lang = empty( $lang ) ? $locale : $lang;
288 + echo sprintf( '<option value="%1$s">%2$s</option>',
289 + esc_attr( $locale ), esc_html( $lang ) );
290 + }
282 291
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 - );
292 + echo '</select>';
293 + echo '<p>';
294 + echo sprintf(
295 + '<button type="button" class="button" id="%1$s">%2$s</button>',
296 + 'bogo-add-translation',
297 + esc_html( __( 'Add Translation', 'bogo' ) ) );
298 + echo '<span class="spinner"></span>';
299 + echo '</p>';
300 + echo '<div class="clear"></div>';
301 + echo '</div>';
302 + } while (0);
303 +}
304 +
305 +add_action( 'save_post', 'bogo_save_post', 10, 2 );
306 +
307 +function bogo_save_post( $post_id, $post ) {
308 + if ( did_action( 'import_start' ) && ! did_action( 'import_end' ) ) {
309 + // Importing
310 + return;
311 + }
312 +
313 + if ( ! bogo_is_localizable_post_type( $post->post_type ) ) {
314 + return;
315 + }
316 +
317 + $current_locales = get_post_meta( $post_id, '_locale' );
318 + $locale = null;
319 +
320 + if ( ! empty( $current_locales ) ) {
321 + foreach ( $current_locales as $current_locale ) {
322 + if ( bogo_is_available_locale( $current_locale ) ) {
323 + $locale = $current_locale;
324 + break;
325 + }
294 326 }
295 327
296 - $li_content .= sprintf(
297 - ' [%s]',
298 - $available_languages[$locale] ?? $locale
299 - );
328 + if ( empty( $locale ) || 1 < count( $current_locales ) ) {
329 + delete_post_meta( $post_id, '_locale' );
330 + $current_locales = array();
331 + }
332 + }
300 333
301 - echo wp_kses_post( sprintf( '<li>%s</li>', $li_content ) );
334 + if ( empty( $current_locales ) ) {
335 + if ( bogo_is_available_locale( $locale ) ) {
336 + // $locale = $locale;
337 + } elseif ( ! empty( $_REQUEST['locale'] )
338 + && bogo_is_available_locale( $_REQUEST['locale'] ) ) {
339 + $locale = $_REQUEST['locale'];
340 + } elseif ( 'auto-draft' == get_post_status( $post_id ) ) {
341 + $locale = bogo_get_user_locale();
342 + } else {
343 + $locale = bogo_get_default_locale();
344 + }
345 +
346 + add_post_meta( $post_id, '_locale', $locale, true );
302 347 }
303 348
304 -?>
305 -</ul>
306 -</div>
349 + $current_original_posts = get_post_meta( $post_id, '_original_post' );
307 350
308 -<?php
351 + if ( ! empty( $current_original_posts ) ) {
352 + if ( 1 < count( $current_original_posts ) ) {
353 + delete_post_meta( $post_id, '_original_post' );
354 + } else {
355 + return;
356 + }
357 + }
309 358
310 - $available_languages = array_diff_key( $available_languages, $translations );
359 + if ( ! empty( $_REQUEST['original_post'] ) ) {
360 + $original = get_post_meta( $_REQUEST['original_post'],
361 + '_original_post', true );
311 362
312 - if ( ! $initial and $available_languages ) {
363 + if ( empty( $original ) ) {
364 + $original = (int) $_REQUEST['original_post'];
365 + }
313 366
314 -?>
367 + add_post_meta( $post_id, '_original_post', $original, true );
368 + return;
369 + }
315 370
316 -<div class="descriptions" id="bogo-add-translation-actions">
317 -<p>
318 - <strong><?php echo wp_kses_data( __( 'Add Translation:', 'bogo' ) ); ?></strong>
319 -</p>
371 + $original = $post_id;
320 372
321 -<?php
373 + while ( 1 ) {
374 + $q = new WP_Query();
322 375
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 - ) );
376 + $posts = $q->query( array(
377 + 'bogo_suppress_locale_query' => true,
378 + 'posts_per_page' => 1,
379 + 'post_status' => 'any',
380 + 'post_type' => $post->post_type,
381 + 'meta_key' => '_original_post',
382 + 'meta_value' => $original ) );
383 +
384 + if ( empty( $posts ) ) {
385 + add_post_meta( $post_id, '_original_post', $original, true );
386 + return;
337 387 }
338 388
339 -?>
340 -<div class="clear"></div>
341 -</div>
342 -<?php
389 + $original += 1;
390 + }
391 +}
343 392
393 +add_filter( 'wp_unique_post_slug', 'bogo_unique_post_slug', 10, 6 );
394 +
395 +function bogo_unique_post_slug( $slug, $post_id, $status, $type, $parent, $original ) {
396 + global $wp_rewrite;
397 +
398 + if ( ! bogo_is_localizable_post_type( $type ) ) {
399 + return $slug;
344 400 }
401 +
402 + $feeds = is_array( $wp_rewrite->feeds ) ? $wp_rewrite->feeds : array();
403 +
404 + if ( in_array( $original, $feeds ) ) {
405 + return $slug;
406 + }
407 +
408 + $locale = bogo_get_post_locale( $post_id );
409 +
410 + if ( empty( $locale ) ) {
411 + return $slug;
412 + }
413 +
414 + $args = array(
415 + 'posts_per_page' => 1,
416 + 'post__not_in' => array( $post_id ),
417 + 'post_type' => $type,
418 + 'name' => $original,
419 + 'lang' => $locale );
420 +
421 + $hierarchical = in_array( $type, get_post_types( array( 'hierarchical' => true ) ) );
422 +
423 + if ( $hierarchical ) {
424 + if ( preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $original ) ) {
425 + return $slug;
426 + }
427 +
428 + $args['post_parent'] = $parent;
429 + }
430 +
431 + $q = new WP_Query();
432 + $posts = $q->query( $args );
433 +
434 + if ( empty( $posts ) ) {
435 + $slug = $original;
436 + }
437 +
438 + return $slug;
345 439 }