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