Where do I start... let me count the ways.
function update_GTour_theme_files()
{
$new_update = file_get_contents(__DIR__ . "/../../themes/grandtour/header.php");
$new_update = preg_replace('/$page_menu_transparent = 1/','$page_menu_transparent = 0',$new_update);
$new_update = preg_replace('/$grandtour_page_menu_transparent = 1/','$grandtour_page_menu_transparent = 0',$new_update);
file_put_contents (__DIR__ . "/../../themes/grandtour/header.php", $new_update);
if ( file_exists (__DIR__ . "/../../themes/grandtour/header.php") && is_writable (__DIR__ . "/../../themes/grandtour/header.php") ){
update_GTour_theme_files();
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}
else {
echo '</br><span style="color:red;font-weight:bold;">Error occured while applying the changes.</span>';
}
}
Lets see when you run this it does:
file_put_contents (__DIR__ . "/../../themes/grandtour/header.php", $new_update);
Then it checks:
if ( file_exists (__DIR__ . "/../../themes/grandtour/header.php") && is_writable (__DIR__ . "/../../themes/grandtour/header.php") ){
update_GTour_theme_files();
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}
Which is obviously true, or we would have gotten some errors already. So that is basically always true. Which means you call this update_GTour_theme_files();
itself again. Repeat the above steps a infinite number of times.
So that is obviously wrong. If you are calling this I would expect your browser to lockup.
So lets fix this up (single file):
function update_GTour_theme_files($file)
{
//fail early
if (!file_exists ($file) || !is_writable ($file) ) die("File $file Does not exists or is not writable");
$new_update = file_get_contents($file);
$new_update = preg_replace('/\$page_menu_transparent\s*=\s*1;/','$page_menu_transparent = 0;',$new_update);
$new_update = preg_replace('/\$grandtour_page_menu_transparent\s*=\s*1;/','$grandtour_page_menu_transparent = 0;',$new_update);
if(file_put_contents ($file, $new_update)){
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}else{
echo '</br><span style="color:red;font-weight:bold;">Error occured while applying the changes.</span>';
}
}
update_GTour_theme_files(__DIR__ . "/../../themes/grandtour/header.php");
This will only update the one file, to do more then that you need to use scandir
, glob
or SPL DirectoryIterator / FilesystemIterator
.
PS your "main" or "big" problem (besides the recursion) is right here:
$new_update = preg_replace('/$page_menu_transparent = 1/','$page_menu_transparent = 0',$new_update);
$new_update = preg_replace('/$grandtour_page_menu_transparent = 1/','$grandtour_page_menu_transparent = 0',$new_update);
These $
in /$page_menu_transparent
are not escaped so the are treated as REGEX. Which means they match the end of the string, which makes no sense. I also added some vaiable space \s*=\s*
and the ;
semi-colon otherwise $page_menu_transparent = 1345;
will become $page_menu_transparent = 0;
. This may have some impact if it's in ()
or an array etc. (anything without the ;
)
For all files in a given folder and it's subfolders
function update_GTour_theme_files($dir)
{
if (!file_dir($dir) || is_writable ($dir) ) die("Dir $dir Does not exists or is not writable");
$Iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$dir,
RecursiveDirectoryIterator::SKIP_DOTS|RecursiveDirectoryIterator::UNIX_PATHS
)
);
foreach($Iterator as $fileInfo){
if($fileInfo->isDir() || $fileInfo->getExtension() != 'php') continue;
$file = $fileInfo->getPathname();
$new_update = file_get_contents($file);
$new_update = preg_replace('/\$page_menu_transparent\s*=\s*1;/','$page_menu_transparent = 0;',$new_update);
$new_update = preg_replace('/\$grandtour_page_menu_transparent\s*=\s*1;/','$grandtour_page_menu_transparent = 0;',$new_update);
if(file_put_contents ($file, $new_update)){
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}else{
echo '</br><span style="color:red;font-weight:bold;">Error occured while applying the changes.</span>';
}
}
}
update_GTour_theme_files(__DIR__ . "/../../themes/grandtour/");
This uses RecursiveDirectoryIterator
so it should look through all sub folders.
But this is all untested so be very careful. If you mess your files up don't come blaming me, you were warned.
That said Enjoy~