Complex classes like CMB2_REST often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CMB2_REST, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class CMB2_REST extends CMB2_Hookup_Base { |
||
|
|
|||
| 15 | |||
| 16 | /** |
||
| 17 | * The current CMB2 REST endpoint version |
||
| 18 | * @var string |
||
| 19 | * @since 2.2.4 |
||
| 20 | */ |
||
| 21 | const VERSION = '1'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The CMB2 REST base namespace (v should always be followed by $version) |
||
| 25 | * @var string |
||
| 26 | * @since 2.2.4 |
||
| 27 | */ |
||
| 28 | const NAME_SPACE = 'cmb2/v1'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var CMB2 object |
||
| 32 | * @since 2.2.4 |
||
| 33 | */ |
||
| 34 | public $cmb; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var CMB2_REST[] objects |
||
| 38 | * @since 2.2.4 |
||
| 39 | */ |
||
| 40 | public static $boxes; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Array of readable field objects. |
||
| 44 | * @var CMB2_Field[] |
||
| 45 | * @since 2.2.4 |
||
| 46 | */ |
||
| 47 | protected $read_fields = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Array of editable field objects. |
||
| 51 | * @var CMB2_Field[] |
||
| 52 | * @since 2.2.4 |
||
| 53 | */ |
||
| 54 | protected $edit_fields = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * whether CMB2 object is readable via the rest api. |
||
| 58 | * @var boolean |
||
| 59 | */ |
||
| 60 | protected $rest_read = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * whether CMB2 object is editable via the rest api. |
||
| 64 | * @var boolean |
||
| 65 | */ |
||
| 66 | protected $rest_edit = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Constructor |
||
| 70 | * |
||
| 71 | * @since 2.2.4 |
||
| 72 | * |
||
| 73 | * @param CMB2 $cmb The CMB2 object to be registered for the API. |
||
| 74 | */ |
||
| 75 | public function __construct( CMB2 $cmb ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Hooks to register on frontend and backend. |
||
| 87 | * |
||
| 88 | * @since 2.2.4 |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function universal_hooks() { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Initiate the CMB2 Boxes and Fields routes |
||
| 107 | * |
||
| 108 | * @since 2.2.4 |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function init_routes() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Loop through REST boxes and call register_rest_field for each object type. |
||
| 124 | * |
||
| 125 | * @since 2.2.4 |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public static function register_cmb2_fields() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Wrapper for register_rest_field. |
||
| 183 | * |
||
| 184 | * @since 2.2.4 |
||
| 185 | * |
||
| 186 | * @param string|array $object_types Object(s) the field is being registered |
||
| 187 | * to, "post"|"term"|"comment" etc. |
||
| 188 | * @param string $object_types Canonical object type for callbacks. |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | protected static function register_rest_field( $object_types, $object_type ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Setup readable and editable fields. |
||
| 202 | * |
||
| 203 | * @since 2.2.4 |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | protected function declare_read_edit_fields() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Determines if a field is readable based on it's show_in_rest value |
||
| 228 | * and the box's show_in_rest value. |
||
| 229 | * |
||
| 230 | * @since 2.2.4 |
||
| 231 | * |
||
| 232 | * @param bool $show_in_rest Field's show_in_rest value. Default null. |
||
| 233 | * |
||
| 234 | * @return bool Whether field is readable. |
||
| 235 | */ |
||
| 236 | protected function can_read( $show_in_rest ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Determines if a field is editable based on it's show_in_rest value |
||
| 248 | * and the box's show_in_rest value. |
||
| 249 | * |
||
| 250 | * @since 2.2.4 |
||
| 251 | * |
||
| 252 | * @param bool $show_in_rest Field's show_in_rest value. Default null. |
||
| 253 | * |
||
| 254 | * @return bool Whether field is editable. |
||
| 255 | */ |
||
| 256 | protected function can_edit( $show_in_rest ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Handler for getting post custom field data. |
||
| 268 | * |
||
| 269 | * @since 2.2.4 |
||
| 270 | * |
||
| 271 | * @param array $object The object data from the response |
||
| 272 | * @param string $field_name Name of field |
||
| 273 | * @param WP_REST_Request $request Current request |
||
| 274 | * @param string $object_type The request object type |
||
| 275 | * |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | public static function get_post_rest_values( $object, $field_name, $request, $object_type ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Handler for getting user custom field data. |
||
| 286 | * |
||
| 287 | * @since 2.2.4 |
||
| 288 | * |
||
| 289 | * @param array $object The object data from the response |
||
| 290 | * @param string $field_name Name of field |
||
| 291 | * @param WP_REST_Request $request Current request |
||
| 292 | * @param string $object_type The request object type |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public static function get_user_rest_values( $object, $field_name, $request, $object_type ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Handler for getting comment custom field data. |
||
| 304 | * |
||
| 305 | * @since 2.2.4 |
||
| 306 | * |
||
| 307 | * @param array $object The object data from the response |
||
| 308 | * @param string $field_name Name of field |
||
| 309 | * @param WP_REST_Request $request Current request |
||
| 310 | * @param string $object_type The request object type |
||
| 311 | * |
||
| 312 | * @return mixed |
||
| 313 | */ |
||
| 314 | public static function get_comment_rest_values( $object, $field_name, $request, $object_type ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Handler for getting term custom field data. |
||
| 322 | * |
||
| 323 | * @since 2.2.4 |
||
| 324 | * |
||
| 325 | * @param array $object The object data from the response |
||
| 326 | * @param string $field_name Name of field |
||
| 327 | * @param WP_REST_Request $request Current request |
||
| 328 | * @param string $object_type The request object type |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | public static function get_term_rest_values( $object, $field_name, $request, $object_type ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Handler for getting custom field data. |
||
| 340 | * |
||
| 341 | * @since 2.2.4 |
||
| 342 | * |
||
| 343 | * @param array $object The object data from the response |
||
| 344 | * @param WP_REST_Request $request Current request |
||
| 345 | * @param string $object_type The request object type |
||
| 346 | * @param string $main_object_type The cmb main object type |
||
| 347 | * |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | protected static function get_rest_values( $object, $request, $object_type, $main_object_type = 'post' ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Handler for updating post custom field data. |
||
| 383 | * |
||
| 384 | * @since 2.2.4 |
||
| 385 | * |
||
| 386 | * @param mixed $value The value of the field |
||
| 387 | * @param object $object The object from the response |
||
| 388 | * @param string $field_name Name of field |
||
| 389 | * @param WP_REST_Request $request Current request |
||
| 390 | * @param string $object_type The request object type |
||
| 391 | * |
||
| 392 | * @return bool|int |
||
| 393 | */ |
||
| 394 | public static function update_post_rest_values( $values, $object, $field_name, $request, $object_type ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Handler for updating user custom field data. |
||
| 402 | * |
||
| 403 | * @since 2.2.4 |
||
| 404 | * |
||
| 405 | * @param mixed $value The value of the field |
||
| 406 | * @param object $object The object from the response |
||
| 407 | * @param string $field_name Name of field |
||
| 408 | * @param WP_REST_Request $request Current request |
||
| 409 | * @param string $object_type The request object type |
||
| 410 | * |
||
| 411 | * @return bool|int |
||
| 412 | */ |
||
| 413 | public static function update_user_rest_values( $values, $object, $field_name, $request, $object_type ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Handler for updating comment custom field data. |
||
| 421 | * |
||
| 422 | * @since 2.2.4 |
||
| 423 | * |
||
| 424 | * @param mixed $value The value of the field |
||
| 425 | * @param object $object The object from the response |
||
| 426 | * @param string $field_name Name of field |
||
| 427 | * @param WP_REST_Request $request Current request |
||
| 428 | * @param string $object_type The request object type |
||
| 429 | * |
||
| 430 | * @return bool|int |
||
| 431 | */ |
||
| 432 | public static function update_comment_rest_values( $values, $object, $field_name, $request, $object_type ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Handler for updating term custom field data. |
||
| 440 | * |
||
| 441 | * @since 2.2.4 |
||
| 442 | * |
||
| 443 | * @param mixed $value The value of the field |
||
| 444 | * @param object $object The object from the response |
||
| 445 | * @param string $field_name Name of field |
||
| 446 | * @param WP_REST_Request $request Current request |
||
| 447 | * @param string $object_type The request object type |
||
| 448 | * |
||
| 449 | * @return bool|int |
||
| 450 | */ |
||
| 451 | public static function update_term_rest_values( $values, $object, $field_name, $request, $object_type ) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Handler for updating custom field data. |
||
| 459 | * |
||
| 460 | * @since 2.2.4 |
||
| 461 | * |
||
| 462 | * @param mixed $value The value of the field |
||
| 463 | * @param object $object The object from the response |
||
| 464 | * @param WP_REST_Request $request Current request |
||
| 465 | * @param string $object_type The request object type |
||
| 466 | * @param string $main_object_type The cmb main object type |
||
| 467 | * |
||
| 468 | * @return bool|int |
||
| 469 | */ |
||
| 470 | protected static function update_rest_values( $values, $object, $request, $object_type, $main_object_type = 'post' ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Loop through box fields and sanitize the values. |
||
| 501 | * |
||
| 502 | * @since 2.2.o |
||
| 503 | * |
||
| 504 | * @param array $values Array of values being provided. |
||
| 505 | * @return array Array of updated/sanitized values. |
||
| 506 | */ |
||
| 507 | public function sanitize_box_values( array $values ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Handles returning a sanitized field value. |
||
| 523 | * |
||
| 524 | * @since 2.2.4 |
||
| 525 | * |
||
| 526 | * @param array $values Array of values being provided. |
||
| 527 | * @param string $field_id The id of the field to update. |
||
| 528 | * |
||
| 529 | * @return mixed The results of saving/sanitizing a field value. |
||
| 530 | */ |
||
| 531 | protected function sanitize_field_value( array $values, $field_id ) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Handles returning a sanitized group field value. |
||
| 554 | * |
||
| 555 | * @since 2.2.4 |
||
| 556 | * |
||
| 557 | * @param array $values Array of values being provided. |
||
| 558 | * @param CMB2_Field $field CMB2_Field object. |
||
| 559 | * |
||
| 560 | * @return mixed The results of saving/sanitizing the group field value. |
||
| 561 | */ |
||
| 562 | protected function sanitize_group_value( array $values, CMB2_Field $field ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Filter whether a meta key is protected. |
||
| 575 | * |
||
| 576 | * @since 2.2.4 |
||
| 577 | * |
||
| 578 | * @param bool $protected Whether the key is protected. Default false. |
||
| 579 | * @param string $meta_key Meta key. |
||
| 580 | * @param string $meta_type Meta type. |
||
| 581 | */ |
||
| 582 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
| 589 | |||
| 590 | protected static function get_object_data( $object ) { |
||
| 612 | |||
| 613 | public function field_can_read( $field_id, $return_object = false ) { |
||
| 616 | |||
| 617 | public function field_can_edit( $field_id, $return_object = false ) { |
||
| 620 | |||
| 621 | protected function field_can( $type = 'read_fields', $field_id, $return_object = false ) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get a CMB2_REST instance object from the registry by a CMB2 id. |
||
| 631 | * |
||
| 632 | * @since 2.2.4 |
||
| 633 | * |
||
| 634 | * @param string $cmb_id CMB2 config id |
||
| 635 | * |
||
| 636 | * @return CMB2_REST|false The CMB2_REST object or false. |
||
| 637 | */ |
||
| 638 | public static function get_rest_box( $cmb_id ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Remove a CMB2_REST instance object from the registry. |
||
| 644 | * |
||
| 645 | * @since 2.2.4 |
||
| 646 | * |
||
| 647 | * @param string $cmb_id A CMB2 instance id. |
||
| 648 | */ |
||
| 649 | public static function remove( $cmb_id ) { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Checks if given value is readable. |
||
| 657 | * |
||
| 658 | * Value is considered readable if it is not empty and if it does not match the editable blacklist. |
||
| 659 | * |
||
| 660 | * @since 2.2.4 |
||
| 661 | * |
||
| 662 | * @param mixed $value Value to check. |
||
| 663 | * |
||
| 664 | * @return boolean Whether value is considered readable. |
||
| 665 | */ |
||
| 666 | public static function is_readable( $value ) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Checks if given value is editable. |
||
| 676 | * |
||
| 677 | * Value is considered editable if matches the editable whitelist. |
||
| 678 | * |
||
| 679 | * @since 2.2.4 |
||
| 680 | * |
||
| 681 | * @param mixed $value Value to check. |
||
| 682 | * |
||
| 683 | * @return boolean Whether value is considered editable. |
||
| 684 | */ |
||
| 685 | public static function is_editable( $value ) { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Magic getter for our object. |
||
| 694 | * |
||
| 695 | * @param string $field |
||
| 696 | * @throws Exception Throws an exception if the field is invalid. |
||
| 697 | * |
||
| 698 | * @return mixed |
||
| 699 | */ |
||
| 700 | public function __get( $field ) { |
||
| 711 | |||
| 712 | } |
||
| 713 |
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.