<?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 = '' ) {
	$repsonses = 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( $repsonses[$error] ) ? $repsonses[$error] : false;
}

function xmlsf_news_sanitize_license( $new ) {
	// prevent double calls to this callback, still happens with object caching
	if ( ! isset($new['action']) ) {
		return $new;
	}

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

	$old_key = isset($old['key']) ? $old['key'] : '';
	$old_status = isset($old['status']) ? $old['status'] : '';
	$new_key = isset($new['key']) ? trim( $new['key'] ) : '';

	if ( empty($new_key) ) {
		return false; // return false to save an empty option, returning null will not save the option
	}

	$sanitized = array(
		'key' => $new_key
	);

	if ( ! class_exists( 'EDD_SL_License' ) ) {
		include XMLSF_NEWS_ADV_DIR . '/vendor/EDD_SL_License.php';
	}
	// instantiate
	$license = new EDD_SL_License( $new_key, XMLSF_NEWS_ADV_STORE_URL, XMLSF_NEWS_ADV_ITEM_ID );

	// check
	$response = $license->check();

	$do_error_message = false;

	// set up admin message
	switch ( $response ) {
		case true:
		if ( 'valid' == $license->status ) {
			// Deactivate ?
			if ( 'deactivate' == $new['action'] ) {
				if ( true === $license->deactivate() ) {
					add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'Your license was successfully deactivated for this site.', 'xml-sitemap-feed' ), 'notice-info' );
				} else {
					$do_error_message = true;
				}
			} else {
				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 ) {
			// Activate ?
			if ( 'activate' == $new['action'] ) {
				if ( true === $license->activate() ) {
					add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'Your license was successfully activated for this site.', 'xml-sitemap-feed' ), 'notice-success' );
				} else {
					$do_error_message = true;
				}
			} else {
				add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'Your license is not active for this site.', 'xml-sitemap-feed' ), 'notice-info' );
			}
		} else {
			if ( $message = xmlsf_news_license_error_msg( $license->error, $license->expires ) ) {
				add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', $message, 'notice-warning' );
			}
		}
		break;

		case false:
		$do_error_message = true;
		break;

		default:
		if ( is_wp_error( $response ) ) {
			add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', $response->get_error_message(), 'notice-error' );
		} elseif ( is_numeric( $response ) ) {
			add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', sprintf( /* Translators: http repsonse code */ __( 'Unexpected response code %d.', 'xml-sitemap-feed' ), '<code>' . $response . '</code>' ), 'notice-error' );
		} else {
			add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', __( 'An error occurred, please try again.', 'xml-sitemap-feed' ), 'notice-error' );
		}
	}

	if ( $do_error_message && $message = xmlsf_news_license_error_msg( $license->error, $license->expires ) ) {
		add_settings_error( 'xmlsf_news_license', 'xmlsf_news_license_notice', $message, 'notice-error' );
	}

	// populate final sanitized license data
	$sanitized['status'] = $license->status;
	$sanitized['expires'] = $license->expires;
	$sanitized['error'] = $license->error;
	$sanitized['payment_id'] = $license->payment_id;

	return $sanitized;
}

function xmlsf_news_check_license( $data ) {

	// update our license data only if we have a key and only once a week
	if ( ! empty( $data['key'] ) && $data['key'] !== get_transient( 'xmlsf_news_license_verified' ) ) {

		if ( ! class_exists( 'EDD_SL_License' ) ) {
			include XMLSF_NEWS_ADV_DIR . '/vendor/EDD_SL_License.php';
		}

		// instantiate
		$license = new EDD_SL_License( $data['key'], XMLSF_NEWS_ADV_STORE_URL, XMLSF_NEWS_ADV_ITEM_ID );

		// check
		$response = $license->check();

		if ( is_bool( $response ) ) {
			// update data only if we got a true or false response
			$data['status'] = $license->status;
			$data['expires'] = $license->expires;
			$data['error'] = $license->error;
			$data['payment_id'] = $license->payment_id;

			set_transient( 'xmlsf_news_license_verified', $data['key'], WEEK_IN_SECONDS );
			update_option( 'xmlsf_news_license', $data );
		}

	}

	return $data;
}
