Last active
November 11, 2019 18:00
-
-
Save jtsternberg/a70e845aca44356b8fbf05aafff4d0c8 to your computer and use it in GitHub Desktop.
replace CMB2 register_rest_field handlers with our own
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Registers a new post type | |
* @uses $wp_post_types Inserts new post type object into the list | |
* | |
* @param string Post type key, must not exceed 20 characters | |
* @param array|string See optional args description above. | |
* @return object|WP_Error the registered post type object, or an error object | |
*/ | |
function jt_register_test_cpts() { | |
$labels = array( | |
'name' => __( 'BLahs', 'text-domain' ), | |
'singular_name' => __( 'BLah', 'text-domain' ), | |
'add_new' => _x( 'Add New BLah', 'text-domain', 'text-domain' ), | |
'add_new_item' => __( 'Add New BLah', 'text-domain' ), | |
'edit_item' => __( 'Edit BLah', 'text-domain' ), | |
'new_item' => __( 'New BLah', 'text-domain' ), | |
'view_item' => __( 'View BLah', 'text-domain' ), | |
'search_items' => __( 'Search BLahs', 'text-domain' ), | |
'not_found' => __( 'No BLahs found', 'text-domain' ), | |
'not_found_in_trash' => __( 'No BLahs found in Trash', 'text-domain' ), | |
'parent_item_colon' => __( 'Parent BLah:', 'text-domain' ), | |
'menu_name' => __( 'BLahs', 'text-domain' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => null, | |
'menu_icon' => 'dashicons-universal-access', | |
'show_in_nav_menus' => true, | |
'publicly_queryable' => true, | |
'exclude_from_search' => false, | |
'has_archive' => true, | |
'query_var' => true, | |
'can_export' => true, | |
'rewrite' => true, | |
'show_in_rest' => true, | |
'capability_type' => 'post', | |
'supports' => array( 'title', 'thumbnail', ), | |
); | |
register_post_type( 'blah', $args ); | |
} | |
add_action( 'init', 'jt_register_test_cpts' ); | |
/** | |
* Hook in and add a demo metabox. Can only happen on the 'cmb2_init' hook. | |
*/ | |
function jt_register_test_cpts_mb() { | |
$cmb_demo = new_cmb2_box( array( | |
'id' => 'blah_metabox', | |
'title' => __( 'BLah Metabox', 'cmb2' ), | |
'object_types' => array( 'blah', ), // Post type | |
'show_in_rest' => WP_REST_Server::ALLMETHODS, | |
'register_rest_field_cb' => 'jt_register_rest_field_cb', | |
) ); | |
$cmb_demo->add_field( array( | |
'name' => __( 'BLah URL', 'cmb2' ), | |
'id' => 'blah_url', | |
'type' => 'text_url', | |
'repeatable' => true, | |
) ); | |
} | |
add_action( 'cmb2_init', 'jt_register_test_cpts_mb' ); | |
function jt_register_rest_field_cb( $rest_box ) { | |
register_rest_field( 'blah', 'pizza', array( | |
'get_callback' => function( $object, $field_name, $request, $object_type ) use ( $rest_box ) { | |
if ( | |
'pizza' === $field_name | |
&& 'blah' === $object_type | |
&& isset( $object['id'] ) | |
) { | |
$values = array(); | |
$main_object_type = 'post'; | |
$result = CMB2_REST::get_box_rest_values( $rest_box, $object['id'], $main_object_type ); | |
if ( ! empty( $result ) ) { | |
$values[ $rest_box->cmb->cmb_id ] = $result; | |
} | |
return $values; | |
} | |
}, | |
'update_callback' => function( $values, $object, $field_name, $request, $object_type ) use ( $rest_box ) { | |
$main_object_type = 'post'; | |
if ( | |
'pizza' === $field_name | |
&& 'blah' === $object_type | |
&& ! empty( $values ) | |
&& is_array( $values ) | |
&& ( $object_id = CMB2_REST::get_object_id( $object, $main_object_type ) ) | |
) { | |
$updated = array(); | |
$result = CMB2_REST::santize_box_rest_values( $values, $rest_box, $object_id, $main_object_type ); | |
if ( ! empty( $result ) ) { | |
$updated[ $rest_box->cmb->cmb_id ] = $result; | |
} | |
return $updated; | |
} | |
}, | |
'schema' => null, | |
) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment