| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Register the Contact Forms block for the block editor. |
| 6 |
*/ |
| 7 |
function accua_forms_register_block() { |
| 8 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
wp_register_script( |
| 13 |
'contact-forms-block-editor', |
| 14 |
plugins_url( 'block-editor/index.js', ACCUA_FORMS_FILE ), |
| 15 |
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-i18n', 'wp-server-side-render' ), |
| 16 |
ACCUA_FORMS_JS_VERSION, |
| 17 |
true |
| 18 |
); |
| 19 |
|
| 20 |
wp_set_script_translations( 'contact-forms-block-editor', 'contact-forms', plugin_dir_path( ACCUA_FORMS_FILE ) . 'languages' ); |
| 21 |
|
| 22 |
// Pass form list to the block editor script |
| 23 |
$forms = get_option( 'accua_forms_saved_forms', array() ); |
| 24 |
$form_list = array(); |
| 25 |
foreach ( $forms as $fid => $form_data ) { |
| 26 |
$title = ! empty( $form_data['title'] ) ? $form_data['title'] : __( '(untitled)', 'contact-forms' ); |
| 27 |
$form_list[] = array( |
| 28 |
'text' => $title, |
| 29 |
'value' => (string) $fid, |
| 30 |
); |
| 31 |
} |
| 32 |
usort( $form_list, function( $a, $b ) { |
| 33 |
return (int) $b['value'] - (int) $a['value']; |
| 34 |
} ); |
| 35 |
|
| 36 |
wp_add_inline_script( |
| 37 |
'contact-forms-block-editor', |
| 38 |
'var accua_forms_block_data = ' . wp_json_encode( array( 'forms' => $form_list ) ) . ';', |
| 39 |
'before' |
| 40 |
); |
| 41 |
|
| 42 |
register_block_type( plugin_dir_path( ACCUA_FORMS_FILE ) . 'block-editor' ); |
| 43 |
} |
| 44 |
add_action( 'init', 'accua_forms_register_block' ); |
| 45 |
|