PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/inc/class-wpseo-shortlinker.php

https://bitbucket.org/carloskikea/helpet
PHP | 103 lines | 51 code | 10 blank | 42 comment | 2 complexity | 50936943cec0b3bc74df07aa21e21386 MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO
  6 */
  7
  8/**
  9 * Helps with creating shortlinks in the plugin
 10 */
 11class WPSEO_Shortlinker {
 12	/**
 13	 * Builds a URL to use in the plugin as shortlink.
 14	 *
 15	 * @param string $url The URL to build upon.
 16	 *
 17	 * @return string The final URL.
 18	 */
 19	public function build_shortlink( $url ) {
 20		return add_query_arg(
 21			array(
 22				'php_version'      => $this->get_php_version(),
 23				'platform'         => 'wordpress',
 24				'platform_version' => $GLOBALS['wp_version'],
 25				'software'         => $this->get_software(),
 26				'software_version' => WPSEO_VERSION,
 27				'role'             => $this->get_filtered_user_role(),
 28			),
 29			$url
 30		);
 31	}
 32
 33	/**
 34	 * Returns a version of the URL with a utm_content with the current version.
 35	 *
 36	 * @param string $url The URL to build upon.
 37	 *
 38	 * @return string The final URL.
 39	 */
 40	public static function get( $url ) {
 41		$shortlinker = new WPSEO_Shortlinker();
 42
 43		return $shortlinker->build_shortlink( $url );
 44	}
 45
 46	/**
 47	 * Echoes a version of the URL with a utm_content with the current version.
 48	 *
 49	 * @param string $url The URL to build upon.
 50	 */
 51	public static function show( $url ) {
 52		echo esc_url( self::get( $url ) );
 53	}
 54
 55	/**
 56	 * Gets the current site's PHP version, without the extra info.
 57	 *
 58	 * @return string The PHP version.
 59	 */
 60	private function get_php_version() {
 61		$version = explode( '.', PHP_VERSION );
 62
 63		return (int) $version[0] . '.' . (int) $version[1] . '.' . (int) $version[2];
 64	}
 65
 66	/**
 67	 * Get our software and whether it's active or not.
 68	 *
 69	 * @return string The software name + activation state.
 70	 */
 71	private function get_software() {
 72		if ( WPSEO_Utils::is_yoast_seo_premium() ) {
 73			return 'premium';
 74		}
 75
 76		return 'free';
 77	}
 78
 79	/**
 80	 * Gets the current user's role without leaking roles that shouldn't be public.
 81	 *
 82	 * @return string The filtered user role.
 83	 */
 84	private function get_filtered_user_role() {
 85		$user           = wp_get_current_user();
 86		$built_in_roles = array(
 87			'administrator',
 88			'wpseo_manager',
 89			'wpseo_editor',
 90			'editor',
 91			'author',
 92			'contributor',
 93			'subscriber',
 94		);
 95		$filtered_roles = array_intersect( $built_in_roles, $user->roles );
 96
 97		$role = current( $filtered_roles );
 98		if ( ! $role ) {
 99			$role = 'unknown';
100		}
101		return $role;
102	}
103}