<?php

/**
 * Theme functions and definitions.
 *
 * @author       DeoThemes
 * @copyright  (c) Copyright by DeoThemes
 * @link       https://deothemes.com
 * @package      Ona
 * @since        1.0
 */
if ( !defined( 'ABSPATH' ) ) {
    exit( 'Direct script access denied.' );
}
// Constants.
define( 'ONA_VERSION', '1.29' );
define( 'ONA_DIR', get_template_directory() );
define( 'ONA_URI', get_template_directory_uri() );
if ( !function_exists( 'ona_fs' ) ) {
    /**
     * Create a helper function for easy SDK access.
     */
    function ona_fs() {
        global $ona_fs;
        if ( !isset( $ona_fs ) ) {
            // Include Freemius SDK.
            require_once __DIR__ . '/vendor/autoload.php';
            $ona_fs = fs_dynamic_init( array(
                'id'               => '9271',
                'slug'             => 'ona',
                'premium_slug'     => 'ona-pro',
                'type'             => 'theme',
                'public_key'       => 'pk_561bffcf4255bd719dad9701963a8',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'has_affiliation'  => 'selected',
                'menu'             => array(
                    'slug'    => 'ona-theme',
                    'contact' => false,
                    'support' => false,
                    'parent'  => array(
                        'slug' => 'themes.php',
                    ),
                ),
                'is_live'          => true,
                'is_org_compliant' => true,
            ) );
        }
        return $ona_fs;
    }

    // Init Freemius after translations are ready.
    add_action( 'after_setup_theme', 'ona_fs' );
    add_action( 'after_setup_theme', function () {
        do_action( 'ona_fs_loaded' );
    } );
}
// Set the content width based on the theme's design and stylesheet.
if ( !isset( $content_width ) ) {
    $content_width = 1170;
    /* pixels */
}
// Includes.
require_once ONA_DIR . '/inc/admin/theme-admin.php';
require_once ONA_DIR . '/inc/patterns/block-patterns.php';
require_once ONA_DIR . '/inc/block-styles.php';
require_once ONA_DIR . '/inc/class-ona-theme-update.php';
/**
* TGMPA plugins activation.
*/
require_once ONA_DIR . '/inc/tgmpa/tgm-plugin-activation.php';
add_action( 'after_setup_theme', function () {
    return;
    /**
     * Theme Wizard.
     */
    require_once get_parent_theme_file_path( '/inc/merlin/vendor/autoload.php' );
    require_once get_parent_theme_file_path( '/inc/merlin/class-merlin.php' );
    require_once get_parent_theme_file_path( '/inc/merlin/merlin-config.php' );
    require_once get_parent_theme_file_path( '/inc/merlin/merlin-filters.php' );
    /**
     * Demo Import.
     */
    require_once ONA_DIR . '/inc/theme-demo-import.php';
    /**
     * WooCommerce Compatibility
     */
    if ( class_exists( 'WooCommerce' ) ) {
        require_once ONA_DIR . '/inc/woocommerce/woocommerce-theme-setup.php';
        require_once ONA_DIR . '/inc/woocommerce/woocommerce-theme-functions.php';
        require_once ONA_DIR . '/inc/woocommerce/woocommerce-theme-hooks.php';
    }
}, 20 );
/*
 * Theme Setup.
 */
if ( !function_exists( 'ona_setup' ) ) {
    /**
     * Sets up theme defaults and registers support for various WordPress features.
     */
    function ona_setup() {
        load_theme_textdomain( 'ona', get_template_directory() . '/languages' );
        add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 1170, 0 );
        add_theme_support( 'editor-styles' );
        add_theme_support( 'html5', array('comment-form', 'comment-list') );
        add_theme_support( 'responsive-embeds' );
        // Adding support for core block visual styles.
        add_theme_support( 'wp-block-styles' );
        // Add classic menu support.
        register_nav_menus( array(
            'primary-menu' => esc_html__( 'Primary Menu', 'ona' ),
            'footer-menu'  => esc_html__( 'Footer Menu', 'ona' ),
        ) );
    }

    add_action( 'after_setup_theme', 'ona_setup' );
}
/*
 * Enqueue Styles.
 */
if ( !function_exists( 'ona_styles' ) ) {
    /**
     * Enqueue front-end styles.
     */
    function ona_styles() {
        wp_register_style(
            'ona-style',
            ONA_URI . '/assets/css/style.min.css',
            array(),
            ONA_VERSION
        );
        wp_add_inline_style( 'ona-style', ona_get_font_face_styles() );
        $dependencies = apply_filters( 'ona_style_dependencies', array('ona-style') );
        wp_register_style(
            'ona-style-blocks',
            ONA_URI . '/assets/css/blocks.min.css',
            $dependencies,
            ONA_VERSION
        );
        wp_enqueue_style( 'ona-style' );
        wp_style_add_data( 'ona-style', 'rtl', 'replace' );
        wp_enqueue_style( 'ona-style-blocks' );
        wp_style_add_data( 'ona-style-blocks', 'rtl', 'replace' );
    }

    add_action( 'wp_enqueue_scripts', 'ona_styles' );
}
/*
 * Enqueue Editor Styles.
 */
if ( !function_exists( 'ona_editor_styles' ) ) {
    /**
     * Enqueue editor styles.
     */
    function ona_editor_styles() {
        // Add support for editor styles.
        add_theme_support( 'editor-styles' );
        add_editor_style( array('./assets/css/editor.min.css', './assets/css/blocks.min.css') );
        if ( defined( 'SBI_PLUGIN_DIR' ) ) {
            add_editor_style( array('../../plugins/instagram-feed/css/sbi-styles.min.css') );
        }
        wp_add_inline_style( 'wp-block-library', ona_get_font_face_styles() );
    }

    add_action( 'admin_init', 'ona_editor_styles' );
}
/*
 * Enqueue Admin Scripts and Styles.
 */
if ( !function_exists( 'ona_admin_scripts' ) ) {
    /**
     * Enqueue admin scripts and styles.
     */
    function ona_admin_scripts() {
        $screen = get_current_screen();
        $user_id = get_current_user_id();
        wp_enqueue_style(
            'ona-admin-styles',
            ONA_URI . '/assets/admin/css/admin-styles.css',
            array(),
            ONA_VERSION
        );
        if ( 'appearance_page_ona-theme' === $screen->id ) {
            wp_enqueue_script(
                'ona-admin-scripts',
                ONA_URI . '/assets/admin/js/admin-scripts.js',
                array('jquery-core'),
                ONA_VERSION,
                true
            );
            wp_enqueue_script(
                'ona-canny-scripts',
                ONA_URI . '/assets/admin/js/canny-scripts.js',
                array(),
                ONA_VERSION,
                true
            );
            wp_localize_script( 'ona-admin-scripts', 'ona_params', array(
                'ajaxurl'    => admin_url( 'admin-ajax.php' ),
                'wpnonce'    => wp_create_nonce( 'ona_ajax_nonce' ),
                'wizard_url' => esc_url( admin_url( 'themes.php?page=merlin' ) ),
                'processing' => esc_html__( 'Processing', 'ona' ),
                'finished'   => esc_html__( 'Finished', 'ona' ),
            ) );
            wp_localize_script( 'ona-canny-scripts', 'ona_canny_params', array(
                'user_email'  => sanitize_email( get_option( 'admin_email' ) ),
                'user_id'     => absint( $user_id ),
                'user_name'   => esc_html( wp_get_current_user()->user_login ),
                'user_avatar' => esc_url( get_avatar_url( $user_id ) ),
            ) );
        }
    }

    add_action( 'admin_enqueue_scripts', 'ona_admin_scripts' );
}
/*
 * Get Fonts.
 */
if ( !function_exists( 'ona_get_font_face_styles' ) ) {
    /**
     * Get font face styles.
     *
     * @return string
     */
    function ona_get_font_face_styles() {
        return "\n\n\t\t@font-face{\n\t\t\tfont-family: 'Bestermind';\n\t\t\tfont-weight: 400;\n\t\t\tfont-style: normal;\n\t\t\tfont-stretch: normal;\n\t\t\tfont-display: swap;\n\t\t\tsrc: url('" . get_theme_file_uri( 'assets/fonts/bestermind/BestermindRegular.woff' ) . "') format('woff');\n\t\t}\n\n\t\t";
    }

}
/*
 * Get Mailchimp Forms.
 */
if ( !function_exists( 'ona_get_mailchimp_forms' ) ) {
    /**
     * Get Mailchimp Forms.
     *
     * @param string $style Newsletter form style variant.
     * @return string
     */
    function ona_get_mailchimp_forms(  $style = 'style-1'  ) {
        $form = '';
        if ( function_exists( 'mc4wp' ) ) {
            $forms = mc4wp_get_forms();
            $form_id = ( isset( $forms[0] ) ? $forms[0]->ID : 0 );
            switch ( $style ) {
                case 'style-1':
                    $form = '<!-- wp:group {"className":"ona-newsletter"} -->
					<div class="wp-block-group ona-newsletter"><!-- wp:mailchimp-for-wp/form {"id":' . absint( $form_id ) . '} /--></div>
					<!-- /wp:group -->';
                    break;
                case 'style-1-light':
                    $form = '<!-- wp:group {"style":{"spacing":{"blockGap":"0px"},"elements":{"link":{"color":{"text":"var:preset|color|background"}}}},"textColor":"background","className":"ona-newsletter"} -->
					<div id="newsletter" class="wp-block-group ona-newsletter has-background-color has-text-color has-link-color">
					<!-- wp:mailchimp-for-wp/form {"id":' . absint( $form_id ) . ',"className":"is-style-ona-newsletter"} /--></div>
					<!-- /wp:group -->';
                    break;
                case 'style-2':
                    $form = '<!-- wp:group {"style":{"spacing":{"blockGap":"0px"}},"className":"ona-newsletter-style-2"} -->			
					<div id="newsletter" class="wp-block-group ona-newsletter-style-2">
					<!-- wp:mailchimp-for-wp/form {"id":' . absint( $form_id ) . ',"className":"is-style-ona-newsletter-style-2"} /-->
					</div><!-- /wp:group -->';
                    break;
                case 'style-3':
                    $form = '<!-- wp:group {"style":{"spacing":{"blockGap":"0px"},"elements":{"link":{"color":{"text":"var:preset|color|background"}}}},"textColor":"background","className":"ona-newsletter-style-3"} -->
					<div class="wp-block-group ona-newsletter-style-3 has-background-color has-text-color has-link-color">
					<!-- wp:mailchimp-for-wp/form {"id":' . absint( $form_id ) . ',"className":"is-style-ona-newsletter-style-3"} /-->
					</div><!-- /wp:group -->';
                    break;
                default:
                    $form = '<!-- wp:group {"className":"ona-newsletter"} -->
					<div class="wp-block-group ona-newsletter"><!-- wp:mailchimp-for-wp/form {"id":' . absint( $form_id ) . '} /--></div>
					<!-- /wp:group -->';
                    break;
            }
        }
        return $form;
    }

}
/*
 * Get Contact Form 7 Forms.
 */
if ( !function_exists( 'ona_get_contact_form_7_forms' ) ) {
    /**
     * Get Contact Form 7 Forms.
     *
     * @return string
     */
    function ona_get_contact_form_7_forms() {
        if ( !class_exists( '\\WPCF7' ) ) {
            return;
        }
        $form = '';
        $wpcf7_posts = get_posts( array(
            'post_type' => 'wpcf7_contact_form',
            'showposts' => 999,
        ) );
        $forms = array();
        if ( !empty( $wpcf7_posts ) && !is_wp_error( $wpcf7_posts ) ) {
            foreach ( $wpcf7_posts as $post ) {
                $forms[$post->ID] = $post->post_title;
            }
        }
        if ( !empty( $forms ) ) {
            $form_id = key( $forms ) ?? 0;
            $form = '<!-- wp:contact-form-7/contact-form-selector {"id":' . absint( $form_id ) . ', "title":"' . $forms[$form_id] . '"} -->
			<div class="wp-block-contact-form-7-contact-form-selector">[contact-form-7 id="' . absint( $form_id ) . '" title="' . $forms[$form_id] . '"]</div>
			<!-- /wp:contact-form-7/contact-form-selector -->';
        }
        return $form;
    }

}
/*
 * Get page ID by title.
 */
/**
 * Get a page by its title.
 *
 * @param string $page_title The page title to search for.
 * @param string $post_type  The post type to search.
 * @return WP_Post|null
 */
function ona_get_page_by_title(  $page_title, $post_type = 'page'  ) {
    $posts = get_posts( array(
        'post_type'              => $post_type,
        'title'                  => $page_title,
        'post_status'            => 'all',
        'numberposts'            => 1,
        'update_post_term_cache' => false,
        'update_post_meta_cache' => false,
        'orderby'                => 'post_date ID',
        'order'                  => 'ASC',
    ) );
    if ( !empty( $posts ) ) {
        $post = $posts[0];
    } else {
        $post = null;
    }
    return $post;
}

add_action( 'after_setup_theme', function () {
    return;
    /**
     * Migrate editor template changes from Ona Free over to Ona Pro
     */
    function ona_migrate_editor_changes() {
        if ( 1 !== get_option( 'ona_pro_migration_compete' ) ) {
            // 1. Update wp_terms ona with ona-pro
            $ona_term = get_term_by( 'slug', 'ona', 'wp_theme' );
            // get_term_by() returns false when the term does not exist (fresh installs).
            if ( $ona_term instanceof WP_Term ) {
                $update = wp_update_term( $ona_term->term_id, 'wp_theme', array(
                    'name' => 'ona-pro',
                    'slug' => 'ona-pro',
                ) );
            }
            // 2. Update post content with the new slug
            $posts = get_posts( array(
                'post_type' => 'wp_template',
            ) );
            if ( $posts ) {
                foreach ( $posts as $post ) {
                    $content = $post->post_content;
                    if ( strpos( $content, '"theme":"ona"' ) !== false ) {
                        $new_content = str_replace( '"theme":"ona"', '"theme":"ona-pro"', $content );
                        wp_update_post( array(
                            'ID'           => $post->ID,
                            'post_content' => $new_content,
                        ) );
                    }
                }
            }
            update_option( 'ona_pro_migration_compete', 1 );
        }
    }

    add_action( 'after_switch_theme', 'ona_migrate_editor_changes' );
}, 20 );
/*
 * Remove wrapping paragraphs for CF7.
 */
if ( class_exists( '\\WPCF7' ) ) {
    add_filter( 'wpcf7_autop_or_not', '__return_false' );
}