forked from buddydev/mediapress
/
loader.php
716 lines (496 loc) · 21.1 KB
/
loader.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
<?php
// Exit if the file is accessed directly over web
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* MediaPress Core Component for BuddyPress
* Adds support for the Media upload/display to various bp component
*
*/
class MPP_Core_Component extends BP_Component {
private static $instance;
/**
* Array of names not available as gallery names
* @var type
*/
public $forbidden_name;
/**
*
* @var I am not sure why I added it in the first place
*/
public $valid_status;
/**
* Paginated page no. for gallery
*
* @var int
*/
private $gpage = 0;
/**
* Paginated page no. for single Media
*
* @var int
*/
private $mpage = 0;
/**
* What are the media/gallery status which are allowed to the current user for the current component?
*
* @var array of strings(status strings like array('private', 'public')
*/
private $accessible_statuses = array();
/**
* Current component the gallery is associated, could be 'members','groups' etc
*
* @var string
*/
private $component = '';
/**
* The current component ID(owner of gallery/media), It could be user id or group id depending on the context
*
* @var int
*/
private $component_id = 0;
/**
* Current MediaPress action 'create/upload/manage/
*
* @var type
*/
private $current_action = '';
/**
* What type of management option is this? delete/edit/reorder etc
*
* @var string
*/
private $current_manage_action = '';
private $action_variables = array();
private $is_json = false;
/**
* Get the singleton instance
*
* @return MPP_Core_Component
*/
public static function get_instance() {
if ( ! isset( self::$instance ) )
self::$instance = new self();
return self::$instance;
}
/**
* Everything starts here
*/
private function __construct() {
parent::start(
'mediapress', //unique id
__( 'Gallery', 'mediapress' ),
untrailingslashit( mediapress()->get_path() )//base path
);
//mark it as active component, otherwise notifications will not be rendered
buddypress()->active_components[$this->id] = 1;
add_action( 'bp_member_plugin_options_nav', array( $this, 'context_menu_edit' ) );
add_action( 'mpp_group_nav', array( $this, 'context_menu_edit' ) );
}
/**
* Include files, we don't as we are using the mediapress->core_init to do it
*/
public function includes( $args = array() ) {
}
/**
* Setup everything for BuddyPress Specific installation
*
*/
public function setup_globals( $args = array() ) {
$bp = buddypress();
//if the 'gallery' slug is not set , set it to mediapress?
if ( ! defined( 'MPP_GALLERY_SLUG' ) )
define( 'MPP_GALLERY_SLUG', $this->id );
//get current component/component_id
$this->component = mpp_get_current_component();
$this->component_id = mpp_get_current_component_id();
//override the component id if we are on user page
if( bp_is_user() )
$this->component_id = bp_displayed_user_id ();
$globals = array(
'slug' => MPP_GALLERY_SLUG,
'root_slug' => isset( $bp->pages->mediapress->slug ) ? $bp->pages->mediapress->slug : MPP_GALLERY_SLUG,
'notification_callback' => 'mpp_format_notifications',
'has_directory' => mpp_get_option( 'has_gallery_directory' ),
'search_string' => __( 'Search Galleries...', 'mediapress' ),
'directory_title' => isset( $bp->pages->mediapress->id )? get_the_title( $bp->pages->mediapress->id ): __( 'Gallery Directory', 'mediapress' ),
);
parent::setup_globals( $globals );
//it will call do_action("bp_gallery_setup_global") after setting up the constants properly
//the only possibility of gallery as component is in case of root galler, gallery directory or user gallery
//let us setup global queries
$current_action = '';
//initialize query objects
mediapress()->the_gallery_query = new MPP_Gallery_Query();
mediapress()->the_media_query = new MPP_Media_Query();
//set the status types allowed for current user
$this->accessible_statuses = mpp_get_accessible_statuses( $this->component, $this->component_id, get_current_user_id() );
//is the root gallery enabled?
if( mpp_is_root_enabled() ) {
$this->setup_root_gallery();
}//end of root gallery section
//if it is either member gallery OR Gallery Directory, let us process it
if( mpp_is_gallery_component () ) {
$this->action_variables = buddypress()->action_variables;
//add the current action at the begining of the stack, we are doing it to unify the things for User gallery and component gallery
array_unshift( $this->action_variables, bp_current_action() );
$this->setup_user_gallery();
}elseif( mpp_is_component_gallery() ) {
//are we on component gallery like groups or events etc?
$this->action_variables = buddypress()->action_variables;
$this->setup_component_gallery();
}
//once we are here, the basic action variables for mediapress are setup and so
//we can go ahead and test for the single gallery/media
$mp = mediapress();
//setup Single Gallery specific things
if( mpp_is_single_gallery() ) {
$current_action = $this->current_action;
//setup and see the actions etc to find out what we need to do
//if it is one of the edit actions, It was already taken care of, don't do anything
if( in_array( $current_action, mpp_get_reserved_actions() ) )
return ;
//check if we are on management screen?
if( $this->current_action == 'manage' ) {
//this is media management page
$mp->set_editing( 'gallery' );
$mp->set_action( 'manage' );
$mp->set_edit_action( $this->current_manage_action );
//on edit bulk media page
if( $mp->is_edit_action( 'edit' ) )
$this->setup_gallery_media_query ();
} elseif ( $media = $this->get_media_id( $this->current_action, $this->component, $this->component_id ) ) {
//yes, It is single media
$this->setup_single_media_query( $media );
} else {
//we already know it is single gallery, so let us setup the media list query
$this->setup_gallery_media_query();
}
}
do_action( 'mpp_setup_globals' );
}
/**
* Check and get single media id else false
*
* @param type $slug
* @param type $component
* @param type $component_id
* @return type
*/
public function get_media_id( $slug, $component, $component_id ) {
return mpp_media_exists( $slug, $component, $component_id );
}
/**
* Set up query for fetching single media
*
* @param type $media_id
*/
public function setup_single_media_query( $media ) {
$mp = mediapress();
$mp->current_media = mpp_get_media( $media );
$mp->the_media_query = new MPP_Media_Query(
array(
'id' => $media->ID
));
//now check if we are on edit page nor not?
$this->current_action = isset( $this->action_variables[2] ) ? $this->action_variables[2] : '';
if( $this->current_action == 'edit' ) {
$mp->set_editing( 'media' );
//it is single media edit
$mp->set_action( 'edit' );
$edit_action = isset( $this->action_variables[3] ) ? $this->action_variables[3] : 'edit';
$mp->set_edit_action( $edit_action );
}
}
/**
* Setup query for listing Media inside single gallery
*
*/
public function setup_gallery_media_query() {
//since we already know that this is a single gallery, It muist be media list screen
$args = array(
'component_id' => $this->component_id,
'component' => $this->component,
'gallery_id' => mpp_get_current_gallery_id(),
'status' => $this->accessible_statuses,
);
if( $this->mpage ) {
$args['page'] = absint( $this->mpage );
}
//check for pagination
//
//we are on User gallery home page
//we do need to check for the access level here and pass it to the query
mediapress()->the_media_query = new MPP_Media_Query( $args );
//set it is the user galleries list view
//mediapress()->is_gallery_home = true;
}
/**
* Setup Root Galleries
* @todo Make it work in 1.1 when we introduce site galleries
*
*/
public function setup_root_gallery() {
if( is_singular( mpp_get_gallery_post_type() ) ) {
$gallery_id = get_queried_object_id();
//setup current gallery
mediapress()->current_gallery = mpp_get_gallery( $gallery_id );
//setup gallery query
mediapress()->the_gallery_query = new MPP_Gallery_Query(
array(
'id' => $gallery_id
));
//check for end points to edit
if( get_query_var( 'edit' ) ) {
$action = get_query_var( 'edit' );
if( in_array( $action, mpp_get_reserved_actions() ) ) {
//$this->setup_edit_action( get_query_var( 'edit' ) );
}
}
}elseif( is_post_type_archive( mpp_get_gallery_post_type() ) ) {
mediapress()->the_gallery_query = new MPP_Gallery_Query(
array(
// 'user_id' => $user_id,
// 'component'=> $component,
));
}
}
public function setup_user_gallery() {
if( mpp_is_active_component( 'members' ) && bp_is_user() ) {
//is User Gallery enabled? and are we on the user section?
$user_id = bp_displayed_user_id();
$this->component = 'members';
$current_action = bp_current_action();
if( $current_action == 'create' || $current_action == 'upload' ) {
mediapress()->set_action( $current_action );
mediapress()->set_edit_action( $current_action );
return ;
}
//Are we looking at single gallery? or Media?
//current action in this case is checked for being a gallery slug
if( $gallery = mpp_gallery_exists( $this->action_variables[0], $this->component, $user_id ) ) {
//setup current gallery & gallery query
mediapress()->current_gallery = mpp_get_gallery( $gallery );
mediapress()->the_gallery_query = new MPP_Gallery_Query(
array(
'id' => $gallery->ID
));
$this->current_action = bp_action_variable( 0 );
$this->current_manage_action = bp_action_variable( 1 );
if( ! empty( $this->action_variables[1] ) && $this->action_variables[1] == 'page' && $this->action_variables[2] > 0 )
$this->mpage = (int) $this->action_variables[2];
} else {
if( $this->action_variables[0] == 'page' && $this->action_variables[1] > 0 )
$this->gpage = (int) $this->action_variables[1];
$args = array(
'user_id' => $user_id,
'component' => $this->component,
'status' => $this->accessible_statuses,
);
if( $this->gpage ) {
$args['page'] = absint( $this->gpage );
}
//we are on User gallery home page(gallery list)
//we do need to check for the access level here and pass it to the query
//how about gallery pagination?
mediapress()->the_gallery_query = new MPP_Gallery_Query( $args );
//set it is the user galleries list view
mediapress()->is_gallery_home = true;
}
//in this case, we are on the gallery directory, check if we have it enabled?
}elseif( mpp_has_gallery_directory()){
$this->setup_gallery_directory_query();
}
}
/**
* Setup query for gallery directory
*
*/
public function setup_gallery_directory_query() {
//make the query and setup
mediapress()->is_directory = true;
//get all public galleries, should we do type filtering
mediapress()->the_gallery_query = new MPP_Gallery_Query(
array(
'status'=> 'public'
));
}
/**
* Setup gallery for components like groups/events etc
*/
public function setup_component_gallery(){
//current_action = mpp_slug(mediapress)
if( mpp_is_active_component( bp_current_component() ) ) {
//is Component Gallery enabled? and are we on the Component section?
$current_action = bp_action_variable( 0 );
if( $current_action == 'create' || $current_action == 'upload' ) {
mediapress()->set_action( $current_action );
mediapress()->set_edit_action( $current_action );
return ;
}
//Are we looking at single gallery? or Media?
//current action in this case is checked for being a gallery slug
if( $this->action_variables && $gallery = mpp_gallery_exists( $this->action_variables[0], $this->component, $this->component_id ) ) {
//setup current gallery & gallery query
mediapress()->current_gallery = mpp_get_gallery( $gallery );
mediapress()->the_gallery_query = new MPP_Gallery_Query(
array(
'id' => $gallery->ID
));
$this->current_action = bp_action_variable( 1 );
$this->current_manage_action = bp_action_variable( 2 );
if( ! empty( $this->action_variables[1] ) && $this->action_variables[1] == 'page' && $this->action_variables[2] > 0 )
$this->mpage = (int) $this->action_variables[2];
} else {
if( $this->action_variables && $this->action_variables[0] == 'page' && $this->action_variables[1] > 0 )
$this->gpage = (int) $this->action_variables[1];
$args = array(
'component_id' => $this->component_id,
'component' => $this->component,
'status' => $this->accessible_statuses,
);
if( $this->gpage ) {
$args['page'] = absint( $this->gpage );
}
//we are on User gallery home page(gallery list)
//we do need to check for the access level here and pass it to the query
//how about gallery pagination?
mediapress()->the_gallery_query = new MPP_Gallery_Query( $args );
//set it is the user galleries list view
mediapress()->is_gallery_home = true;
}
//in this case, we are on the gallery directory, check if we have it enabled?
}
}
public function setup_nav( $main = array(), $sub = array() ) {
$bp = buddypress();
if ( ! mpp_is_active_component( 'members' ) )//allow to disable user galleries in case they don't want it
return false;
$view_helper = MPP_Gallery_Screens::get_instance();
// Add 'Gallery' to the user's main navigation
$main_nav = array(
'name' => sprintf( __( 'Gallery <span>%d</span>', 'mediapress' ), mpp_get_total_gallery_for_user() ),
'slug' => $this->slug,
'position' => 86,
'screen_function' => array( $view_helper, 'user_galleries' ),
'default_subnav_slug' => 'my-galleries',
'item_css_id' => $this->id
);
if( bp_is_user() )
$user_domain = bp_displayed_user_domain ( );
else
$user_domain = bp_loggedin_user_domain ( );
$gallery_link = trailingslashit( $user_domain . $this->slug ); //with a trailing slash
// Add the My Gallery nav item
$sub_nav[] = array(
'name' => __( 'My Gallery', 'mediapress' ),
'slug' => 'my-galleries',
'parent_url' => $gallery_link,
'parent_slug' => $this->slug,
'screen_function' => array( $view_helper, 'my_galleries' ),
'position' => 10,
'item_css_id' => 'gallery-my-gallery'
);
if( mpp_user_can_create_gallery( 'members', get_current_user_id() ) ) {
// Add the Create gallery link to gallery nav
$sub_nav[] = array(
'name' => __( 'Create a Gallery', 'mediapress' ),
'slug' => 'create',
'parent_url' => $gallery_link,
'parent_slug' => $this->slug,
'screen_function' => array( $view_helper, 'create_gallery' ),
'user_has_access' => bp_is_my_profile(),
'position' => 20
);
}
// Add the Upload link to gallery nav
/*$sub_nav[] = array(
'name' => __( 'Upload', 'mediapress'),
'slug' => 'upload',
'parent_url' => $gallery_link,
'parent_slug' => $this->slug,
'screen_function' => array( $view_helper, 'upload_media' ),
'user_has_access' => bp_is_my_profile(),
'position' => 30
);*/
parent::setup_nav( $main_nav, $sub_nav );
//disallow these names in various lists
//we have yet to implement it
$this->forbidden_names = apply_filters( 'mpp_forbidden_names', array( 'gallery', 'galleries', 'my-gallery', 'create', 'delete', 'upload', 'add', 'edit', 'admin', 'request', 'upload', 'tags', 'audio', 'video', 'photo' ) );
//use this to extend the valid status
$this->valid_status = apply_filters( 'mpp_valid_gallery_status', array_keys( mpp_get_active_statuses() ) ) ;
do_action( 'mpp_setup_nav' ); // $bp->gallery->current_gallery->user_has_access
}
//Add the Edit context menu when a user is on single gallery
public function context_menu_edit() {
if( mpp_is_gallery_management() || mpp_is_media_management() )
return;
if( ! mpp_is_single_gallery() )
return;
if( ! mpp_user_can_edit_gallery( mpp_get_current_gallery_id() ) )
return;
if( mpp_is_single_media() )
$url = mpp_get_media_edit_url();
else
$url = mpp_get_gallery_edit_media_url( mpp_get_current_gallery() );//bulk edit media url
printf( '<li><a href="%1$s" title ="%2$s"> %3$s</a></li>', $url, __( 'Edit', 'mediapress' ), __( 'Edit', 'mediapress' ) );
}
/**
* Setup title for various screens
*
*/
public function setup_title() {
parent::setup_title();
}
/**
* Set up the Toolbar.
*
* @param array $wp_admin_nav See {BP_Component::setup_admin_bar()}
* for details.
*/
public function setup_admin_bar( $wp_admin_nav = array() ) {
$bp = buddypress();
// Menus for logged in user if the members gallery is enabled
if ( is_user_logged_in() && mpp_is_active_component( 'members' ) ) {
$component = 'members';
$component_id = get_current_user_id();
$gallery_link = trailingslashit( mpp_get_gallery_base_url( $component, $component_id ) );
$title = __( 'Gallery', 'mediapress' );
$my_galleries = __( 'My Gallery', 'mediapress' );
$create = __( 'Create', 'mediapress' );
// Add main mediapress menu
$wp_admin_nav[] = array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-' . $this->id,
'title' => $title,
'href' => trailingslashit( $gallery_link )
);
// Add main mediapress menu
$wp_admin_nav[] = array(
'parent' => 'my-account-' . $this->id,
'id' => 'my-account-' . $this->id . '-my-galleries',
'title' => $my_galleries,
'href' => trailingslashit( $gallery_link )
);
if( mpp_user_can_create_gallery( $component, $component_id ) ) {
$wp_admin_nav[] = array(
'parent' => 'my-account-' . $this->id,
'id' => 'my-account-' . $this->id . '-create',
'title' => $create,
'href' => mpp_get_gallery_create_url( $component, $component_id )
);
}
}
parent::setup_admin_bar( $wp_admin_nav );
}
}
/**
* Setup MediaPress BP Component
* *
*/
add_action( 'bp_loaded', 'mpp_setup_mediapress_component' );
function mpp_setup_mediapress_component() {
$bp = buddypress();
$bp->mediapress = MPP_Core_Component::get_instance();
}