Skip to content

Commit

Permalink
Add simple shortlinker class
Browse files Browse the repository at this point in the history
  • Loading branch information
atimmer committed Mar 8, 2017
1 parent 793384e commit 9e0aec7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
50 changes: 50 additions & 0 deletions inc/class-wpseo-shortlinker.php
@@ -0,0 +1,50 @@
<?php
/**
* @package WPSEO
*/

/**
* Helps with creating shortlinks in the plugin
*/
class WPSEO_Shortlinker {

/**
* @var string
*/
protected $version;

/**
* @param string $version The version to put in the utm_content tag.
*/
public function __construct( $version ) {
$this->version = $version;
}

/**
* Builds a URL to use in the plugin as shortlink.
*
* @param string $url The URL to build upon.
*
* @return string The final URL.
*/
public function build_shortlink( $url ) {
return $url . '?utm_content=' . $this->version;
}

/**
* Returns a version of the URL with a utm_content with the current version.
*
* @param string $url The URL to build upon.
*
* @return string The final URL.
*/
public static function get( $url ) {
$version = WPSEO_VERSION;

$version = apply_filters( 'wpseo_shortlink_version', $version );

$shortlinker = new WPSEO_Shortlinker( $version );

return $shortlinker->build_shortlink( $url );
}
}
19 changes: 19 additions & 0 deletions tests/inc/test-class-wpseo-shortlinker.php
@@ -0,0 +1,19 @@
<?php


class WPSEO_Shortlinker_Test extends PHPUnit_Framework_TestCase {

public function test_build_shortlink() {
$shortlinks = new WPSEO_Shortlinker( 'version' );

$shortlink = $shortlinks->build_shortlink( 'http://yoa.st/abcdefg' );

$this->assertEquals( 'http://yoa.st/abcdefg?utm_content=version', $shortlink );
}

public function test_get() {
$shortlink = WPSEO_Shortlinker::get( 'http://yoa.st/blaat' );

$this->assertEquals( 'http://yoa.st/blaat?utm_content=' . WPSEO_VERSION, $shortlink );
}
}

0 comments on commit 9e0aec7

Please sign in to comment.