abstract-block.php
152 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Hustle_GHBlock_Abstract |
| 5 | * Extend this class to create new gutenberg block |
| 6 | * |
| 7 | * @since 1.0 Gutenberg Addon |
| 8 | */ |
| 9 | abstract class Hustle_GHBlock_Abstract { |
| 10 | |
| 11 | /** |
| 12 | * Module's dependencies for rendering the preview |
| 13 | * |
| 14 | * @var array |
| 15 | */ |
| 16 | protected $dependencies = array(); |
| 17 | |
| 18 | /** |
| 19 | * Type will be used as identifier |
| 20 | * |
| 21 | * @since 1.0 Gutenber Addon |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $_slug; |
| 26 | |
| 27 | /** |
| 28 | * Get block type |
| 29 | * |
| 30 | * @since 1.0 Gutenber Addon |
| 31 | * @return string |
| 32 | */ |
| 33 | final public function get_slug() { |
| 34 | return $this->_slug; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Initialize block |
| 39 | * |
| 40 | * @since 1.0 Gutenberg Addon |
| 41 | */ |
| 42 | public function init() { |
| 43 | // Register block |
| 44 | $this->register_block(); |
| 45 | |
| 46 | // Load block scripts |
| 47 | add_action( 'enqueue_block_editor_assets', array( $this, 'load_assets' ) ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Register block type callback |
| 52 | * Shouldn't be overridden on block class |
| 53 | * |
| 54 | * @since 1.0 Gutenberg Addon |
| 55 | */ |
| 56 | public function register_block() { |
| 57 | |
| 58 | if ( function_exists( 'register_block_type' ) ) { |
| 59 | |
| 60 | register_block_type( |
| 61 | 'hustle/' . $this->get_slug(), |
| 62 | array( |
| 63 | 'render_callback' => array( $this, 'render_block' ), |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Render block on front-end |
| 72 | * Should be overriden in block class |
| 73 | * |
| 74 | * @since 1.0 Gutenberg Addon |
| 75 | * @param array $properties Block properties |
| 76 | * |
| 77 | * @return string |
| 78 | */ |
| 79 | public function render_block( $properties = array() ) { |
| 80 | return ''; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Enqueue assets ( scritps / styles ) |
| 85 | * Should be overriden in block class |
| 86 | * |
| 87 | * @since 1.0 Gutenberg Addon |
| 88 | */ |
| 89 | public function load_assets() { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get modules list with shortcode |
| 95 | * |
| 96 | * @since 1.0 Gutenberg Addon |
| 97 | * |
| 98 | * @param string $type Module type. |
| 99 | * |
| 100 | * @return array $module_list List of modules with shortcode. |
| 101 | */ |
| 102 | protected function get_modules_by_type( $type ) { |
| 103 | $modules = Hustle_Module_Collection::instance()->get_all( true, array( 'module_type' => $type ) ); |
| 104 | $module_list = array( |
| 105 | array( |
| 106 | 'value' => '', |
| 107 | 'label' => esc_html__( 'Choose module name', 'hustle' ), |
| 108 | ), |
| 109 | ); |
| 110 | if ( is_array( $modules ) ) { |
| 111 | foreach ( $modules as $module ) { |
| 112 | $shortcode_id = $module->get_shortcode_id(); |
| 113 | if ( empty( $shortcode_id ) ) { |
| 114 | continue; |
| 115 | } |
| 116 | if ( ! $this->is_module_included( $module ) ) { |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | $this->check_dependencies( $module ); |
| 121 | |
| 122 | $module_list[] = array( |
| 123 | 'value' => $shortcode_id, |
| 124 | 'label' => $module->module_name, |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | return $module_list; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Check for dependencies in each block type. |
| 133 | * To be overridden as required. |
| 134 | * |
| 135 | * @param Hustle_Model $module Module to be checked. |
| 136 | * @return void |
| 137 | */ |
| 138 | protected function check_dependencies( Hustle_Model $module ) {} |
| 139 | |
| 140 | /** |
| 141 | * Check in every block type if this module should be available. |
| 142 | * |
| 143 | * @since 4.0.0 |
| 144 | * |
| 145 | * @param Hustle_Model $module Instance of the current module. |
| 146 | * @return bool |
| 147 | */ |
| 148 | protected function is_module_included( Hustle_Model $module ) { |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 |