abstracts
6 years ago
admin
6 years ago
export
6 years ago
fields
6 years ago
interfaces
8 years ago
libraries
7 years ago
log-handlers
8 years ago
shortcodes
6 years ago
templates
7 years ago
class-everest-forms.php
6 years ago
class-evf-ajax.php
6 years ago
class-evf-autoloader.php
7 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
8 years ago
class-evf-deprecated-action-hooks.php
7 years ago
class-evf-deprecated-filter-hooks.php
7 years ago
class-evf-emails.php
7 years ago
class-evf-fields.php
7 years ago
class-evf-form-block.php
6 years ago
class-evf-form-handler.php
6 years ago
class-evf-form-task.php
6 years ago
class-evf-forms-features.php
7 years ago
class-evf-frontend-scripts.php
7 years ago
class-evf-install.php
6 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
8 years ago
class-evf-post-types.php
7 years ago
class-evf-privacy.php
7 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
7 years ago
class-evf-smart-tags.php
7 years ago
class-evf-template-loader.php
7 years ago
class-evf-validation.php
8 years ago
evf-conditional-functions.php
7 years ago
evf-core-functions.php
6 years ago
evf-deprecated-functions.php
7 years ago
evf-entry-functions.php
6 years ago
evf-formatting-functions.php
7 years ago
evf-notice-functions.php
6 years ago
evf-template-functions.php
7 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
6 years ago
class-evf-form-block.php
159 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForm Gutenberg blocks |
| 4 | * |
| 5 | * @package EverstForms\Class |
| 6 | * @version 1.3.4 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Guten Block Class. |
| 13 | */ |
| 14 | class EVF_Form_Block { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | add_action( 'init', array( $this, 'register_block' ) ); |
| 21 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Register the block and its scripts. |
| 26 | */ |
| 27 | public function register_block() { |
| 28 | if ( ! function_exists( 'register_block_type' ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | register_block_type( |
| 33 | 'everest-forms/form-selector', |
| 34 | array( |
| 35 | 'attributes' => array( |
| 36 | 'formId' => array( |
| 37 | 'type' => 'string', |
| 38 | ), |
| 39 | 'className' => array( |
| 40 | 'type' => 'string', |
| 41 | ), |
| 42 | 'displayTitle' => array( |
| 43 | 'type' => 'boolean', |
| 44 | ), |
| 45 | 'displayDescription' => array( |
| 46 | 'type' => 'boolean', |
| 47 | ), |
| 48 | ), |
| 49 | 'editor_style' => 'everest-forms-block-editor', |
| 50 | 'editor_script' => 'everest-forms-block-editor', |
| 51 | 'render_callback' => array( $this, 'get_form_html' ), |
| 52 | ) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Load Gutenberg block scripts. |
| 58 | */ |
| 59 | public function enqueue_block_editor_assets() { |
| 60 | wp_register_style( |
| 61 | 'everest-forms-block-editor', |
| 62 | EVF()->plugin_url() . '/assets/css/everest-forms.css', |
| 63 | array( 'wp-edit-blocks' ), |
| 64 | defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( EVF()->plugin_path() . '/assets/css/everest-forms.css' ) : EVF_VERSION |
| 65 | ); |
| 66 | |
| 67 | wp_register_script( |
| 68 | 'everest-forms-block-editor', |
| 69 | EVF()->plugin_url() . '/assets/js/admin/gutenberg/form-block.min.js', |
| 70 | array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-editor', 'wp-components' ), |
| 71 | defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( EVF()->plugin_path() . '/assets/js/admin/gutenberg/form-block.min.js' ) : EVF_VERSION, |
| 72 | true |
| 73 | ); |
| 74 | |
| 75 | $form_block_data = array( |
| 76 | 'forms' => EVF()->form->get( '', array( 'order' => 'DESC' ) ), |
| 77 | 'i18n' => array( |
| 78 | 'title' => esc_html__( 'Everest Forms', 'everest-forms' ), |
| 79 | 'description' => esc_html__( 'Select and display one of your forms.', 'everest-forms' ), |
| 80 | 'form_keywords' => array( |
| 81 | esc_html__( 'form', 'everest-forms' ), |
| 82 | esc_html__( 'contact', 'everest-forms' ), |
| 83 | esc_html__( 'survey', 'everest-forms' ), |
| 84 | ), |
| 85 | 'form_select' => esc_html__( 'Select a Form', 'everest-forms' ), |
| 86 | 'form_settings' => esc_html__( 'Form Settings', 'everest-forms' ), |
| 87 | 'form_selected' => esc_html__( 'Form', 'everest-forms' ), |
| 88 | 'show_title' => esc_html__( 'Show Title', 'everest-forms' ), |
| 89 | 'show_description' => esc_html__( 'Show Description', 'everest-forms' ), |
| 90 | ), |
| 91 | ); |
| 92 | wp_localize_script( 'everest-forms-block-editor', 'evf_form_block_data', $form_block_data ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get form HTML to display in a Gutenberg block. |
| 97 | * |
| 98 | * @param array $attr Attributes passed by Gutenberg block. |
| 99 | * @return string |
| 100 | */ |
| 101 | public function get_form_html( $attr ) { |
| 102 | $form_id = ! empty( $attr['formId'] ) ? absint( $attr['formId'] ) : 0; |
| 103 | |
| 104 | if ( empty( $form_id ) ) { |
| 105 | return ''; |
| 106 | } |
| 107 | |
| 108 | // Wrapper classes. |
| 109 | $classes = 'everest-forms'; |
| 110 | if ( isset( $attr['className'] ) ) { |
| 111 | $classes .= ' ' . $attr['className']; |
| 112 | } |
| 113 | |
| 114 | $is_gb_editor = defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; |
| 115 | $title = ! empty( $attr['displayTitle'] ) ? true : false; |
| 116 | $description = ! empty( $attr['displayDescription'] ) ? true : false; |
| 117 | |
| 118 | // Disable form fields if called from the Gutenberg editor. |
| 119 | if ( $is_gb_editor ) { |
| 120 | add_filter( |
| 121 | 'everest_forms_frontend_container_class', |
| 122 | function ( $classes ) { |
| 123 | $classes[] = 'evf-gutenberg-form-selector'; |
| 124 | $classes[] = 'evf-container-full'; |
| 125 | return $classes; |
| 126 | } |
| 127 | ); |
| 128 | add_action( |
| 129 | 'everest_forms_frontend_output', |
| 130 | function () { |
| 131 | echo '<fieldset disabled>'; |
| 132 | }, |
| 133 | 3 |
| 134 | ); |
| 135 | add_action( |
| 136 | 'everest_forms_frontend_output', |
| 137 | function () { |
| 138 | echo '</fieldset>'; |
| 139 | }, |
| 140 | 30 |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | return EVF_Shortcodes::shortcode_wrapper( |
| 145 | array( 'EVF_Shortcode_Form', 'output' ), |
| 146 | array( |
| 147 | 'id' => $form_id, |
| 148 | 'title' => $title, |
| 149 | 'description' => $description, |
| 150 | ), |
| 151 | array( |
| 152 | 'class' => evf_sanitize_classes( $classes ), |
| 153 | ) |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | new EVF_Form_Block(); |
| 159 |