<?php
/**
 * Plugin Name: Easy Digital Downloads - Mad Mimi
 * Plugin URI: https://easydigitaldownloads.com/downloads/madmimi
 * Description: Include a MadMimi signup option with your Easy Digital Downloads checkout
 * Version: 1.0.2
 * Author: Easy Digital Downloads
 * Author URI: https://easydigitaldownloads.com
 */

if ( ! defined( 'EDDMM_VERSION' ) ) {
	define( 'EDDMM_VERSION', '1.0.2' );
}

/**
 * Enable licensing integration with EDD.
 *
 * @since 1.0.2
 * @return void
 */
function eddmm_licensing() {
	// When the minimum EDD version is 2.11.4, the elseif can be removed.
	if ( class_exists( '\\EDD\\Extensions\\ExtensionRegistry' ) ) {
		add_action( 'edd_extension_license_init', function( \EDD\Extensions\ExtensionRegistry $registry ) {
			$registry->addExtension(
				__FILE__,
				'Mad Mimi',
				3318,
				EDDMM_VERSION
			);
		} );
	} elseif ( class_exists( 'EDD_License' ) ) {
		$license = new EDD_License( __FILE__, 'Mad Mimi', EDDMM_VERSION, 'Easy Digital Downloads', null, null, 3318 );
	}
}
add_action( 'plugins_loaded', 'eddmm_licensing', 99 );

/**
 * Beginning in EDD 2.11.4, email marketing settings are added to the Marketing tab.
 *
 * @since 1.0.2
 * @return void
 */
add_action( 'init', function() {
	if ( ! defined( 'EDD_VERSION' ) ) {
		return;
	}
	$settings_tab = version_compare( EDD_VERSION, '2.11.4', '>=' ) && array_key_exists( 'marketing', edd_get_settings_tabs() ) ? 'marketing' : 'extensions';
	add_filter( "edd_settings_{$settings_tab}", 'eddmm_add_settings' );
	add_filter( "edd_settings_sections_{$settings_tab}", 'eddmm_add_section' );
} );

/**
 * Adds the settings to the Extensions section.
 *
 * @since 1.0
 *
 * @param array $settings
 *
 * @return array
 * @throws Exception
 */
function eddmm_add_settings( $settings ) {

	$eddmm_settings = array(
		array(
			'id'   => 'eddmm_user',
			'name' => __( 'Mad Mimi Email Address', 'eddmm' ),
			'desc' => __( 'Enter the email address associated with your Mad Mimi account', 'eddmm' ),
			'type' => 'text',
			'size' => 'regular'
		),
		array(
			'id'   => 'eddmm_api',
			'name' => __( 'Mad Mimi API Key', 'eddmm' ),
			'desc' => __( 'Enter your Mad Mimi API key', 'eddmm' ),
			'type' => 'text',
			'size' => 'regular'
		),
		array(
			'id'      => 'eddmm_list',
			'name'    => __( 'Choose a list', 'eddmm' ),
			'desc'    => __( 'Select the list you wish to subscribe buyers to', 'eddmm' ),
			'type'    => 'select',
			'options' => eddmm_get_madmimi_lists()
		),
		array(
			'id'   => 'eddmm_label',
			'name' => __( 'Checkout Label', 'eddmm' ),
			'desc' => __( 'This is the text shown next to the signup option', 'eddmm' ),
			'type' => 'text',
			'size' => 'regular'
		)
	);

	return array_merge( $settings, array( 'madmimi' => $eddmm_settings ) );
}

/**
 * Registers a section for Mad Mimi.
 *
 * @since 1.0.2
 * @param array $sections
 * @return array
 */
function eddmm_add_section( $sections ) {
	$sections['madmimi'] = __( 'Mad Mimi', 'eddmm' );

	return $sections;
}

/**
 * Get an array of all Mad Mimi subscription lists.
 *
 * @since 1.0
 *
 * @return array|false
 * @throws Exception
 */
function eddmm_get_madmimi_lists() {

	global $edd_options;

	if ( ! isset( $_GET['page'] ) || $_GET['page'] != 'edd-settings' ) {
		return array();
	}

	if ( isset( $edd_options['eddmm_api'] ) && strlen( trim( $edd_options['eddmm_api'] ) ) > 0 ) {

		if ( ! isset( $edd_options['eddmm_user'] ) || strlen( trim( $edd_options['eddmm_user'] ) ) <= 0 ) {
			return false;
		}

		require_once dirname( __FILE__ ) . '/MadMimi.class.php';

		$mailer = new MadMimi( $edd_options['eddmm_user'], $edd_options['eddmm_api'] );

		$lists = $mailer->Lists();

		$is_xml = simplexml_load_string( $lists );

		// make sure it is valid xml
		if ( $is_xml === false ) {
			return array();
		}

		$lists_array = new SimpleXMLElement( $lists );
		$arr         = array();

		foreach ( $lists_array as $list ) {
			$arr[ (string) $list['name'] ] = $list['name'];
		}

		return $arr;

	}

	return array();
}

/**
 * Adds an email to the Mad Mimi subscription list.
 *
 * @since 1.0
 *
 * @param string $email
 * @param string $first_name
 * @param string $last_name
 *
 * @return bool
 */
function eddmm_subscribe_email( $email, $first_name = '', $last_name = '' ) {
	global $edd_options;

	if ( isset( $edd_options['eddmm_api'] ) && strlen( trim( $edd_options['eddmm_api'] ) ) > 0 ) {

		if ( ! isset( $edd_options['eddmm_user'] ) || strlen( trim( $edd_options['eddmm_user'] ) ) <= 0 ) {
			return false;
		}

		require dirname( __FILE__ ) . '/MadMimi.class.php';

		$mailer = new MadMimi( $edd_options['eddmm_user'], $edd_options['eddmm_api'] );

		$user = array( 'email' => $email, 'firstName' => $first_name, 'lastName' => $last_name, 'add_list' => $edd_options['eddmm_list'] );

		if ( $mailer->AddUser( $user ) ) {
			return true;
		}
	}

	return false;
}

/**
 * Displays the Mad Mimi checkbox.
 *
 * @since 1.0
 */
function eddmm_madmimi_fields() {
	global $edd_options;
	ob_start();
	if ( isset( $edd_options['eddmm_api'] ) && strlen( trim( $edd_options['eddmm_api'] ) ) > 0 ) { ?>
		<p>
			<input name="eddmm_madmimi_signup" id="eddmm_madmimi_signup" type="checkbox" checked="checked"/>
			<label for="eddmm_madmimi_signup"><?php echo isset( $edd_options['eddmm_label'] ) ? $edd_options['eddmm_label'] : __( 'Sign up for our mailing list', 'eddmm' ); ?></label>
		</p>
		<?php
	}
	echo ob_get_clean();
}

add_action( 'edd_purchase_form_before_submit', 'eddmm_madmimi_fields', 100 );

/**
 * Checks whether a user should be signed up for the Mad Mimi list.
 *
 * @since 1.0
 *
 * @param array $posted
 * @param array $user_info
 */
function eddmm_check_for_email_signup( $posted, $user_info ) {
	if ( ! empty( $posted['eddmm_madmimi_signup'] ) ) {

		$email = $user_info['email'];
		eddmm_subscribe_email( $email, $user_info['first_name'], $user_info['last_name'] );
	}
}

add_action( 'edd_checkout_before_gateway', 'eddmm_check_for_email_signup', 10, 2 );
