Skip to content

Commit

Permalink
Set auto_insert field to correct value
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jul 26, 2023
1 parent 4eab92c commit a8dd375
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions lib/experimental/auto-inserting-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ function gutenberg_auto_insert_block( $inserted_block, $relative_position, $anch
};
}

function gutenberg_add_auto_insert_field_to_block_type_controller( $inserted_block, $position, $anchor_block ) {

Check failure on line 59 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing doc comment for function gutenberg_add_auto_insert_field_to_block_type_controller()
return function( $response, $block_type ) use ( $inserted_block, $position, $anchor_block ){

Check failure on line 60 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space before opening brace; found 0

Check failure on line 60 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Space between opening control structure and closing parenthesis is required
if ( $block_type->name !== $inserted_block ) {
return $response;
}

$data = $response->get_data();
$data['auto_insert'] = array( $anchor_block => $position );
$response->set_data( $data );
return $response;
};
}

/**
* Register blocks for auto-insertion, based on their block.json metadata.
*
Expand Down Expand Up @@ -127,16 +140,19 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) {
* @return void
*/
function gutenberg_register_auto_inserted_block( $inserted_block, $position, $anchor_block ) {
$inserted_block = array(
$inserted_block_array = array(
'blockName' => $inserted_block,
'attrs' => array(),
'innerHTML' => '',
'innerContent' => array(),
'innerBlocks' => array(),
);

$inserter = gutenberg_auto_insert_block( $inserted_block, $position, $anchor_block );
$inserter = gutenberg_auto_insert_block( $inserted_block_array, $position, $anchor_block );
add_filter( 'gutenberg_serialize_block', $inserter, 10, 1 );

$controller_extender = gutenberg_add_auto_insert_field_to_block_type_controller( $inserted_block, $position, $anchor_block );
add_filter( 'rest_prepare_block_type', $controller_extender, 10, 2 );
}

/**
Expand Down Expand Up @@ -252,25 +268,9 @@ function gutenberg_register_auto_insert_rest_field() {
array(
'schema' => array(

Check warning on line 269 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 1 space(s) between "'schema'" and double arrow, but found 7.
'description' => __( 'Auto Insert.', 'default' ),
'type' => 'string',
'type' => 'object',
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_auto_insert_rest_field' );

/**
* Add the `auto_insert` value into the API response.
*
* @since 6.4.0 Added 'auto_insert' property.
*
* @param WP_REST_Response $response The response object.
* @param WP_Block_Type $block_type The block type object.
*/
function filter_block_type_response( $response, $block_type ) {
$data = $response->get_data();
$data['auto_insert'] = 'Test';
$response->set_data( $data );
return $response;
}
add_filter( 'rest_prepare_block_type', 'filter_block_type_response', 10, 2 );

0 comments on commit a8dd375

Please sign in to comment.