Completed
Push — add/jetpack-aff-package ( 37ce9f )
by
unknown
97:00 queued 86:38
created

Affiliate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A get_affiliate_code() 0 8 1
A add_code_as_query_arg() 0 7 2
1
<?php
2
/**
3
 * Class to handle affiliate codes.
4
 *
5
 * @package affiliate
6
 */
7
8
namespace Automattic\Jetpack\Partners;
9
10
/**
11
 * This class introduces routines to get an affiliate code, that might be obtained from:
12
 * - a `jetpack_affiliate_code` option in the WP database
13
 * - an affiliate code returned by a filter bound to the `jetpack_affiliate_code` filter hook
14
 */
15
class Affiliate {
16
17
	/**
18
	 * Class instance
19
	 *
20
	 * @var Affiliate This class instance.
21
	 **/
22
	private static $instance = null;
23
24
	/**
25
	 * Initializes the class or returns the singleton
26
	 *
27
	 * @return Affiliate | false
28
	 */
29
	public static function init() {
30
		if ( is_null( self::$instance ) ) {
31
			self::$instance = new Affiliate();
32
		}
33
		return self::$instance;
34
	}
35
36
	/**
37
	 * Returns the affiliate code from database after filtering it.
38
	 *
39
	 * @return string The affiliate code.
40
	 */
41
	public function get_affiliate_code() {
42
		/**
43
		 * Allow to filter the affiliate code.
44
		 *
45
		 * @param string $aff_code The affiliate code, blank by default.
46
		 */
47
		return apply_filters( 'jetpack_affiliate_code', get_option( 'jetpack_affiliate_code', '' ) );
48
	}
49
50
	/**
51
	 * Returns the passed URL with the affiliate code added as a URL query arg.
52
	 *
53
	 * @param string $url The URL where the code will be added.
54
	 *
55
	 * @return string The passed URL with the code added.
56
	 */
57
	public function add_code_as_query_arg( $url ) {
58
		$aff = $this->get_affiliate_code();
59
		if ( '' !== $aff ) {
60
			$url = add_query_arg( 'aff', $aff, $url );
61
		}
62
		return $url;
63
	}
64
}
65