* @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( '
', $wrapper_attributes ); } /** * Retrieves the configuration settings for the AQRCGB block. * * This function returns an array containing configuration options * for the AQRCGB block, which may include settings such as block * attributes, render callbacks, and other relevant parameters. * * @param string $block_type The block type identifier. * * @return array|null The configuration array for the AQRCGB block, or null if not found. */ function aqrcgb_get_block_config($block_type) { switch ($block_type) { case 'advanced-qr-code-generator-block': return [ 'render_callback' => 'aqrcgb_advanced_qr_code_generator_block_render_callback', 'attributes' => [ 'url' => [ 'type' => 'string', 'default' => 'https://example.com', ], 'size' => [ 'type' => 'number', 'default' => 200, ], 'logoUrl' => [ 'type' => 'string', 'default' => '', ], 'logoSize' => [ 'type' => 'number', 'default' => 50, ], 'logoShape' => [ 'type' => 'string', 'default' => 'original', ], 'qrColor' => [ 'type' => 'string', 'default' => '#000000', ], 'backgroundColor' => [ 'type' => 'string', 'default' => '#ffffff', ], 'logoAlignment' => [ 'type' => 'string', 'default' => 'center center', ], 'errorCorrectionLevel' => [ 'type' => 'string', 'default' => 'H', ], 'quietZone' => [ 'type' => 'number', 'default' => 4, ], 'contentType' => [ 'type' => 'string', 'default' => 'url', ], 'content' => [ 'type' => 'string', 'default' => '', ], ], ]; // Example of how to add another block type case 'another-block': return [ 'render_callback' => 'another_block_render_callback', 'attributes' => [ // Block specific attributes ], ]; default: return null; } } /** * Enqueues frontend assets for the Advanced QR Code Generator block. * * This function registers and enqueues the necessary JavaScript and CSS files * required for the block's frontend functionality. * * @since 1.0.0 * * @return void */ function aqrcgb_advanced_qr_code_generator_block_frontend_assets() { if (has_block('create-block/advanced-qr-code-generator-block')) { wp_enqueue_script( 'qrcode-lib', plugin_dir_url(__FILE__) . 'assets/js/qrcode.min.js', [], '1.0.0', true ); } } add_action('wp_enqueue_scripts', 'aqrcgb_advanced_qr_code_generator_block_frontend_assets'); /** * Enqueues editor-specific assets and localizes script data. * * This function adds plugin URL data to the editor script to allow * loading of local assets like the QR code library from the correct path. * * @since 1.0.0 * * @return void */ function aqrcgb_enqueue_editor_assets() { wp_localize_script( 'create-block-advanced-qr-code-generator-block-editor-script', 'aqrcgb_block_data', array( 'plugin_url' => plugin_dir_url(__FILE__), ) ); } add_action('enqueue_block_editor_assets', 'aqrcgb_enqueue_editor_assets');