MortgageNews_API
Class MortgageNews_API.
Contents
Description Description
See also See also
Source Source
File: mortgage-news-widget/inc/MortgageNews_API.php
class MortgageNews_API extends \WP_REST_Controller {
/**
* API constructor.
* @since 1.0.0
* @access public
*/
public function __construct() {
$this->namespace = 'mortgage_news/v1';
$this->rest_base = '/news';
}
/**
* Endpoint API.
* @since 1.0.0
* @access public
*
* @return bool True on success, false on error.
*/
public function api() {
return register_rest_route(
$this->namespace,
$this->rest_base,
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'get_news' ),
'permission_callback' => array( $this, 'permission' ),
),
)
);
}
/**
* Check permissions for the posts.
*
* @access public
* @since 1.0.0
*
* @param \WP_REST_Request $request Current request.
*
* @return bool|\WP_Error True on success, WP_Error on error.
*/
public function permission( $request ) {
if ( false) {
return new \WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the mortgage news.', 'MortgageNewsWidget' ), array( 'status' => $this->authorization_status_code() ) );
}
return true;
}
/**
* Sets up the proper HTTP status code for authorization.
*
* @access public
* @since 1.0.0
*
* @return int Status.
*/
public function authorization_status_code() {
$status = 401;
return $status;
}
/**
* Grabs the five most recent posts and outputs them as a rest response.
*
* @access public
* @since 1.0.0
*
* @param \WP_REST_Request $request Current request.
*
* @return array|\WP_Error
*/
public function get_news( $request ) {
if (MortgageNewsWidget::$is_active_mortgage_platform) {
$website = new MortgageNews_Websites();
$website->update_uri('news_API');
if ($website->check_uri('news_API','')) {
$news = new MortgageNews();
return $news->get_news();
} else {
return new \WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the mortgage news.', 'MortgageNewsWidget' ), array( 'status' => $this->authorization_status_code() ) );
}
} else {
$news = new MortgageNews();
return $news->get_news();
}
}
/**
* Register API post type.
* @access public
* @since 1.0.0
* @return \WP_Error|\WP_Post_Type
*/
public function register_api_post_type() {
$labels = array(
'name' => 'API keys',
'singular_name' => 'API key',
'add_new' => __( 'Add API key', 'MortgageNewsWidget' ),
'add_new_item' => __( 'Add new API key', 'MortgageNewsWidget' ),
'edit_item' => __( 'Edit API key', 'MortgageNewsWidget' ),
'new_item' => __( 'New API key', 'MortgageNewsWidget' ),
'view_item' => __( 'Look API key', 'MortgageNewsWidget' ),
'search_items' => __( 'Find API key', 'MortgageNewsWidget' ),
'not_found' => __( 'API keys not found', 'MortgageNewsWidget' ),
'not_found_in_trash' => __( 'At cart API keys not found', 'MortgageNewsWidget' ),
'parent_item_colon' => '',
'menu_name' => 'API keys',
);
$post_type = register_post_type(
'mn_embedd_api',
array(
'labels' => $labels,
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => 'mortgage_news_widget',
'query_var' => true,
'show_in_rest' => false,
'exclude_from_search' => false,
'rewrite' => false,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title' ),
/*'capabilities' => array(
'edit_post' => true,
'read_post' => true,
'delete_post' => true,
'create_posts' => false
)*/
)
);
return $post_type;
}
}
Methods Methods
- __construct — API constructor.
- api — Endpoint API.
- authorization_status_code — Sets up the proper HTTP status code for authorization.
- get_news — Grabs the five most recent posts and outputs them as a rest response.
- permission — Check permissions for the posts.
- register_api_post_type — Register API post type.
Changelog Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |