add_action( 'wp_ajax_v1', 'endpoint' );
function endpoint() {
if (!isset($_POST['method'])) {
echo json_encode(array('error' => 'empty'));
} else {
$method = $_POST['method'];
$params = $_POST['params'];
$class_methods = get_class_methods('jsonapi');
if (!in_array(functionize($method), $class_methods)) {
echo json_encode(array('error' => 'no method'));
} else {
$api = new jsonapi($params);
$method=functionize($method);
echo json_encode(array('result' => $api->$method()));
}
}
die();
}
function functionize($string)
{
$parts = explode('-', $string);
foreach ($parts as $key => $value) {
$parts[$key] = ucfirst($parts[$key]);
}
$functionized = lcfirst(implode('', $parts));
return $functionized;
}
class jsonapi
{
public $params;
public function __construct($params = array())
{
$this->params = $params;
}
public function home()
{
...
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.