1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Creates CMB2 objects/fields endpoint for WordPres REST API. |
4
|
|
|
* Allows access to fields registered to a specific post type and more. |
5
|
|
|
* |
6
|
|
|
* @todo Add better documentation. |
7
|
|
|
* @todo Research proper schema. |
8
|
|
|
* |
9
|
|
|
* @since 2.2.0 |
10
|
|
|
* |
11
|
|
|
* @category WordPress_Plugin |
12
|
|
|
* @package CMB2 |
13
|
|
|
* @author WebDevStudios |
14
|
|
|
* @license GPL-2.0+ |
15
|
|
|
* @link http://webdevstudios.com |
16
|
|
|
*/ |
17
|
|
|
class CMB2_REST_Controller_Fields extends CMB2_REST_Controller { |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Register the routes for the objects of the controller. |
21
|
|
|
* |
22
|
|
|
* @since 2.2.0 |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function register_routes() { |
|
|
|
|
25
|
|
|
// Returns specific box's fields. |
26
|
|
|
register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)/fields/', array( |
27
|
|
|
array( |
28
|
|
|
'methods' => WP_REST_Server::READABLE, |
29
|
|
|
'callback' => array( $this, 'get_fields' ), |
30
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
31
|
|
|
), |
32
|
|
|
'schema' => array( $this, 'get_item_schema' ), |
33
|
|
|
) ); |
34
|
|
|
|
35
|
|
|
// Returns specific field data. |
36
|
|
|
register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)/fields/(?P<field_id>[\w-]+)', array( |
37
|
|
|
array( |
38
|
|
|
'methods' => WP_REST_Server::READABLE, |
39
|
|
|
'callback' => array( $this, 'get_field' ), |
40
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
41
|
|
|
), |
42
|
|
|
'schema' => array( $this, 'get_item_schema' ), |
43
|
|
|
) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get all box fields |
48
|
|
|
* |
49
|
|
|
* @since 2.2.0 |
50
|
|
|
* |
51
|
|
|
* @param WP_REST_Request $request The API request object. |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function get_fields( $request ) { |
55
|
|
|
$this->initiate_request( $request ); |
56
|
|
|
|
57
|
|
|
$cmb_id = $this->request->get_param( 'cmb_id' ); |
58
|
|
|
|
59
|
|
|
if ( $cmb_id && ( $cmb = cmb2_get_metabox( $cmb_id, $this->object_id, $this->object_type ) ) ) { |
60
|
|
|
$fields = array(); |
61
|
|
|
foreach ( $cmb->prop( 'fields', array() ) as $field ) { |
62
|
|
|
$field = $this->get_rest_field( $cmb, $field['id'] ); |
63
|
|
|
|
64
|
|
|
if ( ! is_wp_error( $field ) ) { |
65
|
|
|
$fields[ $field['id'] ] = $field; |
66
|
|
|
} else { |
67
|
|
|
$fields[ $field['id'] ] = array( 'error' => $field->get_error_message() ); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->prepare_item( $fields ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get a specific field |
79
|
|
|
* |
80
|
|
|
* @since 2.2.0 |
81
|
|
|
* |
82
|
|
|
* @param WP_REST_Request $request The API request object. |
83
|
|
|
* @return array|WP_Error |
84
|
|
|
*/ |
85
|
|
|
public function get_field( $request ) { |
86
|
|
|
$this->initiate_request( $request ); |
87
|
|
|
|
88
|
|
|
$cmb = cmb2_get_metabox( $this->request->get_param( 'cmb_id' ), $this->object_id, $this->object_type ); |
89
|
|
|
|
90
|
|
|
if ( ! $cmb ) { |
91
|
|
|
return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$field = $this->get_rest_field( $cmb, $this->request->get_param( 'field_id' ) ); |
95
|
|
|
|
96
|
|
|
if ( is_wp_error( $field ) ) { |
97
|
|
|
return $this->prepare_item( array( 'error' => $field->get_error_message() ) ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// wp_die( '<xmp>$field: '. print_r( $field, true ) .'</xmp>' ); |
|
|
|
|
101
|
|
|
// echo '<xmp>$field: '. print_r( $field, true ) .'</xmp>'; |
|
|
|
|
102
|
|
|
// $field = $this->prepare_item( $field ); |
|
|
|
|
103
|
|
|
return $this->prepare_item( $field ); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.