<?php
/**
 * Geodir Pay Per Lead
 *
 * @package           Geodir_Pay_Per_Lead
 * @author            AyeCode Ltd
 * @copyright         2019 AyeCode Ltd
 * @license           GPLv3
 *
 * @wordpress-plugin
 * Plugin Name:       GeoDirectory Pay Per Lead
 * Plugin URI:        https://wpgeodirectory.com/downloads/pay-per-lead
 * Description:       Pay Per Lead add-on allows to extend your GeoDirectory with a versatile lead management system.
 * Version:           1.0.10-beta
 * Requires at least: 6.1
 * Requires PHP:      7.2
 * Author:            AyeCode Ltd
 * Author URI:        https://ayecode.io
 * Requires Plugins:  geodirectory, invoicing
 * Text Domain:       geodir-pay-per-lead
 * License:           GPLv3
 * License URI:       http://www.gnu.org/licenses/gpl-3.0.html
 * Domain Path:       /languages
 * Update URL:        https://wpgeodirectory.com
 * Update ID:         5130059
 */

namespace Geodir_PPL;

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

// Define plugin constants.
define( 'GEODIR_PPL_VERSION', '1.0.10-beta' );
define( 'GEODIR_PPL_MINIMUM_PHP_VERSION', '7.2' );
define( 'GEODIR_PPL_MINIMUM_WP_VERSION', '6.1' );
define( 'GEODIR_PPL_MINIMUM_GD_VERSION', '2.3' );
define( 'GEODIR_PPL_NAMESPACE', __NAMESPACE__ );
define( 'GEODIR_PPL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'GEODIR_PPL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/**
 * Autoload classes.
 */
require_once GEODIR_PPL_PLUGIN_DIR . 'includes/class-geodir-ppl-autoloader.php';
$autoloader = new Geodir_PPL_Autoloader( GEODIR_PPL_NAMESPACE, GEODIR_PPL_PLUGIN_DIR );
$autoloader->register();

Geodir_PPL_Environment::instance();


require_once GEODIR_PPL_PLUGIN_DIR . 'includes/functions.php';

/**
 * Display PHP version error.
 *
 * @return void
 */
function php_version_error() {
	$message = sprintf(
		/* translators: 1: Current PHP version 2: Required PHP version */
		esc_html__( 'Geodir Pay Per Lead requires PHP version %2$s or higher. You are running version %1$s. Please upgrade your PHP version.', 'geodir-pay-per-lead' ),
		PHP_VERSION,
		GEODIR_PPL_MINIMUM_PHP_VERSION
	);

	printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $message ) );
}

/**
 * Display WordPress version error.
 *
 * @return void
 */
function wordpress_version_error() {
	$message = sprintf(
		/* translators: 1: Current WordPress version 2: Required WordPress version */
		esc_html__( 'Geodir Pay Per Lead requires WordPress version %2$s or higher. You are running version %1$s. Please upgrade WordPress.', 'geodir-pay-per-lead' ),
		get_bloginfo( 'version' ),
		GEODIR_PPL_MINIMUM_WP_VERSION
	);

	printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $message ) );
}

/**
 * Display GeoDirectory version error.
 *
 * @return void
 */
function geodir_version_error() {
	$message = defined('GEODIRECTORY_VERSION') ? sprintf(
		/* translators: 1: Current GeoDirectory version 2: Required GeoDirectory version */
		esc_html__( 'Geodir Pay Per Lead requires GeoDirectory version %2$s or higher. You are running version %1$s. Please upgrade GeoDirectory.', 'geodir-pay-per-lead' ),
		GEODIR_PPL_MINIMUM_GD_VERSION,
		GEODIRECTORY_VERSION
	) : esc_html__( 'Geodir Pay Per Lead requires GeoDirectory to be installed.', 'geodir-pay-per-lead' );

	printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $message ) );
}

/**
 * Begins execution of the plugin.
 *
 * @since 1.0.0
 * @return void
 */
function initialize() {
	global $geodir_ppl_plugin;

	// Check PHP version.
	if ( version_compare( PHP_VERSION, GEODIR_PPL_MINIMUM_PHP_VERSION, '<' ) ) {
		add_action( 'admin_notices', GEODIR_PPL_NAMESPACE . '\php_version_error' );
		return;
	}

	// Check WordPress version.
	if ( version_compare( get_bloginfo( 'version' ), GEODIR_PPL_MINIMUM_WP_VERSION, '<' ) ) {
		add_action( 'admin_notices', GEODIR_PPL_NAMESPACE . '\wordpress_version_error' );
		return;
	}

	// Min core version check.
	if ( ! function_exists( "geodir_min_version_check" ) || ! geodir_min_version_check( "Geodir Pay Per Lead", GEODIR_PPL_MINIMUM_GD_VERSION ) ) {
		add_action( 'admin_notices', GEODIR_PPL_NAMESPACE . '\geodir_version_error' );
		return;
	}

	define( 'GEODIR_PPL_LOADED', true );

	// Initialize the plugin.
	$geodir_ppl_plugin = Geodir_PPL::instance();
}

add_action( 'geodirectory_loaded', GEODIR_PPL_NAMESPACE . '\initialize' );
