LLMS_Abstract_Generator_Posts::handle_reusable_blocks( LLMS_Post_Model $post, array $raw )

Handle importing of reusable blocks stored in post content


Parameters Parameters

$post

(LLMS_Post_Model) (Required) Instance of a post model.

$raw

(array) (Required) Array of raw data.


Top ↑

Return Return

(null|bool) Returns null when importing is disabled, false when there are no blocks to import, and true on success.


Top ↑

Source Source

File: includes/abstracts/llms-abstract-generator-posts.php

	protected function handle_reusable_blocks( $post, $raw ) {

		// Importing blocks is disabled.
		if ( ! $this->is_reusable_block_importing_enabled() ) {
			return null;
		}

		// No blocks to import.
		if ( empty( $raw['_extras']['blocks'] ) ) {
			return false;
		}

		$find    = array();
		$replace = array();
		foreach ( $raw['_extras']['blocks'] as $block_id => $block ) {

			$new_id = $this->create_reusable_block( $block_id, $block );
			if ( ! is_wp_error( $new_id ) && is_numeric( $new_id ) ) {
				$find[]    = sprintf( '<!-- wp:block {"ref":%d}', absint( $block_id ) );
				$replace[] = sprintf( '<!-- wp:block {"ref":%d}', $new_id );
			}
		}

		if ( $find && $replace ) {
			$args = array(
				'ID'           => $post->get( 'id' ),
				'post_content' => str_replace( $find, $replace, $post->get( 'content', true ) ),
			);
			return wp_update_post( $args ) ? true : false;
		}

		return false;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.