-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagazine-issues.php
281 lines (245 loc) · 10.3 KB
/
magazine-issues.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
<?php
/*
Plugin Name: Magazine Issues
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: Use this plugin to manage and create Magazine Issues.
It will create a new Issue taxonomy and a new Page for the issue,
list Posts under that Issue on the Issue page,
and change the "Current Issue" link to point to this new Page.
Version: 0.1.0
Author: Per Nilsson
Author URI: http://sproutlab.com
License: GPL2
*/
/* Copyright 2013 Per Nilsson (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
TODO
- Make options for:
- current-issue nav menu slug;
- issue slug & issue-cover slug
*/
function dump($what) {
print('<pre>');
print_r($what);
print('</pre>');
}
if (!class_exists('MagazineIssues')) {
Class MagazineIssues {
var $pluginOptions = array(
'adminPageTitle' => 'Add Magazine Issue',
'adminMenuTitle' => 'Issues',
'adminCapability' => 'publish_pages',
'adminMenuSlug' => 'add_magazine_issue',
'adminMenuIconUrl' => null,
'adminMenuPosition' => 6, // After 'Posts'
'taxonomySlug' => 'issue',
'taxonomy' => ''
);
/* Constructor. */
function MagazineIssues() {
// nothing here
}
/* This is run on plugin activation. */
function activate() {
// nothing yet
}
/* Registers a menu item in the WP admin interface. */
function addAdminMenuItem() {
add_menu_page(
$this->pluginOptions['adminPageTitle'],
$this->pluginOptions['adminMenuTitle'],
$this->pluginOptions['adminCapability'],
$this->pluginOptions['adminMenuSlug'],
array($this, 'renderAdminPage'),
$this->pluginOptions['adminMenuIconUrl'],
$this->pluginOptions['adminMenuPosition']
);
}
/* Validates the data POSTed from the Magazine Issue admin interface */
function validateNewIssuePostData($postData) {
$fields = array('magazineIssueTerm', 'magazineIssueTitle');
$validPost = true;
foreach($fields as $field) {
if (!isset($postData[$field]) || strlen(trim($postData[$field])) === 0) {
$validPost = false;
break;
}
}
return $validPost;
}
/* Registers a custom 'issue' taxonomy. */
function registerIssueTaxonomy() {
$taxonomyLabels = array(
'name' => _x('Issues', 'taxonomy general name'),
'singular_name' => _x('Issue', 'taxonomy singular name'),
'search_items' => __('Search Issues'),
'popular_items' => __('Popular Issues'),
'all_items' => __('All Issues'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Edit Issue'),
'update_item' => __('Update Issue'),
'add_new_item' => __('Add New Issue'),
'new_item_name' => __('New Issue Name'),
'separate_items_with_commas' => __('Separate issues with commas'),
'add_or_remove_items' => __('Add or remove issues'),
'choose_from_most_used' => __('Choose from the most commonly used issues'),
'menu_name' => __('Issues'),
);
$args = array(
'hierarchical' => false,
'label' => 'Issues',
'labels' => $taxonomyLabels,
'show_ui' => true,
'show_admin_column' => true, // TODO
'update_count_callback' => '_update_post_term_count',
'query_var' => true, // TODO
'rewrite' => array(
'slug' => 'issue',
'with_front' => false,
'hierarchical' => false
),
);
register_taxonomy('issue', array('post'), $args);
}
/* Registers a custom 'Issue Cover' post type */
function registerIssueCoverPostType() {
$issueCoverLabels = array(
'name' => 'Issue Covers',
'singular_name' => 'Issue Cover',
'add_new' => 'Add New',
'add_new_item' => 'Add New Issue Cover',
'edit_item' => 'Edit Issue Cover',
'new_item' => 'New Issue Cover',
'all_items' => 'All Issue Covers',
'view_item' => 'View Issue Cover',
'search_items' => 'Search Issue Covers',
'not_found' => 'No issue covers found',
'not_found_in_trash' => 'No issue covers found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Issue Covers'
);
$args = array(
'labels' => $issueCoverLabels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'issue-cover' ),
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
'taxonomies' => array('issue')
);
register_post_type('issue_cover', $args);
}
/* Renders the admin page available via the admin menu item. */
function renderAdminPage() {
if (sizeof($_POST) > 0) {
// The user has POSTed the form!
$result = $this->processPostback($_POST);
if (is_wp_error($result)) {
print('<span class="error">Error: ' . $error->get_error_message() . '</span>');
} else {
print("<span class=\"success\">{$result}</span>");
}
}
// Render the form
include_once('php/magazine-issues-admin.html.php');
}
function detectAdminPostbackType($postData) {
if (isset($postData['magazineIssueCurrent'])) {
return 'change_current_issue';
}
return 'new_issue';
}
function handleNewIssuePostback($postData) {
if (!$this->validateNewIssuePostData($postData)) {
return new WP_Error('invalid_post_data', "Please enter both a title and a term name for the issue.");
}
$post;
$postId;
$postTitle = trim($postData['magazineIssueTitle']);
$term;
$termId;
$termName = trim($postData['magazineIssueTerm']);
if (term_exists($termName, 'issue')) {
return new WP_Error('term_exists', "The issue \"{$termName}\" already exists. Please choose a unique name.");
}
$post = array(
'post_title' => $postTitle,
'post_type' => 'issue_cover'
);
$postId = wp_insert_post($post);
$result = wp_set_object_terms($postId, $termName, 'issue', false);
if (is_wp_error($result)) {
return $result;
}
return "Created new Issue Cover: $postTitle";
}
function handleChangeCurrentIssuePostback($postData) {
$postID = $postData['magazineIssueCurrent'];
self::setCurrentIssuePostID($postID);
return "Changed Current Issue.";
}
function processPostback($postData) {
$type = $this->detectAdminPostbackType($postData);
if ($type == 'new_issue') {
return $this->handleNewIssuePostback($postData);
} else if ($type == 'change_current_issue') {
return $this->handleChangeCurrentIssuePostback($postData);
}
}
private static function getCurrentIssueNavItemPostID() {
$postArgs = array(
'post_type' => 'nav_menu_item',
'name' => 'current-issue',
'numberposts' => 1
);
$posts = get_posts($postArgs);
return $posts[0]->ID;
}
public static function getCurrentIssueTermID() {
$currentIssueID = get_metadata('post',
self::getCurrentIssueNavItemPostID(),
'_menu_item_object_id');
return $currentIssueID[0];
}
private static function setCurrentIssuePostID($postID) {
update_metadata('post',
self::getCurrentIssueNavItemPostID(),
'_menu_item_object_id',
$postID,
self::getCurrentIssueTermID());
}
}
}
if (class_exists('MagazineIssues')) {
$magazineIssuesPlugin = new MagazineIssues();
}
// Actions
if (isset($magazineIssuesPlugin)) {
// To make rewrite rules work
add_action('admin_init', 'flush_rewrite_rules');
// Activation of plugin (once)
add_action('activate_magazine-issues/magazine-issues.php', array(&$magazineIssuesPlugin, 'activate'));
// Admin menu item
add_action('admin_menu', array(&$magazineIssuesPlugin, 'addAdminMenuItem'));
// On each loop
add_action('init', array(&$magazineIssuesPlugin, 'registerIssueTaxonomy'));
add_action('init', array(&$magazineIssuesPlugin, 'registerIssueCoverPostType'));
}