Skip to content

Instantly share code, notes, and snippets.

@fiftin
Last active September 10, 2015 08:40
Show Gist options
  • Save fiftin/19edfa83869d362f1c47 to your computer and use it in GitHub Desktop.
Save fiftin/19edfa83869d362f1c47 to your computer and use it in GitHub Desktop.
Peplace "button" class in the WooCommerce templates to "btn btn-default" class. Useful if you can use Bootstrap Theme.
<?php
function ends_with($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
function get_all_files($directory) {
if (!is_dir($directory)) {
throw new Exception("Is not a directory: $directory");
}
$dir = opendir($directory);
if (!$dir) {
throw new Exception("Unknown problem. Directory: $directory");
}
$ret = [];
while (($file = readdir($dir)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
if (!ends_with($directory, '/')) {
$directory .= '/';
}
$file_name = $directory . $file;
if (is_dir($file_name)) {
$ret = array_merge($ret, get_all_files($directory . $file));
} else {
$ret[] = $directory . $file;
}
}
return $ret;
}
define('PATTERN', '/\sclass="(([^"]*)\s)?(button)([^"]*)"/');
define('PEPLACEMENT', ' class="$1btn btn-default$4"');
echo "Woocommerce Template Button Patcher\n";
echo "Directory: {$argv[1]}\n";
$files = get_all_files($argv[1]);
foreach ($files as $file) {
echo "File: $file";
$content = file_get_contents($file);
$count = 0;
$new_content = preg_replace(PATTERN, PEPLACEMENT, $content, -1, $count);
if ($count > 0) {
echo " REPLACED";
file_put_contents($file, $new_content);
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment