<?php
/**
 * Plugin Name:       Publiton – Auto Publication Updater
 * Description:       Auto-updating academic publication lists, powered by the OpenAlex API. Enter an ORCID iD once and your publication list keeps itself current.
 * Version:           1.2.0
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Author:            Rens Brankaert
 * Author URI:        https://www.rensbrankaert.nl
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       publiton-auto-publication-updater
 */

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

if ( function_exists( 'publiton_fs' ) ) {
	publiton_fs()->set_basename( true, __FILE__ );
	return;
}

if ( ! function_exists( 'publiton_fs' ) ) {
	/**
	 * Helper for easy access to the Freemius SDK instance.
	 */
	function publiton_fs() {
		global $publiton_fs;

		if ( ! isset( $publiton_fs ) ) {
			require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

			$publiton_fs = fs_dynamic_init(
				array(
					'id'                  => '34382',
					'slug'                => 'publiton-auto-publication-updater',
					'premium_slug'        => 'publiton-auto-publication-updater-premium',
					'type'                => 'plugin',
					'public_key'          => 'pk_38048a093853af33f838c502ae79a',
					'is_premium'          => false,
					'premium_suffix'      => 'Pro',
					'has_premium_version' => true,
					'has_addons'          => false,
					'has_paid_plans'      => true,
					'is_org_compliant'    => true,
					'menu'                => array(
						'slug'    => 'publiton-auto-publication-updater',
						'parent'  => array( 'slug' => 'options-general.php' ),
						'support' => false,
					),
				)
			);
		}

		return $publiton_fs;
	}

	publiton_fs();
	do_action( 'publiton_fs_loaded' );
}

define( 'PUBLITON_VERSION', '1.2.0' );
define( 'PUBLITON_PLUGIN_FILE', __FILE__ );
define( 'PUBLITON_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PUBLITON_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'PUBLITON_CACHE_KEY', 'publiton_cache_html' );
define( 'PUBLITON_LAST_GOOD_OPTION', 'publiton_last_good' );

function publiton_default_settings() {
	return array(
		'author_id'        => '', // OpenAlex author id, e.g. A5082712655
		'author_name'      => '',
		'author_input'     => '', // what the user typed (ORCID, id, or name)
		'mailto'           => get_bloginfo( 'admin_email' ),
		'group_before'     => 0,  // years before this are collapsed into one section; 0 = off
		'open_recent'      => 2,  // how many recent year sections start expanded
		'highlight_author' => 1,
		'show_authors'     => 1,
		'show_venue'       => 1,
		'show_date'        => 0,
		'author_format'    => 'full', // full | initials
		'max_authors'      => 12, // 0 = no limit, otherwise cap with "et al."
		'excluded_types'   => array( 'dataset', 'peer-review', 'paratext', 'grant', 'erratum', 'letter' ),
		'excluded_titles'  => '',
		'year_overrides'   => '',
		'manual_entries'   => array(),
		// Pro features (only take effect when publiton_can_use_pro() is true).
		'extra_author_ids' => '', // additional OpenAlex author IDs, one per line
		'show_citations'   => 0,  // "Cited by N" badge per publication
		'show_stats'       => 0,  // summary: publications, citations, h-index
		'show_chart'       => 0,  // citations-per-year chart
		'enable_export'    => 0,  // BibTeX / CSV download links
	);
}

function publiton_get_settings() {
	return wp_parse_args( get_option( 'publiton_settings', array() ), publiton_default_settings() );
}

/**
 * Whether the current install may use the Pro (premium) features.
 *
 * True only in the premium build with an active license or trial. In the free
 * build shipped on WordPress.org this always returns false, so premium code
 * paths never execute for free users.
 */
function publiton_can_use_pro() {
	return function_exists( 'publiton_fs' ) && publiton_fs()->can_use_premium_code();
}

require PUBLITON_PLUGIN_DIR . 'includes/openalex.php';
require PUBLITON_PLUGIN_DIR . 'includes/pro.php';
require PUBLITON_PLUGIN_DIR . 'includes/render.php';
require PUBLITON_PLUGIN_DIR . 'includes/public.php';

if ( is_admin() ) {
	require PUBLITON_PLUGIN_DIR . 'includes/settings.php';
}

/**
 * Remove all plugin data on uninstall. Registered through the Freemius SDK
 * (which owns the uninstall lifecycle) instead of a standalone uninstall.php.
 */
function publiton_uninstall_cleanup() {
	delete_option( 'publiton_settings' );
	delete_option( 'publiton_last_good' );
	delete_transient( 'publiton_cache_html' );
	delete_transient( 'publiton_cache_html_pro' );
}
publiton_fs()->add_action( 'after_uninstall', 'publiton_uninstall_cleanup' );
