'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__( 'AffiStyle Error: Template file missing.', 'affistyle' ) . '
'; } return ob_get_clean(); } } new AffiStyle_Shortcode();