handle inline scripts

This commit is contained in:
paul sealock 2024-08-29 15:17:55 +12:00
parent 480771e5ca
commit a9c6b4bc53
3 changed files with 28 additions and 1 deletions

View File

@ -11,6 +11,20 @@ import { useDispatch } from '@wordpress/data';
*/
import { SettingsCheckbox, SettingsInput } from './components';
const SettingsScript = ( { content } ) => {
useEffect( () => {
const scriptElement = document.createElement( 'script' );
scriptElement.type = 'text/javascript';
scriptElement.innerHTML = content;
document.body.appendChild( scriptElement );
return () => {
document.body.removeChild( scriptElement );
};
}, [] );
return null;
};
export const Content = ( { data } ) => {
const { settings } = data;
console.log( settings );
@ -135,6 +149,8 @@ export const Content = ( { data } ) => {
/>
</div>
);
case 'script':
return <SettingsScript content={ setting.content } />;
default:
return null;
}

View File

@ -39,7 +39,6 @@ const appendSettingsScripts = () => {
return Object.entries( settingsScripts ).reduce(
( scripts, [ key, script ] ) => {
if ( ! ignoredSettingsScripts.includes( key ) ) {
console.log( 'script', key );
const scriptElement = document.createElement( 'script' );
scriptElement.src = script.src;

View File

@ -170,6 +170,18 @@ if ( ! class_exists( 'WC_Settings_Page', false ) ) :
}
// Otherwise, render the page's output method.
} else {
if ( 'classes' === $section_id ) {
$tags = new WP_HTML_Tag_Processor( $html );
while( $tags->next_tag( array( 'tag_name' => 'script' ) ) ) {
$script_type = $tags->get_attribute( 'type' );
$script_contents = $tags->get_modifiable_text();
$section_settings_data[] = array(
'type' => 'script',
'content' => $script_contents,
);
}
}
$section_settings_data[] = array(
'type' => 'custom',
'content' => trim( $html ),