'defer', 'in_footer' => true, ) ); } wp_localize_script( 'affistyle-tracker-js', 'affistyleTrackerData', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'affistyle_track_click_nonce' ), ) ); } /** * Render Shortcode: [affistyle_box id="123"] * * @param array $atts Shortcode attributes. * @return string */ public function render_shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => 0, ), $atts, 'affistyle_box' ); $post_id = absint( $atts['id'] ); if ( ! $post_id ) { return is_admin() ? '

' . esc_html__( 'AffiStyle Error: Please provide a valid Affiliate Box ID.', 'affistyle' ) . '

' : ''; } $box_post = get_post( $post_id ); if ( ! $box_post || 'affistyle_box' !== $box_post->post_type || 'publish' !== $box_post->post_status ) { return is_admin() ? '

' . esc_html__( 'AffiStyle Error: Selected Affiliate Box has been moved to trash or deleted.', 'affistyle' ) . '

' : ''; } if ( ! class_exists( 'AffiStyle_Database' ) ) { return ''; } $data = AffiStyle_Database::get_box_data( $post_id ); if ( ! $data ) { return is_admin() ? '

' . esc_html__( 'AffiStyle Error: Affiliate Box not found.', 'affistyle' ) . '

' : ''; } return $this->generate_card_html( $data ); } /** * Register Gutenberg Block Server-Side Render. */ public function register_gutenberg_block() { if ( ! function_exists( 'register_block_type' ) ) { return; } wp_register_script( 'affistyle-block-js', AFFISTYLE_URL . 'assets/js/block.js', array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', '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() ); } register_block_type( 'affistyle/aff-box', array( 'editor_script' => 'affistyle-block-js', 'render_callback' => array( $this, 'render_block_callback' ), 'attributes' => array( 'boxId' => array( 'type' => 'string', 'default' => '', ), ), ) ); } /** * Gutenberg Server-Side Render Callback. * * @param array $attributes Block attributes. * @return string */ public function render_block_callback( $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 ''; } $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 ''; } return $this->generate_card_html( $data ); } /** * Generate Clean, Responsive HTML Markup for the Card using the templates folder. * * @param array $data Card metadata. * @return string */ public function generate_card_html( $data ) { // Enqueue styles and tracker scripts on-demand only when a box is rendered. wp_enqueue_style( 'affistyle-block-frontend-css' ); wp_enqueue_script( 'affistyle-tracker-js' ); $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'; } $box_id = isset( $data['id'] ) ? absint( $data['id'] ) : 0; if ( $box_id && class_exists( 'AffiStyle_Cloaking' ) ) { $data['destination'] = AffiStyle_Cloaking::get_cloaked_url( $box_id, 1 ); if ( isset( $data['url_2_default'] ) || isset( $data['title_2'] ) ) { $data['url_2_default'] = AffiStyle_Cloaking::get_cloaked_url( $box_id, 2 ); $data['link_2'] = $data['url_2_default']; } } ob_start(); if ( file_exists( $template_file ) ) { // Include template with $data in scope. include $template_file; do_action( 'affistyle_box_footer', $box_id ); } else { echo '

' . esc_html__( 'AffiStyle Error: Template file missing.', 'affistyle' ) . '

'; } return ob_get_clean(); } } new AffiStyle_Shortcode();