wp/wp-admin/edit-form-blocks.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    49 wp_enqueue_script( 'heartbeat' );
    49 wp_enqueue_script( 'heartbeat' );
    50 wp_enqueue_script( 'wp-edit-post' );
    50 wp_enqueue_script( 'wp-edit-post' );
    51 
    51 
    52 $rest_path = rest_get_route_for_post( $post );
    52 $rest_path = rest_get_route_for_post( $post );
    53 
    53 
       
    54 $active_theme                   = get_stylesheet();
       
    55 $global_styles_endpoint_context = current_user_can( 'edit_theme_options' ) ? 'edit' : 'view';
       
    56 $template_lookup_slug           = 'page' === $post->post_type ? 'page' : 'single-' . $post->post_type;
       
    57 if ( ! empty( $post->post_name ) ) {
       
    58 	$template_lookup_slug .= '-' . $post->post_name;
       
    59 }
    54 // Preload common data.
    60 // Preload common data.
    55 $preload_paths = array(
    61 $preload_paths = array(
    56 	'/wp/v2/types?context=view',
    62 	'/wp/v2/types?context=view',
    57 	'/wp/v2/taxonomies?context=view',
    63 	'/wp/v2/taxonomies?context=view',
    58 	add_query_arg(
       
    59 		array(
       
    60 			'context'  => 'edit',
       
    61 			'per_page' => -1,
       
    62 		),
       
    63 		rest_get_route_for_post_type_items( 'wp_block' )
       
    64 	),
       
    65 	add_query_arg( 'context', 'edit', $rest_path ),
    64 	add_query_arg( 'context', 'edit', $rest_path ),
    66 	sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
    65 	sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
    67 	'/wp/v2/users/me',
    66 	'/wp/v2/users/me',
    68 	array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
    67 	array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
    69 	array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
    68 	array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
    70 	array( rest_get_route_for_post_type_items( 'wp_block' ), 'OPTIONS' ),
    69 	array( rest_get_route_for_post_type_items( 'wp_block' ), 'OPTIONS' ),
    71 	array( rest_get_route_for_post_type_items( 'wp_template' ), 'OPTIONS' ),
    70 	array( rest_get_route_for_post_type_items( 'wp_template' ), 'OPTIONS' ),
    72 	sprintf( '%s/autosaves?context=edit', $rest_path ),
    71 	sprintf( '%s/autosaves?context=edit', $rest_path ),
    73 	'/wp/v2/settings',
    72 	'/wp/v2/settings',
    74 	array( '/wp/v2/settings', 'OPTIONS' ),
    73 	array( '/wp/v2/settings', 'OPTIONS' ),
    75 	'/wp/v2/global-styles/themes/' . get_stylesheet(),
    74 	'/wp/v2/global-styles/themes/' . $active_theme . '?context=view',
       
    75 	'/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view',
    76 	'/wp/v2/themes?context=edit&status=active',
    76 	'/wp/v2/themes?context=edit&status=active',
    77 	'/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id() . '?context=edit',
    77 	array( '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id(), 'OPTIONS' ),
       
    78 	/*
       
    79 	 * Preload the global styles path with the correct context based on user caps.
       
    80 	 * NOTE: There is an equivalent conditional check in the client-side code to fetch
       
    81 	 * the global styles entity using the appropriate context value.
       
    82 	 * See the call to `canUser()`, under `useGlobalStylesUserConfig()` in `packages/edit-site/src/components/use-global-styles-user-config/index.js`.
       
    83 	 * Please ensure that the equivalent check is kept in sync with this preload path.
       
    84 	 */
       
    85 	'/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id() . '?context=' . $global_styles_endpoint_context,
       
    86 	// Used by getBlockPatternCategories in useBlockEditorSettings.
       
    87 	'/wp/v2/block-patterns/categories',
       
    88 	// @see packages/core-data/src/entities.js
       
    89 	'/?_fields=' . implode(
       
    90 		',',
       
    91 		array(
       
    92 			'description',
       
    93 			'gmt_offset',
       
    94 			'home',
       
    95 			'name',
       
    96 			'site_icon',
       
    97 			'site_icon_url',
       
    98 			'site_logo',
       
    99 			'timezone_string',
       
   100 			'url',
       
   101 			'page_for_posts',
       
   102 			'page_on_front',
       
   103 			'show_on_front',
       
   104 		)
       
   105 	),
       
   106 	$paths[] = add_query_arg(
       
   107 		'slug',
       
   108 		// @see https://github.com/WordPress/gutenberg/blob/e093fefd041eb6cc4a4e7f67b92ab54fd75c8858/packages/core-data/src/private-selectors.ts#L244-L254
       
   109 		$template_lookup_slug,
       
   110 		'/wp/v2/templates/lookup'
       
   111 	),
    78 );
   112 );
    79 
   113 
    80 block_editor_rest_api_preload( $preload_paths, $block_editor_context );
   114 block_editor_rest_api_preload( $preload_paths, $block_editor_context );
    81 
   115 
    82 wp_add_inline_script(
   116 wp_add_inline_script(
   110 // Preload server-registered block schemas.
   144 // Preload server-registered block schemas.
   111 wp_add_inline_script(
   145 wp_add_inline_script(
   112 	'wp-blocks',
   146 	'wp-blocks',
   113 	'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
   147 	'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
   114 );
   148 );
       
   149 
       
   150 // Preload server-registered block bindings sources.
       
   151 $registered_sources = get_all_registered_block_bindings_sources();
       
   152 if ( ! empty( $registered_sources ) ) {
       
   153 	$filtered_sources = array();
       
   154 	foreach ( $registered_sources as $source ) {
       
   155 		$filtered_sources[] = array(
       
   156 			'name'        => $source->name,
       
   157 			'label'       => $source->label,
       
   158 			'usesContext' => $source->uses_context,
       
   159 		);
       
   160 	}
       
   161 	$script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
       
   162 	wp_add_inline_script(
       
   163 		'wp-blocks',
       
   164 		$script
       
   165 	);
       
   166 }
   115 
   167 
   116 // Get admin url for handling meta boxes.
   168 // Get admin url for handling meta boxes.
   117 $meta_box_url = admin_url( 'post.php' );
   169 $meta_box_url = admin_url( 'post.php' );
   118 $meta_box_url = add_query_arg(
   170 $meta_box_url = add_query_arg(
   119 	array(
   171 	array(
   128 	'wp-editor',
   180 	'wp-editor',
   129 	sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
   181 	sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
   130 	'before'
   182 	'before'
   131 );
   183 );
   132 
   184 
       
   185 // Set Heartbeat interval to 10 seconds, used to refresh post locks.
       
   186 wp_add_inline_script(
       
   187 	'heartbeat',
       
   188 	'jQuery( function() {
       
   189 		wp.heartbeat.interval( 10 );
       
   190 	} );',
       
   191 	'after'
       
   192 );
       
   193 
   133 /*
   194 /*
   134  * Get all available templates for the post/page attributes meta-box.
   195  * Get all available templates for the post/page attributes meta-box.
   135  * The "Default template" array element should only be added if the array is
   196  * The "Default template" array element should only be added if the array is
   136  * not empty so we do not trigger the template select element without any options
   197  * not empty so we do not trigger the template select element without any options
   137  * besides the default value.
   198  * besides the default value.
   157 
   218 
   158 	$user_details = null;
   219 	$user_details = null;
   159 	if ( $locked ) {
   220 	if ( $locked ) {
   160 		$user         = get_userdata( $user_id );
   221 		$user         = get_userdata( $user_id );
   161 		$user_details = array(
   222 		$user_details = array(
   162 			'avatar' => get_avatar_url( $user_id, array( 'size' => 128 ) ),
   223 			'name' => $user->display_name,
   163 			'name'   => $user->display_name,
       
   164 		);
   224 		);
       
   225 
       
   226 		if ( get_option( 'show_avatars' ) ) {
       
   227 			$user_details['avatar'] = get_avatar_url( $user_id, array( 'size' => 128 ) );
       
   228 		}
   165 	}
   229 	}
   166 
   230 
   167 	$lock_details = array(
   231 	$lock_details = array(
   168 		'isLocked' => $locked,
   232 		'isLocked' => $locked,
   169 		'user'     => $user_details,
   233 		'user'     => $user_details,