3, 'editor_script' => 'aiph-block', 'style' => 'aiph-button', 'render_callback' => fn($a) => aiph_button_html(array('product_id'=>$a['productId'] ?? 0, 'label'=>$a['label'] ?? '', 'align'=>$a['buttonAlign'] ?? '', 'preview'=>defined('REST_REQUEST') && REST_REQUEST)), 'attributes' => array('productId'=>array('type'=>'number','default'=>0), 'label'=>array('type'=>'string','default'=>''), 'buttonAlign'=>array('type'=>'string','default'=>'')), )); } }); add_action('enqueue_block_editor_assets', function () { wp_add_inline_script('aiph-block', 'window.aiphBlock = '.wp_json_encode(array('products'=>aiph_designer_products(), 'defaultLabel'=>aiph_opt('button_label'))).';', 'before'); }); /** Published products with the designer turned on, for editor dropdowns. */ function aiph_designer_products() { $out = array(); if (!function_exists('wc_get_products')) return $out; foreach (wc_get_products(array('limit'=>200, 'status'=>'publish', 'type'=>array('simple','variable'), 'orderby'=>'title', 'order'=>'ASC')) as $p) { if (aiph_enabled($p->get_id())) $out[] = array('id'=>$p->get_id(), 'name'=>wp_strip_all_tags($p->get_name())); } return $out; } /** * Button markup. product_id 0 = the current product (single product pages and product loops). * preview = render a sample button when no product resolves (block editor / Elementor editor). */ function aiph_button_html($args = array()) { $a = wp_parse_args($args, array('product_id'=>0, 'label'=>'', 'align'=>'', 'class'=>'', 'preview'=>false)); $id = absint($a['product_id']); $chosen = $id > 0; if (!$id) { global $product; if ($product instanceof WC_Product) $id = $product->get_id(); elseif (function_exists('is_product') && is_product()) $id = get_queried_object_id(); } $p = $id && function_exists('wc_get_product') ? wc_get_product($id) : null; $ok = $p && aiph_supported($p) && aiph_enabled($id) && $p->get_status()==='publish'; if (!$ok && !($a['preview'] && !$chosen)) { return current_user_can('edit_products') ? '

'.esc_html__('Product Designer: choose a product that has the designer turned on.', 'printloom').'

' : ''; } wp_enqueue_style('aiph-button'); $label = trim((string) $a['label'])!=='' ? $a['label'] : aiph_opt('button_label'); $align = in_array($a['align'], array('left','center','right','full'), true) ? ' aiph-align-'.$a['align'] : ''; $href = $ok ? aiph_studio_url($id) : '#'; return '
'.esc_html($label).'
'; } add_shortcode('aiph_designer', function ($atts) { $atts = shortcode_atts(array('product_id'=>0, 'label'=>'', 'align'=>'', 'class'=>''), $atts, 'aiph_designer'); return aiph_button_html($atts); }); add_action('elementor/widgets/register', function ($widgets_manager) { require_once AIPH_DIR.'/includes/elementor-widget.php'; $widgets_manager->register(new AIPH_Elementor_Designer_Widget()); });