本文整理匯總了PHP中WP_Customize_Manager::add_dynamic_settings方法的典型用法代碼示例。如果您正苦於以下問題:PHP WP_Customize_Manager::add_dynamic_settings方法的具體用法?PHP WP_Customize_Manager::add_dynamic_settings怎麽用?PHP WP_Customize_Manager::add_dynamic_settings使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類WP_Customize_Manager
的用法示例。
在下文中一共展示了WP_Customize_Manager::add_dynamic_settings方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: register_settings
/**
* Inspect the incoming customized data for any widget settings, and dynamically add them up-front so widgets will be initialized properly.
*
* @since 4.2.0
* @access public
*/
public function register_settings() {
$widget_setting_ids = array();
$incoming_setting_ids = array_keys( $this->manager->unsanitized_post_values() );
foreach ( $incoming_setting_ids as $setting_id ) {
if ( ! is_null( $this->get_setting_type( $setting_id ) ) ) {
$widget_setting_ids[] = $setting_id;
}
}
if ( $this->manager->doing_ajax( 'update-widget' ) && isset( $_REQUEST['widget-id'] ) ) {
$widget_setting_ids[] = $this->get_setting_id( wp_unslash( $_REQUEST['widget-id'] ) );
}
$settings = $this->manager->add_dynamic_settings( array_unique( $widget_setting_ids ) );
/*
* Preview settings right away so that widgets and sidebars will get registered properly.
* But don't do this if a customize_save because this will cause WP to think there is nothing
* changed that needs to be saved.
*/
if ( ! $this->manager->doing_ajax( 'customize_save' ) ) {
foreach ( $settings as $setting ) {
$setting->preview();
}
}
}
示例2: _save
private function _save()
{
$post_values = $this->_customize->unsanitized_post_values();
$key = '_' . static::KEY;
if (isset($post_values[$key])) {
$this->_customize->add_dynamic_settings(array_map('sanitize_key', $post_values[$key]));
}
}
示例3: save_settings_with_publish_snapshot
/**
* Publish snapshot changes when snapshot post is being published.
*
* The logic in here is the inverse of to publish_snapshot_with_customize_save_after.
*
* The meat of the logic that manipulates the post_content and validates the settings
* needs to be done in wp_insert_post_data filter in like a
* filter_insert_post_data_to_validate_published_snapshot method? This would
* have the benefit of reducing one wp_insert_post() call.
*
* @todo Consider using wp_insert_post_data to prevent double calls to wp_insert_post().
* @see Customize_Snapshot_Manager::publish_snapshot_with_customize_save_after()
*
* @param string $new_status New status.
* @param string $old_status Old status.
* @param \WP_Post $post Post object.
* @return bool Whether the settings were saved.
*/
public function save_settings_with_publish_snapshot($new_status, $old_status, $post)
{
// Abort if not transitioning a snapshot post to publish from a non-publish status.
if (Post_Type::SLUG !== $post->post_type || 'publish' !== $new_status || $new_status === $old_status) {
return false;
}
$this->ensure_customize_manager();
if ($this->doing_customize_save_ajax()) {
// Short circuit because customize_save ajax call is changing status.
return false;
}
if (!did_action('customize_register')) {
/*
* When running from CLI or Cron, we have to remove the action because
* it will get added with a default priority of 10, after themes and plugins
* have already done add_action( 'customize_register' ), resulting in them
* being called first at the priority 10. So we manually call the
* prerequisite function WP_Customize_Manager::register_controls() and
* remove it from being called when the customize_register action fires.
*/
remove_action('customize_register', array($this->customize_manager, 'register_controls'));
$this->customize_manager->register_controls();
/*
* Unfortunate hack to prevent \WP_Customize_Widgets::customize_register()
* from calling preview() on settings. This needs to be cleaned up in core.
* It is important for previewing to be prevented because if an option has
* a filter it will short-circuit when an update is attempted since it
* detects that there is no change to be put into the DB.
* See: https://github.com/xwp/wordpress-develop/blob/e8c58c47db1421a1d0b2afa9ad4b9eb9e1e338e0/src/wp-includes/class-wp-customize-widgets.php#L208-L217
*/
if (!defined('DOING_AJAX')) {
define('DOING_AJAX', true);
}
$_REQUEST['action'] = 'customize_save';
/** This action is documented in wp-includes/class-wp-customize-manager.php */
do_action('customize_register', $this->customize_manager);
// undefine( 'DOING_AJAX' )... just kidding. This is the end of the unfortunate hack and it should be fixed in Core.
unset($_REQUEST['action']);
}
$snapshot_content = $this->post_type->get_post_content($post);
if (method_exists($this->customize_manager, 'validate_setting_values')) {
/** This action is documented in wp-includes/class-wp-customize-manager.php */
do_action('customize_save_validation_before', $this->customize_manager);
}
$setting_ids = array_keys($snapshot_content);
$this->customize_manager->add_dynamic_settings($setting_ids);
/** This action is documented in wp-includes/class-wp-customize-manager.php */
do_action('customize_save', $this->customize_manager);
/**
* Settings to save.
*
* @var \WP_Customize_Setting[]
*/
$settings = array();
$publish_error_count = 0;
foreach ($snapshot_content as $setting_id => &$setting_params) {
// Missing value error.
if (!isset($setting_params['value']) || is_null($setting_params['value'])) {
if (!is_array($setting_params)) {
if (!empty($setting_params)) {
$setting_params = array('value' => $setting_params);
} else {
$setting_params = array();
}
}
$setting_params['publish_error'] = 'null_value';
$publish_error_count += 1;
continue;
}
// Unrecognized setting error.
$this->customize_manager->set_post_value($setting_id, $setting_params['value']);
$setting = $this->customize_manager->get_setting($setting_id);
if (!$setting instanceof \WP_Customize_Setting) {
$setting_params['publish_error'] = 'unrecognized_setting';
$publish_error_count += 1;
continue;
}
// Validate setting value.
if (method_exists($setting, 'validate')) {
$validity = $setting->validate($setting_params['value']);
if (is_wp_error($validity)) {
$setting_params['publish_error'] = $validity->get_error_code();
//.........這裏部分代碼省略.........
示例4: customize_register
/**
* Add the customizer settings and controls.
*
* @since 4.3.0
* @access public
*/
public function customize_register()
{
// Preview settings for nav menus early so that the sections and controls will be added properly.
$nav_menus_setting_ids = array();
foreach (array_keys($this->manager->unsanitized_post_values()) as $setting_id) {
if (preg_match('/^(nav_menu_locations|nav_menu|nav_menu_item)\\[/', $setting_id)) {
$nav_menus_setting_ids[] = $setting_id;
}
}
$this->manager->add_dynamic_settings($nav_menus_setting_ids);
if (!$this->manager->doing_ajax('customize_save')) {
foreach ($nav_menus_setting_ids as $setting_id) {
$setting = $this->manager->get_setting($setting_id);
if ($setting) {
$setting->preview();
}
}
}
// Require JS-rendered control types.
$this->manager->register_panel_type('WP_Customize_Nav_Menus_Panel');
$this->manager->register_control_type('WP_Customize_Nav_Menu_Control');
$this->manager->register_control_type('WP_Customize_Nav_Menu_Name_Control');
$this->manager->register_control_type('WP_Customize_Nav_Menu_Auto_Add_Control');
$this->manager->register_control_type('WP_Customize_Nav_Menu_Item_Control');
// Create a panel for Menus.
$description = '<p>' . __('This panel is used for managing navigation menus for content you have already published on your site. You can create menus and add items for existing content such as pages, posts, categories, tags, formats, or custom links.') . '</p>';
if (current_theme_supports('widgets')) {
/* translators: URL to the widgets panel of the customizer */
$description .= '<p>' . sprintf(__('Menus can be displayed in locations defined by your theme or in <a href="%s">widget areas</a> by adding a “Custom Menu” widget.'), "javascript:wp.customize.panel( 'widgets' ).focus();") . '</p>';
} else {
$description .= '<p>' . __('Menus can be displayed in locations defined by your theme.') . '</p>';
}
$this->manager->add_panel(new WP_Customize_Nav_Menus_Panel($this->manager, 'nav_menus', array('title' => __('Menus'), 'description' => $description, 'priority' => 100)));
$menus = wp_get_nav_menus();
// Menu locations.
$locations = get_registered_nav_menus();
$num_locations = count(array_keys($locations));
if (1 == $num_locations) {
$description = '<p>' . __('Your theme supports one menu. Select which menu you would like to use.') . '</p>';
} else {
/* translators: %s: number of menu locations */
$description = '<p>' . sprintf(_n('Your theme supports %s menu. Select which menu appears in each location.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations), number_format_i18n($num_locations)) . '</p>';
}
if (current_theme_supports('widgets')) {
/* translators: URL to the widgets panel of the customizer */
$description .= '<p>' . sprintf(__('You can also place menus in <a href="%s">widget areas</a> with the “Custom Menu” widget.'), "javascript:wp.customize.panel( 'widgets' ).focus();") . '</p>';
}
$this->manager->add_section('menu_locations', array('title' => __('Menu Locations'), 'panel' => 'nav_menus', 'priority' => 5, 'description' => $description));
$choices = array('0' => __('— Select —'));
foreach ($menus as $menu) {
$choices[$menu->term_id] = wp_html_excerpt($menu->name, 40, '…');
}
foreach ($locations as $location => $description) {
$setting_id = "nav_menu_locations[{$location}]";
$setting = $this->manager->get_setting($setting_id);
if ($setting) {
$setting->transport = 'postMessage';
remove_filter("customize_sanitize_{$setting_id}", 'absint');
add_filter("customize_sanitize_{$setting_id}", array($this, 'intval_base10'));
} else {
$this->manager->add_setting($setting_id, array('sanitize_callback' => array($this, 'intval_base10'), 'theme_supports' => 'menus', 'type' => 'theme_mod', 'transport' => 'postMessage', 'default' => 0));
}
$this->manager->add_control(new WP_Customize_Nav_Menu_Location_Control($this->manager, $setting_id, array('label' => $description, 'location_id' => $location, 'section' => 'menu_locations', 'choices' => $choices)));
}
// Register each menu as a Customizer section, and add each menu item to each menu.
foreach ($menus as $menu) {
$menu_id = $menu->term_id;
// Create a section for each menu.
$section_id = 'nav_menu[' . $menu_id . ']';
$this->manager->add_section(new WP_Customize_Nav_Menu_Section($this->manager, $section_id, array('title' => html_entity_decode($menu->name, ENT_QUOTES, get_bloginfo('charset')), 'priority' => 10, 'panel' => 'nav_menus')));
$nav_menu_setting_id = 'nav_menu[' . $menu_id . ']';
$this->manager->add_setting(new WP_Customize_Nav_Menu_Setting($this->manager, $nav_menu_setting_id, array('transport' => 'postMessage')));
// Add the menu contents.
$menu_items = (array) wp_get_nav_menu_items($menu_id);
foreach (array_values($menu_items) as $i => $item) {
// Create a setting for each menu item (which doesn't actually manage data, currently).
$menu_item_setting_id = 'nav_menu_item[' . $item->ID . ']';
$value = (array) $item;
if (empty($value['post_title'])) {
$value['title'] = '';
}
$value['nav_menu_term_id'] = $menu_id;
$this->manager->add_setting(new WP_Customize_Nav_Menu_Item_Setting($this->manager, $menu_item_setting_id, array('value' => $value, 'transport' => 'postMessage')));
// Create a control for each menu item.
$this->manager->add_control(new WP_Customize_Nav_Menu_Item_Control($this->manager, $menu_item_setting_id, array('label' => $item->title, 'section' => $section_id, 'priority' => 10 + $i)));
}
// Note: other controls inside of this section get added dynamically in JS via the MenuSection.ready() function.
}
// Add the add-new-menu section and controls.
$this->manager->add_section(new WP_Customize_New_Menu_Section($this->manager, 'add_menu', array('title' => __('Add a Menu'), 'panel' => 'nav_menus', 'priority' => 999)));
$this->manager->add_control('new_menu_name', array('label' => '', 'section' => 'add_menu', 'type' => 'text', 'settings' => array(), 'input_attrs' => array('class' => 'menu-name-field', 'placeholder' => __('New menu name'))));
$this->manager->add_control(new WP_Customize_New_Menu_Control($this->manager, 'create_new_menu', array('section' => 'add_menu', 'settings' => array())));
$this->manager->add_setting(new WP_Customize_Filter_Setting($this->manager, 'nav_menus_created_posts', array('transport' => 'postMessage', 'type' => 'option', 'default' => array(), 'sanitize_callback' => array($this, 'sanitize_nav_menus_created_posts'))));
}