| 1 |
<?php |
| 2 |
|
| 3 |
class HappyForms_Block { |
| 4 |
|
| 5 |
/** |
| 6 |
* The singleton instance. |
| 7 |
* |
| 8 |
* @var HappyForms_Block |
| 9 |
*/ |
| 10 |
private static $instance; |
| 11 |
|
| 12 |
/** |
| 13 |
* The singleton constructor. |
| 14 |
* |
| 15 |
* @return HappyForms_Block |
| 16 |
*/ |
| 17 |
public static function instance() { |
| 18 |
if ( is_null( self::$instance ) ) { |
| 19 |
self::$instance = new self(); |
| 20 |
} |
| 21 |
|
| 22 |
self::$instance->hook(); |
| 23 |
|
| 24 |
return self::$instance; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register hooks. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function hook() { |
| 33 |
add_action( 'init', array( $this, 'register' ) ); |
| 34 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
private function get_attributes() { |
| 38 |
$attributes = array( |
| 39 |
'id' => array( |
| 40 |
'type' => 'string', |
| 41 |
), |
| 42 |
'anchor' => array( |
| 43 |
'type' => 'string', |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
return $attributes; |
| 48 |
} |
| 49 |
|
| 50 |
private function get_properties() { |
| 51 |
$properties = array( |
| 52 |
'title' => __( 'Forms', 'happyforms' ), |
| 53 |
'description' => __( 'Displays a form.', 'happyforms' ), |
| 54 |
'category' => 'widgets', |
| 55 |
'icon' => 'feedback', |
| 56 |
'keywords' => array( |
| 57 |
'form', 'contact', 'email', |
| 58 |
), |
| 59 |
); |
| 60 |
|
| 61 |
return $properties; |
| 62 |
} |
| 63 |
|
| 64 |
public function register() { |
| 65 |
register_block_type( 'thethemefoundry/happyforms', array( |
| 66 |
'attributes' => $this->get_attributes(), |
| 67 |
'editor_script' => 'happyforms-block', |
| 68 |
'render_callback' => array( $this, 'render' ), |
| 69 |
) ); |
| 70 |
} |
| 71 |
|
| 72 |
public function render( $attrs ) { |
| 73 |
$block_classes = isset( $attrs['className'] ) ? esc_attr( trim( $attrs['className'] ) ) : ''; |
| 74 |
$html_id = isset( $attrs['anchor'] ) ? esc_attr( trim( $attrs['anchor'] ) ) : ''; |
| 75 |
|
| 76 |
if ( isset( $attrs['id'] ) && empty( $attrs['id'] ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ( '' !== $block_classes ) { |
| 81 |
$block_classes = explode( ' ', $block_classes ); |
| 82 |
|
| 83 |
add_filter( 'happyforms_form_class', function( $class, $form ) use ( $block_classes ) { |
| 84 |
$class = array_merge( $class, $block_classes ); |
| 85 |
return $class; |
| 86 |
}, 10, 2 ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( '' !== $html_id ) { |
| 90 |
add_filter( 'happyforms_form_id', function( $id ) use ( $html_id ) { |
| 91 |
return $html_id; |
| 92 |
}, 10, 2 ); |
| 93 |
} |
| 94 |
|
| 95 |
return HappyForms()->handle_shortcode( $attrs ); |
| 96 |
} |
| 97 |
|
| 98 |
public function enqueue_scripts() { |
| 99 |
$asset_file = require( happyforms_get_core_folder() . '/assets/jsx/build/admin/block.asset.php' ); |
| 100 |
|
| 101 |
wp_enqueue_script( |
| 102 |
'happyforms-block', |
| 103 |
happyforms_get_plugin_url() . 'core/assets/jsx/build/admin/block.js', |
| 104 |
$asset_file['dependencies'] |
| 105 |
); |
| 106 |
|
| 107 |
$forms = happyforms_get_form_controller()->get(); |
| 108 |
$forms = array_values( wp_list_filter( $forms, array( 'post_status' => 'publish' ) ) ); |
| 109 |
$forms = array_map( function ( $form ) { |
| 110 |
return array( |
| 111 |
'ID' => $form['ID'], |
| 112 |
'post_title' => ( happyforms_get_form_title( $form ) ) |
| 113 |
); |
| 114 |
}, $forms ); |
| 115 |
$block_properties = $this->get_properties(); |
| 116 |
$data = array( |
| 117 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 118 |
'forms' => $forms, |
| 119 |
'block' => $block_properties, |
| 120 |
); |
| 121 |
|
| 122 |
wp_localize_script( 'happyforms-block', '_happyFormsBlockSettings', $data ); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
if ( ! function_exists( 'happyforms_get_block' ) ): |
| 128 |
/** |
| 129 |
* Get the HappyForms_Block class instance. |
| 130 |
* |
| 131 |
* @return HappyForms_Block |
| 132 |
*/ |
| 133 |
function happyforms_get_block() { |
| 134 |
return HappyForms_Block::instance(); |
| 135 |
} |
| 136 |
|
| 137 |
endif; |
| 138 |
|
| 139 |
/** |
| 140 |
* Initialize the HappyForms_Block class immediately. |
| 141 |
*/ |
| 142 |
happyforms_get_block(); |
| 143 |
|