add_action('after_setup_theme', function () {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('html5', ['search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'script', 'style']);
add_theme_support('custom-logo');
load_theme_textdomain('premium-tea');
});
add_action('wp_enqueue_scripts', function () {
$ver = wp_get_theme()->get('Version');
wp_enqueue_style('premium-tea-fonts', 'https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;600;700&family=Inter:wght@400;500;700&display=swap', [], null);
wp_enqueue_style('premium-tea-style', get_stylesheet_uri(), [], $ver);
wp_enqueue_style('premium-tea-theme', get_template_directory_uri() . '/assets/css/theme.css', ['premium-tea-style'], $ver);
wp_enqueue_script('premium-tea-catalog', get_template_directory_uri() . '/assets/js/catalog-filters.js', ['jquery'], $ver, true);
});
register_nav_menus([
'primary' => __('Primary Menu', 'premium-tea'),
]);
add_action('init', function () {
$labels = [
'name' => __('Tea Products', 'premium-tea'),
'singular_name' => __('Tea Product', 'premium-tea'),
'add_new' => __('Add New', 'premium-tea'),
'add_new_item' => __('Add New Tea Product', 'premium-tea'),
'edit_item' => __('Edit Tea Product', 'premium-tea'),
'new_item' => __('New Tea Product', 'premium-tea'),
'view_item' => __('View Tea Product', 'premium-tea'),
'search_items' => __('Search Tea Products', 'premium-tea'),
'not_found' => __('No tea products found', 'premium-tea'),
'not_found_in_trash' => __('No tea products found in Trash', 'premium-tea'),
'menu_name' => __('Tea Products', 'premium-tea'),
];
register_post_type('tea_product', [
'labels' => $labels,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-leaves',
'supports' => ['title', 'editor', 'thumbnail', 'excerpt'],
'rewrite' => ['slug' => 'tea'],
'show_in_rest' => true,
]);
register_taxonomy('tea_type', 'tea_product', [
'labels' => [
'name' => __('Tea Types', 'premium-tea'),
'singular_name' => __('Tea Type', 'premium-tea'),
],
'public' => true,
'hierarchical' => true,
'rewrite' => ['slug' => 'tea-type'],
'show_in_rest' => true,
]);
});
add_action('init', function () {
$defaults = ['Green Tea', 'Black Tea', 'Herbal Tea'];
foreach ($defaults as $name) {
if (!term_exists($name, 'tea_type')) {
wp_insert_term($name, 'tea_type');
}
}
});
add_action('add_meta_boxes', function () {
add_meta_box('premium_tea_meta', __('Tea Details', 'premium-tea'), function ($post) {
wp_nonce_field('premium_tea_meta', 'premium_tea_meta_nonce');
$short = get_post_meta($post->ID, 'tea_short_desc', true);
$sizes = get_post_meta($post->ID, 'tea_packaging_sizes', true);
$wholesale = get_post_meta($post->ID, 'wholesale_available', true);
$featured = get_post_meta($post->ID, 'featured', true);
echo '
';
echo '
';
echo '';
echo '';
}, 'tea_product', 'normal', 'default');
});
add_action('save_post_tea_product', function ($post_id) {
if (!isset($_POST['premium_tea_meta_nonce']) || !wp_verify_nonce($_POST['premium_tea_meta_nonce'], 'premium_tea_meta')) return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
$short = isset($_POST['tea_short_desc']) ? sanitize_textarea_field($_POST['tea_short_desc']) : '';
$sizes = isset($_POST['tea_packaging_sizes']) ? sanitize_text_field($_POST['tea_packaging_sizes']) : '';
$wholesale = isset($_POST['wholesale_available']) ? '1' : '';
$featured = isset($_POST['featured']) ? '1' : '';
update_post_meta($post_id, 'tea_short_desc', $short);
update_post_meta($post_id, 'tea_packaging_sizes', $sizes);
update_post_meta($post_id, 'wholesale_available', $wholesale);
update_post_meta($post_id, 'featured', $featured);
});
add_shortcode('tea_featured', function ($atts) {
$a = shortcode_atts(['count' => 4], $atts);
$count = intval($a['count']);
$q = new WP_Query([
'post_type' => 'tea_product',
'posts_per_page' => $count,
'meta_key' => 'featured',
'meta_value' => '1',
]);
ob_start();
echo '';
if ($q->have_posts()) {
while ($q->have_posts()) {
$q->the_post();
$desc = get_post_meta(get_the_ID(), 'tea_short_desc', true);
echo '
';
if (has_post_thumbnail()) {
echo get_the_post_thumbnail(get_the_ID(), 'medium_large', ['class' => 'tea-card-image']);
}
echo '' . esc_html(get_the_title()) . '
';
if ($desc) {
echo '' . esc_html($desc) . '
';
}
echo '';
echo '';
}
wp_reset_postdata();
}
echo '
';
$catalog = site_url('/tea-catalog');
echo '';
return ob_get_clean();
});
add_shortcode('tea_catalog', function ($atts) {
$a = shortcode_atts(['type' => 'all', 'per_page' => 12], $atts);
$type = isset($_GET['tea_type']) ? sanitize_text_field($_GET['tea_type']) : $a['type'];
$tax_query = [];
if ($type !== 'all') {
$tax_query[] = [
'taxonomy' => 'tea_type',
'field' => 'slug',
'terms' => $type,
];
}
$q = new WP_Query([
'post_type' => 'tea_product',
'posts_per_page' => intval($a['per_page']),
'tax_query' => $tax_query,
]);
ob_start();
echo '';
$terms = get_terms(['taxonomy' => 'tea_type', 'hide_empty' => true]);
if (!is_wp_error($terms) && $terms) {
echo '';
}
echo '';
if ($q->have_posts()) {
while ($q->have_posts()) {
$q->the_post();
$desc = get_post_meta(get_the_ID(), 'tea_short_desc', true);
$sizes = get_post_meta(get_the_ID(), 'tea_packaging_sizes', true);
$wholesale = get_post_meta(get_the_ID(), 'wholesale_available', true) ? esc_html__('Wholesale Available', 'premium-tea') : '';
echo '
';
if (has_post_thumbnail()) {
echo get_the_post_thumbnail(get_the_ID(), 'large', ['class' => 'tea-card-image']);
}
echo '' . esc_html(get_the_title()) . '
';
if ($desc) {
echo '' . esc_html($desc) . '
';
}
if ($sizes) {
echo '' . esc_html__('Packaging: ', 'premium-tea') . esc_html($sizes) . '
';
}
if ($wholesale) {
echo '' . $wholesale . '
';
}
echo '';
}
wp_reset_postdata();
}
echo '
';
echo '';
return ob_get_clean();
});
add_action('after_switch_theme', function () {
$home_id = 0;
$catalog_id = 0;
if (get_option('premium_tea_demo_setup_done')) return;
$home = get_page_by_path('home');
if (!$home) {
$home_id = wp_insert_post([
'post_title' => 'Home',
'post_name' => 'home',
'post_type' => 'page',
'post_status' => 'publish',
]);
if ($home_id && !is_wp_error($home_id)) {
update_post_meta($home_id, '_wp_page_template', 'template-fullwidth.php');
if (did_action('elementor/loaded')) {
$section = function ($widgets) {
$col_id = uniqid('col_');
return [
'id' => uniqid('sec_'),
'elType' => 'section',
'settings' => [
'content_width' => 'boxed'
],
'elements' => [
[
'id' => $col_id,
'elType' => 'column',
'settings' => ['_column_size' => 100],
'elements' => $widgets,
'isInner' => false
]
],
'isInner' => false
];
};
$w_heading = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'heading',
'settings' => [
'title' => 'Pure Tea Leaves, Crafted to Perfection'
],
'elements' => []
];
$w_text = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'text-editor',
'settings' => [
'editor' => 'Premium quality, tradition, and freshness in every leaf.'
],
'elements' => []
];
$w_button = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'button',
'settings' => [
'text' => 'View Catalog',
'link' => ['url' => site_url('/tea-catalog')]
],
'elements' => []
];
$w_about = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'text-editor',
'settings' => [
'editor' => 'Ethical sourcing, freshness and quality control, premium packaging.'
],
'elements' => []
];
$w_shortcodes_featured = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'shortcode',
'settings' => [
'shortcode' => '[tea_featured count="4"]'
],
'elements' => []
];
$data = [
$section([$w_heading, $w_text, $w_button]),
$section([$w_about]),
$section([$w_shortcodes_featured]),
];
update_post_meta($home_id, '_elementor_edit_mode', 'builder');
update_post_meta($home_id, '_elementor_data', wp_json_encode($data));
}
}
} else {
$home_id = $home->ID;
}
$catalog = get_page_by_path('tea-catalog');
if (!$catalog) {
$catalog_id = wp_insert_post([
'post_title' => 'Tea Catalog',
'post_name' => 'tea-catalog',
'post_type' => 'page',
'post_status' => 'publish',
]);
if ($catalog_id && !is_wp_error($catalog_id)) {
update_post_meta($catalog_id, '_wp_page_template', 'template-fullwidth.php');
if (did_action('elementor/loaded')) {
$section = function ($widgets) {
$col_id = uniqid('col_');
return [
'id' => uniqid('sec_'),
'elType' => 'section',
'settings' => [
'content_width' => 'boxed'
],
'elements' => [
[
'id' => $col_id,
'elType' => 'column',
'settings' => ['_column_size' => 100],
'elements' => $widgets,
'isInner' => false
]
],
'isInner' => false
];
};
$w_heading = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'heading',
'settings' => [
'title' => 'Our Tea Collection'
],
'elements' => []
];
$w_text = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'text-editor',
'settings' => [
'editor' => 'Explore our premium teas by type, flavor, and craft.'
],
'elements' => []
];
$w_shortcodes_catalog = [
'id' => uniqid('wid_'),
'elType' => 'widget',
'widgetType' => 'shortcode',
'settings' => [
'shortcode' => '[tea_catalog per_page="12"]'
],
'elements' => []
];
$data = [
$section([$w_heading, $w_text]),
$section([$w_shortcodes_catalog]),
];
update_post_meta($catalog_id, '_elementor_edit_mode', 'builder');
update_post_meta($catalog_id, '_elementor_data', wp_json_encode($data));
}
}
} else {
$catalog_id = $catalog->ID;
}
if (!get_option('show_on_front')) {
update_option('show_on_front', 'page');
}
if ($home_id) update_option('page_on_front', $home_id);
if ($catalog_id) update_option('page_for_posts', 0);
$samples = [
['title' => 'Heritage Green', 'type' => 'Green Tea', 'sizes' => '100g, 250g, 500g', 'featured' => true],
['title' => 'Classic Assam Black', 'type' => 'Black Tea', 'sizes' => '100g, 250g, 500g', 'featured' => true],
['title' => 'Calming Herbal Blend', 'type' => 'Herbal Tea', 'sizes' => '100g, 250g', 'featured' => false],
['title' => 'Estate Darjeeling', 'type' => 'Black Tea', 'sizes' => '100g, 250g', 'featured' => false],
['title' => 'Jasmine Green', 'type' => 'Green Tea', 'sizes' => '100g, 250g', 'featured' => false],
['title' => 'Mint Herbal Refresh', 'type' => 'Herbal Tea', 'sizes' => '100g, 250g', 'featured' => false],
];
foreach ($samples as $s) {
$exists = get_page_by_title($s['title'], OBJECT, 'tea_product');
if ($exists) continue;
$pid = wp_insert_post([
'post_title' => $s['title'],
'post_type' => 'tea_product',
'post_status' => 'publish',
]);
if ($pid && !is_wp_error($pid)) {
update_post_meta($pid, 'tea_short_desc', 'A refined tea with balanced flavor and aroma.');
update_post_meta($pid, 'tea_packaging_sizes', $s['sizes']);
update_post_meta($pid, 'wholesale_available', '1');
update_post_meta($pid, 'featured', $s['featured'] ? '1' : '');
$term = get_term_by('name', $s['type'], 'tea_type');
if ($term && !is_wp_error($term)) {
wp_set_object_terms($pid, [$term->term_id], 'tea_type');
}
}
}
update_option('premium_tea_demo_setup_done', 1);
});
XML-RPC server accepts POST requests only.