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