Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/plugin.php

    r41751 r42343  
    3434}
    3535
    36 if ( ! isset( $wp_actions ) )
     36if ( ! isset( $wp_actions ) ) {
    3737        $wp_actions = array();
    38 
    39 if ( ! isset( $wp_current_filter ) )
     38}
     39
     40if ( ! isset( $wp_current_filter ) ) {
    4041        $wp_current_filter = array();
     42}
    4143
    4244/**
    … …  
    129131 *                   return value.
    130132 */
    131 function has_filter($tag, $function_to_check = false) {
     133function has_filter( $tag, $function_to_check = false ) {
    132134        global $wp_filter;
    133135
    … …  
    180182
    181183        // Do 'all' actions first.
    182         if ( isset($wp_filter['all']) ) {
     184        if ( isset( $wp_filter['all'] ) ) {
    183185                $wp_current_filter[] = $tag;
     186                $args                = func_get_args();
     187                _wp_call_all_hook( $args );
     188        }
     189
     190        if ( ! isset( $wp_filter[ $tag ] ) ) {
     191                if ( isset( $wp_filter['all'] ) ) {
     192                        array_pop( $wp_current_filter );
     193                }
     194                return $value;
     195        }
     196
     197        if ( ! isset( $wp_filter['all'] ) ) {
     198                $wp_current_filter[] = $tag;
     199        }
     200
     201        if ( empty( $args ) ) {
    184202                $args = func_get_args();
    185                 _wp_call_all_hook($args);
    186         }
    187 
    188         if ( !isset($wp_filter[$tag]) ) {
    189                 if ( isset($wp_filter['all']) )
    190                         array_pop($wp_current_filter);
    191                 return $value;
    192         }
    193 
    194         if ( !isset($wp_filter['all']) )
    195                 $wp_current_filter[] = $tag;
    196 
    197         if ( empty($args) )
    198                 $args = func_get_args();
     203        }
    199204
    200205        // don't pass the tag name to WP_Hook
    … …  
    223228 * @return mixed The filtered value after all hooked functions are applied to it.
    224229 */
    225 function apply_filters_ref_array($tag, $args) {
     230function apply_filters_ref_array( $tag, $args ) {
    226231        global $wp_filter, $wp_current_filter;
    227232
    228233        // Do 'all' actions first
    229         if ( isset($wp_filter['all']) ) {
     234        if ( isset( $wp_filter['all'] ) ) {
    230235                $wp_current_filter[] = $tag;
    231                 $all_args = func_get_args();
    232                 _wp_call_all_hook($all_args);
    233         }
    234 
    235         if ( !isset($wp_filter[$tag]) ) {
    236                 if ( isset($wp_filter['all']) )
    237                         array_pop($wp_current_filter);
     236                $all_args            = func_get_args();
     237                _wp_call_all_hook( $all_args );
     238        }
     239
     240        if ( ! isset( $wp_filter[ $tag ] ) ) {
     241                if ( isset( $wp_filter['all'] ) ) {
     242                        array_pop( $wp_current_filter );
     243                }
    238244                return $args[0];
    239245        }
    240246
    241         if ( !isset($wp_filter['all']) )
     247        if ( ! isset( $wp_filter['all'] ) ) {
    242248                $wp_current_filter[] = $tag;
     249        }
    243250
    244251        $filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args );
    … …  
    297304        global $wp_filter;
    298305
    299         if ( isset( $wp_filter[ $tag ]) ) {
     306        if ( isset( $wp_filter[ $tag ] ) ) {
    300307                $wp_filter[ $tag ]->remove_all_filters( $priority );
    301308                if ( ! $wp_filter[ $tag ]->has_filters() ) {
    … …  
    396403 * @return true Will always return true.
    397404 */
    398 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    399         return add_filter($tag, $function_to_add, $priority, $accepted_args);
     405function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
     406        return add_filter( $tag, $function_to_add, $priority, $accepted_args );
    400407}
    401408
    … …  
    419426 *                        functions hooked to the action. Default empty.
    420427 */
    421 function do_action($tag, $arg = '') {
     428function do_action( $tag, $arg = '' ) {
    422429        global $wp_filter, $wp_actions, $wp_current_filter;
    423430
    424         if ( ! isset($wp_actions[$tag]) )
    425                 $wp_actions[$tag] = 1;
    426         else
    427                 ++$wp_actions[$tag];
     431        if ( ! isset( $wp_actions[ $tag ] ) ) {
     432                $wp_actions[ $tag ] = 1;
     433        } else {
     434                ++$wp_actions[ $tag ];
     435        }
    428436
    429437        // Do 'all' actions first
    430         if ( isset($wp_filter['all']) ) {
     438        if ( isset( $wp_filter['all'] ) ) {
    431439                $wp_current_filter[] = $tag;
    432                 $all_args = func_get_args();
    433                 _wp_call_all_hook($all_args);
    434         }
    435 
    436         if ( !isset($wp_filter[$tag]) ) {
    437                 if ( isset($wp_filter['all']) )
    438                         array_pop($wp_current_filter);
     440                $all_args            = func_get_args();
     441                _wp_call_all_hook( $all_args );
     442        }
     443
     444        if ( ! isset( $wp_filter[ $tag ] ) ) {
     445                if ( isset( $wp_filter['all'] ) ) {
     446                        array_pop( $wp_current_filter );
     447                }
    439448                return;
    440449        }
    441450
    442         if ( !isset($wp_filter['all']) )
     451        if ( ! isset( $wp_filter['all'] ) ) {
    443452                $wp_current_filter[] = $tag;
     453        }
    444454
    445455        $args = array();
    446         if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
     456        if ( is_array( $arg ) && 1 == count( $arg ) && isset( $arg[0] ) && is_object( $arg[0] ) ) { // array(&$this)
    447457                $args[] =& $arg[0];
    448         else
     458        } else {
    449459                $args[] = $arg;
    450         for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
    451                 $args[] = func_get_arg($a);
     460        }
     461        for ( $a = 2, $num = func_num_args(); $a < $num; $a++ ) {
     462                $args[] = func_get_arg( $a );
     463        }
    452464
    453465        $wp_filter[ $tag ]->do_action( $args );
    454466
    455         array_pop($wp_current_filter);
     467        array_pop( $wp_current_filter );
    456468}
    457469
    … …  
    466478 * @return int The number of times action hook $tag is fired.
    467479 */
    468 function did_action($tag) {
     480function did_action( $tag ) {
    469481        global $wp_actions;
    470482
    471         if ( ! isset( $wp_actions[ $tag ] ) )
     483        if ( ! isset( $wp_actions[ $tag ] ) ) {
    472484                return 0;
    473 
    474         return $wp_actions[$tag];
     485        }
     486
     487        return $wp_actions[ $tag ];
    475488}
    476489
    … …  
    489502 * @param array  $args The arguments supplied to the functions hooked to `$tag`.
    490503 */
    491 function do_action_ref_array($tag, $args) {
     504function do_action_ref_array( $tag, $args ) {
    492505        global $wp_filter, $wp_actions, $wp_current_filter;
    493506
    494         if ( ! isset($wp_actions[$tag]) )
    495                 $wp_actions[$tag] = 1;
    496         else
    497                 ++$wp_actions[$tag];
     507        if ( ! isset( $wp_actions[ $tag ] ) ) {
     508                $wp_actions[ $tag ] = 1;
     509        } else {
     510                ++$wp_actions[ $tag ];
     511        }
    498512
    499513        // Do 'all' actions first
    500         if ( isset($wp_filter['all']) ) {
     514        if ( isset( $wp_filter['all'] ) ) {
    501515                $wp_current_filter[] = $tag;
    502                 $all_args = func_get_args();
    503                 _wp_call_all_hook($all_args);
    504         }
    505 
    506         if ( !isset($wp_filter[$tag]) ) {
    507                 if ( isset($wp_filter['all']) )
    508                         array_pop($wp_current_filter);
     516                $all_args            = func_get_args();
     517                _wp_call_all_hook( $all_args );
     518        }
     519
     520        if ( ! isset( $wp_filter[ $tag ] ) ) {
     521                if ( isset( $wp_filter['all'] ) ) {
     522                        array_pop( $wp_current_filter );
     523                }
    509524                return;
    510525        }
    511526
    512         if ( !isset($wp_filter['all']) )
     527        if ( ! isset( $wp_filter['all'] ) ) {
    513528                $wp_current_filter[] = $tag;
     529        }
    514530
    515531        $wp_filter[ $tag ]->do_action( $args );
    516532
    517         array_pop($wp_current_filter);
     533        array_pop( $wp_current_filter );
    518534}
    519535
    … …  
    534550 *                  return value.
    535551 */
    536 function has_action($tag, $function_to_check = false) {
    537         return has_filter($tag, $function_to_check);
     552function has_action( $tag, $function_to_check = false ) {
     553        return has_filter( $tag, $function_to_check );
    538554}
    539555
    … …  
    565581 * @return true True when finished.
    566582 */
    567 function remove_all_actions($tag, $priority = false) {
    568         return remove_all_filters($tag, $priority);
     583function remove_all_actions( $tag, $priority = false ) {
     584        return remove_all_filters( $tag, $priority );
    569585}
    570586
    … …  
    661677        }
    662678
    663         $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
     679        $plugin_dir    = wp_normalize_path( WP_PLUGIN_DIR );
    664680        $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
    665681
    666         $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
    667         $file = trim($file, '/');
     682        $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file ); // get relative path from plugins dir
     683        $file = trim( $file, '/' );
    668684        return $file;
    669685}
    … …  
    692708        static $wp_plugin_path = null, $wpmu_plugin_path = null;
    693709        if ( ! isset( $wp_plugin_path ) ) {
    694                 $wp_plugin_path   = wp_normalize_path( WP_PLUGIN_DIR   );
     710                $wp_plugin_path   = wp_normalize_path( WP_PLUGIN_DIR );
    695711                $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
    696712        }
    697713
    698         $plugin_path = wp_normalize_path( dirname( $file ) );
     714        $plugin_path     = wp_normalize_path( dirname( $file ) );
    699715        $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
    700716
    … …  
    752768 * @param callable $function The function hooked to the 'activate_PLUGIN' action.
    753769 */
    754 function register_activation_hook($file, $function) {
    755         $file = plugin_basename($file);
    756         add_action('activate_' . $file, $function);
     770function register_activation_hook( $file, $function ) {
     771        $file = plugin_basename( $file );
     772        add_action( 'activate_' . $file, $function );
    757773}
    758774
    … …  
    775791 * @param callable $function The function hooked to the 'deactivate_PLUGIN' action.
    776792 */
    777 function register_deactivation_hook($file, $function) {
    778         $file = plugin_basename($file);
    779         add_action('deactivate_' . $file, $function);
     793function register_deactivation_hook( $file, $function ) {
     794        $file = plugin_basename( $file );
     795        add_action( 'deactivate_' . $file, $function );
    780796}
    781797
    … …  
    817833         * uninstalling the plugin.
    818834         */
    819         $uninstallable_plugins = (array) get_option('uninstall_plugins');
    820         $uninstallable_plugins[plugin_basename($file)] = $callback;
    821 
    822         update_option('uninstall_plugins', $uninstallable_plugins);
     835        $uninstallable_plugins                             = (array) get_option( 'uninstall_plugins' );
     836        $uninstallable_plugins[ plugin_basename( $file ) ] = $callback;
     837
     838        update_option( 'uninstall_plugins', $uninstallable_plugins );
    823839}
    824840
    … …  
    841857 * @param array $args The collected parameters from the hook that was called.
    842858 */
    843 function _wp_call_all_hook($args) {
     859function _wp_call_all_hook( $args ) {
    844860        global $wp_filter;
    845861
    … …  
    881897 *                      a unique id.
    882898 */
    883 function _wp_filter_build_unique_id($tag, $function, $priority) {
     899function _wp_filter_build_unique_id( $tag, $function, $priority ) {
    884900        global $wp_filter;
    885901        static $filter_id_count = 0;
    886902
    887         if ( is_string($function) )
     903        if ( is_string( $function ) ) {
    888904                return $function;
    889 
    890         if ( is_object($function) ) {
     905        }
     906
     907        if ( is_object( $function ) ) {
    891908                // Closures are currently implemented as objects
    892909                $function = array( $function, '' );
    … …  
    895912        }
    896913
    897         if (is_object($function[0]) ) {
     914        if ( is_object( $function[0] ) ) {
    898915                // Object Class Calling
    899                 if ( function_exists('spl_object_hash') ) {
    900                         return spl_object_hash($function[0]) . $function[1];
     916                if ( function_exists( 'spl_object_hash' ) ) {
     917                        return spl_object_hash( $function[0] ) . $function[1];
    901918                } else {
    902                         $obj_idx = get_class($function[0]).$function[1];
    903                         if ( !isset($function[0]->wp_filter_id) ) {
    904                                 if ( false === $priority )
     919                        $obj_idx = get_class( $function[0] ) . $function[1];
     920                        if ( ! isset( $function[0]->wp_filter_id ) ) {
     921                                if ( false === $priority ) {
    905922                                        return false;
    906                                 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
     923                                }
     924                                $obj_idx                  .= isset( $wp_filter[ $tag ][ $priority ] ) ? count( (array) $wp_filter[ $tag ][ $priority ] ) : $filter_id_count;
    907925                                $function[0]->wp_filter_id = $filter_id_count;
    908926                                ++$filter_id_count;
Note: See TracChangeset for help on using the changeset viewer.