Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Blog 2 Cleanup Utility in case you mistakenly uninstalled Blog without first running the inbuilt Cleanup Utility
<?php
/**
*
* 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
*
* https://github.com/kongondo/Blog
* 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) {
$form->processInput($this->input->post);
$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(
'blog-posts',
'blog-categories',
'blog-tags',
'blog-comments',
'blog-widgets',
'blog-authors',
'blog-archives',
'blog-settings',
);
//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
unset($pages[0]);
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("parent.name=repeaters, 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(
'blog',
'blog-archives',
'blog-authors',
'blog-categories',
'blog-category',
'blog-comments',
'blog-links',
'blog-post',
'blog-posts',
'blog-recent-comments',
'blog-recent-posts',
'blog-recent-tweets',
'blog-tag',
'blog-tags',
'blog-widgets',
'blog-widget-basic',
'blog-settings',
'blog-basic',
'repeater_blog-links',
);
//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) {
unset($templates[5]);//blog-comments
unset($templates[9]);//blog-recent-comments
unset($templates[17]);//blog-basic
}
//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('templates')->delete($t);
}
wire('templates')->delete($t);
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) {
$fg->remove(wire('fields')->get('blog_body'));
}
$img = wire('fields')->get('blog_images');
if ($img !=null && $img->id) {
$fg->remove(wire('fields')->get('blog_images'));
}
//we also remove title since we added it BUT WE WONT BE DELETING IT!
$title = wire('fields')->get('title');
if ($title !=null && $title->id) {
$fg->remove(wire('fields')->get('title'));
}
$fg->save();
//array of blog fields. We'll use this to delete each, one by one as applicable
$fields = array(
'blog_body',
'blog_categories',
'blog_comments',
'blog_comments_view',
'blog_comments_max',
'blog_quantity',
'blog_date',
'blog_files',
'blog_headline',
'blog_href',
'blog_images',
'blog_links',
'blog_note',
'blog_summary',
'blog_tags',
'blog_small',
);
//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) {
unset($fields[2]);//blog_comments
unset($fields[3]);//blog_comments_view
unset($fields[4]);//blog_comments_max
}
//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');
if($r->id)$r->delete();
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(
'blog.php',
'blog-archives.php',
'blog-authors.php',
'blog-categories.php',
'blog-category.php',
'blog-comments.php',
'blog-links.php',
'blog-post.php',
'blog-posts.php',
'blog-recent-comments.php',
'blog-recent-posts.php',
'blog-recent-tweets.php',
'blog-side-bar.inc',//will only be present if demo template files were installed
'blog-tag.php',
'blog-tags.php',
'blog-main.inc',//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) {
unset($templateFiles[12]);//blog-side-bar.inc
unset($templateFiles[15]);//blog-main.inc
}*/
if ($this->commentsUse ==2) {
unset($templateFiles[5]);//blog-comments.php
unset($templateFiles[9]);//blog-recent-comments.php
}
$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";
$form->attr("id+name",'cleanup_form');
//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->addOptions($radioOptions);
$r->value = '';
$form->append($r);
//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'));
$form->append($s);
$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();
$cleanup->cleanUp($form);
}
//render form if nothing submitted yet
else {
$tokenName = $session->CSRF->getTokenName();
$tokenValue = $session->CSRF->getTokenValue();
$out = '';
$jquery = '';
$modules->get("JqueryCore");
foreach($config->scripts->unique() as $file) $jquery .= "\n\t<script type='text/javascript' src='$file'></script>";
$out .= <<<EOT
<!DOCTYPE html>
<html>
<head>
<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 {
height:100%;
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;}
</style>
<!-- Include jquery-->
$jquery
<script type="text/javascript">
$(document).ready(function(){
$('#confirm_agree').change(function(){
$('#settings').toggle($(this).is(':checked'));
});
$('#confirm_agree').trigger('change');
});
</script>
</head>
EOT;
$out .= '
<body>
<div id="container">
<h2 id="cleanup">Blog Cleanup Utility (External Version)</h2>
<div id="warning">
<h3>Warning</h3>
<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>
</ol>
</div>
<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>
<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">
</div>
</form>
</div>
</body>
</html>
';
echo $out;
}//end else render form if nothing submitted yet
exit;
}//end this user is Supersuser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment