<?php

/**
 * Get all public and common taxonomies for a post type or post types
 * Returns an array associated taxonomy object names and labels.
 *
 * @since 1.2
 * @param string|array $post_type
 * @return array
 */
function xmlsf_news_available_taxonomies( $post_type ) {
	global $wp_taxonomies;
	$tax_array = array();

	if ( is_array($post_type) ) {
		// backwards compatibility for when including more than one
		// post type in the same sitemap, then only allow common taxonomies
		foreach ( $post_type as $type ) {
			$tax_intersect = isset( $tax_intersect ) ? array_intersect( $tax_intersect, get_object_taxonomies( $type ) ) : get_object_taxonomies( $type );
		}
	} else {
		$tax_intersect = get_object_taxonomies( $post_type );
	}

	foreach ( $tax_intersect as $tax_name ) {
		// check each tax public flag and append name to array
		if ( is_object( $wp_taxonomies[$tax_name] ) && !empty( $wp_taxonomies[$tax_name]->public ) && !in_array( $tax_name, xmlsf()->disabled_taxonomies() ) ) {
			$tax_array[$tax_name] = $wp_taxonomies[$tax_name]->label;
		}
	}

	return $tax_array;
}

/***
 * POST TYPES
 */

function xmlsf_news_post_type_field_type( $type ) {
	return 'checkbox';
}

/***
 * Options sanitization
 */

function xmlsf_news_sanitize_advanced_settings( $new ) {
	$sanitized = is_array( $new ) ? $new : array();

	return $sanitized;
}

function xmlsf_news_sanitize_log_clear( $new ) {
	if ( $new ) {
		delete_option( 'xmlsf_news_log' );
	}
	return false; //do not create db option
}

function xmlsf_news_license_error_msg( $error, $expires = '' ) {
	$responses = array (
		'expired' => ! empty($expires) ?
			sprintf( /* Translators: Expiration date */ __( 'Your license key has expired on %s.', 'xml-sitemap-feed' ), date_i18n( get_option( 'date_format' ), strtotime( $expires, current_time( 'timestamp' ) ) ) ) :
			__( 'Your license key has expired.', 'xml-sitemap-feed' ),
		'disabled' => __( 'Your license key has been disabled.', 'xml-sitemap-feed' ),
		'revoked' => __( 'Your license key has been disabled.', 'xml-sitemap-feed' ),
		'missing' => __( 'This appears to be an invalid license key.', 'xml-sitemap-feed' ),
		'invalid' => __( 'This appears to be an invalid license key.', 'xml-sitemap-feed' ),
		'deactivated' => __( 'Your license is not active for this site.', 'xml-sitemap-feed' ),
		'inactive' => __( 'Your license is not active for this site.', 'xml-sitemap-feed' ),
		'site_inactive' => __( 'Your license is not active for this site.', 'xml-sitemap-feed' ),
		'invalid_item_id' => sprintf( /* Translators: Plugin name */ __( 'This appears to be an invalid license key for %s.', 'xml-sitemap-feed' ), __('Google News Advanced', 'xml-sitemap-feed') ),
		'item_name_mismatch' => sprintf( /* Translators: Plugin name */ __( 'This appears to be an invalid license key for %s.', 'xml-sitemap-feed' ), __('Google News Advanced', 'xml-sitemap-feed') ),
		'no_activations_left' => __( 'Your license key has reached its activation limit.', 'xml-sitemap-feed' ),
		'error' => __( 'An error occurred, please try again.', 'xml-sitemap-feed' )
	);

	return isset( $responses[$error] ) ? $responses[$error] : false;
}

function xmlsf_news_sanitize_license( $new ) {
	$new = (array) $new;
	
	// Prevent double calls to this callback, still happens with object caching.
	if ( ! isset( $new['action'] ) ) {
		return $new;
	}
	
	$sanitized['key'] = ! empty( $new['key'] ) ?  trim( $new['key'] ) : '';
	$sanitized['beta'] = ! empty( $new['beta'] );

	$old = (array) get_option( 'xmlsf_news_license', array() );

	if ( ! empty( $old['key'] ) && $old['key'] === $sanitized['key'] ) {
		$sanitized['status'] = ! empty( $old['status'] ) ? $old['status'] : '';
		$sanitized['expires'] = ! empty( $old['expires'] ) ? $old['expires'] : 0;
		$sanitized['lifetime'] = ! empty( $old['lifetime'] ) ? $old['lifetime'] : '';
		$sanitized['error'] = ! empty( $old['error'] ) ? $old['error'] : '';
		$sanitized['payment_id'] = ! empty( $old['payment_id'] ) ? $old['payment_id'] : '';
	}

	if ( ! empty( $sanitized['key'] ) ) {
		// Include license manager class.
		if ( ! class_exists( 'EDD_SL_License' ) ) {
			include XMLSF_NEWS_ADV_DIR . '/vendor/EDD_SL_License.php';
		}

		$action = isset( $new['action'] ) ? $new['action'] : '';
		
		// Instantiate.
		$license = new EDD_SL_License( $sanitized['key'], XMLSF_NEWS_ADV_STORE_URL, XMLSF_NEWS_ADV_ITEM_ID );

		switch( $action ) {
			case 'activate':
				// Activate license.
				$response = $license->activate();
				if ( true === $response ) {
					add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'Your license was successfully activated for this site.', 'xml-sitemap-feed' ), 'notice-success' );
				}
				break;
			
			case 'deactivate':
				// Deactivate license.
				$response = $license->deactivate();
				if ( true === $response ) {
					add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'Your license was successfully deactivated for this site.', 'xml-sitemap-feed' ), 'notice-info' );
				}
				break;
			
			default:
				// Check license.
				$response = $license->check();
				if ( true === $response ) {
					if ( 'valid' == $license->status ) {
						add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'Your license is active for this site.', 'xml-sitemap-feed' ), 'notice-success' );
					} elseif ( 'inactive' == $license->status ) {
						add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'Your license is not active for this site.', 'xml-sitemap-feed' ), 'notice-info' );
					} else {
						xmlsf_news_license_admin_error_msg( $response, $license );
					}
				}
		}

		// Update valid response data.
		if ( true === $response ) {
			$sanitized['status'] = $license->status;
			$sanitized['expires'] = $license->expires;
			$sanitized['lifetime'] = $license->lifetime;
			$sanitized['error'] = $license->error;
			$sanitized['payment_id'] = $license->payment_id;	
		} else {
			xmlsf_news_license_admin_error_msg( $response, $license );
		}
	}

	return $sanitized;
}
