* @license GPL-2.0-or-later https://www.gnu.org/licenses/gpl-2.0.html * @link https://wordpress.org/plugins/advanced-qr-code-generator-block/ * @phpversion 7.4 */ if (! defined('ABSPATH')) { exit; // Exit if accessed directly. } /** * Registers the block using a `blocks-manifest.php` file, * which improves the performance of block type registration. * Behind the scenes, it also registers all assets so they can be enqueued * through the block editor in the corresponding context. * * @see https://make.wordpress.org/core/2025/03/13/more-efficient-block-type-registration-in-6-8/ * @see https://make.wordpress.org/core/2024/10/17/new-block-type-registration-apis-to-improve-performance-in-wordpress-6-7/ * @return void * @since 0.1.0 */ function aqrcgb_create_block_advanced_qr_code_generator_block_block_init() { /** * Registers the block(s) metadata from the `blocks-manifest.php` file. * Added to WordPress 6.7 to improve the performance of block type registration. * * @see https://make.wordpress.org/core/2024/10/17/new-block-type-registration-apis-to-improve-performance-in-wordpress-6-7/ */ if (function_exists('wp_register_block_metadata_collection')) { wp_register_block_metadata_collection(__DIR__ . '/build', __DIR__ . '/build/blocks-manifest.php'); } /** * Registers the block type(s) in the `blocks-manifest.php` file. * * @see https://developer.wordpress.org/reference/functions/register_block_type/ */ $manifest_data = include __DIR__ . '/build/blocks-manifest.php'; foreach (array_keys($manifest_data) as $block_type) { $block_config = aqrcgb_get_block_config($block_type); if ($block_config) { register_block_type(__DIR__ . "/build/{$block_type}", $block_config); // Add script translations if (function_exists('wp_set_script_translations')) { wp_set_script_translations( 'create-block-advanced-qr-code-generator-block-editor-script', // Script handle 'advanced-qr-code-generator-block', // Text domain plugin_dir_path(__FILE__) . 'languages' // Path to translations directory ); } } } } add_action('init', 'aqrcgb_create_block_advanced_qr_code_generator_block_block_init'); /** * Renders the Advanced QR Code Generator block. * * This callback function generates the HTML output for the QR code block, * setting up the necessary wrapper attributes and data attributes based on * the provided block attributes. * * @param array $attributes { * Block attributes used to configure the QR code. * * @type string $contentType The type of content ('text' or other). * @type string $content The text content to encode (if contentType is 'text'). * @type string $url The URL to encode (if contentType is not 'text'). * @type int $size The size of the QR code. * @type string $logoUrl The URL of the logo to embed in the QR code. * @type int $logoSize The size of the logo. * @type string $logoShape The shape of the logo (e.g., 'circle', 'square'). * @type string $logoAlignment The alignment of the logo within the QR code. * @type string $qrColor The color of the QR code. * @type string $backgroundColor The background color of the QR code. * @type string $errorCorrectionLevel The error correction level for the QR code. * @type int $quietZone The size of the quiet zone around the QR code. * } * * @return string The HTML markup for the QR code block. */ function aqrcgb_advanced_qr_code_generator_block_render_callback($attributes) { // Get the appropriate content based on type $content = isset($attributes['contentType']) && $attributes['contentType'] === 'text' ? ($attributes['content'] ?? '') : ($attributes['url'] ?? get_permalink()); $wrapper_attributes = get_block_wrapper_attributes([ 'class' => 'wp-block-create-block-advanced-qr-code-generator-block', 'data-url' => $content, 'data-size' => $attributes['size'], 'data-logo-url' => $attributes['logoUrl'], 'data-logo-size' => $attributes['logoSize'], 'data-logo-shape' => $attributes['logoShape'], 'data-logo-alignment' => $attributes['logoAlignment'], 'data-qr-color' => $attributes['qrColor'], 'data-background-color' => $attributes['backgroundColor'], 'data-error-correction' => $attributes['errorCorrectionLevel'], 'data-quiet-zone' => $attributes['quietZone'], ]); return sprintf( '