* @return void */ public function __construct() { // Invite WordPress to register the custom Gutenberg blocks add_action( 'init', array( $this, 'register_blocks' ) ); // In apiVersion 3 the block's editor script runs inside an iframe. // Localize affilizz_l10n on the block's own handle so the data is // available there (the global localized for `affilizz-admin` lives in // the parent frame and is not visible from inside the iframe). add_action( 'enqueue_block_editor_assets', array( $this, 'localize_block_editor_data' ) ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_legacy_shortcode_migration' ) ); add_action( 'enqueue_block_assets', array( $this, 'enqueue_rendering_assets' ) ); } /** * Registers the Affilizz publication gutenberg block */ public function register_blocks() { register_block_type( AFFILIZZ_DIRECTORY . 'assets/dist/blocks/publication' ); } /** * Localizes affilizz_l10n on the block's editor script so the iframe sees it. */ public function localize_block_editor_data() { $core = \Affilizz\Core::get_instance(); $core->load_translations(); wp_localize_script( 'affilizz-publication-editor-script', 'affilizz_l10n', $core->get_javascript_l10n() ); } /** * Enqueues the Affilizz rendering web component runtime in the block editor iframe. */ public function enqueue_rendering_assets() { if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) { return; } $screen = get_current_screen(); if ( empty( $screen ) || ! method_exists( $screen, 'is_block_editor' ) || ! $screen->is_block_editor() ) { return; } wp_enqueue_script( 'affilizz-rendering', ( \Affilizz\Util\Assets::get_instance() )->get_diverted_js_file_url(), array(), AFFILIZZ_VERSION, array( 'strategy' => 'defer' ) ); } /** * Converts legacy raw Affilizz shortcode blocks to the dedicated block in the parent editor frame. */ public function enqueue_legacy_shortcode_migration() { wp_register_script( 'affilizz-legacy-shortcode-migration', false, array( 'wp-blocks', 'wp-data', 'wp-dom-ready' ), AFFILIZZ_ASSETS_BUILD_VERSION, true ); wp_enqueue_script( 'affilizz-legacy-shortcode-migration' ); wp_add_inline_script( 'affilizz-legacy-shortcode-migration', "( function( wp ) { if ( ! wp || ! wp.blocks || ! wp.data || ! wp.domReady ) { return; } var singleShortcodePattern = /^\\s*(?:]*>\\s*)?\\[affilizz-publication\\b([^\\]]*)\\]\\s*(?:<\\/p>)?\\s*$/i; var paragraphShortcodePattern = /(?:]*>\\s*)?\\[affilizz-publication\\b([^\\]]*)\\]\\s*(?:<\\/p>)?/gi; var attempts = 0; var isConverting = false; var unsubscribe = null; function decodeHtmlEntities( value ) { var textarea = document.createElement( 'textarea' ); textarea.innerHTML = value; return textarea.value; } function getIdFromAttributes( attributes ) { var idMatch = attributes.match( /\\bid\\s*=\\s*(?:\"([^\"]+)\"|'([^']+)'|([^\\s\\]]+))/i ); return idMatch ? ( idMatch[1] || idMatch[2] || idMatch[3] || '' ) : ''; } function getSingleShortcodeId( value ) { var shortcode = decodeHtmlEntities( String( value || '' ) ); var shortcodeMatch = shortcode.match( singleShortcodePattern ); if ( ! shortcodeMatch ) { return false; } return getIdFromAttributes( shortcodeMatch[1] ); } function getFreeformShortcodeIds( block ) { if ( block.name !== 'core/freeform' && block.name !== 'core/html' ) { return false; } var content = decodeHtmlEntities( String( ( block.attributes && block.attributes.content ) || '' ) ); var remainingContent = content.replace( paragraphShortcodePattern, '' ).trim(); var ids = []; var match; if ( remainingContent ) { return false; } paragraphShortcodePattern.lastIndex = 0; while ( ( match = paragraphShortcodePattern.exec( content ) ) !== null ) { ids.push( getIdFromAttributes( match[1] ) ); } paragraphShortcodePattern.lastIndex = 0; return ids.length ? ids : false; } function getSingleBlockShortcodeId( block ) { if ( block.name === 'core/paragraph' ) { return getSingleShortcodeId( block.attributes && block.attributes.content ); } if ( block.name === 'core/shortcode' ) { return getSingleShortcodeId( block.attributes && ( block.attributes.text || block.attributes.content ) ); } return false; } function collectLegacyBlocks( blocks ) { return blocks.reduce( function( legacyBlocks, block ) { var ids = getFreeformShortcodeIds( block ); var id = getSingleBlockShortcodeId( block ); if ( ids !== false ) { legacyBlocks.push( { clientId: block.clientId, ids: ids } ); } if ( id !== false ) { legacyBlocks.push( { clientId: block.clientId, ids: [ id ] } ); } if ( block.innerBlocks && block.innerBlocks.length ) { legacyBlocks = legacyBlocks.concat( collectLegacyBlocks( block.innerBlocks ) ); } return legacyBlocks; }, [] ); } function convertLegacyBlocks() { var blocks = wp.data.select( 'core/block-editor' ).getBlocks(); var legacyBlocks = collectLegacyBlocks( blocks ); legacyBlocks.forEach( function( legacyBlock ) { wp.data.dispatch( 'core/block-editor' ).replaceBlocks( legacyBlock.clientId, legacyBlock.ids.map( function( id ) { return wp.blocks.createBlock( 'affilizz/publication', { id: id } ); } ) ); } ); return legacyBlocks.length > 0; } function runMigration() { if ( isConverting ) { return; } attempts += 1; isConverting = true; var converted = convertLegacyBlocks(); isConverting = false; if ( unsubscribe && ( converted || attempts > 200 ) ) { unsubscribe(); } } wp.domReady( function() { unsubscribe = wp.data.subscribe( runMigration ); window.setTimeout( runMigration, 0 ); window.setTimeout( runMigration, 1000 ); window.setTimeout( runMigration, 3000 ); } ); }( window.wp ) );" ); } }