Passed
Push — master ( 330418...c4952f )
by Brian
05:45 queued 27s
created
includes/data/discount-schema.php 1 patch
Indentation   +172 added lines, -172 removed lines patch added patch discarded remove patch
@@ -13,177 +13,177 @@
 block discarded – undo
13 13
 
14 14
 return array(
15 15
 
16
-	'id'              => array(
17
-		'description' => __( 'Unique identifier for the discount.', 'invoicing' ),
18
-		'type'        => 'integer',
19
-		'context'     => array( 'view', 'edit', 'embed' ),
20
-		'readonly'    => true,
21
-	),
22
-
23
-	'status'          => array(
24
-		'description' => __( 'A named status for the discount.', 'invoicing' ),
25
-		'type'        => 'string',
26
-		'enum'        => array( 'publish', 'pending', 'draft', 'expired' ),
27
-		'default'     => 'draft',
28
-		'context'     => array( 'view', 'edit', 'embed' ),
29
-	),
30
-
31
-	'version'         => array(
32
-		'description' => __( 'Plugin version when the discount was created.', 'invoicing' ),
33
-		'type'        => 'string',
34
-		'context'     => array( 'view', 'edit', 'embed' ),
35
-		'readonly'    => true,
36
-	),
37
-
38
-	'date_created'    => array(
39
-		'description' => __( "The date the discount was created, in the site's timezone.", 'invoicing' ),
40
-		'type'        => 'string',
41
-		'context'     => array( 'view', 'edit', 'embed' ),
42
-	),
43
-
44
-	'date_created_gmt'    => array(
45
-		'description' => __( 'The GMT date the discount was created.', 'invoicing' ),
46
-		'type'        => 'string',
47
-		'context'     => array( 'view', 'edit', 'embed' ),
48
-		'readonly'    => true,
49
-	),
50
-
51
-	'date_modified'   => array(
52
-		'description' => __( "The date the discount was last modified, in the site's timezone.", 'invoicing' ),
53
-		'type'        => 'string',
54
-		'context'     => array( 'view', 'edit', 'embed' ),
55
-		'readonly'    => true,
56
-	),
57
-
58
-	'date_modified_gmt'    => array(
59
-		'description' => __( 'The GMT date the discount was last modified.', 'invoicing' ),
60
-		'type'        => 'string',
61
-		'context'     => array( 'view', 'edit', 'embed' ),
62
-		'readonly'    => true,
63
-	),
64
-
65
-	'name'			  => array(
66
-		'description' => __( 'The discount name.', 'invoicing' ),
67
-		'type'        => 'string',
68
-		'context'     => array( 'view', 'edit', 'embed' ),
69
-	),
70
-
71
-	'description'     => array(
72
-		'description' => __( 'A description of what the discount is all about.', 'invoicing' ),
73
-		'type'        => 'string',
74
-		'context'     => array( 'view', 'edit', 'embed' ),
75
-	),
76
-
77
-	'code'            => array(
78
-		'description' => __( 'The discount code.', 'invoicing' ),
79
-		'type'        => 'string',
80
-		'context'     => array( 'view', 'edit', 'embed' ),
81
-		'required'	  => true,
82
-	),
83
-
84
-	'type'            => array(
85
-		'description' => __( 'The type of discount.', 'invoicing' ),
86
-		'type'        => 'string',
87
-		'enum'        => array_keys( wpinv_get_discount_types() ),
88
-		'context'     => array( 'view', 'edit', 'embed' ),
89
-		'default'	  => 'percent',
90
-	),
91
-
92
-	'amount'        => array(
93
-		'description' => __( 'The discount value.', 'invoicing' ),
94
-		'type'        => 'number',
95
-		'context'     => array( 'view', 'edit', 'embed' ),
96
-		'required'	  => true,
97
-	),
98
-
99
-	'formatted_amount'        => array(
100
-		'description' => __( 'The formatted discount value.', 'invoicing' ),
101
-		'type'        => 'string',
102
-		'context'     => array( 'view', 'edit', 'embed' ),
103
-		'readonly'    => true,
104
-	),
105
-
106
-	'uses'            => array(
107
-		'description' => __( 'The number of times the discount has been used.', 'invoicing' ),
108
-		'type'        => 'integer',
109
-		'context'     => array( 'view', 'embed' ),
110
-		'readonly'    => true,
111
-	),
112
-
113
-	'max_uses'        => array(
114
-		'description' => __( 'The maximum number of times the discount can be used.', 'invoicing' ),
115
-		'type'        => 'integer',
116
-		'context'     => array( 'view', 'edit' ),
117
-	),
118
-
119
-	'usage'           => array(
120
-		'description' => __( "The discount's usage, i.e uses / max uses.", 'invoicing' ),
121
-		'type'        => 'string',
122
-		'context'     => array( 'view', 'embed' ),
123
-		'readonly'    => true,
124
-	),
125
-
126
-	'is_single_use'   => array(
127
-		'description' => __( 'Whether or not the discount can only be used once per user.', 'invoicing' ),
128
-		'type'        => 'boolean',
129
-		'context'     => array( 'view', 'edit' ),
130
-	),
131
-
132
-	'is_recurring'   => array(
133
-		'description' => __( 'Whether or not the discount applies to the initial payment only or all recurring payments.', 'invoicing' ),
134
-		'type'        => 'boolean',
135
-		'context'     => array( 'view', 'edit' ),
136
-	),
137
-
138
-	'start_date'      => array(
139
-		'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss. If provided, the discount can only be used after or on this date.', 'invoicing' ),
140
-		'type'        => 'string',
141
-		'context'     => array( 'view', 'edit' ),
142
-	),
143
-
144
-	'end_date'        => array(
145
-		'description' => __( 'The expiration date for the discount.', 'invoicing' ),
146
-		'type'        => 'string',
147
-		'context'     => array( 'view', 'edit' ),
148
-	),
149
-
150
-	'allowed_items'   => array(
151
-		'description' => __( 'Items which are allowed to use this discount. Leave blank to enable for all items.', 'invoicing' ),
152
-		'type'        => 'array',
153
-		'context'     => array( 'view', 'edit' ),
154
-		'items'       => array(
155
-			'type'    => 'integer'
156
-		)
157
-	),
158
-
159
-	'excluded_items'  => array(
160
-		'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ),
161
-		'type'        => 'array',
162
-		'context'     => array( 'view', 'edit' ),
163
-		'items'       => array(
164
-			'type'    => 'integer'
165
-		)
166
-	),
167
-
168
-	'required_items'  => array(
169
-		'description' => __( 'Items which are required to be in the cart before using this discount.', 'invoicing' ),
170
-		'type'        => 'array',
171
-		'context'     => array( 'view', 'edit' ),
172
-		'items'       => array(
173
-			'type'    => 'integer'
174
-		)
175
-	),
176
-
177
-	'minimum_total'   => array(
178
-		'description' => __( 'The minimum total needed to use this invoice.', 'invoicing' ),
179
-		'type'        => 'number',
180
-		'context'     => array( 'view', 'edit' ),
181
-	),
182
-
183
-	'maximum_total'   => array(
184
-		'description' => __( 'The maximum total needed to use this invoice.', 'invoicing' ),
185
-		'type'        => 'number',
186
-		'context'     => array( 'view', 'edit' ),
187
-	),
16
+    'id'              => array(
17
+        'description' => __( 'Unique identifier for the discount.', 'invoicing' ),
18
+        'type'        => 'integer',
19
+        'context'     => array( 'view', 'edit', 'embed' ),
20
+        'readonly'    => true,
21
+    ),
22
+
23
+    'status'          => array(
24
+        'description' => __( 'A named status for the discount.', 'invoicing' ),
25
+        'type'        => 'string',
26
+        'enum'        => array( 'publish', 'pending', 'draft', 'expired' ),
27
+        'default'     => 'draft',
28
+        'context'     => array( 'view', 'edit', 'embed' ),
29
+    ),
30
+
31
+    'version'         => array(
32
+        'description' => __( 'Plugin version when the discount was created.', 'invoicing' ),
33
+        'type'        => 'string',
34
+        'context'     => array( 'view', 'edit', 'embed' ),
35
+        'readonly'    => true,
36
+    ),
37
+
38
+    'date_created'    => array(
39
+        'description' => __( "The date the discount was created, in the site's timezone.", 'invoicing' ),
40
+        'type'        => 'string',
41
+        'context'     => array( 'view', 'edit', 'embed' ),
42
+    ),
43
+
44
+    'date_created_gmt'    => array(
45
+        'description' => __( 'The GMT date the discount was created.', 'invoicing' ),
46
+        'type'        => 'string',
47
+        'context'     => array( 'view', 'edit', 'embed' ),
48
+        'readonly'    => true,
49
+    ),
50
+
51
+    'date_modified'   => array(
52
+        'description' => __( "The date the discount was last modified, in the site's timezone.", 'invoicing' ),
53
+        'type'        => 'string',
54
+        'context'     => array( 'view', 'edit', 'embed' ),
55
+        'readonly'    => true,
56
+    ),
57
+
58
+    'date_modified_gmt'    => array(
59
+        'description' => __( 'The GMT date the discount was last modified.', 'invoicing' ),
60
+        'type'        => 'string',
61
+        'context'     => array( 'view', 'edit', 'embed' ),
62
+        'readonly'    => true,
63
+    ),
64
+
65
+    'name'			  => array(
66
+        'description' => __( 'The discount name.', 'invoicing' ),
67
+        'type'        => 'string',
68
+        'context'     => array( 'view', 'edit', 'embed' ),
69
+    ),
70
+
71
+    'description'     => array(
72
+        'description' => __( 'A description of what the discount is all about.', 'invoicing' ),
73
+        'type'        => 'string',
74
+        'context'     => array( 'view', 'edit', 'embed' ),
75
+    ),
76
+
77
+    'code'            => array(
78
+        'description' => __( 'The discount code.', 'invoicing' ),
79
+        'type'        => 'string',
80
+        'context'     => array( 'view', 'edit', 'embed' ),
81
+        'required'	  => true,
82
+    ),
83
+
84
+    'type'            => array(
85
+        'description' => __( 'The type of discount.', 'invoicing' ),
86
+        'type'        => 'string',
87
+        'enum'        => array_keys( wpinv_get_discount_types() ),
88
+        'context'     => array( 'view', 'edit', 'embed' ),
89
+        'default'	  => 'percent',
90
+    ),
91
+
92
+    'amount'        => array(
93
+        'description' => __( 'The discount value.', 'invoicing' ),
94
+        'type'        => 'number',
95
+        'context'     => array( 'view', 'edit', 'embed' ),
96
+        'required'	  => true,
97
+    ),
98
+
99
+    'formatted_amount'        => array(
100
+        'description' => __( 'The formatted discount value.', 'invoicing' ),
101
+        'type'        => 'string',
102
+        'context'     => array( 'view', 'edit', 'embed' ),
103
+        'readonly'    => true,
104
+    ),
105
+
106
+    'uses'            => array(
107
+        'description' => __( 'The number of times the discount has been used.', 'invoicing' ),
108
+        'type'        => 'integer',
109
+        'context'     => array( 'view', 'embed' ),
110
+        'readonly'    => true,
111
+    ),
112
+
113
+    'max_uses'        => array(
114
+        'description' => __( 'The maximum number of times the discount can be used.', 'invoicing' ),
115
+        'type'        => 'integer',
116
+        'context'     => array( 'view', 'edit' ),
117
+    ),
118
+
119
+    'usage'           => array(
120
+        'description' => __( "The discount's usage, i.e uses / max uses.", 'invoicing' ),
121
+        'type'        => 'string',
122
+        'context'     => array( 'view', 'embed' ),
123
+        'readonly'    => true,
124
+    ),
125
+
126
+    'is_single_use'   => array(
127
+        'description' => __( 'Whether or not the discount can only be used once per user.', 'invoicing' ),
128
+        'type'        => 'boolean',
129
+        'context'     => array( 'view', 'edit' ),
130
+    ),
131
+
132
+    'is_recurring'   => array(
133
+        'description' => __( 'Whether or not the discount applies to the initial payment only or all recurring payments.', 'invoicing' ),
134
+        'type'        => 'boolean',
135
+        'context'     => array( 'view', 'edit' ),
136
+    ),
137
+
138
+    'start_date'      => array(
139
+        'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss. If provided, the discount can only be used after or on this date.', 'invoicing' ),
140
+        'type'        => 'string',
141
+        'context'     => array( 'view', 'edit' ),
142
+    ),
143
+
144
+    'end_date'        => array(
145
+        'description' => __( 'The expiration date for the discount.', 'invoicing' ),
146
+        'type'        => 'string',
147
+        'context'     => array( 'view', 'edit' ),
148
+    ),
149
+
150
+    'allowed_items'   => array(
151
+        'description' => __( 'Items which are allowed to use this discount. Leave blank to enable for all items.', 'invoicing' ),
152
+        'type'        => 'array',
153
+        'context'     => array( 'view', 'edit' ),
154
+        'items'       => array(
155
+            'type'    => 'integer'
156
+        )
157
+    ),
158
+
159
+    'excluded_items'  => array(
160
+        'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ),
161
+        'type'        => 'array',
162
+        'context'     => array( 'view', 'edit' ),
163
+        'items'       => array(
164
+            'type'    => 'integer'
165
+        )
166
+    ),
167
+
168
+    'required_items'  => array(
169
+        'description' => __( 'Items which are required to be in the cart before using this discount.', 'invoicing' ),
170
+        'type'        => 'array',
171
+        'context'     => array( 'view', 'edit' ),
172
+        'items'       => array(
173
+            'type'    => 'integer'
174
+        )
175
+    ),
176
+
177
+    'minimum_total'   => array(
178
+        'description' => __( 'The minimum total needed to use this invoice.', 'invoicing' ),
179
+        'type'        => 'number',
180
+        'context'     => array( 'view', 'edit' ),
181
+    ),
182
+
183
+    'maximum_total'   => array(
184
+        'description' => __( 'The maximum total needed to use this invoice.', 'invoicing' ),
185
+        'type'        => 'number',
186
+        'context'     => array( 'view', 'edit' ),
187
+    ),
188 188
 
189 189
 );
Please login to merge, or discard this patch.
includes/class-wpinv-discount.php 1 patch
Indentation   +1349 added lines, -1349 removed lines patch added patch discarded remove patch
@@ -15,30 +15,30 @@  discard block
 block discarded – undo
15 15
  */
16 16
 class WPInv_Discount extends GetPaid_Data  {
17 17
 
18
-	/**
19
-	 * Which data store to load.
20
-	 *
21
-	 * @var string
22
-	 */
18
+    /**
19
+     * Which data store to load.
20
+     *
21
+     * @var string
22
+     */
23 23
     protected $data_store_name = 'discount';
24 24
 
25 25
     /**
26
-	 * This is the name of this object type.
27
-	 *
28
-	 * @var string
29
-	 */
30
-	protected $object_type = 'discount';
31
-
32
-	/**
33
-	 * Discount Data array. This is the core item data exposed in APIs.
34
-	 *
35
-	 * @since 1.0.19
36
-	 * @var array
37
-	 */
38
-	protected $data = array(
39
-		'status'               => 'draft',
40
-		'version'              => '',
41
-		'date_created'         => null,
26
+     * This is the name of this object type.
27
+     *
28
+     * @var string
29
+     */
30
+    protected $object_type = 'discount';
31
+
32
+    /**
33
+     * Discount Data array. This is the core item data exposed in APIs.
34
+     *
35
+     * @since 1.0.19
36
+     * @var array
37
+     */
38
+    protected $data = array(
39
+        'status'               => 'draft',
40
+        'version'              => '',
41
+        'date_created'         => null,
42 42
         'date_modified'        => null,
43 43
         'name'                 => 'no-name',
44 44
         'description'          => '',
@@ -49,7 +49,7 @@  discard block
 block discarded – undo
49 49
         'start'                => null,
50 50
         'items'                => array(),
51 51
         'excluded_items'       => array(),
52
-		'required_items'       => array(),
52
+        'required_items'       => array(),
53 53
         'uses' 				   => 0,
54 54
         'max_uses'             => null,
55 55
         'is_recurring'         => null,
@@ -59,144 +59,144 @@  discard block
 block discarded – undo
59 59
         'amount'               => null,
60 60
     );
61 61
 
62
-	/**
63
-	 * Stores meta in cache for future reads.
64
-	 *
65
-	 * A group must be set to to enable caching.
66
-	 *
67
-	 * @var string
68
-	 */
69
-	protected $cache_group = 'getpaid_discounts';
62
+    /**
63
+     * Stores meta in cache for future reads.
64
+     *
65
+     * A group must be set to to enable caching.
66
+     *
67
+     * @var string
68
+     */
69
+    protected $cache_group = 'getpaid_discounts';
70 70
 
71 71
     /**
72 72
      * Stores a reference to the original WP_Post object
73 73
      *
74 74
      * @var WP_Post
75 75
      */
76
-	protected $post = null;
77
-
78
-	/**
79
-	 * Get the discount if ID is passed, otherwise the discount is new and empty.
80
-	 *
81
-	 * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code.
82
-	 */
83
-	public function __construct( $discount = 0 ) {
84
-		parent::__construct( $discount );
85
-
86
-		if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) {
87
-			$this->set_id( $discount );
88
-		} elseif ( $discount instanceof self ) {
89
-			$this->set_id( $discount->get_id() );
90
-		} elseif ( ! empty( $discount->ID ) ) {
91
-			$this->set_id( $discount->ID );
92
-		} elseif ( is_array( $discount ) ) {
93
-			$this->set_props( $discount );
94
-
95
-			if ( isset( $discount['ID'] ) ) {
96
-				$this->set_id( $discount['ID'] );
97
-			}
98
-
99
-		} elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) {
100
-			$this->set_id( $discount );
101
-		} else {
102
-			$this->set_object_read( true );
103
-		}
76
+    protected $post = null;
77
+
78
+    /**
79
+     * Get the discount if ID is passed, otherwise the discount is new and empty.
80
+     *
81
+     * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code.
82
+     */
83
+    public function __construct( $discount = 0 ) {
84
+        parent::__construct( $discount );
85
+
86
+        if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) {
87
+            $this->set_id( $discount );
88
+        } elseif ( $discount instanceof self ) {
89
+            $this->set_id( $discount->get_id() );
90
+        } elseif ( ! empty( $discount->ID ) ) {
91
+            $this->set_id( $discount->ID );
92
+        } elseif ( is_array( $discount ) ) {
93
+            $this->set_props( $discount );
94
+
95
+            if ( isset( $discount['ID'] ) ) {
96
+                $this->set_id( $discount['ID'] );
97
+            }
98
+
99
+        } elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) {
100
+            $this->set_id( $discount );
101
+        } else {
102
+            $this->set_object_read( true );
103
+        }
104 104
 
105 105
         // Load the datastore.
106
-		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
106
+        $this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
107 107
 
108
-		if ( $this->get_id() > 0 ) {
108
+        if ( $this->get_id() > 0 ) {
109 109
             $this->post = get_post( $this->get_id() );
110 110
             $this->ID   = $this->get_id();
111
-			$this->data_store->read( $this );
111
+            $this->data_store->read( $this );
112
+        }
113
+
114
+    }
115
+
116
+    /**
117
+     * Fetch a discount from the db/cache
118
+     *
119
+     *
120
+     * @static
121
+     * @param string $field The field to query against: 'ID', 'discount_code'
122
+     * @param string|int $value The field value
123
+     * @deprecated
124
+     * @since 1.0.15
125
+     * @return array|bool array of discount details on success. False otherwise.
126
+     */
127
+    public static function get_data_by( $field, $value ) {
128
+
129
+        if ( 'id' == strtolower( $field ) ) {
130
+            // Make sure the value is numeric to avoid casting objects, for example,
131
+            // to int 1.
132
+            if ( ! is_numeric( $value ) )
133
+                return false;
134
+            $value = intval( $value );
135
+            if ( $value < 1 )
136
+                return false;
137
+        }
138
+
139
+        if ( ! $value || ! is_string( $field ) ) {
140
+            return false;
141
+        }
142
+
143
+        $field = trim( $field );
144
+
145
+        // prepare query args
146
+        switch ( strtolower( $field ) ) {
147
+            case 'id':
148
+                $discount_id = $value;
149
+                $args		 = array( 'include' => array( $value ) );
150
+                break;
151
+            case 'discount_code':
152
+            case 'code':
153
+                $value       = trim( $value );
154
+                $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' );
155
+                $args		 = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value );
156
+                break;
157
+            case 'name':
158
+                $discount_id = 0;
159
+                $args		 = array( 'name' => trim( $value ) );
160
+                break;
161
+            default:
162
+                $args		 = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value );
163
+                if ( ! is_array( $args ) ) {
164
+                    return apply_filters( "wpinv_discount_get_data_by_$field", false, $value );
165
+                }
166
+
167
+        }
168
+
169
+        // Check if there is a cached value.
170
+        if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) {
171
+            return $discount;
172
+        }
173
+
174
+        $args = array_merge(
175
+            $args,
176
+            array(
177
+                'post_type'      => 'wpi_discount',
178
+                'posts_per_page' => 1,
179
+                'post_status'    => array( 'publish', 'pending', 'draft', 'expired' )
180
+            )
181
+        );
182
+
183
+        $discount = get_posts( $args );
184
+
185
+        if( empty( $discount ) ) {
186
+            return false;
112 187
         }
113 188
 
114
-	}
115
-
116
-	/**
117
-	 * Fetch a discount from the db/cache
118
-	 *
119
-	 *
120
-	 * @static
121
-	 * @param string $field The field to query against: 'ID', 'discount_code'
122
-	 * @param string|int $value The field value
123
-	 * @deprecated
124
-	 * @since 1.0.15
125
-	 * @return array|bool array of discount details on success. False otherwise.
126
-	 */
127
-	public static function get_data_by( $field, $value ) {
128
-
129
-		if ( 'id' == strtolower( $field ) ) {
130
-			// Make sure the value is numeric to avoid casting objects, for example,
131
-			// to int 1.
132
-			if ( ! is_numeric( $value ) )
133
-				return false;
134
-			$value = intval( $value );
135
-			if ( $value < 1 )
136
-				return false;
137
-		}
138
-
139
-		if ( ! $value || ! is_string( $field ) ) {
140
-			return false;
141
-		}
142
-
143
-		$field = trim( $field );
144
-
145
-		// prepare query args
146
-		switch ( strtolower( $field ) ) {
147
-			case 'id':
148
-				$discount_id = $value;
149
-				$args		 = array( 'include' => array( $value ) );
150
-				break;
151
-			case 'discount_code':
152
-			case 'code':
153
-				$value       = trim( $value );
154
-				$discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' );
155
-				$args		 = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value );
156
-				break;
157
-			case 'name':
158
-				$discount_id = 0;
159
-				$args		 = array( 'name' => trim( $value ) );
160
-				break;
161
-			default:
162
-				$args		 = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value );
163
-				if ( ! is_array( $args ) ) {
164
-					return apply_filters( "wpinv_discount_get_data_by_$field", false, $value );
165
-				}
166
-
167
-		}
168
-
169
-		// Check if there is a cached value.
170
-		if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) {
171
-			return $discount;
172
-		}
173
-
174
-		$args = array_merge(
175
-			$args,
176
-			array(
177
-				'post_type'      => 'wpi_discount',
178
-				'posts_per_page' => 1,
179
-				'post_status'    => array( 'publish', 'pending', 'draft', 'expired' )
180
-			)
181
-		);
182
-
183
-		$discount = get_posts( $args );
184
-
185
-		if( empty( $discount ) ) {
186
-			return false;
187
-		}
188
-
189
-		$discount = $discount[0];
190
-
191
-		// Prepare the return data.
192
-		$return = array(
189
+        $discount = $discount[0];
190
+
191
+        // Prepare the return data.
192
+        $return = array(
193 193
             'ID'                          => $discount->ID,
194 194
             'code'                        => get_post_meta( $discount->ID, '_wpi_discount_code', true ),
195 195
             'amount'                      => get_post_meta( $discount->ID, '_wpi_discount_amount', true ),
196 196
             'date_created'                => $discount->post_date,
197
-			'date_modified'               => $discount->post_modified,
198
-			'status'               		  => $discount->post_status,
199
-			'start'                  	  => get_post_meta( $discount->ID, '_wpi_discount_start', true ),
197
+            'date_modified'               => $discount->post_modified,
198
+            'status'               		  => $discount->post_status,
199
+            'start'                  	  => get_post_meta( $discount->ID, '_wpi_discount_start', true ),
200 200
             'expiration'                  => get_post_meta( $discount->ID, '_wpi_discount_expiration', true ),
201 201
             'type'               		  => get_post_meta( $discount->ID, '_wpi_discount_type', true ),
202 202
             'description'                 => $discount->post_excerpt,
@@ -204,84 +204,84 @@  discard block
 block discarded – undo
204 204
             'is_single_use'               => get_post_meta( $discount->ID, '_wpi_discount_is_single_use', true ),
205 205
             'items'              	      => get_post_meta( $discount->ID, '_wpi_discount_items', true ),
206 206
             'excluded_items'              => get_post_meta( $discount->ID, '_wpi_discount_excluded_items', true ),
207
-			'required_items'              => get_post_meta( $discount->ID, '_wpi_discount_required_items', true ),
207
+            'required_items'              => get_post_meta( $discount->ID, '_wpi_discount_required_items', true ),
208 208
             'max_uses'                    => get_post_meta( $discount->ID, '_wpi_discount_max_uses', true ),
209 209
             'is_recurring'                => get_post_meta( $discount->ID, '_wpi_discount_is_recurring', true ),
210 210
             'min_total'                   => get_post_meta( $discount->ID, '_wpi_discount_min_total', true ),
211 211
             'max_total'                   => get_post_meta( $discount->ID, '_wpi_discount_max_total', true ),
212 212
         );
213 213
 
214
-		$return = apply_filters( 'wpinv_discount_properties', $return );
215
-
216
-		// Update the cache with our data
217
-		wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' );
218
-		wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' );
219
-
220
-		return $return;
221
-	}
222
-
223
-	/**
224
-	 * Given a discount code, it returns a discount id.
225
-	 *
226
-	 *
227
-	 * @static
228
-	 * @param string $discount_code
229
-	 * @since 1.0.15
230
-	 * @return int
231
-	 */
232
-	public static function get_discount_id_by_code( $discount_code ) {
233
-
234
-		// Trim the code.
235
-		$discount_code = trim( $discount_code );
236
-
237
-		// Ensure a value has been passed.
238
-		if ( empty( $discount_code ) ) {
239
-			return 0;
240
-		}
241
-
242
-		// Maybe retrieve from the cache.
243
-		$discount_id   = wp_cache_get( $discount_code, 'getpaid_discount_codes' );
244
-		if ( ! empty( $discount_id ) ) {
245
-			return $discount_id;
246
-		}
247
-
248
-		// Fetch the first discount codes.
249
-		$discounts = get_posts(
250
-			array(
251
-				'meta_key'       => '_wpi_discount_code',
252
-				'meta_value'     => $discount_code,
253
-				'post_type'      => 'wpi_discount',
254
-				'posts_per_page' => 1,
255
-				'post_status'    => array( 'publish', 'pending', 'draft', 'expired' ),
256
-				'fields'         => 'ids',
257
-			)
258
-		);
259
-
260
-		if ( empty( $discounts ) ) {
261
-			return 0;
262
-		}
263
-
264
-		$discount_id = $discounts[0];
265
-
266
-		// Update the cache with our data
267
-		wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' );
268
-
269
-		return $discount_id;
270
-	}
271
-
272
-	/**
273
-	 * Magic method for checking the existence of a certain custom field.
274
-	 *
275
-	 * @since 1.0.15
276
-	 * @access public
277
-	 *
278
-	 * @return bool Whether the given discount field is set.
279
-	 */
280
-	public function __isset( $key ){
281
-		return isset( $this->data[$key] ) || method_exists( $this, "get_$key");
282
-	}
283
-
284
-	/*
214
+        $return = apply_filters( 'wpinv_discount_properties', $return );
215
+
216
+        // Update the cache with our data
217
+        wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' );
218
+        wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' );
219
+
220
+        return $return;
221
+    }
222
+
223
+    /**
224
+     * Given a discount code, it returns a discount id.
225
+     *
226
+     *
227
+     * @static
228
+     * @param string $discount_code
229
+     * @since 1.0.15
230
+     * @return int
231
+     */
232
+    public static function get_discount_id_by_code( $discount_code ) {
233
+
234
+        // Trim the code.
235
+        $discount_code = trim( $discount_code );
236
+
237
+        // Ensure a value has been passed.
238
+        if ( empty( $discount_code ) ) {
239
+            return 0;
240
+        }
241
+
242
+        // Maybe retrieve from the cache.
243
+        $discount_id   = wp_cache_get( $discount_code, 'getpaid_discount_codes' );
244
+        if ( ! empty( $discount_id ) ) {
245
+            return $discount_id;
246
+        }
247
+
248
+        // Fetch the first discount codes.
249
+        $discounts = get_posts(
250
+            array(
251
+                'meta_key'       => '_wpi_discount_code',
252
+                'meta_value'     => $discount_code,
253
+                'post_type'      => 'wpi_discount',
254
+                'posts_per_page' => 1,
255
+                'post_status'    => array( 'publish', 'pending', 'draft', 'expired' ),
256
+                'fields'         => 'ids',
257
+            )
258
+        );
259
+
260
+        if ( empty( $discounts ) ) {
261
+            return 0;
262
+        }
263
+
264
+        $discount_id = $discounts[0];
265
+
266
+        // Update the cache with our data
267
+        wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' );
268
+
269
+        return $discount_id;
270
+    }
271
+
272
+    /**
273
+     * Magic method for checking the existence of a certain custom field.
274
+     *
275
+     * @since 1.0.15
276
+     * @access public
277
+     *
278
+     * @return bool Whether the given discount field is set.
279
+     */
280
+    public function __isset( $key ){
281
+        return isset( $this->data[$key] ) || method_exists( $this, "get_$key");
282
+    }
283
+
284
+    /*
285 285
 	|--------------------------------------------------------------------------
286 286
 	| CRUD methods
287 287
 	|--------------------------------------------------------------------------
@@ -296,441 +296,441 @@  discard block
 block discarded – undo
296 296
 	|--------------------------------------------------------------------------
297 297
 	*/
298 298
 
299
-	/**
300
-	 * Get discount status.
301
-	 *
302
-	 * @since 1.0.19
303
-	 * @param  string $context View or edit context.
304
-	 * @return string
305
-	 */
306
-	public function get_status( $context = 'view' ) {
307
-		return $this->get_prop( 'status', $context );
299
+    /**
300
+     * Get discount status.
301
+     *
302
+     * @since 1.0.19
303
+     * @param  string $context View or edit context.
304
+     * @return string
305
+     */
306
+    public function get_status( $context = 'view' ) {
307
+        return $this->get_prop( 'status', $context );
308 308
     }
309 309
 
310 310
     /**
311
-	 * Get plugin version when the discount was created.
312
-	 *
313
-	 * @since 1.0.19
314
-	 * @param  string $context View or edit context.
315
-	 * @return string
316
-	 */
317
-	public function get_version( $context = 'view' ) {
318
-		return $this->get_prop( 'version', $context );
311
+     * Get plugin version when the discount was created.
312
+     *
313
+     * @since 1.0.19
314
+     * @param  string $context View or edit context.
315
+     * @return string
316
+     */
317
+    public function get_version( $context = 'view' ) {
318
+        return $this->get_prop( 'version', $context );
319 319
     }
320 320
 
321 321
     /**
322
-	 * Get date when the discount was created.
323
-	 *
324
-	 * @since 1.0.19
325
-	 * @param  string $context View or edit context.
326
-	 * @return string
327
-	 */
328
-	public function get_date_created( $context = 'view' ) {
329
-		return $this->get_prop( 'date_created', $context );
322
+     * Get date when the discount was created.
323
+     *
324
+     * @since 1.0.19
325
+     * @param  string $context View or edit context.
326
+     * @return string
327
+     */
328
+    public function get_date_created( $context = 'view' ) {
329
+        return $this->get_prop( 'date_created', $context );
330 330
     }
331 331
 
332 332
     /**
333
-	 * Get GMT date when the discount was created.
334
-	 *
335
-	 * @since 1.0.19
336
-	 * @param  string $context View or edit context.
337
-	 * @return string
338
-	 */
339
-	public function get_date_created_gmt( $context = 'view' ) {
333
+     * Get GMT date when the discount was created.
334
+     *
335
+     * @since 1.0.19
336
+     * @param  string $context View or edit context.
337
+     * @return string
338
+     */
339
+    public function get_date_created_gmt( $context = 'view' ) {
340 340
         $date = $this->get_date_created( $context );
341 341
 
342 342
         if ( $date ) {
343 343
             $date = get_gmt_from_date( $date );
344 344
         }
345
-		return $date;
345
+        return $date;
346 346
     }
347 347
 
348 348
     /**
349
-	 * Get date when the discount was last modified.
350
-	 *
351
-	 * @since 1.0.19
352
-	 * @param  string $context View or edit context.
353
-	 * @return string
354
-	 */
355
-	public function get_date_modified( $context = 'view' ) {
356
-		return $this->get_prop( 'date_modified', $context );
349
+     * Get date when the discount was last modified.
350
+     *
351
+     * @since 1.0.19
352
+     * @param  string $context View or edit context.
353
+     * @return string
354
+     */
355
+    public function get_date_modified( $context = 'view' ) {
356
+        return $this->get_prop( 'date_modified', $context );
357 357
     }
358 358
 
359 359
     /**
360
-	 * Get GMT date when the discount was last modified.
361
-	 *
362
-	 * @since 1.0.19
363
-	 * @param  string $context View or edit context.
364
-	 * @return string
365
-	 */
366
-	public function get_date_modified_gmt( $context = 'view' ) {
360
+     * Get GMT date when the discount was last modified.
361
+     *
362
+     * @since 1.0.19
363
+     * @param  string $context View or edit context.
364
+     * @return string
365
+     */
366
+    public function get_date_modified_gmt( $context = 'view' ) {
367 367
         $date = $this->get_date_modified( $context );
368 368
 
369 369
         if ( $date ) {
370 370
             $date = get_gmt_from_date( $date );
371 371
         }
372
-		return $date;
373
-    }
374
-
375
-    /**
376
-	 * Get the discount name.
377
-	 *
378
-	 * @since 1.0.19
379
-	 * @param  string $context View or edit context.
380
-	 * @return string
381
-	 */
382
-	public function get_name( $context = 'view' ) {
383
-		return $this->get_prop( 'name', $context );
384
-    }
385
-
386
-    /**
387
-	 * Alias of self::get_name().
388
-	 *
389
-	 * @since 1.0.19
390
-	 * @param  string $context View or edit context.
391
-	 * @return string
392
-	 */
393
-	public function get_title( $context = 'view' ) {
394
-		return $this->get_name( $context );
395
-    }
396
-
397
-    /**
398
-	 * Get the discount description.
399
-	 *
400
-	 * @since 1.0.19
401
-	 * @param  string $context View or edit context.
402
-	 * @return string
403
-	 */
404
-	public function get_description( $context = 'view' ) {
405
-		return $this->get_prop( 'description', $context );
406
-    }
407
-
408
-    /**
409
-	 * Alias of self::get_description().
410
-	 *
411
-	 * @since 1.0.19
412
-	 * @param  string $context View or edit context.
413
-	 * @return string
414
-	 */
415
-	public function get_excerpt( $context = 'view' ) {
416
-		return $this->get_description( $context );
417
-    }
418
-
419
-    /**
420
-	 * Alias of self::get_description().
421
-	 *
422
-	 * @since 1.0.19
423
-	 * @param  string $context View or edit context.
424
-	 * @return string
425
-	 */
426
-	public function get_summary( $context = 'view' ) {
427
-		return $this->get_description( $context );
428
-    }
429
-
430
-    /**
431
-	 * Get the owner of the discount.
432
-	 *
433
-	 * @since 1.0.19
434
-	 * @param  string $context View or edit context.
435
-	 * @return string
436
-	 */
437
-	public function get_author( $context = 'view' ) {
438
-		return (int) $this->get_prop( 'author', $context );
439
-	}
440
-
441
-	/**
442
-	 * Get the discount code.
443
-	 *
444
-	 * @since 1.0.19
445
-	 * @param  string $context View or edit context.
446
-	 * @return string
447
-	 */
448
-	public function get_code( $context = 'view' ) {
449
-		return $this->get_prop( 'code', $context );
450
-	}
451
-
452
-	/**
453
-	 * Alias for self::get_code().
454
-	 *
455
-	 * @since 1.0.19
456
-	 * @param  string $context View or edit context.
457
-	 * @return string
458
-	 */
459
-	public function get_coupon_code( $context = 'view' ) {
460
-		return $this->get_code( $context );
461
-	}
462
-
463
-	/**
464
-	 * Alias for self::get_code().
465
-	 *
466
-	 * @since 1.0.19
467
-	 * @param  string $context View or edit context.
468
-	 * @return string
469
-	 */
470
-	public function get_discount_code( $context = 'view' ) {
471
-		return $this->get_code( $context );
472
-	}
473
-
474
-	/**
475
-	 * Get the discount's amount.
476
-	 *
477
-	 * @since 1.0.19
478
-	 * @param  string $context View or edit context.
479
-	 * @return float
480
-	 */
481
-	public function get_amount( $context = 'view' ) {
482
-		return $context == 'view' ? floatval( $this->get_prop( 'amount', $context ) ) : $this->get_prop( 'amount', $context );
483
-	}
484
-
485
-	/**
486
-	 * Get the discount's formated amount/rate.
487
-	 *
488
-	 * @since 1.0.19
489
-	 * @return string
490
-	 */
491
-	public function get_formatted_amount() {
492
-
493
-		if ( $this->is_type( 'flat' ) ) {
494
-			$rate = wpinv_price( $this->get_amount() );
495
-		} else {
496
-			$rate = $this->get_amount() . '%';
497
-		}
498
-
499
-		return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() );
500
-	}
501
-
502
-	/**
503
-	 * Get the discount's start date.
504
-	 *
505
-	 * @since 1.0.19
506
-	 * @param  string $context View or edit context.
507
-	 * @return string
508
-	 */
509
-	public function get_start( $context = 'view' ) {
510
-		return $this->get_prop( 'start', $context );
511
-	}
512
-
513
-	/**
514
-	 * Alias for self::get_start().
515
-	 *
516
-	 * @since 1.0.19
517
-	 * @param  string $context View or edit context.
518
-	 * @return string
519
-	 */
520
-	public function get_start_date( $context = 'view' ) {
521
-		return $this->get_start( $context );
522
-	}
523
-
524
-	/**
525
-	 * Get the discount's expiration date.
526
-	 *
527
-	 * @since 1.0.19
528
-	 * @param  string $context View or edit context.
529
-	 * @return string
530
-	 */
531
-	public function get_expiration( $context = 'view' ) {
532
-		return $this->get_prop( 'expiration', $context );
533
-	}
534
-
535
-	/**
536
-	 * Alias for self::get_expiration().
537
-	 *
538
-	 * @since 1.0.19
539
-	 * @param  string $context View or edit context.
540
-	 * @return string
541
-	 */
542
-	public function get_expiration_date( $context = 'view' ) {
543
-		return $this->get_expiration( $context );
544
-	}
545
-
546
-	/**
547
-	 * Alias for self::get_expiration().
548
-	 *
549
-	 * @since 1.0.19
550
-	 * @param  string $context View or edit context.
551
-	 * @return string
552
-	 */
553
-	public function get_end_date( $context = 'view' ) {
554
-		return $this->get_expiration( $context );
555
-	}
556
-
557
-	/**
558
-	 * Get the discount's type.
559
-	 *
560
-	 * @since 1.0.19
561
-	 * @param  string $context View or edit context.
562
-	 * @return string
563
-	 */
564
-	public function get_type( $context = 'view' ) {
565
-		return $this->get_prop( 'type', $context );
566
-	}
567
-
568
-	/**
569
-	 * Get the number of times a discount has been used.
570
-	 *
571
-	 * @since 1.0.19
572
-	 * @param  string $context View or edit context.
573
-	 * @return int
574
-	 */
575
-	public function get_uses( $context = 'view' ) {
576
-		return (int) $this->get_prop( 'uses', $context );
577
-	}
578
-
579
-	/**
580
-	 * Get the discount's usage, i.e uses / max uses.
581
-	 *
582
-	 * @since 1.0.19
583
-	 * @return string
584
-	 */
585
-	public function get_usage() {
586
-
587
-		if ( ! $this->has_limit() ) {
588
-			return $this->get_uses() . ' / ' . ' &infin;';
589
-		}
590
-
591
-		return $this->get_uses() . ' / ' . (int) $this->get_max_uses();
592
-
593
-	}
594
-
595
-	/**
596
-	 * Get the maximum number of time a discount can be used.
597
-	 *
598
-	 * @since 1.0.19
599
-	 * @param  string $context View or edit context.
600
-	 * @return int
601
-	 */
602
-	public function get_max_uses( $context = 'view' ) {
603
-		$max_uses = $this->get_prop( 'max_uses', $context );
604
-		return empty( $max_uses ) ? null : $max_uses;
605
-	}
606
-
607
-	/**
608
-	 * Checks if this is a single use discount or not.
609
-	 *
610
-	 * @since 1.0.19
611
-	 * @param  string $context View or edit context.
612
-	 * @return bool
613
-	 */
614
-	public function get_is_single_use( $context = 'view' ) {
615
-		return $this->get_prop( 'is_single_use', $context );
616
-	}
617
-
618
-	/**
619
-	 * Get the items that can be used with this discount.
620
-	 *
621
-	 * @since 1.0.19
622
-	 * @param  string $context View or edit context.
623
-	 * @return array
624
-	 */
625
-	public function get_items( $context = 'view' ) {
626
-		return array_filter( wp_parse_id_list( $this->get_prop( 'items', $context ) ) );
627
-	}
628
-
629
-	/**
630
-	 * Alias for self::get_items().
631
-	 *
632
-	 * @since 1.0.19
633
-	 * @param  string $context View or edit context.
634
-	 * @return array
635
-	 */
636
-	public function get_allowed_items( $context = 'view' ) {
637
-		return $this->get_items( $context );
638
-	}
639
-
640
-	/**
641
-	 * Get the items that are not allowed to use this discount.
642
-	 *
643
-	 * @since 1.0.19
644
-	 * @param  string $context View or edit context.
645
-	 * @return array
646
-	 */
647
-	public function get_excluded_items( $context = 'view' ) {
648
-		return array_filter( wp_parse_id_list( $this->get_prop( 'excluded_items', $context ) ) );
649
-	}
650
-
651
-	/**
652
-	 * Get the items that are required to be in the cart before using this discount.
653
-	 *
654
-	 * @since 1.0.19
655
-	 * @param  string $context View or edit context.
656
-	 * @return array
657
-	 */
658
-	public function get_required_items( $context = 'view' ) {
659
-		return array_filter( wp_parse_id_list( $this->get_prop( 'required_items', $context ) ) );
660
-	}
661
-
662
-	/**
663
-	 * Checks if this is a recurring discount or not.
664
-	 *
665
-	 * @since 1.0.19
666
-	 * @param  string $context View or edit context.
667
-	 * @return int|string|bool
668
-	 */
669
-	public function get_is_recurring( $context = 'view' ) {
670
-		return $this->get_prop( 'is_recurring', $context );
671
-	}
672
-
673
-	/**
674
-	 * Get's the minimum total amount allowed for this discount.
675
-	 *
676
-	 * @since 1.0.19
677
-	 * @param  string $context View or edit context.
678
-	 * @return float
679
-	 */
680
-	public function get_min_total( $context = 'view' ) {
681
-		$minimum = $this->get_prop( 'min_total', $context );
682
-		return empty( $minimum ) ? null : $minimum;
683
-	}
684
-
685
-	/**
686
-	 * Alias for self::get_min_total().
687
-	 *
688
-	 * @since 1.0.19
689
-	 * @param  string $context View or edit context.
690
-	 * @return float
691
-	 */
692
-	public function get_minimum_total( $context = 'view' ) {
693
-		return $this->get_min_total( $context );
694
-	}
695
-
696
-	/**
697
-	 * Get's the maximum total amount allowed for this discount.
698
-	 *
699
-	 * @since 1.0.19
700
-	 * @param  string $context View or edit context.
701
-	 * @return float
702
-	 */
703
-	public function get_max_total( $context = 'view' ) {
704
-		$maximum = $this->get_prop( 'max_total', $context );
705
-		return empty( $maximum ) ? null : $maximum;
706
-	}
707
-
708
-	/**
709
-	 * Alias for self::get_max_total().
710
-	 *
711
-	 * @since 1.0.19
712
-	 * @param  string $context View or edit context.
713
-	 * @return float
714
-	 */
715
-	public function get_maximum_total( $context = 'view' ) {
716
-		return $this->get_max_total( $context );
717
-	}
718
-
719
-	/**
720
-	 * Magic method for accessing discount properties.
721
-	 *
722
-	 * @since 1.0.15
723
-	 * @access public
724
-	 *
725
-	 * @param string $key Discount data to retrieve
726
-	 * @param  string $context View or edit context.
727
-	 * @return mixed Value of the given discount property (if set).
728
-	 */
729
-	public function get( $key, $context = 'view' ) {
372
+        return $date;
373
+    }
374
+
375
+    /**
376
+     * Get the discount name.
377
+     *
378
+     * @since 1.0.19
379
+     * @param  string $context View or edit context.
380
+     * @return string
381
+     */
382
+    public function get_name( $context = 'view' ) {
383
+        return $this->get_prop( 'name', $context );
384
+    }
385
+
386
+    /**
387
+     * Alias of self::get_name().
388
+     *
389
+     * @since 1.0.19
390
+     * @param  string $context View or edit context.
391
+     * @return string
392
+     */
393
+    public function get_title( $context = 'view' ) {
394
+        return $this->get_name( $context );
395
+    }
396
+
397
+    /**
398
+     * Get the discount description.
399
+     *
400
+     * @since 1.0.19
401
+     * @param  string $context View or edit context.
402
+     * @return string
403
+     */
404
+    public function get_description( $context = 'view' ) {
405
+        return $this->get_prop( 'description', $context );
406
+    }
407
+
408
+    /**
409
+     * Alias of self::get_description().
410
+     *
411
+     * @since 1.0.19
412
+     * @param  string $context View or edit context.
413
+     * @return string
414
+     */
415
+    public function get_excerpt( $context = 'view' ) {
416
+        return $this->get_description( $context );
417
+    }
418
+
419
+    /**
420
+     * Alias of self::get_description().
421
+     *
422
+     * @since 1.0.19
423
+     * @param  string $context View or edit context.
424
+     * @return string
425
+     */
426
+    public function get_summary( $context = 'view' ) {
427
+        return $this->get_description( $context );
428
+    }
429
+
430
+    /**
431
+     * Get the owner of the discount.
432
+     *
433
+     * @since 1.0.19
434
+     * @param  string $context View or edit context.
435
+     * @return string
436
+     */
437
+    public function get_author( $context = 'view' ) {
438
+        return (int) $this->get_prop( 'author', $context );
439
+    }
440
+
441
+    /**
442
+     * Get the discount code.
443
+     *
444
+     * @since 1.0.19
445
+     * @param  string $context View or edit context.
446
+     * @return string
447
+     */
448
+    public function get_code( $context = 'view' ) {
449
+        return $this->get_prop( 'code', $context );
450
+    }
451
+
452
+    /**
453
+     * Alias for self::get_code().
454
+     *
455
+     * @since 1.0.19
456
+     * @param  string $context View or edit context.
457
+     * @return string
458
+     */
459
+    public function get_coupon_code( $context = 'view' ) {
460
+        return $this->get_code( $context );
461
+    }
462
+
463
+    /**
464
+     * Alias for self::get_code().
465
+     *
466
+     * @since 1.0.19
467
+     * @param  string $context View or edit context.
468
+     * @return string
469
+     */
470
+    public function get_discount_code( $context = 'view' ) {
471
+        return $this->get_code( $context );
472
+    }
473
+
474
+    /**
475
+     * Get the discount's amount.
476
+     *
477
+     * @since 1.0.19
478
+     * @param  string $context View or edit context.
479
+     * @return float
480
+     */
481
+    public function get_amount( $context = 'view' ) {
482
+        return $context == 'view' ? floatval( $this->get_prop( 'amount', $context ) ) : $this->get_prop( 'amount', $context );
483
+    }
484
+
485
+    /**
486
+     * Get the discount's formated amount/rate.
487
+     *
488
+     * @since 1.0.19
489
+     * @return string
490
+     */
491
+    public function get_formatted_amount() {
492
+
493
+        if ( $this->is_type( 'flat' ) ) {
494
+            $rate = wpinv_price( $this->get_amount() );
495
+        } else {
496
+            $rate = $this->get_amount() . '%';
497
+        }
498
+
499
+        return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() );
500
+    }
501
+
502
+    /**
503
+     * Get the discount's start date.
504
+     *
505
+     * @since 1.0.19
506
+     * @param  string $context View or edit context.
507
+     * @return string
508
+     */
509
+    public function get_start( $context = 'view' ) {
510
+        return $this->get_prop( 'start', $context );
511
+    }
512
+
513
+    /**
514
+     * Alias for self::get_start().
515
+     *
516
+     * @since 1.0.19
517
+     * @param  string $context View or edit context.
518
+     * @return string
519
+     */
520
+    public function get_start_date( $context = 'view' ) {
521
+        return $this->get_start( $context );
522
+    }
523
+
524
+    /**
525
+     * Get the discount's expiration date.
526
+     *
527
+     * @since 1.0.19
528
+     * @param  string $context View or edit context.
529
+     * @return string
530
+     */
531
+    public function get_expiration( $context = 'view' ) {
532
+        return $this->get_prop( 'expiration', $context );
533
+    }
534
+
535
+    /**
536
+     * Alias for self::get_expiration().
537
+     *
538
+     * @since 1.0.19
539
+     * @param  string $context View or edit context.
540
+     * @return string
541
+     */
542
+    public function get_expiration_date( $context = 'view' ) {
543
+        return $this->get_expiration( $context );
544
+    }
545
+
546
+    /**
547
+     * Alias for self::get_expiration().
548
+     *
549
+     * @since 1.0.19
550
+     * @param  string $context View or edit context.
551
+     * @return string
552
+     */
553
+    public function get_end_date( $context = 'view' ) {
554
+        return $this->get_expiration( $context );
555
+    }
556
+
557
+    /**
558
+     * Get the discount's type.
559
+     *
560
+     * @since 1.0.19
561
+     * @param  string $context View or edit context.
562
+     * @return string
563
+     */
564
+    public function get_type( $context = 'view' ) {
565
+        return $this->get_prop( 'type', $context );
566
+    }
567
+
568
+    /**
569
+     * Get the number of times a discount has been used.
570
+     *
571
+     * @since 1.0.19
572
+     * @param  string $context View or edit context.
573
+     * @return int
574
+     */
575
+    public function get_uses( $context = 'view' ) {
576
+        return (int) $this->get_prop( 'uses', $context );
577
+    }
578
+
579
+    /**
580
+     * Get the discount's usage, i.e uses / max uses.
581
+     *
582
+     * @since 1.0.19
583
+     * @return string
584
+     */
585
+    public function get_usage() {
586
+
587
+        if ( ! $this->has_limit() ) {
588
+            return $this->get_uses() . ' / ' . ' &infin;';
589
+        }
590
+
591
+        return $this->get_uses() . ' / ' . (int) $this->get_max_uses();
592
+
593
+    }
594
+
595
+    /**
596
+     * Get the maximum number of time a discount can be used.
597
+     *
598
+     * @since 1.0.19
599
+     * @param  string $context View or edit context.
600
+     * @return int
601
+     */
602
+    public function get_max_uses( $context = 'view' ) {
603
+        $max_uses = $this->get_prop( 'max_uses', $context );
604
+        return empty( $max_uses ) ? null : $max_uses;
605
+    }
606
+
607
+    /**
608
+     * Checks if this is a single use discount or not.
609
+     *
610
+     * @since 1.0.19
611
+     * @param  string $context View or edit context.
612
+     * @return bool
613
+     */
614
+    public function get_is_single_use( $context = 'view' ) {
615
+        return $this->get_prop( 'is_single_use', $context );
616
+    }
617
+
618
+    /**
619
+     * Get the items that can be used with this discount.
620
+     *
621
+     * @since 1.0.19
622
+     * @param  string $context View or edit context.
623
+     * @return array
624
+     */
625
+    public function get_items( $context = 'view' ) {
626
+        return array_filter( wp_parse_id_list( $this->get_prop( 'items', $context ) ) );
627
+    }
628
+
629
+    /**
630
+     * Alias for self::get_items().
631
+     *
632
+     * @since 1.0.19
633
+     * @param  string $context View or edit context.
634
+     * @return array
635
+     */
636
+    public function get_allowed_items( $context = 'view' ) {
637
+        return $this->get_items( $context );
638
+    }
639
+
640
+    /**
641
+     * Get the items that are not allowed to use this discount.
642
+     *
643
+     * @since 1.0.19
644
+     * @param  string $context View or edit context.
645
+     * @return array
646
+     */
647
+    public function get_excluded_items( $context = 'view' ) {
648
+        return array_filter( wp_parse_id_list( $this->get_prop( 'excluded_items', $context ) ) );
649
+    }
650
+
651
+    /**
652
+     * Get the items that are required to be in the cart before using this discount.
653
+     *
654
+     * @since 1.0.19
655
+     * @param  string $context View or edit context.
656
+     * @return array
657
+     */
658
+    public function get_required_items( $context = 'view' ) {
659
+        return array_filter( wp_parse_id_list( $this->get_prop( 'required_items', $context ) ) );
660
+    }
661
+
662
+    /**
663
+     * Checks if this is a recurring discount or not.
664
+     *
665
+     * @since 1.0.19
666
+     * @param  string $context View or edit context.
667
+     * @return int|string|bool
668
+     */
669
+    public function get_is_recurring( $context = 'view' ) {
670
+        return $this->get_prop( 'is_recurring', $context );
671
+    }
672
+
673
+    /**
674
+     * Get's the minimum total amount allowed for this discount.
675
+     *
676
+     * @since 1.0.19
677
+     * @param  string $context View or edit context.
678
+     * @return float
679
+     */
680
+    public function get_min_total( $context = 'view' ) {
681
+        $minimum = $this->get_prop( 'min_total', $context );
682
+        return empty( $minimum ) ? null : $minimum;
683
+    }
684
+
685
+    /**
686
+     * Alias for self::get_min_total().
687
+     *
688
+     * @since 1.0.19
689
+     * @param  string $context View or edit context.
690
+     * @return float
691
+     */
692
+    public function get_minimum_total( $context = 'view' ) {
693
+        return $this->get_min_total( $context );
694
+    }
695
+
696
+    /**
697
+     * Get's the maximum total amount allowed for this discount.
698
+     *
699
+     * @since 1.0.19
700
+     * @param  string $context View or edit context.
701
+     * @return float
702
+     */
703
+    public function get_max_total( $context = 'view' ) {
704
+        $maximum = $this->get_prop( 'max_total', $context );
705
+        return empty( $maximum ) ? null : $maximum;
706
+    }
707
+
708
+    /**
709
+     * Alias for self::get_max_total().
710
+     *
711
+     * @since 1.0.19
712
+     * @param  string $context View or edit context.
713
+     * @return float
714
+     */
715
+    public function get_maximum_total( $context = 'view' ) {
716
+        return $this->get_max_total( $context );
717
+    }
718
+
719
+    /**
720
+     * Magic method for accessing discount properties.
721
+     *
722
+     * @since 1.0.15
723
+     * @access public
724
+     *
725
+     * @param string $key Discount data to retrieve
726
+     * @param  string $context View or edit context.
727
+     * @return mixed Value of the given discount property (if set).
728
+     */
729
+    public function get( $key, $context = 'view' ) {
730 730
         return $this->get_prop( $key, $context );
731
-	}
731
+    }
732 732
 
733
-	/*
733
+    /*
734 734
 	|--------------------------------------------------------------------------
735 735
 	| Setters
736 736
 	|--------------------------------------------------------------------------
@@ -740,41 +740,41 @@  discard block
 block discarded – undo
740 740
 	| object.
741 741
 	*/
742 742
 
743
-	/**
744
-	 * Sets discount status.
745
-	 *
746
-	 * @since 1.0.19
747
-	 * @param  string $status New status.
748
-	 * @return array details of change.
749
-	 */
750
-	public function set_status( $status ) {
743
+    /**
744
+     * Sets discount status.
745
+     *
746
+     * @since 1.0.19
747
+     * @param  string $status New status.
748
+     * @return array details of change.
749
+     */
750
+    public function set_status( $status ) {
751 751
         $old_status = $this->get_status();
752 752
 
753 753
         $this->set_prop( 'status', $status );
754 754
 
755
-		return array(
756
-			'from' => $old_status,
757
-			'to'   => $status,
758
-		);
755
+        return array(
756
+            'from' => $old_status,
757
+            'to'   => $status,
758
+        );
759 759
     }
760 760
 
761 761
     /**
762
-	 * Set plugin version when the discount was created.
763
-	 *
764
-	 * @since 1.0.19
765
-	 */
766
-	public function set_version( $value ) {
767
-		$this->set_prop( 'version', $value );
762
+     * Set plugin version when the discount was created.
763
+     *
764
+     * @since 1.0.19
765
+     */
766
+    public function set_version( $value ) {
767
+        $this->set_prop( 'version', $value );
768 768
     }
769 769
 
770 770
     /**
771
-	 * Set date when the discount was created.
772
-	 *
773
-	 * @since 1.0.19
774
-	 * @param string $value Value to set.
771
+     * Set date when the discount was created.
772
+     *
773
+     * @since 1.0.19
774
+     * @param string $value Value to set.
775 775
      * @return bool Whether or not the date was set.
776
-	 */
777
-	public function set_date_created( $value ) {
776
+     */
777
+    public function set_date_created( $value ) {
778 778
         $date = strtotime( $value );
779 779
 
780 780
         if ( $date ) {
@@ -787,13 +787,13 @@  discard block
 block discarded – undo
787 787
     }
788 788
 
789 789
     /**
790
-	 * Set date when the discount was last modified.
791
-	 *
792
-	 * @since 1.0.19
793
-	 * @param string $value Value to set.
790
+     * Set date when the discount was last modified.
791
+     *
792
+     * @since 1.0.19
793
+     * @param string $value Value to set.
794 794
      * @return bool Whether or not the date was set.
795
-	 */
796
-	public function set_date_modified( $value ) {
795
+     */
796
+    public function set_date_modified( $value ) {
797 797
         $date = strtotime( $value );
798 798
 
799 799
         if ( $date ) {
@@ -806,334 +806,334 @@  discard block
 block discarded – undo
806 806
     }
807 807
 
808 808
     /**
809
-	 * Set the discount name.
810
-	 *
811
-	 * @since 1.0.19
812
-	 * @param  string $value New name.
813
-	 */
814
-	public function set_name( $value ) {
809
+     * Set the discount name.
810
+     *
811
+     * @since 1.0.19
812
+     * @param  string $value New name.
813
+     */
814
+    public function set_name( $value ) {
815 815
         $name = sanitize_text_field( $value );
816
-		$this->set_prop( 'name', $name );
816
+        $this->set_prop( 'name', $name );
817 817
     }
818 818
 
819 819
     /**
820
-	 * Alias of self::set_name().
821
-	 *
822
-	 * @since 1.0.19
823
-	 * @param  string $value New name.
824
-	 */
825
-	public function set_title( $value ) {
826
-		$this->set_name( $value );
820
+     * Alias of self::set_name().
821
+     *
822
+     * @since 1.0.19
823
+     * @param  string $value New name.
824
+     */
825
+    public function set_title( $value ) {
826
+        $this->set_name( $value );
827 827
     }
828 828
 
829 829
     /**
830
-	 * Set the discount description.
831
-	 *
832
-	 * @since 1.0.19
833
-	 * @param  string $value New description.
834
-	 */
835
-	public function set_description( $value ) {
830
+     * Set the discount description.
831
+     *
832
+     * @since 1.0.19
833
+     * @param  string $value New description.
834
+     */
835
+    public function set_description( $value ) {
836 836
         $description = wp_kses_post( $value );
837
-		return $this->set_prop( 'description', $description );
838
-    }
839
-
840
-    /**
841
-	 * Alias of self::set_description().
842
-	 *
843
-	 * @since 1.0.19
844
-	 * @param  string $value New description.
845
-	 */
846
-	public function set_excerpt( $value ) {
847
-		$this->set_description( $value );
848
-    }
849
-
850
-    /**
851
-	 * Alias of self::set_description().
852
-	 *
853
-	 * @since 1.0.19
854
-	 * @param  string $value New description.
855
-	 */
856
-	public function set_summary( $value ) {
857
-		$this->set_description( $value );
858
-    }
859
-
860
-    /**
861
-	 * Set the owner of the discount.
862
-	 *
863
-	 * @since 1.0.19
864
-	 * @param  int $value New author.
865
-	 */
866
-	public function set_author( $value ) {
867
-		$this->set_prop( 'author', (int) $value );
868
-	}
869
-
870
-	/**
871
-	 * Sets the discount code.
872
-	 *
873
-	 * @since 1.0.19
874
-	 * @param string $value New discount code.
875
-	 */
876
-	public function set_code( $value ) {
877
-		$code = sanitize_text_field( $value );
878
-		$this->set_prop( 'code', $code );
879
-	}
880
-
881
-	/**
882
-	 * Alias of self::set_code().
883
-	 *
884
-	 * @since 1.0.19
885
-	 * @param string $value New discount code.
886
-	 */
887
-	public function set_coupon_code( $value ) {
888
-		$this->set_code( $value );
889
-	}
890
-
891
-	/**
892
-	 * Alias of self::set_code().
893
-	 *
894
-	 * @since 1.0.19
895
-	 * @param string $value New discount code.
896
-	 */
897
-	public function set_discount_code( $value ) {
898
-		$this->set_code( $value );
899
-	}
900
-
901
-	/**
902
-	 * Sets the discount amount.
903
-	 *
904
-	 * @since 1.0.19
905
-	 * @param float $value New discount code.
906
-	 */
907
-	public function set_amount( $value ) {
908
-		$amount = floatval( wpinv_sanitize_amount( $value ) );
909
-		$this->set_prop( 'amount', $amount );
910
-	}
911
-
912
-	/**
913
-	 * Sets the discount's start date.
914
-	 *
915
-	 * @since 1.0.19
916
-	 * @param float $value New start date.
917
-	 */
918
-	public function set_start( $value ) {
919
-		$date = strtotime( $value );
837
+        return $this->set_prop( 'description', $description );
838
+    }
839
+
840
+    /**
841
+     * Alias of self::set_description().
842
+     *
843
+     * @since 1.0.19
844
+     * @param  string $value New description.
845
+     */
846
+    public function set_excerpt( $value ) {
847
+        $this->set_description( $value );
848
+    }
849
+
850
+    /**
851
+     * Alias of self::set_description().
852
+     *
853
+     * @since 1.0.19
854
+     * @param  string $value New description.
855
+     */
856
+    public function set_summary( $value ) {
857
+        $this->set_description( $value );
858
+    }
859
+
860
+    /**
861
+     * Set the owner of the discount.
862
+     *
863
+     * @since 1.0.19
864
+     * @param  int $value New author.
865
+     */
866
+    public function set_author( $value ) {
867
+        $this->set_prop( 'author', (int) $value );
868
+    }
869
+
870
+    /**
871
+     * Sets the discount code.
872
+     *
873
+     * @since 1.0.19
874
+     * @param string $value New discount code.
875
+     */
876
+    public function set_code( $value ) {
877
+        $code = sanitize_text_field( $value );
878
+        $this->set_prop( 'code', $code );
879
+    }
880
+
881
+    /**
882
+     * Alias of self::set_code().
883
+     *
884
+     * @since 1.0.19
885
+     * @param string $value New discount code.
886
+     */
887
+    public function set_coupon_code( $value ) {
888
+        $this->set_code( $value );
889
+    }
890
+
891
+    /**
892
+     * Alias of self::set_code().
893
+     *
894
+     * @since 1.0.19
895
+     * @param string $value New discount code.
896
+     */
897
+    public function set_discount_code( $value ) {
898
+        $this->set_code( $value );
899
+    }
900
+
901
+    /**
902
+     * Sets the discount amount.
903
+     *
904
+     * @since 1.0.19
905
+     * @param float $value New discount code.
906
+     */
907
+    public function set_amount( $value ) {
908
+        $amount = floatval( wpinv_sanitize_amount( $value ) );
909
+        $this->set_prop( 'amount', $amount );
910
+    }
911
+
912
+    /**
913
+     * Sets the discount's start date.
914
+     *
915
+     * @since 1.0.19
916
+     * @param float $value New start date.
917
+     */
918
+    public function set_start( $value ) {
919
+        $date = strtotime( $value );
920 920
 
921 921
         if ( $date ) {
922 922
             $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) );
923 923
             return true;
924
-		}
924
+        }
925 925
 
926
-		$this->set_prop( 'start', '' );
926
+        $this->set_prop( 'start', '' );
927 927
 
928 928
         return false;
929
-	}
930
-
931
-	/**
932
-	 * Alias of self::set_start().
933
-	 *
934
-	 * @since 1.0.19
935
-	 * @param string $value New start date.
936
-	 */
937
-	public function set_start_date( $value ) {
938
-		$this->set_start( $value );
939
-	}
940
-
941
-	/**
942
-	 * Sets the discount's expiration date.
943
-	 *
944
-	 * @since 1.0.19
945
-	 * @param float $value New expiration date.
946
-	 */
947
-	public function set_expiration( $value ) {
948
-		$date = strtotime( $value );
929
+    }
930
+
931
+    /**
932
+     * Alias of self::set_start().
933
+     *
934
+     * @since 1.0.19
935
+     * @param string $value New start date.
936
+     */
937
+    public function set_start_date( $value ) {
938
+        $this->set_start( $value );
939
+    }
940
+
941
+    /**
942
+     * Sets the discount's expiration date.
943
+     *
944
+     * @since 1.0.19
945
+     * @param float $value New expiration date.
946
+     */
947
+    public function set_expiration( $value ) {
948
+        $date = strtotime( $value );
949 949
 
950 950
         if ( $date ) {
951 951
             $this->set_prop( 'expiration', date( 'Y-m-d H:i', $date ) );
952 952
             return true;
953 953
         }
954 954
 
955
-		$this->set_prop( 'expiration', '' );
955
+        $this->set_prop( 'expiration', '' );
956 956
         return false;
957
-	}
958
-
959
-	/**
960
-	 * Alias of self::set_expiration().
961
-	 *
962
-	 * @since 1.0.19
963
-	 * @param string $value New expiration date.
964
-	 */
965
-	public function set_expiration_date( $value ) {
966
-		$this->set_expiration( $value );
967
-	}
968
-
969
-	/**
970
-	 * Alias of self::set_expiration().
971
-	 *
972
-	 * @since 1.0.19
973
-	 * @param string $value New expiration date.
974
-	 */
975
-	public function set_end_date( $value ) {
976
-		$this->set_expiration( $value );
977
-	}
978
-
979
-	/**
980
-	 * Sets the discount type.
981
-	 *
982
-	 * @since 1.0.19
983
-	 * @param string $value New discount type.
984
-	 */
985
-	public function set_type( $value ) {
986
-		if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) {
987
-			$this->set_prop( 'type', sanitize_text_field( $value ) );
988
-		}
989
-	}
990
-
991
-	/**
992
-	 * Sets the number of times a discount has been used.
993
-	 *
994
-	 * @since 1.0.19
995
-	 * @param int $value usage count.
996
-	 */
997
-	public function set_uses( $value ) {
998
-
999
-		$value = (int) $value;
1000
-
1001
-		if ( $value < 0 ) {
1002
-			$value = 0;
1003
-		}
1004
-
1005
-		$this->set_prop( 'uses', (int) $value );
1006
-	}
1007
-
1008
-	/**
1009
-	 * Sets the maximum number of times a discount can be used.
1010
-	 *
1011
-	 * @since 1.0.19
1012
-	 * @param int $value maximum usage count.
1013
-	 */
1014
-	public function set_max_uses( $value ) {
1015
-		$this->set_prop( 'max_uses', absint( $value ) );
1016
-	}
1017
-
1018
-	/**
1019
-	 * Sets if this is a single use discount or not.
1020
-	 *
1021
-	 * @since 1.0.19
1022
-	 * @param int|bool $value is single use.
1023
-	 */
1024
-	public function set_is_single_use( $value ) {
1025
-		$this->set_prop( 'is_single_use', (bool) $value );
1026
-	}
1027
-
1028
-	/**
1029
-	 * Sets the items that can be used with this discount.
1030
-	 *
1031
-	 * @since 1.0.19
1032
-	 * @param array $value items.
1033
-	 */
1034
-	public function set_items( $value ) {
1035
-		$this->set_prop( 'items', array_filter( wp_parse_id_list( $value ) ) );
1036
-	}
1037
-
1038
-	/**
1039
-	 * Alias for self::set_items().
1040
-	 *
1041
-	 * @since 1.0.19
1042
-	 * @param array $value items.
1043
-	 */
1044
-	public function set_allowed_items( $value ) {
1045
-		$this->set_items( $value );
1046
-	}
1047
-
1048
-	/**
1049
-	 * Sets the items that can not be used with this discount.
1050
-	 *
1051
-	 * @since 1.0.19
1052
-	 * @param array $value items.
1053
-	 */
1054
-	public function set_excluded_items( $value ) {
1055
-		$this->set_prop( 'excluded_items', array_filter( wp_parse_id_list( $value ) ) );
1056
-	}
1057
-
1058
-	/**
1059
-	 * Sets the items that are required to be in the cart before using this discount.
1060
-	 *
1061
-	 * @since 1.0.19
1062
-	 * @param array $value items.
1063
-	 */
1064
-	public function set_required_items( $value ) {
1065
-		$this->set_prop( 'required_items', array_filter( wp_parse_id_list( $value ) ) );
1066
-	}
1067
-
1068
-	/**
1069
-	 * Sets if this is a recurring discounts or not.
1070
-	 *
1071
-	 * @since 1.0.19
1072
-	 * @param int|bool $value is recurring.
1073
-	 */
1074
-	public function set_is_recurring( $value ) {
1075
-		$this->set_prop( 'is_recurring', (bool) $value );
1076
-	}
1077
-
1078
-	/**
1079
-	 * Sets the minimum total that can not be used with this discount.
1080
-	 *
1081
-	 * @since 1.0.19
1082
-	 * @param float $value minimum total.
1083
-	 */
1084
-	public function set_min_total( $value ) {
1085
-		$this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) );
1086
-	}
1087
-
1088
-	/**
1089
-	 * Alias for self::set_min_total().
1090
-	 *
1091
-	 * @since 1.0.19
1092
-	 * @param float $value minimum total.
1093
-	 */
1094
-	public function set_minimum_total( $value ) {
1095
-		$this->set_min_total( $value );
1096
-	}
1097
-
1098
-	/**
1099
-	 * Sets the maximum total that can not be used with this discount.
1100
-	 *
1101
-	 * @since 1.0.19
1102
-	 * @param float $value maximum total.
1103
-	 */
1104
-	public function set_max_total( $value ) {
1105
-		$this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) );
1106
-	}
1107
-
1108
-	/**
1109
-	 * Alias for self::set_max_total().
1110
-	 *
1111
-	 * @since 1.0.19
1112
-	 * @param float $value maximum total.
1113
-	 */
1114
-	public function set_maximum_total( $value ) {
1115
-		$this->set_max_total( $value );
1116
-	}
1117
-
1118
-	/**
1119
-	 * @deprecated
1120
-	 */
1121
-	public function refresh(){}
1122
-
1123
-	/**
1124
-	 * @deprecated
1125
-	 *
1126
-	 */
1127
-	public function update_status( $status = 'publish' ){
1128
-
1129
-		if ( $this->exists() && $this->get_status() != $status ) {
1130
-			$this->set_status( $status );
1131
-			$this->save();
1132
-		}
1133
-
1134
-	}
1135
-
1136
-	/*
957
+    }
958
+
959
+    /**
960
+     * Alias of self::set_expiration().
961
+     *
962
+     * @since 1.0.19
963
+     * @param string $value New expiration date.
964
+     */
965
+    public function set_expiration_date( $value ) {
966
+        $this->set_expiration( $value );
967
+    }
968
+
969
+    /**
970
+     * Alias of self::set_expiration().
971
+     *
972
+     * @since 1.0.19
973
+     * @param string $value New expiration date.
974
+     */
975
+    public function set_end_date( $value ) {
976
+        $this->set_expiration( $value );
977
+    }
978
+
979
+    /**
980
+     * Sets the discount type.
981
+     *
982
+     * @since 1.0.19
983
+     * @param string $value New discount type.
984
+     */
985
+    public function set_type( $value ) {
986
+        if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) {
987
+            $this->set_prop( 'type', sanitize_text_field( $value ) );
988
+        }
989
+    }
990
+
991
+    /**
992
+     * Sets the number of times a discount has been used.
993
+     *
994
+     * @since 1.0.19
995
+     * @param int $value usage count.
996
+     */
997
+    public function set_uses( $value ) {
998
+
999
+        $value = (int) $value;
1000
+
1001
+        if ( $value < 0 ) {
1002
+            $value = 0;
1003
+        }
1004
+
1005
+        $this->set_prop( 'uses', (int) $value );
1006
+    }
1007
+
1008
+    /**
1009
+     * Sets the maximum number of times a discount can be used.
1010
+     *
1011
+     * @since 1.0.19
1012
+     * @param int $value maximum usage count.
1013
+     */
1014
+    public function set_max_uses( $value ) {
1015
+        $this->set_prop( 'max_uses', absint( $value ) );
1016
+    }
1017
+
1018
+    /**
1019
+     * Sets if this is a single use discount or not.
1020
+     *
1021
+     * @since 1.0.19
1022
+     * @param int|bool $value is single use.
1023
+     */
1024
+    public function set_is_single_use( $value ) {
1025
+        $this->set_prop( 'is_single_use', (bool) $value );
1026
+    }
1027
+
1028
+    /**
1029
+     * Sets the items that can be used with this discount.
1030
+     *
1031
+     * @since 1.0.19
1032
+     * @param array $value items.
1033
+     */
1034
+    public function set_items( $value ) {
1035
+        $this->set_prop( 'items', array_filter( wp_parse_id_list( $value ) ) );
1036
+    }
1037
+
1038
+    /**
1039
+     * Alias for self::set_items().
1040
+     *
1041
+     * @since 1.0.19
1042
+     * @param array $value items.
1043
+     */
1044
+    public function set_allowed_items( $value ) {
1045
+        $this->set_items( $value );
1046
+    }
1047
+
1048
+    /**
1049
+     * Sets the items that can not be used with this discount.
1050
+     *
1051
+     * @since 1.0.19
1052
+     * @param array $value items.
1053
+     */
1054
+    public function set_excluded_items( $value ) {
1055
+        $this->set_prop( 'excluded_items', array_filter( wp_parse_id_list( $value ) ) );
1056
+    }
1057
+
1058
+    /**
1059
+     * Sets the items that are required to be in the cart before using this discount.
1060
+     *
1061
+     * @since 1.0.19
1062
+     * @param array $value items.
1063
+     */
1064
+    public function set_required_items( $value ) {
1065
+        $this->set_prop( 'required_items', array_filter( wp_parse_id_list( $value ) ) );
1066
+    }
1067
+
1068
+    /**
1069
+     * Sets if this is a recurring discounts or not.
1070
+     *
1071
+     * @since 1.0.19
1072
+     * @param int|bool $value is recurring.
1073
+     */
1074
+    public function set_is_recurring( $value ) {
1075
+        $this->set_prop( 'is_recurring', (bool) $value );
1076
+    }
1077
+
1078
+    /**
1079
+     * Sets the minimum total that can not be used with this discount.
1080
+     *
1081
+     * @since 1.0.19
1082
+     * @param float $value minimum total.
1083
+     */
1084
+    public function set_min_total( $value ) {
1085
+        $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) );
1086
+    }
1087
+
1088
+    /**
1089
+     * Alias for self::set_min_total().
1090
+     *
1091
+     * @since 1.0.19
1092
+     * @param float $value minimum total.
1093
+     */
1094
+    public function set_minimum_total( $value ) {
1095
+        $this->set_min_total( $value );
1096
+    }
1097
+
1098
+    /**
1099
+     * Sets the maximum total that can not be used with this discount.
1100
+     *
1101
+     * @since 1.0.19
1102
+     * @param float $value maximum total.
1103
+     */
1104
+    public function set_max_total( $value ) {
1105
+        $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) );
1106
+    }
1107
+
1108
+    /**
1109
+     * Alias for self::set_max_total().
1110
+     *
1111
+     * @since 1.0.19
1112
+     * @param float $value maximum total.
1113
+     */
1114
+    public function set_maximum_total( $value ) {
1115
+        $this->set_max_total( $value );
1116
+    }
1117
+
1118
+    /**
1119
+     * @deprecated
1120
+     */
1121
+    public function refresh(){}
1122
+
1123
+    /**
1124
+     * @deprecated
1125
+     *
1126
+     */
1127
+    public function update_status( $status = 'publish' ){
1128
+
1129
+        if ( $this->exists() && $this->get_status() != $status ) {
1130
+            $this->set_status( $status );
1131
+            $this->save();
1132
+        }
1133
+
1134
+    }
1135
+
1136
+    /*
1137 1137
 	|--------------------------------------------------------------------------
1138 1138
 	| Conditionals
1139 1139
 	|--------------------------------------------------------------------------
@@ -1142,290 +1142,290 @@  discard block
 block discarded – undo
1142 1142
 	|
1143 1143
 	*/
1144 1144
 
1145
-	/**
1146
-	 * Checks whether a discount exists in the database or not
1147
-	 *
1148
-	 * @since 1.0.15
1149
-	 */
1150
-	public function exists(){
1151
-		$id = $this->get_id();
1152
-		return ! empty( $id );
1153
-	}
1154
-
1155
-	/**
1156
-	 * Checks the discount type.
1157
-	 *
1158
-	 *
1159
-	 * @param  string $type the discount type to check against
1160
-	 * @since 1.0.15
1161
-	 * @return bool
1162
-	 */
1163
-	public function is_type( $type ) {
1164
-		return $this->get_type() == $type;
1165
-	}
1166
-
1167
-	/**
1168
-	 * Checks whether the discount is published or not
1169
-	 *
1170
-	 * @since 1.0.15
1171
-	 * @return bool
1172
-	 */
1173
-	public function is_active() {
1174
-		return $this->get_status() == 'publish';
1175
-	}
1176
-
1177
-	/**
1178
-	 * Checks whether the discount has max uses
1179
-	 *
1180
-	 * @since 1.0.15
1181
-	 * @return bool
1182
-	 */
1183
-	public function has_limit() {
1184
-		$limit = $this->get_max_uses();
1185
-		return ! empty( $limit );
1186
-	}
1187
-
1188
-	/**
1189
-	 * Checks whether the discount has ever been used.
1190
-	 *
1191
-	 * @since 1.0.15
1192
-	 * @return bool
1193
-	 */
1194
-	public function has_uses() {
1195
-		return $this->get_uses() > 0;
1196
-	}
1197
-
1198
-	/**
1199
-	 * Checks whether the discount is has exided the usage limit or not
1200
-	 *
1201
-	 * @since 1.0.15
1202
-	 * @return bool
1203
-	 */
1204
-	public function has_exceeded_limit() {
1205
-
1206
-		if ( ! $this->has_limit() || ! $this->has_uses() ) {
1207
-			$exceeded = false ;
1208
-		} else {
1209
-			$exceeded = (int) $this->get_max_uses() <= $this->get_uses();
1210
-		}
1211
-
1212
-		return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() );
1213
-	}
1214
-
1215
-	/**
1216
-	 * Checks whether the discount has an expiration date.
1217
-	 *
1218
-	 * @since 1.0.15
1219
-	 * @return bool
1220
-	 */
1221
-	public function has_expiration_date() {
1222
-		$date = $this->get_expiration_date();
1223
-		return ! empty( $date );
1224
-	}
1225
-
1226
-	/**
1227
-	 * Checks if the discount is expired
1228
-	 *
1229
-	 * @since 1.0.15
1230
-	 * @return bool
1231
-	 */
1232
-	public function is_expired() {
1233
-		$expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false;
1234
-		return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() );
1235
-	}
1236
-
1237
-	/**
1238
-	 * Checks whether the discount has a start date.
1239
-	 *
1240
-	 * @since 1.0.15
1241
-	 * @return bool
1242
-	 */
1243
-	public function has_start_date() {
1244
-		$date = $this->get_start_date();
1245
-		return ! empty( $date );
1246
-	}
1247
-
1248
-	/**
1249
-	 * Checks the discount start date.
1250
-	 *
1251
-	 * @since 1.0.15
1252
-	 * @return bool
1253
-	 */
1254
-	public function has_started() {
1255
-		$started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() );
1256
-		return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() );
1257
-	}
1258
-
1259
-	/**
1260
-	 * Checks the discount has allowed items or not.
1261
-	 *
1262
-	 * @since 1.0.15
1263
-	 * @return bool
1264
-	 */
1265
-	public function has_allowed_items() {
1266
-		$allowed_items = $this->get_allowed_items();
1267
-		return ! empty( $allowed_items );
1268
-	}
1269
-
1270
-	/**
1271
-	 * Checks the discount has excluded items or not.
1272
-	 *
1273
-	 * @since 1.0.15
1274
-	 * @return bool
1275
-	 */
1276
-	public function has_excluded_items() {
1277
-		$excluded_items = $this->get_excluded_items();
1278
-		return ! empty( $excluded_items );
1279
-	}
1280
-
1281
-	/**
1282
-	 * Check if a discount is valid for a given item id.
1283
-	 *
1284
-	 * @param  int|int[]  $item_ids
1285
-	 * @since 1.0.15
1286
-	 * @return boolean
1287
-	 */
1288
-	public function is_valid_for_items( $item_ids ) {
1289
-
1290
-		$item_ids = array_filter( wp_parse_id_list( $item_ids ) );
1291
-		$included = array_intersect( $item_ids, $this->get_allowed_items() );
1292
-		$excluded = array_intersect( $item_ids, $this->get_excluded_items() );
1293
-
1294
-		if ( $this->has_excluded_items() && ! empty( $excluded ) ) {
1295
-			return false;
1296
-		}
1297
-
1298
-		if ( $this->has_allowed_items() && empty( $included ) ) {
1299
-			return false;
1300
-		}
1301
-
1302
-		return true;
1303
-	}
1304
-
1305
-	/**
1306
-	 * Checks the discount has required items or not.
1307
-	 *
1308
-	 * @since 1.0.15
1309
-	 * @return bool
1310
-	 */
1311
-	public function has_required_items() {
1312
-		$required_items = $this->get_required_items();
1313
-		return ! empty( $required_items );
1314
-	}
1315
-
1316
-	/**
1317
-	 * Checks if the required items are met
1318
-	 *
1319
-	 * @param  int|int[]  $item_ids
1320
-	 * @since 1.0.15
1321
-	 * @return boolean
1322
-	 */
1323
-	public function is_required_items_met( $item_ids ) {
1324
-
1325
-		if ( ! $this->has_required_items() ) {
1326
-			return true;
1327
-		}
1328
-
1329
-		return ! array_diff( $this->get_required_items(), array_filter( wp_parse_id_list( $item_ids ) ) );
1330
-	}
1331
-
1332
-	/**
1333
-	 * Check if a discount is valid for the given amount
1334
-	 *
1335
-	 * @param  float  $amount The amount to check against
1336
-	 * @since 1.0.15
1337
-	 * @return boolean
1338
-	 */
1339
-	public function is_valid_for_amount( $amount ) {
1340
-		return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount );
1341
-	}
1342
-
1343
-	/**
1344
-	 * Checks if the minimum amount is set
1345
-	 *
1346
-	 * @since 1.0.15
1347
-	 * @return boolean
1348
-	 */
1349
-	public function has_minimum_amount() {
1350
-		$minimum = $this->get_minimum_total();
1351
-		return ! empty( $minimum );
1352
-	}
1353
-
1354
-	/**
1355
-	 * Checks if the minimum amount is met
1356
-	 *
1357
-	 * @param  float  $amount The amount to check against
1358
-	 * @since 1.0.15
1359
-	 * @return boolean
1360
-	 */
1361
-	public function is_minimum_amount_met( $amount ) {
1362
-		$amount = floatval( wpinv_sanitize_amount( $amount ) );
1363
-		$min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) );
1364
-		return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount );
1365
-	}
1366
-
1367
-	/**
1368
-	 * Checks if the maximum amount is set
1369
-	 *
1370
-	 * @since 1.0.15
1371
-	 * @return boolean
1372
-	 */
1373
-	public function has_maximum_amount() {
1374
-		$maximum = $this->get_maximum_total();
1375
-		return ! empty( $maximum );
1376
-	}
1377
-
1378
-	/**
1379
-	 * Checks if the maximum amount is met
1380
-	 *
1381
-	 * @param  float  $amount The amount to check against
1382
-	 * @since 1.0.15
1383
-	 * @return boolean
1384
-	 */
1385
-	public function is_maximum_amount_met( $amount ) {
1386
-		$amount = floatval( wpinv_sanitize_amount( $amount ) );
1387
-		$max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) );
1388
-		return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount );
1389
-	}
1390
-
1391
-	/**
1392
-	 * Checks if the discount is recurring.
1393
-	 *
1394
-	 * @since 1.0.15
1395
-	 * @return boolean
1396
-	 */
1397
-	public function is_recurring() {
1398
-		$recurring = $this->get_is_recurring();
1399
-		return ! empty( $recurring );
1400
-	}
1401
-
1402
-	/**
1403
-	 * Checks if the discount is single use.
1404
-	 *
1405
-	 * @since 1.0.15
1406
-	 * @return boolean
1407
-	 */
1408
-	public function is_single_use() {
1409
-		$usage = $this->get_is_single_use();
1410
-		return ! empty( $usage );
1411
-	}
1412
-
1413
-	/**
1414
-	 * Check if a discount is valid for the given user
1415
-	 *
1416
-	 * @param  int|string  $user
1417
-	 * @since 1.0.15
1418
-	 * @return boolean
1419
-	 */
1420
-	public function is_valid_for_user( $user ) {
1421
-
1422
-		// Ensure that the discount is single use.
1423
-		if ( empty( $user ) || ! $this->is_single_use() ) {
1424
-			return true;
1425
-		}
1426
-
1427
-		// Prepare the user id.
1428
-		$user_id = 0;
1145
+    /**
1146
+     * Checks whether a discount exists in the database or not
1147
+     *
1148
+     * @since 1.0.15
1149
+     */
1150
+    public function exists(){
1151
+        $id = $this->get_id();
1152
+        return ! empty( $id );
1153
+    }
1154
+
1155
+    /**
1156
+     * Checks the discount type.
1157
+     *
1158
+     *
1159
+     * @param  string $type the discount type to check against
1160
+     * @since 1.0.15
1161
+     * @return bool
1162
+     */
1163
+    public function is_type( $type ) {
1164
+        return $this->get_type() == $type;
1165
+    }
1166
+
1167
+    /**
1168
+     * Checks whether the discount is published or not
1169
+     *
1170
+     * @since 1.0.15
1171
+     * @return bool
1172
+     */
1173
+    public function is_active() {
1174
+        return $this->get_status() == 'publish';
1175
+    }
1176
+
1177
+    /**
1178
+     * Checks whether the discount has max uses
1179
+     *
1180
+     * @since 1.0.15
1181
+     * @return bool
1182
+     */
1183
+    public function has_limit() {
1184
+        $limit = $this->get_max_uses();
1185
+        return ! empty( $limit );
1186
+    }
1187
+
1188
+    /**
1189
+     * Checks whether the discount has ever been used.
1190
+     *
1191
+     * @since 1.0.15
1192
+     * @return bool
1193
+     */
1194
+    public function has_uses() {
1195
+        return $this->get_uses() > 0;
1196
+    }
1197
+
1198
+    /**
1199
+     * Checks whether the discount is has exided the usage limit or not
1200
+     *
1201
+     * @since 1.0.15
1202
+     * @return bool
1203
+     */
1204
+    public function has_exceeded_limit() {
1205
+
1206
+        if ( ! $this->has_limit() || ! $this->has_uses() ) {
1207
+            $exceeded = false ;
1208
+        } else {
1209
+            $exceeded = (int) $this->get_max_uses() <= $this->get_uses();
1210
+        }
1211
+
1212
+        return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() );
1213
+    }
1214
+
1215
+    /**
1216
+     * Checks whether the discount has an expiration date.
1217
+     *
1218
+     * @since 1.0.15
1219
+     * @return bool
1220
+     */
1221
+    public function has_expiration_date() {
1222
+        $date = $this->get_expiration_date();
1223
+        return ! empty( $date );
1224
+    }
1225
+
1226
+    /**
1227
+     * Checks if the discount is expired
1228
+     *
1229
+     * @since 1.0.15
1230
+     * @return bool
1231
+     */
1232
+    public function is_expired() {
1233
+        $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false;
1234
+        return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() );
1235
+    }
1236
+
1237
+    /**
1238
+     * Checks whether the discount has a start date.
1239
+     *
1240
+     * @since 1.0.15
1241
+     * @return bool
1242
+     */
1243
+    public function has_start_date() {
1244
+        $date = $this->get_start_date();
1245
+        return ! empty( $date );
1246
+    }
1247
+
1248
+    /**
1249
+     * Checks the discount start date.
1250
+     *
1251
+     * @since 1.0.15
1252
+     * @return bool
1253
+     */
1254
+    public function has_started() {
1255
+        $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() );
1256
+        return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() );
1257
+    }
1258
+
1259
+    /**
1260
+     * Checks the discount has allowed items or not.
1261
+     *
1262
+     * @since 1.0.15
1263
+     * @return bool
1264
+     */
1265
+    public function has_allowed_items() {
1266
+        $allowed_items = $this->get_allowed_items();
1267
+        return ! empty( $allowed_items );
1268
+    }
1269
+
1270
+    /**
1271
+     * Checks the discount has excluded items or not.
1272
+     *
1273
+     * @since 1.0.15
1274
+     * @return bool
1275
+     */
1276
+    public function has_excluded_items() {
1277
+        $excluded_items = $this->get_excluded_items();
1278
+        return ! empty( $excluded_items );
1279
+    }
1280
+
1281
+    /**
1282
+     * Check if a discount is valid for a given item id.
1283
+     *
1284
+     * @param  int|int[]  $item_ids
1285
+     * @since 1.0.15
1286
+     * @return boolean
1287
+     */
1288
+    public function is_valid_for_items( $item_ids ) {
1289
+
1290
+        $item_ids = array_filter( wp_parse_id_list( $item_ids ) );
1291
+        $included = array_intersect( $item_ids, $this->get_allowed_items() );
1292
+        $excluded = array_intersect( $item_ids, $this->get_excluded_items() );
1293
+
1294
+        if ( $this->has_excluded_items() && ! empty( $excluded ) ) {
1295
+            return false;
1296
+        }
1297
+
1298
+        if ( $this->has_allowed_items() && empty( $included ) ) {
1299
+            return false;
1300
+        }
1301
+
1302
+        return true;
1303
+    }
1304
+
1305
+    /**
1306
+     * Checks the discount has required items or not.
1307
+     *
1308
+     * @since 1.0.15
1309
+     * @return bool
1310
+     */
1311
+    public function has_required_items() {
1312
+        $required_items = $this->get_required_items();
1313
+        return ! empty( $required_items );
1314
+    }
1315
+
1316
+    /**
1317
+     * Checks if the required items are met
1318
+     *
1319
+     * @param  int|int[]  $item_ids
1320
+     * @since 1.0.15
1321
+     * @return boolean
1322
+     */
1323
+    public function is_required_items_met( $item_ids ) {
1324
+
1325
+        if ( ! $this->has_required_items() ) {
1326
+            return true;
1327
+        }
1328
+
1329
+        return ! array_diff( $this->get_required_items(), array_filter( wp_parse_id_list( $item_ids ) ) );
1330
+    }
1331
+
1332
+    /**
1333
+     * Check if a discount is valid for the given amount
1334
+     *
1335
+     * @param  float  $amount The amount to check against
1336
+     * @since 1.0.15
1337
+     * @return boolean
1338
+     */
1339
+    public function is_valid_for_amount( $amount ) {
1340
+        return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount );
1341
+    }
1342
+
1343
+    /**
1344
+     * Checks if the minimum amount is set
1345
+     *
1346
+     * @since 1.0.15
1347
+     * @return boolean
1348
+     */
1349
+    public function has_minimum_amount() {
1350
+        $minimum = $this->get_minimum_total();
1351
+        return ! empty( $minimum );
1352
+    }
1353
+
1354
+    /**
1355
+     * Checks if the minimum amount is met
1356
+     *
1357
+     * @param  float  $amount The amount to check against
1358
+     * @since 1.0.15
1359
+     * @return boolean
1360
+     */
1361
+    public function is_minimum_amount_met( $amount ) {
1362
+        $amount = floatval( wpinv_sanitize_amount( $amount ) );
1363
+        $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) );
1364
+        return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount );
1365
+    }
1366
+
1367
+    /**
1368
+     * Checks if the maximum amount is set
1369
+     *
1370
+     * @since 1.0.15
1371
+     * @return boolean
1372
+     */
1373
+    public function has_maximum_amount() {
1374
+        $maximum = $this->get_maximum_total();
1375
+        return ! empty( $maximum );
1376
+    }
1377
+
1378
+    /**
1379
+     * Checks if the maximum amount is met
1380
+     *
1381
+     * @param  float  $amount The amount to check against
1382
+     * @since 1.0.15
1383
+     * @return boolean
1384
+     */
1385
+    public function is_maximum_amount_met( $amount ) {
1386
+        $amount = floatval( wpinv_sanitize_amount( $amount ) );
1387
+        $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) );
1388
+        return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount );
1389
+    }
1390
+
1391
+    /**
1392
+     * Checks if the discount is recurring.
1393
+     *
1394
+     * @since 1.0.15
1395
+     * @return boolean
1396
+     */
1397
+    public function is_recurring() {
1398
+        $recurring = $this->get_is_recurring();
1399
+        return ! empty( $recurring );
1400
+    }
1401
+
1402
+    /**
1403
+     * Checks if the discount is single use.
1404
+     *
1405
+     * @since 1.0.15
1406
+     * @return boolean
1407
+     */
1408
+    public function is_single_use() {
1409
+        $usage = $this->get_is_single_use();
1410
+        return ! empty( $usage );
1411
+    }
1412
+
1413
+    /**
1414
+     * Check if a discount is valid for the given user
1415
+     *
1416
+     * @param  int|string  $user
1417
+     * @since 1.0.15
1418
+     * @return boolean
1419
+     */
1420
+    public function is_valid_for_user( $user ) {
1421
+
1422
+        // Ensure that the discount is single use.
1423
+        if ( empty( $user ) || ! $this->is_single_use() ) {
1424
+            return true;
1425
+        }
1426
+
1427
+        // Prepare the user id.
1428
+        $user_id = 0;
1429 1429
         if ( is_numeric( $user ) ) {
1430 1430
             $user_id = absint( $user );
1431 1431
         } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) {
@@ -1434,117 +1434,117 @@  discard block
 block discarded – undo
1434 1434
             $user_id = $user_data->ID;
1435 1435
         }
1436 1436
 
1437
-		// Ensure that we have a user.
1438
-		if ( empty( $user_id ) ) {
1439
-			return true;
1440
-		}
1437
+        // Ensure that we have a user.
1438
+        if ( empty( $user_id ) ) {
1439
+            return true;
1440
+        }
1441 1441
 
1442
-		// Get all payments with matching user id.
1442
+        // Get all payments with matching user id.
1443 1443
         $payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false, 'paginate' => false ) );
1444
-		$code     = strtolower( $this->get_code() );
1445
-
1446
-		// For each payment...
1447
-		foreach ( $payments as $payment ) {
1448
-
1449
-			// Only check for paid invoices.
1450
-			if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) {
1451
-				return false;
1452
-			}
1453
-
1454
-		}
1455
-
1456
-		return true;
1457
-	}
1458
-
1459
-	/**
1460
-	 * Deletes the discount from the database
1461
-	 *
1462
-	 * @since 1.0.15
1463
-	 * @return boolean
1464
-	 */
1465
-	public function remove() {
1466
-		return $this->delete();
1467
-	}
1468
-
1469
-	/**
1470
-	 * Increases a discount's usage.
1471
-	 *
1472
-	 * @since 1.0.15
1473
-	 * @param int $by The number of usages to increas by.
1474
-	 * @return int
1475
-	 */
1476
-	public function increase_usage( $by = 1 ) {
1477
-
1478
-		// Abort if zero.
1479
-		if ( empty( $by ) ) {
1480
-			return;
1481
-		}
1482
-
1483
-		// Increase the usage.
1484
-		$this->set_uses( $this->get_uses() + (int) $by );
1485
-
1486
-		// Save the discount.
1487
-		$this->save();
1488
-
1489
-		// Fire relevant hooks.
1490
-		if( (int) $by > 0 ) {
1491
-			do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(),  absint( $by ) );
1492
-		} else {
1493
-			do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) );
1494
-		}
1495
-
1496
-		// Return the number of times the discount has been used.
1497
-		return $this->get_uses();
1498
-	}
1499
-
1500
-	/**
1501
-	 * Alias of self::__toString()
1502
-	 *
1503
-	 * @since 1.0.15
1504
-	 * @return string|false
1505
-	 */
1506
-	public function get_data_as_json() {
1507
-		return $this->__toString();
1508
-	}
1509
-
1510
-	/**
1511
-	 * Returns a discount's discounted amount.
1512
-	 *
1513
-	 * @since 1.0.15
1514
-	 * @param float $amount
1515
-	 * @return float
1516
-	 */
1517
-	public function get_discounted_amount( $amount ) {
1518
-
1519
-		// Convert amount to float.
1520
-		$amount = (float) $amount;
1521
-
1522
-		// Get discount amount.
1523
-		$discount_amount = $this->get_amount();
1524
-
1525
-		if ( empty( $discount_amount ) ) {
1526
-			return 0;
1527
-		}
1528
-
1529
-		// Format the amount.
1530
-		$discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) );
1531
-
1532
-		// If this is a percentage discount.
1533
-		if ( $this->is_type( 'percent' ) ) {
1444
+        $code     = strtolower( $this->get_code() );
1445
+
1446
+        // For each payment...
1447
+        foreach ( $payments as $payment ) {
1448
+
1449
+            // Only check for paid invoices.
1450
+            if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) {
1451
+                return false;
1452
+            }
1453
+
1454
+        }
1455
+
1456
+        return true;
1457
+    }
1458
+
1459
+    /**
1460
+     * Deletes the discount from the database
1461
+     *
1462
+     * @since 1.0.15
1463
+     * @return boolean
1464
+     */
1465
+    public function remove() {
1466
+        return $this->delete();
1467
+    }
1468
+
1469
+    /**
1470
+     * Increases a discount's usage.
1471
+     *
1472
+     * @since 1.0.15
1473
+     * @param int $by The number of usages to increas by.
1474
+     * @return int
1475
+     */
1476
+    public function increase_usage( $by = 1 ) {
1477
+
1478
+        // Abort if zero.
1479
+        if ( empty( $by ) ) {
1480
+            return;
1481
+        }
1482
+
1483
+        // Increase the usage.
1484
+        $this->set_uses( $this->get_uses() + (int) $by );
1485
+
1486
+        // Save the discount.
1487
+        $this->save();
1488
+
1489
+        // Fire relevant hooks.
1490
+        if( (int) $by > 0 ) {
1491
+            do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(),  absint( $by ) );
1492
+        } else {
1493
+            do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) );
1494
+        }
1495
+
1496
+        // Return the number of times the discount has been used.
1497
+        return $this->get_uses();
1498
+    }
1499
+
1500
+    /**
1501
+     * Alias of self::__toString()
1502
+     *
1503
+     * @since 1.0.15
1504
+     * @return string|false
1505
+     */
1506
+    public function get_data_as_json() {
1507
+        return $this->__toString();
1508
+    }
1509
+
1510
+    /**
1511
+     * Returns a discount's discounted amount.
1512
+     *
1513
+     * @since 1.0.15
1514
+     * @param float $amount
1515
+     * @return float
1516
+     */
1517
+    public function get_discounted_amount( $amount ) {
1518
+
1519
+        // Convert amount to float.
1520
+        $amount = (float) $amount;
1521
+
1522
+        // Get discount amount.
1523
+        $discount_amount = $this->get_amount();
1524
+
1525
+        if ( empty( $discount_amount ) ) {
1526
+            return 0;
1527
+        }
1528
+
1529
+        // Format the amount.
1530
+        $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) );
1531
+
1532
+        // If this is a percentage discount.
1533
+        if ( $this->is_type( 'percent' ) ) {
1534 1534
             $discount_amount = $amount * ( $discount_amount / 100 );
1535
-		}
1535
+        }
1536 1536
 
1537
-		// Discount can not be less than zero...
1538
-		if ( $discount_amount < 0 ) {
1539
-			$discount_amount = 0;
1540
-		}
1537
+        // Discount can not be less than zero...
1538
+        if ( $discount_amount < 0 ) {
1539
+            $discount_amount = 0;
1540
+        }
1541 1541
 
1542
-		// ... or more than the amount.
1543
-		if ( $discount_amount > $amount ) {
1544
-			$discount_amount = $amount;
1545
-		}
1542
+        // ... or more than the amount.
1543
+        if ( $discount_amount > $amount ) {
1544
+            $discount_amount = $amount;
1545
+        }
1546 1546
 
1547
-		return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this );
1548
-	}
1547
+        return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this );
1548
+    }
1549 1549
 
1550 1550
 }
Please login to merge, or discard this patch.
includes/data-stores/class-getpaid-discount-data-store.php 1 patch
Indentation   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
  *
6 6
  */
7 7
 if ( ! defined( 'ABSPATH' ) ) {
8
-	exit;
8
+    exit;
9 9
 }
10 10
 
11 11
 /**
@@ -15,198 +15,198 @@  discard block
 block discarded – undo
15 15
  */
16 16
 class GetPaid_Discount_Data_Store extends GetPaid_Data_Store_WP {
17 17
 
18
-	/**
19
-	 * Data stored in meta keys, but not considered "meta" for a discount.
20
-	 *
21
-	 * @since 1.0.19
22
-	 * @var array
23
-	 */
24
-	protected $internal_meta_keys = array(
25
-		'_wpi_discount_code',
26
-		'_wpi_discount_amount',
27
-		'_wpi_discount_start',
28
-		'_wpi_discount_expiration',
29
-		'_wpi_discount_type',
30
-		'_wpi_discount_uses',
31
-		'_wpi_discount_is_single_use',
32
-		'_wpi_discount_items',
33
-		'_wpi_discount_excluded_items',
34
-		'_wpi_discount_required_items',
35
-		'_wpi_discount_max_uses',
36
-		'_wpi_discount_is_recurring',
37
-		'_wpi_discount_min_total',
38
-		'_wpi_discount_max_total',
39
-	);
40
-
41
-	/**
42
-	 * A map of meta keys to data props.
43
-	 *
44
-	 * @since 1.0.19
45
-	 *
46
-	 * @var array
47
-	 */
48
-	protected $meta_key_to_props = array(
49
-		'_wpi_discount_code'           => 'code',
50
-		'_wpi_discount_amount'         => 'amount',
51
-		'_wpi_discount_start'          => 'start',
52
-		'_wpi_discount_expiration'     => 'expiration',
53
-		'_wpi_discount_type'           => 'type',
54
-		'_wpi_discount_uses'           => 'uses',
55
-		'_wpi_discount_is_single_use'  => 'is_single_use',
56
-		'_wpi_discount_items'          => 'items',
57
-		'_wpi_discount_excluded_items' => 'excluded_items',
58
-		'_wpi_discount_required_items' => 'required_items',
59
-		'_wpi_discount_max_uses'       => 'max_uses',
60
-		'_wpi_discount_is_recurring'   => 'is_recurring',
61
-		'_wpi_discount_min_total'      => 'min_total',
62
-		'_wpi_discount_max_total'      => 'max_total',
63
-	);
64
-
65
-	/*
18
+    /**
19
+     * Data stored in meta keys, but not considered "meta" for a discount.
20
+     *
21
+     * @since 1.0.19
22
+     * @var array
23
+     */
24
+    protected $internal_meta_keys = array(
25
+        '_wpi_discount_code',
26
+        '_wpi_discount_amount',
27
+        '_wpi_discount_start',
28
+        '_wpi_discount_expiration',
29
+        '_wpi_discount_type',
30
+        '_wpi_discount_uses',
31
+        '_wpi_discount_is_single_use',
32
+        '_wpi_discount_items',
33
+        '_wpi_discount_excluded_items',
34
+        '_wpi_discount_required_items',
35
+        '_wpi_discount_max_uses',
36
+        '_wpi_discount_is_recurring',
37
+        '_wpi_discount_min_total',
38
+        '_wpi_discount_max_total',
39
+    );
40
+
41
+    /**
42
+     * A map of meta keys to data props.
43
+     *
44
+     * @since 1.0.19
45
+     *
46
+     * @var array
47
+     */
48
+    protected $meta_key_to_props = array(
49
+        '_wpi_discount_code'           => 'code',
50
+        '_wpi_discount_amount'         => 'amount',
51
+        '_wpi_discount_start'          => 'start',
52
+        '_wpi_discount_expiration'     => 'expiration',
53
+        '_wpi_discount_type'           => 'type',
54
+        '_wpi_discount_uses'           => 'uses',
55
+        '_wpi_discount_is_single_use'  => 'is_single_use',
56
+        '_wpi_discount_items'          => 'items',
57
+        '_wpi_discount_excluded_items' => 'excluded_items',
58
+        '_wpi_discount_required_items' => 'required_items',
59
+        '_wpi_discount_max_uses'       => 'max_uses',
60
+        '_wpi_discount_is_recurring'   => 'is_recurring',
61
+        '_wpi_discount_min_total'      => 'min_total',
62
+        '_wpi_discount_max_total'      => 'max_total',
63
+    );
64
+
65
+    /*
66 66
 	|--------------------------------------------------------------------------
67 67
 	| CRUD Methods
68 68
 	|--------------------------------------------------------------------------
69 69
 	*/
70 70
 
71
-	/**
72
-	 * Method to create a new discount in the database.
73
-	 *
74
-	 * @param WPInv_Discount $discount Discount object.
75
-	 */
76
-	public function create( &$discount ) {
77
-		$discount->set_version( WPINV_VERSION );
78
-		$discount->set_date_created( current_time('mysql') );
79
-
80
-		// Create a new post.
81
-		$id = wp_insert_post(
82
-			apply_filters(
83
-				'getpaid_new_discount_data',
84
-				array(
85
-					'post_date'     => $discount->get_date_created( 'edit' ),
86
-					'post_type'     => 'wpi_discount',
87
-					'post_status'   => $this->get_post_status( $discount ),
88
-					'ping_status'   => 'closed',
89
-					'post_author'   => $discount->get_author( 'edit' ),
90
-					'post_title'    => $discount->get_name( 'edit' ),
91
-					'post_excerpt'  => $discount->get_description( 'edit' ),
92
-				)
93
-			),
94
-			true
95
-		);
96
-
97
-		if ( $id && ! is_wp_error( $id ) ) {
98
-			$discount->set_id( $id );
99
-			$this->update_post_meta( $discount );
100
-			$discount->save_meta_data();
101
-			$discount->apply_changes();
102
-			$this->clear_caches( $discount );
103
-			do_action( 'getpaid_new_discount', $discount );
104
-			return true;
105
-		}
106
-
107
-		if ( is_wp_error( $id ) ) {
108
-			$discount->last_error = $id->get_error_message();
109
-		}
110
-
111
-		return false;
112
-	}
113
-
114
-	/**
115
-	 * Method to read a discount from the database.
116
-	 *
117
-	 * @param WPInv_Discount $discount Discount object.
118
-	 *
119
-	 */
120
-	public function read( &$discount ) {
121
-
122
-		$discount->set_defaults();
123
-		$discount_object = get_post( $discount->get_id() );
124
-
125
-		if ( ! $discount->get_id() || ! $discount_object || $discount_object->post_type != 'wpi_discount' ) {
126
-			$discount->last_error = __( 'Invalid discount.', 'invoicing' );
127
-			$discount->set_id( 0 );
128
-			return false;
129
-		}
130
-
131
-		$discount->set_props(
132
-			array(
133
-				'date_created'  => 0 < $discount_object->post_date ? $discount_object->post_date : null,
134
-				'date_modified' => 0 < $discount_object->post_modified ? $discount_object->post_modified : null,
135
-				'status'        => $discount_object->post_status,
136
-				'name'          => $discount_object->post_title,
137
-				'author'        => $discount_object->post_author,
138
-				'description'   => $discount_object->post_excerpt,
139
-			)
140
-		);
141
-
142
-		$this->read_object_data( $discount, $discount_object );
143
-		$discount->read_meta_data();
144
-		$discount->set_object_read( true );
145
-		do_action( 'getpaid_read_discount', $discount );
146
-
147
-	}
148
-
149
-	/**
150
-	 * Method to update a discount in the database.
151
-	 *
152
-	 * @param WPInv_Discount $discount Discount object.
153
-	 */
154
-	public function update( &$discount ) {
155
-		$discount->save_meta_data();
156
-		$discount->set_version( WPINV_VERSION );
157
-
158
-		if ( null === $discount->get_date_created( 'edit' ) ) {
159
-			$discount->set_date_created(  current_time('mysql') );
160
-		}
161
-
162
-		// Grab the current status so we can compare.
163
-		$previous_status = get_post_status( $discount->get_id() );
164
-
165
-		$changes = $discount->get_changes();
166
-
167
-		// Only update the post when the post data changes.
168
-		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) {
169
-			$post_data = array(
170
-				'post_date'         => $discount->get_date_created( 'edit' ),
171
-				'post_status'       => $discount->get_status( 'edit' ),
172
-				'post_title'        => $discount->get_name( 'edit' ),
173
-				'post_author'       => $discount->get_author( 'edit' ),
174
-				'post_modified'     => $discount->get_date_modified( 'edit' ),
175
-				'post_excerpt'      => $discount->get_description( 'edit' ),
176
-			);
177
-
178
-			/**
179
-			 * When updating this object, to prevent infinite loops, use $wpdb
180
-			 * to update data, since wp_update_post spawns more calls to the
181
-			 * save_post action.
182
-			 *
183
-			 * This ensures hooks are fired by either WP itself (admin screen save),
184
-			 * or an update purely from CRUD.
185
-			 */
186
-			if ( doing_action( 'save_post' ) ) {
187
-				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) );
188
-				clean_post_cache( $discount->get_id() );
189
-			} else {
190
-				wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) );
191
-			}
192
-			$discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
193
-		}
194
-		$this->update_post_meta( $discount );
195
-		$discount->apply_changes();
196
-		$this->clear_caches( $discount );
197
-
198
-		// Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
199
-		$new_status = $discount->get_status( 'edit' );
200
-
201
-		if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
202
-			do_action( 'getpaid_new_discount', $discount );
203
-		} else {
204
-			do_action( 'getpaid_update_discount', $discount );
205
-		}
206
-
207
-	}
208
-
209
-	/*
71
+    /**
72
+     * Method to create a new discount in the database.
73
+     *
74
+     * @param WPInv_Discount $discount Discount object.
75
+     */
76
+    public function create( &$discount ) {
77
+        $discount->set_version( WPINV_VERSION );
78
+        $discount->set_date_created( current_time('mysql') );
79
+
80
+        // Create a new post.
81
+        $id = wp_insert_post(
82
+            apply_filters(
83
+                'getpaid_new_discount_data',
84
+                array(
85
+                    'post_date'     => $discount->get_date_created( 'edit' ),
86
+                    'post_type'     => 'wpi_discount',
87
+                    'post_status'   => $this->get_post_status( $discount ),
88
+                    'ping_status'   => 'closed',
89
+                    'post_author'   => $discount->get_author( 'edit' ),
90
+                    'post_title'    => $discount->get_name( 'edit' ),
91
+                    'post_excerpt'  => $discount->get_description( 'edit' ),
92
+                )
93
+            ),
94
+            true
95
+        );
96
+
97
+        if ( $id && ! is_wp_error( $id ) ) {
98
+            $discount->set_id( $id );
99
+            $this->update_post_meta( $discount );
100
+            $discount->save_meta_data();
101
+            $discount->apply_changes();
102
+            $this->clear_caches( $discount );
103
+            do_action( 'getpaid_new_discount', $discount );
104
+            return true;
105
+        }
106
+
107
+        if ( is_wp_error( $id ) ) {
108
+            $discount->last_error = $id->get_error_message();
109
+        }
110
+
111
+        return false;
112
+    }
113
+
114
+    /**
115
+     * Method to read a discount from the database.
116
+     *
117
+     * @param WPInv_Discount $discount Discount object.
118
+     *
119
+     */
120
+    public function read( &$discount ) {
121
+
122
+        $discount->set_defaults();
123
+        $discount_object = get_post( $discount->get_id() );
124
+
125
+        if ( ! $discount->get_id() || ! $discount_object || $discount_object->post_type != 'wpi_discount' ) {
126
+            $discount->last_error = __( 'Invalid discount.', 'invoicing' );
127
+            $discount->set_id( 0 );
128
+            return false;
129
+        }
130
+
131
+        $discount->set_props(
132
+            array(
133
+                'date_created'  => 0 < $discount_object->post_date ? $discount_object->post_date : null,
134
+                'date_modified' => 0 < $discount_object->post_modified ? $discount_object->post_modified : null,
135
+                'status'        => $discount_object->post_status,
136
+                'name'          => $discount_object->post_title,
137
+                'author'        => $discount_object->post_author,
138
+                'description'   => $discount_object->post_excerpt,
139
+            )
140
+        );
141
+
142
+        $this->read_object_data( $discount, $discount_object );
143
+        $discount->read_meta_data();
144
+        $discount->set_object_read( true );
145
+        do_action( 'getpaid_read_discount', $discount );
146
+
147
+    }
148
+
149
+    /**
150
+     * Method to update a discount in the database.
151
+     *
152
+     * @param WPInv_Discount $discount Discount object.
153
+     */
154
+    public function update( &$discount ) {
155
+        $discount->save_meta_data();
156
+        $discount->set_version( WPINV_VERSION );
157
+
158
+        if ( null === $discount->get_date_created( 'edit' ) ) {
159
+            $discount->set_date_created(  current_time('mysql') );
160
+        }
161
+
162
+        // Grab the current status so we can compare.
163
+        $previous_status = get_post_status( $discount->get_id() );
164
+
165
+        $changes = $discount->get_changes();
166
+
167
+        // Only update the post when the post data changes.
168
+        if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) {
169
+            $post_data = array(
170
+                'post_date'         => $discount->get_date_created( 'edit' ),
171
+                'post_status'       => $discount->get_status( 'edit' ),
172
+                'post_title'        => $discount->get_name( 'edit' ),
173
+                'post_author'       => $discount->get_author( 'edit' ),
174
+                'post_modified'     => $discount->get_date_modified( 'edit' ),
175
+                'post_excerpt'      => $discount->get_description( 'edit' ),
176
+            );
177
+
178
+            /**
179
+             * When updating this object, to prevent infinite loops, use $wpdb
180
+             * to update data, since wp_update_post spawns more calls to the
181
+             * save_post action.
182
+             *
183
+             * This ensures hooks are fired by either WP itself (admin screen save),
184
+             * or an update purely from CRUD.
185
+             */
186
+            if ( doing_action( 'save_post' ) ) {
187
+                $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) );
188
+                clean_post_cache( $discount->get_id() );
189
+            } else {
190
+                wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) );
191
+            }
192
+            $discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
193
+        }
194
+        $this->update_post_meta( $discount );
195
+        $discount->apply_changes();
196
+        $this->clear_caches( $discount );
197
+
198
+        // Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
199
+        $new_status = $discount->get_status( 'edit' );
200
+
201
+        if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
202
+            do_action( 'getpaid_new_discount', $discount );
203
+        } else {
204
+            do_action( 'getpaid_update_discount', $discount );
205
+        }
206
+
207
+    }
208
+
209
+    /*
210 210
 	|--------------------------------------------------------------------------
211 211
 	| Additional Methods
212 212
 	|--------------------------------------------------------------------------
Please login to merge, or discard this patch.
includes/admin/meta-boxes/class-getpaid-meta-box-item-details.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  */
9 9
 
10 10
 if ( ! defined( 'ABSPATH' ) ) {
11
-	exit; // Exit if accessed directly
11
+    exit; // Exit if accessed directly
12 12
 }
13 13
 
14 14
 /**
@@ -17,10 +17,10 @@  discard block
 block discarded – undo
17 17
 class GetPaid_Meta_Box_Item_Details {
18 18
 
19 19
     /**
20
-	 * Output the metabox.
21
-	 *
22
-	 * @param WP_Post $post
23
-	 */
20
+     * Output the metabox.
21
+     *
22
+     * @param WP_Post $post
23
+     */
24 24
     public static function output( $post ) {
25 25
 
26 26
         // Prepare the item.
@@ -270,35 +270,35 @@  discard block
 block discarded – undo
270 270
     }
271 271
 
272 272
     /**
273
-	 * Save meta box data.
274
-	 *
275
-	 * @param int $post_id
276
-	 */
277
-	public static function save( $post_id ) {
273
+     * Save meta box data.
274
+     *
275
+     * @param int $post_id
276
+     */
277
+    public static function save( $post_id ) {
278 278
 
279 279
         // Prepare the item.
280 280
         $item = new WPInv_Item( $post_id );
281 281
 
282 282
         // Load new data.
283 283
         $item->set_props(
284
-			array(
285
-				'price'                => isset( $_POST['wpinv_item_price'] ) ? getpaid_standardize_amount( $_POST['wpinv_item_price'] ) : null,
286
-				'vat_rule'             => isset( $_POST['wpinv_vat_rules'] ) ? wpinv_clean( $_POST['wpinv_vat_rules'] ) : null,
287
-				'vat_class'            => isset( $_POST['wpinv_vat_class'] ) ? wpinv_clean( $_POST['wpinv_vat_class'] ) : null,
288
-				'type'                 => isset( $_POST['wpinv_item_type'] ) ? wpinv_clean( $_POST['wpinv_item_type'] ) : null,
289
-				'is_dynamic_pricing'   => ! empty( $_POST['wpinv_name_your_price'] ),
284
+            array(
285
+                'price'                => isset( $_POST['wpinv_item_price'] ) ? getpaid_standardize_amount( $_POST['wpinv_item_price'] ) : null,
286
+                'vat_rule'             => isset( $_POST['wpinv_vat_rules'] ) ? wpinv_clean( $_POST['wpinv_vat_rules'] ) : null,
287
+                'vat_class'            => isset( $_POST['wpinv_vat_class'] ) ? wpinv_clean( $_POST['wpinv_vat_class'] ) : null,
288
+                'type'                 => isset( $_POST['wpinv_item_type'] ) ? wpinv_clean( $_POST['wpinv_item_type'] ) : null,
289
+                'is_dynamic_pricing'   => ! empty( $_POST['wpinv_name_your_price'] ),
290 290
                 'minimum_price'        => isset( $_POST['wpinv_minimum_price'] ) ? getpaid_standardize_amount( $_POST['wpinv_minimum_price'] ) : null,
291
-				'is_recurring'         => ! empty( $_POST['wpinv_is_recurring'] ),
292
-				'recurring_period'     => isset( $_POST['wpinv_recurring_period'] ) ? wpinv_clean( $_POST['wpinv_recurring_period'] ) : null,
293
-				'recurring_interval'   => isset( $_POST['wpinv_recurring_interval'] ) ? (int) $_POST['wpinv_recurring_interval'] : 1,
294
-				'recurring_limit'      => isset( $_POST['wpinv_recurring_limit'] ) ? (int) $_POST['wpinv_recurring_limit'] : null,
295
-				'is_free_trial'        => isset( $_POST['wpinv_trial_interval'] ) ? ( 0 != (int) $_POST['wpinv_trial_interval'] ) : null,
296
-				'trial_period'         => isset( $_POST['wpinv_trial_period'] ) ? wpinv_clean( $_POST['wpinv_trial_period'] ) : null,
297
-				'trial_interval'       => isset( $_POST['wpinv_trial_interval'] ) ? (int) $_POST['wpinv_trial_interval'] : null,
298
-			)
291
+                'is_recurring'         => ! empty( $_POST['wpinv_is_recurring'] ),
292
+                'recurring_period'     => isset( $_POST['wpinv_recurring_period'] ) ? wpinv_clean( $_POST['wpinv_recurring_period'] ) : null,
293
+                'recurring_interval'   => isset( $_POST['wpinv_recurring_interval'] ) ? (int) $_POST['wpinv_recurring_interval'] : 1,
294
+                'recurring_limit'      => isset( $_POST['wpinv_recurring_limit'] ) ? (int) $_POST['wpinv_recurring_limit'] : null,
295
+                'is_free_trial'        => isset( $_POST['wpinv_trial_interval'] ) ? ( 0 != (int) $_POST['wpinv_trial_interval'] ) : null,
296
+                'trial_period'         => isset( $_POST['wpinv_trial_period'] ) ? wpinv_clean( $_POST['wpinv_trial_period'] ) : null,
297
+                'trial_interval'       => isset( $_POST['wpinv_trial_interval'] ) ? (int) $_POST['wpinv_trial_interval'] : null,
298
+            )
299 299
         );
300 300
 
301
-		$item->save();
302
-		do_action( 'getpaid_item_metabox_save', $post_id, $item );
303
-	}
301
+        $item->save();
302
+        do_action( 'getpaid_item_metabox_save', $post_id, $item );
303
+    }
304 304
 }
Please login to merge, or discard this patch.
includes/admin/meta-boxes/class-getpaid-meta-box-discount-details.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  */
9 9
 
10 10
 if ( ! defined( 'ABSPATH' ) ) {
11
-	exit; // Exit if accessed directly
11
+    exit; // Exit if accessed directly
12 12
 }
13 13
 
14 14
 /**
@@ -17,10 +17,10 @@  discard block
 block discarded – undo
17 17
 class GetPaid_Meta_Box_Discount_Details {
18 18
 
19 19
     /**
20
-	 * Output the metabox.
21
-	 *
22
-	 * @param WP_Post $post
23
-	 */
20
+     * Output the metabox.
21
+     *
22
+     * @param WP_Post $post
23
+     */
24 24
     public static function output( $post ) {
25 25
 
26 26
         // Prepare the discount.
@@ -396,35 +396,35 @@  discard block
 block discarded – undo
396 396
     }
397 397
 
398 398
     /**
399
-	 * Save meta box data.
400
-	 *
401
-	 * @param int $post_id
402
-	 */
403
-	public static function save( $post_id ) {
399
+     * Save meta box data.
400
+     *
401
+     * @param int $post_id
402
+     */
403
+    public static function save( $post_id ) {
404 404
 
405 405
         // Prepare the discount.
406 406
         $discount = new WPInv_Discount( $post_id );
407 407
 
408 408
         // Load new data.
409 409
         $discount->set_props(
410
-			array(
411
-				'code'                 => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null,
412
-				'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null,
413
-				'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
414
-				'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
415
-				'is_single_use'        => ! empty( $_POST['wpinv_discount_single_use'] ),
410
+            array(
411
+                'code'                 => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null,
412
+                'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null,
413
+                'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
414
+                'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
415
+                'is_single_use'        => ! empty( $_POST['wpinv_discount_single_use'] ),
416 416
                 'type'                 => isset( $_POST['wpinv_discount_type'] ) ? wpinv_clean( $_POST['wpinv_discount_type'] ) : null,
417
-				'is_recurring'         => ! empty( $_POST['wpinv_discount_recurring'] ),
418
-				'items'                => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(),
419
-				'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(),
417
+                'is_recurring'         => ! empty( $_POST['wpinv_discount_recurring'] ),
418
+                'items'                => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(),
419
+                'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(),
420 420
                 'required_items'       => isset( $_POST['wpinv_discount_required_items'] ) ? wpinv_clean( $_POST['wpinv_discount_required_items'] ) : array(),
421
-				'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null,
422
-				'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null,
423
-				'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null,
424
-			)
421
+                'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null,
422
+                'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null,
423
+                'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null,
424
+            )
425 425
         );
426 426
 
427
-		$discount->save();
428
-		do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
429
-	}
427
+        $discount->save();
428
+        do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
429
+    }
430 430
 }
Please login to merge, or discard this patch.
includes/admin/meta-boxes/class-getpaid-meta-box-invoice-address.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  */
9 9
 
10 10
 if ( ! defined( 'ABSPATH' ) ) {
11
-	exit; // Exit if accessed directly
11
+    exit; // Exit if accessed directly
12 12
 }
13 13
 
14 14
 /**
@@ -17,10 +17,10 @@  discard block
 block discarded – undo
17 17
 class GetPaid_Meta_Box_Invoice_Address {
18 18
 
19 19
     /**
20
-	 * Output the metabox.
21
-	 *
22
-	 * @param WP_Post $post
23
-	 */
20
+     * Output the metabox.
21
+     *
22
+     * @param WP_Post $post
23
+     */
24 24
     public static function output( $post ) {
25 25
 
26 26
         // Prepare the invoice.
@@ -366,18 +366,18 @@  discard block
 block discarded – undo
366 366
     }
367 367
 
368 368
     /**
369
-	 * Save meta box data.
370
-	 *
371
-	 * @param int $post_id
372
-	 */
373
-	public static function save( $post_id ) {
369
+     * Save meta box data.
370
+     *
371
+     * @param int $post_id
372
+     */
373
+    public static function save( $post_id ) {
374 374
 
375 375
         // Prepare the invoice.
376 376
         $invoice = new WPInv_Invoice( $post_id );
377 377
 
378 378
         // Load new data.
379 379
         $invoice->set_props(
380
-			array(
380
+            array(
381 381
                 'template'             => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null,
382 382
                 'email_cc'             => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null,
383 383
                 'disable_taxes'        => ! empty( $_POST['disable_taxes'] ),
@@ -400,7 +400,7 @@  discard block
 block discarded – undo
400 400
                 'due_date'             => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null,
401 401
                 'number'               => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null,
402 402
                 'status'               => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null,
403
-			)
403
+            )
404 404
         );
405 405
 
406 406
         // Discount code.
@@ -465,6 +465,6 @@  discard block
 block discarded – undo
465 465
         }
466 466
 
467 467
         // Fires after an invoice is saved.
468
-		do_action( 'wpinv_invoice_metabox_saved', $invoice );
469
-	}
468
+        do_action( 'wpinv_invoice_metabox_saved', $invoice );
469
+    }
470 470
 }
Please login to merge, or discard this patch.
includes/class-getpaid-subscription-notification-emails.php 1 patch
Indentation   +257 added lines, -257 removed lines patch added patch discarded remove patch
@@ -13,300 +13,300 @@
 block discarded – undo
13 13
 class GetPaid_Subscription_Notification_Emails {
14 14
 
15 15
     /**
16
-	 * The array of subscription email actions.
17
-	 *
18
-	 * @param array
19
-	 */
20
-	public $subscription_actions;
16
+     * The array of subscription email actions.
17
+     *
18
+     * @param array
19
+     */
20
+    public $subscription_actions;
21 21
 
22 22
     /**
23
-	 * Class constructor
23
+     * Class constructor
24 24
      *
25
-	 */
26
-	public function __construct() {
27
-
28
-		$this->subscription_actions = apply_filters(
29
-			'getpaid_notification_email_subscription_triggers',
30
-			array(
31
-				'getpaid_subscription_trialling' => 'subscription_trial',
32
-				'getpaid_subscription_cancelled' => 'subscription_cancelled',
33
-				'getpaid_subscription_expired'   => 'subscription_expired',
34
-				'getpaid_subscription_completed' => 'subscription_complete',
35
-				'getpaid_daily_maintenance'      => 'renewal_reminder',
36
-			)
37
-		);
38
-
39
-		$this->init_hooks();
25
+     */
26
+    public function __construct() {
27
+
28
+        $this->subscription_actions = apply_filters(
29
+            'getpaid_notification_email_subscription_triggers',
30
+            array(
31
+                'getpaid_subscription_trialling' => 'subscription_trial',
32
+                'getpaid_subscription_cancelled' => 'subscription_cancelled',
33
+                'getpaid_subscription_expired'   => 'subscription_expired',
34
+                'getpaid_subscription_completed' => 'subscription_complete',
35
+                'getpaid_daily_maintenance'      => 'renewal_reminder',
36
+            )
37
+        );
38
+
39
+        $this->init_hooks();
40 40
 
41 41
     }
42 42
 
43 43
     /**
44
-	 * Registers email hooks.
45
-	 */
46
-	public function init_hooks() {
47
-
48
-		add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 );
49
-		foreach ( $this->subscription_actions as $hook => $email_type ) {
50
-
51
-			$email = new GetPaid_Notification_Email( $email_type );
52
-
53
-			if ( ! $email->is_active() ) {
54
-				continue;
55
-			}
56
-
57
-			if ( method_exists( $this, $email_type ) ) {
58
-				add_action( $hook, array( $this, $email_type ), 100, 2 );
59
-				continue;
60
-			}
61
-
62
-			do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook );
63
-
64
-		}
65
-
66
-	}
67
-
68
-	/**
69
-	 * Filters subscription merge tags.
70
-	 *
71
-	 * @param array $merge_tags
72
-	 * @param mixed|WPInv_Invoice|WPInv_Subscription $object
73
-	 */
74
-	public function subscription_merge_tags( $merge_tags, $object ) {
75
-
76
-		if ( is_a( $object, 'WPInv_Subscription' ) ) {
77
-			$merge_tags = array_merge(
78
-				$merge_tags,
79
-				$this->get_subscription_merge_tags( $object )
80
-			);
81
-		}
82
-
83
-		return $merge_tags;
84
-
85
-	}
86
-
87
-	/**
88
-	 * Generates subscription merge tags.
89
-	 *
90
-	 * @param WPInv_Subscription $subscription
91
-	 * @return array
92
-	 */
93
-	public function get_subscription_merge_tags( $subscription ) {
94
-
95
-		// Abort if it does not exist.
96
-		if ( ! $subscription->get_id() ) {
97
-			return array();
98
-		}
99
-
100
-		$invoice    = $subscription->get_parent_invoice();
101
-		return array(
102
-			'{subscription_renewal_date}'     => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ),
103
-			'{subscription_created}'          => getpaid_format_date_value( $subscription->get_date_created() ),
104
-			'{subscription_status}'           => sanitize_text_field( $subscription->get_status_label() ),
105
-			'{subscription_profile_id}'       => sanitize_text_field( $subscription->get_profile_id() ),
106
-			'{subscription_id}'               => absint( $subscription->get_id() ),
107
-			'{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ),
108
-			'{subscription_initial_amount}'   => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ),
109
-			'{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ),
110
-			'{subscription_bill_times}'       => $subscription->get_bill_times(),
111
-			'{subscription_url}'              => esc_url( $subscription->get_view_url() ),
112
-		);
113
-
114
-	}
115
-
116
-	/**
117
-	 * Checks if we should send a notification for a subscription.
118
-	 *
119
-	 * @param WPInv_Invoice $invoice
120
-	 * @return bool
121
-	 */
122
-	public function should_send_notification( $invoice ) {
123
-		return 0 != $invoice->get_id();
124
-	}
125
-
126
-	/**
127
-	 * Returns notification recipients.
128
-	 *
129
-	 * @param WPInv_Invoice $invoice
130
-	 * @return array
131
-	 */
132
-	public function get_recipients( $invoice ) {
133
-		$recipients = array( $invoice->get_email() );
134
-
135
-		$cc = $invoice->get_email_cc();
136
-
137
-		if ( ! empty( $cc ) ) {
138
-			$cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) );
139
-			$recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) );
140
-		}
141
-
142
-		return $recipients;
143
-	}
144
-
145
-	/**
146
-	 * Helper function to send an email.
147
-	 *
148
-	 * @param WPInv_Subscription $subscription
149
-	 * @param GetPaid_Notification_Email $email
150
-	 * @param string $type
151
-	 * @param array $extra_args Extra template args.
152
-	 */
153
-	public function send_email( $subscription, $email, $type, $extra_args = array() ) {
154
-
155
-		// Abort in case the parent invoice does not exist.
156
-		$invoice = $subscription->get_parent_invoice();
157
-		if ( ! $this->should_send_notification( $invoice ) ) {
158
-			return;
159
-		}
160
-
161
-		if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) {
162
-			return;
163
-		}
164
-
165
-		do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email );
166
-
167
-		$recipients  = $this->get_recipients( $invoice );
168
-		$mailer      = new GetPaid_Notification_Email_Sender();
169
-		$merge_tags  = $email->get_merge_tags();
170
-		$content     = $email->get_content( $merge_tags, $extra_args );
171
-		$subject     = $email->add_merge_tags( $email->get_subject(), $merge_tags );
172
-		$attachments = $email->get_attachments();
173
-
174
-		$result = $mailer->send(
175
-			apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ),
176
-			$subject,
177
-			$content,
178
-			$attachments
179
-		);
180
-
181
-		// Maybe send a copy to the admin.
182
-		if ( $email->include_admin_bcc() ) {
183
-			$mailer->send(
184
-				wpinv_get_admin_email(),
185
-				$subject . __( ' - ADMIN BCC COPY', 'invoicing' ),
186
-				$content,
187
-				$attachments
188
-			);
189
-		}
190
-
191
-		if ( $result ) {
192
-			$invoice->add_system_note(
193
-				sprintf(
194
-					__( 'Successfully sent %s notification email to %s.', 'invoicing' ),
195
-					sanitize_key( $type ),
196
-					$email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
197
-				)
198
-			);
199
-		} else {
200
-			$invoice->add_system_note(
201
-				sprintf(
202
-					__( 'Failed sending %s notification email to %s.', 'invoicing' ),
203
-					sanitize_key( $type ),
204
-					$email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
205
-				)
206
-			);	
207
-		}
208
-
209
-		do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email );
210
-
211
-	}
44
+     * Registers email hooks.
45
+     */
46
+    public function init_hooks() {
47
+
48
+        add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 );
49
+        foreach ( $this->subscription_actions as $hook => $email_type ) {
50
+
51
+            $email = new GetPaid_Notification_Email( $email_type );
52
+
53
+            if ( ! $email->is_active() ) {
54
+                continue;
55
+            }
56
+
57
+            if ( method_exists( $this, $email_type ) ) {
58
+                add_action( $hook, array( $this, $email_type ), 100, 2 );
59
+                continue;
60
+            }
61
+
62
+            do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook );
63
+
64
+        }
65
+
66
+    }
212 67
 
213 68
     /**
214
-	 * Sends a new trial notification.
215
-	 *
216
-	 * @param WPInv_Subscription $subscription
217
-	 */
218
-	public function subscription_trial( $subscription ) {
69
+     * Filters subscription merge tags.
70
+     *
71
+     * @param array $merge_tags
72
+     * @param mixed|WPInv_Invoice|WPInv_Subscription $object
73
+     */
74
+    public function subscription_merge_tags( $merge_tags, $object ) {
219 75
 
220
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
221
-		$this->send_email( $subscription, $email, __FUNCTION__ );
76
+        if ( is_a( $object, 'WPInv_Subscription' ) ) {
77
+            $merge_tags = array_merge(
78
+                $merge_tags,
79
+                $this->get_subscription_merge_tags( $object )
80
+            );
81
+        }
222 82
 
223
-	}
83
+        return $merge_tags;
224 84
 
225
-	/**
226
-	 * Sends a cancelled subscription notification.
227
-	 *
228
-	 * @param WPInv_Subscription $subscription
229
-	 */
230
-	public function subscription_cancelled( $subscription ) {
85
+    }
231 86
 
232
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
233
-		$this->send_email( $subscription, $email, __FUNCTION__ );
87
+    /**
88
+     * Generates subscription merge tags.
89
+     *
90
+     * @param WPInv_Subscription $subscription
91
+     * @return array
92
+     */
93
+    public function get_subscription_merge_tags( $subscription ) {
94
+
95
+        // Abort if it does not exist.
96
+        if ( ! $subscription->get_id() ) {
97
+            return array();
98
+        }
99
+
100
+        $invoice    = $subscription->get_parent_invoice();
101
+        return array(
102
+            '{subscription_renewal_date}'     => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ),
103
+            '{subscription_created}'          => getpaid_format_date_value( $subscription->get_date_created() ),
104
+            '{subscription_status}'           => sanitize_text_field( $subscription->get_status_label() ),
105
+            '{subscription_profile_id}'       => sanitize_text_field( $subscription->get_profile_id() ),
106
+            '{subscription_id}'               => absint( $subscription->get_id() ),
107
+            '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ),
108
+            '{subscription_initial_amount}'   => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ),
109
+            '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ),
110
+            '{subscription_bill_times}'       => $subscription->get_bill_times(),
111
+            '{subscription_url}'              => esc_url( $subscription->get_view_url() ),
112
+        );
234 113
 
235
-	}
114
+    }
236 115
 
237
-	/**
238
-	 * Sends a subscription expired notification.
239
-	 *
240
-	 * @param WPInv_Subscription $subscription
241
-	 */
242
-	public function subscription_expired( $subscription ) {
116
+    /**
117
+     * Checks if we should send a notification for a subscription.
118
+     *
119
+     * @param WPInv_Invoice $invoice
120
+     * @return bool
121
+     */
122
+    public function should_send_notification( $invoice ) {
123
+        return 0 != $invoice->get_id();
124
+    }
243 125
 
244
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
245
-		$this->send_email( $subscription, $email, __FUNCTION__ );
126
+    /**
127
+     * Returns notification recipients.
128
+     *
129
+     * @param WPInv_Invoice $invoice
130
+     * @return array
131
+     */
132
+    public function get_recipients( $invoice ) {
133
+        $recipients = array( $invoice->get_email() );
246 134
 
247
-	}
135
+        $cc = $invoice->get_email_cc();
248 136
 
249
-	/**
250
-	 * Sends a completed subscription notification.
251
-	 *
252
-	 * @param WPInv_Subscription $subscription
253
-	 */
254
-	public function subscription_complete( $subscription ) {
137
+        if ( ! empty( $cc ) ) {
138
+            $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) );
139
+            $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) );
140
+        }
255 141
 
256
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
257
-		$this->send_email( $subscription, $email, __FUNCTION__ );
142
+        return $recipients;
143
+    }
258 144
 
259
-	}
145
+    /**
146
+     * Helper function to send an email.
147
+     *
148
+     * @param WPInv_Subscription $subscription
149
+     * @param GetPaid_Notification_Email $email
150
+     * @param string $type
151
+     * @param array $extra_args Extra template args.
152
+     */
153
+    public function send_email( $subscription, $email, $type, $extra_args = array() ) {
154
+
155
+        // Abort in case the parent invoice does not exist.
156
+        $invoice = $subscription->get_parent_invoice();
157
+        if ( ! $this->should_send_notification( $invoice ) ) {
158
+            return;
159
+        }
160
+
161
+        if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) {
162
+            return;
163
+        }
164
+
165
+        do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email );
166
+
167
+        $recipients  = $this->get_recipients( $invoice );
168
+        $mailer      = new GetPaid_Notification_Email_Sender();
169
+        $merge_tags  = $email->get_merge_tags();
170
+        $content     = $email->get_content( $merge_tags, $extra_args );
171
+        $subject     = $email->add_merge_tags( $email->get_subject(), $merge_tags );
172
+        $attachments = $email->get_attachments();
173
+
174
+        $result = $mailer->send(
175
+            apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ),
176
+            $subject,
177
+            $content,
178
+            $attachments
179
+        );
180
+
181
+        // Maybe send a copy to the admin.
182
+        if ( $email->include_admin_bcc() ) {
183
+            $mailer->send(
184
+                wpinv_get_admin_email(),
185
+                $subject . __( ' - ADMIN BCC COPY', 'invoicing' ),
186
+                $content,
187
+                $attachments
188
+            );
189
+        }
190
+
191
+        if ( $result ) {
192
+            $invoice->add_system_note(
193
+                sprintf(
194
+                    __( 'Successfully sent %s notification email to %s.', 'invoicing' ),
195
+                    sanitize_key( $type ),
196
+                    $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
197
+                )
198
+            );
199
+        } else {
200
+            $invoice->add_system_note(
201
+                sprintf(
202
+                    __( 'Failed sending %s notification email to %s.', 'invoicing' ),
203
+                    sanitize_key( $type ),
204
+                    $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
205
+                )
206
+            );	
207
+        }
208
+
209
+        do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email );
260 210
 
261
-	/**
262
-	 * Sends a subscription renewal reminder notification.
263
-	 *
264
-	 */
265
-	public function renewal_reminder() {
211
+    }
266 212
 
267
-		$email = new GetPaid_Notification_Email( __FUNCTION__ );
213
+    /**
214
+     * Sends a new trial notification.
215
+     *
216
+     * @param WPInv_Subscription $subscription
217
+     */
218
+    public function subscription_trial( $subscription ) {
268 219
 
269
-		// Fetch reminder days.
270
-		$reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) );
220
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
221
+        $this->send_email( $subscription, $email, __FUNCTION__ );
271 222
 
272
-		// Abort if non is set.
273
-		if ( empty( $reminder_days ) ) {
274
-			return;
275
-		}
223
+    }
276 224
 
277
-		// Fetch matching subscriptions.
225
+    /**
226
+     * Sends a cancelled subscription notification.
227
+     *
228
+     * @param WPInv_Subscription $subscription
229
+     */
230
+    public function subscription_cancelled( $subscription ) {
231
+
232
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
233
+        $this->send_email( $subscription, $email, __FUNCTION__ );
234
+
235
+    }
236
+
237
+    /**
238
+     * Sends a subscription expired notification.
239
+     *
240
+     * @param WPInv_Subscription $subscription
241
+     */
242
+    public function subscription_expired( $subscription ) {
243
+
244
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
245
+        $this->send_email( $subscription, $email, __FUNCTION__ );
246
+
247
+    }
248
+
249
+    /**
250
+     * Sends a completed subscription notification.
251
+     *
252
+     * @param WPInv_Subscription $subscription
253
+     */
254
+    public function subscription_complete( $subscription ) {
255
+
256
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
257
+        $this->send_email( $subscription, $email, __FUNCTION__ );
258
+
259
+    }
260
+
261
+    /**
262
+     * Sends a subscription renewal reminder notification.
263
+     *
264
+     */
265
+    public function renewal_reminder() {
266
+
267
+        $email = new GetPaid_Notification_Email( __FUNCTION__ );
268
+
269
+        // Fetch reminder days.
270
+        $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) );
271
+
272
+        // Abort if non is set.
273
+        if ( empty( $reminder_days ) ) {
274
+            return;
275
+        }
276
+
277
+        // Fetch matching subscriptions.
278 278
         $args  = array(
279 279
             'number'             => -1,
280
-			'count_total'        => false,
281
-			'status'             => 'trialling active',
280
+            'count_total'        => false,
281
+            'status'             => 'trialling active',
282 282
             'date_expires_query' => array(
283
-				'relation'  => 'OR'
283
+                'relation'  => 'OR'
284 284
             ),
285
-		);
285
+        );
286 286
 
287
-		foreach ( $reminder_days as $days ) {
288
-			$date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) );
287
+        foreach ( $reminder_days as $days ) {
288
+            $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) );
289 289
 
290
-			$args['date_expires_query'][] = array(
291
-				'year'  => $date['year'],
292
-				'month' => $date['month'],
293
-				'day'   => $date['day'],
294
-			);
290
+            $args['date_expires_query'][] = array(
291
+                'year'  => $date['year'],
292
+                'month' => $date['month'],
293
+                'day'   => $date['day'],
294
+            );
295 295
 
296
-		}
296
+        }
297 297
 
298
-		$subscriptions = new GetPaid_Subscriptions_Query( $args );
298
+        $subscriptions = new GetPaid_Subscriptions_Query( $args );
299 299
 
300 300
         foreach ( $subscriptions as $subscription ) {
301 301
 
302
-			// Skip packages.
303
-			if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) {
304
-				$email->object = $subscription;
305
-            	$this->send_email( $subscription, $email, __FUNCTION__ );
306
-			}
302
+            // Skip packages.
303
+            if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) {
304
+                $email->object = $subscription;
305
+                $this->send_email( $subscription, $email, __FUNCTION__ );
306
+            }
307 307
 
308
-		}
308
+        }
309 309
 
310
-	}
310
+    }
311 311
 
312 312
 }
Please login to merge, or discard this patch.
includes/class-getpaid-notification-email.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -13,36 +13,36 @@  discard block
 block discarded – undo
13 13
 class GetPaid_Notification_Email {
14 14
 
15 15
     /**
16
-	 * Contains the type of this notification email.
17
-	 *
18
-	 * @var string
19
-	 */
16
+     * Contains the type of this notification email.
17
+     *
18
+     * @var string
19
+     */
20 20
     public $id;
21 21
 
22 22
     /**
23
-	 * Contains any object to use in filters.
24
-	 *
25
-	 * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription
26
-	 */
23
+     * Contains any object to use in filters.
24
+     *
25
+     * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription
26
+     */
27 27
     public $object;
28 28
 
29 29
     /**
30
-	 * Class constructor.
31
-	 *
30
+     * Class constructor.
31
+     *
32 32
      * @param string $id Email Type.
33 33
      * @param mixed $object Optional. Associated object.
34
-	 */
35
-	public function __construct( $id, $object = false ) {
34
+     */
35
+    public function __construct( $id, $object = false ) {
36 36
         $this->id     = $id;
37 37
         $this->object = $object;
38 38
     }
39 39
 
40 40
     /**
41
-	 * Retrieves an option
42
-	 *
41
+     * Retrieves an option
42
+     *
43 43
      * @return mixed
44
-	 */
45
-	public function get_option( $key ) {
44
+     */
45
+    public function get_option( $key ) {
46 46
 
47 47
         $key   = "email_{$this->id}_$key";
48 48
         $value = wpinv_get_option( $key, null );
@@ -60,80 +60,80 @@  discard block
 block discarded – undo
60 60
     }
61 61
 
62 62
     /**
63
-	 * Retrieves the email body.
64
-	 *
63
+     * Retrieves the email body.
64
+     *
65 65
      * @return string
66
-	 */
67
-	public function get_body() {
66
+     */
67
+    public function get_body() {
68 68
         $body = $this->get_option( 'body' );
69 69
         return apply_filters( 'getpaid_get_email_body', $body, $this->id, $this->object );
70 70
     }
71 71
 
72 72
     /**
73
-	 * Retrieves the email subject.
74
-	 *
73
+     * Retrieves the email subject.
74
+     *
75 75
      * @return string
76
-	 */
77
-	public function get_subject() {
76
+     */
77
+    public function get_subject() {
78 78
         $subject = $this->get_option( 'subject' );
79 79
         return apply_filters( 'getpaid_get_email_subject', $subject, $this->id, $this->object );
80 80
     }
81 81
 
82 82
     /**
83
-	 * Retrieves the email heading.
84
-	 *
83
+     * Retrieves the email heading.
84
+     *
85 85
      * @return string
86
-	 */
87
-	public function get_heading() {
86
+     */
87
+    public function get_heading() {
88 88
         $heading = $this->get_option( 'heading' );
89 89
         return apply_filters( 'getpaid_get_email_heading', $heading, $this->id, $this->object );
90 90
     }
91 91
 
92 92
     /**
93
-	 * Checks if an email is active.
94
-	 *
93
+     * Checks if an email is active.
94
+     *
95 95
      * @return bool
96
-	 */
97
-	public function is_active() {
96
+     */
97
+    public function is_active() {
98 98
         $is_active = ! empty( $this->get_option( 'active' ) );
99 99
         return apply_filters( 'getpaid_email_type_is_active', $is_active, $this->id, $this->object );
100 100
     }
101 101
 
102 102
     /**
103
-	 * Checks if the site's admin should receive email notifications.
104
-	 *
103
+     * Checks if the site's admin should receive email notifications.
104
+     *
105 105
      * @return bool
106
-	 */
107
-	public function include_admin_bcc() {
106
+     */
107
+    public function include_admin_bcc() {
108 108
         $include_admin_bcc = ! empty( $this->get_option( 'admin_bcc' ) );
109 109
         return apply_filters( 'getpaid_email_type_include_admin_bcc', $include_admin_bcc, $this->id, $this->object );
110 110
     }
111 111
 
112 112
     /**
113
-	 * Checks whether this email should be sent to the customer or admin.
114
-	 *
113
+     * Checks whether this email should be sent to the customer or admin.
114
+     *
115 115
      * @return bool
116
-	 */
117
-	public function is_admin_email() {
116
+     */
117
+    public function is_admin_email() {
118 118
         $is_admin_email = in_array( $this->id, array( 'new_invoice', 'failed_invoice' ) );
119 119
         return apply_filters( 'getpaid_email_type_is_admin_email', $is_admin_email, $this->id, $this->object );
120 120
     }
121 121
 
122 122
     /**
123
-	 * Returns email attachments.
124
-	 *
123
+     * Returns email attachments.
124
+     *
125 125
      * @return array
126
-	 */
127
-	public function get_attachments() {
126
+     */
127
+    public function get_attachments() {
128 128
         return apply_filters( 'getpaid_get_email_attachments', array(), $this->id, $this->object );
129 129
     }
130 130
 
131 131
     /**
132
-	 * Returns an array of merge tags.
133
-	 *
132
+     * Returns an array of merge tags.
133
+     *
134 134
      * @return array
135
-	 */
136
-	public function get_merge_tags() {
135
+     */
136
+    public function get_merge_tags() {
137 137
 
138 138
         $merge_tags = array(
139 139
             '{site_title}' => wpinv_get_blogname(),
@@ -144,13 +144,13 @@  discard block
 block discarded – undo
144 144
     }
145 145
 
146 146
     /**
147
-	 * Adds merge tags to a text.
148
-	 *
147
+     * Adds merge tags to a text.
148
+     *
149 149
      * @param string string $text
150 150
      * @param array $merge_tags
151 151
      * @return string
152
-	 */
153
-	public function add_merge_tags( $text, $merge_tags = array() ) {
152
+     */
153
+    public function add_merge_tags( $text, $merge_tags = array() ) {
154 154
 
155 155
         foreach ( $merge_tags as $key => $value ) {
156 156
             $text = str_replace( $key, $value, $text );
@@ -160,13 +160,13 @@  discard block
 block discarded – undo
160 160
     }
161 161
 
162 162
     /**
163
-	 * Returns the email content
164
-	 *
163
+     * Returns the email content
164
+     *
165 165
      * @param array $merge_tags
166 166
      * @param array $extra_args Extra template args
167 167
      * @return string
168
-	 */
169
-	public function get_content( $merge_tags = array(), $extra_args = array() ) {
168
+     */
169
+    public function get_content( $merge_tags = array(), $extra_args = array() ) {
170 170
 
171 171
         $content = wpinv_get_template_html(
172 172
             "emails/wpinv-email-{$this->id}.php",
Please login to merge, or discard this patch.
includes/wpinv-subscription.php 1 patch
Indentation   +1035 added lines, -1035 removed lines patch added patch discarded remove patch
@@ -15,125 +15,125 @@  discard block
 block discarded – undo
15 15
  */
16 16
 class WPInv_Subscription extends GetPaid_Data {
17 17
 
18
-	/**
19
-	 * Which data store to load.
20
-	 *
21
-	 * @var string
22
-	 */
23
-	protected $data_store_name = 'subscription';
24
-
25
-	/**
26
-	 * This is the name of this object type.
27
-	 *
28
-	 * @var string
29
-	 */
30
-	protected $object_type = 'subscription';
31
-
32
-	/**
33
-	 * Item Data array. This is the core item data exposed in APIs.
34
-	 *
35
-	 * @since 1.0.19
36
-	 * @var array
37
-	 */
38
-	protected $data = array(
39
-		'customer_id'       => 0,
40
-		'frequency'         => 1,
41
-		'period'            => 'D',
42
-		'initial_amount'    => null,
43
-		'recurring_amount'  => null,
44
-		'bill_times'        => 0,
45
-		'transaction_id'    => '',
46
-		'parent_payment_id' => null,
47
-		'product_id'        => 0,
48
-		'created'           => '0000-00-00 00:00:00',
49
-		'expiration'        => '0000-00-00 00:00:00',
50
-		'trial_period'      => '',
51
-		'status'            => 'pending',
52
-		'profile_id'        => '',
53
-		'gateway'           => '',
54
-		'customer'          => '',
55
-	);
56
-
57
-	/**
58
-	 * Stores the status transition information.
59
-	 *
60
-	 * @since 1.0.19
61
-	 * @var bool
62
-	 */
63
-	protected $status_transition = false;
64
-
65
-	/**
66
-	 * Get the subscription if ID is passed, otherwise the subscription is new and empty.
67
-	 *
68
-	 * @param  int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read.
69
-	 * @param  bool $deprecated
70
-	 */
71
-	function __construct( $subscription = 0, $deprecated = false ) {
72
-
73
-		parent::__construct( $subscription );
74
-
75
-		if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) {
76
-			$this->set_id( $subscription );
77
-		} elseif ( $subscription instanceof self ) {
78
-			$this->set_id( $subscription->get_id() );
79
-		} elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) {
80
-			$this->set_id( $subscription_id );
81
-		} elseif ( ! empty( $subscription->id ) ) {
82
-			$this->set_id( $subscription->id );
83
-		} else {
84
-			$this->set_object_read( true );
85
-		}
86
-
87
-		// Load the datastore.
88
-		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
89
-
90
-		if ( $this->get_id() > 0 ) {
91
-			$this->data_store->read( $this );
92
-		}
93
-
94
-	}
95
-
96
-	/**
97
-	 * Given an invoice id, profile id, transaction id, it returns the subscription's id.
98
-	 *
99
-	 *
100
-	 * @static
101
-	 * @param string $value
102
-	 * @param string $field Either invoice_id, transaction_id or profile_id.
103
-	 * @since 1.0.19
104
-	 * @return int
105
-	 */
106
-	public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) {
18
+    /**
19
+     * Which data store to load.
20
+     *
21
+     * @var string
22
+     */
23
+    protected $data_store_name = 'subscription';
24
+
25
+    /**
26
+     * This is the name of this object type.
27
+     *
28
+     * @var string
29
+     */
30
+    protected $object_type = 'subscription';
31
+
32
+    /**
33
+     * Item Data array. This is the core item data exposed in APIs.
34
+     *
35
+     * @since 1.0.19
36
+     * @var array
37
+     */
38
+    protected $data = array(
39
+        'customer_id'       => 0,
40
+        'frequency'         => 1,
41
+        'period'            => 'D',
42
+        'initial_amount'    => null,
43
+        'recurring_amount'  => null,
44
+        'bill_times'        => 0,
45
+        'transaction_id'    => '',
46
+        'parent_payment_id' => null,
47
+        'product_id'        => 0,
48
+        'created'           => '0000-00-00 00:00:00',
49
+        'expiration'        => '0000-00-00 00:00:00',
50
+        'trial_period'      => '',
51
+        'status'            => 'pending',
52
+        'profile_id'        => '',
53
+        'gateway'           => '',
54
+        'customer'          => '',
55
+    );
56
+
57
+    /**
58
+     * Stores the status transition information.
59
+     *
60
+     * @since 1.0.19
61
+     * @var bool
62
+     */
63
+    protected $status_transition = false;
64
+
65
+    /**
66
+     * Get the subscription if ID is passed, otherwise the subscription is new and empty.
67
+     *
68
+     * @param  int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read.
69
+     * @param  bool $deprecated
70
+     */
71
+    function __construct( $subscription = 0, $deprecated = false ) {
72
+
73
+        parent::__construct( $subscription );
74
+
75
+        if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) {
76
+            $this->set_id( $subscription );
77
+        } elseif ( $subscription instanceof self ) {
78
+            $this->set_id( $subscription->get_id() );
79
+        } elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) {
80
+            $this->set_id( $subscription_id );
81
+        } elseif ( ! empty( $subscription->id ) ) {
82
+            $this->set_id( $subscription->id );
83
+        } else {
84
+            $this->set_object_read( true );
85
+        }
86
+
87
+        // Load the datastore.
88
+        $this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
89
+
90
+        if ( $this->get_id() > 0 ) {
91
+            $this->data_store->read( $this );
92
+        }
93
+
94
+    }
95
+
96
+    /**
97
+     * Given an invoice id, profile id, transaction id, it returns the subscription's id.
98
+     *
99
+     *
100
+     * @static
101
+     * @param string $value
102
+     * @param string $field Either invoice_id, transaction_id or profile_id.
103
+     * @since 1.0.19
104
+     * @return int
105
+     */
106
+    public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) {
107 107
         global $wpdb;
108 108
 
109
-		// Trim the value.
110
-		$value = trim( $value );
109
+        // Trim the value.
110
+        $value = trim( $value );
111 111
 
112
-		if ( empty( $value ) ) {
113
-			return 0;
114
-		}
112
+        if ( empty( $value ) ) {
113
+            return 0;
114
+        }
115 115
 
116
-		if ( 'invoice_id' == $field ) {
117
-			$field = 'parent_payment_id';
118
-		}
116
+        if ( 'invoice_id' == $field ) {
117
+            $field = 'parent_payment_id';
118
+        }
119 119
 
120 120
         // Valid fields.
121 121
         $fields = array(
122
-			'parent_payment_id',
123
-			'transaction_id',
124
-			'profile_id'
125
-		);
126
-
127
-		// Ensure a field has been passed.
128
-		if ( empty( $field ) || ! in_array( $field, $fields ) ) {
129
-			return 0;
130
-		}
131
-
132
-		// Maybe retrieve from the cache.
133
-		$subscription_id   = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" );
134
-		if ( ! empty( $subscription_id ) ) {
135
-			return $subscription_id;
136
-		}
122
+            'parent_payment_id',
123
+            'transaction_id',
124
+            'profile_id'
125
+        );
126
+
127
+        // Ensure a field has been passed.
128
+        if ( empty( $field ) || ! in_array( $field, $fields ) ) {
129
+            return 0;
130
+        }
131
+
132
+        // Maybe retrieve from the cache.
133
+        $subscription_id   = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" );
134
+        if ( ! empty( $subscription_id ) ) {
135
+            return $subscription_id;
136
+        }
137 137
 
138 138
         // Fetch from the db.
139 139
         $table            = $wpdb->prefix . 'wpinv_subscriptions';
@@ -141,34 +141,34 @@  discard block
 block discarded – undo
141 141
             $wpdb->prepare( "SELECT `id` FROM $table WHERE `$field`=%s LIMIT 1", $value )
142 142
         );
143 143
 
144
-		if ( empty( $subscription_id ) ) {
145
-			return 0;
146
-		}
144
+        if ( empty( $subscription_id ) ) {
145
+            return 0;
146
+        }
147 147
 
148
-		// Update the cache with our data.
149
-		wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" );
148
+        // Update the cache with our data.
149
+        wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" );
150 150
 
151
-		return $subscription_id;
152
-	}
151
+        return $subscription_id;
152
+    }
153 153
 
154
-	/**
154
+    /**
155 155
      * Clears the subscription's cache.
156 156
      */
157 157
     public function clear_cache() {
158
-		wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' );
159
-		wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' );
160
-		wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' );
161
-		wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' );
162
-	}
158
+        wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' );
159
+        wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' );
160
+        wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' );
161
+        wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' );
162
+    }
163 163
 
164
-	/**
164
+    /**
165 165
      * Checks if a subscription key is set.
166 166
      */
167 167
     public function _isset( $key ) {
168 168
         return isset( $this->data[$key] ) || method_exists( $this, "get_$key" );
169
-	}
169
+    }
170 170
 
171
-	/*
171
+    /*
172 172
 	|--------------------------------------------------------------------------
173 173
 	| CRUD methods
174 174
 	|--------------------------------------------------------------------------
@@ -177,545 +177,545 @@  discard block
 block discarded – undo
177 177
 	|
178 178
     */
179 179
 
180
-	/*
181
-	|--------------------------------------------------------------------------
182
-	| Getters
183
-	|--------------------------------------------------------------------------
184
-	*/
180
+    /*
181
+	|--------------------------------------------------------------------------
182
+	| Getters
183
+	|--------------------------------------------------------------------------
184
+	*/
185
+
186
+    /**
187
+     * Get customer id.
188
+     *
189
+     * @since 1.0.19
190
+     * @param  string $context View or edit context.
191
+     * @return int
192
+     */
193
+    public function get_customer_id( $context = 'view' ) {
194
+        return (int) $this->get_prop( 'customer_id', $context );
195
+    }
196
+
197
+    /**
198
+     * Get customer information.
199
+     *
200
+     * @since 1.0.19
201
+     * @param  string $context View or edit context.
202
+     * @return WP_User|false WP_User object on success, false on failure.
203
+     */
204
+    public function get_customer( $context = 'view' ) {
205
+        return get_userdata( $this->get_customer_id( $context ) );
206
+    }
207
+
208
+    /**
209
+     * Get parent invoice id.
210
+     *
211
+     * @since 1.0.19
212
+     * @param  string $context View or edit context.
213
+     * @return int
214
+     */
215
+    public function get_parent_invoice_id( $context = 'view' ) {
216
+        return (int) $this->get_prop( 'parent_payment_id', $context );
217
+    }
218
+
219
+    /**
220
+     * Alias for self::get_parent_invoice_id().
221
+     *
222
+     * @since 1.0.19
223
+     * @param  string $context View or edit context.
224
+     * @return int
225
+     */
226
+    public function get_parent_payment_id( $context = 'view' ) {
227
+        return $this->get_parent_invoice_id( $context );
228
+    }
229
+
230
+    /**
231
+     * Alias for self::get_parent_invoice_id().
232
+     *
233
+     * @since  1.0.0
234
+     * @return int
235
+     */
236
+    public function get_original_payment_id( $context = 'view' ) {
237
+        return $this->get_parent_invoice_id( $context );
238
+    }
239
+
240
+    /**
241
+     * Get parent invoice.
242
+     *
243
+     * @since 1.0.19
244
+     * @param  string $context View or edit context.
245
+     * @return WPInv_Invoice
246
+     */
247
+    public function get_parent_invoice( $context = 'view' ) {
248
+        return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) );
249
+    }
250
+
251
+    /**
252
+     * Alias for self::get_parent_invoice().
253
+     *
254
+     * @since 1.0.19
255
+     * @param  string $context View or edit context.
256
+     * @return WPInv_Invoice
257
+     */
258
+    public function get_parent_payment( $context = 'view' ) {
259
+        return $this->get_parent_invoice( $context );
260
+    }
261
+
262
+    /**
263
+     * Get subscription's product id.
264
+     *
265
+     * @since 1.0.19
266
+     * @param  string $context View or edit context.
267
+     * @return int
268
+     */
269
+    public function get_product_id( $context = 'view' ) {
270
+        return (int) $this->get_prop( 'product_id', $context );
271
+    }
272
+
273
+    /**
274
+     * Get the subscription product.
275
+     *
276
+     * @since 1.0.19
277
+     * @param  string $context View or edit context.
278
+     * @return WPInv_Item
279
+     */
280
+    public function get_product( $context = 'view' ) {
281
+        return new WPInv_Item( $this->get_product_id( $context ) );
282
+    }
283
+
284
+    /**
285
+     * Get parent invoice's gateway.
286
+     *
287
+     * Here for backwards compatibility.
288
+     *
289
+     * @since 1.0.19
290
+     * @param  string $context View or edit context.
291
+     * @return string
292
+     */
293
+    public function get_gateway( $context = 'view' ) {
294
+        return $this->get_parent_invoice( $context )->get_gateway();
295
+    }
296
+
297
+    /**
298
+     * Get the period of a renewal.
299
+     *
300
+     * @since 1.0.19
301
+     * @param  string $context View or edit context.
302
+     * @return string
303
+     */
304
+    public function get_period( $context = 'view' ) {
305
+        return $this->get_prop( 'period', $context );
306
+    }
307
+
308
+    /**
309
+     * Get number of periods each renewal is valid for.
310
+     *
311
+     * @since 1.0.19
312
+     * @param  string $context View or edit context.
313
+     * @return int
314
+     */
315
+    public function get_frequency( $context = 'view' ) {
316
+        return (int) $this->get_prop( 'frequency', $context );
317
+    }
318
+
319
+    /**
320
+     * Get the initial amount for the subscription.
321
+     *
322
+     * @since 1.0.19
323
+     * @param  string $context View or edit context.
324
+     * @return float
325
+     */
326
+    public function get_initial_amount( $context = 'view' ) {
327
+        return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) );
328
+    }
329
+
330
+    /**
331
+     * Get the recurring amount for the subscription.
332
+     *
333
+     * @since 1.0.19
334
+     * @param  string $context View or edit context.
335
+     * @return float
336
+     */
337
+    public function get_recurring_amount( $context = 'view' ) {
338
+        return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) );
339
+    }
340
+
341
+    /**
342
+     * Get number of times that this subscription can be renewed.
343
+     *
344
+     * @since 1.0.19
345
+     * @param  string $context View or edit context.
346
+     * @return int
347
+     */
348
+    public function get_bill_times( $context = 'view' ) {
349
+        return (int) $this->get_prop( 'bill_times', $context );
350
+    }
351
+
352
+    /**
353
+     * Get transaction id of this subscription's parent invoice.
354
+     *
355
+     * @since 1.0.19
356
+     * @param  string $context View or edit context.
357
+     * @return string
358
+     */
359
+    public function get_transaction_id( $context = 'view' ) {
360
+        return $this->get_prop( 'transaction_id', $context );
361
+    }
362
+
363
+    /**
364
+     * Get the date that the subscription was created.
365
+     *
366
+     * @since 1.0.19
367
+     * @param  string $context View or edit context.
368
+     * @return string
369
+     */
370
+    public function get_created( $context = 'view' ) {
371
+        return $this->get_prop( 'created', $context );
372
+    }
373
+
374
+    /**
375
+     * Alias for self::get_created().
376
+     *
377
+     * @since 1.0.19
378
+     * @param  string $context View or edit context.
379
+     * @return string
380
+     */
381
+    public function get_date_created( $context = 'view' ) {
382
+        return $this->get_created( $context );
383
+    }
384
+
385
+    /**
386
+     * Retrieves the creation date in a timestamp
387
+     *
388
+     * @since  1.0.0
389
+     * @return int
390
+     */
391
+    public function get_time_created() {
392
+        $created = $this->get_date_created();
393
+        return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) );
394
+    }
395
+
396
+    /**
397
+     * Get GMT date when the subscription was created.
398
+     *
399
+     * @since 1.0.19
400
+     * @param  string $context View or edit context.
401
+     * @return string
402
+     */
403
+    public function get_date_created_gmt( $context = 'view' ) {
404
+        $date = $this->get_date_created( $context );
405
+
406
+        if ( $date ) {
407
+            $date = get_gmt_from_date( $date );
408
+        }
409
+        return $date;
410
+    }
411
+
412
+    /**
413
+     * Get the date that the subscription will renew.
414
+     *
415
+     * @since 1.0.19
416
+     * @param  string $context View or edit context.
417
+     * @return string
418
+     */
419
+    public function get_next_renewal_date( $context = 'view' ) {
420
+        return $this->get_prop( 'expiration', $context );
421
+    }
422
+
423
+    /**
424
+     * Alias for self::get_next_renewal_date().
425
+     *
426
+     * @since 1.0.19
427
+     * @param  string $context View or edit context.
428
+     * @return string
429
+     */
430
+    public function get_expiration( $context = 'view' ) {
431
+        return $this->get_next_renewal_date( $context );
432
+    }
433
+
434
+    /**
435
+     * Retrieves the expiration date in a timestamp
436
+     *
437
+     * @since  1.0.0
438
+     * @return int
439
+     */
440
+    public function get_expiration_time() {
441
+        $expiration = $this->get_expiration();
442
+
443
+        if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) {
444
+            return current_time( 'timestamp' );
445
+        }
446
+
447
+        $expiration = strtotime( $expiration, current_time( 'timestamp' ) );
448
+        return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration;
449
+    }
450
+
451
+    /**
452
+     * Get GMT date when the subscription will renew.
453
+     *
454
+     * @since 1.0.19
455
+     * @param  string $context View or edit context.
456
+     * @return string
457
+     */
458
+    public function get_next_renewal_date_gmt( $context = 'view' ) {
459
+        $date = $this->get_next_renewal_date( $context );
460
+
461
+        if ( $date ) {
462
+            $date = get_gmt_from_date( $date );
463
+        }
464
+        return $date;
465
+    }
466
+
467
+    /**
468
+     * Get the subscription's trial period.
469
+     *
470
+     * @since 1.0.19
471
+     * @param  string $context View or edit context.
472
+     * @return string
473
+     */
474
+    public function get_trial_period( $context = 'view' ) {
475
+        return $this->get_prop( 'trial_period', $context );
476
+    }
477
+
478
+    /**
479
+     * Get the subscription's status.
480
+     *
481
+     * @since 1.0.19
482
+     * @param  string $context View or edit context.
483
+     * @return string
484
+     */
485
+    public function get_status( $context = 'view' ) {
486
+        return $this->get_prop( 'status', $context );
487
+    }
488
+
489
+    /**
490
+     * Get the subscription's profile id.
491
+     *
492
+     * @since 1.0.19
493
+     * @param  string $context View or edit context.
494
+     * @return string
495
+     */
496
+    public function get_profile_id( $context = 'view' ) {
497
+        return $this->get_prop( 'profile_id', $context );
498
+    }
499
+
500
+    /*
501
+	|--------------------------------------------------------------------------
502
+	| Setters
503
+	|--------------------------------------------------------------------------
504
+	*/
505
+
506
+    /**
507
+     * Set customer id.
508
+     *
509
+     * @since 1.0.19
510
+     * @param  int $value The customer's id.
511
+     */
512
+    public function set_customer_id( $value ) {
513
+        $this->set_prop( 'customer_id', (int) $value );
514
+    }
515
+
516
+    /**
517
+     * Set parent invoice id.
518
+     *
519
+     * @since 1.0.19
520
+     * @param  int $value The parent invoice id.
521
+     */
522
+    public function set_parent_invoice_id( $value ) {
523
+        $this->set_prop( 'parent_payment_id', (int) $value );
524
+    }
525
+
526
+    /**
527
+     * Alias for self::set_parent_invoice_id().
528
+     *
529
+     * @since 1.0.19
530
+     * @param  int $value The parent invoice id.
531
+     */
532
+    public function set_parent_payment_id( $value ) {
533
+        $this->set_parent_invoice_id( $value );
534
+    }
535
+
536
+    /**
537
+     * Alias for self::set_parent_invoice_id().
538
+     *
539
+     * @since 1.0.19
540
+     * @param  int $value The parent invoice id.
541
+     */
542
+    public function set_original_payment_id( $value ) {
543
+        $this->set_parent_invoice_id( $value );
544
+    }
545
+
546
+    /**
547
+     * Set subscription's product id.
548
+     *
549
+     * @since 1.0.19
550
+     * @param  int $value The subscription product id.
551
+     */
552
+    public function set_product_id( $value ) {
553
+        $this->set_prop( 'product_id', (int) $value );
554
+    }
555
+
556
+    /**
557
+     * Set the period of a renewal.
558
+     *
559
+     * @since 1.0.19
560
+     * @param  string $value The renewal period.
561
+     */
562
+    public function set_period( $value ) {
563
+        $this->set_prop( 'period', $value );
564
+    }
565
+
566
+    /**
567
+     * Set number of periods each renewal is valid for.
568
+     *
569
+     * @since 1.0.19
570
+     * @param  int $value The subscription frequency.
571
+     */
572
+    public function set_frequency( $value ) {
573
+        $value = empty( $value ) ? 1 : (int) $value;
574
+        $this->set_prop( 'frequency', absint( $value ) );
575
+    }
576
+
577
+    /**
578
+     * Set the initial amount for the subscription.
579
+     *
580
+     * @since 1.0.19
581
+     * @param  float $value The initial subcription amount.
582
+     */
583
+    public function set_initial_amount( $value ) {
584
+        $this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) );
585
+    }
586
+
587
+    /**
588
+     * Set the recurring amount for the subscription.
589
+     *
590
+     * @since 1.0.19
591
+     * @param  float $value The recurring subcription amount.
592
+     */
593
+    public function set_recurring_amount( $value ) {
594
+        $this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) );
595
+    }
596
+
597
+    /**
598
+     * Set number of times that this subscription can be renewed.
599
+     *
600
+     * @since 1.0.19
601
+     * @param  int $value Bill times.
602
+     */
603
+    public function set_bill_times( $value ) {
604
+        $this->set_prop( 'bill_times', (int) $value );
605
+    }
606
+
607
+    /**
608
+     * Get transaction id of this subscription's parent invoice.
609
+     *
610
+     * @since 1.0.19
611
+     * @param string $value Bill times.
612
+     */
613
+    public function set_transaction_id( $value ) {
614
+        $this->set_prop( 'transaction_id', sanitize_text_field( $value ) );
615
+    }
616
+
617
+    /**
618
+     * Set date when this subscription started.
619
+     *
620
+     * @since 1.0.19
621
+     * @param string $value strtotime compliant date.
622
+     */
623
+    public function set_created( $value ) {
624
+        $date = strtotime( $value );
625
+
626
+        if ( $date && $value !== '0000-00-00 00:00:00' ) {
627
+            $this->set_prop( 'created', date( 'Y-m-d H:i:s', $date ) );
628
+            return;
629
+        }
630
+
631
+        $this->set_prop( 'created', '' );
185 632
 
186
-	/**
187
-	 * Get customer id.
188
-	 *
189
-	 * @since 1.0.19
190
-	 * @param  string $context View or edit context.
191
-	 * @return int
192
-	 */
193
-	public function get_customer_id( $context = 'view' ) {
194
-		return (int) $this->get_prop( 'customer_id', $context );
195
-	}
196
-
197
-	/**
198
-	 * Get customer information.
199
-	 *
200
-	 * @since 1.0.19
201
-	 * @param  string $context View or edit context.
202
-	 * @return WP_User|false WP_User object on success, false on failure.
203
-	 */
204
-	public function get_customer( $context = 'view' ) {
205
-		return get_userdata( $this->get_customer_id( $context ) );
206
-	}
207
-
208
-	/**
209
-	 * Get parent invoice id.
210
-	 *
211
-	 * @since 1.0.19
212
-	 * @param  string $context View or edit context.
213
-	 * @return int
214
-	 */
215
-	public function get_parent_invoice_id( $context = 'view' ) {
216
-		return (int) $this->get_prop( 'parent_payment_id', $context );
217
-	}
218
-
219
-	/**
220
-	 * Alias for self::get_parent_invoice_id().
221
-	 *
222
-	 * @since 1.0.19
223
-	 * @param  string $context View or edit context.
224
-	 * @return int
225
-	 */
226
-    public function get_parent_payment_id( $context = 'view' ) {
227
-        return $this->get_parent_invoice_id( $context );
228
-	}
633
+    }
229 634
 
230
-	/**
231
-     * Alias for self::get_parent_invoice_id().
635
+    /**
636
+     * Alias for self::set_created().
232 637
      *
233
-     * @since  1.0.0
234
-     * @return int
638
+     * @since 1.0.19
639
+     * @param string $value strtotime compliant date.
235 640
      */
236
-    public function get_original_payment_id( $context = 'view' ) {
237
-        return $this->get_parent_invoice_id( $context );
641
+    public function set_date_created( $value ) {
642
+        $this->set_created( $value );
238 643
     }
239 644
 
240
-	/**
241
-	 * Get parent invoice.
242
-	 *
243
-	 * @since 1.0.19
244
-	 * @param  string $context View or edit context.
245
-	 * @return WPInv_Invoice
246
-	 */
247
-	public function get_parent_invoice( $context = 'view' ) {
248
-		return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) );
249
-	}
250
-
251
-	/**
252
-	 * Alias for self::get_parent_invoice().
253
-	 *
254
-	 * @since 1.0.19
255
-	 * @param  string $context View or edit context.
256
-	 * @return WPInv_Invoice
257
-	 */
258
-    public function get_parent_payment( $context = 'view' ) {
259
-        return $this->get_parent_invoice( $context );
260
-	}
261
-
262
-	/**
263
-	 * Get subscription's product id.
264
-	 *
265
-	 * @since 1.0.19
266
-	 * @param  string $context View or edit context.
267
-	 * @return int
268
-	 */
269
-	public function get_product_id( $context = 'view' ) {
270
-		return (int) $this->get_prop( 'product_id', $context );
271
-	}
272
-
273
-	/**
274
-	 * Get the subscription product.
275
-	 *
276
-	 * @since 1.0.19
277
-	 * @param  string $context View or edit context.
278
-	 * @return WPInv_Item
279
-	 */
280
-	public function get_product( $context = 'view' ) {
281
-		return new WPInv_Item( $this->get_product_id( $context ) );
282
-	}
283
-
284
-	/**
285
-	 * Get parent invoice's gateway.
286
-	 *
287
-	 * Here for backwards compatibility.
288
-	 *
289
-	 * @since 1.0.19
290
-	 * @param  string $context View or edit context.
291
-	 * @return string
292
-	 */
293
-	public function get_gateway( $context = 'view' ) {
294
-		return $this->get_parent_invoice( $context )->get_gateway();
295
-	}
296
-
297
-	/**
298
-	 * Get the period of a renewal.
299
-	 *
300
-	 * @since 1.0.19
301
-	 * @param  string $context View or edit context.
302
-	 * @return string
303
-	 */
304
-	public function get_period( $context = 'view' ) {
305
-		return $this->get_prop( 'period', $context );
306
-	}
307
-
308
-	/**
309
-	 * Get number of periods each renewal is valid for.
310
-	 *
311
-	 * @since 1.0.19
312
-	 * @param  string $context View or edit context.
313
-	 * @return int
314
-	 */
315
-	public function get_frequency( $context = 'view' ) {
316
-		return (int) $this->get_prop( 'frequency', $context );
317
-	}
318
-
319
-	/**
320
-	 * Get the initial amount for the subscription.
321
-	 *
322
-	 * @since 1.0.19
323
-	 * @param  string $context View or edit context.
324
-	 * @return float
325
-	 */
326
-	public function get_initial_amount( $context = 'view' ) {
327
-		return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) );
328
-	}
329
-
330
-	/**
331
-	 * Get the recurring amount for the subscription.
332
-	 *
333
-	 * @since 1.0.19
334
-	 * @param  string $context View or edit context.
335
-	 * @return float
336
-	 */
337
-	public function get_recurring_amount( $context = 'view' ) {
338
-		return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) );
339
-	}
340
-
341
-	/**
342
-	 * Get number of times that this subscription can be renewed.
343
-	 *
344
-	 * @since 1.0.19
345
-	 * @param  string $context View or edit context.
346
-	 * @return int
347
-	 */
348
-	public function get_bill_times( $context = 'view' ) {
349
-		return (int) $this->get_prop( 'bill_times', $context );
350
-	}
351
-
352
-	/**
353
-	 * Get transaction id of this subscription's parent invoice.
354
-	 *
355
-	 * @since 1.0.19
356
-	 * @param  string $context View or edit context.
357
-	 * @return string
358
-	 */
359
-	public function get_transaction_id( $context = 'view' ) {
360
-		return $this->get_prop( 'transaction_id', $context );
361
-	}
362
-
363
-	/**
364
-	 * Get the date that the subscription was created.
365
-	 *
366
-	 * @since 1.0.19
367
-	 * @param  string $context View or edit context.
368
-	 * @return string
369
-	 */
370
-	public function get_created( $context = 'view' ) {
371
-		return $this->get_prop( 'created', $context );
372
-	}
373
-
374
-	/**
375
-	 * Alias for self::get_created().
376
-	 *
377
-	 * @since 1.0.19
378
-	 * @param  string $context View or edit context.
379
-	 * @return string
380
-	 */
381
-	public function get_date_created( $context = 'view' ) {
382
-		return $this->get_created( $context );
383
-	}
384
-
385
-	/**
386
-	 * Retrieves the creation date in a timestamp
387
-	 *
388
-	 * @since  1.0.0
389
-	 * @return int
390
-	 */
391
-	public function get_time_created() {
392
-		$created = $this->get_date_created();
393
-		return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) );
394
-	}
395
-
396
-	/**
397
-	 * Get GMT date when the subscription was created.
398
-	 *
399
-	 * @since 1.0.19
400
-	 * @param  string $context View or edit context.
401
-	 * @return string
402
-	 */
403
-	public function get_date_created_gmt( $context = 'view' ) {
404
-        $date = $this->get_date_created( $context );
645
+    /**
646
+     * Set the date that the subscription will renew.
647
+     *
648
+     * @since 1.0.19
649
+     * @param string $value strtotime compliant date.
650
+     */
651
+    public function set_next_renewal_date( $value ) {
652
+        $date = strtotime( $value );
405 653
 
406
-        if ( $date ) {
407
-            $date = get_gmt_from_date( $date );
654
+        if ( $date && $value !== '0000-00-00 00:00:00' ) {
655
+            $this->set_prop( 'expiration', date( 'Y-m-d H:i:s', $date ) );
656
+            return;
408 657
         }
409
-		return $date;
410
-	}
411
-
412
-	/**
413
-	 * Get the date that the subscription will renew.
414
-	 *
415
-	 * @since 1.0.19
416
-	 * @param  string $context View or edit context.
417
-	 * @return string
418
-	 */
419
-	public function get_next_renewal_date( $context = 'view' ) {
420
-		return $this->get_prop( 'expiration', $context );
421
-	}
422
-
423
-	/**
424
-	 * Alias for self::get_next_renewal_date().
425
-	 *
426
-	 * @since 1.0.19
427
-	 * @param  string $context View or edit context.
428
-	 * @return string
429
-	 */
430
-	public function get_expiration( $context = 'view' ) {
431
-		return $this->get_next_renewal_date( $context );
432
-	}
433
-
434
-	/**
435
-	 * Retrieves the expiration date in a timestamp
436
-	 *
437
-	 * @since  1.0.0
438
-	 * @return int
439
-	 */
440
-	public function get_expiration_time() {
441
-		$expiration = $this->get_expiration();
442
-
443
-		if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) {
444
-			return current_time( 'timestamp' );
445
-		}
446
-
447
-		$expiration = strtotime( $expiration, current_time( 'timestamp' ) );
448
-		return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration;
449
-	}
450
-
451
-	/**
452
-	 * Get GMT date when the subscription will renew.
453
-	 *
454
-	 * @since 1.0.19
455
-	 * @param  string $context View or edit context.
456
-	 * @return string
457
-	 */
458
-	public function get_next_renewal_date_gmt( $context = 'view' ) {
459
-        $date = $this->get_next_renewal_date( $context );
460 658
 
461
-        if ( $date ) {
462
-            $date = get_gmt_from_date( $date );
463
-        }
464
-		return $date;
465
-	}
466
-
467
-	/**
468
-	 * Get the subscription's trial period.
469
-	 *
470
-	 * @since 1.0.19
471
-	 * @param  string $context View or edit context.
472
-	 * @return string
473
-	 */
474
-	public function get_trial_period( $context = 'view' ) {
475
-		return $this->get_prop( 'trial_period', $context );
476
-	}
477
-
478
-	/**
479
-	 * Get the subscription's status.
480
-	 *
481
-	 * @since 1.0.19
482
-	 * @param  string $context View or edit context.
483
-	 * @return string
484
-	 */
485
-	public function get_status( $context = 'view' ) {
486
-		return $this->get_prop( 'status', $context );
487
-	}
488
-
489
-	/**
490
-	 * Get the subscription's profile id.
491
-	 *
492
-	 * @since 1.0.19
493
-	 * @param  string $context View or edit context.
494
-	 * @return string
495
-	 */
496
-	public function get_profile_id( $context = 'view' ) {
497
-		return $this->get_prop( 'profile_id', $context );
498
-	}
499
-
500
-	/*
501
-	|--------------------------------------------------------------------------
502
-	| Setters
503
-	|--------------------------------------------------------------------------
504
-	*/
659
+        $this->set_prop( 'expiration', '' );
505 660
 
506
-	/**
507
-	 * Set customer id.
508
-	 *
509
-	 * @since 1.0.19
510
-	 * @param  int $value The customer's id.
511
-	 */
512
-	public function set_customer_id( $value ) {
513
-		$this->set_prop( 'customer_id', (int) $value );
514
-	}
515
-
516
-	/**
517
-	 * Set parent invoice id.
518
-	 *
519
-	 * @since 1.0.19
520
-	 * @param  int $value The parent invoice id.
521
-	 */
522
-	public function set_parent_invoice_id( $value ) {
523
-		$this->set_prop( 'parent_payment_id', (int) $value );
524
-	}
525
-
526
-	/**
527
-	 * Alias for self::set_parent_invoice_id().
528
-	 *
529
-	 * @since 1.0.19
530
-	 * @param  int $value The parent invoice id.
531
-	 */
532
-    public function set_parent_payment_id( $value ) {
533
-        $this->set_parent_invoice_id( $value );
534
-	}
661
+    }
535 662
 
536
-	/**
537
-     * Alias for self::set_parent_invoice_id().
663
+    /**
664
+     * Alias for self::set_next_renewal_date().
538 665
      *
539 666
      * @since 1.0.19
540
-	 * @param  int $value The parent invoice id.
667
+     * @param string $value strtotime compliant date.
541 668
      */
542
-    public function set_original_payment_id( $value ) {
543
-        $this->set_parent_invoice_id( $value );
544
-	}
545
-
546
-	/**
547
-	 * Set subscription's product id.
548
-	 *
549
-	 * @since 1.0.19
550
-	 * @param  int $value The subscription product id.
551
-	 */
552
-	public function set_product_id( $value ) {
553
-		$this->set_prop( 'product_id', (int) $value );
554
-	}
555
-
556
-	/**
557
-	 * Set the period of a renewal.
558
-	 *
559
-	 * @since 1.0.19
560
-	 * @param  string $value The renewal period.
561
-	 */
562
-	public function set_period( $value ) {
563
-		$this->set_prop( 'period', $value );
564
-	}
565
-
566
-	/**
567
-	 * Set number of periods each renewal is valid for.
568
-	 *
569
-	 * @since 1.0.19
570
-	 * @param  int $value The subscription frequency.
571
-	 */
572
-	public function set_frequency( $value ) {
573
-		$value = empty( $value ) ? 1 : (int) $value;
574
-		$this->set_prop( 'frequency', absint( $value ) );
575
-	}
576
-
577
-	/**
578
-	 * Set the initial amount for the subscription.
579
-	 *
580
-	 * @since 1.0.19
581
-	 * @param  float $value The initial subcription amount.
582
-	 */
583
-	public function set_initial_amount( $value ) {
584
-		$this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) );
585
-	}
586
-
587
-	/**
588
-	 * Set the recurring amount for the subscription.
589
-	 *
590
-	 * @since 1.0.19
591
-	 * @param  float $value The recurring subcription amount.
592
-	 */
593
-	public function set_recurring_amount( $value ) {
594
-		$this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) );
595
-	}
596
-
597
-	/**
598
-	 * Set number of times that this subscription can be renewed.
599
-	 *
600
-	 * @since 1.0.19
601
-	 * @param  int $value Bill times.
602
-	 */
603
-	public function set_bill_times( $value ) {
604
-		$this->set_prop( 'bill_times', (int) $value );
605
-	}
606
-
607
-	/**
608
-	 * Get transaction id of this subscription's parent invoice.
609
-	 *
610
-	 * @since 1.0.19
611
-	 * @param string $value Bill times.
612
-	 */
613
-	public function set_transaction_id( $value ) {
614
-		$this->set_prop( 'transaction_id', sanitize_text_field( $value ) );
615
-	}
616
-
617
-	/**
618
-	 * Set date when this subscription started.
619
-	 *
620
-	 * @since 1.0.19
621
-	 * @param string $value strtotime compliant date.
622
-	 */
623
-	public function set_created( $value ) {
624
-        $date = strtotime( $value );
669
+    public function set_expiration( $value ) {
670
+        $this->set_next_renewal_date( $value );
671
+    }
625 672
 
626
-        if ( $date && $value !== '0000-00-00 00:00:00' ) {
627
-            $this->set_prop( 'created', date( 'Y-m-d H:i:s', $date ) );
673
+    /**
674
+     * Set the subscription's trial period.
675
+     *
676
+     * @since 1.0.19
677
+     * @param string $value trial period e.g 1 year.
678
+     */
679
+    public function set_trial_period( $value ) {
680
+        $this->set_prop( 'trial_period', $value );
681
+    }
682
+
683
+    /**
684
+     * Set the subscription's status.
685
+     *
686
+     * @since 1.0.19
687
+     * @param string $new_status    New subscription status.
688
+     */
689
+    public function set_status( $new_status ) {
690
+
691
+        // Abort if this is not a valid status;
692
+        if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) {
628 693
             return;
629 694
         }
630 695
 
631
-		$this->set_prop( 'created', '' );
632 696
 
633
-	}
697
+        $old_status = ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $this->get_status();
698
+        if ( true === $this->object_read && $old_status !== $new_status ) {
699
+            $this->status_transition = array(
700
+                'from'   => $old_status,
701
+                'to'     => $new_status,
702
+            );
703
+        }
634 704
 
635
-	/**
636
-	 * Alias for self::set_created().
637
-	 *
638
-	 * @since 1.0.19
639
-	 * @param string $value strtotime compliant date.
640
-	 */
641
-	public function set_date_created( $value ) {
642
-		$this->set_created( $value );
705
+        $this->set_prop( 'status', $new_status );
643 706
     }
644 707
 
645
-	/**
646
-	 * Set the date that the subscription will renew.
647
-	 *
648
-	 * @since 1.0.19
649
-	 * @param string $value strtotime compliant date.
650
-	 */
651
-	public function set_next_renewal_date( $value ) {
652
-		$date = strtotime( $value );
708
+    /**
709
+     * Set the subscription's (remote) profile id.
710
+     *
711
+     * @since 1.0.19
712
+     * @param  string $value the remote profile id.
713
+     */
714
+    public function set_profile_id( $value ) {
715
+        $this->set_prop( 'profile_id', sanitize_text_field( $value ) );
716
+    }
653 717
 
654
-        if ( $date && $value !== '0000-00-00 00:00:00' ) {
655
-            $this->set_prop( 'expiration', date( 'Y-m-d H:i:s', $date ) );
656
-            return;
657
-		}
658
-
659
-		$this->set_prop( 'expiration', '' );
660
-
661
-	}
662
-
663
-	/**
664
-	 * Alias for self::set_next_renewal_date().
665
-	 *
666
-	 * @since 1.0.19
667
-	 * @param string $value strtotime compliant date.
668
-	 */
669
-	public function set_expiration( $value ) {
670
-		$this->set_next_renewal_date( $value );
671
-    }
672
-
673
-	/**
674
-	 * Set the subscription's trial period.
675
-	 *
676
-	 * @since 1.0.19
677
-	 * @param string $value trial period e.g 1 year.
678
-	 */
679
-	public function set_trial_period( $value ) {
680
-		$this->set_prop( 'trial_period', $value );
681
-	}
682
-
683
-	/**
684
-	 * Set the subscription's status.
685
-	 *
686
-	 * @since 1.0.19
687
-	 * @param string $new_status    New subscription status.
688
-	 */
689
-	public function set_status( $new_status ) {
690
-
691
-		// Abort if this is not a valid status;
692
-		if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) {
693
-			return;
694
-		}
695
-
696
-
697
-		$old_status = ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $this->get_status();
698
-		if ( true === $this->object_read && $old_status !== $new_status ) {
699
-			$this->status_transition = array(
700
-				'from'   => $old_status,
701
-				'to'     => $new_status,
702
-			);
703
-		}
704
-
705
-		$this->set_prop( 'status', $new_status );
706
-	}
707
-
708
-	/**
709
-	 * Set the subscription's (remote) profile id.
710
-	 *
711
-	 * @since 1.0.19
712
-	 * @param  string $value the remote profile id.
713
-	 */
714
-	public function set_profile_id( $value ) {
715
-		$this->set_prop( 'profile_id', sanitize_text_field( $value ) );
716
-	}
717
-
718
-	/*
718
+    /*
719 719
 	|--------------------------------------------------------------------------
720 720
 	| Boolean methods
721 721
 	|--------------------------------------------------------------------------
@@ -724,55 +724,55 @@  discard block
 block discarded – undo
724 724
 	|
725 725
 	*/
726 726
 
727
-	/**
727
+    /**
728 728
      * Checks if the subscription has a given status.
729
-	 *
730
-	 * @param string|array String or array of strings to check for.
731
-	 * @return bool
729
+     *
730
+     * @param string|array String or array of strings to check for.
731
+     * @return bool
732 732
      */
733 733
     public function has_status( $status ) {
734 734
         return in_array( $this->get_status(), wpinv_clean( wpinv_parse_list( $status ) ) );
735
-	}
735
+    }
736 736
 
737
-	/**
737
+    /**
738 738
      * Checks if the subscription has a trial period.
739
-	 *
740
-	 * @return bool
739
+     *
740
+     * @return bool
741 741
      */
742 742
     public function has_trial_period() {
743
-		$period = $this->get_trial_period();
743
+        $period = $this->get_trial_period();
744 744
         return ! empty( $period );
745
-	}
746
-
747
-	/**
748
-	 * Is the subscription active?
749
-	 *
750
-	 * @return bool
751
-	 */
752
-	public function is_active() {
753
-		return $this->has_status( 'active trialling' ) && ! $this->is_expired();
754
-	}
755
-
756
-	/**
757
-	 * Is the subscription expired?
758
-	 *
759
-	 * @return bool
760
-	 */
761
-	public function is_expired() {
762
-		return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'timestamp' ) );
763
-	}
764
-
765
-	/**
766
-	 * Is this the last renewals?
767
-	 *
768
-	 * @return bool
769
-	 */
770
-	public function is_last_renewal() {
771
-		$max_bills = $this->get_bill_times();
772
-		return ! empty( $max_bills ) && $max_bills <= $this->get_times_billed();
773
-	}
774
-
775
-	/*
745
+    }
746
+
747
+    /**
748
+     * Is the subscription active?
749
+     *
750
+     * @return bool
751
+     */
752
+    public function is_active() {
753
+        return $this->has_status( 'active trialling' ) && ! $this->is_expired();
754
+    }
755
+
756
+    /**
757
+     * Is the subscription expired?
758
+     *
759
+     * @return bool
760
+     */
761
+    public function is_expired() {
762
+        return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'timestamp' ) );
763
+    }
764
+
765
+    /**
766
+     * Is this the last renewals?
767
+     *
768
+     * @return bool
769
+     */
770
+    public function is_last_renewal() {
771
+        $max_bills = $this->get_bill_times();
772
+        return ! empty( $max_bills ) && $max_bills <= $this->get_times_billed();
773
+    }
774
+
775
+    /*
776 776
 	|--------------------------------------------------------------------------
777 777
 	| Additional methods
778 778
 	|--------------------------------------------------------------------------
@@ -781,27 +781,27 @@  discard block
 block discarded – undo
781 781
 	|
782 782
 	*/
783 783
 
784
-	/**
785
-	 * Backwards compatibilty.
786
-	 */
787
-	public function create( $data = array() ) {
784
+    /**
785
+     * Backwards compatibilty.
786
+     */
787
+    public function create( $data = array() ) {
788 788
 
789
-		// Set the properties.
790
-		if ( is_array( $data ) ) {
791
-			$this->set_props( $data );
792
-		}
789
+        // Set the properties.
790
+        if ( is_array( $data ) ) {
791
+            $this->set_props( $data );
792
+        }
793 793
 
794
-		// Save the item.
795
-		return $this->save();
794
+        // Save the item.
795
+        return $this->save();
796 796
 
797
-	}
797
+    }
798 798
 
799
-	/**
800
-	 * Backwards compatibilty.
801
-	 */
802
-	public function update( $args = array() ) {
803
-		return $this->create( $args );
804
-	}
799
+    /**
800
+     * Backwards compatibilty.
801
+     */
802
+    public function update( $args = array() ) {
803
+        return $this->create( $args );
804
+    }
805 805
 
806 806
     /**
807 807
      * Retrieve renewal payments for a subscription
@@ -811,22 +811,22 @@  discard block
 block discarded – undo
811 811
      */
812 812
     public function get_child_payments( $hide_pending = true ) {
813 813
 
814
-		$statuses = array( 'publish', 'wpi-processing', 'wpi-renewal' );
814
+        $statuses = array( 'publish', 'wpi-processing', 'wpi-renewal' );
815 815
 
816
-		if ( ! $hide_pending ) {
817
-			$statuses = array_keys( wpinv_get_invoice_statuses() );
818
-		}
816
+        if ( ! $hide_pending ) {
817
+            $statuses = array_keys( wpinv_get_invoice_statuses() );
818
+        }
819 819
 
820 820
         return get_posts(
821
-			array(
822
-            	'post_parent'    => $this->get_parent_payment_id(),
823
-            	'numberposts'    => -1,
824
-            	'post_status'    => $statuses,
825
-            	'orderby'        => 'ID',
826
-            	'order'          => 'ASC',
827
-            	'post_type'      => 'wpi_invoice'
828
-			)
829
-		);
821
+            array(
822
+                'post_parent'    => $this->get_parent_payment_id(),
823
+                'numberposts'    => -1,
824
+                'post_status'    => $statuses,
825
+                'orderby'        => 'ID',
826
+                'order'          => 'ASC',
827
+                'post_type'      => 'wpi_invoice'
828
+            )
829
+        );
830 830
     }
831 831
 
832 832
     /**
@@ -836,7 +836,7 @@  discard block
 block discarded – undo
836 836
      * @return int
837 837
      */
838 838
     public function get_total_payments() {
839
-		return getpaid_count_subscription_invoices( $this->get_parent_invoice_id(), $this->get_id() );
839
+        return getpaid_count_subscription_invoices( $this->get_parent_invoice_id(), $this->get_id() );
840 840
     }
841 841
 
842 842
     /**
@@ -860,57 +860,57 @@  discard block
 block discarded – undo
860 860
      *
861 861
      * @since  2.4
862 862
      * @param  array $args Array of values for the payment, including amount and transaction ID
863
-	 * @param  WPInv_Invoice $invoice If adding an existing invoice.
863
+     * @param  WPInv_Invoice $invoice If adding an existing invoice.
864 864
      * @return bool
865 865
      */
866 866
     public function add_payment( $args = array(), $invoice = false ) {
867 867
 
868
-		// Process each payment once.
868
+        // Process each payment once.
869 869
         if ( ! empty( $args['transaction_id'] ) && $this->payment_exists( $args['transaction_id'] ) ) {
870 870
             return false;
871 871
         }
872 872
 
873
-		// Are we creating a new invoice?
874
-		if ( empty( $invoice ) ) {
875
-			$invoice = $this->create_payment();
873
+        // Are we creating a new invoice?
874
+        if ( empty( $invoice ) ) {
875
+            $invoice = $this->create_payment();
876 876
 
877
-			if ( empty( $invoice ) ) {
878
-				return false;
879
-			}
877
+            if ( empty( $invoice ) ) {
878
+                return false;
879
+            }
880 880
 
881
-		}
881
+        }
882 882
 
883
-		$invoice->set_status( 'wpi-renewal' );
883
+        $invoice->set_status( 'wpi-renewal' );
884 884
 
885
-		// Maybe set a transaction id.
886
-		if ( ! empty( $args['transaction_id'] ) ) {
887
-			$invoice->set_transaction_id( $args['transaction_id'] );
888
-		}
885
+        // Maybe set a transaction id.
886
+        if ( ! empty( $args['transaction_id'] ) ) {
887
+            $invoice->set_transaction_id( $args['transaction_id'] );
888
+        }
889 889
 
890
-		// Set the completed date.
891
-		$invoice->set_completed_date( current_time( 'mysql' ) );
890
+        // Set the completed date.
891
+        $invoice->set_completed_date( current_time( 'mysql' ) );
892 892
 
893
-		// And the gateway.
894
-		if ( ! empty( $args['gateway'] ) ) {
895
-			$invoice->set_gateway( $args['gateway'] );
896
-		}
893
+        // And the gateway.
894
+        if ( ! empty( $args['gateway'] ) ) {
895
+            $invoice->set_gateway( $args['gateway'] );
896
+        }
897 897
 
898
-		$invoice->save();
898
+        $invoice->save();
899 899
 
900
-		if ( ! $invoice->exists() ) {
901
-			return false;
902
-		}
900
+        if ( ! $invoice->exists() ) {
901
+            return false;
902
+        }
903 903
 
904
-		do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this );
905
-		do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this );
904
+        do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this );
905
+        do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this );
906 906
         do_action( 'wpinv_recurring_record_payment', $invoice->get_id(), $this->get_parent_invoice_id(), $invoice->get_recurring_total(), $invoice->get_transaction_id() );
907 907
 
908 908
         update_post_meta( $invoice->get_id(), '_wpinv_subscription_id', $this->id );
909 909
 
910 910
         return $invoice->get_id();
911
-	}
911
+    }
912 912
 
913
-	/**
913
+    /**
914 914
      * Creates a new invoice and returns it.
915 915
      *
916 916
      * @since  1.0.19
@@ -918,124 +918,124 @@  discard block
 block discarded – undo
918 918
      */
919 919
     public function create_payment() {
920 920
 
921
-		$parent_invoice = $this->get_parent_payment();
922
-
923
-		if ( ! $parent_invoice->exists() ) {
924
-			return false;
925
-		}
926
-
927
-		// Duplicate the parent invoice.
928
-		$invoice = getpaid_duplicate_invoice( $parent_invoice );
929
-		$invoice->set_parent_id( $parent_invoice->get_id() );
930
-		$invoice->set_subscription_id( $this->get_id() );
931
-		$invoice->set_remote_subscription_id( $this->get_profile_id() );
932
-
933
-		// Set invoice items.
934
-		$subscription_group = getpaid_get_invoice_subscription_group( $parent_invoice->get_id(), $this->get_id() );
935
-		$allowed_items      = empty( $subscription_group ) ? array( $this->get_product_id() ) : array_keys( $subscription_group['items'] );
936
-		$invoice_items      = array();
937
-
938
-		foreach ( $invoice->get_items() as $item ) {
939
-			if ( in_array( $item->get_id(), $allowed_items ) ) {
940
-				$invoice_items[] = $item;
941
-			}
942
-		}
943
-
944
-		$invoice->set_items( $invoice_items );
945
-
946
-		if ( ! empty( $subscription_group['fees'] ) ) {
947
-			$invoice->set_fees( $subscription_group['fees'] );
948
-		}
949
-
950
-		// Maybe recalculate discount (Pre-GetPaid Fix).
951
-		$discount = new WPInv_Discount( $invoice->get_discount_code() );
952
-		if ( $discount->exists() && $discount->is_recurring() && 0 == $invoice->get_total_discount() ) {
953
-			$invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) );
954
-		}
955
-
956
-		$invoice->recalculate_total();
957
-		$invoice->set_status( 'wpi-pending' );
958
-		$invoice->save();
959
-
960
-		return $invoice->exists() ? $invoice : false;
961
-    }
962
-
963
-	/**
964
-	 * Renews or completes a subscription
965
-	 *
966
-	 * @since  1.0.0
967
-	 * @return int The subscription's id
968
-	 */
969
-	public function renew() {
970
-
971
-		// Complete subscription if applicable
972
-		if ( $this->is_last_renewal() ) {
973
-			return $this->complete();
974
-		}
975
-
976
-		// Calculate new expiration
977
-		$frequency      = $this->get_frequency();
978
-		$period         = $this->get_period();
979
-		$new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() );
980
-
981
-		$this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) );
982
-		$this->set_status( 'active' );
983
-		$this->save();
984
-
985
-		do_action( 'getpaid_subscription_renewed', $this );
986
-
987
-		return $this->get_id();
988
-	}
989
-
990
-	/**
991
-	 * Marks a subscription as completed
992
-	 *
993
-	 * Subscription is completed when the number of payments matches the billing_times field
994
-	 *
995
-	 * @since  1.0.0
996
-	 * @return int|bool Subscription id or false if the subscription is cancelled.
997
-	 */
998
-	public function complete() {
999
-
1000
-		// Only mark a subscription as complete if it's not already cancelled.
1001
-		if ( $this->has_status( 'cancelled' ) ) {
1002
-			return false;
1003
-		}
1004
-
1005
-		$this->set_status( 'completed' );
1006
-		return $this->save();
1007
-
1008
-	}
1009
-
1010
-	/**
1011
-	 * Marks a subscription as expired
1012
-	 *
1013
-	 * @since  1.0.0
1014
-	 * @param  bool $check_expiration
1015
-	 * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future.
1016
-	 */
1017
-	public function expire( $check_expiration = false ) {
1018
-
1019
-		if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) {
1020
-			// Do not mark as expired since real expiration date is in the future
1021
-			return false;
1022
-		}
1023
-
1024
-		$this->set_status( 'expired' );
1025
-		return $this->save();
1026
-
1027
-	}
1028
-
1029
-	/**
1030
-	 * Marks a subscription as failing
1031
-	 *
1032
-	 * @since  2.4.2
1033
-	 * @return int Subscription id.
1034
-	 */
1035
-	public function failing() {
1036
-		$this->set_status( 'failing' );
1037
-		return $this->save();
1038
-	}
921
+        $parent_invoice = $this->get_parent_payment();
922
+
923
+        if ( ! $parent_invoice->exists() ) {
924
+            return false;
925
+        }
926
+
927
+        // Duplicate the parent invoice.
928
+        $invoice = getpaid_duplicate_invoice( $parent_invoice );
929
+        $invoice->set_parent_id( $parent_invoice->get_id() );
930
+        $invoice->set_subscription_id( $this->get_id() );
931
+        $invoice->set_remote_subscription_id( $this->get_profile_id() );
932
+
933
+        // Set invoice items.
934
+        $subscription_group = getpaid_get_invoice_subscription_group( $parent_invoice->get_id(), $this->get_id() );
935
+        $allowed_items      = empty( $subscription_group ) ? array( $this->get_product_id() ) : array_keys( $subscription_group['items'] );
936
+        $invoice_items      = array();
937
+
938
+        foreach ( $invoice->get_items() as $item ) {
939
+            if ( in_array( $item->get_id(), $allowed_items ) ) {
940
+                $invoice_items[] = $item;
941
+            }
942
+        }
943
+
944
+        $invoice->set_items( $invoice_items );
945
+
946
+        if ( ! empty( $subscription_group['fees'] ) ) {
947
+            $invoice->set_fees( $subscription_group['fees'] );
948
+        }
949
+
950
+        // Maybe recalculate discount (Pre-GetPaid Fix).
951
+        $discount = new WPInv_Discount( $invoice->get_discount_code() );
952
+        if ( $discount->exists() && $discount->is_recurring() && 0 == $invoice->get_total_discount() ) {
953
+            $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) );
954
+        }
955
+
956
+        $invoice->recalculate_total();
957
+        $invoice->set_status( 'wpi-pending' );
958
+        $invoice->save();
959
+
960
+        return $invoice->exists() ? $invoice : false;
961
+    }
962
+
963
+    /**
964
+     * Renews or completes a subscription
965
+     *
966
+     * @since  1.0.0
967
+     * @return int The subscription's id
968
+     */
969
+    public function renew() {
970
+
971
+        // Complete subscription if applicable
972
+        if ( $this->is_last_renewal() ) {
973
+            return $this->complete();
974
+        }
975
+
976
+        // Calculate new expiration
977
+        $frequency      = $this->get_frequency();
978
+        $period         = $this->get_period();
979
+        $new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() );
980
+
981
+        $this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) );
982
+        $this->set_status( 'active' );
983
+        $this->save();
984
+
985
+        do_action( 'getpaid_subscription_renewed', $this );
986
+
987
+        return $this->get_id();
988
+    }
989
+
990
+    /**
991
+     * Marks a subscription as completed
992
+     *
993
+     * Subscription is completed when the number of payments matches the billing_times field
994
+     *
995
+     * @since  1.0.0
996
+     * @return int|bool Subscription id or false if the subscription is cancelled.
997
+     */
998
+    public function complete() {
999
+
1000
+        // Only mark a subscription as complete if it's not already cancelled.
1001
+        if ( $this->has_status( 'cancelled' ) ) {
1002
+            return false;
1003
+        }
1004
+
1005
+        $this->set_status( 'completed' );
1006
+        return $this->save();
1007
+
1008
+    }
1009
+
1010
+    /**
1011
+     * Marks a subscription as expired
1012
+     *
1013
+     * @since  1.0.0
1014
+     * @param  bool $check_expiration
1015
+     * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future.
1016
+     */
1017
+    public function expire( $check_expiration = false ) {
1018
+
1019
+        if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) {
1020
+            // Do not mark as expired since real expiration date is in the future
1021
+            return false;
1022
+        }
1023
+
1024
+        $this->set_status( 'expired' );
1025
+        return $this->save();
1026
+
1027
+    }
1028
+
1029
+    /**
1030
+     * Marks a subscription as failing
1031
+     *
1032
+     * @since  2.4.2
1033
+     * @return int Subscription id.
1034
+     */
1035
+    public function failing() {
1036
+        $this->set_status( 'failing' );
1037
+        return $this->save();
1038
+    }
1039 1039
 
1040 1040
     /**
1041 1041
      * Marks a subscription as cancelled
@@ -1044,19 +1044,19 @@  discard block
 block discarded – undo
1044 1044
      * @return int Subscription id.
1045 1045
      */
1046 1046
     public function cancel() {
1047
-		$this->set_status( 'cancelled' );
1048
-		return $this->save();
1047
+        $this->set_status( 'cancelled' );
1048
+        return $this->save();
1049 1049
     }
1050 1050
 
1051
-	/**
1052
-	 * Determines if a subscription can be cancelled both locally and with a payment processor.
1053
-	 *
1054
-	 * @since  1.0.0
1055
-	 * @return bool
1056
-	 */
1057
-	public function can_cancel() {
1058
-		return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this );
1059
-	}
1051
+    /**
1052
+     * Determines if a subscription can be cancelled both locally and with a payment processor.
1053
+     *
1054
+     * @since  1.0.0
1055
+     * @return bool
1056
+     */
1057
+    public function can_cancel() {
1058
+        return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this );
1059
+    }
1060 1060
 
1061 1061
     /**
1062 1062
      * Returns an array of subscription statuses that can be cancelled
@@ -1069,96 +1069,96 @@  discard block
 block discarded – undo
1069 1069
         return apply_filters( 'wpinv_recurring_cancellable_statuses', array( 'active', 'trialling', 'failing' ) );
1070 1070
     }
1071 1071
 
1072
-	/**
1073
-	 * Retrieves the URL to cancel subscription
1074
-	 *
1075
-	 * @since  1.0.0
1076
-	 * @return string
1077
-	 */
1078
-	public function get_cancel_url() {
1079
-		$url = getpaid_get_authenticated_action_url( 'subscription_cancel', $this->get_view_url() );
1080
-		return apply_filters( 'wpinv_subscription_cancel_url', $url, $this );
1081
-	}
1082
-
1083
-	/**
1084
-	 * Retrieves the URL to view a subscription
1085
-	 *
1086
-	 * @since  1.0.19
1087
-	 * @return string
1088
-	 */
1089
-	public function get_view_url() {
1090
-
1091
-		$url = getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) );
1092
-		$url = add_query_arg( 'subscription', $this->get_id(), $url );
1093
-
1094
-		return apply_filters( 'getpaid_get_subscription_view_url', $url, $this );
1095
-	}
1096
-
1097
-	/**
1098
-	 * Determines if subscription can be manually renewed
1099
-	 *
1100
-	 * This method is filtered by payment gateways in order to return true on subscriptions
1101
-	 * that can be renewed manually
1102
-	 *
1103
-	 * @since  2.5
1104
-	 * @return bool
1105
-	 */
1106
-	public function can_renew() {
1107
-		return apply_filters( 'wpinv_subscription_can_renew', true, $this );
1108
-	}
1109
-
1110
-	/**
1111
-	 * Retrieves the URL to renew a subscription
1112
-	 *
1113
-	 * @since  2.5
1114
-	 * @return string
1115
-	 */
1116
-	public function get_renew_url() {
1117
-		$url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' );
1118
-		return apply_filters( 'wpinv_subscription_renew_url', $url, $this );
1119
-	}
1120
-
1121
-	/**
1122
-	 * Determines if subscription can have their payment method updated
1123
-	 *
1124
-	 * @since  1.0.0
1125
-	 * @return bool
1126
-	 */
1127
-	public function can_update() {
1128
-		return apply_filters( 'wpinv_subscription_can_update', false, $this );
1129
-	}
1130
-
1131
-	/**
1132
-	 * Retrieves the URL to update subscription
1133
-	 *
1134
-	 * @since  1.0.0
1135
-	 * @return string
1136
-	 */
1137
-	public function get_update_url() {
1138
-		$url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) );
1139
-		return apply_filters( 'wpinv_subscription_update_url', $url, $this );
1140
-	}
1141
-
1142
-	/**
1143
-	 * Retrieves the subscription status label
1144
-	 *
1145
-	 * @since  1.0.0
1146
-	 * @return string
1147
-	 */
1148
-	public function get_status_label() {
1149
-		return getpaid_get_subscription_status_label( $this->get_status() );
1150
-	}
1151
-
1152
-	/**
1153
-	 * Retrieves the subscription status class
1154
-	 *
1155
-	 * @since  1.0.19
1156
-	 * @return string
1157
-	 */
1158
-	public function get_status_class() {
1159
-		$statuses = getpaid_get_subscription_status_classes();
1160
-		return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'badge-dark';
1161
-	}
1072
+    /**
1073
+     * Retrieves the URL to cancel subscription
1074
+     *
1075
+     * @since  1.0.0
1076
+     * @return string
1077
+     */
1078
+    public function get_cancel_url() {
1079
+        $url = getpaid_get_authenticated_action_url( 'subscription_cancel', $this->get_view_url() );
1080
+        return apply_filters( 'wpinv_subscription_cancel_url', $url, $this );
1081
+    }
1082
+
1083
+    /**
1084
+     * Retrieves the URL to view a subscription
1085
+     *
1086
+     * @since  1.0.19
1087
+     * @return string
1088
+     */
1089
+    public function get_view_url() {
1090
+
1091
+        $url = getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) );
1092
+        $url = add_query_arg( 'subscription', $this->get_id(), $url );
1093
+
1094
+        return apply_filters( 'getpaid_get_subscription_view_url', $url, $this );
1095
+    }
1096
+
1097
+    /**
1098
+     * Determines if subscription can be manually renewed
1099
+     *
1100
+     * This method is filtered by payment gateways in order to return true on subscriptions
1101
+     * that can be renewed manually
1102
+     *
1103
+     * @since  2.5
1104
+     * @return bool
1105
+     */
1106
+    public function can_renew() {
1107
+        return apply_filters( 'wpinv_subscription_can_renew', true, $this );
1108
+    }
1109
+
1110
+    /**
1111
+     * Retrieves the URL to renew a subscription
1112
+     *
1113
+     * @since  2.5
1114
+     * @return string
1115
+     */
1116
+    public function get_renew_url() {
1117
+        $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' );
1118
+        return apply_filters( 'wpinv_subscription_renew_url', $url, $this );
1119
+    }
1120
+
1121
+    /**
1122
+     * Determines if subscription can have their payment method updated
1123
+     *
1124
+     * @since  1.0.0
1125
+     * @return bool
1126
+     */
1127
+    public function can_update() {
1128
+        return apply_filters( 'wpinv_subscription_can_update', false, $this );
1129
+    }
1130
+
1131
+    /**
1132
+     * Retrieves the URL to update subscription
1133
+     *
1134
+     * @since  1.0.0
1135
+     * @return string
1136
+     */
1137
+    public function get_update_url() {
1138
+        $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) );
1139
+        return apply_filters( 'wpinv_subscription_update_url', $url, $this );
1140
+    }
1141
+
1142
+    /**
1143
+     * Retrieves the subscription status label
1144
+     *
1145
+     * @since  1.0.0
1146
+     * @return string
1147
+     */
1148
+    public function get_status_label() {
1149
+        return getpaid_get_subscription_status_label( $this->get_status() );
1150
+    }
1151
+
1152
+    /**
1153
+     * Retrieves the subscription status class
1154
+     *
1155
+     * @since  1.0.19
1156
+     * @return string
1157
+     */
1158
+    public function get_status_class() {
1159
+        $statuses = getpaid_get_subscription_status_classes();
1160
+        return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'badge-dark';
1161
+    }
1162 1162
 
1163 1163
     /**
1164 1164
      * Retrieves the subscription status label
@@ -1168,11 +1168,11 @@  discard block
 block discarded – undo
1168 1168
      */
1169 1169
     public function get_status_label_html() {
1170 1170
 
1171
-		$status_label = sanitize_text_field( $this->get_status_label() );
1172
-		$class        = esc_attr( $this->get_status_class() );
1173
-		$status       = sanitize_html_class( $this->get_status() );
1171
+        $status_label = sanitize_text_field( $this->get_status_label() );
1172
+        $class        = esc_attr( $this->get_status_class() );
1173
+        $status       = sanitize_html_class( $this->get_status() );
1174 1174
 
1175
-		return "<span class='bsui'><span class='badge $class $status'>$status_label</span></span>";
1175
+        return "<span class='bsui'><span class='badge $class $status'>$status_label</span></span>";
1176 1176
     }
1177 1177
 
1178 1178
     /**
@@ -1183,75 +1183,75 @@  discard block
 block discarded – undo
1183 1183
      * @return bool
1184 1184
      */
1185 1185
     public function payment_exists( $txn_id = '' ) {
1186
-		$invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' );
1186
+        $invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' );
1187 1187
         return ! empty( $invoice_id );
1188
-	}
1189
-
1190
-	/**
1191
-	 * Handle the status transition.
1192
-	 */
1193
-	protected function status_transition() {
1194
-		$status_transition = $this->status_transition;
1195
-
1196
-		// Reset status transition variable.
1197
-		$this->status_transition = false;
1198
-
1199
-		if ( $status_transition ) {
1200
-			try {
1201
-
1202
-				// Fire a hook for the status change.
1203
-				do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition );
1204
-				do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition );
1205
-
1206
-				if ( ! empty( $status_transition['from'] ) ) {
1207
-
1208
-					/* translators: 1: old subscription status 2: new subscription status */
1209
-					$transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) );
1210
-
1211
-					// Note the transition occurred.
1212
-					$this->get_parent_payment()->add_note( $transition_note, false, false, true );
1213
-
1214
-					// Fire another hook.
1215
-					do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this );
1216
-					do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] );
1217
-
1218
-				} else {
1219
-					/* translators: %s: new invoice status */
1220
-					$transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) );
1221
-
1222
-					// Note the transition occurred.
1223
-					$this->get_parent_payment()->add_note( $transition_note, false, false, true );
1224
-
1225
-				}
1226
-			} catch ( Exception $e ) {
1227
-				$this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() );
1228
-			}
1229
-		}
1230
-
1231
-	}
1232
-
1233
-	/**
1234
-	 * Save data to the database.
1235
-	 *
1236
-	 * @since 1.0.19
1237
-	 * @return int subscription ID
1238
-	 */
1239
-	public function save() {
1240
-		parent::save();
1241
-		$this->status_transition();
1242
-		return $this->get_id();
1243
-	}
1244
-
1245
-	/**
1246
-	 * Activates a subscription.
1247
-	 *
1248
-	 * @since 1.0.19
1249
-	 * @return int subscription ID
1250
-	 */
1251
-	public function activate() {
1252
-		$status = 'trialling' == $this->get_status() ? 'trialling' : 'active';
1253
-		$this->set_status( $status );
1254
-		return $this->save();
1255
-	}
1188
+    }
1189
+
1190
+    /**
1191
+     * Handle the status transition.
1192
+     */
1193
+    protected function status_transition() {
1194
+        $status_transition = $this->status_transition;
1195
+
1196
+        // Reset status transition variable.
1197
+        $this->status_transition = false;
1198
+
1199
+        if ( $status_transition ) {
1200
+            try {
1201
+
1202
+                // Fire a hook for the status change.
1203
+                do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition );
1204
+                do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition );
1205
+
1206
+                if ( ! empty( $status_transition['from'] ) ) {
1207
+
1208
+                    /* translators: 1: old subscription status 2: new subscription status */
1209
+                    $transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) );
1210
+
1211
+                    // Note the transition occurred.
1212
+                    $this->get_parent_payment()->add_note( $transition_note, false, false, true );
1213
+
1214
+                    // Fire another hook.
1215
+                    do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this );
1216
+                    do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] );
1217
+
1218
+                } else {
1219
+                    /* translators: %s: new invoice status */
1220
+                    $transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) );
1221
+
1222
+                    // Note the transition occurred.
1223
+                    $this->get_parent_payment()->add_note( $transition_note, false, false, true );
1224
+
1225
+                }
1226
+            } catch ( Exception $e ) {
1227
+                $this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() );
1228
+            }
1229
+        }
1230
+
1231
+    }
1232
+
1233
+    /**
1234
+     * Save data to the database.
1235
+     *
1236
+     * @since 1.0.19
1237
+     * @return int subscription ID
1238
+     */
1239
+    public function save() {
1240
+        parent::save();
1241
+        $this->status_transition();
1242
+        return $this->get_id();
1243
+    }
1244
+
1245
+    /**
1246
+     * Activates a subscription.
1247
+     *
1248
+     * @since 1.0.19
1249
+     * @return int subscription ID
1250
+     */
1251
+    public function activate() {
1252
+        $status = 'trialling' == $this->get_status() ? 'trialling' : 'active';
1253
+        $this->set_status( $status );
1254
+        return $this->save();
1255
+    }
1256 1256
 
1257 1257
 }
Please login to merge, or discard this patch.