Skip to content

Instantly share code, notes, and snippets.

@tangrufus
Created August 10, 2014 13:05
Show Gist options
  • Save tangrufus/d5b94b07a3cc5e6b0469 to your computer and use it in GitHub Desktop.
Save tangrufus/d5b94b07a3cc5e6b0469 to your computer and use it in GitHub Desktop.
<div id="sunny_demo_url_purger" class="wrap">
<form id="sunny_demo_url_purger_form" method="POST">
<?php wp_nonce_field( 'sunny_demo_url_purger', 'sunny_demo_url_purger_nonce'); ?>
<?php do_settings_sections( 'sunny_demo_url_purger_section' ); ?>
<?php submit_button( __('Purge', $plugin_slug ), 'primary', 'sunny_demo_url_purger_button' ); ?>
</form>
<br class="clear">
<div id="sunny_demo_url_purger_result" style="display: none">
<h3 id="sunny_demo_url_purger_result_heading">URL Purger Result</h3>
<img id="sunny_demo_url_purger_form_spinner" style="display: none" src="<?php echo admin_url(); ?>images/spinner-2x.gif">
</div>
</div>
// sunny-demo/admin/assets/js/admin.js
(function ( $ ) {
"use strict";
$(function () {
jQuery("#sunny_demo_url_purger_form").submit(function(){
// Prevent Default Action
event.preventDefault();
jQuery("#sunny_demo_url_purger_result").show();
jQuery("#sunny_demo_url_purger_form_spinner").show();
jQuery("#sunny_demo_url_purger_button").attr("disabled", "disabled")
var data = {
action: 'sunny_demo_purge_url',
nonce: jQuery('#sunny_demo_url_purger_nonce').val(),// The security nonce
"post_url": jQuery('#sunny_demo_purge_url').val()
};
jQuery.post( ajaxurl, data, function (response) {
// React to the response
var output = "<p>" + response.message + "</p>";
jQuery("#sunny_demo_url_purger_result").append(jQuery(output).fadeIn('slow'));
jQuery("#sunny_demo_url_purger_form_spinner").hide();
jQuery("#sunny_demo_url_purger_button").removeAttr("disabled");
}, 'json');
// Prevent Default Action Again
return false;
}); // end #url_purger_form submit
});
}(jQuery));
<?php
// inside class Sunny_Demo_URL_Purger
class Sunny_Demo_URL_Purger {
// Tons of code skipped!
private function __construct() {
// Some lines skipped!
add_action( 'wp_ajax_sunny_demo_purge_url', array( $this, 'process_ajax' ) );
}
public function process_ajax() {
header('Content-Type: application/json');
// Check that user has proper secuity level && Check the nonce field
if ( ! current_user_can( 'manage_options') || ! check_ajax_referer( 'sunny_demo_url_purger', 'nonce', false ) ) {
$return_args = array(
"result" => "Error",
"message" => "403 Forbidden",
);
$response = json_encode( $return_args );
echo $response;
die;
}
// It's safe to carry on
// Prepare return message
$message = '';
$links = array();
$post_url = esc_url_raw( $_REQUEST['post_url'], array( 'http', 'https' ) );
if ( '' == $post_url || $_REQUEST['post_url'] != $post_url ) {
$message = 'Error: Invalid URL.';
} elseif ( '' != $post_url ) {
$_response = Sunny_Demo_Purger::purge_cloudflare_cache_by_url( $post_url );
if ( is_wp_error( $_response ) ) {
$message .= 'WP Error: ' . $_response->get_error_message();
} // end wp error
else {
// API made
$_response_array = json_decode( $_response['body'], true );
if ( 'error' == $_response_array['result'] ) {
$message .= 'API Error: ';
$message .= $_response_array['msg'] . ' -- ';
} // end api returns error
elseif ( 'success' == $_response_array['result'] ) {
$message .= 'Success: ';
} // end api success //end elseif
} // end else
} // end elseif
$message .= esc_url( $post_url );
$return_args = array(
'message' => $message,
);
$response = json_encode( $return_args );
echo $response;
die;
} // end process_ajax
}
<?php
// sunny-demo/admin/class-sunny-demo-admin.php
class Sunny_Demo_Admin {
// Note that lots of lines skipped.
private function __construct() {
// Add the option settings
add_action( 'admin_init', array( 'Sunny_Demo_Option', 'get_instance' ) );
add_action( 'admin_init', array( 'Sunny_Demo_URL_Purger', 'get_instance' ) );
}
/**
* This function loads files in the admin/includes directory
*
* @since 1.0.0
*/
public function load_includes() {
require_once( 'includes/class-sunny-demo-option.php' );
require_once( 'includes/class-sunny-demo-post-purger.php' );
require_once( 'includes/class-sunny-demo-api-logger.php' );
require_once( 'includes/class-sunny-demo-purger.php' );
require_once( 'includes/class-sunny-demo-url-purger.php' );
}
}
<?php
// sunny-demo/admin/includes/class-sunny-demo-url-purger.php
/**
* @link http://tangrufus.com/wordpress-plugin-boilerplate-tutorial-ajax
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
class Sunny_Demo_URL_Purger {
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin by registrating settings
*
* @since 1.0.0
*/
private function __construct() {
// Get $plugin_slug from public plugin class.
$plugin = Sunny_Demo::get_instance();
$this->plugin_slug = $plugin->get_plugin_slug();
// Add Settings
$this->add_settings();
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Register the CloudFlare account section, CloudFlare email field
* and CloudFlare api key field
*
* @since 1.0.0
*/
private function add_settings() {
/**
* First, we register a section. This is necessary since all future settings must belong to one.
*/
add_settings_section(
// ID used to identify this section and with which to register options
'sunny_demo_url_purger_section',
// Title to be displayed on the administration page
'URL Purger',
// Callback used to render the description of the section
array( $this, 'sunny_demo_display_url_purger' ),
// Page on which to add this section of options
'sunny_demo_url_purger_section'
);
/**
* Next, we will introduce the fields for CloudFlare Account info.
*/
add_settings_field(
// ID used to identify the field throughout the theme
'sunny_demo_purge_url',
// The label to the left of the option interface element
'URL',
// The name of the function responsible for rendering the option interface
array( $this, 'sunny_demo_render_purge_url_input_html' ),
// The page on which this option will be displayed
'sunny_demo_url_purger_section',
// The name of the section to which this field belongs
'sunny_demo_url_purger_section'
);
} // end of add_settings
/**
* This function is being passed as a parameter in the add_settings_section function.
*
* @since 1.0.0
*/
public function sunny_demo_display_url_purger() {
echo '<p>Clear cache of this URL from CloudFlare.</p>';
} // end of sunny_demo_display_url_purger
/**
* This function generate the HTML input element for
* sunny_demo_purge_url
*
* @since 1.0.0
*/
public function sunny_demo_render_purge_url_input_html() {
// Render the output
echo '<input type="url" id="sunny_demo_purge_url" name="sunny_demo_purge_url" size="40" />';
} // end sunny_demo_render_purge_url_input_html
} //end Sunny_Demo_URL_Purger
<?php
// sunny-demo/sunny-demo.php
// Some lines here..
// At the bottom, change this condition to allow Ajax
// if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
if ( is_admin() ) {
require_once( plugin_dir_path( __FILE__ ) . 'admin/class-sunny-demo-admin.php' );
add_action( 'plugins_loaded', array( 'Sunny_Demo_Admin', 'get_instance' ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment