/
practical-publishing-actions.php
169 lines (132 loc) · 3.97 KB
/
practical-publishing-actions.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
<?php
/**
* Practical Publishing Actions main plugin and class file.
*
* @package Practical_Publishing_Actions
*/
/**
* Plugin Name: Practical Publishing Actions
* Plugin URI: https://github.com/lancehmd/practical-publishing-actions
* Description: Add a new post or return to the posts list after creating or updating one.
* Version: 1.0.0
* Author: Lance Hammond
* Author URI: https://github.com/lancehmd
* Text Domain: practical-publishing-actions
* Domain Path: /languages/
* License: MIT
* Tags: practical, publish, publishing, actions, add new, go back, save and add new, save and go back
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Practical Publishing Actions main class.
*
* @since 1.0.0
*/
final class Practical_Publishing_Actions {
/**
* Initialize our plugin hooks.
*
* @return void
*
* @since 1.0.0
*/
public function init() {
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_stylesheet' ) );
add_action( 'post_submitbox_start', array( $this, 'render_publishing_actions' ), 99 );
add_filter( 'redirect_post_location', array( $this, 'redirect_after_publish' ), 10, 2 );
}
/**
* Register our admin stylesheet.
*
* @return void
*
* @since 1.0.0
*/
public function register_admin_stylesheet() {
wp_register_style( 'practical-publishing-actions', plugin_dir_url( __FILE__ ) . 'practical-publishing-actions.css', array(), '1.0.0' );
}
/**
* Render our button with the other publish actions.
*
* @return void
*
* @since 1.0.0
*/
public function render_publishing_actions() {
global $pagenow;
// Enqueue our stylesheet.
wp_enqueue_style( 'practical-publishing-actions' );
// Button title for publishing.
$add_new_title = __( 'Publish and Add New', 'practical-publishing-actions' );
$go_back_title = __( 'Publish and Go Back', 'practical-publishing-actions' );
// If we are updating an existing post, appropriate the button title.
if ( 'post.php' === $pagenow ) {
$add_new_title = __( 'Update and Add New', 'practical-publishing-actions' );
$go_back_title = __( 'Update and Go Back', 'practical-publishing-actions' );
}
?>
<div class="ppa-actions">
<?php wp_nonce_field( 'ppa-actions', '_ppanonce' ); ?>
<button class="button button-small" name="ppa-add-new">
<?php echo esc_html( $add_new_title ); ?>
</button>
<button class="button button-small" name="ppa-go-back">
<?php echo esc_html( $go_back_title ); ?>
</button>
</div>
<?php
}
/**
* Redirect to the add new post page for the current post type.
*
* @param string $location Location to redirect to.
* @param integer $post_id Current post ID.
* @return string The new location to redirect to.
*
* @since 1.0.0
*/
public function redirect_after_publish( $location, $post_id ) {
// Verify our nonce.
if ( ! check_admin_referer( 'ppa-actions', '_ppanonce' ) ) {
return;
}
// If either button was pressed, get URL variables ready.
if ( isset( $_POST['ppa-add-new'] ) || isset( $_POST['ppa-go-back'] ) ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'publish',
) );
$post_type = get_post_type();
$query_args = array( 'post_type' => $post_type );
}
// Add new after save/update.
if ( isset( $_POST['ppa-add-new'] ) ) {
$admin_url = admin_url( 'post-new.php' );
$location = add_query_arg( $query_args, $admin_url );
}
// Return to post type list after save/update.
if ( isset( $_POST['ppa-go-back'] ) ) {
$admin_url = admin_url( 'edit.php' );
$location = add_query_arg( $query_args, $admin_url );
}
return $location;
}
}
/**
* Start the plugin.
*
* @since 1.0.0
*/
function practical_publishing_actions() {
$practical_publishing_actions = new Practical_Publishing_Actions();
$practical_publishing_actions->init();
}
/**
* Hook the plugin bootstrap function.
*/
if ( is_admin() ) {
add_action( 'plugins_loaded', 'practical_publishing_actions' );
}