/** * Registers blocks in the Gutenberg editor. * * @since 1.9.6.5 * * @package ConvertKit * @author ConvertKit */ // Register Gutenberg Blocks if the Gutenberg Editor is loaded on screen. // This prevents JS errors if this script is accidentally enqueued on a non- // Gutenberg editor screen, or the Classic Editor Plugin is active. if ( typeof wp !== 'undefined' && typeof wp.blocks !== 'undefined' ) { // Register each ConvertKit Block in Gutenberg. for ( const block in convertkit_blocks ) { convertKitGutenbergRegisterBlock( convertkit_blocks[ block ] ); } // Register ConvertKit Pre-publish actions in Gutenberg. if ( typeof convertkit_pre_publish_actions !== 'undefined' ) { convertKitGutenbergRegisterPrePublishActions( convertkit_pre_publish_actions ); } } /** * Registers the given block in Gutenberg. * * @since 1.9.6.5 * * @param object block Block */ function convertKitGutenbergRegisterBlock( block ) { ( function ( blocks, editor, element, components ) { // Define some constants for the various items we'll use. const el = element.createElement; const { registerBlockType } = blocks; const { InspectorControls } = editor; const { Fragment, useState } = element; const { Button, Dashicon, TextControl, SelectControl, ToggleControl, Panel, PanelBody, PanelRow } = components; /** * Returns the icon to display for this block, depending * on the supplied block's configuration. * * @since 2.2.0 * * @return element|string */ const getIcon = function () { // Return a fallback default icon if none is specified for this block. if ( typeof block.gutenberg_icon === 'undefined' ) { return 'dashicons-tablet'; } // Return HTML element if the icon is an SVG string. if ( block.gutenberg_icon.search( 'svg' ) >= 0 ) { return element.RawHTML( { children: block.gutenberg_icon } ); } // Just return the string, as it's a dashicon CSS class. return block.gutenberg_icon; } /** * Return a field element for the block sidebar, which is displayed in a panel's row * when this block is being edited. * * @since 2.2.0 * * @param object props Block properties. * @param object field Field attributes. * @param string attribute Attribute name to store the field's data in. * @return array Field element */ const getField = function ( props, field, attribute ) { // Define some field properties shared across all field types. let fieldProperties = { id: 'convertkit_' + block.name + '_' + attribute, label: field.label, help: field.description, value: props.attributes[ attribute ], onChange: function ( value ) { if ( field.type === 'number' ) { // If value is a blank string i.e. no attribute value was provided, // cast it to the field's minimum number setting. // This prevents WordPress' block renderer API returning a 400 error // because a blank value will be passed as a string, when WordPress // expects it to be a numerical value. if ( value === '' ) { value = field.min; } // Cast value to integer if a value exists. if ( value.length > 0 ) { value = Number( value ); } } let newValue = {}; newValue[ attribute ] = value; props.setAttributes( newValue ); } }; // Define additional Field Properties and the Field Element, // depending on the Field Type (select, textarea, text etc). switch ( field.type ) { case 'select': // Build options for