/wp-includes/class-wp-matchesmapregex.php

  1. <?php 
  2. /** 
  3. * WP_MatchesMapRegex helper class 
  4. * 
  5. * @package WordPress 
  6. * @since 4.7.0 
  7. */ 
  8.  
  9. /** 
  10. * Helper class to remove the need to use eval to replace $matches[] in query strings. 
  11. * 
  12. * @since 2.9.0 
  13. */ 
  14. class WP_MatchesMapRegex { 
  15. /** 
  16. * store for matches 
  17. * 
  18. * @access private 
  19. * @var array 
  20. */ 
  21. private $_matches; 
  22.  
  23. /** 
  24. * store for mapping result 
  25. * 
  26. * @access public 
  27. * @var string 
  28. */ 
  29. public $output; 
  30.  
  31. /** 
  32. * subject to perform mapping on (query string containing $matches[] references 
  33. * 
  34. * @access private 
  35. * @var string 
  36. */ 
  37. private $_subject; 
  38.  
  39. /** 
  40. * regexp pattern to match $matches[] references 
  41. * 
  42. * @var string 
  43. */ 
  44. public $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number 
  45.  
  46. /** 
  47. * constructor 
  48. * 
  49. * @param string $subject subject if regex 
  50. * @param array $matches data to use in map 
  51. */ 
  52. public function __construct($subject, $matches) { 
  53. $this->_subject = $subject; 
  54. $this->_matches = $matches; 
  55. $this->output = $this->_map(); 
  56.  
  57. /** 
  58. * Substitute substring matches in subject. 
  59. * 
  60. * static helper function to ease use 
  61. * 
  62. * @static 
  63. * @access public 
  64. * 
  65. * @param string $subject subject 
  66. * @param array $matches data used for substitution 
  67. * @return string 
  68. */ 
  69. public static function apply($subject, $matches) { 
  70. $oSelf = new WP_MatchesMapRegex($subject, $matches); 
  71. return $oSelf->output; 
  72.  
  73. /** 
  74. * do the actual mapping 
  75. * 
  76. * @access private 
  77. * @return string 
  78. */ 
  79. private function _map() { 
  80. $callback = array($this, 'callback'); 
  81. return preg_replace_callback($this->_pattern, $callback, $this->_subject); 
  82.  
  83. /** 
  84. * preg_replace_callback hook 
  85. * 
  86. * @access public 
  87. * @param array $matches preg_replace regexp matches 
  88. * @return string 
  89. */ 
  90. public function callback($matches) { 
  91. $index = intval(substr($matches[0], 9, -1)); 
  92. return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' ); 
.