This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haste-archives-content.php
173 lines (151 loc) · 6.04 KB
/
haste-archives-content.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
<?php
/**
* Plugin Name: Haste Archives Content
* Plugin URI:
* Description: A Plugins to create archives content insertion pages.
* Version: 1.0
* Author: Allyson Souza
* Author URI: http://www.hastedesign.com.br
* License: GPL2
*/
namespace haste_ac;
if ( ! defined( 'ABSPATH' ) )
{
exit;
}
if ( ! class_exists( 'ArchivesContent' ) )
{
class ArchivesContent {
protected static $instance;
private $post_types;
private $author_ID;
public function __construct() {
//Get all the registered post types of the site
add_action( 'init', array( &$this, 'set_the_post_types' ), 60 );
//Call the post type Archives creation function
add_action( 'init', array( &$this, 'create_post_type' ), 80 );
//Create archives posts for each custom post type created
add_action( 'init', array( &$this, 'create_posts' ), 100 );
//Get current archive content
add_filter( 'haste_archive_content', array( &$this, 'archive_content' ) );
// includes
$this->include_before_theme();
}
public static function init()
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
/**
* Creates the Archive Content post type
*
* @return void
*/
public function set_the_post_types() {
$args = array(
'_builtin' => false
);
$this->post_types = get_post_types( $args, 'objects' );
unset($this->post_types['archives']);
}
/**
* Creates the Archive Content post type
*
* @return void
*/
public function create_post_type() {
$labels = array(
'name' => 'Archives',
'singular_name' => 'Archive',
'menu_name' => 'Archives',
'name_admin_bar' => 'Archives',
'add_new' => 'Add New',
'add_new_item' => 'Add New Archive',
'new_item' => 'New Archive',
'edit_item' => 'Edit Archive',
'view_item' => 'View Archive',
'all_items' => 'All Archives',
'search_items' => 'Search Archives',
'parent_item_colon' => '',
'not_found' => 'No archive found.',
'not_found_in_trash' => 'No archives found in trash.'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search'=> true,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => true,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 60,
'has_archive' => false,
'can_export' => false,
'menu_icon' => 'dashicons-archive',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'capabilities' => array(
'edit_post' => true,
'edit_posts' => 'edit_posts',
'edit_others_posts' => 'edit_other_posts',
'publish_posts' => 'publish_posts',
'read_post' => 'read_post',
'read_private_posts' => 'read_private_posts',
'delete_post' => true,
'create_posts' => false,
),
'map_meta_cap' => true, //Set to false, if users are not allowed to edit/delete existing posts
);
register_post_type( 'archives', $args );
}
/**
* Programmatically create Archives posts.
*
* @returns -1 if the post was never created, -2 if a post with the same title exists, or the ID
* of the post if successful.
*/
public function create_posts() {
foreach( $this->post_types as $post_type ) {
// Setup the author, slug, and title for the post
$author_ID = 1;
$slug = $post_type->rewrite['slug'] . '-archive';
$title = $post_type->label . ' Archive';
$page = get_page_by_title( $title, 'OBJECT', 'archives' );
// If the page doesn't already exist, then create it
if( empty( $page ) ) {
wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_ID,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'archives',
'wp_error' => true
)
);
}
}
}
function include_before_theme() {
include_once('core/api.php');
}
/**
* Delete the Archives posts from database
*
* @return void
*/
public static function on_uninstall() {
$wpdb->query(
$wpdb->prepare( "DELETE FROM $wpdb->posts WHERE post_type = %s", 'archives' )
);
}
}
}
register_uninstall_hook( __FILE__ , array( 'haste_ac\ArchivesContent', 'on_uninstall') );
add_action( 'plugins_loaded', array( 'haste_ac\ArchivesContent', 'init' ) );
?>