Make WordPress Core

Ticket #26826: plugin.php.patch

File plugin.php.patch, 18.3 KB (added by mgibbs189, 10 years ago)
  • plugin.php

     
    8282function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
    8383        global $wp_filter, $merged_filters;
    8484
    85         $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
    86         $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
     85        $idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
     86        $wp_filter[ $tag ][ $priority ][ $idx ] = array( 'function' => $function_to_add, 'accepted_args' => $accepted_args );
    8787        unset( $merged_filters[ $tag ] );
    8888        return true;
    8989}
     
    103103 *      When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
    104104 *      (e.g.) 0, so use the === operator for testing the return value.
    105105 */
    106 function has_filter($tag, $function_to_check = false) {
     106function has_filter( $tag, $function_to_check = false ) {
    107107        global $wp_filter;
    108108
    109         $has = !empty($wp_filter[$tag]);
     109        $has = !empty( $wp_filter[ $tag ] );
    110110        if ( false === $function_to_check || false == $has )
    111111                return $has;
    112112
    113         if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
     113        if ( ! $idx = _wp_filter_build_unique_id( $tag, $function_to_check, false ) )
    114114                return false;
    115115
    116         foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
    117                 if ( isset($wp_filter[$tag][$priority][$idx]) )
     116        foreach ( (array) array_keys( $wp_filter[ $tag ] ) as $priority ) {
     117                if ( isset( $wp_filter[ $tag ][ $priority ][ $idx ] ) )
    118118                        return $priority;
    119119        }
    120120
     
    166166        $args = array();
    167167
    168168        // Do 'all' actions first
    169         if ( isset($wp_filter['all']) ) {
     169        if ( isset( $wp_filter['all'] ) ) {
    170170                $wp_current_filter[] = $tag;
    171171                $args = func_get_args();
    172                 _wp_call_all_hook($args);
     172                _wp_call_all_hook( $args );
    173173        }
    174174
    175         if ( !isset($wp_filter[$tag]) ) {
    176                 if ( isset($wp_filter['all']) )
    177                         array_pop($wp_current_filter);
     175        if ( ! isset( $wp_filter[ $tag ] ) ) {
     176                if ( isset( $wp_filter['all'] ) )
     177                        array_pop( $wp_current_filter );
    178178                return $value;
    179179        }
    180180
    181         if ( !isset($wp_filter['all']) )
     181        if ( ! isset( $wp_filter['all'] ) )
    182182                $wp_current_filter[] = $tag;
    183183
    184184        // Sort
    185         if ( !isset( $merged_filters[ $tag ] ) ) {
    186                 ksort($wp_filter[$tag]);
     185        if ( ! isset( $merged_filters[ $tag ] ) ) {
     186                ksort( $wp_filter[ $tag ] );
    187187                $merged_filters[ $tag ] = true;
    188188        }
    189189
    190190        reset( $wp_filter[ $tag ] );
    191191
    192         if ( empty($args) )
     192        if ( empty( $args ) )
    193193                $args = func_get_args();
    194194
    195195        do {
    196                 foreach( (array) current($wp_filter[$tag]) as $the_ )
    197                         if ( !is_null($the_['function']) ){
     196                foreach ( (array) current( $wp_filter[ $tag ] ) as $the_ )
     197                        if ( ! is_null( $the_['function'] ) ){
    198198                                $args[1] = $value;
    199                                 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
     199                                $value = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
    200200                        }
    201201
    202         } while ( next($wp_filter[$tag]) !== false );
     202        } while ( next( $wp_filter[$tag] ) !== false );
    203203
    204204        array_pop( $wp_current_filter );
    205205
     
    223223 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
    224224 * @return mixed The filtered value after all hooked functions are applied to it.
    225225 */
    226 function apply_filters_ref_array($tag, $args) {
     226function apply_filters_ref_array( $tag, $args ) {
    227227        global $wp_filter, $merged_filters, $wp_current_filter;
    228228
    229229        // Do 'all' actions first
    230         if ( isset($wp_filter['all']) ) {
     230        if ( isset( $wp_filter['all'] ) ) {
    231231                $wp_current_filter[] = $tag;
    232232                $all_args = func_get_args();
    233                 _wp_call_all_hook($all_args);
     233                _wp_call_all_hook( $all_args );
    234234        }
    235235
    236         if ( !isset($wp_filter[$tag]) ) {
    237                 if ( isset($wp_filter['all']) )
    238                         array_pop($wp_current_filter);
     236        if ( ! isset( $wp_filter[ $tag ] ) ) {
     237                if ( isset( $wp_filter['all'] ) )
     238                        array_pop( $wp_current_filter );
    239239                return $args[0];
    240240        }
    241241
    242         if ( !isset($wp_filter['all']) )
     242        if ( ! isset( $wp_filter['all'] ) )
    243243                $wp_current_filter[] = $tag;
    244244
    245245        // Sort
    246         if ( !isset( $merged_filters[ $tag ] ) ) {
    247                 ksort($wp_filter[$tag]);
     246        if ( ! isset( $merged_filters[ $tag ] ) ) {
     247                ksort( $wp_filter[ $tag ] );
    248248                $merged_filters[ $tag ] = true;
    249249        }
    250250
     
    251251        reset( $wp_filter[ $tag ] );
    252252
    253253        do {
    254                 foreach( (array) current($wp_filter[$tag]) as $the_ )
    255                         if ( !is_null($the_['function']) )
    256                                 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     254                foreach ( (array) current( $wp_filter[ $tag ] ) as $the_ )
     255                        if ( ! is_null( $the_['function'] ) )
     256                                $args[0] = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
    257257
    258         } while ( next($wp_filter[$tag]) !== false );
     258        } while ( next( $wp_filter[ $tag ] ) !== false );
    259259
    260260        array_pop( $wp_current_filter );
    261261
     
    284284 * @return boolean Whether the function existed before it was removed.
    285285 */
    286286function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
    287         $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
     287        $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
    288288
    289         $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
     289        $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
    290290
    291291        if ( true === $r) {
    292                 unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
    293                 if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
    294                         unset($GLOBALS['wp_filter'][$tag][$priority]);
    295                 unset($GLOBALS['merged_filters'][$tag]);
     292                unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
     293                if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) )
     294                        unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] );
     295                unset( $GLOBALS['merged_filters'][ $tag ] );
    296296        }
    297297
    298298        return $r;
     
    307307 * @param int $priority The priority number to remove.
    308308 * @return bool True when finished.
    309309 */
    310 function remove_all_filters($tag, $priority = false) {
     310function remove_all_filters( $tag, $priority = false ) {
    311311        global $wp_filter, $merged_filters;
    312312
    313         if( isset($wp_filter[$tag]) ) {
    314                 if( false !== $priority && isset($wp_filter[$tag][$priority]) )
    315                         unset($wp_filter[$tag][$priority]);
     313        if( isset( $wp_filter[ $tag ] ) ) {
     314                if( false !== $priority && isset( $wp_filter[ $tag ][ $priority ] ) )
     315                        unset( $wp_filter[ $tag ][ $priority ] );
    316316                else
    317                         unset($wp_filter[$tag]);
     317                        unset( $wp_filter[ $tag ] );
    318318        }
    319319
    320         if( isset($merged_filters[$tag]) )
    321                 unset($merged_filters[$tag]);
     320        if( isset( $merged_filters[ $tag ] ) )
     321                unset( $merged_filters[ $tag ] );
    322322
    323323        return true;
    324324}
     
    356356 * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
    357357 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
    358358 */
    359 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    360         return add_filter($tag, $function_to_add, $priority, $accepted_args);
     359function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
     360        return add_filter( $tag, $function_to_add, $priority, $accepted_args );
    361361}
    362362
    363363/**
     
    383383 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
    384384 * @return null Will return null if $tag does not exist in $wp_filter array
    385385 */
    386 function do_action($tag, $arg = '') {
     386function do_action( $tag, $arg = '' ) {
    387387        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    388388
    389         if ( ! isset($wp_actions[$tag]) )
    390                 $wp_actions[$tag] = 1;
     389        if ( ! isset( $wp_actions[ $tag ] ) )
     390                $wp_actions[ $tag ] = 1;
    391391        else
    392                 ++$wp_actions[$tag];
     392                ++$wp_actions[ $tag ];
    393393
    394394        // Do 'all' actions first
    395         if ( isset($wp_filter['all']) ) {
     395        if ( isset( $wp_filter['all'] ) ) {
    396396                $wp_current_filter[] = $tag;
    397397                $all_args = func_get_args();
    398                 _wp_call_all_hook($all_args);
     398                _wp_call_all_hook( $all_args );
    399399        }
    400400
    401         if ( !isset($wp_filter[$tag]) ) {
    402                 if ( isset($wp_filter['all']) )
    403                         array_pop($wp_current_filter);
     401        if ( ! isset( $wp_filter[ $tag ] ) ) {
     402                if ( isset( $wp_filter['all'] ) )
     403                        array_pop( $wp_current_filter );
    404404                return;
    405405        }
    406406
    407         if ( !isset($wp_filter['all']) )
     407        if ( ! isset( $wp_filter['all'] ) )
    408408                $wp_current_filter[] = $tag;
    409409
    410410        $args = array();
    411         if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
     411        if ( is_array( $arg ) && 1 == count( $arg ) && isset( $arg[0] ) && is_object( $arg[0] ) ) // array(&$this)
    412412                $args[] =& $arg[0];
    413413        else
    414414                $args[] = $arg;
    415415        for ( $a = 2; $a < func_num_args(); $a++ )
    416                 $args[] = func_get_arg($a);
     416                $args[] = func_get_arg( $a );
    417417
    418418        // Sort
    419419        if ( !isset( $merged_filters[ $tag ] ) ) {
    420                 ksort($wp_filter[$tag]);
     420                ksort( $wp_filter[ $tag ] );
    421421                $merged_filters[ $tag ] = true;
    422422        }
    423423
     
    424424        reset( $wp_filter[ $tag ] );
    425425
    426426        do {
    427                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    428                         if ( !is_null($the_['function']) )
    429                                 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     427                foreach ( (array) current( $wp_filter[ $tag ] ) as $the_ )
     428                        if ( ! is_null( $the_['function'] ) )
     429                                call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
    430430
    431         } while ( next($wp_filter[$tag]) !== false );
     431        } while ( next( $wp_filter[ $tag ] ) !== false );
    432432
    433         array_pop($wp_current_filter);
     433        array_pop( $wp_current_filter );
    434434}
    435435
    436436/**
     
    444444 * @param string $tag The name of the action hook.
    445445 * @return int The number of times action hook <tt>$tag</tt> is fired
    446446 */
    447 function did_action($tag) {
     447function did_action( $tag ) {
    448448        global $wp_actions;
    449449
    450450        if ( ! isset( $wp_actions[ $tag ] ) )
    451451                return 0;
    452452
    453         return $wp_actions[$tag];
     453        return $wp_actions[ $tag ];
    454454}
    455455
    456456/**
     
    469469 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
    470470 * @return null Will return null if $tag does not exist in $wp_filter array
    471471 */
    472 function do_action_ref_array($tag, $args) {
     472function do_action_ref_array( $tag, $args ) {
    473473        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    474474
    475         if ( ! isset($wp_actions[$tag]) )
     475        if ( ! isset( $wp_actions[ $tag ] ) )
    476476                $wp_actions[$tag] = 1;
    477477        else
    478478                ++$wp_actions[$tag];
    479479
    480480        // Do 'all' actions first
    481         if ( isset($wp_filter['all']) ) {
     481        if ( isset( $wp_filter['all'] ) ) {
    482482                $wp_current_filter[] = $tag;
    483483                $all_args = func_get_args();
    484                 _wp_call_all_hook($all_args);
     484                _wp_call_all_hook( $all_args );
    485485        }
    486486
    487         if ( !isset($wp_filter[$tag]) ) {
    488                 if ( isset($wp_filter['all']) )
    489                         array_pop($wp_current_filter);
     487        if ( ! isset( $wp_filter[ $tag ] ) ) {
     488                if ( isset( $wp_filter['all'] ) )
     489                        array_pop( $wp_current_filter );
    490490                return;
    491491        }
    492492
    493         if ( !isset($wp_filter['all']) )
     493        if ( ! isset( $wp_filter['all'] ) )
    494494                $wp_current_filter[] = $tag;
    495495
    496496        // Sort
    497         if ( !isset( $merged_filters[ $tag ] ) ) {
    498                 ksort($wp_filter[$tag]);
     497        if ( ! isset( $merged_filters[ $tag ] ) ) {
     498                ksort( $wp_filter[ $tag ] );
    499499                $merged_filters[ $tag ] = true;
    500500        }
    501501
     
    502502        reset( $wp_filter[ $tag ] );
    503503
    504504        do {
    505                 foreach( (array) current($wp_filter[$tag]) as $the_ )
    506                         if ( !is_null($the_['function']) )
    507                                 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     505                foreach( (array) current( $wp_filter[ $tag ] ) as $the_ )
     506                        if ( ! is_null( $the_['function'] ) )
     507                                call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
    508508
    509         } while ( next($wp_filter[$tag]) !== false );
     509        } while ( next( $wp_filter[ $tag ] ) !== false );
    510510
    511         array_pop($wp_current_filter);
     511        array_pop( $wp_current_filter );
    512512}
    513513
    514514/**
     
    526526 *      When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
    527527 *      (e.g.) 0, so use the === operator for testing the return value.
    528528 */
    529 function has_action($tag, $function_to_check = false) {
    530         return has_filter($tag, $function_to_check);
     529function has_action( $tag, $function_to_check = false ) {
     530        return has_filter( $tag, $function_to_check );
    531531}
    532532
    533533/**
     
    559559 * @param int $priority The priority number to remove them from.
    560560 * @return bool True when finished.
    561561 */
    562 function remove_all_actions($tag, $priority = false) {
    563         return remove_all_filters($tag, $priority);
     562function remove_all_actions( $tag, $priority = false ) {
     563        return remove_all_filters( $tag, $priority );
    564564}
    565565
    566566//
     
    582582 * @return string The name of a plugin.
    583583 * @uses WP_PLUGIN_DIR
    584584 */
    585 function plugin_basename($file) {
    586         $file = str_replace('\\','/',$file); // sanitize for Win32 installs
    587         $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
    588         $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
    589         $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
    590         $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
    591         $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
    592         $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
    593         $file = trim($file, '/');
     585function plugin_basename( $file ) {
     586        $file = str_replace( '\\', '/', $file ); // sanitize for Win32 installs
     587        $file = preg_replace( '|/+|', '/', $file ); // remove any duplicate slash
     588        $plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR ); // sanitize for Win32 installs
     589        $plugin_dir = preg_replace( '|/+|', '/', $plugin_dir ); // remove any duplicate slash
     590        $mu_plugin_dir = str_replace( '\\', '/', WPMU_PLUGIN_DIR ); // sanitize for Win32 installs
     591        $mu_plugin_dir = preg_replace( '|/+|', '/', $mu_plugin_dir ); // remove any duplicate slash
     592        $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file); // get relative path from plugins dir
     593        $file = trim( $file, '/' );
    594594        return $file;
    595595}
    596596
     
    640640 * @param string $file The filename of the plugin including the path.
    641641 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
    642642 */
    643 function register_activation_hook($file, $function) {
    644         $file = plugin_basename($file);
    645         add_action('activate_' . $file, $function);
     643function register_activation_hook( $file, $function ) {
     644        $file = plugin_basename( $file );
     645        add_action( 'activate_' . $file, $function );
    646646}
    647647
    648648/**
     
    665665 * @param string $file The filename of the plugin including the path.
    666666 * @param callback $function the function hooked to the 'deactivate_PLUGIN' action.
    667667 */
    668 function register_deactivation_hook($file, $function) {
    669         $file = plugin_basename($file);
    670         add_action('deactivate_' . $file, $function);
     668function register_deactivation_hook( $file, $function ) {
     669        $file = plugin_basename( $file );
     670        add_action( 'deactivate_' . $file, $function );
    671671}
    672672
    673673/**
     
    704704        // The option should not be autoloaded, because it is not needed in most
    705705        // cases. Emphasis should be put on using the 'uninstall.php' way of
    706706        // uninstalling the plugin.
    707         $uninstallable_plugins = (array) get_option('uninstall_plugins');
    708         $uninstallable_plugins[plugin_basename($file)] = $callback;
    709         update_option('uninstall_plugins', $uninstallable_plugins);
     707        $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
     708        $uninstallable_plugins[ plugin_basename( $file ) ] = $callback;
     709        update_option( 'uninstall_plugins', $uninstallable_plugins );
    710710}
    711711
    712712/**
     
    729729 *
    730730 * @param array $args The collected parameters from the hook that was called.
    731731 */
    732 function _wp_call_all_hook($args) {
     732function _wp_call_all_hook( $args ) {
    733733        global $wp_filter;
    734734
    735735        reset( $wp_filter['all'] );
    736736        do {
    737                 foreach( (array) current($wp_filter['all']) as $the_ )
    738                         if ( !is_null($the_['function']) )
    739                                 call_user_func_array($the_['function'], $args);
     737                foreach( (array) current( $wp_filter['all'] ) as $the_ )
     738                        if ( ! is_null( $the_['function'] ) )
     739                                call_user_func_array( $the_['function'], $args );
    740740
    741         } while ( next($wp_filter['all']) !== false );
     741        } while ( next( $wp_filter['all'] ) !== false );
    742742}
    743743
    744744/**
     
    770770 * @param int|bool $priority Used in counting how many hooks were applied. If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.
    771771 * @return string|bool Unique ID for usage as array key or false if $priority === false and $function is an object reference, and it does not already have a unique id.
    772772 */
    773 function _wp_filter_build_unique_id($tag, $function, $priority) {
     773function _wp_filter_build_unique_id( $tag, $function, $priority ) {
    774774        global $wp_filter;
    775775        static $filter_id_count = 0;
    776776
    777         if ( is_string($function) )
     777        if ( is_string( $function ) )
    778778                return $function;
    779779
    780         if ( is_object($function) ) {
     780        if ( is_object( $function ) ) {
    781781                // Closures are currently implemented as objects
    782782                $function = array( $function, '' );
    783783        } else {
     
    784784                $function = (array) $function;
    785785        }
    786786
    787         if (is_object($function[0]) ) {
     787        if ( is_object( $function[0] ) ) {
    788788                // Object Class Calling
    789                 if ( function_exists('spl_object_hash') ) {
    790                         return spl_object_hash($function[0]) . $function[1];
     789                if ( function_exists( 'spl_object_hash' ) ) {
     790                        return spl_object_hash( $function[0] ) . $function[1];
    791791                } else {
    792                         $obj_idx = get_class($function[0]).$function[1];
    793                         if ( !isset($function[0]->wp_filter_id) ) {
     792                        $obj_idx = get_class( $function[0] ) . $function[1];
     793                        if ( ! isset( $function[0]->wp_filter_id ) ) {
    794794                                if ( false === $priority )
    795795                                        return false;
    796                                 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
     796                                $obj_idx .= isset( $wp_filter[ $tag ][ $priority ] ) ? count( (array) $wp_filter[ $tag ][ $priority ] ) : $filter_id_count;
    797797                                $function[0]->wp_filter_id = $filter_id_count;
    798798                                ++$filter_id_count;
    799799                        } else {
     
    802802
    803803                        return $obj_idx;
    804804                }
    805         } else if ( is_string($function[0]) ) {
     805        } else if ( is_string( $function[0] ) ) {
    806806                // Static Calling
    807807                return $function[0] . '::' . $function[1];
    808808        }