class-evf-blocks.php
145 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Everest Forms blocks. |
| 4 | * |
| 5 | * @since 2.0.9 |
| 6 | * @package everest-forms |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | /** |
| 11 | * Everest Forms blocks class. |
| 12 | */ |
| 13 | class EVF_Blocks { |
| 14 | /** |
| 15 | * Constructor. |
| 16 | */ |
| 17 | public function __construct() { |
| 18 | $this->init_hooks(); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Init hooks. |
| 23 | * |
| 24 | * @since 2.0.9 |
| 25 | */ |
| 26 | private function init_hooks() { |
| 27 | $this->includes(); |
| 28 | add_filter( 'block_categories_all', array( $this, 'block_categories' ), PHP_INT_MAX, 2 ); |
| 29 | add_action( 'init', array( $this, 'register_block_types' ) ); |
| 30 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 31 | } |
| 32 | /** |
| 33 | * Includes the block type files. |
| 34 | * |
| 35 | * @since 0 |
| 36 | */ |
| 37 | private function includes() { |
| 38 | include_once EVF_ABSPATH . 'includes/blocks/block-types/class-evf-blocks-abstract.php'; |
| 39 | include_once EVF_ABSPATH . 'includes/blocks/block-types/class-evf-blocks-form-selector.php'; |
| 40 | include_once EVF_ABSPATH . 'includes/blocks/block-types/class-evf-blocks-frontend-listing.php'; |
| 41 | include_once EVF_ABSPATH . 'includes/blocks/block-types/class-evf-blocks-user-login.php'; |
| 42 | } |
| 43 | /** |
| 44 | * Enqueue Block Editor Assets. |
| 45 | * |
| 46 | * @return void. |
| 47 | */ |
| 48 | public function enqueue_block_editor_assets() { |
| 49 | wp_register_style( |
| 50 | 'everest-forms-block-editor', |
| 51 | evf()->plugin_url() . '/assets/css/everest-forms.css', |
| 52 | array( 'wp-edit-blocks' ), |
| 53 | evf()->version |
| 54 | ); |
| 55 | wp_enqueue_style( 'everest-forms-block-editor' ); |
| 56 | $enqueue_script = array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-editor', 'wp-components', 'react', 'react-dom' ); |
| 57 | wp_register_script( |
| 58 | 'everest-forms-block-editor', |
| 59 | evf()->plugin_url() . '/dist/blocks.min.js', |
| 60 | $enqueue_script, |
| 61 | evf()->version, |
| 62 | true |
| 63 | ); |
| 64 | wp_register_script( |
| 65 | 'everest-forms-shortcode-embed-form', |
| 66 | evf()->plugin_url() . '/assets/js/admin/shortcode-form-embed.js', |
| 67 | array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-components', 'wp-dom-ready', 'wp-edit-post', 'jquery', 'jquery-blockui', 'jquery-ui-sortable', 'jquery-ui-widget', 'jquery-ui-core', 'tooltipster', 'wp-color-picker', 'perfect-scrollbar' ), |
| 68 | defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( evf()->plugin_path() . '/assets/js/admin/shortcode-form-embed.js' ) : EVF_VERSION, |
| 69 | true |
| 70 | ); |
| 71 | $form_block_data = array( |
| 72 | 'evfRestApiNonce' => wp_create_nonce( 'wp_rest' ), |
| 73 | 'restURL' => rest_url(), |
| 74 | 'forms' => evf()->form->get_multiple( array( 'order' => 'DESC' ) ), |
| 75 | 'isPro' => defined( 'EFP_VERSION' ) && version_compare( EFP_VERSION, '1.7.3', '>=' ) ? true : false, |
| 76 | 'isFrontendListingActive' => defined( 'EVF_FRONTEND_LISTING_VERSION' ) && version_compare( EVF_FRONTEND_LISTING_VERSION, '1.0.0', '>=' ) ? true : false, |
| 77 | 'isUserRegistrationActive' => defined( 'EVF_USER_REGISTRATION_VERSION' ) && version_compare( EVF_USER_REGISTRATION_VERSION, '1.1.3', '>=' ) ? true : false, |
| 78 | ); |
| 79 | |
| 80 | wp_localize_script( 'everest-forms-block-editor', '_EVF_BLOCKS_', $form_block_data ); |
| 81 | $action_page = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 82 | |
| 83 | if ( 'edit' === $action_page ) { |
| 84 | wp_enqueue_script( 'everest-forms-shortcode-embed-form' ); |
| 85 | } |
| 86 | wp_enqueue_script( 'everest-forms-block-editor' ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Add "Everest Forms" category to the blocks listing in post edit screen. |
| 91 | * |
| 92 | * @param array $block_categories All registered block categories. |
| 93 | * @return array |
| 94 | * @since 2.0.9 |
| 95 | */ |
| 96 | public function block_categories( array $block_categories ) { |
| 97 | |
| 98 | return array_merge( |
| 99 | array( |
| 100 | array( |
| 101 | 'slug' => 'everest-forms', |
| 102 | 'title' => esc_html__( 'Everest Forms', 'everest-forms' ), |
| 103 | ), |
| 104 | ), |
| 105 | $block_categories |
| 106 | ); |
| 107 | } |
| 108 | /** |
| 109 | * Register block types. |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | public function register_block_types() { |
| 114 | $block_types = $this->get_block_types(); |
| 115 | foreach ( $block_types as $block_type ) { |
| 116 | new $block_type(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get block types. |
| 122 | * |
| 123 | * @return AbstractBlock[] |
| 124 | */ |
| 125 | private function get_block_types() { |
| 126 | $is_pro = defined( 'EFP_VERSION' ) && version_compare( EFP_VERSION, '1.7.3', '>=' ) ? true : false; |
| 127 | $is_frontendlisting_active = defined( 'EVF_FRONTEND_LISTING_VERSION' ) && version_compare( EVF_FRONTEND_LISTING_VERSION, '1.0.0', '>=' ) ? true : false; |
| 128 | $is_use_registration_active = defined( 'EVF_USER_REGISTRATION_VERSION' ) && version_compare( EVF_USER_REGISTRATION_VERSION, '1.1.3', '>=' ) ? true : false; |
| 129 | $class = array( |
| 130 | EVF_Blocks_Form_Selector::class, //phpcs:ignore; |
| 131 | ); |
| 132 | if ( $is_pro && $is_frontendlisting_active ) { |
| 133 | $class[]= EVF_Blocks_Frontend_Listing::class; //phpcs:ignore; |
| 134 | } |
| 135 | if ( $is_pro && $is_use_registration_active ) { |
| 136 | $class[]= EVF_Blocks_User_Login::class; //phpcs:ignore; |
| 137 | } |
| 138 | return apply_filters( |
| 139 | 'everest_forms_block_types', |
| 140 | $class |
| 141 | ); |
| 142 | } |
| 143 | } |
| 144 | return new EVF_Blocks(); |
| 145 |