<?php
/**
 * Options Page Builder - Elementor Dynamic Tag Integration
 *
 * Registers a dynamic tag to use WP PowerSuite options page fields in Elementor.
 *
 * @package OptionsPageBuilder
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Register PowerSuite Options dynamic tag group.
 */
function wpps_opb_register_elementor_group( $dynamic_tags_manager ) {
	$dynamic_tags_manager->register_group( 'wppowersuite-lite', [
		'title' => __( 'WP PowerSuite', 'wppowersuite-lite' ),
	] );
}

/**
 * Register PowerSuite Option dynamic tag.
 */
function wpps_opb_register_elementor_tag( $dynamic_tags_manager ) {
	require_once __DIR__ . '/elementor-tag.php';
	$dynamic_tags_manager->register( new \OptionsPageBuilder\Elementor_OPB_Option_Tag() );
}

/**
 * Register Elementor integrations.
 */
function wpps_opb_register_elementor_tags( $dynamic_tags_manager ) {
	wpps_opb_register_elementor_group( $dynamic_tags_manager );
	wpps_opb_register_elementor_tag( $dynamic_tags_manager );
}

function wpps_opb_init_elementor() {
	if ( did_action( 'elementor/loaded' ) ) {
		add_action( 'elementor/dynamic_tags/register', 'wpps_opb_register_elementor_tags' );
	} else {
		add_action( 'elementor/loaded', function() {
			add_action( 'elementor/dynamic_tags/register', 'wpps_opb_register_elementor_tags' );
		} );
	}
}

add_action( 'init', 'wpps_opb_init_elementor', 20 );
