<?php
/**
 * Plugin Name: fluXtore Funnel Builder
 * Description: Create beautiful checkout experiences and sell more with fluXtore! Now with Bricks 2.0 support.
 * Plugin URI: https://fluxtore.com/?utm_source=wp-plugins&utm_campaign=plugin-uri&utm_medium=wp-dash
 * Author: AmazeWP
 * Version: 1.6.5
 * Author URI: https://fluxtore.com/?utm_source=wp-plugins&utm_campaign=author-uri&utm_medium=wp-dash
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: fluxtore
 *
 * @package fluXtore
 * @category Core
 *
 */

defined( 'ABSPATH' ) || exit;

define( 'FLUXTORE_VERSION', '1.6.5' );
define( 'FLUXTORE_PLUGIN_FILE', __FILE__ );
define( 'FLUXTORE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'FLUXTORE_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
define( 'FLUXTORE_ASSETS_URL', FLUXTORE_PLUGIN_URL . '/assets' );
define( 'FLUXTORE_PLUGIN_PATH', FLUXTORE_PLUGIN_DIR );
define( 'FLUXTORE_DEFAULT_EDITIOR', get_option( '__fluxtore_default_editior' ) );

if ( ! function_exists( 'is_plugin_active' ) ) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$upsell_downsell_blocks_path = FLUXTORE_PLUGIN_DIR . 'core/gutenberg/upsell-downsell-blocks.php';

if ( file_exists( $upsell_downsell_blocks_path ) ) {
        require_once $upsell_downsell_blocks_path;
}

require_once FLUXTORE_PLUGIN_DIR . 'core/admin/class-fluxtore-update-notice.php';

function fluxtore_activated() {
        update_option( 'FLUXTORE_INSTALLATION', 1 );

        if ( class_exists( '\\fluXtore\\Core\\Admin\\fluXtore_Update_Notice' ) ) {
                \fluXtore\Core\Admin\fluXtore_Update_Notice::activate();
        }
}

function fluxtore_deactivated() {
        global $wpdb;

        if ( get_option( '__fluxtore__flows_persistence' ) ) {
                $flows = get_posts(
                        [
                                'post_type'   => 'fluxtore_flow',
                                'numberposts' => -1,
                                'post_status' => [ 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ],
                        ]
                );

                foreach ( $flows as $flow ) {
                        wp_delete_post( $flow->ID, true );
                }

                $steps = get_posts(
                        [
                                'post_type'   => 'fluxtore_step',
                                'numberposts' => -1,
                                'post_status' => [ 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ],
                        ]
                );

                foreach ( $steps as $step ) {
                        wp_delete_post( $step->ID, true );
                }

                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key LIKE '__fluxtore__%'" ) );
        }

        if ( get_option( '__fluxtore__options_persistence' ) ) {
                $options = $wpdb->get_results( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE '__fluxtore__%'" );

                foreach ( $options as $option ) {
                        delete_option( $option->option_name );
                }
        }

        delete_option( 'FLUXTORE_INSTALLATION' );
}

register_activation_hook( __FILE__, 'fluxtore_activated' );
register_deactivation_hook( __FILE__, 'fluxtore_deactivated' );

require_once FLUXTORE_PLUGIN_DIR . 'vendor/autoload.php';
require_once FLUXTORE_PLUGIN_DIR . 'core/class-fluxtore-loader.php';

/**
 * Register Bricks elements - compatible with Bricks 1.x and 2.0.
 */

// Register custom element category for Bricks 2.0+
add_filter( 'bricks/elements/categories', function ( $categories ) {
        $categories['fluxtore'] = [
                'title' => esc_html__( 'fluXtore', 'fluxtore' ),
                'icon'  => 'ti-shopping-cart',
        ];
        return $categories;
} );

// Bricks 2.0+ registration (via WordPress init action at priority 11)
add_action( 'init', function () {
        // Only register if Bricks Elements class exists
        if ( ! class_exists( '\\Bricks\\Elements' ) ) {
                return;
        }

        $element_files = [
                FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-checkout-block.php',
                FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-yes-block.php',
                FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-no-block.php',
                FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-order-details-block.php',
                FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-next-step-block.php',
        ];

        foreach ( $element_files as $file ) {
                if ( file_exists( $file ) ) {
                        \Bricks\Elements::register_element( $file );
                }
        }
}, 11 );

// Bricks 1.x registration (via filter)
add_filter( 'bricks/elements/register', function ( $elements ) {
        $element_files = [
                'fluxtore-checkout-block'       => FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-checkout-block.php',
                'fluxtore-yes-block'            => FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-yes-block.php',
                'fluxtore-no-block'             => FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-no-block.php',
                'fluxtore-order-details-block'  => FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-order-details-block.php',
                'fluxtore-next-step-block'      => FLUXTORE_PLUGIN_PATH . 'core/bricks/element-bricks-next-step-block.php',
        ];

        foreach ( $element_files as $element_name => $file ) {
                if ( file_exists( $file ) ) {
                        $elements[ $element_name ] = $file;
                }
        }

        return $elements;
} );
