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