HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1014-azure #14~24.04.1-Ubuntu SMP Fri Oct 3 20:52:11 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/dev/dev-dreamstour-wp/wp-content/themes/dreamstour/functions.php
<?php
if ( ! defined('ABSPATH') ) {
    exit;
}

 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( DREAMSTOUR_URL.'/inc/dreamstour-demo-content.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' ),
    ) );
     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);
//   Security codes
// Remove WordPress version number
remove_action('wp_head', 'wp_generator');
// Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');

  function add_security_headers() {
    header( 'X-Frame-Options: SAMEORIGIN' );
    header( 'X-Content-Type-Options: nosniff' );
    header( 'X-XSS-Protection: 1; mode=block' );
    header( 'Referrer-Policy: strict-origin-when-cross-origin' );
}
add_action( 'send_headers', 'add_security_headers' );

add_filter('rest_authentication_errors', function($result){
    if (!is_user_logged_in()) {
        return new WP_Error('rest_forbidden', 'Login required', ['status'=>401]);
    }
    return $result;
});