Last active
April 13, 2016 18:04
-
-
Save vc27/0a12980fbf7b2c78e13d0dffb2dc177e to your computer and use it in GitHub Desktop.
Simple started class for the wp api, requires https://wordpress.org/plugins/rest-api/
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 | |
/** | |
* @package WordPress | |
* @subpackage ProjectName | |
* @license GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
* | |
* Description: | |
**/ | |
#################################################################################################### | |
/** | |
* WP_API_Starter_WP | |
* @since 1.0 | |
**/ | |
new WP_API_Starter_WP(); | |
class WP_API_Starter_WP extends WP_REST_Controller { | |
/** | |
* version | |
* | |
* @access public | |
* @var int | |
* @since 1.0 | |
**/ | |
public $version = 1; | |
/** | |
* namespace | |
* | |
* @access public | |
* @var string | |
* @since 1.0 | |
**/ | |
public $namespace = 'vendor'; | |
/** | |
* __construct | |
**/ | |
function __construct() { | |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
} // end function __construct | |
#################################################################################################### | |
/** | |
* Set Get | |
**/ | |
#################################################################################################### | |
/** | |
* set | |
* @since 1.0 | |
**/ | |
function set( $key, $val = false ) { | |
if ( isset( $key ) AND ! empty( $key ) ) { | |
$this->$key = $val; | |
} | |
} // end function set | |
/** | |
* get | |
* @since 1.0 | |
**/ | |
function get( $key ) { | |
if ( isset( $key ) AND ! empty( $key ) AND isset( $this->$key ) AND ! empty( $this->$key ) ) { | |
return $this->$key; | |
} else { | |
return false; | |
} | |
} // end function get | |
/** | |
* get_route | |
* @since 1.0 | |
**/ | |
function get_route( $route ) { | |
return '/v' . $this->get('version') . '/' . $route; | |
} // end function get_route | |
#################################################################################################### | |
/** | |
* Functionality | |
**/ | |
#################################################################################################### | |
/** | |
* Register the routes for the objects of the controller. | |
*/ | |
public function register_routes() { | |
register_rest_route( $this->get('namespace'), $this->get_route( 'route/test' ), array( | |
array( | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => array( $this, 'testing_function' ), | |
'permission_callback' => '__return_true', | |
'args' => array() | |
) | |
) ); | |
} | |
/** | |
* Testing | |
* | |
* @param WP_REST_Request $request Full data about the request. | |
* @return WP_Error|WP_REST_Response | |
*/ | |
public function testing_function( $request ) { | |
$data = $this->prepare_response_for_collection( array( 'data' => array( 'data', 'data', 'data' ) ) ); | |
if ( $data ) { | |
return new WP_REST_Response( $data, 200 ); | |
} else { | |
return new WP_Error( 'no data', __( 'message', 'text-domain'), array( 'status' => 500 ) ); | |
} | |
} | |
} // end class WP_API_Starter_WP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment