• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / WP_Customize_Manager::import_theme_starter_content() – Import theme starter content into the customized state.

You appear to be a bot. Output may be restricted

Description

Import theme starter content into the customized state.

Usage

WP_Customize_Manager::import_theme_starter_content( $starter_content );

Parameters

$starter_content
( array ) optional – Starter content. Defaults to `get_theme_starter_content()`.

Returns

void

Source

File name: wordpress/wp-includes/class-wp-customize-manager.php
Lines:

1 to 100 of 445
  function import_theme_starter_content( $starter_content = array() ) {
    if ( empty( $starter_content ) ) {
      $starter_content = get_theme_starter_content();
    }

    $changeset_data = array();
    if ( $this->WP_Customize_Manager::changeset_post_id() ) {
      /*
			 * Don't re-import starter content into a changeset saved persistently.
			 * This will need to be revisited in the future once theme switching
			 * is allowed with drafted/scheduled changesets, since switching to
			 * another theme could result in more starter content being applied.
			 * However, when doing an explicit save it is currently possible for
			 * nav menus and nav menu items specifically to lose their starter_content
			 * flags, thus resulting in duplicates being created since they fail
			 * to get re-used. See #40146.
			 */
      if ( 'auto-draft' !== get_post_status( $this->WP_Customize_Manager::changeset_post_id() ) ) {
        return;
      }

      $changeset_data = $this->WP_Customize_Manager::get_changeset_post_data( $this->WP_Customize_Manager::changeset_post_id() );
    }

    $sidebars_widgets = isset( $starter_content['widgets'] ) && ! empty( $this->widgets ) ? $starter_content['widgets'] : array();
    $attachments      = isset( $starter_content['attachments'] ) && ! empty( $this->nav_menus ) ? $starter_content['attachments'] : array();
    $posts            = isset( $starter_content['posts'] ) && ! empty( $this->nav_menus ) ? $starter_content['posts'] : array();
    $options          = isset( $starter_content['options'] ) ? $starter_content['options'] : array();
    $nav_menus        = isset( $starter_content['nav_menus'] ) && ! empty( $this->nav_menus ) ? $starter_content['nav_menus'] : array();
    $theme_mods       = isset( $starter_content['theme_mods'] ) ? $starter_content['theme_mods'] : array();

    // Widgets.
    $max_widget_numbers = array();
    foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
      $sidebar_widget_ids = array();
      foreach ( $widgets as $widget ) {
        list( $id_base, $instance ) = $widget;

        if ( ! isset( $max_widget_numbers[ $id_base ] ) ) {

          // When $settings is an array-like object, get an intrinsic array for use with array_keys().
          $settings = get_option( "widget_{$id_base}", array() );
          if ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) {
            $settings = $settings->getArrayCopy();
          }

          unset( $settings['_multiwidget'] );

          // Find the max widget number for this type.
          $widget_numbers = array_keys( $settings );
          if ( count( $widget_numbers ) > 0 ) {
            $widget_numbers[]               = 1;
            $max_widget_numbers[ $id_base ] = max( ...$widget_numbers );
          } else {
            $max_widget_numbers[ $id_base ] = 1;
          }
        }
        $max_widget_numbers[ $id_base ] += 1;

        $widget_id  = sprintf( '%s-%d', $id_base, $max_widget_numbers[ $id_base ] );
        $setting_id = sprintf( 'widget_%s[%d]', $id_base, $max_widget_numbers[ $id_base ] );

        $setting_value = $this->widgets->sanitize_widget_js_instance( $instance );
        if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
          $this->WP_Customize_Manager::set_post_value( $setting_id, $setting_value );
          $this->pending_starter_content_settings_ids[] = $setting_id;
        }
        $sidebar_widget_ids[] = $widget_id;
      }

      $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id );
      if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
        $this->WP_Customize_Manager::set_post_value( $setting_id, $sidebar_widget_ids );
        $this->pending_starter_content_settings_ids[] = $setting_id;
      }
    }

    $starter_content_auto_draft_post_ids = array();
    if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) {
      $starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_ids, $changeset_data['nav_menus_created_posts']['value'] );
    }

    // Make an index of all the posts needed and what their slugs are.
    $needed_posts = array();
    $attachments  = $this->WP_Customize_Manager::prepare_starter_content_attachments( $attachments );
    foreach ( $attachments as $attachment ) {
      $key                  = 'attachment:' . $attachment['post_name'];
      $needed_posts[ $key ] = true;
    }
    foreach ( array_keys( $posts ) as $post_symbol ) {
      if ( empty( $posts[ $post_symbol ]['post_name'] ) && empty( $posts[ $post_symbol ]['post_title'] ) ) {
        unset( $posts[ $post_symbol ] );
        continue;
      }
      if ( empty( $posts[ $post_symbol ]['post_name'] ) ) {
        $posts[ $post_symbol ]['post_name'] = sanitize_title( $posts[ $post_symbol ]['post_title'] );
      }
      if ( empty( $posts[ $post_symbol ]['post_type'] ) ) {
        $posts[ $post_symbol ]['post_type'] = 'post';
      }
 
[1] [2] [3] … [5] Next »

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: WP_Customize_Manager::import_theme_starter_content
Class ref: WP_Customize_Manager
Plugin ref: WordPress
Version: 5.7.1
Sourcefile: wp-includes/class-wp-customize-manager.php
File ref: wp-includes/class-wp-customize-manager.php
Deprecated?: No
API Letters: C,I,M,T,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.7.1
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites