Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
Blog 2 Cleanup Utility in case you mistakenly uninstalled Blog without first running the inbuilt Cleanup Utility
* CONTEXT: This is a utility class for 'cleaning-up' Blog in case you uninstalled Blog without first running its inbuilt Cleanup utility.
* WARNING: The utility will irreversibly delete the following Blog Components
* Fields (blog_xxx)
* Templates (blog-xxx)
* Optionally Template Files (in case you installed the blank/demo Template Files) (blog-xxx.php/inc)
* Pages
* Role (blog-author)
* @author Kongondo
* @version 1
* Created February 2014
* Modified for the above CONTEXT January 2015
class BlogCleanup extends WireData {
//blog Style that was installed
private $blogStyle;
//whether comments feature was installed
private $commentsUse;
//whether demo/blank template files were installed
private $templateFilesInstall;
//whether to remove template files.
private $removeTplFiles;
* Prepare cleaning up.
* @access public
public function cleanUp($form) {
$cleanupBtn = $this->input->post->cleanup_btn;
$cleanupAgree = $this->input->post->confirm_agree;
$this->blogStyle = $this->input->post->blog_style;
$this->commentsUse = $this->input->post->comments_use;
$this->removeTplFiles = $this->input->post->remove_tpl_files;
$this->templateFilesInstall = $this->input->post->tpl_files_installed;
//was the right button clicked
if($cleanupBtn && $cleanupBtn == 'Cleanup' && $cleanupAgree) {
if(!$this->blogStyle) exit('<h2>'. $this->_('You must specify a Blog Style!') . '</h2>');
if(!$this->commentsUse) exit('<h2>'. $this->_('You must specify if you had Comments Feature enabled!') . '</h2>');
if(!$this->templateFilesInstall) exit('<h2>'. $this->_('You must specify if you installed the Demo/Blank Blog Template Files!') . '</h2>');
if(!$this->removeTplFiles) exit('<h2>'. $this->_('You must specify if you wish to delete Blog Template Files!') . '</h2>');
return $this->cleanUpPages();
* Delete blog pages.
* @access private
private function cleanUpPages() {
//grab the main blog parent pages IDs where blogStyle == 3.
//Similar for if blogStyle == 4 but we'll unset 'blog-posts' in that case
$pages = array(
//if blogStyle == 1 or blogStyle == 2: 'blog' is main parent of all blog pages
if ($this->blogStyle == 1 || $this->blogStyle == 2) {
//since no blog config data in this CONTEXT, we don't know if the 'Blog' page was renamed
//we can only grab it by its template
$p = wire('pages')->get('template=blog');
//we only proceed if we found the page /blog/
//recursively delete the blog page - i.e., including its children
if ($p->id) wire('pages')->delete($p, true);
//if blogStyle == 3: root is the main parent of all blog pages
elseif ($this->blogStyle == 3) {
foreach ($pages as $page) {
//since not Blog settings we can only get them by page
$p = wire('pages')->get("template=" . $page);
//recursively delete the blog pages - i.e., including their children
if ($p->id) wire('pages')->delete($p, true);
//if blogStyle == 4: root is the main parent of all blog pages but 'blog-post' pages live directly under root
elseif ($this->blogStyle == 4) {
//remove 'blog-posts' since that page does not exist in this case
foreach ($pages as $page) {
//since not Blog settings we can only get them by page
$p = wire('pages')->get("template=" . $page);
//recursively delete the blog pages - i.e., including their children
if ($p->id) wire('pages')->delete($p, true);
//additionally in this case since parent of each blog post is root,
//we delete all pages using the template 'blog-post'
foreach (wire('pages')->find('template=blog-post, include=all') as $p) {if ($p->id) wire('pages')->delete($p);}
return $this->cleanUpRepeater();
* Delete repeater page.
* @access private
private function cleanUpRepeater() {
//we delete our repeater page: admin/repeaters/for-field-xxx
$r = wire('fields')->get('blog_links');
if($r !=null && $r->id) {
$repeaterID = wire('fields')->get('blog_links')->id;
$repeaterPage = wire('pages')->get(", name=for-field-$repeaterID");//making sure we are getting the right page
if($repeaterPage->id) wire('pages')->delete($repeaterPage);
return $this->cleanUpTemplates();
* Delete blog templates.
* @access private
private function cleanUpTemplates() {
$templates = array(
//unset irrelevant templates depending on blogStyle (important since user could have a template with similar name that is not part of Blog!) AND commentsUse
if ($this->blogStyle == 3 || $this->blogStyle == 4) unset($templates[0]);//blog template
if ($this->blogStyle == 2 || $this->blogStyle == 4) unset($templates[8]);//blog-posts template
if ($this->commentsUse ==2) {
//delete each found template one by one
foreach ($templates as $template) {
$t = wire('templates')->get($template);
if ($t !=null && $t->id) {
//two step process to delete system template (repeater_blog-links)
if ($t->flags == 8) {
$t->flags = Template::flagSystemOverride;
$t->flags = 0;
wire('fieldgroups')->delete($t->fieldgroup);//delete the associated fieldgroups
return $this->cleanUpFields();
* Delete blog fields.
* @access private
private function cleanUpFields() {
//remove some fields from 'user' template before deleting them. The fields were added by ProcessBlog during install
$t = wire('templates')->get('user');
$fg = $t->fieldgroup;
$b = wire('fields')->get('blog_body');
if ($b !=null && $b->id) {
$img = wire('fields')->get('blog_images');
if ($img !=null && $img->id) {
//we also remove title since we added it BUT WE WONT BE DELETING IT!
$title = wire('fields')->get('title');
if ($title !=null && $title->id) {
//array of blog fields. We'll use this to delete each, one by one as applicable
$fields = array(
//unset irrelevant fields depending on commentsUse (important since user could have a field with similar name that is not part of Blog!)
if ($this->commentsUse ==2) {
//delete each found field
foreach ($fields as $field) {
$f = wire('fields')->get($field);
if($f !=null && $f->id) wire('fields')->delete($f);
return $this->cleanUpRoles();
* Delete blog role.
* @access private
private function cleanUpRoles() {
//delete 'blog-author' role
$r = wire('roles')->get('blog-author');
return $this->cleanUpTemplateFiles();
* Delete blog template files (optional).
* @access private
private function cleanUpTemplateFiles() {
$this->deleteTf = false;
//if user has chosen to also delete template files AND these were installed (blank or demo)
if ($this->removeTplFiles == 1 && $this->templateFilesInstall == 1) {
$this->deleteTf = true;
$templateFiles = array(
'',//will only be present if demo template files were installed
'',//will only be present if demo template files were installed
//remove non-existent template files based on the blogStyle, commentsUse and templateFilesInstall
//also safeguards againts removing user created template files with similar names!
if ($this->blogStyle == 2 || $this->blogStyle == 4) unset($templateFiles[8]);//blog-posts.php
if ($this->blogStyle == 3 || $this->blogStyle == 4) unset($templateFiles[0]);//blog.php
/*if ($this->templateFilesInstall !=2) {
if ($this->commentsUse ==2) {
$sourcepath = wire('config')->paths->templates;//source: '/site/templates/'
foreach ($templateFiles as $tf) {
if(is_file($sourcepath . $tf)) unlink($tf);//we delete the template file
exit('<h2>Clean-up complete. Delete this file/Remove it from your Template File.</h2>');
}//END CLASS BlogCleanup
//only show form to superusers + only run code for superusers
if ($user->isSuperuser()) {
//create a new form field
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
//radios: blog styles selection
$r = $modules->get("InputfieldRadios");
$r->attr('id+name', 'blog_style');
$r->label = __('Select the Blog URL style you had installed');
$radioOptions = array (
1 => __('Style 1'),
2 => __('Style 2'),
3 => __('Style 3'),
4 => __('Style 4'),
$r->value = '';
//submit button
$s = $modules->get('InputfieldSubmit');
$s->attr('id+name', 'cleanup_btn');
$s->class .= " cleanup";//add a custom class to this submit button
$s->attr('value', __('Cleanup'));
$post = $input->post;
//if form submitted and is validated, send input->post values to execute cleanup of blog components: fields, templates, template files, pages and role;
if($post->cleanup_btn && $session->CSRF->validate()) {
$cleanup = new BlogCleanup();
//render form if nothing submitted yet
else {
$tokenName = $session->CSRF->getTokenName();
$tokenValue = $session->CSRF->getTokenValue();
$out = '';
$jquery = '';
foreach($config->scripts->unique() as $file) $jquery .= "\n\t<script type='text/javascript' src='$file'></script>";
$out .= <<<EOT
<!DOCTYPE html>
<title>Blog Cleanup</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style type="text/css">
htlm, body {background: #1C2836;}
div#container {
min-height: 985px;
width: 90%;
margin: 0 auto;
background: #1C2836;
padding: 0.5em 2em;
color: #FFF;
line-height: 1.5;
font-size: 1.125em;
font-family: Arial, Georgia, sans-serif;
h2#cleanup {color: #FFF;}
div#settings {display: none;}
div#settings input[type="radio"] {float: left;}
label.settings {
display: block;
margin: 10px 0 5px;
color: #F86507;
font-weight: bold;
span.option_title {display: block;}
input#cleanup_btn {
margin-top: 10px;
background: none repeat scroll 0 0 #F86507;
border: 1px solid #F86507;
border-radius: 5px;
color: #fff;
font-size: 1em !important;
font-weight: bold;
padding: 0.6em 1.1em;
display: inline-block;
float: right;
cursor: pointer;
input#cleanup_btn:hover {color: #1C2836;}
ol#warning_list {padding: 0 0 0 0.8em; margin: 0;}
ol#warning_list li {font-size: 0.875em; padding-bottom: 2px;}
ol#warning_list li a {color: #FFF3A1; text-decoration: none;}
<!-- Include jquery-->
<script type="text/javascript">
$out .= '
<div id="container">
<h2 id="cleanup">Blog Cleanup Utility (External Version)</h2>
<div id="warning">
<ol id="warning_list">
<li>Only use this utility if you mistakenly uninstalled the Blog Module before using its in-built Cleanup Utility.</li>
<li>The utility will irreversibly delete your leftover Blog Components (fields, templates, role, pages and optionally template files).</li>
<li>Your Blog settings are no longer available, hence Blog pages will be searched for using their default Blog templates.</li>
<li>If you have other non-blog pages using those templates, they too will be deleted.</li>
<li>You need to make sure your Blog fields are not in use by other non-Blog templates otherwise ProcessWire will throw an error.</li>
<li>You will need to specify the Blog Style you had installed.</li>
<li>This utility is provided as is without any guarantees implicit or otherwise. Use at your own risk!</li>
<li>If you do not know what you are doing, exit <a href="' . $pages->get('/')->url . '">now</a>.</li>
<form action="./" method="post" name="frm">
<div id="confirm">
<input type="hidden" id="cal_post_token" name="' . $tokenName . '" value="' . $tokenValue . '">
<label class="settings" for="confirm_agree">Tick box to confirm you have read and understood the above and still want to proceed.</label>
<input type=checkbox id="confirm_agree" name="confirm_agree" value="1">
<div id="settings">
<h3>All questions need to be answered</h3>
<label for="blog_style" class="settings">What was your installed Blog Style?</label>
<input type="radio" name="blog_style" value="1" id="blog_style">
<span class="option_title">Blog Style 1 (/blog/posts/example-post/)</span>
<input type="radio" name="blog_style" value="2">
<span class="option_title">Blog Style 2 (/blog/example-post/)</span>
<input type="radio" name="blog_style" value="3">
<span class="option_title">Blog Style 3 (/posts/example-post/)</span>
<input type="radio" name="blog_style" value="4">
<span class="option_title">Blog Style 4 (/example-post/)</span>
<label for="comments_use" class="settings">Was the Commenting Feature installed?</label>
<input type="radio" name="comments_use" id="comments_use" value="1">
<span class="option_title">Yes</span>
<input type="radio" name="comments_use" value="2">
<span class="option_title">No</span>
<label for="tpl_files_installed" class="settings">Did you install either the Blank or Demo Template Files?</label>
<input type="radio" name="tpl_files_installed" id="tpl_files_installed" value="1">
<span class="option_title">Yes</span>
<input type="radio" name="tpl_files_installed" value="2">
<span class="option_title">No</span>
<label for="remove_tpl_files" class="settings">Do you wish to delete those Template Files Blog installed?</label>
<input type="radio" name="remove_tpl_files" id="remove_tpl_files" value="1">
<span class="option_title">Yes</span>
<input type="radio" name="remove_tpl_files" value="2">
<span class="option_title">No</span>
<input type="submit" value="Cleanup" name="cleanup_btn" id="cleanup_btn">
echo $out;
}//end else render form if nothing submitted yet
}//end this user is Supersuser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment