Created
August 4, 2014 13:45
-
-
Save tangrufus/28deada10dd91edecfb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// sunny-demo/admin/class-sunny-demo-admin.php | |
class Sunny_Demo_Admin { | |
// Some code is skipped!!!!!!! | |
/** | |
* Initialize the plugin by loading admin scripts & styles and adding a | |
* settings page and menu. | |
* | |
* @since 1.0.0 | |
*/ | |
private function __construct() { | |
// Few lines skipped!!!!!!! | |
// Load includes | |
add_action( 'admin_init', array( $this, 'load_includes' ) ); | |
// Add the option settings | |
add_action( 'admin_init', array( 'Sunny_Demo_Option', 'get_instance' ) ); | |
// Initialize Sunny_Demo_Post_Purger during post saving | |
add_action( 'save_post', array( 'Sunny_Demo_Post_Purger', 'get_instance' ), 5 ); | |
} | |
// Lots of lines skipped here!!!!!!! | |
/** | |
* 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' ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// sunny-demo/admin/includes/class-sunny-demo-api-logger.php | |
/** | |
* Helper class. Log API responses. | |
*/ | |
class Sunny_Demo_API_Logger { | |
/** | |
* Log debug messages in php error log. | |
* | |
* @since 1.0.0 | |
* | |
* @param $response The response after api call, could be WP Error object or HTTP return object | |
* @param $url The Url that API calls | |
* | |
* @return void No return | |
*/ | |
public static function write_report( $response, $url ) { | |
if ( is_wp_error( $response ) ) { | |
error_log( 'Sunny Demo ' . 'WP Error ' . $response->get_error_message() . $url ); | |
}// end WP Error | |
else { | |
// API made | |
$response_array = json_decode( $response['body'], true ); | |
if ( 'error' == $response_array['result'] ) { | |
error_log( 'Sunny Demo ' . 'API Error ' . $response_array['msg'] .'--' . $url ); | |
error_log( 'Sunny Demo ' . 'API Error ' . $response_array['msg'] .'--' . $url ); | |
} else { | |
error_log( 'Sunny Demo' . 'API Success ' . $url ); | |
} // end else | |
} // end API made | |
} // end write_report( $response, $url ) | |
} // end Sunny_Demo_API_Logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// sunny-demo/admin/includes/class-sunny-demo-purger.php | |
Class Sunny_Demo_Purger { | |
// The URL of the API | |
private static $CLOUDFLARE_API_ENDPOINT = 'https://www.cloudflare.com/api_json.html'; | |
/** | |
* Purge single file in CloudFlare's cache by making `zone_file_purge` calls | |
* | |
* @since 1.0.0 | |
* | |
* @param array $url Url to be purged | |
*/ | |
public static function purge_cloudflare_cache_by_url( $url ) { | |
$temp_domain = explode( '.', parse_url( esc_url_raw( $url ), PHP_URL_HOST ) ); | |
$domain = $temp_domain[count( $temp_domain )-2] . '.' . $temp_domain[count( $temp_domain )-1]; | |
$data = array( | |
'email' => get_option( 'sunny_demo_cloudflare_email' ), | |
'tkn' => get_option( 'sunny_demo_cloudflare_api_key' ), | |
'a' => 'zone_file_purge', | |
'z' => $domain, | |
'url' => $url | |
); | |
$response = wp_remote_post( | |
self::$CLOUDFLARE_API_ENDPOINT, | |
array( | |
'body' => $data | |
) | |
); | |
Sunny_Demo_API_Logger::write_report( $response, $url ); | |
return $response; | |
} // end purge_cloudflare_cache_by_url | |
} // end Sunny_Demo_Purger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// inside class Sunny_Demo_Post_Purger | |
/** | |
* Purge the updated post only if it is published. | |
* Hooked into 'save_post' | |
* | |
* @param integer $post_id The current post being saved | |
* | |
* @since 1.0.0 | |
*/ | |
public function purge_post( $post_id ) { | |
if ( $this->should_purge( $post_id ) ) { | |
$url = get_permalink( $post_id ); | |
Sunny_Demo_Purger::purge_cloudflare_cache_by_url( $url ); | |
} | |
return $post_id; | |
} // end purge_post | |
/** | |
* Verifies that the user who is currently logged in has permission to save the post | |
* and the post is published. | |
* | |
* @since 1.0.0 | |
* | |
* @param integer $post_id The current post being saved. | |
* | |
* @return boolean True if the user can save the information | |
*/ | |
private function should_purge( $post_id ) { | |
$post = get_post( $post_id ); | |
if ( false == is_object( $post ) ) { | |
return false; | |
} // end if | |
$is_autosave = wp_is_post_autosave( $post_id ); | |
$is_revision = wp_is_post_revision( $post_id ); | |
$is_published = ( 'publish' == get_post_status( $post_id ) ); | |
return ! ( $is_autosave || $is_revision ) && $is_published; | |
} //end should_purge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// sunny-demo/admin/includes/class-sunny-demo-post-purger.php | |
/** | |
* This class takes care the purge process fired from the admin dashboard. | |
*/ | |
class Sunny_Demo_Post_Purger { | |
/** | |
* Instance of this class. | |
* | |
* @since 1.0.0 | |
* | |
* @var object | |
*/ | |
protected static $instance = null; | |
/** | |
* Initialize the class and purge after post saved | |
* | |
* @since 1.0.0 | |
*/ | |
private function __construct() { | |
add_action( 'save_post', array( $this, 'purge_post' ), 20 ); | |
} | |
/** | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment