Commit efdee867 authored by Dave Lane's avatar Dave Lane
Browse files

add an admin function to remove the accidentally created BFF pages on subsites....

parent 2a7473a8
...@@ -65,7 +65,7 @@ define('BFF_ADMIN_STYLE', 'bff-admin-style'); ...@@ -65,7 +65,7 @@ define('BFF_ADMIN_STYLE', 'bff-admin-style');
// other useful parameters // other useful parameters
define('BFF_MAX_FILE_READ_CHAR', 1000000); // limit max size of the file to read to 1mb define('BFF_MAX_FILE_READ_CHAR', 1000000); // limit max size of the file to read to 1mb
// turn on debugging with true, off with false // turn on debugging with true, off with false
define('BFF_DEBUG', false); define('BFF_DEBUG', true);
define('LOG_STREAM', getenv('LOG_STREAM')); define('LOG_STREAM', getenv('LOG_STREAM'));
// include the dependencies // include the dependencies
......
...@@ -26,32 +26,38 @@ class BFFForm extends BFFCourse { ...@@ -26,32 +26,38 @@ class BFFForm extends BFFCourse {
// this starts everything... // this starts everything...
public function init() { public function init() {
$this->log('in init'); if (get_current_blog_id() == 1) {
// register actions $this->log('in init');
add_shortcode(BFF_SHORTCODE, array($this, 'shortcode')); // register actions
// allows us to add a class to our post add_shortcode(BFF_SHORTCODE, array($this, 'shortcode'));
add_filter('body_class', array($this, 'add_post_class')); // allows us to add a class to our post
add_filter('post_class', array($this, 'add_post_class')); add_filter('body_class', array($this, 'add_post_class'));
// and create the post to hold short code... add_filter('post_class', array($this, 'add_post_class'));
$this->create_post(BFF_SLUG); if (get_current_blog_id() == 1 && !$this->get_bff_page_for_site(1)) {
$this->log('setting up scripts'); // and create the post to hold short code...
// add the ajax handlers $this->log("We're on the default blog and no BFF page exists, so we're creating one.");
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts')); $this->create_post(BFF_SLUG);
// this enables the feedfinder service for authenticated users... }
add_action('wp_ajax_bff_submit', array($this, 'ajax_submit'));
// this allows users who aren't authenticated to use the feedfinder $this->log('setting up scripts');
add_action('wp_ajax_nopriv_bff_submit', array($this, 'ajax_submit')); // add the ajax handlers
// this enables the setblogfeed service for authenticated users... add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_ajax_bff_set', array($this, 'ajax_set')); // this enables the feedfinder service for authenticated users...
// stuff related to the WEnotes plugin... add_action('wp_ajax_bff_submit', array($this, 'ajax_submit'));
//add_action('wenotes_update_feed', array($this, 'update_user_feed_meta'), 10, 4); // this allows users who aren't authenticated to use the feedfinder
add_action('wenotes_remove_feed', array($this, 'cleanup_user_feed_meta'), 10, 2); add_action('wp_ajax_nopriv_bff_submit', array($this, 'ajax_submit'));
// Admin Settings // this enables the setblogfeed service for authenticated users...
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts')); add_action('wp_ajax_bff_set', array($this, 'ajax_set'));
add_action('network_admin_menu', array($this, 'multisite_menu')); // stuff related to the WEnotes plugin...
add_action('wp_ajax_bff_settings_submit', array($this, 'ajax_settings_submit')); //add_action('wenotes_update_feed', array($this, 'update_user_feed_meta'), 10, 4);
add_action('wenotes_remove_feed', array($this, 'cleanup_user_feed_meta'), 10, 2);
$this->log('finished setting up scripts'); // Admin Settings
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
add_action('network_admin_menu', array($this, 'multisite_menu'));
add_action('wp_ajax_bff_settings_submit', array($this, 'ajax_settings_submit'));
add_action('wp_ajax_bff_cleanup_submit', array($this, 'ajax_cleanup_submit'));
$this->log('finished setting up scripts');
}
} }
public function enqueue_scripts() { public function enqueue_scripts() {
...@@ -106,6 +112,86 @@ class BFFForm extends BFFCourse { ...@@ -106,6 +112,86 @@ class BFFForm extends BFFCourse {
$this->ajax_response(array('success'=> $this->save_settings())); $this->ajax_response(array('success'=> $this->save_settings()));
exit; exit;
} }
// called when the ajax form is successfully submitted
public function ajax_cleanup_submit() {
$this->log('in ajax_cleanup_submit: '.print_r($_POST, true));
// check if the submitted nonce matches the generated nonce created in the auth_init functionality
if (! wp_verify_nonce( $_POST['nonce'], 'bff-admin-nonce')) {
die (__("Someone is using this form without permission!", 'oeru-bff'));
}
// generate the response
header( "Content-Type: application/json" );
$this->ajax_response(array('success'=> $this->cleanup()));
exit;
}
public function cleanup() {
$this->log('cleanup: '. print_r($_POST, true));
// find if there are any extra BFF pages
// Get a list of subsites (including the default blog, 1)
$sites = wp_get_sites();
$site_ids = array();
foreach ($sites as $site) {
if ($site['public'] == 1 && $site['archived'] == 0) {
//$this->log('adding site '.$site['path'].' ('.$site['blog_id'].')');
$site_ids[] = $site['blog_id'];
} else {
$this->log('rejecting site '.$site['path'].' ('.$site['blog_id'].')');
}
}
$this->log('found sites: '.print_r($site_ids, true));
$msg = '';
if (count($site_ids)) {
$found = array();
$not_found = array();
$deleted = array();
foreach($site_ids as $site_id) {
if ($this->get_bff_page_for_site($site_id)) {
$found[] = $site_id;
//$this->log("Found a BFF page for site ".$site_id);
} else {
$not_found[] = $site_id;
//$this->log("Didn't find a BFF page for site ".$site_id);
}
}
if (count($found)) {
$this->log('BFF-equipped sites: '.print_r($found, true));
foreach($found as $sid) {
if ($sid != 1) {
// we have to make sure we're in the right site context!
switch_to_blog($sid);
$post_id = $this->get_bff_page_for_site($sid);
$removed = wp_delete_post($post_id, true);
restore_current_blog();
if ($removed != false || $removed != null) {
$this->log('Successfully deleted old post '.$post_id.' from site ('.$sid.')...');
$deleted[] = $post_id.' ('.$sid.')';
} else {
$this->log('Failed to fully delete old post '.$post_id.'('.$sid.')...');
}
}
}
if (count($deleted)) {
$list = join(',', $deleted);
$msg = sprintf(__(' Deleted unnecessary BFF pages with the following ids: $s', 'oeru-bff'), $list);
}
}
if (count($not_found)) {
$this->log('Sites without a BFF page: '.print_r($not_found, true));
}
}
//$this->log('sites '. print_r($sites, true));
// and check each for the
// translators: %s = the name of this plugin, BFF or Blog Feed Finder by default
$message = __('Finished cleanup!', 'oeru-bff').$msg;
$this->ajax_response(array(
'success' => true,
'message' => $message
));
return true;
}
public function save_option($sid, $option, $val) { public function save_option($sid, $option, $val) {
$this->log('saving option '.$option.' = '.$val.' for site '.$sid.'.'); $this->log('saving option '.$option.' = '.$val.' for site '.$sid.'.');
...@@ -288,7 +374,14 @@ class BFFForm extends BFFCourse { ...@@ -288,7 +374,14 @@ class BFFForm extends BFFCourse {
<input type="submit" id="bff-submit" class="button-primary" value="<?php echo __('Save Changes', 'oeru-bff'); ?>" /> <input type="submit" id="bff-submit" class="button-primary" value="<?php echo __('Save Changes', 'oeru-bff'); ?>" />
</p> </p>
<p id="bff-status" style="color: red">&nbsp;</p> <p id="bff-status" style="color: red">&nbsp;</p>
<p class="submit">
<span class="description"><?php echo __("Due to a coding problem, a past release of this plugin created extra 'Blog Feed Finder' pages for each subsite/course. These don't do any harm, but they clutter things up and should be cleaned up. Click here to clean them up.", 'oeru-bff'); ?></span>
<input type="submit" id="bff-cleanup" class="button-primary" value="<?php echo __('Clean up', 'oeru-bff'); ?>" />
</p>
</form> </form>
<?php <?php
} }
...@@ -428,16 +521,40 @@ class BFFForm extends BFFCourse { ...@@ -428,16 +521,40 @@ class BFFForm extends BFFCourse {
<?php <?php
} }
// implicitly checks the current site unless a site is specified
// returns false if no BFF page is found.
public function get_bff_page_for_site($site_id = false) {
$post_id = false;
// switch to the default site.
if ($site_id) {
$this->log('checking for BFF page on site '.$site_id);
switch_to_blog($site_id);
}
$this->log('The current site is '. get_current_blog_id());
// check for the page...
$post = $this->get_post($slug);
$blog_page_check = get_page_by_title($post['post_title']);
// check if there's a valid page ID
if (isset($blog_page_check->ID)) {
$post_id = $blog_page_check->ID;
$this->log('found a BFF page with id '.$post_id);
}
if ($site_id) {
restore_current_blog();
$this->log('Returned site to '. get_current_blog_id());
}
return $post_id;
}
// create a default post to hold our form... // create a default post to hold our form...
public function create_post($slug) { public function create_post($slug) {
// create the new post content // create the new post content
$this->log('Creating a post at '.$slug.'...'); $this->log('Creating a post at '.$slug.'...');
$post = $this->get_post($slug); $post_id = $this->get_bff_page_for_site();
// check to see if this page title is already used...
$blog_page_check = get_page_by_title($post['post_title']);
$post_id = -1;
// if it already exists, we'll delete it now so we can recreate it // if it already exists, we'll delete it now so we can recreate it
if (isset($blog_page_check->ID) && $post_id = $blog_page_check->ID) { if ($post_id) {
$removed = wp_delete_post($post_id, true); $removed = wp_delete_post($post_id, true);
$this->log('Result of attempt to delete post '.$post_id.': '. print_r($removed, true)); $this->log('Result of attempt to delete post '.$post_id.': '. print_r($removed, true));
if ($removed != false || $removed != null) { if ($removed != false || $removed != null) {
...@@ -448,8 +565,8 @@ class BFFForm extends BFFCourse { ...@@ -448,8 +565,8 @@ class BFFForm extends BFFCourse {
$this->log('Recreating post (replacing previous version)...'); $this->log('Recreating post (replacing previous version)...');
} }
// check again to make sure the post is gone... // check again to make sure the post is gone...
$blog_page_check = get_page_by_title($post['post_title']); $post_id = $this->get_bff_page_for_site();
if (!isset($blog_page_check->ID)) { if (!$post_id) {
$this->log('Creating post...'); $this->log('Creating post...');
if (!($post_id = wp_insert_post($post))) { if (!($post_id = wp_insert_post($post))) {
$this->log('Inserting post at '.$slug.' failed!'); $this->log('Inserting post at '.$slug.' failed!');
...@@ -467,7 +584,7 @@ class BFFForm extends BFFCourse { ...@@ -467,7 +584,7 @@ class BFFForm extends BFFCourse {
$this->log('Already have a page with this title - id: '.$blog_page_check->ID); $this->log('Already have a page with this title - id: '.$blog_page_check->ID);
return false; return false;
} }
$this->log('returing post id '.$post_id); $this->log('returning post id '.$post_id);
return $post_id; return $post_id;
} }
......
...@@ -138,5 +138,42 @@ jQuery(document).ready(function() { ...@@ -138,5 +138,42 @@ jQuery(document).ready(function() {
// if nothing else returns this first, there was a problem... // if nothing else returns this first, there was a problem...
return false; return false;
}); });
$('#bff-cleanup').on('click', function() {
LOG('in cleanup function');
checked = false;
$('#bff-status').html('Processing...');
$.ajax({
type: 'POST',
dataType: 'json',
url: bff_data.ajaxurl,
data: {
'action': 'bff_cleanup_submit',
'nonce': bff_data.nonce,
},
success: function(data) {
var msg = '';
LOG('Success: data: ', data);
if (data.hasOwnProperty('success')) {
// strip links out
msg = data.message.replace(/<a[^>]*>[^<]*<\/a>/g, '');
console.log('Success msg', msg);
$('#bff-status').html(msg);
// disable the form until further changes are made
$('#bff-submit').attr('disabled', true);
} else if (data.hasOwnProperty('error')) {
msg = data.message;
LOG('message:', msg);
$('#bff-status').html(msg);
}
return true;
},
failure: function() {
LOG('Failure: data: ', data);
$('#bff-status').text('Error!');
}
});
// if nothing else returns this first, there was a problem...
return false;
});
}); });
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment