post_value() final public method

During a save request prior to save, post_value() provides the new value while value() does not.
Since: 3.4.0
final public post_value ( mixed $default = null ) : mixed
$default mixed A default value which is used as a fallback. Default is null.
return mixed The default value on failure, otherwise the sanitized and validated value.
示例#1
0
 /**
  * Save data in special option for better performance in query
  *
  * @param \WP_Customize_Setting $setting
  */
 protected function _save_post_id($setting)
 {
     Posts::update($setting->id, $setting->post_value());
 }
示例#2
0
/**
 * Preview changes made to a nav menu.
 *
 * Filters nav menu display to show customized items in the customized order.
 *
 * @since Menu Customizer 0.0
 *
 * @param array                $value   Array of the menu items to preview, in order.
 * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
 */
function menu_customizer_preview_nav_menu($setting)
{
    $menu_id = str_replace('nav_menu_', '', $setting->id);
    // Ensure that $menu_id is valid.
    $menu_id = (int) $menu_id;
    $menu = wp_get_nav_menu_object($menu_id);
    if (!$menu || !$menu_id) {
        return new WP_Error('invalid_menu_id', __('Invalid menu ID.'));
    }
    if (is_wp_error($menu)) {
        return $menu;
    }
    $menu_id = $menu->term_id;
    // @todo don't use a closure for PHP 5.2
    add_filter('wp_get_nav_menu_items', function ($items, $menu, $args) use($menu_id, $setting) {
        $preview_menu_id = $menu->term_id;
        if ($menu_id == $preview_menu_id) {
            $new_ids = $setting->post_value();
            $new_items = array();
            $i = 0;
            // For each item, get object and update menu order property.
            foreach ($new_ids as $item_id) {
                $item = get_post($item_id);
                $item = wp_setup_nav_menu_item($item);
                $item->menu_order = $i;
                $new_items[] = $item;
                $i++;
            }
            return $new_items;
        } else {
            return $items;
        }
    }, 10, 3);
}
 /**
  * @param WP_Customize_Setting $setting
  */
 function custom_type_preview($setting)
 {
     $previewed_value = $setting->post_value($this->undefined);
     if ($this->undefined !== $previewed_value) {
         $this->custom_type_data_previewed[$setting->id] = $previewed_value;
     }
 }
 /**
  * Publish the auto-draft posts that were created for nav menu items.
  *
  * The post IDs will have been sanitized by already by
  * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to
  * remove any post IDs for which the user cannot publish or for which the
  * post is not an auto-draft.
  *
  * @since 4.7.0
  * @access public
  *
  * @param WP_Customize_Setting $setting Customizer setting object.
  */
 public function save_nav_menus_created_posts($setting)
 {
     $post_ids = $setting->post_value();
     if (!empty($post_ids)) {
         foreach ($post_ids as $post_id) {
             $target_status = 'attachment' === get_post_type($post_id) ? 'inherit' : 'publish';
             $args = array('ID' => $post_id, 'post_status' => $target_status);
             $post_name = get_post_meta($post_id, '_customize_draft_post_name', true);
             if ($post_name) {
                 $args['post_name'] = $post_name;
             }
             // Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
             wp_update_post(wp_slash($args));
             delete_post_meta($post_id, '_customize_draft_post_name');
         }
     }
 }
 /**
  * Publish the auto-draft posts that were created for nav menu items.
  *
  * The post IDs will have been sanitized by already by
  * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to
  * remove any post IDs for which the user cannot publish or for which the
  * post is not an auto-draft.
  *
  * @since 4.7.0
  * @access public
  *
  * @param WP_Customize_Setting $setting Customizer setting object.
  */
 public function save_nav_menus_created_posts($setting)
 {
     $post_ids = $setting->post_value();
     if (!empty($post_ids)) {
         foreach ($post_ids as $post_id) {
             wp_publish_post($post_id);
         }
     }
 }
 /**
  * Publish the auto-draft posts that were created for nav menu items.
  *
  * The post IDs will have been sanitized by already by
  * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to
  * remove any post IDs for which the user cannot publish or for which the
  * post is not an auto-draft.
  *
  * @since 4.7.0
  * @access public
  *
  * @param WP_Customize_Setting $setting Customizer setting object.
  */
 public function save_nav_menus_created_posts($setting)
 {
     $post_ids = $setting->post_value();
     if (!empty($post_ids)) {
         foreach ($post_ids as $post_id) {
             // Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
             wp_update_post(array('ID' => $post_id, 'post_status' => 'publish'));
         }
     }
 }
 function set_wp2android_theme_menu($wp_customize)
 {
     $setting = new WP_Customize_Setting($wp_customize, 'wp2android_theme_menu', array('default' => ''));
     $menu = $setting->post_value();
     $wp_customize->add_setting($setting);
     if (!empty($menu)) {
         wp2android_plugin_menus()->setMenu($menu);
     }
 }
 /**
  * Publish the auto-draft posts that were created for nav menu items.
  *
  * The post IDs will have been sanitized by already by
  * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to
  * remove any post IDs for which the user cannot publish or for which the
  * post is not an auto-draft.
  *
  * @since 4.7.0
  * @access public
  *
  * @param WP_Customize_Setting $setting Customizer setting object.
  */
 public function save_nav_menus_created_posts($setting)
 {
     $post_ids = $setting->post_value();
     if (!empty($post_ids)) {
         foreach ($post_ids as $post_id) {
             $target_status = 'attachment' === get_post_type($post_id) ? 'inherit' : 'publish';
             // Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
             wp_update_post(array('ID' => $post_id, 'post_status' => $target_status));
         }
     }
 }