212 // Got a match. |
212 // Got a match. |
213 $this->matched_rule = $match; |
213 $this->matched_rule = $match; |
214 |
214 |
215 // Trim the query of everything up to the '?'. |
215 // Trim the query of everything up to the '?'. |
216 $query = preg_replace("!^.+\?!", '', $query); |
216 $query = preg_replace("!^.+\?!", '', $query); |
217 |
217 |
218 // Substitute the substring matches into the query. |
218 // Substitute the substring matches into the query. |
219 $query = addslashes(WP_MatchesMapRegex::apply($query, $matches)); |
219 $query = addslashes(WP_MatchesMapRegex::apply($query, $matches)); |
220 |
220 |
221 $this->matched_query = $query; |
221 $this->matched_query = $query; |
222 |
222 |
1592 } |
1592 } |
1593 } |
1593 } |
1594 |
1594 |
1595 /** |
1595 /** |
1596 * Helper class to remove the need to use eval to replace $matches[] in query strings. |
1596 * Helper class to remove the need to use eval to replace $matches[] in query strings. |
1597 * |
1597 * |
1598 * @since 2.9.0 |
1598 * @since 2.9.0 |
1599 */ |
1599 */ |
1600 class WP_MatchesMapRegex { |
1600 class WP_MatchesMapRegex { |
1601 /** |
1601 /** |
1602 * store for matches |
1602 * store for matches |
1603 * |
1603 * |
1604 * @access private |
1604 * @access private |
1605 * @var array |
1605 * @var array |
1606 */ |
1606 */ |
1607 var $_matches; |
1607 var $_matches; |
1608 |
1608 |
1609 /** |
1609 /** |
1610 * store for mapping result |
1610 * store for mapping result |
1611 * |
1611 * |
1612 * @access public |
1612 * @access public |
1613 * @var string |
1613 * @var string |
1614 */ |
1614 */ |
1615 var $output; |
1615 var $output; |
1616 |
1616 |
1617 /** |
1617 /** |
1618 * subject to perform mapping on (query string containing $matches[] references |
1618 * subject to perform mapping on (query string containing $matches[] references |
1619 * |
1619 * |
1620 * @access private |
1620 * @access private |
1621 * @var string |
1621 * @var string |
1622 */ |
1622 */ |
1623 var $_subject; |
1623 var $_subject; |
1624 |
1624 |
1625 /** |
1625 /** |
1626 * regexp pattern to match $matches[] references |
1626 * regexp pattern to match $matches[] references |
1627 * |
1627 * |
1628 * @var string |
1628 * @var string |
1629 */ |
1629 */ |
1630 var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number |
1630 var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number |
1631 |
1631 |
1632 /** |
1632 /** |
1633 * constructor |
1633 * constructor |
1634 * |
1634 * |
1635 * @param string $subject subject if regex |
1635 * @param string $subject subject if regex |
1636 * @param array $matches data to use in map |
1636 * @param array $matches data to use in map |
1637 * @return self |
1637 * @return self |
1638 */ |
1638 */ |
1639 function WP_MatchesMapRegex($subject, $matches) { |
1639 function WP_MatchesMapRegex($subject, $matches) { |
1640 $this->_subject = $subject; |
1640 $this->_subject = $subject; |
1641 $this->_matches = $matches; |
1641 $this->_matches = $matches; |
1642 $this->output = $this->_map(); |
1642 $this->output = $this->_map(); |
1643 } |
1643 } |
1644 |
1644 |
1645 /** |
1645 /** |
1646 * Substitute substring matches in subject. |
1646 * Substitute substring matches in subject. |
1647 * |
1647 * |
1648 * static helper function to ease use |
1648 * static helper function to ease use |
1649 * |
1649 * |
1650 * @access public |
1650 * @access public |
1651 * @param string $subject subject |
1651 * @param string $subject subject |
1652 * @param array $matches data used for subsitution |
1652 * @param array $matches data used for subsitution |
1653 * @return string |
1653 * @return string |
1654 */ |
1654 */ |
1655 function apply($subject, $matches) { |
1655 function apply($subject, $matches) { |
1656 $oSelf =& new WP_MatchesMapRegex($subject, $matches); |
1656 $oSelf =& new WP_MatchesMapRegex($subject, $matches); |
1657 return $oSelf->output; |
1657 return $oSelf->output; |
1658 } |
1658 } |
1659 |
1659 |
1660 /** |
1660 /** |
1661 * do the actual mapping |
1661 * do the actual mapping |
1662 * |
1662 * |
1663 * @access private |
1663 * @access private |
1664 * @return string |
1664 * @return string |
1665 */ |
1665 */ |
1666 function _map() { |
1666 function _map() { |
1667 $callback = array(&$this, 'callback'); |
1667 $callback = array(&$this, 'callback'); |
1668 return preg_replace_callback($this->_pattern, $callback, $this->_subject); |
1668 return preg_replace_callback($this->_pattern, $callback, $this->_subject); |
1669 } |
1669 } |
1670 |
1670 |
1671 /** |
1671 /** |
1672 * preg_replace_callback hook |
1672 * preg_replace_callback hook |
1673 * |
1673 * |
1674 * @access public |
1674 * @access public |
1675 * @param array $matches preg_replace regexp matches |
1675 * @param array $matches preg_replace regexp matches |
1676 * @return string |
1676 * @return string |
1677 */ |
1677 */ |
1678 function callback($matches) { |
1678 function callback($matches) { |
1679 $index = intval(substr($matches[0], 9, -1)); |
1679 $index = intval(substr($matches[0], 9, -1)); |
1680 return ( isset( $this->_matches[$index] ) ? $this->_matches[$index] : '' ); |
1680 return ( isset( $this->_matches[$index] ) ? $this->_matches[$index] : '' ); |
1681 } |
1681 } |
1682 |
1682 |
1683 } |
1683 } |
1684 |
1684 |
1685 ?> |
1685 ?> |