admin = $admin; add_action( 'admin_menu', array( $this, 'menu' ), 25 ); add_action( 'admin_post_shapeoptions_install_template', array( $this, 'install' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); } /** * Load the template library's CSS on its own page only. * * @param string $hook Current admin page hook suffix. */ public function assets( $hook ) { if ( 'shapeoptions_group_page_shapeoptions-templates' !== $hook ) { return; } wp_enqueue_style( 'shapeoptions-admin-templates', SHAPEOPTIONS_URL . 'assets/css/shapeoptions-admin-templates.css', array(), shapeoptions_asset_version( SHAPEOPTIONS_PATH . 'assets/css/shapeoptions-admin-templates.css', SHAPEOPTIONS_VERSION ) ); } /** * Register the library page. */ public function menu() { add_submenu_page( 'edit.php?post_type=shapeoptions_group', __( 'ShapeOptions Templates', 'shapeoptions' ), __( 'Templates', 'shapeoptions' ), 'manage_woocommerce', 'shapeoptions-templates', array( $this, 'render_page' ) ); } /** * All templates. Add-ons register more through the filter below. * * @return array */ public function get_templates() { $templates = array( 'gift_wrap_basic' => array( 'title' => __( 'Gift wrap (basic)', 'shapeoptions' ), 'description' => __( 'A gift-wrap toggle with an optional note — the classic upsell.', 'shapeoptions' ), 'fields' => array( array( 'type' => 'switch', 'label' => __( 'Gift wrap this item', 'shapeoptions' ), 'key' => 'gift_wrap', 'price' => 5 ), array( 'type' => 'textarea', 'label' => __( 'Gift note (optional)', 'shapeoptions' ), 'key' => 'gift_note', 'maxlength' => 200 ), ), ), 'personalization' => array( 'title' => __( 'Personalization (basic)', 'shapeoptions' ), 'description' => __( 'A name/text line plus a style choice with per-choice fees.', 'shapeoptions' ), 'fields' => array( array( 'type' => 'text', 'label' => __( 'Personalization text', 'shapeoptions' ), 'key' => 'perso_text', 'required' => 1, 'price' => 4, 'maxlength' => 25, 'description' => __( 'Exactly as it should appear on the product.', 'shapeoptions' ) ), array( 'type' => 'radio', 'label' => __( 'Style', 'shapeoptions' ), 'key' => 'perso_style', 'choices' => array( array( 'label' => __( 'Printed', 'shapeoptions' ), 'value' => 'printed', 'price' => 0 ), array( 'label' => __( 'Embroidered', 'shapeoptions' ), 'value' => 'embroidered', 'price' => 6 ), ) ), ), ), 'delivery_notes' => array( 'title' => __( 'Delivery preferences', 'shapeoptions' ), 'description' => __( 'Preferred delivery date, time window and courier instructions.', 'shapeoptions' ), 'fields' => array( array( 'type' => 'date', 'label' => __( 'Preferred delivery date', 'shapeoptions' ), 'key' => 'delivery_date' ), array( 'type' => 'select', 'label' => __( 'Time window', 'shapeoptions' ), 'key' => 'delivery_window', 'choices' => array( array( 'label' => __( 'Anytime', 'shapeoptions' ), 'value' => 'any', 'price' => 0 ), array( 'label' => __( 'Morning (9–12)', 'shapeoptions' ), 'value' => 'morning', 'price' => 3 ), array( 'label' => __( 'Evening (17–21)', 'shapeoptions' ), 'value' => 'evening', 'price' => 3 ), ) ), array( 'type' => 'textarea', 'label' => __( 'Instructions for the courier', 'shapeoptions' ), 'key' => 'delivery_notes', 'maxlength' => 300 ), ), ), ); /** * Register extra templates. * * @param array $templates slug => template definition. */ return apply_filters( 'shapeoptions/templates', $templates ); } /** * Render the library grid with per-template field previews. */ public function render_page() { $installed = isset( $_GET['shapeoptions_installed'] ) ? absint( $_GET['shapeoptions_installed'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $labels = shapeoptions()->fields->get_labels(); ?>

get_templates() as $slug => $template ) : ?>

  • * ()
get_templates(); if ( ! isset( $templates[ $slug ] ) ) { wp_die( esc_html__( 'Unknown template.', 'shapeoptions' ) ); } $post_id = wp_insert_post( array( 'post_type' => 'shapeoptions_group', 'post_title' => $templates[ $slug ]['title'], 'post_status' => 'draft', ) ); if ( is_wp_error( $post_id ) || ! $post_id ) { wp_die( esc_html__( 'The template could not be installed. Please try again.', 'shapeoptions' ) ); } $clean = array(); $seen = array(); foreach ( $templates[ $slug ]['fields'] as $raw ) { $field = $this->admin->sanitize_field( $raw, $seen ); if ( $field ) { $seen[] = $field['key']; $clean[] = $field; } } update_post_meta( $post_id, '_shapeoptions_fields', $clean ); update_post_meta( $post_id, '_shapeoptions_conditions', array( 'type' => 'all', 'ids' => array() ) ); update_post_meta( $post_id, '_shapeoptions_placement', 'before' ); wp_safe_redirect( add_query_arg( 'shapeoptions_installed', $post_id, admin_url( 'edit.php?post_type=shapeoptions_group&page=shapeoptions-templates' ) ) ); exit; } } endif;