diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
index 9f5e9a6..cdd9bd6 100644
|
|
|
final class WP_Customize_Manager { |
| 523 | 523 | } |
| 524 | 524 | } |
| 525 | 525 | |
| 526 | | // Import theme starter content for fresh installs when landing in the customizer and no existing changeset loaded. |
| 527 | | if ( get_option( 'fresh_site' ) && 'customize.php' === $pagenow && ! $this->changeset_post_id() ) { |
| | 526 | /* |
| | 527 | * Import theme starter content for fresh installs when landing in the customizer. |
| | 528 | * Import starter content at after_setup_theme:100 so that any |
| | 529 | * add_theme_support( 'starter-content' ) calls will have been made. |
| | 530 | */ |
| | 531 | if ( get_option( 'fresh_site' ) && 'customize.php' === $pagenow ) { |
| 528 | 532 | add_action( 'after_setup_theme', array( $this, 'import_theme_starter_content' ), 100 ); |
| 529 | 533 | } |
| 530 | 534 | |
| … |
… |
final class WP_Customize_Manager { |
| 885 | 889 | } |
| 886 | 890 | |
| 887 | 891 | /** |
| 888 | | * Import theme starter content into post values. |
| | 892 | * Starter content setting IDs. |
| | 893 | * |
| | 894 | * @since 4.7.0 |
| | 895 | * @access private |
| | 896 | * @var array |
| | 897 | */ |
| | 898 | protected $starter_content_settings_ids = array(); |
| | 899 | |
| | 900 | /** |
| | 901 | * Import theme starter content into the customized state. |
| 889 | 902 | * |
| 890 | 903 | * @since 4.7.0 |
| 891 | 904 | * @access public |
| … |
… |
final class WP_Customize_Manager { |
| 897 | 910 | $starter_content = get_theme_starter_content(); |
| 898 | 911 | } |
| 899 | 912 | |
| | 913 | $changeset_data = array(); |
| | 914 | if ( $this->changeset_post_id() ) { |
| | 915 | $changeset_data = $this->get_changeset_post_data( $this->changeset_post_id() ); |
| | 916 | } |
| | 917 | |
| 900 | 918 | $sidebars_widgets = isset( $starter_content['widgets'] ) && ! empty( $this->widgets ) ? $starter_content['widgets'] : array(); |
| 901 | 919 | $posts = isset( $starter_content['posts'] ) && ! empty( $this->nav_menus ) ? $starter_content['posts'] : array(); |
| 902 | 920 | $options = isset( $starter_content['options'] ) ? $starter_content['options'] : array(); |
| … |
… |
final class WP_Customize_Manager { |
| 932 | 950 | $widget_id = sprintf( '%s-%d', $id_base, $max_widget_numbers[ $id_base ] ); |
| 933 | 951 | $setting_id = sprintf( 'widget_%s[%d]', $id_base, $max_widget_numbers[ $id_base ] ); |
| 934 | 952 | |
| 935 | | $class = 'WP_Customize_Setting'; |
| 936 | | |
| 937 | | /** This filter is documented in wp-includes/class-wp-customize-manager.php */ |
| 938 | | $args = apply_filters( 'customize_dynamic_setting_args', false, $setting_id ); |
| 939 | | |
| 940 | | if ( false !== $args ) { |
| 941 | | |
| 942 | | /** This filter is documented in wp-includes/class-wp-customize-manager.php */ |
| 943 | | $class = apply_filters( 'customize_dynamic_setting_class', $class, $setting_id, $args ); |
| 944 | | |
| 945 | | $setting = new $class( $this, $setting_id, $args ); |
| 946 | | $setting_value = call_user_func( $setting->sanitize_js_callback, $instance, $setting ); |
| | 953 | $setting_value = $this->widgets->sanitize_widget_js_instance( $instance ); |
| | 954 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| 947 | 955 | $this->set_post_value( $setting_id, $setting_value ); |
| 948 | | $sidebar_widget_ids[] = $widget_id; |
| | 956 | $this->starter_content_settings_ids[] = $setting_id; |
| 949 | 957 | } |
| | 958 | $sidebar_widget_ids[] = $widget_id; |
| 950 | 959 | } |
| 951 | 960 | |
| 952 | | $this->set_post_value( sprintf( 'sidebars_widgets[%s]', $sidebar_id ), $sidebar_widget_ids ); |
| | 961 | $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id ); |
| | 962 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| | 963 | $this->set_post_value( $setting_id, $sidebar_widget_ids ); |
| | 964 | $this->starter_content_settings_ids[] = $setting_id; |
| | 965 | } |
| 953 | 966 | } |
| 954 | 967 | |
| 955 | 968 | // Posts & pages. |
| 956 | 969 | if ( ! empty( $posts ) ) { |
| | 970 | $nav_menus_created_posts = array(); |
| | 971 | if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) { |
| | 972 | $nav_menus_created_posts = $changeset_data['nav_menus_created_posts']['value']; |
| | 973 | } |
| | 974 | |
| | 975 | $existing_posts = array(); |
| | 976 | if ( ! empty( $nav_menus_created_posts ) ) { |
| | 977 | $existing_posts_query = new WP_Query( array( |
| | 978 | 'post__in' => $nav_menus_created_posts, |
| | 979 | 'post_status' => 'auto-draft', |
| | 980 | 'post_type' => 'any', |
| | 981 | 'number' => -1, |
| | 982 | ) ); |
| | 983 | foreach ( $existing_posts_query->posts as $existing_post ) { |
| | 984 | $existing_posts[ $existing_post->post_type . ':' . $existing_post->post_name ] = $existing_post; |
| | 985 | } |
| | 986 | } |
| | 987 | |
| 957 | 988 | foreach ( array_keys( $posts ) as $post_symbol ) { |
| | 989 | if ( empty( $posts[ $post_symbol ]['post_type'] ) ) { |
| | 990 | continue; |
| | 991 | } |
| | 992 | $post_type = $posts[ $post_symbol ]['post_type']; |
| | 993 | if ( ! empty( $posts[ $post_symbol ]['post_name'] ) ) { |
| | 994 | $post_name = $posts[ $post_symbol ]['post_name']; |
| | 995 | } elseif ( ! empty( $posts[ $post_symbol ]['post_title'] ) ) { |
| | 996 | $post_name = sanitize_title( $posts[ $post_symbol ]['post_title'] ); |
| | 997 | } else { |
| | 998 | continue; |
| | 999 | } |
| | 1000 | |
| | 1001 | // Use existing auto-draft post if one already exists with the same type and name. |
| | 1002 | if ( isset( $existing_posts[ $post_type . ':' . $post_name ] ) ) { |
| | 1003 | $posts[ $post_symbol ] = $existing_posts[ $post_type . ':' . $post_name ]->ID; |
| | 1004 | continue; |
| | 1005 | } |
| | 1006 | |
| 958 | 1007 | $r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] ); |
| 959 | 1008 | if ( $r instanceof WP_Post ) { |
| 960 | 1009 | $posts[ $post_symbol ]['ID'] = $r->ID; |
| 961 | 1010 | } |
| 962 | 1011 | } |
| 963 | | $this->set_post_value( 'nav_menus_created_posts', wp_list_pluck( $posts, 'ID' ) ); // This is why nav_menus component is dependency for adding posts. |
| | 1012 | |
| | 1013 | // The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts. |
| | 1014 | $setting_id = 'nav_menus_created_posts'; |
| | 1015 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| | 1016 | $nav_menus_created_posts = array_unique( array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) ) ); |
| | 1017 | $this->set_post_value( $setting_id, $nav_menus_created_posts ); |
| | 1018 | $this->starter_content_settings_ids[] = $setting_id; |
| | 1019 | } |
| 964 | 1020 | } |
| 965 | 1021 | |
| 966 | 1022 | // Nav menus. |
| … |
… |
final class WP_Customize_Manager { |
| 968 | 1024 | foreach ( $nav_menus as $nav_menu_location => $nav_menu ) { |
| 969 | 1025 | $nav_menu_term_id = $placeholder_id--; |
| 970 | 1026 | $nav_menu_setting_id = sprintf( 'nav_menu[%d]', $nav_menu_term_id ); |
| 971 | | $this->set_post_value( $nav_menu_setting_id, array( |
| 972 | | 'name' => isset( $nav_menu['name'] ) ? $nav_menu['name'] : $nav_menu_location, |
| 973 | | ) ); |
| | 1027 | |
| | 1028 | |
| | 1029 | if ( empty( $changeset_data[ $nav_menu_setting_id ] ) || ! empty( $changeset_data[ $nav_menu_setting_id ]['starter_content'] ) ) { |
| | 1030 | $this->set_post_value( $nav_menu_setting_id, array( |
| | 1031 | 'name' => isset( $nav_menu['name'] ) ? $nav_menu['name'] : $nav_menu_location, |
| | 1032 | ) ); |
| | 1033 | $this->starter_content_settings_ids[] = $nav_menu_setting_id; |
| | 1034 | } |
| 974 | 1035 | |
| 975 | 1036 | // @todo Add support for menu_item_parent. |
| 976 | 1037 | $position = 0; |
| … |
… |
final class WP_Customize_Manager { |
| 994 | 1055 | } else { |
| 995 | 1056 | $nav_menu_item['object_id'] = 0; |
| 996 | 1057 | } |
| 997 | | $this->set_post_value( $nav_menu_item_setting_id, $nav_menu_item ); |
| | 1058 | |
| | 1059 | if ( empty( $changeset_data[ $nav_menu_item_setting_id ] ) || ! empty( $changeset_data[ $nav_menu_item_setting_id ]['starter_content'] ) ) { |
| | 1060 | $this->set_post_value( $nav_menu_item_setting_id, $nav_menu_item ); |
| | 1061 | $this->starter_content_settings_ids[] = $nav_menu_item_setting_id; |
| | 1062 | } |
| 998 | 1063 | } |
| 999 | 1064 | |
| 1000 | | $this->set_post_value( sprintf( 'nav_menu_locations[%s]', $nav_menu_location ), $nav_menu_term_id ); |
| | 1065 | $setting_id = sprintf( 'nav_menu_locations[%s]', $nav_menu_location ); |
| | 1066 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| | 1067 | $this->set_post_value( $setting_id, $nav_menu_term_id ); |
| | 1068 | $this->starter_content_settings_ids[] = $setting_id; |
| | 1069 | } |
| 1001 | 1070 | } |
| 1002 | 1071 | |
| 1003 | 1072 | // Options. |
| … |
… |
final class WP_Customize_Manager { |
| 1005 | 1074 | if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) { |
| 1006 | 1075 | $value = $posts[ $matches['symbol'] ]['ID']; |
| 1007 | 1076 | } |
| 1008 | | $this->set_post_value( $name, $value ); |
| | 1077 | |
| | 1078 | if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) { |
| | 1079 | $this->set_post_value( $name, $value ); |
| | 1080 | $this->starter_content_settings_ids[] = $name; |
| | 1081 | } |
| 1009 | 1082 | } |
| 1010 | 1083 | |
| 1011 | 1084 | // Theme mods. |
| … |
… |
final class WP_Customize_Manager { |
| 1013 | 1086 | if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) { |
| 1014 | 1087 | $value = $posts[ $matches['symbol'] ]['ID']; |
| 1015 | 1088 | } |
| 1016 | | $this->set_post_value( $name, $value ); |
| | 1089 | |
| | 1090 | if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) { |
| | 1091 | $this->set_post_value( $name, $value ); |
| | 1092 | $this->starter_content_settings_ids[] = $name; |
| | 1093 | } |
| | 1094 | } |
| | 1095 | |
| | 1096 | if ( ! empty( $this->starter_content_settings_ids ) ) { |
| | 1097 | if ( did_action( 'customize_register' ) ) { |
| | 1098 | $this->_save_starter_content_changeset(); |
| | 1099 | } else { |
| | 1100 | add_action( 'customize_register', array( $this, '_save_starter_content_changeset' ), 1000 ); |
| | 1101 | } |
| 1017 | 1102 | } |
| 1018 | 1103 | } |
| 1019 | 1104 | |
| 1020 | 1105 | /** |
| | 1106 | * Save starter content changeset. |
| | 1107 | * |
| | 1108 | * @since 4.7.0 |
| | 1109 | * @access private |
| | 1110 | */ |
| | 1111 | public function _save_starter_content_changeset() { |
| | 1112 | |
| | 1113 | if ( empty( $this->starter_content_settings_ids ) ) { |
| | 1114 | return; |
| | 1115 | } |
| | 1116 | |
| | 1117 | $this->save_changeset_post( array( |
| | 1118 | 'data' => array_fill_keys( $this->starter_content_settings_ids, array( 'starter_content' => true ) ), |
| | 1119 | 'starter_content' => true, |
| | 1120 | ) ); |
| | 1121 | } |
| | 1122 | |
| | 1123 | /** |
| 1021 | 1124 | * Get dirty pre-sanitized setting values in the current customized state. |
| 1022 | 1125 | * |
| 1023 | 1126 | * The returned array consists of a merge of three sources: |
| … |
… |
final class WP_Customize_Manager { |
| 1823 | 1926 | * @param array $args { |
| 1824 | 1927 | * Args for changeset post. |
| 1825 | 1928 | * |
| 1826 | | * @type array $data Optional additional changeset data. Values will be merged on top of any existing post values. |
| 1827 | | * @type string $status Post status. Optional. If supplied, the save will be transactional and a post revision will be allowed. |
| 1828 | | * @type string $title Post title. Optional. |
| 1829 | | * @type string $date_gmt Date in GMT. Optional. |
| 1830 | | * @type int $user_id ID for user who is saving the changeset. Optional, defaults to the current user ID. |
| | 1929 | * @type array $data Optional additional changeset data. Values will be merged on top of any existing post values. |
| | 1930 | * @type string $status Post status. Optional. If supplied, the save will be transactional and a post revision will be allowed. |
| | 1931 | * @type string $title Post title. Optional. |
| | 1932 | * @type string $date_gmt Date in GMT. Optional. |
| | 1933 | * @type int $user_id ID for user who is saving the changeset. Optional, defaults to the current user ID. |
| | 1934 | * @type bool $starter_content Whether the data is starter content. If false (default), then $starter_content will be cleared for any $data being saved. |
| 1831 | 1935 | * } |
| 1832 | 1936 | * |
| 1833 | 1937 | * @return array|WP_Error Returns array on success and WP_Error with array data on error. |
| … |
… |
final class WP_Customize_Manager { |
| 1841 | 1945 | 'data' => array(), |
| 1842 | 1946 | 'date_gmt' => null, |
| 1843 | 1947 | 'user_id' => get_current_user_id(), |
| | 1948 | 'starter_content' => false, |
| 1844 | 1949 | ), |
| 1845 | 1950 | $args |
| 1846 | 1951 | ); |
| … |
… |
final class WP_Customize_Manager { |
| 1977 | 2082 | if ( ! isset( $data[ $changeset_setting_id ] ) ) { |
| 1978 | 2083 | $data[ $changeset_setting_id ] = array(); |
| 1979 | 2084 | } |
| | 2085 | |
| 1980 | 2086 | $data[ $changeset_setting_id ] = array_merge( |
| 1981 | 2087 | $data[ $changeset_setting_id ], |
| 1982 | 2088 | $setting_params, |
| … |
… |
final class WP_Customize_Manager { |
| 1985 | 2091 | 'user_id' => $args['user_id'], |
| 1986 | 2092 | ) |
| 1987 | 2093 | ); |
| | 2094 | |
| | 2095 | // Clear starter_content flag in data if changeset is not explicitly being updated for starter content. |
| | 2096 | if ( empty( $args['starter_content'] ) ) { |
| | 2097 | unset( $data[ $changeset_setting_id ]['starter_content'] ); |
| | 2098 | } |
| 1988 | 2099 | } |
| 1989 | 2100 | } |
| 1990 | 2101 | |