Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 17 | class CMB2_REST_Controller_Boxes extends CMB2_REST_Controller { |
||
|
|
|||
| 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 | * |
||
| 36 | * @since 2.2.3 |
||
| 37 | */ |
||
| 38 | public function __construct( WP_REST_Server $wp_rest_server ) { |
||
| 39 | $this->namespace_base = $this->namespace . '/' . $this->rest_base; |
||
| 40 | parent::__construct( $wp_rest_server ); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Register the routes for the objects of the controller. |
||
| 45 | * |
||
| 46 | * @since 2.2.3 |
||
| 47 | */ |
||
| 48 | public function register_routes() { |
||
| 49 | $args = array( |
||
| 50 | '_embed' => array( |
||
| 51 | 'description' => __( 'Includes the registered fields for the box in the response.', 'cmb2' ), |
||
| 52 | ), |
||
| 53 | ); |
||
| 54 | |||
| 55 | // @todo determine what belongs in the context param. |
||
| 56 | // $args['context'] = $this->get_context_param(); |
||
| 57 | // $args['context']['required'] = false; |
||
| 58 | // $args['context']['default'] = 'view'; |
||
| 59 | // $args['context']['enum'] = array( 'view', 'embed' ); |
||
| 60 | // Returns all boxes data. |
||
| 61 | register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
||
| 62 | array( |
||
| 63 | 'methods' => WP_REST_Server::READABLE, |
||
| 64 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
| 65 | 'callback' => array( $this, 'get_items' ), |
||
| 66 | 'args' => $args, |
||
| 67 | ), |
||
| 68 | 'schema' => array( $this, 'get_item_schema' ), |
||
| 69 | ) ); |
||
| 70 | |||
| 71 | $args['_rendered'] = array( |
||
| 72 | 'description' => __( 'Includes the fully rendered attributes, \'form_open\', \'form_close\', as well as the enqueued \'js_dependencies\' script handles, and \'css_dependencies\' stylesheet handles.', 'cmb2' ), |
||
| 73 | ); |
||
| 74 | |||
| 75 | // Returns specific box's data. |
||
| 76 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)', array( |
||
| 77 | array( |
||
| 78 | 'methods' => WP_REST_Server::READABLE, |
||
| 79 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
| 80 | 'callback' => array( $this, 'get_item' ), |
||
| 81 | 'args' => $args, |
||
| 82 | ), |
||
| 83 | 'schema' => array( $this, 'get_item_schema' ), |
||
| 84 | ) ); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check if a given request has access to get boxes. |
||
| 89 | * |
||
| 90 | * @since 2.2.3 |
||
| 91 | * |
||
| 92 | * @param WP_REST_Request $request Full data about the request. |
||
| 93 | * @return WP_Error|boolean |
||
| 94 | */ |
||
| 95 | public function get_items_permissions_check( $request ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get all public CMB2 boxes. |
||
| 111 | * |
||
| 112 | * @since 2.2.3 |
||
| 113 | * |
||
| 114 | * @param WP_REST_Request $request Full data about the request. |
||
| 115 | * @return WP_Error|WP_REST_Response |
||
| 116 | */ |
||
| 117 | public function get_items( $request ) { |
||
| 118 | $this->initiate_request( $request, 'boxes_read' ); |
||
| 119 | |||
| 120 | $boxes = CMB2_REST::get_all(); |
||
| 121 | if ( empty( $boxes ) ) { |
||
| 122 | return new WP_Error( 'cmb2_rest_no_boxes', __( 'No boxes found.', 'cmb2' ), array( |
||
| 123 | 'status' => 403, |
||
| 124 | ) ); |
||
| 125 | } |
||
| 126 | |||
| 127 | $boxes_data = array(); |
||
| 128 | |||
| 129 | // Loop and prepare boxes data. |
||
| 130 | foreach ( $boxes as $this->rest_box ) { |
||
| 131 | View Code Duplication | if ( |
|
| 132 | // Make sure this box can be read |
||
| 133 | $this->rest_box->rest_read |
||
| 134 | // And make sure current user can view this box. |
||
| 135 | && $this->get_item_permissions_check_filter( $this->request ) |
||
| 136 | ) { |
||
| 137 | $boxes_data[] = $this->server->response_to_data( |
||
| 138 | $this->get_rest_box(), |
||
| 139 | isset( $this->request['_embed'] ) |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $this->prepare_item( $boxes_data ); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Check if a given request has access to a box. |
||
| 149 | * By default, no special permissions needed, but filtering return value. |
||
| 150 | * |
||
| 151 | * @since 2.2.3 |
||
| 152 | * |
||
| 153 | * @param WP_REST_Request $request Full details about the request. |
||
| 154 | * @return WP_Error|boolean |
||
| 155 | */ |
||
| 156 | public function get_item_permissions_check( $request ) { |
||
| 157 | $this->initiate_rest_read_box( $request, 'box_read' ); |
||
| 158 | |||
| 159 | return $this->get_item_permissions_check_filter(); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Check by filter if a given request has access to a box. |
||
| 164 | * By default, no special permissions needed, but filtering return value. |
||
| 165 | * |
||
| 166 | * @since 2.2.3 |
||
| 167 | * |
||
| 168 | * @param bool $can_access Whether the current request has access to view the box by default. |
||
| 169 | * @return WP_Error|boolean |
||
| 170 | */ |
||
| 171 | public function get_item_permissions_check_filter( $can_access = true ) { |
||
| 172 | /** |
||
| 173 | * By default, no special permissions needed. |
||
| 174 | * |
||
| 175 | * @since 2.2.3 |
||
| 176 | * |
||
| 177 | * @param bool $can_access Whether this CMB2 endpoint can be accessed. |
||
| 178 | * @param object $controller This CMB2_REST_Controller object. |
||
| 179 | */ |
||
| 180 | return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_box_permissions_check', $can_access ); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get one CMB2 box from the collection. |
||
| 185 | * |
||
| 186 | * @since 2.2.3 |
||
| 187 | * |
||
| 188 | * @param WP_REST_Request $request Full data about the request. |
||
| 189 | * @return WP_Error|WP_REST_Response |
||
| 190 | */ |
||
| 191 | public function get_item( $request ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get a CMB2 box prepared for REST |
||
| 203 | * |
||
| 204 | * @since 2.2.3 |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function get_rest_box() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return an array of contextual links for box/boxes. |
||
| 242 | * |
||
| 243 | * @since 2.2.3 |
||
| 244 | * |
||
| 245 | * @param CMB2_REST $cmb CMB2_REST object to build links from. |
||
| 246 | * |
||
| 247 | * @return array Array of links |
||
| 248 | */ |
||
| 249 | protected function prepare_links( $cmb ) { |
||
| 269 | |||
| 270 | } |
||
| 271 |
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.