<?php

/**
 * Theme functions and definitions.
 *
 * @author       Aleksandr Samokhin
 * @copyright  (c) Copyright by Aleksandr Samokhin
 * @link       https://aleksandrsamokhin.com
 * @package      Strakture
 * @since        1.0
 */
if ( !defined( 'ABSPATH' ) ) {
    exit( 'Direct script access denied.' );
}
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
    require_once __DIR__ . '/vendor/autoload.php';
}
if ( !function_exists( 'strakture_theme_fs' ) ) {
    // Create a helper function for easy SDK access.
    function strakture_theme_fs() {
        global $strakture_theme_fs;
        if ( !isset( $strakture_theme_fs ) ) {
            // Include Freemius SDK.
            // SDK is auto-loaded through Composer
            $strakture_theme_fs = fs_dynamic_init( array(
                'id'                             => '24822',
                'slug'                           => 'strakture',
                'premium_slug'                   => 'strakture-pro',
                'type'                           => 'theme',
                'public_key'                     => 'pk_ef532478d69589e2404d87195d1ac',
                'is_premium'                     => false,
                'premium_suffix'                 => 'Pro',
                'bundle_id'                      => '25301',
                'bundle_public_key'              => 'pk_e61c37718ee953cb5f66bac0a855b',
                'bundle_license_auto_activation' => true,
                'has_addons'                     => false,
                'has_paid_plans'                 => true,
                'is_org_compliant'               => true,
                'menu'                           => array(
                    'slug'    => 'strakture-theme',
                    'support' => false,
                    'parent'  => array(
                        'slug' => 'themes.php',
                    ),
                ),
                'is_live'                        => true,
            ) );
        }
        return $strakture_theme_fs;
    }

    // Init Freemius.
    strakture_theme_fs();
    // Signal that SDK was initiated.
    do_action( 'strakture_theme_fs_loaded' );
}
// Constants
define( 'STRAKTURE_VERSION', '1.0.9' );
define( 'STRAKTURE_DIR', get_template_directory() );
define( 'STRAKTURE_URI', get_template_directory_uri() );
/**
 * Change theme icon
 *
 * @since 1.0.0
 */
function strakture_theme_fs_custom_icon() {
    return STRAKTURE_DIR . '/assets/admin/img/theme_thumb.png';
}

strakture_theme_fs()->add_filter( 'plugin_icon', 'strakture_theme_fs_custom_icon' );
// Set the content width based on the theme's design and stylesheet.
if ( !isset( $content_width ) ) {
    $content_width = 1200;
    /* pixels */
}
// Includes
require_once STRAKTURE_DIR . '/includes/admin/theme-admin.php';
require_once STRAKTURE_DIR . '/includes/block-styles.php';
require_once STRAKTURE_DIR . '/includes/patterns/block-patterns.php';
/**
* TGMPA plugins activation.
*/
require_once STRAKTURE_DIR . '/includes/tgmpa/tgm-plugin-activation.php';
/*
--------------------------------------------------------------
// Theme Setup
--------------------------------------------------------------*/
if ( !function_exists( 'strakture_setup' ) ) {
    function strakture_setup() {
        load_theme_textdomain( 'strakture', get_template_directory() . '/languages' );
        add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 1200, 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_action( 'after_setup_theme', 'strakture_setup' );
}
/*
--------------------------------------------------------------
// Enqueue Styles
--------------------------------------------------------------*/
if ( !function_exists( 'strakture_styles' ) ) {
    function strakture_styles() {
        wp_register_style(
            'strakture-style',
            STRAKTURE_URI . '/assets/css/style.css',
            array(),
            STRAKTURE_VERSION
        );
        $dependencies = apply_filters( 'strakture_style_dependencies', array('strakture-style') );
        wp_register_style(
            'strakture-style-blocks',
            STRAKTURE_URI . '/assets/css/blocks.css',
            $dependencies,
            STRAKTURE_VERSION
        );
        wp_enqueue_style( 'strakture-style' );
        wp_style_add_data( 'strakture-style', 'rtl', 'replace' );
        wp_enqueue_style( 'strakture-style-blocks' );
        wp_style_add_data( 'strakture-style-blocks', 'rtl', 'replace' );
        if ( class_exists( '\\WPCF7' ) ) {
            wp_enqueue_style(
                'strakture-contact-form-7',
                STRAKTURE_URI . '/assets/css/contact-form-7.css',
                array(),
                STRAKTURE_VERSION
            );
            wp_style_add_data( 'strakture-contact-form-7', 'rtl', 'replace' );
        }
        if ( function_exists( 'mc4wp' ) ) {
            wp_enqueue_style(
                'strakture-mc4wp',
                STRAKTURE_URI . '/assets/css/mc4wp.css',
                array(),
                STRAKTURE_VERSION
            );
            wp_style_add_data( 'strakture-mc4wp', 'rtl', 'replace' );
        }
    }

    add_action( 'wp_enqueue_scripts', 'strakture_styles' );
}
/*
--------------------------------------------------------------
// Enqueue Block Styles
--------------------------------------------------------------*/
function strakture_enqueue_block_styles() {
    wp_enqueue_block_style( 'core/navigation', array(
        'handle' => 'strakture-navigation-styles',
        'src'    => get_theme_file_uri( 'assets/css/navigation.css' ),
        'path'   => get_theme_file_path( 'assets/css/navigation.css' ),
    ) );
    wp_enqueue_block_style( 'core/comments', array(
        'handle' => 'strakture-comments-styles',
        'src'    => get_theme_file_uri( 'assets/css/comments.css' ),
        'path'   => get_theme_file_path( 'assets/css/comments.css' ),
    ) );
}

add_action( 'init', 'strakture_enqueue_block_styles' );
/*
--------------------------------------------------------------
// Enqueue Editor Styles
--------------------------------------------------------------*/
if ( !function_exists( 'strakture_editor_styles' ) ) {
    function strakture_editor_styles() {
        // Add support for editor styles.
        add_theme_support( 'editor-styles' );
        add_editor_style( array('./assets/css/editor.min.css', './assets/css/blocks.css') );
    }

    add_action( 'admin_init', 'strakture_editor_styles' );
}
/**
 * Default theme.json data filter.
 *
 * Remove duotone, gradients, and palette values.
 *
 * @param WP_Theme_JSON_Data $theme_json Default data from theme.json.
 *
 * @return WP_Theme_JSON_Data Modified data from theme.json.
 */
add_filter( 'wp_theme_json_data_default', function ( $theme_json ) {
    $data = $theme_json->get_data();
    $data['settings']['color']['duotone']['default'] = array();
    $data['settings']['color']['gradients']['default'] = array();
    $data['settings']['color']['palette']['default'] = array();
    return $theme_json->update_with( $data );
} );
/*
--------------------------------------------------------------
// Get page ID by title
--------------------------------------------------------------*/
if ( !function_exists( 'strakture_get_page_by_title' ) ) {
    function strakture_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;
    }

}
/*
--------------------------------------------------------------
// Remove wrapping paragraphs for CF7
--------------------------------------------------------------*/
if ( class_exists( '\\WPCF7' ) ) {
    add_filter( 'wpcf7_autop_or_not', '__return_false' );
}