PluginProbe
Edit Flow / 0.9
Edit Flow v0.9
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.9, at edit_flow.php

417 lines 14.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.9
8 Author URI: http://editflow.org/
9
10 Copyright 2009-2019 Mohammad Jangda, Daniel Bachhuber, Automattic, 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 /**
31 * Print admin notice regarding having an old version of PHP.
32 *
33 * @since 0.9
34 */
35 function _ef_print_php_version_admin_notice() {
36 ?>
37 <div class="notice notice-error">
38 <p><?php esc_html_e( 'Edit Flow requires PHP 5.4+. Please contact your host to update your PHP version.', 'edit-flow' ); ?></p>
39 </div>
40 <?php
41 }
42
43 if ( version_compare( phpversion(), '5.4', '<' ) ) {
44 add_action( 'admin_notices', '_ef_print_php_version_admin_notice' );
45 return;
46 }
47
48 // Define contants
49 define( 'EDIT_FLOW_VERSION' , '0.9' );
50 define( 'EDIT_FLOW_ROOT' , dirname(__FILE__) );
51 define( 'EDIT_FLOW_FILE_PATH' , EDIT_FLOW_ROOT . '/' . basename(__FILE__) );
52 define( 'EDIT_FLOW_URL' , plugins_url( '/', __FILE__ ) );
53 define( 'EDIT_FLOW_SETTINGS_PAGE' , add_query_arg( 'page', 'ef-settings', get_admin_url( null, 'admin.php' ) ) );
54
55 // Core class
56 class edit_flow {
57
58 // Unique identified added as a prefix to all options
59 var $options_group = 'edit_flow_';
60 var $options_group_name = 'edit_flow_options';
61
62 /**
63 * @var EditFlow The one true EditFlow
64 */
65 private static $instance;
66
67 /**
68 * Main EditFlow Instance
69 *
70 * Insures that only one instance of EditFlow exists in memory at any one
71 * time. Also prevents needing to define globals all over the place.
72 *
73 * @since EditFlow 0.7.4
74 * @staticvar array $instance
75 * @uses EditFlow::setup_globals() Setup the globals needed
76 * @uses EditFlow::includes() Include the required files
77 * @uses EditFlow::setup_actions() Setup the hooks and actions
78 * @see EditFlow()
79 * @return The one true EditFlow
80 */
81 public static function instance() {
82 if ( ! isset( self::$instance ) ) {
83 self::$instance = new edit_flow;
84 self::$instance->setup_globals();
85 self::$instance->setup_actions();
86 // Backwards compat for when we promoted use of the $edit_flow global
87 global $edit_flow;
88 $edit_flow = self::$instance;
89 }
90 return self::$instance;
91 }
92
93 private function __construct() {
94 /** Do nothing **/
95 }
96
97 private function setup_globals() {
98
99 $this->modules = new stdClass();
100
101 }
102
103 /**
104 * Include the common resources to Edit Flow and dynamically load the modules
105 */
106 private function load_modules() {
107
108 // We use the WP_List_Table API for some of the table gen
109 if ( !class_exists( 'WP_List_Table' ) )
110 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
111
112 // Edit Flow base module
113 require_once( EDIT_FLOW_ROOT . '/common/php/class-module.php' );
114
115 // Edit Flow Block Editor Compat trait
116 require_once( EDIT_FLOW_ROOT . '/common/php/trait-block-editor-compatible.php' );
117
118 // Scan the modules directory and include any modules that exist there
119 $module_dirs = scandir( EDIT_FLOW_ROOT . '/modules/' );
120 $class_names = array();
121 foreach( $module_dirs as $module_dir ) {
122 if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" ) ) {
123 include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" );
124
125 // Try to load Gutenberg compat files
126 if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/compat/block-editor.php" ) ) {
127 include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/compat/block-editor.php" );
128 }
129 // Prepare the class name because it should be standardized
130 $tmp = explode( '-', $module_dir );
131 $class_name = '';
132 $slug_name = '';
133 foreach( $tmp as $word ) {
134 $class_name .= ucfirst( $word ) . '_';
135 $slug_name .= $word . '_';
136 }
137 $slug_name = rtrim( $slug_name, '_' );
138 $class_names[$slug_name] = 'EF_' . rtrim( $class_name, '_' );
139 }
140 }
141
142 // Instantiate EF_Module as $helpers for back compat and so we can
143 // use it in this class
144 $this->helpers = new EF_Module();
145
146 // Other utils
147 require_once( EDIT_FLOW_ROOT . '/common/php/util.php' );
148
149 // Instantiate all of our classes onto the Edit Flow object
150 // but make sure they exist too
151 foreach( $class_names as $slug => $class_name ) {
152 if ( class_exists( $class_name ) ) {
153 $this->$slug = new $class_name();
154 $compat_class_name = "{$class_name}_Block_Editor_Compat";
155 if ( class_exists( $compat_class_name ) ) {
156 $this->$slug->compat = new $compat_class_name( $this->$slug, $this->$slug->get_compat_hooks() );
157 }
158 }
159 }
160
161 /**
162 * Fires after edit_flow has loaded all Edit Flow internal modules.
163 *
164 * Plugin authors can hook into this action, include their own modules add them to the $edit_flow object
165 *
166 */
167 do_action( 'ef_modules_loaded' );
168
169 }
170
171 /**
172 * Setup the default hooks and actions
173 *
174 * @since EditFlow 0.7.4
175 * @access private
176 * @uses add_action() To add various actions
177 */
178 private function setup_actions() {
179 add_action( 'init', array( $this, 'action_init' ) );
180 add_action( 'init', array( $this, 'action_init_after' ), 1000 );
181
182 add_action( 'admin_init', array( $this, 'action_admin_init' ) );
183
184 /**
185 * Fires after setup of all edit_flow actions.
186 *
187 * Plugin authors can hook into this action to manipulate the edit_flow class after initial actions have been registered.
188 *
189 * @param edit_flow $this The core edit flow class
190 */
191 do_action_ref_array( 'editflow_after_setup_actions', array( &$this ) );
192 }
193
194 /**
195 * Inititalizes the Edit Flows!
196 * Loads options for each registered module and then initializes it if it's active
197 */
198 function action_init() {
199
200 load_plugin_textdomain( 'edit-flow', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
201
202 $this->load_modules();
203
204 // Load all of the module options
205 $this->load_module_options();
206
207 // Load all of the modules that are enabled.
208 // Modules won't have an options value if they aren't enabled
209 foreach ( $this->modules as $mod_name => $mod_data )
210 if ( isset( $mod_data->options->enabled ) && $mod_data->options->enabled == 'on' )
211 $this->$mod_name->init();
212
213 /**
214 * Fires after edit_flow has loaded all modules and module options.
215 *
216 * Plugin authors can hook into this action to trigger functionaltiy after all Edit Flow module's have been loaded.
217 *
218 */
219 do_action( 'ef_init' );
220 }
221
222 /**
223 * Initialize the plugin for the admin
224 */
225 function action_admin_init() {
226
227 // Upgrade if need be but don't run the upgrade if the plugin has never been used
228 $previous_version = get_option( $this->options_group . 'version' );
229 if ( $previous_version && version_compare( $previous_version, EDIT_FLOW_VERSION, '<' ) ) {
230 foreach ( $this->modules as $mod_name => $mod_data ) {
231 if ( method_exists( $this->$mod_name, 'upgrade' ) )
232 $this->$mod_name->upgrade( $previous_version );
233 }
234 update_option( $this->options_group . 'version', EDIT_FLOW_VERSION );
235 } else if ( !$previous_version ) {
236 update_option( $this->options_group . 'version', EDIT_FLOW_VERSION );
237 }
238
239 // For each module that's been loaded, auto-load data if it's never been run before
240 foreach ( $this->modules as $mod_name => $mod_data ) {
241 // If the module has never been loaded before, run the install method if there is one
242 if ( !isset( $mod_data->options->loaded_once ) || !$mod_data->options->loaded_once ) {
243 if ( method_exists( $this->$mod_name, 'install' ) )
244 $this->$mod_name->install();
245 $this->update_module_option( $mod_name, 'loaded_once', true );
246 }
247 }
248
249 $this->register_scripts_and_styles();
250
251 }
252
253 /**
254 * Register a new module with Edit Flow
255 */
256 public function register_module( $name, $args = array() ) {
257
258 // A title and name is required for every module
259 if ( !isset( $args['title'], $name ) )
260 return false;
261
262 $defaults = array(
263 'title' => '',
264 'short_description' => '',
265 'extended_description' => '',
266 'img_url' => false,
267 'slug' => '',
268 'post_type_support' => '',
269 'default_options' => array(),
270 'options' => false,
271 'configure_page_cb' => false,
272 'configure_link_text' => __( 'Configure', 'edit-flow' ),
273 // These messages are applied to modules and can be overridden if custom messages are needed
274 'messages' => array(
275 'settings-updated' => __( 'Settings updated.', 'edit-flow' ),
276 'form-error' => __( 'Please correct your form errors below and try again.', 'edit-flow' ),
277 'nonce-failed' => __( 'Cheatin&#8217; uh?', 'edit-flow' ),
278 'invalid-permissions' => __( 'You do not have necessary permissions to complete this action.', 'edit-flow' ),
279 'missing-post' => __( 'Post does not exist', 'edit-flow' ),
280 ),
281 'autoload' => false, // autoloading a module will remove the ability to enable or disable it
282 );
283 if ( isset( $args['messages'] ) )
284 $args['messages'] = array_merge( (array)$args['messages'], $defaults['messages'] );
285 $args = array_merge( $defaults, $args );
286 $args['name'] = $name;
287 $args['options_group_name'] = $this->options_group . $name . '_options';
288 if ( !isset( $args['settings_slug'] ) )
289 $args['settings_slug'] = 'ef-' . $args['slug'] . '-settings';
290 if ( empty( $args['post_type_support'] ) )
291 $args['post_type_support'] = 'ef_' . $name;
292 // If there's a Help Screen registered for the module, make sure we
293 // auto-load it
294 if ( !empty( $args['settings_help_tab'] ) )
295 add_action( 'load-edit-flow_page_' . $args['settings_slug'], array( &$this->$name, 'action_settings_help_menu' ) );
296
297 $this->modules->$name = (object) $args;
298
299 /**
300 * Fires after edit_flow has registered a module.
301 *
302 * Plugin authors can hook into this action to trigger functionaltiy after a module has been loaded.
303 *
304 * @param string $name The name of the registered module
305 */
306 do_action( 'ef_module_registered', $name );
307 return $this->modules->$name;
308 }
309
310 /**
311 * Load all of the module options from the database
312 * If a given option isn't yet set, then set it to the module's default (upgrades, etc.)
313 */
314 function load_module_options() {
315
316 foreach ( $this->modules as $mod_name => $mod_data ) {
317
318 $this->modules->$mod_name->options = get_option( $this->options_group . $mod_name . '_options', new stdClass );
319 foreach ( $mod_data->default_options as $default_key => $default_value ) {
320 if ( !isset( $this->modules->$mod_name->options->$default_key ) )
321 $this->modules->$mod_name->options->$default_key = $default_value;
322 }
323
324 $this->$mod_name->module = $this->modules->$mod_name;
325 }
326
327 /**
328 * Fires after edit_flow has loaded all of the module options from the database.
329 *
330 * Plugin authors can hook into this action to read and manipulate module settings.
331 *
332 */
333 do_action( 'ef_module_options_loaded' );
334 }
335
336 /**
337 * Load the post type options again so we give add_post_type_support() a chance to work
338 *
339 * @see http://dev.editflow.org/2011/11/17/edit-flow-v0-7-alpha2-notes/#comment-232
340 */
341 function action_init_after() {
342 foreach ( $this->modules as $mod_name => $mod_data ) {
343
344 if ( isset( $this->modules->$mod_name->options->post_types ) )
345 $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 );
346
347 $this->$mod_name->module = $this->modules->$mod_name;
348 }
349 }
350
351 /**
352 * Get a module by one of its descriptive values
353 *
354 * @param string $key The property to use for searching a module (ex: 'name')
355 * @param string|int|array $value The value to compare (using ==)
356 */
357 function get_module_by( $key, $value ) {
358 $module = false;
359 foreach ( $this->modules as $mod_name => $mod_data ) {
360
361 if ( $key == 'name' && $value == $mod_name ) {
362 $module = $this->modules->$mod_name;
363 } else {
364 foreach( $mod_data as $mod_data_key => $mod_data_value ) {
365 if ( $mod_data_key == $key && $mod_data_value == $value )
366 $module = $this->modules->$mod_name;
367 }
368 }
369 }
370 return $module;
371 }
372
373 /**
374 * Update the $edit_flow object with new value and save to the database
375 */
376 function update_module_option( $mod_name, $key, $value ) {
377 $this->modules->$mod_name->options->$key = $value;
378 $this->$mod_name->module = $this->modules->$mod_name;
379 return update_option( $this->options_group . $mod_name . '_options', $this->modules->$mod_name->options );
380 }
381
382 function update_all_module_options( $mod_name, $new_options ) {
383 if ( is_array( $new_options ) )
384 $new_options = (object)$new_options;
385 $this->modules->$mod_name->options = $new_options;
386 $this->$mod_name->module = $this->modules->$mod_name;
387 return update_option( $this->options_group . $mod_name . '_options', $this->modules->$mod_name->options );
388 }
389
390 /**
391 * Registers commonly used scripts + styles for easy enqueueing
392 */
393 function register_scripts_and_styles() {
394 wp_enqueue_style( 'ef-admin-css', EDIT_FLOW_URL . 'common/css/edit-flow-admin.css', false, EDIT_FLOW_VERSION, 'all' );
395
396 wp_register_script( 'jquery-listfilterizer', EDIT_FLOW_URL . 'common/js/jquery.listfilterizer.js', array( 'jquery' ), EDIT_FLOW_VERSION, true );
397 wp_register_style( 'jquery-listfilterizer', EDIT_FLOW_URL . 'common/css/jquery.listfilterizer.css', false, EDIT_FLOW_VERSION, 'all' );
398
399
400 wp_localize_script( 'jquery-listfilterizer',
401 '__i18n_jquery_filterizer',
402 array(
403 'all' => esc_html__( 'All', 'edit-flow' ),
404 'selected' => esc_html__( 'Selected', 'edit-flow' ),
405 ) );
406
407 wp_register_script( 'jquery-quicksearch', EDIT_FLOW_URL . 'common/js/jquery.quicksearch.js', array( 'jquery' ), EDIT_FLOW_VERSION, true );
408
409 }
410
411 }
412
413 function EditFlow() {
414 return edit_flow::instance();
415 }
416 add_action( 'plugins_loaded', 'EditFlow' );
417