| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') || die(); |
| 4 |
|
| 5 |
class HashFormBlock { |
| 6 |
public static $stylesheet; |
| 7 |
public static $gfonts = array(); |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
add_action('init', array($this, 'register_block')); |
| 11 |
add_action('enqueue_block_editor_assets', array($this, 'enqueue_block_editor_assets')); |
| 12 |
add_action('enqueue_block_assets', array($this, 'enqueue_block_canvas_styles')); |
| 13 |
|
| 14 |
// Load translation files |
| 15 |
add_action('enqueue_block_editor_assets', array($this, 'block_localization')); |
| 16 |
} |
| 17 |
|
| 18 |
public function register_block() { |
| 19 |
$asset_file = include(HASHFORM_PATH . 'build/index.asset.php'); |
| 20 |
|
| 21 |
/* |
| 22 |
* No 'style' handle: css/form-block.css was an empty file, and naming |
| 23 |
* it here had WordPress request an empty stylesheet on every page that |
| 24 |
* carries the block. The block's own appearance comes from the form |
| 25 |
* styles, which the shortcode path loads as well. |
| 26 |
*/ |
| 27 |
wp_register_style('hfb-editor', HASHFORM_URL . 'css/editor.css', array(), HASHFORM_VERSION); |
| 28 |
wp_register_script('hfb-blocks', HASHFORM_URL . 'build/index.js', $asset_file['dependencies'], $asset_file['version'], false); |
| 29 |
|
| 30 |
register_block_type( |
| 31 |
'hash-form/form-selector', array( |
| 32 |
'api_version' => 2, |
| 33 |
'editor_script' => 'hfb-blocks', |
| 34 |
'editor_style' => 'hfb-editor', |
| 35 |
'attributes' => [ |
| 36 |
"id" => ["type" => "string"], |
| 37 |
"hfStyle" => ["type" => "string"], |
| 38 |
"formId" => ["type" => "string"], |
| 39 |
|
| 40 |
"enableCustomStyle" => ["type" => "boolean"], |
| 41 |
|
| 42 |
"formColumnGap" => ["type" => "string"], |
| 43 |
"formColumnGapUnit" => ["type" => "string", "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 44 |
|
| 45 |
"formRowGap" => ["type" => "string"], |
| 46 |
"formRowGapUnit" => ["type" => "string", "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 47 |
|
| 48 |
"labelTypoFamily" => ["type" => "string"], |
| 49 |
"labelTypoWeight" => ["type" => "string"], |
| 50 |
"labelTypoTextTransform" => ["type" => "string"], |
| 51 |
"labelTypoTextDecoration" => ["type" => "string"], |
| 52 |
"labelTypoFontSizeSm" => ["type" => "string"], |
| 53 |
"labelTypoFontSizeMd" => ["type" => "string"], |
| 54 |
"labelTypoFontSize" => ["type" => "string"], |
| 55 |
"labelTypoFontSizeUnit" => ["type" => "string", "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 56 |
"labelTypoLetterSpacingSm" => ["type" => "string"], |
| 57 |
"labelTypoLetterSpacingMd" => ["type" => "string"], |
| 58 |
"labelTypoLetterSpacing" => ["type" => "string"], |
| 59 |
"labelTypoLetterSpacingUnit" => ["type" => "string", "enum" => ['px', 'em', 'rem'], "default" => "px"], |
| 60 |
"labelTypoLineHeightSm" => ["type" => 'string'], |
| 61 |
"labelTypoLineHeightMd" => ["type" => "string"], |
| 62 |
"labelTypoLineHeight" => ["type" => "string"], |
| 63 |
"labelTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 64 |
"labelTypoFontColor" => ["type" => 'string'], |
| 65 |
"labelRequiredColor" => ["type" => 'string'], |
| 66 |
|
| 67 |
"labelSpacingTop" => ["type" => 'string'], |
| 68 |
"labelSpacingLeft" => ["type" => 'string'], |
| 69 |
"labelSpacingRight" => ["type" => 'string'], |
| 70 |
"labelSpacingBottom" => ["type" => 'string'], |
| 71 |
"labelSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 72 |
|
| 73 |
"descTypoFamily" => ["type" => 'string'], |
| 74 |
"descTypoWeight" => ["type" => 'string'], |
| 75 |
"descTypoTextTransform" => ["type" => 'string'], |
| 76 |
"descTypoTextDecoration" => ["type" => 'string'], |
| 77 |
"descTypoFontSizeSm" => ["type" => 'string'], |
| 78 |
"descTypoFontSizeMd" => ["type" => 'string'], |
| 79 |
"descTypoFontSize" => ["type" => 'string'], |
| 80 |
"descTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 81 |
"descTypoLetterSpacingSm" => ["type" => 'string'], |
| 82 |
"descTypoLetterSpacingMd" => ["type" => 'string'], |
| 83 |
"descTypoLetterSpacing" => ["type" => 'string'], |
| 84 |
"descTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 85 |
"descTypoLineHeightSm" => ["type" => 'string'], |
| 86 |
"descTypoLineHeightMd" => ["type" => 'string'], |
| 87 |
"descTypoLineHeight" => ["type" => 'string'], |
| 88 |
"descTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 89 |
|
| 90 |
"descTypoFontColor" => ["type" => 'string'], |
| 91 |
|
| 92 |
"descSpacingTop" => ["type" => 'string'], |
| 93 |
"descSpacingLeft" => ["type" => 'string'], |
| 94 |
"descSpacingRight" => ["type" => 'string'], |
| 95 |
"descSpacingBottom" => ["type" => 'string'], |
| 96 |
"descSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 97 |
|
| 98 |
"fieldTypoFamily" => ["type" => 'string'], |
| 99 |
"fieldTypoWeight" => ["type" => 'string'], |
| 100 |
"fieldTypoTextTransform" => ["type" => 'string'], |
| 101 |
"fieldTypoTextDecoration" => ["type" => 'string'], |
| 102 |
"fieldTypoFontSizeSm" => ["type" => 'string'], |
| 103 |
"fieldTypoFontSizeMd" => ["type" => 'string'], |
| 104 |
"fieldTypoFontSize" => ["type" => 'string'], |
| 105 |
"fieldTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 106 |
"fieldTypoLetterSpacingSm" => ["type" => 'string'], |
| 107 |
"fieldTypoLetterSpacingMd" => ["type" => 'string'], |
| 108 |
"fieldTypoLetterSpacing" => ["type" => 'string'], |
| 109 |
"fieldTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 110 |
"fieldTypoLineHeightSm" => ["type" => 'string'], |
| 111 |
"fieldTypoLineHeightMd" => ["type" => 'string'], |
| 112 |
"fieldTypoLineHeight" => ["type" => 'string'], |
| 113 |
"fieldTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 114 |
|
| 115 |
"fieldColorNormal" => ["type" => 'string'], |
| 116 |
"fieldBgColorNormal" => ["type" => 'string'], |
| 117 |
|
| 118 |
"fieldShadowNormalX" => ["type" => 'string'], |
| 119 |
"fieldShadowNormalY" => ["type" => 'string'], |
| 120 |
"fieldShadowNormalBlur" => ["type" => 'string'], |
| 121 |
"fieldShadowNormalSpread" => ["type" => 'string'], |
| 122 |
"fieldShadowNormalColor" => ["type" => 'string'], |
| 123 |
"fieldShadowNormalInset" => ["type" => 'string'], |
| 124 |
|
| 125 |
"fieldBorder" => ["type" => 'string'], |
| 126 |
|
| 127 |
"fieldBorderTop" => ["type" => 'string'], |
| 128 |
"fieldBorderLeft" => ["type" => 'string'], |
| 129 |
"fieldBorderRight" => ["type" => 'string'], |
| 130 |
"fieldBorderBottom" => ["type" => 'string'], |
| 131 |
"fieldBorderUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', 'vw'], "default" => 'px'], |
| 132 |
|
| 133 |
"fieldBorderColorNormal" => ["type" => 'string'], |
| 134 |
|
| 135 |
"fieldColorFocus" => ["type" => 'string'], |
| 136 |
"fieldBgColorFocus" => ["type" => 'string'], |
| 137 |
"fieldShadowFocusX" => ["type" => 'string'], |
| 138 |
"fieldShadowFocusY" => ["type" => 'string'], |
| 139 |
"fieldShadowFocusBlur" => ["type" => 'string'], |
| 140 |
"fieldShadowFocusSpread" => ["type" => 'string'], |
| 141 |
"fieldShadowFocusColor" => ["type" => 'string'], |
| 142 |
"fieldShadowFocusInset" => ["type" => 'string'], |
| 143 |
|
| 144 |
"fieldBorderFocusColor" => ["type" => 'string'], |
| 145 |
|
| 146 |
"fieldBorderRadiusTop" => ["type" => 'string'], |
| 147 |
"fieldBorderRadiusLeft" => ["type" => 'string'], |
| 148 |
"fieldBorderRadiusRight" => ["type" => 'string'], |
| 149 |
"fieldBorderRadiusBottom" => ["type" => 'string'], |
| 150 |
"fieldBorderRadiusUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 151 |
|
| 152 |
"fieldPaddingTop" => ["type" => 'string'], |
| 153 |
"fieldPaddingLeft" => ["type" => 'string'], |
| 154 |
"fieldPaddingRight" => ["type" => 'string'], |
| 155 |
"fieldPaddingBottom" => ["type" => 'string'], |
| 156 |
"fieldPaddingUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 157 |
|
| 158 |
"uploadTypoFamily" => ["type" => 'string'], |
| 159 |
"uploadTypoWeight" => ["type" => 'string'], |
| 160 |
"uploadTypoTextTransform" => ["type" => 'string'], |
| 161 |
"uploadTypoTextDecoration" => ["type" => 'string'], |
| 162 |
"uploadTypoFontSizeSm" => ["type" => 'string'], |
| 163 |
"uploadTypoFontSizeMd" => ["type" => 'string'], |
| 164 |
"uploadTypoFontSize" => ["type" => 'string'], |
| 165 |
"uploadTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 166 |
"uploadTypoLetterSpacingSm" => ["type" => 'string'], |
| 167 |
"uploadTypoLetterSpacingMd" => ["type" => 'string'], |
| 168 |
"uploadTypoLetterSpacing" => ["type" => 'string'], |
| 169 |
"uploadTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 170 |
"uploadTypoLineHeightSm" => ["type" => 'string'], |
| 171 |
"uploadTypoLineHeightMd" => ["type" => 'string'], |
| 172 |
"uploadTypoLineHeight" => ["type" => 'string'], |
| 173 |
"uploadTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 174 |
|
| 175 |
"uploadColorNormal" => ["type" => 'string'], |
| 176 |
"uploadBgColorNormal" => ["type" => 'string'], |
| 177 |
|
| 178 |
"uploadShadowNormalX" => ["type" => 'string'], |
| 179 |
"uploadShadowNormalY" => ["type" => 'string'], |
| 180 |
"uploadShadowNormalBlur" => ["type" => 'string'], |
| 181 |
"uploadShadowNormalSpread" => ["type" => 'string'], |
| 182 |
"uploadShadowNormalColor" => ["type" => 'string'], |
| 183 |
"uploadShadowNormalInset" => ["type" => 'string'], |
| 184 |
|
| 185 |
"uploadBorder" => ["type" => 'string'], |
| 186 |
"uploadBorderTop" => ["type" => 'string'], |
| 187 |
"uploadBorderLeft" => ["type" => 'string'], |
| 188 |
"uploadBorderRight" => ["type" => 'string'], |
| 189 |
"uploadBorderBottom" => ["type" => 'string'], |
| 190 |
"uploadBorderUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', 'vw'], "default" => 'px'], |
| 191 |
|
| 192 |
"uploadBorderColorNormal" => ["type" => 'string'], |
| 193 |
|
| 194 |
"uploadColorHover" => ["type" => 'string'], |
| 195 |
"uploadBgColorHover" => ["type" => 'string'], |
| 196 |
|
| 197 |
"uploadShadowHoverX" => ["type" => 'string'], |
| 198 |
"uploadShadowHoverY" => ["type" => 'string'], |
| 199 |
"uploadShadowHoverBlur" => ["type" => 'string'], |
| 200 |
"uploadShadowHoverSpread" => ["type" => 'string'], |
| 201 |
"uploadShadowHoverColor" => ["type" => 'string'], |
| 202 |
"uploadShadowHoverInset" => ["type" => 'string'], |
| 203 |
|
| 204 |
"uploadBorderColorHover" => ["type" => 'string'], |
| 205 |
|
| 206 |
"uploadBorderRadiusTop" => ["type" => 'string'], |
| 207 |
"uploadBorderRadiusLeft" => ["type" => 'string'], |
| 208 |
"uploadBorderRadiusRight" => ["type" => 'string'], |
| 209 |
"uploadBorderRadiusBottom" => ["type" => 'string'], |
| 210 |
"uploadBorderRadiusUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 211 |
|
| 212 |
"uploadPaddingTop" => ["type" => 'string'], |
| 213 |
"uploadPaddingLeft" => ["type" => 'string'], |
| 214 |
"uploadPaddingRight" => ["type" => 'string'], |
| 215 |
"uploadPaddingBottom" => ["type" => 'string'], |
| 216 |
"uploadPaddingUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 217 |
|
| 218 |
"buttonTypoFamily" => ["type" => 'string'], |
| 219 |
"buttonTypoWeight" => ["type" => 'string'], |
| 220 |
"buttonTypoTextTransform" => ["type" => 'string'], |
| 221 |
"buttonTypoTextDecoration" => ["type" => 'string'], |
| 222 |
"buttonTypoFontSizeSm" => ["type" => 'string'], |
| 223 |
"buttonTypoFontSizeMd" => ["type" => 'string'], |
| 224 |
"buttonTypoFontSize" => ["type" => 'string'], |
| 225 |
"buttonTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 226 |
"buttonTypoLetterSpacingSm" => ["type" => 'string'], |
| 227 |
"buttonTypoLetterSpacingMd" => ["type" => 'string'], |
| 228 |
"buttonTypoLetterSpacing" => ["type" => 'string'], |
| 229 |
"buttonTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 230 |
"buttonTypoLineHeightSm" => ["type" => 'string'], |
| 231 |
"buttonTypoLineHeightMd" => ["type" => 'string'], |
| 232 |
"buttonTypoLineHeight" => ["type" => 'string'], |
| 233 |
"buttonTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 234 |
|
| 235 |
"buttonColorNormal" => ["type" => 'string'], |
| 236 |
"buttonBgColorNormal" => ["type" => 'string'], |
| 237 |
|
| 238 |
"buttonShadowNormalX" => ["type" => 'string'], |
| 239 |
"buttonShadowNormalY" => ["type" => 'string'], |
| 240 |
"buttonShadowNormalBlur" => ["type" => 'string'], |
| 241 |
"buttonShadowNormalSpread" => ["type" => 'string'], |
| 242 |
"buttonShadowNormalColor" => ["type" => 'string'], |
| 243 |
"buttonShadowNormalInset" => ["type" => 'string'], |
| 244 |
|
| 245 |
"buttonBorder" => ["type" => 'string'], |
| 246 |
"buttonBorderTop" => ["type" => 'string'], |
| 247 |
"buttonBorderLeft" => ["type" => 'string'], |
| 248 |
"buttonBorderRight" => ["type" => 'string'], |
| 249 |
"buttonBorderBottom" => ["type" => 'string'], |
| 250 |
"buttonBorderUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', 'vw'], "default" => 'px'], |
| 251 |
"buttonBorderColorNormal" => ["type" => 'string'], |
| 252 |
|
| 253 |
"buttonColorHover" => ["type" => 'string'], |
| 254 |
"buttonBgColorHover" => ["type" => 'string'], |
| 255 |
|
| 256 |
"buttonShadowHoverX" => ["type" => 'string'], |
| 257 |
"buttonShadowHoverY" => ["type" => 'string'], |
| 258 |
"buttonShadowHoverBlur" => ["type" => 'string'], |
| 259 |
"buttonShadowHoverSpread" => ["type" => 'string'], |
| 260 |
"buttonShadowHoverColor" => ["type" => 'string'], |
| 261 |
"buttonShadowHoverInset" => ["type" => 'string'], |
| 262 |
|
| 263 |
"buttonBorderColorHover" => ["type" => 'string'], |
| 264 |
|
| 265 |
"buttonBorderRadiusTop" => ["type" => 'string'], |
| 266 |
"buttonBorderRadiusLeft" => ["type" => 'string'], |
| 267 |
"buttonBorderRadiusRight" => ["type" => 'string'], |
| 268 |
"buttonBorderRadiusBottom" => ["type" => 'string'], |
| 269 |
"buttonBorderRadiusUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 270 |
|
| 271 |
"buttonPaddingTop" => ["type" => 'string'], |
| 272 |
"buttonPaddingLeft" => ["type" => 'string'], |
| 273 |
"buttonPaddingRight" => ["type" => 'string'], |
| 274 |
"buttonPaddingBottom" => ["type" => 'string'], |
| 275 |
"buttonPaddingUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 276 |
|
| 277 |
"validationTypoFamily" => ["type" => 'string'], |
| 278 |
"validationTypoWeight" => ["type" => 'string'], |
| 279 |
"validationTypoTextTransform" => ["type" => 'string'], |
| 280 |
"validationTypoTextDecoration" => ["type" => 'string'], |
| 281 |
"validationTypoFontSizeSm" => ["type" => 'string'], |
| 282 |
"validationTypoFontSizeMd" => ["type" => 'string'], |
| 283 |
"validationTypoFontSize" => ["type" => 'string'], |
| 284 |
"validationTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 285 |
"validationTypoLetterSpacingSm" => ["type" => 'string'], |
| 286 |
"validationTypoLetterSpacingMd" => ["type" => 'string'], |
| 287 |
"validationTypoLetterSpacing" => ["type" => 'string'], |
| 288 |
"validationTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 289 |
"validationTypoLineHeightSm" => ["type" => 'string'], |
| 290 |
"validationTypoLineHeightMd" => ["type" => 'string'], |
| 291 |
"validationTypoLineHeight" => ["type" => 'string'], |
| 292 |
"validationTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 293 |
|
| 294 |
"validationTypoFontColor" => ["type" => 'string'], |
| 295 |
"validationTextalign" => ["type" => 'string'], |
| 296 |
|
| 297 |
"formTitleTypoFamily" => ["type" => 'string'], |
| 298 |
"formTitleTypoWeight" => ["type" => 'string'], |
| 299 |
"formTitleTypoTextTransform" => ["type" => 'string'], |
| 300 |
"formTitleTypoTextDecoration" => ["type" => 'string'], |
| 301 |
"formTitleTypoFontSizeSm" => ["type" => 'string'], |
| 302 |
"formTitleTypoFontSizeMd" => ["type" => 'string'], |
| 303 |
"formTitleTypoFontSize" => ["type" => 'string'], |
| 304 |
"formTitleTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 305 |
"formTitleTypoLetterSpacingSm" => ["type" => 'string'], |
| 306 |
"formTitleTypoLetterSpacingMd" => ["type" => 'string'], |
| 307 |
"formTitleTypoLetterSpacing" => ["type" => 'string'], |
| 308 |
"formTitleTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 309 |
"formTitleTypoLineHeightSm" => ["type" => 'string'], |
| 310 |
"formTitleTypoLineHeightMd" => ["type" => 'string'], |
| 311 |
"formTitleTypoLineHeight" => ["type" => 'string'], |
| 312 |
"formTitleTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 313 |
|
| 314 |
"formTitleTypoFontColor" => ["type" => 'string'], |
| 315 |
|
| 316 |
"formTitleSpacingTop" => ["type" => 'string'], |
| 317 |
"formTitleSpacingLeft" => ["type" => 'string'], |
| 318 |
"formTitleSpacingRight" => ["type" => 'string'], |
| 319 |
"formTitleSpacingBottom" => ["type" => 'string'], |
| 320 |
"formTitleSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 321 |
|
| 322 |
"formDescTypoFamily" => ["type" => 'string'], |
| 323 |
"formDescTypoWeight" => ["type" => 'string'], |
| 324 |
"formDescTypoTextTransform" => ["type" => 'string'], |
| 325 |
"formDescTypoTextDecoration" => ["type" => 'string'], |
| 326 |
"formDescTypoFontSizeSm" => ["type" => 'string'], |
| 327 |
"formDescTypoFontSizeMd" => ["type" => 'string'], |
| 328 |
"formDescTypoFontSize" => ["type" => 'string'], |
| 329 |
"formDescTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 330 |
"formDescTypoLetterSpacingSm" => ["type" => 'string'], |
| 331 |
"formDescTypoLetterSpacingMd" => ["type" => 'string'], |
| 332 |
"formDescTypoLetterSpacing" => ["type" => 'string'], |
| 333 |
"formDescTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 334 |
"formDescTypoLineHeightSm" => ["type" => 'string'], |
| 335 |
"formDescTypoLineHeightMd" => ["type" => 'string'], |
| 336 |
"formDescTypoLineHeight" => ["type" => 'string'], |
| 337 |
"formDescTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 338 |
|
| 339 |
"formDescTypoFontColor" => ["type" => 'string'], |
| 340 |
"formDescSpacingTop" => ["type" => 'string'], |
| 341 |
"formDescSpacingLeft" => ["type" => 'string'], |
| 342 |
"formDescSpacingRight" => ["type" => 'string'], |
| 343 |
"formDescSpacingBottom" => ["type" => 'string'], |
| 344 |
"formDescSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', '%'], "default" => 'px'], |
| 345 |
|
| 346 |
"headingTypoFamily" => ["type" => 'string'], |
| 347 |
"headingTypoWeight" => ["type" => 'string'], |
| 348 |
"headingTypoTextTransform" => ["type" => 'string'], |
| 349 |
"headingTypoTextDecoration" => ["type" => 'string'], |
| 350 |
"headingTypoFontSizeSm" => ["type" => 'string'], |
| 351 |
"headingTypoFontSizeMd" => ["type" => 'string'], |
| 352 |
"headingTypoFontSize" => ["type" => 'string'], |
| 353 |
"headingTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 354 |
"headingTypoLetterSpacingSm" => ["type" => 'string'], |
| 355 |
"headingTypoLetterSpacingMd" => ["type" => 'string'], |
| 356 |
"headingTypoLetterSpacing" => ["type" => 'string'], |
| 357 |
"headingTypoLetterSpacingUnit" => ["type" => 'string'], |
| 358 |
"headingTypoLineHeightSm" => ["type" => 'string'], |
| 359 |
"headingTypoLineHeightMd" => ["type" => 'string'], |
| 360 |
"headingTypoLineHeight" => ["type" => 'string'], |
| 361 |
"headingTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 362 |
|
| 363 |
"headingTypoFontColor" => ["type" => 'string'], |
| 364 |
|
| 365 |
"paragraphTypoFamily" => ["type" => 'string'], |
| 366 |
"paragraphTypoWeight" => ["type" => 'string'], |
| 367 |
"paragraphTypoTextTransform" => ["type" => 'string'], |
| 368 |
"paragraphTypoTextDecoration" => ["type" => 'string'], |
| 369 |
"paragraphTypoFontSizeSm" => ["type" => 'string'], |
| 370 |
"paragraphTypoFontSizeMd" => ["type" => 'string'], |
| 371 |
"paragraphTypoFontSize" => ["type" => 'string'], |
| 372 |
"paragraphTypoFontSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem', '%'], "default" => 'px'], |
| 373 |
"paragraphTypoLetterSpacingSm" => ["type" => 'string'], |
| 374 |
"paragraphTypoLetterSpacingMd" => ["type" => 'string'], |
| 375 |
"paragraphTypoLetterSpacing" => ["type" => 'string'], |
| 376 |
"paragraphTypoLetterSpacingUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 377 |
"paragraphTypoLineHeightSm" => ["type" => 'string'], |
| 378 |
"paragraphTypoLineHeightMd" => ["type" => 'string'], |
| 379 |
"paragraphTypoLineHeight" => ["type" => 'string'], |
| 380 |
"paragraphTypoLineHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 381 |
|
| 382 |
"paragraphTypoFontColor" => ["type" => 'string'], |
| 383 |
"dividerColor" => ["type" => 'string'], |
| 384 |
"starSize" => ["type" => 'string'], |
| 385 |
"starSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 386 |
|
| 387 |
"starColor" => ["type" => 'string'], |
| 388 |
"starColorActive" => ["type" => 'string'], |
| 389 |
|
| 390 |
"rangeHeight" => ["type" => 'string'], |
| 391 |
"rangeHeightUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 392 |
|
| 393 |
"rangeHandleSize" => ["type" => 'string'], |
| 394 |
"rangeHandleSizeUnit" => ["type" => 'string', "enum" => ['px', 'em', 'rem'], "default" => 'px'], |
| 395 |
|
| 396 |
"rangeColor" => ["type" => 'string'], |
| 397 |
"rangeColorActive" => ["type" => 'string'], |
| 398 |
"rangeHandleColor" => ["type" => 'string'], |
| 399 |
], |
| 400 |
'script' => 'hfb-script', |
| 401 |
'render_callback' => array($this, 'get_form_html') |
| 402 |
) |
| 403 |
); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* The form's own stylesheet, where the block is actually drawn. |
| 408 |
* |
| 409 |
* This is a dynamic block: the editor asks the server for its markup and |
| 410 |
* injects what comes back, so nothing on that path enqueues the form's |
| 411 |
* assets the way a front-end render does. |
| 412 |
* |
| 413 |
* It has to be this hook rather than enqueue_block_editor_assets. The |
| 414 |
* editor canvas is an iframe, and that hook only dresses the editor around |
| 415 |
* it - the stylesheet went into the outer document and never reached the |
| 416 |
* block. enqueue_block_assets is the one WordPress carries into the canvas. |
| 417 |
* |
| 418 |
* Admin only: on the front end these are enqueued when a form actually |
| 419 |
* renders, so a page without one still loads none of them. |
| 420 |
*/ |
| 421 |
public function enqueue_block_canvas_styles() { |
| 422 |
if (!is_admin()) { |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
// Registered here as well as enqueued, because the front-end |
| 427 |
// registration runs on wp_enqueue_scripts, which never fires in the |
| 428 |
// admin - the handles would otherwise not exist to enqueue. |
| 429 |
HashFormLoader::enqueue_styles(); |
| 430 |
HashFormLoader::enqueue_form_styles(); |
| 431 |
} |
| 432 |
|
| 433 |
public function enqueue_block_editor_assets() { |
| 434 |
// The forms list is only needed inside the block editor; querying it |
| 435 |
// on every request (init) was wasted work. |
| 436 |
$all_forms = HashFormHelper::get_all_forms_list_options(); |
| 437 |
unset($all_forms['']); |
| 438 |
|
| 439 |
wp_localize_script('hfb-blocks', 'hash_form_block_data', array( |
| 440 |
'forms' => $all_forms, |
| 441 |
'create_form_link' => esc_url(add_query_arg('page', 'hashform', admin_url('admin.php'))) |
| 442 |
)); |
| 443 |
} |
| 444 |
|
| 445 |
public function get_form_html($attr) { |
| 446 |
$form_id = !empty($attr['formId']) ? absint($attr['formId']) : 0; |
| 447 |
if (empty($form_id)) { |
| 448 |
return ''; |
| 449 |
} |
| 450 |
ob_start(); |
| 451 |
|
| 452 |
$enable_custom_style = isset($attr['enableCustomStyle']) ? $attr['enableCustomStyle'] : 'no'; |
| 453 |
|
| 454 |
if ($enable_custom_style == 'yes') { |
| 455 |
add_filter('hashform_form_classes', array($this, 'modify_class')); |
| 456 |
add_filter('hashform_enable_style', '__return_false'); |
| 457 |
self::$stylesheet = $this->get_stylesheet($attr); |
| 458 |
|
| 459 |
/* |
| 460 |
* On a page the stylesheet goes out with wp_footer, which keeps it |
| 461 |
* out of the middle of the content. |
| 462 |
* |
| 463 |
* The editor is different: it asks for this block through the |
| 464 |
* block-renderer endpoint and injects only what comes back, and no |
| 465 |
* footer ever runs there. The css has to travel with the markup, or |
| 466 |
* the block sits in the editor stripped of its default styling and |
| 467 |
* without the custom styling meant to replace it. |
| 468 |
*/ |
| 469 |
if (defined('REST_REQUEST') && REST_REQUEST) { |
| 470 |
// Already stripped of markup in get_stylesheet(), which is |
| 471 |
// where the attribute is read, so it cannot close the element. |
| 472 |
echo '<style>' . $this->strip_whitespace(self::$stylesheet) . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- css, sanitised at source. |
| 473 |
} else { |
| 474 |
add_action('wp_footer', array($this, 'print_stylesheet'), 11); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
if (!is_admin()) { |
| 479 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns a ready-escaped attribute string, and is echoed unescaped by core itself. |
| 480 |
echo '<div ' . get_block_wrapper_attributes(['class' => 'wp-block-hash-form', 'id' => esc_attr($attr['id'])]) . '>'; |
| 481 |
} |
| 482 |
|
| 483 |
echo do_shortcode('[hashform id="' . $form_id . '"]'); |
| 484 |
if (!is_admin()) { |
| 485 |
echo '</div>'; |
| 486 |
} |
| 487 |
|
| 488 |
if ($enable_custom_style == 'yes') { |
| 489 |
remove_filter('hashform_form_classes', array($this, 'modify_class')); |
| 490 |
remove_filter('hashform_enable_style', '__return_false'); |
| 491 |
} |
| 492 |
return ob_get_clean(); |
| 493 |
} |
| 494 |
|
| 495 |
public function modify_class($classes) { |
| 496 |
$remove_classes = array('hf-form-default-style', 'hf-form-no-style'); |
| 497 |
$classes = array_diff($classes, $remove_classes); |
| 498 |
$classes[] = 'hf-hashform-block'; |
| 499 |
$classes[] = 'hf-form-custom-style'; |
| 500 |
|
| 501 |
return $classes; |
| 502 |
} |
| 503 |
|
| 504 |
// Enqueue localization data for our blocks. |
| 505 |
public function block_localization() { |
| 506 |
if (function_exists('wp_set_script_translations')) { |
| 507 |
wp_set_script_translations('hfb-blocks', 'hash-form', HASHFORM_PATH . 'languages'); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
public function get_stylesheet($blockAttrs) { |
| 512 |
$block_css_arr = []; |
| 513 |
$block_css = ''; |
| 514 |
|
| 515 |
foreach ($blockAttrs as $attrs => $value) { |
| 516 |
$family = ''; |
| 517 |
$weight = ''; |
| 518 |
|
| 519 |
if (str_contains($attrs, 'Family')) { |
| 520 |
$family = $value; |
| 521 |
$weight_key = str_replace('Family', 'Weight', $attrs); |
| 522 |
$weight = isset($blockAttrs[$weight_key]) ? $blockAttrs[$weight_key] : ''; |
| 523 |
} |
| 524 |
|
| 525 |
if ($family && $family != 'inherit') { |
| 526 |
self::blocks_google_font($family, $weight ? str_replace('italic', 'i', $weight) : 400); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
// Get CSS for the Block. The attribute is arbitrary post content and |
| 531 |
// ends up inside a <style> tag, so make sure no markup can survive — |
| 532 |
// otherwise a "</style><script>" payload would execute for viewers. |
| 533 |
if (isset($blockAttrs['hfStyle'])) { |
| 534 |
$block_style = is_array($blockAttrs['hfStyle']) ? '' : $blockAttrs['hfStyle']; |
| 535 |
$block_css_arr[$blockAttrs['id']] = str_replace('<', '', wp_strip_all_tags($block_style)); |
| 536 |
} |
| 537 |
|
| 538 |
foreach ($block_css_arr as $val) { |
| 539 |
$block_css .= $val; |
| 540 |
} |
| 541 |
return $block_css; |
| 542 |
} |
| 543 |
|
| 544 |
public static function blocks_google_font($font_family, $font_weight, $font_subset = NULL) { |
| 545 |
if (strtolower($font_family) != 'inherit') { |
| 546 |
|
| 547 |
if (!array_key_exists($font_family, self::$gfonts)) { |
| 548 |
$add_font = array( |
| 549 |
'fontfamily' => $font_family, |
| 550 |
'fontvariants' => (isset($font_weight) && !empty($font_weight) ? array($font_weight) : array()), |
| 551 |
'fontsubsets' => (isset($font_subset) && !empty($font_subset) ? array($font_subset) : array()), |
| 552 |
); |
| 553 |
self::$gfonts[$font_family] = $add_font; |
| 554 |
|
| 555 |
} else { |
| 556 |
if (isset($font_weight) && ($font_weight != 'inherit') && !empty($font_weight)) { |
| 557 |
if (!in_array($font_weight, self::$gfonts[$font_family]['fontvariants'], true)) { |
| 558 |
array_push(self::$gfonts[$font_family]['fontvariants'], $font_weight); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
if (isset($font_subset) && !empty($font_subset)) { |
| 563 |
if (!in_array($font_subset, self::$gfonts[$font_family]['fontsubsets'], true)) { |
| 564 |
array_push(self::$gfonts[$font_family]['fontsubsets'], $font_subset); |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
public function print_stylesheet() { |
| 572 |
|
| 573 |
if (is_null(self::$stylesheet) || '' === self::$stylesheet) { |
| 574 |
return; |
| 575 |
} |
| 576 |
|
| 577 |
wp_register_style('sb-style-frontend', false, array(), HASHFORM_VERSION); |
| 578 |
wp_enqueue_style('sb-style-frontend'); |
| 579 |
wp_add_inline_style('sb-style-frontend', $this->strip_whitespace(self::$stylesheet)); |
| 580 |
$frontend_gfonts = $this->frontend_gfonts(); |
| 581 |
wp_enqueue_style('sb-fonts-frontend', $frontend_gfonts, array(), HASHFORM_VERSION); |
| 582 |
} |
| 583 |
|
| 584 |
public function strip_whitespace($css) { |
| 585 |
$replace = array( |
| 586 |
"#/\*.*?\*/#s" => "", // Strip C style comments. |
| 587 |
"#\s\s+#" => " ", // Strip excess whitespace. |
| 588 |
); |
| 589 |
$search = array_keys($replace); |
| 590 |
$css = preg_replace($search, $replace, $css); |
| 591 |
|
| 592 |
$replace = array( |
| 593 |
": " => ":", |
| 594 |
"; " => ";", |
| 595 |
" {" => "{", |
| 596 |
" }" => "}", |
| 597 |
", " => ",", |
| 598 |
"{ " => "{", |
| 599 |
";}" => "}", // Strip optional semicolons. |
| 600 |
",\n" => ",", // Don't wrap multiple selectors. |
| 601 |
"\n}" => "}", // Don't wrap closing braces. |
| 602 |
"} " => "}\n", // Put each rule on it's own line. |
| 603 |
); |
| 604 |
$search = array_keys($replace); |
| 605 |
$css = str_replace($search, $replace, $css); |
| 606 |
|
| 607 |
return trim($css); |
| 608 |
} |
| 609 |
|
| 610 |
public function frontend_gfonts() { |
| 611 |
if (empty(self::$gfonts)) { |
| 612 |
return; |
| 613 |
} |
| 614 |
$link = ''; |
| 615 |
$subsets = array(); |
| 616 |
|
| 617 |
foreach (self::$gfonts as $key => $gfont_values) { |
| 618 |
if (!empty($link)) { |
| 619 |
$link .= '%7C'; // Append a new font to the string. |
| 620 |
} |
| 621 |
$link .= $gfont_values['fontfamily']; |
| 622 |
if (!empty($gfont_values['fontvariants'])) { |
| 623 |
$link .= ':'; |
| 624 |
$link .= implode(',', $gfont_values['fontvariants']); |
| 625 |
} |
| 626 |
|
| 627 |
if (!empty($gfont_values['fontsubsets'])) { |
| 628 |
foreach ($gfont_values['fontsubsets'] as $subset) { |
| 629 |
if (!in_array($subset, $subsets, true)) { |
| 630 |
array_push($subsets, $subset); |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
if (!empty($subsets)) { |
| 637 |
$link .= '&subset=' . implode(',', $subsets); |
| 638 |
} |
| 639 |
|
| 640 |
return '//fonts.googleapis.com/css?family=' . esc_attr(str_replace('|', '%7C', $link)); |
| 641 |
} |
| 642 |
|
| 643 |
} |
| 644 |
|
| 645 |
new HashFormBlock(); |
| 646 |
|