Created
February 26, 2024 16:13
-
-
Save danielpost/78ffac10dc1d4d4ca3217846014b5ee0 to your computer and use it in GitHub Desktop.
Example ConversionWP controls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { addFilter } from '@wordpress/hooks'; | |
import { __ } from '@wordpress/i18n'; | |
import { createHigherOrderComponent } from '@wordpress/compose'; | |
import { InspectorControls } from '@wordpress/block-editor'; | |
import { PanelBody, ToggleControl, TextControl } from '@wordpress/components'; | |
const addAttributes = ( settings, name ) => { | |
if ( name !== 'core/button' ) { | |
return settings; | |
} | |
return { | |
...settings, | |
attributes: { | |
...settings.attributes, | |
conversionTracking: { | |
type: 'boolean', | |
default: false, | |
}, | |
conversionLabel: { | |
type: 'string', | |
default: '', | |
}, | |
}, | |
}; | |
}; | |
const withConversionControls = createHigherOrderComponent( ( BlockEdit ) => { | |
return ( props ) => { | |
const { name, attributes, setAttributes } = props; | |
if ( name !== 'core/button' ) { | |
return <BlockEdit { ...props } />; | |
} | |
return ( | |
<> | |
<BlockEdit { ...props } /> | |
<InspectorControls> | |
<PanelBody | |
title={ __( 'Conversion Tracking', 'conversionwp' ) } | |
> | |
<ToggleControl | |
label={ __( | |
'Enable Conversion Tracking', | |
'conversionwp' | |
) } | |
checked={ attributes.conversionTracking } | |
onChange={ ( value ) => | |
setAttributes( { conversionTracking: value } ) | |
} | |
/> | |
<TextControl | |
label={ __( | |
'Conversion Tracking Label', | |
'conversionwp' | |
) } | |
value={ attributes.conversionLabel } | |
onChange={ ( value ) => | |
setAttributes( { conversionLabel: value } ) | |
} | |
/> | |
</PanelBody> | |
</InspectorControls> | |
</> | |
); | |
}; | |
}, 'withConversionControls' ); | |
addFilter( | |
'blocks.registerBlockType', | |
'conversion-bridge/conversion-tracking-attributes', | |
addAttributes | |
); | |
addFilter( | |
'editor.BlockEdit', | |
'conversion-bridge/conversion-tracking-attributes', | |
withConversionControls | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment