WebDevStudios /
CMB2
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * CMB2 objects/boxes endpoint for WordPres REST API. |
||
| 4 | * Allows access to boxes configuration data. |
||
| 5 | * |
||
| 6 | * @todo Add better documentation. |
||
| 7 | * @todo Research proper schema. |
||
| 8 | * |
||
| 9 | * @since 2.2.4 |
||
| 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_Boxes extends CMB2_REST_Controller { |
||
|
0 ignored issues
–
show
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * The base of this controller's route. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $rest_base = 'boxes'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The combined $namespace and $rest_base for these routes. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $namespace_base = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Constructor |
||
| 35 | * @since 2.2.4 |
||
| 36 | */ |
||
| 37 | public function __construct( WP_REST_Server $wp_rest_server ) { |
||
| 38 | $this->namespace_base = $this->namespace . '/' . $this->rest_base; |
||
| 39 | parent::__construct( $wp_rest_server ); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Register the routes for the objects of the controller. |
||
| 44 | * |
||
| 45 | * @since 2.2.4 |
||
| 46 | */ |
||
| 47 | public function register_routes() { |
||
| 48 | |||
| 49 | // Returns all boxes data. |
||
| 50 | register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
||
| 51 | array( |
||
| 52 | 'methods' => WP_REST_Server::READABLE, |
||
| 53 | 'callback' => array( $this, 'get_items' ), |
||
| 54 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
| 55 | ), |
||
| 56 | 'schema' => array( $this, 'get_item_schema' ), |
||
| 57 | ) ); |
||
| 58 | |||
| 59 | // Returns specific box's data. |
||
| 60 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)', array( |
||
| 61 | array( |
||
| 62 | 'methods' => WP_REST_Server::READABLE, |
||
| 63 | 'callback' => array( $this, 'get_item' ), |
||
| 64 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
| 65 | ), |
||
| 66 | 'schema' => array( $this, 'get_item_schema' ), |
||
| 67 | ) ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get all public CMB2 boxes. |
||
| 72 | * |
||
| 73 | * @since 2.2.4 |
||
| 74 | * |
||
| 75 | * @param WP_REST_Request $request Full data about the request. |
||
| 76 | * @return WP_Error|WP_REST_Response |
||
| 77 | */ |
||
| 78 | public function get_items( $request ) { |
||
| 79 | $this->initiate_request( $request, 'boxes_read' ); |
||
| 80 | |||
| 81 | if ( empty( CMB2_REST::$boxes ) ) { |
||
| 82 | return $this->prepare_item( array( 'error' => __( 'No boxes found.', 'cmb2' ) ), $this->request ); |
||
|
0 ignored issues
–
show
The call to
CMB2_REST_Controller_Boxes::prepare_item() has too many arguments starting with $this->request.
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. In this case you can add the Loading history...
|
|||
| 83 | } |
||
| 84 | |||
| 85 | $boxes_data = array(); |
||
| 86 | // Loop boxes and get specific field. |
||
| 87 | foreach ( CMB2_REST::$boxes as $this->rest_box ) { |
||
| 88 | if ( $this->rest_box->rest_read ) { |
||
|
0 ignored issues
–
show
The property
$rest_read is declared protected in CMB2_REST. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 89 | $rest_box = $this->get_rest_box(); |
||
| 90 | $boxes_data[ $this->rest_box->cmb->cmb_id ] = $this->server->response_to_data( $rest_box, isset( $this->request['_embed'] ) ); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | return $this->prepare_item( $boxes_data ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get one CMB2 box from the collection. |
||
| 99 | * |
||
| 100 | * @since 2.2.4 |
||
| 101 | * |
||
| 102 | * @param WP_REST_Request $request Full data about the request. |
||
| 103 | * @return WP_Error|WP_REST_Response |
||
| 104 | */ |
||
| 105 | public function get_item( $request ) { |
||
| 106 | $this->initiate_rest_read_box( $request, 'box_read' ); |
||
| 107 | |||
| 108 | if ( is_wp_error( $this->rest_box ) ) { |
||
| 109 | return $this->prepare_item( array( 'error' => $this->rest_box->get_error_message() ) ); |
||
|
0 ignored issues
–
show
The method
get_error_message() does not seem to exist on object<CMB2_REST>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 110 | } |
||
| 111 | |||
| 112 | return $this->prepare_item( $this->get_rest_box() ); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get a CMB2 box prepared for REST |
||
| 117 | * |
||
| 118 | * @since 2.2.4 |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function get_rest_box() { |
||
| 123 | $cmb = $this->rest_box->cmb; |
||
| 124 | |||
| 125 | $boxes_data = $cmb->meta_box; |
||
| 126 | |||
| 127 | if ( isset( $this->request['_rendered'] ) && $this->namespace_base !== CMB2_REST_Controller::get_intial_route() ) { |
||
| 128 | $boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) ); |
||
| 129 | $boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) ); |
||
| 130 | |||
| 131 | global $wp_scripts, $wp_styles; |
||
|
0 ignored issues
–
show
Compatibility
Best Practice
introduced
by
Use of
global functionality is not recommended; it makes your code harder to test, and less reusable.
Instead of relying on 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
Loading history...
|
|||
| 132 | $before_css = $wp_styles->queue; |
||
| 133 | $before_js = $wp_scripts->queue; |
||
| 134 | |||
| 135 | CMB2_JS::enqueue(); |
||
| 136 | |||
| 137 | $boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) ); |
||
| 138 | $boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) ); |
||
| 139 | } |
||
| 140 | |||
| 141 | // TODO: look into 'embed' parameter. |
||
| 142 | // http://demo.wp-api.org/wp-json/wp/v2/posts?_embed |
||
| 143 | unset( $boxes_data['fields'] ); |
||
| 144 | // Handle callable properties. |
||
| 145 | unset( $boxes_data['show_on_cb'] ); |
||
| 146 | |||
| 147 | $response = rest_ensure_response( $boxes_data ); |
||
| 148 | |||
| 149 | $response->add_links( $this->prepare_links( $cmb ) ); |
||
|
0 ignored issues
–
show
$cmb is of type object<CMB2>, but the function expects a object<CMB2_REST>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 150 | |||
| 151 | return $response; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return an array of contextual links for box/boxes. |
||
| 156 | * |
||
| 157 | * @since 2.2.4 |
||
| 158 | * |
||
| 159 | * @param CMB2_REST $cmb CMB2_REST object to build links from. |
||
| 160 | * |
||
| 161 | * @return array Array of links |
||
| 162 | */ |
||
| 163 | protected function prepare_links( $cmb ) { |
||
| 164 | $boxbase = $this->namespace_base . '/' . $cmb->cmb_id; |
||
|
0 ignored issues
–
show
The property
cmb_id does not exist on object<CMB2_REST>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 165 | $query_string = $this->get_query_string(); |
||
| 166 | |||
| 167 | return array( |
||
| 168 | // Standard Link Relations -- http://v2.wp-api.org/extending/linking/ |
||
| 169 | 'self' => array( |
||
| 170 | 'href' => rest_url( $boxbase . $query_string ), |
||
| 171 | ), |
||
| 172 | 'collection' => array( |
||
| 173 | 'href' => rest_url( $this->namespace_base . $query_string ), |
||
| 174 | ), |
||
| 175 | // Custom Link Relations -- http://v2.wp-api.org/extending/linking/ |
||
| 176 | // TODO URL should document relationship. |
||
| 177 | 'https://cmb2.io/fields' => array( |
||
| 178 | 'href' => rest_url( trailingslashit( $boxbase ) . 'fields' . $query_string ), |
||
| 179 | 'embeddable' => true, |
||
| 180 | ), |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | |||
| 184 | } |
||
| 185 |
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.