shortcode = $shortcode; } public function init() { add_action('init', [$this, 'register']); } public function register() { if (!function_exists('register_block_type')) { return; } wp_register_script( 'adghost-block', ADGHOST_PLUGIN_URL . 'assets/js/block.js', ['wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-i18n'], ADGHOST_VERSION, true ); wp_add_inline_script('adghost-block', 'window.adghostBlockUnits=' . wp_json_encode(self::units(), JSON_HEX_TAG | JSON_HEX_AMP) . ';', 'before'); if (function_exists('wp_set_script_translations')) { wp_set_script_translations('adghost-block', 'adghost-lite'); } register_block_type(self::NAME, [ 'api_version' => 3, 'title' => __('AdGhost Ad', 'adghost-lite'), 'category' => 'widgets', 'icon' => 'megaphone', 'editor_script' => 'adghost-block', 'attributes' => [ 'unit' => ['type' => 'string', 'default' => ''], ], 'supports' => ['html' => false, 'reusable' => true], 'render_callback' => [$this, 'render'], ]); } public static function units() { $out = []; foreach (AdGhost_Helper::units(true) as $unit) { $position = AdGhost_Helper::get_unit_position($unit); if (($unit['type'] ?? 'standard') !== 'standard' || $position === 'anchor' || in_array($position, AdGhost_Helper::screen_positions(), true)) { continue; } $code = AdGhost_Helper::gam_code($unit); if ($code === '') { continue; } $out[] = [ 'code' => $code, 'name' => (string) ($unit['name'] ?? '') !== '' ? (string) $unit['name'] : $code, 'sizes' => (string) ($unit['sizes'] ?? ''), ]; } return $out; } public function render($attributes) { $code = AdGhost_Helper::sanitize_gam_code((string) ($attributes['unit'] ?? '')); if ($code === '') { return ''; } return $this->shortcode->render_shortcode(['id' => $code]); } }