/
actions.php
725 lines (496 loc) · 17.8 KB
/
actions.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
<?php
/**
* Various Gallery related actions handlers
*
*/
/**
* Handles Gallery creation on the front end in non ajax case
*
* @return type
*/
function mpp_action_create_gallery() {
//allow gallery to be created from anywhere
//the form must have mpp-action set and It should be set to 'create-gallery'
if( empty( $_POST['mpp-action'] ) || $_POST['mpp-action'] != 'create-gallery' ) {
return;
}
$referer = wp_get_referer();
//if we are here, It is gallery create action
if( ! wp_verify_nonce( $_POST['mpp-nonce'], 'mpp-create-gallery' ) ) {
//add error message and return back to the old page
mpp_add_feedback( __( 'Action not authorized!', 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return ;
}
//update it to allow passing component/id from the form
$component = mpp_get_current_component();
$component_id = mpp_get_current_component_id();
//check for permission
//we may want to allow passing of component from the form in future!
if( ! mpp_user_can_create_gallery( $component, $component_id ) ) {
mpp_add_feedback( __( "You don't have permission to create gallery!", 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//if we are here, validate the data and let us see if we can create
$title = $_POST['mpp-gallery-title'];
$description = $_POST['mpp-gallery-description'];
$type = $_POST['mpp-gallery-type'];
$status = $_POST['mpp-gallery-status'];
$errors = array();
if( ! mpp_is_active_status( $status ) ) {
$errors['status'] = __( 'Invalid Gallery status!', 'mediapress' );
}
if( ! mpp_is_active_type( $type ) ) {
$errors['type'] = __( 'Invalid gallery type!', 'mediapress' );
}
//check for current component
if( ! mpp_is_active_component( $component ) ) {
$errors['component'] = __( 'Invalid gallery component!', 'mediapress' );
}
if( empty( $title ) ) {
$errors['title'] = __( 'Title can not be empty', 'mediapress' );
}
//give opportunity to other plugins to add their own validation errors
$validation_errors = apply_filters( 'mpp-create-gallery-field-validation', $errors, $_POST );
if( ! empty( $validation_errors ) ) {
//let us add the validation error and return back to the earlier page
$message = join( '\r\n', $validation_errors );
mpp_add_feedback( $message, 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return ;
}
//let us create gallery
$gallery_id = mpp_create_gallery( array(
'title' => $title,
'description' => $description,
'type' => $type,
'status' => $status,
'creator_id' => get_current_user_id(),
'component' => $component,
'component_id' => $component_id
));
if( ! $gallery_id ) {
mpp_add_feedback( __( 'Unable to create gallery!', 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//if we are here, the gallery was created successfully,
//let us redirect to the gallery_slug/manage/upload page
$redirect_url = mpp_get_gallery_add_media_url( $gallery_id );
mpp_add_feedback( __( 'Gallery created successfully!', 'mediapress' ) );
mpp_redirect( $redirect_url );
}
add_action( 'bp_actions', 'mpp_action_create_gallery', 2 );
/**
* Handles gallery details updation
*
* @return type
*/
function mpp_action_edit_gallery() {
//allow gallery to be created from anywhere
if( empty( $_POST['mpp-action'] ) || $_POST['mpp-action'] != 'edit-gallery' ) {
return;
}
$referer = wp_get_referer();
//if we are here, It is gallery create action
if( ! wp_verify_nonce( $_POST['mpp-nonce'], 'mpp-edit-gallery' ) ) {
//add error message and return back to the old page
mpp_add_feedback( __('Action not authorized!', 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
$gallery_id = absint( $_POST['mpp-gallery-id'] );
if( ! $gallery_id ) {
return;
}
//check for permission
//we may want to allow passing of component from the form in future!
if( ! mpp_user_can_edit_gallery( $gallery_id ) ) {
mpp_add_feedback( __( "You don't have permission to edit this gallery!", 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//if we are here, validate the data and let us see if we can create
$title = $_POST['mpp-gallery-title'];
$description = $_POST['mpp-gallery-description'];
$status = $_POST['mpp-gallery-status'];
$errors = array();
if( ! mpp_is_active_status( $status ) ) {
$errors['status'] = __( 'Invalid Gallery status!', 'mediapress' );
}
if( empty( $title ) ) {
$errors['title'] = __( 'Title can not be empty', 'mediapress' );
}
//give opportunity to other plugins to add their own validation errors
$validation_errors = apply_filters( 'mpp-edit-gallery-field-validation', $errors, $_POST );
if( ! empty( $validation_errors ) ) {
//let us add the validation error and return back to the earlier page
$message = join( '\r\n', $validation_errors );
mpp_add_feedback( $message, 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//let us create gallery
$gallery_id = mpp_update_gallery( array(
'title' => $title,
'description' => $description,
'status' => $status,
'creator_id' => get_current_user_id(),
'id' => $gallery_id,
));
if( ! $gallery_id ) {
mpp_add_feedback( __( 'Unable to update gallery!', 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//if we are here, the gallery was created successfully,
//let us redirect to the gallery_slug/manage/upload page
$redirect_url = mpp_get_gallery_settings_url( $gallery_id );
mpp_add_feedback( __( 'Gallery updated successfully!', 'mediapress' ) );
mpp_redirect( $redirect_url );
}
add_action( 'bp_actions', 'mpp_action_edit_gallery', 2 );//update gallery settings, cover
/**
* Handles Gallery deletion
*
* @return type
*/
function mpp_action_delete_gallery() {
if( empty( $_POST['mpp-action'] ) || $_POST['mpp-action'] != 'delete-gallery' ) {
return;
}
if( empty ( $_POST['gallery_id'] ) ) {
return;
}
$referer = wp_get_referer();
if( ! wp_verify_nonce( $_POST['mpp-nonce'], 'mpp-delete-gallery' ) ) {
//add error message and return back to the old page
mpp_add_feedback( __( 'Action not authorized!', 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
if( empty( $_POST['mpp-delete-agree'] ) ) {
return;
//@todo add feedback that you must agree
}
$gallery = '';
if( ! empty( $_POST['gallery_id'] ) ) {
$gallery = mpp_get_gallery ( absint( $_POST['gallery_id'] ) );
}
//check for permission
//we may want to allow passing of component from the form in future!
if( ! mpp_user_can_delete_gallery( $gallery ) ) {
mpp_add_feedback( __( "You don't have permission to delete this gallery!", 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//if we are here, delete gallery and redirect to the component base url
$redirect_url = mpp_get_gallery_base_url( $gallery->component, $gallery->component_id ) ;
mpp_delete_gallery( $gallery->id );
mpp_redirect( $redirect_url );
}
add_action( 'bp_actions', 'mpp_action_delete_gallery', 2 );
/**
* Hndles bulk edit action
*
*/
function mpp_action_gallery_media_bulkedit() {
if( empty( $_POST['mpp-action'] ) || $_POST['mpp-action'] != 'edit-gallery-media' ) {
return;
}
if( ! $_POST['mpp-editing-media-ids'] ) {
return;
}
$referer = wp_get_referer();
if( ! wp_verify_nonce( $_POST['mpp-nonce'], 'mpp-edit-gallery-media' ) ) {
//add error message and return back to the old page
mpp_add_feedback( __( 'Action not authorized!', 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
$media_ids = $_POST['mpp-editing-media-ids'];
$media_ids = wp_parse_id_list( $media_ids );
$bulk_action = false;
if( ! empty( $_POST['mpp-edit-media-bulk-action'] ) ) {
$bulk_action = $_POST['mpp-edit-media-bulk-action'];//we are leaving this to allow future enhancements with other bulk action and not restricting to delete only
}
foreach( $media_ids as $media_id ) {
//check what action should we take?
//1. check if $bulk_action is set? then we may ned to check for deletion
//otherwise, just update the details :)
if( $bulk_action == 'delete' && ! empty( $_POST['mpp-delete-media-check'][ $media_id ] ) ) {
//delete and continue
//check if current user can delete?
if( ! mpp_user_can_delete_media( $media_id ) ) {
//if the user is unable to delete media, should we just continue the loop or breakout and redirect back with error?
//I am in favour of showing error
mpp_add_feedback( __( 'Not allowed to delete!', 'mediapress' ) );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//if we are here, let us delete the media
mpp_delete_media( $media_id );
mpp_add_feedback( __( 'Deleted successfully!', 'mediapress' ),'error' ); //it will do for each media, that is not good thing btw
continue;
}
//since we already handled delete for the media checked above,
//we don't want to do it for the other media hoping that the user was performing bulk delete and not updating the media info
if( $bulk_action == 'delete' ) {
continue;
}
$media_title = $_POST['mpp-media-title'][ $media_id ];
$media_description = $_POST['mpp-media-description'][ $media_id ];
$status = $_POST['mpp-media-status'][ $media_id ];
//type is not editable
//$type = $_POST['mpp-media-type'][$media_id];
//if we are here, It must not be a bulk action
$media_info = array(
'id' => $media_id,
'title' => $media_title,
'description' => $media_description,
// 'type' => $type,
'status' => $status,
);
mpp_update_media( $media_info );
}
if( ! $bulk_action ) {
mpp_add_feedback( __( 'Updated!', 'mediapress' ) );
}
if( $referer ) {
mpp_redirect( $referer );
}
}
add_action( 'bp_actions', 'mpp_action_gallery_media_bulkedit', 2 );
/**
* Handles Gallery Media Reordering
*
*
* @return type
*/
function mpp_action_reorder_gallery_media() {
if( empty( $_POST['mpp-action'] ) || $_POST['mpp-action'] != 'reorder-gallery-media' ) {
return;
}
$referer = wp_get_referer();
if( ! wp_verify_nonce( $_POST['mpp-nonce'], 'mpp-reorder-gallery-media' ) ) {
//add error message and return back to the old page
mpp_add_feedback( __( 'Action not authorized!', 'mediapress' ), 'error' );
mpp_redirect( $referer );
}
//should we check for the permission? not here
$media_ids = $_POST['mpp-media-ids'];//array
$media_ids = wp_parse_id_list( $media_ids );
$order = count( $media_ids );
foreach( $media_ids as $media_id ) {
if( ! mpp_user_can_edit_media( $media_id ) ) {
//unauthorized attemt
mpp_add_feedback( __( "You don't have permission to update!", 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return ;
}
//if we are here, let us update the order
mpp_update_media_order( $media_id, $order );
$order--;
}
if( $media_id ) {
//mark the gallery assorted, we use it in MPP_Media_query to see what should be the default order
$media = mpp_get_media( $media_id );
//mark the gallery as sorted
mpp_mark_gallery_sorted( $media->gallery_id );
}
mpp_add_feedback( __( 'Updated', 'mediapress' ) );
if( $referer ) {
mpp_redirect( $referer );
}
}
add_action( 'bp_actions', 'mpp_action_reorder_gallery_media', 2 );
/**
* Handles Gallery Cover deletion
*
* @return type
*/
function mpp_action_delete_gallery_cover() {
if( ! mpp_is_gallery_cover_delete() ) {
return;
}
if( ! $_REQUEST['gallery_id'] ) {
return;
}
$gallery = mpp_get_gallery( absint( $_REQUEST['gallery_id'] ) );
$referer = $redirect_url = mpp_get_gallery_settings_url( $gallery ) ;
if( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete-cover' ) ) {
//add error message and return back to the old page
mpp_add_feedback( __( 'Action not authorized!', 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//we may want to allow passing of component from the form in future!
if( ! mpp_user_can_delete_gallery( $gallery ) ) {
mpp_add_feedback( __( "You don't have permission to delete this cover!", 'mediapress' ), 'error' );
if( $referer ) {
mpp_redirect( $referer );
}
return;
}
//we always need to delete this
$cover_id = mpp_get_gallery_cover_id( $gallery->id );
mpp_delete_gallery_cover_id( $gallery->id );
//if( $gallery->type != 'photo' ) {
//delete the uploaded cover too
mpp_delete_media( $cover_id );
//}
mpp_add_feedback( __( 'Cover deleted successfully!', 'mediapress' ) );
//if we are here, delete gallery and redirect to the component base url
mpp_redirect( $redirect_url );
}
add_action( 'bp_actions', 'mpp_action_delete_gallery_cover', 2 );
function mpp_action_publish_gallery_media_to_activity() {
if( ! is_user_logged_in() || ! mpp_is_gallery_management() || ! bp_is_action_variable( 'publish', 1 ) ) {
return;
}
$gallery_id = absint( $_GET['gallery_id'] );
if( ! $gallery_id ) {
return ;
}
//$referrer = wp_get_referer();
//if( empty( $referrer ) ) {
$referrer = mpp_get_gallery_edit_media_url( $gallery_id );
//}
//verify nonce
if( ! wp_verify_nonce( $_GET['_wpnonce'], 'publish' ) ) {
mpp_add_feedback( __( 'Unauthorized action.', 'mediapress' ), 'error' );
bp_core_redirect( $referrer );
}
//all is good check for permission
if( ! mpp_user_can_publish_gallery_activity( $gallery_id ) ) {
mpp_add_feedback( __( "You don't have sufficient permission.", 'mediapress' ), 'error' );
bp_core_redirect( $referrer );
}
if( ! mpp_gallery_has_unpublished_media( $gallery_id ) ) {
mpp_add_feedback( __( 'Nothing to publish.', 'mediapress' ), 'error' );
bp_core_redirect( $referrer );
}
//now we can safely publish
$media_ids = mpp_gallery_get_unpublished_media( $gallery_id );
$media_count = count( $media_ids );
$gallery = mpp_get_gallery( $gallery_id );
$type = $gallery->type;
$type_name = _n( $type, $type.'s', $media_count );
$user_link = bp_core_get_userlink( get_current_user_id() );
$gallery_url = mpp_get_gallery_permalink( $gallery );
$gallery_link = '<a href="' . esc_url( $gallery_url ) . '" title="'. esc_attr( $gallery->title ) . '">{$gallery->title}</a>';
//has media, has permission, so just publish now
//
$activity_id = mpp_gallery_record_activity( array(
'gallery_id' => $gallery_id,
'media_ids' => $media_ids,
'type' => 'media_publish',
'action' => sprintf( __( '%s shared %d %s to %s ', 'mediaprses'), $user_link, $media_count, $type_name, $gallery_link ),
'content' => '',
) );
if( $activity_id ) {
mpp_gallery_delete_unpublished_media( $gallery_id );
mpp_add_feedback( __( "Published to activity successfully.", 'mediapress' ) );
} else {
mpp_add_feedback( __( "There was a problem. Please try again later.", 'mediapress' ), 'error' );
}
bp_core_redirect( $referrer );
}
add_action( 'bp_actions', 'mpp_action_publish_gallery_media_to_activity', 2 );
function mpp_action_hide_unpublished_media() {
if( ! is_user_logged_in() || ! mpp_is_gallery_management() || ! bp_is_action_variable( 'delete-unpublished', 1 ) ) {
return;
}
$gallery_id = absint( $_GET['gallery_id'] );
if( ! $gallery_id ) {
return ;
}
//verify nonce
if( ! wp_verify_nonce( $_GET['_wpnonce'], 'delete-unpublished' ) ) {
//should we return or show error?
return ;
}
$referrer = mpp_get_gallery_edit_media_url( $gallery_id );
if( ! mpp_gallery_has_unpublished_media( $gallery_id ) ) {
mpp_add_feedback( __( 'Nothing to hide.', 'mediapress' ), 'error' );
bp_core_redirect( $referrer );
}
//check if user has permission
if( ! mpp_user_can_publish_gallery_activity( $gallery_id ) ) {
mpp_add_feedback( __( "You don't have sufficient permission.", 'mediapress' ), 'error' );
bp_core_redirect( $referrer );
}
mpp_gallery_delete_unpublished_media( $gallery_id );
mpp_add_feedback( __( "Successfully hidden!", 'mediapress' ) );
bp_core_redirect( $referrer );
}
add_action( 'bp_actions', 'mpp_action_hide_unpublished_media', 2 );
//if we are here, delete gallery and return back
//
//when a gallery is saved, let us do some magic
//it is called for backend as well as front end gallery creation
/**
* When a gallery post type post is created from the dashboard, we force to make it look like the one created from front end
*
* @param type $post_id
* @param type $post
* @param type $update
* @return type
*/
/** Clanup actions*/
function mpp_clean_gallery_cache( $gallery ) {
mpp_delete_gallery_cache( $gallery->id );
}
add_action( 'mpp_gallery_deleted', 'mpp_clean_gallery_cache' );
/**
* Generate an activity when a new gallery is created
* We will not generate any activity for profile galleries though.
* @param type $gallery_id
*/
function mpp_new_gallery_activity( $gallery_id ) {
//if the admin settings does not ask us to create new activity,
//Or it is explicitly restricted, do not proceed
if( ! mpp_is_auto_publish_to_activity_enabled( 'create_gallery' ) || apply_filters( 'mpp_do_not_record_create_gallery_activity', false ) ) {
return ;
}
$gallery = mpp_get_gallery( $gallery_id );
$user_link = bp_core_get_userlink( $gallery->user_id );
$link = mpp_get_gallery_permalink( $gallery );
mpp_gallery_record_activity( array(
'gallery_id' => $gallery_id,
'type' => 'create_gallery',
'content' => '',
'action' => sprintf( __( '%s created a %s <a href="%s">gallery</a>', 'mediapress' ), $user_link, $gallery->type, $link ),
) );
//record gallery creation as activity, there will be issue with wall gallery though
}
add_action( 'mpp_gallery_created', 'mpp_new_gallery_activity' );