CMB2.php000064400000152073151717007230005753 0ustar00 '', 'title' => '', // Post type slug, or 'user', 'term', 'comment', or 'options-page'. 'object_types' => array(), /** * The context within the screen where the boxes should display. Available contexts vary * from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. * * For placement in locations outside of a metabox, other options include: * 'form_top', 'before_permalink', 'after_title', 'after_editor' * * Comments screen contexts include 'normal' and 'side'. Default is 'normal'. */ 'context' => 'normal', 'priority' => 'high', // Or 10 for options pages. 'show_names' => true, // Show field names on the left. 'show_on_cb' => null, // Callback to determine if metabox should display. 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb'. 'cmb_styles' => true, // Include CMB2 stylesheet. 'enqueue_js' => true, // Include CMB2 JS. 'fields' => array(), /** * Handles hooking CMB2 forms/metaboxes into the post/attachement/user/options-page screens * and handles hooking in and saving those fields. */ 'hookup' => true, 'save_fields' => true, // Will not save during hookup if false. 'closed' => false, // Default metabox to being closed. 'taxonomies' => array(), 'new_user_section' => 'add-new-user', // or 'add-existing-user'. 'new_term_section' => true, 'show_in_rest' => false, 'classes' => null, // Optionally add classes to the CMB2 wrapper. 'classes_cb' => '', // Optionally add classes to the CMB2 wrapper (via a callback). /* * The following parameter is for post alternate-context metaboxes only. * * To output the fields 'naked' (without a postbox wrapper/style), then * add a `'remove_box_wrap' => true` to your metabox registration array. */ 'remove_box_wrap' => false, /* * The following parameter is any additional arguments passed as $callback_args * to add_meta_box, if/when applicable. * * CMB2 does not use these arguments in the add_meta_box callback, however, these args * are parsed for certain special properties, like determining Gutenberg/block-editor * compatibility. * * Examples: * * - Make sure default editor is used as metabox is not compatible with block editor * [ '__block_editor_compatible_meta_box' => false/true ] * * - Or declare this box exists for backwards compatibility * [ '__back_compat_meta_box' => false ] * * More: https://wordpress.org/gutenberg/handbook/extensibility/meta-box/ */ 'mb_callback_args' => null, /* * The following parameters are for options-page metaboxes, * and several are passed along to add_menu_page()/add_submenu_page() */ // 'menu_title' => null, // Falls back to 'title' (above). Do not define here so we can set a fallback. 'message_cb' => '', // Optionally define the options-save message (via a callback). 'option_key' => '', // The actual option key and admin menu page slug. 'parent_slug' => '', // Used as first param in add_submenu_page(). 'capability' => 'manage_options', // Cap required to view options-page. 'icon_url' => '', // Menu icon. Only applicable if 'parent_slug' is left empty. 'position' => null, // Menu position. Only applicable if 'parent_slug' is left empty. 'admin_menu_hook' => 'admin_menu', // Alternately 'network_admin_menu' to add network-level options page. 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()). 'save_button' => '', // The text for the options-page save button. Defaults to 'Save'. 'disable_settings_errors' => false, // On settings pages (not options-general.php sub-pages), allows disabling. 'tab_group' => '', // Tab-group identifier, enables options page tab navigation. // 'tab_title' => null, // Falls back to 'title' (above). Do not define here so we can set a fallback. // 'autoload' => true, // Defaults to true, the options-page option will be autloaded. ); /** * Metabox field objects * * @var array * @since 2.0.3 */ protected $fields = array(); /** * An array of hidden fields to output at the end of the form * * @var array * @since 2.0.0 */ protected $hidden_fields = array(); /** * Array of key => value data for saving. Likely $_POST data. * * @var string * @since 2.0.0 */ protected $generated_nonce = ''; /** * Whether there are fields to be shown in columns. Set in CMB2::add_field(). * * @var bool * @since 2.2.2 */ protected $has_columns = false; /** * If taxonomy field is requesting to remove_default, we store the taxonomy here. * * @var array * @since 2.2.3 */ protected $tax_metaboxes_to_remove = array(); /** * Get started * * @since 0.4.0 * @param array $config Metabox config array. * @param integer $object_id Optional object id. */ public function __construct( $config, $object_id = 0 ) { if ( empty( $config['id'] ) ) { wp_die( esc_html__( 'Metabox configuration is required to have an ID parameter.', 'cmb2' ) ); } $this->cmb_id = $config['id']; $this->meta_box = wp_parse_args( $config, $this->mb_defaults ); $this->meta_box['fields'] = array(); // Ensures object_types is an array. $this->set_prop( 'object_types', $this->box_types() ); $this->object_id( $object_id ); if ( $this->is_options_page_mb() ) { // Check initial priority. if ( empty( $config['priority'] ) ) { // If not explicitly defined, Reset the priority to 10 // Fixes https://github.com/CMB2/CMB2/issues/1410. $this->meta_box['priority'] = 10; } $this->init_options_mb(); } $this->mb_object_type(); if ( ! empty( $config['fields'] ) && is_array( $config['fields'] ) ) { $this->add_fields( $config['fields'] ); } CMB2_Boxes::add( $this ); /** * Hook during initiation of CMB2 object * * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. * * @param array $cmb This CMB2 object */ do_action( "cmb2_init_{$this->cmb_id}", $this ); // Hook in the hookup... how meta. add_action( "cmb2_init_hookup_{$this->cmb_id}", array( 'CMB2_Hookup', 'maybe_init_and_hookup' ) ); // Hook in the rest api functionality. add_action( "cmb2_init_hookup_{$this->cmb_id}", array( 'CMB2_REST', 'maybe_init_and_hookup' ) ); } /** * Loops through and displays fields * * @since 1.0.0 * @param int $object_id Object ID. * @param string $object_type Type of object being saved. (e.g., post, user, or comment). * * @return CMB2 */ public function show_form( $object_id = 0, $object_type = '' ) { $this->render_form_open( $object_id, $object_type ); foreach ( $this->prop( 'fields' ) as $field_args ) { $this->render_field( $field_args ); } return $this->render_form_close( $object_id, $object_type ); } /** * Outputs the opening form markup and runs corresponding hooks: * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" * * @since 2.2.0 * @param integer $object_id Object ID. * @param string $object_type Object type. * * @return CMB2 */ public function render_form_open( $object_id = 0, $object_type = '' ) { $object_type = $this->object_type( $object_type ); $object_id = $this->object_id( $object_id ); echo "\n\n"; $this->nonce_field(); /** * Hook before form table begins * * @param array $cmb_id The current box ID. * @param int $object_id The ID of the current object. * @param string $object_type The type of object you are working with. * Usually `post` (this applies to all post-types). * Could also be `comment`, `user` or `options-page`. * @param array $cmb This CMB2 object. */ do_action( 'cmb2_before_form', $this->cmb_id, $object_id, $object_type, $this ); /** * Hook before form table begins * * The first dynamic portion of the hook name, $object_type, is the type of object * you are working with. Usually `post` (this applies to all post-types). * Could also be `comment`, `user` or `options-page`. * * The second dynamic portion of the hook name, $this->cmb_id, is the meta_box id. * * @param array $cmb_id The current box ID * @param int $object_id The ID of the current object * @param array $cmb This CMB2 object */ do_action( "cmb2_before_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); echo '
'; return $this; } /** * Defines the classes for the CMB2 form/wrap. * * @since 2.0.0 * @return string Space concatenated list of classes */ public function box_classes() { $classes = array( 'cmb2-wrap', 'form-table' ); // Use the callback to fetch classes. if ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); $classes = array_merge( $classes, $added_classes ); } if ( $added_classes = $this->prop( 'classes' ) ) { $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); $classes = array_merge( $classes, $added_classes ); } /** * Add our context classes for non-standard metaboxes. * * @since 2.2.4 */ if ( $this->is_alternate_context_box() ) { $context = array(); // Include custom class if requesting no title. if ( ! $this->prop( 'title' ) && ! $this->prop( 'remove_box_wrap' ) ) { $context[] = 'cmb2-context-wrap-no-title'; } // Include a generic context wrapper. $context[] = 'cmb2-context-wrap'; // Include a context-type based context wrapper. $context[] = 'cmb2-context-wrap-' . $this->prop( 'context' ); // Include an ID based context wrapper as well. $context[] = 'cmb2-context-wrap-' . $this->prop( 'id' ); // And merge all the classes back into the array. $classes = array_merge( $classes, $context ); } /** * Globally filter box wrap classes * * @since 2.2.2 * * @param string $classes Array of classes for the cmb2-wrap. * @param CMB2 $cmb This CMB2 object. */ $classes = apply_filters( 'cmb2_wrap_classes', $classes, $this ); $split = array(); foreach ( array_filter( $classes ) as $class ) { foreach ( explode( ' ', $class ) as $_class ) { // Clean up & sanitize. $split[] = sanitize_html_class( strip_tags( $_class ) ); } } $classes = $split; // Remove any duplicates. $classes = array_unique( $classes ); // Make it a string. return implode( ' ', $classes ); } /** * Outputs the closing form markup and runs corresponding hooks: * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" * * @since 2.2.0 * @param integer $object_id Object ID. * @param string $object_type Object type. * * @return CMB2 */ public function render_form_close( $object_id = 0, $object_type = '' ) { $object_type = $this->object_type( $object_type ); $object_id = $this->object_id( $object_id ); echo '
'; $this->render_hidden_fields(); /** * Hook after form form has been rendered * * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. * * The first dynamic portion of the hook name, $object_type, is the type of object * you are working with. Usually `post` (this applies to all post-types). * Could also be `comment`, `user` or `options-page`. * * @param int $object_id The ID of the current object * @param array $cmb This CMB2 object */ do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); /** * Hook after form form has been rendered * * @param array $cmb_id The current box ID. * @param int $object_id The ID of the current object. * @param string $object_type The type of object you are working with. * Usually `post` (this applies to all post-types). * Could also be `comment`, `user` or `options-page`. * @param array $cmb This CMB2 object. */ do_action( 'cmb2_after_form', $this->cmb_id, $object_id, $object_type, $this ); echo "\n\n"; return $this; } /** * Renders a field based on the field type * * @since 2.2.0 * @param array $field_args A field configuration array. * @return mixed CMB2_Field object if successful. */ public function render_field( $field_args ) { $field_args['context'] = $this->prop( 'context' ); if ( 'group' === $field_args['type'] ) { if ( ! isset( $field_args['show_names'] ) ) { $field_args['show_names'] = $this->prop( 'show_names' ); } $field = $this->render_group( $field_args ); } elseif ( 'hidden' === $field_args['type'] && $this->get_field( $field_args )->should_show() ) { // Save rendering for after the metabox. $field = $this->add_hidden_field( $field_args ); } else { $field_args['show_names'] = $this->prop( 'show_names' ); // Render default fields. $field = $this->get_field( $field_args )->render_field(); } return $field; } /** * Render a group of fields. * * @param array|CMB2_Field $args Array of field arguments for a group field parent or the group parent field. * @return CMB2_Field|null Group field object. */ public function render_group( $args ) { $field_group = false; if ( $args instanceof CMB2_Field ) { $field_group = 'group' === $args->type() ? $args : false; } elseif ( isset( $args['id'], $args['fields'] ) && is_array( $args['fields'] ) ) { $field_group = $this->get_field( $args ); } if ( ! $field_group ) { return; } $field_group->render_context = 'edit'; $field_group->peform_param_callback( 'render_row_cb' ); return $field_group; } /** * The default callback to render a group of fields. * * @since 2.2.6 * * @param array $field_args Array of field arguments for the group field parent. * @param CMB2_Field $field_group The CMB2_Field group object. * * @return CMB2_Field|null Group field object. */ public function render_group_callback( $field_args, $field_group ) { // If field is requesting to be conditionally shown. if ( ! $field_group || ! $field_group->should_show() ) { return; } $field_group->index = 0; $field_group->peform_param_callback( 'before_group' ); $desc = $field_group->args( 'description' ); $label = $field_group->args( 'name' ); $group_val = (array) $field_group->value(); echo '
group_wrap_attributes( $field_group ), '>'; if ( $desc || $label ) { $class = $desc ? ' cmb-group-description' : ''; echo '
'; if ( $label ) { echo '

', $label, '

'; } if ( $desc ) { echo '

', $desc, '

'; } echo '
'; } if ( ! empty( $group_val ) ) { foreach ( $group_val as $group_key => $field_id ) { $this->render_group_row( $field_group ); $field_group->index++; } } else { $this->render_group_row( $field_group ); } if ( $field_group->args( 'repeatable' ) ) { echo '

'; } echo '
'; $field_group->peform_param_callback( 'after_group' ); return $field_group; } /** * Get the group wrap attributes, which are passed through a filter. * * @since 2.2.3 * @param CMB2_Field $field_group The group CMB2_Field object. * @return string The attributes string. */ public function group_wrap_attributes( $field_group ) { $classes = 'cmb-nested cmb-field-list cmb-repeatable-group'; $classes .= $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; $classes .= $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; $group_wrap_attributes = array( 'class' => $classes, 'style' => 'width:100%;', ); /** * Allow for adding additional HTML attributes to a group wrapper. * * The attributes will be an array of key => value pairs for each attribute. * * @since 2.2.2 * * @param string $group_wrap_attributes Current attributes array. * @param CMB2_Field $field_group The group CMB2_Field object. */ $group_wrap_attributes = apply_filters( 'cmb2_group_wrap_attributes', $group_wrap_attributes, $field_group ); $atts = array(); foreach ( $group_wrap_attributes as $att => $att_value ) { if ( ! CMB2_Utils::is_data_attribute( $att ) ) { $att_value = htmlspecialchars( $att_value, ENT_COMPAT ); } $atts[ sanitize_html_class( $att ) ] = sanitize_text_field( $att_value ); } return CMB2_Utils::concat_attrs( $atts ); } /** * Render a repeatable group row * * @since 1.0.2 * @param CMB2_Field $field_group CMB2_Field group field object. * * @return CMB2 */ public function render_group_row( $field_group ) { $field_group->peform_param_callback( 'before_group_row' ); $closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; $confirm_deletion = $field_group->options( 'remove_confirm' ); $confirm_deletion = ! empty( $confirm_deletion ) ? $confirm_deletion : ''; echo '
'; if ( $field_group->args( 'repeatable' ) ) { echo ''; } echo '

', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '

'; // Loop and render repeatable group fields. foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { if ( 'hidden' === $field_args['type'] ) { // Save rendering for after the metabox. $this->add_hidden_field( $field_args, $field_group ); } else { $field_args['show_names'] = $field_group->args( 'show_names' ); $field_args['context'] = $field_group->args( 'context' ); $this->get_field( $field_args, $field_group )->render_field(); } } if ( $field_group->args( 'repeatable' ) ) { echo '
'; } echo '
'; $field_group->peform_param_callback( 'after_group_row' ); return $this; } /** * Add a hidden field to the list of hidden fields to be rendered later. * * @since 2.0.0 * * @param array $field_args Array of field arguments to be passed to CMB2_Field. * @param CMB2_Field|null $field_group CMB2_Field group field object. * @return CMB2_Field */ public function add_hidden_field( $field_args, $field_group = null ) { if ( isset( $field_args['field_args'] ) ) { // For back-compatibility. $field = new CMB2_Field( $field_args ); } else { $field = $this->get_new_field( $field_args, $field_group ); } $types = new CMB2_Types( $field ); if ( $field_group ) { $types->iterator = $field_group->index; } $this->hidden_fields[] = $types; return $field; } /** * Loop through and output hidden fields * * @since 2.0.0 * * @return CMB2 */ public function render_hidden_fields() { if ( ! empty( $this->hidden_fields ) ) { foreach ( $this->hidden_fields as $hidden ) { $hidden->render(); } } return $this; } /** * Returns array of sanitized field values (without saving them) * * @since 2.0.3 * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). * @return mixed */ public function get_sanitized_values( array $data_to_sanitize ) { $this->data_to_save = $data_to_sanitize; $stored_id = $this->object_id(); // We do this So CMB will sanitize our data for us, but not save it. $this->object_id( '_' ); // Ensure temp. data store is empty. cmb2_options( 0 )->set(); // We want to get any taxonomy values back. add_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' ); // Process/save fields. $this->process_fields(); // Put things back the way they were. remove_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' ); // Get data from temp. data store. $sanitized_values = cmb2_options( 0 )->get_options(); // Empty out temp. data store again. cmb2_options( 0 )->set(); // Reset the object id. $this->object_id( $stored_id ); return $sanitized_values; } /** * Loops through and saves field data * * @since 1.0.0 * @param int $object_id Object ID. * @param string $object_type Type of object being saved. (e.g., post, user, or comment). * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. * * @return CMB2 */ public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { // Fall-back to $_POST data. $this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST; $object_id = $this->object_id( $object_id ); $object_type = $this->object_type( $object_type ); $this->process_fields(); // If options page, save the updated options. if ( 'options-page' === $object_type ) { cmb2_options( $object_id )->set(); } return $this->after_save(); } /** * Process and save form fields * * @since 2.0.0 * * @return CMB2 */ public function process_fields() { $this->pre_process(); // Remove the show_on properties so saving works. $this->prop( 'show_on', array() ); // save field ids of those that are updated. $this->updated = array(); foreach ( $this->prop( 'fields' ) as $field_args ) { $this->process_field( $field_args ); } return $this; } /** * Process and save a field * * @since 2.0.0 * @param array $field_args Array of field arguments. * * @return CMB2 */ public function process_field( $field_args ) { switch ( $field_args['type'] ) { case 'group': if ( $this->save_group( $field_args ) ) { $this->updated[] = $field_args['id']; } break; case 'title': // Don't process title fields. break; default: $field = $this->get_new_field( $field_args ); if ( $field->save_field_from_data( $this->data_to_save ) ) { $this->updated[] = $field->id(); } break; } return $this; } /** * Fires the "cmb2_{$object_type}_process_fields_{$cmb_id}" action hook. * * @since 2.2.2 * * @return CMB2 */ public function pre_process() { $object_type = $this->object_type(); /** * Fires before fields have been processed/saved. * * The dynamic portion of the hook name, $object_type, refers to the * metabox/form's object type * Usually `post` (this applies to all post-types). * Could also be `comment`, `user` or `options-page`. * * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. * * @param array $cmb This CMB2 object * @param int $object_id The ID of the current object */ do_action( "cmb2_{$object_type}_process_fields_{$this->cmb_id}", $this, $this->object_id() ); return $this; } /** * Fires the "cmb2_save_{$object_type}_fields" and * "cmb2_save_{$object_type}_fields_{$cmb_id}" action hooks. * * @since 2.x.x * * @return CMB2 */ public function after_save() { $object_type = $this->object_type(); $object_id = $this->object_id(); /** * Fires after all fields have been saved. * * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type * Usually `post` (this applies to all post-types). * Could also be `comment`, `user` or `options-page`. * * @param int $object_id The ID of the current object * @param array $cmb_id The current box ID * @param string $updated Array of field ids that were updated. * Will only include field ids that had values change. * @param array $cmb This CMB2 object */ do_action( "cmb2_save_{$object_type}_fields", $object_id, $this->cmb_id, $this->updated, $this ); /** * Fires after all fields have been saved. * * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. * * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type * Usually `post` (this applies to all post-types). * Could also be `comment`, `user` or `options-page`. * * @param int $object_id The ID of the current object * @param string $updated Array of field ids that were updated. * Will only include field ids that had values change. * @param array $cmb This CMB2 object */ do_action( "cmb2_save_{$object_type}_fields_{$this->cmb_id}", $object_id, $this->updated, $this ); return $this; } /** * Save a repeatable group * * @since 1.x.x * @param array $args Field arguments array. * @return mixed Return of CMB2_Field::update_data(). */ public function save_group( $args ) { if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { return; } return $this->save_group_field( $this->get_new_field( $args ) ); } /** * Save a repeatable group * * @since 1.x.x * @param CMB2_Field $field_group CMB2_Field group field object. * @return mixed Return of CMB2_Field::update_data(). */ public function save_group_field( $field_group ) { $base_id = $field_group->id(); if ( ! isset( $this->data_to_save[ $base_id ] ) ) { return; } $old = $field_group->get_data(); // Check if group field has sanitization_cb. $group_vals = $field_group->sanitization_cb( $this->data_to_save[ $base_id ] ); $saved = array(); $field_group->index = 0; $field_group->data_to_save = $this->data_to_save; foreach ( array_values( $field_group->fields() ) as $field_args ) { if ( 'title' === $field_args['type'] ) { // Don't process title fields. continue; } $field = $this->get_new_field( $field_args, $field_group ); $sub_id = $field->id( true ); if ( empty( $saved[ $field_group->index ] ) ) { $saved[ $field_group->index ] = array(); } foreach ( (array) $group_vals as $field_group->index => $post_vals ) { // Get value. $new_val = isset( $group_vals[ $field_group->index ][ $sub_id ] ) ? $group_vals[ $field_group->index ][ $sub_id ] : false; // Sanitize. $new_val = $field->sanitization_cb( $new_val ); if ( is_array( $new_val ) && $field->args( 'has_supporting_data' ) ) { if ( $field->args( 'repeatable' ) ) { $_new_val = array(); foreach ( $new_val as $group_index => $grouped_data ) { // Add the supporting data to the $saved array stack. $saved[ $field_group->index ][ $grouped_data['supporting_field_id'] ][] = $grouped_data['supporting_field_value']; // Reset var to the actual value. $_new_val[ $group_index ] = $grouped_data['value']; } $new_val = $_new_val; } else { // Add the supporting data to the $saved array stack. $saved[ $field_group->index ][ $new_val['supporting_field_id'] ] = $new_val['supporting_field_value']; // Reset var to the actual value. $new_val = $new_val['value']; } } // Get old value. $old_val = is_array( $old ) && isset( $old[ $field_group->index ][ $sub_id ] ) ? $old[ $field_group->index ][ $sub_id ] : false; $is_updated = ( ! CMB2_Utils::isempty( $new_val ) && $new_val !== $old_val ); $is_removed = ( CMB2_Utils::isempty( $new_val ) && ! CMB2_Utils::isempty( $old_val ) ); // Compare values and add to `$updated` array. if ( $is_updated || $is_removed ) { $this->updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id; } // Add to `$saved` array. $saved[ $field_group->index ][ $sub_id ] = $new_val; }// End foreach. $saved[ $field_group->index ] = CMB2_Utils::filter_empty( $saved[ $field_group->index ] ); }// End foreach. $saved = CMB2_Utils::filter_empty( $saved ); return $field_group->update_data( $saved, true ); } /** * Get object id from global space if no id is provided * * @since 1.0.0 * @param integer|string $object_id Object ID. * @return integer|string $object_id Object ID. */ public function object_id( $object_id = 0 ) { global $pagenow; if ( $object_id ) { $this->object_id = $object_id; return $this->object_id; } if ( $this->object_id ) { return $this->object_id; } // Try to get our object ID from the global space. switch ( $this->object_type() ) { case 'user': $object_id = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['user_id'] ) ) : $object_id; $object_id = ! $object_id && 'user-new.php' !== $pagenow && isset( $GLOBALS['user_ID'] ) ? $GLOBALS['user_ID'] : $object_id; break; case 'comment': $object_id = isset( $_REQUEST['c'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['c'] ) ) : $object_id; $object_id = ! $object_id && isset( $GLOBALS['comments']->comment_ID ) ? $GLOBALS['comments']->comment_ID : $object_id; break; case 'term': $object_id = isset( $_REQUEST['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tag_ID'] ) ) : $object_id; break; case 'options-page': $key = $this->doing_options_page(); if ( ! empty( $key ) ) { $object_id = $key; } break; default: $object_id = isset( $GLOBALS['post']->ID ) ? $GLOBALS['post']->ID : $object_id; $object_id = isset( $_REQUEST['post'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post'] ) ) : $object_id; break; } /** * Filter the object id. * * @since 2.11.0 * * @param integer|string $object_id Object ID. * @param CMB2 $cmb This CMB2 object. */ $object_id = apply_filters( 'cmb2_set_object_id', $object_id, $this ); // reset to id or 0. $this->object_id = ! empty( $object_id ) ? $object_id : 0; return $this->object_id; } /** * Sets the $object_type based on metabox settings * * @since 1.0.0 * @return string Object type. */ public function mb_object_type() { if ( null !== $this->mb_object_type ) { return $this->mb_object_type; } $found_type = ''; if ( $this->is_options_page_mb() ) { $found_type = 'options-page'; } else { $registered_types = $this->box_types(); // if it's an array of one, extract it. if ( 1 === count( $registered_types ) ) { $last = end( $registered_types ); if ( is_string( $last ) ) { $found_type = $last; } } else { $current_object_type = $this->current_object_type(); if ( in_array( $current_object_type, $registered_types, true ) ) { $found_type = $current_object_type; } } } // Get our object type. $mb_object_type = $this->is_supported_core_object_type( $found_type ) ? $found_type : 'post'; /** * Filter the metabox object type. * * @since 2.11.0 * * @param string $mb_object_type The metabox object type. * @param string $found_type The found object type. * @param CMB2 $cmb This CMB2 object. */ $this->mb_object_type = apply_filters( 'cmb2_set_box_object_type', $mb_object_type, $found_type, $this ); return $this->mb_object_type; } /** * Gets the box 'object_types' array based on box settings. * * @since 2.2.3 * @param array $fallback Fallback value. * * @return array Object types. */ public function box_types( $fallback = array() ) { return CMB2_Utils::ensure_array( $this->prop( 'object_types' ), $fallback ); } /** * Check if given object_type(s) matches any of the registered object types or * taxonomies for this box. * * @since 2.7.0 * @param string|array $object_types The object type(s) to check. * @param array $fallback Fallback object_types value. * * @return bool Whether given object type(s) are registered to this box. */ public function is_box_type( $object_types = array(), $fallback = array() ) { $object_types = (array) $object_types; $box_types = $this->box_types( $fallback ); if ( in_array( 'term', $box_types, true ) ) { $taxonomies = CMB2_Utils::ensure_array( $this->prop( 'taxonomies' ) ); $box_types = array_merge( $box_types, $taxonomies ); } $found = array_intersect( $object_types, $box_types ); return ! empty( $found ); } /** * Initates the object types and option key for an options page metabox. * * @since 2.2.5 * * @return void */ public function init_options_mb() { $keys = $this->options_page_keys(); $types = $this->box_types(); if ( empty( $keys ) ) { $keys = ''; $types = $this->deinit_options_mb( $types ); } else { // Make sure 'options-page' is one of the object types. $types[] = 'options-page'; } // Set/Reset the option_key property. $this->set_prop( 'option_key', $keys ); // Reset the object types. $this->set_prop( 'object_types', array_unique( $types ) ); } /** * If object-page initiation failed, remove traces options page setup. * * @since 2.2.5 * * @param array $types Array of types. * @return array */ protected function deinit_options_mb( $types ) { if ( isset( $this->meta_box['show_on']['key'] ) && 'options-page' === $this->meta_box['show_on']['key'] ) { unset( $this->meta_box['show_on']['key'] ); } if ( array_key_exists( 'options-page', $this->meta_box['show_on'] ) ) { unset( $this->meta_box['show_on']['options-page'] ); } $index = array_search( 'options-page', $types ); if ( false !== $index ) { unset( $types[ $index ] ); } return $types; } /** * Determines if metabox is for an options page * * @since 1.0.1 * @return boolean True/False. */ public function is_options_page_mb() { return ( // 'show_on' values checked for back-compatibility. $this->is_old_school_options_page_mb() || in_array( 'options-page', $this->box_types() ) ); } /** * Determines if metabox uses old-schoold options page config. * * @since 2.2.5 * @return boolean True/False. */ public function is_old_school_options_page_mb() { return ( // 'show_on' values checked for back-compatibility. isset( $this->meta_box['show_on']['key'] ) && 'options-page' === $this->meta_box['show_on']['key'] || array_key_exists( 'options-page', $this->meta_box['show_on'] ) ); } /** * Determine if we are on an options page (or saving the options page). * * @since 2.2.5 * * @return bool */ public function doing_options_page() { $found_key = false; $keys = $this->options_page_keys(); if ( empty( $keys ) ) { return $found_key; } if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], $keys ) ) { $found_key = $_GET['page']; } if ( ! empty( $_POST['action'] ) && in_array( $_POST['action'], $keys ) ) { $found_key = $_POST['action']; } return $found_key ? $found_key : false; } /** * Get the options page key. * * @since 2.2.5 * @return string|array */ public function options_page_keys() { $key = ''; if ( ! $this->is_options_page_mb() ) { return $key; } $values = null; if ( ! empty( $this->meta_box['show_on']['value'] ) ) { $values = $this->meta_box['show_on']['value']; } elseif ( ! empty( $this->meta_box['show_on']['options-page'] ) ) { $values = $this->meta_box['show_on']['options-page']; } elseif ( $this->prop( 'option_key' ) ) { $values = $this->prop( 'option_key' ); } if ( $values ) { $key = $values; } if ( ! is_array( $key ) ) { $key = array( $key ); } return $key; } /** * Returns the object type * * @since 1.0.0 * @param string $object_type Type of object being saved. (e.g., post, user, or comment). Optional. * @return string Object type. */ public function object_type( $object_type = '' ) { if ( $object_type ) { $this->object_type = $object_type; return $this->object_type; } if ( $this->object_type ) { return $this->object_type; } $this->object_type = $this->current_object_type(); return $this->object_type; } /** * Get the object type for the current page, based on the $pagenow global. * * @since 2.2.2 * @return string Page object type name. */ public function current_object_type() { global $pagenow; $type = 'post'; if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) { $type = 'user'; } if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) { $type = 'comment'; } if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) { $type = 'term'; } if ( defined( 'DOING_AJAX' ) && isset( $_POST['action'] ) && 'add-tag' === $_POST['action'] ) { $type = 'term'; } if ( in_array( $pagenow, array( 'admin.php', 'admin-post.php' ), true ) && $this->doing_options_page() ) { $type = 'options-page'; } return $type; } /** * Set metabox property. * * @since 2.2.2 * @param string $property Metabox config property to retrieve. * @param mixed $value Value to set if no value found. * @return mixed Metabox config property value or false. */ public function set_prop( $property, $value ) { $this->meta_box[ $property ] = $value; return $this->prop( $property ); } /** * Get metabox property and optionally set a fallback * * @since 2.0.0 * @param string $property Metabox config property to retrieve. * @param mixed $fallback Fallback value to set if no value found. * @return mixed Metabox config property value or false. */ public function prop( $property, $fallback = null ) { if ( array_key_exists( $property, $this->meta_box ) ) { return $this->meta_box[ $property ]; } elseif ( $fallback ) { return $this->meta_box[ $property ] = $fallback; } } /** * Get a field object * * @since 2.0.3 * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object. * @param CMB2_Field|null $field_group (optional) CMB2_Field object (group parent). * @param bool $reset_cached (optional) Reset the internal cache for this field object. * Use sparingly. * * @return CMB2_Field|false CMB2_Field object (or false). */ public function get_field( $field, $field_group = null, $reset_cached = false ) { if ( $field instanceof CMB2_Field ) { return $field; } $field_id = is_string( $field ) ? $field : $field['id']; $parent_field_id = ! empty( $field_group ) ? $field_group->id() : ''; $ids = $this->get_field_ids( $field_id, $parent_field_id ); if ( ! $ids ) { return false; } list( $field_id, $sub_field_id ) = $ids; $index = $field_id . ( $sub_field_id ? '|' . $sub_field_id : '' ) . ( $field_group ? '|' . $field_group->index : '' ); if ( array_key_exists( $index, $this->fields ) && ! $reset_cached ) { return $this->fields[ $index ]; } $this->fields[ $index ] = new CMB2_Field( $this->get_field_args( $field_id, $field, $sub_field_id, $field_group ) ); return $this->fields[ $index ]; } /** * Handles determining which type of arguments to pass to CMB2_Field * * @since 2.0.7 * @param mixed $field_id Field (or group field) ID. * @param mixed $field_args Array of field arguments. * @param mixed $sub_field_id Sub field ID (if field_group exists). * @param CMB2_Field|null $field_group If a sub-field, will be the parent group CMB2_Field object. * @return array Array of CMB2_Field arguments. */ public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { // Check if group is passed and if fields were added in the old-school fields array. if ( $field_group && ( $sub_field_id || 0 === $sub_field_id ) ) { // Update the fields array w/ any modified properties inherited from the group field. $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] = $field_args; return $this->get_default_args( $field_args, $field_group ); } if ( is_array( $field_args ) ) { $this->meta_box['fields'][ $field_id ] = array_merge( $field_args, $this->meta_box['fields'][ $field_id ] ); } return $this->get_default_args( $this->meta_box['fields'][ $field_id ] ); } /** * Get default field arguments specific to this CMB2 object. * * @since 2.2.0 * @param array $field_args Metabox field config array. * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent). * @return array Array of field arguments. */ protected function get_default_args( $field_args, $field_group = null ) { if ( $field_group ) { $args = array( 'field_args' => $field_args, 'group_field' => $field_group, ); } else { $args = array( 'field_args' => $field_args, 'object_type' => $this->object_type(), 'object_id' => $this->object_id(), 'cmb_id' => $this->cmb_id, ); } return $args; } /** * When fields are added in the old-school way, intitate them as they should be * * @since 2.1.0 * @param array $fields Array of fields to add. * @param mixed $parent_field_id Parent field id or null. * * @return CMB2 */ protected function add_fields( $fields, $parent_field_id = null ) { foreach ( $fields as $field ) { $sub_fields = false; if ( array_key_exists( 'fields', $field ) ) { $sub_fields = $field['fields']; unset( $field['fields'] ); } $field_id = $parent_field_id ? $this->add_group_field( $parent_field_id, $field ) : $this->add_field( $field ); if ( $sub_fields ) { $this->add_fields( $sub_fields, $field_id ); } } return $this; } /** * Add a field to the metabox * * @since 2.0.0 * @param array $field Metabox field config array. * @param int $position (optional) Position of metabox. 1 for first, etc. * @return string|false Field id or false. */ public function add_field( array $field, $position = 0 ) { if ( ! array_key_exists( 'id', $field ) ) { return false; } $this->_add_field_to_array( $field, $this->meta_box['fields'], $position ); return $field['id']; } /** * Add a field to a group * * @since 2.0.0 * @param string $parent_field_id The field id of the group field to add the field. * @param array $field Metabox field config array. * @param int $position (optional) Position of metabox. 1 for first, etc. * @return mixed Array of parent/field ids or false. */ public function add_group_field( $parent_field_id, array $field, $position = 0 ) { if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) { return false; } $parent_field = $this->meta_box['fields'][ $parent_field_id ]; if ( 'group' !== $parent_field['type'] ) { return false; } if ( ! isset( $parent_field['fields'] ) ) { $this->meta_box['fields'][ $parent_field_id ]['fields'] = array(); } $this->_add_field_to_array( $field, $this->meta_box['fields'][ $parent_field_id ]['fields'], $position ); return array( $parent_field_id, $field['id'] ); } /** * Perform some field-type-specific initiation actions. * * @since 2.7.0 * @param array $field Metabox field config array. * @return void */ protected function field_actions( $field ) { $field = CMB2_Hookup_Field::init( $field, $this ); if ( isset( $field['column'] ) && false !== $field['column'] ) { $field = $this->define_field_column( $field ); } if ( isset( $field['taxonomy'] ) && ! empty( $field['remove_default'] ) ) { $this->tax_metaboxes_to_remove[ $field['taxonomy'] ] = $field['taxonomy']; } return $field; } /** * Defines a field's column if requesting to be show in admin columns. * * @since 2.2.3 * @param array $field Metabox field config array. * @return array Modified metabox field config array. */ protected function define_field_column( array $field ) { $this->has_columns = true; $column = is_array( $field['column'] ) ? $field['column'] : array(); $field['column'] = wp_parse_args( $column, array( 'name' => isset( $field['name'] ) ? $field['name'] : '', 'position' => false, ) ); return $field; } /** * Add a field array to a fields array in desired position * * @since 2.0.2 * @param array $field Metabox field config array. * @param array $fields Array (passed by reference) to append the field (array) to. * @param integer $position Optionally specify a position in the array to be inserted. */ protected function _add_field_to_array( $field, &$fields, $position = 0 ) { $field = $this->field_actions( $field ); if ( $position ) { CMB2_Utils::array_insert( $fields, array( $field['id'] => $field ), $position ); } else { $fields[ $field['id'] ] = $field; } } /** * Remove a field from the metabox * * @since 2.0.0 * @param string $field_id The field id of the field to remove. * @param string $parent_field_id (optional) The field id of the group field to remove field from. * @return bool True if field was removed. */ public function remove_field( $field_id, $parent_field_id = '' ) { $ids = $this->get_field_ids( $field_id, $parent_field_id ); if ( ! $ids ) { return false; } list( $field_id, $sub_field_id ) = $ids; unset( $this->fields[ implode( '', $ids ) ] ); if ( ! $sub_field_id ) { unset( $this->meta_box['fields'][ $field_id ] ); return true; } if ( isset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] ) ) { unset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] ); } if ( isset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] ) ) { unset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] ); } return true; } /** * Update or add a property to a field * * @since 2.0.0 * @param string $field_id Field id. * @param string $property Field property to set/update. * @param mixed $value Value to set the field property. * @param string $parent_field_id (optional) The field id of the group field to remove field from. * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0). */ public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { $ids = $this->get_field_ids( $field_id, $parent_field_id ); if ( ! $ids ) { return false; } list( $field_id, $sub_field_id ) = $ids; if ( ! $sub_field_id ) { $this->meta_box['fields'][ $field_id ][ $property ] = $value; return $field_id; } $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ][ $property ] = $value; return $field_id; } /** * Check if field ids match a field and return the index/field id * * @since 2.0.2 * @param string $field_id Field id. * @param string $parent_field_id (optional) Parent field id. * @return mixed Array of field/parent ids, or false. */ public function get_field_ids( $field_id, $parent_field_id = '' ) { $sub_field_id = $parent_field_id ? $field_id : ''; $field_id = $parent_field_id ? $parent_field_id : $field_id; $fields =& $this->meta_box['fields']; if ( ! array_key_exists( $field_id, $fields ) ) { $field_id = $this->search_old_school_array( $field_id, $fields ); } if ( false === $field_id ) { return false; } if ( ! $sub_field_id ) { return array( $field_id, $sub_field_id ); } if ( 'group' !== $fields[ $field_id ]['type'] ) { return false; } if ( ! array_key_exists( $sub_field_id, $fields[ $field_id ]['fields'] ) ) { $sub_field_id = $this->search_old_school_array( $sub_field_id, $fields[ $field_id ]['fields'] ); } return false === $sub_field_id ? false : array( $field_id, $sub_field_id ); } /** * When using the old array filter, it is unlikely field array indexes will be the field id. * * @since 2.0.2 * @param string $field_id The field id. * @param array $fields Array of fields to search. * @return mixed Field index or false. */ public function search_old_school_array( $field_id, $fields ) { $ids = wp_list_pluck( $fields, 'id' ); $index = array_search( $field_id, $ids ); return false !== $index ? $index : false; } /** * Handles metabox property callbacks, and passes this $cmb object as property. * * @since 2.2.3 * @param callable $cb The callback method/function/closure. * @param mixed $additional_params Any additoinal parameters which should be passed to the callback. * @return mixed Return of the callback function. */ public function do_callback( $cb, $additional_params = null ) { return call_user_func( $cb, $this, $additional_params ); } /** * Generate a unique nonce field for each registered meta_box * * @since 2.0.0 * @return void */ public function nonce_field() { wp_nonce_field( $this->nonce(), $this->nonce(), false, true ); } /** * Generate a unique nonce for each registered meta_box * * @since 2.0.0 * @return string unique nonce string. */ public function nonce() { if ( ! $this->generated_nonce ) { $this->generated_nonce = sanitize_html_class( 'nonce_' . basename( __FILE__ ) . $this->cmb_id ); } return $this->generated_nonce; } /** * Checks if field-saving updated any fields. * * @since 2.2.5 * * @return bool */ public function was_updated() { return ! empty( $this->updated ); } /** * Whether this box is an "alternate context" box. This means the box has a 'context' property defined as: * 'form_top', 'before_permalink', 'after_title', or 'after_editor'. * * @since 2.2.4 * @return bool */ public function is_alternate_context_box() { return $this->prop( 'context' ) && in_array( $this->prop( 'context' ), array( 'form_top', 'before_permalink', 'after_title', 'after_editor' ), true ); } /** * Whether given object type is one of the core supported object types. * * @since 2.11.0 * @return bool */ public function is_supported_core_object_type( $object_type ) { return in_array( $object_type, $this->core_object_types, true ); } /** * Magic getter for our object. * * @param string $property Object property. * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __get( $property ) { switch ( $property ) { case 'updated': case 'has_columns': case 'tax_metaboxes_to_remove': case 'core_object_types': return $this->{$property}; default: return parent::__get( $property ); } } } CMB2_Ajax.php000064400000022051151717007230006706 0ustar00' . esc_html__( 'Please Try Again', 'cmb2' ) . '

' ); } // Set width of embed. $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640'; // Set url. $oembed_url = esc_url( $oembed_string ); // Set args. $embed_args = array( 'width' => $embed_width, ); $this->ajax_update = true; // Get embed code (or fallback link). $html = $this->get_oembed( array( 'url' => $oembed_url, 'object_id' => $_REQUEST['object_id'], 'object_type' => isset( $_REQUEST['object_type'] ) ? $_REQUEST['object_type'] : 'post', 'oembed_args' => $embed_args, 'field_id' => $_REQUEST['field_id'], ) ); wp_send_json_success( $html ); } /** * Retrieves oEmbed from url/object ID * * @since 0.9.5 * @param array $args Arguments for method. * @return mixed HTML markup with embed or fallback. */ public function get_oembed_no_edit( $args ) { global $wp_embed; $oembed_url = esc_url( $args['url'] ); // Sanitize object_id. $this->object_id = is_numeric( $args['object_id'] ) ? absint( $args['object_id'] ) : sanitize_text_field( $args['object_id'] ); $args = wp_parse_args( $args, array( 'object_type' => 'post', 'oembed_args' => array(), 'field_id' => false, 'wp_error' => false, ) ); $this->embed_args =& $args; /* * Set the post_ID so oEmbed won't fail * wp-includes/class-wp-embed.php, WP_Embed::shortcode() */ $wp_embed->post_ID = $this->object_id; // Special scenario if NOT a post object. if ( isset( $args['object_type'] ) && 'post' != $args['object_type'] ) { if ( 'options-page' == $args['object_type'] ) { // Bogus id to pass some numeric checks. Issue with a VERY large WP install? $wp_embed->post_ID = 1987645321; } // Ok, we need to hijack the oembed cache system. $this->hijack = true; $this->object_type = $args['object_type']; // Gets ombed cache from our object's meta (vs postmeta). add_filter( 'get_post_metadata', array( $this, 'hijack_oembed_cache_get' ), 10, 3 ); // Sets ombed cache in our object's meta (vs postmeta). add_filter( 'update_post_metadata', array( $this, 'hijack_oembed_cache_set' ), 10, 4 ); } $embed_args = ''; foreach ( $args['oembed_args'] as $key => $val ) { $embed_args .= " $key=\"$val\""; } // Ping WordPress for an embed. $embed = $wp_embed->run_shortcode( '[embed' . $embed_args . ']' . $oembed_url . '[/embed]' ); // Fallback that WordPress creates when no oEmbed was found. $fallback = $wp_embed->maybe_make_link( $oembed_url ); return compact( 'embed', 'fallback', 'args' ); } /** * Retrieves oEmbed from url/object ID * * @since 0.9.5 * @param array $args Arguments for method. * @return string HTML markup with embed or fallback. */ public function get_oembed( $args ) { $oembed = $this->get_oembed_no_edit( $args ); // Send back our embed. if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) { return '
' . $oembed['embed'] . '

' . esc_html__( 'Remove Embed', 'cmb2' ) . '

'; } // Otherwise, send back error info that no oEmbeds were found. return sprintf( '

%s

', sprintf( /* translators: 1: results for. 2: link to codex.wordpress.org/Embeds */ esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ), $oembed['fallback'], 'codex.wordpress.org/Embeds' ) ); } /** * Hijacks retrieving of cached oEmbed. * Returns cached data from relevant object metadata (vs postmeta) * * @since 0.9.5 * @param boolean $check Whether to retrieve postmeta or override. * @param int $object_id Object ID. * @param string $meta_key Object metakey. * @return mixed Object's oEmbed cached data. */ public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) { return $check; } if ( $this->ajax_update ) { return false; } return $this->cache_action( $meta_key ); } /** * Hijacks saving of cached oEmbed. * Saves cached data to relevant object metadata (vs postmeta) * * @since 0.9.5 * @param boolean $check Whether to continue setting postmeta. * @param int $object_id Object ID to get postmeta from. * @param string $meta_key Postmeta's key. * @param mixed $meta_value Value of the postmeta to be saved. * @return boolean Whether to continue setting. */ public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) // Only want to hijack oembed meta values. || 0 !== strpos( $meta_key, '_oembed_' ) ) { return $check; } $this->cache_action( $meta_key, $meta_value ); // Anything other than `null` to cancel saving to postmeta. return true; } /** * Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta). * * @since 1.3.0 * * @param string $meta_key Postmeta's key. * @return mixed */ protected function cache_action( $meta_key ) { $func_args = func_get_args(); $action = isset( $func_args[1] ) ? 'update' : 'get'; if ( 'options-page' === $this->object_type ) { $args = array( $meta_key ); if ( 'update' === $action ) { $args[] = $func_args[1]; $args[] = true; } // Cache the result to our options. $status = call_user_func_array( array( cmb2_options( $this->object_id ), $action ), $args ); } else { $args = array( $this->object_type, $this->object_id, $meta_key ); $args[] = 'update' === $action ? $func_args[1] : true; // Cache the result to our metadata. $status = call_user_func_array( $action . '_metadata', $args ); } return $status; } /** * Hooks in when options-page data is saved to clean stale * oembed cache data from the option value. * * @since 2.2.0 * @param string $option_key The options-page option key. * @return void */ public static function clean_stale_options_page_oembeds( $option_key ) { $options = cmb2_options( $option_key )->get_options(); $modified = false; if ( is_array( $options ) ) { $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, '', array(), 0 ); $now = time(); foreach ( $options as $key => $value ) { // Check for cached oembed data. if ( 0 === strpos( $key, '_oembed_time_' ) ) { $cached_recently = ( $now - $value ) < $ttl; if ( ! $cached_recently ) { $modified = true; // Remove the the cached ttl expiration, and the cached oembed value. unset( $options[ $key ] ); unset( $options[ str_replace( '_oembed_time_', '_oembed_', $key ) ] ); } } // End if. // Remove the cached unknown values. elseif ( '{{unknown}}' === $value ) { $modified = true; unset( $options[ $key ] ); } } } // Update the option and remove stale cache data. if ( $modified ) { $updated = cmb2_options( $option_key )->set( $options ); } } } CMB2_Base.php000064400000036134151717007230006704 0ustar00 value data for saving. Likely $_POST data. * * @var array * @since 2.2.3 */ public $data_to_save = array(); /** * Array of field param callback results * * @var array * @since 2.0.0 */ protected $callback_results = array(); /** * The deprecated_param method deprecated param message signature. */ const DEPRECATED_PARAM = 1; /** * The deprecated_param method deprecated callback param message signature. */ const DEPRECATED_CB_PARAM = 2; /** * Get started * * @since 2.2.3 * @param array $args Object properties array. */ public function __construct( $args = array() ) { if ( ! empty( $args ) ) { foreach ( array( 'cmb_id', 'properties_name', 'object_id', 'object_type', 'data_to_save', ) as $object_prop ) { if ( isset( $args[ $object_prop ] ) ) { $this->{$object_prop} = $args[ $object_prop ]; } } } } /** * Returns the object ID * * @since 2.2.3 * @param integer $object_id Object ID. * @return integer Object ID */ public function object_id( $object_id = 0 ) { if ( $object_id ) { $this->object_id = $object_id; } return $this->object_id; } /** * Returns the object type * * @since 2.2.3 * @param string $object_type Object Type. * @return string Object type */ public function object_type( $object_type = '' ) { if ( $object_type ) { $this->object_type = $object_type; } return $this->object_type; } /** * Get the object type for the current page, based on the $pagenow global. * * @since 2.2.2 * @return string Page object type name. */ public function current_object_type() { global $pagenow; $type = 'post'; if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) { $type = 'user'; } if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) { $type = 'comment'; } if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) { $type = 'term'; } return $type; } /** * Set object property. * * @since 2.2.2 * @param string $property Metabox config property to retrieve. * @param mixed $value Value to set if no value found. * @return mixed Metabox config property value or false. */ public function set_prop( $property, $value ) { $this->{$this->properties_name}[ $property ] = $value; return $this->prop( $property ); } /** * Get object property and optionally set a fallback * * @since 2.0.0 * @param string $property Metabox config property to retrieve. * @param mixed $fallback Fallback value to set if no value found. * @return mixed Metabox config property value or false */ public function prop( $property, $fallback = null ) { if ( array_key_exists( $property, $this->{$this->properties_name} ) ) { return $this->{$this->properties_name}[ $property ]; } elseif ( $fallback ) { return $this->{$this->properties_name}[ $property ] = $fallback; } } /** * Get default field arguments specific to this CMB2 object. * * @since 2.2.0 * @param array $field_args Metabox field config array. * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent). * @return array Array of field arguments. */ protected function get_default_args( $field_args, $field_group = null ) { if ( $field_group ) { $args = array( 'field_args' => $field_args, 'group_field' => $field_group, ); } else { $args = array( 'field_args' => $field_args, 'object_type' => $this->object_type(), 'object_id' => $this->object_id(), 'cmb_id' => $this->cmb_id, ); } return $args; } /** * Get a new field object specific to this CMB2 object. * * @since 2.2.0 * @param array $field_args Metabox field config array. * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent). * @return CMB2_Field CMB2_Field object */ protected function get_new_field( $field_args, $field_group = null ) { return new CMB2_Field( $this->get_default_args( $field_args, $field_group ) ); } /** * Determine whether this cmb object should show, based on the 'show_on_cb' callback. * * @since 2.0.9 * * @return bool Whether this cmb should be shown. */ public function should_show() { // Default to showing this cmb $show = true; // Use the callback to determine showing the cmb, if it exists. if ( is_callable( $this->prop( 'show_on_cb' ) ) ) { $show = (bool) call_user_func( $this->prop( 'show_on_cb' ), $this ); } return $show; } /** * Displays the results of the param callbacks. * * @since 2.0.0 * @param string $param Field parameter. */ public function peform_param_callback( $param ) { echo $this->get_param_callback_result( $param ); } /** * Store results of the param callbacks for continual access * * @since 2.0.0 * @param string $param Field parameter. * @return mixed Results of param/param callback */ public function get_param_callback_result( $param ) { // If we've already retrieved this param's value. if ( array_key_exists( $param, $this->callback_results ) ) { // Send it back. return $this->callback_results[ $param ]; } // Check if parameter has registered a callback. if ( $cb = $this->maybe_callback( $param ) ) { // Ok, callback is good, let's run it and store the result. ob_start(); $returned = $this->do_callback( $cb ); // Grab the result from the output buffer and store it. $echoed = ob_get_clean(); // This checks if the user returned or echoed their callback. // Defaults to using the echoed value. $this->callback_results[ $param ] = $echoed ? $echoed : $returned; } else { // Otherwise just get whatever is there. $this->callback_results[ $param ] = isset( $this->{$this->properties_name}[ $param ] ) ? $this->{$this->properties_name}[ $param ] : false; } return $this->callback_results[ $param ]; } /** * Unset the cached results of the param callback. * * @since 2.2.6 * @param string $param Field parameter. * @return CMB2_Base */ public function unset_param_callback_cache( $param ) { if ( isset( $this->callback_results[ $param ] ) ) { unset( $this->callback_results[ $param ] ); } return $this; } /** * Handles the parameter callbacks, and passes this object as parameter. * * @since 2.2.3 * @param callable $cb The callback method/function/closure. * @param mixed $additional_params Any additoinal parameters which should be passed to the callback. * @return mixed Return of the callback function. */ protected function do_callback( $cb, $additional_params = null ) { return call_user_func( $cb, $this->{$this->properties_name}, $this, $additional_params ); } /** * Checks if field has a callback value * * @since 1.0.1 * @param string $cb Callback string. * @return mixed NULL, false for NO validation, or $cb string if it exists. */ public function maybe_callback( $cb ) { $args = $this->{$this->properties_name}; if ( ! isset( $args[ $cb ] ) ) { return null; } // Check if requesting explicitly false. $cb = false !== $args[ $cb ] && 'false' !== $args[ $cb ] ? $args[ $cb ] : false; // If requesting NO validation, return false. if ( ! $cb ) { return false; } if ( is_callable( $cb ) ) { return $cb; } return null; } /** * Checks if this object has parameter corresponding to the given filter * which is callable. If so, it registers the callback, and if not, * converts the maybe-modified $val to a boolean for return. * * The registered handlers will have a parameter name which matches the filter, except: * - The 'cmb2_api' prefix will be removed * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). * * @since 2.2.3 * * @param string $hook_name The hook name. * @param bool $val The default value. * @param string $hook_function The hook function. Default: 'add_filter'. * * @return null|bool Null if hook is registered, or bool for value. */ public function maybe_hook_parameter( $hook_name, $val = null, $hook_function = 'add_filter' ) { // Remove filter prefix, add param suffix. $parameter = substr( $hook_name, strlen( 'cmb2_api_' ) ) . '_cb'; return self::maybe_hook( $this->prop( $parameter, $val ), $hook_name, $hook_function ); } /** * Checks if given value is callable, and registers the callback. * If is non-callable, converts the $val to a boolean for return. * * @since 2.2.3 * * @param bool $val The default value. * @param string $hook_name The hook name. * @param string $hook_function The hook function. * * @return null|bool Null if hook is registered, or bool for value. */ public static function maybe_hook( $val, $hook_name, $hook_function ) { if ( is_callable( $val ) ) { call_user_func( $hook_function, $hook_name, $val, 10, 2 ); return null; } // Cast to bool. return ! ! $val; } /** * Mark a param as deprecated and inform when it has been used. * * There is a default WordPress hook deprecated_argument_run that will be called * that can be used to get the backtrace up to what file and function used the * deprecated argument. * * The current behavior is to trigger a user error if WP_DEBUG is true. * * @since 2.2.3 * * @param string $function The function that was called. * @param string $version The version of CMB2 that deprecated the argument used. * @param string $message Optional. A message regarding the change, or numeric * key to generate message from additional arguments. * Default null. */ protected function deprecated_param( $function, $version, $message = null ) { $args = func_get_args(); if ( is_numeric( $message ) ) { switch ( $message ) { case self::DEPRECATED_PARAM: $message = sprintf( __( 'The "%1$s" field parameter has been deprecated in favor of the "%2$s" parameter.', 'cmb2' ), $args[3], $args[4] ); break; case self::DEPRECATED_CB_PARAM: $message = sprintf( __( 'Using the "%1$s" field parameter as a callback has been deprecated in favor of the "%2$s" parameter.', 'cmb2' ), $args[3], $args[4] ); break; default: $message = null; break; } } /** * Fires when a deprecated argument is called. This is a WP core action. * * @since 2.2.3 * * @param string $function The function that was called. * @param string $message A message regarding the change. * @param string $version The version of CMB2 that deprecated the argument used. */ do_action( 'deprecated_argument_run', $function, $message, $version ); /** * Filters whether to trigger an error for deprecated arguments. This is a WP core filter. * * @since 2.2.3 * * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true. */ if ( defined( 'WP_DEBUG' ) && WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) { if ( function_exists( '__' ) ) { if ( ! is_null( $message ) ) { trigger_error( sprintf( __( '%1$s was called with a parameter that is deprecated since version %2$s! %3$s', 'cmb2' ), $function, $version, $message ) ); } else { trigger_error( sprintf( __( '%1$s was called with a parameter that is deprecated since version %2$s with no alternative available.', 'cmb2' ), $function, $version ) ); } } else { if ( ! is_null( $message ) ) { trigger_error( sprintf( '%1$s was called with a parameter that is deprecated since version %2$s! %3$s', $function, $version, $message ) ); } else { trigger_error( sprintf( '%1$s was called with a parameter that is deprecated since version %2$s with no alternative available.', $function, $version ) ); } } } } /** * Magic getter for our object. * * @param string $field Requested property. * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __get( $field ) { switch ( $field ) { case 'args': case 'meta_box': if ( $field === $this->properties_name ) { return $this->{$this->properties_name}; } case 'properties': return $this->{$this->properties_name}; case 'cmb_id': case 'object_id': case 'object_type': return $this->{$field}; default: throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); } } /** * Allows overloading the object with methods... Whooaaa oooh it's magic, y'knoooow. * * @since 1.0.0 * @throws Exception Invalid method exception. * * @param string $method Non-existent method. * @param array $args All arguments passed to the method. * @return mixed */ public function __call( $method, $args ) { $object_class = strtolower( get_class( $this ) ); if ( ! has_filter( "{$object_class}_inherit_{$method}" ) ) { throw new Exception( sprintf( esc_html__( 'Invalid %1$s method: %2$s', 'cmb2' ), get_class( $this ), $method ) ); } array_unshift( $args, $this ); /** * Allows overloading the object (CMB2 or CMB2_Field) with additional capabilities * by registering hook callbacks. * * The first dynamic portion of the hook name, $object_class, refers to the object class, * either cmb2 or cmb2_field. * * The second dynamic portion of the hook name, $method, is the non-existent method being * called on the object. To avoid possible future methods encroaching on your hooks, * use a unique method (aka, $cmb->prefix_my_method()). * * When registering your callback, you will need to ensure that you register the correct * number of `$accepted_args`, accounting for this object instance being the first argument. * * @param array $args The arguments to be passed to the hook. * The first argument will always be this object instance. */ return apply_filters_ref_array( "{$object_class}_inherit_{$method}", $args ); } } CMB2_Boxes.php000064400000006244151717007230007111 0ustar00cmb_id ] = $cmb_instance; } /** * Remove a CMB2 instance object from the registry. * * @since 1.X.X * * @param string $cmb_id A CMB2 instance id. */ public static function remove( $cmb_id ) { if ( array_key_exists( $cmb_id, self::$cmb2_instances ) ) { unset( self::$cmb2_instances[ $cmb_id ] ); } } /** * Retrieve a CMB2 instance by cmb id. * * @since 1.X.X * * @param string $cmb_id A CMB2 instance id. * * @return CMB2|bool False or CMB2 object instance. */ public static function get( $cmb_id ) { if ( empty( self::$cmb2_instances ) || empty( self::$cmb2_instances[ $cmb_id ] ) ) { return false; } return self::$cmb2_instances[ $cmb_id ]; } /** * Retrieve all CMB2 instances registered. * * @since 1.X.X * @return CMB2[] Array of all registered cmb2 instances. */ public static function get_all() { return self::$cmb2_instances; } /** * Retrieve all CMB2 instances that have the specified property set. * * @since 2.4.0 * @param string $property Property name. * @param mixed $compare (Optional) The value to compare. * @return CMB2[] Array of matching cmb2 instances. */ public static function get_by( $property, $compare = 'nocompare' ) { $boxes = array(); foreach ( self::$cmb2_instances as $cmb_id => $cmb ) { $prop = $cmb->prop( $property ); if ( 'nocompare' === $compare ) { if ( ! empty( $prop ) ) { $boxes[ $cmb_id ] = $cmb; } continue; } if ( $compare === $prop ) { $boxes[ $cmb_id ] = $cmb; } } return $boxes; } /** * Retrieve all CMB2 instances as long as they do not include the ignored property. * * @since 2.4.0 * @param string $property Property name. * @param mixed $to_ignore The value to ignore. * @return CMB2[] Array of matching cmb2 instances. */ public static function filter_by( $property, $to_ignore = null ) { $boxes = array(); foreach ( self::$cmb2_instances as $cmb_id => $cmb ) { if ( $to_ignore === $cmb->prop( $property ) ) { continue; } $boxes[ $cmb_id ] = $cmb; } return $boxes; } /** * Deprecated and left for back-compatibility. The original `get_by_property` * method was misnamed and never actually used by CMB2 core. * * @since 2.2.3 * * @param string $property Property name. * @param mixed $to_ignore The value to ignore. * @return CMB2[] Array of matching cmb2 instances. */ public static function get_by_property( $property, $to_ignore = null ) { _deprecated_function( __METHOD__, '2.4.0', 'CMB2_Boxes::filter_by()' ); return self::filter_by( $property ); } } CMB2_Field.php000064400000133415151717007230007055 0ustar00group = $args['group_field']; $this->object_id = $this->group->object_id; $this->object_type = $this->group->object_type; $this->cmb_id = $this->group->cmb_id; } else { $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; if ( isset( $args['cmb_id'] ) ) { $this->cmb_id = $args['cmb_id']; } } $this->args = $this->_set_field_defaults( $args['field_args'] ); if ( $this->object_id ) { $this->value = $this->get_data(); } } /** * Non-existent methods fallback to checking for field arguments of the same name * * @since 1.1.0 * @param string $name Method name. * @param array $arguments Array of passed-in arguments. * @return mixed Value of field argument */ public function __call( $name, $arguments ) { if ( 'string' === $name ) { return call_user_func_array( array( $this, 'get_string' ), $arguments ); } $key = isset( $arguments[0] ) ? $arguments[0] : ''; return $this->args( $name, $key ); } /** * Retrieves the field id * * @since 1.1.0 * @param boolean $raw Whether to retrieve pre-modidifed id. * @return string Field id */ public function id( $raw = false ) { $id = $raw ? '_id' : 'id'; return $this->args( $id ); } /** * Get a field argument * * @since 1.1.0 * @param string $key Argument to check. * @param string $_key Sub argument to check. * @return mixed Argument value or false if non-existent */ public function args( $key = '', $_key = '' ) { $arg = $this->_data( 'args', $key ); if ( in_array( $key, array( 'default', 'default_cb' ), true ) ) { $arg = $this->get_default(); } elseif ( $_key ) { $arg = isset( $arg[ $_key ] ) ? $arg[ $_key ] : false; } return $arg; } /** * Retrieve a portion of a field property * * @since 1.1.0 * @param string $var Field property to check. * @param string $key Field property array key to check. * @return mixed Queried property value or false */ public function _data( $var, $key = '' ) { $vars = $this->{$var}; if ( $key ) { return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; } return $vars; } /** * Get Field's value * * @since 1.1.0 * @param string $key If value is an array, is used to get array key->value. * @return mixed Field value or false if non-existent */ public function value( $key = '' ) { return $this->_data( 'value', $key ); } /** * Retrieves metadata/option data * * @since 1.0.1 * @param string $field_id Meta key/Option array key. * @param array $args Override arguments. * @return mixed Meta/Option value */ public function get_data( $field_id = '', $args = array() ) { if ( $field_id ) { $args['field_id'] = $field_id; } elseif ( $this->group ) { $args['field_id'] = $this->group->id(); } $a = $this->data_args( $args ); /** * Filter whether to override getting of meta value. * Returning a non 'cmb2_field_no_override_val' value * will effectively short-circuit the value retrieval. * * @since 2.0.0 * * @param mixed $value The value get_metadata() should * return - a single metadata value, * or an array of values. * * @param int $object_id Object ID. * * @param array $args { * An array of arguments for retrieving data * * @type string $type The current object type * @type int $id The current object ID * @type string $field_id The ID of the field being requested * @type bool $repeat Whether current field is repeatable * @type bool $single Whether current field is a single database row * } * * @param CMB2_Field object $field This field object */ $data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this ); /** * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above). * * The dynamic portion of the hook, $field_id, refers to the current * field id paramater. Returning a non 'cmb2_field_no_override_val' value * will effectively short-circuit the value retrieval. * * @since 2.0.0 */ $data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this ); // If no override, get value normally. if ( 'cmb2_field_no_override_val' === $data ) { $data = 'options-page' === $a['type'] ? cmb2_options( $a['id'] )->get( $a['field_id'] ) : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) ); } if ( $this->group ) { $data = is_array( $data ) && isset( $data[ $this->group->index ][ $this->args( '_id' ) ] ) ? $data[ $this->group->index ][ $this->args( '_id' ) ] : false; } return $data; } /** * Updates metadata/option data. * * @since 1.0.1 * @param mixed $new_value Value to update data with. * @param bool $single Whether data is an array (add_metadata). * @return mixed */ public function update_data( $new_value, $single = true ) { $a = $this->data_args( array( 'single' => $single, ) ); $a['value'] = $a['repeat'] ? array_values( $new_value ) : $new_value; /** * Filter whether to override saving of meta value. * Returning a non-null value will effectively short-circuit the function. * * @since 2.0.0 * * @param null|bool $check Whether to allow updating metadata for the given type. * * @param array $args { * Array of data about current field including: * * @type string $value The value to set * @type string $type The current object type * @type int $id The current object ID * @type string $field_id The ID of the field being updated * @type bool $repeat Whether current field is repeatable * @type bool $single Whether current field is a single database row * } * * @param array $field_args All field arguments * * @param CMB2_Field object $field This field object */ $override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this ); /** * Filter and parameters are documented for 'cmb2_override_meta_save' filter (above). * * The dynamic portion of the hook, $a['field_id'], refers to the current * field id paramater. Returning a non-null value * will effectively short-circuit the function. * * @since 2.0.0 */ $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_save", $override, $a, $this->args(), $this ); // If override, return that. if ( null !== $override ) { return $override; } // Options page handling (or temp data store). if ( 'options-page' === $a['type'] || empty( $a['id'] ) ) { return cmb2_options( $a['id'] )->update( $a['field_id'], $a['value'], false, $a['single'] ); } // Add metadata if not single. if ( ! $a['single'] ) { return add_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'], false ); } // Delete meta if we have an empty array. if ( is_array( $a['value'] ) && empty( $a['value'] ) ) { return delete_metadata( $a['type'], $a['id'], $a['field_id'], $this->value ); } // Update metadata. return update_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'] ); } /** * Removes/updates metadata/option data. * * @since 1.0.1 * @param string $old Old value. * @return mixed */ public function remove_data( $old = '' ) { $a = $this->data_args( array( 'old' => $old, ) ); /** * Filter whether to override removing of meta value. * Returning a non-null value will effectively short-circuit the function. * * @since 2.0.0 * * @param null|bool $delete Whether to allow metadata deletion of the given type. * @param array $args Array of data about current field including: * 'type' : Current object type * 'id' : Current object ID * 'field_id' : Current Field ID * 'repeat' : Whether current field is repeatable * 'single' : Whether to save as a * single meta value * @param array $field_args All field arguments * @param CMB2_Field object $field This field object */ $override = apply_filters( 'cmb2_override_meta_remove', null, $a, $this->args(), $this ); /** * Filter whether to override removing of meta value. * * The dynamic portion of the hook, $a['field_id'], refers to the current * field id paramater. Returning a non-null value * will effectively short-circuit the function. * * @since 2.0.0 * * @param null|bool $delete Whether to allow metadata deletion of the given type. * @param array $args Array of data about current field including: * 'type' : Current object type * 'id' : Current object ID * 'field_id' : Current Field ID * 'repeat' : Whether current field is repeatable * 'single' : Whether to save as a * single meta value * @param array $field_args All field arguments * @param CMB2_Field object $field This field object */ $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_remove", $override, $a, $this->args(), $this ); // If no override, remove as usual. if ( null !== $override ) { return $override; } // End if. // Option page handling. elseif ( 'options-page' === $a['type'] || empty( $a['id'] ) ) { return cmb2_options( $a['id'] )->remove( $a['field_id'] ); } // Remove metadata. return delete_metadata( $a['type'], $a['id'], $a['field_id'], $old ); } /** * Data variables for get/set data methods * * @since 1.1.0 * @param array $args Override arguments. * @return array Updated arguments */ public function data_args( $args = array() ) { $args = wp_parse_args( $args, array( 'type' => $this->object_type, 'id' => $this->object_id, 'field_id' => $this->id( true ), 'repeat' => $this->args( 'repeatable' ), 'single' => ! $this->args( 'multiple' ), ) ); return $args; } /** * Checks if field has a registered sanitization callback * * @since 1.0.1 * @param mixed $meta_value Meta value. * @return mixed Possibly sanitized meta value */ public function sanitization_cb( $meta_value ) { if ( $this->args( 'repeatable' ) && is_array( $meta_value ) ) { // Remove empties. $meta_value = array_filter( $meta_value ); } // Check if the field has a registered validation callback. $cb = $this->maybe_callback( 'sanitization_cb' ); if ( false === $cb ) { // If requesting NO validation, return meta value. return $meta_value; } elseif ( $cb ) { // Ok, callback is good, let's run it. return call_user_func( $cb, $meta_value, $this->args(), $this ); } $sanitizer = new CMB2_Sanitize( $this, $meta_value ); $field_type = $this->type(); /** * Filter the value before it is saved. * * The dynamic portion of the hook name, $field_type, refers to the field type. * * Passing a non-null value to the filter will short-circuit saving * the field value, saving the passed value instead. * * @param bool|mixed $override_value Sanitization/Validation override value to return. * Default: null. false to skip it. * @param mixed $value The value to be saved to this field. * @param int $object_id The ID of the object where the value will be saved * @param array $field_args The current field's arguments * @param object $sanitizer This `CMB2_Sanitize` object */ $override_value = apply_filters( "cmb2_sanitize_{$field_type}", null, $sanitizer->value, $this->object_id, $this->args(), $sanitizer ); if ( null !== $override_value ) { return $override_value; } // Sanitization via 'CMB2_Sanitize'. return $sanitizer->{$field_type}(); } /** * Process $_POST data to save this field's value * * @since 2.0.3 * @param array $data_to_save $_POST data to check. * @return array|int|bool Result of save, false on failure */ public function save_field_from_data( array $data_to_save ) { $this->data_to_save = $data_to_save; $meta_value = isset( $this->data_to_save[ $this->id( true ) ] ) ? $this->data_to_save[ $this->id( true ) ] : null; return $this->save_field( $meta_value ); } /** * Sanitize/store a value to this field * * @since 2.0.0 * @param array $meta_value Desired value to sanitize/store. * @return array|int|bool Result of save. false on failure */ public function save_field( $meta_value ) { $updated = false; $action = ''; $new_value = $this->sanitization_cb( $meta_value ); if ( ! $this->args( 'save_field' ) ) { // Nothing to see here. $action = 'disabled'; } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { $this->remove_data(); $count = 0; if ( ! empty( $new_value ) ) { foreach ( $new_value as $add_new ) { if ( $this->update_data( $add_new, false ) ) { $count++; } } } $updated = $count ? $count : false; $action = 'repeatable'; } elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) { $updated = $this->update_data( $new_value ); $action = 'updated'; } elseif ( CMB2_Utils::isempty( $new_value ) ) { $updated = $this->remove_data(); $action = 'removed'; } if ( $updated ) { $this->value = $this->get_data(); $this->escaped_value = null; } $field_id = $this->id( true ); /** * Hooks after save field action. * * @since 2.2.0 * * @param string $field_id the current field id paramater. * @param bool $updated Whether the metadata update action occurred. * @param string $action Action performed. Could be "repeatable", "updated", or "removed". * @param CMB2_Field object $field This field object */ do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); /** * Hooks after save field action. * * The dynamic portion of the hook, $field_id, refers to the * current field id paramater. * * @since 2.2.0 * * @param bool $updated Whether the metadata update action occurred. * @param string $action Action performed. Could be "repeatable", "updated", or "removed". * @param CMB2_Field object $field This field object */ do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); return $updated; } /** * Determine if current type is exempt from escaping * * @since 1.1.0 * @return bool True if exempt */ public function escaping_exception() { // These types cannot be escaped. return in_array( $this->type(), array( 'file_list', 'multicheck', 'text_datetime_timestamp_timezone', ) ); } /** * Determine if current type cannot be repeatable * * @since 1.1.0 * @param string $type Field type to check. * @return bool True if type cannot be repeatable */ public function repeatable_exception( $type ) { // These types cannot be repeatable. $internal_fields = array( // Use file_list instead. 'file' => 1, 'radio' => 1, 'title' => 1, 'wysiwyg' => 1, 'checkbox' => 1, 'radio_inline' => 1, 'taxonomy_radio' => 1, 'taxonomy_radio_inline' => 1, 'taxonomy_radio_hierarchical' => 1, 'taxonomy_select' => 1, 'taxonomy_select_hierarchical' => 1, 'taxonomy_multicheck' => 1, 'taxonomy_multicheck_inline' => 1, 'taxonomy_multicheck_hierarchical' => 1, ); /** * Filter field types that are non-repeatable. * * Note that this does *not* allow overriding the default non-repeatable types. * * @since 2.1.1 * * @param array $fields Array of fields designated as non-repeatable. Note that the field names are *keys*, * and not values. The value can be anything, because it is meaningless. Example: * array( 'my_custom_field' => 1 ) */ $all_fields = array_merge( apply_filters( 'cmb2_non_repeatable_fields', array() ), $internal_fields ); return isset( $all_fields[ $type ] ); } /** * Determine if current type has its own defaults field-arguments method. * * @since 2.2.6 * @param string $type Field type to check. * @return bool True if has own method. */ public function has_args_method( $type ) { // These types have their own arguments parser. $type_methods = array( 'group' => 'set_field_defaults_group', 'wysiwyg' => 'set_field_defaults_wysiwyg', ); if ( isset( $type_methods[ $type ] ) ) { return $type_methods[ $type ]; } $all_or_nothing_types = array_flip( apply_filters( 'cmb2_all_or_nothing_types', array( 'select', 'radio', 'radio_inline', 'taxonomy_select', 'taxonomy_select_hierarchical', 'taxonomy_radio', 'taxonomy_radio_inline', 'taxonomy_radio_hierarchical', ), $this ) ); if ( isset( $all_or_nothing_types[ $type ] ) ) { return 'set_field_defaults_all_or_nothing_types'; } return false; } /** * Escape the value before output. Defaults to 'esc_attr()' * * @since 1.0.1 * @param callable|string $func Escaping function (if not esc_attr()). * @param mixed $meta_value Meta value. * @return mixed Final value. */ public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { if ( null !== $this->escaped_value ) { return $this->escaped_value; } $meta_value = $meta_value ? $meta_value : $this->value(); // Check if the field has a registered escaping callback. if ( $cb = $this->maybe_callback( 'escape_cb' ) ) { // Ok, callback is good, let's run it. return call_user_func( $cb, $meta_value, $this->args(), $this ); } $field_type = $this->type(); /** * Filter the value for escaping before it is ouput. * * The dynamic portion of the hook name, $field_type, refers to the field type. * * Passing a non-null value to the filter will short-circuit the built-in * escaping for this field. * * @param bool|mixed $override_value Escaping override value to return. * Default: null. false to skip it. * @param mixed $meta_value The value to be output. * @param array $field_args The current field's arguments. * @param object $field This `CMB2_Field` object. */ $esc = apply_filters( "cmb2_types_esc_{$field_type}", null, $meta_value, $this->args(), $this ); if ( null !== $esc ) { return $esc; } if ( false === $cb || $this->escaping_exception() ) { // If requesting NO escaping, return meta value. return $this->val_or_default( $meta_value ); } // escaping function passed in? $func = $func ? $func : 'esc_attr'; $meta_value = $this->val_or_default( $meta_value ); if ( is_array( $meta_value ) ) { foreach ( $meta_value as $key => $value ) { $meta_value[ $key ] = call_user_func( $func, $value ); } } else { $meta_value = call_user_func( $func, $meta_value ); } $this->escaped_value = $meta_value; return $this->escaped_value; } /** * Return non-empty value or field default if value IS empty * * @since 2.0.0 * @param mixed $meta_value Field value. * @return mixed Field value, or default value */ public function val_or_default( $meta_value ) { return ! CMB2_Utils::isempty( $meta_value ) ? $meta_value : $this->get_default(); } /** * Offset a time value based on timezone * * @since 1.0.0 * @return string Offset time string */ public function field_timezone_offset() { return CMB2_Utils::timezone_offset( $this->field_timezone() ); } /** * Return timezone string * * @since 1.0.0 * @return string Timezone string */ public function field_timezone() { $value = ''; // Is timezone arg set? if ( $this->args( 'timezone' ) ) { $value = $this->args( 'timezone' ); } // End if. // Is there another meta key with a timezone stored as its value we should use? elseif ( $this->args( 'timezone_meta_key' ) ) { $value = $this->get_data( $this->args( 'timezone_meta_key' ) ); } return $value; } /** * Format the timestamp field value based on the field date/time format arg * * @since 2.0.0 * @param int $meta_value Timestamp. * @param string $format Either date_format or time_format. * @return string Formatted date */ public function format_timestamp( $meta_value, $format = 'date_format' ) { return date( stripslashes( $this->args( $format ) ), $meta_value ); } /** * Return a formatted timestamp for a field * * @since 2.0.0 * @param string $format Either date_format or time_format. * @param string|int $meta_value Optional meta value to check. * @return string Formatted date */ public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { $meta_value = $meta_value ? $meta_value : $this->escaped_value(); if ( empty( $meta_value ) ) { $meta_value = $this->get_default(); } $meta_value = CMB2_Utils::make_valid_time_stamp( $meta_value ); if ( empty( $meta_value ) ) { return ''; } return is_array( $meta_value ) ? array_map( array( $this, 'format_timestamp' ), $meta_value, $format ) : $this->format_timestamp( $meta_value, $format ); } /** * Get timestamp from text date * * @since 2.2.0 * @param string $value Date value. * @return mixed Unix timestamp representing the date. */ public function get_timestamp_from_value( $value ) { $timestamp = CMB2_Utils::get_timestamp_from_value( $value, $this->args( 'date_format' ) ); if ( empty( $timestamp ) && CMB2_Utils::is_valid_date( $value ) ) { $timestamp = CMB2_Utils::make_valid_time_stamp( $value ); } return $timestamp; } /** * Get field render callback and Render the field row * * @since 1.0.0 */ public function render_field() { $this->render_context = 'edit'; $this->peform_param_callback( 'render_row_cb' ); // For chaining. return $this; } /** * Default field render callback * * @since 2.1.1 */ public function render_field_callback() { // If field is requesting to not be shown on the front-end. if ( ! is_admin() && ! $this->args( 'on_front' ) ) { return; } // If field is requesting to be conditionally shown. if ( ! $this->should_show() ) { return; } $field_type = $this->type(); /** * Hook before field row begins. * * @param CMB2_Field $field The current field object. */ do_action( 'cmb2_before_field_row', $this ); /** * Hook before field row begins. * * The dynamic portion of the hook name, $field_type, refers to the field type. * * @param CMB2_Field $field The current field object. */ do_action( "cmb2_before_{$field_type}_field_row", $this ); $this->peform_param_callback( 'before_row' ); printf( "
\n", $this->row_classes(), $field_type ); if ( ! $this->args( 'show_names' ) ) { echo "\n\t
\n"; $this->peform_param_callback( 'label_cb' ); } else { if ( $this->get_param_callback_result( 'label_cb' ) ) { echo '
', $this->peform_param_callback( 'label_cb' ), '
'; } echo "\n\t
\n"; } $this->peform_param_callback( 'before' ); $types = new CMB2_Types( $this ); $types->render(); $this->peform_param_callback( 'after' ); echo "\n\t
\n
"; $this->peform_param_callback( 'after_row' ); /** * Hook after field row ends. * * The dynamic portion of the hook name, $field_type, refers to the field type. * * @param CMB2_Field $field The current field object. */ do_action( "cmb2_after_{$field_type}_field_row", $this ); /** * Hook after field row ends. * * @param CMB2_Field $field The current field object. */ do_action( 'cmb2_after_field_row', $this ); // For chaining. return $this; } /** * The default label_cb callback (if not a title field) * * @since 2.1.1 * @return string Label html markup. */ public function label() { if ( ! $this->args( 'name' ) ) { return ''; } $style = ! $this->args( 'show_names' ) ? ' style="display:none;"' : ''; return sprintf( "\n" . '%3$s' . "\n", $style, $this->id(), $this->args( 'name' ) ); } /** * Defines the classes for the current CMB2 field row * * @since 2.0.0 * @return string Space concatenated list of classes */ public function row_classes() { $classes = array(); /** * By default, 'text_url' and 'text' fields get table-like styling * * @since 2.0.0 * * @param array $field_types The types of fields which should get the 'table-layout' class */ $repeat_table_rows_types = apply_filters( 'cmb2_repeat_table_row_types', array( 'text_url', 'text', ) ); $conditional_classes = array( 'cmb-type-' . str_replace( '_', '-', sanitize_html_class( $this->type() ) ) => true, 'cmb2-id-' . str_replace( '_', '-', sanitize_html_class( $this->id() ) ) => true, 'cmb-repeat' => $this->args( 'repeatable' ), 'cmb-repeat-group-field' => $this->group, 'cmb-inline' => $this->args( 'inline' ), 'table-layout' => 'edit' === $this->render_context && in_array( $this->type(), $repeat_table_rows_types ), ); foreach ( $conditional_classes as $class => $condition ) { if ( $condition ) { $classes[] = $class; } } if ( $added_classes = $this->args( 'classes' ) ) { $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; } elseif ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; } if ( $added_classes ) { $classes[] = esc_attr( $added_classes ); } /** * Globally filter row classes * * @since 2.0.0 * * @param string $classes Space-separated list of row classes * @param CMB2_Field object $field This field object */ return apply_filters( 'cmb2_row_classes', implode( ' ', $classes ), $this ); } /** * Get field display callback and render the display value in the column. * * @since 2.2.2 */ public function render_column() { $this->render_context = 'display'; $this->peform_param_callback( 'display_cb' ); // For chaining. return $this; } /** * The method to fetch the value for this field for the REST API. * * @since 2.5.0 */ public function get_rest_value() { $field_type = $this->type(); $field_id = $this->id( true ); if ( $cb = $this->maybe_callback( 'rest_value_cb' ) ) { add_filter( "cmb2_get_rest_value_for_{$field_id}", $cb, 99 ); } $value = $this->get_data(); /** * Filter the value before it is sent to the REST request. * * @since 2.5.0 * * @param mixed $value The value from CMB2_Field::get_data() * @param CMB2_Field $field This field object. */ $value = apply_filters( 'cmb2_get_rest_value', $value, $this ); /** * Filter the value before it is sent to the REST request. * * The dynamic portion of the hook name, $field_type, refers to the field type. * * @since 2.5.0 * * @param mixed $value The value from CMB2_Field::get_data() * @param CMB2_Field $field This field object. */ $value = apply_filters( "cmb2_get_rest_value_{$field_type}", $value, $this ); /** * Filter the value before it is sent to the REST request. * * The dynamic portion of the hook name, $field_id, refers to the field id. * * @since 2.5.0 * * @param mixed $value The value from CMB2_Field::get_data() * @param CMB2_Field $field This field object. */ return apply_filters( "cmb2_get_rest_value_for_{$field_id}", $value, $this ); } /** * Get a field object for a supporting field. (e.g. file field) * * @since 2.7.0 * * @return CMB2_Field|bool Supporting field object, if supported. */ public function get_supporting_field() { $suffix = $this->args( 'has_supporting_data' ); if ( empty( $suffix ) ) { return false; } return $this->get_field_clone( array( 'id' => $this->_id( '', false ) . $suffix, 'sanitization_cb' => false, ) ); } /** * Default callback to outputs field value in a display format. * * @since 2.2.2 */ public function display_value_callback() { // If field is requesting to be conditionally shown. if ( ! $this->should_show() ) { return; } $display = new CMB2_Field_Display( $this ); $field_type = $this->type(); /** * A filter to bypass the default display. * * The dynamic portion of the hook name, $field_type, refers to the field type. * * Passing a non-null value to the filter will short-circuit the default display. * * @param bool|mixed $pre_output Default null value. * @param CMB2_Field $field This field object. * @param CMB2_Field_Display $display The `CMB2_Field_Display` object. */ $pre_output = apply_filters( "cmb2_pre_field_display_{$field_type}", null, $this, $display ); if ( null !== $pre_output ) { echo $pre_output; return; } $this->peform_param_callback( 'before_display_wrap' ); printf( "
\n", $this->row_classes(), $field_type ); $this->peform_param_callback( 'before_display' ); CMB2_Field_Display::get( $this )->display(); $this->peform_param_callback( 'after_display' ); echo "\n
"; $this->peform_param_callback( 'after_display_wrap' ); // For chaining. return $this; } /** * Replaces a hash key - {#} - with the repeatable index * * @since 1.2.0 * @param string $value Value to update. * @return string Updated value */ public function replace_hash( $value ) { // Replace hash with 1 based count. return str_replace( '{#}', ( $this->index + 1 ), $value ); } /** * Retrieve text parameter from field's text array (if it has one), or use fallback text * For back-compatibility, falls back to checking the options array. * * @since 2.2.2 * @param string $text_key Key in field's text array. * @param string $fallback Fallback text. * @return string Text */ public function get_string( $text_key, $fallback ) { // If null, populate with our field strings values. if ( null === $this->strings ) { $this->strings = (array) $this->args['text']; if ( is_callable( $this->args['text_cb'] ) ) { $strings = call_user_func( $this->args['text_cb'], $this ); if ( $strings && is_array( $strings ) ) { $this->strings += $strings; } } } // If we have that string value, send it back. if ( isset( $this->strings[ $text_key ] ) ) { return $this->strings[ $text_key ]; } // Check options for back-compat. $string = $this->options( $text_key ); return $string ? $string : $fallback; } /** * Retrieve options args. * * @since 2.0.0 * @param string $key Specific option to retrieve. * @return array|mixed Array of options or specific option. */ public function options( $key = '' ) { if ( empty( $this->field_options ) ) { $this->set_options(); } if ( $key ) { return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false; } return $this->field_options; } /** * Generates/sets options args. Calls options_cb if it exists. * * @since 2.2.5 * * @return array Array of options */ public function set_options() { $this->field_options = (array) $this->args['options']; if ( is_callable( $this->args['options_cb'] ) ) { $options = call_user_func( $this->args['options_cb'], $this ); if ( $options && is_array( $options ) ) { $this->field_options = $options + $this->field_options; } } return $this->field_options; } /** * Store JS dependencies as part of the field args. * * @since 2.2.0 * @param array $dependencies Dependies to register for this field. */ public function add_js_dependencies( $dependencies = array() ) { foreach ( (array) $dependencies as $dependency ) { $this->args['js_dependencies'][ $dependency ] = $dependency; } CMB2_JS::add_dependencies( $dependencies ); } /** * Send field data to JS. * * @since 2.2.0 */ public function register_js_data() { if ( $this->group ) { CMB2_JS::add_field_data( $this->group ); } return CMB2_JS::add_field_data( $this ); } /** * Get an array of some of the field data to be used in the Javascript. * * @since 2.2.4 * * @return array */ public function js_data() { return array( 'label' => $this->args( 'name' ), 'id' => $this->id( true ), 'type' => $this->type(), 'hash' => $this->hash_id(), 'box' => $this->cmb_id, 'id_attr' => $this->id(), 'name_attr' => $this->args( '_name' ), 'default' => $this->get_default(), 'group' => $this->group_id(), 'index' => $this->group ? $this->group->index : null, ); } /** * Returns a unique hash representing this field. * * @since 2.2.4 * * @return string */ public function hash_id() { if ( '' === $this->hash_id ) { $this->hash_id = CMB2_Utils::generate_hash( $this->cmb_id . '||' . $this->id() ); } return $this->hash_id; } /** * Gets the id of the group field if this field is part of a group. * * @since 2.2.4 * * @return string */ public function group_id() { return $this->group ? $this->group->id( true ) : ''; } /** * Get CMB2_Field default value, either from default param or default_cb param. * * @since 0.2.2 * * @return mixed Default field value */ public function get_default() { $default = $this->args['default']; if ( null !== $default ) { return apply_filters( 'cmb2_default_filter', $default, $this ); } $param = is_callable( $this->args['default_cb'] ) ? 'default_cb' : 'default'; $default = $this->args['default'] = $this->get_param_callback_result( $param ); // Allow a filter override of the default value. return apply_filters( 'cmb2_default_filter', $this->args['default'], $this ); } /** * Fills in empty field parameters with defaults * * @since 1.1.0 * * @param array $args Field config array. * @return array Modified field config array. */ public function _set_field_defaults( $args ) { $defaults = $this->get_default_field_args( $args ); /** * Filter the CMB2 Field defaults. * * @since 2.6.0 * @param array $defaults Metabox field config array defaults. * @param string $id Field id for the current field to allow for selective filtering. * @param string $type Field type for the current field to allow for selective filtering. * @param CMB2_Field object $field This field object. */ $defaults = apply_filters( 'cmb2_field_defaults', $defaults, $args['id'], $args['type'], $this ); // Set up blank or default values for empty ones. $args = wp_parse_args( $args, $defaults ); /** * Filtering the CMB2 Field arguments once merged with the defaults, but before further processing. * * @since 2.6.0 * @param array $args Metabox field config array defaults. * @param CMB2_Field object $field This field object. */ $args = apply_filters( 'cmb2_field_arguments_raw', $args, $this ); /* * Deprecated usage: * * 'std' -- use 'default' (no longer works) * 'row_classes' -- use 'class', or 'class_cb' * 'default' -- as callback (use default_cb) */ $args = $this->convert_deprecated_params( $args ); $args['repeatable'] = $args['repeatable'] && ! $this->repeatable_exception( $args['type'] ); $args['inline'] = $args['inline'] || false !== stripos( $args['type'], '_inline' ); $args['_id'] = $args['id']; $args['_name'] = $args['id']; if ( $method = $this->has_args_method( $args['type'] ) ) { $args = $this->{$method}( $args ); } if ( $this->group ) { $args = $this->set_group_sub_field_defaults( $args ); } $with_supporting = array( // CMB2_Sanitize::_save_file_id_value()/CMB2_Sanitize::_get_group_file_value_array(). 'file' => '_id', // See CMB2_Sanitize::_save_utc_value(). 'text_datetime_timestamp_timezone' => '_utc', ); $args['has_supporting_data'] = isset( $with_supporting[ $args['type'] ] ) ? $with_supporting[ $args['type'] ] : false; // Repeatable fields require jQuery sortable library. if ( ! empty( $args['repeatable'] ) ) { CMB2_JS::add_dependencies( 'jquery-ui-sortable' ); } /** * Filter the CMB2 Field arguments after processing. * * @since 2.6.0 * @param array $args Metabox field config array after processing. * @param CMB2_Field object $field This field object. */ return apply_filters( 'cmb2_field_arguments', $args, $this ); } /** * Sets default arguments for the group field types. * * @since 2.2.6 * * @param array $args Field config array. * @return array Modified field config array. */ protected function set_field_defaults_group( $args ) { $args['options'] = wp_parse_args( $args['options'], array( 'add_button' => esc_html__( 'Add Group', 'cmb2' ), 'remove_button' => esc_html__( 'Remove Group', 'cmb2' ), 'remove_confirm' => '', ) ); return $args; } /** * Sets default arguments for the wysiwyg field types. * * @since 2.2.6 * * @param array $args Field config array. * @return array Modified field config array. */ protected function set_field_defaults_wysiwyg( $args ) { $args['id'] = strtolower( str_ireplace( '-', '_', $args['id'] ) ); $args['options']['textarea_name'] = $args['_name']; return $args; } /** * Sets default arguments for the all-or-nothing field types. * * @since 2.2.6 * * @param array $args Field config array. * @return array Modified field config array. */ protected function set_field_defaults_all_or_nothing_types( $args ) { $args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : null; $args['show_option_none'] = true === $args['show_option_none'] ? esc_html__( 'None', 'cmb2' ) : $args['show_option_none']; if ( null === $args['show_option_none'] ) { $off_by_default = in_array( $args['type'], array( 'select', 'radio', 'radio_inline' ), true ); $args['show_option_none'] = $off_by_default ? false : esc_html__( 'None', 'cmb2' ); } return $args; } /** * Sets default arguments for group sub-fields. * * @since 2.2.6 * * @param array $args Field config array. * @return array Modified field config array. */ protected function set_group_sub_field_defaults( $args ) { $args['id'] = $this->group->args( 'id' ) . '_' . $this->group->index . '_' . $args['id']; $args['_name'] = $this->group->args( 'id' ) . '[' . $this->group->index . '][' . $args['_name'] . ']'; return $args; } /** * Gets the default arguments for all fields. * * @since 2.2.6 * * @param array $args Field config array. * @return array Field defaults. */ protected function get_default_field_args( $args ) { $type = isset( $args['type'] ) ? $args['type'] : ''; return array( 'type' => $type, 'name' => '', 'desc' => '', 'before' => '', 'after' => '', 'options' => array(), 'options_cb' => '', 'text' => array(), 'text_cb' => '', 'attributes' => array(), 'protocols' => null, 'default' => null, 'default_cb' => '', 'classes' => null, 'classes_cb' => '', 'select_all_button' => true, 'multiple' => false, 'repeatable' => 'group' === $type, 'inline' => false, 'on_front' => true, 'show_names' => true, 'save_field' => true, // Will not save if false. 'date_format' => 'm\/d\/Y', 'time_format' => 'h:i A', 'description' => isset( $args['desc'] ) ? $args['desc'] : '', 'preview_size' => 'file' === $type ? array( 350, 350 ) : array( 50, 50 ), 'render_row_cb' => array( $this, 'render_field_callback' ), 'display_cb' => array( $this, 'display_value_callback' ), 'label_cb' => 'title' !== $type ? array( $this, 'label' ) : '', 'column' => false, 'js_dependencies' => array(), 'show_in_rest' => null, 'char_counter' => false, 'char_max' => false, 'char_max_enforce' => false, ); } /** * Get default field arguments specific to this CMB2 object. * * @since 2.2.0 * @param array $field_args Metabox field config array. * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent). * @return array Array of field arguments. */ protected function get_default_args( $field_args, $field_group = null ) { $args = parent::get_default_args( array(), $this->group ); if ( isset( $field_args['field_args'] ) ) { $args = wp_parse_args( $field_args, $args ); } else { $args['field_args'] = wp_parse_args( $field_args, $this->args ); } return $args; } /** * Returns a cloned version of this field object, but with * modified/overridden field arguments. * * @since 2.2.2 * @param array $field_args Array of field arguments, or entire array of * arguments for CMB2_Field. * * @return CMB2_Field The new CMB2_Field instance. */ public function get_field_clone( $field_args ) { return $this->get_new_field( $field_args ); } /** * Returns the CMB2 instance this field is registered to. * * @since 2.2.2 * * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. */ public function get_cmb() { if ( ! $this->cmb_id ) { return new WP_Error( 'no_cmb_id', esc_html__( 'Sorry, this field does not have a cmb_id specified.', 'cmb2' ) ); } return cmb2_get_metabox( $this->cmb_id, $this->object_id, $this->object_type ); } /** * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. * * @since 2.2.3 * @param array $args Metabox field config array. * @return array Modified field config array. */ protected function convert_deprecated_params( $args ) { if ( isset( $args['row_classes'] ) ) { // We'll let this one be. // $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_PARAM, 'row_classes', 'classes' ); // row_classes param could be a callback. This is definitely deprecated. if ( is_callable( $args['row_classes'] ) ) { $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'row_classes', 'classes_cb' ); $args['classes_cb'] = $args['row_classes']; $args['classes'] = null; } else { $args['classes'] = $args['row_classes']; } unset( $args['row_classes'] ); } // default param can be passed a callback as well. if ( is_callable( $args['default'] ) ) { $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'default', 'default_cb' ); $args['default_cb'] = $args['default']; $args['default'] = null; } // options param can be passed a callback as well. if ( is_callable( $args['options'] ) ) { $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'options', 'options_cb' ); $args['options_cb'] = $args['options']; $args['options'] = array(); } return $args; } } CMB2_Field_Display.php000064400000030522151717007230010535 0ustar00type(); $display_class_name = $field->args( 'display_class' ); if ( empty( $display_class_name ) ) { switch ( $fieldtype ) { case 'text_url': $display_class_name = 'CMB2_Display_Text_Url'; break; case 'text_money': $display_class_name = 'CMB2_Display_Text_Money'; break; case 'colorpicker': $display_class_name = 'CMB2_Display_Colorpicker'; break; case 'checkbox': $display_class_name = 'CMB2_Display_Checkbox'; break; case 'wysiwyg': case 'textarea_small': $display_class_name = 'CMB2_Display_Textarea'; break; case 'textarea_code': $display_class_name = 'CMB2_Display_Textarea_Code'; break; case 'text_time': $display_class_name = 'CMB2_Display_Text_Time'; break; case 'text_date': case 'text_date_timestamp': case 'text_datetime_timestamp': $display_class_name = 'CMB2_Display_Text_Date'; break; case 'text_datetime_timestamp_timezone': $display_class_name = 'CMB2_Display_Text_Date_Timezone'; break; case 'select': case 'radio': case 'radio_inline': $display_class_name = 'CMB2_Display_Select'; break; case 'multicheck': case 'multicheck_inline': $display_class_name = 'CMB2_Display_Multicheck'; break; case 'taxonomy_radio': case 'taxonomy_radio_inline': case 'taxonomy_select': case 'taxonomy_select_hierarchical': case 'taxonomy_radio_hierarchical': $display_class_name = 'CMB2_Display_Taxonomy_Radio'; break; case 'taxonomy_multicheck': case 'taxonomy_multicheck_inline': case 'taxonomy_multicheck_hierarchical': $display_class_name = 'CMB2_Display_Taxonomy_Multicheck'; break; case 'file': $display_class_name = 'CMB2_Display_File'; break; case 'file_list': $display_class_name = 'CMB2_Display_File_List'; break; case 'oembed': $display_class_name = 'CMB2_Display_oEmbed'; break; default: $display_class_name = __CLASS__; break; }// End switch. } if ( has_action( "cmb2_display_class_{$fieldtype}" ) ) { /** * Filters the custom field display class used for displaying the field. Class is required to extend CMB2_Type_Base. * * The dynamic portion of the hook name, $fieldtype, refers to the (custom) field type. * * @since 2.2.4 * * @param string $display_class_name The custom field display class to use. * @param object $field The `CMB2_Field` object. */ $display_class_name = apply_filters( "cmb2_display_class_{$fieldtype}", $display_class_name, $field ); } return new $display_class_name( $field ); } /** * Setup our class vars * * @since 2.2.2 * @param CMB2_Field $field A CMB2 field object. */ public function __construct( CMB2_Field $field ) { $this->field = $field; $this->value = $this->field->value; } /** * Catchall method if field's 'display_cb' is NOT defined, or field type does * not have a corresponding display method * * @since 2.2.2 */ public function display() { // If repeatable. if ( $this->field->args( 'repeatable' ) ) { // And has a repeatable value. if ( is_array( $this->field->value ) ) { // Then loop and output. echo ''; } } else { $this->_display(); } } /** * Default fallback display method. * * @since 2.2.2 */ protected function _display() { print_r( $this->value ); } } class CMB2_Display_Text_Url extends CMB2_Field_Display { /** * Display url value. * * @since 2.2.2 */ protected function _display() { echo make_clickable( esc_url( $this->value ) ); } } class CMB2_Display_Text_Money extends CMB2_Field_Display { /** * Display text_money value. * * @since 2.2.2 */ protected function _display() { $this->value = $this->value ? $this->value : '0'; echo ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$' : ' ' ), $this->value; } } class CMB2_Display_Colorpicker extends CMB2_Field_Display { /** * Display color picker value. * * @since 2.2.2 */ protected function _display() { echo ' ', esc_html( $this->value ), ''; } } class CMB2_Display_Checkbox extends CMB2_Field_Display { /** * Display multicheck value. * * @since 2.2.2 */ protected function _display() { echo $this->value === 'on' ? 'on' : 'off'; } } class CMB2_Display_Select extends CMB2_Field_Display { /** * Display select value. * * @since 2.2.2 */ protected function _display() { $options = $this->field->options(); $fallback = $this->field->args( 'show_option_none' ); if ( ! $fallback && isset( $options[''] ) ) { $fallback = $options['']; } if ( ! $this->value && $fallback ) { echo $fallback; } elseif ( isset( $options[ $this->value ] ) ) { echo $options[ $this->value ]; } else { echo esc_attr( $this->value ); } } } class CMB2_Display_Multicheck extends CMB2_Field_Display { /** * Display multicheck value. * * @since 2.2.2 */ protected function _display() { if ( empty( $this->value ) || ! is_array( $this->value ) ) { return; } $options = $this->field->options(); $output = array(); foreach ( $this->value as $val ) { if ( isset( $options[ $val ] ) ) { $output[] = $options[ $val ]; } else { $output[] = esc_attr( $val ); } } echo implode( ', ', $output ); } } class CMB2_Display_Textarea extends CMB2_Field_Display { /** * Display textarea value. * * @since 2.2.2 */ protected function _display() { echo wpautop( wp_kses_post( $this->value ) ); } } class CMB2_Display_Textarea_Code extends CMB2_Field_Display { /** * Display textarea_code value. * * @since 2.2.2 */ protected function _display() { echo '' . print_r( $this->value, true ) . ''; } } class CMB2_Display_Text_Time extends CMB2_Field_Display { /** * Display text_time value. * * @since 2.2.2 */ protected function _display() { echo $this->field->get_timestamp_format( 'time_format', $this->value ); } } class CMB2_Display_Text_Date extends CMB2_Field_Display { /** * Display text_date value. * * @since 2.2.2 */ protected function _display() { echo $this->field->get_timestamp_format( 'date_format', $this->value ); } } class CMB2_Display_Text_Date_Timezone extends CMB2_Field_Display { /** * Display text_datetime_timestamp_timezone value. * * @since 2.2.2 */ protected function _display() { if ( empty( $this->value ) ) { return; } $datetime = CMB2_Utils::get_datetime_from_value( $this->value ); if ( ! $datetime || ! $datetime instanceof DateTime ) { return; } $date = $datetime->format( stripslashes( $this->field->args( 'date_format' ) ) ); $time = $datetime->format( stripslashes( $this->field->args( 'time_format' ) ) ); $timezone = $datetime->getTimezone()->getName(); echo $date; if ( $time ) { echo ' ' . $time; } if ( $timezone ) { echo ', ' . $timezone; } } } class CMB2_Display_Taxonomy_Radio extends CMB2_Field_Display { /** * Display single taxonomy value. * * @since 2.2.2 */ protected function _display() { $taxonomy = $this->field->args( 'taxonomy' ); $types = new CMB2_Types( $this->field ); $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_Taxonomy_Radio' ); $terms = $type->get_object_terms(); $term = false; if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) { $term = get_term_by( 'slug', $default, $taxonomy ); } elseif ( ! empty( $terms ) ) { $term = $terms[ key( $terms ) ]; } if ( $term ) { $link = get_edit_term_link( $term->term_id, $taxonomy ); echo '', esc_html( $term->name ), ''; } } } class CMB2_Display_Taxonomy_Multicheck extends CMB2_Field_Display { /** * Display taxonomy values. * * @since 2.2.2 */ protected function _display() { $taxonomy = $this->field->args( 'taxonomy' ); $types = new CMB2_Types( $this->field ); $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_Taxonomy_Multicheck' ); $terms = $type->get_object_terms(); if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) { $terms = array(); if ( is_array( $default ) ) { foreach ( $default as $slug ) { $terms[] = get_term_by( 'slug', $slug, $taxonomy ); } } else { $terms[] = get_term_by( 'slug', $default, $taxonomy ); } } if ( is_array( $terms ) ) { $links = array(); foreach ( $terms as $term ) { $link = get_edit_term_link( $term->term_id, $taxonomy ); $links[] = '' . esc_html( $term->name ) . ''; } // Then loop and output. echo '
'; echo implode( ', ', $links ); echo '
'; } } } class CMB2_Display_File extends CMB2_Field_Display { /** * Display file value. * * @since 2.2.2 */ protected function _display() { if ( empty( $this->value ) ) { return; } $this->value = esc_url_raw( $this->value ); $types = new CMB2_Types( $this->field ); $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_File_Base' ); $id = $this->field->get_field_clone( array( 'id' => $this->field->_id( '', false ) . '_id', ) )->escaped_value( 'absint' ); $this->file_output( $this->value, $id, $type ); } protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) { // If there is no ID saved yet, try to get it from the url. if ( $url_value && ! $id ) { $id = CMB2_Utils::image_id_from_url( esc_url_raw( $url_value ) ); } if ( $field_type->is_valid_img_ext( $url_value ) ) { $img_size = $this->field->args( 'preview_size' ); if ( $id ) { $image = wp_get_attachment_image( $id, $img_size, null, array( 'class' => 'cmb-image-display', ) ); } else { $size = is_array( $img_size ) ? $img_size[0] : 200; $image = ''; } echo $image; } else { printf( '
%1$s %3$s
', esc_html( $field_type->_text( 'file_text', __( 'File:', 'cmb2' ) ) ), esc_url( $url_value ), esc_html( CMB2_Utils::get_file_name_from_path( $url_value ) ) ); } } } class CMB2_Display_File_List extends CMB2_Display_File { /** * Display file_list value. * * @since 2.2.2 */ protected function _display() { if ( empty( $this->value ) || ! is_array( $this->value ) ) { return; } $types = new CMB2_Types( $this->field ); $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_File_Base' ); echo ''; } } class CMB2_Display_oEmbed extends CMB2_Field_Display { /** * Display oembed value. * * @since 2.2.2 */ protected function _display() { if ( ! $this->value ) { return; } cmb2_do_oembed( array( 'url' => $this->value, 'object_id' => $this->field->object_id, 'object_type' => $this->field->object_type, 'oembed_args' => array( 'width' => '300', ), 'field_id' => $this->field->id(), ) ); } } CMB2_Hookup.php000064400000066703151717007230007304 0ustar00prop( 'hookup' ) ) { $hookup = new self( $cmb ); // Hook in the hookup... how meta. return $hookup->universal_hooks(); } return false; } public function universal_hooks() { foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); } if ( is_admin() ) { // Register our scripts and styles for cmb. $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); $this->maybe_enqueue_column_display_styles(); switch ( $this->object_type ) { case 'post': return $this->post_hooks(); case 'comment': return $this->comment_hooks(); case 'user': return $this->user_hooks(); case 'term': return $this->term_hooks(); case 'options-page': return $this->options_page_hooks(); } } do_action( 'cmb2_init_hooks', $this ); return $this; } public function post_hooks() { // Fetch the context we set in our call. $context = $this->cmb->prop( 'context' ) ? $this->cmb->prop( 'context' ) : 'normal'; // Call the proper hook based on the context provided. switch ( $context ) { case 'form_top': add_action( 'edit_form_top', array( $this, 'add_context_metaboxes' ) ); break; case 'before_permalink': add_action( 'edit_form_before_permalink', array( $this, 'add_context_metaboxes' ) ); break; case 'after_title': add_action( 'edit_form_after_title', array( $this, 'add_context_metaboxes' ) ); break; case 'after_editor': add_action( 'edit_form_after_editor', array( $this, 'add_context_metaboxes' ) ); break; default: add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); } add_action( 'add_meta_boxes', array( $this, 'remove_default_tax_metaboxes' ) ); add_action( 'add_attachment', array( $this, 'save_post' ) ); add_action( 'edit_attachment', array( $this, 'save_post' ) ); add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); if ( $this->cmb->has_columns ) { foreach ( $this->cmb->box_types() as $post_type ) { add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) ); add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 ); add_filter( "manage_edit-{$post_type}_sortable_columns", array( $this, 'columns_sortable' ) ); add_action( 'pre_get_posts', array( $this, 'columns_sortable_orderby' ) ); } } return $this; } public function comment_hooks() { add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); add_action( 'edit_comment', array( $this, 'save_comment' ) ); if ( $this->cmb->has_columns ) { add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) ); add_action( 'manage_comments_custom_column', array( $this, 'column_display' ), 10, 3 ); add_filter( 'manage_edit-comments_sortable_columns', array( $this, 'columns_sortable' ) ); add_action( 'pre_get_posts', array( $this, 'columns_sortable_orderby' ) ); } return $this; } public function user_hooks() { $priority = $this->get_priority(); add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); add_action( 'personal_options_update', array( $this, 'save_user' ) ); add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); add_action( 'user_register', array( $this, 'save_user' ) ); if ( $this->cmb->has_columns ) { add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) ); add_filter( 'manage_users_custom_column', array( $this, 'return_column_display' ), 10, 3 ); add_filter( 'manage_users_sortable_columns', array( $this, 'columns_sortable' ) ); add_action( 'pre_get_posts', array( $this, 'columns_sortable_orderby' ) ); } return $this; } public function term_hooks() { if ( ! function_exists( 'get_term_meta' ) ) { wp_die( esc_html__( 'Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress install.', 'cmb2' ) ); } if ( ! $this->cmb->prop( 'taxonomies' ) ) { wp_die( esc_html__( 'Term metaboxes configuration requires a "taxonomies" parameter.', 'cmb2' ) ); } $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); $show_on_term_add = $this->cmb->prop( 'new_term_section' ); $priority = $this->get_priority( 8 ); foreach ( $this->taxonomies as $taxonomy ) { // Display our form data. add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); $show_on_add = is_array( $show_on_term_add ) ? in_array( $taxonomy, $show_on_term_add ) : (bool) $show_on_term_add; /** * Filter to determine if the term's fields should show in the "Add term" section. * * The dynamic portion of the hook name, $cmb_id, is the metabox id. * * @param bool $show_on_add Default is the value of the new_term_section cmb parameter. * @param object $cmb The CMB2 instance */ $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); // Display form in add-new section (unless specified not to). if ( $show_on_add ) { add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); } if ( $this->cmb->has_columns ) { add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) ); add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display' ), 10, 3 ); add_filter( "manage_edit-{$taxonomy}_sortable_columns", array( $this, 'columns_sortable' ) ); add_action( 'pre_get_posts', array( $this, 'columns_sortable_orderby' ) ); } } add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); return $this; } public function options_page_hooks() { $option_keys = $this->cmb->options_page_keys(); if ( ! empty( $option_keys ) ) { foreach ( $option_keys as $option_key ) { $this->options_hookup[ $option_key ] = new CMB2_Options_Hookup( $this->cmb, $option_key ); $this->options_hookup[ $option_key ]->hooks(); } } return $this; } /** * Registers styles for CMB2 * * @since 2.0.7 */ protected static function register_styles() { if ( self::$css_registration_done ) { return; } // Only use minified files if SCRIPT_DEBUG is off. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; $front = is_admin() ? '' : '-front'; $rtl = is_rtl() ? '-rtl' : ''; /** * Filters the registered style dependencies for the cmb2 stylesheet. * * @param array $dependencies The registered style dependencies for the cmb2 stylesheet. */ $dependencies = apply_filters( 'cmb2_style_dependencies', array() ); wp_register_style( 'cmb2-styles', CMB2_Utils::url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies ); wp_register_style( 'cmb2-display-styles', CMB2_Utils::url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies ); self::$css_registration_done = true; } /** * Registers scripts for CMB2 * * @since 2.0.7 */ protected static function register_js() { if ( self::$js_registration_done ) { return; } $hook = is_admin() ? 'admin_footer' : 'wp_footer'; add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); self::$js_registration_done = true; } /** * Registers scripts and styles for CMB2 * * @since 1.0.0 */ public static function register_scripts() { self::register_styles(); self::register_js(); } /** * Enqueues scripts and styles for CMB2 in admin_head. * * @since 1.0.0 * * @param string $hook Current hook for the admin page. */ public function do_scripts( $hook ) { $should_pre_enqueue = in_array( $hook, array( 'post.php', 'post-new.php', 'page-new.php', 'page.php', 'comment.php', 'edit-tags.php', 'term.php', 'user-new.php', 'profile.php', 'user-edit.php', ), true ); /** * Filter to determine if CMB2 should be pre-enqueued on the current page. * * `show_form_for_type` will have us covered if we miss something here, but may be a * flash of unstyled content. * * @param bool $should_pre_enqueue Whether CMB2 should be pre-enqueued on the current page. * @param string $hook The current hook for the admin page. * @param object $cmb The CMB2 object. */ $should_pre_enqueue = apply_filters( 'cmb2_should_pre_enqueue', $should_pre_enqueue, $hook, $this ); // only pre-enqueue our scripts/styles on the proper pages // show_form_for_type will have us covered if we miss something here. if ( $should_pre_enqueue ) { if ( $this->cmb->prop( 'cmb_styles' ) ) { self::enqueue_cmb_css(); } if ( $this->cmb->prop( 'enqueue_js' ) ) { self::enqueue_cmb_js(); } } } /** * Register the CMB2 field column headers. * * @since 2.2.2 * * @param array $columns Array of columns available for the admin page. */ public function register_column_headers( $columns ) { foreach ( $this->cmb->prop( 'fields' ) as $field ) { if ( empty( $field['column'] ) ) { continue; } $column = $field['column']; if ( false === $column['position'] ) { $columns[ $field['id'] ] = $column['name']; } else { $before = array_slice( $columns, 0, absint( $column['position'] ) ); $before[ $field['id'] ] = $column['name']; $columns = $before + $columns; } $column['field'] = $field; $this->columns[ $field['id'] ] = $column; } return $columns; } /** * The CMB2 field column display output. * * @since 2.2.2 * * @param string $column_name Current column name. * @param mixed $object_id Current object ID. */ public function column_display( $column_name, $object_id ) { if ( isset( $this->columns[ $column_name ] ) ) { $field = new CMB2_Field( array( 'field_args' => $this->columns[ $column_name ]['field'], 'object_type' => $this->object_type, 'object_id' => $this->cmb->object_id( $object_id ), 'cmb_id' => $this->cmb->cmb_id, ) ); $this->cmb->get_field( $field )->render_column(); } } /** * Returns the columns sortable array. * * @since 2.6.1 * * @param array $columns An array of sortable columns. * * @return array $columns An array of sortable columns with CMB2 columns. */ public function columns_sortable( $columns ) { foreach ( $this->cmb->prop( 'fields' ) as $field ) { if ( ! empty( $field['column'] ) && empty( $field['column']['disable_sortable'] ) ) { $columns[ $field['id'] ] = $field['id']; } } return $columns; } /** * Return the query object to order by custom columns if selected * * @since 2.6.1 * * @param object $query Object query from WordPress * * @return void */ public function columns_sortable_orderby( $query ) { if ( ! is_admin() ) { return; } $orderby = $query->get( 'orderby' ); foreach ( $this->cmb->prop( 'fields' ) as $field ) { if ( empty( $field['column'] ) || ! empty( $field['column']['disable_sortable'] ) || $field['id'] !== $orderby ) { continue; } $query->set( 'meta_key', $field['id'] ); $type = $field['type']; if ( ! empty( $field['attributes']['type'] ) ) { switch ( $field['attributes']['type'] ) { case 'number': case 'date': $type = $field['attributes']['type']; break; case 'range': $type = 'number'; break; } } switch ( $type ) { case 'number': case 'text_date_timestamp': case 'text_datetime_timestamp': case 'text_money': $query->set( 'orderby', 'meta_value_num' ); break; case 'text_time': $query->set( 'orderby', 'meta_value_time' ); break; case 'text_date': $query->set( 'orderby', 'meta_value_date' ); break; default: $query->set( 'orderby', 'meta_value' ); break; } } } /** * Returns the column display. * * @since 2.2.2 */ public function return_column_display( $empty, $custom_column, $object_id ) { ob_start(); $this->column_display( $custom_column, $object_id ); $column = ob_get_clean(); return $column ? $column : $empty; } /** * Output the CMB2 box/fields in an alternate context (not in a standard metabox area). * * @since 2.2.4 */ public function add_context_metaboxes() { if ( ! $this->show_on() ) { return; } $page = get_current_screen()->id; foreach ( $this->cmb->box_types() as $object_type ) { $screen = convert_to_screen( $object_type ); // If we're on the right post-type/object... if ( isset( $screen->id ) && $screen->id === $page ) { // Show the box. $this->output_context_metabox(); } } } /** * Output the CMB2 box/fields in an alternate context (not in a standard metabox area). * * @since 2.2.4 */ public function output_context_metabox() { $title = $this->cmb->prop( 'title' ); /* * To keep from outputting the open/close markup, do not include * a 'title' property in your metabox registration array. * * To output the fields 'naked' (without a postbox wrapper/style), then * add a `'remove_box_wrap' => true` to your metabox registration array. */ $add_wrap = ! empty( $title ) || ! $this->cmb->prop( 'remove_box_wrap' ); $add_handle = $add_wrap && ! empty( $title ); // Open the context-box wrap. $this->context_box_title_markup_open( $add_handle ); // Show the form fields. $this->cmb->show_form(); // Close the context-box wrap. $this->context_box_title_markup_close( $add_handle ); } /** * Output the opening markup for a context box. * * @since 2.2.4 * @param bool $add_handle Whether to add the metabox handle and opening div for .inside. */ public function context_box_title_markup_open( $add_handle = true ) { $cmb_id = $this->cmb->cmb_id; $title = $this->cmb->prop( 'title' ); $screen = get_current_screen(); $page = $screen->id; $is_55 = CMB2_Utils::wp_at_least( '5.5' ); add_filter( "postbox_classes_{$page}_{$cmb_id}", array( $this, 'postbox_classes' ) ); $hidden_class = ''; if ( $is_55 ) { // get_hidden_meta_boxes() doesn't apply in the block editor. $is_hidden = ! $screen->is_block_editor() && in_array( $cmb_id, get_hidden_meta_boxes( $screen ), true ); $hidden_class = $is_hidden ? ' hide-if-js' : ''; } $toggle_button = sprintf( '', /* translators: %s: name of CMB2 box (panel) */ sprintf( __( 'Toggle panel: %s' ), $title ) ); $title_tag = '

' . esc_attr( $title ) . '

' . "\n"; echo '
' . "\n"; if ( $add_handle ) { if ( $is_55 ) { echo '
'; echo $title_tag; echo '
'; echo $toggle_button; echo '
'; echo '
' . "\n"; } else { echo $toggle_button; echo $title_tag; } echo '
' . "\n"; } } /** * Output the closing markup for a context box. * * @since 2.2.4 * @param bool $add_inside_close Whether to add closing div for .inside. */ public function context_box_title_markup_close( $add_inside_close = true ) { // Load the closing divs for a title box. if ( $add_inside_close ) { echo '
' . "\n"; // .inside } echo '
' . "\n"; // .context-box } /** * Add metaboxes (to 'post' or 'comment' object types) * * @since 1.0.0 */ public function add_metaboxes() { if ( ! $this->show_on() ) { return; } /* * To keep from registering an actual post-screen metabox, * omit the 'title' property from the metabox registration array. * * (WordPress will not display metaboxes without titles anyway) * * This is a good solution if you want to handle outputting your * metaboxes/fields elsewhere in the post-screen. */ if ( ! $this->cmb->prop( 'title' ) ) { return; } $page = get_current_screen()->id; add_filter( "postbox_classes_{$page}_{$this->cmb->cmb_id}", array( $this, 'postbox_classes' ) ); foreach ( $this->cmb->box_types() as $object_type ) { add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $object_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ), $this->cmb->prop( 'mb_callback_args' ) ); } } /** * Remove the specified default taxonomy metaboxes for a post-type. * * @since 2.2.3 * */ public function remove_default_tax_metaboxes() { $to_remove = array_filter( (array) $this->cmb->tax_metaboxes_to_remove, 'taxonomy_exists' ); if ( empty( $to_remove ) ) { return; } foreach ( $this->cmb->box_types() as $post_type ) { foreach ( $to_remove as $taxonomy ) { $mb_id = is_taxonomy_hierarchical( $taxonomy ) ? "{$taxonomy}div" : "tagsdiv-{$taxonomy}"; remove_meta_box( $mb_id, $post_type, 'side' ); } } } /** * Modify metabox postbox classes. * * @since 2.2.4 * @param array $classes Array of classes. * @return array Modified array of classes */ public function postbox_classes( $classes ) { if ( $this->cmb->prop( 'closed' ) && ! in_array( 'closed', $classes ) ) { $classes[] = 'closed'; } if ( $this->cmb->is_alternate_context_box() ) { $classes = $this->alternate_context_postbox_classes( $classes ); } else { $classes[] = 'cmb2-postbox'; } return $classes; } /** * Modify metabox altnernate context postbox classes. * * @since 2.2.4 * @param array $classes Array of classes. * @return array Modified array of classes */ protected function alternate_context_postbox_classes( $classes ) { $classes[] = 'context-box'; $classes[] = 'context-' . $this->cmb->prop( 'context' ) . '-box'; if ( in_array( $this->cmb->cmb_id, get_hidden_meta_boxes( get_current_screen() ) ) ) { $classes[] = 'hide-if-js'; } $add_wrap = $this->cmb->prop( 'title' ) || ! $this->cmb->prop( 'remove_box_wrap' ); if ( $add_wrap ) { $classes[] = 'cmb2-postbox postbox'; } else { $classes[] = 'cmb2-no-box-wrap'; } return $classes; } /** * Display metaboxes for a post or comment object. * * @since 1.0.0 */ public function metabox_callback() { $object_id = 'comment' === $this->object_type ? get_comment_ID() : get_the_ID(); $this->cmb->show_form( $object_id, $this->object_type ); } /** * Display metaboxes for new user page. * * @since 1.0.0 * * @param mixed $section User section metabox. */ public function user_new_metabox( $section ) { if ( $section === $this->cmb->prop( 'new_user_section' ) ) { $object_id = $this->cmb->object_id(); $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id ); $this->user_metabox(); } } /** * Display metaboxes for a user object. * * @since 1.0.0 */ public function user_metabox() { $this->show_form_for_type( 'user' ); } /** * Display metaboxes for a taxonomy term object. * * @since 2.2.0 */ public function term_metabox() { $this->show_form_for_type( 'term' ); } /** * Display metaboxes for an object type. * * @since 2.2.0 * @param string $type Object type. * @return void */ public function show_form_for_type( $type ) { if ( $type != $this->object_type ) { return; } if ( ! $this->show_on() ) { return; } if ( $this->cmb->prop( 'cmb_styles' ) ) { self::enqueue_cmb_css(); } if ( $this->cmb->prop( 'enqueue_js' ) ) { self::enqueue_cmb_js(); } $this->cmb->show_form( 0, $type ); } /** * Determines if metabox should be shown in current context. * * @since 2.0.0 * @return bool Whether metabox should be added/shown. */ public function show_on() { // If metabox is requesting to be conditionally shown. $show = $this->cmb->should_show(); /** * Filter to determine if metabox should show. Default is true. * * @param array $show Default is true, show the metabox. * @param mixed $meta_box_args Array of the metabox arguments. * @param mixed $cmb The CMB2 instance. */ $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb ); return $show; } /** * Get the CMB priority property set to numeric hook priority. * * @since 2.2.0 * * @param integer $default Default display hook priority. * @return integer Hook priority. */ public function get_priority( $default = 10 ) { $priority = $this->cmb->prop( 'priority' ); if ( ! is_numeric( $priority ) ) { switch ( $priority ) { case 'high': $priority = 5; break; case 'low': $priority = 20; break; default: $priority = $default; break; } } return $priority; } /** * Save data from post metabox * * @since 1.0.0 * @param int $post_id Post ID. * @param mixed $post Post object. * @return void */ public function save_post( $post_id, $post = false ) { $post_type = $post ? $post->post_type : get_post_type( $post_id ); $do_not_pass_go = ( ! $this->can_save( $post_type ) // Check user editing permissions. || ( 'page' === $post_type && ! current_user_can( 'edit_page', $post_id ) ) || ! current_user_can( 'edit_post', $post_id ) ); if ( $do_not_pass_go ) { return; } $this->cmb->save_fields( $post_id, 'post', $_POST ); } /** * Save data from comment metabox. * * @since 2.0.9 * @param int $comment_id Comment ID. * @return void */ public function save_comment( $comment_id ) { $can_edit = current_user_can( 'moderate_comments', $comment_id ); if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) { $this->cmb->save_fields( $comment_id, 'comment', $_POST ); } } /** * Save data from user fields. * * @since 1.0.x * @param int $user_id User ID. * @return void */ public function save_user( $user_id ) { // check permissions. if ( $this->can_save( 'user' ) ) { $this->cmb->save_fields( $user_id, 'user', $_POST ); } } /** * Save data from term fields * * @since 2.2.0 * @param int $term_id Term ID. * @param int $tt_id Term Taxonomy ID. * @param string $taxonomy Taxonomy. * @return void */ public function save_term( $term_id, $tt_id, $taxonomy = '' ) { $taxonomy = $taxonomy ? $taxonomy : $tt_id; // check permissions. if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) { $this->cmb->save_fields( $term_id, 'term', $_POST ); } } /** * Delete term meta when a term is deleted. * * @since 2.2.0 * @param int $term_id Term ID. * @param int $tt_id Term Taxonomy ID. * @param string $taxonomy Taxonomy. * @return void */ public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { if ( $this->taxonomy_can_save( $taxonomy ) ) { $data_to_delete = array(); foreach ( $this->cmb->prop( 'fields' ) as $field ) { $data_to_delete[ $field['id'] ] = ''; } $this->cmb->save_fields( $term_id, 'term', $data_to_delete ); } } /** * Determines if the current object is able to be saved. * * @since 2.0.9 * @param string $type Current object type. * @return bool Whether object can be saved. */ public function can_save( $type = '' ) { $can_save = ( $this->cmb->prop( 'save_fields' ) // check nonce. && isset( $_POST[ $this->cmb->nonce() ] ) && wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() ) // check if autosave. && ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // get the metabox types & compare it to this type. && ( $type && in_array( $type, $this->cmb->box_types() ) ) // Don't do updates during a switch-to-blog instance. && ! ( is_multisite() && ms_is_switched() ) ); /** * Filter to determine if metabox is allowed to save. * * @param bool $can_save Whether the current metabox can save. * @param object $cmb The CMB2 instance. */ return apply_filters( 'cmb2_can_save', $can_save, $this->cmb ); } /** * Determine if taxonomy of term being modified is cmb2-editable. * * @since 2.2.0 * * @param string $taxonomy Taxonomy of term being modified. * @return bool Whether taxonomy is editable. */ public function taxonomy_can_save( $taxonomy ) { if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) { return false; } $taxonomy_object = get_taxonomy( $taxonomy ); // Can the user edit this term? if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) { return false; } return true; } /** * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). * * @since 2.2.2.1 */ protected function maybe_enqueue_column_display_styles() { global $pagenow; if ( $pagenow && $this->cmb->has_columns && $this->cmb->prop( 'cmb_styles' ) && in_array( $pagenow, array( 'edit.php', 'users.php', 'edit-comments.php', 'edit-tags.php' ), 1 ) ) { self::enqueue_cmb_css( 'cmb2-display-styles' ); } } /** * Includes CMB2 styles. * * @since 2.0.0 * * @param string $handle CSS handle. * @return mixed */ public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { /** * Filter to determine if CMB2'S css should be enqueued. * * @param bool $enqueue_css Default is true. */ if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) { return false; } self::register_styles(); /* * White list the options as this method can be used as a hook callback * and have a different argument passed. */ return wp_enqueue_style( 'cmb2-display-styles' === $handle ? $handle : 'cmb2-styles' ); } /** * Includes CMB2 JS. * * @since 2.0.0 */ public static function enqueue_cmb_js() { /** * Filter to determine if CMB2'S JS should be enqueued. * * @param bool $enqueue_js Default is true. */ if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) { return false; } self::register_js(); return true; } } CMB2_Hookup_Base.php000064400000005112151717007230010221 0ustar00cmb = $cmb; $this->object_type = $this->cmb->mb_object_type(); } abstract public function universal_hooks(); /** * Ensures WordPress hook only gets fired once per object. * * @since 2.0.0 * @param string $action The name of the filter to hook the $hook callback to. * @param callback $hook The callback to be run when the filter is applied. * @param integer $priority Order the functions are executed. * @param int $accepted_args The number of arguments the function accepts. */ public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { static $hooks_completed = array(); $args = func_get_args(); // Get object hash.. This bypasses issues with serializing closures. if ( is_object( $hook ) ) { $args[1] = spl_object_hash( $args[1] ); } elseif ( is_array( $hook ) && is_object( $hook[0] ) ) { $args[1][0] = spl_object_hash( $hook[0] ); } $key = md5( serialize( $args ) ); if ( ! isset( $hooks_completed[ $key ] ) ) { $hooks_completed[ $key ] = 1; add_filter( $action, $hook, $priority, $accepted_args ); } } /** * Magic getter for our object. * * @param string $field Property to return. * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __get( $field ) { switch ( $field ) { case 'object_type': case 'cmb': return $this->{$field}; default: throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); } } } CMB2_Hookup_Field.php000064400000013400151717007230010371 0ustar00box_types() as $object_type ) { if ( ! $cmb->is_supported_core_object_type( $object_type ) ) { // Ignore post-types... continue; } if ( empty( $field['field_hookup_instance'][ $object_type ] ) ) { $instance = new self( $field, $object_type, $cmb ); $method = 'options-page' === $object_type ? 'text_datetime_timestamp_timezone_option_back_compat' : 'text_datetime_timestamp_timezone_back_compat'; $field['field_hookup_instance'][ $object_type ] = array( $instance, $method ); } if ( false === $field['field_hookup_instance'][ $object_type ] ) { // If set to false, no need to filter. // This can be set if you have updated your use of the field type value to // assume the JSON value. continue; } if ( 'options-page' === $object_type ) { $option_name = $cmb->object_id(); add_filter( "pre_option_{$option_name}", $field['field_hookup_instance'][ $object_type ], 10, 3 ); continue; } add_filter( "get_{$object_type}_metadata", $field['field_hookup_instance'][ $object_type ], 10, 5 ); } break; } return $field; } /** * Constructor * * @since 2.11.0 * @param CMB2 $cmb The CMB2 object to hookup. */ public function __construct( $field, $object_type, CMB2 $cmb ) { $this->field_id = $field['id']; $this->object_type = $object_type; $this->cmb_id = $cmb->cmb_id; } /** * Adds a back-compat shim for text_datetime_timestamp_timezone field type values. * * Handles old serialized DateTime values, as well as the new JSON formatted values. * * @since 2.11.0 * * @param mixed $value The value of the metadata. * @param int $object_id ID of the object metadata is for. * @param string $meta_key Meta key. * @param bool $single Whether to return a single value. * @param string $meta_type Type of object metadata is for. * @return mixed Maybe reserialized value. */ public function text_datetime_timestamp_timezone_back_compat( $value, $object_id, $meta_key, $single, $meta_type ) { if ( $meta_key === $this->field_id ) { remove_filter( "get_{$meta_type}_metadata", [ $this, __FUNCTION__ ], 10, 5 ); $value = get_metadata( $meta_type, $object_id, $meta_key, $single ); add_filter( "get_{$meta_type}_metadata", [ $this, __FUNCTION__ ], 10, 5 ); $value = $this->reserialize_safe_value( $value ); } return $value; } /** * Adds a back-compat shim for text_datetime_timestamp_timezone field type values on options pages. * * Handles old serialized DateTime values, as well as the new JSON formatted values. * * @since 2.11.0 * * @param mixed $value The value of the option. * @param string $option Option name. * @param mixed $default_value Default value. * @return mixed The updated value. */ public function text_datetime_timestamp_timezone_option_back_compat( $value, $option, $default_value ) { remove_filter( "pre_option_{$option}", [ $this, __FUNCTION__ ], 10, 3 ); $value = get_option( $option, $default_value ); add_filter( "pre_option_{$option}", [ $this, __FUNCTION__ ], 10, 3 ); if ( ! empty( $value ) && is_array( $value ) ) { // Loop fields and update values for all text_datetime_timestamp_timezone fields. foreach ( CMB2_Boxes::get( $this->cmb_id )->prop( 'fields' ) as $field ) { if ( 'text_datetime_timestamp_timezone' === $field['type'] && ! empty( $value[ $field['id'] ] ) ) { $value[ $field['id'] ] = $this->reserialize_safe_value( $value[ $field['id'] ] ); } } } return $value; } /** * Reserialize a value to a safe serialized DateTime value. * * @since 2.11.0 * * @param mixed $value The value to check. * @return mixed The value, possibly reserialized. */ protected function reserialize_safe_value( $value ) { if ( is_array( $value ) ) { return array_map( [ $this, 'reserialize_safe_value' ], $value ); } $updated_val = CMB2_Utils::get_datetime_from_value( $value ); $value = $updated_val ? serialize( $updated_val ) : ''; return $value; } } CMB2_JS.php000064400000020200151717007230006331 0ustar00 'jquery', ); /** * Array of CMB2 fields model data for JS. * * @var array * @since 2.4.0 */ protected static $fields = array(); /** * Add a dependency to the array of CMB2 JS dependencies * * @since 2.0.7 * @param array|string $dependencies Array (or string) of dependencies to add. */ public static function add_dependencies( $dependencies ) { foreach ( (array) $dependencies as $dependency ) { self::$dependencies[ $dependency ] = $dependency; } } /** * Add field model data to the array for JS. * * @since 2.4.0 * * @param CMB2_Field $field Field object. */ public static function add_field_data( CMB2_Field $field ) { $hash = $field->hash_id(); if ( ! isset( self::$fields[ $hash ] ) ) { self::$fields[ $hash ] = $field->js_data(); } } /** * Enqueue the CMB2 JS * * @since 2.0.7 */ public static function enqueue() { // Filter required script dependencies. $dependencies = self::$dependencies = apply_filters( 'cmb2_script_dependencies', self::$dependencies ); // Only use minified files if SCRIPT_DEBUG is off. $debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; $min = $debug ? '' : '.min'; // if colorpicker. if ( isset( $dependencies['wp-color-picker'] ) ) { if ( ! is_admin() ) { self::colorpicker_frontend(); } // Enqueue colorpicker if ( ! wp_script_is( 'wp-color-picker', 'enqueued' ) ) { wp_enqueue_script( 'wp-color-picker' ); } if ( isset( $dependencies['wp-color-picker-alpha'] ) ) { self::register_colorpicker_alpha(); } } // if file/file_list. if ( isset( $dependencies['media-editor'] ) ) { wp_enqueue_media(); CMB2_Type_File_Base::output_js_underscore_templates(); } // if timepicker. if ( isset( $dependencies['jquery-ui-datetimepicker'] ) ) { self::register_datetimepicker(); } // if cmb2-wysiwyg. $enqueue_wysiwyg = isset( $dependencies['cmb2-wysiwyg'] ) && $debug; unset( $dependencies['cmb2-wysiwyg'] ); // if cmb2-char-counter. $enqueue_char_counter = isset( $dependencies['cmb2-char-counter'] ) && $debug; unset( $dependencies['cmb2-char-counter'] ); // Enqueue cmb JS. wp_enqueue_script( self::$handle, CMB2_Utils::url( "js/cmb2{$min}.js" ), array_values( $dependencies ), CMB2_VERSION, true ); // if SCRIPT_DEBUG, we need to enqueue separately. if ( $enqueue_wysiwyg ) { wp_enqueue_script( 'cmb2-wysiwyg', CMB2_Utils::url( 'js/cmb2-wysiwyg.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); } if ( $enqueue_char_counter ) { wp_enqueue_script( 'cmb2-char-counter', CMB2_Utils::url( 'js/cmb2-char-counter.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); } self::localize( $debug ); do_action( 'cmb2_footer_enqueue' ); } /** * Register or enqueue the wp-color-picker-alpha script. * * @since 2.2.7 * * @param boolean $enqueue Whether or not to enqueue. * * @return void */ public static function register_colorpicker_alpha( $enqueue = false ) { // Only use minified files if SCRIPT_DEBUG is off. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; $func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script'; $func( 'wp-color-picker-alpha', CMB2_Utils::url( "js/wp-color-picker-alpha{$min}.js" ), array( 'wp-color-picker' ), '2.1.3' ); } /** * Register or enqueue the jquery-ui-datetimepicker script. * * @since 2.2.7 * * @param boolean $enqueue Whether or not to enqueue. * * @return void */ public static function register_datetimepicker( $enqueue = false ) { $func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script'; $func( 'jquery-ui-datetimepicker', CMB2_Utils::url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), '1.5.0' ); } /** * We need to register colorpicker on the front-end * * @since 2.0.7 */ protected static function colorpicker_frontend() { wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION ); wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION ); wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array( 'clear' => esc_html__( 'Clear', 'cmb2' ), 'defaultString' => esc_html__( 'Default', 'cmb2' ), 'pick' => esc_html__( 'Select Color', 'cmb2' ), 'current' => esc_html__( 'Current Color', 'cmb2' ), ) ); } /** * Localize the php variables for CMB2 JS * * @since 2.0.7 * * @param mixed $debug Whether or not we are debugging. */ protected static function localize( $debug ) { static $localized = false; if ( $localized ) { return; } $localized = true; $l10n = array( 'fields' => self::$fields, 'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), 'ajaxurl' => admin_url( '/admin-ajax.php' ), 'script_debug' => $debug, 'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2', 'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2', 'user_can_richedit' => user_can_richedit(), 'defaults' => array( 'code_editor' => false, 'color_picker' => false, 'date_picker' => array( 'changeMonth' => true, 'changeYear' => true, 'dateFormat' => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ), 'dayNames' => explode( ',', esc_html__( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ), 'dayNamesMin' => explode( ',', esc_html__( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ), 'dayNamesShort' => explode( ',', esc_html__( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ), 'monthNames' => explode( ',', esc_html__( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ), 'monthNamesShort' => explode( ',', esc_html__( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ), 'nextText' => esc_html__( 'Next', 'cmb2' ), 'prevText' => esc_html__( 'Prev', 'cmb2' ), 'currentText' => esc_html__( 'Today', 'cmb2' ), 'closeText' => esc_html__( 'Done', 'cmb2' ), 'clearText' => esc_html__( 'Clear', 'cmb2' ), ), 'time_picker' => array( 'timeOnlyTitle' => esc_html__( 'Choose Time', 'cmb2' ), 'timeText' => esc_html__( 'Time', 'cmb2' ), 'hourText' => esc_html__( 'Hour', 'cmb2' ), 'minuteText' => esc_html__( 'Minute', 'cmb2' ), 'secondText' => esc_html__( 'Second', 'cmb2' ), 'currentText' => esc_html__( 'Now', 'cmb2' ), 'closeText' => esc_html__( 'Done', 'cmb2' ), 'timeFormat' => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ), 'controlType' => 'select', 'stepMinute' => 5, ), ), 'strings' => array( 'upload_file' => esc_html__( 'Use this file', 'cmb2' ), 'upload_files' => esc_html__( 'Use these files', 'cmb2' ), 'remove_image' => esc_html__( 'Remove Image', 'cmb2' ), 'remove_file' => esc_html__( 'Remove', 'cmb2' ), 'file' => esc_html__( 'File:', 'cmb2' ), 'download' => esc_html__( 'Download', 'cmb2' ), 'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ), ), ); if ( isset( self::$dependencies['code-editor'] ) && function_exists( 'wp_enqueue_code_editor' ) ) { $l10n['defaults']['code_editor'] = wp_enqueue_code_editor( array( 'type' => 'text/html', ) ); } wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) ); } } CMB2_Options.php000064400000014060151717007230007457 0ustar00key = ! empty( $option_key ) ? $option_key : ''; } /** * Delete the option from the db * * @since 2.0.0 * @return mixed Delete success or failure */ public function delete_option() { $deleted = $this->key ? delete_option( $this->key ) : true; $this->options = $deleted ? array() : $this->options; return $this->options; } /** * Removes an option from an option array * * @since 1.0.1 * @param string $field_id Option array field key. * @param bool $resave Whether or not to resave. * @return array Modified options */ public function remove( $field_id, $resave = false ) { $this->get_options(); if ( isset( $this->options[ $field_id ] ) ) { unset( $this->options[ $field_id ] ); } if ( $resave ) { $this->set(); } return $this->options; } /** * Retrieves an option from an option array * * @since 1.0.1 * @param string $field_id Option array field key. * @param mixed $default Fallback value for the option. * @return array Requested field or default */ public function get( $field_id, $default = false ) { $opts = $this->get_options(); if ( 'all' == $field_id ) { return $opts; } elseif ( array_key_exists( $field_id, $opts ) ) { return false !== $opts[ $field_id ] ? $opts[ $field_id ] : $default; } return $default; } /** * Updates Option data * * @since 1.0.1 * @param string $field_id Option array field key. * @param mixed $value Value to update data with. * @param bool $resave Whether to re-save the data. * @param bool $single Whether data should not be an array. * @return boolean Return status of update. */ public function update( $field_id, $value = '', $resave = false, $single = true ) { $this->get_options(); if ( true !== $field_id ) { if ( ! $single ) { // If multiple, add to array. $this->options[ $field_id ][] = $value; } else { $this->options[ $field_id ] = $value; } } if ( $resave || true === $field_id ) { return $this->set(); } return true; } /** * Saves the option array * Needs to be run after finished using remove/update_option * * @uses apply_filters() Calls 'cmb2_override_option_save_{$this->key}' hook * to allow overwriting the option value to be stored. * * @since 1.0.1 * @param array $options Optional options to override. * @return bool Success/Failure */ public function set( $options = array() ) { if ( ! empty( $options ) || empty( $options ) && empty( $this->key ) ) { $this->options = $options; } $this->options = wp_unslash( $this->options ); // get rid of those evil magic quotes. if ( empty( $this->key ) ) { return false; } $test_save = apply_filters( "cmb2_override_option_save_{$this->key}", 'cmb2_no_override_option_save', $this->options, $this ); if ( 'cmb2_no_override_option_save' !== $test_save ) { // If override, do not proceed to update the option, just return result. return $test_save; } /** * Whether to auto-load the option when WordPress starts up. * * The dynamic portion of the hook name, $this->key, refers to the option key. * * @since 2.4.0 * * @param bool $autoload Whether to load the option when WordPress starts up. * @param CMB2_Option $cmb_option This object. */ $autoload = apply_filters( "cmb2_should_autoload_{$this->key}", true, $this ); return update_option( $this->key, $this->options, ! $autoload || 'no' === $autoload ? false : true ); } /** * Retrieve option value based on name of option. * * @uses apply_filters() Calls 'cmb2_override_option_get_{$this->key}' hook to allow * overwriting the option value to be retrieved. * * @since 1.0.1 * @param mixed $default Optional. Default value to return if the option does not exist. * @return mixed Value set for the option. */ public function get_options( $default = null ) { if ( empty( $this->options ) && ! empty( $this->key ) ) { $test_get = apply_filters( "cmb2_override_option_get_{$this->key}", 'cmb2_no_override_option_get', $default, $this ); if ( 'cmb2_no_override_option_get' !== $test_get ) { $this->options = $test_get; } else { // If no override, get the option. $this->options = get_option( $this->key, $default ); } } $this->options = (array) $this->options; return $this->options; } /** * Magic getter for our object. * * @since 2.6.0 * * @param string $field Requested property. * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __get( $field ) { switch ( $field ) { case 'options': case 'key': return $this->{$field}; default: throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); } } } CMB2_Options_Hookup.php000064400000026011151717007230011003 0ustar00cmb = $cmb; $this->option_key = $option_key; } public function hooks() { if ( empty( $this->option_key ) ) { return; } if ( ! $this->cmb->prop( 'autoload', true ) ) { // Disable option autoload if requested. add_filter( "cmb2_should_autoload_{$this->option_key}", '__return_false' ); } /** * For WP < 4.7. Ensure the register_setting function exists. */ if ( ! CMB2_Utils::wp_at_least( '4.7' ) && ! function_exists( 'register_setting' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } // Register setting to cmb2 group. register_setting( 'cmb2', $this->option_key ); // Handle saving the data. add_action( 'admin_post_' . $this->option_key, array( $this, 'save_options' ) ); // Optionally network_admin_menu. $hook = $this->cmb->prop( 'admin_menu_hook' ); // Hook in to add our menu. add_action( $hook, array( $this, 'options_page_menu_hooks' ), $this->get_priority() ); // If in the network admin, need to use get/update_site_option. if ( 'network_admin_menu' === $hook ) { // Override CMB's getter. add_filter( "cmb2_override_option_get_{$this->option_key}", array( $this, 'network_get_override' ), 10, 2 ); // Override CMB's setter. add_filter( "cmb2_override_option_save_{$this->option_key}", array( $this, 'network_update_override' ), 10, 2 ); } } /** * Hook up our admin menu item and admin page. * * @since 2.2.5 * * @return void */ public function options_page_menu_hooks() { $parent_slug = $this->cmb->prop( 'parent_slug' ); $title = $this->cmb->prop( 'title' ); $menu_title = $this->cmb->prop( 'menu_title', $title ); $capability = $this->cmb->prop( 'capability' ); $callback = array( $this, 'options_page_output' ); if ( $parent_slug ) { $page_hook = add_submenu_page( $parent_slug, $title, $menu_title, $capability, $this->option_key, $callback ); } else { $page_hook = add_menu_page( $title, $menu_title, $capability, $this->option_key, $callback, $this->cmb->prop( 'icon_url' ), $this->cmb->prop( 'position' ) ); } if ( $this->cmb->prop( 'cmb_styles' ) ) { // Include CMB CSS in the head to avoid FOUC. add_action( "admin_print_styles-{$page_hook}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) ); } $this->maybe_register_message(); } /** * If there is a message callback, let it determine how to register the message, * else add a settings message if on this settings page. * * @since 2.2.6 * * @return void */ public function maybe_register_message() { $is_options_page = self::is_page( $this->option_key ); $should_notify = ! $this->cmb->prop( 'disable_settings_errors' ) && isset( $_GET['settings-updated'] ) && $is_options_page; $is_updated = $should_notify && 'true' === $_GET['settings-updated']; $setting = "{$this->option_key}-notices"; $code = ''; $message = __( 'Nothing to update.', 'cmb2' ); $type = 'notice-warning'; if ( $is_updated ) { $message = __( 'Settings updated.', 'cmb2' ); $type = 'updated'; } // Check if parameter has registered a callback. if ( $cb = $this->cmb->maybe_callback( 'message_cb' ) ) { /** * The 'message_cb' callback will receive the following parameters. * Unless there are other reasons for notifications, the callback should only * `add_settings_error()` if `$args['should_notify']` is truthy. * * @param CMB2 $cmb The CMB2 object. * @param array $args { * An array of message arguments * * @type bool $is_options_page Whether current page is this options page. * @type bool $should_notify Whether options were saved and we should be notified. * @type bool $is_updated Whether options were updated with save (or stayed the same). * @type string $setting For add_settings_error(), Slug title of the setting to which * this error applies. * @type string $code For add_settings_error(), Slug-name to identify the error. * Used as part of 'id' attribute in HTML output. * @type string $message For add_settings_error(), The formatted message text to display * to the user (will be shown inside styled `
` and `

` tags). * Will be 'Settings updated.' if $is_updated is true, else 'Nothing to update.' * @type string $type For add_settings_error(), Message type, controls HTML class. * Accepts 'error', 'updated', '', 'notice-warning', etc. * Will be 'updated' if $is_updated is true, else 'notice-warning'. * } */ $args = compact( 'is_options_page', 'should_notify', 'is_updated', 'setting', 'code', 'message', 'type' ); $this->cmb->do_callback( $cb, $args ); } elseif ( $should_notify ) { add_settings_error( $setting, $code, $message, $type ); } } /** * Display options-page output. To override, set 'display_cb' box property. * * @since 2.2.5 */ public function options_page_output() { $this->maybe_output_settings_notices(); $callback = $this->cmb->prop( 'display_cb' ); if ( is_callable( $callback ) ) { return call_user_func( $callback, $this ); } ?>

cmb->prop( 'title' ) ) : ?>

cmb->prop( 'title' ) ); ?>

options_page_tab_nav_output(); ?>
options_page_metabox(); ?> cmb->prop( 'save_button' ) ), 'primary', 'submit-cmb' ); ?>
get_tab_group_tabs(); if ( empty( $tabs ) ) { return; } ?> option_key}-notices" ); } } /** * Gets navigation tabs array for CMB2 options pages which share the * same tab_group property. * * @since 2.4.0 * @return array Array of tab information ($option_key => $tab_title) */ public function get_tab_group_tabs() { $tab_group = $this->cmb->prop( 'tab_group' ); $tabs = array(); if ( $tab_group ) { $boxes = CMB2_Boxes::get_by( 'tab_group', $tab_group ); foreach ( $boxes as $cmb_id => $cmb ) { $option_key = $cmb->options_page_keys(); // Must have an option key, must be an options page box. if ( ! isset( $option_key[0] ) || 'options-page' !== $cmb->mb_object_type() ) { continue; } $tabs[ $option_key[0] ] = $cmb->prop( 'tab_title', $cmb->prop( 'title' ) ); } } return apply_filters( 'cmb2_tab_group_tabs', $tabs, $tab_group ); } /** * Display metaboxes for an options-page object. * * @since 2.2.5 */ public function options_page_metabox() { $this->show_form_for_type( 'options-page' ); } /** * Save data from options page, then redirects back. * * @since 2.2.5 * @return void */ public function save_options() { $url = wp_get_referer(); if ( ! $url ) { $url = admin_url(); } if ( $this->can_save( 'options-page' ) // check params. && isset( $_POST['submit-cmb'], $_POST['action'] ) && $this->option_key === $_POST['action'] ) { $updated = $this->cmb ->save_fields( $this->option_key, $this->cmb->object_type(), $_POST ) ->was_updated(); // Will be false if no values were changed/updated. $url = add_query_arg( 'settings-updated', $updated ? 'true' : 'false', $url ); } wp_safe_redirect( esc_url_raw( $url ), 303 /* WP_Http::SEE_OTHER */ ); exit; } /** * Replaces get_option with get_site_option. * * @since 2.2.5 * * @param mixed $test Not used. * @param mixed $default Default value to use. * @return mixed Value set for the network option. */ public function network_get_override( $test, $default = false ) { return get_site_option( $this->option_key, $default ); } /** * Replaces update_option with update_site_option. * * @since 2.2.5 * * @param mixed $test Not used. * @param mixed $option_value Value to use. * @return bool Success/Failure */ public function network_update_override( $test, $option_value ) { return update_site_option( $this->option_key, $option_value ); } /** * Determines if given page slug matches the 'page' GET query variable. * * @since 2.4.0 * * @param string $page Page slug. * @return boolean */ public static function is_page( $page ) { return isset( $_GET['page'] ) && $page === $_GET['page']; } /** * Magic getter for our object. * * @param string $field Property to retrieve. * * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __get( $field ) { switch ( $field ) { case 'object_type': case 'option_key': case 'cmb': return $this->{$field}; default: throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); } } } CMB2_Sanitize.php000064400000042163151717007230007617 0ustar00field = $field; $this->value = $value; } /** * Catchall method if field's 'sanitization_cb' is NOT defined, * or field type does not have a corresponding validation method. * * @since 1.0.0 * * @param string $name Non-existent method name. * @param array $arguments All arguments passed to the method. * @return mixed */ public function __call( $name, $arguments ) { return $this->default_sanitization(); } /** * Default fallback sanitization method. Applies filters. * * @since 1.0.2 */ public function default_sanitization() { $field_type = $this->field->type(); /** * This exists for back-compatibility, but validation * is not what happens here. * * @deprecated See documentation for "cmb2_sanitize_{$field_type}". */ if ( function_exists( 'apply_filters_deprecated' ) ) { $override_value = apply_filters_deprecated( "cmb2_validate_{$field_type}", array( null, $this->value, $this->field->object_id, $this->field->args(), $this ), '2.0.0', "cmb2_sanitize_{$field_type}" ); } else { $override_value = apply_filters( "cmb2_validate_{$field_type}", null, $this->value, $this->field->object_id, $this->field->args(), $this ); } if ( null !== $override_value ) { return $override_value; } $sanitized_value = ''; switch ( $field_type ) { case 'wysiwyg': case 'textarea_small': case 'oembed': $sanitized_value = $this->textarea(); break; case 'taxonomy_select': case 'taxonomy_select_hierarchical': case 'taxonomy_radio': case 'taxonomy_radio_inline': case 'taxonomy_radio_hierarchical': case 'taxonomy_multicheck': case 'taxonomy_multicheck_hierarchical': case 'taxonomy_multicheck_inline': $sanitized_value = $this->taxonomy(); break; case 'multicheck': case 'multicheck_inline': case 'file_list': case 'group': // no filtering $sanitized_value = $this->value; break; default: // Handle repeatable fields array // We'll fallback to 'sanitize_text_field' $sanitized_value = $this->_default_sanitization(); break; } return $this->_is_empty_array( $sanitized_value ) ? '' : $sanitized_value; } /** * Default sanitization method, sanitize_text_field. Checks if value is array. * * @since 2.2.4 * @return mixed Sanitized value. */ protected function _default_sanitization() { // Handle repeatable fields array. return is_array( $this->value ) ? array_map( 'sanitize_text_field', $this->value ) : sanitize_text_field( $this->value ); } /** * Sets the object terms to the object (if not options-page) and optionally returns the sanitized term values. * * @since 2.2.4 * @return mixed Blank value, or sanitized term values if "cmb2_return_taxonomy_values_{$cmb_id}" is true. */ public function taxonomy() { $sanitized_value = ''; if ( ! $this->field->args( 'taxonomy' ) ) { CMB2_Utils::log_if_debug( __METHOD__, __LINE__, "{$this->field->type()} {$this->field->_id( '', false )} is missing the 'taxonomy' parameter." ); } else { if ( in_array( $this->field->object_type, array( 'options-page', 'term' ), true ) ) { $return_values = true; } else { wp_set_object_terms( $this->field->object_id, $this->value, $this->field->args( 'taxonomy' ) ); $return_values = false; } $cmb_id = $this->field->cmb_id; /** * Filter whether 'taxonomy_*' fields should return their value when being sanitized. * * By default, these fields do not return a value as we do not want them stored to meta * (as they are stored as terms). This allows overriding that and is used by CMB2::get_sanitized_values(). * * The dynamic portion of the hook, $cmb_id, refers to the this field's CMB2 box id. * * @since 2.2.4 * * @param bool $return_values By default, this is only true for 'options-page' boxes. To enable: * `add_filter( "cmb2_return_taxonomy_values_{$cmb_id}", '__return_true' );` * @param CMB2_Sanitize $sanitizer This object. */ if ( apply_filters( "cmb2_return_taxonomy_values_{$cmb_id}", $return_values, $this ) ) { $sanitized_value = $this->_default_sanitization(); } } return $sanitized_value; } /** * Simple checkbox validation * * @since 1.0.1 * @return string|false 'on' or false */ public function checkbox() { return $this->value === 'on' ? 'on' : false; } /** * Validate url in a meta value. * * @since 1.0.1 * @return string Empty string or escaped url */ public function text_url() { $protocols = $this->field->args( 'protocols' ); $default = $this->field->get_default(); // for repeatable. if ( is_array( $this->value ) ) { foreach ( $this->value as $key => $val ) { $this->value[ $key ] = self::sanitize_and_secure_url( $val, $protocols, $default ); } } else { $this->value = self::sanitize_and_secure_url( $this->value, $protocols, $default ); } return $this->value; } public function colorpicker() { // for repeatable. if ( is_array( $this->value ) ) { $check = $this->value; $this->value = array(); foreach ( $check as $key => $val ) { if ( $val && '#' != $val ) { $this->value[ $key ] = esc_attr( $val ); } } } else { $this->value = ! $this->value || '#' == $this->value ? '' : esc_attr( $this->value ); } return $this->value; } /** * Validate email in a meta value * * @since 1.0.1 * @return string Empty string or sanitized email */ public function text_email() { // for repeatable. if ( is_array( $this->value ) ) { foreach ( $this->value as $key => $val ) { $val = trim( $val ); $this->value[ $key ] = is_email( $val ) ? $val : ''; } } else { $this->value = trim( $this->value ); $this->value = is_email( $this->value ) ? $this->value : ''; } return $this->value; } /** * Validate money in a meta value * * @since 1.0.1 * @return string Empty string or sanitized money value */ public function text_money() { if ( ! $this->value ) { return ''; } global $wp_locale; $search = array( $wp_locale->number_format['thousands_sep'], $wp_locale->number_format['decimal_point'] ); $replace = array( '', '.' ); // Strip slashes. Example: 2\'180.00. // See https://github.com/CMB2/CMB2/issues/1014. $this->value = wp_unslash( $this->value ); // for repeatable. if ( is_array( $this->value ) ) { foreach ( $this->value as $key => $val ) { if ( $val ) { $this->value[ $key ] = number_format_i18n( (float) str_ireplace( $search, $replace, $val ), 2 ); } } } else { $this->value = number_format_i18n( (float) str_ireplace( $search, $replace, $this->value ), 2 ); } return $this->value; } /** * Converts text date to timestamp * * @since 1.0.2 * @return string Timestring */ public function text_date_timestamp() { // date_create_from_format if there is a slash in the value. $this->value = wp_unslash( $this->value ); return is_array( $this->value ) ? array_map( array( $this->field, 'get_timestamp_from_value' ), $this->value ) : $this->field->get_timestamp_from_value( $this->value ); } /** * Datetime to timestamp * * @since 1.0.1 * * @param bool $repeat Whether or not to repeat. * @return string|array Timestring */ public function text_datetime_timestamp( $repeat = false ) { // date_create_from_format if there is a slash in the value. $this->value = wp_unslash( $this->value ); if ( $this->is_empty_value() ) { return ''; } $repeat_value = $this->_check_repeat( __FUNCTION__, $repeat ); if ( false !== $repeat_value ) { return $repeat_value; } // Account for timestamp values passed through REST API. if ( $this->is_valid_date_value() ) { $this->value = CMB2_Utils::make_valid_time_stamp( $this->value ); } elseif ( isset( $this->value['date'], $this->value['time'] ) ) { $this->value = $this->field->get_timestamp_from_value( $this->value['date'] . ' ' . $this->value['time'] ); } if ( $tz_offset = $this->field->field_timezone_offset() ) { $this->value += (int) $tz_offset; } return $this->value; } /** * Datetime to timestamp with timezone * * @since 1.0.1 * * @param bool $repeat Whether or not to repeat. * @return string Timestring */ public function text_datetime_timestamp_timezone( $repeat = false ) { static $utc_values = array(); if ( $this->is_empty_value() ) { return ''; } // date_create_from_format if there is a slash in the value. $this->value = wp_unslash( $this->value ); $utc_key = $this->field->_id( '', false ) . '_utc'; $repeat_value = $this->_check_repeat( __FUNCTION__, $repeat ); if ( false !== $repeat_value ) { if ( ! empty( $utc_values[ $utc_key ] ) ) { $this->_save_utc_value( $utc_key, $utc_values[ $utc_key ] ); unset( $utc_values[ $utc_key ] ); } return $repeat_value; } $tzstring = null; if ( is_array( $this->value ) && array_key_exists( 'timezone', $this->value ) ) { $tzstring = $this->value['timezone']; } if ( empty( $tzstring ) ) { $tzstring = CMB2_Utils::timezone_string(); } $offset = CMB2_Utils::timezone_offset( $tzstring ); if ( 'UTC' === substr( $tzstring, 0, 3 ) ) { $tzstring = timezone_name_from_abbr( '', $offset, 0 ); /** * The timezone_name_from_abbr() returns false if not found based on offset. * Since there are currently some invalid timezones in wp_timezone_dropdown(), * fallback to an offset of 0 (UTC+0) * https://core.trac.wordpress.org/ticket/29205 */ $tzstring = false !== $tzstring ? $tzstring : timezone_name_from_abbr( '', 0, 0 ); } $full_format = $this->field->args['date_format'] . ' ' . $this->field->args['time_format']; try { $datetime = null; if ( is_array( $this->value ) ) { $full_date = $this->value['date'] . ' ' . $this->value['time']; $datetime = date_create_from_format( $full_format, $full_date ); } elseif ( $this->is_valid_date_value() ) { $timestamp = CMB2_Utils::make_valid_time_stamp( $this->value ); if ( $timestamp ) { $datetime = new DateTime(); $datetime->setTimestamp( $timestamp ); } } if ( ! is_object( $datetime ) ) { $this->value = $utc_stamp = ''; } else { $datetime->setTimezone( new DateTimeZone( $tzstring ) ); $utc_stamp = date_timestamp_get( $datetime ) - $offset; $this->value = json_encode( $datetime ); } if ( $this->field->group ) { $this->value = array( 'supporting_field_value' => $utc_stamp, 'supporting_field_id' => $utc_key, 'value' => $this->value, ); } else { // Save the utc timestamp supporting field. if ( $repeat ) { $utc_values[ $utc_key ][] = $utc_stamp; } else { $this->_save_utc_value( $utc_key, $utc_stamp ); } } } catch ( Exception $e ) { $this->value = ''; CMB2_Utils::log_if_debug( __METHOD__, __LINE__, $e->getMessage() ); } return $this->value; } /** * Sanitize textareas and wysiwyg fields * * @since 1.0.1 * @return string Sanitized data */ public function textarea() { return is_array( $this->value ) ? array_map( 'wp_kses_post', $this->value ) : wp_kses_post( $this->value ); } /** * Sanitize code textareas * * @since 1.0.2 * * @param bool $repeat Whether or not to repeat. * @return string Sanitized data */ public function textarea_code( $repeat = false ) { $repeat_value = $this->_check_repeat( __FUNCTION__, $repeat ); if ( false !== $repeat_value ) { return $repeat_value; } return htmlspecialchars_decode( stripslashes( $this->value ), ENT_COMPAT ); } /** * Handles saving of attachment post ID and sanitizing file url * * @since 1.1.0 * @return string Sanitized url */ public function file() { $file_id_key = $this->field->_id( '', false ) . '_id'; if ( $this->field->group ) { // Return an array with url/id if saving a group field. $this->value = $this->_get_group_file_value_array( $file_id_key ); } else { $this->_save_file_id_value( $file_id_key ); $this->text_url(); } return $this->value; } /** * Gets the values for the `file` field type from the data being saved. * * @since 2.2.0 * * @param mixed $id_key ID key to use. * @return array */ public function _get_group_file_value_array( $id_key ) { $alldata = $this->field->group->data_to_save; $base_id = $this->field->group->_id( '', false ); $i = $this->field->group->index; // Check group $alldata data. $id_val = isset( $alldata[ $base_id ][ $i ][ $id_key ] ) ? absint( $alldata[ $base_id ][ $i ][ $id_key ] ) : ''; // We don't want to save 0 to the DB for file fields. if ( 0 === $id_val ) { $id_val = ''; } return array( 'value' => $this->text_url(), 'supporting_field_value' => $id_val, 'supporting_field_id' => $id_key, ); } /** * Peforms saving of `file` attachement's ID * * @since 1.1.0 * * @param mixed $file_id_key ID key to use. * @return mixed */ public function _save_file_id_value( $file_id_key ) { $id_field = $this->_new_supporting_field( $file_id_key ); // Check standard data_to_save data. $id_val = isset( $this->field->data_to_save[ $file_id_key ] ) ? $this->field->data_to_save[ $file_id_key ] : null; // If there is no ID saved yet, try to get it from the url. if ( $this->value && ! $id_val ) { $id_val = CMB2_Utils::image_id_from_url( $this->value ); // If there is an ID but user emptied the input value, remove the ID. } elseif ( ! $this->value && $id_val ) { $id_val = null; } return $id_field->save_field( $id_val ); } /** * Peforms saving of `text_datetime_timestamp_timezone` utc timestamp * * @since 2.2.0 * * @param mixed $utc_key UTC key. * @param mixed $utc_stamp UTC timestamp. * @return mixed */ public function _save_utc_value( $utc_key, $utc_stamp ) { return $this->_new_supporting_field( $utc_key )->save_field( $utc_stamp ); } /** * Returns a new, supporting, CMB2_Field object based on a new field id. * * @since 2.2.0 * * @param mixed $new_field_id New field ID. * @return CMB2_Field */ public function _new_supporting_field( $new_field_id ) { return $this->field->get_field_clone( array( 'id' => $new_field_id, 'sanitization_cb' => false, ) ); } /** * If repeating, loop through and re-apply sanitization method * * @since 1.1.0 * @param string $method Class method. * @param bool $repeat Whether repeating or not. * @return mixed Sanitized value */ public function _check_repeat( $method, $repeat ) { if ( $repeat || ! $this->field->args( 'repeatable' ) ) { return false; } $values_array = $this->value; $new_value = array(); foreach ( $values_array as $iterator => $this->value ) { if ( $this->value ) { $val = $this->$method( true ); if ( ! empty( $val ) ) { $new_value[] = $val; } } } $this->value = $new_value; return empty( $this->value ) ? null : $this->value; } /** * Determine if passed value is an empty array * * @since 2.0.6 * @param mixed $to_check Value to check. * @return boolean Whether value is an array that's empty */ public function _is_empty_array( $to_check ) { if ( is_array( $to_check ) ) { $cleaned_up = array_filter( $to_check ); return empty( $cleaned_up ); } return false; } /** * Sanitize a URL. Make the default scheme HTTPS. * * @since 2.10.0 * @param string $value Unescaped URL. * @param array $protocols Allowed protocols for URL. * @param string $default Default value if no URL found. * @return string escaped URL. */ public static function sanitize_and_secure_url( $url, $protocols = null, $default = null ) { if ( empty( $url ) ) { return $default; } $orig_scheme = parse_url( $url, PHP_URL_SCHEME ); $url = esc_url_raw( $url, $protocols ); // If original url has no scheme... if ( null === $orig_scheme ) { // Let's make sure the added scheme is https. $url = set_url_scheme( $url, 'https' ); } return $url; } /** * Check if the current field's value is empty. * * @since 2.9.1 * * @return boolean Wether value is empty. */ public function is_empty_value() { if ( empty( $this->value ) ) { return true; } if ( is_array( $this->value ) ) { $test = array_filter( $this->value ); if ( empty( $test ) ) { return true; } } return false; } /** * Check if the current field's value is a valid date value. * * @since 2.9.1 * * @return boolean Wether value is a valid date value. */ public function is_valid_date_value() { return is_scalar( $this->value ) && CMB2_Utils::is_valid_date( $this->value ); } } CMB2_Show_Filters.php000064400000010572151717007230010440 0ustar00object_id() : get_the_ID(); if ( ! $object_id ) { return false; } // If current page id is in the included array, display the metabox. return in_array( $object_id, (array) self::get_show_on_value( $meta_box_args ) ); } /** * Add metaboxes for an specific Page Template * * @since 1.0.0 * @param bool $display To display or not. * @param array $meta_box_args Metabox config array. * @param CMB2 $cmb CMB2 object. * @return bool Whether to display this metabox on the current page. */ public static function check_page_template( $display, $meta_box_args, $cmb ) { $key = self::get_show_on_key( $meta_box_args ); if ( ! $key || 'page-template' !== $key ) { return $display; } $object_id = $cmb->object_id(); if ( ! $object_id || 'post' !== $cmb->object_type() ) { return false; } // Get current template. $current_template = get_post_meta( $object_id, '_wp_page_template', true ); // See if there's a match. if ( $current_template && in_array( $current_template, (array) self::get_show_on_value( $meta_box_args ) ) ) { return true; } return false; } /** * Only show options-page metaboxes on their options page (but only enforce on the admin side) * * @since 1.0.0 * @param bool $display To display or not. * @param array $meta_box_args Metabox config array. * @return bool Whether to display this metabox on the current page. */ public static function check_admin_page( $display, $meta_box_args ) { $key = self::get_show_on_key( $meta_box_args ); // check if this is a 'options-page' metabox. if ( ! $key || 'options-page' !== $key ) { return $display; } // Enforce 'show_on' filter in the admin. if ( is_admin() ) { // If there is no 'page' query var, our filter isn't applicable. if ( ! isset( $_GET['page'] ) ) { return $display; } $show_on = self::get_show_on_value( $meta_box_args ); if ( empty( $show_on ) ) { return false; } if ( is_array( $show_on ) ) { foreach ( $show_on as $page ) { if ( $_GET['page'] == $page ) { return true; } } } else { if ( $_GET['page'] == $show_on ) { return true; } } return false; } // Allow options-page metaboxes to be displayed anywhere on the front-end. return true; } } CMB2_Types.php000064400000051642151717007230007137 0ustar00field = $field; } /** * Default fallback. Allows rendering fields via "cmb2_render_$fieldtype" hook * * @since 1.0.0 * @param string $fieldtype Non-existent field type name * @param array $arguments All arguments passed to the method */ public function __call( $fieldtype, $arguments ) { // Check for methods to be proxied to the CMB2_Type_Base object. if ( $exists = $this->maybe_proxy_method( $fieldtype, $arguments ) ) { return $exists['value']; } // Check for custom field type class. if ( $object = $this->maybe_custom_field_object( $fieldtype, $arguments ) ) { return $object->render(); } /** * Pass non-existent field types through an action. * * The dynamic portion of the hook name, $fieldtype, refers to the field type. * * @param array $field The passed in `CMB2_Field` object * @param mixed $escaped_value The value of this field escaped. * It defaults to `sanitize_text_field`. * If you need the unescaped value, you can access it * via `$field->value()` * @param int $object_id The ID of the current object * @param string $object_type The type of object you are working with. * Most commonly, `post` (this applies to all post-types), * but could also be `comment`, `user` or `options-page`. * @param object $field_type_object This `CMB2_Types` object */ do_action( "cmb2_render_{$fieldtype}", $this->field, $this->field->escaped_value(), $this->field->object_id, $this->field->object_type, $this ); } /** * Render a field (and handle repeatable) * * @since 1.1.0 */ public function render() { if ( $this->field->args( 'repeatable' ) ) { $this->render_repeatable_field(); } else { $this->_render(); } } /** * Render a field type * * @since 1.1.0 */ protected function _render() { $this->field->peform_param_callback( 'before_field' ); echo $this->{$this->field->type()}(); $this->field->peform_param_callback( 'after_field' ); } /** * Proxies the method call to the CMB2_Type_Base object, if it exists, otherwise returns a default fallback value. * * @since 2.2.2 * * @param string $method Method to call on the CMB2_Type_Base object. * @param mixed $default Default fallback value if method is not found. * @param array $args Optional arguments to pass to proxy method. * * @return mixed Results from called method. */ protected function proxy_method( $method, $default, $args = array() ) { if ( ! is_object( $this->type ) ) { $this->guess_type_object( $method ); } if ( is_object( $this->type ) && method_exists( $this->type, $method ) ) { return empty( $args ) ? $this->type->$method() : call_user_func_array( array( $this->type, $method ), $args ); } return $default; } /** * If no CMB2_Types::$type object is initiated when a proxy method is called, it means * it's a custom field type (which SHOULD be instantiating a Type), but let's try and * guess the type object for them and instantiate it. * * @since 2.2.3 * * @param string $method Method attempting to be called on the CMB2_Type_Base object. * @return bool */ protected function guess_type_object( $method ) { $fieldtype = $this->field->type(); // Try to "guess" the Type object based on the method requested. switch ( $method ) { case 'select_option': case 'list_input': case 'list_input_checkbox': case 'concat_items': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Select' ); break; case 'is_valid_img_ext': case 'img_status_output': case 'file_status_output': $this->get_new_render_type( $fieldtype, 'CMB2_Type_File_Base' ); break; case 'parse_picker_options': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Text_Date' ); break; case 'get_object_terms': case 'get_terms': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Taxonomy_Multicheck' ); break; case 'date_args': case 'time_args': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Text_Datetime_Timestamp' ); break; case 'parse_args': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Text' ); break; } return null !== $this->type; } /** * Check for methods to be proxied to the CMB2_Type_Base object. * * @since 2.2.4 * @param string $method The possible method to proxy. * @param array $arguments All arguments passed to the method. * @return bool|array False if not proxied, else array with 'value' key being the return of the method. */ public function maybe_proxy_method( $method, $arguments ) { $exists = false; $proxied = array( 'get_object_terms' => array(), 'is_valid_img_ext' => false, 'parse_args' => array(), 'concat_items' => '', 'select_option' => '', 'list_input' => '', 'list_input_checkbox' => '', 'img_status_output' => '', 'file_status_output' => '', 'parse_picker_options' => array(), ); if ( isset( $proxied[ $method ] ) ) { $exists = array( // Ok, proxy the method call to the CMB2_Type_Base object. 'value' => $this->proxy_method( $method, $proxied[ $method ], $arguments ), ); } return $exists; } /** * Checks for a custom field CMB2_Type_Base class to use for rendering. * * @since 2.2.4 * * @param string $fieldtype Non-existent field type name. * @param array $args Optional field arguments. * * @return bool|CMB2_Type_Base Type object if custom field is an object, false if field was added with * `cmb2_render_{$field_type}` action. * @throws Exception if custom field type class does not extend CMB2_Type_Base. */ public function maybe_custom_field_object( $fieldtype, $args = array() ) { if ( $render_class_name = $this->get_render_type_class( $fieldtype ) ) { $this->type = new $render_class_name( $this, $args ); if ( ! ( $this->type instanceof CMB2_Type_Base ) ) { throw new Exception( __( 'Custom CMB2 field type classes must extend CMB2_Type_Base.', 'cmb2' ) ); } return $this->type; } return false; } /** * Gets the render type CMB2_Type_Base object to use for rendering the field. * * @since 2.2.4 * @param string $fieldtype The type of field being rendered. * @param string $render_class_name The default field type class to use. Defaults to null. * @param array $args Optional arguments to pass to type class. * @param mixed $additional Optional additional argument to pass to type class. * @return CMB2_Type_Base Type object. */ public function get_new_render_type( $fieldtype, $render_class_name = null, $args = array(), $additional = '' ) { $render_class_name = $this->get_render_type_class( $fieldtype, $render_class_name ); $this->type = new $render_class_name( $this, $args, $additional ); return $this->type; } /** * Checks for the render type class to use for rendering the field. * * @since 2.2.4 * @param string $fieldtype The type of field being rendered. * @param string $render_class_name The default field type class to use. Defaults to null. * @return string The field type class to use. */ public function get_render_type_class( $fieldtype, $render_class_name = null ) { $render_class_name = $this->field->args( 'render_class' ) ? $this->field->args( 'render_class' ) : $render_class_name; if ( has_action( "cmb2_render_class_{$fieldtype}" ) ) { /** * Filters the custom field type class used for rendering the field. Class is required to extend CMB2_Type_Base. * * The dynamic portion of the hook name, $fieldtype, refers to the (custom) field type. * * @since 2.2.4 * * @param string $render_class_name The custom field type class to use. Default null. * @param object $field_type_object This `CMB2_Types` object. */ $render_class_name = apply_filters( "cmb2_render_class_{$fieldtype}", $render_class_name, $this ); } return $render_class_name && class_exists( $render_class_name ) ? $render_class_name : false; } /** * Retrieve text parameter from field's options array (if it has one), or use fallback text * * @since 2.0.0 * @param string $text_key Key in field's options array. * @param string $fallback Fallback text. * @return string */ public function _text( $text_key, $fallback = '' ) { return $this->field->get_string( $text_key, $fallback ); } /** * Determine a file's extension * * @since 1.0.0 * @param string $file File url * @return string|false File extension or false */ public function get_file_ext( $file ) { return CMB2_Utils::get_file_ext( $file ); } /** * Get the file name from a url * * @since 2.0.0 * @param string $value File url or path * @return string File name */ public function get_file_name_from_path( $value ) { return CMB2_Utils::get_file_name_from_path( $value ); } /** * Combines attributes into a string for a form element * * @since 1.1.0 * @param array $attrs Attributes to concatenate * @param array $attr_exclude Attributes that should NOT be concatenated * @return string String of attributes for form element */ public function concat_attrs( $attrs, $attr_exclude = array() ) { return CMB2_Utils::concat_attrs( $attrs, $attr_exclude ); } /** * Generates repeatable field table markup * * @since 1.0.0 */ public function render_repeatable_field() { $table_id = $this->field->id() . '_repeat'; $this->_desc( true, true, true ); ?>
repeatable_rows(); ?>

iterator = 0; } /** * Generates repeatable field rows * * @since 1.1.0 */ public function repeatable_rows() { $meta_value = array_filter( (array) $this->field->escaped_value() ); // check for default content $default = $this->field->get_default(); // check for saved data if ( ! empty( $meta_value ) ) { $meta_value = is_array( $meta_value ) ? array_filter( $meta_value ) : $meta_value; $meta_value = ! empty( $meta_value ) ? $meta_value : $default; } else { $meta_value = $default; } // Loop value array and add a row if ( ! empty( $meta_value ) ) { foreach ( (array) $meta_value as $val ) { $this->field->escaped_value = $val; $this->repeat_row(); $this->iterator++; } } else { // If value is empty (including empty array), then clear the value. $this->field->escaped_value = $this->field->value = null; // Otherwise add one row $this->repeat_row(); } // Then add an empty row $this->field->escaped_value = $default; $this->iterator = $this->iterator ? $this->iterator : 1; $this->repeat_row( 'empty-row hidden' ); } /** * Generates a repeatable row's markup * * @since 1.1.0 * @param string $classes Repeatable table row's class */ protected function repeat_row( $classes = 'cmb-repeat-row' ) { $classes = explode( ' ', $classes ); $classes = array_map( 'sanitize_html_class', $classes ); ?>
_render(); ?>
field->args( 'repeatable' ) || $this->iterator > 0 ) ) { return ''; } $desc = $this->field->args( 'description' ); if ( ! $desc ) { return; } $tag = $paragraph ? 'p' : 'span'; $desc = sprintf( "\n" . '<%1$s class="cmb2-metabox-description">%2$s' . "\n", $tag, $desc ); if ( $echo ) { echo $desc; } return $desc; } /** * Generate field name attribute * * @since 1.1.0 * @param string $suffix For multi-part fields * @return string Name attribute */ public function _name( $suffix = '' ) { return $this->field->args( '_name' ) . ( $this->field->args( 'repeatable' ) ? '[' . $this->iterator . ']' : '' ) . $suffix; } /** * Generate field id attribute * * @since 1.1.0 * @param string $suffix For multi-part fields * @param bool $append_repeatable_iterator Whether to append the iterator attribue if the field is repeatable. * @return string Id attribute */ public function _id( $suffix = '', $append_repeatable_iterator = true ) { $id = $this->field->id() . $suffix . ( $this->field->args( 'repeatable' ) ? '_' . $this->iterator : '' ); if ( $append_repeatable_iterator && $this->field->args( 'repeatable' ) ) { $id .= '" data-iterator="' . $this->iterator; } return $id; } /** * Handles outputting an 'input' element * * @since 1.1.0 * @param array $args Override arguments * @param string $type Field type * @return string Form input element */ public function input( $args = array(), $type = __FUNCTION__ ) { return $this->get_new_render_type( 'text', 'CMB2_Type_Text', $args, $type )->render(); } /** * Handles outputting an 'textarea' element * * @since 1.1.0 * @param array $args Override arguments * @return string Form textarea element */ public function textarea( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Textarea', $args )->render(); } /** * Begin Field Types */ public function text() { return $this->input(); } public function hidden() { $args = array( 'type' => 'hidden', 'desc' => '', 'class' => 'cmb2-hidden', ); if ( $this->field->group ) { $args['data-groupid'] = $this->field->group->id(); $args['data-iterator'] = $this->iterator; } return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text', $args, 'input' )->render(); } public function text_small() { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text', array( 'class' => 'cmb2-text-small', 'desc' => $this->_desc(), ), 'input' )->render(); } public function text_medium() { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text', array( 'class' => 'cmb2-text-medium', 'desc' => $this->_desc(), ), 'input' )->render(); } public function text_email() { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text', array( 'class' => 'cmb2-text-email cmb2-text-medium', 'type' => 'email', ), 'input' )->render(); } public function text_url() { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text', array( 'class' => 'cmb2-text-url cmb2-text-medium regular-text', 'value' => $this->field->escaped_value( 'esc_url' ), ), 'input' )->render(); } public function text_money() { $input = $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text', array( 'class' => 'cmb2-text-money', 'desc' => $this->_desc(), ), 'input' )->render(); return ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$ ' : ' ' ) . $input; } public function textarea_small() { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Textarea', array( 'class' => 'cmb2-textarea-small', 'rows' => 4, ) )->render(); } public function textarea_code( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Textarea_Code', $args )->render(); } public function wysiwyg( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Wysiwyg', $args )->render(); } public function text_date( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text_Date', $args )->render(); } // Alias for text_date public function text_date_timestamp( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text_Date', $args )->render(); } public function text_time( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text_Time', $args )->render(); } public function text_datetime_timestamp( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text_Datetime_Timestamp', $args )->render(); } public function text_datetime_timestamp_timezone( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Text_Datetime_Timestamp_Timezone', $args )->render(); } public function select_timezone( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Select_Timezone', $args )->render(); } public function colorpicker( $args = array(), $meta_value = '' ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Colorpicker', $args, $meta_value )->render(); } public function title( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Title', $args )->render(); } public function select( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Select', $args )->render(); } public function taxonomy_select( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Taxonomy_Select', $args )->render(); } public function taxonomy_select_hierarchical( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Taxonomy_Select_Hierarchical', $args )->render(); } public function radio( $args = array(), $type = __FUNCTION__ ) { return $this->get_new_render_type( $type, 'CMB2_Type_Radio', $args, $type )->render(); } public function radio_inline( $args = array() ) { return $this->radio( $args, __FUNCTION__ ); } public function multicheck( $type = 'checkbox' ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Multicheck', array(), $type )->render(); } public function multicheck_inline() { return $this->multicheck( 'multicheck_inline' ); } public function checkbox( $args = array(), $is_checked = null ) { // Avoid get_new_render_type since we need a different default for the 3rd argument than ''. $render_class_name = $this->get_render_type_class( __FUNCTION__, 'CMB2_Type_Checkbox' ); $this->type = new $render_class_name( $this, $args, $is_checked ); return $this->type->render(); } public function taxonomy_radio( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Taxonomy_Radio', $args )->render(); } public function taxonomy_radio_hierarchical( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Taxonomy_Radio_Hierarchical', $args )->render(); } public function taxonomy_radio_inline( $args = array() ) { return $this->taxonomy_radio( $args ); } public function taxonomy_multicheck( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Taxonomy_Multicheck', $args )->render(); } public function taxonomy_multicheck_hierarchical( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Taxonomy_Multicheck_Hierarchical', $args )->render(); } public function taxonomy_multicheck_inline( $args = array() ) { return $this->taxonomy_multicheck( $args ); } public function oembed( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_Oembed', $args )->render(); } public function file_list( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_File_List', $args )->render(); } public function file( $args = array() ) { return $this->get_new_render_type( __FUNCTION__, 'CMB2_Type_File', $args )->render(); } } CMB2_Utils.php000064400000053270151717007230007132 0ustar00 'attachment', 'post_status' => 'inherit', 'fields' => 'ids', 'meta_query' => array( array( 'value' => $file, 'compare' => 'LIKE', 'key' => '_wp_attachment_metadata', ), ), ); $query = new WP_Query( $query_args ); if ( $query->have_posts() ) { foreach ( $query->posts as $post_id ) { $meta = wp_get_attachment_metadata( $post_id ); $original_file = basename( $meta['file'] ); $cropped_image_files = isset( $meta['sizes'] ) ? wp_list_pluck( $meta['sizes'], 'file' ) : array(); if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) { $attachment_id = $post_id; break; } } } return 0 === $attachment_id ? false : $attachment_id; } /** * Utility method to get a combined list of default and custom registered image sizes * * @since 2.2.4 * @link http://core.trac.wordpress.org/ticket/18947 * @global array $_wp_additional_image_sizes * @return array The image sizes */ public static function get_available_image_sizes() { global $_wp_additional_image_sizes; $default_image_sizes = array( 'thumbnail', 'medium', 'large' ); foreach ( $default_image_sizes as $size ) { $image_sizes[ $size ] = array( 'height' => intval( get_option( "{$size}_size_h" ) ), 'width' => intval( get_option( "{$size}_size_w" ) ), 'crop' => get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false, ); } if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) { $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes ); } return $image_sizes; } /** * Utility method to return the closest named size from an array of values * * Based off of WordPress's image_get_intermediate_size() * If the size matches an existing size then it will be used. If there is no * direct match, then the nearest image size larger than the specified size * will be used. If nothing is found, then the function will return false. * Uses get_available_image_sizes() to get all available sizes. * * @since 2.2.4 * @param array|string $size Image size. Accepts an array of width and height (in that order). * @return false|string Named image size e.g. 'thumbnail' */ public static function get_named_size( $size ) { $data = array(); // Find the best match when '$size' is an array. if ( is_array( $size ) ) { $image_sizes = self::get_available_image_sizes(); $candidates = array(); foreach ( $image_sizes as $_size => $data ) { // If there's an exact match to an existing image size, short circuit. if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) { $candidates[ $data['width'] * $data['height'] ] = array( $_size, $data ); break; } // If it's not an exact match, consider larger sizes with the same aspect ratio. if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) { /** * To test for varying crops, we constrain the dimensions of the larger image * to the dimensions of the smaller image and see if they match. */ if ( $data['width'] > $size[0] ) { $constrained_size = wp_constrain_dimensions( $data['width'], $data['height'], $size[0] ); $expected_size = array( $size[0], $size[1] ); } else { $constrained_size = wp_constrain_dimensions( $size[0], $size[1], $data['width'] ); $expected_size = array( $data['width'], $data['height'] ); } // If the image dimensions are within 1px of the expected size, we consider it a match. $matched = ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ); if ( $matched ) { $candidates[ $data['width'] * $data['height'] ] = array( $_size, $data ); } } } if ( ! empty( $candidates ) ) { // Sort the array by size if we have more than one candidate. if ( 1 < count( $candidates ) ) { ksort( $candidates ); } $data = array_shift( $candidates ); $data = $data[0]; } elseif ( ! empty( $image_sizes['thumbnail'] ) && $image_sizes['thumbnail']['width'] >= $size[0] && $image_sizes['thumbnail']['width'] >= $size[1] ) { /* * When the size requested is smaller than the thumbnail dimensions, we * fall back to the thumbnail size. */ $data = 'thumbnail'; } else { return false; } } elseif ( ! empty( $image_sizes[ $size ] ) ) { $data = $size; }// End if. // If we still don't have a match at this point, return false. if ( empty( $data ) ) { return false; } return $data; } /** * Utility method that returns time string offset by timezone * * @since 1.0.0 * @param string $tzstring Time string. * @return string Offset time string */ public static function timezone_offset( $tzstring ) { $tz_offset = 0; if ( ! empty( $tzstring ) && is_string( $tzstring ) ) { if ( 'UTC' === substr( $tzstring, 0, 3 ) ) { $tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring ); return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS ); } try { $date_time_zone_selected = new DateTimeZone( $tzstring ); $tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() ); } catch ( Exception $e ) { self::log_if_debug( __METHOD__, __LINE__, $e->getMessage() ); } } return $tz_offset; } /** * Utility method that returns a timezone string representing the default timezone for the site. * * Roughly copied from WordPress, as get_option('timezone_string') will return * an empty string if no value has been set on the options page. * A timezone string is required by the wp_timezone_choice() used by the * select_timezone field. * * @since 1.0.0 * @return string Timezone string */ public static function timezone_string() { $current_offset = get_option( 'gmt_offset' ); $tzstring = get_option( 'timezone_string' ); // Remove old Etc mappings. Fallback to gmt_offset. if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) { $tzstring = ''; } if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists. if ( 0 == $current_offset ) { $tzstring = 'UTC+0'; } elseif ( $current_offset < 0 ) { $tzstring = 'UTC' . $current_offset; } else { $tzstring = 'UTC+' . $current_offset; } } return $tzstring; } /** * Returns a unix timestamp, first checking if value already is a timestamp. * * @since 2.0.0 * @param string|int $string Possible timestamp string. * @return int Time stamp. */ public static function make_valid_time_stamp( $string ) { if ( ! $string ) { return 0; } $valid = self::is_valid_time_stamp( $string ); if ( $valid ) { $timestamp = (int) $string; $length = strlen( (string) $timestamp ); $unixlength = strlen( (string) time() ); $diff = $length - $unixlength; // If value is larger than a unix timestamp, we need to round to the // nearest unix timestamp (in seconds). if ( $diff > 0 ) { $divider = (int) '1' . str_repeat( '0', $diff ); $timestamp = round( $timestamp / $divider ); } } else { $timestamp = @strtotime( (string) $string ); } return $timestamp; } /** * Determine if a value is a valid date. * * @since 2.9.1 * @param mixed $date Value to check. * @return boolean Whether value is a valid date */ public static function is_valid_date( $date ) { return ( is_string( $date ) && @strtotime( $date ) ) || self::is_valid_time_stamp( $date ); } /** * Determine if a value is a valid timestamp * * @since 2.0.0 * @param mixed $timestamp Value to check. * @return boolean Whether value is a valid timestamp */ public static function is_valid_time_stamp( $timestamp ) { return (string) (int) $timestamp === (string) $timestamp && $timestamp <= PHP_INT_MAX && $timestamp >= ~PHP_INT_MAX; } /** * Checks if a value is 'empty'. Still accepts 0. * * @since 2.0.0 * @param mixed $value Value to check. * @return bool True or false */ public static function isempty( $value ) { return null === $value || '' === $value || false === $value || array() === $value; } /** * Checks if a value is not 'empty'. 0 doesn't count as empty. * * @since 2.2.2 * @param mixed $value Value to check. * @return bool True or false */ public static function notempty( $value ) { return null !== $value && '' !== $value && false !== $value && array() !== $value; } /** * Filters out empty values (not including 0). * * @since 2.2.2 * @param mixed $value Value to check. * @return array True or false. */ public static function filter_empty( $value ) { return array_filter( $value, array( __CLASS__, 'notempty' ) ); } /** * Insert a single array item inside another array at a set position * * @since 2.0.2 * @param array $array Array to modify. Is passed by reference, and no return is needed. Passed by reference. * @param array $new New array to insert. * @param int $position Position in the main array to insert the new array. */ public static function array_insert( &$array, $new, $position ) { $before = array_slice( $array, 0, $position - 1 ); $after = array_diff_key( $array, $before ); $array = array_merge( $before, $new, $after ); } /** * Defines the url which is used to load local resources. * This may need to be filtered for local Window installations. * If resources do not load, please check the wiki for details. * * @since 1.0.1 * * @param string $path URL path. * @return string URL to CMB2 resources */ public static function url( $path = '' ) { if ( self::$url ) { return self::$url . $path; } $cmb2_url = self::get_url_from_dir( cmb2_dir() ); /** * Filter the CMB location url. * * @param string $cmb2_url Currently registered url. */ self::$url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) ); return self::$url . $path; } /** * Converts a system path to a URL * * @since 2.2.2 * @param string $dir Directory path to convert. * @return string Converted URL. */ public static function get_url_from_dir( $dir ) { $dir = self::normalize_path( $dir ); // Let's test if We are in the plugins or mu-plugins dir. $test_dir = trailingslashit( $dir ) . 'unneeded.php'; if ( 0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) ) || 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) ) ) { // Ok, then use plugins_url, as it is more reliable. return trailingslashit( plugins_url( '', $test_dir ) ); } // Ok, now let's test if we are in the theme dir. $theme_root = self::normalize_path( get_theme_root() ); if ( 0 === strpos( $dir, $theme_root ) ) { // Ok, then use get_theme_root_uri. return set_url_scheme( trailingslashit( str_replace( untrailingslashit( $theme_root ), untrailingslashit( get_theme_root_uri() ), $dir ) ) ); } // Check to see if it's anywhere in the root directory. $site_dir = self::get_normalized_abspath(); $site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() ); $url = str_replace( array( $site_dir, WP_PLUGIN_DIR ), array( $site_url, WP_PLUGIN_URL ), $dir ); return set_url_scheme( $url ); } /** * Get the normalized absolute path defined by WordPress. * * @since 2.2.6 * * @return string Normalized absolute path. */ protected static function get_normalized_abspath() { return self::normalize_path( self::$ABSPATH ); } /** * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. * * On windows systems, replaces backslashes with forward slashes * and forces upper-case drive letters. * Allows for two leading slashes for Windows network shares, but * ensures that all other duplicate slashes are reduced to a single. * * @since 2.2.0 * * @param string $path Path to normalize. * @return string Normalized path. */ protected static function normalize_path( $path ) { if ( function_exists( 'wp_normalize_path' ) ) { return wp_normalize_path( $path ); } // Replace newer WP's version of wp_normalize_path. $path = str_replace( '\\', '/', $path ); $path = preg_replace( '|(?<=.)/+|', '/', $path ); if ( ':' === substr( $path, 1, 1 ) ) { $path = ucfirst( $path ); } return $path; } /** * Get timestamp from text date * * @since 2.2.0 * @param string $value Date value. * @param string $date_format Expected date format. * @return mixed Unix timestamp representing the date. */ public static function get_timestamp_from_value( $value, $date_format ) { $date_object = date_create_from_format( $date_format, $value ); return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value ); } /** * Takes a php date() format string and returns a string formatted to suit for the date/time pickers * It will work only with the following subset of date() options: * * Formats: d, l, j, z, m, F, n, y, and Y. * * A slight effort is made to deal with escaped characters. * * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or * bring even more translation troubles. * * @since 2.2.0 * @param string $format PHP date format. * @return string reformatted string */ public static function php_to_js_dateformat( $format ) { // order is relevant here, since the replacement will be done sequentially. $supported_options = array( 'd' => 'dd', // Day, leading 0. 'j' => 'd', // Day, no 0. 'z' => 'o', // Day of the year, no leading zeroes. // 'D' => 'D', // Day name short, not sure how it'll work with translations. 'l ' => 'DD ', // Day name full, idem before. 'l, ' => 'DD, ', // Day name full, idem before. 'm' => 'mm', // Month of the year, leading 0. 'n' => 'm', // Month of the year, no leading 0. // 'M' => 'M', // Month, Short name. 'F ' => 'MM ', // Month, full name. 'F, ' => 'MM, ', // Month, full name. 'y' => 'y', // Year, two digit. 'Y' => 'yy', // Year, full. 'H' => 'HH', // Hour with leading 0 (24 hour). 'G' => 'H', // Hour with no leading 0 (24 hour). 'h' => 'hh', // Hour with leading 0 (12 hour). 'g' => 'h', // Hour with no leading 0 (12 hour). 'i' => 'mm', // Minute with leading 0. 's' => 'ss', // Second with leading 0. 'a' => 'tt', // am/pm. 'A' => 'TT', // AM/PM. ); foreach ( $supported_options as $php => $js ) { // replaces every instance of a supported option, but skips escaped characters. $format = preg_replace( "~(? 'DD', // Day name full, idem before. 'F' => 'MM', // Month, full name. ); if ( isset( $supported_options[ $format ] ) ) { $format = $supported_options[ $format ]; } $format = preg_replace_callback( '~(?:\\\.)+~', array( __CLASS__, 'wrap_escaped_chars' ), $format ); return $format; } /** * Get a DateTime object from a value. * * @since 2.11.0 * * @param string $value The value to convert to a DateTime object. * * @return DateTime|null */ public static function get_datetime_from_value( $value ) { return is_serialized( $value ) // Ok, we need to unserialize the value // -- allows back-compat for older field values with serialized DateTime objects. ? self::unserialize_datetime( $value ) // Handle new json formatted values. : self::json_to_datetime( $value ); } /** * Unserialize a datetime value string. * * This is a back-compat method for older field values with serialized DateTime objects. * * @since 2.11.0 * * @param string $date_value The serialized datetime value. * * @return DateTime|null */ public static function unserialize_datetime( $date_value ) { $datetime = @unserialize( trim( $date_value ), array( 'allowed_classes' => array( 'DateTime' ) ) ); return $datetime && $datetime instanceof DateTime ? $datetime : null; } /** * Convert a json datetime value string to a DateTime object. * * @since 2.11.0 * * @param string $json_string The json value string. * * @return DateTime|null */ public static function json_to_datetime( $json_string ) { if ( ! is_string( $json_string ) ) { return null; } $json = json_decode( $json_string ); // Check if json decode was successful if ( json_last_error() !== JSON_ERROR_NONE ) { return null; } // If so, convert to DateTime object. return self::unserialize_datetime( str_replace( 'stdClass', 'DateTime', serialize( $json ) ) ); } /** * Helper function for CMB_Utils::php_to_js_dateformat(). * * @since 2.2.0 * @param string $value Value to wrap/escape. * @return string Modified value */ public static function wrap_escaped_chars( $value ) { return ''' . str_replace( '\\', '', $value[0] ) . '''; } /** * Send to debug.log if WP_DEBUG is defined and true * * @since 2.2.0 * * @param string $function Function name. * @param int $line Line number. * @param mixed $msg Message to output. * @param mixed $debug Variable to print_r. */ public static function log_if_debug( $function, $line, $msg, $debug = null ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) ); } } /** * Determine a file's extension * * @since 1.0.0 * @param string $file File url. * @return string|false File extension or false */ public static function get_file_ext( $file ) { $parsed = parse_url( $file, PHP_URL_PATH ); return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false; } /** * Get the file name from a url * * @since 2.0.0 * @param string $value File url or path. * @return string File name */ public static function get_file_name_from_path( $value ) { $parts = explode( '/', $value ); return is_array( $parts ) ? end( $parts ) : $value; } /** * Check if WP version is at least $version. * * @since 2.2.2 * @param string $version WP version string to compare. * @return bool Result of comparison check. */ public static function wp_at_least( $version ) { return version_compare( get_bloginfo( 'version' ), $version, '>=' ); } /** * Combines attributes into a string for a form element. * * @since 1.1.0 * @param array $attrs Attributes to concatenate. * @param array $attr_exclude Attributes that should NOT be concatenated. * @return string String of attributes for form element. */ public static function concat_attrs( $attrs, $attr_exclude = array() ) { $attr_exclude[] = 'rendered'; $attr_exclude[] = 'js_dependencies'; $attributes = ''; foreach ( $attrs as $attr => $val ) { $excluded = in_array( $attr, (array) $attr_exclude, true ); $empty = false === $val && 'value' !== $attr; if ( ! $excluded && ! $empty ) { $val = is_array( $val ) ? implode( ',', $val ) : $val; // if data attribute, use single quote wraps, else double. $quotes = self::is_data_attribute( $attr ) ? "'" : '"'; $attributes .= sprintf( ' %1$s=%3$s%2$s%3$s', $attr, $val, $quotes ); } } return $attributes; } /** * Check if given attribute is a data attribute. * * @since 2.2.5 * * @param string $att HTML attribute. * @return boolean */ public static function is_data_attribute( $att ) { return 0 === stripos( $att, 'data-' ); } /** * Ensures value is an array. * * @since 2.2.3 * * @param mixed $value Value to ensure is array. * @param array $default Default array. Defaults to empty array. * * @return array The array. */ public static function ensure_array( $value, $default = array() ) { if ( empty( $value ) ) { return $default; } if ( is_array( $value ) || is_object( $value ) ) { return (array) $value; } // Not sure anything would be non-scalar that is not an array or object? if ( ! is_scalar( $value ) ) { return $default; } return (array) $value; } /** * If number is numeric, normalize it with floatval or intval, depending on if decimal is found. * * @since 2.2.6 * * @param mixed $value Value to normalize (if numeric). * @return mixed Possibly normalized value. */ public static function normalize_if_numeric( $value ) { if ( is_numeric( $value ) ) { $value = false !== strpos( $value, '.' ) ? floatval( $value ) : intval( $value ); } return $value; } /** * Generates a 12 character unique hash from a string. * * @since 2.4.0 * * @param string $string String to create a hash from. * * @return string */ public static function generate_hash( $string ) { return substr( base_convert( md5( $string ), 16, 32 ), 0, 12 ); } } helper-functions.php000064400000031370151717007230010551 0ustar00get_oembed_no_edit( $args ); // Send back our embed. if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) { return '
' . $oembed['embed'] . '
'; } $error = sprintf( /* translators: 1: results for. 2: link to codex.wordpress.org/Embeds */ esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ), $oembed['fallback'], 'codex.wordpress.org/Embeds' ); if ( isset( $args['wp_error'] ) && $args['wp_error'] ) { return new WP_Error( 'cmb2_get_oembed_result', $error, compact( 'oembed', 'args' ) ); } // Otherwise, send back error info that no oEmbeds were found. return '

' . $error . '

'; } /** * Outputs the return of cmb2_get_oembed. * * @since 2.2.2 * @see cmb2_get_oembed * * @param array $args oEmbed args. */ function cmb2_do_oembed( $args = array() ) { echo cmb2_get_oembed( $args ); } add_action( 'cmb2_do_oembed', 'cmb2_do_oembed' ); /** * A helper function to get an option from a CMB2 options array * * @since 1.0.1 * @param string $option_key Option key. * @param string $field_id Option array field key. * @param mixed $default Optional default fallback value. * @return array Options array or specific field */ function cmb2_get_option( $option_key, $field_id = '', $default = false ) { return cmb2_options( $option_key )->get( $field_id, $default ); } /** * A helper function to update an option in a CMB2 options array * * @since 2.0.0 * @param string $option_key Option key. * @param string $field_id Option array field key. * @param mixed $value Value to update data with. * @param boolean $single Whether data should not be an array. * @return boolean Success/Failure */ function cmb2_update_option( $option_key, $field_id, $value, $single = true ) { if ( cmb2_options( $option_key )->update( $field_id, $value, false, $single ) ) { return cmb2_options( $option_key )->set(); } return false; } /** * Get a CMB2 field object. * * @since 1.1.0 * @param array $meta_box Metabox ID or Metabox config array. * @param array $field_id Field ID or all field arguments. * @param int|string $object_id Object ID (string for options-page). * @param string $object_type Type of object being saved. (e.g., post, user, term, comment, or options-page). * Defaults to metabox object type. * @return CMB2_Field|null CMB2_Field object unless metabox config cannot be found */ function cmb2_get_field( $meta_box, $field_id, $object_id = 0, $object_type = '' ) { $object_id = $object_id ? $object_id : get_the_ID(); $cmb = $meta_box instanceof CMB2 ? $meta_box : cmb2_get_metabox( $meta_box, $object_id ); if ( ! $cmb ) { return; } $cmb->object_type( $object_type ? $object_type : $cmb->mb_object_type() ); return $cmb->get_field( $field_id ); } /** * Get a field's value. * * @since 1.1.0 * @param array $meta_box Metabox ID or Metabox config array. * @param array $field_id Field ID or all field arguments. * @param int|string $object_id Object ID (string for options-page). * @param string $object_type Type of object being saved. (e.g., post, user, term, comment, or options-page). * Defaults to metabox object type. * @return mixed Maybe escaped value */ function cmb2_get_field_value( $meta_box, $field_id, $object_id = 0, $object_type = '' ) { $field = cmb2_get_field( $meta_box, $field_id, $object_id, $object_type ); return $field->escaped_value(); } /** * Because OOP can be scary * * @since 2.0.2 * @param array $meta_box_config Metabox Config array. * @return CMB2 object Instantiated CMB2 object */ function new_cmb2_box( array $meta_box_config ) { return cmb2_get_metabox( $meta_box_config ); } /** * Retrieve a CMB2 instance by the metabox ID * * @since 2.0.0 * @param mixed $meta_box Metabox ID or Metabox config array. * @param int|string $object_id Object ID (string for options-page). * @param string $object_type Type of object being saved. * (e.g., post, user, term, comment, or options-page). * Defaults to metabox object type. * @return CMB2 object */ function cmb2_get_metabox( $meta_box, $object_id = 0, $object_type = '' ) { if ( $meta_box instanceof CMB2 ) { return $meta_box; } if ( is_string( $meta_box ) ) { $cmb = CMB2_Boxes::get( $meta_box ); } else { // See if we already have an instance of this metabox. $cmb = CMB2_Boxes::get( $meta_box['id'] ); // If not, we'll initate a new metabox. $cmb = $cmb ? $cmb : new CMB2( $meta_box, $object_id ); } if ( $cmb && $object_id ) { $cmb->object_id( $object_id ); } if ( $cmb && $object_type ) { $cmb->object_type( $object_type ); } return $cmb; } /** * Returns array of sanitized field values from a metabox (without saving them) * * @since 2.0.3 * @param mixed $meta_box Metabox ID or Metabox config array. * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). * @return mixed Array of sanitized values or false if no CMB2 object found */ function cmb2_get_metabox_sanitized_values( $meta_box, array $data_to_sanitize ) { $cmb = cmb2_get_metabox( $meta_box ); return $cmb ? $cmb->get_sanitized_values( $data_to_sanitize ) : false; } /** * Retrieve a metabox form * * @since 2.0.0 * @param mixed $meta_box Metabox config array or Metabox ID. * @param int|string $object_id Object ID (string for options-page). * @param array $args Optional arguments array. * @return string CMB2 html form markup */ function cmb2_get_metabox_form( $meta_box, $object_id = 0, $args = array() ) { $object_id = $object_id ? $object_id : get_the_ID(); $cmb = cmb2_get_metabox( $meta_box, $object_id ); ob_start(); // Get cmb form. cmb2_print_metabox_form( $cmb, $object_id, $args ); $form = ob_get_clean(); return apply_filters( 'cmb2_get_metabox_form', $form, $object_id, $cmb ); } /** * Display a metabox form & save it on submission * * @since 1.0.0 * @param mixed $meta_box Metabox config array or Metabox ID. * @param int|string $object_id Object ID (string for options-page). * @param array $args Optional arguments array. */ function cmb2_print_metabox_form( $meta_box, $object_id = 0, $args = array() ) { $object_id = $object_id ? $object_id : get_the_ID(); $cmb = cmb2_get_metabox( $meta_box, $object_id ); // if passing a metabox ID, and that ID was not found. if ( ! $cmb ) { return; } $args = wp_parse_args( $args, array( 'form_format' => '
%3$s
', 'save_button' => esc_html__( 'Save', 'cmb2' ), 'object_type' => $cmb->mb_object_type(), 'cmb_styles' => $cmb->prop( 'cmb_styles' ), 'enqueue_js' => $cmb->prop( 'enqueue_js' ), ) ); // Set object type explicitly (rather than trying to guess from context). $cmb->object_type( $args['object_type'] ); // Save the metabox if it's been submitted // check permissions // @todo more hardening? if ( $cmb->prop( 'save_fields' ) // check nonce. && isset( $_POST['submit-cmb'], $_POST['object_id'], $_POST[ $cmb->nonce() ] ) && wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() ) && $object_id && $_POST['object_id'] == $object_id ) { $cmb->save_fields( $object_id, $cmb->object_type(), $_POST ); } // Enqueue JS/CSS. if ( $args['cmb_styles'] ) { CMB2_Hookup::enqueue_cmb_css(); } if ( $args['enqueue_js'] ) { CMB2_Hookup::enqueue_cmb_js(); } $form_format = apply_filters( 'cmb2_get_metabox_form_format', $args['form_format'], $object_id, $cmb ); $format_parts = explode( '%3$s', $form_format ); // Show cmb form. printf( $format_parts[0], esc_attr( $cmb->cmb_id ), esc_attr( $object_id ) ); $cmb->show_form(); if ( isset( $format_parts[1] ) && $format_parts[1] ) { printf( str_ireplace( '%4$s', '%1$s', $format_parts[1] ), esc_attr( $args['save_button'] ) ); } } /** * Display a metabox form (or optionally return it) & save it on submission. * * @since 1.0.0 * @param mixed $meta_box Metabox config array or Metabox ID. * @param int|string $object_id Object ID (string for options-page). * @param array $args Optional arguments array. * @return string */ function cmb2_metabox_form( $meta_box, $object_id = 0, $args = array() ) { if ( ! isset( $args['echo'] ) || $args['echo'] ) { cmb2_print_metabox_form( $meta_box, $object_id, $args ); } else { return cmb2_get_metabox_form( $meta_box, $object_id, $args ); } } if ( ! function_exists( 'date_create_from_format' ) ) { /** * Reimplementation of DateTime::createFromFormat for PHP < 5.3. :( * Borrowed from http://stackoverflow.com/questions/5399075/php-datetimecreatefromformat-in-5-2 * * @param string $date_format Date format. * @param string $date_value Date value. * * @return DateTime */ function date_create_from_format( $date_format, $date_value ) { $schedule_format = str_replace( array( 'M', 'Y', 'm', 'd', 'H', 'i', 'a' ), array( '%b', '%Y', '%m', '%d', '%H', '%M', '%p' ), $date_format ); /* * %Y, %m and %d correspond to date()'s Y m and d. * %I corresponds to H, %M to i and %p to a */ // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.strptimeDeprecated $parsed_time = strptime( $date_value, $schedule_format ); $ymd = sprintf( /** * This is a format string that takes six total decimal * arguments, then left-pads them with zeros to either * 4 or 2 characters, as needed */ '%04d-%02d-%02d %02d:%02d:%02d', $parsed_time['tm_year'] + 1900, // This will be "111", so we need to add 1900. $parsed_time['tm_mon'] + 1, // This will be the month minus one, so we add one. $parsed_time['tm_mday'], $parsed_time['tm_hour'], $parsed_time['tm_min'], $parsed_time['tm_sec'] ); return new DateTime( $ymd ); } }// End if. if ( ! function_exists( 'date_timestamp_get' ) ) { /** * Returns the Unix timestamp representing the date. * Reimplementation of DateTime::getTimestamp for PHP < 5.3. :( * * @param DateTime $date DateTime instance. * * @return int */ function date_timestamp_get( DateTime $date ) { return $date->format( 'U' ); } }// End if. index.php000064400000000033151717007230006363 0ustar00 array(), 'user' => array(), 'comment' => array(), 'term' => array(), ); /** * Array of readable field objects. * * @var CMB2_Field[] * @since 2.2.3 */ protected $read_fields = array(); /** * Array of editable field objects. * * @var CMB2_Field[] * @since 2.2.3 */ protected $edit_fields = array(); /** * Whether CMB2 object is readable via the rest api. * * @var boolean */ protected $rest_read = false; /** * Whether CMB2 object is editable via the rest api. * * @var boolean */ protected $rest_edit = false; /** * A functionalized constructor, used for the hookup action callbacks. * * @since 2.2.6 * * @param CMB2 $cmb The CMB2 object to hookup * * @return CMB2_Hookup_Base $hookup The hookup object. */ public static function maybe_init_and_hookup( CMB2 $cmb ) { if ( $cmb->prop( 'show_in_rest' ) && function_exists( 'rest_get_server' ) ) { $hookup = new self( $cmb ); return $hookup->universal_hooks(); } return false; } /** * Constructor * * @since 2.2.3 * * @param CMB2 $cmb The CMB2 object to be registered for the API. */ public function __construct( CMB2 $cmb ) { $this->cmb = $cmb; self::$boxes[ $cmb->cmb_id ] = $this; $show_value = $this->cmb->prop( 'show_in_rest' ); $this->rest_read = self::is_readable( $show_value ); $this->rest_edit = self::is_editable( $show_value ); } /** * Hooks to register on frontend and backend. * * @since 2.2.3 * * @return void */ public function universal_hooks() { // hook up the CMB rest endpoint classes $this->once( 'rest_api_init', array( __CLASS__, 'init_routes' ), 0 ); if ( function_exists( 'register_rest_field' ) ) { $this->once( 'rest_api_init', array( __CLASS__, 'register_cmb2_fields' ), 50 ); } $this->declare_read_edit_fields(); add_filter( 'is_protected_meta', array( $this, 'is_protected_meta' ), 10, 3 ); return $this; } /** * Initiate the CMB2 Boxes and Fields routes * * @since 2.2.3 * * @return void */ public static function init_routes() { $wp_rest_server = rest_get_server(); $boxes_controller = new CMB2_REST_Controller_Boxes( $wp_rest_server ); $boxes_controller->register_routes(); $fields_controller = new CMB2_REST_Controller_Fields( $wp_rest_server ); $fields_controller->register_routes(); } /** * Loop through REST boxes and call register_rest_field for each object type. * * @since 2.2.3 * * @return void */ public static function register_cmb2_fields() { $alltypes = $taxonomies = array(); foreach ( self::$boxes as $cmb_id => $rest_box ) { // Hook box specific filter callbacks. $callback = $rest_box->cmb->prop( 'register_rest_field_cb' ); if ( is_callable( $callback ) ) { call_user_func( $callback, $rest_box ); continue; } $types = array_flip( $rest_box->cmb->box_types( array( 'post' ) ) ); if ( isset( $types['user'] ) ) { unset( $types['user'] ); self::$type_boxes['user'][ $cmb_id ] = $cmb_id; } if ( isset( $types['comment'] ) ) { unset( $types['comment'] ); self::$type_boxes['comment'][ $cmb_id ] = $cmb_id; } if ( isset( $types['term'] ) ) { unset( $types['term'] ); $taxonomies = array_merge( $taxonomies, CMB2_Utils::ensure_array( $rest_box->cmb->prop( 'taxonomies' ) ) ); self::$type_boxes['term'][ $cmb_id ] = $cmb_id; } if ( ! empty( $types ) ) { $alltypes = array_merge( $alltypes, array_flip( $types ) ); self::$type_boxes['post'][ $cmb_id ] = $cmb_id; } } $alltypes = array_unique( $alltypes ); if ( ! empty( $alltypes ) ) { self::register_rest_field( $alltypes, 'post' ); } if ( ! empty( self::$type_boxes['user'] ) ) { self::register_rest_field( 'user', 'user' ); } if ( ! empty( self::$type_boxes['comment'] ) ) { self::register_rest_field( 'comment', 'comment' ); } if ( ! empty( self::$type_boxes['term'] ) ) { self::register_rest_field( $taxonomies, 'term' ); } } /** * Wrapper for register_rest_field. * * @since 2.2.3 * * @param string|array $object_types Object(s) the field is being registered * to, "post"|"term"|"comment" etc. * @param string $object_types Canonical object type for callbacks. * * @return void */ protected static function register_rest_field( $object_types, $object_type ) { register_rest_field( $object_types, 'cmb2', array( 'get_callback' => array( __CLASS__, "get_{$object_type}_rest_values" ), 'update_callback' => array( __CLASS__, "update_{$object_type}_rest_values" ), 'schema' => null, // @todo add schema ) ); } /** * Setup readable and editable fields. * * @since 2.2.3 * * @return void */ protected function declare_read_edit_fields() { foreach ( $this->cmb->prop( 'fields' ) as $field ) { $show_in_rest = isset( $field['show_in_rest'] ) ? $field['show_in_rest'] : null; if ( false === $show_in_rest ) { continue; } if ( $this->can_read( $show_in_rest ) ) { $this->read_fields[] = $field['id']; } if ( $this->can_edit( $show_in_rest ) ) { $this->edit_fields[] = $field['id']; } } } /** * Determines if a field is readable based on it's show_in_rest value * and the box's show_in_rest value. * * @since 2.2.3 * * @param bool $show_in_rest Field's show_in_rest value. Default null. * * @return bool Whether field is readable. */ protected function can_read( $show_in_rest ) { // if 'null', then use default box value. if ( null === $show_in_rest ) { return $this->rest_read; } // Else check if the value represents readable. return self::is_readable( $show_in_rest ); } /** * Determines if a field is editable based on it's show_in_rest value * and the box's show_in_rest value. * * @since 2.2.3 * * @param bool $show_in_rest Field's show_in_rest value. Default null. * * @return bool Whether field is editable. */ protected function can_edit( $show_in_rest ) { // if 'null', then use default box value. if ( null === $show_in_rest ) { return $this->rest_edit; } // Else check if the value represents editable. return self::is_editable( $show_in_rest ); } /** * Handler for getting post custom field data. * * @since 2.2.3 * * @param array $object The object data from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return mixed */ public static function get_post_rest_values( $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::get_rest_values( $object, $request, $object_type, 'post' ); } } /** * Handler for getting user custom field data. * * @since 2.2.3 * * @param array $object The object data from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return mixed */ public static function get_user_rest_values( $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::get_rest_values( $object, $request, $object_type, 'user' ); } } /** * Handler for getting comment custom field data. * * @since 2.2.3 * * @param array $object The object data from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return mixed */ public static function get_comment_rest_values( $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::get_rest_values( $object, $request, $object_type, 'comment' ); } } /** * Handler for getting term custom field data. * * @since 2.2.3 * * @param array $object The object data from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return mixed */ public static function get_term_rest_values( $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::get_rest_values( $object, $request, $object_type, 'term' ); } } /** * Handler for getting custom field data. * * @since 2.2.3 * * @param array $object The object data from the response * @param WP_REST_Request $request Current request * @param string $object_type The request object type * @param string $main_object_type The cmb main object type * * @return mixed */ protected static function get_rest_values( $object, $request, $object_type, $main_object_type = 'post' ) { if ( ! isset( $object['id'] ) ) { return; } $values = array(); if ( ! empty( self::$type_boxes[ $main_object_type ] ) ) { foreach ( self::$type_boxes[ $main_object_type ] as $cmb_id ) { $rest_box = self::$boxes[ $cmb_id ]; if ( ! $rest_box->cmb->is_box_type( $object_type ) ) { continue; } $result = self::get_box_rest_values( $rest_box, $object['id'], $main_object_type ); if ( ! empty( $result ) ) { if ( empty( $values[ $cmb_id ] ) ) { $values[ $cmb_id ] = $result; } else { $values[ $cmb_id ] = array_merge( $values[ $cmb_id ], $result ); } } } } return $values; } /** * Get box rest values. * * @since 2.7.0 * * @param CMB2_REST $rest_box The CMB2_REST object. * @param integer $object_id The object ID. * @param string $main_object_type The object type (post, user, term, etc) * * @return array Array of box rest values. */ public static function get_box_rest_values( $rest_box, $object_id = 0, $main_object_type = 'post' ) { $rest_box->cmb->object_id( $object_id ); $rest_box->cmb->object_type( $main_object_type ); $values = array(); foreach ( $rest_box->read_fields as $field_id ) { $field = $rest_box->cmb->get_field( $field_id ); $field->object_id( $object_id ); $field->object_type( $main_object_type ); $values[ $field->id( true ) ] = $field->get_rest_value(); if ( $field->args( 'has_supporting_data' ) ) { $field = $field->get_supporting_field(); $values[ $field->id( true ) ] = $field->get_rest_value(); } } return $values; } /** * Handler for updating post custom field data. * * @since 2.2.3 * * @param mixed $values The value of the field * @param object $object The object from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return bool|int */ public static function update_post_rest_values( $values, $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::update_rest_values( $values, $object, $request, $object_type, 'post' ); } } /** * Handler for updating user custom field data. * * @since 2.2.3 * * @param mixed $values The value of the field * @param object $object The object from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return bool|int */ public static function update_user_rest_values( $values, $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::update_rest_values( $values, $object, $request, $object_type, 'user' ); } } /** * Handler for updating comment custom field data. * * @since 2.2.3 * * @param mixed $values The value of the field * @param object $object The object from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return bool|int */ public static function update_comment_rest_values( $values, $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::update_rest_values( $values, $object, $request, $object_type, 'comment' ); } } /** * Handler for updating term custom field data. * * @since 2.2.3 * * @param mixed $values The value of the field * @param object $object The object from the response * @param string $field_name Name of field * @param WP_REST_Request $request Current request * @param string $object_type The request object type * * @return bool|int */ public static function update_term_rest_values( $values, $object, $field_name, $request, $object_type ) { if ( 'cmb2' === $field_name ) { return self::update_rest_values( $values, $object, $request, $object_type, 'term' ); } } /** * Handler for updating custom field data. * * @since 2.2.3 * * @param mixed $values The value of the field * @param object $object The object from the response * @param WP_REST_Request $request Current request * @param string $object_type The request object type * @param string $main_object_type The cmb main object type * * @return bool|int */ protected static function update_rest_values( $values, $object, $request, $object_type, $main_object_type = 'post' ) { if ( empty( $values ) || ! is_array( $values ) ) { return; } $object_id = self::get_object_id( $object, $main_object_type ); if ( ! $object_id ) { return; } $updated = array(); if ( ! empty( self::$type_boxes[ $main_object_type ] ) ) { foreach ( self::$type_boxes[ $main_object_type ] as $cmb_id ) { $result = self::santize_box_rest_values( $values, self::$boxes[ $cmb_id ], $object_id, $main_object_type ); if ( ! empty( $result ) ) { $updated[ $cmb_id ] = $result; } } } return $updated; } /** * Updates box rest values. * * @since 2.7.0 * * @param array $values Array of values. * @param CMB2_REST $rest_box The CMB2_REST object. * @param integer $object_id The object ID. * @param string $main_object_type The object type (post, user, term, etc) * * @return mixed|bool Array of updated statuses if successful. */ public static function santize_box_rest_values( $values, $rest_box, $object_id = 0, $main_object_type = 'post' ) { if ( ! array_key_exists( $rest_box->cmb->cmb_id, $values ) ) { return false; } $rest_box->cmb->object_id( $object_id ); $rest_box->cmb->object_type( $main_object_type ); return $rest_box->sanitize_box_values( $values ); } /** * Loop through box fields and sanitize the values. * * @since 2.2.o * * @param array $values Array of values being provided. * @return array Array of updated/sanitized values. */ public function sanitize_box_values( array $values ) { $updated = array(); $this->cmb->pre_process(); foreach ( $this->edit_fields as $field_id ) { $updated[ $field_id ] = $this->sanitize_field_value( $values, $field_id ); } $this->cmb->after_save(); return $updated; } /** * Handles returning a sanitized field value. * * @since 2.2.3 * * @param array $values Array of values being provided. * @param string $field_id The id of the field to update. * * @return mixed The results of saving/sanitizing a field value. */ protected function sanitize_field_value( array $values, $field_id ) { if ( ! array_key_exists( $field_id, $values[ $this->cmb->cmb_id ] ) ) { return; } $field = $this->cmb->get_field( $field_id ); if ( 'title' == $field->type() ) { return; } $field->object_id( $this->cmb->object_id() ); $field->object_type( $this->cmb->object_type() ); if ( 'group' == $field->type() ) { return $this->sanitize_group_value( $values, $field ); } return $field->save_field( $values[ $this->cmb->cmb_id ][ $field_id ] ); } /** * Handles returning a sanitized group field value. * * @since 2.2.3 * * @param array $values Array of values being provided. * @param CMB2_Field $field CMB2_Field object. * * @return mixed The results of saving/sanitizing the group field value. */ protected function sanitize_group_value( array $values, CMB2_Field $field ) { $fields = $field->fields(); if ( empty( $fields ) ) { return; } $this->cmb->data_to_save[ $field->_id( '', false ) ] = $values[ $this->cmb->cmb_id ][ $field->_id( '', false ) ]; return $this->cmb->save_group_field( $field ); } /** * Filter whether a meta key is protected. * * @since 2.2.3 * * @param bool $protected Whether the key is protected. Default false. * @param string $meta_key Meta key. * @param string $meta_type Meta type. */ public function is_protected_meta( $protected, $meta_key, $meta_type ) { if ( $this->field_can_edit( $meta_key ) ) { return false; } return $protected; } /** * Get the object ID for the given object/type. * * @since 2.2.3 * * @param mixed $object The object to get the ID for. * @param string $object_type The object type we are looking for. * * @return int The object ID if found. */ public static function get_object_id( $object, $object_type = 'post' ) { switch ( $object_type ) { case 'user': case 'post': if ( isset( $object->ID ) ) { return intval( $object->ID ); } case 'comment': if ( isset( $object->comment_ID ) ) { return intval( $object->comment_ID ); } case 'term': if ( is_array( $object ) && isset( $object['term_id'] ) ) { return intval( $object['term_id'] ); } elseif ( isset( $object->term_id ) ) { return intval( $object->term_id ); } } return 0; } /** * Checks if a given field can be read. * * @since 2.2.3 * * @param string|CMB2_Field $field_id Field ID or CMB2_Field object. * @param boolean $return_object Whether to return the Field object. * * @return mixed False if field can't be read or true|CMB2_Field object. */ public function field_can_read( $field_id, $return_object = false ) { return $this->field_can( 'read_fields', $field_id, $return_object ); } /** * Checks if a given field can be edited. * * @since 2.2.3 * * @param string|CMB2_Field $field_id Field ID or CMB2_Field object. * @param boolean $return_object Whether to return the Field object. * * @return mixed False if field can't be edited or true|CMB2_Field object. */ public function field_can_edit( $field_id, $return_object = false ) { return $this->field_can( 'edit_fields', $field_id, $return_object ); } /** * Checks if a given field can be read or edited. * * @since 2.2.3 * * @param string $type Whether we are checking for read or edit fields. * @param string|CMB2_Field $field_id Field ID or CMB2_Field object. * @param boolean $return_object Whether to return the Field object. * * @return mixed False if field can't be read or edited or true|CMB2_Field object. */ protected function field_can( $type, $field_id, $return_object = false ) { if ( ! in_array( $field_id instanceof CMB2_Field ? $field_id->id() : $field_id, $this->{$type}, true ) ) { return false; } return $return_object ? $this->cmb->get_field( $field_id ) : true; } /** * Get a CMB2_REST instance object from the registry by a CMB2 id. * * @since 2.2.3 * * @param string $cmb_id CMB2 config id * * @return CMB2_REST|false The CMB2_REST object or false. */ public static function get_rest_box( $cmb_id ) { return isset( self::$boxes[ $cmb_id ] ) ? self::$boxes[ $cmb_id ] : false; } /** * Remove a CMB2_REST instance object from the registry. * * @since 2.2.3 * * @param string $cmb_id A CMB2 instance id. */ public static function remove( $cmb_id ) { if ( array_key_exists( $cmb_id, self::$boxes ) ) { unset( self::$boxes[ $cmb_id ] ); } } /** * Retrieve all CMB2_REST instances from the registry. * * @since 2.2.3 * @return CMB2[] Array of all registered CMB2_REST instances. */ public static function get_all() { return self::$boxes; } /** * Checks if given value is readable. * * Value is considered readable if it is not empty and if it does not match the editable blacklist. * * @since 2.2.3 * * @param mixed $value Value to check. * * @return boolean Whether value is considered readable. */ public static function is_readable( $value ) { return ! empty( $value ) && ! in_array( $value, array( WP_REST_Server::CREATABLE, WP_REST_Server::EDITABLE, WP_REST_Server::DELETABLE, ), true ); } /** * Checks if given value is editable. * * Value is considered editable if matches the editable whitelist. * * @since 2.2.3 * * @param mixed $value Value to check. * * @return boolean Whether value is considered editable. */ public static function is_editable( $value ) { return in_array( $value, array( WP_REST_Server::EDITABLE, WP_REST_Server::ALLMETHODS, ), true ); } /** * Magic getter for our object. * * @param string $field * @throws Exception Throws an exception if the field is invalid. * * @return mixed */ public function __get( $field ) { switch ( $field ) { case 'read_fields': case 'edit_fields': case 'rest_read': case 'rest_edit': return $this->{$field}; default: throw new Exception( 'Invalid ' . __CLASS__ . ' property: ' . $field ); } } } rest-api/CMB2_REST_Controller.php000064400000025635151717007230012542 0ustar00server = $wp_rest_server; } /** * A wrapper for `apply_filters` which checks for box/field properties to hook to the filter. * * Checks if a CMB object callback property exists, and if it does, * hook it to the permissions filter. * * @since 2.2.3 * * @param string $filter The name of the filter to apply. * @param bool $default_access The default access for this request. * * @return void */ public function maybe_hook_callback_and_apply_filters( $filter, $default_access ) { if ( ! $this->rest_box && $this->request->get_param( 'cmb_id' ) ) { $this->rest_box = CMB2_REST::get_rest_box( $this->request->get_param( 'cmb_id' ) ); } $default_access = $this->maybe_hook_registered_callback( $filter, $default_access ); /** * Apply the permissions check filter. * * @since 2.2.3 * * @param bool $default_access Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ $default_access = apply_filters( $filter, $default_access, $this ); $this->maybe_unhook_registered_callback( $filter ); return $default_access; } /** * Checks if the CMB2 box has any registered callback parameters for the given filter. * * The registered handlers will have a property name which matches the filter, except: * - The 'cmb2_api' prefix will be removed * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). * * @since 2.2.3 * * @param string $filter The filter name. * @param bool $default_val The default filter value. * * @return bool The possibly-modified filter value (if the '*_cb' param is non-callable). */ public function maybe_hook_registered_callback( $filter, $default_val ) { if ( ! $this->rest_box || is_wp_error( $this->rest_box ) ) { return $default_val; } // Hook box specific filter callbacks. $val = $this->rest_box->cmb->maybe_hook_parameter( $filter, $default_val ); if ( null !== $val ) { $default_val = $val; } return $default_val; } /** * Unhooks any CMB2 box registered callback parameters for the given filter. * * @since 2.2.3 * * @param string $filter The filter name. * * @return void */ public function maybe_unhook_registered_callback( $filter ) { if ( ! $this->rest_box || is_wp_error( $this->rest_box ) ) { return; } // Unhook box specific filter callbacks. $this->rest_box->cmb->maybe_hook_parameter( $filter, null, 'remove_filter' ); } /** * Prepare a CMB2 object for serialization * * @since 2.2.3 * * @param mixed $data * @return array $data */ public function prepare_item( $data ) { return $this->prepare_item_for_response( $data, $this->request ); } /** * Output buffers a callback and returns the results. * * @since 2.2.3 * * @param mixed $cb Callable function/method. * @return mixed Results of output buffer after calling function/method. */ public function get_cb_results( $cb ) { $args = func_get_args(); array_shift( $args ); // ignore $cb ob_start(); call_user_func_array( $cb, $args ); return ob_get_clean(); } /** * Prepare the CMB2 item for the REST response. * * @since 2.2.3 * * @param mixed $item WordPress representation of the item. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $response */ public function prepare_item_for_response( $data, $request = null ) { $data = $this->filter_response_by_context( $data, $this->request['context'] ); /** * Filter the prepared CMB2 item response. * * @since 2.2.3 * * @param mixed $data Prepared data * @param object $request The WP_REST_Request object * @param object $cmb2_endpoints This endpoints object */ return apply_filters( 'cmb2_rest_prepare', rest_ensure_response( $data ), $this->request, $this ); } /** * Initiates the request property and the rest_box property if box is readable. * * @since 2.2.3 * * @param WP_REST_Request $request Request object. * @param string $request_type A description of the type of request being made. * * @return void */ protected function initiate_rest_read_box( $request, $request_type ) { $this->initiate_rest_box( $request, $request_type ); if ( ! is_wp_error( $this->rest_box ) && ! $this->rest_box->rest_read ) { $this->rest_box = new WP_Error( 'cmb2_rest_no_read_error', __( 'This box does not have read permissions.', 'cmb2' ), array( 'status' => 403, ) ); } } /** * Initiates the request property and the rest_box property if box is writeable. * * @since 2.2.3 * * @param WP_REST_Request $request Request object. * @param string $request_type A description of the type of request being made. * * @return void */ protected function initiate_rest_edit_box( $request, $request_type ) { $this->initiate_rest_box( $request, $request_type ); if ( ! is_wp_error( $this->rest_box ) && ! $this->rest_box->rest_edit ) { $this->rest_box = new WP_Error( 'cmb2_rest_no_write_error', __( 'This box does not have write permissions.', 'cmb2' ), array( 'status' => 403, ) ); } } /** * Initiates the request property and the rest_box property. * * @since 2.2.3 * * @param WP_REST_Request $request Request object. * @param string $request_type A description of the type of request being made. * * @return void */ protected function initiate_rest_box( $request, $request_type ) { $this->initiate_request( $request, $request_type ); $this->rest_box = CMB2_REST::get_rest_box( $this->request->get_param( 'cmb_id' ) ); if ( ! $this->rest_box ) { $this->rest_box = new WP_Error( 'cmb2_rest_box_not_found_error', __( 'No box found by that id. A box needs to be registered with the "show_in_rest" parameter configured.', 'cmb2' ), array( 'status' => 403, ) ); } else { if ( isset( $this->request['object_id'] ) ) { $this->rest_box->cmb->object_id( sanitize_text_field( $this->request['object_id'] ) ); } if ( isset( $this->request['object_type'] ) ) { $this->rest_box->cmb->object_type( sanitize_text_field( $this->request['object_type'] ) ); } } } /** * Initiates the request property and sets up the initial static properties. * * @since 2.2.3 * * @param WP_REST_Request $request Request object. * @param string $request_type A description of the type of request being made. * * @return void */ public function initiate_request( $request, $request_type ) { $this->request = $request; if ( ! isset( $this->request['context'] ) || empty( $this->request['context'] ) ) { $this->request['context'] = 'view'; } if ( ! self::$request_type ) { self::$request_type = $request_type; } if ( ! self::$route ) { self::$route = $this->request->get_route(); } } /** * Useful when getting `_embed`-ed items * * @since 2.2.3 * * @return string Initial requested type. */ public static function get_intial_request_type() { return self::$request_type; } /** * Useful when getting `_embed`-ed items * * @since 2.2.3 * * @return string Initial requested route. */ public static function get_intial_route() { return self::$route; } /** * Get CMB2 fields schema, conforming to JSON Schema * * @since 2.2.3 * * @return array */ public function get_item_schema() { $schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => 'CMB2', 'type' => 'object', 'properties' => array( 'description' => array( 'description' => __( 'A human-readable description of the object.', 'cmb2' ), 'type' => 'string', 'context' => array( 'view', ), ), 'name' => array( 'description' => __( 'The id for the object.', 'cmb2' ), 'type' => 'integer', 'context' => array( 'view', ), ), 'name' => array( 'description' => __( 'The title for the object.', 'cmb2' ), 'type' => 'string', 'context' => array( 'view', ), ), ), ); return $this->add_additional_fields_schema( $schema ); } /** * Return an array of contextual links for endpoint/object * * @link http://v2.wp-api.org/extending/linking/ * @link http://www.iana.org/assignments/link-relations/link-relations.xhtml * * @since 2.2.3 * * @param mixed $object Object to build links from. * * @return array Array of links */ abstract protected function prepare_links( $object ); /** * Get whitelisted query strings from URL for appending to link URLS. * * @since 2.2.3 * * @return string URL query stringl */ public function get_query_string() { $defaults = array( 'object_id' => 0, 'object_type' => '', '_rendered' => '', // '_embed' => '', ); $query_string = ''; foreach ( $defaults as $key => $value ) { if ( isset( $this->request[ $key ] ) ) { $query_string .= $query_string ? '&' : '?'; $query_string .= $key; if ( $value = sanitize_text_field( $this->request[ $key ] ) ) { $query_string .= '=' . $value; } } } return $query_string; } } rest-api/CMB2_REST_Controller_Boxes.php000064400000016717151717007230013703 0ustar00namespace_base = $this->namespace . '/' . $this->rest_base; parent::__construct( $wp_rest_server ); } /** * Register the routes for the objects of the controller. * * @since 2.2.3 */ public function register_routes() { $args = array( '_embed' => array( 'description' => __( 'Includes the registered fields for the box in the response.', 'cmb2' ), ), ); // @todo determine what belongs in the context param. // $args['context'] = $this->get_context_param(); // $args['context']['required'] = false; // $args['context']['default'] = 'view'; // $args['context']['enum'] = array( 'view', 'embed' ); // Returns all boxes data. register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'callback' => array( $this, 'get_items' ), 'args' => $args, ), 'schema' => array( $this, 'get_item_schema' ), ) ); $args['_rendered'] = array( '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' ), ); // Returns specific box's data. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'callback' => array( $this, 'get_item' ), 'args' => $args, ), 'schema' => array( $this, 'get_item_schema' ), ) ); } /** * Check if a given request has access to get boxes. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|boolean */ public function get_items_permissions_check( $request ) { $this->initiate_request( $request, __FUNCTION__ ); /** * By default, no special permissions needed. * * @since 2.2.3 * * @param bool $can_access Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return apply_filters( 'cmb2_api_get_boxes_permissions_check', true, $this ); } /** * Get all public CMB2 boxes. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { $this->initiate_request( $request, 'boxes_read' ); $boxes = CMB2_REST::get_all(); if ( empty( $boxes ) ) { return new WP_Error( 'cmb2_rest_no_boxes', __( 'No boxes found.', 'cmb2' ), array( 'status' => 403, ) ); } $boxes_data = array(); // Loop and prepare boxes data. foreach ( $boxes as $this->rest_box ) { if ( // Make sure this box can be read $this->rest_box->rest_read // And make sure current user can view this box. && $this->get_item_permissions_check_filter( $this->request ) ) { $boxes_data[] = $this->server->response_to_data( $this->get_rest_box(), isset( $this->request['_embed'] ) ); } } return $this->prepare_item( $boxes_data ); } /** * Check if a given request has access to a box. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|boolean */ public function get_item_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'box_read' ); return $this->get_item_permissions_check_filter(); } /** * Check by filter if a given request has access to a box. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param bool $can_access Whether the current request has access to view the box by default. * @return WP_Error|boolean */ public function get_item_permissions_check_filter( $can_access = true ) { /** * By default, no special permissions needed. * * @since 2.2.3 * * @param bool $can_access Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_box_permissions_check', $can_access ); } /** * Get one CMB2 box from the collection. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { $this->initiate_rest_read_box( $request, 'box_read' ); if ( is_wp_error( $this->rest_box ) ) { return $this->rest_box; } return $this->prepare_item( $this->get_rest_box() ); } /** * Get a CMB2 box prepared for REST * * @since 2.2.3 * * @return array */ public function get_rest_box() { $cmb = $this->rest_box->cmb; $boxes_data = $cmb->meta_box; if ( isset( $this->request['_rendered'] ) && $this->namespace_base !== ltrim( CMB2_REST_Controller::get_intial_route(), '/' ) ) { $boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) ); $boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) ); global $wp_scripts, $wp_styles; $before_css = $wp_styles->queue; $before_js = $wp_scripts->queue; CMB2_JS::enqueue(); $boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) ); $boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) ); } // TODO: look into 'embed' parameter. // http://demo.wp-api.org/wp-json/wp/v2/posts?_embed unset( $boxes_data['fields'] ); // Handle callable properties. unset( $boxes_data['show_on_cb'] ); $response = rest_ensure_response( $boxes_data ); $response->add_links( $this->prepare_links( $cmb ) ); return $response; } /** * Return an array of contextual links for box/boxes. * * @since 2.2.3 * * @param CMB2_REST $cmb CMB2_REST object to build links from. * * @return array Array of links */ protected function prepare_links( $cmb ) { $boxbase = $this->namespace_base . '/' . $cmb->cmb_id; $query_string = $this->get_query_string(); return array( // Standard Link Relations -- http://v2.wp-api.org/extending/linking/ 'self' => array( 'href' => rest_url( $boxbase . $query_string ), ), 'collection' => array( 'href' => rest_url( $this->namespace_base . $query_string ), ), // Custom Link Relations -- http://v2.wp-api.org/extending/linking/ // TODO URL should document relationship. 'https://cmb2.io/fields' => array( 'href' => rest_url( trailingslashit( $boxbase ) . 'fields' . $query_string ), 'embeddable' => true, ), ); } } rest-api/CMB2_REST_Controller_Fields.php000064400000037706151717007230014032 0ustar00 array( 'description' => __( 'Includes the box object which the fields are registered to in the response.', 'cmb2' ), ), '_rendered' => array( 'description' => __( 'When the \'_rendered\' argument is passed, the renderable field attributes will be returned fully rendered. By default, the names of the callback handers for the renderable attributes will be returned.', 'cmb2' ), ), 'object_id' => array( 'description' => __( 'To view or modify the field\'s value, the \'object_id\' and \'object_type\' arguments are required.', 'cmb2' ), ), 'object_type' => array( 'description' => __( 'To view or modify the field\'s value, the \'object_id\' and \'object_type\' arguments are required.', 'cmb2' ), ), ); // Returns specific box's fields. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/fields/', array( array( 'methods' => WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'callback' => array( $this, 'get_items' ), 'args' => $args, ), 'schema' => array( $this, 'get_item_schema' ), ) ); $delete_args = $args; $delete_args['object_id']['required'] = true; $delete_args['object_type']['required'] = true; // Returns specific field data. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/fields/(?P[\w-]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'callback' => array( $this, 'get_item' ), 'args' => $args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'callback' => array( $this, 'update_item' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 'args' => $args, ), array( 'methods' => WP_REST_Server::DELETABLE, 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'callback' => array( $this, 'delete_item' ), 'args' => $delete_args, ), 'schema' => array( $this, 'get_item_schema' ), ) ); } /** * Check if a given request has access to get fields. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|boolean */ public function get_items_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'fields_read' ); $can_access = true; /** * By default, no special permissions needed. * * @since 2.2.3 * * @param bool $can_access Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_fields_permissions_check', $can_access ); } /** * Get all public CMB2 box fields. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { if ( ! $this->rest_box ) { $this->initiate_rest_read_box( $request, 'fields_read' ); } if ( is_wp_error( $this->rest_box ) ) { return $this->rest_box; } $fields = array(); foreach ( $this->rest_box->cmb->prop( 'fields', array() ) as $field ) { // Make sure this field can be read. $this->field = $this->rest_box->field_can_read( $field['id'], true ); // And make sure current user can view this box. if ( $this->field && $this->get_item_permissions_check_filter() ) { $fields[ $field['id'] ] = $this->server->response_to_data( $this->prepare_field_response(), isset( $this->request['_embed'] ) ); } } return $this->prepare_item( $fields ); } /** * Check if a given request has access to a field. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|boolean */ public function get_item_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'field_read' ); if ( ! is_wp_error( $this->rest_box ) ) { $this->field = $this->rest_box->field_can_read( $this->request->get_param( 'field_id' ), true ); } return $this->get_item_permissions_check_filter(); } /** * Check by filter if a given request has access to a field. * By default, no special permissions needed, but filtering return value. * * @since 2.2.3 * * @param bool $can_access Whether the current request has access to view the field by default. * @return WP_Error|boolean */ public function get_item_permissions_check_filter( $can_access = true ) { /** * By default, no special permissions needed. * * @since 2.2.3 * * @param bool $can_access Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_field_permissions_check', $can_access ); } /** * Get one CMB2 field from the collection. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { $this->initiate_rest_read_box( $request, 'field_read' ); if ( is_wp_error( $this->rest_box ) ) { return $this->rest_box; } return $this->prepare_read_field( $this->request->get_param( 'field_id' ) ); } /** * Check if a given request has access to update a field value. * By default, requires 'edit_others_posts' capability, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|boolean */ public function update_item_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'field_value_update' ); if ( ! is_wp_error( $this->rest_box ) ) { $this->field = $this->rest_box->field_can_edit( $this->request->get_param( 'field_id' ), true ); } $can_update = current_user_can( 'edit_others_posts' ); /** * By default, 'edit_others_posts' is required capability. * * @since 2.2.3 * * @param bool $can_update Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_update_field_value_permissions_check', $can_update ); } /** * Update CMB2 field value. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function update_item( $request ) { $this->initiate_rest_read_box( $request, 'field_value_update' ); if ( ! $this->request['value'] ) { return new WP_Error( 'cmb2_rest_update_field_error', __( 'CMB2 Field value cannot be updated without the value parameter specified.', 'cmb2' ), array( 'status' => 400, ) ); } return $this->modify_field_value( 'updated' ); } /** * Check if a given request has access to delete a field value. * By default, requires 'delete_others_posts' capability, but filtering return value. * * @since 2.2.3 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|boolean */ public function delete_item_permissions_check( $request ) { $this->initiate_rest_read_box( $request, 'field_value_delete' ); if ( ! is_wp_error( $this->rest_box ) ) { $this->field = $this->rest_box->field_can_edit( $this->request->get_param( 'field_id' ), true ); } $can_delete = current_user_can( 'delete_others_posts' ); /** * By default, 'delete_others_posts' is required capability. * * @since 2.2.3 * * @param bool $can_delete Whether this CMB2 endpoint can be accessed. * @param object $controller This CMB2_REST_Controller object. */ return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_delete_field_value_permissions_check', $can_delete ); } /** * Delete CMB2 field value. * * @since 2.2.3 * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function delete_item( $request ) { $this->initiate_rest_read_box( $request, 'field_value_delete' ); return $this->modify_field_value( 'deleted' ); } /** * Modify CMB2 field value. * * @since 2.2.3 * * @param string $activity The modification activity (updated or deleted). * @return WP_Error|WP_REST_Response */ public function modify_field_value( $activity ) { if ( ! $this->request['object_id'] || ! $this->request['object_type'] ) { return new WP_Error( 'cmb2_rest_modify_field_value_error', __( 'CMB2 Field value cannot be modified without the object_id and object_type parameters specified.', 'cmb2' ), array( 'status' => 400, ) ); } if ( is_wp_error( $this->rest_box ) ) { return $this->rest_box; } $this->field = $this->rest_box->field_can_edit( $this->field ? $this->field : $this->request->get_param( 'field_id' ), true ); if ( ! $this->field ) { return new WP_Error( 'cmb2_rest_no_field_by_id_error', __( 'No field found by that id.', 'cmb2' ), array( 'status' => 403, ) ); } $this->field->args[ "value_{$activity}" ] = (bool) 'deleted' === $activity ? $this->field->remove_data() : $this->field->save_field( $this->request['value'] ); // If options page, save the $activity options if ( 'options-page' == $this->request['object_type'] ) { $this->field->args[ "value_{$activity}" ] = cmb2_options( $this->request['object_id'] )->set(); } return $this->prepare_read_field( $this->field ); } /** * Get a response object for a specific field ID. * * @since 2.2.3 * * @param string\CMB2_Field Field id or Field object. * @return WP_Error|WP_REST_Response */ public function prepare_read_field( $field ) { $this->field = $this->rest_box->field_can_read( $field, true ); if ( ! $this->field ) { return new WP_Error( 'cmb2_rest_no_field_by_id_error', __( 'No field found by that id.', 'cmb2' ), array( 'status' => 403, ) ); } return $this->prepare_item( $this->prepare_field_response() ); } /** * Get a specific field response. * * @since 2.2.3 * * @param CMB2_Field Field object. * @return array Response array. */ public function prepare_field_response() { $field_data = $this->prepare_field_data( $this->field ); $response = rest_ensure_response( $field_data ); $response->add_links( $this->prepare_links( $this->field ) ); return $response; } /** * Prepare the field data array for JSON. * * @since 2.2.3 * * @param CMB2_Field $field field object. * * @return array Array of field data. */ protected function prepare_field_data( CMB2_Field $field ) { $field_data = array(); $params_to_ignore = array( 'show_in_rest', 'options' ); $params_to_rename = array( 'label_cb' => 'label', 'options_cb' => 'options', ); // Run this first so the js_dependencies arg is populated. $rendered = ( $cb = $field->maybe_callback( 'render_row_cb' ) ) // Ok, callback is good, let's run it. ? $this->get_cb_results( $cb, $field->args(), $field ) : false; $field_args = $field->args(); foreach ( $field_args as $key => $value ) { if ( in_array( $key, $params_to_ignore, true ) ) { continue; } if ( 'options_cb' === $key ) { $value = $field->options(); } elseif ( in_array( $key, CMB2_Field::$callable_fields, true ) ) { if ( isset( $this->request['_rendered'] ) ) { $value = $key === 'render_row_cb' ? $rendered : $field->get_param_callback_result( $key ); } elseif ( is_array( $value ) ) { // We need to rewrite callbacks as string as they will cause // JSON recursion errors. $class = is_string( $value[0] ) ? $value[0] : get_class( $value[0] ); $value = $class . '::' . $value[1]; } } $key = isset( $params_to_rename[ $key ] ) ? $params_to_rename[ $key ] : $key; if ( empty( $value ) || is_scalar( $value ) || is_array( $value ) ) { $field_data[ $key ] = $value; } else { $field_data[ $key ] = sprintf( __( 'Value Error for %s', 'cmb2' ), $key ); } } if ( $field->args( 'has_supporting_data' ) ) { $field_data = $this->get_supporting_data( $field_data, $field ); } if ( $this->request['object_id'] && $this->request['object_type'] ) { $field_data['value'] = $field->get_rest_value(); } return $field_data; } /** * Gets field supporting data (field id and value). * * @since 2.7.0 * * @param CMB2_Field $field Field object. * @param array $field_data Array of field data. * * @return array Array of field data. */ public function get_supporting_data( $field_data, $field ) { // Reset placement of this property. unset( $field_data['has_supporting_data'] ); $field_data['has_supporting_data'] = true; $field = $field->get_supporting_field(); $field_data['supporting_data'] = array( 'id' => $field->_id( '', false ), ); if ( $this->request['object_id'] && $this->request['object_type'] ) { $field_data['supporting_data']['value'] = $field->get_rest_value(); } return $field_data; } /** * Return an array of contextual links for field/fields. * * @since 2.2.3 * * @param CMB2_Field $field Field object to build links from. * * @return array Array of links */ protected function prepare_links( $field ) { $boxbase = $this->namespace_base . '/' . $this->rest_box->cmb->cmb_id; $query_string = $this->get_query_string(); $links = array( 'self' => array( 'href' => rest_url( trailingslashit( $boxbase ) . 'fields/' . $field->_id( '', false ) . $query_string ), ), 'collection' => array( 'href' => rest_url( trailingslashit( $boxbase ) . 'fields' . $query_string ), ), 'up' => array( 'embeddable' => true, 'href' => rest_url( $boxbase . $query_string ), ), ); return $links; } /** * Checks if the CMB2 box or field has any registered callback parameters for the given filter. * * The registered handlers will have a property name which matches the filter, except: * - The 'cmb2_api' prefix will be removed * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). * * @since 2.2.3 * * @param string $filter The filter name. * @param bool $default_val The default filter value. * * @return bool The possibly-modified filter value (if the _cb param is a non-callable). */ public function maybe_hook_registered_callback( $filter, $default_val ) { $default_val = parent::maybe_hook_registered_callback( $filter, $default_val ); if ( $this->field ) { // Hook field specific filter callbacks. $val = $this->field->maybe_hook_parameter( $filter, $default_val ); if ( null !== $val ) { $default_val = $val; } } return $default_val; } /** * Unhooks any CMB2 box or field registered callback parameters for the given filter. * * @since 2.2.3 * * @param string $filter The filter name. * * @return void */ public function maybe_unhook_registered_callback( $filter ) { parent::maybe_unhook_registered_callback( $filter ); if ( $this->field ) { // Unhook field specific filter callbacks. $this->field->maybe_hook_parameter( $filter, null, 'remove_filter' ); } } } shim/WP_REST_Controller.php000064400000036736151717007230011625 0ustar00 405, ) ); } /** * Get a collection of items. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Check if a given request has access to get a specific item. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|boolean */ public function get_item_permissions_check( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Get one item from the collection. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Check if a given request has access to create items. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|boolean */ public function create_item_permissions_check( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Create one item from the collection. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function create_item( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Check if a given request has access to update a specific item. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|boolean */ public function update_item_permissions_check( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Update one item from the collection. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function update_item( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Check if a given request has access to delete a specific item. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|boolean */ public function delete_item_permissions_check( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Delete one item from the collection. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function delete_item( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Prepare the item for create or update operation. * * @param WP_REST_Request $request Request object. * @return WP_Error|object $prepared_item */ protected function prepare_item_for_database( $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Prepare the item for the REST response. * * @param mixed $item WordPress representation of the item. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $response */ public function prepare_item_for_response( $item, $request ) { return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405, ) ); } /** * Prepare a response for inserting into a collection. * * @param WP_REST_Response $response Response object. * @return array Response data, ready for insertion into collection data. */ public function prepare_response_for_collection( $response ) { if ( ! ( $response instanceof WP_REST_Response ) ) { return $response; } $data = (array) $response->get_data(); $server = rest_get_server(); if ( method_exists( $server, 'get_compact_response_links' ) ) { $links = call_user_func( array( $server, 'get_compact_response_links' ), $response ); } else { $links = call_user_func( array( $server, 'get_response_links' ), $response ); } if ( ! empty( $links ) ) { $data['_links'] = $links; } return $data; } /** * Filter a response based on the context defined in the schema. * * @param array $data * @param string $context * @return array */ public function filter_response_by_context( $data, $context ) { $schema = $this->get_item_schema(); foreach ( $data as $key => $value ) { if ( empty( $schema['properties'][ $key ] ) || empty( $schema['properties'][ $key ]['context'] ) ) { continue; } if ( ! in_array( $context, $schema['properties'][ $key ]['context'] ) ) { unset( $data[ $key ] ); continue; } if ( 'object' === $schema['properties'][ $key ]['type'] && ! empty( $schema['properties'][ $key ]['properties'] ) ) { foreach ( $schema['properties'][ $key ]['properties'] as $attribute => $details ) { if ( empty( $details['context'] ) ) { continue; } if ( ! in_array( $context, $details['context'] ) ) { if ( isset( $data[ $key ][ $attribute ] ) ) { unset( $data[ $key ][ $attribute ] ); } } } } } return $data; } /** * Get the item's schema, conforming to JSON Schema. * * @return array */ public function get_item_schema() { return $this->add_additional_fields_schema( array() ); } /** * Get the item's schema for display / public consumption purposes. * * @return array */ public function get_public_item_schema() { $schema = $this->get_item_schema(); foreach ( $schema['properties'] as &$property ) { if ( isset( $property['arg_options'] ) ) { unset( $property['arg_options'] ); } } return $schema; } /** * Get the query params for collections. * * @return array */ public function get_collection_params() { return array( 'context' => $this->get_context_param(), 'page' => array( 'description' => __( 'Current page of the collection.' ), 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', 'minimum' => 1, ), 'per_page' => array( 'description' => __( 'Maximum number of items to be returned in result set.' ), 'type' => 'integer', 'default' => 10, 'minimum' => 1, 'maximum' => 100, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ), 'search' => array( 'description' => __( 'Limit results to those matching a string.' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg', ), ); } /** * Get the magical context param. * * Ensures consistent description between endpoints, and populates enum from schema. * * @param array $args * @return array */ public function get_context_param( $args = array() ) { $param_details = array( 'description' => __( 'Scope under which the request is made; determines fields present in response.' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_key', 'validate_callback' => 'rest_validate_request_arg', ); $schema = $this->get_item_schema(); if ( empty( $schema['properties'] ) ) { return array_merge( $param_details, $args ); } $contexts = array(); foreach ( $schema['properties'] as $attributes ) { if ( ! empty( $attributes['context'] ) ) { $contexts = array_merge( $contexts, $attributes['context'] ); } } if ( ! empty( $contexts ) ) { $param_details['enum'] = array_unique( $contexts ); rsort( $param_details['enum'] ); } return array_merge( $param_details, $args ); } /** * Add the values from additional fields to a data object. * * @param array $object * @param WP_REST_Request $request * @return array modified object with additional fields. */ protected function add_additional_fields_to_object( $object, $request ) { $additional_fields = $this->get_additional_fields(); foreach ( $additional_fields as $field_name => $field_options ) { if ( ! $field_options['get_callback'] ) { continue; } $object[ $field_name ] = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type() ); } return $object; } /** * Update the values of additional fields added to a data object. * * @param array $object * @param WP_REST_Request $request */ protected function update_additional_fields_for_object( $object, $request ) { $additional_fields = $this->get_additional_fields(); foreach ( $additional_fields as $field_name => $field_options ) { if ( ! $field_options['update_callback'] ) { continue; } // Don't run the update callbacks if the data wasn't passed in the request. if ( ! isset( $request[ $field_name ] ) ) { continue; } call_user_func( $field_options['update_callback'], $request[ $field_name ], $object, $field_name, $request, $this->get_object_type() ); } } /** * Add the schema from additional fields to an schema array. * * The type of object is inferred from the passed schema. * * @param array $schema Schema array. */ protected function add_additional_fields_schema( $schema ) { if ( empty( $schema['title'] ) ) { return $schema; } /** * Can't use $this->get_object_type otherwise we cause an inf loop. */ $object_type = $schema['title']; $additional_fields = $this->get_additional_fields( $object_type ); foreach ( $additional_fields as $field_name => $field_options ) { if ( ! $field_options['schema'] ) { continue; } $schema['properties'][ $field_name ] = $field_options['schema']; } return $schema; } /** * Get all the registered additional fields for a given object-type. * * @param string $object_type * @return array */ protected function get_additional_fields( $object_type = null ) { if ( ! $object_type ) { $object_type = $this->get_object_type(); } if ( ! $object_type ) { return array(); } global $wp_rest_additional_fields; if ( ! $wp_rest_additional_fields || ! isset( $wp_rest_additional_fields[ $object_type ] ) ) { return array(); } return $wp_rest_additional_fields[ $object_type ]; } /** * Get the object type this controller is responsible for managing. * * @return string */ protected function get_object_type() { $schema = $this->get_item_schema(); if ( ! $schema || ! isset( $schema['title'] ) ) { return null; } return $schema['title']; } /** * Get an array of endpoint arguments from the item schema for the controller. * * @param string $method HTTP method of the request. The arguments * for `CREATABLE` requests are checked for required * values and may fall-back to a given default, this * is not done on `EDITABLE` requests. Default is * WP_REST_Server::CREATABLE. * @return array $endpoint_args */ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { $schema = $this->get_item_schema(); $schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); $endpoint_args = array(); foreach ( $schema_properties as $field_id => $params ) { // Arguments specified as `readonly` are not allowed to be set. if ( ! empty( $params['readonly'] ) ) { continue; } $endpoint_args[ $field_id ] = array( 'validate_callback' => 'rest_validate_request_arg', 'sanitize_callback' => 'rest_sanitize_request_arg', ); if ( isset( $params['description'] ) ) { $endpoint_args[ $field_id ]['description'] = $params['description']; } if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) { $endpoint_args[ $field_id ]['default'] = $params['default']; } if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) { $endpoint_args[ $field_id ]['required'] = true; } foreach ( array( 'type', 'format', 'enum' ) as $schema_prop ) { if ( isset( $params[ $schema_prop ] ) ) { $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ]; } } // Merge in any options provided by the schema property. if ( isset( $params['arg_options'] ) ) { // Only use required / default from arg_options on CREATABLE endpoints. if ( WP_REST_Server::CREATABLE !== $method ) { $params['arg_options'] = array_diff_key( $params['arg_options'], array( 'required' => '', 'default' => '', ) ); } $endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] ); } }// End foreach(). return $endpoint_args; } /** * Retrieves post data given a post ID or post object. * * This is a subset of the functionality of the `get_post()` function, with * the additional functionality of having `the_post` action done on the * resultant post object. This is done so that plugins may manipulate the * post that is used in the REST API. * * @see get_post() * @global WP_Query $wp_query * * @param int|WP_Post $post Post ID or post object. Defaults to global $post. * @return WP_Post|null A `WP_Post` object when successful. */ public function get_post( $post ) { $post_obj = get_post( $post ); /** * Filter the post. * * Allows plugins to filter the post object as returned by `\WP_REST_Controller::get_post()`. * * @param WP_Post|null $post_obj The post object as returned by `get_post()`. * @param int|WP_Post $post The original value used to obtain the post object. */ $post = apply_filters( 'rest_the_post', $post_obj, $post ); return $post; } } types/CMB2_Type_Base.php000064400000011215151717007230011042 0ustar00types = $types; $args['rendered'] = isset( $args['rendered'] ) ? (bool) $args['rendered'] : true; $this->args = $args; } /** * Handles rendering this field type. * * @since 2.2.2 * @return string Rendered field type. */ abstract public function render(); /** * Stores the rendered field output. * * @since 2.2.2 * @param string|CMB2_Type_Base $rendered Rendered output. * @return string|CMB2_Type_Base Rendered output or this object. */ public function rendered( $rendered ) { $this->field->register_js_data(); if ( $this->args['rendered'] ) { return is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered; } $this->rendered = is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered; return $this; } /** * Returns the stored rendered field output. * * @since 2.2.2 * @return string Stored rendered output (if 'rendered' argument is set to false). */ public function get_rendered() { return $this->rendered; } /** * Handles parsing and filtering attributes while preserving any passed in via field config. * * @since 1.1.0 * @param string $element Element for filter. * @param array $type_defaults Type default arguments. * @param array $type_overrides Type override arguments. * @return array Parsed and filtered arguments. */ public function parse_args( $element, $type_defaults, $type_overrides = array() ) { $args = $this->parse_args_from_overrides( $type_overrides ); /** * Filter attributes for a field type. * The dynamic portion of the hook name, $element, refers to the field type. * * @since 1.1.0 * @param array $args The array of attribute arguments. * @param array $type_defaults The array of default values. * @param array $field The `CMB2_Field` object. * @param object $field_type_object This `CMB2_Types` object. */ $args = apply_filters( "cmb2_{$element}_attributes", $args, $type_defaults, $this->field, $this->types ); $args = wp_parse_args( $args, $type_defaults ); if ( ! empty( $args['js_dependencies'] ) ) { $this->field->add_js_dependencies( $args['js_dependencies'] ); } return $args; } /** * Handles parsing and filtering attributes while preserving any passed in via field config. * * @since 2.2.4 * @param array $type_overrides Type override arguments. * @return array Parsed arguments */ protected function parse_args_from_overrides( $type_overrides = array() ) { $type_overrides = empty( $type_overrides ) ? $this->args : $type_overrides; if ( true !== $this->field->args( 'disable_hash_data_attribute' ) ) { $type_overrides['data-hash'] = $this->field->hash_id(); } $field_overrides = $this->field->args( 'attributes' ); return ! empty( $field_overrides ) ? wp_parse_args( $field_overrides, $type_overrides ) : $type_overrides; } /** * Fall back to CMB2_Types methods * * @param string $method Method name being invoked. * @param array $arguments Arguments passed for the method. * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __call( $method, $arguments ) { switch ( $method ) { case '_id': case '_name': case '_desc': case '_text': case 'concat_attrs': return call_user_func_array( array( $this->types, $method ), $arguments ); default: throw new Exception( sprintf( esc_html__( 'Invalid %1$s method: %2$s', 'cmb2' ), __CLASS__, $method ) ); } } /** * Magic getter for our object. * * @param string $field Property being requested. * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __get( $field ) { switch ( $field ) { case 'field': return $this->types->field; default: throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); } } } types/CMB2_Type_Checkbox.php000064400000003005151717007230011714 0ustar00is_checked = $is_checked; } /** * Render the field for the field type. * * @since 2.2.2 * * @param array $args Array of arguments for the rendering. * @return CMB2_Type_Base|string */ public function render( $args = array() ) { $defaults = array( 'type' => 'checkbox', 'class' => 'cmb2-option cmb2-list', 'value' => 'on', 'desc' => '', ); $meta_value = $this->field->escaped_value(); $is_checked = null === $this->is_checked ? ! empty( $meta_value ) : $this->is_checked; if ( $is_checked ) { $defaults['checked'] = 'checked'; } $args = $this->parse_args( 'checkbox', $defaults ); return $this->rendered( sprintf( '%s ', parent::render( $args ), $this->_id( '', false ), $this->_desc() ) ); } } types/CMB2_Type_Colorpicker.php000064400000005456151717007230012456 0ustar00value = $value ? $value : $this->value; } /** * Render the field for the field type. * * @since 2.2.2 * * @param array $args Array of arguments for the rendering. * * @return CMB2_Type_Base|string */ public function render( $args = array() ) { $meta_value = $this->value ? $this->value : $this->field->escaped_value(); $meta_value = self::sanitize_color( $meta_value ); wp_enqueue_style( 'wp-color-picker' ); $args = wp_parse_args( $args, array( 'class' => 'cmb2-text-small', ) ); $args['class'] .= ' cmb2-colorpicker'; $args['value'] = $meta_value; $args['js_dependencies'] = array( 'wp-color-picker' ); if ( $this->field->options( 'alpha' ) ) { $args['js_dependencies'][] = 'wp-color-picker-alpha'; $args['data-alpha'] = 'true'; } $args = wp_parse_args( $this->args, $args ); return parent::render( $args ); } /** * Sanitizes the given color, or array of colors. * * @since 2.9.0 * * @param string|array $color The color or array of colors to sanitize. * * @return string|array The color or array of colors, sanitized. */ public static function sanitize_color( $color ) { if ( is_array( $color ) ) { $color = array_map( array( 'CMB2_Type_Colorpicker', 'sanitize_color' ), $color ); } else { // Regexp for hexadecimal colors $hex_color = '(([a-fA-F0-9]){3}){1,2}$'; if ( preg_match( '/^' . $hex_color . '/i', $color ) ) { // Value is just 123abc, so prepend # $color = '#' . $color; } elseif ( // If value doesn't match #123abc... ! preg_match( '/^#' . $hex_color . '/i', $color ) // And value doesn't match rgba()... && 0 !== strpos( trim( $color ), 'rgba' ) ) { // Then sanitize to just #. $color = '#'; } } return $color; } /** * Provide the option to use a rgba colorpicker. * * @since 2.2.6.2 */ public static function dequeue_rgba_colorpicker_script() { if ( wp_script_is( 'jw-cmb2-rgba-picker-js', 'enqueued' ) ) { wp_dequeue_script( 'jw-cmb2-rgba-picker-js' ); CMB2_JS::register_colorpicker_alpha( true ); } } } types/CMB2_Type_Counter_Base.php000064400000006630151717007230012546 0ustar00field->args( 'char_counter' ) ) { return $markup; } $type = (string) $this->field->args( 'char_counter' ); $field_id = $this->_id( '', false ); $char_max = (int) $this->field->prop( 'char_max' ); if ( $char_max ) { $char_max = 'data-max="' . $char_max . '"'; } switch ( $type ) { case 'words': $label = $char_max ? $this->_text( 'words_left_text', esc_html__( 'Words left', 'cmb2' ) ) : $this->_text( 'words_text', esc_html__( 'Words', 'cmb2' ) ); break; default: $type = 'characters'; $label = $char_max ? $this->_text( 'characters_left_text', esc_html__( 'Characters left', 'cmb2' ) ) : $this->_text( 'characters_text', esc_html__( 'Characters', 'cmb2' ) ); break; } $msg = $char_max ? sprintf( '%s', $this->_text( 'characters_truncated_text', esc_html__( 'Your text may be truncated.', 'cmb2' ) ) ) : ''; $length = strlen( $val ); $width = $length > 1 ? ( 8 * strlen( (string) $length ) ) + 15 : false; $markup .= '

'; $markup .= sprintf( '%8$s', esc_attr( 'char-counter-' . $field_id ), $label, esc_attr( $field_id ), $type, $char_max, $length, $width ? "width: {$width}px;" : '', $msg ); $markup .= '

'; // Enqueue the required JS. $this->field->add_js_dependencies( array( 'word-count', 'wp-util', 'cmb2-char-counter', ) ); $this->has_counter = true; return $markup; } /** * Maybe update attributes for the character counter. * * @since 2.7.0 * * @param array $attributes Array of parsed attributes. * * @return array Potentially modified attributes. */ public function maybe_update_attributes_for_char_counter( $attributes ) { $char_counter = $this->char_counter_markup( $attributes['value'] ); // Has character counter? if ( $char_counter ) { $attributes['class'] = ! empty( $attributes['class'] ) ? $attributes['class'] . ' cmb2-count-chars' : ' cmb2-count-chars'; // Enforce max chars? $max = $this->enforce_max(); if ( $max ) { $attributes['maxlength'] = $max; } $attributes['desc'] = $char_counter . $attributes['desc']; } return $attributes; } /** * Enforce max chars? * * @since 2.7.0 * * @return bool Whether to enforce max characters. */ public function enforce_max() { $char_max = (int) $this->field->args( 'char_max' ); // Enforce max chars? return ( $this->field->args( 'char_max_enforce' ) && $char_max > 0 && 'words' !== $this->field->args( 'char_counter' ) ) ? $char_max : false; } } types/CMB2_Type_File.php000064400000012644151717007230011056 0ustar00args : $args; $field = $this->field; $options = (array) $field->options(); $a = $this->args = $this->parse_args( 'file', array( 'class' => 'cmb2-upload-file regular-text', 'id' => $this->_id(), 'name' => $this->_name(), 'value' => $field->escaped_value(), 'id_value' => null, 'desc' => $this->_desc( true ), 'size' => 45, 'js_dependencies' => 'media-editor', 'preview_size' => $field->args( 'preview_size' ), 'query_args' => $field->args( 'query_args' ), // if options array and 'url' => false, then hide the url field. 'type' => array_key_exists( 'url', $options ) && false === $options['url'] ? 'hidden' : 'text', ), $args ); // get an array of image size meta data, fallback to 'large'. $this->args['img_size_data'] = $img_size_data = parent::get_image_size_data( $a['preview_size'], 'large' ); $output = ''; $output .= parent::render( array( 'type' => $a['type'], 'class' => $a['class'], 'value' => $a['value'], 'id' => $a['id'], 'name' => $a['name'], 'size' => $a['size'], 'desc' => '', 'data-previewsize' => sprintf( '[%d,%d]', $img_size_data['width'], $img_size_data['height'] ), 'data-sizename' => $img_size_data['name'], 'data-queryargs' => ! empty( $a['query_args'] ) ? json_encode( $a['query_args'] ) : '', 'js_dependencies' => $a['js_dependencies'], ) ); // Now remove the data-iterator attribute if it exists. // (Possible if being used within a custom field) // This is not elegant, but compensates for CMB2_Types::_id // automagically & inelegantly adding the data-iterator attribute. // Single responsibility principle? pffft. $parts = explode( '"', $this->args['id'] ); $this->args['id'] = $parts[0]; $output .= sprintf( '', esc_attr( $this->_text( 'add_upload_file_text', esc_html__( 'Add or Upload File', 'cmb2' ) ) ) ); $output .= $a['desc']; $output .= $this->get_id_field_output(); $output .= '
'; if ( ! empty( $a['value'] ) ) { $output .= $this->get_file_preview_output(); } $output .= '
'; return $this->rendered( $output ); } /** * Return attempted file preview output for a provided file. * * @since 2.2.5 * * @return string */ public function get_file_preview_output() { if ( ! $this->is_valid_img_ext( $this->args['value'] ) ) { return $this->file_status_output( array( 'value' => $this->args['value'], 'tag' => 'div', 'cached_id' => $this->args['id'], ) ); } if ( $this->args['id_value'] ) { $image = wp_get_attachment_image( $this->args['id_value'], $this->args['preview_size'], null, array( 'class' => 'cmb-file-field-image', ) ); } else { $image = ''; } return $this->img_status_output( array( 'image' => $image, 'tag' => 'div', 'cached_id' => $this->args['id'], ) ); } /** * Return field ID output as a hidden field. * * @since 2.2.5 * * @return string */ public function get_id_field_output() { $field = $this->field; /* * A little bit of magic (tsk tsk) replacing the $this->types->field object, * So that the render function is using the proper field object. */ $this->types->field = $this->get_id_field(); $output = parent::render( array( 'type' => 'hidden', 'class' => 'cmb2-upload-file-id', 'value' => $this->types->field->value, 'desc' => '', ) ); // We need to put the original field object back // or other fields in a custom field will be broken. $this->types->field = $field; return $output; } /** * Return field ID data. * * @since 2.2.5 * * @return mixed */ public function get_id_field() { // reset field args for attachment id. $args = array( // if we're looking at a file in a group, we need to get the non-prefixed id. 'id' => ( $this->field->group ? $this->field->args( '_id' ) : $this->args['id'] ) . '_id', 'disable_hash_data_attribute' => true, ); // and get new field object // (need to set it to the types field property). $id_field = $this->field->get_field_clone( $args ); $id_value = absint( null !== $this->args['id_value'] ? $this->args['id_value'] : $id_field->escaped_value() ); // we don't want to output "0" as a value. if ( ! $id_value ) { $id_value = ''; } // if there is no id saved yet, try to get it from the url. if ( $this->args['value'] && ! $id_value ) { $id_value = CMB2_Utils::image_id_from_url( esc_url_raw( $this->args['value'] ) ); } $id_field->value = $id_value; return $id_field; } } types/CMB2_Type_File_Base.php000064400000020676151717007230012014 0ustar00 $mime) { if ( 0 === strpos( $mime, 'image/' ) ) { $types = explode( '|', $type ); $valid_types = array_merge( $valid_types, $types ); } } $valid_types = array_unique( $valid_types ); } /** * Which image types are considered valid image file extensions. * * @since 2.0.9 * * @param array $valid_types The valid image file extensions. */ $is_valid_types = apply_filters( 'cmb2_valid_img_types', $valid_types ); $is_valid = $file_ext && in_array( $file_ext, (array) $is_valid_types ); $field_id = $this->field->id(); /** * Filter for determining if a field value has a valid image file-type extension. * * The dynamic portion of the hook name, $field_id, refers to the field id attribute. * * @since 2.0.9 * * @param bool $is_valid Whether field value has a valid image file-type extension. * @param string $file File url. * @param string $file_ext File extension. */ return (bool) apply_filters( "cmb2_{$field_id}_is_valid_img_ext", $is_valid, $file, $file_ext ); } /** * file/file_list image wrap * * @since 2.0.2 * @param array $args Array of arguments for output * @return string Image wrap output */ public function img_status_output( $args ) { return sprintf( '<%1$s class="img-status cmb2-media-item">%2$s

%4$s

%5$s', $args['tag'], $args['image'], isset( $args['cached_id'] ) ? ' rel="' . esc_attr( $args['cached_id'] ) . '"' : '', esc_html( $this->_text( 'remove_image_text', esc_html__( 'Remove Image', 'cmb2' ) ) ), isset( $args['id_input'] ) ? $args['id_input'] : '' ); } /** * file/file_list file wrap * * @since 2.0.2 * @param array $args Array of arguments for output * @return string File wrap output */ public function file_status_output( $args ) { return sprintf( '<%1$s class="file-status cmb2-media-item">%2$s %3$s   (%5$s / %7$s)%8$s', $args['tag'], esc_html( $this->_text( 'file_text', esc_html__( 'File:', 'cmb2' ) ) ), esc_html( CMB2_Utils::get_file_name_from_path( $args['value'] ) ), esc_url( $args['value'] ), esc_html( $this->_text( 'file_download_text', esc_html__( 'Download', 'cmb2' ) ) ), isset( $args['cached_id'] ) ? ' rel="' . esc_attr( $args['cached_id'] ) . '"' : '', esc_html( $this->_text( 'remove_text', esc_html__( 'Remove', 'cmb2' ) ) ), isset( $args['id_input'] ) ? $args['id_input'] : '' ); } /** * Outputs the file/file_list underscore Javascript templates in the footer. * * @since 2.2.4 * @return void */ public static function output_js_underscore_templates() { ?> (int) image size width * 'height' => (int) image size height * 'name' => (string) e.g. 'thumbnail' * ) */ static function get_image_size_data( $img_size = '', $fallback = 'thumbnail' ) { $data = array(); if ( is_array( $img_size ) ) { $data['width'] = intval( $img_size[0] ); $data['height'] = intval( $img_size[1] ); $data['name'] = ''; // Try and get the closest named size from our array of dimensions if ( $named_size = CMB2_Utils::get_named_size( $img_size ) ) { $data['name'] = $named_size; } } else { $image_sizes = CMB2_Utils::get_available_image_sizes(); // The 'thumb' alias, which works elsewhere, doesn't work in the wp.media uploader if ( 'thumb' == $img_size ) { $img_size = 'thumbnail'; } // Named size doesn't exist, use $fallback if ( ! array_key_exists( $img_size, $image_sizes ) ) { $img_size = $fallback; } // Get image dimensions from named sizes $data['width'] = intval( $image_sizes[ $img_size ]['width'] ); $data['height'] = intval( $image_sizes[ $img_size ]['height'] ); $data['name'] = $img_size; } return $data; } /** * Filters attachment data prepared for JavaScript. * * Adds the url, width, height, and orientation for custom sizes to the JavaScript * object returned by the wp.media uploader. Hooked to 'wp_prepare_attachment_for_js'. * * @since 2.2.4 * @param array $response Array of prepared attachment data * @param int|object $attachment Attachment ID or object * @param array $meta Array of attachment meta data ( from wp_get_attachment_metadata() ) * @return array filtered $response array */ public static function prepare_image_sizes_for_js( $response, $attachment, $meta ) { foreach ( CMB2_Utils::get_available_image_sizes() as $size => $info ) { // registered image size exists for this attachment if ( isset( $meta['sizes'][ $size ] ) ) { $attachment_url = wp_get_attachment_url( $attachment->ID ); $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url ); $size_meta = $meta['sizes'][ $size ]; $response['sizes'][ $size ] = array( 'url' => $base_url . $size_meta['file'], 'height' => $size_meta['height'], 'width' => $size_meta['width'], 'orientation' => $size_meta['height'] > $size_meta['width'] ? 'portrait' : 'landscape', ); } } return $response; } } types/CMB2_Type_File_List.php000064400000004426151717007230012050 0ustar00field; $meta_value = $field->escaped_value(); $name = $this->_name(); $img_size = $field->args( 'preview_size' ); $query_args = $field->args( 'query_args' ); $output = ''; // get an array of image size meta data, fallback to 'thumbnail' $img_size_data = parent::get_image_size_data( $img_size, 'thumbnail' ); $output .= parent::render( array( 'type' => 'hidden', 'class' => 'cmb2-upload-file cmb2-upload-list', 'size' => 45, 'desc' => '', 'value' => '', 'data-previewsize' => sprintf( '[%d,%d]', $img_size_data['width'], $img_size_data['height'] ), 'data-sizename' => $img_size_data['name'], 'data-queryargs' => ! empty( $query_args ) ? json_encode( $query_args ) : '', 'js_dependencies' => 'media-editor', ) ); $output .= parent::render( array( 'type' => 'button', 'class' => 'cmb2-upload-button button-secondary cmb2-upload-list', 'value' => esc_attr( $this->_text( 'add_upload_files_text', esc_html__( 'Add or Upload Files', 'cmb2' ) ) ), 'name' => false, 'id' => false, ) ); $output .= '
    '; if ( $meta_value && is_array( $meta_value ) ) { foreach ( $meta_value as $id => $fullurl ) { $id_input = parent::render( array( 'type' => 'hidden', 'value' => $fullurl, 'name' => $name . '[' . $id . ']', 'id' => 'filelist-' . $id, 'data-id' => $id, 'desc' => '', 'class' => false, ) ); if ( $this->is_valid_img_ext( $fullurl ) ) { $output .= $this->img_status_output( array( 'image' => wp_get_attachment_image( $id, $img_size ), 'tag' => 'li', 'id_input' => $id_input, ) ); } else { $output .= $this->file_status_output( array( 'value' => $fullurl, 'tag' => 'li', 'id_input' => $id_input, ) ); } } } $output .= '
'; return $this->rendered( $output ); } } types/CMB2_Type_Multicheck.php000064400000001502151717007230012256 0ustar00field->args( 'select_all_button' ) ? 'cmb2-checkbox-list no-select-all cmb2-list' : 'cmb2-checkbox-list cmb2-list'; $args = $this->parse_args( $this->type, array( 'class' => $classes, 'options' => $this->concat_items( array( 'name' => $this->_name() . '[]', 'method' => 'list_input_checkbox', ) ), 'desc' => $this->_desc( true ), ) ); return $this->rendered( $this->ul( $args ) ); } } types/CMB2_Type_Multi_Base.php000064400000005702151717007230012220 0ustar00%s', $args['value'], selected( isset( $args['checked'] ) && $args['checked'], true, false ), $args['label'] ) . "\n"; } /** * Generates html for list item with input * * @since 1.1.0 * @param array $args Override arguments * @param int $i Iterator value * @return string Gnerated list item html */ public function list_input( $args = array(), $i = '' ) { $a = $this->parse_args( 'list_input', array( 'type' => 'radio', 'class' => 'cmb2-option', 'name' => $this->_name(), 'id' => $this->_id( $i ), 'value' => $this->field->escaped_value(), 'label' => '', ), $args ); return sprintf( "\t" . '
  • ' . "\n", $this->concat_attrs( $a, array( 'label' ) ), $a['id'], $a['label'] ); } /** * Generates html for list item with checkbox input * * @since 1.1.0 * @param array $args Override arguments * @param int $i Iterator value * @return string Gnerated list item html */ public function list_input_checkbox( $args, $i ) { $saved_value = $this->field->escaped_value(); if ( is_array( $saved_value ) && in_array( $args['value'], $saved_value ) ) { $args['checked'] = 'checked'; } $args['type'] = 'checkbox'; return $this->list_input( $args, $i ); } /** * Generates html for concatenated items * * @since 1.1.0 * @param array $args Optional arguments * @return string Concatenated html items */ public function concat_items( $args = array() ) { $field = $this->field; $method = isset( $args['method'] ) ? $args['method'] : 'select_option'; unset( $args['method'] ); $value = null !== $field->escaped_value() ? $field->escaped_value() : $field->get_default(); $value = CMB2_Utils::normalize_if_numeric( $value ); $concatenated_items = ''; $i = 1; $options = array(); if ( $option_none = $field->args( 'show_option_none' ) ) { $options[''] = $option_none; } $options = $options + (array) $field->options(); foreach ( $options as $opt_value => $opt_label ) { // Clone args & modify for just this item $a = $args; $a['value'] = $opt_value; $a['label'] = $opt_label; // Check if this option is the value of the input if ( $value === CMB2_Utils::normalize_if_numeric( $opt_value ) ) { $a['checked'] = 'checked'; } $concatenated_items .= $this->$method( $a, $i++ ); } return $concatenated_items; } } types/CMB2_Type_Oembed.php000064400000002011151717007230011355 0ustar00field; $meta_value = trim( $field->escaped_value() ); $oembed = ! empty( $meta_value ) ? cmb2_ajax()->get_oembed( array( 'url' => $field->escaped_value(), 'object_id' => $field->object_id, 'object_type' => $field->object_type, 'oembed_args' => array( 'width' => '640', ), 'field_id' => $this->_id( '', false ), ) ) : ''; return parent::render( array( 'class' => 'cmb2-oembed regular-text', 'data-objectid' => $field->object_id, 'data-objecttype' => $field->object_type, ) ) . '

    ' . '
    ' . $oembed . '
    '; } } types/CMB2_Type_Picker_Base.php000064400000002546151717007230012346 0ustar00field->args['attributes']) * @return array Array of field attributes */ public function parse_picker_options( $arg = 'date', $args = array() ) { $att = 'data-' . $arg . 'picker'; $update = empty( $args ); $atts = array(); $format = $this->field->args( $arg . '_format' ); if ( $js_format = CMB2_Utils::php_to_js_dateformat( $format ) ) { if ( $update ) { $atts = $this->field->args( 'attributes' ); } else { $atts = isset( $args['attributes'] ) ? $args['attributes'] : $atts; } // Don't override user-provided datepicker values $data = isset( $atts[ $att ] ) ? json_decode( $atts[ $att ], true ) : array(); $data[ $arg . 'Format' ] = $js_format; $atts[ $att ] = function_exists( 'wp_json_encode' ) ? wp_json_encode( $data ) : json_encode( $data ); } if ( $update ) { $this->field->args['attributes'] = $atts; } return array_merge( $args, $atts ); } } types/CMB2_Type_Radio.php000064400000002034151717007230011225 0ustar00type = $type ? $type : $this->type; } public function render() { $args = $this->parse_args( $this->type, array( 'class' => 'cmb2-radio-list cmb2-list', 'options' => $this->concat_items( array( 'label' => 'test', 'method' => 'list_input', ) ), 'desc' => $this->_desc( true ), ) ); return $this->rendered( $this->ul( $args ) ); } protected function ul( $a ) { return sprintf( '
      %s
    %s', $a['class'], $a['options'], $a['desc'] ); } } types/CMB2_Type_Select.php000064400000001237151717007230011412 0ustar00parse_args( 'select', array( 'class' => 'cmb2_select', 'name' => $this->_name(), 'id' => $this->_id(), 'desc' => $this->_desc( true ), 'options' => $this->concat_items(), ) ); $attrs = $this->concat_attrs( $a, array( 'desc', 'options' ) ); return $this->rendered( sprintf( '%s%s', $attrs, $a['options'], $a['desc'] ) ); } } types/CMB2_Type_Select_Timezone.php000064400000001227151717007230013263 0ustar00field->args['default'] = $this->field->get_default() ? $this->field->get_default() : CMB2_Utils::timezone_string(); $this->args = wp_parse_args( $this->args, array( 'class' => 'cmb2_select cmb2-select-timezone', 'options' => wp_timezone_choice( $this->field->escaped_value() ), 'desc' => $this->_desc(), ) ); return parent::render(); } } types/CMB2_Type_Taxonomy_Base.php000064400000011237151717007230012744 0ustar00field->object_type ) { case 'options-page': case 'term': return $this->options_terms(); case 'post': // WP caches internally so it's better to use return get_the_terms( $this->field->object_id, $this->field->args( 'taxonomy' ) ); default: return $this->non_post_object_terms(); } } /** * Gets the term objects for the terms stored via options boxes. * * @since 2.2.4 * @return mixed Array of terms on success */ public function options_terms() { if ( empty( $this->field->value ) ) { return array(); } $terms = (array) $this->field->value; foreach ( $terms as $index => $term ) { $terms[ $index ] = get_term_by( 'slug', $term, $this->field->args( 'taxonomy' ) ); } return $terms; } /** * For non-post objects, wraps the call to wp_get_object_terms with transient caching. * * @since 2.2.4 * @return mixed Array of terms on success */ public function non_post_object_terms() { $object_id = $this->field->object_id; $taxonomy = $this->field->args( 'taxonomy' ); $cache_key = "cmb-cache-{$taxonomy}-{$object_id}"; // Check cache $cached = get_transient( $cache_key ); if ( ! $cached ) { $cached = wp_get_object_terms( $object_id, $taxonomy ); // Do our own (minimal) caching. Long enough for a page-load. set_transient( $cache_key, $cached, 60 ); } return $cached; } /** * Wrapper for `get_terms` to account for changes in WP 4.6 where taxonomy is expected * as part of the arguments. * * @since 2.2.2 * @return mixed Array of terms on success */ public function get_terms() { $args = array( 'taxonomy' => $this->field->args( 'taxonomy' ), 'hide_empty' => false, ); if ( null !== $this->parent ) { $args['parent'] = $this->parent; } $args = wp_parse_args( $this->field->prop( 'query_args', array() ), $args ); return CMB2_Utils::wp_at_least( '4.5.0' ) ? get_terms( $args ) : get_terms( $this->field->args( 'taxonomy' ), http_build_query( $args ) ); } protected function no_terms_result( $error, $tag = 'li' ) { if ( is_wp_error( $error ) ) { $message = $error->get_error_message(); $data = 'data-error="' . esc_attr( $error->get_error_code() ) . '"'; } else { $message = $this->_text( 'no_terms_text', esc_html__( 'No terms', 'cmb2' ) ); $data = ''; } $this->field->args['select_all_button'] = false; return sprintf( '<%3$s>', $data, esc_html( $message ), $tag ); } public function get_object_term_or_default() { $saved_terms = $this->get_object_terms(); return is_wp_error( $saved_terms ) || empty( $saved_terms ) ? $this->field->get_default() : array_shift( $saved_terms )->slug; } /** * Takes a list of all tax terms and outputs. * * @since 2.2.5 * * @param array $all_terms Array of all terms. * @param array|string $saved Array of terms set to the object, or single term slug. * * @return string List of terms. */ protected function loop_terms( $all_terms, $saved_terms ) { return ''; } /** * Build children hierarchy. * * @param object $parent_term The parent term object. * @param array|string $saved Array of terms set to the object, or single term slug. * * @return string List of terms. */ protected function build_children( $parent_term, $saved ) { if ( empty( $parent_term->term_id ) ) { return ''; } $this->parent = $parent_term->term_id; $terms = $this->get_terms(); $options = ''; if ( ! empty( $terms ) && is_array( $terms ) ) { $options .= $this->child_option_output( $terms, $saved ); } return $options; } /** * Build child terms output. * * @since 2.6.1 * * @param array $terms Array of child terms. * @param array|string $saved Array of terms set to the object, or single term slug. * * @return string Child option output. */ public function child_option_output( $terms, $saved ) { $output = '
    • '; $output .= $this->loop_terms( $terms, $saved ); $output .= '
  • '; return $output; } } types/CMB2_Type_Taxonomy_Multicheck.php000064400000003421151717007230014156 0ustar00rendered( $this->types->radio( array( 'class' => $this->get_wrapper_classes(), 'options' => $this->get_term_options(), ), 'taxonomy_multicheck' ) ); } protected function get_term_options() { $all_terms = $this->get_terms(); if ( ! $all_terms || is_wp_error( $all_terms ) ) { return $this->no_terms_result( $all_terms ); } return $this->loop_terms( $all_terms, $this->get_object_term_or_default() ); } protected function loop_terms( $all_terms, $saved_terms ) { $options = ''; foreach ( $all_terms as $term ) { $options .= $this->list_term_input( $term, $saved_terms ); } return $options; } protected function list_term_input( $term, $saved_terms ) { $args = array( 'value' => $term->slug, 'label' => $term->name, 'type' => 'checkbox', 'name' => $this->_name() . '[]', ); if ( is_array( $saved_terms ) && in_array( $term->slug, $saved_terms ) ) { $args['checked'] = 'checked'; } return $this->list_input( $args, ++$this->counter ); } public function get_object_term_or_default() { $saved_terms = $this->get_object_terms(); return is_wp_error( $saved_terms ) || empty( $saved_terms ) ? $this->field->get_default() : wp_list_pluck( $saved_terms, 'slug' ); } protected function get_wrapper_classes() { $classes = 'cmb2-checkbox-list cmb2-list'; if ( false === $this->field->args( 'select_all_button' ) ) { $classes .= ' no-select-all'; } return $classes; } } types/CMB2_Type_Taxonomy_Multicheck_Hierarchical.php000064400000001620151717007230016613 0ustar00rendered( $this->types->radio( array( 'class' => $this->get_wrapper_classes(), 'options' => $this->get_term_options(), ), 'taxonomy_multicheck_hierarchical' ) ); } protected function list_term_input( $term, $saved_terms ) { $options = parent::list_term_input( $term, $saved_terms ); $children = $this->build_children( $term, $saved_terms ); if ( ! empty( $children ) ) { $options .= $children; } return $options; } } types/CMB2_Type_Taxonomy_Radio.php000064400000004222151717007230013124 0ustar00rendered( $this->types->radio( array( 'options' => $this->get_term_options(), ), 'taxonomy_radio' ) ); } protected function get_term_options() { $all_terms = $this->get_terms(); if ( ! $all_terms || is_wp_error( $all_terms ) ) { return $this->no_terms_result( $all_terms ); } $saved_term = $this->get_object_term_or_default(); $option_none = $this->field->args( 'show_option_none' ); $options = ''; if ( ! empty( $option_none ) ) { $field_id = $this->_id( '', false ); /** * Default (option-none) taxonomy-radio value. * * @since 1.3.0 * * @param string $option_none_value Default (option-none) taxonomy-radio value. */ $option_none_value = apply_filters( 'cmb2_taxonomy_radio_default_value', '' ); /** * Default (option-none) taxonomy-radio value. * * The dynamic portion of the hook name, $field_id, refers to the field id attribute. * * @since 1.3.0 * * @param string $option_none_value Default (option-none) taxonomy-radio value. */ $option_none_value = apply_filters( "cmb2_taxonomy_radio_{$field_id}_default_value", $option_none_value ); $options .= $this->list_term_input( (object) array( 'slug' => $option_none_value, 'name' => $option_none, ), $saved_term ); } $options .= $this->loop_terms( $all_terms, $saved_term ); return $options; } protected function loop_terms( $all_terms, $saved_term ) { $options = ''; foreach ( $all_terms as $term ) { $options .= $this->list_term_input( $term, $saved_term ); } return $options; } protected function list_term_input( $term, $saved_term ) { $args = array( 'value' => $term->slug, 'label' => $term->name, ); if ( $saved_term == $term->slug ) { $args['checked'] = 'checked'; } return $this->list_input( $args, ++$this->counter ); } } types/CMB2_Type_Taxonomy_Radio_Hierarchical.php000064400000001512151717007230015561 0ustar00rendered( $this->types->radio( array( 'options' => $this->get_term_options(), ), 'taxonomy_radio_hierarchical' ) ); } protected function list_term_input( $term, $saved_term ) { $options = parent::list_term_input( $term, $saved_term ); $children = $this->build_children( $term, $saved_term ); if ( ! empty( $children ) ) { $options .= $children; } return $options; } } types/CMB2_Type_Taxonomy_Select.php000064400000004407151717007230013312 0ustar00rendered( $this->types->select( array( 'options' => $this->get_term_options(), ) ) ); } protected function get_term_options() { $all_terms = $this->get_terms(); if ( ! $all_terms || is_wp_error( $all_terms ) ) { return $this->no_terms_result( $all_terms, 'strong' ); } $this->saved_term = $this->get_object_term_or_default(); $option_none = $this->field->args( 'show_option_none' ); $options = ''; if ( ! empty( $option_none ) ) { $field_id = $this->_id( '', false ); /** * Default (option-none) taxonomy-select value. * * @since 1.3.0 * * @param string $option_none_value Default (option-none) taxonomy-select value. */ $option_none_value = apply_filters( 'cmb2_taxonomy_select_default_value', '' ); /** * Default (option-none) taxonomy-select value. * * The dynamic portion of the hook name, $field_id, refers to the field id attribute. * * @since 1.3.0 * * @param string $option_none_value Default (option-none) taxonomy-select value. */ $option_none_value = apply_filters( "cmb2_taxonomy_select_{$field_id}_default_value", $option_none_value ); $options .= $this->select_option( array( 'label' => $option_none, 'value' => $option_none_value, 'checked' => $this->saved_term == $option_none_value, ) ); } $options .= $this->loop_terms( $all_terms, $this->saved_term ); return $options; } protected function loop_terms( $all_terms, $saved_term ) { $options = ''; foreach ( $all_terms as $term ) { $this->current_term = $term; $options .= $this->select_option( array( 'label' => $term->name, 'value' => $term->slug, 'checked' => $this->saved_term === $term->slug, ) ); } return $options; } } types/CMB2_Type_Taxonomy_Select_Hierarchical.php000064400000002754151717007230015753 0ustar00rendered( $this->types->select( array( 'options' => $this->get_term_options(), ), 'taxonomy_select_hierarchical' ) ); } public function select_option( $args = array() ) { if ( $this->level > 0 ) { $args['label'] = str_repeat( '    ', $this->level ) . $args['label']; } $option = parent::select_option( $args ); $children = $this->build_children( $this->current_term, $this->saved_term ); if ( ! empty( $children ) ) { $option .= $children; } return $option; } /** * Build children hierarchy. * * @since 2.6.1 * * @param array $terms Array of child terms. * @param array|string $saved Array of terms set to the object, or single term slug. * * @return string Child option output. */ public function child_option_output( $terms, $saved ) { $this->level++; $output = $this->loop_terms( $terms, $saved ); $this->level--; return $output; } } types/CMB2_Type_Text.php000064400000002601151717007230011113 0ustar00type = $type ? $type : $this->type; } /** * Handles outputting an 'input' element * * @since 1.1.0 * @param array $args Override arguments * @return string Form input element */ public function render( $args = array() ) { $args = empty( $args ) ? $this->args : $args; $a = $this->parse_args( $this->type, array( 'type' => 'text', 'class' => 'regular-text', 'name' => $this->_name(), 'id' => $this->_id(), 'value' => $this->field->escaped_value(), 'desc' => $this->_desc( true ), 'js_dependencies' => array(), ), $args ); // Add character counter? $a = $this->maybe_update_attributes_for_char_counter( $a ); return $this->rendered( sprintf( '%s', $this->concat_attrs( $a, array( 'desc' ) ), $a['desc'] ) ); } } types/CMB2_Type_Textarea.php000064400000002021151717007230011740 0ustar00args : $args; $a = $this->parse_args( 'textarea', array( 'class' => 'cmb2_textarea', 'name' => $this->_name(), 'id' => $this->_id(), 'cols' => 60, 'rows' => 10, 'value' => $this->field->escaped_value( 'esc_textarea' ), 'desc' => $this->_desc( true ), ), $args ); // Add character counter? $a = $this->maybe_update_attributes_for_char_counter( $a ); return $this->rendered( sprintf( '%s%s', $this->concat_attrs( $a, array( 'desc', 'value' ) ), $a['value'], $a['desc'] ) ); } } types/CMB2_Type_Textarea_Code.php000064400000001652151717007230012703 0ustar00 'cmb2-textarea-code', 'desc' => '' . $this->_desc( true ), ) ); if ( true !== $this->field->options( 'disable_codemirror' ) && function_exists( 'wp_enqueue_code_editor' ) ) { $args['js_dependencies'] = array( 'code-editor' ); } else { $args['class'] = rtrim( $args['class'] ) . ' disable-codemirror'; } return $this->rendered( sprintf( '
    %s', parent::render( $args ) )
    		);
    	}
    }
    types/CMB2_Type_Text_Date.php000064400000001340151717007230012047 0ustar00parse_args( 'text_date', array(
    			'class'           => 'cmb2-text-small cmb2-datepicker',
    			'value'           => $this->field->get_timestamp_format(),
    			'desc'            => $this->_desc(),
    			'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker' ),
    		) );
    
    		if ( false === strpos( $args['class'], 'timepicker' ) ) {
    			$this->parse_picker_options( 'date' );
    		}
    
    		return parent::render( $args );
    	}
    
    }
    types/CMB2_Type_Text_Datetime_Timestamp.php000064400000004526151717007230014762 0ustar00field;
    
    		$value = $field->escaped_value();
    		if ( empty( $value ) ) {
    			$value = $field->get_default();
    		}
    
    		$args = wp_parse_args( $this->args, array(
    			'value'      => $value,
    			'desc'       => $this->_desc(),
    			'datepicker' => array(),
    			'timepicker' => array(),
    		) );
    
    		if ( empty( $args['value'] ) ) {
    			$args['value'] = $value;
    			// This will be used if there is a select_timezone set for this field
    			$tz_offset = $field->field_timezone_offset();
    			if ( ! empty( $tz_offset ) ) {
    				$args['value'] -= $tz_offset;
    			}
    		}
    
    		$has_good_value = ! empty( $args['value'] ) && ! is_array( $args['value'] );
    
    		$date_input = parent::render( $this->date_args( $args, $has_good_value ) );
    		$time_input = parent::render( $this->time_args( $args, $has_good_value ) );
    
    		return $this->rendered( $date_input . "\n" . $time_input );
    	}
    
    	public function date_args( $args, $has_good_value ) {
    		$date_args = wp_parse_args( $args['datepicker'], array(
    			'class' => 'cmb2-text-small cmb2-datepicker',
    			'name'  => $this->_name( '[date]' ),
    			'id'    => $this->_id( '_date' ),
    			'value' => $has_good_value ? $this->field->get_timestamp_format( 'date_format', $args['value'] ) : '',
    			'desc'  => '',
    		) );
    
    		$date_args['rendered'] = true;
    
    		// Let's get the date-format, and set it up as a data attr for the field.
    		return $this->parse_picker_options( 'date', $date_args );
    	}
    
    	public function time_args( $args, $has_good_value ) {
    		$time_args = wp_parse_args( $args['timepicker'], array(
    			'class' => 'cmb2-timepicker text-time',
    			'name'  => $this->_name( '[time]' ),
    			'id'    => $this->_id( '_time' ),
    			'value' => $has_good_value ? $this->field->get_timestamp_format( 'time_format', $args['value'] ) : '',
    			'desc'  => $args['desc'],
    			'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ),
    		) );
    
    		$time_args['rendered'] = true;
    
    		// Let's get the time-format, and set it up as a data attr for the field.
    		return $this->parse_picker_options( 'time', $time_args );
    	}
    
    }
    types/CMB2_Type_Text_Datetime_Timestamp_Timezone.php000064400000003323151717007230016626 0ustar00field;
    
    		$value = $field->escaped_value();
    		if ( empty( $value ) ) {
    			$value = $field->get_default();
    		}
    
    		$args = wp_parse_args( $this->args, array(
    			'value'                   => $value,
    			'desc'                    => $this->_desc( true ),
    			'text_datetime_timestamp' => array(),
    			'select_timezone'         => array(),
    		) );
    
    		$args['value'] = $value;
    		if ( is_array( $args['value'] ) ) {
    			$args['value'] = '';
    		}
    
    		$datetime = CMB2_Utils::get_datetime_from_value( $args['value'] );
    		$value    = '';
    		$tzstring = '';
    
    		if ( $datetime && $datetime instanceof DateTime ) {
    			$tzstring = $datetime->getTimezone()->getName();
    			$value    = $datetime->getTimestamp();
    		}
    
    		$timestamp_args = wp_parse_args( $args['text_datetime_timestamp'], array(
    			'desc'     => '',
    			'value'    => $value,
    			'rendered' => true,
    		) );
    		$datetime_timestamp = $this->types->text_datetime_timestamp( $timestamp_args );
    
    		$timezone_select_args = wp_parse_args( $args['select_timezone'], array(
    			'class'    => 'cmb2_select cmb2-select-timezone',
    			'name'     => $this->_name( '[timezone]' ),
    			'id'       => $this->_id( '_timezone' ),
    			'options'  => wp_timezone_choice( $tzstring ),
    			'desc'     => $args['desc'],
    			'rendered' => true,
    		) );
    		$select = $this->types->select( $timezone_select_args );
    
    		return $this->rendered(
    			$datetime_timestamp . "\n" . $select
    		);
    	}
    }
    types/CMB2_Type_Text_Time.php000064400000001222151717007230012067 0ustar00args = $this->parse_picker_options( 'time', wp_parse_args( $this->args, array(
    			'class'           => 'cmb2-timepicker text-time',
    			'value'           => $this->field->get_timestamp_format( 'time_format' ),
    			'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ),
    		) ) );
    
    		return parent::render();
    	}
    
    }
    types/CMB2_Type_Title.php000064400000001745151717007230011260 0ustar00field->args( 'name' );
    		$tag  = 'span';
    
    		if ( ! empty( $name ) ) {
    			$tag = $this->field->object_type == 'post' ? 'h5' : 'h3';
    		}
    
    		$a = $this->parse_args( 'title', array(
    			'tag'   => $tag,
    			'class' => empty( $name ) ? 'cmb2-metabox-title-anchor' : 'cmb2-metabox-title',
    			'name'  => $name,
    			'desc'  => $this->_desc( true ),
    			'id'    => str_replace( '_', '-', sanitize_html_class( $this->field->id() ) ),
    		) );
    
    		return $this->rendered(
    			sprintf(
    				'<%1$s %2$s>%3$s%4$s',
    				$a['tag'],
    				$this->concat_attrs( $a, array( 'tag', 'name', 'desc' ) ),
    				$a['name'],
    				$a['desc']
    			)
    		);
    	}
    
    }
    types/CMB2_Type_Wysiwyg.php000064400000006420151717007230011654 0ustar00field;
    		$a = $this->parse_args( 'wysiwyg', array(
    			'id'      => $this->_id( '', false ),
    			'value'   => $field->escaped_value( 'stripslashes' ),
    			'desc'    => $this->_desc( true ),
    			'options' => $field->options(),
    		) );
    
    		if ( ! $field->group ) {
    
    			$a = $this->maybe_update_attributes_for_char_counter( $a );
    
    			if ( $this->has_counter ) {
    				$a['options']['editor_class'] = ! empty( $a['options']['editor_class'] )
    					? $a['options']['editor_class'] . ' cmb2-count-chars'
    					: 'cmb2-count-chars';
    			}
    
    			return $this->rendered( $this->get_wp_editor( $a ) . $a['desc'] );
    		}
    
    		// Character counter not currently working for grouped WYSIWYG
    		$this->field->args['char_counter'] = false;
    
    		// wysiwyg fields in a group need some special handling.
    		$field->add_js_dependencies( array( 'wp-util', 'cmb2-wysiwyg' ) );
    
    		// Hook in our template-output to the footer.
    		add_action( is_admin() ? 'admin_footer' : 'wp_footer', array( $this, 'add_wysiwyg_template_for_group' ) );
    
    		return $this->rendered(
    			sprintf( '
    %s', parent::render( array( 'class' => 'cmb2_textarea cmb2-wysiwyg-placeholder', 'data-groupid' => $field->group->id(), 'data-iterator' => $field->group->index, 'data-fieldid' => $field->id( true ), 'desc' => '
    ' . $this->_desc( true ), ) ) ) ); } protected function get_wp_editor( $args ) { ob_start(); wp_editor( $args['value'], $args['id'], $args['options'] ); return ob_get_clean(); } public function add_wysiwyg_template_for_group() { $group_id = $this->field->group->id(); $field_id = $this->field->id( true ); $hash = $this->field->hash_id(); $options = $this->field->options(); $options['textarea_name'] = 'cmb2_n_' . $group_id . $field_id; // Initate the editor with special id/value/name so we can retrieve the options in JS. $editor = $this->get_wp_editor( array( 'value' => 'cmb2_v_' . $group_id . $field_id, 'id' => 'cmb2_i_' . $group_id . $field_id, 'options' => $options, ) ); // Then replace the special id/value/name with underscore placeholders. $editor = str_replace( array( 'cmb2_n_' . $group_id . $field_id, 'cmb2_v_' . $group_id . $field_id, 'cmb2_i_' . $group_id . $field_id, ), array( '{{ data.name }}', '{{{ data.value }}}', '{{ data.id }}', ), $editor ); // And put the editor instance in a JS template wrapper. echo ''; } }