Skip to content

Commit

Permalink
Merge pull request #1418 from rocklobster-in/dev/5.9
Browse files Browse the repository at this point in the history
Dev/5.9
  • Loading branch information
takayukister authored May 6, 2024
2 parents 2867463 + 5236ec8 commit 006c01e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 43 deletions.
38 changes: 2 additions & 36 deletions includes/block-editor/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function wpcf7_init_block_editor_assets() {

$assets = wp_parse_args( $assets, array(
'dependencies' => array(
'react',
'wp-api-fetch',
'wp-block-editor',
'wp-blocks',
Expand All @@ -45,42 +46,7 @@ function wpcf7_init_block_editor_assets() {
register_block_type(
wpcf7_plugin_path( 'includes/block-editor' ),
array(
'editor_script' => 'contact-form-7-block-editor',
'editor_script_handles' => 'contact-form-7-block-editor',
)
);
}


add_action(
'enqueue_block_editor_assets',
'wpcf7_enqueue_block_editor_assets',
10, 0
);

function wpcf7_enqueue_block_editor_assets() {
$contact_forms = array_map(
static function ( $contact_form ) {
return array(
'id' => $contact_form->id(),
'hash' => $contact_form->hash(),
'slug' => $contact_form->name(),
'title' => $contact_form->title(),
'locale' => $contact_form->locale(),
);
},
WPCF7_ContactForm::find( array(
'posts_per_page' => 20,
'orderby' => 'modified',
'order' => 'DESC',
) )
);

wp_add_inline_script(
'contact-form-7-block-editor',
sprintf(
'window.wpcf7 = {contactForms:%s};',
wp_json_encode( $contact_forms )
),
'before'
);
}
2 changes: 1 addition & 1 deletion includes/block-editor/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions includes/block-editor/src/edit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';

import {
Expand Down Expand Up @@ -31,9 +31,15 @@ export default function ContactFormSelectorEdit( { attributes, setAttributes } )
};

const [ contactForms, setContactForms ] = useState(
() => createMap( window.wpcf7.contactForms ?? [] )
() => createMap( [] )
);

useEffect( () => {
fetchContactForms().then( response => {
setContactForms( createMap( response ) );
} );
}, [] );

const blockProps = {
className: 'components-placeholder',
style: {
Expand Down
4 changes: 0 additions & 4 deletions includes/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps } from '@wordpress/block-editor';

window.wpcf7 = window.wpcf7 ?? {
contactForms: [],
};

import icon from './icon';
import edit from './edit';
import transforms from './transforms';
Expand Down

0 comments on commit 006c01e

Please sign in to comment.