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