/** * Fetch the index data * * @throws Requests_Exception Failed to retrieve the index * @throws Exception Failed to decode JSON * @return boolean Was it successful? */ public function fetch() { $response = $this->api->get(WPAPI::ROUTE_INDEX); $response->throw_for_status(); $data = json_decode($response->body, true); $has_error = function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE; if (!$has_error && $data === null || $has_error) { throw new Exception($response->body); } $this->data = $data; return true; }
/** * Create a new post * * @throws Requests_Exception Failed to retrieve the post * @throws Exception Failed to decode JSON * @param array $data Post data to create * @return WPAPI_Post */ public function create($data) { $data = json_encode($data); $headers = array('Content-Type' => 'application/json'); $response = $this->api->post(WPAPI::ROUTE_POSTS, $headers, $data); $response->throw_for_status(); $data = json_decode($response->body, true); $has_error = function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE; if (!$has_error && $data === null || $has_error) { throw new Exception($response->body); } return new WPAPI_Post($this->api, $data); }
/** * Delete the object * * @throws Requests_Exception Failed to delete object * @return boolean Was the deletion successful? */ public function delete($permanent = false) { $url = $this->meta['links']['self']; if ($permanent) { if (strpos($url, '?') !== false) { $url .= '&force=true'; } else { $url .= '?force=true'; } } $response = Requests::delete($url, array(), $this->api->getDefaultOptions()); $response->throw_for_status(); $this->data = array(); $this->changed = array(); return true; }
<?php require '../vendor/autoload.php'; require '../library/WPAPI.php'; WPAPI::register_autoloader(); $testconfig = array(); if (!file_exists('./config.php')) { echo 'Please copy config.php.example to config.php and fill in your details'; die(1); } require './config.php'; function wpapi_tests_autoloader($class) { // Check that the class starts with "WPAPI_Test_" if (strpos($class, 'WPAPI_Test_') !== 0) { return; } $class = str_replace('WPAPI_Test_', '', $class); $file = str_replace('_', '/', $class); if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) { require_once dirname(__FILE__) . '/' . $file . '.php'; } } spl_autoload_register('wpapi_tests_autoloader');