module.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Switch_Page_On_Change; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 7 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 8 | use JFB_Components\Module\Base_Module_Dir_It; |
| 9 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 10 | use JFB_Components\Module\Base_Module_Handle_It; |
| 11 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 12 | use JFB_Components\Module\Base_Module_It; |
| 13 | use JFB_Components\Module\Base_Module_Url_It; |
| 14 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 15 | |
| 16 | // If this file is called directly, abort. |
| 17 | if ( ! defined( 'WPINC' ) ) { |
| 18 | die; |
| 19 | } |
| 20 | |
| 21 | class Module implements |
| 22 | Base_Module_It, |
| 23 | Base_Module_Url_It, |
| 24 | Base_Module_Handle_It, |
| 25 | Base_Module_After_Install_It, |
| 26 | Base_Module_Dir_It { |
| 27 | |
| 28 | use Base_Module_Url_Trait; |
| 29 | use Base_Module_Handle_Trait; |
| 30 | use Base_Module_Dir_Trait; |
| 31 | |
| 32 | const SUPPORT_NAME = 'jetFBSwitchPageOnChange'; |
| 33 | const ATTRIBUTE_NAME = 'switch_on_change'; |
| 34 | |
| 35 | public function rep_item_id() { |
| 36 | return 'switch-page-on-change'; |
| 37 | } |
| 38 | |
| 39 | public function condition(): bool { |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | public function on_install() { |
| 44 | \WP_Block_Supports::get_instance()->register( |
| 45 | self::SUPPORT_NAME, |
| 46 | array( |
| 47 | 'register_attribute' => array( $this, 'register_support' ), |
| 48 | 'apply' => array( $this, 'apply_support' ), |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | public function on_uninstall() { |
| 54 | \WP_Block_Supports::get_instance()->register( self::SUPPORT_NAME, array() ); |
| 55 | } |
| 56 | |
| 57 | public function init_hooks() { |
| 58 | add_action( |
| 59 | 'jet-form-builder/editor-assets/before', |
| 60 | array( $this, 'register_editor_scripts' ), |
| 61 | 0 |
| 62 | ); |
| 63 | |
| 64 | add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 65 | add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 66 | } |
| 67 | |
| 68 | public function remove_hooks() { |
| 69 | remove_action( |
| 70 | 'jet-form-builder/editor-assets/before', |
| 71 | array( $this, 'register_editor_scripts' ), |
| 72 | 0 |
| 73 | ); |
| 74 | |
| 75 | remove_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 76 | remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 77 | } |
| 78 | |
| 79 | public function register_editor_scripts() { |
| 80 | $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' ); |
| 81 | |
| 82 | wp_enqueue_script( |
| 83 | $this->get_handle(), |
| 84 | $this->get_url( 'assets/build/editor.js' ), |
| 85 | $script_asset['dependencies'], |
| 86 | $script_asset['version'], |
| 87 | true |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | public function register_frontend_scripts() { |
| 92 | $script_asset = require_once $this->get_dir( 'assets/build/frontend.asset.php' ); |
| 93 | |
| 94 | if ( true === $script_asset ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | array_push( |
| 99 | $script_asset['dependencies'], |
| 100 | \Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE |
| 101 | ); |
| 102 | |
| 103 | wp_register_script( |
| 104 | $this->get_handle(), |
| 105 | $this->get_url( 'assets/build/frontend.js' ), |
| 106 | $script_asset['dependencies'], |
| 107 | $script_asset['version'], |
| 108 | true |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | public function register_support( \WP_Block_Type $block_type ) { |
| 113 | // Setup attributes and styles within that if needed. |
| 114 | if ( ! $block_type->attributes ) { |
| 115 | $block_type->attributes = array(); |
| 116 | } |
| 117 | |
| 118 | if ( block_has_support( $block_type, array( self::SUPPORT_NAME ) ) && |
| 119 | ! array_key_exists( self::ATTRIBUTE_NAME, $block_type->attributes ) |
| 120 | ) { |
| 121 | $block_type->attributes[ self::ATTRIBUTE_NAME ] = array( |
| 122 | 'type' => 'boolean', |
| 123 | 'default' => false, |
| 124 | ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | public function apply_support( \WP_Block_Type $block_type, array $block_attributes ): array { |
| 129 | if ( ! is_array( $block_type->supports ) ) { |
| 130 | return array(); |
| 131 | } |
| 132 | $support_config = Array_Tools::get( $block_type->supports, array( self::SUPPORT_NAME ), false ); |
| 133 | $value = $block_attributes[ self::ATTRIBUTE_NAME ] ?? array(); |
| 134 | |
| 135 | if ( empty( $support_config ) || empty( $value ) ) { |
| 136 | return array(); |
| 137 | } |
| 138 | |
| 139 | wp_enqueue_script( $this->get_handle() ); |
| 140 | |
| 141 | return array( |
| 142 | 'class' => $this->get_handle(), |
| 143 | ); |
| 144 | } |
| 145 | } |
| 146 |