| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Blocks; |
| 4 |
|
| 5 |
abstract class AbstractBlock { |
| 6 |
|
| 7 |
protected $namespace = 'filebird'; |
| 8 |
|
| 9 |
protected $block_name = ''; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$this->init(); |
| 13 |
} |
| 14 |
|
| 15 |
abstract protected function enqueue_frontend_assets(); |
| 16 |
|
| 17 |
protected function get_block_type() { |
| 18 |
return $this->namespace . '/' . $this->block_name; |
| 19 |
} |
| 20 |
|
| 21 |
protected function get_block_attributes() { |
| 22 |
return array(); |
| 23 |
} |
| 24 |
|
| 25 |
protected function get_editor_dependencies() { |
| 26 |
return array(); |
| 27 |
} |
| 28 |
|
| 29 |
protected function register_block_editor_script() { |
| 30 |
wp_register_script( |
| 31 |
$this->namespace . '-' . $this->block_name . '-js', |
| 32 |
NJFB_PLUGIN_URL . 'blocks/' . $this->block_name . '/dist/index.js', |
| 33 |
$this->get_editor_dependencies(), |
| 34 |
NJFB_VERSION, |
| 35 |
true |
| 36 |
); |
| 37 |
wp_register_style( |
| 38 |
$this->namespace . '-' . $this->block_name . '-css', |
| 39 |
NJFB_PLUGIN_URL . 'blocks/' . $this->block_name . '/dist/index.css', |
| 40 |
$this->get_editor_dependencies(), |
| 41 |
NJFB_VERSION |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function render_callback( $attributes = array(), $content = '' ) { |
| 46 |
if ( ! is_admin() ) { |
| 47 |
$this->enqueue_frontend_assets(); |
| 48 |
} |
| 49 |
return $this->render( $attributes, $content ); |
| 50 |
} |
| 51 |
|
| 52 |
protected function render( $attributes, $content ) { |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
protected function get_render_callback() { |
| 57 |
return array( $this, 'render_callback' ); |
| 58 |
} |
| 59 |
|
| 60 |
protected function register_block_type() { |
| 61 |
$this->register_block_editor_script(); |
| 62 |
register_block_type( |
| 63 |
$this->get_block_type(), |
| 64 |
array( |
| 65 |
'editor_script' => $this->namespace . '-' . $this->block_name . '-js', |
| 66 |
'editor_style' => $this->namespace . '-' . $this->block_name . '-css', |
| 67 |
// 'style' => $this->namespace . '-' . $this->block_name, |
| 68 |
'render_callback' => $this->get_render_callback(), |
| 69 |
'attributes' => $this->get_block_attributes(), |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
protected function init() { |
| 75 |
$this->register_block_type(); |
| 76 |
} |
| 77 |
} |