forked from Automattic/zoninator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoninator.php
1427 lines (1114 loc) · 44.2 KB
/
zoninator.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
/*
Plugin Name: Zone Manager (Zoninator)
Description: Curation made easy! Create "zones" then add and order your content!
Author: Mohammad Jangda, Automattic
Version: 0.5
Author URI: http://vip.wordpress.com
Copyright 2010-2012 Mohammad Jangda, Automattic
This plugin was built by Mohammad Jangda in conjunction with William Davis and the Bangor Daily News.
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
if( ! class_exists( 'Zoninator' ) ) :
define( 'ZONINATOR_VERSION', '0.5' );
define( 'ZONINATOR_PATH', dirname( __FILE__ ) );
define( 'ZONINATOR_URL', trailingslashit( plugins_url( '', __FILE__ ) ) );
require_once( ZONINATOR_PATH . '/functions.php' );
require_once( ZONINATOR_PATH . '/widget.zone-posts.php');
class Zoninator
{
var $key = 'zoninator';
var $zone_taxonomy = 'zoninator_zones';
var $zone_term_prefix = 'zone-';
var $zone_meta_prefix = '_zoninator_order_';
var $zone_nonce_prefix = 'zone-nonce';
var $zone_ajax_nonce_action = 'ajax-action';
var $zone_lock_period = 30; // number of seconds a lock is valid for
var $zone_max_lock_period = 600; // max number of seconds for all locks in a session
var $post_types = null;
var $zone_detail_defaults = array(
'description' => ''
// Add additional properties here!
);
var $zone_messages = null;
var $posts_per_page = 10;
function __construct() {
add_action( 'init', array( $this, 'init' ), 99 ); // init later after other post types have been registered
add_action( 'widgets_init', array( $this, 'widgets_init' ) );
$this->zone_messages = array(
'insert-success' => __( 'The zone was successfully created.', 'zoninator' ),
'update-success' => __( 'The zone was successfully updated.', 'zoninator' ),
'delete-success' => __( 'The zone was successfully deleted.', 'zoninator' ),
'error-general' => __( 'Sorry, something went wrong! Please try again?', 'zoninator' ),
'error-zone-lock' => __( 'Sorry, this zone is in use by %s and is currently locked. Please try again later.', 'zoninator' ),
'error-zone-lock-max' => __( 'Sorry, you have reached the maximum idle limit and will now be redirected to the Dashboard.', 'zoninator' ),
);
$this->default_post_types = array( 'post' );
$this->zone_lock_period = apply_filters( 'zoninator_zone_lock_period', $this->zone_lock_period );
$this->zone_max_lock_period = apply_filters( 'zoninator_zone_max_lock_period', $this->zone_max_lock_period );
$this->posts_per_page = apply_filters( 'zoninator_posts_per_page', $this->posts_per_page );
}
function init() {
do_action( 'zoninator_pre_init' );
// Default post type support
foreach( $this->default_post_types as $post_type )
add_post_type_support( $post_type, $this->zone_taxonomy );
// Register taxonomy
if( ! taxonomy_exists( $this->zone_taxonomy ) ) {
register_taxonomy( $this->zone_taxonomy, $this->get_supported_post_types(), array(
'label' => __( 'Zones', 'zoninator' ),
'hierarchical' => false,
'query_var' => false,
'rewrite' => false,
'public' => false,
) );
}
add_action( 'admin_init', array( $this, 'admin_controller' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_page_init' ) );
do_action( 'zoninator_post_init' );
}
function widgets_init() {
register_widget( 'Zoninator_ZonePosts_Widget' );
}
// Add necessary AJAX actions
function admin_ajax_init( ) {
add_action( 'wp_ajax_zoninator_reorder_posts', array( $this, 'ajax_reorder_posts' ) );
add_action( 'wp_ajax_zoninator_add_post', array( $this, 'ajax_add_post' ) );
add_action( 'wp_ajax_zoninator_remove_post', array( $this, 'ajax_remove_post' ) );
add_action( 'wp_ajax_zoninator_search_posts', array( $this, 'ajax_search_posts' ) );
add_action( 'wp_ajax_zoninator_update_lock', array( $this, 'ajax_update_lock' ) );
add_action( 'wp_ajax_zoninator_update_recent', array( $this, 'ajax_recent_posts' ) );
}
function admin_init() {
$this->admin_ajax_init();
// Enqueue Scripts and Styles
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
}
function admin_page_init() {
// Set up page
add_menu_page( __( 'Zoninator', 'zoninator' ), __( 'Zones', 'zoninator' ), $this->_get_manage_zones_cap(), $this->key, array( $this, 'admin_page' ), '', 11 );
}
function admin_enqueue_scripts() {
if( $this->is_zoninator_page() ) {
wp_enqueue_script( 'zoninator-js', ZONINATOR_URL . 'js/zoninator.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-mouse', 'jquery-ui-position', 'jquery-ui-sortable', 'jquery-ui-autocomplete' ), ZONINATOR_VERSION, true );
$options = array(
'baseUrl' => $this->_get_zone_page_url(),
'adminUrl' => admin_url(),
'ajaxNonceAction' => $this->_get_nonce_key( $this->zone_ajax_nonce_action ),
'errorGeneral' => $this->_get_message( 'error-general' ),
'errorZoneLock' => sprintf( $this->_get_message( 'error-zone-lock' ), __( 'another user', 'zoninator' ) ),
'errorZoneLockMax' => $this->_get_message( 'error-zone-lock-max' ),
'zoneLockPeriod' => $this->zone_lock_period,
'zoneLockPeriodMax' => $this->zone_max_lock_period,
);
wp_localize_script( 'zoninator-js', 'zoninatorOptions', $options );
// For mobile support
// http://github.com/furf/jquery-ui-touch-punch
wp_enqueue_script( 'jquery-ui-touch-punch', ZONINATOR_URL . 'js/jquery.ui.touch-punch.min.js', array( 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-mouse' ) );
}
}
function admin_enqueue_styles() {
if( $this->is_zoninator_page() ) {
wp_enqueue_style( 'zoninator-jquery-ui', ZONINATOR_URL . 'css/jquery-ui/smoothness/jquery-ui-zoninator.css', false, ZONINATOR_VERSION, 'all' );
wp_enqueue_style( 'zoninator-styles', ZONINATOR_URL . 'css/zoninator.css', false, ZONINATOR_VERSION, 'all' );
}
}
function admin_controller() {
if( $this->is_zoninator_page() ) {
$action = $this->_get_request_var( 'action' );
switch( $action ) {
case 'insert':
case 'update':
$zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' );
$this->verify_nonce( $action );
$this->verify_access( $action, $zone_id );
$name = $this->_get_post_var( 'name' );
$slug = $this->_get_post_var( 'slug', sanitize_title( $name ) );
$details = array(
'description' => $this->_get_post_var( 'description', '', 'strip_tags' )
);
// TODO: handle additional properties
if( $zone_id ) {
$result = $this->update_zone( $zone_id, array(
'name' => $name,
'slug' => $slug,
'details' => $details
) );
} else {
$result = $this->insert_zone( $slug, $name, $details );
}
if( is_wp_error( $result ) ) {
wp_redirect( add_query_arg( 'message', 'error-general' ) );
exit;
} else {
if( ! $zone_id && isset( $result['term_id'] ) )
$zone_id = $result['term_id'];
// Redirect with success message
$message = sprintf( '%s-success', $action );
wp_redirect( $this->_get_zone_page_url( array( 'action' => 'edit', 'zone_id' => $zone_id, 'message' => $message ) ) );
exit;
}
break;
case 'delete':
$zone_id = $this->_get_request_var( 'zone_id', 0, 'absint' );
$this->verify_nonce( $action );
$this->verify_access( $action, $zone_id );
if( $zone_id ) {
$result = $this->delete_zone( $zone_id );
}
if( is_wp_error( $result ) ) {
$redirect_args = array( 'error' => $result->get_error_messages() );
} else {
$redirect_args = array( 'message' => 'delete-success' );
}
wp_redirect( $this->_get_zone_page_url( $redirect_args ) );
exit;
}
}
}
function admin_page() {
global $zoninator_admin_page;
$view = $this->_get_value_or_default( 'view', $zoninator_admin_page, 'edit.php' );
$view = sprintf( '%s/views/%s', ZONINATOR_PATH, $view );
$title = __( 'Zones', 'zoninator' );
$zones = $this->get_zones();
$default_active_zone = 0;
if( ! $this->_current_user_can_add_zones() ) {
if( ! empty( $zones ) )
$default_active_zone = $zones[0]->term_id;
}
$active_zone_id = $this->_get_request_var( 'zone_id', $default_active_zone, 'absint' );
$active_zone = ! empty( $active_zone_id ) ? $this->get_zone( $active_zone_id ) : array();
if ( ! empty( $active_zone ) )
$title = __( 'Edit Zone', 'zoninator' );
$message = $this->_get_message( $this->_get_get_var( 'message', '', 'urldecode' ) );
$error = $this->_get_get_var( 'error', '', 'urldecode' );
?>
<div class="wrap zoninator-page">
<div id="icon-edit-pages" class="icon32"><br /></div>
<h2>
<?php echo esc_html( $title ); ?>
<?php if( $this->_current_user_can_add_zones() ) : ?>
<?php if( $active_zone_id ) : ?>
<a href="<?php echo $new_link; ?>" class="add-new-h2 zone-button-add-new"><?php _e( 'Add New', 'zoninator' ); ?></a>
<?php else : ?>
<span class="nav-tab nav-tab-active zone-tab zone-tab-active"><?php _e( 'Add New', 'zoninator' ); ?></span>
<?php endif; ?>
<?php endif; ?>
</h2>
<?php if( $message ) : ?>
<div id="zone-message" class="updated below-h2">
<p><?php echo esc_html( $message ); ?></p>
</div>
<?php endif; ?>
<?php if( $error ) : ?>
<div id="zone-message" class="error below-h2">
<p><?php echo esc_html( $error ); ?></p>
</div>
<?php endif; ?>
<div id="zoninator-wrap">
<?php $this->admin_page_zone_tabs( $zones, $active_zone_id ); ?>
<?php $this->admin_page_zone_edit( $active_zone ); ?>
</div>
</div>
<?php
}
function admin_page_zone_tabs( $zones, $active_zone_id = 0 ) {
$new_link = $this->_get_zone_page_url( array( 'action' => 'new' ) );
?>
<div class="nav-tabs-container zone-tabs-container">
<div class="nav-tabs-nav-wrapper zone-tabs-nav-wrapper">
<div class="nav-tabs-wrapper zone-tabs-wrapper">
<div class="nav-tabs zone-tabs">
<?php foreach( $zones as $zone ) : ?>
<?php $zone_id = $this->get_zone_id( $zone ); ?>
<?php $zone_link = $this->_get_zone_page_url( array( 'action' => 'edit', 'zone_id' => $zone_id ) ); ?>
<?php if( $active_zone_id && $zone_id == $active_zone_id ) : ?>
<span class="nav-tab nav-tab-active zone-tab zone-tab-active"><?php echo esc_html( $zone->name ); ?></span>
<?php else : ?>
<a href="<?php echo esc_url( $zone_link ); ?>" class="nav-tab zone-tab"><?php echo esc_html( $zone->name ); ?></a>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<?php
}
function admin_page_zone_edit( $zone = null ) {
$zone_id = $this->_get_value_or_default( 'term_id', $zone, 0, 'absint' );
$zone_name = $this->_get_value_or_default( 'name', $zone );
$zone_slug = $this->_get_value_or_default( 'slug', $zone, '', array( $this, 'get_unformatted_zone_slug' ) );
$zone_description = $this->_get_value_or_default( 'description', $zone );
$zone_posts = $zone_id ? $this->get_zone_posts( $zone ) : array();
$zone_locked = $this->is_zone_locked( $zone_id );
$delete_link = $this->_get_zone_page_url( array( 'action' => 'delete', 'zone_id' => $zone_id ) );
$delete_link = wp_nonce_url( $delete_link, $this->_get_nonce_key( 'delete' ) );
?>
<div id="zone-edit-wrapper">
<?php if( ( $zone_id == 0 && $this->_current_user_can_add_zones() ) || ( $zone_id != 0 && $this->_current_user_can_manage_zones() ) ) : ?>
<?php if( $zone_locked ) : ?>
<?php $locking_user = get_userdata( $zone_locked ); ?>
<div class="updated below-h2">
<p><?php echo sprintf( $this->_get_message( 'error-zone-lock' ), sprintf( '<a href="mailto:%s">%s</a>', $locking_user->user_email, $locking_user->display_name ) ); ?></p>
</div>
<input type="hidden" id="zone-locked" name="zone-locked" value="1" />
<?php endif; ?>
<div class="col-wrap zone-col zone-info-col">
<div class="form-wrap zone-form zone-info-form">
<?php if( $this->_current_user_can_edit_zones( $zone_id ) && ! $zone_locked ) : ?>
<form id="zone-info" method="post">
<?php do_action( 'zoninator_pre_zone_fields', $zone ); ?>
<div class="form-field zone-field">
<label for="zone-name"><?php _e( 'Name', 'zoninator' ); ?></label>
<input type="text" id="zone-name" name="name" value="<?php echo esc_attr( $zone_name ); ?>" />
</div>
<?php if( $zone_id ) : ?>
<div class="form-field zone-field">
<label for="zone-slug"><?php _e( 'Slug', 'zoninator' ); ?></label>
<span><?php echo esc_attr( $zone_slug ); ?></span>
<input type="hidden" id="zone-slug" name="slug" value="<?php echo esc_attr( $zone_slug ); ?>" />
</div>
<?php endif; ?>
<div class="form-field zone-field">
<label for="zone-description"><?php _e( 'Description', 'zoninator' ); ?></label>
<textarea id="zone-description" name="description"><?php echo esc_html( $zone_description ); ?></textarea>
</div>
<?php do_action( 'zoninator_post_zone_fields', $zone ); ?>
<?php if( $zone_id ) : ?>
<input type="hidden" name="zone_id" id="zone_id" value="<?php echo esc_attr( $zone_id ) ?>" />
<?php wp_nonce_field( $this->_get_nonce_key( 'update' ) ); ?>
<?php else : ?>
<?php wp_nonce_field( $this->_get_nonce_key( 'insert' ) ); ?>
<?php endif; ?>
<div class="submit-field submitbox">
<input type="submit" value="<?php _e('Save', 'zoninator'); ?>" name="submit" class="button-primary" />
<?php if( $zone_id ) : ?>
<a href="<?php echo $delete_link ?>" class="submitdelete" onclick="return confirm('<?php echo esc_js( 'Are you sure you want to delete this zone?', 'zoninator' ); ?>')"><?php _e('Delete', 'zoninator') ?></a>
<?php endif; ?>
</div>
<input type="hidden" name="action" value="<?php echo $zone_id ? 'update' : 'insert'; ?>">
<input type="hidden" name="page" value="<?php echo $this->key; ?>">
</form>
<?php else : ?>
<div id="zone-info-readonly" class="readonly">
<?php do_action( 'zoninator_pre_zone_readonly', $zone ); ?>
<div class="form-field zone-field">
<label for="zone-name"><?php _e( 'Name', 'zoninator' ); ?></label>
<span><?php echo esc_attr( $zone_name ); ?></span>
</div>
<!--
<div class="form-field zone-field">
<label for="zone-slug"><?php _e( 'Slug', 'zoninator' ); ?></label>
<span><?php echo esc_attr( $zone_slug ); ?></span>
</div>
-->
<div class="form-field zone-field">
<label for="zone-description"><?php _e( 'Description', 'zoninator' ); ?></label>
<span><?php echo esc_html( $zone_description ); ?></span>
</div>
<input type="hidden" name="zone_id" id="zone_id" value="<?php echo esc_attr( $zone_id ) ?>" />
<?php do_action( 'zoninator_post_zone_readonly', $zone ); ?>
</div>
<?php endif; ?>
<?php // Ideally, we should seperate nonces for each action. But this will do for simplicity. ?>
<?php wp_nonce_field( $this->_get_nonce_key( $this->zone_ajax_nonce_action ), $this->_get_nonce_key( $this->zone_ajax_nonce_action ), false ); ?>
</div>
</div>
<div class="col-wrap zone-col zone-posts-col">
<div class="zone-posts-wrapper <?php echo ! $this->_current_user_can_manage_zones( $zone_id ) || $zone_locked ? 'readonly' : ''; ?>">
<?php if( $zone_id ) : ?>
<h3><?php _e( 'Zone Content', 'zoninator' ); ?></h3>
<?php $this->zone_advanced_search_filters(); ?>
<?php $this->zone_admin_recent_posts_dropdown( $zone_id ); ?>
<?php $this->zone_admin_search_form(); ?>
<div class="zone-posts-list">
<?php foreach( $zone_posts as $post ) : ?>
<?php $this->admin_page_zone_post( $post, $zone ); ?>
<?php endforeach; ?>
</div>
<?php else : ?>
<p class="description"><?php _e( 'To create a zone, enter a name (and any other info) to to left and click "Save". You can then choose content items to add to the zone.', 'zoninator' ); ?></p>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
<?php
}
function admin_page_zone_post( $post, $zone ) {
$columns = apply_filters( 'zoninator_zone_post_columns', array(
'position' => array( $this, 'admin_page_zone_post_col_position' ),
'info' => array( $this, 'admin_page_zone_post_col_info' )
), $post, $zone );
?>
<div id="zone-post-<?php echo $post->ID; ?>" class="zone-post" data-post-id="<?php echo $post->ID; ?>">
<table>
<tr>
<?php foreach( $columns as $column_key => $column_callback ) : ?>
<?php if( is_callable( $column_callback ) ) : ?>
<td class="zone-post-col zone-post-<?php echo $column_key; ?>">
<?php call_user_func( $column_callback, $post, $zone ); ?>
</td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
</table>
<input type="hidden" name="zone-post-id" value="<?php echo $post->ID; ?>" />
</div>
<?php
}
function admin_page_zone_post_col_position( $post, $zone ) {
$current_position = $this->get_post_order( $post->ID, $zone );
?>
<span title="<?php esc_attr_e( 'Click and drag to change the position of this item.', 'zoninator' ); ?>">
<?php echo esc_html( $current_position ); ?>
</span>
<?php
}
function admin_page_zone_post_col_info( $post, $zone ) {
$action_links = array(
sprintf( '<a href="%s" class="edit" target="_blank" title="%s">%s</a>', get_edit_post_link( $post->ID ), __( 'Opens in new window', 'zoninator' ), __( 'Edit', 'zoninator' ) ),
sprintf( '<a href="#" class="delete" title="%s">%s</a>', __( 'Remove this item from the zone', 'zoninator' ), __( 'Remove', 'zoninator' ) ),
sprintf( '<a href="%s" class="view" target="_blank" title="%s">%s</a>', get_permalink( $post->ID ), __( 'Opens in new window', 'zoninator' ), __( 'View', 'zoninator' ) ),
// Move To
// Copy To
);
?>
<?php echo sprintf( '%s <span class="zone-post-status">(%s)</span>', esc_html( $post->post_title ), esc_html( $post->post_status ) ); ?>
<div class="row-actions">
<?php echo implode( ' | ', $action_links ); ?>
</div>
<?php
}
function zone_advanced_search_filters() {
$current_cat = $this->_get_post_var( 'zone_advanced_filter_taxonomy', '', 'absint' );
$current_date = $this->_get_post_var( 'zone_advanced_filter_date', '', 'striptags' );
?>
<div class="zone-advanced-search-filters-heading">
<span class="zone-toggle-advanced-search" data-alt-label="<?php esc_attr_e( 'Hide', 'zoninator' ); ?>"><?php _e( 'Show Filters', 'zoninator' ); ?></span>
</div>
<div class="zone-advanced-search-filters-wrapper">
<label for="zone_advanced_filter_taxonomy"><?php _e( 'Filter:', 'zoninator' ); ?></label>
<?php
wp_dropdown_categories( apply_filters( 'zoninator_advanced_filter_category', array(
'show_option_all' => __( 'Show all Categories', 'zoninator' ),
'selected' => $current_cat,
'name' => 'zone_advanced_filter_taxonomy',
'id' => 'zone_advanced_filter_taxonomy',
'hide_if_empty' => true,
) ) );
$date_filters = apply_filters( 'zoninator_advanced_filter_date', array( 'all', 'today', 'yesterday') );
?>
<select name="zone_advanced_filter_date" id="zone_advanced_filter_date">
<?php
// Convert string dates into actual dates
foreach( $date_filters as $date ) :
$timestamp = strtotime( $date );
$output = ( $timestamp ) ? date( 'Y-m-d', $timestamp ) : 0;
echo sprintf( '<option value="%s" %s>%s</option>', esc_attr( $output ), selected( $output, $current_date, false ), esc_html( $date ) );
endforeach;
?>
</select>
</div>
<?php
}
function ajax_recent_posts() {
$cat = $this->_get_post_var( 'cat', '', 'absint' );
$date = $this->_get_post_var( 'date', '', 'striptags' );
$zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' );
$limit = $this->posts_per_page;
$post_types = $this->get_supported_post_types();
$zone_posts = $this->get_zone_posts( $zone_id );
$zone_post_ids = wp_list_pluck( $zone_posts, 'ID' );
// Verify nonce
$this->verify_nonce( $this->zone_ajax_nonce_action );
$this->verify_access( '', $zone_id );
if( is_wp_error( $result ) ) {
$status = 0;
$content = $result->get_error_message();
} else {
$args = array(
'posts_per_page' => $limit,
'order' => 'DESC',
'orderby' => 'post_date',
'post_type' => $post_types,
'ignore_sticky_posts' => true,
'post_status' => array( 'publish', 'future' ),
'post__not_in' => $zone_post_ids,
);
if ( $this->_validate_category_filter( $cat ) ) {
$args['cat'] = $cat;
}
if ( $this->_validate_date_filter( $date ) ) {
$filter_date_parts = explode( '-', $date );
$args['year'] = $filter_date_parts[0];
$args['monthnum'] = $filter_date_parts[1];
$args['day'] = $filter_date_parts[2];
}
$content = '';
$recent_posts = get_posts( $args );
foreach ( $recent_posts as $post ) :
$content .= sprintf( '<option value="%d">%s</option>', $post->ID, get_the_title( $post->ID ) );
endforeach;
wp_reset_postdata();
$status = 1;
}
$empty_label = '';
if ( ! $content ) {
$empty_label = __( 'No results found', 'zoninator' );
} elseif ( $cat ) {
$empty_label = sprintf( __( 'Choose post from %s', 'zoninator' ), get_the_category_by_ID( $cat ) );
} else {
$empty_label = __( 'Choose a post', 'zoninator' );
}
$content = '<option value="">' . esc_html( $empty_label ) . '</option>' . $content;
$this->ajax_return( $status, $content );
}
function zone_admin_recent_posts_dropdown( $zone_id ) {
$limit = $this->posts_per_page;
$post_types = $this->get_supported_post_types();
$zone_posts = $this->get_zone_posts( $zone_id );
$zone_post_ids = wp_list_pluck( $zone_posts, 'ID' );
$args = apply_filters( 'zoninator_recent_posts_args', array(
'posts_per_page' => $limit,
'order' => 'DESC',
'orderby' => 'post_date',
'post_type' => $post_types,
'ignore_sticky_posts' => true,
'post_status' => array( 'publish', 'future' ),
'post__not_in' => $zone_post_ids,
) );
$recent_posts = get_posts( $args );
?>
<div class="zone-search-wrapper">
<label for="zone-post-search-latest"><?php _e( 'Add Recent Content', 'zoninator' );?></label><br />
<select name="search-posts" id="zone-post-latest">
<option value=""><?php _e( 'Choose a post', 'zoninator' ); ?></option>
<?php
foreach ( $recent_posts as $post ) :
echo sprintf( '<option value="%d">%s</option>', $post->ID, esc_html( get_the_title( $post->ID ) ) );
endforeach;
wp_reset_postdata();
?>
</select>
</div>
<?php
}
function zone_admin_search_form() {
?>
<div class="zone-search-wrapper">
<label for="zone-post-search"><?php _e( 'Search for content', '' );?></label>
<input type="text" id="zone-post-search" name="search" />
<p class="description"><?php _e( 'Enter a term or phrase in the text box above to search for and add content to this zone.', 'zoninator' ); ?></p>
</div>
<?php
}
function is_zoninator_page() {
global $current_screen;
if( function_exists( 'get_current_screen' ) )
$screen = get_current_screen();
if( empty( $screen ) ) {
return ! empty( $_REQUEST['page'] ) && sanitize_key( $_REQUEST['page'] ) == $this->key;
} else {
return ! empty( $screen->id ) && strstr( $screen->id, $this->key );
}
}
function ajax_return( $status, $content = '', $action = '' ) {
$action = ! empty( $action ) ? $action : $this->zone_ajax_nonce_action;
$nonce = wp_create_nonce( $this->_get_nonce_key( $action ) );
echo json_encode( array(
'status' => $status,
'content' => $content,
'nonce' => $nonce,
) );
exit;
}
function ajax_add_post() {
$zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' );
$post_id = $this->_get_post_var( 'post_id', 0, 'absint' );
// Verify nonce
$this->verify_nonce( $this->zone_ajax_nonce_action );
$this->verify_access( '', $zone_id );
// Validate
if( ! $zone_id || ! $post_id )
$this->ajax_return( 0 );
$result = $this->add_zone_posts( $zone_id, $post_id, true );
if( is_wp_error( $result ) ) {
$status = 0;
$content = $result->get_error_message();
} else {
$post = get_post( $post_id );
$zone = $this->get_zone( $zone_id );
ob_start();
$this->admin_page_zone_post( $post, $zone );
$content = ob_get_contents();
ob_end_clean();
$status = 1;
}
$this->ajax_return( $status, $content );
}
function ajax_remove_post() {
$zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' );
$post_id = $this->_get_post_var( 'post_id', 0, 'absint' );
// Verify nonce
$this->verify_nonce( $this->zone_ajax_nonce_action );
$this->verify_access( '', $zone_id );
// Validate
if( ! $zone_id || ! $post_id )
$this->ajax_return( 0 );
$result = $this->remove_zone_posts( $zone_id, $post_id );
if( is_wp_error( $result ) ) {
$status = 0;
$content = $result->get_error_message();
} else {
$status = 1;
$content = '';
}
$this->ajax_return( $status, $content );
}
function ajax_reorder_posts() {
$zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' );
$post_ids = (array) $this->_get_post_var( 'posts', array(), 'absint' );
// Verify nonce
$this->verify_nonce( $this->zone_ajax_nonce_action );
$this->verify_access( '', $zone_id );
// validate
if( ! $zone_id || empty( $post_ids ) )
$this->ajax_return( 0 );
$result = $this->add_zone_posts( $zone_id, $post_ids, false );
if( is_wp_error( $result ) ) {
$status = 0;
$content = $result->get_error_message();
} else {
$status = 1;
$content = '';
}
$this->ajax_return( $status, $content );
}
// TODO: implement in front-end
function ajax_move_zone_post( $from_zone, $to_zone, $post_id ) {
$from_zone_id = $this->_get_post_var( 'from_zone_id', 0, 'absint' );
$to_zone_id = $this->_get_post_var( 'to_zone_id', 0, 'absint' );
$this->verify_nonce( $this->zone_ajax_nonce_action );
// TODO: validate both zones exist, post exists
// Add to new zone
$this->add_zone_posts( $to_zone_id, $post_id, true );
// Remove from old zone
$this->remove_zone_posts( $from_zone_id, $post_id );
}
function ajax_search_posts() {
$q = $this->_get_request_var( 'term', '', 'stripslashes' );
if( ! empty( $q ) ) {
$filter_cat = $this->_get_request_var( 'cat', '', 'absint' );
$filter_date = $this->_get_request_var( 'date', '', 'striptags' );
$post_types = $this->get_supported_post_types();
$limit = $this->_get_request_var( 'limit', $this->posts_per_page );
if( $limit <= 0 )
$limit = $this->posts_per_page;
$exclude = (array) $this->_get_request_var( 'exclude', array(), 'absint' );
$args = apply_filters( 'zoninator_search_args', array(
's' => $q,
'post__not_in' => $exclude,
'posts_per_page' => $limit,
'post_type' => $post_types,
'post_status' => array( 'publish', 'future' ),
'order' => 'DESC',
'orderby' => 'post_date',
'suppress_filters' => true,
) );
if ( $this->_validate_category_filter( $filter_cat ) ) {
$args['cat'] = $filter_cat;
}
if ( $this->_validate_date_filter( $filter_date ) ) {
$filter_date_parts = explode( '-', $filter_date );
$args['year'] = $filter_date_parts[0];
$args['monthnum'] = $filter_date_parts[1];
$args['day'] = $filter_date_parts[2];
}
$query = new WP_Query( $args );
$stripped_posts = array();
if ( ! $query->have_posts() )
exit;
foreach( $query->posts as $post ) {
$stripped_posts[] = array(
'title' => ! empty( $post->post_title ) ? $post->post_title : __( '(no title)', 'zoninator' ),
'post_id' => $post->ID,
'date' => get_the_time( get_option( 'date_format' ), $post ),
'post_type' => $post->post_type,
'post_status' => $post->post_status,
);
}
echo json_encode( $stripped_posts );
exit;
}
}
function ajax_update_lock() {
$zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' );
$this->verify_nonce( $this->zone_ajax_nonce_action );
$this->verify_access( '', $zone_id );
if( ! $zone_id )
exit;
if( ! $this->is_zone_locked( $zone_id ) ) {
$this->lock_zone( $zone_id );
$this->ajax_return( 1, '' );
}
}
function get_supported_post_types() {
if( isset( $this->post_types ) )
return $this->post_types;
$this->post_types = array();
foreach( get_post_types() as $post_type ) {
if( post_type_supports( $post_type, $this->zone_taxonomy ) )
array_push( $this->post_types, $post_type );
}
return $this->post_types;
}
function insert_zone( $slug, $name = '', $details = array() ) {
// slug cannot be empty
if( empty( $slug ) )
return new WP_Error( 'zone-empty-slug', __( 'Slug is a required field.', 'zoninator' ) );
$slug = $this->get_formatted_zone_slug( $slug );
$name = ! empty( $name ) ? $name : $slug;
$details = wp_parse_args( $details, $this->zone_detail_defaults );
$details = maybe_serialize( stripslashes_deep( $details ) );
$args = array(
'slug' => $slug,
'description' => $details,
);
// Filterize to allow other inputs
$args = apply_filters( 'zoninator_insert_zone', $args );
return wp_insert_term( $name, $this->zone_taxonomy, $args );
}
function update_zone( $zone, $data = array() ) {
$zone_id = $this->get_zone_id( $zone );
if( $this->zone_exists( $zone_id ) ) {
$zone = $this->get_zone( $zone );
$name = $this->_get_value_or_default( 'name', $data, $zone->name );
$slug = $this->_get_value_or_default( 'slug', $data, $zone->slug, array( $this, 'get_formatted_zone_slug' ) );
$details = $this->_get_value_or_default( 'details', $data, array() );
// TODO: Back-fill current zone details
//$details = wp_parse_args( $details, $this->zone_detail_defaults );
$details = wp_parse_args( $details, $this->zone_detail_defaults );
$details = maybe_serialize( stripslashes_deep( $details ) );
$args = array(
'name' => $name,
'slug' => $slug,
'description' => $details
);
// Filterize to allow other inputs
$args = apply_filters( 'zoninator_update_zone', $args, $zone_id, $zone );
return wp_update_term( $zone_id, $this->zone_taxonomy, $args );
}
return new WP_Error( 'invalid-zone', __( 'Sorry, that zone doesn\'t exist.', 'zoninator' ) );
}
function delete_zone( $zone ) {
$zone_id = $this->get_zone_id( $zone );
$meta_key = $this->get_zone_meta_key( $zone );
$this->_empty_zone_posts_cache( $meta_key );
if( $this->zone_exists( $zone_id ) ) {
// Delete all post associations for the zone
$this->remove_zone_posts( $zone_id );
// Delete the term
$delete = wp_delete_term( $zone_id, $this->zone_taxonomy );
if( ! $delete ) {
return new WP_Error( 'delete-zone', __( 'Sorry, we couldn\'t delete the zone.', 'zoninator' ) );
} else {
do_action( 'zoninator_delete_zone', $zone_id );
return $delete;
}
}
return new WP_Error( 'invalid-zone', __( 'Sorry, that zone doesn\'t exist.', 'zoninator' ) );
}
function add_zone_posts( $zone, $posts, $append = false ) {
$zone = $this->get_zone( $zone );
$meta_key = $this->get_zone_meta_key( $zone );
$this->_empty_zone_posts_cache( $meta_key );
if( $append ) {
// Order should be the highest post order
$last_post = $this->get_last_post_in_zone( $zone );
if( $last_post )
$order = $this->get_post_order( $last_post, $zone );
else
$order = 0;
} else {
$order = 0;
$this->remove_zone_posts( $zone );
}
foreach( (array) $posts as $post ) {
$post_id = $this->get_post_id( $post );
if( $post_id ) {
$order++;
update_metadata( 'post', $post_id, $meta_key, $order, true );
}
// TODO: remove_object_terms -- but need remove object terms function :(
}
clean_term_cache( $this->get_zone_id( $zone ), $this->zone_taxonomy ); // flush cache for our zone term and related APC caches
do_action( 'zoninator_add_zone_posts', $posts, $zone );
}
function remove_zone_posts( $zone, $posts = null ) {
$zone = $this->get_zone( $zone );
$meta_key = $this->get_zone_meta_key( $zone );
$this->_empty_zone_posts_cache( $meta_key );
// if null, delete all
if( ! $posts )
$posts = $this->get_zone_posts( $zone );
foreach( (array) $posts as $post ) {
$post_id = $this->get_post_id( $post );
if( $post_id )
delete_metadata( 'post', $post_id, $meta_key );
}
clean_term_cache( $this->get_zone_id( $zone ), $this->zone_taxonomy ); // flush cache for our zone term and related APC caches
do_action( 'zoninator_remove_zone_posts', $posts, $zone );
}
function get_zone_posts( $zone, $args = array() ) {
// Check cache first
if( $posts = $this->get_zone_posts_from_cache( $zone, $args ) )
return $posts;
$query = $this->get_zone_query( $zone, $args );
$posts = $query->get_posts();
// Add posts to cache
$this->add_zone_posts_to_cache( $posts, $zone, $args );
return $posts;
}
function get_zone_query( $zone, $args = array() ) {
$meta_key = $this->get_zone_meta_key( $zone );
$defaults = array(
'order' => 'ASC',
'posts_per_page' => -1,
'post_type' => $this->get_supported_post_types(),