1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
// Exit if accessed directly |
5
|
|
|
exit; |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
use Automattic\Jetpack\Status; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class introduces routines to get an affiliate code, that might be obtained from: |
12
|
|
|
* - an `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
|
|
|
* @since 6.9.0 |
16
|
|
|
*/ |
17
|
|
|
class Jetpack_Affiliate { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @since 6.9.0 |
21
|
|
|
* @var Jetpack_Affiliate This class instance. |
22
|
|
|
**/ |
23
|
|
|
private static $instance = null; |
24
|
|
|
|
25
|
|
|
private function __construct() { |
26
|
|
|
if ( ( new Status() )->is_development_mode() ) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes the class or returns the singleton |
33
|
|
|
* |
34
|
|
|
* @since 6.9.0 |
35
|
|
|
* |
36
|
|
|
* @return Jetpack_Affiliate | false |
37
|
|
|
*/ |
38
|
|
|
public static function init() { |
39
|
|
|
if ( is_null( self::$instance ) ) { |
40
|
|
|
self::$instance = new Jetpack_Affiliate(); |
41
|
|
|
} |
42
|
|
|
return self::$instance; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the affiliate code from database after filtering it. |
47
|
|
|
* |
48
|
|
|
* @since 6.9.0 |
49
|
|
|
* |
50
|
|
|
* @return string The affiliate code. |
51
|
|
|
*/ |
52
|
|
|
public function get_affiliate_code() { |
53
|
|
|
/** |
54
|
|
|
* Allow to filter the affiliate code. |
55
|
|
|
* |
56
|
|
|
* @since 6.9.0 |
57
|
|
|
* |
58
|
|
|
* @param string $aff_code The affiliate code, blank by default. |
59
|
|
|
*/ |
60
|
|
|
return apply_filters( 'jetpack_affiliate_code', get_option( 'jetpack_affiliate_code', '' ) ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the passed URL with the affiliate code added as a URL query arg. |
65
|
|
|
* |
66
|
|
|
* @since 6.9.0 |
67
|
|
|
* |
68
|
|
|
* @param string $url The URL where the code will be added. |
69
|
|
|
* |
70
|
|
|
* @return string The passed URL with the code added. |
71
|
|
|
*/ |
72
|
|
|
public function add_code_as_query_arg( $url ) { |
73
|
|
|
if ( '' !== ( $aff = $this->get_affiliate_code() ) ) { |
74
|
|
|
$url = add_query_arg( 'aff', $aff, $url ); |
75
|
|
|
} |
76
|
|
|
return $url; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
add_action( 'init', array( 'Jetpack_Affiliate', 'init' ) ); |
81
|
|
|
|