File: /mnt/data/smarthr-co-in/wp-content/themes/dreamslanding/functions.php
<?php
if(defined('DREAMSTOUR_URL') == false) define('DREAMSTOUR_URL', get_template_directory());
if(defined('DREAMSTOUR_URI') == false) define('DREAMSTOUR_URI', get_template_directory_uri());
add_action('after_setup_theme', 'dreamstour_load_textdomain');
function dreamstour_load_textdomain() {
load_theme_textdomain('dreamstour', get_template_directory() . '/languages');
}
// Require metabox
if( is_admin() ){
// Require TGM
require_once ( DREAMSTOUR_URL.'/inc/active_plugins.php' );
require_once (DREAMSTOUR_URL . '/inc/class-tgm-plugin-activation.php');
}
// require libraries, function
require( DREAMSTOUR_URL.'/inc/init.php' );
// Add js, css
require( DREAMSTOUR_URL.'/inc/add_js_css.php' );
// register menu, widget
require( DREAMSTOUR_URL.'/inc/register_widget.php' );
// require menu
require_once (DREAMSTOUR_URL.'/inc/dsr_walker_nav_menu.php');
// require content
require_once (DREAMSTOUR_URL.'/templates/blog/define_blocks_content.php');
// require breadcrumbs
require( DREAMSTOUR_URL.'/inc/breadcrumbs.php' );
//Redux Functions
if(in_array('redux-framework/redux-framework.php', apply_filters('active_plugins', get_option('active_plugins'))))
{
add_action( 'after_setup_theme', 'dreamstour_register_redux_sections', 20 );
function dreamstour_register_redux_sections() {
require_once ( DREAMSTOUR_URL.'/inc/options-init.php' );
}
}
function dreamstour_remove_widget_editort() {
remove_theme_support( 'widgets-block-editor' );
}
add_action( 'after_setup_theme', 'dreamstour_remove_widget_editort' );
//get options from wp_options for redux
if( !function_exists('dreamstour_fl_framework_getoptions') )
{
function dreamstour_fl_framework_getoptions($get_text)
{
global $dreamstour_theme_options;
if(isset($dreamstour_theme_options[$get_text]) && $dreamstour_theme_options[$get_text] !=""):
return $dreamstour_theme_options[$get_text];
else:
return false;
endif;
}
}
// Remove <p> and <br/> from Contact Form 7
add_filter('wpcf7_autop_or_not', '__return_false');
//added for password and confirm password
function enqueue_jquery() {
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'enqueue_jquery');
require_once ( get_template_directory() . '/inc/typo.php');
// Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');
add_filter( 'woocommerce_should_load_paypal_standard', '__return_true' );
add_theme_support( 'woocommerce' );
function add_paypal_standard_gateway($gateways) {
$gateways[] = 'WC_Gateway_Paypal';
return $gateways;
}
add_filter( 'woocommerce_payment_gateways', 'add_paypal_standard_gateway' );
function my_theme_register_block_styles() {
// Add custom styles for the Paragraph block
register_block_style(
'core/paragraph',
array(
'name' => 'fancy-paragraph',
'label' => __( 'Fancy Paragraph', 'dreamstour' ),
'inline_style' => '.wp-block-paragraph.is-style-fancy-paragraph { font-style: italic; color: #3498db; }',
)
);
// Add custom styles for the Image block
register_block_style(
'core/image',
array(
'name' => 'rounded-image',
'label' => __( 'Rounded Image', 'dreamstour' ),
'inline_style' => '.wp-block-image.is-style-rounded-image img { border-radius: 50%; }',
)
);
}
add_action( 'init', 'my_theme_register_block_styles' );
function my_theme_register_block_patterns() {
// Register a simple hero block pattern
register_block_pattern(
'my-theme/hero-section',
array(
'title' => __( 'Hero Section', 'dreamstour' ),
'description' => __( 'A hero section with a heading, paragraph, and button.', 'dreamstour' ),
'content' => '<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"50px","bottom":"50px"}}},"backgroundColor":"primary","textColor":"white"} -->
<div class="wp-block-group alignfull has-white-color has-primary-background-color has-text-color has-background" style="padding-top:50px;padding-bottom:50px;">
<!-- wp:heading {"textAlign":"center"} -->
<h2 style="text-align:center">Welcome to My Website</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"align":"center"} -->
<p style="text-align:center">This is a hero section to grab attention and provide important information.</p>
<!-- /wp:paragraph -->
<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"center"}} -->
<div class="wp-block-buttons">
<!-- wp:button {"backgroundColor":"secondary","textColor":"white"} -->
<div class="wp-block-button"><a class="wp-block-button__link has-white-color has-secondary-background-color has-text-color has-background">Learn More</a></div>
<!-- /wp:button -->
</div>
<!-- /wp:buttons -->
</div>
<!-- /wp:group -->',
'categories' => array( 'featured' ),
)
);
}
add_action( 'init', 'my_theme_register_block_patterns' );
/**
* Theme setup for adding block editor support.
*/
function dreamstour_theme_setup() {
add_theme_support( 'custom-logo', array(
'height' => 40,
'width' => 180,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
) );
// SEO: allow WP/SEO plugins to manage <title>, feeds, and semantic HTML5
add_theme_support( 'title-tag' );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'script', 'style', 'navigation-widgets' ) );
add_theme_support( 'wp-block-styles' );
add_theme_support( 'align-wide' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'editor-styles' );
add_editor_style( 'editor-style.css' );
}
add_action( 'after_setup_theme', 'dreamstour_theme_setup' );
// Fix SVG thumbnail display in Media Library
function fix_svg_display_admin() {
echo '<style>
.attachment-266x266, .thumbnail img {
width: 100% !important;
height: auto !important;
}
</style>';
}
add_action( 'admin_head', 'fix_svg_display_admin' );
add_filter( 'site_status_tests', function( $tests ) {
unset( $tests['direct']['persistent_object_cache'] );
return $tests;
});
// Remove the default "Have a coupon?" notice at the top of checkout
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
// Enqueue JS only when WooCommerce is active
function dreamstour_checkout_coupon_js() {
// Make sure WooCommerce functions exist
if ( function_exists( 'is_checkout' ) && is_checkout() ) {
wp_add_inline_script( 'wc-checkout', "
jQuery(function($){
$('body').on('click','.apply-coupon',function(e){
e.preventDefault();
var code = $('#coupon_code').val();
if(!code) return;
var data = {
security: wc_checkout_params.apply_coupon_nonce,
coupon_code: code
};
$.ajax({
type: 'POST',
url: wc_checkout_params.wc_ajax_url.toString().replace('%%endpoint%%', 'apply_coupon'),
data: data,
success: function(response){
// Show any notices returned by WooCommerce
$('.coupon-notices').html(response);
// Refresh checkout totals
$('body').trigger('update_checkout');
}
});
});
});
" );
}
}
add_action( 'wp_enqueue_scripts', 'dreamstour_checkout_coupon_js' );
function my_enqueue_google_places() {
if (is_page_template('page-template-add-tour.php')) { // Load only on specific template
global $dreamstour_theme_options;
$api_key = dreamstour_fl_framework_getoptions('map_api_key');
if ($api_key) {
wp_enqueue_script(
'google-places-api',
'https://maps.googleapis.com/maps/api/js?key=' . esc_attr($api_key) . '&libraries=places',
array(),
null,
true
);
}
}
}
add_action('wp_enqueue_scripts', 'my_enqueue_google_places');
function enqueue_google_maps_script() {
if (is_single()) { // Only on single posts
global $dreamstour_theme_options;
$api_key = dreamstour_fl_framework_getoptions('map_api_key');
if ($api_key) {
wp_enqueue_script(
'google-maps',
'https://maps.googleapis.com/maps/api/js?key=' . esc_attr($api_key) . '&callback=initMap',
array(),
null,
true
);
}
}
}
add_action('wp_enqueue_scripts', 'enqueue_google_maps_script');
// Add query variables
add_filter('query_vars', 'dreamstour_dashboard_query_vars');
function dreamstour_dashboard_query_vars($vars) {
$vars[] = 'user_type';
$vars[] = 'dashboard_tab';
return $vars;
}
add_action('admin_post_dt_set_tour_status', 'dreamstour_handle_set_tour_status');
function dreamstour_handle_set_tour_status() {
if (!is_user_logged_in()) {
wp_die(esc_html__('You must be logged in to perform this action.', 'dreamstour'));
}
$post_id = isset($_GET['post_id']) ? (int) $_GET['post_id'] : 0;
$status = isset($_GET['status']) ? sanitize_key($_GET['status']) : '';
if (!$post_id || !in_array($status, array('publish', 'draft'), true)) {
wp_die(esc_html__('Invalid request.', 'dreamstour'));
}
$nonce_action = 'dt_set_tour_status_' . $post_id;
if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], $nonce_action)) {
wp_die(esc_html__('Security check failed.', 'dreamstour'));
}
// Capability check
if (!current_user_can('edit_post', $post_id)) {
wp_die(esc_html__('You are not allowed to update this tour.', 'dreamstour'));
}
$post = get_post($post_id);
if (!$post || (int)$post->post_author !== (int)get_current_user_id()) {
wp_die(esc_html__('You are not the owner of this tour.', 'dreamstour'));
}
// Update status
$updated = wp_update_post(array(
'ID' => $post_id,
'post_status' => $status,
), true);
if (is_wp_error($updated)) {
wp_die(esc_html__('Failed to update status.', 'dreamstour'));
}
// Redirect back
$redirect = wp_get_referer();
if (!$redirect) {
$redirect = home_url('/');
}
wp_safe_redirect($redirect);
exit;
}
function dreamstour_improve_checkout_state_handling($fields) {
if (isset($fields['billing']['billing_state'])) {
$fields['billing']['billing_state']['class'] = ['form-row-wide', 'address-field', 'validate-state'];
}
return $fields;
}
add_filter('woocommerce_checkout_fields', 'dreamstour_improve_checkout_state_handling', 20);
add_filter('get_avatar_data', 'dreamstour_custom_avatar_data', 10, 2);
function dreamstour_custom_avatar_data($args, $id_or_email) {
$user = false;
if (is_numeric($id_or_email)) {
$user = get_user_by('id', (int) $id_or_email);
} elseif (is_object($id_or_email)) {
if (!empty($id_or_email->user_id)) {
$user = get_user_by('id', (int) $id_or_email->user_id);
}
} else {
$user = get_user_by('email', $id_or_email);
}
if ($user && is_object($user)) {
$avatar_id = get_user_meta($user->ID, 'custom_avatar_id', true);
if (!$avatar_id) {
$avatar_id = get_user_meta($user->ID, 'simple_local_avatar', true);
}
if (!$avatar_id) {
$avatar_id = get_user_meta($user->ID, 'wp_user_avatar', true);
}
if ($avatar_id) {
$size = isset($args['size']) ? $args['size'] : 96;
$avatar_url = wp_get_attachment_image_url($avatar_id, array($size, $size));
if ($avatar_url) {
$args['url'] = $avatar_url;
$args['default'] = false;
}
}
}
return $args;
}
function add_agent_role_on_admin_promotion($user_id, $new_role, $old_roles) {
if ($new_role === 'administrator') {
$user = get_userdata($user_id);
$user->add_role('agent');
}
}
add_action('set_user_role', 'add_agent_role_on_admin_promotion', 10, 3);
if (!function_exists('dt_get_wpdb')) {
/**
* Return the global $wpdb instance
*
* @return wpdb
*/
function dt_get_wpdb() {
return $GLOBALS['wpdb'];
}
}
function dreamstour_meta_description() {
if (apply_filters('dreamstour_skip_meta_description', false)) {
return;
}
if (class_exists('WPSEO_Frontend') || defined('RANK_MATH_VERSION') || defined('SEOPRESS_VERSION') || class_exists('All_in_One_SEO_Pack')) {
return;
}
$desc = '';
if (is_singular()) {
global $post;
if ($post) {
if (has_excerpt($post)) {
$desc = get_the_excerpt($post);
} else {
$desc = wp_trim_words(wp_strip_all_tags($post->post_content), 30, '');
}
}
} elseif (is_home() || is_front_page()) {
$desc = get_bloginfo('description', 'display');
} elseif (is_category() || is_tag() || is_tax()) {
$term = get_queried_object();
if (!empty($term) && !empty($term->description)) {
$desc = wp_trim_words(wp_strip_all_tags($term->description), 30, '');
}
} elseif (is_search()) {
$desc = sprintf(__('Search results for "%s"', 'dreamstour'), get_search_query());
} elseif (is_archive()) {
$desc = get_bloginfo('description', 'display');
}
$desc = trim($desc);
$desc = wp_strip_all_tags($desc);
if (empty($desc)) {
return;
}
$desc = esc_attr($desc);
echo '<meta name="description" content="' . $desc . '" />' . "\n";
}
add_action('wp_head', 'dreamstour_meta_description', 1);
add_action('wp_head', 'add_microsoft_clarity');
function add_microsoft_clarity() {
echo '<script type="text/javascript">
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "qnzic4lsn0");
</script>';
}