Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Created September 5, 2013 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justintadlock/6452060 to your computer and use it in GitHub Desktop.
Save justintadlock/6452060 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Theme Hybrid - API
* Plugin URI: http://themehybrid.com
* Description: API for ThemeHybrid.com
* Version: 0.1.0
* Author: Justin Tadlock
* Author URI: http://justintadlock.com
*/
final class TH_API {
/**
* The query variable field to look out for.
*
* @access public
* @since 0.1.0
* @var string
*/
public $listener_query_var = 'api';
/**
* The query variable value.
*
* @since 0.1.0
* @access public
* @var string
*/
public $listener_query_var_value = '';
/**
* Sets up needed actions and filters.
*
* @since 0.1.0
* @access public
* @return void
*/
function __construct() {
/* Parses the request to see if our query vars were called. */
add_action( 'parse_request', array( $this, 'parse_request' ) );
/* Adds custom rewrite rules. */
add_action( 'init', array( $this, 'rewrite' ) );
}
/**
* Creates custom rewrite rules for the API. It gives us "/api" (?api) and
* "/api/$var" (?api=$var).
*
* @since 0.1.0
* @access public
* @return void
*/
public function rewrite() {
/* Adds the 'api' query var. */
add_rewrite_tag( '%' . $this->listener_query_var . '%', '([^&]+)' );
add_rewrite_rule( '^' . $this->listener_query_var . '$', 'index.php?' . $this->listener_query_var, 'top' );
add_rewrite_rule( '^' . $this->listener_query_var . '/?([^/]*)$', 'index.php?' . $this->listener_query_var . '=$matches[1]', 'top' );
}
/**
* Filters 'parse_request' to check for the matching query string for the API.
*
* @since 0.1.0
* @access public
* @param object $wp
*/
public function parse_request( $wp ) {
//var_dump( $wp->query_vars );
/* Check if the 'api' query var is set. */
if ( isset( $wp->query_vars[ $this->listener_query_var ] ) ) {
/* If so, get the value. */
$this->listener_query_var_value = sanitize_key( $wp->query_vars[$this->listener_query_var] );
/* If the value is 'update_theme', check for a theme update. */
if ( 'update_theme' === $this->listener_query_var_value )
$this->update_theme();
/* Else, display the API root/index page. */
else
$this->do_api_root();
}
}
/**
* Process the data sent.
*
* @since 0.1.0
* @access public
* @return void
*/
public function update_theme() {
/* Check if the user agent is WordPress. */
if ( true == stristr( $_SERVER['HTTP_USER_AGENT'], 'WordPress' ) ) {
/* Unserialize the data sent. */
$args = maybe_unserialize( stripslashes( $_POST['request'] ) );
/* If we don't have a theme slug, bail. */
if ( empty( $args->slug ) )
exit;
/* Get the theme object by name. */
$theme = get_page_by_path( sanitize_title( $args['slug'] ), 'OBJECT', 'theme' );
/* If no theme was returned, bail. */
if ( empty( $theme ) )
exit;
/* Get and sanitize the data for the theme. */
$new_version = strtolower( get_post_meta( $theme->ID, 'version_number', true ) );
$package = esc_url( get_post_meta( $theme->ID, 'download_url', true ) );
$url = esc_url( $args['theme_uri'] );
/* If any of the required theme data isn't here, bail. */
if ( empty( $new_version ) || empty( $package ) || empty( $url ) )
exit;
/* Make sure the package (theme download URL) is actually a ZIP file. */
if ( !preg_match( "/^.*\.(zip)$/i", $package ) )
exit;
/* Set up the theme data in an array. */
$data = array(
'new_version' => $new_version,
'package' => $package,
'url' => $url
);
/* Print the serialized data. */
print serialize( $data );
}
/* Make sure we don't keep loading the rest of the site. */
exit;
}
/**
* Prints data for the API root page or any page with the 'api' query var that doesn't work.
*
* @todo Turn this into an actual page on the site with info about using the API.
*
* @since 0.1.0
* @access public
* @return void
*/
public function do_api_root() {
echo 'API for <a href="http://themehybrid.com">ThemeHybrid.com</a>';
exit;
}
}
new TH_API();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment