-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpsstm-core-tracks.php
1427 lines (1069 loc) · 47.8 KB
/
wpsstm-core-tracks.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 WPSSTM_Core_Tracks{
static $artist_taxonomy = 'wpsstm_artist';
static $track_taxonomy = 'wpsstm_track';
static $album_taxonomy = 'wpsstm_album';
static $duration_metakey = '_wpsstm_length_ms';
static $image_url_metakey = '_wpsstm_track_image_url';
function __construct() {
global $wpsstm_track;
add_action( 'init', array($this,'register_track_post_type' ));
add_action( 'init', array($this,'register_track_taxonomy' ));
/*
populate single global track.
Be sure it works frontend, backend, and on post-new.php page
*/
$wpsstm_track = new WPSSTM_Track();
add_action( 'wp', array($this, 'populate_global_track_frontend'),1 );
add_action( 'admin_head', array($this, 'populate_global_track_backend'),1);
add_action( 'the_post', array($this,'populate_global_track_loop'),10,2);
add_filter( 'query_vars', array($this,'add_query_vars_track') );
add_action( 'wp', array($this,'handle_track_action'), 8);
add_filter( 'the_title', array($this, 'the_track_post_title'), 9, 2 );
//rewrite rules
add_action('wp_loaded', array($this, 'tracks_rewrite_rules') );
add_action( 'wp_enqueue_scripts', array( $this, 'register_tracks_scripts_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_tracks_scripts_styles' ) );
add_action( 'wpsstm_register_submenus', array( $this, 'backend_tracks_submenu' ) );
add_action( 'add_meta_boxes', array($this, 'metabox_track_register'));
add_action( 'save_post', array($this,'metabox_save_music_details'), 5);
add_filter( sprintf('manage_%s_posts_columns',wpsstm()->post_type_track), array(__class__,'tracks_columns_register') );
add_action( sprintf('manage_%s_posts_custom_column',wpsstm()->post_type_track), array(__class__,'tracks_columns_content') );
add_filter( sprintf("views_edit-%s",wpsstm()->post_type_track), array(__class__,'register_orphan_tracks_view') );
add_filter( sprintf("views_edit-%s",wpsstm()->post_type_track), array(__class__,'register_tracklist_tracks_view') );
add_filter( sprintf("views_edit-%s",wpsstm()->post_type_track), array(wpsstm(),'register_imported_view'), 5 );
//track shortcode
add_shortcode( 'wpsstm-track', array($this, 'shortcode_track'));
/* manager */
add_action( 'wp', array($this,'handle_manager_action'), 8);
add_filter( 'template_include', array($this,'manager_template'));
/*
QUERIES
*/
//TOUFIX TOUCHECK clean this ?
add_filter( 'pre_get_posts', array($this,'filter_single_subtrack_query') );
add_filter( 'posts_join', array($this,'include_subtracks_query_join'), 10, 2 );
add_filter( 'posts_join', array($this,'exclude_subtracks_query_join'), 10, 2 );
add_filter( 'posts_fields', array($this,'tracks_query_subtrack_ids'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_exclude_subtracks'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_where_tracklist_id'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_where_subtrack_id'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_where_subtrack_in'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_where_subtrack_position'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_where_subtrack_author'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_where_playing'), 10, 2 );
add_filter( 'posts_where', array($this,'track_query_where_favorited'), 10, 2 );
add_filter( 'posts_orderby', array($this,'tracks_query_sort_by_subtrack_position'), 10, 2 );
add_filter( 'posts_orderby', array($this,'tracks_query_sort_by_subtrack_time'), 10, 2 );
/*
AJAX
*/
add_action('wp_ajax_wpsstm_get_track_links_autolinked', array($this,'ajax_get_track_links_autolinked'));
add_action('wp_ajax_nopriv_wpsstm_get_track_links_autolinked', array($this,'ajax_get_track_links_autolinked'));
add_action('wp_ajax_nopriv_wpsstm_update_subtrack_position', array($this,'ajax_update_subtrack_position'));
add_action('wp_ajax_wpsstm_update_subtrack_position', array($this,'ajax_update_subtrack_position'));
add_action('wp_ajax_wpsstm_track_toggle_favorite', array($this,'ajax_track_toggle_favorite'));
add_action('wp_ajax_wpsstm_subtrack_dequeue', array($this,'ajax_subtrack_dequeue'));
add_action('wp_ajax_wpsstm_track_trash', array($this,'ajax_track_trash'));
add_action('wp_ajax_wpsstm_update_track_links_order', array($this,'ajax_update_track_links_order'));
/*
DB relationships
*/
add_action( 'before_delete_post', array($this,'delete_subtracks') );
add_action( 'before_delete_post', array($this,'unparent_track_links') );
}
function populate_global_track_frontend(){
global $post;
global $wpsstm_track;
if ( !is_single() || ( get_post_type() != wpsstm()->post_type_track ) ) return;
$wpsstm_track = new WPSSTM_Track($post);
}
function populate_global_track_backend(){
global $post;
global $wpsstm_track;
//is posts.php or post-new.php ?
$screen = get_current_screen();
$is_track_backend = ( $screen->id == wpsstm()->post_type_track );
if ( !$is_track_backend ) return;
$wpsstm_track = new WPSSTM_Track($post);
}
/*
Register the global within posts loop
*/
function populate_global_track_loop($post,$query){
global $wpsstm_track;
if ( $query->get('post_type') != wpsstm()->post_type_track ) return;
//set global $wpsstm_tracklist
$is_already_populated = ($wpsstm_track && ($wpsstm_track->post_id == $post->ID) );
if ($is_already_populated) return;
$wpsstm_track = new WPSSTM_Track($post);
}
//TOUFIX needed ?
function handle_track_action(){
global $wpsstm_track;
global $wp_query;
$success = null;
$redirect_url = null;
$action_feedback = null;
if ( !$action = get_query_var( 'wpsstm_action' ) ) return; //action does not exist
if ( get_query_var('post_type') !== wpsstm()->post_type_track ) return;
switch($action){
}
/*
Redirect or notice
*/
if ($redirect_url){
$redirect_args = array(
'wpsstm_did_action' => $action,
'wpsstm_action_feedback' => ( is_wp_error($success) ) ? $success->get_error_code() : true,
);
$redirect_url = add_query_arg($redirect_args, $redirect_url);
wp_safe_redirect($redirect_url);
exit;
}else{
if ( is_wp_error($success) ){
$wpsstm_tracklist->add_notice($success->get_error_code(),$success->get_error_message());
}
}
}
function handle_manager_action(){
global $wpsstm_track;
$success = null;
if ( 'manage' !== get_query_var( 'wpsstm_action' ) ) return; //action does not exist
$manager_action = wpsstm_get_array_value(array('wpsstm_manager_action'),$_REQUEST);
$manager_data = wpsstm_get_array_value(array('wpsstm_manager_data'),$_REQUEST);
switch ($manager_action){
case 'toggle_tracklists':
$checked_tracklists = wpsstm_get_array_value(array('new_tids'),$manager_data);
$previous_values = wpsstm_get_array_value(array('old_tids'),$manager_data);
$edit_values = array();
if (!$previous_values) break; //no range to compare to
//use bool values instead of strings
foreach((array)$previous_values as $key => $value){
$previous_values[$key] = ($value === '1') ? true : false;
}
foreach((array)$checked_tracklists as $key => $value){
$checked_tracklists[$key] = true;
}
//build an array containing the tracklists IDs that have been updated
foreach((array)$previous_values as $key => $value){
if ( $value && !array_key_exists($key,$checked_tracklists) ){//item has been unchecked
$edit_values[$key] = false;
}elseif ( !$value && array_key_exists($key,$checked_tracklists) ){//item has been checked
$edit_values[$key] = true;
}
}
//process changed values
if ($edit_values){
foreach($edit_values as $tracklist_id => $is_child){
$tracklist = new WPSSTM_Post_Tracklist($tracklist_id);
if ($is_child){
$success = $tracklist->queue_track($wpsstm_track);
}else{
$success = $tracklist->dequeue_track($wpsstm_track);
}
if ( is_wp_error($success) ){
break; //break at first error
}
}
}
break;
case 'new_tracklist':
$tracklist_title = wpsstm_get_array_value(array('new_tracklist_title'),$manager_data);
if (!$tracklist_title){
$success = new WP_Error('wpsstm_missing_tracklist_title',__('Missing tracklist title','wpsstm'));
}else{
//create new tracklist
$tracklist = new WPSSTM_Post_Tracklist();
$tracklist->title = $tracklist_title;
$success = $tracklist->save_tracklist();
//append subtrack if any
if ( !is_wp_error($success) ){
$tracklist_id = $success;
$tracklist = new WPSSTM_Post_Tracklist($tracklist_id);
$success = $tracklist->queue_track($wpsstm_track);
}
}
break;
}
if ($success){
if ( is_wp_error($success) ){
//TOUFIX we should remove that track notice function.
$wpsstm_track->add_notice($success->get_error_code(),$success->get_error_message());
}else{
$wpsstm_track->add_notice('success',__('Track action success!','wpsstm'));
}
}
}
function manager_template($template){
global $wpsstm_track;
if ( is_404() ) return $template;
if ( !is_single() ) return $template;
$post_type = get_query_var( 'post_type' );
if ($post_type !== wpsstm()->post_type_track ) return $template;
if ( !$wpsstm_track->subtrack_id && !$wpsstm_track->post_id ) return $template;
//check action
$action = get_query_var( 'wpsstm_action' );
if($action != 'manage') return $template;
return wpsstm_locate_template( 'tracklist-manager.php' );
}
function tracks_rewrite_rules(){
$track_post_type_obj = get_post_type_object( wpsstm()->post_type_track );
//single NEW subtrack action
//TOUFIX TOUCHECK used ?
add_rewrite_rule(
sprintf('%s/%s/%s/([^/]+)/([^/]+)/([^/]+)/action/([^/]+)/?',WPSSTM_BASE_SLUG,WPSSTM_SUBTRACKS_SLUG,WPSSTM_NEW_ITEM_SLUG), // = /music/subtracks/ID/action/ACTION
sprintf('index.php?post_type=%s&wpsstm_track_data[artist]=$matches[1]&wpsstm_track_data[album]=$matches[2]&wpsstm_track_data[title]=$matches[3]&wpsstm_action=$matches[4]',wpsstm()->post_type_track), // = /index.php?post_type=wpsstm_track&wpsstm_track_data[artist]=ARTIST&wpsstm_track_data[album]=ALBUM&wpsstm_track_data[title]=TITLE&wpsstm_action=dequeue
'top'
);
//single ID subtrack action
add_rewrite_rule(
sprintf('%s/%s/(\d+)/action/([^/]+)/?',WPSSTM_BASE_SLUG,WPSSTM_SUBTRACKS_SLUG), // = /music/subtracks/ID/action/ACTION
sprintf('index.php?post_type=%s&subtrack_id=$matches[1]&wpsstm_action=$matches[2]',wpsstm()->post_type_track), // = /index.php?post_type=wpsstm_track&subtrack-id=251&wpsstm_action=dequeue
'top'
);
//single ID subtrack
add_rewrite_rule(
sprintf('%s/%s/(\d+)/?',WPSSTM_BASE_SLUG,WPSSTM_SUBTRACKS_SLUG), // = /music/subtracks/ID
sprintf('index.php?post_type=%s&subtrack_id=$matches[1]',wpsstm()->post_type_track), // = /index.php?post_type=wpsstm_track&subtrack-id=251
'top'
);
//single track action
add_rewrite_rule(
sprintf('%s/(\d+)/action/([^/]+)/?',$track_post_type_obj->rewrite['slug']), // = /music/tracks/ID/action/ACTION
sprintf('index.php?post_type=%s&p=$matches[1]&wpsstm_action=$matches[2]',wpsstm()->post_type_track), // = /index.php?post_type=wpsstm_track&subtrack-id=251&wpsstm_action=dequeue
'top'
);
//single track
add_rewrite_rule(
sprintf('%s/(\d+)/?',$track_post_type_obj->rewrite['slug']), // = /music/tracks/ID
sprintf('index.php?post_type=%s&p=$matches[1]',wpsstm()->post_type_track), // = /index.php?post_type=wpsstm_trackp=251
'top'
);
}
//add custom admin submenu under WPSSTM
function backend_tracks_submenu($parent_slug){
//capability check
$post_type_slug = wpsstm()->post_type_track;
$post_type_obj = get_post_type_object($post_type_slug);
//tracks
add_submenu_page(
$parent_slug,
$post_type_obj->labels->name, //page title - TO FIX TO CHECK what is the purpose of this ?
$post_type_obj->labels->name, //submenu title
$post_type_obj->cap->edit_posts, //cap required
sprintf('edit.php?post_type=%s',$post_type_slug) //url or slug
);
}
function register_tracks_scripts_styles(){
wp_register_script( 'wpsstm-tracks', wpsstm()->plugin_url . '_inc/js/wpsstm-tracks.js', array('jquery','wpsstm-functions','jquery-ui-tabs','wpsstm-links'),wpsstm()->version, true );
}
static public function tracks_columns_register($defaults) {
global $post;
$before = array();
$after = array();
$after['track-links'] = __('Links','wpsstm');
$after['track-tracklists'] = __('Tracklists','wpsstm');
$after['track-favoriters'] = __('Favorited','wpsstm');
return array_merge($before,$defaults,$after);
}
static public function tracks_columns_content($column){
global $post;
global $wpsstm_track;
switch ( $column ) {
case 'track-tracklists':
if ( $list = $wpsstm_track->get_parents_list() ){
echo $list;
}else{
echo '—';
}
break;
case 'track-favoriters':
$output = '—';
if ( $list = $wpsstm_track->get_favoriters_list() ){
$output = $list;
}
echo $output;
break;
case 'track-links':
$links_query = $wpsstm_track->query_links();
$url = admin_url('edit.php');
$url = add_query_arg( array('post_type'=>wpsstm()->post_type_track_link,'parent_track'=>$wpsstm_track->post_id,'post_status'=>'publish'),$url );
echo sprintf('<a href="%s">%d</a>',$url,$links_query->post_count);
break;
}
}
static function register_orphan_tracks_view($views){
$screen = get_current_screen();
$post_type = $screen->post_type;
$subtracks_exclude = get_query_var('subtrack_exclude');
$link = add_query_arg( array('post_type'=>$post_type,'subtrack_exclude'=>true),admin_url('edit.php') );
$count = count(WPSSTM_Core_Tracks::get_orphan_track_ids());
$attr = array(
'href' => $link,
);
if ($subtracks_exclude){
$attr['class'] = 'current';
}
$views['orphan'] = sprintf('<a %s>%s <span class="count">(%d)</span></a>',wpsstm_get_html_attr($attr),__('Orphan','wpsstm'),$count);
return $views;
}
static function register_tracklist_tracks_view($views){
$screen = get_current_screen();
$post_type = $screen->post_type;
$tracklist_id = get_query_var('tracklist_id');
$tracklist = new WPSSTM_Post_Tracklist($tracklist_id);
if (!$tracklist->post_id) return $views;
$link = $tracklist->get_backend_tracks_url();
$tracks = $tracklist->get_static_subtracks();
$count = count($tracks);
$attr = array(
'href' => $link,
'class' => 'current',
);
$views['tracklist'] = sprintf('<a %s>%s <span class="count">(%d)</span></a>',wpsstm_get_html_attr($attr),get_the_title($tracklist_id),$count);
return $views;
}
private function is_subtracks_query($query){
return ( ( $query->get('post_type') == wpsstm()->post_type_track ) && $query->get('subtrack_query') );
}
function filter_single_subtrack_query($query){
if ( !$query->is_main_query() ) return;
if ( !$subtrack_id = $query->get( 'subtrack_id' ) ) return;
$query->set('post_type',wpsstm()->post_type_track);
$query->set('subtrack_query',true);
$query->set('posts_per_page',1);
$query->is_single = true; //so single template is shown, instead of search results
$query->is_archive = false;
$query->is_post_type_archive = false;
return $query;
}
function include_subtracks_query_join($join,$query){
global $wpdb;
if ( !$this->is_subtracks_query($query) ) return $join;
$subtracks_table = $wpdb->prefix . wpsstm()->subtracks_table_name;
$join .= sprintf("INNER JOIN %s AS subtracks ON %s.ID = subtracks.track_id",$subtracks_table,$wpdb->posts);
return $join;
}
function exclude_subtracks_query_join($join,$query){
global $wpdb;
if ( !$query->get('subtrack_exclude') ) return $join;
$subtracks_table = $wpdb->prefix . wpsstm()->subtracks_table_name;
$join .= sprintf("LEFT JOIN %s AS subtracks ON %s.ID = subtracks.track_id",$subtracks_table,$wpdb->posts); //so we can rely on subtracks.subtrack_id = null
return $join;
}
/*
Include the subtrack_id when fetching subtracks
*/
function tracks_query_subtrack_ids($fields,$query) {
global $wpdb;
if ( !$this->is_subtracks_query($query) ) return $fields;
if ( $query->get('fields') === 'ids' ) return $fields;//when requesting ids, we don't want several fields returned.
$fields = (array)$fields;
$fields[] = sprintf('%s.*','subtracks');
return implode(', ',$fields);
}
function track_query_exclude_subtracks($where,$query){
global $wpdb;
if ( !$query->get('subtrack_exclude') ) return $where;
$where .= sprintf(" AND subtracks.track_id IS NULL");
return $where;
}
function track_query_where_subtrack_position($where,$query){
if ( !$this->is_subtracks_query($query) ) return $where;
if ( !$subtrack_position = $query->get('subtrack_position') ) return $where;
if ( !$tracklist_id = $query->get('tracklist_id') ) return $where;
$where.= sprintf(" AND subtracks.tracklist_id = %s AND subtracks.subtrack_order = %s",$tracklist_id,$subtrack_position);
//so single template is shown, instead of search results
//TOUFIX this is maybe quite hackish, should be improved ? eg. setting $query->is_singular = true crashes wordpress.
$query->is_single = true;
return $where;
}
function track_query_where_subtrack_author($where,$query){
if ( !$this->is_subtracks_query($query) ) return $where;
if ( !$subtrack_author = $query->get('subtrack_author') ) return $where;
$where.= sprintf(" AND subtracks.subtrack_author = %s",$subtrack_author);
return $where;
}
/*
Get recents subtracks added in the 'now playing' tracklist
*/
function track_query_where_playing($where,$query){
if ( !$this->is_subtracks_query($query) ) return $where;
if ( !$query->get('subtrack_playing') ) return $where;
if ( !$nowplaying_id = wpsstm()->get_options('nowplaying_id') ) return $where;
$seconds = wpsstm()->get_options('playing_timeout');
$where.= sprintf(" AND subtracks.tracklist_id = %s AND subtrack_time > DATE_SUB(NOW(), INTERVAL %s SECOND)",$nowplaying_id,$seconds);
return $where;
}
function track_query_where_tracklist_id($where,$query){
if ( !$this->is_subtracks_query($query) ) return $where;
if ( !$tracklist_id = $query->get('tracklist_id') ) return $where;
$where.= sprintf(" AND subtracks.tracklist_id = %s",$tracklist_id);
return $where;
}
function track_query_where_subtrack_id($where,$query){
if ( !$this->is_subtracks_query($query) ) return $where;
if ( !$subtrack_id = $query->get('subtrack_id') ) return $where;
$where.= sprintf(" AND subtracks.subtrack_id = %s",$subtrack_id);
return $where;
}
function track_query_where_subtrack_in($where,$query){
if ( !$this->is_subtracks_query($query) ) return $where;
if ( !$ids_str = $query->get('subtrack__in') ) return $where;
if ( is_array($ids_str) ){
$ids_str = implode(',',$ids_str);
}
$where .= sprintf(" AND subtracks.subtrack_id IN (%s)",$ids_str);
return $where;
}
function track_query_where_favorited($where,$query){
if ( !$this->is_subtracks_query($query) ) return $where;
if ( !$query->get( 'subtrack_favorites' ) ) return $where;
//get all favorited tracklists
if ( !$ids = WPSSTM_Core_User::get_sitewide_favtracks_playlist_ids() ){
$ids = array(0);//so won't fetch anything
}
$ids_str = implode(',',$ids);
$where .= sprintf(" AND subtracks.tracklist_id IN (%s)",$ids_str);
return $where;
}
function tracks_query_sort_by_subtrack_position($orderby_sql, $query){
if ( !$this->is_subtracks_query($query) ) return $orderby_sql;
if ( $query->get('orderby') != 'subtrack_position' ) return $orderby_sql;
$orderby_sql = 'subtracks.subtrack_order ' . $query->get('order');
return $orderby_sql;
}
function tracks_query_sort_by_subtrack_time($orderby_sql, $query){
if ( !$this->is_subtracks_query($query) ) return $orderby_sql;
if ( $query->get('orderby') != 'subtrack_time' ) return $orderby_sql;
$orderby_sql = 'subtracks.subtrack_time ' . $query->get('order');
return $orderby_sql;
}
function register_track_post_type() {
$labels = array(
'name' => _x( 'Tracks', 'Tracks General Name', 'wpsstm' ),
'singular_name' => _x( 'Track', 'Track Singular Name', 'wpsstm' ),
'menu_name' => __( 'Tracks', 'wpsstm' ),
'name_admin_bar' => __( 'Track', 'wpsstm' ),
'archives' => __( 'Track Archives', 'wpsstm' ),
'attributes' => __( 'Track Attributes', 'wpsstm' ),
'parent_item_colon' => __( 'Parent Track:', 'wpsstm' ),
'all_items' => __( 'All Tracks', 'wpsstm' ),
'add_new_item' => __( 'Add New Track', 'wpsstm' ),
//'add_new' => __( 'Add New', 'wpsstm' ),
'new_item' => __( 'New Track', 'wpsstm' ),
'edit_item' => __( 'Edit Track', 'wpsstm' ),
'update_item' => __( 'Update Track', 'wpsstm' ),
'view_item' => __( 'View Track', 'wpsstm' ),
'view_items' => __( 'View Tracks', 'wpsstm' ),
'search_items' => __( 'Search Tracks', 'wpsstm' ),
//'not_found' => __( 'Not found', 'wpsstm' ),
//'not_found_in_trash' => __( 'Not found in Trash', 'wpsstm' ),
//'featured_image' => __( 'Featured Image', 'wpsstm' ),
//'set_featured_image' => __( 'Set featured image', 'wpsstm' ),
//'remove_featured_image' => __( 'Remove featured image', 'wpsstm' ),
//'use_featured_image' => __( 'Use as featured image', 'wpsstm' ),
'insert_into_item' => __( 'Insert into track', 'wpsstm' ),
'uploaded_to_this_item' => __( 'Uploaded to this track', 'wpsstm' ),
'items_list' => __( 'Tracks list', 'wpsstm' ),
'items_list_navigation' => __( 'Tracks list navigation', 'wpsstm' ),
'filter_items_list' => __( 'Filter tracks list', 'wpsstm' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'author','thumbnail', 'comments' ),
'taxonomies' => array( 'post_tag' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => sprintf('%s/%s',WPSSTM_BASE_SLUG,WPSSTM_TRACKS_SLUG), // = /music/tracks
'with_front' => FALSE
),
/**
* A string used to build the edit, delete, and read capabilities for posts of this type. You
* can use a string or an array (for singular and plural forms). The array is useful if the
* plural form can't be made by simply adding an 's' to the end of the word. For example,
* array( 'box', 'boxes' ).
*/
'capability_type' => 'track', // string|array (defaults to 'post')
/**
* Whether WordPress should map the meta capabilities (edit_post, read_post, delete_post) for
* you. If set to FALSE, you'll need to roll your own handling of this by filtering the
* 'map_meta_cap' hook.
*/
'map_meta_cap' => true, // bool (defaults to FALSE)
/**
* Provides more precise control over the capabilities than the defaults. By default, WordPress
* will use the 'capability_type' argument to build these capabilities. More often than not,
* this results in many extra capabilities that you probably don't need. The following is how
* I set up capabilities for many post types, which only uses three basic capabilities you need
* to assign to roles: 'manage_examples', 'edit_examples', 'create_examples'. Each post type
* is unique though, so you'll want to adjust it to fit your needs.
*/
'capabilities' => array(
// meta caps (don't assign these to roles)
'edit_post' => 'edit_track',
'read_post' => 'read_track',
'delete_post' => 'delete_track',
// primitive/meta caps
'create_posts' => 'create_tracks',
// primitive caps used outside of map_meta_cap()
'edit_posts' => 'edit_tracks',
'edit_others_posts' => 'manage_tracks',
'publish_posts' => 'manage_tracks',
'read_private_posts' => 'read',
// primitive caps used inside of map_meta_cap()
'read' => 'read',
'delete_posts' => 'manage_tracks',
'delete_private_posts' => 'manage_tracks',
'delete_published_posts' => 'manage_tracks',
'delete_others_posts' => 'manage_tracks',
'edit_private_posts' => 'edit_tracks',
'edit_published_posts' => 'edit_tracks'
)
);
register_post_type( wpsstm()->post_type_track, $args );
}
function register_track_taxonomy(){
$labels = array(
'name' => _x( 'Track Titles', 'Taxonomy General Name', 'wpsstm' ),
'singular_name' => _x( 'Track Title', 'Taxonomy Singular Name', 'wpsstm' ),
'menu_name' => __( 'Taxonomy', 'wpsstm' ),
'all_items' => __( 'All Items', 'wpsstm' ),
'parent_item' => __( 'Parent Item', 'wpsstm' ),
'parent_item_colon' => __( 'Parent Item:', 'wpsstm' ),
'new_item_name' => __( 'New Item Name', 'wpsstm' ),
'add_new_item' => __( 'Add New Item', 'wpsstm' ),
'edit_item' => __( 'Edit Item', 'wpsstm' ),
'update_item' => __( 'Update Item', 'wpsstm' ),
'view_item' => __( 'View Item', 'wpsstm' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'wpsstm' ),
'add_or_remove_items' => __( 'Add or remove items', 'wpsstm' ),
'choose_from_most_used' => __( 'Choose from the most used', 'wpsstm' ),
'popular_items' => __( 'Popular Items', 'wpsstm' ),
'search_items' => __( 'Search Items', 'wpsstm' ),
'not_found' => __( 'Not Found', 'wpsstm' ),
'no_terms' => __( 'No items', 'wpsstm' ),
'items_list' => __( 'Items list', 'wpsstm' ),
'items_list_navigation' => __( 'Items list navigation', 'wpsstm' ),
);
$capabilities = array(
'manage_terms' => 'manage_tracks',
'edit_terms' => 'manage_tracks',
'delete_terms' => 'manage_tracks',
'assign_terms' => 'edit_tracks',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => false,
'show_ui' => false,
'show_admin_column' => false,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'capabilities' => $capabilities,
);
register_taxonomy(
WPSSTM_Core_Tracks::$track_taxonomy,
array( wpsstm()->post_type_track ),
$args
);
}
function add_query_vars_track( $qvars ) {
$qvars[] = 'wpsstm_track_data';
$qvars[] = 'tracklist_id';
$qvars[] = 'subtrack_query';
$qvars[] = 'subtrack_playing';
$qvars[] = 'subtrack_author';
$qvars[] = 'subtrack_favorites';
$qvars[] = 'subtrack_id';
$qvars[] = 'subtrack__in';
$qvars[] = 'subtrack_position';
$qvars[] = 'subtrack_exclude';
return $qvars;
}
function metabox_track_register(){
add_meta_box(
'wpsstm-track-info',
__('Track','wpsstm'),
array(__class__,'metabox_music_infos_content'),
wpsstm()->post_type_track,
'after_title',
'high'
);
add_meta_box(
'wpsstm-track-options',
__('Track Settings','wpsstm'),
array($this,'metabox_track_options_content'),
wpsstm()->post_type_track,
'side', //context
'default' //priority
);
}
static function metabox_music_infos_content( $post ){
$post_type = get_post_type($post);
switch($post_type){
case wpsstm()->post_type_artist:
//artist
echo self::get_edit_artist_input($post->ID);
break;
case wpsstm()->post_type_album:
//artist
echo self::get_edit_artist_input($post->ID);
//album
echo self::get_edit_album_input($post->ID);
break;
case wpsstm()->post_type_track:
//artist
echo self::get_edit_artist_input($post->ID);
//album
echo self::get_edit_album_input($post->ID);
//title
echo self::get_edit_track_title_input($post->ID);
//length
echo self::get_edit_track_length_input($post->ID);
break;
}
wp_nonce_field( 'wpsstm_music_details_meta_box', 'wpsstm_music_details_meta_box_nonce' );
}
static function get_edit_track_title_input($post_id = null){
global $post;
if (!$post) $post_id = $post->ID;
$input_attr = array(
'id' => 'wpsstm-track-title',
'name' => 'wpsstm_track_title',
'value' => wpsstm_get_post_track($post_id),
'icon' => '<i class="fa fa-music" aria-hidden="true"></i>',
'label' => __("Track title",'wpsstm'),
'placeholder' => __("Enter track title here",'wpsstm')
);
return wpsstm_get_backend_form_input($input_attr);
}
static function get_edit_artist_input($post_id = null){
global $post;
if (!$post) $post_id = $post->ID;
$input_attr = array(
'id' => 'wpsstm-artist',
'name' => 'wpsstm_artist',
'value' => wpsstm_get_post_artist($post_id),
'icon' => '<i class="fa fa-user-o" aria-hidden="true"></i>',
'label' => __("Artist",'wpsstm'),
'placeholder' => __("Enter artist here",'wpsstm')
);
return wpsstm_get_backend_form_input($input_attr);
}
static function get_edit_album_input($post_id = null){
global $post;
if (!$post) $post_id = $post->ID;
$input_attr = array(
'id' => 'wpsstm-album',
'name' => 'wpsstm_album',
'value' => wpsstm_get_post_album($post_id),
'icon' => '<i class="fa fa-bars" aria-hidden="true"></i>',
'label' => __("Album",'wpsstm'),
'placeholder' => __("Enter album here",'wpsstm')
);
return wpsstm_get_backend_form_input($input_attr);
}
static function get_edit_track_length_input($post_id = null){
global $post;
if (!$post) $post_id = $post->ID;
$input_attr = array(
'id' => 'wpsstm-length',
'name' => 'wpsstm_length',
'value' => wpsstm_get_post_duration($post_id,true),
'icon' => '<i class="fa fa-music" aria-hidden="true"></i>',
'label' => __("Duration (milliseconds)",'wpsstm'),
'placeholder' => __("Enter length here",'wpsstm')
);
return wpsstm_get_backend_form_input($input_attr);
}
function metabox_track_options_content( $post ){
global $wpsstm_track;
/*
playlists manager
*/
$manager = '';
//in playlists
if ( $in_playlists = $wpsstm_track->get_parents_list() ){
$manager .= sprintf('<p>%s</p>',$in_playlists);
}
//manager bt
$classes = array('wpsstm-action-popup button');
$attr = array(
'href' => $wpsstm_track->get_track_action_url('manage'),
'class' => implode(' ',$classes),
'target' => '_blank'
);
$attr_str = wpsstm_get_html_attr($attr);
$manager.= sprintf('<p><a %s>%s</a></p>',$attr_str,__('Playlists manager','wpsstm'));
//
printf('<div><label>%s</label>%s</div>',__('Playlists','wpsstm'),$manager);
}
/**
Save track field for this post
**/
function metabox_save_music_details( $post_id ) {
//check save status
$is_autosave = ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || wp_is_post_autosave($post_id) );
$is_autodraft = ( get_post_status( $post_id ) == 'auto-draft' );
$is_revision = wp_is_post_revision( $post_id );
$is_metabox = isset($_POST['wpsstm_music_details_meta_box_nonce']);
if ( !$is_metabox || $is_autosave || $is_autodraft || $is_revision ) return;
//nonce
$is_valid_nonce = ( wp_verify_nonce( $_POST['wpsstm_music_details_meta_box_nonce'], 'wpsstm_music_details_meta_box' ) );
if ( !$is_valid_nonce ) return;
//this should run only once (for the main post); so unset meta box nonce.
//without this the function would be called for every subtrack if there was some.
unset($_POST['wpsstm_music_details_meta_box_nonce']);
//sanitize datas
$artist = ( isset($_POST[ 'wpsstm_artist' ]) ) ? $_POST[ 'wpsstm_artist' ] : null;
$album = ( isset($_POST[ 'wpsstm_album' ]) ) ? $_POST[ 'wpsstm_album' ] : null;
$title = ( isset($_POST[ 'wpsstm_track_title' ]) ) ? $_POST[ 'wpsstm_track_title' ] : null;
$length = ( isset($_POST[ 'wpsstm_length' ]) && ctype_digit($_POST[ 'wpsstm_length' ]) ) ? ( (int)$_POST[ 'wpsstm_length' ] ) : null; //ms
$post_type = get_post_type($post_id);
switch($post_type){
case wpsstm()->post_type_artist:
//artist
self::save_track_artist($post_id, $artist);
break;
case wpsstm()->post_type_album:
//artist
self::save_track_artist($post_id, $artist);
//album
self::save_track_album($post_id, $album);
break;
case wpsstm()->post_type_track:
//artist
self::save_track_artist($post_id, $artist);
//album
self::save_track_album($post_id, $album);
//title
self::save_track_title($post_id, $title);
//length
self::save_track_duration($post_id, $length);
break;
}