<?php

// phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
 * Plugin Name: GravityOps Search - Search and Display Gravity Forms Entries - Lite
 * Description: A shortcode to search and display Gravity Forms entries based on specified criteria and attributes.
 * Version: 1.2.1
 * Author: BrightLeaf Digital
 * Author URI: https://brightleafdigital.io/
 * Plugin URI: https://brightleafdigital.io/gravityops-search/
 * License: GPL-2.0+
 *
 */
// If this file is called directly, abort.
use GOSP\GravityOps\Core\SuiteCore\SuiteCore;
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
define( 'GRAVITYOPS_SEARCH_BASENAME', plugin_basename( __FILE__ ) );
define( 'GRAVITYOPS_SEARCH_VERSION', '1.2.1' );
if ( !function_exists( 'fs_dynamic_init' ) ) {
    require_once __DIR__ . '/vendor/freemius/wordpress-sdk/start.php';
    gravityops_search_mark_freemius_composer_file_loaded();
}
if ( function_exists( 'gosp_fs' ) ) {
    gosp_fs()->set_basename( false, __FILE__ );
} else {
    // DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE `function_exists` CALL ABOVE TO PROPERLY WORK.
    if ( !function_exists( 'gosp_fs' ) ) {
        /** Create a helper function for easy SDK access. */
        function gosp_fs() {
            global $gosp_fs;
            if ( !isset( $gosp_fs ) ) {
                // Include Freemius SDK.
                // SDK is auto-loaded through Composer
                $gosp_fs = fs_dynamic_init( [
                    'id'                             => '28776',
                    'slug'                           => 'gravityops-search',
                    'type'                           => 'plugin',
                    'public_key'                     => 'pk_ac19677b960bf594f4360e31be69e',
                    'is_premium'                     => false,
                    'has_addons'                     => false,
                    'has_paid_plans'                 => true,
                    'is_org_compliant'               => true,
                    'trial'                          => [
                        'days'               => 14,
                        'is_require_payment' => false,
                    ],
                    'menu'                           => [
                        'slug'        => 'gravityops-search',
                        'account'     => false,
                        'contact'     => false,
                        'support'     => false,
                        'affiliation' => false,
                        'pricing'     => false,
                    ],
                    'navigation'                     => 'tabs',
                    'bundle_license_auto_activation' => true,
                    'is_live'                        => true,
                ] );
            }
            return $gosp_fs;
        }

        // Init Freemius.
        gosp_fs();
        // Signal that SDK was initiated.
        do_action( 'gosp_fs_loaded' );
    }
    require_once __DIR__ . '/vendor/autoload.php';
    if ( file_exists( __DIR__ . '/vendor/GOSP/autoload.php' ) ) {
        require_once __DIR__ . '/vendor/GOSP/autoload.php';
    }
    // Register this plugin with SuiteCore early so the latest provider can be selected.
    add_action( 'plugins_loaded', function () {
        if ( file_exists( __DIR__ . '/vendor/GOSP/gravityops/core/assets/' ) ) {
            $assets_base_url = plugins_url( 'vendor/GOSP/gravityops/core/assets/', __FILE__ );
        } else {
            $assets_base_url = plugins_url( 'vendor/gravityops/core/assets/', __FILE__ );
        }
        SuiteCore::register( [
            'assets_base_url' => $assets_base_url,
        ] );
    }, 1 );
    add_action( 'admin_notices', function () {
        if ( is_admin() && current_user_can( 'activate_plugins' ) && !class_exists( 'GFForms' ) ) {
            echo '<div class="notice notice-warning"><p>GravityOps Search requires Gravity Forms. Please install and activate Gravity Forms to use this plugin.</p></div>';
        }
    } );
    add_action( 'gform_loaded', function () {
        if ( !method_exists( 'GFForms', 'include_addon_framework' ) ) {
            return;
        }
        GFForms::include_addon_framework();
        require_once 'includes/class-gravityops-search.php';
        GFAddOn::register( 'GravityOps_Search' );
    }, 5 );
}
/**
 * Marks the Freemius WordPress SDK composer file as loaded in the global composer autoload files array,
 * ensuring its initialization is recognized and avoided from being included multiple times.
 *
 * This method checks the Composer autoload files array for the presence of the Freemius WordPress SDK
 * start file and marks it as loaded if it matches to prevent duplicated inclusion.
 *
 * @return void
 */
function gravityops_search_mark_freemius_composer_file_loaded() : void {
    $autoload_files = __DIR__ . '/vendor/composer/autoload_files.php';
    if ( !file_exists( $autoload_files ) ) {
        return;
    }
    $freemius_start = str_replace( '\\', '/', __DIR__ . '/vendor/freemius/wordpress-sdk/start.php' );
    foreach ( require $autoload_files as $identifier => $file ) {
        if ( str_replace( '\\', '/', $file ) === $freemius_start ) {
            $GLOBALS['__composer_autoload_files'][$identifier] = true;
            return;
        }
    }
}
