is_registered( 'affistyle/aff-box' ) ) { return; } // Register the scoped frontend stylesheet handle on init so WordPress can // resolve it when loading assets declared in block.json's "style" field into // the iframed editor. Must happen before register_block_type_from_metadata(). // All rules in this file must be scoped under .affistyle-box-container or // .wp-block-affistyle-aff-box to avoid leaking into the admin UI. wp_register_style( 'affistyle-block-frontend-css', AFFISTYLE_URL . 'assets/css/affistyle-frontend.css', array(), AFFISTYLE_VERSION ); // Register the scoped editor-only stylesheet handle on init so block.json's // "editorStyle" field injects it into the iframed block canvas. // All rules in this file must be scoped under .wp-block-affistyle-aff-box // to avoid leaking into the WordPress admin or theme editor UI. wp_register_style( 'affistyle-block-editor-css', AFFISTYLE_URL . 'assets/css/affistyle-editor.css', array( 'wp-edit-blocks' ), AFFISTYLE_VERSION ); // Register the editor script handle on init so block.json's "editorScript" // field can resolve it for the classic and iframed block editor. wp_register_script( 'affistyle-block-js', AFFISTYLE_URL . 'assets/js/block.js', array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n', 'wp-server-side-render' ), AFFISTYLE_VERSION, array( 'strategy' => 'defer', 'in_footer' => true, ) ); if ( class_exists( 'AffiStyle_Database' ) ) { wp_localize_script( 'affistyle-block-js', 'affistyle_boxes_list', AffiStyle_Database::get_all_boxes_for_dropdown() ); } // Prefer metadata-driven registration so block.json owns the style/script // declarations — WordPress handles iframe injection automatically. if ( function_exists( 'register_block_type_from_metadata' ) && file_exists( AFFISTYLE_PATH . 'blocks/block.json' ) ) { register_block_type_from_metadata( AFFISTYLE_PATH . 'blocks', array( 'render_callback' => array( $this, 'render_block' ), ) ); } else { register_block_type( 'affistyle/aff-box', array( 'api_version' => 3, 'editor_script' => 'affistyle-block-js', 'editor_style' => 'affistyle-block-editor-css', 'style' => 'affistyle-block-frontend-css', 'render_callback' => array( $this, 'render_block' ), 'attributes' => array( 'boxId' => array( 'type' => 'string', 'default' => '', ), ), ) ); } } /** * Render callback for the Gutenberg block. * * @param array $attributes Block attributes. * @return string */ public function render_block( $attributes ) { $box_id = isset( $attributes['boxId'] ) ? absint( $attributes['boxId'] ) : 0; $is_editor_or_admin = is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ); if ( ! $box_id ) { if ( $is_editor_or_admin ) { return '
' . esc_html__( 'Please select an Affiliate Box from the block settings panel.', 'affistyle' ) . '
'; } return ''; } // Validate that the Affiliate Box exists, is the correct post type, and is published. $box_post = get_post( $box_id ); if ( ! $box_post || 'affistyle_box' !== $box_post->post_type || 'publish' !== $box_post->post_status ) { if ( $is_editor_or_admin ) { return '
' . esc_html__( 'Selected Affiliate Box has been moved to trash or deleted.', 'affistyle' ) . '
'; } return ''; } if ( ! class_exists( 'AffiStyle_Database' ) ) { return ''; } $data = AffiStyle_Database::get_box_data( $box_id ); if ( ! $data ) { if ( $is_editor_or_admin ) { return '
' . esc_html__( 'Selected Affiliate Box has been moved to trash or deleted.', 'affistyle' ) . '
'; } return ''; } // Dynamic Template Style Resolution. $template_style = ! empty( $data['template_style'] ) ? sanitize_html_class( $data['template_style'] ) : 'style-1'; $template_file = apply_filters( 'affistyle_template_file_path', AFFISTYLE_PATH . 'templates/' . $template_style . '.php', $template_style ); if ( ! file_exists( $template_file ) ) { $template_file = AFFISTYLE_PATH . 'templates/style-1.php'; } ob_start(); if ( file_exists( $template_file ) ) { // Include template with $data in scope. include $template_file; do_action( 'affistyle_box_footer', $box_id ); } return ob_get_clean(); } /** * Backward compatibility alias for render_block. * * @param array $attributes Block attributes. * @return string */ public function render_callback( $attributes ) { return $this->render_block( $attributes ); } } new AffiStyle_Block_Renderer();