Make WordPress Core


Ignore:
Timestamp:
08/26/2016 06:10:23 PM (10 years ago)
Author:
wonderboymusic
Message:

Load: move WP_MatchesMapRegex into its own file.

See #37827.

File:
1 edited

Legend:

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

    r37542 r38376  
    739739        }
    740740}
    741 
    742 /**
    743  * Helper class to remove the need to use eval to replace $matches[] in query strings.
    744  *
    745  * @since 2.9.0
    746  */
    747 class WP_MatchesMapRegex {
    748         /**
    749          * store for matches
    750          *
    751          * @access private
    752          * @var array
    753          */
    754         private $_matches;
    755 
    756         /**
    757          * store for mapping result
    758          *
    759          * @access public
    760          * @var string
    761          */
    762         public $output;
    763 
    764         /**
    765          * subject to perform mapping on (query string containing $matches[] references
    766          *
    767          * @access private
    768          * @var string
    769          */
    770         private $_subject;
    771 
    772         /**
    773          * regexp pattern to match $matches[] references
    774          *
    775          * @var string
    776          */
    777         public $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
    778 
    779         /**
    780          * constructor
    781          *
    782          * @param string $subject subject if regex
    783          * @param array  $matches data to use in map
    784          */
    785         public function __construct($subject, $matches) {
    786                 $this->_subject = $subject;
    787                 $this->_matches = $matches;
    788                 $this->output = $this->_map();
    789         }
    790 
    791         /**
    792          * Substitute substring matches in subject.
    793          *
    794          * static helper function to ease use
    795          *
    796          * @static
    797          * @access public
    798          *
    799          * @param string $subject subject
    800          * @param array  $matches data used for substitution
    801          * @return string
    802          */
    803         public static function apply($subject, $matches) {
    804                 $oSelf = new WP_MatchesMapRegex($subject, $matches);
    805                 return $oSelf->output;
    806         }
    807 
    808         /**
    809          * do the actual mapping
    810          *
    811          * @access private
    812          * @return string
    813          */
    814         private function _map() {
    815                 $callback = array($this, 'callback');
    816                 return preg_replace_callback($this->_pattern, $callback, $this->_subject);
    817         }
    818 
    819         /**
    820          * preg_replace_callback hook
    821          *
    822          * @access public
    823          * @param  array $matches preg_replace regexp matches
    824          * @return string
    825          */
    826         public function callback($matches) {
    827                 $index = intval(substr($matches[0], 9, -1));
    828                 return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );
    829         }
    830 }
Note: See TracChangeset for help on using the changeset viewer.