-
Notifications
You must be signed in to change notification settings - Fork 2
/
archive.php
1321 lines (1131 loc) · 35.4 KB
/
archive.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 # -*- coding: utf-8 -*-
/**
* Plugin Name: Archive
* Description: Archive your post types, also with cron.
* Version: 1.0.2
* Plugin URI: https://github.com/bueltge/Archive
* Text Domain: archive
* Domain Path: /languages
* Author: Frank Bültge
* Author URI: http://bueltge.de/
* Licence: GPLv3+
* License URI: ./license.txt
*
* Php Version 5.3
*
* @package WordPress
* @author Frank Bültge <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 2015-04-06
*/
/**
* Don't call this file directly.
*/
defined( 'ABSPATH' ) || die();
add_action( 'plugins_loaded', array( 'FB_Archive', 'get_object' ) );
register_activation_hook( __FILE__, array( 'FB_Archive', 'on_activate' ) );
register_deactivation_hook( __FILE__, array( 'FB_Archive', 'on_deactivate' ) );
register_uninstall_hook( __FILE__, array( 'FB_Archive', 'on_deactivate' ) );
/**
* Class FB_Archive
*/
class FB_Archive {
static protected $classobj = NULL;
/*
* Key for textdomain
*
* @var string
*/
static public $textdomain = 'archive';
/*
* Key for custom post type
*
* @var string
*/
static public $post_type_1 = 'archiv';
/*
* Key for custom taxonomy
*
* @var string
*/
static public $taxonomy_type_1 = 'archive_structure';
/**
* Roles for edit, create custom post type
*
* @var array
*/
static public $todo_roles = array(
'administrator',
'editor',
);
/**
* Roles to read post type
*
* @var array
*/
static public $read_roles = array(
'administrator',
'editor',
'author',
'contributor',
'subscriber',
);
/**
* Key for save the original post type
*
* @var string
*/
public $post_meta_key = '_archived_post_type'; // use underline for don't see in custom fields
/**
* Keys for view archive-link on defined screens
* Add Screen Id or not an array for view link on all screens
*
* @see http://codex.wordpress.org/Plugin_API/Admin_Screen_Reference
* @var array
*/
public $def_archive_screens = array( 'edit-post', 'edit-page' );
/**
* Keys for view undo-archive-link on defined screens
*
* @var array
*/
public $def_unset_screens = array( 'edit-archiv' );
/**
* Key for active Scheduling posts
*
* @var boolean
*/
public $scheduled_archiving = FALSE; // true or false
/**
* Key for days, there we archived posts
*
* @var integer
*/
public $scheduled_archiving_days = 21; // in days
/**
* Keys for scheduled posts types
*
* @var array
*/
public $scheduled_archiving_post_type = array( 'post' ); // example: 'post', 'page'
/**
* Key for writing cache
*
* @since 2015-04-06
* @var string
*/
public $cache_archived_posts = 'fb_archived_posts';
/**
* Bool for add to query loop
*
* @var boolean
*/
public $add_to_query = FALSE; // true or false
/**
* points the class
*
* @access public
* @since 0.0.1
* @return object
*/
public static function get_object() {
if ( NULL === self::$classobj ) {
self::$classobj = new self;
}
return self::$classobj;
}
/**
* construct
*
* @uses add_filter, add_action, localize_plugin, register_activation_hook, register_uninstall_hook
* @access public
* @since 0.0.1
* @return \FB_Archive
*/
public function __construct() {
// include settings on profile
//require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'inc/class.settings.php';
//$fb_archive_settings = FB_Archive_Settings :: get_object();
// load language file
$this->localize_plugin();
// add post type
add_action( 'init', array( $this, 'build_post_type' ) );
// add scheduled archive
add_action( 'init', array( $this, 'schedule_archived_check' ) );
if ( (bool) $this->scheduled_archiving ) {
add_action( 'scheduled_archiving', array( $this, 'scheduled_archiving' ) );
}
add_action( 'admin_init', array( $this, 'add_settings_error' ) );
// admin interface init
add_action( 'admin_init', array( &$this, 'on_admin_init' ) );
add_action( 'admin_menu', array( $this, 'remove_menu_entry' ) );
// include js
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script' ), 10, 1 );
// help on snippet-pages in next version
add_action( 'contextual_help', array( &$this, 'add_help_text' ), 10, 3 );
// add to query loop
if ( $this->add_to_query ) {
add_action( 'pre_get_posts', array( $this, 'add_to_query' ) );
}
// add shortcode for list items of archive
add_shortcode( 'archive', array( $this, 'add_shortcode' ) );
}
/**
* Return Textdomain string
*
* @access public
* @since 0.0.2
* @return string
*/
public static function get_textdomain() {
return self::$textdomain;
}
/**
* localize_plugin function.
*
* @uses load_plugin_textdomain, plugin_basename
* @access public
* @since 0.0.1
* @return void
*/
public function localize_plugin() {
load_plugin_textdomain(
self::$textdomain,
FALSE,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
}
/**
* return plugin comment data
*
* @uses get_plugin_data
* @access public
* @since 0.0.1
*
* @param $value string, default = 'Version'
* Name, PluginURI, Version, Description, Author, AuthorURI, TextDomain, DomainPath, Network, Title
*
* @return string
*/
public static function get_plugin_data( $value = 'Version' ) {
static $plugin_data = array();
// fetch the data just once.
if ( isset( $plugin_data[ $value ] ) ) {
return $plugin_data[ $value ];
}
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
$plugin_data = get_plugin_data( __FILE__ );
$plugin_data = empty( $plugin_data[ $value ] ) ? '' : $plugin_data[ $value ];
return $plugin_data;
}
/**
* On activate plugin
*
* @uses deactivate_plugins, get_plugin_data, wp_sprintf, flush_rules
* @access public
* @since 0.0.1
* @return void
*/
static public function on_activate() {
global $wp_roles, $wp_version;
// check wp version
if ( ! version_compare( $wp_version, '3.0', '>=' ) ) {
deactivate_plugins( __FILE__ );
wp_die(
wp_sprintf(
'<strong>%s:</strong> ' .
esc_attr__( 'Sorry, This plugin requires WordPress 3.0+', self::$textdomain )
, self::get_plugin_data( 'Name' )
)
);
}
// check php version
if ( version_compare( PHP_VERSION, '5.2.0', '<' ) ) {
deactivate_plugins( __FILE__ ); // Deactivate our self
wp_die(
wp_sprintf(
'<strong>%1s:</strong> ' .
esc_attr__(
'Sorry, This plugin has taken a bold step in requiring PHP 5.0+, Your server is currently running PHP %2s, Please bug your host to upgrade to a recent version of PHP which is less bug-prone. At last count, <strong>over 80%% of WordPress installs are using PHP 5.2+</strong>.',
self::$textdomain
)
, self::get_plugin_data( 'Name' ), PHP_VERSION
)
);
}
foreach ( self::$todo_roles as $role ) {
$wp_roles->add_cap( $role, 'edit_' . self::$post_type_1 );
$wp_roles->add_cap( $role, 'read_' . self::$post_type_1 );
$wp_roles->add_cap( $role, 'delete_' . self::$post_type_1 );
$wp_roles->add_cap( $role, 'edit_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'edit_others_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'publish_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'read_private_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_private_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_published_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_others_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'edit_private_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'edit_published_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'manage_' . self::$taxonomy_type_1 );
$wp_roles->add_cap( $role, 'edit_' . self::$taxonomy_type_1 );
$wp_roles->add_cap( $role, 'delete_' . self::$taxonomy_type_1 );
$wp_roles->add_cap( $role, 'assign_' . self::$taxonomy_type_1 );
}
foreach ( self::$read_roles as $role ) {
$wp_roles->add_cap( $role, 'read_' . self::$post_type_1 );
}
flush_rewrite_rules();
}
/**
* On deactivate plugin remove capabilities
*
* @uses get_object, remove_cap, read_roles, flush_rules
* @access public
* @since 0.0.1
* @return void
*/
static public function on_deactivate() {
global $wp_roles;
foreach ( self::$todo_roles as $role ) {
$wp_roles->remove_cap( $role, 'edit_' . self::$post_type_1 );
$wp_roles->remove_cap( $role, 'read_' . self::$post_type_1 );
$wp_roles->remove_cap( $role, 'delete_' . self::$post_type_1 );
$wp_roles->remove_cap( $role, 'edit_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'edit_others_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'publish_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'read_private_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'delete_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'delete_private_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'delete_published_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'delete_others_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'edit_private_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'edit_published_' . self::$post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'manage_' . self::$taxonomy_type_1 );
$wp_roles->remove_cap( $role, 'edit_' . self::$taxonomy_type_1 );
$wp_roles->remove_cap( $role, 'delete_' . self::$taxonomy_type_1 );
$wp_roles->remove_cap( $role, 'assign_' . self::$taxonomy_type_1 );
}
foreach ( self::$read_roles as $role ) {
$wp_roles->remove_cap( $role, 'read_' . self::$post_type_1 );
}
flush_rewrite_rules();
}
/**
* On admin init
*
* @uses wp_register_style, wp_enqueue_style, add_action, add_filter, add_meta_box
* @access public
* @since 0.0.1
* @return void
*/
public function on_admin_init() {
add_filter( 'post_row_actions', array( $this, 'add_archive_link' ), 10, 2 );
add_filter( 'page_row_actions', array( $this, 'add_archive_link' ), 10, 2 );
add_action( 'admin_action_archive', array( $this, 'archive_post_type' ) );
add_action( 'admin_notices', array( $this, 'get_admin_notices' ) );
// modify bulk actions - current not possible in WP
//add_filter( 'bulk_actions-edit-' . self::$post_type_1, array( $this, 'filter_bulk_actions' ) );
add_filter( 'post_row_actions', array( $this, 'add_unset_archive_link' ), 10, 2 );
add_action( 'admin_action_unset_archive', array( $this, 'unset_archive_post_type' ) );
$this->add_value_to_row();
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'admin_head-edit.php', array( $this, 'add_custom_style' ) );
}
/**
* Enqueue scripts in WP
*
* @uses wp_enqueue_script
* @access public
* @since 0.0.1
*
* @param $pagehook
*
* @return void
*/
public function enqueue_script( $pagehook ) {
if ( 'edit.php' !== $pagehook ) {
return NULL;
}
$screen = get_current_screen();
if ( self::$post_type_1 !== $screen->post_type ) {
return NULL;
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : '';
wp_register_script(
'jquery-archive-script',
WP_PLUGIN_URL . '/' . dirname( plugin_basename( __FILE__ ) ) . '/js/script' . $suffix . '.js',
array( 'jquery' )
);
wp_enqueue_script( 'jquery-archive-script' );
}
/**
* Add meta box to post type Archive with information about ID and old post type
*
* @since 2015-01-16
* @return void
*/
public function add_meta_boxes() {
// add meta box with ID
add_meta_box(
'post_id',
esc_attr__( 'Archive Info', self::$textdomain ),
array( $this, 'additional_meta_box' ),
self::$post_type_1,
'side', 'high'
);
}
/**
* Add style value in head for ID column
*
* @since 2015-03-12
* @return null
*/
public function add_custom_style() {
$screen = get_current_screen();
if ( 'edit-archiv' !== $screen->id ) {
return NULL;
}
$style = '<style type="text/css">#aid { width: 10%; }</style>';
echo $style;
}
/**
* Schedule check
*
* @uses wp_schedule_event, wp_next_scheduled
* @access public
* @since 0.0.1
* @return void
*/
public function schedule_archived_check() {
if ( ! wp_next_scheduled( 'scheduled_archiving' ) && (bool) $this->scheduled_archiving ) {
wp_schedule_event( time(), 'twicedaily', 'scheduled_archiving' ); // hourly, daily and twicedaily
} elseif ( ! (bool) $this->scheduled_archiving && wp_next_scheduled( 'scheduled_archiving' ) ) {
wp_clear_scheduled_hook( 'scheduled_archiving' );
}
}
/**
* Add link on archive
*
* @uses get_post_type_object, get_archive_post_link, current_user_can, esc_attr
* @access public
* @since 0.0.1
*
* @param array string $actions
* @param integer $id
*
* @return array $actions
*/
public function add_archive_link( $actions, $id ) {
$screen = get_current_screen();
if ( ! isset( $screen->post_type ) ) {
return $actions;
}
// Not enough rights
$post_type_object = get_post_type_object( $screen->post_type );
if ( ! current_user_can( $post_type_object->cap->delete_post, $id->ID ) ) {
return $actions;
}
// Not on the right screen
if ( ! in_array( $screen->id, $this->def_archive_screens ) ) {
return $actions;
}
$actions[ 'archive' ] = '<a href="' . $this->get_archive_post_link( $id->ID )
. '" title="'
. esc_attr__( 'Move this item to the Archive', self::$textdomain )
. '">' . esc_attr__( 'Archive', self::$textdomain ) . '</a>';
return $actions;
}
/**
* Add undo-link on archive
*
* @uses get_post_type_object, current_user_can, get_post_meta, esc_attr
* @access public
* @since 0.0.1
*
* @param array string $actions
* @param integer $id
*
* @return array $actions
*/
public function add_unset_archive_link( $actions, $id ) {
$screen = get_current_screen();
if ( ! isset( $screen->post_type ) ) {
return $actions;
}
// Not enough rights
$post_type_object = get_post_type_object( $screen->post_type );
if ( ! current_user_can( $post_type_object->cap->delete_post, $id->ID ) ) {
return $actions;
}
// Not on the right screen
if ( ! in_array( $screen->id, $this->def_unset_screens ) ) {
return $actions;
}
$archived_post_type = get_post_meta( $id->ID, $this->post_meta_key, TRUE );
$actions[ 'archive' ] = '<a href="' . $this->get_unset_archive_post_link( $id->ID )
. '&on_archive=1" title="'
. esc_attr__( 'Move this item to the archived post type', self::$textdomain )
. ': ' . $archived_post_type
. '">' . esc_attr__( 'Restore to', self::$textdomain ) . ' <code>' . $archived_post_type . '</code></a>';
return $actions;
}
/**
* Return link for archive post type
* Use filter get_archive_post_link for custom link
*
* @uses get_post, get_post_type_object, current_user_can, apply_filters, admin_url, wp_nonce_url
* @access public
* @since 0.0.1
*
* @param integer $id , Default is 0
*
* @return string
*/
public function get_archive_post_link( $id = 0 ) {
if ( ! $post = get_post( $id ) ) {
return NULL;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( ! $post_type_object ) {
return NULL;
}
if ( ! current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
return NULL;
}
$action = NULL;
$archive_link = admin_url( 'admin.php?post=' . $post->ID . '&action=archive' );
return apply_filters(
'get_archive_post_link',
wp_nonce_url( $archive_link, "$action-{$post->post_type}_{$post->ID}" ),
$post->ID
);
}
/**
* Return link for undo-archive post type
* Use filter get_unset_archive_post_link for custom link
*
* @uses get_post, get_post_type_object, current_user_can, apply_filters, admin_url, wp_nonce_url
* @access public
* @since 0.0.1
*
* @param integer $id , Default is 0
*
* @return string
*/
public function get_unset_archive_post_link( $id = 0 ) {
if ( ! $post = get_post( $id ) ) {
return NULL;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( ! $post_type_object ) {
return NULL;
}
if ( ! current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
return NULL;
}
$action = NULL;
$archive_link = admin_url( 'admin.php?post=' . $post->ID . '&action=unset_archive' );
$archived_post_type = get_post_meta( $id, $this->post_meta_key, TRUE );
return apply_filters(
'get_unset_archive_post_link',
wp_nonce_url( $archive_link, "$action-{$archived_post_type}_{$post->ID}" ),
$post->ID
);
}
public function filter_bulk_actions( $actions ) {
$actions[ 'restore_archive' ] = esc_attr__( 'Restore to Post Type', self::$textdomain );
return $actions;
}
/**
* Archive post type
*
* @uses wp_die, set_post_type, add_post_meta, wp_redirect, admin_url
* @access public
* @since 0.0.1
* @return void
*/
public function archive_post_type() {
if ( ! (
isset( $_GET[ 'post' ] ) || ( isset( $_REQUEST[ 'action' ] ) && 'archive' == $_REQUEST[ 'action' ] )
)
) {
wp_die( esc_attr__( 'No post to archive has been supplied!', self::$textdomain ) );
}
$id = (int) ( isset( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : $_REQUEST[ 'post' ] );
if ( $id ) {
$redirect_post_type = '';
$archived_post_type = get_post_type( $id );
if ( ! empty( $archived_post_type ) ) {
$redirect_post_type = 'post_type=' . $archived_post_type . '&';
}
// change post type
set_post_type( $id, self::$post_type_1 );
// add old post_type to post meta
add_post_meta( $id, $this->post_meta_key, $archived_post_type, TRUE );
wp_redirect( admin_url( 'edit.php?' . $redirect_post_type . 'archived=1&ids=' . $id ) );
exit;
} else {
wp_die( esc_attr__( "Sorry, I can't find the post-post_id", self::$textdomain ) );
}
}
/**
* Undo archive post type
*
* @uses wp_die, set_post_type, delete_post_meta, wp_redirect, admin_url
* @access public
* @since 0.0.1
* @return void
*/
public function unset_archive_post_type() {
if ( ! (
isset( $_GET[ 'post' ] ) || ( isset( $_REQUEST[ 'action' ] ) && 'unset_archive' == $_REQUEST[ 'action' ] )
)
) {
wp_die( esc_attr__( 'No item to undo archive has been supplied!', self::$textdomain ) );
}
$id = (int) ( isset( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : $_REQUEST[ 'post' ] );
if ( $id ) {
$redirect_post_type = '';
// get archived post type
$archived_post_type = get_post_meta( $id, $this->post_meta_key, TRUE );
if ( ! empty( $archived_post_type ) ) {
$redirect_post_type = 'post_type=' . $archived_post_type . '&';
}
// change post type to archived post type
set_post_type( $id, $archived_post_type );
// remove archived post type on post meta
delete_post_meta( $id, $this->post_meta_key );
// redirect to edit-page od post type
wp_redirect( admin_url( 'edit.php?' . $redirect_post_type . 'unset_archived=1&ids=' . $id ) );
exit;
} else {
wp_die( esc_attr__( 'Sorry, i cant find the post-post_id', self::$textdomain ) );
}
}
/**
* For the scheduled archiving
*
* @uses wp_die, set_post_type, delete_post_meta, wp_redirect, admin_url
* @access public
* @since 0.0.1
* @return void
*/
public function scheduled_archiving() {
global $wpdb;
$current = get_site_transient( 'archivise_posts' );
if ( ! is_object( $current ) ) {
$current = new stdClass;
}
// Update last_checked for current to prevent multiple blocking requests if request hangs
$current->last_checked = time();
set_site_transient( 'archivise_posts', $current );
// convert post type array to string
$scheduled_archiving_post_types = NULL;
foreach ( $this->scheduled_archiving_post_type as $item ) {
$scheduled_archiving_post_types .= "'" . $item . "'" . ', ';
}
$scheduled_archiving_post_types = substr( $scheduled_archiving_post_types, 0, - strlen( ', ' ) );
// Check for cache
$archived = wp_cache_get( $this->cache_archived_posts );
if ( FALSE === $archived ) {
$archived = $wpdb->get_results(
"SELECT ID
FROM $wpdb->posts
WHERE post_type IN ( $scheduled_archiving_post_types)
AND post_date < '" . date(
'Y-m-d', strtotime( '-' . (int) $this->scheduled_archiving_days . ' days' )
) . "'",
ARRAY_A
);
// Write cache
wp_cache_set( $this->cache_archived_posts, $archived );
}
if ( is_wp_error( $archived ) ) {
return NULL;
}
if ( ! $archived ) {
return NULL;
}
foreach ( $archived as $value ) {
if ( $value[ 'ID' ] ) {
$archived_post_type = get_post_type( $value[ 'ID' ] );
// change post type
set_post_type( $value[ 'ID' ], self::$post_type_1 );
// add old post_type to post meta
add_post_meta( $value[ 'ID' ], $this->post_meta_key, $archived_post_type, TRUE );
}
}
$updates = new stdClass();
$updates->last_checked = time();
set_site_transient( 'archivise_posts', $updates );
}
/**
* Add Messages for admin notice
*
* @uses number_format_i18n, get_post_type, add_settings_error
* @access public
* @since 0.0.2
* @return void
*/
public function add_settings_error() {
$message_archived = NULL;
$message_unset_archived = NULL;
if ( isset( $_REQUEST[ 'archived' ] ) ) {
$message_archived = sprintf(
_n(
'Item moved to the Archive.',
'%s items moved to the Archive.',
number_format_i18n( $_REQUEST[ 'archived' ] ),
self::$textdomain
),
number_format_i18n( $_REQUEST[ 'archived' ] )
);
$ids = isset( $_REQUEST[ 'ids' ] ) ? $_REQUEST[ 'ids' ] : 0;
$message_archived .= ' <a href="' . $this->get_unset_archive_post_link( $ids ) . '">'
. esc_attr__( 'Undo' ) . '</a>';
}
if ( isset( $_REQUEST[ 'unset_archived' ] ) ) {
$message_unset_archived = sprintf(
_n(
'Item moved to the Post Type: %2$s.',
'%1$s items moved to the Post Types: %2$s.',
$_REQUEST[ 'unset_archived' ],
self::$textdomain
),
number_format_i18n( $_REQUEST[ 'unset_archived' ] ),
'<code>' . get_post_type( $_REQUEST[ 'ids' ] ) . '</code>'
);
}
if ( isset( $_REQUEST[ 'archived' ] ) && (int) $_REQUEST[ 'archived' ] ) {
add_settings_error(
'archived_message',
'archived',
$message_archived,
'updated'
);
}
if ( isset( $_REQUEST[ 'unset_archived' ] ) && (int) $_REQUEST[ 'unset_archived' ] ) {
add_settings_error(
'unset_archived_message',
'unset_archived',
$message_unset_archived,
'updated'
);
}
}
/**
* Return Admin Notice for inform about actions
*
* @uses settings_errors
* @access public
* @since 0.0.1
* @return string
*/
public function get_admin_notices() {
settings_errors( 'archived_message' );
settings_errors( 'unset_archived_message' );
}
/**
* Admin post meta contents
*
* @uses get_post_meta
* @access public
* @since 0.0.1
*
* @param array $data
*
* @return string markup with post-post_id
*/
public function additional_meta_box( $data ) {
if ( $data->ID ) {
$data_id = (int) $data->ID;
echo '<p>' . esc_attr__( 'ID of this archived item:', self::$textdomain )
. ' <code>' . $data_id . '</code></p>';
echo '<p>' . esc_attr__( 'Archived Post Type:', self::$textdomain )
. ' <code>' . get_post_meta( $data_id, $this->post_meta_key, TRUE ) . '</code></p>';
}
}
/**
* Register post type 'snippet'
*
* @uses register_post_type
* @access public
* @since 0.0.1
* @return void
*/
public function build_post_type() {
// labels for return post type Snippet
$labels = array(
'name' => esc_attr__( 'Archive', self::$textdomain ),
'singular_name' => esc_attr__( 'Archive', self::$textdomain ),
'add_new' => esc_attr__( 'Add New', self::$textdomain ),
'add_new_item' => esc_attr__( 'Add New Item', self::$textdomain ),
'edit_item' => esc_attr__( 'Edit Item', self::$textdomain ),
'new_item' => esc_attr__( 'New Item in Archive', self::$textdomain ),
'view_item' => esc_attr__( 'View Item', self::$textdomain ),
'search_items' => esc_attr__( 'Search in Archive', self::$textdomain ),
'not_found' => esc_attr__( 'No item found in Archive', self::$textdomain ),
'not_found_in_trash' => esc_attr__( 'No item found in Archive-Trash', self::$textdomain ),
'parent_item_colon' => esc_attr__( 'Parent item in Archive', self::$textdomain )
);
$capabilities = array(
'edit_post' => 'edit_' . self::$post_type_1,
'read_post' => 'read_' . self::$post_type_1,
'delete_post' => 'delete_' . self::$post_type_1,
'edit_posts' => 'edit_' . self::$post_type_1 . 's',
'edit_others_posts' => 'edit_others_' . self::$post_type_1 . 's',
'publish_posts' => 'publish_' . self::$post_type_1 . 's',
'read_private_posts' => 'read_private_' . self::$post_type_1 . 's',
'delete_posts' => 'delete_' . self::$post_type_1 . 's',
'delete_private_posts' => 'delete_private_' . self::$post_type_1 . 's',
'delete_published_posts' => 'delete_published_' . self::$post_type_1 . 's',
'delete_others_posts' => 'delete_others_' . self::$post_type_1 . 's',
'edit_private_posts' => 'edit_private_' . self::$post_type_1 . 's',
'edit_published_posts' => 'edit_published_' . self::$post_type_1 . 's',
);
$args = array(
'labels' => $labels,
'description' => esc_attr__(
'Archive post, pages and other post types to a Archive.', self::$textdomain
),
'public' => TRUE,
'exclude_from_search' => TRUE,
'publicly_queryable' => TRUE,
'show_in_nav_menus' => FALSE,
'show_in_rest' => TRUE,
'menu_position' => 22,
'menu_icon' => 'dashicons-archive',
//'capability_type' => 'post',
'capabilities' => $capabilities,
'supports' => array(
'title',
'editor',
'comments',
'revisions',
'trackbacks',
'author',
'excerpt',
'page-attributes',
'thumbnail',
'custom-fields',
'post-formats',
'page-attributes',
),
'taxonomies' => array( 'category', 'post_tag', self::$taxonomy_type_1 ),
'has_archive' => TRUE,
);
/**
* Filter to change the default values to create the custom post type for the Archive
*/
$args = apply_filters( 'archive_post_type_arguments', $args );
register_post_type( self::$post_type_1, $args );
}
/**
* Remove entry in submenu to add enw archive post type
*
* @access public
* @since 0.0.1
* @return void
*/
public function remove_menu_entry() {
global $submenu;
unset( $submenu[ 'edit.php?post_type=archiv' ][ 10 ] );
unset( $submenu[ 'edit.php?post_type=archiv' ][ 15 ] );
unset( $submenu[ 'edit.php?post_type=archiv' ][ 16 ] );
}
/**
* Return taxonomy strings
*
* @uses get_object_term_cache, wp_cache_add, wp_get_object_terms, _make_cat_compat
* @access public
* @since 0.0.1
*
* @param string $taxonomy key
* @param bool|int $post_id , Default is FALSE
*
* @return array string $categories
*/
public function get_the_taxonomy( $taxonomy, $post_id = FALSE ) {
global $post;
$post_id = (int) $post_id;
if ( ! $post_id ) {
$post_id = (int) $post->ID;
}