forked from Automattic/babble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-switcher-content.php
551 lines (513 loc) · 19.5 KB
/
class-switcher-content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
/**
* Class for providing the switch/create links for the current
* content items. Works in the admin or public areas of the site.
*
* Used by the admin bar class, for example.
*
* @package Babble
* @since 0.2
*/
class Babble_Switcher_Menu {
/**
* The translations for the current content item.
*
* @var array
**/
protected $translations;
/**
* A multi-dimensional array of the links for the current
* translation structure.
*
* @var array
**/
protected $links;
/**
* The WP Screen object
*
* @var object
**/
protected $screen;
// PUBLIC METHODS
// ==============
/**
* Returns an array of links to the other objects
* in this translation group. Each element in the array
* looks like:
* array (
*
* )
*
* @param string $id_prefix A prefix to the ID for each item
* @return array An array of link info for the objects in this current translation group.
**/
public function get_switcher_links( $id_prefix ) {
$this->populate_links();
return $this->links;
}
// PRIVATE/PROTECTED METHODS
// =========================
/**
* undocumented function
*
* @return void
**/
protected function populate_links() {
if ( is_array( $this->links ) && ! empty( $this->links ) )
return; // Already done
$this->links = array();
// @FIXME: Not sure this is the best way to specify languages
$alt_langs = bbl_get_active_langs();
$this->screen = is_admin() ? get_current_screen() : false;
// Create a handy flag for whether we're editing a post or listing posts
$editing_post = false;
$listing_posts = false;
if ( is_admin() ) {
$editing_post = ( is_admin() && 'post' == $this->screen->base && isset( $_GET[ 'post' ] ) );
$listing_posts = ( is_admin() && 'edit' == $this->screen->base && ! isset( $_GET[ 'post' ] ) );
}
// Create a handy flag for whether we're editing a term or listing terms
$editing_term = false;
$listing_terms = false;
if ( is_admin() ) {
$editing_term = ( is_admin() && 'edit-tags' == $this->screen->base && isset( $_GET[ 'tag_ID' ] ) );
$listing_terms = ( is_admin() && 'edit-tags' == $this->screen->base && ! isset( $_GET[ 'tag_ID' ] ) );
}
if ( is_singular() || is_single() || $editing_post ) {
$this->translations = bbl_get_post_translations( get_the_ID() );
$this->jobs = bbl_get_incomplete_post_jobs( get_the_ID() );
} else if ( 'page' == get_option( 'show_on_front' ) && is_home() ) {
$this->translations = bbl_get_post_translations( get_option( 'page_for_posts' ) );
$this->jobs = bbl_get_incomplete_post_jobs( get_option( 'page_for_posts' ) );
} else if ( ( !is_admin() and ( is_tax() || is_category() ) ) || $editing_term ) {
if ( isset( $_REQUEST[ 'tag_ID' ] ) )
$term = get_term( (int) @ $_REQUEST[ 'tag_ID' ], $this->screen->taxonomy );
else
$term = get_queried_object();
$this->translations = bbl_get_term_translations( $term->term_id, $term->taxonomy );
$this->jobs = bbl_get_term_jobs( $term->term_id, $term->taxonomy );
}
foreach ( $alt_langs as $i => & $alt_lang ) {
// @TODO: Convert to a switch statement, convert all the vars to a single property on the class
if ( is_admin() ) {
if ( $editing_post ) { // Admin: Editing post link
$this->add_admin_post_link( $alt_lang );
} else if ( $editing_term ) { // Admin: Editing term link
$this->add_admin_term_link( $alt_lang );
} else if ( $listing_posts ) { // Admin: Listing posts link
$this->add_admin_list_posts_link( $alt_lang );
} else if ( $listing_terms ) { // Admin: Listing terms link
$this->add_admin_list_terms_link( $alt_lang );
} else { // Admin: Generic link link
$this->add_admin_generic_link( $alt_lang );
}
continue;
}
if (
is_singular() || is_single() ||
( 'page' == get_option( 'show_on_front' ) && is_home() )
) { // Single posts, pages, blog homepage
$this->add_post_link( $alt_lang );
continue;
}
// Don't add a switcher link if the language is not public and
// the user cannot edit any posts (as a rough guide to whether
// they are more than just a subscriber).
// @TODO this cap check should move into each add_*_link() method:
if ( ! bbl_is_public_lang( $alt_lang->code ) && ! current_user_can( 'edit_posts' ) )
continue;
if ( is_front_page() ) { // Language homepage
// is_front_page works for language homepages, phew
$this->add_front_page_link( $alt_lang );
} else if ( is_post_type_archive() ) { // Post type archives
$this->add_post_type_archive_link( $alt_lang );
} else if ( is_tax() || is_category() ) { // Category or taxonomy archive
$this->add_taxonomy_archive_link( $alt_lang );
} else { // 404's, amongst other things
$this->add_arbitrary_link( $alt_lang );
}
}
// Make up the class attribute on all links
foreach ( $this->links as $lang_code => & $link ){
$link[ 'class' ] = implode( ' ', $link[ 'classes' ] );
$link[ 'active' ] = $lang_code == bbl_get_current_lang_code();
}
}
/**
* Add an admin link to the same page, but with the language switch GET
* parameter set.
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_admin_generic_link( $lang ) {
$classes = array();
$href = add_query_arg( array( 'lang' => $lang->code ) );
$href = apply_filters( 'bbl_switch_admin_generic_link', $href, $lang );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-admin';
$classes[] = 'bbl-admin-generic';
$classes[] = 'bbl-lang';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
// Preventing errors on initial plugin load - before settings saved the first time
if ( empty( $lang->display_name ) ) {
$lang->display_name = '';
}
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add an admin term link to the parent link provided (by reference).
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_admin_term_link( $lang ) {
$classes = array();
if ( isset( $this->translations[ $lang->code ]->term_id ) ) { // Translation exists
$args = array(
'lang' => $lang->code,
'taxonomy' => $this->translations[ $lang->code ]->taxonomy,
'tag_ID' => $this->translations[ $lang->code ]->term_id
);
$href = add_query_arg( $args );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-existing-edit';
$classes[] = 'bbl-existing-edit-term';
} else if ( isset( $this->jobs[ $lang->code ]->ID ) ) { // Translation job exists
$href = get_edit_post_link( $this->jobs[ $lang->code ]->ID, 'url' );
$title = sprintf( _x( '%s: %s', 'Translation job status and language (example: In Progress: French)', 'babble' ), get_post_status_object( $this->jobs[ $lang->code ]->post_status )->label, $lang->display_name );
$classes[] = 'bbl-job-edit';
$classes[] = 'bbl-job-edit-term';
} else { // Translation does not exist
$default_term = (int) $_GET[ 'tag_ID' ];
$href = bbl_get_new_term_translation_url( $default_term, $lang->code, $this->screen->taxonomy );
$title = sprintf( __( 'Create for %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-add';
$classes[] = 'bbl-add-term';
}
$href = apply_filters( 'bbl_switch_admin_term_link', $href, $lang, $this->translations );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-admin';
$classes[] = 'bbl-admin-taxonomy';
$classes[] = 'bbl-admin-edit-term';
$classes[] = 'bbl-lang';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add an admin list terms screen link to the parent link provided (by reference).
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_admin_list_terms_link( $lang ) {
$classes = array();
$args = array(
'lang' => $lang->code,
'taxonomy' => bbl_get_taxonomy_in_lang( $this->screen->taxonomy, $lang->code ),
);
$href = add_query_arg( $args );
$href = apply_filters( 'bbl_switch_admin_list_terms_link', $href, $lang );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-admin';
$classes[] = 'bbl-admin-taxonomy';
$classes[] = 'bbl-admin-list-terms';
$classes[] = 'bbl-lang';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add an admin post link to the parent link provided (by reference)
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_admin_post_link( $lang ) {
$classes = array();
if ( isset( $this->translations[ $lang->code ]->ID ) ) { // Translation exists
$href = add_query_arg( array( 'lang' => $lang->code, 'post' => $this->translations[ $lang->code ]->ID ) );
$href = remove_query_arg( 'message', $href );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-existing-edit';
$classes[] = 'bbl-existing-edit-post';
} else if ( isset( $this->jobs[ $lang->code ]->ID ) ) { // Translation job exists
$href = add_query_arg( array( 'lang' => $lang->code, 'post' => $this->jobs[ $lang->code ]->ID ) );
$href = remove_query_arg( 'message', $href );
$title = sprintf( _x( '%s: %s', 'Translation job status and language (example: In Progress: French)', 'babble' ), get_post_status_object( $this->jobs[ $lang->code ]->post_status )->label, $lang->display_name );
$classes[] = 'bbl-job-edit';
$classes[] = 'bbl-job-edit-post';
} else { // Translation does not exist
if ( isset( $this->translations[ bbl_get_default_lang_code() ] ) ) {
$default_post = $this->translations[ bbl_get_default_lang_code() ];
$href = bbl_get_new_post_translation_url( $default_post, $lang->code );
$title = sprintf( __( 'Create for %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-add';
$classes[] = 'bbl-add-post';
} else {
return; // Don't create the switcher menu items yet
}
}
$href = apply_filters( 'bbl_switch_admin_post_link', $href, $lang, $this->translations );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-admin';
$classes[] = 'bbl-admin-edit-post';
$classes[] = 'bbl-admin-post-type';
$classes[] = 'bbl-lang';
$classes[] = 'bbl-post';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add an admin list posts screen link to the parent link provided (by reference).
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_admin_list_posts_link( $lang ) {
$classes = array();
$args = array(
'lang' => $lang->code,
'post_type' => bbl_get_post_type_in_lang( $this->screen->post_type, $lang->code ),
);
$href = add_query_arg( $args );
$href = apply_filters( 'bbl_switch_admin_list_posts_link', $href, $lang );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-admin';
$classes[] = 'bbl-admin-edit-post';
$classes[] = 'bbl-admin-post-type';
$classes[] = 'bbl-lang';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add a post link to the parent link provided (by reference)
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_post_link( $lang ) {
$classes = array();
if ( isset( $this->translations[ $lang->code ] ) ) { // Translation exists
// Don't add this link if the user cannot edit THIS post and
// the language is not public.
if (
! bbl_is_public_lang( $lang->code ) &&
! current_user_can( 'edit_post', $this->translations[ $lang->code ]->ID )
) {
return;
}
bbl_switch_to_lang( $lang->code );
$href = get_permalink( $this->translations[ $lang->code ]->ID );
bbl_restore_lang();
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-existing';
$classes[] = 'bbl-existing-post';
} else if ( current_user_can( 'edit_post', $this->translations[ bbl_get_default_lang_code() ]->ID ) ) {
// Generate a URL to create the translation
$default_post = $this->translations[ bbl_get_default_lang_code() ];
$href = bbl_get_new_post_translation_url( $default_post, $lang->code );
$title = sprintf( __( 'Create for %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-add';
$classes[] = 'bbl-add-post';
} else {
// Don't show links to non-public languages
if ( ! bbl_is_public_lang( $lang->code ) )
return;
// Show a blank link for unavailable translations
$href = false;
$title = sprintf( __( 'This content is unavailable in %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-unavailable';
}
$href = apply_filters( 'bbl_switch_post_link', $href, $lang, $this->translations );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-lang';
$classes[] = 'bbl-post';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add a link to a language specific front page.
*
* @TODO: Is this any different from a regular post link?
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_front_page_link( $lang ) {
global $bbl_locale;
$classes = array();
remove_filter( 'home_url', array( $bbl_locale, 'home_url'), null, 2 );
$href = home_url( "$lang->url_prefix/" );
add_filter( 'home_url', array( $bbl_locale, 'home_url'), null, 2 );
$href = apply_filters( 'bbl_switch_front_page_link', $href, $lang );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-existing';
$classes[] = 'bbl-front-page';
$classes[] = 'bbl-lang';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'id' => $lang->url_prefix,
'href' => $href,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add a link to a post_type archive.
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_post_type_archive_link( $lang ) {
$classes = array();
bbl_switch_to_lang( $lang->code );
$href = get_post_type_archive_link( get_query_var( 'post_type' ) );
bbl_restore_lang();
$href = apply_filters( 'bbl_switch_post_type_archive_link', $href, $lang );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-existing';
$classes[] = 'bbl-post-type-archive';
$classes[] = 'bbl-lang';
if ( $lang->code == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'id' => $lang->url_prefix,
'href' => $href,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add a link to a taxonomy archive.
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_taxonomy_archive_link( $lang ) {
$classes = array();
$queried_object = get_queried_object();
if ( ! bbl_is_translated_taxonomy( $queried_object->taxonomy ) ) {
$this->add_arbitrary_link( $lang );
return;
} elseif ( isset( $this->translations[ $lang->code ]->term_id ) ) { // Translation exists
bbl_switch_to_lang( $lang->code );
$href = get_term_link( $this->translations[ $lang->code ], bbl_get_base_taxonomy( $queried_object->taxonomy ) );
bbl_restore_lang();
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-existing';
$classes[] = 'bbl-existing-term';
} else { // Translation does not exist
// Generate a URL to create the translation
$default_term = $this->translations[ bbl_get_default_lang_code() ];
$href = bbl_get_new_term_translation_url( $default_term->term_id, $lang->code, $default_term->taxonomy );
$title = sprintf( __( 'Create for %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-add';
$classes[] = 'bbl-add-term';
}
$href = apply_filters( 'bbl_switch_taxonomy_archive_link', $href, $lang, $this->translations );
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-lang';
$classes[] = 'bbl-term';
if ( $lang == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
/**
* Add a link to an arbitrary link, e.g. 404, within the site.
*
* @param object $lang A Babble language object for this link
* @return void
**/
protected function add_arbitrary_link( $lang ) {
$classes = array();
if ( ! preg_match( '|^/[^/]+/(.*)?|', $_SERVER[ 'REQUEST_URI' ], $matches ) )
return;
bbl_switch_to_lang( $lang->code );
$href = home_url( $matches[ 1 ] );
bbl_restore_lang();
$href = apply_filters( 'bbl_switch_arbitrary_link', $href, $lang );
$title = sprintf( __( 'Switch to %s', 'babble' ), $lang->display_name );
$classes[] = 'bbl-existing';
$classes[] = 'bbl-existing-term';
$classes[] = "bbl-lang-$lang->code bbl-lang-$lang->url_prefix";
$classes[] = 'bbl-lang';
$classes[] = 'bbl-term';
if ( $lang == bbl_get_current_lang_code() )
$classes[] = 'bbl-active';
$this->links[ $lang->code ] = array(
'classes' => $classes,
'href' => $href,
'id' => $lang->url_prefix,
'meta' => array( 'class' => strtolower( join( ' ', array_unique( $classes ) ) ) ),
'title' => $title,
'lang' => $lang,
);
}
}
global $bbl_switcher_menu;
$bbl_switcher_menu = new Babble_Switcher_Menu();