<?php
/**
 * @package: scholar-sync
 */

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              https://scholar-sync.org
 * @since             1.0.0
 * @package           ScholarSync
 *
 * @wordpress-plugin
 * Plugin Name:       Scholar Sync
 * Plugin URI:        https://wordpress.org/plugins/scholar-sync
 * Description:       Sync and display OpenAlex publication feeds for researchers, labs, and faculties, keeping portfolios updated automatically.
 * Version:           1.0.6
 * Requires at least: 6.2
 * Tested up to:      7.1
 * Author:            ScholarSync Team
 * Author URI:        https://scholar-sync.org
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       scholar-sync
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}


/**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 * Rename this for your plugin and update it as you release new versions.
 */
define ('SCHOLAR_SYNC_VERSION', '1.0.6');
define('SCHOLAR_SYNC_PLUGIN_FILE', __FILE__);
define('SCHOLAR_SYNC_PLUGIN_DIR', untrailingslashit(plugin_dir_path(__FILE__)));
define('SCHOLAR_SYNC_PLUGIN_URL', plugin_dir_url(__FILE__));
define('SCHOLAR_SYNC_SITE_URL', get_site_url());
define ('SCHOLAR_SYNC_TIMEOUT', 30);
const SCHOLAR_SYNC_OPTION_PREFIX = 'scholar-sync-option-';
global $wpdb;
define('SCHOLAR_SYNC_DB_PREFIX', $wpdb->prefix . 'ssync_');
const SCHOLAR_SYNC_DB_ARTICLES      = SCHOLAR_SYNC_DB_PREFIX . 'articles';
const SCHOLAR_SYNC_DB_AUTHORS       = SCHOLAR_SYNC_DB_PREFIX . 'authors';
const SCHOLAR_SYNC_DB_GROUPS        = SCHOLAR_SYNC_DB_PREFIX . 'groups';
const SCHOLAR_SYNC_DB_GROUP_AUTHORS = SCHOLAR_SYNC_DB_PREFIX . 'group_authors';
const SCHOLAR_SYNC_DB_CO_AUTHORS    = SCHOLAR_SYNC_DB_PREFIX . 'co_authors';
const SCHOLAR_SYNC_DB_SYNC_RUNS     = SCHOLAR_SYNC_DB_PREFIX . 'sync_runs';
const SCHOLAR_SYNC_DB_SYNC_LOG      = SCHOLAR_SYNC_DB_PREFIX . 'sync_author_log';

const SCHOLAR_SYNC_SERVICE_URL = 'https://api.scholar-sync.org/sclr/v1/';

//
/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-scholar-sync-activator.php
 */
function scholar_sync_activate() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-scholar-sync-activator.php';
	$activator = new ScholarSync_Activator();
    $activator->activate();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-scholar-sync-deactivator.php
 */
function scholar_sync_deactivate() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-scholar-sync-deactivator.php';
	ScholarSync_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'scholar_sync_activate' );
register_deactivation_hook( __FILE__, 'scholar_sync_deactivate' );

add_filter('plugin_action_links', 'scholar_sync_plugin_action_links', 10, 2 );

function scholar_sync_plugin_action_links( $links, $file ) {
    if (strpos($file, 'scholar-sync') === false) {
        return $links;
    }
    $settings_link = '<a href="' . esc_url( menu_page_url( 'scholar-sync-authors-slug', false ) ) . '">Settings</a>';
    array_unshift( $links, $settings_link );
    return $links;
}

if ( ! function_exists( 'scholar_sync_fs' ) ) {
    function scholar_sync_fs() {
        global $scholar_sync_fs;
        if ( ! isset( $scholar_sync_fs ) ) {
            require_once SCHOLAR_SYNC_PLUGIN_DIR . '/vendor/freemius/start.php';
            $scholar_sync_fs = fs_dynamic_init( [
                'id'             => '30436',
                'slug'           => 'scholar-sync',
                'type'           => 'plugin',
                'public_key'     => 'pk_0e23a9b46968f8ec74f602ac414a4',
                'is_premium'     => false,
                'has_premium_version' => false,
                'has_paid_plans' => true,
                'has_addons'     => false,
                'menu'           => [
                    'slug'    => 'scholar-sync-settings-slug',
                    'support' => false,
                    'parent'  => [ 'slug' => 'scholar-sync-parent-slug' ],
                ],
            ] );
        }
        return $scholar_sync_fs;
    }
    scholar_sync_fs();
    do_action( 'scholar_sync_fs_loaded' );

    scholar_sync_fs()->add_filter( 'connect_message', function( $message, $first_name, $plugin_title, $user_login, $site_link, $freemius_link ) {
        return __( 'To automatically sync and display your publications, ScholarSync needs to connect to our secure cloud service. This connection enables the retrieval of scholar data and ensures your profile remains up-to-date without manual entry. <b>Note: This is a service connection only; we do not send email notifications.</b>', 'scholar-sync' );
    }, 10, 6 );

    scholar_sync_fs()->add_filter( 'connect_message_on_update', function( $message, $first_name, $plugin_title, $user_login, $site_link, $freemius_link ) {
        return __( 'To automatically sync and display your publications, ScholarSync needs to connect to our secure cloud service. This connection enables the retrieval of scholar data and ensures your profile remains up-to-date without manual entry. <b>Note: This is a service connection only; we do not send email notifications.</b>', 'scholar-sync' );
    }, 10, 6 );

    // Notify scholar-sync.org whenever the Freemius license state changes so the
    // server can immediately reflect the customer's current plan.
    $scholar_sync_plan_notify = function() {
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/includes/class-scholar-sync-activator.php';
        ScholarSync_Activator::notify_plan_update();
    };
    scholar_sync_fs()->add_action( 'after_license_change',       $scholar_sync_plan_notify );
    scholar_sync_fs()->add_action( 'after_license_activation',   $scholar_sync_plan_notify );
    scholar_sync_fs()->add_action( 'after_license_deactivation', $scholar_sync_plan_notify );

    scholar_sync_fs()->add_filter( 'after_connect_url', function() {
        return admin_url( 'admin.php?page=scholar-sync-add-author-slug' );
    } );
    scholar_sync_fs()->add_filter( 'after_skip_url', function() {
        return admin_url( 'admin.php?page=scholar-sync-add-author-slug' );
    } );
}

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . 'includes/class-scholar-sync.php';

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function scholar_sync_run() {
    $plugin = new ScholarSync();
	$plugin->run();
}

scholar_sync_run();


