<?php
/**
 * Plugin Name: Webien Search
 * Plugin URI: https://plugins.webien.io
 * Author: Webien
 * Description: Adds ajax search together with integration of Elementor & Relevanssi
 * Author URI: https://plugins.webien.io
 * Version: 0.2.1
 * License: GPL
 * Primary Branch: main
 */

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

define('WEBIEN_SEARCH_ADDON_PLUGIN_PATH', __DIR__);
define('WEBIEN_SEARCH_ADDON_PLUGIN_URI', plugin_dir_url(__FILE__));
//define('HAUS_SEARCH_ADDON_SETTINGS_PAGE_NAME', 'acf-options-widgets-locker');

if (file_exists(WEBIEN_SEARCH_ADDON_PLUGIN_PATH . '/vendor/autoload.php')) {
    require WEBIEN_SEARCH_ADDON_PLUGIN_PATH . '/vendor/autoload.php';
}


// Wait for theme setup to see if hooks has been added
add_action('after_setup_theme', function () {
    if(is_admin()){
        (new \Webien\SearchAddon\OptionPage())->register();
    }

    (new \Webien\SearchAddon\Cache())->register();
    $search_addon = new \Webien\SearchAddon\SearchAddon;
});


// Plugin updates
if(is_admin()){

    $version = '0.2.1';
    $slug = 'webien-search';
    $indexFile = $slug . '/index.php';
    $info = 'https://plugins.webien.io/plugins/webien-search/info.php?key=xxx';

    $updater = new \Webien\SearchAddon\PluginUpdater($info, $indexFile, $slug, $version);

}




add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style('webien-search-addon',  WEBIEN_SEARCH_ADDON_PLUGIN_URI . 'dist/style.css', [], false);
    wp_enqueue_script('webien-search-addon',  WEBIEN_SEARCH_ADDON_PLUGIN_URI . 'dist/scripts.js', ['jquery'], '', true);
    wp_localize_script( 'webien-search-addon', 'webien_search_ajax_object', [
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        'ajax_search_cache_api' =>  WEBIEN_SEARCH_ADDON_PLUGIN_URI.'src/CacheApi.php'
    ] );
});

add_action( 'admin_enqueue_scripts', function() {
    wp_enqueue_style( 'webien-search-addon-admin', WEBIEN_SEARCH_ADDON_PLUGIN_URI . 'dist/admin.css', false, '1.0.0' );
    wp_enqueue_script('webien-search-addon-admin-js',  WEBIEN_SEARCH_ADDON_PLUGIN_URI . 'assets/js/admin.js', ['jquery'], true);
    wp_localize_script( 'webien-search-addon', 'webien_search_ajax_object', [
        'ajax_url' => admin_url( 'admin-ajax.php' )
    ] );
});


// Add Widgets
add_action('init', function () {
    if (!defined('WP_INSTALLING') && did_action('elementor/loaded') && class_exists('acf')) {
        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\SearchAddon\SearchInput\Widget());

        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\SearchAddon\SearchResult\Widget());
        
        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\SearchAddon\PostType\Widget());

        \Elementor\Plugin::instance()
            ->elements_manager
            ->add_category('webien', ['title'=>'Webien']);
    }
});


function on_activation() {
    \Webien\SearchAddon\Cache::setup_resources();
}
register_activation_hook( __FILE__, 'on_activation' );
