Skip to content

Instantly share code, notes, and snippets.

@AgelxNash
Created October 23, 2012 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgelxNash/3939722 to your computer and use it in GitHub Desktop.
Save AgelxNash/3939722 to your computer and use it in GitHub Desktop.
StaticUrl plugin for MODX Revo
<?php
/**************************************/
/** StaticUrl plugin for MODX Revo
*
* @version 1.2
* @author Borisov Evgeniy aka Agel Nash (agel-nash@xaker.ru)
* @date 23.10.2012
*
* @category plugin
* @internal @event OnWebPagePrerender
* @internal @properties &StaticNewUrl=CDN URL;integer;1
* @internal @modx_category HTML-code
*
*
*
* INSTALL
* 3) Создать параметр к плагину с именем StaticNewUrl
* - Значение параметра: URL домена с которого будет грузиться статика
* - Тип ввода: текст
*/
/*************************************/
if($modx->event->name=='OnWebPagePrerender'){
$html = &$modx->resource->_output;
$replaceD=array();
preg_match_all('#src=(?:"|\')(.*?)>#',$html,$matches);
foreach($matches[1] as $item){
if(substr($item,0,1)!='/'){
continue;
}
if(substr($item,0,2)!='//'){
$replaceD[md5($item)]=$item;
}
}
preg_match_all('#<link.*?href=(?:"|\')(.*?)>#',$html,$matches);
foreach($matches[1] as $item){
if(substr($item,0,1)!='/'){
continue;
}
if(substr($item,0,2)!='//' && substr($item,0,12)!='/favicon.ico'){
$replaceD[md5($item)]=$item;
}
}
array_unique($replaceD);
foreach($replaceD as $item){
$html=str_replace($item,$StaticNewUrl.substr($item,1),$html);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment