| 1 |
<?php |
| 2 |
/** |
| 3 |
* Widgetized area block. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Widgetized_Area_Block |
| 14 |
*/ |
| 15 |
class GhostKit_Widgetized_Area_Block { |
| 16 |
/** |
| 17 |
* Is in rendering loop. |
| 18 |
* We have to check if sidebar is rendering and prevent render another sidebar. |
| 19 |
* In short, of you insert the dynamic sidebar inside the same sidebar, there will be an infinite loop and PHP fatal error. |
| 20 |
* |
| 21 |
* @var boolean |
| 22 |
*/ |
| 23 |
private $rendering; |
| 24 |
|
| 25 |
/** |
| 26 |
* GhostKit_Widgetized_Area_Block constructor. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
add_action( 'init', array( $this, 'init' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Init. |
| 34 |
*/ |
| 35 |
public function init() { |
| 36 |
register_block_type_from_metadata( |
| 37 |
dirname( __FILE__ ), |
| 38 |
array( |
| 39 |
'render_callback' => array( $this, 'block_render' ), |
| 40 |
) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Register gutenberg block output |
| 46 |
* |
| 47 |
* @param array $attributes - block attributes. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function block_render( $attributes ) { |
| 52 |
if ( $this->rendering ) { |
| 53 |
return ''; |
| 54 |
} |
| 55 |
|
| 56 |
$this->rendering = true; |
| 57 |
|
| 58 |
ob_start(); |
| 59 |
|
| 60 |
$class = isset( $attributes['className'] ) ? $attributes['className'] : ''; |
| 61 |
$class .= ' ghostkit-widgetized-area'; |
| 62 |
|
| 63 |
if ( $attributes['id'] ) { |
| 64 |
echo '<div class="' . esc_attr( $class ) . '">'; |
| 65 |
dynamic_sidebar( $attributes['id'] ); |
| 66 |
echo '</div>'; |
| 67 |
} |
| 68 |
|
| 69 |
$this->rendering = false; |
| 70 |
|
| 71 |
return ob_get_clean(); |
| 72 |
} |
| 73 |
} |
| 74 |
new GhostKit_Widgetized_Area_Block(); |
| 75 |
|