Source of file StaticSiteLinkRewriter.php
Size: 1,817 Bytes - Last Modified: 2021-12-23T10:34:50+00:00
/var/www/docs.ssmods.com/process/src/code/StaticSiteLinkRewriter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 | <?php require_once(dirname(__FILE__) . "/../thirdparty/phpQuery/phpQuery/phpQuery.php"); /** * Helper class for rewriting links using phpQuery. */ class StaticSiteLinkRewriter { protected $tagMap = array( 'a' => array('href'), 'img' => array('src'), ); protected $callback; public function __construct($callback) { $this->callback = $callback; } /** * Set a map of tags & attributes to search for URls. * * Each key is a tagname, and each value is an array of attribute names. */ public function setTagMap($tagMap) { $this->tagMap = $tagMap; } /** * Return the tagmap */ public function getTagMap($tagMap) { $this->tagMap = $tagMap; } /** * Rewrite URLs in a PHPQuery object. The content of the object will be modified. * * @param phpQuery $pq The content containing the links to rewrite */ public function rewriteInPQ($pq) { $callback = $this->callback; // Make URLs absolute foreach ($this->tagMap as $tag => $attributes) { foreach ($pq[$tag] as $tagObj) { foreach ($attributes as $attribute) { if ($url = pq($tagObj)->attr($attribute)) { $newURL = $callback($url); pq($tagObj)->attr($attribute, $newURL); } } } } } /** * Rewrite URLs in the given content snippet. Returns the updated content. * * @param phpQuery $pq The content containing the links to rewrite */ public function rewriteInContent($content) { $pq = phpQuery::newDocument($content); $this->rewriteInPQ($pq); return $pq->html(); } } |