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