Skip to content

Instantly share code, notes, and snippets.

@patvdleer
Created February 21, 2017 12:57
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 patvdleer/602e39944601409b6dbcb159521e9fc0 to your computer and use it in GitHub Desktop.
Save patvdleer/602e39944601409b6dbcb159521e9fc0 to your computer and use it in GitHub Desktop.
Magento clean cms blocks
<?php
require_once 'abstract.php';
class X043_Shell_CMSFixer extends Mage_Shell_Abstract {
public function getCollection() {
return Mage::getModel('cms/block')->getCollection();
}
protected function _getPattern($url) {
$_url = str_replace('/', '\/', str_replace('.', '\.', rtrim($url, '/')));
return array(
array(
str_replace('{{BASE_URL}}', $_url, '/"{{BASE_URL}}\/media\/(.+?)"/i'),
str_replace('{{BASE_URL}}', $_url, '/"{{BASE_URL}}\/skin\/(?:.+?)\/(?:.+?)\/(?:.+?)\/(.+?)"/i'),
# str_replace('{{BASE_URL}}', $_url, '/"{{BASE_URL}}"/i')
),
array(
'"{{media url="$1"}}"',
'"{{skin url="$1"}}"',
# '"{{store url=""}}"'
)
);
}
/**
* Run script
*
* @return void
*/
public function run() {
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
setlocale(LC_ALL, 'en_US.UTF-8');
$use_ssl = True;
$envs = array(
'dev',
'test',
'staging'
);
$base_urls = array();
foreach (Mage::app()->getStores() as $_storeId => $_) {
$base_urls[] = Mage::app()->getStore($_storeId)->getBaseUrl();
}
if($use_ssl) {
$_base_urls = array();
foreach($base_urls as $base_url) {
$_base_urls[] = str_replace('https://', 'http://', $base_url);
}
$base_urls = array_merge($base_urls, $_base_urls);
}
$base_urls = array_unique($base_urls);
$_base_urls = array();
foreach($base_urls as $base_url) {
foreach($envs as $env) {
$_base_urls[] = str_replace('www', $env, $base_url);
}
}
$base_urls = array_unique(array_merge($base_urls, $_base_urls));
foreach($this->getCollection() as $block) {
$_block = Mage::getModel('cms/block')->load($block->getId());
$_content = $_block->getData('content');
foreach($base_urls as $base_url) {
list($pattern, $replacement) = $this->_getPattern($base_url);
$_content = preg_replace($pattern, $replacement, $_content);
}
if($_block->getData('content') != $_content) {
$_block->setData('content', $_content);
$_block->save();
}
}
}
}
$shell = new X043_Shell_CMSFixer();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment