|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Table block class. |
| 4 | + * |
| 5 | + * @package Sovereign Health Care |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Creode_Blocks; |
| 9 | + |
| 10 | +/** |
| 11 | + * Table block class. |
| 12 | + * |
| 13 | + * @wordpress-block-version 2.3.0 |
| 14 | + */ |
| 15 | +class Table_Block extends Block { |
| 16 | + |
| 17 | + use Trait_Has_Modifier_Classes; |
| 18 | + use Trait_Has_Reduce_Bottom_Space_Option; |
| 19 | + use Trait_Has_Icons; |
| 20 | + use Trait_Has_Color_Choices; |
| 21 | + |
| 22 | + /** |
| 23 | + * The blocks icon from https://developer.wordpress.org/resource/dashicons/ or an inline SVG. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $icon = 'editor-table'; |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritdoc} |
| 31 | + */ |
| 32 | + protected function name(): string { |
| 33 | + return 'table'; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + protected function label(): string { |
| 40 | + return 'Table'; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + protected function setup(): bool { |
| 47 | + add_action( |
| 48 | + 'wp_enqueue_scripts', |
| 49 | + function () { |
| 50 | + $this->check_js_dependencies(); |
| 51 | + }, |
| 52 | + 20 |
| 53 | + ); |
| 54 | + |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + protected function fields(): array { |
| 62 | + return array( |
| 63 | + array( |
| 64 | + 'key' => 'field_table_id', |
| 65 | + 'label' => 'ID', |
| 66 | + 'name' => 'id', |
| 67 | + 'type' => 'text', |
| 68 | + ), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritdoc} |
| 74 | + */ |
| 75 | + protected function child_blocks(): array { |
| 76 | + return array( |
| 77 | + new Child_Block( |
| 78 | + 'additional-content', |
| 79 | + 'Additional Content', |
| 80 | + array(), |
| 81 | + __DIR__ . '/templates/additional-content.php', |
| 82 | + array(), |
| 83 | + 'text' |
| 84 | + ), |
| 85 | + new Child_Block( |
| 86 | + 'table', |
| 87 | + 'Table', |
| 88 | + array( |
| 89 | + array( |
| 90 | + 'key' => 'field_table_device_visibility', |
| 91 | + 'label' => 'Device Visibility', |
| 92 | + 'name' => 'device_visibility', |
| 93 | + 'type' => 'select', |
| 94 | + 'instructions' => 'Please choose which devices the table should be visible on.', |
| 95 | + 'choices' => array( |
| 96 | + 'all' => 'All', |
| 97 | + 'desktop-only' => 'Desktop Only', |
| 98 | + 'mobile-only' => 'Mobile Only', |
| 99 | + ), |
| 100 | + ), |
| 101 | + ), |
| 102 | + __DIR__ . '/templates/table.php', |
| 103 | + array( |
| 104 | + new Child_Block( |
| 105 | + 'row', |
| 106 | + 'Table Row', |
| 107 | + array( |
| 108 | + array( |
| 109 | + 'key' => 'field_table_row_has_bottom_space', |
| 110 | + 'label' => 'Bottom Space', |
| 111 | + 'name' => 'has_bottom_space', |
| 112 | + 'type' => 'true_false', |
| 113 | + 'message' => 'Has bottom space?', |
| 114 | + 'default_value' => true, |
| 115 | + ), |
| 116 | + ), |
| 117 | + __DIR__ . '/templates/row.php', |
| 118 | + array( |
| 119 | + new Child_Block( |
| 120 | + 'cell', |
| 121 | + 'Table Cell', |
| 122 | + array( |
| 123 | + array( |
| 124 | + 'key' => 'field_table_cell_colspan', |
| 125 | + 'label' => 'Colspan', |
| 126 | + 'name' => 'colspan', |
| 127 | + 'type' => 'number', |
| 128 | + 'instructions' => 'The number of columns the cell should span. This will not be reflected in the editor.', |
| 129 | + 'default_value' => 1, |
| 130 | + ), |
| 131 | + ), |
| 132 | + __DIR__ . '/templates/cell.php', |
| 133 | + array( |
| 134 | + new Child_Block( |
| 135 | + 'cell-content', |
| 136 | + 'Table Cell Content', |
| 137 | + array(), |
| 138 | + __DIR__ . '/templates/cell-content.php', |
| 139 | + array(), |
| 140 | + 'text', |
| 141 | + array( |
| 142 | + 'mode' => false, |
| 143 | + 'color' => array( |
| 144 | + 'text' => true, |
| 145 | + 'background' => true, |
| 146 | + ), |
| 147 | + ), |
| 148 | + ), |
| 149 | + ), |
| 150 | + 'table-col-after' |
| 151 | + ), |
| 152 | + ), |
| 153 | + 'table-row-after' |
| 154 | + ), |
| 155 | + ), |
| 156 | + 'editor-table' |
| 157 | + ), |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * {@inheritdoc} |
| 163 | + */ |
| 164 | + protected function template(): string { |
| 165 | + return __DIR__ . '/templates/block.php'; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * {@inheritdoc} |
| 170 | + */ |
| 171 | + protected function supports(): array { |
| 172 | + return array( |
| 173 | + 'mode' => false, |
| 174 | + 'color' => array( |
| 175 | + 'text' => true, |
| 176 | + 'background' => true, |
| 177 | + ), |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * {@inheritdoc |
| 183 | + */ |
| 184 | + protected function scripts(): array { |
| 185 | + return array( |
| 186 | + new Script( 'match-table-height', plugin_dir_url( __FILE__ ) . 'assets/match-table-height.js', array( 'match-height' ), '1' ), |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Checks if the js dependencies are working correctly. |
| 192 | + * |
| 193 | + * @throws \Exception If dependencies are not met. |
| 194 | + * |
| 195 | + * @return void |
| 196 | + */ |
| 197 | + protected function check_js_dependencies() { |
| 198 | + // Check if we have match heights script. |
| 199 | + if ( ! wp_script_is( 'match-height', 'registered' ) ) { |
| 200 | + throw new \Exception( 'Please ensure that the match heights script has been registered. You can find an example of how to add this to existing projects inside the theme package.' ); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments