1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @package Freemius for EDD Add-On |
4
|
|
|
* @copyright Copyright (c) 2015, Freemius, Inc. |
5
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
6
|
|
|
* @since 1.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
10
|
|
|
exit; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
class FS_Pricing extends FS_Entity { |
|
|
|
|
14
|
|
|
|
15
|
|
|
#region Properties |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var number |
19
|
|
|
*/ |
20
|
|
|
public $plan_id; |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
public $licenses; |
25
|
|
|
/** |
26
|
|
|
* @var null|float |
27
|
|
|
*/ |
28
|
|
|
public $monthly_price; |
29
|
|
|
/** |
30
|
|
|
* @var null|float |
31
|
|
|
*/ |
32
|
|
|
public $annual_price; |
33
|
|
|
/** |
34
|
|
|
* @var null|float |
35
|
|
|
*/ |
36
|
|
|
public $lifetime_price; |
37
|
|
|
|
38
|
|
|
#endregion Properties |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param object|bool $pricing |
42
|
|
|
*/ |
43
|
|
|
function __construct( $pricing = false ) { |
|
|
|
|
44
|
|
|
parent::__construct( $pricing ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
static function get_type() { |
|
|
|
|
48
|
|
|
return 'pricing'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @author Vova Feldman (@svovaf) |
53
|
|
|
* @since 1.1.8 |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
function has_monthly() { |
|
|
|
|
58
|
|
|
return ( is_numeric( $this->monthly_price ) && $this->monthly_price > 0 ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @author Vova Feldman (@svovaf) |
63
|
|
|
* @since 1.1.8 |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
function has_annual() { |
|
|
|
|
68
|
|
|
return ( is_numeric( $this->annual_price ) && $this->annual_price > 0 ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @author Vova Feldman (@svovaf) |
73
|
|
|
* @since 1.1.8 |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
function has_lifetime() { |
|
|
|
|
78
|
|
|
return ( is_numeric( $this->lifetime_price ) && $this->lifetime_price > 0 ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if unlimited licenses pricing. |
83
|
|
|
* |
84
|
|
|
* @author Vova Feldman (@svovaf) |
85
|
|
|
* @since 1.1.8 |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
function is_unlimited() { |
|
|
|
|
90
|
|
|
return is_null( $this->licenses ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if pricing has more than one billing cycle. |
96
|
|
|
* |
97
|
|
|
* @author Vova Feldman (@svovaf) |
98
|
|
|
* @since 1.1.8 |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
function is_multi_cycle() { |
|
|
|
|
103
|
|
|
$cycles = 0; |
104
|
|
|
if ( $this->has_monthly() ) { |
105
|
|
|
$cycles ++; |
106
|
|
|
} |
107
|
|
|
if ( $this->has_annual() ) { |
108
|
|
|
$cycles ++; |
109
|
|
|
} |
110
|
|
|
if ( $this->has_lifetime() ) { |
111
|
|
|
$cycles ++; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $cycles > 1; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get annual over monthly discount. |
119
|
|
|
* |
120
|
|
|
* @author Vova Feldman (@svovaf) |
121
|
|
|
* @since 1.1.8 |
122
|
|
|
* |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
|
|
function annual_discount_percentage() { |
|
|
|
|
126
|
|
|
return floor( $this->annual_savings() / ( $this->monthly_price * 12 * ( $this->is_unlimited() ? 1 : $this->licenses ) ) * 100 ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get annual over monthly savings. |
131
|
|
|
* |
132
|
|
|
* @author Vova Feldman (@svovaf) |
133
|
|
|
* @since 1.1.8 |
134
|
|
|
* |
135
|
|
|
* @return float |
136
|
|
|
*/ |
137
|
|
|
function annual_savings() { |
|
|
|
|
138
|
|
|
return ( $this->monthly_price * 12 - $this->annual_price ) * ( $this->is_unlimited() ? 1 : $this->licenses ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.