forked from publishpress/publishpress-series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orgSeries-admin.php
412 lines (354 loc) · 17.9 KB
/
orgSeries-admin.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
<?php
/**
* This file contains all the code that hooks orgSeries into the various pages of the WordPress administration. Some hooks will reference other files (series management, and series options).
*
* @package Publishpress Series WordPress Plugin
* @since 2.2
*/
global $pagenow, $orgseries;
/**
* All the add_action and apply_filter hooks for this file go here
*/
//add action for admin-series.css and thickbox
add_action('admin_enqueue_scripts', 'orgSeries_admin_header');
##ADMIN-Write/Edit Post Script loader
add_action('admin_print_scripts','orgSeries_admin_script');
add_action('admin_print_scripts-post.php', 'orgSeries_post_script');
add_action('admin_print_scripts-post-new.php', 'orgSeries_post_script');
add_action('admin_print_scripts-edit-tags.php', 'orgSeries_manage_script');
//add ajax for on-the-fly series adds
add_action('wp_ajax_add_series', 'admin_ajax_series');
//add_action('wp_ajax_inline-series', 'admin_inline_series_ajax');
add_action('admin_init', 'orgseries_load_custom_column_actions', 10);
add_action('admin_init', 'orgseries_load_custom_column_filters', 10);
// Load JavaScript and CSS
add_action('admin_enqueue_scripts','orgSeries_admin_assets');
//add footer credit
add_action( 'in_admin_footer', 'orgSeries_admin_footer' );
function orgseries_load_custom_column_actions() {
//support for custom post types
$posttypes = apply_filters('orgseries_posttype_support', array('post') );
foreach ( $posttypes as $posttype ) {
$action_ref = ( $posttype == 'post' ) ? 'manage_posts_custom_column' : 'manage_' . $posttype . 'posts_custom_column';
$action_ref = ( $posttype == 'page' ) ? 'manage_pages_custom_column' : $action_ref;
add_action($action_ref,'orgSeries_custom_column_action', 12, 2);
}
}
function orgseries_load_custom_column_filters() {
//support for custom post types
$posttypes = apply_filters('orgseries_posttype_support', array('post') );
foreach ( $posttypes as $posttype ) {
$filter_ref = ( $posttype == 'page' ) ? 'manage_pages_columns' : 'manage_posts_columns';
add_filter($filter_ref, 'orgSeries_custom_column_filter');
}
}
if ( $pagenow != 'upload.php' )
add_action('restrict_manage_posts', 'orgSeries_custom_manage_posts_filter');
add_action('post_relatedlinks_list', 'add_series_management_link');
add_action( 'right_now_content_table_end', 'add_series_to_right_now');
//function: Add Meta-box
add_action('add_meta_boxes', 'orgseries_add_meta_box', 9);
function orgSeries_admin_header() {
$plugin_path = PPSERIES_URL;
$csspath = $plugin_path . "orgSeries-admin.css";
wp_register_style( 'orgSeries-admin', $csspath );
wp_enqueue_style( 'orgSeries-admin' );
wp_enqueue_style( 'thickbox' );
}
//add_action filter for the manage_series page...
function orgSeries_post_script() {
wp_enqueue_script('ajaxseries');
}
function orgSeries_admin_script() {
//load in the series.js script and set localization variables.
global $pagenow;
if ( ( 'edit-tags.php' == $pagenow || 'term.php' == $pagenow ) && 'series' == $_GET['taxonomy'] ) {
orgSeries_manage_script();
}
wp_register_style('pps-admin-common', SERIES_PATH_URL . 'assets/css/pressshack-admin.css', [], ORG_SERIES_VERSION);
if (is_ppseries_admin_pages()) {
wp_enqueue_style( 'pps-admin-common' );
}
}
function orgSeries_admin_assets() {
wp_register_script( 'pps-admin-js', SERIES_PATH_URL . 'assets/js/admin.js', array( 'jquery' ), ORG_SERIES_VERSION );
wp_register_style('pps-admin-common', SERIES_PATH_URL . 'assets/css/pressshack-admin.css', [], ORG_SERIES_VERSION);
if (is_ppseries_admin_pages()) {
wp_enqueue_style( 'pps-admin-common' );
wp_enqueue_script( 'pps-admin-js' );
}
}
function orgSeries_admin_footer() {
if (is_ppseries_admin_pages()) {
?>
<div class="pressshack-admin-wrapper ppseries-footer-credit temporary">
<footer>
<div class="pp-rating">
<a href="https://wordpress.org/support/plugin/organize-series/reviews/#new-post" target="_blank" rel="noopener noreferrer">
<?php printf(__('If you like %s, please leave us a %s rating. Thank you!', 'organize-series'), '<strong>PublishPress Series</strong>', '<span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span>'
);
?> </a>
</div>
<hr>
<nav>
<ul>
<li><a href="https://publishpress.com/series/" target="_blank" rel="noopener noreferrer" title="<?php _e('About PublishPress Series', 'organize-series');?>"><?php _e('About', 'organize-series');?></a></li>
<li><a href=" https://publishpress.com/knowledge-base/start-series/" target="_blank" rel="noopener noreferrer" title="<?php _e('PublishPress Series Documentation', 'organize-series');?>"><?php _e('Documentation', 'organize-series');?></a></li>
<li><a href="https://publishpress.com/contact" target="_blank" rel="noopener noreferrer" title="<?php _e('Contact the PublishPress team', 'organize-series');?>"><?php _e('Contact', 'organize-series');?></a></li>
<li><a href="https://twitter.com/publishpresscom" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-twitter"></span></a></li>
<li><a href="https://facebook.com/publishpress" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-facebook"></span></a></li>
</ul>
</nav>
<div class="pp-pressshack-logo">
<a href="https://publishpress.com" target="_blank" rel="noopener noreferrer">
<img src="<?php echo SERIES_PATH_URL . 'assets/images/publishpress-logo.png';?>" />
</a>
</div>
</footer>
</div>
<div class="clear"></div>
<?php
}
}
function orgSeries_manage_script() {
wp_enqueue_media();
wp_enqueue_script( 'thickbox' );
wp_enqueue_script('media-upload');
wp_enqueue_script('orgseries_scripts', SERIES_PATH_URL . 'js/orgseries_scripts.js', array('jquery', 'thickbox', 'media-upload'), ORG_SERIES_VERSION, true);
}
######ON THE FLY ADD SERIES########
function admin_ajax_series() {
$response = array();
if ( !current_user_can( 'manage_series' ) )
$response['error'] = __('Sorry but you don\'t have permission to add series', 'organize-series');
if ( ! check_ajax_referer ( 'add-series-nonce', 'addnonce', false ) ) {
$response['error'] = 'Sorry but security check failed';
}
$new_nonce = wp_create_nonce('add-series-nonce');
$name = $_POST['newseries'];
$series_name = trim($name);
if ( !$series_nicename = sanitize_title($series_name) )
$response['error'] = __('The name you picked isn\'t sanitizing correctly. Try something different.', 'organize-series');
if ( empty( $response['error'] ) ) {
if ( !$series_id = series_exists( $series_name ) ) {
$ser_id = wp_create_single_series( $series_name );
$series_id = $ser_id['term_id'];
} else {
$response['error'] = __('Hmm... it looks like there is already a series with that name. Try something else', 'organize-series');
}
$series_name = esc_html(stripslashes($series_name));
}
if ( empty( $response['error'] ) ) {
$response = array(
'id' => $series_id,
'html' => "<li id='series-$series_id' class='series-added-indicator'><label for='in-series-$series_id' class='selectit'><input value='$series_id' type='radio' name='post_series' id='in-series-$series_id' checked /><input type='hidden' name='is_series_save' value='1' /> <span class='li-series-name'>$series_name</span></label><span id='new_series_id' class='hidden'>$series_id</span></li>",
'new_nonce' => $new_nonce,
'error' => false
);
}
echo json_encode($response);
exit();
}
###AJAX FOR INLINE_SERIES UPDATE###
function admin_inline_series_ajax() {
check_ajax_referer ( 'inlineeditnonce', '_inline_edit' );
$data[$seriesid] = isset($_POST['post_series']) ? $_POST['post_series'] : '';
$data[$series_part] = isset($_POST['series_part']) ? $_POST['series_part'] : '';
$data[$id] = isset($_POST['series_post_id']) ? $_POST['series_post_id'] : '';
$data[$is_series_save] = isset($_POST['is_series_save']) ? $_POST['is_series_save'] : 0;
/*$series_id = $_POST['post_series'];
$part = $_POST['series_part'];
$post_id = $_POST['series_post_id'];*/
//orgSeries_custom_column_action('series', $data[$id]);
exit;
}
/**
* get_series_list() - queries the list of series from the database and creates an array indicating what
* series is associated with the current post (if existent).
*
* The code here is mirrored from get_nested_categories in the core wp-admin\includes\template.php
* file.
*
* @uses count() - internal WordPress function
* @uses wp_get_post_series()
* @uses get_series()
* @uses get_the_series_by_ID()
* @uses apply_filters() - user can modify the array that is returned.
* @param int $default - id for the default series. Currently there is not an option for users to set a
* default series. This may change in the future.
* @return array|mixed $result - series ids, series names, and checked value.
*/
function get_series_list( $default = 0 ) {
global $post, $postdata, $checked_series;
$post_ID = isset( $post ) ? $post->ID : $postdata->ID;
if ( empty( $checked_series ) ) {
if ( $post_ID ) {
$checked_series = wp_get_post_series( $post_ID );
$checked_series = is_array($checked_series) ? $checked_series : array();
if ( count( $checked_series ) == 0 ) {
$checked_series[] = $default;
}
} else {
$checked_series[] = $default;
}
$series = get_series( "hide_empty=0&fields=ids" );
$result = array ();
if ( is_array( $series ) ) {
foreach ( $series as $serial ) {
$result[$serial]['series_ID'] = $serial;
$result[$serial]['checked'] = in_array( $serial, $checked_series );
$result[$serial]['ser_name'] = get_the_series_by_ID( $serial );
}
}
$unsorted_result = $result;
usort( $result, '_usort_series_by_name' );
$result = apply_filters(
'get_series_list',
$result,
$unsorted_result
);
return $result;
}
}
/**
* write_series_list() - html output for the list of series in for user selection on the write/edit post screen
* Code is mirror'd from the write_nested_categories() function in the core wp-admin\includes\template.php file.
*
* @uses esc_html
* @param array|mixed $series - contains series_ID, ser_name, and checked (does post belong to this series)
*/
function write_series_list( $series ) { //copied from write_nested_categories in template.php
global $orgseries;
echo '<li id="series-0"><label for ="in-series-0" class="selectit"><input value="0" type="radio" name="post_series" id="in-series-0" checked="checked" /><span class="li-series-name">' . __('Not part of a series', 'organize-series') . '</span></label></li>';
foreach ( $series as $serial ) {
echo '<li id="series-'. $serial['series_ID'].'"><label for="in-series-'. $serial['series_ID']. '" class="selectit"><input value="' . $serial['series_ID'] . '" type="radio" name="post_series" id="in-series-' . $serial['series_ID'] . '"' . ($serial['checked'] ? ' checked="checked"' : '' ) . '/> <span class="li-series-name">' . esc_html( $serial['ser_name'] ) . "</span></label></li>";
}
}
/**
* get_series_to_select() - wrapper function to output the list of series on the write/edit post page.
* Calls up the list of existing series in the WordPress db and displays the series that the post belongs to already selected.
*
*@uses write_series_list()
*@uses get_series_list()
*@param int $default = id for the default series. Currently there is not an option for users to set a default series id. This may change in the future.
*/
function get_series_to_select( $default = 0 ) {
write_series_list( get_series_list( $default) );
}
//series-edit box for wp 2.8 forward
function series_edit_meta_box() {
global $post, $postdata, $content, $orgseries;
$id = isset($post) ? $post->ID : $postdata->ID;
$ser_id = wp_get_post_series( $id );
?>
<div class="series-metadiv">
<div class="tabs-panel">
<p id="jaxseries"></p>
<span id="series-ajax-response"></span>
<ul id="serieschecklist" class="list:series serieschecklist categorychecklist form-no-clear">
<?php get_series_to_select(); ?>
</ul>
<div class="series-part-wrap">
<span id="seriespart"><strong> <?php _e('Series Part:', 'organize-series'); ?> </strong><input type="text" name="series_part[<?php echo isset($ser_id[0]) ? $ser_id[0] : 0; ?>]" id="series_part" size="5" value="<?php echo get_post_meta($id, SERIES_PART_KEY, true); ?>" /></span>
<p id="part-description" class="howto"><?php _e('If you leave this blank, this post will automatically be added to the end of the series.', 'organize-series'); ?></p>
</div>
<strong> <?php _e('Post title in widget:', 'organize-series'); ?></strong>
<p id="part-description" class="howto">
<?php _e('A short title of this post that will be used in the Series widget. Leave blank to use the full title.', 'organize-series'); ?>
</p>
<input type="text" name="serie_post_shorttitle[<?php echo isset($ser_id[0]) ? $ser_id[0] : 0; ?>]" id="serie_post_shorttitle" size="30" value="<?php echo get_post_meta($id, SPOST_SHORTTITLE_KEY, true); ?>"/>
<input type="hidden" name="is_series_save" value="1" />
</div>
</div>
<?php
}
function orgseries_add_meta_box() {
global $orgseries;
$posttypes = apply_filters('orgseries_posttype_support', array('post') );
foreach ( $posttypes as $posttype ) {
add_meta_box('seriesdiv', __('Series', 'organize-series'), 'series_edit_meta_box', $posttype, 'side');
remove_meta_box('tagsdiv-series', $posttype, 'side'); //removes series meta box added by WordPress Taxonomy api.
}
}
/* ADDING SERIES INFO TO EDIT POST PAGE */
function orgSeries_custom_column_filter($defaults) {
global $orgseries;
$post_types = apply_filters( 'orgseries_posttype_support', array('post') );
if ( isset($_REQUEST['post_type']) && !in_array($_REQUEST['post_type'], $post_types) )
return $defaults; //get out we only want this showing up on post post types for now.*/
$defaults['series'] = __('Series', 'organize-series');
return $defaults;
}
function orgSeries_custom_column_action($column_name, $id) {
global $orgseries;
$seriesid = null;
$series_part = null;
$series_name = null;
$column_content = null;
$post_types = apply_filters('orgseries_posttype_support', array('post'));
if ($column_name == 'series') {
$column_content .= '<div class="series_column">';
$column_content .= '<input type="hidden" name="is_series_save" value="1" />';
if ( $series = get_the_series($id, false) ) {
$seriesid = $series[0]->term_id;
$series_name = $series[0]->name;
$series_link = get_series_link($series_name);
$series_part = get_post_meta($id, SERIES_PART_KEY, TRUE);
$count = $series[0]->count;
$draft_posts = get_posts( array(
'post_type' => $post_types,
'post_status' => array('draft', 'future', 'pending'),
'taxonomy' => 'series',
'term' => $series_name
) );
$count_draft_posts = is_array($draft_posts) ? count($draft_posts) : 0;
$drafts_included = '';
if($count_draft_posts != 0){
$all_serie_posts = $count_draft_posts+$count;
$drafts_included = "($all_serie_posts)";
}
$post_status = get_post_status($id);
if ($series && in_array( $post_status, array( 'publish', 'private' ) ) ) {
$column_content .= sprintf(__('Part %1$s of %2$s%6$s in the series <br/><a href="%3$s" title="%4$s">%5$s</a>', 'organize-series'), $series_part, $count, $series_link, $series_name, $series_name, $drafts_included);
$column_content .= '<div class="hidden" id="inline_series_' . $id . '"><div class="series_inline_edit">'.$seriesid.'</div><div class="series_inline_part">'.$series_part.'</div><div class="series_post_id">'.$id.'</div><div class="series_inline_name">'.$series_name.'</div></div>';
} else {
$column_content .= sprintf(__('<a href="%1$s" title="%2$s">%3$s</a> - (currently set as Part %4$s)', 'organize-series'), $series_link, $series_name, $series_name, $series_part);
$column_content .= '<div class="hidden" id="inline_series_' . $id . '"><div class="series_inline_edit">'.$seriesid.'</div><div class="series_inline_part">'.$series_part.'</div><div class="series_post_id">'.$id.'</div><div class="series_inline_name">'.$series_name.'</div></div>';
}
} else {
$column_content .= '<div class="hidden" id="inline_series_' . $id . '"><div class="series_inline_edit">'.$seriesid.'</div><div class="series_inline_part">'.$series_part.'</div><div class="series_post_id">'.$id.'</div><div class="series_inline_name">'.$series_name.'</div></div>';
$column_content .= '<em>'.__('No Series', 'organize-series').'</em>';
}
$column_content .= '</div>';
echo $column_content;
}
}
function orgSeries_custom_manage_posts_filter() {
global $orgseries;
$series_name = '';
if (isset($_GET[SERIES_QUERYVAR])) $series_name = $_GET[SERIES_QUERYVAR];
wp_dropdown_series('show_option_all='.__('View all series', 'organize-series').'&hide_empty=0&show_count=0&selected='.$series_name);
}
function add_series_management_link() {
global $orgseries;
$link = get_option('siteurl') . '/wp-admin/edit.php?page=' . SERIES_DIR . '/orgSeries-manage.php';
?>
<li><a href="<?php echo $link; ?>"><?php _e('Manage All Series', 'organize-series'); ?></a></li>
<?php
}
function add_series_to_right_now() {
global $orgseries;
$num_series = wp_count_terms('series');
$num = number_format_i18n( $num_series );
$text = _n( 'Series', 'Series', $num_series, 'organize-series' );
$manage_link = get_option('siteurl') . '/wp-admin/edit-tags.php?taxonomy=' . SERIES_QUERYVAR;
if ( current_user_can( 'manage_series' ) ) {
$series_num = "<a href='$manage_link'>$num</a>";
$series_text = "<a href='$manage_link'>$text</a>";
}
echo '<tr>';
echo '<td class="first b b-tags">'.$series_num.'</td>';
echo '<td class="t tags">' . $series_text . '</td>';
echo '<td></td><td></td></tr>';
}
?>