| 1 |
<?php |
| 2 |
/** |
| 3 |
* Widgetized Area Block. |
| 4 |
* |
| 5 |
* @package Canvas |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Initialize Widgetized Area block. |
| 10 |
*/ |
| 11 |
class CNVS_Block_Widgetized_Area { |
| 12 |
|
| 13 |
/** |
| 14 |
* Initialize |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
add_action( 'init', array( $this, 'init' ) ); |
| 18 |
add_filter( 'canvas_register_block_type', array( $this, 'register_block_type' ) ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Enqueue the block's assets for the editor. |
| 23 |
*/ |
| 24 |
public function init() { |
| 25 |
// Editor Scripts. |
| 26 |
wp_register_script( |
| 27 |
'canvas-block-widgetized-area-editor-script', |
| 28 |
plugins_url( 'block-widgetized-area/block.js', __FILE__ ), |
| 29 |
array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor' ), |
| 30 |
filemtime( plugin_dir_path( __FILE__ ) . 'block-widgetized-area/block.js' ), |
| 31 |
true |
| 32 |
); |
| 33 |
|
| 34 |
// Get all sidebars. |
| 35 |
$sidebars = array(); |
| 36 |
if ( ! empty( $GLOBALS['wp_registered_sidebars'] ) ) { |
| 37 |
foreach ( $GLOBALS['wp_registered_sidebars'] as $k => $sidebar ) { |
| 38 |
$sidebars[ $k ] = array( |
| 39 |
'id' => $sidebar['id'], |
| 40 |
'name' => $sidebar['name'], |
| 41 |
); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
wp_localize_script( |
| 46 |
'canvas-block-widgetized-area-editor-script', 'canvasWidgetizedBlock', array( |
| 47 |
'sidebars' => $sidebars, |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Register block |
| 54 |
* |
| 55 |
* @param array $blocks all registered blocks. |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
public function register_block_type( $blocks ) { |
| 59 |
$blocks[] = array( |
| 60 |
'name' => 'canvas/widgetized-area', |
| 61 |
'title' => esc_html__( 'Widgetized Area', 'canvas' ), |
| 62 |
'category' => 'canvas', |
| 63 |
'keywords' => array(), |
| 64 |
'icon' => ' |
| 65 |
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 66 |
<path d="M3 13H11V15H3V13ZM3 17H11V19H3V17ZM3 9H11V11H3V9ZM3 5H11V7H3V5ZM19 7V17H15V7H19ZM21 5H13V19H21V5Z" fill="currentColor"/> |
| 67 |
</svg> |
| 68 |
', |
| 69 |
'supports' => array( |
| 70 |
'className' => true, |
| 71 |
'anchor' => true, |
| 72 |
'html' => false, |
| 73 |
'canvasSpacings' => true, |
| 74 |
'canvasBorder' => true, |
| 75 |
'canvasResponsive' => true, |
| 76 |
), |
| 77 |
'styles' => array(), |
| 78 |
'location' => array(), |
| 79 |
|
| 80 |
'sections' => array(), |
| 81 |
'layouts' => array(), |
| 82 |
|
| 83 |
// Set fields just for add block attributes. |
| 84 |
// Editor render for this block is custom JSX |
| 85 |
// so we don't need to render fields automatically. |
| 86 |
'fields' => array( |
| 87 |
array( |
| 88 |
'key' => 'area', |
| 89 |
'type' => 'type-string', |
| 90 |
'default' => '', |
| 91 |
), |
| 92 |
), |
| 93 |
'template' => dirname( __FILE__ ) . '/block-widgetized-area/render.php', |
| 94 |
|
| 95 |
// enqueue registered scripts/styles. |
| 96 |
'editor_script' => 'canvas-block-widgetized-area-editor-script', |
| 97 |
); |
| 98 |
|
| 99 |
return $blocks; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
new CNVS_Block_Widgetized_Area(); |
| 104 |
|