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 | * 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_Boxes extends CMB2_REST_Controller { |
||
|
0 ignored issues
–
show
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * CMB2 Instance |
||
| 21 | * |
||
| 22 | * @var CMB2 |
||
| 23 | */ |
||
| 24 | protected $cmb; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Register the routes for the objects of the controller. |
||
| 28 | * |
||
| 29 | * @since 2.2.0 |
||
| 30 | */ |
||
| 31 | View Code Duplication | public function register_routes() { |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 32 | // Returns all boxes data. |
||
| 33 | register_rest_route( CMB2_REST::BASE, '/boxes/', array( |
||
| 34 | array( |
||
| 35 | 'methods' => WP_REST_Server::READABLE, |
||
| 36 | 'callback' => array( $this, 'get_items' ), |
||
| 37 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
| 38 | ), |
||
| 39 | 'schema' => array( $this, 'get_item_schema' ), |
||
| 40 | ) ); |
||
| 41 | |||
| 42 | // Returns specific box's data. |
||
| 43 | register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)', array( |
||
| 44 | array( |
||
| 45 | 'methods' => WP_REST_Server::READABLE, |
||
| 46 | 'callback' => array( $this, 'get_item' ), |
||
| 47 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
| 48 | ), |
||
| 49 | 'schema' => array( $this, 'get_item_schema' ), |
||
| 50 | ) ); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get all public fields |
||
| 55 | * |
||
| 56 | * @since 2.2.0 |
||
| 57 | * |
||
| 58 | * @param WP_REST_Request $request The API request object. |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function get_items( $request ) { |
||
|
0 ignored issues
–
show
get_items uses the super-global variable $_GET which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 62 | $this->initiate_request( $request, 'boxes_read' ); |
||
| 63 | |||
| 64 | if ( empty( CMB2_REST::$boxes ) ) { |
||
| 65 | return $this->prepare_item( array( 'error' => __( 'No boxes found.', 'cmb2' ) ), $this->request ); |
||
| 66 | } |
||
| 67 | |||
| 68 | $boxes_data = array(); |
||
| 69 | // Loop boxes and get specific field. |
||
| 70 | foreach ( CMB2_REST::$boxes as $this->rest_box ) { |
||
| 71 | if ( $this->rest_box->rest_read ) { |
||
| 72 | $rest_box = $this->get_rest_box(); |
||
| 73 | $boxes_data[ $this->rest_box->cmb->cmb_id ] = $this->server->response_to_data( $rest_box, isset( $_GET['_embed'] ) ); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | return $this->prepare_item( $boxes_data ); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get all public fields |
||
| 82 | * |
||
| 83 | * @since 2.2.0 |
||
| 84 | * |
||
| 85 | * @param WP_REST_Request $request The API request object. |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function get_item( $request ) { |
||
| 89 | $this->initiate_rest_read_box( $request, 'box_read' ); |
||
| 90 | |||
| 91 | if ( is_wp_error( $this->rest_box ) ) { |
||
| 92 | return $this->prepare_item( array( 'error' => $this->rest_box->get_error_message() ) ); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $this->prepare_item( $this->get_rest_box() ); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get a CMB2 box prepared for REST |
||
| 100 | * |
||
| 101 | * @since 2.2.0 |
||
| 102 | * |
||
| 103 | * @param CMB2 $cmb |
||
|
0 ignored issues
–
show
There is no parameter named
$cmb. Was it maybe removed?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. Consider the following example. The parameter /**
* @param array $germany
* @param array $island
* @param array $italy
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was removed, but the annotation was not. Loading history...
|
|||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function get_rest_box() { |
||
|
0 ignored issues
–
show
get_rest_box uses the super-global variable $_REQUEST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 107 | $cmb = $this->rest_box->cmb; |
||
| 108 | $cmb->object_type( $this->object_id ); |
||
| 109 | $cmb->object_id( $this->object_type ); |
||
| 110 | |||
| 111 | $boxes_data = $cmb->meta_box; |
||
| 112 | |||
| 113 | if ( isset( $_REQUEST['_rendered'] ) && '/cmb2/v1/boxes' !== CMB2_REST_Controller::get_intial_route() ) { |
||
| 114 | $boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) ); |
||
| 115 | $boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) ); |
||
| 116 | |||
| 117 | 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...
|
|||
| 118 | $before_css = $wp_styles->queue; |
||
| 119 | $before_js = $wp_scripts->queue; |
||
| 120 | |||
| 121 | CMB2_JS::enqueue(); |
||
| 122 | |||
| 123 | $boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) ); |
||
| 124 | $boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) ); |
||
| 125 | } |
||
| 126 | |||
| 127 | // TODO: look into 'embed' parameter. |
||
| 128 | // http://demo.wp-api.org/wp-json/wp/v2/posts?_embed |
||
| 129 | unset( $boxes_data['fields'] ); |
||
| 130 | // Handle callable properties. |
||
| 131 | unset( $boxes_data['show_on_cb'] ); |
||
| 132 | |||
| 133 | $response = rest_ensure_response( $boxes_data ); |
||
| 134 | |||
| 135 | $response->add_links( $this->prepare_links( $cmb ) ); |
||
| 136 | |||
| 137 | return $response; |
||
| 138 | } |
||
| 139 | |||
| 140 | protected function prepare_links( $cmb ) { |
||
| 141 | $base = CMB2_REST::BASE . '/boxes'; |
||
| 142 | $boxbase = $base . '/' . $cmb->cmb_id; |
||
| 143 | |||
| 144 | return array( |
||
| 145 | // Standard Link Relations -- http://v2.wp-api.org/extending/linking/ |
||
| 146 | 'self' => array( |
||
| 147 | 'href' => rest_url( $boxbase ), |
||
| 148 | ), |
||
| 149 | 'collection' => array( |
||
| 150 | 'href' => rest_url( $base ), |
||
| 151 | ), |
||
| 152 | // Custom Link Relations -- http://v2.wp-api.org/extending/linking/ |
||
| 153 | // TODO URL should document relationship. |
||
| 154 | 'https://cmb2.io/fields' => array( |
||
| 155 | 'href' => rest_url( trailingslashit( $boxbase ) . 'fields' ), |
||
| 156 | 'embeddable' => true, |
||
| 157 | ), |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | |||
| 161 | } |
||
| 162 |
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.