Passed
Pull Request — master (#375)
by Brian
101:34
created
includes/admin/meta-boxes/class-getpaid-meta-box-payment-form.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
  */
8 8
 
9 9
 if ( ! defined( 'ABSPATH' ) ) {
10
-	exit; // Exit if accessed directly
10
+    exit; // Exit if accessed directly
11 11
 }
12 12
 
13 13
 /**
@@ -16,10 +16,10 @@  discard block
 block discarded – undo
16 16
 class GetPaid_Meta_Box_Payment_Form {
17 17
 
18 18
     /**
19
-	 * Output the metabox.
20
-	 *
21
-	 * @param WP_Post $post
22
-	 */
19
+     * Output the metabox.
20
+     *
21
+     * @param WP_Post $post
22
+     */
23 23
     public static function output( $post ) {
24 24
         ?>
25 25
         <div id="wpinv-form-builder" class="bsui">
@@ -82,11 +82,11 @@  discard block
 block discarded – undo
82 82
     }
83 83
 
84 84
     /**
85
-	 * Save meta box data.
86
-	 *
87
-	 * @param int $post_id
88
-	 */
89
-	public static function save( $post_id ) {
85
+     * Save meta box data.
86
+     *
87
+     * @param int $post_id
88
+     */
89
+    public static function save( $post_id ) {
90 90
 
91 91
         // verify nonce
92 92
         if ( ! isset( $_POST['wpinv_save_payment_form'] ) || ! wp_verify_nonce( $_POST['wpinv_save_payment_form'], 'wpinv_save_payment_form' ) ) {
Please login to merge, or discard this patch.
includes/data-stores/class-getpaid-item-data-store.php 1 patch
Indentation   +193 added lines, -193 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
  *
5 5
  */
6 6
 if ( ! defined( 'ABSPATH' ) ) {
7
-	exit;
7
+    exit;
8 8
 }
9 9
 
10 10
 /**
@@ -14,212 +14,212 @@  discard block
 block discarded – undo
14 14
  */
15 15
 class GetPaid_Item_Data_Store extends GetPaid_Data_Store_WP {
16 16
 
17
-	/**
18
-	 * Data stored in meta keys, but not considered "meta" for an item.
19
-	 *
20
-	 * @since 1.0.19
21
-	 * @var array
22
-	 */
23
-	protected $internal_meta_keys = array(
24
-		'_wpinv_price',
25
-		'_wpinv_vat_rule',
26
-		'_wpinv_vat_class',
27
-		'_wpinv_type',
28
-		'_wpinv_custom_id',
29
-		'_wpinv_custom_name',
30
-		'_wpinv_custom_singular_name',
31
-		'_wpinv_editable',
32
-		'_wpinv_dynamic_pricing',
33
-		'_minimum_price',
34
-		'_wpinv_is_recurring',
35
-		'_wpinv_recurring_period',
36
-		'_wpinv_recurring_interval',
37
-		'_wpinv_recurring_limit',
38
-		'_wpinv_free_trial',
39
-		'_wpinv_trial_period',
40
-		'_wpinv_trial_interval'
41
-	);
42
-
43
-	/**
44
-	 * A map of meta keys to data props.
45
-	 *
46
-	 * @since 1.0.19
47
-	 *
48
-	 * @var array
49
-	 */
50
-	protected $meta_key_to_props = array(
51
-		'_wpinv_price'                => 'price',
52
-		'_wpinv_vat_rule'             => 'vat_rule',
53
-		'_wpinv_vat_class'            => 'vat_class',
54
-		'_wpinv_type'                 => 'type',
55
-		'_wpinv_custom_id'            => 'custom_id',
56
-		'_wpinv_custom_name'          => 'custom_name',
57
-		'_wpinv_custom_singular_name' => 'custom_singular_name',
58
-		'_wpinv_editable'             => 'is_editable',
59
-		'_wpinv_dynamic_pricing'      => 'is_dynamic_pricing',
60
-		'_minimum_price'              => 'minimum_price',
61
-		'_wpinv_custom_name'          => 'custom_name',
62
-		'_wpinv_is_recurring'         => 'is_recurring',
63
-		'_wpinv_recurring_period'     => 'recurring_period',
64
-		'_wpinv_recurring_interval'   => 'recurring_interval',
65
-		'_wpinv_recurring_limit'      => 'recurring_limit',
66
-		'_wpinv_free_trial'           => 'is_free_trial',
67
-		'_wpinv_trial_period'         => 'trial_period',
68
-		'_wpinv_trial_interval'       => 'trial_interval',
69
-		'_wpinv_version'              => 'version',
70
-	);
71
-
72
-	/*
17
+    /**
18
+     * Data stored in meta keys, but not considered "meta" for an item.
19
+     *
20
+     * @since 1.0.19
21
+     * @var array
22
+     */
23
+    protected $internal_meta_keys = array(
24
+        '_wpinv_price',
25
+        '_wpinv_vat_rule',
26
+        '_wpinv_vat_class',
27
+        '_wpinv_type',
28
+        '_wpinv_custom_id',
29
+        '_wpinv_custom_name',
30
+        '_wpinv_custom_singular_name',
31
+        '_wpinv_editable',
32
+        '_wpinv_dynamic_pricing',
33
+        '_minimum_price',
34
+        '_wpinv_is_recurring',
35
+        '_wpinv_recurring_period',
36
+        '_wpinv_recurring_interval',
37
+        '_wpinv_recurring_limit',
38
+        '_wpinv_free_trial',
39
+        '_wpinv_trial_period',
40
+        '_wpinv_trial_interval'
41
+    );
42
+
43
+    /**
44
+     * A map of meta keys to data props.
45
+     *
46
+     * @since 1.0.19
47
+     *
48
+     * @var array
49
+     */
50
+    protected $meta_key_to_props = array(
51
+        '_wpinv_price'                => 'price',
52
+        '_wpinv_vat_rule'             => 'vat_rule',
53
+        '_wpinv_vat_class'            => 'vat_class',
54
+        '_wpinv_type'                 => 'type',
55
+        '_wpinv_custom_id'            => 'custom_id',
56
+        '_wpinv_custom_name'          => 'custom_name',
57
+        '_wpinv_custom_singular_name' => 'custom_singular_name',
58
+        '_wpinv_editable'             => 'is_editable',
59
+        '_wpinv_dynamic_pricing'      => 'is_dynamic_pricing',
60
+        '_minimum_price'              => 'minimum_price',
61
+        '_wpinv_custom_name'          => 'custom_name',
62
+        '_wpinv_is_recurring'         => 'is_recurring',
63
+        '_wpinv_recurring_period'     => 'recurring_period',
64
+        '_wpinv_recurring_interval'   => 'recurring_interval',
65
+        '_wpinv_recurring_limit'      => 'recurring_limit',
66
+        '_wpinv_free_trial'           => 'is_free_trial',
67
+        '_wpinv_trial_period'         => 'trial_period',
68
+        '_wpinv_trial_interval'       => 'trial_interval',
69
+        '_wpinv_version'              => 'version',
70
+    );
71
+
72
+    /*
73 73
 	|--------------------------------------------------------------------------
74 74
 	| CRUD Methods
75 75
 	|--------------------------------------------------------------------------
76 76
 	*/
77 77
 
78
-	/**
79
-	 * Method to create a new item in the database.
80
-	 *
81
-	 * @param WPInv_Item $item Item object.
82
-	 */
83
-	public function create( &$item ) {
84
-		$item->set_version( WPINV_VERSION );
85
-		$item->set_date_created( current_time('mysql') );
86
-
87
-		// Create a new post.
88
-		$id = wp_insert_post(
89
-			apply_filters(
90
-				'getpaid_new_item_data',
91
-				array(
92
-					'post_date'     => $item->get_date_created( 'edit' ),
93
-					'post_type'     => 'wpi_item',
94
-					'post_status'   => $this->get_post_status( $item ),
95
-					'ping_status'   => 'closed',
96
-					'post_author'   => $item->get_author( 'edit' ),
97
-					'post_title'    => $item->get_name( 'edit' ),
98
-					'post_parent'   => 0,
99
-					'post_excerpt'  => $item->get_description( 'edit' ),
100
-				)
101
-			),
102
-			true
103
-		);
104
-
105
-		if ( $id && ! is_wp_error( $id ) ) {
106
-			$item->set_id( $id );
107
-			$this->update_post_meta( $item );
108
-			$item->save_meta_data();
109
-			$item->apply_changes();
110
-			$this->clear_caches( $item );
111
-			return true;
112
-		}
113
-
114
-		if ( is_wp_error( $id ) ) {
115
-			$item->last_error = $id->get_error_message();
116
-		}
78
+    /**
79
+     * Method to create a new item in the database.
80
+     *
81
+     * @param WPInv_Item $item Item object.
82
+     */
83
+    public function create( &$item ) {
84
+        $item->set_version( WPINV_VERSION );
85
+        $item->set_date_created( current_time('mysql') );
86
+
87
+        // Create a new post.
88
+        $id = wp_insert_post(
89
+            apply_filters(
90
+                'getpaid_new_item_data',
91
+                array(
92
+                    'post_date'     => $item->get_date_created( 'edit' ),
93
+                    'post_type'     => 'wpi_item',
94
+                    'post_status'   => $this->get_post_status( $item ),
95
+                    'ping_status'   => 'closed',
96
+                    'post_author'   => $item->get_author( 'edit' ),
97
+                    'post_title'    => $item->get_name( 'edit' ),
98
+                    'post_parent'   => 0,
99
+                    'post_excerpt'  => $item->get_description( 'edit' ),
100
+                )
101
+            ),
102
+            true
103
+        );
104
+
105
+        if ( $id && ! is_wp_error( $id ) ) {
106
+            $item->set_id( $id );
107
+            $this->update_post_meta( $item );
108
+            $item->save_meta_data();
109
+            $item->apply_changes();
110
+            $this->clear_caches( $item );
111
+            return true;
112
+        }
113
+
114
+        if ( is_wp_error( $id ) ) {
115
+            $item->last_error = $id->get_error_message();
116
+        }
117 117
 		
118
-		return false;
119
-	}
120
-
121
-	/**
122
-	 * Method to read an item from the database.
123
-	 *
124
-	 * @param WPInv_Item $item Item object.
125
-	 *
126
-	 */
127
-	public function read( &$item ) {
128
-
129
-		$item->set_defaults();
130
-		$item_object = get_post( $item->get_id() );
131
-
132
-		if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) {
133
-			$item->last_error = __( 'Invalid item.', 'invoicing' );
134
-			return false;
135
-		}
136
-
137
-		$item->set_props(
138
-			array(
139
-				'parent_id'     => $item_object->post_parent,
140
-				'date_created'  => 0 < $item_object->post_date_gmt ? $item_object->post_date_gmt : null,
141
-				'date_modified' => 0 < $item_object->post_modified_gmt ? $item_object->post_modified_gmt : null,
142
-				'status'        => $item_object->post_status,
143
-				'name'          => $item_object->post_title,
144
-				'description'   => $item_object->post_excerpt,
145
-				'author'        => $item_object->post_author,
146
-			)
147
-		);
148
-
149
-		$this->read_object_data( $item, $item_object );
150
-		$item->read_meta_data();
151
-		$item->set_object_read( true );
152
-
153
-	}
154
-
155
-	/**
156
-	 * Method to update an item in the database.
157
-	 *
158
-	 * @param WPInv_Item $item Item object.
159
-	 */
160
-	public function update( &$item ) {
161
-		$item->save_meta_data();
162
-		$item->set_version( WPINV_VERSION );
163
-
164
-		if ( null === $item->get_date_created( 'edit' ) ) {
165
-			$item->set_date_created(  current_time('mysql') );
166
-		}
167
-
168
-		$changes = $item->get_changes();
169
-
170
-		// Only update the post when the post data changes.
171
-		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt', 'name', 'author' ), array_keys( $changes ) ) ) {
172
-			$post_data = array(
173
-				'post_date'         => $item->get_date_created( 'edit' ),
174
-				'post_status'       => $item->get_status( 'edit' ),
175
-				'post_parent'       => $item->get_parent_id( 'edit' ),
176
-				'post_excerpt'      => $item->get_description( 'edit' ),
177
-				'post_modified'     => $item->get_date_modified( 'edit' ),
178
-				'post_title'        => $item->get_name( 'edit' ),
179
-				'post_author'       => $item->get_author( 'edit' ),
180
-			);
181
-
182
-			/**
183
-			 * When updating this object, to prevent infinite loops, use $wpdb
184
-			 * to update data, since wp_update_post spawns more calls to the
185
-			 * save_post action.
186
-			 *
187
-			 * This ensures hooks are fired by either WP itself (admin screen save),
188
-			 * or an update purely from CRUD.
189
-			 */
190
-			if ( doing_action( 'save_post' ) ) {
191
-				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) );
192
-				clean_post_cache( $item->get_id() );
193
-			} else {
194
-				wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) );
195
-			}
196
-			$item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
197
-		}
198
-		$this->update_post_meta( $item );
199
-		$item->apply_changes();
200
-		$this->clear_caches( $item );
201
-	}
202
-
203
-	/*
118
+        return false;
119
+    }
120
+
121
+    /**
122
+     * Method to read an item from the database.
123
+     *
124
+     * @param WPInv_Item $item Item object.
125
+     *
126
+     */
127
+    public function read( &$item ) {
128
+
129
+        $item->set_defaults();
130
+        $item_object = get_post( $item->get_id() );
131
+
132
+        if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) {
133
+            $item->last_error = __( 'Invalid item.', 'invoicing' );
134
+            return false;
135
+        }
136
+
137
+        $item->set_props(
138
+            array(
139
+                'parent_id'     => $item_object->post_parent,
140
+                'date_created'  => 0 < $item_object->post_date_gmt ? $item_object->post_date_gmt : null,
141
+                'date_modified' => 0 < $item_object->post_modified_gmt ? $item_object->post_modified_gmt : null,
142
+                'status'        => $item_object->post_status,
143
+                'name'          => $item_object->post_title,
144
+                'description'   => $item_object->post_excerpt,
145
+                'author'        => $item_object->post_author,
146
+            )
147
+        );
148
+
149
+        $this->read_object_data( $item, $item_object );
150
+        $item->read_meta_data();
151
+        $item->set_object_read( true );
152
+
153
+    }
154
+
155
+    /**
156
+     * Method to update an item in the database.
157
+     *
158
+     * @param WPInv_Item $item Item object.
159
+     */
160
+    public function update( &$item ) {
161
+        $item->save_meta_data();
162
+        $item->set_version( WPINV_VERSION );
163
+
164
+        if ( null === $item->get_date_created( 'edit' ) ) {
165
+            $item->set_date_created(  current_time('mysql') );
166
+        }
167
+
168
+        $changes = $item->get_changes();
169
+
170
+        // Only update the post when the post data changes.
171
+        if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt', 'name', 'author' ), array_keys( $changes ) ) ) {
172
+            $post_data = array(
173
+                'post_date'         => $item->get_date_created( 'edit' ),
174
+                'post_status'       => $item->get_status( 'edit' ),
175
+                'post_parent'       => $item->get_parent_id( 'edit' ),
176
+                'post_excerpt'      => $item->get_description( 'edit' ),
177
+                'post_modified'     => $item->get_date_modified( 'edit' ),
178
+                'post_title'        => $item->get_name( 'edit' ),
179
+                'post_author'       => $item->get_author( 'edit' ),
180
+            );
181
+
182
+            /**
183
+             * When updating this object, to prevent infinite loops, use $wpdb
184
+             * to update data, since wp_update_post spawns more calls to the
185
+             * save_post action.
186
+             *
187
+             * This ensures hooks are fired by either WP itself (admin screen save),
188
+             * or an update purely from CRUD.
189
+             */
190
+            if ( doing_action( 'save_post' ) ) {
191
+                $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) );
192
+                clean_post_cache( $item->get_id() );
193
+            } else {
194
+                wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) );
195
+            }
196
+            $item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
197
+        }
198
+        $this->update_post_meta( $item );
199
+        $item->apply_changes();
200
+        $this->clear_caches( $item );
201
+    }
202
+
203
+    /*
204 204
 	|--------------------------------------------------------------------------
205 205
 	| Additional Methods
206 206
 	|--------------------------------------------------------------------------
207 207
 	*/
208 208
 
209
-	/**
210
-	 * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class.
211
-	 *
212
-	 * @param WPInv_Item $item WPInv_Item object.
213
-	 * @since 1.0.19
214
-	 */
215
-	protected function update_post_meta( &$item ) {
209
+    /**
210
+     * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class.
211
+     *
212
+     * @param WPInv_Item $item WPInv_Item object.
213
+     * @since 1.0.19
214
+     */
215
+    protected function update_post_meta( &$item ) {
216 216
 
217
-		// Ensure that we have a custom id.
217
+        // Ensure that we have a custom id.
218 218
         if ( ! $item->get_custom_id() ) {
219 219
             $item->set_custom_id( $item->get_id() );
220
-		}
220
+        }
221 221
 
222
-		parent::update_post_meta( $item );
223
-	}
222
+        parent::update_post_meta( $item );
223
+    }
224 224
 
225 225
 }
Please login to merge, or discard this patch.
includes/data-stores/class-getpaid-payment-form-data-store.php 1 patch
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
  *
5 5
  */
6 6
 if ( ! defined( 'ABSPATH' ) ) {
7
-	exit;
7
+    exit;
8 8
 }
9 9
 
10 10
 /**
@@ -14,163 +14,163 @@  discard block
 block discarded – undo
14 14
  */
15 15
 class GetPaid_Payment_Form_Data_Store extends GetPaid_Data_Store_WP {
16 16
 
17
-	/**
18
-	 * Data stored in meta keys, but not considered "meta" for a form.
19
-	 *
20
-	 * @since 1.0.19
21
-	 * @var array
22
-	 */
23
-	protected $internal_meta_keys = array(
24
-		'wpinv_form_elements',
25
-		'wpinv_form_items',
26
-		'wpinv_form_earned',
27
-		'wpinv_form_refunded',
28
-		'wpinv_form_cancelled',
29
-		'wpinv_form_failed'
30
-	);
31
-
32
-	/**
33
-	 * A map of meta keys to data props.
34
-	 *
35
-	 * @since 1.0.19
36
-	 *
37
-	 * @var array
38
-	 */
39
-	protected $meta_key_to_props = array(
40
-		'wpinv_form_elements'  => 'elements',
41
-		'wpinv_form_items'     => 'items',
42
-		'wpinv_form_earned'    => 'earned',
43
-		'wpinv_form_refunded'  => 'refunded',
44
-		'wpinv_form_cancelled' => 'cancelled',
45
-		'wpinv_form_failed'    => 'failed',
46
-	);
47
-
48
-	/*
17
+    /**
18
+     * Data stored in meta keys, but not considered "meta" for a form.
19
+     *
20
+     * @since 1.0.19
21
+     * @var array
22
+     */
23
+    protected $internal_meta_keys = array(
24
+        'wpinv_form_elements',
25
+        'wpinv_form_items',
26
+        'wpinv_form_earned',
27
+        'wpinv_form_refunded',
28
+        'wpinv_form_cancelled',
29
+        'wpinv_form_failed'
30
+    );
31
+
32
+    /**
33
+     * A map of meta keys to data props.
34
+     *
35
+     * @since 1.0.19
36
+     *
37
+     * @var array
38
+     */
39
+    protected $meta_key_to_props = array(
40
+        'wpinv_form_elements'  => 'elements',
41
+        'wpinv_form_items'     => 'items',
42
+        'wpinv_form_earned'    => 'earned',
43
+        'wpinv_form_refunded'  => 'refunded',
44
+        'wpinv_form_cancelled' => 'cancelled',
45
+        'wpinv_form_failed'    => 'failed',
46
+    );
47
+
48
+    /*
49 49
 	|--------------------------------------------------------------------------
50 50
 	| CRUD Methods
51 51
 	|--------------------------------------------------------------------------
52 52
 	*/
53 53
 
54
-	/**
55
-	 * Method to create a new form in the database.
56
-	 *
57
-	 * @param GetPaid_Payment_Form $form Form object.
58
-	 */
59
-	public function create( &$form ) {
60
-		$form->set_version( WPINV_VERSION );
61
-		$form->set_date_created( current_time('mysql') );
62
-
63
-		// Create a new post.
64
-		$id = wp_insert_post(
65
-			apply_filters(
66
-				'getpaid_new_payment_form_data',
67
-				array(
68
-					'post_date'     => $form->get_date_created( 'edit' ),
69
-					'post_type'     => 'wpi_payment_form',
70
-					'post_status'   => $this->get_post_status( $form ),
71
-					'ping_status'   => 'closed',
72
-					'post_author'   => $form->get_author( 'edit' ),
73
-					'post_title'    => $form->get_name( 'edit' ),
74
-				)
75
-			),
76
-			true
77
-		);
78
-
79
-		if ( $id && ! is_wp_error( $id ) ) {
80
-			$form->set_id( $id );
81
-			$this->update_post_meta( $form );
82
-			$form->save_meta_data();
83
-			$form->apply_changes();
84
-			$this->clear_caches( $form );
85
-			return true;
86
-		}
87
-
88
-		if ( is_wp_error( $id ) ) {
89
-			$form->last_error = $id->get_error_message();
90
-		}
91
-
92
-		return false;
93
-	}
94
-
95
-	/**
96
-	 * Method to read a form from the database.
97
-	 *
98
-	 * @param GetPaid_Payment_Form $form Form object.
99
-	 *
100
-	 */
101
-	public function read( &$form ) {
102
-
103
-		$form->set_defaults();
104
-		$form_object = get_post( $form->get_id() );
105
-
106
-		if ( ! $form->get_id() || ! $form_object || $form_object->post_type != 'wpi_payment_form' ) {
107
-			$form->last_error = __( 'Invalid form.', 'invoicing' );
108
-			return false;
109
-		}
110
-
111
-		$form->set_props(
112
-			array(
113
-				'date_created'  => 0 < $form_object->post_date_gmt ? $form_object->post_date_gmt : null,
114
-				'date_modified' => 0 < $form_object->post_modified_gmt ? $form_object->post_modified_gmt : null,
115
-				'status'        => $form_object->post_status,
116
-				'name'          => $form_object->post_title,
117
-				'author'        => $form_object->post_author,
118
-			)
119
-		);
120
-
121
-		$this->read_object_data( $form, $form_object );
122
-		$form->read_meta_data();
123
-		$form->set_object_read( true );
124
-
125
-	}
126
-
127
-	/**
128
-	 * Method to update a form in the database.
129
-	 *
130
-	 * @param GetPaid_Payment_Form $form Form object.
131
-	 */
132
-	public function update( &$form ) {
133
-		$form->save_meta_data();
134
-		$form->set_version( WPINV_VERSION );
135
-
136
-		if ( null === $form->get_date_created( 'edit' ) ) {
137
-			$form->set_date_created(  current_time('mysql') );
138
-		}
139
-
140
-		$changes = $form->get_changes();
141
-
142
-		// Only update the post when the post data changes.
143
-		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author' ), array_keys( $changes ) ) ) {
144
-			$post_data = array(
145
-				'post_date'         => $form->get_date_created( 'edit' ),
146
-				'post_status'       => $form->get_status( 'edit' ),
147
-				'post_title'        => $form->get_name( 'edit' ),
148
-				'post_author'       => $form->get_author( 'edit' ),
149
-				'post_modified'     => $form->get_date_modified( 'edit' ),
150
-			);
151
-
152
-			/**
153
-			 * When updating this object, to prevent infinite loops, use $wpdb
154
-			 * to update data, since wp_update_post spawns more calls to the
155
-			 * save_post action.
156
-			 *
157
-			 * This ensures hooks are fired by either WP itself (admin screen save),
158
-			 * or an update purely from CRUD.
159
-			 */
160
-			if ( doing_action( 'save_post' ) ) {
161
-				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $form->get_id() ) );
162
-				clean_post_cache( $form->get_id() );
163
-			} else {
164
-				wp_update_post( array_merge( array( 'ID' => $form->get_id() ), $post_data ) );
165
-			}
166
-			$form->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
167
-		}
168
-		$this->update_post_meta( $form );
169
-		$form->apply_changes();
170
-		$this->clear_caches( $form );
171
-	}
172
-
173
-	/*
54
+    /**
55
+     * Method to create a new form in the database.
56
+     *
57
+     * @param GetPaid_Payment_Form $form Form object.
58
+     */
59
+    public function create( &$form ) {
60
+        $form->set_version( WPINV_VERSION );
61
+        $form->set_date_created( current_time('mysql') );
62
+
63
+        // Create a new post.
64
+        $id = wp_insert_post(
65
+            apply_filters(
66
+                'getpaid_new_payment_form_data',
67
+                array(
68
+                    'post_date'     => $form->get_date_created( 'edit' ),
69
+                    'post_type'     => 'wpi_payment_form',
70
+                    'post_status'   => $this->get_post_status( $form ),
71
+                    'ping_status'   => 'closed',
72
+                    'post_author'   => $form->get_author( 'edit' ),
73
+                    'post_title'    => $form->get_name( 'edit' ),
74
+                )
75
+            ),
76
+            true
77
+        );
78
+
79
+        if ( $id && ! is_wp_error( $id ) ) {
80
+            $form->set_id( $id );
81
+            $this->update_post_meta( $form );
82
+            $form->save_meta_data();
83
+            $form->apply_changes();
84
+            $this->clear_caches( $form );
85
+            return true;
86
+        }
87
+
88
+        if ( is_wp_error( $id ) ) {
89
+            $form->last_error = $id->get_error_message();
90
+        }
91
+
92
+        return false;
93
+    }
94
+
95
+    /**
96
+     * Method to read a form from the database.
97
+     *
98
+     * @param GetPaid_Payment_Form $form Form object.
99
+     *
100
+     */
101
+    public function read( &$form ) {
102
+
103
+        $form->set_defaults();
104
+        $form_object = get_post( $form->get_id() );
105
+
106
+        if ( ! $form->get_id() || ! $form_object || $form_object->post_type != 'wpi_payment_form' ) {
107
+            $form->last_error = __( 'Invalid form.', 'invoicing' );
108
+            return false;
109
+        }
110
+
111
+        $form->set_props(
112
+            array(
113
+                'date_created'  => 0 < $form_object->post_date_gmt ? $form_object->post_date_gmt : null,
114
+                'date_modified' => 0 < $form_object->post_modified_gmt ? $form_object->post_modified_gmt : null,
115
+                'status'        => $form_object->post_status,
116
+                'name'          => $form_object->post_title,
117
+                'author'        => $form_object->post_author,
118
+            )
119
+        );
120
+
121
+        $this->read_object_data( $form, $form_object );
122
+        $form->read_meta_data();
123
+        $form->set_object_read( true );
124
+
125
+    }
126
+
127
+    /**
128
+     * Method to update a form in the database.
129
+     *
130
+     * @param GetPaid_Payment_Form $form Form object.
131
+     */
132
+    public function update( &$form ) {
133
+        $form->save_meta_data();
134
+        $form->set_version( WPINV_VERSION );
135
+
136
+        if ( null === $form->get_date_created( 'edit' ) ) {
137
+            $form->set_date_created(  current_time('mysql') );
138
+        }
139
+
140
+        $changes = $form->get_changes();
141
+
142
+        // Only update the post when the post data changes.
143
+        if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author' ), array_keys( $changes ) ) ) {
144
+            $post_data = array(
145
+                'post_date'         => $form->get_date_created( 'edit' ),
146
+                'post_status'       => $form->get_status( 'edit' ),
147
+                'post_title'        => $form->get_name( 'edit' ),
148
+                'post_author'       => $form->get_author( 'edit' ),
149
+                'post_modified'     => $form->get_date_modified( 'edit' ),
150
+            );
151
+
152
+            /**
153
+             * When updating this object, to prevent infinite loops, use $wpdb
154
+             * to update data, since wp_update_post spawns more calls to the
155
+             * save_post action.
156
+             *
157
+             * This ensures hooks are fired by either WP itself (admin screen save),
158
+             * or an update purely from CRUD.
159
+             */
160
+            if ( doing_action( 'save_post' ) ) {
161
+                $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $form->get_id() ) );
162
+                clean_post_cache( $form->get_id() );
163
+            } else {
164
+                wp_update_post( array_merge( array( 'ID' => $form->get_id() ), $post_data ) );
165
+            }
166
+            $form->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
167
+        }
168
+        $this->update_post_meta( $form );
169
+        $form->apply_changes();
170
+        $this->clear_caches( $form );
171
+    }
172
+
173
+    /*
174 174
 	|--------------------------------------------------------------------------
175 175
 	| Additional Methods
176 176
 	|--------------------------------------------------------------------------
Please login to merge, or discard this patch.
includes/data-stores/class-getpaid-data.php 1 patch
Indentation   +807 added lines, -807 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  discard block
 block discarded – undo
9 9
  */
10 10
 
11 11
 if ( ! defined( 'ABSPATH' ) ) {
12
-	exit;
12
+    exit;
13 13
 }
14 14
 
15 15
 /**
@@ -21,814 +21,814 @@  discard block
 block discarded – undo
21 21
  */
22 22
 abstract class GetPaid_Data {
23 23
 
24
-	/**
25
-	 * ID for this object.
26
-	 *
27
-	 * @since 1.0.19
28
-	 * @var int
29
-	 */
30
-	protected $id = 0;
31
-
32
-	/**
33
-	 * Core data for this object. Name value pairs (name + default value).
34
-	 *
35
-	 * @since 1.0.19
36
-	 * @var array
37
-	 */
38
-	protected $data = array();
39
-
40
-	/**
41
-	 * Core data changes for this object.
42
-	 *
43
-	 * @since 1.0.19
44
-	 * @var array
45
-	 */
46
-	protected $changes = array();
47
-
48
-	/**
49
-	 * This is false until the object is read from the DB.
50
-	 *
51
-	 * @since 1.0.19
52
-	 * @var bool
53
-	 */
54
-	protected $object_read = false;
55
-
56
-	/**
57
-	 * This is the name of this object type.
58
-	 *
59
-	 * @since 1.0.19
60
-	 * @var string
61
-	 */
62
-	protected $object_type = 'data';
63
-
64
-	/**
65
-	 * Extra data for this object. Name value pairs (name + default value).
66
-	 * Used as a standard way for sub classes (like item types) to add
67
-	 * additional information to an inherited class.
68
-	 *
69
-	 * @since 1.0.19
70
-	 * @var array
71
-	 */
72
-	protected $extra_data = array();
73
-
74
-	/**
75
-	 * Set to _data on construct so we can track and reset data if needed.
76
-	 *
77
-	 * @since 1.0.19
78
-	 * @var array
79
-	 */
80
-	protected $default_data = array();
81
-
82
-	/**
83
-	 * Contains a reference to the data store for this class.
84
-	 *
85
-	 * @since 1.0.19
86
-	 * @var GetPaid_Data_Store
87
-	 */
88
-	protected $data_store;
89
-
90
-	/**
91
-	 * Stores meta in cache for future reads.
92
-	 * A group must be set to to enable caching.
93
-	 *
94
-	 * @since 1.0.19
95
-	 * @var string
96
-	 */
97
-	protected $cache_group = '';
98
-
99
-	/**
100
-	 * Stores the last error.
101
-	 *
102
-	 * @since 1.0.19
103
-	 * @var string
104
-	 */
105
-	public $last_error = '';
106
-
107
-	/**
108
-	 * Stores additional meta data.
109
-	 *
110
-	 * @since 1.0.19
111
-	 * @var array
112
-	 */
113
-	protected $meta_data = null;
114
-
115
-	/**
116
-	 * Default constructor.
117
-	 *
118
-	 * @param int|object|array $read ID to load from the DB (optional) or already queried data.
119
-	 */
120
-	public function __construct( $read = 0 ) {
121
-		$this->data         = array_merge( $this->data, $this->extra_data );
122
-		$this->default_data = $this->data;
123
-	}
124
-
125
-	/**
126
-	 * Only store the object ID to avoid serializing the data object instance.
127
-	 *
128
-	 * @return array
129
-	 */
130
-	public function __sleep() {
131
-		return array( 'id' );
132
-	}
133
-
134
-	/**
135
-	 * Re-run the constructor with the object ID.
136
-	 *
137
-	 * If the object no longer exists, remove the ID.
138
-	 */
139
-	public function __wakeup() {
140
-		try {
141
-			$this->__construct( absint( $this->id ) );
142
-		} catch ( Exception $e ) {
143
-			$this->set_id( 0 );
144
-			$this->set_object_read( true );
145
-		}
146
-	}
147
-
148
-	/**
149
-	 * When the object is cloned, make sure meta is duplicated correctly.
150
-	 *
151
-	 * @since 1.0.19
152
-	 */
153
-	public function __clone() {
154
-		$this->maybe_read_meta_data();
155
-		if ( ! empty( $this->meta_data ) ) {
156
-			foreach ( $this->meta_data as $array_key => $meta ) {
157
-				$this->meta_data[ $array_key ] = clone $meta;
158
-				if ( ! empty( $meta->id ) ) {
159
-					$this->meta_data[ $array_key ]->id = null;
160
-				}
161
-			}
162
-		}
163
-	}
164
-
165
-	/**
166
-	 * Get the data store.
167
-	 *
168
-	 * @since  1.0.19
169
-	 * @return object
170
-	 */
171
-	public function get_data_store() {
172
-		return $this->data_store;
173
-	}
174
-
175
-	/**
176
-	 * Get the object type.
177
-	 *
178
-	 * @since  1.0.19
179
-	 * @return string
180
-	 */
181
-	public function get_object_type() {
182
-		return $this->object_type;
183
-	}
184
-
185
-	/**
186
-	 * Returns the unique ID for this object.
187
-	 *
188
-	 * @since  1.0.19
189
-	 * @return int
190
-	 */
191
-	public function get_id() {
192
-		return $this->id;
193
-	}
194
-
195
-	/**
196
-	 * Get form status.
197
-	 *
198
-	 * @since 1.0.19
199
-	 * @param  string $context View or edit context.
200
-	 * @return string
201
-	 */
202
-	public function get_status( $context = 'view' ) {
203
-		return $this->get_prop( 'status', $context );
204
-    }
205
-
206
-	/**
207
-	 * Delete an object, set the ID to 0, and return result.
208
-	 *
209
-	 * @since  1.0.19
210
-	 * @param  bool $force_delete Should the data be deleted permanently.
211
-	 * @return bool result
212
-	 */
213
-	public function delete( $force_delete = false ) {
214
-		if ( $this->data_store ) {
215
-			$this->data_store->delete( $this, array( 'force_delete' => $force_delete ) );
216
-			$this->set_id( 0 );
217
-			return true;
218
-		}
219
-		return false;
220
-	}
221
-
222
-	/**
223
-	 * Save should create or update based on object existence.
224
-	 *
225
-	 * @since  1.0.19
226
-	 * @return int
227
-	 */
228
-	public function save() {
229
-		if ( ! $this->data_store ) {
230
-			return $this->get_id();
231
-		}
232
-
233
-		/**
234
-		 * Trigger action before saving to the DB. Allows you to adjust object props before save.
235
-		 *
236
-		 * @param GetPaid_Data          $this The object being saved.
237
-		 * @param GetPaid_Data_Store_WP $data_store The data store persisting the data.
238
-		 */
239
-		do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store );
240
-
241
-		if ( $this->get_id() ) {
242
-			$this->data_store->update( $this );
243
-		} else {
244
-			$this->data_store->create( $this );
245
-		}
246
-
247
-		/**
248
-		 * Trigger action after saving to the DB.
249
-		 *
250
-		 * @param GetPaid_Data          $this The object being saved.
251
-		 * @param GetPaid_Data_Store_WP $data_store The data store persisting the data.
252
-		 */
253
-		do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store );
254
-
255
-		return $this->get_id();
256
-	}
257
-
258
-	/**
259
-	 * Change data to JSON format.
260
-	 *
261
-	 * @since  1.0.19
262
-	 * @return string Data in JSON format.
263
-	 */
264
-	public function __toString() {
265
-		return wp_json_encode( $this->get_data() );
266
-	}
267
-
268
-	/**
269
-	 * Returns all data for this object.
270
-	 *
271
-	 * @since  1.0.19
272
-	 * @return array
273
-	 */
274
-	public function get_data() {
275
-		return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) );
276
-	}
277
-
278
-	/**
279
-	 * Returns array of expected data keys for this object.
280
-	 *
281
-	 * @since   1.0.19
282
-	 * @return array
283
-	 */
284
-	public function get_data_keys() {
285
-		return array_keys( $this->data );
286
-	}
287
-
288
-	/**
289
-	 * Returns all "extra" data keys for an object (for sub objects like item types).
290
-	 *
291
-	 * @since  1.0.19
292
-	 * @return array
293
-	 */
294
-	public function get_extra_data_keys() {
295
-		return array_keys( $this->extra_data );
296
-	}
297
-
298
-	/**
299
-	 * Filter null meta values from array.
300
-	 *
301
-	 * @since  1.0.19
302
-	 * @param mixed $meta Meta value to check.
303
-	 * @return bool
304
-	 */
305
-	protected function filter_null_meta( $meta ) {
306
-		return ! is_null( $meta->value );
307
-	}
308
-
309
-	/**
310
-	 * Get All Meta Data.
311
-	 *
312
-	 * @since 1.0.19
313
-	 * @return array of objects.
314
-	 */
315
-	public function get_meta_data() {
316
-		$this->maybe_read_meta_data();
317
-		return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) );
318
-	}
319
-
320
-	/**
321
-	 * Check if the key is an internal one.
322
-	 *
323
-	 * @since  1.0.19
324
-	 * @param  string $key Key to check.
325
-	 * @return bool   true if it's an internal key, false otherwise
326
-	 */
327
-	protected function is_internal_meta_key( $key ) {
328
-		$internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true );
329
-
330
-		if ( ! $internal_meta_key ) {
331
-			return false;
332
-		}
333
-
334
-		$has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) );
335
-
336
-		if ( ! $has_setter_or_getter ) {
337
-			return false;
338
-		}
339
-
340
-		/* translators: %s: $key Key to check */
341
-		getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' );
342
-
343
-		return true;
344
-	}
345
-
346
-	/**
347
-	 * Get Meta Data by Key.
348
-	 *
349
-	 * @since  1.0.19
350
-	 * @param  string $key Meta Key.
351
-	 * @param  bool   $single return first found meta with key, or all with $key.
352
-	 * @param  string $context What the value is for. Valid values are view and edit.
353
-	 * @return mixed
354
-	 */
355
-	public function get_meta( $key = '', $single = true, $context = 'view' ) {
356
-
357
-		// Check if this is an internal meta key.
358
-		if ( $this->is_internal_meta_key( $key ) ) {
359
-			$function = 'get_' . $key;
360
-
361
-			if ( is_callable( array( $this, $function ) ) ) {
362
-				return $this->{$function}();
363
-			}
364
-		}
365
-
366
-		// Read the meta data if not yet read.
367
-		$this->maybe_read_meta_data();
368
-		$meta_data  = $this->get_meta_data();
369
-		$array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true );
370
-		$value      = $single ? '' : array();
371
-
372
-		if ( ! empty( $array_keys ) ) {
373
-			// We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()).
374
-			if ( $single ) {
375
-				$value = $meta_data[ current( $array_keys ) ]->value;
376
-			} else {
377
-				$value = array_intersect_key( $meta_data, array_flip( $array_keys ) );
378
-			}
379
-		}
380
-
381
-		if ( 'view' === $context ) {
382
-			$value = apply_filters( $this->get_hook_prefix() . $key, $value, $this );
383
-		}
384
-
385
-		return $value;
386
-	}
387
-
388
-	/**
389
-	 * See if meta data exists, since get_meta always returns a '' or array().
390
-	 *
391
-	 * @since  1.0.19
392
-	 * @param  string $key Meta Key.
393
-	 * @return boolean
394
-	 */
395
-	public function meta_exists( $key = '' ) {
396
-		$this->maybe_read_meta_data();
397
-		$array_keys = wp_list_pluck( $this->get_meta_data(), 'key' );
398
-		return in_array( $key, $array_keys, true );
399
-	}
400
-
401
-	/**
402
-	 * Set all meta data from array.
403
-	 *
404
-	 * @since 1.0.19
405
-	 * @param array $data Key/Value pairs.
406
-	 */
407
-	public function set_meta_data( $data ) {
408
-		if ( ! empty( $data ) && is_array( $data ) ) {
409
-			$this->maybe_read_meta_data();
410
-			foreach ( $data as $meta ) {
411
-				$meta = (array) $meta;
412
-				if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
413
-					$this->meta_data[] = new GetPaid_Meta_Data(
414
-						array(
415
-							'id'    => $meta['id'],
416
-							'key'   => $meta['key'],
417
-							'value' => $meta['value'],
418
-						)
419
-					);
420
-				}
421
-			}
422
-		}
423
-	}
424
-
425
-	/**
426
-	 * Add meta data.
427
-	 *
428
-	 * @since 1.0.19
429
-	 *
430
-	 * @param string       $key Meta key.
431
-	 * @param string|array $value Meta value.
432
-	 * @param bool         $unique Should this be a unique key?.
433
-	 */
434
-	public function add_meta_data( $key, $value, $unique = false ) {
435
-		if ( $this->is_internal_meta_key( $key ) ) {
436
-			$function = 'set_' . $key;
437
-
438
-			if ( is_callable( array( $this, $function ) ) ) {
439
-				return $this->{$function}( $value );
440
-			}
441
-		}
442
-
443
-		$this->maybe_read_meta_data();
444
-		if ( $unique ) {
445
-			$this->delete_meta_data( $key );
446
-		}
447
-		$this->meta_data[] = new GetPaid_Meta_Data(
448
-			array(
449
-				'key'   => $key,
450
-				'value' => $value,
451
-			)
452
-		);
453
-	}
454
-
455
-	/**
456
-	 * Update meta data by key or ID, if provided.
457
-	 *
458
-	 * @since  1.0.19
459
-	 *
460
-	 * @param  string       $key Meta key.
461
-	 * @param  string|array $value Meta value.
462
-	 * @param  int          $meta_id Meta ID.
463
-	 */
464
-	public function update_meta_data( $key, $value, $meta_id = 0 ) {
465
-		if ( $this->is_internal_meta_key( $key ) ) {
466
-			$function = 'set_' . $key;
467
-
468
-			if ( is_callable( array( $this, $function ) ) ) {
469
-				return $this->{$function}( $value );
470
-			}
471
-		}
472
-
473
-		$this->maybe_read_meta_data();
474
-
475
-		$array_key = false;
476
-
477
-		if ( $meta_id ) {
478
-			$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true );
479
-			$array_key  = $array_keys ? current( $array_keys ) : false;
480
-		} else {
481
-			// Find matches by key.
482
-			$matches = array();
483
-			foreach ( $this->meta_data as $meta_data_array_key => $meta ) {
484
-				if ( $meta->key === $key ) {
485
-					$matches[] = $meta_data_array_key;
486
-				}
487
-			}
488
-
489
-			if ( ! empty( $matches ) ) {
490
-				// Set matches to null so only one key gets the new value.
491
-				foreach ( $matches as $meta_data_array_key ) {
492
-					$this->meta_data[ $meta_data_array_key ]->value = null;
493
-				}
494
-				$array_key = current( $matches );
495
-			}
496
-		}
497
-
498
-		if ( false !== $array_key ) {
499
-			$meta        = $this->meta_data[ $array_key ];
500
-			$meta->key   = $key;
501
-			$meta->value = $value;
502
-		} else {
503
-			$this->add_meta_data( $key, $value, true );
504
-		}
505
-	}
506
-
507
-	/**
508
-	 * Delete meta data.
509
-	 *
510
-	 * @since 1.0.19
511
-	 * @param string $key Meta key.
512
-	 */
513
-	public function delete_meta_data( $key ) {
514
-		$this->maybe_read_meta_data();
515
-		$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true );
516
-
517
-		if ( $array_keys ) {
518
-			foreach ( $array_keys as $array_key ) {
519
-				$this->meta_data[ $array_key ]->value = null;
520
-			}
521
-		}
522
-	}
523
-
524
-	/**
525
-	 * Delete meta data.
526
-	 *
527
-	 * @since 1.0.19
528
-	 * @param int $mid Meta ID.
529
-	 */
530
-	public function delete_meta_data_by_mid( $mid ) {
531
-		$this->maybe_read_meta_data();
532
-		$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true );
533
-
534
-		if ( $array_keys ) {
535
-			foreach ( $array_keys as $array_key ) {
536
-				$this->meta_data[ $array_key ]->value = null;
537
-			}
538
-		}
539
-	}
540
-
541
-	/**
542
-	 * Read meta data if null.
543
-	 *
544
-	 * @since 1.0.19
545
-	 */
546
-	protected function maybe_read_meta_data() {
547
-		if ( is_null( $this->meta_data ) ) {
548
-			$this->read_meta_data();
549
-		}
550
-	}
551
-
552
-	/**
553
-	 * Read Meta Data from the database. Ignore any internal properties.
554
-	 * Uses it's own caches because get_metadata does not provide meta_ids.
555
-	 *
556
-	 * @since 1.0.19
557
-	 * @param bool $force_read True to force a new DB read (and update cache).
558
-	 */
559
-	public function read_meta_data( $force_read = false ) {
560
-		$this->meta_data = array();
561
-		$cache_loaded    = false;
562
-
563
-		if ( ! $this->get_id() ) {
564
-			return;
565
-		}
566
-
567
-		if ( ! $this->data_store ) {
568
-			return;
569
-		}
570
-
571
-		if ( ! empty( $this->cache_group ) ) {
572
-			$cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id();
573
-		}
574
-
575
-		if ( ! $force_read ) {
576
-			if ( ! empty( $this->cache_group ) ) {
577
-				$cached_meta  = wp_cache_get( $cache_key, $this->cache_group );
578
-				$cache_loaded = ! empty( $cached_meta );
579
-			}
580
-		}
581
-
582
-		$raw_meta_data = $cache_loaded ? $cached_meta : $this->data_store->read_meta( $this );
583
-		if ( $raw_meta_data ) {
584
-			foreach ( $raw_meta_data as $meta ) {
585
-				$this->meta_data[] = new GetPaid_Meta_Data(
586
-					array(
587
-						'id'    => (int) $meta->meta_id,
588
-						'key'   => $meta->meta_key,
589
-						'value' => maybe_unserialize( $meta->meta_value ),
590
-					)
591
-				);
592
-			}
593
-
594
-			if ( ! $cache_loaded && ! empty( $this->cache_group ) ) {
595
-				wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group );
596
-			}
597
-		}
598
-	}
599
-
600
-	/**
601
-	 * Update Meta Data in the database.
602
-	 *
603
-	 * @since 1.0.19
604
-	 */
605
-	public function save_meta_data() {
606
-		if ( ! $this->data_store || is_null( $this->meta_data ) ) {
607
-			return;
608
-		}
609
-		foreach ( $this->meta_data as $array_key => $meta ) {
610
-			if ( is_null( $meta->value ) ) {
611
-				if ( ! empty( $meta->id ) ) {
612
-					$this->data_store->delete_meta( $this, $meta );
613
-					unset( $this->meta_data[ $array_key ] );
614
-				}
615
-			} elseif ( empty( $meta->id ) ) {
616
-				$meta->id = $this->data_store->add_meta( $this, $meta );
617
-				$meta->apply_changes();
618
-			} else {
619
-				if ( $meta->get_changes() ) {
620
-					$this->data_store->update_meta( $this, $meta );
621
-					$meta->apply_changes();
622
-				}
623
-			}
624
-		}
625
-		if ( ! empty( $this->cache_group ) ) {
626
-			$cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id();
627
-			wp_cache_delete( $cache_key, $this->cache_group );
628
-		}
629
-	}
630
-
631
-	/**
632
-	 * Set ID.
633
-	 *
634
-	 * @since 1.0.19
635
-	 * @param int $id ID.
636
-	 */
637
-	public function set_id( $id ) {
638
-		$this->id = absint( $id );
639
-	}
640
-
641
-	/**
642
-	 * Sets item status.
643
-	 *
644
-	 * @since 1.0.19
645
-	 * @param  string $status New status.
646
-	 * @return array details of change.
647
-	 */
648
-	public function set_status( $status ) {
24
+    /**
25
+     * ID for this object.
26
+     *
27
+     * @since 1.0.19
28
+     * @var int
29
+     */
30
+    protected $id = 0;
31
+
32
+    /**
33
+     * Core data for this object. Name value pairs (name + default value).
34
+     *
35
+     * @since 1.0.19
36
+     * @var array
37
+     */
38
+    protected $data = array();
39
+
40
+    /**
41
+     * Core data changes for this object.
42
+     *
43
+     * @since 1.0.19
44
+     * @var array
45
+     */
46
+    protected $changes = array();
47
+
48
+    /**
49
+     * This is false until the object is read from the DB.
50
+     *
51
+     * @since 1.0.19
52
+     * @var bool
53
+     */
54
+    protected $object_read = false;
55
+
56
+    /**
57
+     * This is the name of this object type.
58
+     *
59
+     * @since 1.0.19
60
+     * @var string
61
+     */
62
+    protected $object_type = 'data';
63
+
64
+    /**
65
+     * Extra data for this object. Name value pairs (name + default value).
66
+     * Used as a standard way for sub classes (like item types) to add
67
+     * additional information to an inherited class.
68
+     *
69
+     * @since 1.0.19
70
+     * @var array
71
+     */
72
+    protected $extra_data = array();
73
+
74
+    /**
75
+     * Set to _data on construct so we can track and reset data if needed.
76
+     *
77
+     * @since 1.0.19
78
+     * @var array
79
+     */
80
+    protected $default_data = array();
81
+
82
+    /**
83
+     * Contains a reference to the data store for this class.
84
+     *
85
+     * @since 1.0.19
86
+     * @var GetPaid_Data_Store
87
+     */
88
+    protected $data_store;
89
+
90
+    /**
91
+     * Stores meta in cache for future reads.
92
+     * A group must be set to to enable caching.
93
+     *
94
+     * @since 1.0.19
95
+     * @var string
96
+     */
97
+    protected $cache_group = '';
98
+
99
+    /**
100
+     * Stores the last error.
101
+     *
102
+     * @since 1.0.19
103
+     * @var string
104
+     */
105
+    public $last_error = '';
106
+
107
+    /**
108
+     * Stores additional meta data.
109
+     *
110
+     * @since 1.0.19
111
+     * @var array
112
+     */
113
+    protected $meta_data = null;
114
+
115
+    /**
116
+     * Default constructor.
117
+     *
118
+     * @param int|object|array $read ID to load from the DB (optional) or already queried data.
119
+     */
120
+    public function __construct( $read = 0 ) {
121
+        $this->data         = array_merge( $this->data, $this->extra_data );
122
+        $this->default_data = $this->data;
123
+    }
124
+
125
+    /**
126
+     * Only store the object ID to avoid serializing the data object instance.
127
+     *
128
+     * @return array
129
+     */
130
+    public function __sleep() {
131
+        return array( 'id' );
132
+    }
133
+
134
+    /**
135
+     * Re-run the constructor with the object ID.
136
+     *
137
+     * If the object no longer exists, remove the ID.
138
+     */
139
+    public function __wakeup() {
140
+        try {
141
+            $this->__construct( absint( $this->id ) );
142
+        } catch ( Exception $e ) {
143
+            $this->set_id( 0 );
144
+            $this->set_object_read( true );
145
+        }
146
+    }
147
+
148
+    /**
149
+     * When the object is cloned, make sure meta is duplicated correctly.
150
+     *
151
+     * @since 1.0.19
152
+     */
153
+    public function __clone() {
154
+        $this->maybe_read_meta_data();
155
+        if ( ! empty( $this->meta_data ) ) {
156
+            foreach ( $this->meta_data as $array_key => $meta ) {
157
+                $this->meta_data[ $array_key ] = clone $meta;
158
+                if ( ! empty( $meta->id ) ) {
159
+                    $this->meta_data[ $array_key ]->id = null;
160
+                }
161
+            }
162
+        }
163
+    }
164
+
165
+    /**
166
+     * Get the data store.
167
+     *
168
+     * @since  1.0.19
169
+     * @return object
170
+     */
171
+    public function get_data_store() {
172
+        return $this->data_store;
173
+    }
174
+
175
+    /**
176
+     * Get the object type.
177
+     *
178
+     * @since  1.0.19
179
+     * @return string
180
+     */
181
+    public function get_object_type() {
182
+        return $this->object_type;
183
+    }
184
+
185
+    /**
186
+     * Returns the unique ID for this object.
187
+     *
188
+     * @since  1.0.19
189
+     * @return int
190
+     */
191
+    public function get_id() {
192
+        return $this->id;
193
+    }
194
+
195
+    /**
196
+     * Get form status.
197
+     *
198
+     * @since 1.0.19
199
+     * @param  string $context View or edit context.
200
+     * @return string
201
+     */
202
+    public function get_status( $context = 'view' ) {
203
+        return $this->get_prop( 'status', $context );
204
+    }
205
+
206
+    /**
207
+     * Delete an object, set the ID to 0, and return result.
208
+     *
209
+     * @since  1.0.19
210
+     * @param  bool $force_delete Should the data be deleted permanently.
211
+     * @return bool result
212
+     */
213
+    public function delete( $force_delete = false ) {
214
+        if ( $this->data_store ) {
215
+            $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) );
216
+            $this->set_id( 0 );
217
+            return true;
218
+        }
219
+        return false;
220
+    }
221
+
222
+    /**
223
+     * Save should create or update based on object existence.
224
+     *
225
+     * @since  1.0.19
226
+     * @return int
227
+     */
228
+    public function save() {
229
+        if ( ! $this->data_store ) {
230
+            return $this->get_id();
231
+        }
232
+
233
+        /**
234
+         * Trigger action before saving to the DB. Allows you to adjust object props before save.
235
+         *
236
+         * @param GetPaid_Data          $this The object being saved.
237
+         * @param GetPaid_Data_Store_WP $data_store The data store persisting the data.
238
+         */
239
+        do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store );
240
+
241
+        if ( $this->get_id() ) {
242
+            $this->data_store->update( $this );
243
+        } else {
244
+            $this->data_store->create( $this );
245
+        }
246
+
247
+        /**
248
+         * Trigger action after saving to the DB.
249
+         *
250
+         * @param GetPaid_Data          $this The object being saved.
251
+         * @param GetPaid_Data_Store_WP $data_store The data store persisting the data.
252
+         */
253
+        do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store );
254
+
255
+        return $this->get_id();
256
+    }
257
+
258
+    /**
259
+     * Change data to JSON format.
260
+     *
261
+     * @since  1.0.19
262
+     * @return string Data in JSON format.
263
+     */
264
+    public function __toString() {
265
+        return wp_json_encode( $this->get_data() );
266
+    }
267
+
268
+    /**
269
+     * Returns all data for this object.
270
+     *
271
+     * @since  1.0.19
272
+     * @return array
273
+     */
274
+    public function get_data() {
275
+        return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) );
276
+    }
277
+
278
+    /**
279
+     * Returns array of expected data keys for this object.
280
+     *
281
+     * @since   1.0.19
282
+     * @return array
283
+     */
284
+    public function get_data_keys() {
285
+        return array_keys( $this->data );
286
+    }
287
+
288
+    /**
289
+     * Returns all "extra" data keys for an object (for sub objects like item types).
290
+     *
291
+     * @since  1.0.19
292
+     * @return array
293
+     */
294
+    public function get_extra_data_keys() {
295
+        return array_keys( $this->extra_data );
296
+    }
297
+
298
+    /**
299
+     * Filter null meta values from array.
300
+     *
301
+     * @since  1.0.19
302
+     * @param mixed $meta Meta value to check.
303
+     * @return bool
304
+     */
305
+    protected function filter_null_meta( $meta ) {
306
+        return ! is_null( $meta->value );
307
+    }
308
+
309
+    /**
310
+     * Get All Meta Data.
311
+     *
312
+     * @since 1.0.19
313
+     * @return array of objects.
314
+     */
315
+    public function get_meta_data() {
316
+        $this->maybe_read_meta_data();
317
+        return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) );
318
+    }
319
+
320
+    /**
321
+     * Check if the key is an internal one.
322
+     *
323
+     * @since  1.0.19
324
+     * @param  string $key Key to check.
325
+     * @return bool   true if it's an internal key, false otherwise
326
+     */
327
+    protected function is_internal_meta_key( $key ) {
328
+        $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true );
329
+
330
+        if ( ! $internal_meta_key ) {
331
+            return false;
332
+        }
333
+
334
+        $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) );
335
+
336
+        if ( ! $has_setter_or_getter ) {
337
+            return false;
338
+        }
339
+
340
+        /* translators: %s: $key Key to check */
341
+        getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' );
342
+
343
+        return true;
344
+    }
345
+
346
+    /**
347
+     * Get Meta Data by Key.
348
+     *
349
+     * @since  1.0.19
350
+     * @param  string $key Meta Key.
351
+     * @param  bool   $single return first found meta with key, or all with $key.
352
+     * @param  string $context What the value is for. Valid values are view and edit.
353
+     * @return mixed
354
+     */
355
+    public function get_meta( $key = '', $single = true, $context = 'view' ) {
356
+
357
+        // Check if this is an internal meta key.
358
+        if ( $this->is_internal_meta_key( $key ) ) {
359
+            $function = 'get_' . $key;
360
+
361
+            if ( is_callable( array( $this, $function ) ) ) {
362
+                return $this->{$function}();
363
+            }
364
+        }
365
+
366
+        // Read the meta data if not yet read.
367
+        $this->maybe_read_meta_data();
368
+        $meta_data  = $this->get_meta_data();
369
+        $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true );
370
+        $value      = $single ? '' : array();
371
+
372
+        if ( ! empty( $array_keys ) ) {
373
+            // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()).
374
+            if ( $single ) {
375
+                $value = $meta_data[ current( $array_keys ) ]->value;
376
+            } else {
377
+                $value = array_intersect_key( $meta_data, array_flip( $array_keys ) );
378
+            }
379
+        }
380
+
381
+        if ( 'view' === $context ) {
382
+            $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this );
383
+        }
384
+
385
+        return $value;
386
+    }
387
+
388
+    /**
389
+     * See if meta data exists, since get_meta always returns a '' or array().
390
+     *
391
+     * @since  1.0.19
392
+     * @param  string $key Meta Key.
393
+     * @return boolean
394
+     */
395
+    public function meta_exists( $key = '' ) {
396
+        $this->maybe_read_meta_data();
397
+        $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' );
398
+        return in_array( $key, $array_keys, true );
399
+    }
400
+
401
+    /**
402
+     * Set all meta data from array.
403
+     *
404
+     * @since 1.0.19
405
+     * @param array $data Key/Value pairs.
406
+     */
407
+    public function set_meta_data( $data ) {
408
+        if ( ! empty( $data ) && is_array( $data ) ) {
409
+            $this->maybe_read_meta_data();
410
+            foreach ( $data as $meta ) {
411
+                $meta = (array) $meta;
412
+                if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
413
+                    $this->meta_data[] = new GetPaid_Meta_Data(
414
+                        array(
415
+                            'id'    => $meta['id'],
416
+                            'key'   => $meta['key'],
417
+                            'value' => $meta['value'],
418
+                        )
419
+                    );
420
+                }
421
+            }
422
+        }
423
+    }
424
+
425
+    /**
426
+     * Add meta data.
427
+     *
428
+     * @since 1.0.19
429
+     *
430
+     * @param string       $key Meta key.
431
+     * @param string|array $value Meta value.
432
+     * @param bool         $unique Should this be a unique key?.
433
+     */
434
+    public function add_meta_data( $key, $value, $unique = false ) {
435
+        if ( $this->is_internal_meta_key( $key ) ) {
436
+            $function = 'set_' . $key;
437
+
438
+            if ( is_callable( array( $this, $function ) ) ) {
439
+                return $this->{$function}( $value );
440
+            }
441
+        }
442
+
443
+        $this->maybe_read_meta_data();
444
+        if ( $unique ) {
445
+            $this->delete_meta_data( $key );
446
+        }
447
+        $this->meta_data[] = new GetPaid_Meta_Data(
448
+            array(
449
+                'key'   => $key,
450
+                'value' => $value,
451
+            )
452
+        );
453
+    }
454
+
455
+    /**
456
+     * Update meta data by key or ID, if provided.
457
+     *
458
+     * @since  1.0.19
459
+     *
460
+     * @param  string       $key Meta key.
461
+     * @param  string|array $value Meta value.
462
+     * @param  int          $meta_id Meta ID.
463
+     */
464
+    public function update_meta_data( $key, $value, $meta_id = 0 ) {
465
+        if ( $this->is_internal_meta_key( $key ) ) {
466
+            $function = 'set_' . $key;
467
+
468
+            if ( is_callable( array( $this, $function ) ) ) {
469
+                return $this->{$function}( $value );
470
+            }
471
+        }
472
+
473
+        $this->maybe_read_meta_data();
474
+
475
+        $array_key = false;
476
+
477
+        if ( $meta_id ) {
478
+            $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true );
479
+            $array_key  = $array_keys ? current( $array_keys ) : false;
480
+        } else {
481
+            // Find matches by key.
482
+            $matches = array();
483
+            foreach ( $this->meta_data as $meta_data_array_key => $meta ) {
484
+                if ( $meta->key === $key ) {
485
+                    $matches[] = $meta_data_array_key;
486
+                }
487
+            }
488
+
489
+            if ( ! empty( $matches ) ) {
490
+                // Set matches to null so only one key gets the new value.
491
+                foreach ( $matches as $meta_data_array_key ) {
492
+                    $this->meta_data[ $meta_data_array_key ]->value = null;
493
+                }
494
+                $array_key = current( $matches );
495
+            }
496
+        }
497
+
498
+        if ( false !== $array_key ) {
499
+            $meta        = $this->meta_data[ $array_key ];
500
+            $meta->key   = $key;
501
+            $meta->value = $value;
502
+        } else {
503
+            $this->add_meta_data( $key, $value, true );
504
+        }
505
+    }
506
+
507
+    /**
508
+     * Delete meta data.
509
+     *
510
+     * @since 1.0.19
511
+     * @param string $key Meta key.
512
+     */
513
+    public function delete_meta_data( $key ) {
514
+        $this->maybe_read_meta_data();
515
+        $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true );
516
+
517
+        if ( $array_keys ) {
518
+            foreach ( $array_keys as $array_key ) {
519
+                $this->meta_data[ $array_key ]->value = null;
520
+            }
521
+        }
522
+    }
523
+
524
+    /**
525
+     * Delete meta data.
526
+     *
527
+     * @since 1.0.19
528
+     * @param int $mid Meta ID.
529
+     */
530
+    public function delete_meta_data_by_mid( $mid ) {
531
+        $this->maybe_read_meta_data();
532
+        $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true );
533
+
534
+        if ( $array_keys ) {
535
+            foreach ( $array_keys as $array_key ) {
536
+                $this->meta_data[ $array_key ]->value = null;
537
+            }
538
+        }
539
+    }
540
+
541
+    /**
542
+     * Read meta data if null.
543
+     *
544
+     * @since 1.0.19
545
+     */
546
+    protected function maybe_read_meta_data() {
547
+        if ( is_null( $this->meta_data ) ) {
548
+            $this->read_meta_data();
549
+        }
550
+    }
551
+
552
+    /**
553
+     * Read Meta Data from the database. Ignore any internal properties.
554
+     * Uses it's own caches because get_metadata does not provide meta_ids.
555
+     *
556
+     * @since 1.0.19
557
+     * @param bool $force_read True to force a new DB read (and update cache).
558
+     */
559
+    public function read_meta_data( $force_read = false ) {
560
+        $this->meta_data = array();
561
+        $cache_loaded    = false;
562
+
563
+        if ( ! $this->get_id() ) {
564
+            return;
565
+        }
566
+
567
+        if ( ! $this->data_store ) {
568
+            return;
569
+        }
570
+
571
+        if ( ! empty( $this->cache_group ) ) {
572
+            $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id();
573
+        }
574
+
575
+        if ( ! $force_read ) {
576
+            if ( ! empty( $this->cache_group ) ) {
577
+                $cached_meta  = wp_cache_get( $cache_key, $this->cache_group );
578
+                $cache_loaded = ! empty( $cached_meta );
579
+            }
580
+        }
581
+
582
+        $raw_meta_data = $cache_loaded ? $cached_meta : $this->data_store->read_meta( $this );
583
+        if ( $raw_meta_data ) {
584
+            foreach ( $raw_meta_data as $meta ) {
585
+                $this->meta_data[] = new GetPaid_Meta_Data(
586
+                    array(
587
+                        'id'    => (int) $meta->meta_id,
588
+                        'key'   => $meta->meta_key,
589
+                        'value' => maybe_unserialize( $meta->meta_value ),
590
+                    )
591
+                );
592
+            }
593
+
594
+            if ( ! $cache_loaded && ! empty( $this->cache_group ) ) {
595
+                wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group );
596
+            }
597
+        }
598
+    }
599
+
600
+    /**
601
+     * Update Meta Data in the database.
602
+     *
603
+     * @since 1.0.19
604
+     */
605
+    public function save_meta_data() {
606
+        if ( ! $this->data_store || is_null( $this->meta_data ) ) {
607
+            return;
608
+        }
609
+        foreach ( $this->meta_data as $array_key => $meta ) {
610
+            if ( is_null( $meta->value ) ) {
611
+                if ( ! empty( $meta->id ) ) {
612
+                    $this->data_store->delete_meta( $this, $meta );
613
+                    unset( $this->meta_data[ $array_key ] );
614
+                }
615
+            } elseif ( empty( $meta->id ) ) {
616
+                $meta->id = $this->data_store->add_meta( $this, $meta );
617
+                $meta->apply_changes();
618
+            } else {
619
+                if ( $meta->get_changes() ) {
620
+                    $this->data_store->update_meta( $this, $meta );
621
+                    $meta->apply_changes();
622
+                }
623
+            }
624
+        }
625
+        if ( ! empty( $this->cache_group ) ) {
626
+            $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id();
627
+            wp_cache_delete( $cache_key, $this->cache_group );
628
+        }
629
+    }
630
+
631
+    /**
632
+     * Set ID.
633
+     *
634
+     * @since 1.0.19
635
+     * @param int $id ID.
636
+     */
637
+    public function set_id( $id ) {
638
+        $this->id = absint( $id );
639
+    }
640
+
641
+    /**
642
+     * Sets item status.
643
+     *
644
+     * @since 1.0.19
645
+     * @param  string $status New status.
646
+     * @return array details of change.
647
+     */
648
+    public function set_status( $status ) {
649 649
         $old_status = $this->get_status();
650 650
 
651 651
         $this->set_prop( 'status', $status );
652 652
 
653
-		return array(
654
-			'from' => $old_status,
655
-			'to'   => $status,
656
-		);
657
-    }
658
-
659
-	/**
660
-	 * Set all props to default values.
661
-	 *
662
-	 * @since 1.0.19
663
-	 */
664
-	public function set_defaults() {
665
-		$this->data    = $this->default_data;
666
-		$this->changes = array();
667
-		$this->set_object_read( false );
668
-	}
669
-
670
-	/**
671
-	 * Set object read property.
672
-	 *
673
-	 * @since 1.0.19
674
-	 * @param boolean $read Should read?.
675
-	 */
676
-	public function set_object_read( $read = true ) {
677
-		$this->object_read = (bool) $read;
678
-	}
679
-
680
-	/**
681
-	 * Get object read property.
682
-	 *
683
-	 * @since  1.0.19
684
-	 * @return boolean
685
-	 */
686
-	public function get_object_read() {
687
-		return (bool) $this->object_read;
688
-	}
689
-
690
-	/**
691
-	 * Set a collection of props in one go, collect any errors, and return the result.
692
-	 * Only sets using public methods.
693
-	 *
694
-	 * @since  1.0.19
695
-	 *
696
-	 * @param array  $props Key value pairs to set. Key is the prop and should map to a setter function name.
697
-	 * @param string $context In what context to run this.
698
-	 *
699
-	 * @return bool|WP_Error
700
-	 */
701
-	public function set_props( $props, $context = 'set' ) {
702
-		$errors = false;
703
-
704
-		foreach ( $props as $prop => $value ) {
705
-			try {
706
-				/**
707
-				 * Checks if the prop being set is allowed, and the value is not null.
708
-				 */
709
-				if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) {
710
-					continue;
711
-				}
712
-				$setter = "set_$prop";
713
-
714
-				if ( is_callable( array( $this, $setter ) ) ) {
715
-					$this->{$setter}( $value );
716
-				}
717
-			} catch ( Exception $e ) {
718
-				if ( ! $errors ) {
719
-					$errors = new WP_Error();
720
-				}
721
-				$errors->add( $e->getCode(), $e->getMessage() );
722
-				$this->last_error = $e->getMessage();
723
-			}
724
-		}
725
-
726
-		return $errors && count( $errors->get_error_codes() ) ? $errors : true;
727
-	}
728
-
729
-	/**
730
-	 * Sets a prop for a setter method.
731
-	 *
732
-	 * This stores changes in a special array so we can track what needs saving
733
-	 * the the DB later.
734
-	 *
735
-	 * @since 1.0.19
736
-	 * @param string $prop Name of prop to set.
737
-	 * @param mixed  $value Value of the prop.
738
-	 */
739
-	protected function set_prop( $prop, $value ) {
740
-		if ( array_key_exists( $prop, $this->data ) ) {
741
-			if ( true === $this->object_read ) {
742
-				if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) {
743
-					$this->changes[ $prop ] = $value;
744
-				}
745
-			} else {
746
-				$this->data[ $prop ] = $value;
747
-			}
748
-		}
749
-	}
750
-
751
-	/**
752
-	 * Return data changes only.
753
-	 *
754
-	 * @since 1.0.19
755
-	 * @return array
756
-	 */
757
-	public function get_changes() {
758
-		return $this->changes;
759
-	}
760
-
761
-	/**
762
-	 * Merge changes with data and clear.
763
-	 *
764
-	 * @since 1.0.19
765
-	 */
766
-	public function apply_changes() {
767
-		$this->data    = array_replace_recursive( $this->data, $this->changes );
768
-		$this->changes = array();
769
-	}
770
-
771
-	/**
772
-	 * Prefix for action and filter hooks on data.
773
-	 *
774
-	 * @since  1.0.19
775
-	 * @return string
776
-	 */
777
-	protected function get_hook_prefix() {
778
-		return 'wpinv_get_' . $this->object_type . '_';
779
-	}
780
-
781
-	/**
782
-	 * Gets a prop for a getter method.
783
-	 *
784
-	 * Gets the value from either current pending changes, or the data itself.
785
-	 * Context controls what happens to the value before it's returned.
786
-	 *
787
-	 * @since  1.0.19
788
-	 * @param  string $prop Name of prop to get.
789
-	 * @param  string $context What the value is for. Valid values are view and edit.
790
-	 * @return mixed
791
-	 */
792
-	protected function get_prop( $prop, $context = 'view' ) {
793
-		$value = null;
794
-
795
-		if ( array_key_exists( $prop, $this->data ) ) {
796
-			$value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ];
797
-
798
-			if ( 'view' === $context ) {
799
-				$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
800
-			}
801
-		}
802
-
803
-		return $value;
804
-	}
805
-
806
-	/**
807
-	 * Sets a date prop whilst handling formatting and datetime objects.
808
-	 *
809
-	 * @since 1.0.19
810
-	 * @param string         $prop Name of prop to set.
811
-	 * @param string|integer $value Value of the prop.
812
-	 */
813
-	protected function set_date_prop( $prop, $value ) {
814
-
815
-		if ( empty( $value ) ) {
816
-			$this->set_prop( $prop, null );
817
-			return;
818
-		}
819
-		$this->set_prop( $prop, $value );
820
-
821
-	}
822
-
823
-	/**
824
-	 * When invalid data is found, throw an exception unless reading from the DB.
825
-	 *
826
-	 * @throws Exception Data Exception.
827
-	 * @since 1.0.19
828
-	 * @param string $code             Error code.
829
-	 * @param string $message          Error message.
830
-	 */
831
-	protected function error( $code, $message ) {
832
-		throw new Exception( $message, $code );
833
-	}
653
+        return array(
654
+            'from' => $old_status,
655
+            'to'   => $status,
656
+        );
657
+    }
658
+
659
+    /**
660
+     * Set all props to default values.
661
+     *
662
+     * @since 1.0.19
663
+     */
664
+    public function set_defaults() {
665
+        $this->data    = $this->default_data;
666
+        $this->changes = array();
667
+        $this->set_object_read( false );
668
+    }
669
+
670
+    /**
671
+     * Set object read property.
672
+     *
673
+     * @since 1.0.19
674
+     * @param boolean $read Should read?.
675
+     */
676
+    public function set_object_read( $read = true ) {
677
+        $this->object_read = (bool) $read;
678
+    }
679
+
680
+    /**
681
+     * Get object read property.
682
+     *
683
+     * @since  1.0.19
684
+     * @return boolean
685
+     */
686
+    public function get_object_read() {
687
+        return (bool) $this->object_read;
688
+    }
689
+
690
+    /**
691
+     * Set a collection of props in one go, collect any errors, and return the result.
692
+     * Only sets using public methods.
693
+     *
694
+     * @since  1.0.19
695
+     *
696
+     * @param array  $props Key value pairs to set. Key is the prop and should map to a setter function name.
697
+     * @param string $context In what context to run this.
698
+     *
699
+     * @return bool|WP_Error
700
+     */
701
+    public function set_props( $props, $context = 'set' ) {
702
+        $errors = false;
703
+
704
+        foreach ( $props as $prop => $value ) {
705
+            try {
706
+                /**
707
+                 * Checks if the prop being set is allowed, and the value is not null.
708
+                 */
709
+                if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) {
710
+                    continue;
711
+                }
712
+                $setter = "set_$prop";
713
+
714
+                if ( is_callable( array( $this, $setter ) ) ) {
715
+                    $this->{$setter}( $value );
716
+                }
717
+            } catch ( Exception $e ) {
718
+                if ( ! $errors ) {
719
+                    $errors = new WP_Error();
720
+                }
721
+                $errors->add( $e->getCode(), $e->getMessage() );
722
+                $this->last_error = $e->getMessage();
723
+            }
724
+        }
725
+
726
+        return $errors && count( $errors->get_error_codes() ) ? $errors : true;
727
+    }
728
+
729
+    /**
730
+     * Sets a prop for a setter method.
731
+     *
732
+     * This stores changes in a special array so we can track what needs saving
733
+     * the the DB later.
734
+     *
735
+     * @since 1.0.19
736
+     * @param string $prop Name of prop to set.
737
+     * @param mixed  $value Value of the prop.
738
+     */
739
+    protected function set_prop( $prop, $value ) {
740
+        if ( array_key_exists( $prop, $this->data ) ) {
741
+            if ( true === $this->object_read ) {
742
+                if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) {
743
+                    $this->changes[ $prop ] = $value;
744
+                }
745
+            } else {
746
+                $this->data[ $prop ] = $value;
747
+            }
748
+        }
749
+    }
750
+
751
+    /**
752
+     * Return data changes only.
753
+     *
754
+     * @since 1.0.19
755
+     * @return array
756
+     */
757
+    public function get_changes() {
758
+        return $this->changes;
759
+    }
760
+
761
+    /**
762
+     * Merge changes with data and clear.
763
+     *
764
+     * @since 1.0.19
765
+     */
766
+    public function apply_changes() {
767
+        $this->data    = array_replace_recursive( $this->data, $this->changes );
768
+        $this->changes = array();
769
+    }
770
+
771
+    /**
772
+     * Prefix for action and filter hooks on data.
773
+     *
774
+     * @since  1.0.19
775
+     * @return string
776
+     */
777
+    protected function get_hook_prefix() {
778
+        return 'wpinv_get_' . $this->object_type . '_';
779
+    }
780
+
781
+    /**
782
+     * Gets a prop for a getter method.
783
+     *
784
+     * Gets the value from either current pending changes, or the data itself.
785
+     * Context controls what happens to the value before it's returned.
786
+     *
787
+     * @since  1.0.19
788
+     * @param  string $prop Name of prop to get.
789
+     * @param  string $context What the value is for. Valid values are view and edit.
790
+     * @return mixed
791
+     */
792
+    protected function get_prop( $prop, $context = 'view' ) {
793
+        $value = null;
794
+
795
+        if ( array_key_exists( $prop, $this->data ) ) {
796
+            $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ];
797
+
798
+            if ( 'view' === $context ) {
799
+                $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
800
+            }
801
+        }
802
+
803
+        return $value;
804
+    }
805
+
806
+    /**
807
+     * Sets a date prop whilst handling formatting and datetime objects.
808
+     *
809
+     * @since 1.0.19
810
+     * @param string         $prop Name of prop to set.
811
+     * @param string|integer $value Value of the prop.
812
+     */
813
+    protected function set_date_prop( $prop, $value ) {
814
+
815
+        if ( empty( $value ) ) {
816
+            $this->set_prop( $prop, null );
817
+            return;
818
+        }
819
+        $this->set_prop( $prop, $value );
820
+
821
+    }
822
+
823
+    /**
824
+     * When invalid data is found, throw an exception unless reading from the DB.
825
+     *
826
+     * @throws Exception Data Exception.
827
+     * @since 1.0.19
828
+     * @param string $code             Error code.
829
+     * @param string $message          Error message.
830
+     */
831
+    protected function error( $code, $message ) {
832
+        throw new Exception( $message, $code );
833
+    }
834 834
 }
Please login to merge, or discard this patch.
includes/data-stores/class-getpaid-data-store-wp.php 1 patch
Indentation   +333 added lines, -333 removed lines patch added patch discarded remove patch
@@ -14,341 +14,341 @@
 block discarded – undo
14 14
  */
15 15
 class GetPaid_Data_Store_WP {
16 16
 
17
-	/**
18
-	 * Meta type. This should match up with
19
-	 * the types available at https://developer.wordpress.org/reference/functions/add_metadata/.
20
-	 * WP defines 'post', 'user', 'comment', and 'term'.
21
-	 *
22
-	 * @var string
23
-	 */
24
-	protected $meta_type = 'post';
25
-
26
-	/**
27
-	 * This only needs set if you are using a custom metadata type.
28
-	 *
29
-	 * @var string
30
-	 */
31
-	protected $object_id_field_for_meta = '';
32
-
33
-	/**
34
-	 * Data stored in meta keys, but not considered "meta" for an object.
35
-	 *
36
-	 * @since 1.0.19
37
-	 *
38
-	 * @var array
39
-	 */
40
-	protected $internal_meta_keys = array();
41
-
42
-	/**
43
-	 * Meta data which should exist in the DB, even if empty.
44
-	 *
45
-	 * @since 1.0.19
46
-	 *
47
-	 * @var array
48
-	 */
49
-	protected $must_exist_meta_keys = array();
50
-
51
-	/**
52
-	 * A map of meta keys to data props.
53
-	 *
54
-	 * @since 1.0.19
55
-	 *
56
-	 * @var array
57
-	 */
58
-	protected $meta_key_to_props = array();
59
-
60
-	/**
61
-	 * Returns an array of meta for an object.
62
-	 *
63
-	 * @since  1.0.19
64
-	 * @param  GetPaid_Data $object GetPaid_Data object.
65
-	 * @return array
66
-	 */
67
-	public function read_meta( &$object ) {
68
-		global $wpdb;
69
-		$db_info       = $this->get_db_info();
70
-		$raw_meta_data = $wpdb->get_results(
71
-			$wpdb->prepare(
72
-				"SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value
17
+    /**
18
+     * Meta type. This should match up with
19
+     * the types available at https://developer.wordpress.org/reference/functions/add_metadata/.
20
+     * WP defines 'post', 'user', 'comment', and 'term'.
21
+     *
22
+     * @var string
23
+     */
24
+    protected $meta_type = 'post';
25
+
26
+    /**
27
+     * This only needs set if you are using a custom metadata type.
28
+     *
29
+     * @var string
30
+     */
31
+    protected $object_id_field_for_meta = '';
32
+
33
+    /**
34
+     * Data stored in meta keys, but not considered "meta" for an object.
35
+     *
36
+     * @since 1.0.19
37
+     *
38
+     * @var array
39
+     */
40
+    protected $internal_meta_keys = array();
41
+
42
+    /**
43
+     * Meta data which should exist in the DB, even if empty.
44
+     *
45
+     * @since 1.0.19
46
+     *
47
+     * @var array
48
+     */
49
+    protected $must_exist_meta_keys = array();
50
+
51
+    /**
52
+     * A map of meta keys to data props.
53
+     *
54
+     * @since 1.0.19
55
+     *
56
+     * @var array
57
+     */
58
+    protected $meta_key_to_props = array();
59
+
60
+    /**
61
+     * Returns an array of meta for an object.
62
+     *
63
+     * @since  1.0.19
64
+     * @param  GetPaid_Data $object GetPaid_Data object.
65
+     * @return array
66
+     */
67
+    public function read_meta( &$object ) {
68
+        global $wpdb;
69
+        $db_info       = $this->get_db_info();
70
+        $raw_meta_data = $wpdb->get_results(
71
+            $wpdb->prepare(
72
+                "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value
73 73
 				FROM {$db_info['table']}
74 74
 				WHERE {$db_info['object_id_field']} = %d
75 75
 				ORDER BY {$db_info['meta_id_field']}",
76
-				$object->get_id()
77
-			)
78
-		);
79
-
80
-		$this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys );
81
-		$meta_data                = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) );
82
-		return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this );
83
-	}
84
-
85
-	/**
86
-	 * Deletes meta based on meta ID.
87
-	 *
88
-	 * @since  1.0.19
89
-	 * @param  GetPaid_Data  $object GetPaid_Data object.
90
-	 * @param  stdClass $meta (containing at least ->id).
91
-	 */
92
-	public function delete_meta( &$object, $meta ) {
93
-		delete_metadata_by_mid( $this->meta_type, $meta->id );
94
-	}
95
-
96
-	/**
97
-	 * Add new piece of meta.
98
-	 *
99
-	 * @since  1.0.19
100
-	 * @param  GetPaid_Data  $object GetPaid_Data object.
101
-	 * @param  stdClass $meta (containing ->key and ->value).
102
-	 * @return int meta ID
103
-	 */
104
-	public function add_meta( &$object, $meta ) {
105
-		return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false );
106
-	}
107
-
108
-	/**
109
-	 * Update meta.
110
-	 *
111
-	 * @since  1.0.19
112
-	 * @param  GetPaid_Data  $object GetPaid_Data object.
113
-	 * @param  stdClass $meta (containing ->id, ->key and ->value).
114
-	 */
115
-	public function update_meta( &$object, $meta ) {
116
-		update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key );
117
-	}
118
-
119
-	/**
120
-	 * Table structure is slightly different between meta types, this function will return what we need to know.
121
-	 *
122
-	 * @since  1.0.19
123
-	 * @return array Array elements: table, object_id_field, meta_id_field
124
-	 */
125
-	protected function get_db_info() {
126
-		global $wpdb;
127
-
128
-		$meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well.
129
-		$table         = $wpdb->prefix;
130
-
131
-		// If we are dealing with a type of metadata that is not a core type, the table should be prefixed.
132
-		if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) {
133
-			$table .= 'getpaid_';
134
-		}
135
-
136
-		$table          .= $this->meta_type . 'meta';
137
-		$object_id_field = $this->meta_type . '_id';
138
-
139
-		// Figure out our field names.
140
-		if ( 'user' === $this->meta_type ) {
141
-			$meta_id_field = 'umeta_id';
142
-			$table         = $wpdb->usermeta;
143
-		}
144
-
145
-		if ( ! empty( $this->object_id_field_for_meta ) ) {
146
-			$object_id_field = $this->object_id_field_for_meta;
147
-		}
148
-
149
-		return array(
150
-			'table'           => $table,
151
-			'object_id_field' => $object_id_field,
152
-			'meta_id_field'   => $meta_id_field,
153
-		);
154
-	}
155
-
156
-	/**
157
-	 * Internal meta keys we don't want exposed as part of meta_data. This is in
158
-	 * addition to all data props with _ prefix.
159
-	 *
160
-	 * @since 1.0.19
161
-	 *
162
-	 * @param string $key Prefix to be added to meta keys.
163
-	 * @return string
164
-	 */
165
-	protected function prefix_key( $key ) {
166
-		return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key;
167
-	}
168
-
169
-	/**
170
-	 * Callback to remove unwanted meta data.
171
-	 *
172
-	 * @param object $meta Meta object to check if it should be excluded or not.
173
-	 * @return bool
174
-	 */
175
-	protected function exclude_internal_meta_keys( $meta ) {
176
-		return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' );
177
-	}
178
-
179
-	/**
180
-	 * Gets a list of props and meta keys that need updated based on change state
181
-	 * or if they are present in the database or not.
182
-	 *
183
-	 * @param  GetPaid_Data $object         The GetPaid_Data object.
184
-	 * @param  array   $meta_key_to_props   A mapping of meta keys => prop names.
185
-	 * @param  string  $meta_type           The internal WP meta type (post, user, etc).
186
-	 * @return array                        A mapping of meta keys => prop names, filtered by ones that should be updated.
187
-	 */
188
-	protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) {
189
-		$props_to_update = array();
190
-		$changed_props   = $object->get_changes();
191
-
192
-		// Props should be updated if they are a part of the $changed array or don't exist yet.
193
-		foreach ( $meta_key_to_props as $meta_key => $prop ) {
194
-			if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) {
195
-				$props_to_update[ $meta_key ] = $prop;
196
-			}
197
-		}
198
-
199
-		return $props_to_update;
200
-	}
201
-
202
-	/**
203
-	 * Read object data.
204
-	 *
205
-	 * @param GetPaid_Data $object GetPaid_Data object.
206
-	 * @param WP_Post   $post_object Post object.
207
-	 * @since 1.0.19
208
-	 */
209
-	protected function read_object_data( &$object, $post_object ) {
210
-		$id    = $object->get_id();
211
-		$props = array();
212
-
213
-		foreach ( $this->meta_key_to_props as $meta_key => $prop ) {
214
-			$props[ $prop ] = get_post_meta( $id, $meta_key, true );
215
-		}
216
-
217
-		// Set object properties.
218
-		$object->set_props( $props );
219
-
220
-		// Gets extra data associated with the object if needed.
221
-		foreach ( $object->get_extra_data_keys() as $key ) {
222
-			$function = 'set_' . $key;
223
-			if ( is_callable( array( $object, $function ) ) ) {
224
-				$object->{$function}( get_post_meta( $object->get_id(), $key, true ) );
225
-			}
226
-		}
227
-	}
228
-
229
-	/**
230
-	 * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class.
231
-	 *
232
-	 * @param GetPaid_Data $object GetPaid_Data object.
233
-	 * @since 1.0.19
234
-	 */
235
-	protected function update_post_meta( &$object ) {
236
-
237
-		$updated_props   = array();
238
-		$props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props );
239
-		$object_type     = $object->get_object_type();
240
-
241
-		foreach ( $props_to_update as $meta_key => $prop ) {
242
-			$value = $object->{"get_$prop"}( 'edit' );
243
-			$value = is_string( $value ) ? wp_slash( $value ) : $value;
244
-
245
-			$updated = $this->update_or_delete_post_meta( $object, $meta_key, $value );
246
-
247
-			if ( $updated ) {
248
-				$updated_props[] = $prop;
249
-			}
250
-		}
251
-
252
-		do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props );
253
-	}
254
-
255
-	/**
256
-	 * Update meta data in, or delete it from, the database.
257
-	 *
258
-	 * Avoids storing meta when it's either an empty string or empty array or null.
259
-	 * Other empty values such as numeric 0 should still be stored.
260
-	 * Data-stores can force meta to exist using `must_exist_meta_keys`.
261
-	 *
262
-	 * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist.
263
-	 *
264
-	 * @param GetPaid_Data $object The GetPaid_Data object.
265
-	 * @param string  $meta_key Meta key to update.
266
-	 * @param mixed   $meta_value Value to save.
267
-	 *
268
-	 * @since 1.0.19 Added to prevent empty meta being stored unless required.
269
-	 *
270
-	 * @return bool True if updated/deleted.
271
-	 */
272
-	protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) {
273
-		if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) {
274
-			$updated = delete_post_meta( $object->get_id(), $meta_key );
275
-		} else {
276
-			$updated = update_post_meta( $object->get_id(), $meta_key, $meta_value );
277
-		}
278
-
279
-		return (bool) $updated;
280
-	}
281
-
282
-	/**
283
-	 * Return list of internal meta keys.
284
-	 *
285
-	 * @since 1.0.19
286
-	 * @return array
287
-	 */
288
-	public function get_internal_meta_keys() {
289
-		return $this->internal_meta_keys;
290
-	}
291
-
292
-	/**
293
-	 * Clear any caches.
294
-	 *
295
-	 * @param GetPaid_Data $object GetPaid_Data object.
296
-	 * @since 1.0.19
297
-	 */
298
-	protected function clear_caches( &$object ) {
299
-		clean_post_cache( $object->get_id() );
300
-	}
301
-
302
-	/**
303
-	 * Method to delete a data object from the database.
304
-	 *
305
-	 * @param GetPaid_Data $object GetPaid_Data object.
306
-	 * @param array    $args Array of args to pass to the delete method.
307
-	 *
308
-	 * @return void
309
-	 */
310
-	public function delete( &$object, $args = array() ) {
311
-		$id          = $object->get_id();
312
-		$object_type = $object->get_object_type();
313
-		$args        = wp_parse_args(
314
-			$args,
315
-			array(
316
-				'force_delete' => false,
317
-			)
318
-		);
319
-
320
-		if ( ! $id ) {
321
-			return;
322
-		}
323
-
324
-		if ( $args['force_delete'] ) {
325
-			wp_delete_post( $id, true );
326
-			$object->set_id( 0 );
327
-			do_action( "getpaid_delete_$object_type", $id );
328
-		} else {
329
-			wp_trash_post( $id );
330
-			$object->set_status( 'trash' );
331
-			do_action( "getpaid_trash_$object_type", $id );
332
-		}
333
-	}
334
-
335
-	/**
336
-	 * Get the status to save to the post object.
337
-	 *
338
-	 *
339
-	 * @since 1.0.19
340
-	 * @param  GetPaid_Data $object GetPaid_Data object.
341
-	 * @return string
342
-	 */
343
-	protected function get_post_status( $object ) {
344
-		$object_status = $object->get_status( 'edit' );
345
-		$object_type   = $object->get_object_type();
346
-
347
-		if ( ! $object_status ) {
348
-			$object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' );
349
-		}
350
-
351
-		return $object_status;
352
-	}
76
+                $object->get_id()
77
+            )
78
+        );
79
+
80
+        $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys );
81
+        $meta_data                = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) );
82
+        return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this );
83
+    }
84
+
85
+    /**
86
+     * Deletes meta based on meta ID.
87
+     *
88
+     * @since  1.0.19
89
+     * @param  GetPaid_Data  $object GetPaid_Data object.
90
+     * @param  stdClass $meta (containing at least ->id).
91
+     */
92
+    public function delete_meta( &$object, $meta ) {
93
+        delete_metadata_by_mid( $this->meta_type, $meta->id );
94
+    }
95
+
96
+    /**
97
+     * Add new piece of meta.
98
+     *
99
+     * @since  1.0.19
100
+     * @param  GetPaid_Data  $object GetPaid_Data object.
101
+     * @param  stdClass $meta (containing ->key and ->value).
102
+     * @return int meta ID
103
+     */
104
+    public function add_meta( &$object, $meta ) {
105
+        return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false );
106
+    }
107
+
108
+    /**
109
+     * Update meta.
110
+     *
111
+     * @since  1.0.19
112
+     * @param  GetPaid_Data  $object GetPaid_Data object.
113
+     * @param  stdClass $meta (containing ->id, ->key and ->value).
114
+     */
115
+    public function update_meta( &$object, $meta ) {
116
+        update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key );
117
+    }
118
+
119
+    /**
120
+     * Table structure is slightly different between meta types, this function will return what we need to know.
121
+     *
122
+     * @since  1.0.19
123
+     * @return array Array elements: table, object_id_field, meta_id_field
124
+     */
125
+    protected function get_db_info() {
126
+        global $wpdb;
127
+
128
+        $meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well.
129
+        $table         = $wpdb->prefix;
130
+
131
+        // If we are dealing with a type of metadata that is not a core type, the table should be prefixed.
132
+        if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) {
133
+            $table .= 'getpaid_';
134
+        }
135
+
136
+        $table          .= $this->meta_type . 'meta';
137
+        $object_id_field = $this->meta_type . '_id';
138
+
139
+        // Figure out our field names.
140
+        if ( 'user' === $this->meta_type ) {
141
+            $meta_id_field = 'umeta_id';
142
+            $table         = $wpdb->usermeta;
143
+        }
144
+
145
+        if ( ! empty( $this->object_id_field_for_meta ) ) {
146
+            $object_id_field = $this->object_id_field_for_meta;
147
+        }
148
+
149
+        return array(
150
+            'table'           => $table,
151
+            'object_id_field' => $object_id_field,
152
+            'meta_id_field'   => $meta_id_field,
153
+        );
154
+    }
155
+
156
+    /**
157
+     * Internal meta keys we don't want exposed as part of meta_data. This is in
158
+     * addition to all data props with _ prefix.
159
+     *
160
+     * @since 1.0.19
161
+     *
162
+     * @param string $key Prefix to be added to meta keys.
163
+     * @return string
164
+     */
165
+    protected function prefix_key( $key ) {
166
+        return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key;
167
+    }
168
+
169
+    /**
170
+     * Callback to remove unwanted meta data.
171
+     *
172
+     * @param object $meta Meta object to check if it should be excluded or not.
173
+     * @return bool
174
+     */
175
+    protected function exclude_internal_meta_keys( $meta ) {
176
+        return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' );
177
+    }
178
+
179
+    /**
180
+     * Gets a list of props and meta keys that need updated based on change state
181
+     * or if they are present in the database or not.
182
+     *
183
+     * @param  GetPaid_Data $object         The GetPaid_Data object.
184
+     * @param  array   $meta_key_to_props   A mapping of meta keys => prop names.
185
+     * @param  string  $meta_type           The internal WP meta type (post, user, etc).
186
+     * @return array                        A mapping of meta keys => prop names, filtered by ones that should be updated.
187
+     */
188
+    protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) {
189
+        $props_to_update = array();
190
+        $changed_props   = $object->get_changes();
191
+
192
+        // Props should be updated if they are a part of the $changed array or don't exist yet.
193
+        foreach ( $meta_key_to_props as $meta_key => $prop ) {
194
+            if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) {
195
+                $props_to_update[ $meta_key ] = $prop;
196
+            }
197
+        }
198
+
199
+        return $props_to_update;
200
+    }
201
+
202
+    /**
203
+     * Read object data.
204
+     *
205
+     * @param GetPaid_Data $object GetPaid_Data object.
206
+     * @param WP_Post   $post_object Post object.
207
+     * @since 1.0.19
208
+     */
209
+    protected function read_object_data( &$object, $post_object ) {
210
+        $id    = $object->get_id();
211
+        $props = array();
212
+
213
+        foreach ( $this->meta_key_to_props as $meta_key => $prop ) {
214
+            $props[ $prop ] = get_post_meta( $id, $meta_key, true );
215
+        }
216
+
217
+        // Set object properties.
218
+        $object->set_props( $props );
219
+
220
+        // Gets extra data associated with the object if needed.
221
+        foreach ( $object->get_extra_data_keys() as $key ) {
222
+            $function = 'set_' . $key;
223
+            if ( is_callable( array( $object, $function ) ) ) {
224
+                $object->{$function}( get_post_meta( $object->get_id(), $key, true ) );
225
+            }
226
+        }
227
+    }
228
+
229
+    /**
230
+     * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class.
231
+     *
232
+     * @param GetPaid_Data $object GetPaid_Data object.
233
+     * @since 1.0.19
234
+     */
235
+    protected function update_post_meta( &$object ) {
236
+
237
+        $updated_props   = array();
238
+        $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props );
239
+        $object_type     = $object->get_object_type();
240
+
241
+        foreach ( $props_to_update as $meta_key => $prop ) {
242
+            $value = $object->{"get_$prop"}( 'edit' );
243
+            $value = is_string( $value ) ? wp_slash( $value ) : $value;
244
+
245
+            $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value );
246
+
247
+            if ( $updated ) {
248
+                $updated_props[] = $prop;
249
+            }
250
+        }
251
+
252
+        do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props );
253
+    }
254
+
255
+    /**
256
+     * Update meta data in, or delete it from, the database.
257
+     *
258
+     * Avoids storing meta when it's either an empty string or empty array or null.
259
+     * Other empty values such as numeric 0 should still be stored.
260
+     * Data-stores can force meta to exist using `must_exist_meta_keys`.
261
+     *
262
+     * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist.
263
+     *
264
+     * @param GetPaid_Data $object The GetPaid_Data object.
265
+     * @param string  $meta_key Meta key to update.
266
+     * @param mixed   $meta_value Value to save.
267
+     *
268
+     * @since 1.0.19 Added to prevent empty meta being stored unless required.
269
+     *
270
+     * @return bool True if updated/deleted.
271
+     */
272
+    protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) {
273
+        if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) {
274
+            $updated = delete_post_meta( $object->get_id(), $meta_key );
275
+        } else {
276
+            $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value );
277
+        }
278
+
279
+        return (bool) $updated;
280
+    }
281
+
282
+    /**
283
+     * Return list of internal meta keys.
284
+     *
285
+     * @since 1.0.19
286
+     * @return array
287
+     */
288
+    public function get_internal_meta_keys() {
289
+        return $this->internal_meta_keys;
290
+    }
291
+
292
+    /**
293
+     * Clear any caches.
294
+     *
295
+     * @param GetPaid_Data $object GetPaid_Data object.
296
+     * @since 1.0.19
297
+     */
298
+    protected function clear_caches( &$object ) {
299
+        clean_post_cache( $object->get_id() );
300
+    }
301
+
302
+    /**
303
+     * Method to delete a data object from the database.
304
+     *
305
+     * @param GetPaid_Data $object GetPaid_Data object.
306
+     * @param array    $args Array of args to pass to the delete method.
307
+     *
308
+     * @return void
309
+     */
310
+    public function delete( &$object, $args = array() ) {
311
+        $id          = $object->get_id();
312
+        $object_type = $object->get_object_type();
313
+        $args        = wp_parse_args(
314
+            $args,
315
+            array(
316
+                'force_delete' => false,
317
+            )
318
+        );
319
+
320
+        if ( ! $id ) {
321
+            return;
322
+        }
323
+
324
+        if ( $args['force_delete'] ) {
325
+            wp_delete_post( $id, true );
326
+            $object->set_id( 0 );
327
+            do_action( "getpaid_delete_$object_type", $id );
328
+        } else {
329
+            wp_trash_post( $id );
330
+            $object->set_status( 'trash' );
331
+            do_action( "getpaid_trash_$object_type", $id );
332
+        }
333
+    }
334
+
335
+    /**
336
+     * Get the status to save to the post object.
337
+     *
338
+     *
339
+     * @since 1.0.19
340
+     * @param  GetPaid_Data $object GetPaid_Data object.
341
+     * @return string
342
+     */
343
+    protected function get_post_status( $object ) {
344
+        $object_status = $object->get_status( 'edit' );
345
+        $object_type   = $object->get_object_type();
346
+
347
+        if ( ! $object_status ) {
348
+            $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' );
349
+        }
350
+
351
+        return $object_status;
352
+    }
353 353
 
354 354
 }
Please login to merge, or discard this patch.
includes/data-stores/class-getpaid-data-store.php 1 patch
Indentation   +178 added lines, -178 removed lines patch added patch discarded remove patch
@@ -11,183 +11,183 @@
 block discarded – undo
11 11
  */
12 12
 class GetPaid_Data_Store {
13 13
 
14
-	/**
15
-	 * Contains an instance of the data store class that we are working with.
16
-	 *
17
-	 * @var GetPaid_Data_Store
18
-	 */
19
-	private $instance = null;
20
-
21
-	/**
22
-	 * Contains an array of default supported data stores.
23
-	 * Format of object name => class name.
24
-	 * Example: 'item' => 'GetPaid_Item_Data_Store'
25
-	 * You can also pass something like item-<type> for item stores and
26
-	 * that type will be used first when available, if a store is requested like
27
-	 * this and doesn't exist, then the store would fall back to 'item'.
28
-	 * Ran through `getpaid_data_stores`.
29
-	 *
30
-	 * @var array
31
-	 */
32
-	private $stores = array(
33
-		'item'         => 'GetPaid_Item_Data_Store',
34
-		'payment_form' => 'GetPaid_Payment_Form_Data_Store',
35
-	);
36
-
37
-	/**
38
-	 * Contains the name of the current data store's class name.
39
-	 *
40
-	 * @var string
41
-	 */
42
-	private $current_class_name = '';
43
-
44
-	/**
45
-	 * The object type this store works with.
46
-	 *
47
-	 * @var string
48
-	 */
49
-	private $object_type = '';
50
-
51
-	/**
52
-	 * Tells GetPaid_Data_Store which object
53
-	 * store we want to work with.
54
-	 *
55
-	 * @param string $object_type Name of object.
56
-	 */
57
-	public function __construct( $object_type ) {
58
-		$this->object_type = $object_type;
59
-		$this->stores      = apply_filters( 'getpaid_data_stores', $this->stores );
60
-
61
-		// If this object type can't be found, check to see if we can load one
62
-		// level up (so if item-type isn't found, we try item).
63
-		if ( ! array_key_exists( $object_type, $this->stores ) ) {
64
-			$pieces      = explode( '-', $object_type );
65
-			$object_type = $pieces[0];
66
-		}
67
-
68
-		if ( array_key_exists( $object_type, $this->stores ) ) {
69
-			$store = apply_filters( 'getpaid_' . $object_type . '_data_store', $this->stores[ $object_type ] );
70
-			if ( is_object( $store ) ) {
71
-				$this->current_class_name = get_class( $store );
72
-				$this->instance           = $store;
73
-			} else {
74
-				if ( ! class_exists( $store ) ) {
75
-					throw new Exception( __( 'Data store class does not exist.', 'invoicing' ) );
76
-				}
77
-				$this->current_class_name = $store;
78
-				$this->instance           = new $store();
79
-			}
80
-		} else {
81
-			throw new Exception( __( 'Invalid data store.', 'invoicing' ) );
82
-		}
83
-	}
84
-
85
-	/**
86
-	 * Only store the object type to avoid serializing the data store instance.
87
-	 *
88
-	 * @return array
89
-	 */
90
-	public function __sleep() {
91
-		return array( 'object_type' );
92
-	}
93
-
94
-	/**
95
-	 * Re-run the constructor with the object type.
96
-	 *
97
-	 * @throws Exception When validation fails.
98
-	 */
99
-	public function __wakeup() {
100
-		$this->__construct( $this->object_type );
101
-	}
102
-
103
-	/**
104
-	 * Loads a data store.
105
-	 *
106
-	 * @param string $object_type Name of object.
107
-	 *
108
-	 * @since 1.0.19
109
-	 * @throws Exception When validation fails.
110
-	 * @return GetPaid_Data_Store
111
-	 */
112
-	public static function load( $object_type ) {
113
-		return new GetPaid_Data_Store( $object_type );
114
-	}
115
-
116
-	/**
117
-	 * Returns the class name of the current data store.
118
-	 *
119
-	 * @since 1.0.19
120
-	 * @return string
121
-	 */
122
-	public function get_current_class_name() {
123
-		return $this->current_class_name;
124
-	}
125
-
126
-	/**
127
-	 * Returns the object type of the current data store.
128
-	 *
129
-	 * @since 1.0.19
130
-	 * @return string
131
-	 */
132
-	public function get_object_type() {
133
-		return $this->object_type;
134
-	}
135
-
136
-	/**
137
-	 * Reads an object from the data store.
138
-	 *
139
-	 * @since 1.0.19
140
-	 * @param GetPaid_Data $data GetPaid data instance.
141
-	 */
142
-	public function read( &$data ) {
143
-		$this->instance->read( $data );
144
-	}
145
-
146
-	/**
147
-	 * Create an object in the data store.
148
-	 *
149
-	 * @since 1.0.19
150
-	 * @param GetPaid_Data $data GetPaid data instance.
151
-	 */
152
-	public function create( &$data ) {
153
-		$this->instance->create( $data );
154
-	}
155
-
156
-	/**
157
-	 * Update an object in the data store.
158
-	 *
159
-	 * @since 1.0.19
160
-	 * @param GetPaid_Data $data GetPaid data instance.
161
-	 */
162
-	public function update( &$data ) {
163
-		$this->instance->update( $data );
164
-	}
165
-
166
-	/**
167
-	 * Delete an object from the data store.
168
-	 *
169
-	 * @since 1.0.19
170
-	 * @param GetPaid_Data $data GetPaid data instance.
171
-	 * @param array   $args Array of args to pass to the delete method.
172
-	 */
173
-	public function delete( &$data, $args = array() ) {
174
-		$this->instance->delete( $data, $args );
175
-	}
176
-
177
-	/**
178
-	 * Data stores can define additional function. This passes
179
-	 * through to the instance if that function exists.
180
-	 *
181
-	 * @since 1.0.19
182
-	 * @param string $method     Method.
183
-	 * @return mixed
184
-	 */
185
-	public function __call( $method, $parameters ) {
186
-		if ( is_callable( array( $this->instance, $method ) ) ) {
187
-			$object     = array_shift( $parameters );
188
-			$parameters = array_merge( array( &$object ), $parameters );
189
-			return call_user_func_array( array( $this->instance, $method ), $parameters );
190
-		}
191
-	}
14
+    /**
15
+     * Contains an instance of the data store class that we are working with.
16
+     *
17
+     * @var GetPaid_Data_Store
18
+     */
19
+    private $instance = null;
20
+
21
+    /**
22
+     * Contains an array of default supported data stores.
23
+     * Format of object name => class name.
24
+     * Example: 'item' => 'GetPaid_Item_Data_Store'
25
+     * You can also pass something like item-<type> for item stores and
26
+     * that type will be used first when available, if a store is requested like
27
+     * this and doesn't exist, then the store would fall back to 'item'.
28
+     * Ran through `getpaid_data_stores`.
29
+     *
30
+     * @var array
31
+     */
32
+    private $stores = array(
33
+        'item'         => 'GetPaid_Item_Data_Store',
34
+        'payment_form' => 'GetPaid_Payment_Form_Data_Store',
35
+    );
36
+
37
+    /**
38
+     * Contains the name of the current data store's class name.
39
+     *
40
+     * @var string
41
+     */
42
+    private $current_class_name = '';
43
+
44
+    /**
45
+     * The object type this store works with.
46
+     *
47
+     * @var string
48
+     */
49
+    private $object_type = '';
50
+
51
+    /**
52
+     * Tells GetPaid_Data_Store which object
53
+     * store we want to work with.
54
+     *
55
+     * @param string $object_type Name of object.
56
+     */
57
+    public function __construct( $object_type ) {
58
+        $this->object_type = $object_type;
59
+        $this->stores      = apply_filters( 'getpaid_data_stores', $this->stores );
60
+
61
+        // If this object type can't be found, check to see if we can load one
62
+        // level up (so if item-type isn't found, we try item).
63
+        if ( ! array_key_exists( $object_type, $this->stores ) ) {
64
+            $pieces      = explode( '-', $object_type );
65
+            $object_type = $pieces[0];
66
+        }
67
+
68
+        if ( array_key_exists( $object_type, $this->stores ) ) {
69
+            $store = apply_filters( 'getpaid_' . $object_type . '_data_store', $this->stores[ $object_type ] );
70
+            if ( is_object( $store ) ) {
71
+                $this->current_class_name = get_class( $store );
72
+                $this->instance           = $store;
73
+            } else {
74
+                if ( ! class_exists( $store ) ) {
75
+                    throw new Exception( __( 'Data store class does not exist.', 'invoicing' ) );
76
+                }
77
+                $this->current_class_name = $store;
78
+                $this->instance           = new $store();
79
+            }
80
+        } else {
81
+            throw new Exception( __( 'Invalid data store.', 'invoicing' ) );
82
+        }
83
+    }
84
+
85
+    /**
86
+     * Only store the object type to avoid serializing the data store instance.
87
+     *
88
+     * @return array
89
+     */
90
+    public function __sleep() {
91
+        return array( 'object_type' );
92
+    }
93
+
94
+    /**
95
+     * Re-run the constructor with the object type.
96
+     *
97
+     * @throws Exception When validation fails.
98
+     */
99
+    public function __wakeup() {
100
+        $this->__construct( $this->object_type );
101
+    }
102
+
103
+    /**
104
+     * Loads a data store.
105
+     *
106
+     * @param string $object_type Name of object.
107
+     *
108
+     * @since 1.0.19
109
+     * @throws Exception When validation fails.
110
+     * @return GetPaid_Data_Store
111
+     */
112
+    public static function load( $object_type ) {
113
+        return new GetPaid_Data_Store( $object_type );
114
+    }
115
+
116
+    /**
117
+     * Returns the class name of the current data store.
118
+     *
119
+     * @since 1.0.19
120
+     * @return string
121
+     */
122
+    public function get_current_class_name() {
123
+        return $this->current_class_name;
124
+    }
125
+
126
+    /**
127
+     * Returns the object type of the current data store.
128
+     *
129
+     * @since 1.0.19
130
+     * @return string
131
+     */
132
+    public function get_object_type() {
133
+        return $this->object_type;
134
+    }
135
+
136
+    /**
137
+     * Reads an object from the data store.
138
+     *
139
+     * @since 1.0.19
140
+     * @param GetPaid_Data $data GetPaid data instance.
141
+     */
142
+    public function read( &$data ) {
143
+        $this->instance->read( $data );
144
+    }
145
+
146
+    /**
147
+     * Create an object in the data store.
148
+     *
149
+     * @since 1.0.19
150
+     * @param GetPaid_Data $data GetPaid data instance.
151
+     */
152
+    public function create( &$data ) {
153
+        $this->instance->create( $data );
154
+    }
155
+
156
+    /**
157
+     * Update an object in the data store.
158
+     *
159
+     * @since 1.0.19
160
+     * @param GetPaid_Data $data GetPaid data instance.
161
+     */
162
+    public function update( &$data ) {
163
+        $this->instance->update( $data );
164
+    }
165
+
166
+    /**
167
+     * Delete an object from the data store.
168
+     *
169
+     * @since 1.0.19
170
+     * @param GetPaid_Data $data GetPaid data instance.
171
+     * @param array   $args Array of args to pass to the delete method.
172
+     */
173
+    public function delete( &$data, $args = array() ) {
174
+        $this->instance->delete( $data, $args );
175
+    }
176
+
177
+    /**
178
+     * Data stores can define additional function. This passes
179
+     * through to the instance if that function exists.
180
+     *
181
+     * @since 1.0.19
182
+     * @param string $method     Method.
183
+     * @return mixed
184
+     */
185
+    public function __call( $method, $parameters ) {
186
+        if ( is_callable( array( $this->instance, $method ) ) ) {
187
+            $object     = array_shift( $parameters );
188
+            $parameters = array_merge( array( &$object ), $parameters );
189
+            return call_user_func_array( array( $this->instance, $method ), $parameters );
190
+        }
191
+    }
192 192
 
193 193
 }
Please login to merge, or discard this patch.
includes/class-wpinv.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -161,10 +161,10 @@  discard block
 block discarded – undo
161 161
         require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-error-functions.php' );
162 162
 
163 163
         // Register autoloader.
164
-		try {
165
-			spl_autoload_register( array( $this, 'autoload' ), true );
166
-		} catch ( Exception $e ) {
167
-			wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
164
+        try {
165
+            spl_autoload_register( array( $this, 'autoload' ), true );
166
+        } catch ( Exception $e ) {
167
+            wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
168 168
         }
169 169
 
170 170
         require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-post-types.php' );
@@ -188,11 +188,11 @@  discard block
 block discarded – undo
188 188
         require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' );
189 189
         require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' );
190 190
         require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' );
191
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' );
192
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' );
193
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' );
194
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' );
195
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' );
191
+        require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' );
192
+        require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' );
193
+        require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' );
194
+        require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' );
195
+        require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' );
196 196
         require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' );
197 197
         require_once( WPINV_PLUGIN_DIR . 'widgets/getpaid.php' );
198 198
         require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php' );
@@ -254,47 +254,47 @@  discard block
 block discarded – undo
254 254
     }
255 255
 
256 256
     /**
257
-	 * Class autoloader
258
-	 *
259
-	 * @param       string $class_name The name of the class to load.
260
-	 * @access      public
261
-	 * @since       1.0.19
262
-	 * @return      void
263
-	 */
264
-	public function autoload( $class_name ) {
265
-
266
-		// Normalize the class name...
267
-		$class_name  = strtolower( $class_name );
268
-
269
-		// ... and make sure it is our class.
270
-		if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
271
-			return;
272
-		}
273
-
274
-		// Next, prepare the file name from the class.
275
-		$file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
276
-
277
-		// And an array of possible locations in order of importance.
278
-		$locations = array(
279
-			'includes',
280
-			'includes/data-stores',
257
+     * Class autoloader
258
+     *
259
+     * @param       string $class_name The name of the class to load.
260
+     * @access      public
261
+     * @since       1.0.19
262
+     * @return      void
263
+     */
264
+    public function autoload( $class_name ) {
265
+
266
+        // Normalize the class name...
267
+        $class_name  = strtolower( $class_name );
268
+
269
+        // ... and make sure it is our class.
270
+        if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
271
+            return;
272
+        }
273
+
274
+        // Next, prepare the file name from the class.
275
+        $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
276
+
277
+        // And an array of possible locations in order of importance.
278
+        $locations = array(
279
+            'includes',
280
+            'includes/data-stores',
281 281
             'includes/admin',
282 282
             'includes/admin/meta-boxes'
283
-		);
283
+        );
284 284
 
285
-		// Base path of the classes.
286
-		$plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
285
+        // Base path of the classes.
286
+        $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
287 287
 
288
-		foreach ( $locations as $location ) {
288
+        foreach ( $locations as $location ) {
289 289
 
290
-			if ( file_exists( "$plugin_path/$location/$file_name" ) ) {
291
-				include "$plugin_path/$location/$file_name";
292
-				break;
293
-			}
290
+            if ( file_exists( "$plugin_path/$location/$file_name" ) ) {
291
+                include "$plugin_path/$location/$file_name";
292
+                break;
293
+            }
294 294
 
295
-		}
295
+        }
296 296
 
297
-	}
297
+    }
298 298
 
299 299
     public function init() {
300 300
     }
@@ -384,7 +384,7 @@  discard block
 block discarded – undo
384 384
             wp_register_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui' . $suffix . '.css', array(), '1.8.16' );
385 385
             wp_enqueue_style( 'jquery-ui-css' );
386 386
             wp_deregister_style( 'yoast-seo-select2' );
387
-	        wp_deregister_style( 'yoast-seo-monorepo' );
387
+            wp_deregister_style( 'yoast-seo-monorepo' );
388 388
         }
389 389
 
390 390
         wp_register_style( 'wpinv_meta_box_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION );
@@ -398,7 +398,7 @@  discard block
 block discarded – undo
398 398
         if ( $page == 'wpinv-subscriptions' ) {
399 399
             wp_enqueue_script( 'jquery-ui-datepicker' );
400 400
             wp_deregister_style( 'yoast-seo-select2' );
401
-	        wp_deregister_style( 'yoast-seo-monorepo' );
401
+            wp_deregister_style( 'yoast-seo-monorepo' );
402 402
         }
403 403
         
404 404
         if ( $enqueue_datepicker = apply_filters( 'wpinv_admin_enqueue_jquery_ui_datepicker', $enqueue ) ) {
@@ -564,19 +564,19 @@  discard block
 block discarded – undo
564 564
         require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' );
565 565
     }
566 566
 
567
-	/**
568
-	 * Register widgets
569
-	 *
570
-	 */
571
-	public function register_widgets() {
572
-		register_widget( "WPInv_Checkout_Widget" );
573
-		register_widget( "WPInv_History_Widget" );
574
-		register_widget( "WPInv_Receipt_Widget" );
575
-		register_widget( "WPInv_Subscriptions_Widget" );
576
-		register_widget( "WPInv_Buy_Item_Widget" );
567
+    /**
568
+     * Register widgets
569
+     *
570
+     */
571
+    public function register_widgets() {
572
+        register_widget( "WPInv_Checkout_Widget" );
573
+        register_widget( "WPInv_History_Widget" );
574
+        register_widget( "WPInv_Receipt_Widget" );
575
+        register_widget( "WPInv_Subscriptions_Widget" );
576
+        register_widget( "WPInv_Buy_Item_Widget" );
577 577
         register_widget( "WPInv_Messages_Widget" );
578 578
         register_widget( 'WPInv_GetPaid_Widget' );
579
-	}
579
+    }
580 580
     
581 581
     /**
582 582
      * Remove our pages from yoast sitemaps.
Please login to merge, or discard this patch.
includes/admin/meta-boxes/class-getpaid-meta-box-payment-form-info.php 1 patch
Indentation   +5 added lines, -5 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_Payment_Form_Info {
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 form.
Please login to merge, or discard this patch.
includes/wpinv-template-functions.php 1 patch
Indentation   +168 added lines, -168 removed lines patch added patch discarded remove patch
@@ -103,29 +103,29 @@  discard block
 block discarded – undo
103 103
 
104 104
 function wpinv_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
105 105
     if ( ! empty( $args ) && is_array( $args ) ) {
106
-		extract( $args );
107
-	}
106
+        extract( $args );
107
+    }
108 108
 
109
-	$located = wpinv_locate_template( $template_name, $template_path, $default_path );
110
-	// Allow 3rd party plugin filter template file from their plugin.
111
-	$located = apply_filters( 'wpinv_get_template', $located, $template_name, $args, $template_path, $default_path );
109
+    $located = wpinv_locate_template( $template_name, $template_path, $default_path );
110
+    // Allow 3rd party plugin filter template file from their plugin.
111
+    $located = apply_filters( 'wpinv_get_template', $located, $template_name, $args, $template_path, $default_path );
112 112
 
113
-	if ( ! file_exists( $located ) ) {
113
+    if ( ! file_exists( $located ) ) {
114 114
         _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
115
-		return;
116
-	}
115
+        return;
116
+    }
117 117
 
118
-	do_action( 'wpinv_before_template_part', $template_name, $template_path, $located, $args );
118
+    do_action( 'wpinv_before_template_part', $template_name, $template_path, $located, $args );
119 119
 
120
-	include( $located );
120
+    include( $located );
121 121
 
122
-	do_action( 'wpinv_after_template_part', $template_name, $template_path, $located, $args );
122
+    do_action( 'wpinv_after_template_part', $template_name, $template_path, $located, $args );
123 123
 }
124 124
 
125 125
 function wpinv_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
126
-	ob_start();
127
-	wpinv_get_template( $template_name, $args, $template_path, $default_path );
128
-	return ob_get_clean();
126
+    ob_start();
127
+    wpinv_get_template( $template_name, $args, $template_path, $default_path );
128
+    return ob_get_clean();
129 129
 }
130 130
 
131 131
 function wpinv_locate_template( $template_name, $template_path = '', $default_path = '' ) {
@@ -155,126 +155,126 @@  discard block
 block discarded – undo
155 155
 }
156 156
 
157 157
 function wpinv_get_template_part( $slug, $name = null, $load = true ) {
158
-	do_action( 'get_template_part_' . $slug, $slug, $name );
158
+    do_action( 'get_template_part_' . $slug, $slug, $name );
159 159
 
160
-	// Setup possible parts
161
-	$templates = array();
162
-	if ( isset( $name ) )
163
-		$templates[] = $slug . '-' . $name . '.php';
164
-	$templates[] = $slug . '.php';
160
+    // Setup possible parts
161
+    $templates = array();
162
+    if ( isset( $name ) )
163
+        $templates[] = $slug . '-' . $name . '.php';
164
+    $templates[] = $slug . '.php';
165 165
 
166
-	// Allow template parts to be filtered
167
-	$templates = apply_filters( 'wpinv_get_template_part', $templates, $slug, $name );
166
+    // Allow template parts to be filtered
167
+    $templates = apply_filters( 'wpinv_get_template_part', $templates, $slug, $name );
168 168
 
169
-	// Return the part that is found
170
-	return wpinv_locate_tmpl( $templates, $load, false );
169
+    // Return the part that is found
170
+    return wpinv_locate_tmpl( $templates, $load, false );
171 171
 }
172 172
 
173 173
 function wpinv_locate_tmpl( $template_names, $load = false, $require_once = true ) {
174
-	// No file found yet
175
-	$located = false;
174
+    // No file found yet
175
+    $located = false;
176 176
 
177
-	// Try to find a template file
178
-	foreach ( (array)$template_names as $template_name ) {
177
+    // Try to find a template file
178
+    foreach ( (array)$template_names as $template_name ) {
179 179
 
180
-		// Continue if template is empty
181
-		if ( empty( $template_name ) )
182
-			continue;
180
+        // Continue if template is empty
181
+        if ( empty( $template_name ) )
182
+            continue;
183 183
 
184
-		// Trim off any slashes from the template name
185
-		$template_name = ltrim( $template_name, '/' );
184
+        // Trim off any slashes from the template name
185
+        $template_name = ltrim( $template_name, '/' );
186 186
 
187
-		// try locating this template file by looping through the template paths
188
-		foreach( wpinv_get_theme_template_paths() as $template_path ) {
187
+        // try locating this template file by looping through the template paths
188
+        foreach( wpinv_get_theme_template_paths() as $template_path ) {
189 189
 
190
-			if( file_exists( $template_path . $template_name ) ) {
191
-				$located = $template_path . $template_name;
192
-				break;
193
-			}
194
-		}
190
+            if( file_exists( $template_path . $template_name ) ) {
191
+                $located = $template_path . $template_name;
192
+                break;
193
+            }
194
+        }
195 195
 
196
-		if( !empty( $located ) ) {
197
-			break;
198
-		}
199
-	}
196
+        if( !empty( $located ) ) {
197
+            break;
198
+        }
199
+    }
200 200
 
201
-	if ( ( true == $load ) && ! empty( $located ) )
202
-		load_template( $located, $require_once );
201
+    if ( ( true == $load ) && ! empty( $located ) )
202
+        load_template( $located, $require_once );
203 203
 
204
-	return $located;
204
+    return $located;
205 205
 }
206 206
 
207 207
 function wpinv_get_theme_template_paths() {
208
-	$template_dir = wpinv_get_theme_template_dir_name();
208
+    $template_dir = wpinv_get_theme_template_dir_name();
209 209
 
210
-	$file_paths = array(
211
-		1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
212
-		10 => trailingslashit( get_template_directory() ) . $template_dir,
213
-		100 => wpinv_get_templates_dir()
214
-	);
210
+    $file_paths = array(
211
+        1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
212
+        10 => trailingslashit( get_template_directory() ) . $template_dir,
213
+        100 => wpinv_get_templates_dir()
214
+    );
215 215
 
216
-	$file_paths = apply_filters( 'wpinv_template_paths', $file_paths );
216
+    $file_paths = apply_filters( 'wpinv_template_paths', $file_paths );
217 217
 
218
-	// sort the file paths based on priority
219
-	ksort( $file_paths, SORT_NUMERIC );
218
+    // sort the file paths based on priority
219
+    ksort( $file_paths, SORT_NUMERIC );
220 220
 
221
-	return array_map( 'trailingslashit', $file_paths );
221
+    return array_map( 'trailingslashit', $file_paths );
222 222
 }
223 223
 
224 224
 function wpinv_get_theme_template_dir_name() {
225
-	return trailingslashit( apply_filters( 'wpinv_templates_dir', 'invoicing' ) );
225
+    return trailingslashit( apply_filters( 'wpinv_templates_dir', 'invoicing' ) );
226 226
 }
227 227
 
228 228
 function wpinv_checkout_meta_tags() {
229 229
 
230
-	$pages   = array();
231
-	$pages[] = wpinv_get_option( 'success_page' );
232
-	$pages[] = wpinv_get_option( 'failure_page' );
233
-	$pages[] = wpinv_get_option( 'invoice_history_page' );
234
-	$pages[] = wpinv_get_option( 'invoice_subscription_page' );
230
+    $pages   = array();
231
+    $pages[] = wpinv_get_option( 'success_page' );
232
+    $pages[] = wpinv_get_option( 'failure_page' );
233
+    $pages[] = wpinv_get_option( 'invoice_history_page' );
234
+    $pages[] = wpinv_get_option( 'invoice_subscription_page' );
235 235
 
236
-	if( !wpinv_is_checkout() && !is_page( $pages ) ) {
237
-		return;
238
-	}
236
+    if( !wpinv_is_checkout() && !is_page( $pages ) ) {
237
+        return;
238
+    }
239 239
 
240
-	echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
240
+    echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
241 241
 }
242 242
 add_action( 'wp_head', 'wpinv_checkout_meta_tags' );
243 243
 
244 244
 function wpinv_add_body_classes( $class ) {
245
-	$classes = (array)$class;
245
+    $classes = (array)$class;
246 246
 
247
-	if( wpinv_is_checkout() ) {
248
-		$classes[] = 'wpinv-checkout';
249
-		$classes[] = 'wpinv-page';
250
-	}
247
+    if( wpinv_is_checkout() ) {
248
+        $classes[] = 'wpinv-checkout';
249
+        $classes[] = 'wpinv-page';
250
+    }
251 251
 
252
-	if( wpinv_is_success_page() ) {
253
-		$classes[] = 'wpinv-success';
254
-		$classes[] = 'wpinv-page';
255
-	}
252
+    if( wpinv_is_success_page() ) {
253
+        $classes[] = 'wpinv-success';
254
+        $classes[] = 'wpinv-page';
255
+    }
256 256
 
257
-	if( wpinv_is_failed_transaction_page() ) {
258
-		$classes[] = 'wpinv-failed-transaction';
259
-		$classes[] = 'wpinv-page';
260
-	}
257
+    if( wpinv_is_failed_transaction_page() ) {
258
+        $classes[] = 'wpinv-failed-transaction';
259
+        $classes[] = 'wpinv-page';
260
+    }
261 261
 
262
-	if( wpinv_is_invoice_history_page() ) {
263
-		$classes[] = 'wpinv-history';
264
-		$classes[] = 'wpinv-page';
265
-	}
262
+    if( wpinv_is_invoice_history_page() ) {
263
+        $classes[] = 'wpinv-history';
264
+        $classes[] = 'wpinv-page';
265
+    }
266 266
 
267
-	if( wpinv_is_subscriptions_history_page() ) {
268
-		$classes[] = 'wpinv-subscription';
269
-		$classes[] = 'wpinv-page';
270
-	}
267
+    if( wpinv_is_subscriptions_history_page() ) {
268
+        $classes[] = 'wpinv-subscription';
269
+        $classes[] = 'wpinv-page';
270
+    }
271 271
 
272
-	if( wpinv_is_test_mode() ) {
273
-		$classes[] = 'wpinv-test-mode';
274
-		$classes[] = 'wpinv-page';
275
-	}
272
+    if( wpinv_is_test_mode() ) {
273
+        $classes[] = 'wpinv-test-mode';
274
+        $classes[] = 'wpinv-page';
275
+    }
276 276
 
277
-	return array_unique( $classes );
277
+    return array_unique( $classes );
278 278
 }
279 279
 add_filter( 'body_class', 'wpinv_add_body_classes' );
280 280
 
@@ -958,21 +958,21 @@  discard block
 block discarded – undo
958 958
 
959 959
     $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
960 960
     
961
-	// Remove unavailable tags.
961
+    // Remove unavailable tags.
962 962
     $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
963 963
 
964 964
     // Clean up white space.
965
-	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
965
+    $formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
966 966
     $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
967 967
     
968 968
     // Break newlines apart and remove empty lines/trim commas and white space.
969
-	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
969
+    $formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
970 970
 
971 971
     // Add html breaks.
972
-	$formatted_address = implode( $separator, $formatted_address );
972
+    $formatted_address = implode( $separator, $formatted_address );
973 973
 
974
-	// We're done!
975
-	return $formatted_address;
974
+    // We're done!
975
+    return $formatted_address;
976 976
     
977 977
 }
978 978
 
@@ -1059,7 +1059,7 @@  discard block
 block discarded – undo
1059 1059
 
1060 1060
                     $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1061 1061
                     $summary    = '';
1062
-	                $item_name    = '';
1062
+                    $item_name    = '';
1063 1063
                     $cols       = 3;
1064 1064
                     if ( !empty($item) ) {
1065 1065
                         $item_name  = $item->get_name();
@@ -1416,13 +1416,13 @@  discard block
 block discarded – undo
1416 1416
     $payment_form    = wpinv_get_default_payment_form();
1417 1417
 
1418 1418
     ob_start();
1419
-	    do_action( 'wpinv_checkout_content_before' );
1419
+        do_action( 'wpinv_checkout_content_before' );
1420 1420
 
1421 1421
         if ( wpinv_get_cart_contents() ) {
1422 1422
 
1423 1423
             // Get the form elements and items.
1424
-	        $elements = $invoicing->form_elements->get_form_elements( $payment_form );
1425
-	        $items    = $invoicing->form_elements->convert_checkout_items( wpinv_get_cart_contents(), wpinv_get_invoice_cart() );
1424
+            $elements = $invoicing->form_elements->get_form_elements( $payment_form );
1425
+            $items    = $invoicing->form_elements->convert_checkout_items( wpinv_get_cart_contents(), wpinv_get_invoice_cart() );
1426 1426
             ?>
1427 1427
             <form class="wpinv_payment_form" action="<?php echo $form_action; ?>" method="POST">
1428 1428
                 <?php do_action( 'wpinv_main_checkout_form_top' ); ?>
@@ -1446,10 +1446,10 @@  discard block
 block discarded – undo
1446 1446
             do_action( 'wpinv_cart_empty' );
1447 1447
         }
1448 1448
         echo '</div><!--end #wpinv_checkout_wrap-->';
1449
-	    do_action( 'wpinv_checkout_content_after' );
1449
+        do_action( 'wpinv_checkout_content_after' );
1450 1450
         $content = ob_get_clean();
1451 1451
 
1452
-		return str_replace( 'sr-only', '', $content );
1452
+        return str_replace( 'sr-only', '', $content );
1453 1453
 }
1454 1454
 
1455 1455
 function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
@@ -1475,7 +1475,7 @@  discard block
 block discarded – undo
1475 1475
 add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1476 1476
 
1477 1477
 function wpinv_empty_cart_message() {
1478
-	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1478
+    return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1479 1479
 }
1480 1480
 
1481 1481
 /**
@@ -2295,14 +2295,14 @@  discard block
 block discarded – undo
2295 2295
     global $invoicing;
2296 2296
 
2297 2297
     // Ensure that it is published.
2298
-	if ( 'publish' != get_post_status( $form ) ) {
2299
-		return aui()->alert(
2300
-			array(
2301
-				'type'    => 'warning',
2302
-				'content' => __( 'This payment form is no longer active', 'invoicing' ),
2303
-			)
2304
-		);
2305
-	}
2298
+    if ( 'publish' != get_post_status( $form ) ) {
2299
+        return aui()->alert(
2300
+            array(
2301
+                'type'    => 'warning',
2302
+                'content' => __( 'This payment form is no longer active', 'invoicing' ),
2303
+            )
2304
+        );
2305
+    }
2306 2306
 
2307 2307
     // Get the form.
2308 2308
     $form = new GetPaid_Payment_Form( $form );
@@ -2318,46 +2318,46 @@  discard block
 block discarded – undo
2318 2318
     global $invoicing;
2319 2319
 
2320 2320
     foreach ( array_keys( $items ) as $id ) {
2321
-	    if ( 'publish' != get_post_status( $id ) ) {
2322
-		    unset( $items[ $id ] );
2323
-	    }
2321
+        if ( 'publish' != get_post_status( $id ) ) {
2322
+            unset( $items[ $id ] );
2323
+        }
2324 2324
     }
2325 2325
 
2326 2326
     if ( empty( $items ) ) {
2327
-		return aui()->alert(
2328
-			array(
2329
-				'type'    => 'warning',
2330
-				'content' => __( 'No published items found', 'invoicing' ),
2331
-			)
2332
-		);
2327
+        return aui()->alert(
2328
+            array(
2329
+                'type'    => 'warning',
2330
+                'content' => __( 'No published items found', 'invoicing' ),
2331
+            )
2332
+        );
2333 2333
     }
2334 2334
 
2335 2335
     $item_key = getpaid_convert_items_to_string( $items );
2336 2336
 
2337 2337
     // Get the form elements and items.
2338 2338
     $form     = wpinv_get_default_payment_form();
2339
-	$elements = $invoicing->form_elements->get_form_elements( $form );
2340
-	$items    = $invoicing->form_elements->convert_normal_items( $items );
2339
+    $elements = $invoicing->form_elements->get_form_elements( $form );
2340
+    $items    = $invoicing->form_elements->convert_normal_items( $items );
2341 2341
 
2342
-	ob_start();
2343
-	echo "<form class='wpinv_payment_form'>";
2344
-	do_action( 'wpinv_payment_form_top' );
2342
+    ob_start();
2343
+    echo "<form class='wpinv_payment_form'>";
2344
+    do_action( 'wpinv_payment_form_top' );
2345 2345
     echo "<input type='hidden' name='form_id' value='$form'/>";
2346 2346
     echo "<input type='hidden' name='form_items' value='$item_key'/>";
2347
-	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2348
-	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2347
+    wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2348
+    wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2349 2349
 
2350
-	foreach ( $elements as $element ) {
2351
-		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2352
-		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2353
-	}
2350
+    foreach ( $elements as $element ) {
2351
+        do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2352
+        do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2353
+    }
2354 2354
 
2355
-	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2356
-	do_action( 'wpinv_payment_form_bottom' );
2357
-	echo '</form>';
2355
+    echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2356
+    do_action( 'wpinv_payment_form_bottom' );
2357
+    echo '</form>';
2358 2358
 
2359
-	$content = ob_get_clean();
2360
-	return str_replace( 'sr-only', '', $content );
2359
+    $content = ob_get_clean();
2360
+    return str_replace( 'sr-only', '', $content );
2361 2361
 }
2362 2362
 
2363 2363
 /**
@@ -2369,47 +2369,47 @@  discard block
 block discarded – undo
2369 2369
     $invoice = wpinv_get_invoice( $invoice_id );
2370 2370
 
2371 2371
     if ( empty( $invoice ) ) {
2372
-		return aui()->alert(
2373
-			array(
2374
-				'type'    => 'warning',
2375
-				'content' => __( 'Invoice not found', 'invoicing' ),
2376
-			)
2377
-		);
2372
+        return aui()->alert(
2373
+            array(
2374
+                'type'    => 'warning',
2375
+                'content' => __( 'Invoice not found', 'invoicing' ),
2376
+            )
2377
+        );
2378 2378
     }
2379 2379
 
2380 2380
     if ( $invoice->is_paid() ) {
2381
-		return aui()->alert(
2382
-			array(
2383
-				'type'    => 'warning',
2384
-				'content' => __( 'Invoice has already been paid', 'invoicing' ),
2385
-			)
2386
-		);
2381
+        return aui()->alert(
2382
+            array(
2383
+                'type'    => 'warning',
2384
+                'content' => __( 'Invoice has already been paid', 'invoicing' ),
2385
+            )
2386
+        );
2387 2387
     }
2388 2388
 
2389 2389
     // Get the form elements and items.
2390 2390
     $form     = wpinv_get_default_payment_form();
2391
-	$elements = $invoicing->form_elements->get_form_elements( $form );
2392
-	$items    = $invoicing->form_elements->convert_checkout_items( $invoice->cart_details, $invoice );
2391
+    $elements = $invoicing->form_elements->get_form_elements( $form );
2392
+    $items    = $invoicing->form_elements->convert_checkout_items( $invoice->cart_details, $invoice );
2393 2393
 
2394
-	ob_start();
2395
-	echo "<form class='wpinv_payment_form'>";
2396
-	do_action( 'wpinv_payment_form_top' );
2394
+    ob_start();
2395
+    echo "<form class='wpinv_payment_form'>";
2396
+    do_action( 'wpinv_payment_form_top' );
2397 2397
     echo "<input type='hidden' name='form_id' value='$form'/>";
2398 2398
     echo "<input type='hidden' name='invoice_id' value='$invoice_id'/>";
2399
-	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2400
-	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2399
+    wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2400
+    wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2401 2401
 
2402
-	foreach ( $elements as $element ) {
2403
-		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2404
-		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2405
-	}
2402
+    foreach ( $elements as $element ) {
2403
+        do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2404
+        do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2405
+    }
2406 2406
 
2407
-	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2408
-	do_action( 'wpinv_payment_form_bottom' );
2409
-	echo '</form>';
2407
+    echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2408
+    do_action( 'wpinv_payment_form_bottom' );
2409
+    echo '</form>';
2410 2410
 
2411
-	$content = ob_get_clean();
2412
-	return str_replace( 'sr-only', '', $content );
2411
+    $content = ob_get_clean();
2412
+    return str_replace( 'sr-only', '', $content );
2413 2413
 }
2414 2414
 
2415 2415
 /**
@@ -2464,7 +2464,7 @@  discard block
 block discarded – undo
2464 2464
         return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-form='$form'>$label</button>"; 
2465 2465
     }
2466 2466
 	
2467
-	if ( ! empty( $items ) ) {
2467
+    if ( ! empty( $items ) ) {
2468 2468
         $items  = esc_attr( $items );
2469 2469
         return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-item='$items'>$label</button>"; 
2470 2470
     }
Please login to merge, or discard this patch.