<?php
/**
 * DACdb Plus
 *
 * @category  Plugin
 * @package   DACdbPlus
 * @author    Member Minder Pro, LLC <wordpress@memberminderpro.com>
 * @copyright 2024 Member Minder Pro, LLC
 * @license   Apache License 2.0
 * @link      https://memberminderpro.com
 *
 * @wordpress-plugin
 * Plugin Name:       DACdb Plus
 * Plugin URI:        https://dacdb.com/wordpress
 * GitHub Plugin URI: memberminderpro/dacdbplus
 * Description:       Easily publish your organization's data to WordPress.
 * Version:           3.2.4
 * Author:            Member Minder Pro, LLC
 * Author URI:        https://memberminderpro.com
 * Text Domain:       mmpdb
 * License:           Apache License 2.0
 * License URI:       https://www.apache.org/licenses/LICENSE-2.0
 */

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

$current_plugin = plugin_basename(__FILE__);
$all_plugins = get_plugins();

foreach ($all_plugins as $plugin_path => $plugin_data) {
    $plugin_dir = dirname($plugin_path);
    
    // Check for any directory that:
    // 1. Starts with 'dacdbplus-' (like 'dacdbplus-3.1.9a-revised')
    // 2. Starts with 'dacdbplus_' or 'dacdbplus.'
    // 3. Is 'dacdbplus' but not our current instance
    if (($plugin_dir !== 'dacdbplus' && strpos($plugin_dir, 'dacdbplus') === 0) || 
        ($plugin_dir === 'dacdbplus' && $plugin_path !== $current_plugin)) {
        deactivate_plugins($plugin_path);
        // Add a friendly, reassuring notice
        add_action('admin_notices', function() {
            wp_enqueue_script('common'); // Ensures the dismissible functionality is loaded
            echo '<div class="notice notice-info is-dismissible">';
            echo '<p><strong>DACdb Plus: Important Update Notice</strong><br>';
            echo 'Your previous installation of DACdb Plus was using a non-standard directory name. ';
            echo 'The new version has been installed and activated, and your settings have been preserved. ';
            echo '<br>The previous installation has been deactivated and may now be safely deleted.</p>';
            echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>';
            echo '</div>';
        });
        // Exit early to prevent update checker initialization
        return;
    }
}

add_action('init', function() {
    load_plugin_textdomain('mmpdb', false, dirname(plugin_basename(__FILE__)) . '/languages');
});

// Define the version setting function first
function set_mmpdb_plugin_version() {
    if (!defined('MMPDB_PLUGIN_VERSION')) {
        $plugin_data = get_plugin_data(__FILE__);
        define('MMPDB_PLUGIN_VERSION', $plugin_data['Version']);
    }
}

// Then use it in the init action
add_action('init', function() {
    $plugin = plugin_basename(__FILE__);
    if (!defined('MMPDB_PLUGIN_VERSION')) {
        set_mmpdb_plugin_version();
    }
});

if (!defined('MMPDB_PLUGIN_URL')) {
    define('MMPDB_PLUGIN_URL', plugin_dir_url(__FILE__));
}
if (!defined('MMPDB_PLUGIN_DIR')) {
    define('MMPDB_PLUGIN_DIR', plugin_dir_path(__FILE__));
}

$fDebug = false;
$data_path = '';
$data_file = '';
$public_path = '';
$catid = '';

//SECTION - Required Files and Libraries

require_once plugin_dir_path(__FILE__) . 'plugin-update-checker/plugin-update-checker.php';

use YahnisElsts\PluginUpdateChecker\v5\PucFactory;

$myUpdateChecker = PucFactory::buildUpdateChecker(
    'https://wordpress.dacdb.com/plugins/dacdbplus/dacdbplus.json',
    __FILE__,
    'dacdbplus'
);

require_once MMPDB_PLUGIN_DIR . 'includes/utilities.php';
global $options;
$options = get_mmp_options();
require_once MMPDB_PLUGIN_DIR . 'includes/enqueue.php';
require_once MMPDB_PLUGIN_DIR . 'includes/admin.php';
$plugin = plugin_basename(__FILE__);

//SECTION - JavaScript Functions
if (!function_exists('setDataPath')) {
    function setDataPath()
    {
        global $fDebug, $data_path, $file_path, $options;

        // default to DACdb
        $data_path = 'https://www.IsMyRotaryClub.org/wp_api_1-2/';
        $file_path = 'https://www.dacdb.com/Accounts/';
        $image_path = 'https://www.dacdb.com/images/';                // future, was caded but fa had the icon needed

        if (isset($options['dacdb_system']) && ($options['dacdb_system'] == 'iMembersDB')) {
            $data_path   = 'https://www.emembersdb.com/wp_api_1-2/';
            $file_path   = 'https://www.imembersdb.com/Accounts/';
            $public_path = 'https://www.emembersdb.com/';
            $acct_label  = 'Account';
        }
        if (isset($options['dacdb_system']) && ($options['dacdb_system'] == 'DACdb')) {
            $data_path      = 'https://www.IsMyRotaryClub.org/wp_api_1-2/';
            $file_path      = 'https://www.dacdb.com/Accounts/';
            $public_path = 'https://www.IsMyRotaryClub.com/';
            $acct_label  = 'District';
        }
        $data_path = trim($data_path);
        if (isset($_GET['debug'])) {
            $fDebug = True;
        }
        return;
    }
}
if (!function_exists('myscript')) {
    function myscript()
    {
        wp_add_inline_script('jquery', '
            (function($) {
                $("#pagination-dacdb").twbsPagination({
                    totalPages: mypagenumber,
                    visiblePages: "7",
                    hideOnlyOnePage: true,
                    onPageClick: function(event, page) {
                        $(".page-content").hide();
                        $("#pagsec" + page).show();
                    }
                });
            })(jQuery);
        ');
    }
}
//!SECTION - JavaScript Functions

require_once MMPDB_PLUGIN_DIR . 'includes/class-dacdb_plus.php';
require_once MMPDB_PLUGIN_DIR . 'includes/class-dacdb_plus_widget.php';

// Register activation/deactivation hooks
register_activation_hook(__FILE__, array('dacdb_plus', 'activate'));
register_deactivation_hook(__FILE__, array('dacdb_plus', 'deactivate'));

// Register the widget
function register_dacdb_plus_widget() {
    register_widget('dacdb_plus_widget');
}
add_action('widgets_init', 'register_dacdb_plus_widget');

// Initialize the main plugin class
function init_dacdb_plus() {
    // Create instance of main plugin class
    $dacdb_plus = new dacdb_plus();
}
add_action('plugins_loaded', 'init_dacdb_plus');

//SECTION - Display Functions
require_once MMPDB_PLUGIN_DIR . 'includes/display-club.php'; // dacdb_DisplayClub()
require_once MMPDB_PLUGIN_DIR . 'includes/display-d_events.php'; // dacdb_d_events()
require_once MMPDB_PLUGIN_DIR . 'includes/displayMembers.php';         // dacdb_displayMembers()
require_once MMPDB_PLUGIN_DIR . 'includes/modal-link.php';             // dacdb_modal_link()
require_once MMPDB_PLUGIN_DIR . 'includes/debug.php';
//!SECTION - Display Functions

//SECTION Process Shortcode Functions
require_once MMPDB_PLUGIN_DIR . 'includes/show-areas.php';             // dacdb_areas()
require_once MMPDB_PLUGIN_DIR . 'includes/show-birthdays.php';         // dacdb_birthdays()
require_once MMPDB_PLUGIN_DIR . 'includes/show-calendar.php';          // dacdb_calendar()
require_once MMPDB_PLUGIN_DIR . 'includes/show-club_meetings.php';     // dacdb_club_meetings()
require_once MMPDB_PLUGIN_DIR . 'includes/show-club_presidents.php';   // dacdb_club_presidents()
require_once MMPDB_PLUGIN_DIR . 'includes/show-clubleaders.php';       // dacdb_clubleaders()
require_once MMPDB_PLUGIN_DIR . 'includes/show-cog.php';               // dacdb_cog()
require_once MMPDB_PLUGIN_DIR . 'includes/show-committee_leaders.php'; // dacdb_committee_leaders()
require_once MMPDB_PLUGIN_DIR . 'includes/show-committee.php';         // dacdb_committee()
require_once MMPDB_PLUGIN_DIR . 'includes/show-committees.php';        // dacdb_committees()
require_once MMPDB_PLUGIN_DIR . 'includes/show-dcc.php';               // dacdb_dcc()
require_once MMPDB_PLUGIN_DIR . 'includes/show-district_leaders.php';  // dacdb_d_leaderss()
require_once MMPDB_PLUGIN_DIR . 'includes/show-documents.php';         // dacdb_documents()
require_once MMPDB_PLUGIN_DIR . 'includes/show-events.php';            // dacdb_events()
require_once MMPDB_PLUGIN_DIR . 'includes/show-leaders.php';           // dacdb_leaders()
require_once MMPDB_PLUGIN_DIR . 'includes/show-map.php';               // dacdb_map()
require_once MMPDB_PLUGIN_DIR . 'includes/show-meeting_info.php';      // dacdb_meeting_info()
require_once MMPDB_PLUGIN_DIR . 'includes/show-members.php';           // dacdb_members()
require_once MMPDB_PLUGIN_DIR . 'includes/show-newMemberFormList.php'; // dacdb_newMemberFormList()
require_once MMPDB_PLUGIN_DIR . 'includes/show-newsletter.php';        // dacdb_newsletter()
require_once MMPDB_PLUGIN_DIR . 'includes/show-riThemeLogo.php';       // dacdb_riThemeLogo()
require_once MMPDB_PLUGIN_DIR . 'includes/show-speakers.php';          // dacdb_speakers()
require_once MMPDB_PLUGIN_DIR . 'includes/show-sponsors.php';          // dacdb_sponsors()
require_once MMPDB_PLUGIN_DIR . 'includes/show-stories.php';           // dacdb_stories()
require_once MMPDB_PLUGIN_DIR . 'includes/show-wcm.php';               // dacdb_wcm()
//!SECTION Process Shortcode Functions
