Created
March 20, 2012 04:00
-
-
Save MZAWeb/2131168 to your computer and use it in GitHub Desktop.
XML-RPC Wrapper
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 | |
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