Skip to content

Commit

Permalink
enhance(block): Add missing block render callback properties (#135)
Browse files Browse the repository at this point in the history
* enhance(block): Add `$context` param to block render callback (Fixes #130)
* enhance(block): Add `$wp_block` param to block render callback
* enhance(block): Add `$block->context` property to access `$context`
* enhance(block): Add `$block->instance` property to access `$wp_block`
* chore(block): Add missing `$post_id` property definition
  • Loading branch information
Log1x committed Oct 14, 2022
1 parent 22c5613 commit 71e2695
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ abstract class Block extends Composer implements BlockContract
*
* @param int
*/
public $post_id;

/**
* The block instance.
*
* @var \WP_Block
*/
public $instance;

/**
* The block context.
*
* @var array
*/
public $context;

/**
* The current post.
*
* @param \WP_Post
*/
public $post;

/**
Expand Down Expand Up @@ -261,8 +282,15 @@ public function compose()
'enqueue_assets' => function () {
return $this->enqueue();
},
'render_callback' => function ($block, $content = '', $preview = false, $post_id = 0) {
echo $this->render($block, $content, $preview, $post_id);
'render_callback' => function (
$block,
$content = '',
$preview = false,
$post_id = 0,
$wp_block = false,
$context = false
) {
echo $this->render($block, $content, $preview, $post_id, $wp_block, $context);
},
];

Expand All @@ -284,20 +312,24 @@ public function compose()
/**
* Render the ACF block.
*
* @param array $block
* @param string $content
* @param bool $preview
* @param int $post_id
* @param array $block
* @param string $content
* @param bool $preview
* @param int $post_id
* @param \WP_Block $wp_block
* @param array $context
* @return string
*/
public function render($block, $content = '', $preview = false, $post_id = 0)
public function render($block, $content = '', $preview = false, $post_id = 0, $wp_block = false, $context = false)
{
$this->block = (object) $block;
$this->content = $content;
$this->preview = $preview;
$this->post_id = $post_id;
$this->instance = $wp_block;
$this->context = $context;

$this->post = get_post($post_id);
$this->post_id = $post_id;

$this->classes = collect([
'slug' => Str::start(
Expand Down

0 comments on commit 71e2695

Please sign in to comment.