File: /mnt/data/dreamssalon-wp/wp-content/plugins/dreamsalon-core/dreamsalon-core.php
<?php
/**
* Plugin Name: DreamSalon Core
* Description: Custom Salon plugin with templates.
* Version: 1.0
* Author: Dreams Technologies
* Text Domain: dreamsalon-core
* Domain Path: /languages
*/
if (! defined('ABSPATH')) exit;
define('DSBSALON_PLUGIN_PATH', plugin_dir_path(__FILE__));
/**
* Load plugin textdomain for translations
*/
add_action('init', 'dreamsalon_load_textdomain_core');
function dreamsalon_load_textdomain_core()
{
load_plugin_textdomain(
'dreamsalon-core',
false,
dirname(plugin_basename(__FILE__)) . '/languages'
);
}
// Include CPT tempalets
require_once plugin_dir_path(__FILE__) . 'includes/cpt-salon.php';
require_once plugin_dir_path(__FILE__) . 'includes/recent-posts-widgets.php';
require_once plugin_dir_path(__FILE__) . 'includes/search-widget.php';
require_once plugin_dir_path(__FILE__) . 'includes/categories-widget.php';
require_once plugin_dir_path(__FILE__) . 'includes/share-widget.php';
require_once plugin_dir_path(__FILE__) . 'includes/tags-widget.php';
require_once plugin_dir_path(__FILE__) . 'includes/archieve-widget.php';
if (! class_exists('Gamajo_Template_Loader')) {
require_once plugin_dir_path(__FILE__) . 'lib/class-gamajo-template-loader.php';
}
require_once plugin_dir_path(__FILE__) . 'includes/dreamsrent-core-templates.php';
// inc files
require_once plugin_dir_path(__FILE__) . 'inc/functions.php';
require_once plugin_dir_path(__FILE__) . 'inc/init.php';
// Dashboard shortcode
add_shortcode('dreamsalon_dashboard', 'dreamsalon_dashboard_shortcode');
function dreamsalon_dashboard_shortcode($atts) {
ob_start();
if (!is_user_logged_in()) {
echo '<div class="alert alert-danger my-2 py-3" role="alert">' . esc_html__('Please log in to access the dashboard.', 'dreamsalon-core') . '</div>';
return ob_get_clean();
}
// Load the dashboard template
include( plugin_dir_path(__FILE__).'templates/dashboard/dashboard-main.php' );
return ob_get_clean();
}
// Frontend bookings shortcode
add_shortcode('dreamsalon_bookings', function($atts){
ob_start();
include plugin_dir_path(__FILE__) . 'templates/bookings.php';
return ob_get_clean();
});
add_filter('theme_page_templates', 'DreamService_register_page_template');
function DreamService_register_page_template($templates)
{
$templates['page-template-add-service.php'] = 'Dreams Template Add Service';
return $templates;
}
add_filter('template_include', 'DreamService_load_page_template');
function DreamService_load_page_template($template)
{
if (is_page()) {
$chosen_template = get_page_template_slug(get_queried_object_id());
if ($chosen_template == 'page-template-add-service.php') {
$new_template = plugin_dir_path(__FILE__) . 'templates/page-template-add-service.php';
if (file_exists($new_template)) {
return $new_template;
}
}
}
return $template;
}
add_filter('theme_page_templates', 'DreamService_bookings_page_template');
function DreamService_bookings_page_template($templates)
{
$templates['bookings.php'] = 'Dreams Template Bookings';
return $templates;
}
add_filter('template_include', 'DreamService_bookings_load_page_template');
function DreamService_bookings_load_page_template($template)
{
if (is_page()) {
$chosen_template = get_page_template_slug(get_queried_object_id());
if ($chosen_template == 'bookings.php') {
$new_template = plugin_dir_path(__FILE__) . 'templates/bookings.php';
if (file_exists($new_template)) {
return $new_template;
}
}
}
return $template;
}
add_filter('single_template', 'DreamService_single_template');
function DreamService_single_template($single)
{
global $post;
if ($post->post_type == 'service') {
$template = plugin_dir_path(__FILE__) . 'templates/single-service.php';
if (file_exists($template)) {
return $template;
}
}
return $single;
}
// Create bookings table on plugin activation
register_activation_hook(__FILE__, 'ds_install_bookings_table');
function ds_install_bookings_table(){
global $wpdb;
$table = $wpdb->prefix . 'ds_bookings';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS `$table` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`order_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`order_item_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`product_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`service_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`customer_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`addons` longtext NULL,
`staff_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`date` date NULL,
`slot` varchar(50) NULL,
`branch_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`branch_title` varchar(190) NOT NULL DEFAULT '',
`branch_location` varchar(255) NOT NULL DEFAULT '',
`amount` decimal(18,2) NOT NULL DEFAULT 0.00,
`currency` varchar(10) NOT NULL DEFAULT '',
`payment_status` varchar(20) NOT NULL DEFAULT '',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `order_id` (`order_id`),
KEY `service_id` (`service_id`),
KEY `customer_id` (`customer_id`)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
// Also create enquiries table for service enquiries and threaded replies
$enq = $wpdb->prefix . 'ds_enquiries';
$sql_enq = "CREATE TABLE IF NOT EXISTS `$enq` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`service_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`parent_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`user_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`name` varchar(190) NOT NULL DEFAULT '',
`email` varchar(190) NOT NULL DEFAULT '',
`phone` varchar(50) NOT NULL DEFAULT '',
`message` longtext NULL,
`status` varchar(20) NOT NULL DEFAULT 'open',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `service_id` (`service_id`),
KEY `parent_id` (`parent_id`),
KEY `user_id` (`user_id`)
) $charset_collate;";
dbDelta($sql_enq);
$wishlist_table = $wpdb->prefix . 'ds_wishlists';
$sql2 = "CREATE TABLE {$wishlist_table} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
user_id bigint(20) unsigned NOT NULL,
service_id bigint(20) unsigned NOT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY user_service (user_id, service_id),
KEY service_id (service_id)
) {$charset_collate};";
dbDelta($sql2);
}
add_action('init', 'ds_upgrade_ds_bookings_schema', 20);
function ds_upgrade_ds_bookings_schema(){
global $wpdb;
$table = $wpdb->prefix . 'ds_bookings';
$exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table));
if ($exists !== $table){
return;
}
$columns = array(
'branch_id' => "ALTER TABLE {$table} ADD `branch_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 AFTER `slot`",
'branch_title' => "ALTER TABLE {$table} ADD `branch_title` VARCHAR(190) NOT NULL DEFAULT '' AFTER `branch_id`",
'branch_location' => "ALTER TABLE {$table} ADD `branch_location` VARCHAR(255) NOT NULL DEFAULT '' AFTER `branch_title`",
);
foreach ($columns as $col => $ddl){
$has_column = $wpdb->get_var( $wpdb->prepare("SHOW COLUMNS FROM {$table} LIKE %s", $col) );
if (! $has_column){
$wpdb->query($ddl);
}
}
ds_backfill_booking_branch_data($table);
}
function ds_backfill_booking_branch_data($table){
global $wpdb;
if (get_option('ds_bookings_branch_backfill_complete')){
return;
}
$rows = $wpdb->get_results("SELECT id, service_id FROM {$table} WHERE service_id > 0 AND (branch_id = 0 OR branch_title = '') LIMIT 200");
if (empty($rows)){
update_option('ds_bookings_branch_backfill_complete', 1);
return;
}
foreach ($rows as $row){
$service_id = (int) $row->service_id;
if (! $service_id){
continue;
}
$branches = get_the_terms($service_id, 'service_branch');
if (is_wp_error($branches) || empty($branches)){
continue;
}
$branch = $branches[0];
$branch_id = (int) $branch->term_id;
$branch_title = $branch->name ? sanitize_text_field($branch->name) : '';
$branch_location = '';
if ($branch_id){
$desc = term_description($branch_id, 'service_branch');
if ($desc){
$branch_location = wp_strip_all_tags($desc);
}
}
$wpdb->update(
$table,
array(
'branch_id' => $branch_id,
'branch_title' => $branch_title,
'branch_location' => $branch_location,
),
array('id' => (int) $row->id),
array('%d','%s','%s'),
array('%d')
);
}
}
// Add template to page attributes
function add_service_grid_template($templates)
{
$templates['service-grid.php'] = 'Service Grid Template';
return $templates;
}
add_filter('theme_page_templates', 'add_service_grid_template');
// Load the template
function load_service_grid_template($template)
{
if (is_page()) {
$chosen_template = get_page_template_slug(get_queried_object_id());
if ($chosen_template == 'service-grid.php') {
$new_template = plugin_dir_path(__FILE__) . 'templates/service-grid.php';
if (file_exists($new_template)) {
return $new_template;
}
}
}
return $template;
}
add_filter('template_include', 'load_service_grid_template');
function add_service_list_template($templates)
{
$templates['service-list.php'] = 'Service List Template';
return $templates;
}
add_filter('theme_page_templates', 'add_service_list_template');
// Load the template
function load_service_list_template($template)
{
if (is_page()) {
$chosen_template = get_page_template_slug(get_queried_object_id());
if ($chosen_template == 'service-list.php') {
$new_template = plugin_dir_path(__FILE__) . 'templates/service-list.php';
if (file_exists($new_template)) {
return $new_template;
}
}
}
return $template;
}
add_filter('template_include', 'load_service_list_template');
if ( ! function_exists('dreamsalon_sentencecase') ) {
function dreamsalon_sentencecase( $string ) {
// Trim spaces
$string = trim($string);
// Convert entire string to lowercase (UTF-8 safe)
$string = mb_strtolower($string, 'UTF-8');
// Uppercase the first letter of each word
$string = mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
return $string;
}
}
function DreamService_is_in_wishlist($user_id, $service_id)
{
if (! $user_id || ! $service_id) {
return false;
}
global $wpdb;
$table = $wpdb->prefix . 'ds_wishlists';
$exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$table} WHERE user_id=%d AND service_id=%d LIMIT 1", $user_id, $service_id));
return ! empty($exists);
}
function DreamService_get_wishlist_count($user_id)
{
if (! $user_id) {
return 0;
}
global $wpdb;
$table = $wpdb->prefix . 'ds_wishlists';
$count = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$table} WHERE user_id=%d", $user_id));
return $count;
}
/**
* Toggle wishlist (AJAX, logged-in only)
*/
add_action('wp_ajax_DreamService_toggle_wishlist', 'DreamService_toggle_wishlist');
function DreamService_toggle_wishlist()
{
if (! is_user_logged_in()) {
wp_send_json_error(array('message' => __('Login required', 'dreamsalon-core')), 401);
}
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
if (! wp_verify_nonce($nonce, 'DreamService_wishlist')) {
wp_send_json_error(array('message' => __('Security check failed', 'dreamsalon-core')), 403);
}
$service_id = isset($_POST['service_id']) ? absint($_POST['service_id']) : 0;
if (! $service_id || get_post_type($service_id) !== 'service') {
wp_send_json_error(array('message' => __('Invalid service', 'dreamsalon-core')), 400);
}
$user_id = get_current_user_id();
$user = get_user_by('id', $user_id);
if (! $user || ! in_array('customer', (array) $user->roles, true)) {
wp_send_json_error(array('message' => __('Only customers can use wishlist.', 'dreamsalon-core')), 403);
}
global $wpdb;
$table = $wpdb->prefix . 'ds_wishlists';
if (DreamService_is_in_wishlist($user_id, $service_id)) {
$wpdb->delete($table, array('user_id' => $user_id, 'service_id' => $service_id), array('%d', '%d'));
$count = DreamService_get_wishlist_count($user_id);
wp_send_json_success(array('status' => 'removed', 'count' => $count));
} else {
$wpdb->insert($table, array('user_id' => $user_id, 'service_id' => $service_id, 'created_at' => current_time('mysql')), array('%d', '%d', '%s'));
$count = DreamService_get_wishlist_count($user_id);
wp_send_json_success(array('status' => 'added', 'count' => $count));
}
}
/**
* Load archive template for Service CPT
*/
add_filter('archive_template', 'DreamService_archive_template');
function DreamService_archive_template($archive)
{
if (is_post_type_archive('service')) {
$template = plugin_dir_path(__FILE__) . 'templates/archive-service.php';
if (file_exists($template)) {
return $template;
}
}
return $archive;
}
function add_login_template($templates)
{
$templates['login-form.php'] = 'Dreams Login Template';
return $templates;
}
add_filter('theme_page_templates', 'add_login_template');
function load_login_template($template)
{
if (is_page()) {
$chosen_template = get_page_template_slug(get_queried_object_id());
if ($chosen_template == 'login-form.php') {
$new_template = plugin_dir_path(__FILE__) . 'templates/login-form.php';
if (file_exists($new_template)) {
return $new_template;
}
}
}
return $template;
}
add_filter('template_include', 'load_login_template');