Skip to content
Open
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
38 changes: 20 additions & 18 deletions includes/blocks/helpers/class-convertkit-block-post-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ public static function find( $post_id, $block_name ) {

$occurrence_index = 0;

foreach ( $blocks as $index => $block ) {
foreach ( $blocks as $block ) {
if ( ! isset( $block['blockName'] ) || $block['blockName'] !== $block_name ) {
continue;
}

$found[] = array(
'index' => (int) $index,
'occurrence_index' => (int) $occurrence_index,
'attrs' => $block['attrs'],
);
Expand Down Expand Up @@ -120,6 +119,15 @@ public static function insert( $post_id, $block_name, $attrs, $position = 'appen
// Splice in the new block.
array_splice( $blocks, $insert_at, 0, array( $new_block ) );

// Determine the occurrence index of the newly inserted block, by
// counting how many blocks of the same name precede it.
$occurrence_index = 0;
for ( $i = 0; $i < $insert_at; $i++ ) {
if ( isset( $blocks[ $i ]['blockName'] ) && $blocks[ $i ]['blockName'] === $block_name ) {
++$occurrence_index;
}
}

// Update Post.
$result = wp_update_post(
array(
Expand All @@ -134,10 +142,10 @@ public static function insert( $post_id, $block_name, $attrs, $position = 'appen
return $result;
}

// Return the index the block was inserted at.
// Return the occurrence index of the newly inserted block.
return array(
'post_id' => $post_id,
'index' => $insert_at,
'post_id' => $post_id,
'occurrence_index' => $occurrence_index,
);

}
Expand Down Expand Up @@ -167,13 +175,10 @@ public static function update( $post_id, $block_name, $occurrence_index, $attrs

// Parse blocks.
$blocks = parse_blocks( $post->post_content );
$update_at = 0;
$block_index = 0;
$matched = false;

foreach ( $blocks as $key => $block ) {
++$update_at;

// Skip if the block name does not match.
if ( ! isset( $block['blockName'] ) || $block['blockName'] !== $block_name ) {
continue;
Expand Down Expand Up @@ -212,10 +217,10 @@ public static function update( $post_id, $block_name, $occurrence_index, $attrs
return $result;
}

// Return the index the block was updated at.
// Return the occurrence index of the block that was updated.
return array(
'post_id' => $post_id,
'index' => ( $update_at - 1 ),
'post_id' => $post_id,
'occurrence_index' => (int) $occurrence_index,
);

}
Expand Down Expand Up @@ -244,19 +249,16 @@ public static function delete( $post_id, $block_name, $occurrence_index ) {

// Parse blocks.
$blocks = parse_blocks( $post->post_content );
$delete_at = 0;
$block_index = 0;
$matched = false;

foreach ( $blocks as $key => $block ) {
++$delete_at;

// Skip if the block name does not match.
if ( ! isset( $block['blockName'] ) || $block['blockName'] !== $block_name ) {
continue;
}

// Update the block if the occurrence index matches.
// Delete the block if the occurrence index matches.
if ( $block_index === (int) $occurrence_index ) {
unset( $blocks[ $key ] );
$blocks = array_values( $blocks );
Expand Down Expand Up @@ -290,10 +292,10 @@ public static function delete( $post_id, $block_name, $occurrence_index ) {
return $result;
}

// Return the index the block was deleted from.
// Return the occurrence index of the block that was deleted.
return array(
'post_id' => $post_id,
'index' => ( $delete_at - 1 ),
'post_id' => $post_id,
'occurrence_index' => (int) $occurrence_index,
);

}
Expand Down
30 changes: 30 additions & 0 deletions includes/blocks/helpers/class-convertkit-content-post-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public static function find( $post_id, $element_name ) {
$post_id,
'convertkit/' . $element_name
);

case 'shortcode':
return ConvertKit_Shortcode_Post_Helper::find(
$post_id,
'convertkit_' . $element_name
);
}

return self::unsupported_mechanism_error( $mechanism );
Expand Down Expand Up @@ -90,6 +96,15 @@ public static function insert( $post_id, $element_name, $attrs, $position = 'app
$position,
$index
);

case 'shortcode':
return ConvertKit_Shortcode_Post_Helper::insert(
$post_id,
'convertkit_' . $element_name,
$attrs,
$position,
$index
);
}

return self::unsupported_mechanism_error( $mechanism );
Expand Down Expand Up @@ -126,6 +141,14 @@ public static function update( $post_id, $element_name, $occurrence_index, $attr
$occurrence_index,
$attrs
);

case 'shortcode':
return ConvertKit_Shortcode_Post_Helper::update(
$post_id,
'convertkit_' . $element_name,
$occurrence_index,
$attrs
);
}

return self::unsupported_mechanism_error( $mechanism );
Expand Down Expand Up @@ -160,6 +183,13 @@ public static function delete( $post_id, $element_name, $occurrence_index ) {
'convertkit/' . $element_name,
$occurrence_index
);

case 'shortcode':
return ConvertKit_Shortcode_Post_Helper::delete(
$post_id,
'convertkit_' . $element_name,
$occurrence_index
);
}

return self::unsupported_mechanism_error( $mechanism );
Expand Down
Loading
Loading