| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Widgets; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Form\FormHandler; |
| 10 |
|
| 11 |
final class RegisterGutenBlock |
| 12 |
{ |
| 13 |
public function register() |
| 14 |
{ |
| 15 |
if (!function_exists('register_block_type')) { |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
// self::gutenbergBlock(); |
| 20 |
|
| 21 |
add_action('enqueue_block_editor_assets', function () { |
| 22 |
wp_enqueue_script( |
| 23 |
'bitforms-gutenberg-block', |
| 24 |
BITFORMS_ROOT_URI . '/assets/gutenberg-shortcode-block.js', |
| 25 |
[], |
| 26 |
BITFORMS_VERSION, |
| 27 |
true |
| 28 |
); |
| 29 |
|
| 30 |
$formHandler = FormHandler::getInstance(); |
| 31 |
$all_forms = $formHandler->admin->getAllForm(); |
| 32 |
$bitformsForms = apply_filters( |
| 33 |
'bitforms_localize_block_script', |
| 34 |
[ |
| 35 |
'forms' => $all_forms |
| 36 |
] |
| 37 |
); |
| 38 |
|
| 39 |
$bitformsForms['ajaxUrl'] = admin_url('admin-ajax.php'); |
| 40 |
$bitformsForms['nonce'] = wp_create_nonce('bitforms_save'); |
| 41 |
|
| 42 |
wp_localize_script('bitforms-gutenberg-block', 'bitformsBlock', $bitformsForms); |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
public function shortcodeBlock() |
| 47 |
{ |
| 48 |
wp_enqueue_script( |
| 49 |
'bitforms-gutenberg-block', |
| 50 |
BITFORMS_ROOT_URI . '/assets/gutenberg-shortcode-block.js', |
| 51 |
[], |
| 52 |
BITFORMS_VERSION, |
| 53 |
true |
| 54 |
); |
| 55 |
|
| 56 |
$formHandler = FormHandler::getInstance(); |
| 57 |
$all_forms = $formHandler->admin->getAllForm(); |
| 58 |
$bitformsForms = apply_filters( |
| 59 |
'bitforms_localize_block_script', |
| 60 |
[ |
| 61 |
'forms' => $all_forms |
| 62 |
] |
| 63 |
); |
| 64 |
|
| 65 |
wp_localize_script('bitforms-gutenberg-block', 'bitformsBlock', $bitformsForms); |
| 66 |
} |
| 67 |
|
| 68 |
public static function gutenbergBlock() |
| 69 |
{ |
| 70 |
// TODO: Attention: This function is temporary block. |
| 71 |
// if (!function_exists('register_block_type')) { |
| 72 |
// return; |
| 73 |
// } |
| 74 |
|
| 75 |
register_block_type('bitforms/form-shortcode', [ |
| 76 |
'render_callback' => function ($attributes, $content) { |
| 77 |
$formId = $attributes['formID'] ?? ''; |
| 78 |
// error_log('form id' . $formId); |
| 79 |
// error_log('content' . $content); |
| 80 |
|
| 81 |
if (empty($formId)) { |
| 82 |
return ''; |
| 83 |
} |
| 84 |
|
| 85 |
// $formHandler = FormHandler::getInstance(); |
| 86 |
// $form = $formHandler->admin->getForm($formId); |
| 87 |
// if (empty($form)) { |
| 88 |
// return ''; |
| 89 |
// } |
| 90 |
|
| 91 |
if ($content) { |
| 92 |
return $content; |
| 93 |
} |
| 94 |
$shortCode = '[bitform id=\'' . esc_attr($formId) . '\']'; |
| 95 |
return do_shortcode($shortCode); |
| 96 |
}, |
| 97 |
'attributes' => [ |
| 98 |
'formId' => [ |
| 99 |
'type' => 'string', |
| 100 |
'default' => '', |
| 101 |
], |
| 102 |
], |
| 103 |
]); |
| 104 |
} |
| 105 |
} |
| 106 |
|