<?php

/**
 * Plugin Name: AD Pro Price Lists
 * Plugin URI: https://altaydagistan.com/wordpress-price-lists/
 * Description: Create and display multiple service price lists with per-service inquiry forms. Use shortcode [comhit_pricelist id="123"].
 * Version: 1.7.7
 * Author: Altay Dagistan
 * Author URI: https://altaydagistan.com
 * Text Domain: ad-pro-price-list
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'appl_fs' ) ) {
    appl_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( 'appl_fs' ) ) {
        // Create a helper function for easy SDK access.
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
        function appl_fs() {
            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
            global $appl_fs;
            if ( !isset( $appl_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
                $appl_fs = fs_dynamic_init( array(
                    'id'               => '28227',
                    'slug'             => 'ad-pro-pricing-lists',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_c38af90515b5785bcee2e300e07c7',
                    'is_premium'       => false,
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'       => 'chcpl-account',
                        'first-path' => 'admin.php?page=chcpl-about',
                        'support'    => false,
                        'parent'     => array(
                            'slug' => 'edit.php?post_type=price_list',
                        ),
                    ),
                    'is_live'          => true,
                ) );
            }
            return $appl_fs;
        }

        // Init Freemius.
        appl_fs();
        // Signal that SDK was initiated.
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
        do_action( 'appl_fs_loaded' );
    }
}
// Constants
if ( !defined( 'CHCPL_VERSION' ) ) {
    $chcpl_plugin_data = get_file_data( __FILE__, array(
        'Version' => 'Version',
    ) );
    define( 'CHCPL_VERSION', $chcpl_plugin_data['Version'] );
    unset($chcpl_plugin_data);
}
if ( !defined( 'CHCPL_PLUGIN_FILE' ) ) {
    define( 'CHCPL_PLUGIN_FILE', __FILE__ );
}
if ( !defined( 'CHCPL_PLUGIN_DIR' ) ) {
    define( 'CHCPL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( !defined( 'CHCPL_PLUGIN_URL' ) ) {
    define( 'CHCPL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Activation/Deactivation
function chcpl_activate() {
    // Ensure CPTs are registered before flushing
    require_once CHCPL_PLUGIN_DIR . 'includes/post-types.php';
    require_once CHCPL_PLUGIN_DIR . 'includes/submissions.php';
    chcpl_register_post_types();
    chcpl_create_submissions_table();
    chcpl_create_quote_requests_table();
    // Clear table-existence transients so they re-check after activation
    delete_transient( 'chcpl_submissions_table_ok' );
    delete_transient( 'chcpl_quote_requests_table_ok' );
    flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'chcpl_activate' );
function chcpl_deactivate() {
    flush_rewrite_rules();
}

register_deactivation_hook( __FILE__, 'chcpl_deactivate' );
// Includes
require_once CHCPL_PLUGIN_DIR . 'includes/security.php';
require_once CHCPL_PLUGIN_DIR . 'includes/post-types.php';
require_once CHCPL_PLUGIN_DIR . 'includes/meta-boxes.php';
require_once CHCPL_PLUGIN_DIR . 'includes/shortcodes.php';
require_once CHCPL_PLUGIN_DIR . 'includes/ajax.php';
require_once CHCPL_PLUGIN_DIR . 'includes/admin.php';
require_once CHCPL_PLUGIN_DIR . 'includes/submissions.php';
require_once CHCPL_PLUGIN_DIR . 'includes/settings.php';
/* <fs_premium_only> */
require_once CHCPL_PLUGIN_DIR . 'includes/lock.php';
require_once CHCPL_PLUGIN_DIR . 'includes/import-export.php';
require_once CHCPL_PLUGIN_DIR . 'includes/white-label.php';
/* </fs_premium_only> */
// Init
add_action( 'init', 'chcpl_register_post_types' );
add_action( 'add_meta_boxes', 'chcpl_register_meta_boxes' );
add_action(
    'save_post',
    'chcpl_save_service_meta',
    10,
    2
);
add_action( 'init', 'chcpl_register_shortcodes' );
// Assets — register always, enqueue only on pages that use the shortcode
function chcpl_enqueue_assets() {
    wp_register_style(
        'chcpl-front',
        CHCPL_PLUGIN_URL . 'assets/css/front.css',
        array(),
        CHCPL_VERSION
    );
    wp_register_script(
        'chcpl-front',
        CHCPL_PLUGIN_URL . 'assets/js/front.js',
        array('jquery'),
        CHCPL_VERSION,
        true
    );
    wp_localize_script( 'chcpl-front', 'CHCPL_Ajax', array(
        'ajaxUrl' => admin_url( 'admin-ajax.php' ),
        'nonce'   => wp_create_nonce( 'chcpl_nonce' ),
    ) );
    // Enqueue immediately if this post contains the shortcode (covers singular pages)
    global $post;
    if ( $post && has_shortcode( $post->post_content, 'comhit_pricelist' ) ) {
        wp_enqueue_style( 'chcpl-front' );
        wp_enqueue_script( 'chcpl-front' );
    }
}

add_action( 'wp_enqueue_scripts', 'chcpl_enqueue_assets' );
function chcpl_output_inline_styles() {
    if ( is_admin() ) {
        return;
    }
    if ( !wp_style_is( 'chcpl-front', 'enqueued' ) ) {
        return;
    }
    if ( !function_exists( 'chcpl_get_settings' ) ) {
        return;
    }
    $opt = chcpl_get_settings();
    $lock_max_width_val = ( isset( $opt['lock_full_width'] ) && $opt['lock_full_width'] === '1' ? '100%' : (int) $opt['lock_max_width'] . 'px' );
    $css = ':root{' . '--chcpl-radius:' . (int) $opt['radius'] . 'px;' . '--chcpl-gap:' . (int) $opt['gap'] . 'px;' . '--chcpl-z:' . (int) $opt['z_index'] . ';' . '--chcpl-button-radius:' . (int) $opt['button_radius'] . 'px;' . '--chcpl-category-title-color:' . esc_html( $opt['category_title_color'] ) . ';' . '--chcpl-category-title-bg:' . esc_html( $opt['category_title_bg'] ) . ';' . '--chcpl-category-title-text-color:' . esc_html( $opt['category_title_text_color'] ) . ';' . '--chcpl-service-title-color:' . esc_html( $opt['service_title_color'] ) . ';' . '--chcpl-description-color:' . esc_html( $opt['description_color'] ) . ';' . '--chcpl-price-color:' . esc_html( $opt['price_color'] ) . ';' . '--chcpl-border-color:' . esc_html( $opt['border_color'] ) . ';' . '--chcpl-card-bg:' . esc_html( $opt['card_bg'] ) . ';' . '--chcpl-backdrop:' . esc_html( $opt['backdrop_rgba'] ) . ';' . '--chcpl-inquiry-btn-bg:' . esc_html( $opt['inquiry_btn_bg'] ) . ';' . '--chcpl-inquiry-btn-text:' . esc_html( $opt['inquiry_btn_text'] ) . ';' . '--chcpl-inquiry-btn-hover-bg:' . esc_html( $opt['inquiry_btn_hover_bg'] ) . ';' . '--chcpl-inquiry-btn-hover-text:' . esc_html( $opt['inquiry_btn_hover_text'] ) . ';' . '--chcpl-button-bg:' . esc_html( $opt['button_bg'] ) . ';' . '--chcpl-button-text:' . esc_html( $opt['button_text'] ) . ';' . '--chcpl-button-hover-bg:' . esc_html( $opt['button_hover_bg'] ) . ';' . '--chcpl-button-hover-text:' . esc_html( $opt['button_hover_text'] ) . ';' . '--chcpl-submit-bg:' . esc_html( $opt['submit_bg'] ) . ';' . '--chcpl-submit-hover-bg:' . esc_html( $opt['submit_hover_bg'] ) . ';' . '--chcpl-submit-text:' . esc_html( $opt['submit_text'] ) . ';' . '--chcpl-selected-service-bg:' . esc_html( $opt['selected_service_bg'] ) . ';' . '--chcpl-modal-bg:' . esc_html( $opt['modal_bg'] ) . ';' . '--chcpl-modal-max-width:' . (int) $opt['modal_max_width'] . 'px;' . '--chcpl-modal-border-color:' . esc_html( ( isset( $opt['modal_border_color'] ) ? $opt['modal_border_color'] : 'transparent' ) ) . ';' . '--chcpl-modal-title-color:' . esc_html( $opt['modal_title_color'] ) . ';' . '--chcpl-modal-close-color:' . esc_html( $opt['modal_close_color'] ) . ';' . '--chcpl-modal-label-color:' . esc_html( $opt['modal_label_color'] ) . ';' . '--chcpl-modal-input-bg:' . esc_html( $opt['modal_input_bg'] ) . ';' . '--chcpl-modal-input-border:' . esc_html( $opt['modal_input_border'] ) . ';' . '--chcpl-modal-input-text:' . esc_html( $opt['modal_input_text'] ) . ';' . '--chcpl-modal-service-text-color:' . esc_html( $opt['modal_service_text_color'] ) . ';' . '--chcpl-lock-max-width:' . esc_html( $lock_max_width_val ) . ';' . '--chcpl-lock-card-bg:' . esc_html( $opt['lock_card_bg'] ) . ';' . '--chcpl-lock-card-border-color:' . esc_html( ( isset( $opt['lock_card_border_color'] ) ? $opt['lock_card_border_color'] : 'transparent' ) ) . ';' . '--chcpl-lock-title-color:' . esc_html( $opt['lock_title_color'] ) . ';' . '--chcpl-lock-heading-color:' . esc_html( $opt['lock_heading_color'] ) . ';' . '--chcpl-lock-submit-bg:' . esc_html( $opt['lock_submit_bg'] ) . ';' . '--chcpl-lock-submit-hover-bg:' . esc_html( $opt['lock_submit_hover_bg'] ) . ';' . '--chcpl-lock-submit-text:' . esc_html( $opt['lock_submit_text'] ) . ';' . '--chcpl-lock-submit-hover-text:' . esc_html( $opt['lock_submit_hover_text'] ) . ';' . '--chcpl-access-request-bg:' . esc_html( $opt['access_request_bg'] ) . ';' . '--chcpl-access-message-color:' . esc_html( $opt['access_message_color'] ) . ';' . '--chcpl-access-btn-bg:' . esc_html( $opt['access_btn_bg'] ) . ';' . '--chcpl-access-btn-hover-bg:' . esc_html( $opt['access_btn_hover_bg'] ) . ';' . '--chcpl-access-btn-text:' . esc_html( $opt['access_btn_text'] ) . ';' . '--chcpl-access-btn-hover-text:' . esc_html( $opt['access_btn_hover_text'] ) . ';' . '--chcpl-access-subtext-color:' . esc_html( $opt['access_subtext_color'] ) . ';' . '--chcpl-access-subtext-font-weight:' . esc_html( $opt['access_subtext_font_weight'] ) . ';' . '}';
    wp_add_inline_style( 'chcpl-front', $css );
}

add_action( 'wp_enqueue_scripts', 'chcpl_output_inline_styles', 20 );
// AJAX endpoints
add_action( 'wp_ajax_chcpl_send_inquiry', 'chcpl_handle_inquiry' );
add_action( 'wp_ajax_nopriv_chcpl_send_inquiry', 'chcpl_handle_inquiry' );
/* <fs_premium_only> */
add_action( 'wp_ajax_chcpl_unlock_list', 'chcpl_handle_unlock' );
add_action( 'wp_ajax_nopriv_chcpl_unlock_list', 'chcpl_handle_unlock' );
add_action( 'wp_ajax_chcpl_request_access', 'chcpl_handle_request_access' );
add_action( 'wp_ajax_nopriv_chcpl_request_access', 'chcpl_handle_request_access' );
/* </fs_premium_only> */