-
Notifications
You must be signed in to change notification settings - Fork 2
/
hubspot-form-block.php
105 lines (90 loc) · 2.48 KB
/
hubspot-form-block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Plugin Name: Hubspot Form Block
* Plugin URI: https://github.com/humanmade/hubspot-form-block
* Description: Hubspot form embed block with configuration options
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.2.2
* Author: Human Made Limited
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: hubspot-form-block
*
* @package hubspot-form-block
*/
namespace HM\HubspotFormBlock;
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function block_init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', __NAMESPACE__ . '\\block_init' );
function register_scripts() {
wp_register_script(
'hs-forms',
'https://js.hsforms.net/forms/v2.js',
[],
null,
[ 'strategy' => 'async' ]
);
}
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\\register_scripts' );
function inject_onload_handler( $tag, $handle ) {
if ( 'hs-forms' !== $handle ) {
return $tag;
}
$tag = str_replace( '></script>', ' onload="window.dispatchEvent(new CustomEvent(\'hubspotFormsReady\'));"></script>', $tag );
return $tag;
}
add_filter( 'script_loader_tag', __NAMESPACE__ . '\\inject_onload_handler', 10, 2 );
function enqueue_scripts() {
$portal_id = get_option( 'hubspot_embed_portal_id' );
if ( empty( $portal_id ) ) {
return;
}
$region = get_option( 'hubspot_embed_region', 'eu1' );
wp_enqueue_script(
'hs-script-loader',
sprintf( 'https://js-%s.hs-scripts.com/%s.js', $region, $portal_id ),
[],
null,
[
'strategy' => 'async',
'in_footer' => true,
]
);
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts' );
function rest_api() {
register_setting(
'hubspot_embed',
'hubspot_embed_portal_id',
[
'type' => 'number',
'sanitize_callback' => 'absint',
'show_in_rest' => true,
]
);
register_setting(
'hubspot_embed',
'hubspot_embed_region',
[
'type' => 'string',
'sanitize_callback' => function ( $value ) {
if ( in_array( $value, [ 'eu1', 'na1' ], true ) ) {
return $value;
}
return 'eu1';
},
'show_in_rest' => true,
'default' => 'eu1',
]
);
}
add_action( 'rest_api_init', __NAMESPACE__ . '\\rest_api' );