class WC_Dynamic_Shortcodes_Generator { public function __construct() { add_action('init', [$this, 'register_shortcodes']); } public function get_dynamic_items() { $items = []; // Get product attributes $attributes = wc_get_attribute_taxonomies(); foreach ($attributes as $attribute) { $taxonomy = wc_attribute_taxonomy_name($attribute->attribute_name); $items[] = [ 'type' => 'attribute', 'name' => $attribute->attribute_name, 'label' => $attribute->attribute_label, 'taxonomy' => $taxonomy, 'shortcode' => '[product_' . sanitize_title($attribute->attribute_name) . ']' ]; } // Get taxonomies $taxonomies = get_object_taxonomies('product', 'objects'); foreach ($taxonomies as $taxonomy) { if ($taxonomy->public && !str_starts_with($taxonomy->name, 'pa_')) { $items[] = [ 'type' => 'taxonomy', 'name' => $taxonomy->name, 'label' => $taxonomy->label, 'shortcode' => '[product_' . sanitize_title($taxonomy->name) . ']' ]; } } return $items; } public function register_shortcodes() { foreach ($this->get_dynamic_items() as $item) { add_shortcode('product_' . sanitize_title($item['name']), function() use ($item) { return apply_filters('wc_dynamic_shortcode_content', '', $item); }); } // Register generic shortcode add_shortcode('product_attribute', function($atts) { $atts = shortcode_atts([ 'name' => '', 'default' => '' ], $atts); return apply_filters('wc_dynamic_attribute_content', $atts['default'], $atts['name']); }); } }class WC_Dynamic_Shortcodes_Admin { public function __construct() { add_action('admin_menu', [$this, 'add_admin_menu']); add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']); } public function add_admin_menu() { add_submenu_page( 'woocommerce', __('Dynamic Content Shortcodes', 'wc-dynamic-shortcodes'), __('Shortcode Reference', 'wc-dynamic-shortcodes'), 'manage_woocommerce', 'wc-dynamic-shortcodes', [$this, 'render_admin_page'] ); } public function enqueue_assets($hook) { if ('woocommerce_page_wc-dynamic-shortcodes' !== $hook) { return; } wp_enqueue_style( 'wc-dynamic-shortcodes-admin', plugin_dir_url(__FILE__) . '../assets/css/admin.css' ); wp_enqueue_script( 'clipboard', plugin_dir_url(__FILE__) . '../assets/js/clipboard.js', [], '2.0.11', true ); wp_enqueue_script( 'wc-dynamic-shortcodes-admin', plugin_dir_url(__FILE__) . '../assets/js/admin.js', ['clipboard'], '1.0.0', true ); } public function render_admin_page() { $generator = new WC_Dynamic_Shortcodes_Generator(); $items = $generator->get_dynamic_items(); echo '
'; echo '

' . esc_html__('WooCommerce Dynamic Content Shortcodes', 'wc-dynamic-shortcodes') . '

'; echo '
'; echo '

' . esc_html__('Use these shortcodes to display product attributes and taxonomies in your content:', 'wc-dynamic-shortcodes') . '

'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ($items as $item) { echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo ''; echo '
' . esc_html__('Type', 'wc-dynamic-shortcodes') . '' . esc_html__('Name', 'wc-dynamic-shortcodes') . '' . esc_html__('Shortcode', 'wc-dynamic-shortcodes') . '' . esc_html__('Action', 'wc-dynamic-shortcodes') . '
' . esc_html(ucfirst($item['type'])) . '' . esc_html($item['label']) . '' . esc_html($item['shortcode']) . '
' . esc_html__('Generic Shortcode:', 'wc-dynamic-shortcodes') . ' '; echo '[product_attribute name="attribute-slug" default="N/A"] '; echo '
'; } }class WC_Dynamic_Shortcodes_Handler { public function __construct() { add_filter('wc_dynamic_shortcode_content', [$this, 'handle_shortcode'], 10, 2); add_filter('wc_dynamic_attribute_content', [$this, 'handle_attribute'], 10, 2); } public function handle_shortcode($content, $item) { global $product; if (!$product || !is_product()) { return $content; } if ($item['type'] === 'attribute') { return $this->get_attribute_value($product, $item['taxonomy']); } return $this->get_taxonomy_values($product, $item['name']); } public function handle_attribute($default, $attribute_name) { global $product; if (!$product || !is_product()) { return $default; } $taxonomy = wc_attribute_taxonomy_name($attribute_name); $value = $this->get_attribute_value($product, $taxonomy); return $value ?: $default; } private function get_attribute_value($product, $taxonomy) { $value = $product->get_attribute($taxonomy); return $value ? strip_tags($value) : ''; } private function get_taxonomy_values($product, $taxonomy) { $terms = get_the_terms($product->get_id(), $taxonomy); if (is_wp_error($terms) || empty($terms)) { return ''; } return implode(', ', wp_list_pluck($terms, 'name')); } }