/** * Get data before saving to CMB. */ public function intercept_post_id() { // Check for $_POST data if (empty($_POST)) { return false; } // Check nonce if (!(isset($_POST['submit-cmb'], $_POST['wp_meta_box_nonce']) && wp_verify_nonce($_POST['wp_meta_box_nonce'], cmb_Meta_Box::nonce()))) { return; } // Setup and sanitize data if (isset($_POST[$this->prefix . 'place_name'])) { $this->new_submission = wp_insert_post(array('post_title' => sanitize_text_field($_POST[$this->prefix . 'place_name']), 'post_author' => get_current_user_id(), 'post_status' => 'draft', 'post_type' => 'accommodations', 'post_content' => wp_kses($_POST[$this->prefix . 'place_notes'], '<b><strong><i><em><h1><h2><h3><h4><h5><h6><pre><code><span>')), true); // If no errors, save the data into a new post draft if (!is_wp_error($this->new_submission)) { $address = sanitize_text_field($_POST['address']); $lat = sanitize_text_field($_POST['lat']); $lng = sanitize_text_field($_POST['lng']); $formatted_address = sanitize_text_field($_POST['formatted_address']); // Update the meta field in the database. update_post_meta($this->new_submission, 'address', $address); update_post_meta($this->new_submission, 'lat', $lat); update_post_meta($this->new_submission, 'lng', $lng); update_post_meta($this->new_submission, 'formatted_address', $formatted_address); update_post_meta($this->new_submission, 'place_image_id', $_POST['place_image_id']); //update post parent in place_image_id $image = array('ID' => get_post_meta($this->new_submission, 'place_image_id', 1), 'post_parent' => $this->new_submission); wp_update_post($image); set_post_thumbnail($this->new_submission, get_post_meta($this->new_submission, 'place_image_id', 1)); return $this->new_submission; } } return false; }
/** * Handles saving of the $_POST data * @since 0.1.3 * @param int $term_id Term's ID */ public function do_save($term_id) { if (!class_exists('cmb_Meta_Box')) { return; } if (!isset($_POST['term_opt_name'], $_POST['wp_meta_box_nonce'], $_POST['action']) || !wp_verify_nonce($_POST['wp_meta_box_nonce'], cmb_Meta_Box::nonce())) { return; } $this->do_override_filters($term_id); // Save the metabox if it's been submitted cmb_save_metabox_fields($this->fields(), $this->id()); }
/** * Add metaboxes for an specific Page Template * @since 1.0.0 * @param bool $display To display or not * @param array $meta_box Metabox config array * @return bool Whether to display this metabox on the current page. */ public static function check_page_template($display, $meta_box) { if (!isset($meta_box['show_on']['key']) || 'page-template' !== $meta_box['show_on']['key']) { return $display; } $object_id = cmb_Meta_Box::get_object_id(); if (!$object_id || cmb_Meta_Box::get_object_type() !== 'post') { 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) $meta_box['show_on']['value'])) { return true; } return false; }
/** * 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 static function hijack_oembed_cache_set($check, $object_id, $meta_key, $meta_value) { if (!self::$hijack || self::$object_id != $object_id && 1987645321 !== $object_id) { return $check; } // Cache the result to our metadata if ('options-page' === self::$object_type) { // Set the option cmb_Meta_Box::update_option(self::$object_id, self::$embed_args['cache_key'], $meta_value, array('type' => 'oembed')); // Save the option cmb_Meta_Box::save_option(self::$object_id); } else { update_metadata(self::$object_type, self::$object_id, $meta_key, $meta_value); } // Anything other than `null` to cancel saving to postmeta return true; }
/** * Get data before saving to CMB. */ public function intercept_post_id() { // Check for $_POST data if (empty($_POST)) { return false; } // Check nonce if (!(isset($_POST['submit-cmb'], $_POST['wp_meta_box_nonce']) && wp_verify_nonce($_POST['wp_meta_box_nonce'], cmb_Meta_Box::nonce()))) { return; } // Setup and sanitize data if (isset($_POST[$this->prefix . 'memorial_first_name'])) { add_filter('user_has_cap', array($this, 'grant_publish_caps'), 0, 3); $this->new_submission = wp_insert_post(array('post_title' => sanitize_text_field($_POST[$this->prefix . 'memorial_first_name'] . ' ' . $_POST[$this->prefix . 'memorial_last_name']), 'post_author' => get_current_user_id(), 'post_status' => 'draft', 'post_type' => 'memorials', 'post_content_filtered' => wp_kses($_POST[$this->prefix . 'memorial_story'], '<b><strong><i><em><h1><h2><h3><h4><h5><h6><pre><code><span>')), true); // If no errors, save the data into a new post draft if (!is_wp_error($this->new_submission)) { return $this->new_submission; } } return false; }
public function file() { $meta_value = $this->field->escaped_value(); $allow = $this->field->args('allow'); $input_type = 'url' == $allow || is_array($allow) && in_array('url', $allow) ? 'text' : 'hidden'; echo $this->input(array('type' => $input_type, 'class' => 'cmb_upload_file', 'size' => 45, 'desc' => '')), '<input class="cmb_upload_button button" type="button" value="' . __('Add or Upload File', 'cmb') . '" />', $this->_desc(true); $cached_id = $this->_id(); // Reset field args for attachment ID $args = $this->field->args(); $args['id'] = $args['_id'] . '_id'; unset($args['_id'], $args['_name']); // And get new field object $this->field = new cmb_Meta_Box_field($args, $this->field->group); // Get ID value $_id_value = $this->field->escaped_value('absint'); // If there is no ID saved yet, try to get it from the url if ($meta_value && !$_id_value) { $_id_value = cmb_Meta_Box::image_id_from_url(esc_url_raw($meta_value)); } echo $this->input(array('type' => 'hidden', 'class' => 'cmb_upload_file_id', 'value' => $_id_value, 'desc' => '')), '<div id="', $this->_id('_status'), '" class="cmb_media_status">'; if (!empty($meta_value)) { if ($this->is_valid_img_ext($meta_value)) { echo '<div class="img_status">'; echo '<img style="max-width: 350px; width: 100%; height: auto;" src="', $meta_value, '" alt="" />'; echo '<p class="cmb_remove_wrapper"><a href="#" class="cmb_remove_file_button" rel="', $cached_id, '">' . __('Remove Image', 'cmb') . '</a></p>'; echo '</div>'; } else { // $file_ext = $this->get_file_ext( $meta_value ); $parts = explode('/', $meta_value); for ($i = 0; $i < count($parts); ++$i) { $title = $parts[$i]; } echo __('File:', 'cmb'), ' <strong>', $title, '</strong> (<a href="', $meta_value, '" target="_blank" rel="external">' . __('Download', 'cmb') . '</a> / <a href="#" class="cmb_remove_file_button" rel="', $cached_id, '">' . __('Remove', 'cmb') . '</a>)'; } } echo '</div>'; }
/** * Offset a time value based on timezone * @since 1.0.0 * @return string Offset time string */ public function field_timezone_offset() { return cmb_Meta_Box::timezone_offset($this->field_timezone()); }
/** * Sets the object type * @since 1.0.0 * @return string Object type */ public static function set_object_type($object_type) { return self::$object_type = $object_type; }
public static function file($field, $meta, $object_id, $object_type) { $input_type_url = 'hidden'; if ('url' == $field['allow'] || is_array($field['allow']) && in_array('url', $field['allow'])) { $input_type_url = 'text'; } echo '<input class="cmb_upload_file" type="' . $input_type_url . '" size="45" id="', $field['id'], '" name="', $field['id'], '" value="', $meta, '" />'; echo '<input class="cmb_upload_button button" type="button" value="' . __('Add or Upload File', 'cmb') . '" />'; $_id_name = $field['id'] . '_id'; $_id_meta = get_metadata($object_type, $object_id, $_id_name, !isset($field['multiple']) || !$field['multiple']); // If there is no ID saved yet, try to get it from the url if ($meta && !$_id_meta) { $_id_meta = cmb_Meta_Box::image_id_from_url(esc_url_raw($meta)); } echo '<input class="cmb_upload_file_id" type="hidden" id="', $_id_name, '" name="', $_id_name, '" value="', $_id_meta, '" />', self::desc($field['desc'], true), '<div id="', $field['id'], '_status" class="cmb_media_status">'; if (!empty($meta)) { if (self::is_valid_img_ext($meta)) { echo '<div class="img_status">'; echo '<img style="max-width: 350px; width: 100%; height: auto;" src="', $meta, '" alt="" />'; echo '<p><a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">' . __('Remove Image', 'cmb') . '</a></p>'; echo '</div>'; } else { // $file_ext = self::get_file_ext( $meta ); $parts = explode('/', $meta); for ($i = 0; $i < count($parts); ++$i) { $title = $parts[$i]; } echo __('File:', 'cmb'), ' <strong>', $title, '</strong> (<a href="', $meta, '" target="_blank" rel="external">' . __('Download', 'cmb') . '</a> / <a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">' . __('Remove', 'cmb') . '</a>)'; } } echo '</div>'; }
/** * Saves the cached oEmbed value to relevant object metadata (vs postmeta) * * @since 1.3.0 * @param string $meta_key Postmeta's key * @param mixed $meta_value Value of the postmeta to be saved */ public static function oembed_cache_set($meta_key, $meta_value) { // Cache the result to our metadata if ('options-page' === self::$object_type) { // Set the option cmb_Meta_Box::update_option(self::$object_id, self::$embed_args['cache_key'], $meta_value, array('type' => 'oembed')); // Save the option cmb_Meta_Box::save_option(self::$object_id); } else { update_metadata(self::$object_type, self::$object_id, $meta_key, $meta_value); } }
/** * Peforms saving of `file` attachement's ID * @since 1.1.0 * @param string $value File url */ public function _save_file_id($value) { $group = $this->field->group; $args = $this->field->args(); $args['id'] = $args['_id'] . '_id'; unset($args['_id'], $args['_name']); // And get new field object $field = new cmb_Meta_Box_field($args, $group); $id_key = $field->_id(); $id_val_old = $field->escaped_value('absint'); if ($group) { // Check group $_POST data $i = $group->index; $base_id = $group->_id(); $id_val = isset($_POST[$base_id][$i][$id_key]) ? absint($_POST[$base_id][$i][$id_key]) : 0; } else { // Check standard $_POST data $id_val = isset($_POST[$field->id()]) ? $_POST[$field->id()] : null; } // If there is no ID saved yet, try to get it from the url if ($value && !$id_val) { $id_val = cmb_Meta_Box::image_id_from_url($value); } if ($group) { return array('attach_id' => $id_val, 'field_id' => $id_key); } if ($id_val && $id_val != $id_val_old) { return $field->update_data($id_val); } elseif (empty($id_val) && $id_val_old) { return $field->remove_data($old); } }
/** * Default fallback sanitization method. Applies filters. * @since 1.0.2 * @param mixed $meta_value Meta value * @param array $field Field config array */ public static function default_sanitization($meta_value, $field) { $object_type = cmb_Meta_Box::get_object_type(); $object_id = cmb_Meta_Box::get_object_id(); // Allow field type validation via filter $updated = apply_filters('cmb_validate_' . $field['type'], null, $meta_value, $object_id, $field, $object_type); if (null != $updated) { return $updated; } // we'll fallback to 'sanitize_text_field', or 'wp_kses_post` switch ($field['type']) { case 'wysiwyg': // $cb = 'wp_kses'; // break; // $cb = 'wp_kses'; // break; case 'textarea_small': $cb = array('cmb_Meta_Box_Sanitize', 'textarea'); break; default: $cb = 'sanitize_text_field'; break; } // Handle repeatable fields array if (is_array($meta_value)) { foreach ($meta_value as $key => $value) { $meta_value[$key] = call_user_func($cb, $value); } } else { $meta_value = call_user_func($cb, $meta_value); } return $meta_value; }
/** * Loops through and saves field data * @since 1.0.0 * * @param array $meta_box Metabox config array * @param int $object_id Object ID * @param string $object_type Type of object being saved. (e.g., post, user, or comment) */ public static function save_fields($meta_box, $object_id, $object_type = '') { $meta_box = self::set_mb_defaults($meta_box); $meta_box['show_on'] = empty($meta_box['show_on']) ? array('key' => false, 'value' => false) : $meta_box['show_on']; self::set_object_id($object_id); // Set/get type $object_type = self::set_object_type($object_type ? $object_type : self::set_mb_type($meta_box)); if (!apply_filters('cmb_show_on', true, $meta_box)) { return; } // save field ids of those that are updated self::$updated = array(); foreach ($meta_box['fields'] as $field_args) { if ('group' == $field_args['type']) { self::save_group($field_args); } else { // Save default fields $field = new cmb_Meta_Box_field($field_args); self::save_field(self::sanitize_field($field), $field); } } // If options page, save the updated options if ($object_type == 'options-page') { self::save_option($object_id); } do_action("cmb_save_{$object_type}_fields", $object_id, $meta_box['id'], self::$updated, $meta_box); }
/** * Default fallback 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 */ public function __call($name, $arguments) { list($meta_value, $field) = $arguments; // Handle repeatable fields array if (is_array($meta_value)) { foreach ($meta_value as $key => $value) { // Allow field type validation via filter $updated = apply_filters('cmb_validate_' . $field['type'], $value, cmb_Meta_Box::get_object_id(), $field, cmb_Meta_Box::get_object_type()); if ($updated === $value) { // If nothing changed, we'll fallback to 'sanitize_text_field' $updated = sanitize_text_field($value); } $meta_value[$key] = $updated; } } else { switch ($field['type']) { case 'wysiwyg': case 'textarea_small': return self::textarea($meta_value); default: // Allow field type validation via filter $updated = apply_filters('cmb_validate_' . $field['type'], $meta_value, cmb_Meta_Box::get_object_id(), $field, cmb_Meta_Box::get_object_type()); if ($updated === $meta_value) { // If nothing changed, we'll fallback to 'sanitize_text_field' return sanitize_text_field($meta_value); } return $updated; } } return $meta_value; }
/** * Datetime to imestamp with timezone * @since 1.0.1 * @param string $value Meta value * @return string Timestring */ public function text_datetime_timestamp_timezone($value, $repeat = false) { $test = is_array($value) ? array_filter($value) : ''; if (empty($test)) { return ''; } if ($repeat_value = $this->_check_repeat($value, __FUNCTION__, $repeat)) { return $repeat_value; } $tzstring = null; if (is_array($value) && array_key_exists('timezone', $value)) { $tzstring = $value['timezone']; } if (empty($tzstring)) { $tzstring = cmb_Meta_Box::timezone_string(); } $offset = cmb_Meta_Box::timezone_offset($tzstring, true); if (substr($tzstring, 0, 3) === 'UTC') { $tzstring = timezone_name_from_abbr('', $offset, 0); } $value = new DateTime($value['date'] . ' ' . $value['time'], new DateTimeZone($tzstring)); $value = serialize($value); return $value; }