Skip to content

Instantly share code, notes, and snippets.

@MZAWeb
Created March 20, 2012 04:00
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 MZAWeb/2131168 to your computer and use it in GitHub Desktop.
Save MZAWeb/2131168 to your computer and use it in GitHub Desktop.
XML-RPC Wrapper
<?php
if (!class_exists("MZAXMLRPC")){
abstract class MZAXMLRPC {
protected $calls = Array();
protected $namespace = "myxmlrpc";
function __construct($namespace){
$this->namespace = $namespace;
$reflector = new ReflectionClass($this);
//get all public methods of the child class
foreach ( $reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method){
//filter out all the public methods of this (parent) class and the the constructor, destructor, etc of the child class
if ($method->isUserDefined() && $method->getDeclaringClass()->name != get_class()){
$this->calls[] = $method->name;
}
}
add_filter('xmlrpc_methods', array($this, 'xmlrpc_methods'));
}
public function xmlrpc_methods($methods)
{
foreach ($this->calls as $call){
$methods[$this->namespace . "." . $call] = array($this, "dispatch");
}
return $methods;
}
public function dispatch($args){
global $wp_xmlrpc_server;
$username = $args[1];
$password = $args[2];
$data = $args[3];
if ( !$wp_xmlrpc_server->login($username, $password) )
return $wp_xmlrpc_server->error;
$call = $this->get_called_method();
if (method_exists($this, $call)){
$status = call_user_func_array(array($this, $call), array($data));
return $status;
}else{
return "Method not allowed";
}
}
private function get_called_method(){
global $wp_xmlrpc_server;
$call = $wp_xmlrpc_server->message->methodName;
$pieces = explode(".", $call);
return $pieces[1];
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment