Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/class-pattern-builder-abstract-pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function ( $category ) {
'title' => $post->post_title,
'description' => $post->post_excerpt,
'content' => $post->post_content,
'source' => ( $post->post_type === 'pb_block' ) ? 'theme' : 'user',
'source' => ( $post->post_type === 'tbell_pattern_block' ) ? 'theme' : 'user',
'synced' => ( $metadata['wp_pattern_sync_status'][0] ?? 'synced' ) !== 'unsynced',

'blockTypes' => isset( $metadata['wp_pattern_block_types'][0] ) ? explode( ',', $metadata['wp_pattern_block_types'][0] ) : array(),
Expand Down
36 changes: 18 additions & 18 deletions includes/class-pattern-builder-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function get_patterns( WP_REST_Request $request ): WP_REST_Response {
$theme_patterns = $this->controller->get_block_patterns_from_theme_files();
$theme_patterns = array_map(
function ( $pattern ) {
$pattern_post = $this->controller->get_pb_block_post_for_pattern( $pattern );
$pattern_post = $this->controller->get_tbell_pattern_block_post_for_pattern( $pattern );
$pattern_from_post = Abstract_Pattern::from_post( $pattern_post );
// TODO: The slug doesn't survive the trip to post and back since it has to be normalized.
// so we just pull it form the original pattern and reset it here. Not sure if that is the best way to do this.
Expand All @@ -200,15 +200,15 @@ public function inject_theme_patterns( $response, $server, $request ) {
// Requesting a single pattern. Inject the synced theme pattern.
if ( preg_match( '#/wp/v2/blocks/(?P<id>\d+)#', $request->get_route(), $matches ) ) {
$block_id = intval( $matches['id'] );
$pb_block = get_post( $block_id );
if ( $pb_block && $pb_block->post_type === 'pb_block' ) {
$tbell_pattern_block = get_post( $block_id );
if ( $tbell_pattern_block && $tbell_pattern_block->post_type === 'tbell_pattern_block' ) {
// make sure the pattern has a pattern file
$pattern_file_path = $this->controller->get_pattern_filepath( Abstract_Pattern::from_post( $pb_block ) );
$pattern_file_path = $this->controller->get_pattern_filepath( Abstract_Pattern::from_post( $tbell_pattern_block ) );
if ( ! $pattern_file_path ) {
return $response; // No pattern file found, return the original response
}
$pb_block->post_name = $this->controller->format_pattern_slug_from_post( $pb_block->post_name );
$data = $this->format_pb_block_response( $pb_block, $request );
$tbell_pattern_block->post_name = $this->controller->format_pattern_slug_from_post( $tbell_pattern_block->post_name );
$data = $this->format_tbell_pattern_block_response( $tbell_pattern_block, $request );
$response = new WP_REST_Response( $data );
}
}
Expand All @@ -228,8 +228,8 @@ function ( $pattern ) {
);

foreach ( $patterns as $pattern ) {
$post = $this->controller->get_pb_block_post_for_pattern( $pattern );
$data[] = $this->format_pb_block_response( $post, $request );
$post = $this->controller->get_tbell_pattern_block_post_for_pattern( $pattern );
$data[] = $this->format_tbell_pattern_block_response( $post, $request );
}

$response->set_data( $data );
Expand All @@ -238,7 +238,7 @@ function ( $pattern ) {
return $response;
}

public function format_pb_block_response( $post, $request ) {
public function format_tbell_pattern_block_response( $post, $request ) {
$post->post_type = 'wp_block';

// Create a mock request to pass to the controller
Expand Down Expand Up @@ -277,7 +277,7 @@ public function format_pb_block_response( $post, $request ) {
*
* If the patterns are ALREADY registered, unregister them first.
* Synced patterns are registered with a reference to the post ID of their pattern.
* Unsynced patterns are registered with the content from the pb_block post.
* Unsynced patterns are registered with the content from the tbell_pattern_block post.
*/
public function register_patterns() {

Expand All @@ -287,7 +287,7 @@ public function register_patterns() {

foreach ( $patterns as $pattern ) {

$post = $this->controller->create_pb_block_post_for_pattern( $pattern );
$post = $this->controller->create_tbell_pattern_block_post_for_pattern( $pattern );

if ( $pattern_registry->is_registered( $pattern->name ) ) {
$pattern_registry->unregister( $pattern->name );
Expand Down Expand Up @@ -324,7 +324,7 @@ public function register_patterns() {

/**
*
* Filters delete calls and if the item being deleted is a 'pb_block' (theme pattern)
* Filters delete calls and if the item being deleted is a 'tbell_pattern_block' (theme pattern)
* delete the related pattern php file as well.
*
*/
Expand All @@ -337,7 +337,7 @@ function handle_hijack_block_delete( $response, $server, $request ) {
$id = intval( $matches[1] );
$post = get_post( $id );

if ( $post && $post->post_type === 'pb_block' && $request->get_method() === 'DELETE' ) {
if ( $post && $post->post_type === 'tbell_pattern_block' && $request->get_method() === 'DELETE' ) {

$deleted = wp_delete_post( $id, true );

Expand Down Expand Up @@ -369,7 +369,7 @@ function handle_hijack_block_delete( $response, $server, $request ) {

/**
*
* This filter handles additional logic when a pb_block (theme pattern) is updated.
* This filter handles additional logic when a tbell_pattern_block (theme pattern) is updated.
* It updates the pattern file as well as associated metadata for the pattern.
* Additionally it will optionally localize the content as well as import any media
* referenced by the pattern into the theme.
Expand Down Expand Up @@ -398,7 +398,7 @@ function handle_hijack_block_update($response, $handler, $request)
}
}

if ($post->post_type === 'pb_block' || $convert_user_pattern_to_theme_pattern) {
if ($post->post_type === 'tbell_pattern_block' || $convert_user_pattern_to_theme_pattern) {

// Check write permissions before allowing update
if (! current_user_can('edit_others_posts')) {
Expand All @@ -422,7 +422,7 @@ function handle_hijack_block_update($response, $handler, $request)
$pattern = Abstract_Pattern::from_post($post);

if (isset($updated_pattern['content'])) {
// remap pb_blocks to patterns
// remap tbell_pattern_blocks to patterns
$blocks = parse_blocks($updated_pattern['content']);
$blocks = $this->convert_blocks_to_patterns($blocks);
$pattern->content = serialize_blocks($blocks);
Expand Down Expand Up @@ -481,7 +481,7 @@ function handle_hijack_block_update($response, $handler, $request)
}

$post = get_post($pattern->id);
$formatted_response = $this->format_pb_block_response($post, $request);
$formatted_response = $this->format_tbell_pattern_block_response($post, $request);
$response = new WP_REST_Response($formatted_response, 200);
}
}
Expand Down Expand Up @@ -511,7 +511,7 @@ private function convert_blocks_to_patterns( $blocks ) {
foreach ( $blocks as &$block ) {
if ( isset( $block['blockName'] ) && $block['blockName'] === 'core/block' ) {
$post = get_post( $block['attrs']['ref'] );
if ( $post->post_type === 'pb_block' ) {
if ( $post->post_type === 'tbell_pattern_block' ) {
$slug = Pattern_Builder_Controller::format_pattern_slug_from_post( $post->post_name );
$block['blockName'] = 'core/pattern';
$block['attrs'] = isset( $block['attrs'] ) ? $block['attrs'] : array();
Expand Down
51 changes: 35 additions & 16 deletions includes/class-pattern-builder-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public static function format_pattern_slug_from_post( $slug ) {
return $new_slug;
}

public function get_pb_block_post_for_pattern( $pattern ) {
public function get_tbell_pattern_block_post_for_pattern( $pattern ) {
$path = $this->format_pattern_slug_for_post( $pattern->name );

$posts = get_posts(
array(
'name' => $path,
'post_type' => 'pb_block',
'post_type' => 'tbell_pattern_block',
)
);

Expand All @@ -38,11 +38,11 @@ public function get_pb_block_post_for_pattern( $pattern ) {
return $pattern_post;
}

return $this->create_pb_block_post_for_pattern( $pattern );
return $this->create_tbell_pattern_block_post_for_pattern( $pattern );
}

public function create_pb_block_post_for_pattern( $pattern ) {
$existing_post = get_page_by_path( $this->format_pattern_slug_for_post( $pattern->name ), OBJECT, array( 'pb_block' ) );
public function create_tbell_pattern_block_post_for_pattern( $pattern ) {
$existing_post = get_page_by_path( $this->format_pattern_slug_for_post( $pattern->name ), OBJECT, array( 'tbell_pattern_block' ) );

$post_id = $existing_post ? $existing_post->ID : null;

Expand Down Expand Up @@ -83,6 +83,23 @@ public function create_pb_block_post_for_pattern( $pattern ) {
} else {
delete_post_meta( $post_id, 'wp_pattern_inserter' );
}
if (!$post_id) {

$post_id = wp_insert_post(
array(
'post_title' => $pattern->title,
'post_name' => $this->format_pattern_slug_for_post( $pattern->name ),
'post_content' => $pattern->content,
'post_excerpt' => $pattern->description,
'post_type' => 'tbell_pattern_block',
'post_status' => 'publish',
'ping_status' => 'closed',
'comment_status' => 'closed',
'meta_input' => $meta,
), true
);

} else {

$post_id = wp_insert_post(
array(
Expand All @@ -91,14 +108,16 @@ public function create_pb_block_post_for_pattern( $pattern ) {
'post_name' => $this->format_pattern_slug_for_post( $pattern->name ),
'post_content' => $pattern->content,
'post_excerpt' => $pattern->description,
'post_type' => 'pb_block',
'post_type' => 'tbell_pattern_block',
'post_status' => 'publish',
'ping_status' => 'closed',
'comment_status' => 'closed',
'meta_input' => $meta,
)
), true
);

}

// store categories
wp_set_object_terms( $post_id, $pattern->categories, 'wp_pattern_category', false );

Expand All @@ -111,8 +130,8 @@ public function create_pb_block_post_for_pattern( $pattern ) {

public function update_theme_pattern(Abstract_Pattern $pattern, $options = array())
{
// get the pb_block post if it already exists
$post = get_page_by_path($this->format_pattern_slug_for_post($pattern->name), OBJECT, array('pb_block', 'wp_block'));
// get the tbell_pattern_block post if it already exists
$post = get_page_by_path($this->format_pattern_slug_for_post($pattern->name), OBJECT, array('tbell_pattern_block', 'wp_block'));

if ($post && $post->post_type === 'wp_block') {
// this is being converted to theme patterns, change the slug to include the theme domain
Expand Down Expand Up @@ -142,7 +161,7 @@ public function update_theme_pattern(Abstract_Pattern $pattern, $options = array
'post_name' => $this->format_pattern_slug_for_post($pattern->name),
'post_excerpt' => $pattern->description,
'post_content' => $pattern->content,
'post_type' => 'pb_block',
'post_type' => 'tbell_pattern_block',
)
);

Expand Down Expand Up @@ -389,11 +408,11 @@ public function update_user_pattern( Abstract_Pattern $pattern ) {
$convert_from_theme_pattern = false;

if ( empty( $post ) ) {
// check if the pattern exists in the database as a pb_block post
// check if the pattern exists in the database as a tbell_pattern_block post
// this is for any user patterns that are being converted from theme patterns
// It will be converted to a wp_block post when it is updated
$slug = $this->format_pattern_slug_for_post( $pattern->name );
$post = get_page_by_path( $slug, OBJECT, 'pb_block' );
$post = get_page_by_path( $slug, OBJECT, 'tbell_pattern_block' );
$convert_from_theme_pattern = true;
}

Expand Down Expand Up @@ -520,8 +539,8 @@ public function delete_theme_pattern( Abstract_Pattern $pattern ) {
return new WP_Error( 'pattern_delete_failed', 'Failed to delete pattern', array( 'status' => 500 ) );
}

$pb_block_post = $this->get_pb_block_post_for_pattern( $pattern );
$deleted = wp_delete_post( $pb_block_post->ID, true );
$tbell_pattern_block_post = $this->get_tbell_pattern_block_post_for_pattern( $pattern );
$deleted = wp_delete_post( $tbell_pattern_block_post->ID, true );

if ( ! $deleted ) {
return new WP_Error( 'pattern_delete_failed', 'Failed to delete pattern', array( 'status' => 500 ) );
Expand Down Expand Up @@ -598,8 +617,8 @@ function ( $matches ) use ( $pattern ) {
// get the post of the pattern
$pattern_post = get_post( $attributes['ref'], OBJECT );

// if the post is a pb_block post, we can convert it to a wp:pattern block
if ( $pattern_post && $pattern_post->post_type === 'pb_block' ) {
// if the post is a tbell_pattern_block post, we can convert it to a wp:pattern block
if ( $pattern_post && $pattern_post->post_type === 'tbell_pattern_block' ) {

$pattern_slug = $pattern_post->post_name;

Expand Down
Loading