-
Notifications
You must be signed in to change notification settings - Fork 50
/
class-post-public.php
1669 lines (1464 loc) · 56.2 KB
/
class-post-public.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class for handling the public, content handling post types.
*
* @package Babble
* @since 0.1
*/
class Babble_Post_Public extends Babble_Plugin {
/**
* A simple flag to stop infinite recursion when syncing
* post meta places.
*
* @var boolean
**/
protected $no_meta_recursion;
/**
* A simple flag to stop infinite recursion in various
* places (except for post meta).
*
* @var boolean
**/
protected $no_recursion;
/**
* The shadow (translated) post types created by this plugin.
*
* @var array
**/
protected $post_types;
/**
* A structure describing the languages served by various post types.
*
* @var array
**/
protected $lang_map;
/**
* Another structure describing the languages served by various post types.
*
* @var array
**/
protected $lang_map2;
/**
* A version number to use for cache busting, database updates, etc
*
* @var int
**/
protected $version;
/**
* An array of query_vars and slugs for our shadow post types,
* we use changes to this to determine if rewrite rules
* need flushing.
*
* @var array
**/
protected $slugs_and_vars;
/**
* An array of Post IDs for posts that are in the process of
* being deleted.
*
* @var array
**/
protected $deleting_post_ids;
/**
* An array of meta_keys, indexed by meta_key, containing
* meta_keys we KNOW to be added as unique.
*
* @var array
**/
protected $unique_meta_keys;
public function __construct() {
$this->setup( 'babble-post-public', 'plugin' );
$this->add_action( 'added_post_meta', null, null, 4 );
$this->add_action( 'admin_init' );
$this->add_action( 'clean_post_cache' );
$this->add_action( 'body_class', null, null, 2 );
$this->add_action( 'before_delete_post', 'clean_post_cache' );
$this->add_action( 'deleted_post', 'clean_post_cache' );
$this->add_action( 'deleted_post_meta', null, null, 4 );
$this->add_action( 'load-post-new.php', 'load_post_new' );
$this->add_action( 'manage_pages_custom_column', 'manage_posts_custom_column', null, 2 );
$this->add_action( 'manage_posts_custom_column', 'manage_posts_custom_column', null, 2 );
$this->add_action( 'parse_request' );
$this->add_action( 'post_updated' );
$this->add_action( 'pre_get_posts', null, 11 );
$this->add_action( 'registered_post_type', null, null, 2 );
$this->add_action( 'transition_post_status', null, null, 3 );
$this->add_action( 'updated_post_meta', null, null, 4 );
$this->add_action( 'wp_before_admin_bar_render' );
$this->add_filter( 'add_menu_classes' );
$this->add_filter( 'add_post_metadata', null, null, 5 );
$this->add_filter( 'bbl_sync_meta_key', 'sync_meta_key', null, 2 );
$this->add_filter( 'manage_posts_columns', 'manage_posts_columns', null, 2 );
$this->add_filter( 'page_link', null, null, 2 );
$this->add_filter( 'post_link', 'post_type_link', null, 3 );
$this->add_filter( 'post_type_archive_link', null, null, 2 );
$this->add_filter( 'post_type_link', null, null, 3 );
$this->add_filter( 'get_sample_permalink', null, null, 5 );
$this->add_filter( 'single_template' );
$this->add_filter( 'the_posts', null, null, 2 );
$this->add_filter( 'bbl_translated_taxonomy', null, null, 2 );
$this->add_filter( 'admin_body_class' );
$this->initiate();
}
/**
* Initiates
*
* @return void
**/
public function initiate() {
$this->lang_map = array();
$this->post_types = array();
$this->slugs_and_vars = array();
$this->no_meta_recursion = false;
$this->deleting_post_ids = array();
$this->version = 9;
// Ensure we catch any existing language shadow post_types already registered
$core_post_types = array( 'post', 'page', 'attachment' );
if ( is_array( $this->post_types ) )
$post_types = array_merge( $core_post_types, array_keys( $this->post_types ) );
else
$post_types = $core_post_types;
register_taxonomy( 'post_translation', $post_types, array(
'rewrite' => false,
'public' => false,
'show_ui' => false,
'show_in_nav_menus' => false,
) );
}
/**
* Hooks the WP admin_init action to
*
* @return void
**/
public function admin_init() {
$this->maybe_upgrade();
$post_type = false;
if ( isset( $_GET[ 'post_type' ] ) ) {
$post_type = $_GET[ 'post_type' ];
} else if ( isset( $_GET[ 'post' ] ) ) {
$post = (int) $_GET[ 'post' ];
$post = get_post( $post );
$post_type = $post->post_type;
}
$menu_id = false;
if ( isset( $this->post_types[ $post_type ] ) )
$menu_id = '#menu-posts-' . $this->post_types[ $post_type ];
$data = array(
'menu_id' => $menu_id,
'is_default_lang' => (bool) ( bbl_get_current_lang_code() == bbl_get_default_lang_code() ),
'is_bbl_post_type' => (bool) ( 0 === strpos( $post_type, 'bbl_' ) ),
);
wp_enqueue_script( 'post-public-admin', $this->url( 'js/post-public-admin.js' ), array( 'jquery' ), filemtime( $this->dir( 'js/post-public-admin.js' ) ) );
wp_localize_script( 'post-public-admin', 'bbl_post_public', $data );
}
/**
* Initialise a translation for the given post.
*
* @param WP_Post|int $origin_post The origin post object or post ID
* @param string $lang_code The language code for the new translation
* @return WP_Post The translation post
*/
public function initialise_translation( $origin_post, $lang_code ) {
$origin_post = get_post( $origin_post );
$new_post_type = $this->get_post_type_in_lang( $origin_post->post_type, $lang_code );
$transid = $this->get_transid( $origin_post->ID );
// Insert translation:
$this->no_recursion = true;
$new_post_id = wp_insert_post( array(
'post_type' => $new_post_type,
'post_status' => 'draft',
), true );
$this->no_recursion = false;
$new_post = get_post( $new_post_id );
// Assign transid to translation:
$this->set_transid( $new_post, $transid );
// Copy all the metadata across
$this->sync_post_meta( $new_post->ID );
// Copy the various core post properties across
$this->sync_properties( $origin_post->ID, $new_post->ID );
do_action( 'bbl_created_new_shadow_post', $new_post->ID, $origin_post->ID );
return $new_post;
}
/**
* Hooks the WP load-post-new.php action to stop translators
* creating new posts in languages other than the default.
*
* @return void
**/
public function load_post_new() {
$screen = get_current_screen();
if ( 'post' != $screen->base || 'add' != $screen->action )
return;
if ( bbl_get_current_lang_code() == bbl_get_default_lang_code() )
return;
if ( !bbl_is_translated_post_type( $screen->post_type ) )
return;
wp_die( __( 'You can only create content in your site\'s default language. Please consult your editorial team.', 'babble' ), '', array( 'back_link' => true ) );
}
/**
* Hooks the WP wp_before_admin_bar_render action
* to prune out unneeded post type add controls from
* the add menu.
*
* @return void
**/
public function wp_before_admin_bar_render() {
global $wp_admin_bar;
if ( ! bbl_is_default_lang() )
$wp_admin_bar->remove_node( 'new-content' );
}
/**
* Hooks the WP registered_post_type action.
*
* @param string $post_type The post type which has just been registered.
* @param object $args The arguments with which the post type was registered
* @return void
**/
public function registered_post_type( $post_type, $args ) {
// Don't bother with non-public post_types for now
// @FIXME: This may need to change for menus?
if ( false === $args->public )
return;
// Don't shadow shadow post types, it's going to get silly
if ( in_array( $post_type, $this->post_types ) )
return;
if ( $this->no_recursion )
return;
$this->no_recursion = 'registered_post_type';
$langs = bbl_get_active_langs();
// Lose the default language as any existing post types are in that language
unset( $langs[ bbl_get_default_lang_url_prefix() ] );
// $args is an object at this point, but register_post_type needs an array
$args = get_object_vars( $args );
// @FIXME: Is it reckless to convert ALL object instances in $args to an array?
foreach ( $args as $key => & $arg ) {
if ( is_object( $arg ) )
$arg = get_object_vars( $arg );
// Don't set any args reserved for built-in post_types
if ( '_' == substr( $key, 0, 1 ) )
unset( $args[ $key ] );
}
$features = $this->get_features_supported_by_post_type( $post_type );
$args[ 'supports' ] = array();
foreach ( $features as $feature => $true )
$args[ 'supports' ][] = $feature;
// I am a little concerned that this argument may make things
// brittle, e.g. the UI might stop showing up in the shadow
// post type edit screens, p'raps.
$args[ 'show_ui' ] = true;
$slug = ( $args[ 'rewrite' ][ 'slug' ] ) ? $args[ 'rewrite' ][ 'slug' ] : $post_type;
$archive_slug = false;
if ( $archive_slug = $args[ 'has_archive' ] )
if ( ! is_string( $args[ 'has_archive' ] ) )
$archive_slug = $slug;
$current_lang_code = bbl_get_current_lang_code();
foreach ( $langs as $lang ) {
$new_args = $args;
// @FIXME: We are in danger of a post_type name being longer than 20 chars
// I would prefer to keep the post_type human readable, as human devs and sysadmins always
// end up needing to read this kind of thing.
// @FIXME: Should I be sanitising these values?
$new_post_type = strtolower( "{$post_type}_{$lang->code}" );
if ( strlen( $new_post_type ) > 20 ) {
trigger_error( sprintf( __( 'Warning: The translated name for the post type %s is longer than %d characters. This *will* cause problems.', 'babble' ),
esc_html( $post_type ),
20
) );
}
if ( false !== $args[ 'rewrite' ] ) {
if ( ! is_array( $new_args[ 'rewrite' ] ) )
$new_args[ 'rewrite' ] = array();
$new_args[ 'query_var' ] = $new_args[ 'rewrite' ][ 'slug' ] = $this->get_slug_in_lang( $slug, $lang, $args );
$new_args[ 'has_archive' ] = $this->get_slug_in_lang( $archive_slug, $lang );
}
$this->slugs_and_vars[ $lang->code . '_' . $post_type ] = array(
'query_var' => $new_args[ 'query_var' ],
'has_archive' => $new_args[ 'has_archive' ],
);
// Don't let the translated post types show up in the search if their
// language is not the current language.
if ( $lang->code != $current_lang_code ) {
$new_args['exclude_from_search'] = true;
$new_args['capabilities']['create_posts'] = 'do_not_allow';
}
$result = register_post_type( $new_post_type, $new_args );
if ( is_wp_error( $result ) ) {
error_log( "Error creating shadow post_type for $new_post_type: " . print_r( $result, true ) );
} else {
$this->post_types[ $new_post_type ] = $post_type;
$this->lang_map[ $new_post_type ] = $lang->code;
// @TODO: Refactor the $this::lang_map array so we can use this new structure instead
if ( ! isset( $this->lang_map2[ $lang->code ] ) || ! is_array( $this->lang_map2[ $lang->code ] ) )
$this->lang_map2[ $lang->code ] = array();
$this->lang_map2[ $lang->code ][ $post_type ] = $new_post_type;
// This will not work until init has run at the early priority used
// to register the post_translation taxonomy. However we catch all the
// post_types registered before the hook runs, so we don't miss any
// (take a look at where we register post_translation for more info).
register_taxonomy_for_object_type( 'post_translation', $new_post_type );
}
}
// Exclude the registered post type from search if it's language isn't
// the current language.
if ( $current_lang_code != bbl_get_default_lang_code() ) {
$post_type_obj = get_post_type_object( $post_type );
$post_type_obj->exclude_from_search = true;
}
do_action( 'bbl_registered_shadow_post_types', $post_type );
$this->no_recursion = false;
}
/**
* Store whether a particular meta_key is unique or not. Pretty hacky.
*
* @param null $null Follows a pattern for actions/filters relating to meta, but meta ID not set yet so null
* @param int $post_id The ID for the WordPress Post object this meta relates to
* @param string $meta_key The key for this meta entry
* @param mixed $meta_value The new value for this meta entry
* @param bool $unique Whether the meta_key should be unique
* @return null Always return null, or we are bypassing the meta save to DB
**/
public function add_post_metadata( $null, $post_id, $meta_key, $meta_value, $unique ) {
if ( $unique ) {
$this->unique_meta_keys[ $meta_key ] = $meta_key;
}
return null;
}
/**
* Hooks the WP added_post_meta action to sync metadata across to the
* translations in shadow post types.
*
* @param int $meta_id The ID for this meta entry
* @param int $post_id The ID for the WordPress Post object this meta relates to
* @param string $meta_key The key for this meta entry
* @param mixed $meta_value The new value for this meta entry
* @return void
**/
public function added_post_meta( $meta_id, $post_id, $meta_key, $meta_value ) {
// Some metadata shouldn't be synced
if ( ! apply_filters( 'bbl_sync_meta_key', true, $meta_key ) )
return;
if ( $this->no_meta_recursion )
return;
$this->no_meta_recursion = 'added_post_meta';
$unique = isset( $this->unique_meta_keys[ $meta_key ] );
$translations = $this->get_post_translations( $post_id );
foreach ( $translations as $lang_code => & $translation ) {
if ( $this->get_post_lang_code( $post_id ) == $lang_code )
continue;
add_post_meta( $translation->ID, $meta_key, $meta_value, $unique );
}
$this->no_meta_recursion = false;
}
/**
* Hooks the WP updated_post_meta action to sync metadata across to the
* translations in shadow post types.
*
* @param int $meta_id The ID for this meta entry
* @param int $post_id The ID for the WordPress Post object this meta relates to
* @param string $meta_key The key for this meta entry
* @param mixed $meta_value The new value for this meta entry
* @return void
**/
public function updated_post_meta( $meta_id, $post_id, $meta_key, $meta_value ) {
// Some metadata shouldn't be synced
if ( ! apply_filters( 'bbl_sync_meta_key', true, $meta_key ) )
return;
if ( $this->no_meta_recursion )
return;
$this->no_meta_recursion = 'updated_post_meta';
$translations = $this->get_post_translations( $post_id );
foreach ( $translations as $lang_code => & $translation ) {
if ( $this->get_post_lang_code( $post_id ) == $lang_code )
continue;
update_post_meta( $translation->ID, $meta_key, $meta_value );
}
$this->no_meta_recursion = false;
}
/**
* Hooks the WP deleted_post_meta action to sync metadata across to the
* translations in shadow post types.
*
* @param int $meta_id The ID for this meta entry
* @param int $post_id The ID for the WordPress Post object this meta relates to
* @param string $meta_key The key for this meta entry
* @param mixed $meta_value The new value for this meta entry
* @return void
**/
public function deleted_post_meta( $meta_id, $post_id, $meta_key, $meta_value ) {
// When we are deleting posts, we don't want to sync
// the metadata deletion across the other posts in
// the same translation group
if ( in_array( $post_id, $this->deleting_post_ids ) )
return;
// Some metadata shouldn't be synced
if ( ! apply_filters( 'bbl_sync_meta_key', true, $meta_key ) )
return;
if ( $this->no_meta_recursion )
return;
$this->no_meta_recursion = 'deleted_post_meta';
$translations = $this->get_post_translations( $post_id );
foreach ( $translations as $lang_code => & $translation ) {
if ( $this->get_post_lang_code( $post_id ) == $lang_code )
continue;
delete_post_meta( $translation->ID, $meta_key );
}
$this->no_meta_recursion = false;
}
/**
* Hooks the WP pre_get_posts ref action in the WP_Query,
* for the main query it does nothing, for other queries
* it switches the post types to our shadow post types.
*
* @param WP_Query $wp_query A WP_Query object, passed by reference
* @return void (param passed by reference)
**/
public function pre_get_posts( WP_Query & $query ) {
if ( false === $query->get( 'bbl_translate' ) ) {
return;
}
if ( $query->is_main_query() ) {
return;
}
# @TODO we should scrap this and more intelligently filter the QVs rather than basing it on whether we're on a media tab
if ( $this->is_media_upload_tab( 'gallery' ) ) {
return;
}
if ( $this->is_media_manager() ) {
return;
}
$query->query_vars = $this->translate_query_vars( $query->query_vars );
}
/**
* Hooks the WP parse_request action
*
* FIXME: Should I be extending and replacing the WP class?
*
* @param WP $wp WP object, passed by reference (so no need to return)
* @return void
**/
public function parse_request( WP & $wp ) {
if ( isset( $wp->query_vars['bbl_translate'] ) and ( false === $wp->query_vars['bbl_translate'] ) ) {
return;
}
if ( is_admin() ) {
return;
}
$wp->query_vars = $this->translate_query_vars( $wp->query_vars, $wp->request );
}
/**
* Hooks the WP the_posts filter on WP_Query.
*
* Check the post_title, post_excerpt, post_content and substitute from
* the default language where appropriate.
*
* @param array $posts The posts retrieved by WP_Query, passed by reference
* @param WP_Query $wp_query The WP_Query, passed by reference
* @return array The posts
**/
public function the_posts( array $posts, WP_Query & $wp_query ) {
if ( is_admin() )
return $posts;
// Get fallback content in the default language
$subs_index = array();
foreach ( $posts as & $post ) {
if ( empty( $post->post_title ) || empty( $post->post_excerpt ) || empty( $post->post_content ) ) {
if ( $default_post = bbl_get_default_lang_post( $post->ID ) )
$subs_index[ $post->ID ] = $default_post->ID;
}
if ( ! $this->get_transid( $post ) && bbl_get_default_lang_code() == bbl_get_post_lang_code( $post ) )
$this->set_transid( $post );
}
if ( ! $subs_index )
return $posts;
$subs_posts = get_posts( array( 'include' => array_values( $subs_index ), 'post_status' => 'publish' ) );
// @FIXME: Check the above get_posts call results are cached somewhere… I think they are
// @FIXME: Alternative approach: hook on save_post to save the current value to the translation, BUT content could get out of date – in post_content_filtered
foreach ( $posts as & $post ) {
// @TODO why does this only override the title/excerpt/content? Why not override the post object entirely?
// @FIXME: I'm assuming this get_post call is cached, which it seems to be
if( isset( $subs_index[ $post->ID ] ) ) {
$default_post = get_post( $subs_index[ $post->ID ] );
if ( empty( $post->post_title ) )
$post->post_title = $default_post->post_title;
if ( empty( $post->post_excerpt ) )
$post->post_excerpt = $default_post->post_excerpt;
if ( empty( $post->post_content ) )
$post->post_content = $default_post->post_content;
}
}
return $posts;
}
/**
* Hooks the WP body_class filter to add classes to the
* body element.
*
* @param array $classes An array of class strings, poss with some indexes containing more than one space separated class
* @param string|array $class One or more classes which have been added to the class list.
* @return array An array of class strings, poss with some indexes containing more than one space separated class
**/
public function body_class( array $classes, $class ) {
// Shadow post_type archives also get the post_type class for
// the default language
if ( is_post_type_archive() && ! bbl_is_default_lang() )
$classes[] = 'post-type-archive-' . bbl_get_post_type_in_lang( get_query_var( 'post_type' ), bbl_get_default_lang_code() );
if ( is_single() )
$classes[] = 'single-' . bbl_get_base_post_type( get_post_type() );
return $classes;
}
/**
* Hooks the WP post_type_link filter
*
* @param string $post_link The permalink
* @param object $post The WP Post object being linked to
* @return string The permalink
**/
public function post_type_link( $post_link, $post, $leavename ) {
global $wp_rewrite;
// Regular ol' post types, and other types added by other plugins, etc
if ( 'post' == $post->post_type || 'page' == $post->post_type || ! isset( $this->post_types[ $post->post_type ] ) )
return user_trailingslashit( $post_link );
// Deal with our shadow post types
if ( ! ( $base_post_type = $this->get_base_post_type( $post->post_type ) ) )
return user_trailingslashit( $post_link );
// Deal with post_types shadowing the post post_type
if ( 'post' == $base_post_type ) {
// @FIXME: Probably move this into another function
// @FIXME: Is there any way I can provide an appropriate permastruct so I can avoid having to copy all this code, with the associated maintenance headaches?
// START copying from get_permalink function
// N.B. The $permalink var is replaced with $post_link
$rewritecode = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
'%postname%',
'%post_id%',
'%category%',
'%author%',
'%pagename%',
);
$post_link = get_option('permalink_structure');
// @FIXME: Should I somehow fake this, so plugin authors who hook it still get some consequence?
// $post_link = apply_filters('pre_post_link', $post_link, $post, $leavename);
if ( '' != $post_link && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
$unixtime = strtotime($post->post_date);
$category = '';
if ( strpos($post_link, '%category%') !== false ) {
$cats = get_the_category($post->ID);
if ( $cats ) {
usort($cats, '_usort_terms_by_ID'); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent )
$category = get_category_parents($parent, false, '/', true) . $category;
}
// show default category in permalinks, without
// having to assign it explicitly
if ( empty($category) ) {
$default_category = get_category( get_option( 'default_category' ) );
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
}
}
$author = '';
if ( strpos($post_link, '%author%') !== false ) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$date = explode(" ",date('Y m d H i s', $unixtime));
$rewritereplace =
array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->post_name,
$post->ID,
$category,
$author,
$post->post_name,
);
$lang = bbl_get_post_lang_code( $post );
bbl_switch_to_lang( $lang );
$post_link = home_url( str_replace( $rewritecode, $rewritereplace, $post_link ) );
bbl_restore_lang();
$post_link = user_trailingslashit($post_link, 'single');
// END copying from get_permalink function
return $post_link;
} else { // if they're not using the fancy permalink option the link won't work. Known bug. :)
return $post_link;
}
} else if ( 'page' == $base_post_type ) {
return get_page_link( $post->ID, $leavename );
}
return user_trailingslashit( $post_link );
}
/**
* Hooks the get_sample_permalink filter to provide a correct sample permalink
* in situations where the post_name has been hacked for a particular context.
*
* @filter get_sample_permalink (not yet in existence, see http://core.trac.wordpress.org/attachment/ticket/22338)
*
* @param array $permalink The array, like array( $permalink, $post_name )
* @param string $title A desired title (could be null)
* @param string $name A desired post name (could be null)
* @param int $id The Post ID
* @param object $post A (hacked) Post object
* @return array The array, like array( $permalink, $post_name )
*/
public function get_sample_permalink( $permalink, $title, $name, $id, $post ) {
$permalink[ 0 ] = $this->post_type_link( $permalink[ 0 ], $post, $leavename );
return $permalink;
}
/**
* Hooks the WP page_link filter to ensure correct virtual language directory prefix, etc.
*
* @param string $link The permalink for the page
* @param int $id The ID for the post represented by this permalink
* @return string
**/
public function page_link( $link, $post_id ) {
if ( $this->no_recursion )
return $link;
// Deal with the language front pages
if ( 'page' == get_option('show_on_front') && $page_on_front = get_option( 'page_on_front' ) ) {
$front_page_transid = $this->get_transid( $page_on_front );
$this_transid = $this->get_transid( $post_id );
if ( $front_page_transid == $this_transid ) {
bbl_switch_to_lang( bbl_get_post_lang_code( $post_id ) );
$link = home_url();
bbl_restore_lang();
return $link;
}
}
$this->no_recursion = 'page_link';
$lang = bbl_get_post_lang_code( $post_id );
bbl_switch_to_lang( $lang );
$link = get_page_link( $post_id );
bbl_restore_lang();
$this->no_recursion = false;
return $link;
}
/**
* Hooks the WP post_type_archive_link filter to return the correct
* post type archive link for the current language.
*
* @param string $link The link to the post type archive (probably wrong for this language)
* @param string $post_type The post_type we need an archive for (though we'll probably need to use a translated (shadow) post_type)
* @return string A URL for the translated (shadow) post_type archive
**/
public function post_type_archive_link( $link, $post_type ) {
if ( $this->no_recursion )
return $link;
$this->no_recursion = 'post_type_archive_link';
$lang_post_type = $this->get_post_type_in_lang( $post_type, bbl_get_current_lang_code() );
bbl_switch_to_lang( bbl_get_current_lang_code() );
$link = get_post_type_archive_link( $lang_post_type );
bbl_restore_lang();
$this->no_recursion = false;
return $link;
}
/**
* Hooks the WP clean_post_cache action to clear the Babble
* post translation and transid caches.
*
* Occasionally called directly by within this class.
*
* @param int $post_id The ID of the post to clear the caches for
* @return void
**/
function clean_post_cache( $post_id ) {
wp_cache_delete( $post_id, 'bbl_post_transids' );
// clean_post_cache gets called in some situations where
// the post is already deleted, in which case do not
// force the creation of a transid.
if ( ! $transid = $this->get_transid( $post_id, false ) ) {
return;
}
wp_cache_delete( $transid, 'bbl_post_translations' );
}
/**
* Hooks the WP post_updated action to ensure that the
* required properties are copied to the other posts in
* this translation group.
*
* @param int $post_id The ID of the post being updated
* @return void
**/
public function post_updated( $post_id ) {
if ( $this->no_recursion )
return;
$this->no_recursion = 'post_updated';
$transid = $this->get_transid( $post_id );
$this->clean_post_cache( $post_id );
$translations = $this->get_post_translations( $post_id );
foreach ( $translations as $lang_code => & $translation ) {
if ( $translation->ID == $post_id )
continue;
// Copy the various core post properties across
$this->sync_properties( $post_id, $translation->ID );
}
// Revert comment status, which often gets turned off by
// auto drafts.
$post_lang_code = bbl_get_post_lang_code( $post_id );
if ( bbl_get_default_lang_code() != $post_lang_code ) {
$source_post = bbl_get_post_in_lang( $post_id, bbl_get_default_lang_code() );
$target_post = get_post( $post_id );
$post_data = array(
'ID' => $post_id,
'comment_status' => $source_post->comment_status,
'post_modified' => $target_post->post_modified,
'post_modified_gmt' => $target_post->post_modified_gmt,
);
wp_update_post( $post_data );
}
$this->no_recursion = false;
}
/**
* Hooks the WP transition_post_status action which fires whenever
* a post status changes through use of wp_transition_post_status.
*
* @param string $new_status The new status
* @param string $old_status The old status
* @param object $post The post object
* @return void
**/
public function transition_post_status( $new_status, $old_status, $post ) {
if ( $new_status == $old_status )
return;
if ( $this->no_recursion ) {
return;
}
$this->no_recursion = 'transition_post_status';
if ( 'publish' == $new_status && $new_status != $old_status ) {
// Ensure the date of publication of a translation gets
// sync'd immediately with the original language post.
if ( bbl_get_default_lang_code() != bbl_get_post_lang_code( $post->ID ) ) {
$source_post = bbl_get_post_in_lang( $post->ID, bbl_get_default_lang_code() );
$postdata = array(
'ID' => $post->ID,
'post_date' =>$source_post->post_date,
);
wp_update_post( $postdata );
}
}
$this->no_recursion = false;
}
/**
* Hooks the WP add_menu_classes filter to fixup the side
* admin menu.
*
* @param array $menu The WP admin menu
* @return array The WP admin menu
**/
public function add_menu_classes( $menu ) {
global $submenu;
$lang = bbl_get_current_lang_code();
$default = bbl_get_default_lang_code();
// Remove "new post" links from submenu(s) for non-default languages
if ( $lang != $default ) {
foreach ( $submenu as $parent => $items ) {
foreach ( $items as $key => $item ) {
if ( 'post-new.php' == substr( $item[ 2 ], 0, 12 ) ) {
unset( $submenu[ $parent ][ $key ] );
}
}
}
}
// Remove links to shadow post types
foreach ( $menu as $key => $item ) {
$vars = array();
$url_info = parse_url( $item[ 2 ] );
if ( ! isset( $url_info[ 'query' ] ) )
continue;
parse_str( $url_info[ 'query' ], $vars );
if ( ! isset( $vars[ 'post_type' ] ) || ! isset( $this->post_types[ $vars[ 'post_type' ] ] ) )
continue;
unset( $menu[ $key ] );
}
return $menu;
}
/**
* Hooks the WP filter single_template to deal with the shadow post
* types for pages and singular templates, ensuring they use the
* right template.
*
* @param string $template Path to a template file
* @return Path to a template file
**/
public function single_template( $template ) {
if( bbl_is_default_lang() )
return $template;
// Deal with the language front pages and custom page templates
$post = get_post( get_the_ID() );
if ( 'page' == get_option('show_on_front') ) {
$front_page_transid = $this->get_transid( get_option( 'page_on_front' ) );
$this_transid = $this->get_transid( get_the_ID() );
// Check if this is a translation of the page on the front of the site
if ( $front_page_transid == $this_transid ) {
// global $wp_query, $wp;
if ( 'page' == $this->get_base_post_type( $post->post_type ) ) {
if ( $custom_page_template = get_post_meta( get_option( 'page_on_front' ), '_wp_page_template', true ) )
$templates = (array) $custom_page_template;
else
$templates = (array) 'page.php';
if ( $_template = locate_template( $templates ) ) {
return $_template;
}
}
}
}
// Check if we're dealing with a page or a translation of a page
if ( 'page' == $this->get_base_post_type( $post->post_type ) ) {
$custom_page_template = get_post_meta( get_the_ID(), '_wp_page_template', true );
if ( $custom_page_template && 'default' != $custom_page_template )
$templates = (array) $custom_page_template;
else
$templates = array( 'page.php' );
if ( $_template = locate_template( $templates ) ) {
return $_template;
}
}
$templates[] = "single-{$this->get_base_post_type($post->post_type)}.php";
$templates[] = "single.php";
$template = get_query_template( 'bbl-single', $templates );
return $template;
}
/**
* Hooks the bbl_sync_meta_key filter from this class which checks
* if a meta_key should be synced. If we return false, it won't be.
*
* @TODO correct inline docs
**/
function sync_meta_key( $sync, $meta_key ) {
$sync_not = array(
'_edit_last', // Related to edit lock, should be individual to translations
'_edit_lock', // The edit lock, should be individual to translations
'_bbl_default_text_direction', // The text direction, should be individual to translations
'_wp_trash_meta_status',
'_wp_trash_meta_time',
);
if ( in_array( $meta_key, $sync_not ) )
$sync = false;
return $sync;
}
/**
* Hooks the WP manage_posts_columns filter to add our “link” column.
*
* @param array $cols The columns for this post type lists table
* @param string $post_type The post type for this lists table
* @return array The columns
**/
public function manage_posts_columns( array $columns, $post_type ) {
// Insert our cols just before comments, or date.
if ( $post_type == bbl_get_post_type_in_lang( $post_type, bbl_get_default_lang_code() ) )
return $columns;
# @TODO is this phrase localisable? Might need changing.
$columns[ 'bbl_link' ] = __( 'Translation of', 'babble' );
return $columns;
}
/**
* Hooks the WP manage_posts_custom_column action to add our “link” content.
*
* @param string $column_name The name of this column
* @param int $post_id The ID for the post for the row which parents this column
* @return void
**/
public function manage_posts_custom_column( $column_name, $post_id ) {
if ( 'bbl_link' != $column_name )
return;
$default_post = bbl_get_post_in_lang( $post_id, bbl_get_default_lang_code() );
if ( ! $default_post ) {
echo '<em style="color: #bc0b0b">' . __( 'No link', 'babble' ) . '</em>';
return;
}
$edit_link = get_edit_post_link( $default_post->ID );
$edit_link = add_query_arg( array( 'lang' => bbl_get_default_lang_code() ), $edit_link );
bbl_switch_to_lang( bbl_get_default_lang_code() );
$view_link = get_permalink( $default_post->ID );