forked from Automattic/Edit-Flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_flow.php
348 lines (289 loc) · 12.2 KB
/
edit_flow.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
<?php
/*
Plugin Name: Edit Flow
Plugin URI: http://editflow.org/
Description: Remixing the WordPress admin for better editorial workflow options.
Author: Daniel Bachhuber, Scott Bressler, Mohammad Jangda, Automattic, and others
Version: 0.8.1-alpha
Author URI: http://editflow.org/
Copyright 2009-2013 Mohammad Jangda, Daniel Bachhuber, et al.
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Define contants
define( 'EDIT_FLOW_VERSION' , '0.8.1-alpha' );
define( 'EDIT_FLOW_ROOT' , dirname(__FILE__) );
define( 'EDIT_FLOW_FILE_PATH' , EDIT_FLOW_ROOT . '/' . basename(__FILE__) );
define( 'EDIT_FLOW_URL' , plugins_url( '/', __FILE__ ) );
define( 'EDIT_FLOW_SETTINGS_PAGE' , add_query_arg( 'page', 'ef-settings', get_admin_url( null, 'admin.php' ) ) );
// Core class
class edit_flow {
// Unique identified added as a prefix to all options
var $options_group = 'edit_flow_';
var $options_group_name = 'edit_flow_options';
/**
* @var EditFlow The one true EditFlow
*/
private static $instance;
/**
* Main EditFlow Instance
*
* Insures that only one instance of EditFlow exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since EditFlow 0.7.4
* @staticvar array $instance
* @uses EditFlow::setup_globals() Setup the globals needed
* @uses EditFlow::includes() Include the required files
* @uses EditFlow::setup_actions() Setup the hooks and actions
* @see EditFlow()
* @return The one true EditFlow
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new edit_flow;
self::$instance->setup_globals();
self::$instance->setup_actions();
// Backwards compat for when we promoted use of the $edit_flow global
global $edit_flow;
$edit_flow = self::$instance;
}
return self::$instance;
}
private function __construct() {
/** Do nothing **/
}
private function setup_globals() {
$this->modules = new stdClass();
}
/**
* Include the common resources to Edit Flow and dynamically load the modules
*/
private function load_modules() {
// We use the WP_List_Table API for some of the table gen
if ( !class_exists( 'WP_List_Table' ) )
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
// Edit Flow base module
require_once( EDIT_FLOW_ROOT . '/common/php/class-module.php' );
// Scan the modules directory and include any modules that exist there
$module_dirs = scandir( EDIT_FLOW_ROOT . '/modules/' );
$class_names = array();
foreach( $module_dirs as $module_dir ) {
if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" ) ) {
include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" );
// Prepare the class name because it should be standardized
$tmp = explode( '-', $module_dir );
$class_name = '';
$slug_name = '';
foreach( $tmp as $word ) {
$class_name .= ucfirst( $word ) . '_';
$slug_name .= $word . '_';
}
$slug_name = rtrim( $slug_name, '_' );
$class_names[$slug_name] = 'EF_' . rtrim( $class_name, '_' );
}
}
// Instantiate EF_Module as $helpers for back compat and so we can
// use it in this class
$this->helpers = new EF_Module();
// Other utils
require_once( EDIT_FLOW_ROOT . '/common/php/util.php' );
// Instantiate all of our classes onto the Edit Flow object
// but make sure they exist too
foreach( $class_names as $slug => $class_name ) {
if ( class_exists( $class_name ) ) {
$this->$slug = new $class_name();
}
}
// Supplementary plugins can hook into this, include their own modules
// and add them to the $edit_flow object
do_action( 'ef_modules_loaded' );
}
/**
* Setup the default hooks and actions
*
* @since EditFlow 0.7.4
* @access private
* @uses add_action() To add various actions
*/
private function setup_actions() {
add_action( 'init', array( $this, 'action_init' ) );
add_action( 'init', array( $this, 'action_init_after' ), 1000 );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
do_action_ref_array( 'editflow_after_setup_actions', array( &$this ) );
}
/**
* Inititalizes the Edit Flows!
* Loads options for each registered module and then initializes it if it's active
*/
function action_init() {
load_plugin_textdomain( 'edit-flow', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
$this->load_modules();
// Load all of the module options
$this->load_module_options();
// Load all of the modules that are enabled.
// Modules won't have an options value if they aren't enabled
foreach ( $this->modules as $mod_name => $mod_data )
if ( isset( $mod_data->options->enabled ) && $mod_data->options->enabled == 'on' )
$this->$mod_name->init();
do_action( 'ef_init' );
}
/**
* Initialize the plugin for the admin
*/
function action_admin_init() {
// Upgrade if need be but don't run the upgrade if the plugin has never been used
$previous_version = get_option( $this->options_group . 'version' );
if ( $previous_version && version_compare( $previous_version, EDIT_FLOW_VERSION, '<' ) ) {
foreach ( $this->modules as $mod_name => $mod_data ) {
if ( method_exists( $this->$mod_name, 'upgrade' ) )
$this->$mod_name->upgrade( $previous_version );
}
update_option( $this->options_group . 'version', EDIT_FLOW_VERSION );
} else if ( !$previous_version ) {
update_option( $this->options_group . 'version', EDIT_FLOW_VERSION );
}
// For each module that's been loaded, auto-load data if it's never been run before
foreach ( $this->modules as $mod_name => $mod_data ) {
// If the module has never been loaded before, run the install method if there is one
if ( !isset( $mod_data->options->loaded_once ) || !$mod_data->options->loaded_once ) {
if ( method_exists( $this->$mod_name, 'install' ) )
$this->$mod_name->install();
$this->update_module_option( $mod_name, 'loaded_once', true );
}
}
$this->register_scripts_and_styles();
}
/**
* Register a new module with Edit Flow
*/
public function register_module( $name, $args = array() ) {
// A title and name is required for every module
if ( !isset( $args['title'], $name ) )
return false;
$defaults = array(
'title' => '',
'short_description' => '',
'extended_description' => '',
'img_url' => false,
'slug' => '',
'post_type_support' => '',
'default_options' => array(),
'options' => false,
'configure_page_cb' => false,
'configure_link_text' => __( 'Configure', 'edit-flow' ),
// These messages are applied to modules and can be overridden if custom messages are needed
'messages' => array(
'settings-updated' => __( 'Settings updated.', 'edit-flow' ),
'form-error' => __( 'Please correct your form errors below and try again.', 'edit-flow' ),
'nonce-failed' => __( 'Cheatin’ uh?', 'edit-flow' ),
'invalid-permissions' => __( 'You do not have necessary permissions to complete this action.', 'edit-flow' ),
'missing-post' => __( 'Post does not exist', 'edit-flow' ),
),
'autoload' => false, // autoloading a module will remove the ability to enable or disable it
);
if ( isset( $args['messages'] ) )
$args['messages'] = array_merge( (array)$args['messages'], $defaults['messages'] );
$args = array_merge( $defaults, $args );
$args['name'] = $name;
$args['options_group_name'] = $this->options_group . $name . '_options';
if ( !isset( $args['settings_slug'] ) )
$args['settings_slug'] = 'ef-' . $args['slug'] . '-settings';
if ( empty( $args['post_type_support'] ) )
$args['post_type_support'] = 'ef_' . $name;
// If there's a Help Screen registered for the module, make sure we
// auto-load it
if ( !empty( $args['settings_help_tab'] ) )
add_action( 'load-edit-flow_page_' . $args['settings_slug'], array( &$this->$name, 'action_settings_help_menu' ) );
$this->modules->$name = (object) $args;
do_action( 'ef_module_registered', $name );
return $this->modules->$name;
}
/**
* Load all of the module options from the database
* If a given option isn't yet set, then set it to the module's default (upgrades, etc.)
*/
function load_module_options() {
foreach ( $this->modules as $mod_name => $mod_data ) {
$this->modules->$mod_name->options = get_option( $this->options_group . $mod_name . '_options', new stdClass );
foreach ( $mod_data->default_options as $default_key => $default_value ) {
if ( !isset( $this->modules->$mod_name->options->$default_key ) )
$this->modules->$mod_name->options->$default_key = $default_value;
}
$this->$mod_name->module = $this->modules->$mod_name;
}
do_action( 'ef_module_options_loaded' );
}
/**
* Load the post type options again so we give add_post_type_support() a chance to work
*
* @see http://dev.editflow.org/2011/11/17/edit-flow-v0-7-alpha2-notes/#comment-232
*/
function action_init_after() {
foreach ( $this->modules as $mod_name => $mod_data ) {
if ( isset( $this->modules->$mod_name->options->post_types ) )
$this->modules->$mod_name->options->post_types = $this->helpers->clean_post_type_options( $this->modules->$mod_name->options->post_types, $mod_data->post_type_support );
$this->$mod_name->module = $this->modules->$mod_name;
}
}
/**
* Get a module by one of its descriptive values
*/
function get_module_by( $key, $value ) {
$module = false;
foreach ( $this->modules as $mod_name => $mod_data ) {
if ( $key == 'name' && $value == $mod_name ) {
$module = $this->modules->$mod_name;
} else {
foreach( $mod_data as $mod_data_key => $mod_data_value ) {
if ( $mod_data_key == $key && $mod_data_value == $value )
$module = $this->modules->$mod_name;
}
}
}
return $module;
}
/**
* Update the $edit_flow object with new value and save to the database
*/
function update_module_option( $mod_name, $key, $value ) {
$this->modules->$mod_name->options->$key = $value;
$this->$mod_name->module = $this->modules->$mod_name;
return update_option( $this->options_group . $mod_name . '_options', $this->modules->$mod_name->options );
}
function update_all_module_options( $mod_name, $new_options ) {
if ( is_array( $new_options ) )
$new_options = (object)$new_options;
$this->modules->$mod_name->options = $new_options;
$this->$mod_name->module = $this->modules->$mod_name;
return update_option( $this->options_group . $mod_name . '_options', $this->modules->$mod_name->options );
}
/**
* Registers commonly used scripts + styles for easy enqueueing
*/
function register_scripts_and_styles() {
wp_enqueue_style( 'ef-admin-css', EDIT_FLOW_URL . 'common/css/edit-flow-admin.css', false, EDIT_FLOW_VERSION, 'all' );
wp_register_script( 'jquery-listfilterizer', EDIT_FLOW_URL . 'common/js/jquery.listfilterizer.js', array( 'jquery' ), EDIT_FLOW_VERSION, true );
wp_register_style( 'jquery-listfilterizer', EDIT_FLOW_URL . 'common/css/jquery.listfilterizer.css', false, EDIT_FLOW_VERSION, 'all' );
wp_register_script( 'jquery-quicksearch', EDIT_FLOW_URL . 'common/js/jquery.quicksearch.js', array( 'jquery' ), EDIT_FLOW_VERSION, true );
// @compat 3.3
// Register jQuery datepicker plugin if it doesn't already exist. Datepicker plugin was added in WordPress 3.3
global $wp_scripts;
if ( !isset( $wp_scripts->registered['jquery-ui-datepicker'] ) )
wp_register_script( 'jquery-ui-datepicker', EDIT_FLOW_URL . 'common/js/jquery.ui.datepicker.min.js', array( 'jquery', 'jquery-ui-core'), '1.8.16', true );
}
}
function EditFlow() {
return edit_flow::instance();
}
add_action( 'plugins_loaded', 'EditFlow' );