class-gsc-mapper.php 2.58 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?php
/**
 * @package WPSEO\Admin|Google_Search_Console
 */

/**
 * Class WPSEO_GSC_Mapper
 */
class WPSEO_GSC_Mapper {

	/**
	 * The platforms which can be mapped.
	 *
	 * @var array
	 */
	private static $platforms = array(
		'web'             => 'web',
		'mobile'          => 'mobile',
		'smartphone_only' => 'smartphoneOnly',
		'settings'        => 'settings', // This one is basicly not a platform, but a tab.
	);

	/**
	 * The categories which can be mapped
	 *
	 * @var array
	 */
	private static $categories = array(
		'access_denied'    => 'authPermissions',
		'faulty_redirects' => 'manyToOneRedirect',
		'not_followed'     => 'notFollowed',
		'not_found'        => 'notFound',
		'other'            => 'other',
		'roboted'          => 'roboted',
		'server_error'     => 'serverError',
		'soft_404'         => 'soft404',
	);

	/**
	 * If there is no platform, just get the first key out of the array and redirect to it.
	 *
	 * @param string $platform Platform (desktop, mobile, feature phone).
	 *
	 * @return mixed
	 */
	public static function get_current_platform( $platform ) {
		if ( $current_platform = filter_input( INPUT_GET, $platform ) ) {
			return $current_platform;
		}

		wp_redirect( add_query_arg( $platform, key( self::$platforms ) ) );
		exit;
	}

	/**
	 * Mapping the platform
	 *
	 * @param string $platform Platform (desktop, mobile, feature phone).
	 *
	 * @return mixed
	 */
	public static function platform_to_api( $platform ) {
		if ( ! empty( $platform ) && array_key_exists( $platform, self::$platforms ) ) {
			return self::$platforms[ $platform ];
		}
	}

	/**
	 * Mapping the given platform by value and return its key
	 *
	 * @param string $platform Platform (desktop, mobile, feature phone).
	 *
	 * @return string
	 */
	public static function platform_from_api( $platform ) {
		if ( ! empty( $platform ) && $platform = array_search( $platform, self::$platforms ) ) {
			return $platform;
		}

		return $platform;
	}

	/**
	 * Mapping the given category by searching for its key.
	 *
	 * @param string $category Issue type.
	 *
	 * @return mixed
	 */
	public static function category_to_api( $category ) {
		if ( ! empty( $category ) && array_key_exists( $category, self::$categories ) ) {
			return self::$categories[ $category ];
		}

		return $category;
	}

	/**
	 * Mapping the given category by value and return its key
	 *
	 * @param string $category Issue type.
	 *
	 * @return string
	 */
	public static function category_from_api( $category ) {
		if ( ! empty( $category ) && $category = array_search( $category, self::$categories ) ) {
			return $category;
		}

		return $category;
	}
}