public
Last active

Pug Bomb API Endpoint WordPress Plugin

  • Download Gist
pug-bomb.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
<?php
/*
Plugin Name: Pug Bomb API Endpoint
Description: Adds an API endpoint at /api/pugs/$n_pugs
Version: 0.1
Author: Brian Fegter
Author URL: http://coderrr.com
*/
 
class Pugs_API_Endpoint{
/**
* @var string Pug Bomb Headquarters
*/
protected $api = 'http://pugme.herokuapp.com/bomb?count=';
/** Hook WordPress
* @return void
*/
public function __construct(){
add_filter('query_vars', array($this, 'add_query_vars'), 0);
add_action('parse_request', array($this, 'sniff_requests'), 0);
add_action('init', array($this, 'add_endpoint'), 0);
}
/** Add public query vars
* @param array $vars List of current public query vars
* @return array $vars
*/
public function add_query_vars($vars){
$vars[] = '__api';
$vars[] = 'pugs';
return $vars;
}
/** Add API Endpoint
* This is where the magic happens - brush up on your regex skillz
* @return void
*/
public function add_endpoint(){
add_rewrite_rule('^api/pugs/?([0-9]+)?/?','index.php?__api=1&pugs=$matches[1]','top');
}
 
/** Sniff Requests
* This is where we hijack all API requests
* If $_GET['__api'] is set, we kill WP and serve up pug bomb awesomeness
* @return die if API request
*/
public function sniff_requests(){
global $wp;
if(isset($wp->query_vars['__api'])){
$this->handle_request();
exit;
}
}
/** Handle Requests
* This is where we send off for an intense pug bomb package
* @return void
*/
protected function handle_request(){
global $wp;
$pugs = $wp->query_vars['pugs'];
if(!$pugs)
$this->send_response('Please tell us how many pugs to send.');
$pugs = file_get_contents($this->api.$pugs);
if($pugs)
$this->send_response('200 OK', json_decode($pugs));
else
$this->send_response('Something went wrong with the pug bomb factory');
}
/** Response Handler
* This sends a JSON response to the browser
*/
protected function send_response($msg, $pugs = ''){
$response['message'] = $msg;
if($pugs)
$response['pugs'] = $pugs;
header('content-type: application/json; charset=utf-8');
echo json_encode($response)."\n";
exit;
}
}
new Pugs_API_Endpoint();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.