assets
8 months ago
.eslintrc.js
1 year ago
block-asset.php
1 year ago
block-render.php
1 year ago
block-type-row.php
1 year ago
block-type.php
1 year ago
block.json
1 year ago
block-asset.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Blocks_V2\Repeater_Field; |
| 4 | |
| 5 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 6 | use JFB_Modules\Blocks_V2\Interfaces\Block_Asset_Interface; |
| 7 | use JFB_Modules\Blocks_V2\Module; |
| 8 | use Jet_Form_Builder\Blocks\Module as LegacyBlocksModule; |
| 9 | |
| 10 | class Block_Asset implements Block_Asset_Interface { |
| 11 | |
| 12 | const HANDLE = 'jet-fb-repeater-field'; |
| 13 | |
| 14 | public function init_hooks() { |
| 15 | add_action( |
| 16 | 'jet-form-builder/editor-assets/before', |
| 17 | array( $this, 'enqueue_editor_assets' ) |
| 18 | ); |
| 19 | add_action( |
| 20 | 'wp_enqueue_scripts', |
| 21 | array( $this, 'register_frontend_assets' ) |
| 22 | ); |
| 23 | add_action( |
| 24 | 'jet_plugins/frontend/register_scripts', |
| 25 | array( $this, 'register_frontend_assets' ) |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @throws Repository_Exception |
| 31 | */ |
| 32 | public function register_frontend_assets() { |
| 33 | $asset = require_once $this->get_dir( '/assets/build/frontend.asset.php' ); |
| 34 | |
| 35 | if ( true === $asset ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $asset['dependencies'][] = LegacyBlocksModule::MAIN_SCRIPT_HANDLE; |
| 40 | |
| 41 | wp_register_script( |
| 42 | self::HANDLE, |
| 43 | $this->get_url( '/assets/build/frontend.js' ), |
| 44 | $asset['dependencies'], |
| 45 | $asset['version'], |
| 46 | true |
| 47 | ); |
| 48 | |
| 49 | wp_register_style( |
| 50 | self::HANDLE, |
| 51 | $this->get_url( '/assets/build/frontend.css' ), |
| 52 | array(), |
| 53 | $asset['version'] |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return void |
| 59 | * @throws Repository_Exception |
| 60 | */ |
| 61 | public function enqueue_editor_assets() { |
| 62 | $asset = require_once $this->get_dir( '/assets/build/editor.asset.php' ); |
| 63 | |
| 64 | if ( true === $asset ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | wp_enqueue_script( |
| 69 | $this->get_handle( 'repeater-field-editor' ), |
| 70 | $this->get_url( '/assets/build/editor.js' ), |
| 71 | $asset['dependencies'], |
| 72 | $asset['version'], |
| 73 | true |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 78 | private function get_dir( string $path = '' ): string { |
| 79 | /** @var Module $compat */ |
| 80 | $compat = jet_form_builder()->module( 'blocks-v2' ); |
| 81 | |
| 82 | return $compat->get_dir( 'repeater-field' . $path ); |
| 83 | } |
| 84 | |
| 85 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 86 | private function get_url( string $path ): string { |
| 87 | /** @var Module $compat */ |
| 88 | $compat = jet_form_builder()->module( 'blocks-v2' ); |
| 89 | |
| 90 | return $compat->get_url( 'repeater-field' . $path ); |
| 91 | } |
| 92 | |
| 93 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 94 | private function get_handle( string $prefix ): string { |
| 95 | /** @var Module $compat */ |
| 96 | $compat = jet_form_builder()->module( 'blocks-v2' ); |
| 97 | |
| 98 | return $compat->get_handle( $prefix ); |
| 99 | } |
| 100 | } |
| 101 |