diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
index 150b123..fbb4b30 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 | // Import theme starter content for fresh installs when landing in the customizer. |
| 527 | if ( get_option( 'fresh_site' ) && 'customize.php' === $pagenow ) { |
528 | 528 | add_action( 'after_setup_theme', array( $this, 'import_theme_starter_content' ), 100 ); |
529 | 529 | } |
530 | 530 | |
… |
… |
final class WP_Customize_Manager { |
885 | 885 | } |
886 | 886 | |
887 | 887 | /** |
| 888 | * Starter content setting IDs. |
| 889 | * |
| 890 | * @since 4.7.0 |
| 891 | * @access private |
| 892 | * @var array |
| 893 | */ |
| 894 | protected $starter_content_settings_ids = array(); |
| 895 | |
| 896 | /** |
888 | 897 | * Import theme starter content into post values. |
889 | 898 | * |
890 | 899 | * @since 4.7.0 |
… |
… |
final class WP_Customize_Manager { |
897 | 906 | $starter_content = get_theme_starter_content(); |
898 | 907 | } |
899 | 908 | |
| 909 | $changeset_data = array(); |
| 910 | if ( $this->changeset_post_id() ) { |
| 911 | $changeset_data = $this->get_changeset_post_data( $this->changeset_post_id() ); |
| 912 | } |
| 913 | |
900 | 914 | $sidebars_widgets = isset( $starter_content['widgets'] ) && ! empty( $this->widgets ) ? $starter_content['widgets'] : array(); |
901 | 915 | $posts = isset( $starter_content['posts'] ) && ! empty( $this->nav_menus ) ? $starter_content['posts'] : array(); |
902 | 916 | $options = isset( $starter_content['options'] ) ? $starter_content['options'] : array(); |
… |
… |
final class WP_Customize_Manager { |
944 | 958 | |
945 | 959 | $setting = new $class( $this, $setting_id, $args ); |
946 | 960 | $setting_value = call_user_func( $setting->sanitize_js_callback, $instance, $setting ); |
947 | | $this->set_post_value( $setting_id, $setting_value ); |
| 961 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| 962 | $this->set_post_value( $setting_id, $setting_value ); // $changeset_data[ $setting_id ]['value'] = $setting_value; |
| 963 | $this->starter_content_settings_ids[] = $setting_id; |
| 964 | } |
948 | 965 | $sidebar_widget_ids[] = $widget_id; |
949 | 966 | } |
950 | 967 | } |
951 | 968 | |
952 | | $this->set_post_value( sprintf( 'sidebars_widgets[%s]', $sidebar_id ), $sidebar_widget_ids ); |
| 969 | $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id ); |
| 970 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| 971 | $this->set_post_value( $setting_id, $sidebar_widget_ids ); // $changeset_data[ $setting_id ]['value'] = $sidebar_widget_ids; |
| 972 | $this->starter_content_settings_ids[] = $setting_id; |
| 973 | } |
953 | 974 | } |
954 | 975 | |
955 | 976 | // Posts & pages. |
956 | 977 | if ( ! empty( $posts ) ) { |
| 978 | $nav_menus_created_posts = array(); |
| 979 | if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) { |
| 980 | $nav_menus_created_posts = $changeset_data['nav_menus_created_posts']['value']; |
| 981 | } |
| 982 | |
| 983 | $existing_posts = array(); |
| 984 | if ( ! empty( $nav_menus_created_posts ) ) { |
| 985 | $existing_posts_query = new WP_Query( array( |
| 986 | 'post__in' => $nav_menus_created_posts, |
| 987 | 'post_status' => 'auto-draft', |
| 988 | 'post_type' => 'any', |
| 989 | 'number' => -1, |
| 990 | ) ); |
| 991 | foreach ( $existing_posts_query->posts as $existing_post ) { |
| 992 | $existing_posts[ $existing_post->post_type . ':' . $existing_post->post_name ] = $existing_post; |
| 993 | } |
| 994 | } |
| 995 | |
957 | 996 | foreach ( array_keys( $posts ) as $post_symbol ) { |
| 997 | if ( empty( $posts[ $post_symbol ]['post_type'] ) ) { |
| 998 | continue; |
| 999 | } |
| 1000 | $post_type = $posts[ $post_symbol ]['post_type']; |
| 1001 | if ( ! empty( $posts[ $post_symbol ]['post_name'] ) ) { |
| 1002 | $post_name = $posts[ $post_symbol ]['post_name']; |
| 1003 | } elseif ( ! empty( $posts[ $post_symbol ]['post_title'] ) ) { |
| 1004 | $post_name = sanitize_title( $posts[ $post_symbol ]['post_title'] ); |
| 1005 | } else { |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
| 1009 | // Use existing auto-draft post if one already exists with the same type and name. |
| 1010 | if ( isset( $existing_posts[ $post_type . ':' . $post_name ] ) ) { |
| 1011 | $posts[ $post_symbol ] = $existing_posts[ $post_type . ':' . $post_name ]->ID; |
| 1012 | continue; |
| 1013 | } |
| 1014 | |
958 | 1015 | $r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] ); |
959 | 1016 | if ( $r instanceof WP_Post ) { |
960 | 1017 | $posts[ $post_symbol ]['ID'] = $r->ID; |
961 | 1018 | } |
962 | 1019 | } |
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. |
| 1020 | |
| 1021 | // The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts. |
| 1022 | $setting_id = 'nav_menus_created_posts'; |
| 1023 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| 1024 | $nav_menus_created_posts = array_unique( array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) ) ); |
| 1025 | $this->set_post_value( $setting_id, $nav_menus_created_posts ); // $changeset_data[ $setting_id ]['value'] = $nav_menus_created_posts; |
| 1026 | $this->starter_content_settings_ids[] = $setting_id; |
| 1027 | } |
964 | 1028 | } |
965 | 1029 | |
966 | 1030 | // Nav menus. |
… |
… |
final class WP_Customize_Manager { |
968 | 1032 | foreach ( $nav_menus as $nav_menu_location => $nav_menu ) { |
969 | 1033 | $nav_menu_term_id = $placeholder_id--; |
970 | 1034 | $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 | | ) ); |
| 1035 | |
| 1036 | |
| 1037 | if ( empty( $changeset_data[ $nav_menu_setting_id ] ) || ! empty( $changeset_data[ $nav_menu_setting_id ]['starter_content'] ) ) { |
| 1038 | $this->set_post_value( $nav_menu_setting_id, array( |
| 1039 | 'name' => isset( $nav_menu['name'] ) ? $nav_menu['name'] : $nav_menu_location, |
| 1040 | ) ); // $changeset_data[ $nav_menu_setting_id ]['value'] |
| 1041 | $this->starter_content_settings_ids[] = $nav_menu_setting_id; |
| 1042 | } |
974 | 1043 | |
975 | 1044 | // @todo Add support for menu_item_parent. |
976 | 1045 | $position = 0; |
… |
… |
final class WP_Customize_Manager { |
994 | 1063 | } else { |
995 | 1064 | $nav_menu_item['object_id'] = 0; |
996 | 1065 | } |
997 | | $this->set_post_value( $nav_menu_item_setting_id, $nav_menu_item ); |
| 1066 | |
| 1067 | if ( empty( $changeset_data[ $nav_menu_item_setting_id ] ) || ! empty( $changeset_data[ $nav_menu_item_setting_id ]['starter_content'] ) ) { |
| 1068 | $this->set_post_value( $nav_menu_item_setting_id, $nav_menu_item ); // $changeset_data[ $nav_menu_item_setting_id ]['value'] = $nav_menu_item; |
| 1069 | $this->starter_content_settings_ids[] = $nav_menu_item_setting_id; |
| 1070 | } |
998 | 1071 | } |
999 | 1072 | |
1000 | | $this->set_post_value( sprintf( 'nav_menu_locations[%s]', $nav_menu_location ), $nav_menu_term_id ); |
| 1073 | $setting_id = sprintf( 'nav_menu_locations[%s]', $nav_menu_location ); |
| 1074 | if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) { |
| 1075 | $this->set_post_value( $setting_id, $nav_menu_term_id ); // $changeset_data[ $setting_id ]['value'] = $nav_menu_term_id; |
| 1076 | $this->starter_content_settings_ids[] = $setting_id; |
| 1077 | } |
1001 | 1078 | } |
1002 | 1079 | |
1003 | 1080 | // Options. |
… |
… |
final class WP_Customize_Manager { |
1005 | 1082 | if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) { |
1006 | 1083 | $value = $posts[ $matches['symbol'] ]['ID']; |
1007 | 1084 | } |
1008 | | $this->set_post_value( $name, $value ); |
| 1085 | |
| 1086 | if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) { |
| 1087 | $this->set_post_value( $name, $value ); //$changeset_data[ $name ]['value'] = $value; |
| 1088 | $this->starter_content_settings_ids[] = $name; |
| 1089 | } |
1009 | 1090 | } |
1010 | 1091 | |
1011 | 1092 | // Theme mods. |
… |
… |
final class WP_Customize_Manager { |
1013 | 1094 | if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) { |
1014 | 1095 | $value = $posts[ $matches['symbol'] ]['ID']; |
1015 | 1096 | } |
1016 | | $this->set_post_value( $name, $value ); |
| 1097 | |
| 1098 | if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) { |
| 1099 | $this->set_post_value( $name, $value ); // $changeset_data[ $name ]['value'] = $value; |
| 1100 | $this->starter_content_settings_ids[] = $name; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | if ( ! empty( $this->starter_content_settings_ids ) ) { |
| 1105 | if ( did_action( 'customize_register' ) ) { |
| 1106 | $this->_save_starter_content_changeset(); |
| 1107 | } else { |
| 1108 | add_action( 'customize_register', array( $this, '_save_starter_content_changeset' ), 1000 ); |
| 1109 | } |
1017 | 1110 | } |
1018 | 1111 | } |
1019 | 1112 | |
1020 | 1113 | /** |
| 1114 | * Save starter content changeset. |
| 1115 | * |
| 1116 | * @since 4.7.0 |
| 1117 | * @access private |
| 1118 | */ |
| 1119 | public function _save_starter_content_changeset() { |
| 1120 | |
| 1121 | if ( empty( $this->starter_content_settings_ids ) ) { |
| 1122 | return; |
| 1123 | } |
| 1124 | |
| 1125 | $this->save_changeset_post( array( |
| 1126 | 'data' => array_fill_keys( $this->starter_content_settings_ids, array( 'starter_content' => true ) ), |
| 1127 | 'starter_content' => true, |
| 1128 | ) ); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
1021 | 1132 | * Get dirty pre-sanitized setting values in the current customized state. |
1022 | 1133 | * |
1023 | 1134 | * The returned array consists of a merge of three sources: |
… |
… |
final class WP_Customize_Manager { |
1823 | 1934 | * @param array $args { |
1824 | 1935 | * Args for changeset post. |
1825 | 1936 | * |
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. |
| 1937 | * @type array $data Optional additional changeset data. Values will be merged on top of any existing post values. |
| 1938 | * @type string $status Post status. Optional. If supplied, the save will be transactional and a post revision will be allowed. |
| 1939 | * @type string $title Post title. Optional. |
| 1940 | * @type string $date_gmt Date in GMT. Optional. |
| 1941 | * @type int $user_id ID for user who is saving the changeset. Optional, defaults to the current user ID. |
| 1942 | * @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 | 1943 | * } |
1832 | 1944 | * |
1833 | 1945 | * @return array|WP_Error Returns array on success and WP_Error with array data on error. |
… |
… |
final class WP_Customize_Manager { |
1841 | 1953 | 'data' => array(), |
1842 | 1954 | 'date_gmt' => null, |
1843 | 1955 | 'user_id' => get_current_user_id(), |
| 1956 | 'starter_content' => false, |
1844 | 1957 | ), |
1845 | 1958 | $args |
1846 | 1959 | ); |
… |
… |
final class WP_Customize_Manager { |
1977 | 2090 | if ( ! isset( $data[ $changeset_setting_id ] ) ) { |
1978 | 2091 | $data[ $changeset_setting_id ] = array(); |
1979 | 2092 | } |
| 2093 | |
1980 | 2094 | $data[ $changeset_setting_id ] = array_merge( |
1981 | 2095 | $data[ $changeset_setting_id ], |
1982 | 2096 | $setting_params, |
… |
… |
final class WP_Customize_Manager { |
1985 | 2099 | 'user_id' => $args['user_id'], |
1986 | 2100 | ) |
1987 | 2101 | ); |
| 2102 | |
| 2103 | // Clear starter_content flag in data if changeset is not explicitly being updated for starter content. |
| 2104 | if ( empty( $args['starter_content'] ) ) { |
| 2105 | unset( $data[ $changeset_setting_id ]['starter_content'] ); |
| 2106 | } |
1988 | 2107 | } |
1989 | 2108 | } |
1990 | 2109 | |