HEX
Server: Apache
System: Linux 65-254-81-60.cprapid.com 4.18.0-477.27.2.el8_8.x86_64 #1 SMP Fri Sep 29 08:21:01 EDT 2023 x86_64
User: roshanchandy (1003)
PHP: 8.4.12
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/roshanchandy/public_html/petiva/wp-content/themes/petiva/functions.php
<?php

/**
 * petiva functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package petiva
 */

if (!function_exists('petiva_setup')):
    /**
     * Sets up theme defaults and registers support for various WordPress features.
     *
     * Note that this function is hooked into the after_setup_theme hook, which
     * runs before the init hook. The init hook is too late for some features, such
     * as indicating support for post thumbnails.
     */
    function petiva_setup()
    {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you're building a theme based on petiva, use a find and replace
         * to change 'petiva' to the name of your theme in all the template files.
         */
        load_theme_textdomain('petiva', get_template_directory() . '/languages');

        // Add default posts and comments RSS feed links to head.
        add_theme_support('automatic-feed-links');

        /*
         * Let WordPress manage the document title.
         * By adding theme support, we declare that this theme does not use a
         * hard-coded <title> tag in the document head, and expect WordPress to
         * provide it for us.
         */
        add_theme_support('title-tag');

        /*
         * Enable support for Post Thumbnails on posts and pages.
         *
         * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
         */
        add_theme_support('post-thumbnails');

        // This theme uses wp_nav_menu() in one location.
        register_nav_menus([
            'main-menu' => esc_html__('Main Menu', 'petiva'),
            'category-menu' => esc_html__('Category Menu', 'petiva'),
            'footer-menu' => esc_html__('Footer Menu', 'petiva'),
        ]);

        /*
         * Switch default core markup for search form, comment form, and comments
         * to output valid HTML5.
         */
        add_theme_support('html5', [
            'search-form',
            'comment-form',
            'comment-list',
            'gallery',
            'caption',
        ]);

        // Set up the WordPress core custom background feature.
        add_theme_support('custom-background', apply_filters('petiva_custom_background_args', [
            'default-color' => 'ffffff',
            'default-image' => '',
        ]));

        // Add theme support for selective refresh for widgets.
        add_theme_support('customize-selective-refresh-widgets');

        //Enable custom header
        add_theme_support('custom-header');

        /**
         * Add support for core custom logo.
         *
         * @link https://codex.wordpress.org/Theme_Logo
         */
        add_theme_support('custom-logo', [
            'height'      => 250,
            'width'       => 250,
            'flex-width'  => true,
            'flex-height' => true,
        ]);

        /**
         * Enable suporrt for Post Formats
         *
         * see: https://codex.wordpress.org/Post_Formats
         */
        add_theme_support('post-formats', [
            'image',
            'audio',
            'video',
            'gallery',
            'quote',
        ]);

        // Add support for Block Styles.
        add_theme_support('wp-block-styles');

        // Add support for full and wide align images.
        add_theme_support('align-wide');

        // Add support for editor styles.
        add_theme_support('editor-styles');

        // Add support for responsive embedded content.
        add_theme_support('responsive-embeds');

        remove_theme_support('widgets-block-editor');

        add_image_size('petiva-blog', 416, 235, ['center', 'center']);

        add_theme_support('widgets');
    }
endif;
add_action('after_setup_theme', 'petiva_setup');




add_theme_support('block-templates');
add_theme_support('editor-styles');
add_theme_support('wp-block-styles');


/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function petiva_content_width()
{
    // This variable is intended to be overruled from themes.
    // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
    $GLOBALS['content_width'] = apply_filters('petiva_content_width', 640);
}
add_action('after_setup_theme', 'petiva_content_width', 0);



/**
 * Enqueue scripts and styles.
 */

define('PETIVA_THEME_DIR', get_template_directory());
define('PETIVA_THEME_URI', get_template_directory_uri());
define('PETIVA_THEME_CSS_DIR', PETIVA_THEME_URI . '/assets/css/');
define('PETIVA_THEME_JS_DIR', PETIVA_THEME_URI . '/assets/js/');
define('PETIVA_THEME_INC', PETIVA_THEME_DIR . '/inc/');



// wp_body_open
if (!function_exists('wp_body_open')) {
    function wp_body_open()
    {
        do_action('wp_body_open');
    }
}

/**
 * Implement the Custom Header feature.
 */
require PETIVA_THEME_INC . 'custom-header.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require PETIVA_THEME_INC . 'template-functions.php';

/**
 * Custom template helper function for this theme.
 */
require PETIVA_THEME_INC . 'template-helper.php';

/**
 * initialize kirki customizer class.
 */
include_once PETIVA_THEME_INC . 'kirki-customizer.php';
include_once PETIVA_THEME_INC . 'class-kirki.php';

/**
 * Load Jetpack compatibility file.
 */
if (defined('JETPACK__VERSION')) {
    require PETIVA_THEME_INC . 'jetpack.php';
}



/**
 * include petiva functions file
 */
require_once PETIVA_THEME_INC . 'class-navwalker.php';
require_once PETIVA_THEME_INC . 'class-tgm-plugin-activation.php';
require_once PETIVA_THEME_INC . 'add_plugin.php';
require_once PETIVA_THEME_INC . '/common/breadcrumb.php';
require_once PETIVA_THEME_INC . '/common/scripts.php';
require_once PETIVA_THEME_INC . '/common/widgets.php';





/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function petiva_pingback_header()
{
    if (is_singular() && pings_open()) {
        printf('<link rel="pingback" href="%s">', esc_url(get_bloginfo('pingback_url')));
    }
}
add_action('wp_head', 'petiva_pingback_header');

// change textarea position in comment form
// ----------------------------------------------------------------------------------------
function petiva_move_comment_textarea_to_bottom($fields)
{
    $comment_field       = $fields['comment'];
    unset($fields['comment']);
    $fields['comment'] = $comment_field;
    return $fields;
}
add_filter('comment_form_fields', 'petiva_move_comment_textarea_to_bottom');


// petiva_comment 
if (!function_exists('petiva_comment')) {
    function petiva_comment($comment, $args, $depth)
    {
        $GLOBAL['comment'] = $comment;
        extract($args, EXTR_SKIP);
        $args['reply_text'] = 'Reply';
        $replayClass = 'comment-depth-' . esc_attr($depth);
?>
        <li id="comment-<?php comment_ID(); ?>">
            <div class="comments-box grey-bg-2">
                <div class="comments-avatar">
                    <?php print get_avatar($comment, 102, null, null, ['class' => []]); ?>
                </div>
                <div class="comments-text">
                    <div class="avatar-name">
                        <h5><?php print get_comment_author_link(); ?></h5>
                        <span><?php comment_time(get_option('date_format')); ?></span>
                    </div>
                    <?php comment_text(); ?>

                    <div class="comments-replay">
                        <?php comment_reply_link(array_merge($args, ['depth' => $depth, 'max_depth' => $args['max_depth']])); ?>
                    </div>

                </div>
            </div>
    <?php
    }
}


/**
 * shortcode supports for removing extra p, spance etc
 *
 */
add_filter('the_content', 'petiva_shortcode_extra_content_remove');
/**
 * Filters the content to remove any extra paragraph or break tags
 * caused by shortcodes.
 *
 * @since 1.0.0
 *
 * @param string $content  String of HTML content.
 * @return string $content Amended string of HTML content.
 */
function petiva_shortcode_extra_content_remove($content)
{

    $array = [
        '<p>['    => '[',
        ']</p>'   => ']',
        ']<br />' => ']',
    ];
    return strtr($content, $array);
}

// petiva_search_filter_form
if (!function_exists('petiva_search_filter_form')) {
    function petiva_search_filter_form($form)
    {

        $form = sprintf(
            '<div class="sidebar__widget-px"><div class="search-px"><form class="sidebar__search p-relative d-flex align-items-center" action="%s" method="get">
      	<input type="text" value="%s" required name="s" placeholder="%s">
      	<button type="submit"> <i class="fa-solid fa-magnifying-glass"></i> </button>
		</form></div></div>',
            esc_url(home_url('/')),
            esc_attr(get_search_query()),
            esc_html__('Search', 'petiva')
        );

        return $form;
    }
    add_filter('get_search_form', 'petiva_search_filter_form');
}




add_action('admin_enqueue_scripts', 'petiva_admin_custom_scripts');

function petiva_admin_custom_scripts()
{
    wp_enqueue_media();
    wp_enqueue_style('customizer-style', get_template_directory_uri() . '/inc/css/customizer-style.css', array());
    wp_register_script('petiva-admin-custom', get_template_directory_uri() . '/inc/js/admin_custom.js', ['jquery'], '', true);
    wp_enqueue_script('petiva-admin-custom');
}





function petiva_enqueue_dynamic_styles()
{
    require get_template_directory() . '/inc/dynamic-style.php';
}

add_action('wp_head', 'petiva_enqueue_dynamic_styles');


// ========================Woocommerce Support=============================//

if (class_exists('WooCommerce')) {
    add_theme_support('woocommerce');
    add_theme_support('wc-product-gallery-zoom');
    add_theme_support('wc-product-gallery-lightbox');
    add_theme_support('wc-product-gallery-slider');
}



// Enable block editor support
add_theme_support('editor-styles');
add_theme_support('align-wide');

if (class_exists('WooCommerce')) {
    // Number of Woocommerce Related Product
    add_filter('woocommerce_output_related_products_args', 'bbloomer_change_number_related_products', 9999);
    function bbloomer_change_number_related_products($args)
    {
        $args['posts_per_page'] = 3; // # of related products
        $args['columns'] = 3; // # of columns per row
        return $args;
    }

    // Change the number of products per row (columns) on WooCommerce shop page
    function custom_woocommerce_product_columns($columns)
    {
        return 3; // Change this to the number of columns you want (e.g., 3, 4, 5)
    }
    add_filter('loop_shop_columns', 'custom_woocommerce_product_columns');
    // Remove woocommerce defauly styles
    //add_filter( 'woocommerce_enqueue_styles', '__return_false' );

    // Plus minus in shop details page
    // Customize quantity input fields
    // Show plus minus buttons shop details page
    add_action('woocommerce_after_quantity_input_field', 'bbloomer_display_quantity_plus');
    function bbloomer_display_quantity_plus()
    {
        echo '<button type="button" class="ph ph-plus" onclick="this.previousElementSibling.stepUp();"></button>';
    }

    add_action('woocommerce_before_quantity_input_field', 'bbloomer_display_quantity_minus');
    function bbloomer_display_quantity_minus()
    {
        // Change nextElementSibling to nextElementSibling.nextElementSibling
        echo '<button type="button" class="ph ph-minus" onclick="this.nextElementSibling.nextElementSibling.stepDown();"></button>';
    }



    // ajax for woocommerce add to cart button 


    // Handle Ajax request to load cart contents and include remove button
    function load_cart_contents()
    {
        // Get cart data
        $cart = WC()->cart->get_cart();
        $output = '';

        if (! empty($cart)) {
            foreach ($cart as $cart_item_key => $cart_item) {
                $product = $cart_item['data'];
                $product_id = $cart_item['product_id'];
                $product_name = esc_html($product->get_name());
                $product_price = wc_price($product->get_price());
                $product_qty = esc_html($cart_item['quantity']);

                // Get product image URL
                $product_image_url = wp_get_attachment_image_url($product->get_image_id(), 'thumbnail'); // Get the product thumbnail image

                // Generate a unique ID for the cart item (this will be used for removal)
                $remove_button_id = 'remove-' . $cart_item_key;

                // Output HTML for each cart item with the product image and remove button
                $output .= '<div class="cart-item" data-cart-item-key="' . esc_attr($cart_item_key) . '">';
                $output .= '<img src="' . esc_url($product_image_url) . '" alt="' . esc_attr($product_name) . '" class="cart-item-image">';
                $output .= '<div class="cart-item-details">';
                $output .= '<p class="cart-item-name">' . $product_name . '</p>';
                $output .= '<p class="cart-item-quantity">Qty: ' . $product_qty . '</p>';
                $output .= '<p class="cart-item-price">' . $product_price . '</p>';
                $output .= '</div>'; // .cart-item-details
                $output .= '<button class="remove-item-btn" data-cart-item-key="' . esc_attr($cart_item_key) . '" id="' . esc_attr($remove_button_id) . '">Remove</button>';
                $output .= '</div>'; // .cart-item
            }
        } else {
            $output .= "<p>Your cart is empty!</p>";
        }
        // Escape the whole output before echoing it
        echo wp_kses_post($output);  // Escapes the output for HTML context

        wp_die(); // Always call wp_die() after Ajax response
    }
    add_action('wp_ajax_load_cart_contents', 'load_cart_contents'); // For logged-in users
    add_action('wp_ajax_nopriv_load_cart_contents', 'load_cart_contents'); // For non-logged-in users



    // Handle Ajax request to remove a product from the cart
    function remove_cart_item()
    {
        // Get the cart item key from the Ajax request
        $cart_item_key = isset($_POST['cart_item_key']) ? sanitize_text_field($_POST['cart_item_key']) : '';

        if ($cart_item_key) {
            // Remove the item from the cart
            WC()->cart->remove_cart_item($cart_item_key);

            // Return the updated cart contents (optional, but useful for showing updated data)
            WC_AJAX::get_refreshed_fragments(); // Refresh the cart fragments
        }

        wp_die(); // Always call wp_die() after Ajax response
    }
    add_action('wp_ajax_remove_cart_item', 'remove_cart_item'); // For logged-in users
    add_action('wp_ajax_nopriv_remove_cart_item', 'remove_cart_item'); // For non-logged-in users

}



// RECOMMENDED:
function petiva_add_editor_styles()
{
    add_editor_style('css/custom-editor-style.css');
}
add_action('admin_init', 'petiva_add_editor_styles');

function petiva_register_block_patterns()
{
    register_block_pattern('my-theme/my-pattern', array(
        'title'       => __('My Pattern', 'petiva'),
        'description' => _x('A custom pattern for my theme.', 'Block pattern description', 'petiva'),
        'content'     => "<!-- wp:paragraph --><p>" . __('Hello world!', 'petiva') . "</p><!-- /wp:paragraph -->",
    ));
}
add_action('init', 'petiva_register_block_patterns');

function petiva_register_block_styles()
{
    register_block_style('core/quote', array(
        'name'  => 'fancy-quote',
        'label' => __('Fancy Quote', 'petiva'),
    ));
}
add_action('init', 'petiva_register_block_styles');