forked from nett55/publishing-checklist
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpublishing-checklist.php
239 lines (200 loc) · 6.58 KB
/
publishing-checklist.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
<?php
/*
Plugin Name: Publishing Checklist
Version: 0.2.0-alpha
Description: Pre-flight your posts.
Author: Fusion Engineering
Author URI: http://fusion.net/
Plugin URI: https://github.com/fusioneng/publishing-checklist
Text Domain: publishing-checklist
Domain Path: /languages
*/
define( 'PUBLISHING_CHECKLIST_VERSION', '0.2.0-alpha' );
class Publishing_Checklist {
private static $instance;
private $tasks = array();
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Publishing_Checklist;
self::$instance->setup_actions();
do_action( 'publishing_checklist_init' );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/inc/class-cli-command.php';
}
}
return self::$instance;
}
/**
* Set up actions for the plugin
*/
private function setup_actions() {
add_action( 'publishing_checklist_enqueue_scripts', array( $this, 'action_publishing_checklist_enqueue_scripts' ) );
add_action( 'post_submitbox_misc_actions', array( $this, 'action_post_submitbox_misc_actions_render_checklist' ) );
// Must be called before list table is rendered, but after all tasks have been registered.
add_action( 'admin_head', array( $this, 'register_custom_column_handlers' ) );
add_action( 'wp_ajax_inline-save', array( $this, 'register_custom_column_handlers' ), 1 );
}
/**
* Register the Publishing Checklist column for list tables.
*
* Registers the Publishing Checklist column, and the custom column
* rendering function, for all post types which have checklist tasks
* defined.
*
*/
public function register_custom_column_handlers() {
// Find all post types with tasks associated.
$post_types = array();
foreach ( $this->tasks as $task ) {
$post_types = array_unique( array_merge( $post_types, $task['post_type'] ) );
}
// Add post list table columns
foreach ( $post_types as $post_type ) {
add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'action_manage_posts_custom_column' ), 10, 2 );
add_filter( "manage_{$post_type}_posts_columns", array( $this, 'filter_manage_posts_columns' ), 99 );
}
}
/**
* Register a validation task for our publishing checklist
*
* @param string $id Unique identifier for the task (can be arbitrary, as long as it doesn't conflict with others)
* @param string $label Human-friendly label for the task
* @param mixed $callback Callable function or method to indicate whether or not the task has been complete
* @param string $explanation A longer description as to what needs to be accomplished for the task
*/
public function register_task( $id, $args = array() ) {
$defaults = array(
'label' => $id,
'callback' => '__return_false',
'explanation' => '',
'post_type' => array(),
);
$args = array_merge( $defaults, $args );
$this->tasks[ $id ] = $args;
}
/**
* Render the checklist in the publish submit box
*/
public function action_post_submitbox_misc_actions_render_checklist() {
$post_id = get_the_ID();
$tasks_completed = $this->evaluate_checklist( $post_id );
if ( $tasks_completed ) {
do_action( 'publishing_checklist_enqueue_scripts' );
echo $this->get_template_part( 'post-submitbox-misc-actions',
array(
'tasks' => $tasks_completed['tasks'],
'completed_tasks' => $tasks_completed['completed'],
)
);
}
}
/**
* Evaluate tasks for a post
*
* @param string $post_id WordPress post ID
*
*/
public function evaluate_checklist( $post_id ) {
if ( empty( $post_id ) ) {
return false;
}
if ( empty( $this->tasks ) ) {
return false;
}
$tasks = array();
$completed_tasks = array();
// Get all tasks to be run for this post_id.
foreach ( $this->tasks as $task_id => $task ) {
if ( ! is_callable( $task['callback'] ) ) {
continue;
}
if ( empty( $task['post_type'] ) || ! in_array( get_post_type( $post_id ), $task['post_type'], true ) ) {
continue;
}
$tasks[ $task_id ] = $task;
}
if ( empty( $tasks ) ) {
return false;
}
foreach ( $tasks as $task_id => $task ) {
if ( call_user_func_array( $task['callback'], array( $post_id, $task_id ) ) ) {
$completed_tasks[] = $task_id;
}
}
$checklist_data = array(
'tasks' => $tasks,
'completed' => $completed_tasks,
);
return $checklist_data;
}
/**
* Load our scripts and styles
*/
public function action_publishing_checklist_enqueue_scripts() {
wp_enqueue_style( 'publishing-checklist', plugins_url( 'assets/css/publishing-checklist.css', __FILE__ ), false, PUBLISHING_CHECKLIST_VERSION );
wp_enqueue_script( 'publishing-checklist', plugins_url( 'assets/js/src/publishing-checklist.js', __FILE__ ), array( 'jquery' ), PUBLISHING_CHECKLIST_VERSION );
}
/**
* Get a rendered template part
*
* @param string $template
* @param array $vars
* @return string
*/
private function get_template_part( $template, $vars = array() ) {
$full_path = dirname( __FILE__ ) . '/templates/' . sanitize_file_name( $template ) . '.php';
if ( ! file_exists( $full_path ) ) {
return '';
}
ob_start();
// @codingStandardsIgnoreStart
if ( ! empty( $vars ) ) {
extract( $vars );
}
// @codingStandardsIgnoreEnd
include $full_path;
return ob_get_clean();
}
/**
* Customize columns on the "Manage Posts" views
*/
public function filter_manage_posts_columns( $columns ) {
global $post_type;
$current_post_type = empty( get_post_type() ) ? $post_type : get_post_type();
foreach ( $this->tasks as $task_id => $task ) {
if ( ! is_callable( $task['callback'] ) ) {
unset( $this->tasks[ $task_id ] );
}
if ( ! empty( $task['post_type'] ) && ! in_array( $current_post_type, $task['post_type'], true ) ) {
unset( $this->tasks[ $task_id ] );
}
}
if ( empty( $this->tasks ) ) {
return $columns;
}
$columns['publishing_checklist'] = esc_html__( 'Publishing Checklist', 'publishing-checklist' );
do_action( 'publishing_checklist_enqueue_scripts' );
return $columns;
}
/**
* Handle the output for a custom column
*/
public function action_manage_posts_custom_column( $column_name, $post_id ) {
if ( 'publishing_checklist' === $column_name ) {
$tasks_completed = $this->evaluate_checklist( $post_id );
echo $this->get_template_part( 'column-checklist', array(
'tasks' => $tasks_completed['tasks'],
'completed_tasks' => $tasks_completed['completed'],
) );
}
}
}
/**
* Load the plugin
*/
// @codingStandardsIgnoreStart
function Publishing_Checklist() {
// @codingStandardsIgnoreEnd
return Publishing_Checklist::get_instance();
}
add_action( 'init', 'Publishing_Checklist' );