PluginProbe
Edit Flow / 0.8
Edit Flow v0.8
0.11.1 0.11.0 0.7.2 0.7.3 0.7.4 0.7.5 0.7.6 0.8 0.8.1 0.8.2 0.9 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 trunk 0.1.5 0.10.0 0.10.1 0.10.2 All 44 releases
edit-flow / edit_flow.php

edit_flow.php in Edit Flow 0.8, at edit_flow.php

348 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Edit Flow
4 Plugin URI: http://editflow.org/
5 Description: Remixing the WordPress admin for better editorial workflow options.
6 Author: Daniel Bachhuber, Scott Bressler, Mohammad Jangda, Automattic, and others
7 Version: 0.8
8 Author URI: http://editflow.org/
9
10 Copyright 2009-2013 Mohammad Jangda, Daniel Bachhuber, et al.
11
12 GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28 */
29
30 // Define contants
31 define( 'EDIT_FLOW_VERSION' , '0.8' );
32 define( 'EDIT_FLOW_ROOT' , dirname(__FILE__) );
33 define( 'EDIT_FLOW_FILE_PATH' , EDIT_FLOW_ROOT . '/' . basename(__FILE__) );
34 define( 'EDIT_FLOW_URL' , plugins_url( '/', __FILE__ ) );
35 define( 'EDIT_FLOW_SETTINGS_PAGE' , add_query_arg( 'page', 'ef-settings', get_admin_url( null, 'admin.php' ) ) );
36
37 // Core class
38 class edit_flow {
39
40 // Unique identified added as a prefix to all options
41 var $options_group = 'edit_flow_';
42 var $options_group_name = 'edit_flow_options';
43
44 /**
45 * @var EditFlow The one true EditFlow
46 */
47 private static $instance;
48
49 /**
50 * Main EditFlow Instance
51 *
52 * Insures that only one instance of EditFlow exists in memory at any one
53 * time. Also prevents needing to define globals all over the place.
54 *
55 * @since EditFlow 0.7.4
56 * @staticvar array $instance
57 * @uses EditFlow::setup_globals() Setup the globals needed
58 * @uses EditFlow::includes() Include the required files
59 * @uses EditFlow::setup_actions() Setup the hooks and actions
60 * @see EditFlow()
61 * @return The one true EditFlow
62 */
63 public static function instance() {
64 if ( ! isset( self::$instance ) ) {
65 self::$instance = new edit_flow;
66 self::$instance->setup_globals();
67 self::$instance->setup_actions();
68 // Backwards compat for when we promoted use of the $edit_flow global
69 global $edit_flow;
70 $edit_flow = self::$instance;
71 }
72 return self::$instance;
73 }
74
75 private function __construct() {
76 /** Do nothing **/
77 }
78
79 private function setup_globals() {
80
81 $this->modules = new stdClass();
82
83 }
84
85 /**
86 * Include the common resources to Edit Flow and dynamically load the modules
87 */
88 private function load_modules() {
89
90 // We use the WP_List_Table API for some of the table gen
91 if ( !class_exists( 'WP_List_Table' ) )
92 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
93
94 // Edit Flow base module
95 require_once( EDIT_FLOW_ROOT . '/common/php/class-module.php' );
96
97 // Scan the modules directory and include any modules that exist there
98 $module_dirs = scandir( EDIT_FLOW_ROOT . '/modules/' );
99 $class_names = array();
100 foreach( $module_dirs as $module_dir ) {
101 if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" ) ) {
102 include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" );
103 // Prepare the class name because it should be standardized
104 $tmp = explode( '-', $module_dir );
105 $class_name = '';
106 $slug_name = '';
107 foreach( $tmp as $word ) {
108 $class_name .= ucfirst( $word ) . '_';
109 $slug_name .= $word . '_';
110 }
111 $slug_name = rtrim( $slug_name, '_' );
112 $class_names[$slug_name] = 'EF_' . rtrim( $class_name, '_' );
113 }
114 }
115
116 // Instantiate EF_Module as $helpers for back compat and so we can
117 // use it in this class
118 $this->helpers = new EF_Module();
119
120 // Other utils
121 require_once( EDIT_FLOW_ROOT . '/common/php/util.php' );
122
123 // Instantiate all of our classes onto the Edit Flow object
124 // but make sure they exist too
125 foreach( $class_names as $slug => $class_name ) {
126 if ( class_exists( $class_name ) ) {
127 $this->$slug = new $class_name();
128 }
129 }
130
131 // Supplementary plugins can hook into this, include their own modules
132 // and add them to the $edit_flow object
133 do_action( 'ef_modules_loaded' );
134
135 }
136
137 /**
138 * Setup the default hooks and actions
139 *
140 * @since EditFlow 0.7.4
141 * @access private
142 * @uses add_action() To add various actions
143 */
144 private function setup_actions() {
145 add_action( 'init', array( $this, 'action_init' ) );
146 add_action( 'init', array( $this, 'action_init_after' ), 1000 );
147
148 add_action( 'admin_init', array( $this, 'action_admin_init' ) );
149
150 do_action_ref_array( 'editflow_after_setup_actions', array( &$this ) );
151 }
152
153 /**
154 * Inititalizes the Edit Flows!
155 * Loads options for each registered module and then initializes it if it's active
156 */
157 function action_init() {
158
159 load_plugin_textdomain( 'edit-flow', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
160
161 $this->load_modules();
162
163 // Load all of the module options
164 $this->load_module_options();
165
166 // Load all of the modules that are enabled.
167 // Modules won't have an options value if they aren't enabled
168 foreach ( $this->modules as $mod_name => $mod_data )
169 if ( isset( $mod_data->options->enabled ) && $mod_data->options->enabled == 'on' )
170 $this->$mod_name->init();
171
172 do_action( 'ef_init' );
173 }
174
175 /**
176 * Initialize the plugin for the admin
177 */
178 function action_admin_init() {
179
180 // Upgrade if need be but don't run the upgrade if the plugin has never been used
181 $previous_version = get_option( $this->options_group . 'version' );
182 if ( $previous_version && version_compare( $previous_version, EDIT_FLOW_VERSION, '<' ) ) {
183 foreach ( $this->modules as $mod_name => $mod_data ) {
184 if ( method_exists( $this->$mod_name, 'upgrade' ) )
185 $this->$mod_name->upgrade( $previous_version );
186 }
187 update_option( $this->options_group . 'version', EDIT_FLOW_VERSION );
188 } else if ( !$previous_version ) {
189 update_option( $this->options_group . 'version', EDIT_FLOW_VERSION );
190 }
191
192 // For each module that's been loaded, auto-load data if it's never been run before
193 foreach ( $this->modules as $mod_name => $mod_data ) {
194 // If the module has never been loaded before, run the install method if there is one
195 if ( !isset( $mod_data->options->loaded_once ) || !$mod_data->options->loaded_once ) {
196 if ( method_exists( $this->$mod_name, 'install' ) )
197 $this->$mod_name->install();
198 $this->update_module_option( $mod_name, 'loaded_once', true );
199 }
200 }
201
202 $this->register_scripts_and_styles();
203
204 }
205
206 /**
207 * Register a new module with Edit Flow
208 */
209 public function register_module( $name, $args = array() ) {
210
211 // A title and name is required for every module
212 if ( !isset( $args['title'], $name ) )
213 return false;
214
215 $defaults = array(
216 'title' => '',
217 'short_description' => '',
218 'extended_description' => '',
219 'img_url' => false,
220 'slug' => '',
221 'post_type_support' => '',
222 'default_options' => array(),
223 'options' => false,
224 'configure_page_cb' => false,
225 'configure_link_text' => __( 'Configure', 'edit-flow' ),
226 // These messages are applied to modules and can be overridden if custom messages are needed
227 'messages' => array(
228 'settings-updated' => __( 'Settings updated.', 'edit-flow' ),
229 'form-error' => __( 'Please correct your form errors below and try again.', 'edit-flow' ),
230 'nonce-failed' => __( 'Cheatin&#8217; uh?', 'edit-flow' ),
231 'invalid-permissions' => __( 'You do not have necessary permissions to complete this action.', 'edit-flow' ),
232 'missing-post' => __( 'Post does not exist', 'edit-flow' ),
233 ),
234 'autoload' => false, // autoloading a module will remove the ability to enable or disable it
235 );
236 if ( isset( $args['messages'] ) )
237 $args['messages'] = array_merge( (array)$args['messages'], $defaults['messages'] );
238 $args = array_merge( $defaults, $args );
239 $args['name'] = $name;
240 $args['options_group_name'] = $this->options_group . $name . '_options';
241 if ( !isset( $args['settings_slug'] ) )
242 $args['settings_slug'] = 'ef-' . $args['slug'] . '-settings';
243 if ( empty( $args['post_type_support'] ) )
244 $args['post_type_support'] = 'ef_' . $name;
245 // If there's a Help Screen registered for the module, make sure we
246 // auto-load it
247 if ( !empty( $args['settings_help_tab'] ) )
248 add_action( 'load-edit-flow_page_' . $args['settings_slug'], array( &$this->$name, 'action_settings_help_menu' ) );
249
250 $this->modules->$name = (object) $args;
251 do_action( 'ef_module_registered', $name );
252 return $this->modules->$name;
253 }
254
255 /**
256 * Load all of the module options from the database
257 * If a given option isn't yet set, then set it to the module's default (upgrades, etc.)
258 */
259 function load_module_options() {
260
261 foreach ( $this->modules as $mod_name => $mod_data ) {
262
263 $this->modules->$mod_name->options = get_option( $this->options_group . $mod_name . '_options', new stdClass );
264 foreach ( $mod_data->default_options as $default_key => $default_value ) {
265 if ( !isset( $this->modules->$mod_name->options->$default_key ) )
266 $this->modules->$mod_name->options->$default_key = $default_value;
267 }
268
269 $this->$mod_name->module = $this->modules->$mod_name;
270 }
271 do_action( 'ef_module_options_loaded' );
272 }
273
274 /**
275 * Load the post type options again so we give add_post_type_support() a chance to work
276 *
277 * @see http://dev.editflow.org/2011/11/17/edit-flow-v0-7-alpha2-notes/#comment-232
278 */
279 function action_init_after() {
280 foreach ( $this->modules as $mod_name => $mod_data ) {
281
282 if ( isset( $this->modules->$mod_name->options->post_types ) )
283 $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 );
284
285 $this->$mod_name->module = $this->modules->$mod_name;
286 }
287 }
288
289 /**
290 * Get a module by one of its descriptive values
291 */
292 function get_module_by( $key, $value ) {
293 $module = false;
294 foreach ( $this->modules as $mod_name => $mod_data ) {
295
296 if ( $key == 'name' && $value == $mod_name ) {
297 $module = $this->modules->$mod_name;
298 } else {
299 foreach( $mod_data as $mod_data_key => $mod_data_value ) {
300 if ( $mod_data_key == $key && $mod_data_value == $value )
301 $module = $this->modules->$mod_name;
302 }
303 }
304 }
305 return $module;
306 }
307
308 /**
309 * Update the $edit_flow object with new value and save to the database
310 */
311 function update_module_option( $mod_name, $key, $value ) {
312 $this->modules->$mod_name->options->$key = $value;
313 $this->$mod_name->module = $this->modules->$mod_name;
314 return update_option( $this->options_group . $mod_name . '_options', $this->modules->$mod_name->options );
315 }
316
317 function update_all_module_options( $mod_name, $new_options ) {
318 if ( is_array( $new_options ) )
319 $new_options = (object)$new_options;
320 $this->modules->$mod_name->options = $new_options;
321 $this->$mod_name->module = $this->modules->$mod_name;
322 return update_option( $this->options_group . $mod_name . '_options', $this->modules->$mod_name->options );
323 }
324
325 /**
326 * Registers commonly used scripts + styles for easy enqueueing
327 */
328 function register_scripts_and_styles() {
329 wp_enqueue_style( 'ef-admin-css', EDIT_FLOW_URL . 'common/css/edit-flow-admin.css', false, EDIT_FLOW_VERSION, 'all' );
330
331 wp_register_script( 'jquery-listfilterizer', EDIT_FLOW_URL . 'common/js/jquery.listfilterizer.js', array( 'jquery' ), EDIT_FLOW_VERSION, true );
332 wp_register_style( 'jquery-listfilterizer', EDIT_FLOW_URL . 'common/css/jquery.listfilterizer.css', false, EDIT_FLOW_VERSION, 'all' );
333
334 wp_register_script( 'jquery-quicksearch', EDIT_FLOW_URL . 'common/js/jquery.quicksearch.js', array( 'jquery' ), EDIT_FLOW_VERSION, true );
335
336 // @compat 3.3
337 // Register jQuery datepicker plugin if it doesn't already exist. Datepicker plugin was added in WordPress 3.3
338 global $wp_scripts;
339 if ( !isset( $wp_scripts->registered['jquery-ui-datepicker'] ) )
340 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 );
341 }
342
343 }
344
345 function EditFlow() {
346 return edit_flow::instance();
347 }
348 add_action( 'plugins_loaded', 'EditFlow' );