Passed
Pull Request — master (#865)
by Kiran
08:35 queued 02:32
created
includes/reports/class-getpaid-graph-downloader.php 1 patch
Indentation   +202 added lines, -202 removed lines patch added patch discarded remove patch
@@ -12,218 +12,218 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Graph_Downloader {
14 14
 
15
-	/**
16
-	 * @var GetPaid_Reports_Report
17
-	 */
18
-	public $handler;
19
-
20
-	/**
21
-	 * Class constructor.
22
-	 *
23
-	 */
24
-	public function __construct() {
25
-		$this->handler = new GetPaid_Reports_Report();
26
-	}
27
-
28
-	/**
29
-	 * Prepares the datastore handler.
30
-	 *
31
-	 * @return GetPaid_Reports_Report_Items|GetPaid_Reports_Report_Gateways|GetPaid_Reports_Report_Discounts
32
-	 */
33
-	public function prepare_handler( $graph ) {
34
-
35
-		if ( empty( $this->handler->views[ $graph ] ) ) {
36
-			wp_die( esc_html__( 'Invalid Graph', 'invoicing' ), 400 );
37
-		}
38
-
39
-		return new $this->handler->views[ $graph ]['class']();
40
-
41
-	}
42
-
43
-	/**
44
-	 * Prepares the output stream.
45
-	 *
46
-	 * @return resource
47
-	 */
48
-	public function prepare_output() {
49
-
50
-		$output  = fopen( 'php://output', 'w' );
51
-
52
-		if ( false === $output ) {
53
-			wp_die( esc_html__( 'Unsupported server', 'invoicing' ), 500 );
54
-		}
55
-
56
-		return $output;
57
-	}
58
-
59
-	/**
60
-	 * Prepares the file type.
61
-	 *
62
-	 * @return string
63
-	 */
64
-	public function prepare_file_type( $graph ) {
65
-
66
-		$file_type = empty( $_REQUEST['file_type'] ) ? 'csv' : sanitize_text_field( $_REQUEST['file_type'] );
67
-		$file_name = wpinv_sanitize_key( "getpaid-$graph-" . current_time( 'Y-m-d' ) );
68
-
69
-		header( "Content-Type:application/$file_type" );
70
-		header( "Content-Disposition:attachment;filename=$file_name.$file_type" );
71
-
72
-		return $file_type;
73
-	}
74
-
75
-	/**
76
-	 * Handles the actual download.
77
-	 *
78
-	 */
79
-	public function download( $graph ) {
80
-		global $wpdb;
81
-
82
-		$handler   = $this->prepare_handler( $graph );
83
-		$stream    = $this->prepare_output();
84
-		$stats     = $wpdb->get_results( $handler->get_sql( $handler->get_range() ) );
85
-		$headers   = array( $handler->field, 'total', 'total_raw' );
86
-		$file_type = $this->prepare_file_type( $graph );
87
-
88
-		if ( 'csv' == $file_type ) {
89
-			$this->download_csv( $stats, $stream, $headers );
90
-		} elseif ( 'xml' == $file_type ) {
91
-			$this->download_xml( $stats, $stream, $headers );
92
-		} else {
93
-			$this->download_json( $stats, $stream, $headers );
94
-		}
95
-
96
-		fclose( $stream );
97
-		exit;
98
-	}
99
-
100
-	/**
101
-	 * Downloads graph as csv
102
-	 *
103
-	 * @param array $stats The stats being downloaded.
104
-	 * @param resource $stream The stream to output to.
105
-	 * @param array $headers The fields to stream.
106
-	 * @since       1.0.19
107
-	 */
108
-	public function download_csv( $stats, $stream, $headers ) {
109
-
110
-		// Output the csv column headers.
111
-		fputcsv( $stream, $headers );
112
-
113
-		// Loop through
114
-		foreach ( $stats as $stat ) {
115
-			$row  = array_values( $this->prepare_row( $stat, $headers ) );
116
-			$row  = array_map( 'maybe_serialize', $row );
117
-			fputcsv( $stream, $row );
118
-		}
119
-
120
-	}
121
-
122
-	/**
123
-	 * Downloads graph as json
124
-	 *
125
-	 * @param array $stats The stats being downloaded.
126
-	 * @param resource $stream The stream to output to.
127
-	 * @param array $headers The fields to stream.
128
-	 * @since       1.0.19
129
-	 */
130
-	public function download_json( $stats, $stream, $headers ) {
131
-
132
-		$prepared = array();
133
-
134
-		// Loop through
135
-		foreach ( $stats as $stat ) {
136
-			$prepared[] = $this->prepare_row( $stat, $headers );
137
-		}
138
-
139
-		fwrite( $stream, wp_json_encode( $prepared ) );
140
-
141
-	}
142
-
143
-	/**
144
-	 * Downloads graph as xml
145
-	 *
146
-	 * @param array $stats The stats being downloaded.
147
-	 * @param resource $stream The stream to output to.
148
-	 * @param array $headers The fields to stream.
149
-	 * @since       1.0.19
150
-	 */
151
-	public function download_xml( $stats, $stream, $headers ) {
152
-
153
-		$prepared = array();
154
-
155
-		// Loop through
156
-		foreach ( $stats as $stat ) {
157
-			$prepared[] = $this->prepare_row( $stat, $headers );
158
-		}
159
-
160
-		$xml = new SimpleXMLElement( '<?xml version="1.0"?><data></data>' );
161
-		$this->convert_array_xml( $prepared, $xml );
162
-
163
-		fwrite( $stream, $xml->asXML() );
164
-
165
-	}
166
-
167
-	/**
168
-	 * Converts stats array to xml
169
-	 *
170
-	 * @access      public
171
-	 * @since      1.0.19
172
-	 */
173
-	public function convert_array_xml( $data, $xml ) {
174
-
175
-		// Loop through
176
-		foreach ( $data as $key => $value ) {
177
-
178
-			$key = preg_replace( '/[^A-Za-z0-9_\-]/', '', $key );
179
-
180
-			if ( is_array( $value ) ) {
181
-
182
-				if ( is_numeric( $key ) ) {
183
-					$key = 'item' . $key; //dealing with <0/>..<n/> issues
184
-				}
185
-
186
-				$subnode = $xml->addChild( $key );
187
-				$this->convert_array_xml( $value, $subnode );
188
-
189
-			} else {
190
-				$xml->addChild( $key, $value ?  htmlspecialchars( $value ) : $value );
191
-			}
15
+    /**
16
+     * @var GetPaid_Reports_Report
17
+     */
18
+    public $handler;
19
+
20
+    /**
21
+     * Class constructor.
22
+     *
23
+     */
24
+    public function __construct() {
25
+        $this->handler = new GetPaid_Reports_Report();
26
+    }
27
+
28
+    /**
29
+     * Prepares the datastore handler.
30
+     *
31
+     * @return GetPaid_Reports_Report_Items|GetPaid_Reports_Report_Gateways|GetPaid_Reports_Report_Discounts
32
+     */
33
+    public function prepare_handler( $graph ) {
34
+
35
+        if ( empty( $this->handler->views[ $graph ] ) ) {
36
+            wp_die( esc_html__( 'Invalid Graph', 'invoicing' ), 400 );
37
+        }
38
+
39
+        return new $this->handler->views[ $graph ]['class']();
40
+
41
+    }
42
+
43
+    /**
44
+     * Prepares the output stream.
45
+     *
46
+     * @return resource
47
+     */
48
+    public function prepare_output() {
49
+
50
+        $output  = fopen( 'php://output', 'w' );
51
+
52
+        if ( false === $output ) {
53
+            wp_die( esc_html__( 'Unsupported server', 'invoicing' ), 500 );
54
+        }
55
+
56
+        return $output;
57
+    }
58
+
59
+    /**
60
+     * Prepares the file type.
61
+     *
62
+     * @return string
63
+     */
64
+    public function prepare_file_type( $graph ) {
65
+
66
+        $file_type = empty( $_REQUEST['file_type'] ) ? 'csv' : sanitize_text_field( $_REQUEST['file_type'] );
67
+        $file_name = wpinv_sanitize_key( "getpaid-$graph-" . current_time( 'Y-m-d' ) );
68
+
69
+        header( "Content-Type:application/$file_type" );
70
+        header( "Content-Disposition:attachment;filename=$file_name.$file_type" );
71
+
72
+        return $file_type;
73
+    }
74
+
75
+    /**
76
+     * Handles the actual download.
77
+     *
78
+     */
79
+    public function download( $graph ) {
80
+        global $wpdb;
81
+
82
+        $handler   = $this->prepare_handler( $graph );
83
+        $stream    = $this->prepare_output();
84
+        $stats     = $wpdb->get_results( $handler->get_sql( $handler->get_range() ) );
85
+        $headers   = array( $handler->field, 'total', 'total_raw' );
86
+        $file_type = $this->prepare_file_type( $graph );
87
+
88
+        if ( 'csv' == $file_type ) {
89
+            $this->download_csv( $stats, $stream, $headers );
90
+        } elseif ( 'xml' == $file_type ) {
91
+            $this->download_xml( $stats, $stream, $headers );
92
+        } else {
93
+            $this->download_json( $stats, $stream, $headers );
94
+        }
95
+
96
+        fclose( $stream );
97
+        exit;
98
+    }
99
+
100
+    /**
101
+     * Downloads graph as csv
102
+     *
103
+     * @param array $stats The stats being downloaded.
104
+     * @param resource $stream The stream to output to.
105
+     * @param array $headers The fields to stream.
106
+     * @since       1.0.19
107
+     */
108
+    public function download_csv( $stats, $stream, $headers ) {
109
+
110
+        // Output the csv column headers.
111
+        fputcsv( $stream, $headers );
112
+
113
+        // Loop through
114
+        foreach ( $stats as $stat ) {
115
+            $row  = array_values( $this->prepare_row( $stat, $headers ) );
116
+            $row  = array_map( 'maybe_serialize', $row );
117
+            fputcsv( $stream, $row );
118
+        }
119
+
120
+    }
121
+
122
+    /**
123
+     * Downloads graph as json
124
+     *
125
+     * @param array $stats The stats being downloaded.
126
+     * @param resource $stream The stream to output to.
127
+     * @param array $headers The fields to stream.
128
+     * @since       1.0.19
129
+     */
130
+    public function download_json( $stats, $stream, $headers ) {
131
+
132
+        $prepared = array();
133
+
134
+        // Loop through
135
+        foreach ( $stats as $stat ) {
136
+            $prepared[] = $this->prepare_row( $stat, $headers );
137
+        }
138
+
139
+        fwrite( $stream, wp_json_encode( $prepared ) );
140
+
141
+    }
142
+
143
+    /**
144
+     * Downloads graph as xml
145
+     *
146
+     * @param array $stats The stats being downloaded.
147
+     * @param resource $stream The stream to output to.
148
+     * @param array $headers The fields to stream.
149
+     * @since       1.0.19
150
+     */
151
+    public function download_xml( $stats, $stream, $headers ) {
152
+
153
+        $prepared = array();
154
+
155
+        // Loop through
156
+        foreach ( $stats as $stat ) {
157
+            $prepared[] = $this->prepare_row( $stat, $headers );
158
+        }
159
+
160
+        $xml = new SimpleXMLElement( '<?xml version="1.0"?><data></data>' );
161
+        $this->convert_array_xml( $prepared, $xml );
162
+
163
+        fwrite( $stream, $xml->asXML() );
164
+
165
+    }
166
+
167
+    /**
168
+     * Converts stats array to xml
169
+     *
170
+     * @access      public
171
+     * @since      1.0.19
172
+     */
173
+    public function convert_array_xml( $data, $xml ) {
174
+
175
+        // Loop through
176
+        foreach ( $data as $key => $value ) {
177
+
178
+            $key = preg_replace( '/[^A-Za-z0-9_\-]/', '', $key );
179
+
180
+            if ( is_array( $value ) ) {
181
+
182
+                if ( is_numeric( $key ) ) {
183
+                    $key = 'item' . $key; //dealing with <0/>..<n/> issues
184
+                }
185
+
186
+                $subnode = $xml->addChild( $key );
187
+                $this->convert_array_xml( $value, $subnode );
188
+
189
+            } else {
190
+                $xml->addChild( $key, $value ?  htmlspecialchars( $value ) : $value );
191
+            }
192 192
 }
193 193
 
194
-	}
194
+    }
195 195
 
196
-	/**
197
-	 * Prepares a single row for download.
198
-	 *
199
-	 * @param stdClass|array $row The row to prepare..
200
-	 * @param array $fields The fields to stream.
201
-	 * @since       1.0.19
202
-	 * @return array
203
-	 */
204
-	public function prepare_row( $row, $fields ) {
196
+    /**
197
+     * Prepares a single row for download.
198
+     *
199
+     * @param stdClass|array $row The row to prepare..
200
+     * @param array $fields The fields to stream.
201
+     * @since       1.0.19
202
+     * @return array
203
+     */
204
+    public function prepare_row( $row, $fields ) {
205 205
 
206
-		$prepared = array();
207
-		$row      = (array) $row;
206
+        $prepared = array();
207
+        $row      = (array) $row;
208 208
 
209
-		foreach ( $fields as $field ) {
209
+        foreach ( $fields as $field ) {
210 210
 
211
-			if ( $field === 'total' ) {
212
-				$prepared[ $field ] = html_entity_decode( strip_tags( wpinv_price( $row['total'] ) ), ENT_QUOTES );
213
-				continue;
214
-			}
211
+            if ( $field === 'total' ) {
212
+                $prepared[ $field ] = html_entity_decode( strip_tags( wpinv_price( $row['total'] ) ), ENT_QUOTES );
213
+                continue;
214
+            }
215 215
 
216
-			if ( $field === 'total_raw' ) {
217
-				$prepared[ $field ] = wpinv_round_amount( wpinv_sanitize_amount( $row['total'] ) );
218
-				continue;
219
-			}
216
+            if ( $field === 'total_raw' ) {
217
+                $prepared[ $field ] = wpinv_round_amount( wpinv_sanitize_amount( $row['total'] ) );
218
+                continue;
219
+            }
220 220
 
221
-			$prepared[ $field ] = strip_tags( $row[ $field ] );
221
+            $prepared[ $field ] = strip_tags( $row[ $field ] );
222 222
 
223
-		}
223
+        }
224 224
 
225
-		return $prepared;
226
-	}
225
+        return $prepared;
226
+    }
227 227
 
228 228
 
229 229
 }
Please login to merge, or discard this patch.
includes/reports/class-getpaid-invoice-exporter.php 1 patch
Indentation   +232 added lines, -232 removed lines patch added patch discarded remove patch
@@ -12,236 +12,236 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Invoice_Exporter extends GetPaid_Graph_Downloader {
14 14
 
15
-	/**
16
-	 * Retrieves invoices query args.
17
-	 *
18
-	 * @param string $post_type post type to retrieve.
19
-	 * @param array $args Args to search for.
20
-	 * @return array
21
-	 */
22
-	public function get_invoice_query_args( $post_type, $args ) {
23
-
24
-		$query_args = array(
25
-			'post_type'              => $post_type,
26
-			'post_status'            => array_keys( wpinv_get_invoice_statuses( true, false, $post_type ) ),
27
-			'posts_per_page'         => -1,
28
-			'no_found_rows'          => true,
29
-			'update_post_term_cache' => false,
30
-			'fields'                 => 'ids',
31
-		);
32
-
33
-		if ( ! empty( $args['status'] ) && in_array( $args['status'], $query_args['post_status'], true ) ) {
34
-			$query_args['post_status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) );
35
-		}
36
-
37
-		$date_query = array();
38
-		if ( ! empty( $args['to_date'] ) ) {
39
-			$date_query['before'] = wpinv_clean( $args['to_date'] );
40
-		}
41
-
42
-		if ( ! empty( $args['from_date'] ) ) {
43
-			$date_query['after'] = wpinv_clean( $args['from_date'] );
44
-		}
45
-
46
-		if ( ! empty( $date_query ) ) {
47
-			$date_query['inclusive']  = true;
48
-			$query_args['date_query'] = array( $date_query );
49
-		}
50
-
51
-		return $query_args;
52
-	}
53
-
54
-	/**
55
-	 * Retrieves invoices.
56
-	 *
57
-	 * @param array $query_args WP_Query args.
58
-	 * @return WPInv_Invoice[]
59
-	 */
60
-	public function get_invoices( $query_args ) {
61
-
62
-		// Get invoices.
63
-		$invoices = new WP_Query( $query_args );
64
-
65
-		// Prepare the results.
66
-		return array_map( 'wpinv_get_invoice', $invoices->posts );
67
-
68
-	}
69
-
70
-	/**
71
-	 * Handles the actual download.
72
-	 *
73
-	 */
74
-	public function export( $post_type, $args ) {
75
-
76
-		$invoices  = $this->get_invoices( $this->get_invoice_query_args( $post_type, $args ) );
77
-		$stream    = $this->prepare_output();
78
-		$headers   = $this->get_export_fields( $post_type );
79
-		$file_type = $this->prepare_file_type( strtolower( getpaid_get_post_type_label( $post_type ) ) );
80
-
81
-		if ( 'csv' == $file_type ) {
82
-			$this->download_csv( $invoices, $stream, $headers );
83
-		} elseif ( 'xml' == $file_type ) {
84
-			$this->download_xml( $invoices, $stream, $headers );
85
-		} else {
86
-			$this->download_json( $invoices, $stream, $headers );
87
-		}
88
-
89
-		fclose( $stream );
90
-		exit;
91
-	}
92
-
93
-	/**
94
-	 * Prepares a single invoice for download.
95
-	 *
96
-	 * @param WPInv_Invoice $invoice The invoice to prepare..
97
-	 * @param array $fields The fields to stream.
98
-	 * @since       1.0.19
99
-	 * @return array
100
-	 */
101
-	public function prepare_row( $invoice, $fields ) {
102
-
103
-		$prepared      = array();
104
-		$amount_fields = $this->get_amount_fields( $invoice->get_post_type() );
105
-		$meta_fields = $this->get_payment_form_meta( $invoice );
106
-
107
-		foreach ( $fields as $field ) {
108
-			$value  = '';
109
-			$method = "get_$field";
110
-
111
-			if ( method_exists( $invoice, $method ) ) {
112
-				$value  = $invoice->$method();
113
-			} else if( strpos( $field, '_' ) === 0 && isset( $meta_fields[ $field ] ) ) {
114
-				$value = $meta_fields[ $field ];
115
-			}
116
-
117
-			if ( in_array( $field, $amount_fields ) ) {
118
-				$value  = wpinv_round_amount( wpinv_sanitize_amount( $value ) );
119
-			}
120
-
121
-			$prepared[ $field ] = wpinv_clean( $value );
122
-
123
-		}
124
-
125
-		return $prepared;
126
-	}
127
-
128
-	/**
129
-	 * Retrieves export fields.
130
-	 *
131
-	 * @param string $post_type
132
-	 * @since       1.0.19
133
-	 * @return array
134
-	 */
135
-	public function get_export_fields( $post_type ) {
136
-
137
-		$fields = array(
138
-			'id',
139
-			'parent_id',
140
-			'status',
141
-			'date_created',
142
-			'date_modified',
143
-			'date_due',
144
-			'date_completed',
145
-			'number',
146
-			'key',
147
-			'description',
148
-			'post_type',
149
-			'mode',
150
-			'customer_id',
151
-			'customer_first_name',
152
-			'customer_last_name',
153
-			'customer_phone',
154
-			'customer_email',
155
-			'customer_country',
156
-			'customer_city',
157
-			'customer_state',
158
-			'customer_zip',
159
-			'customer_company',
160
-			'customer_vat_number',
161
-			'customer_address',
162
-			'subtotal',
163
-			'total_discount',
164
-			'total_tax',
165
-			'total_fees',
166
-			'fees',
167
-			'discounts',
168
-			'taxes',
169
-			'cart_details',
170
-			'item_ids',
171
-			'payment_form',
172
-			'discount_code',
173
-			'gateway',
174
-			'transaction_id',
175
-			'currency',
176
-			'disable_taxes',
177
-			'subscription_id',
178
-			'remote_subscription_id',
179
-			'is_viewed',
180
-			'email_cc',
181
-			'template',
182
-			'created_via',
183
-    	);
184
-
185
-		// Payment form meta fields.
186
-		$meta_fields = getpaid_get_payment_form_custom_fields();
187
-
188
-		if ( ! empty( $meta_fields ) ) {
189
-			foreach ( $meta_fields as $field_key => $field_label ) {
190
-				$fields[] = $field_key;
191
-			}
192
-		}
193
-
194
-		return apply_filters( 'getpaid_invoice_exporter_get_fields', $fields, $post_type );
195
-	}
196
-
197
-	/**
198
-	 * Retrieves amount fields.
199
-	 *
200
-	 * @param string $post_type
201
-	 * @since       1.0.19
202
-	 * @return array
203
-	 */
204
-	public function get_amount_fields( $post_type ) {
205
-
206
-		$fields = array(
207
-			'subtotal',
208
-			'total_discount',
209
-			'total_tax',
210
-			'total_fees',
211
-    	);
212
-
213
-		return apply_filters( 'getpaid_invoice_exporter_get_amount_fields', $fields, $post_type );
214
-	}
215
-
216
-	/**
217
-	 * Retrieves payment form meta fields.
218
-	 *
219
-	 * @since 2.8.23
220
-	 *
221
-	 * @return array
222
-	 */
223
-	public function get_payment_form_meta( $invoice ) {
224
-		// Payment form meta fields.
225
-		$field_keys = getpaid_get_payment_form_custom_fields();
226
-		$meta = get_post_meta( $invoice->get_id(), 'additional_meta_data', true );
227
-
228
-		$field_values = array();
229
-		if ( ! empty( $field_keys ) ) {
230
-			foreach ( $field_keys as $field_key => $field_label ) {
231
-				$value = '';
232
-
233
-				if ( ! empty( $meta ) ) {
234
-					foreach ( $meta as $meta_label => $meta_value ) {
235
-						if ( getpaid_strtolower( wpinv_clean( wp_unslash( $meta_label ) ) ) == getpaid_strtolower( $field_label ) ) {
236
-							$value = $meta_value;
237
-						}
238
-					}
239
-				}
240
-
241
-				$field_values[ $field_key ] = $value;
242
-			}
243
-		}
244
-
245
-		return $field_values;
246
-	}
15
+    /**
16
+     * Retrieves invoices query args.
17
+     *
18
+     * @param string $post_type post type to retrieve.
19
+     * @param array $args Args to search for.
20
+     * @return array
21
+     */
22
+    public function get_invoice_query_args( $post_type, $args ) {
23
+
24
+        $query_args = array(
25
+            'post_type'              => $post_type,
26
+            'post_status'            => array_keys( wpinv_get_invoice_statuses( true, false, $post_type ) ),
27
+            'posts_per_page'         => -1,
28
+            'no_found_rows'          => true,
29
+            'update_post_term_cache' => false,
30
+            'fields'                 => 'ids',
31
+        );
32
+
33
+        if ( ! empty( $args['status'] ) && in_array( $args['status'], $query_args['post_status'], true ) ) {
34
+            $query_args['post_status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) );
35
+        }
36
+
37
+        $date_query = array();
38
+        if ( ! empty( $args['to_date'] ) ) {
39
+            $date_query['before'] = wpinv_clean( $args['to_date'] );
40
+        }
41
+
42
+        if ( ! empty( $args['from_date'] ) ) {
43
+            $date_query['after'] = wpinv_clean( $args['from_date'] );
44
+        }
45
+
46
+        if ( ! empty( $date_query ) ) {
47
+            $date_query['inclusive']  = true;
48
+            $query_args['date_query'] = array( $date_query );
49
+        }
50
+
51
+        return $query_args;
52
+    }
53
+
54
+    /**
55
+     * Retrieves invoices.
56
+     *
57
+     * @param array $query_args WP_Query args.
58
+     * @return WPInv_Invoice[]
59
+     */
60
+    public function get_invoices( $query_args ) {
61
+
62
+        // Get invoices.
63
+        $invoices = new WP_Query( $query_args );
64
+
65
+        // Prepare the results.
66
+        return array_map( 'wpinv_get_invoice', $invoices->posts );
67
+
68
+    }
69
+
70
+    /**
71
+     * Handles the actual download.
72
+     *
73
+     */
74
+    public function export( $post_type, $args ) {
75
+
76
+        $invoices  = $this->get_invoices( $this->get_invoice_query_args( $post_type, $args ) );
77
+        $stream    = $this->prepare_output();
78
+        $headers   = $this->get_export_fields( $post_type );
79
+        $file_type = $this->prepare_file_type( strtolower( getpaid_get_post_type_label( $post_type ) ) );
80
+
81
+        if ( 'csv' == $file_type ) {
82
+            $this->download_csv( $invoices, $stream, $headers );
83
+        } elseif ( 'xml' == $file_type ) {
84
+            $this->download_xml( $invoices, $stream, $headers );
85
+        } else {
86
+            $this->download_json( $invoices, $stream, $headers );
87
+        }
88
+
89
+        fclose( $stream );
90
+        exit;
91
+    }
92
+
93
+    /**
94
+     * Prepares a single invoice for download.
95
+     *
96
+     * @param WPInv_Invoice $invoice The invoice to prepare..
97
+     * @param array $fields The fields to stream.
98
+     * @since       1.0.19
99
+     * @return array
100
+     */
101
+    public function prepare_row( $invoice, $fields ) {
102
+
103
+        $prepared      = array();
104
+        $amount_fields = $this->get_amount_fields( $invoice->get_post_type() );
105
+        $meta_fields = $this->get_payment_form_meta( $invoice );
106
+
107
+        foreach ( $fields as $field ) {
108
+            $value  = '';
109
+            $method = "get_$field";
110
+
111
+            if ( method_exists( $invoice, $method ) ) {
112
+                $value  = $invoice->$method();
113
+            } else if( strpos( $field, '_' ) === 0 && isset( $meta_fields[ $field ] ) ) {
114
+                $value = $meta_fields[ $field ];
115
+            }
116
+
117
+            if ( in_array( $field, $amount_fields ) ) {
118
+                $value  = wpinv_round_amount( wpinv_sanitize_amount( $value ) );
119
+            }
120
+
121
+            $prepared[ $field ] = wpinv_clean( $value );
122
+
123
+        }
124
+
125
+        return $prepared;
126
+    }
127
+
128
+    /**
129
+     * Retrieves export fields.
130
+     *
131
+     * @param string $post_type
132
+     * @since       1.0.19
133
+     * @return array
134
+     */
135
+    public function get_export_fields( $post_type ) {
136
+
137
+        $fields = array(
138
+            'id',
139
+            'parent_id',
140
+            'status',
141
+            'date_created',
142
+            'date_modified',
143
+            'date_due',
144
+            'date_completed',
145
+            'number',
146
+            'key',
147
+            'description',
148
+            'post_type',
149
+            'mode',
150
+            'customer_id',
151
+            'customer_first_name',
152
+            'customer_last_name',
153
+            'customer_phone',
154
+            'customer_email',
155
+            'customer_country',
156
+            'customer_city',
157
+            'customer_state',
158
+            'customer_zip',
159
+            'customer_company',
160
+            'customer_vat_number',
161
+            'customer_address',
162
+            'subtotal',
163
+            'total_discount',
164
+            'total_tax',
165
+            'total_fees',
166
+            'fees',
167
+            'discounts',
168
+            'taxes',
169
+            'cart_details',
170
+            'item_ids',
171
+            'payment_form',
172
+            'discount_code',
173
+            'gateway',
174
+            'transaction_id',
175
+            'currency',
176
+            'disable_taxes',
177
+            'subscription_id',
178
+            'remote_subscription_id',
179
+            'is_viewed',
180
+            'email_cc',
181
+            'template',
182
+            'created_via',
183
+        );
184
+
185
+        // Payment form meta fields.
186
+        $meta_fields = getpaid_get_payment_form_custom_fields();
187
+
188
+        if ( ! empty( $meta_fields ) ) {
189
+            foreach ( $meta_fields as $field_key => $field_label ) {
190
+                $fields[] = $field_key;
191
+            }
192
+        }
193
+
194
+        return apply_filters( 'getpaid_invoice_exporter_get_fields', $fields, $post_type );
195
+    }
196
+
197
+    /**
198
+     * Retrieves amount fields.
199
+     *
200
+     * @param string $post_type
201
+     * @since       1.0.19
202
+     * @return array
203
+     */
204
+    public function get_amount_fields( $post_type ) {
205
+
206
+        $fields = array(
207
+            'subtotal',
208
+            'total_discount',
209
+            'total_tax',
210
+            'total_fees',
211
+        );
212
+
213
+        return apply_filters( 'getpaid_invoice_exporter_get_amount_fields', $fields, $post_type );
214
+    }
215
+
216
+    /**
217
+     * Retrieves payment form meta fields.
218
+     *
219
+     * @since 2.8.23
220
+     *
221
+     * @return array
222
+     */
223
+    public function get_payment_form_meta( $invoice ) {
224
+        // Payment form meta fields.
225
+        $field_keys = getpaid_get_payment_form_custom_fields();
226
+        $meta = get_post_meta( $invoice->get_id(), 'additional_meta_data', true );
227
+
228
+        $field_values = array();
229
+        if ( ! empty( $field_keys ) ) {
230
+            foreach ( $field_keys as $field_key => $field_label ) {
231
+                $value = '';
232
+
233
+                if ( ! empty( $meta ) ) {
234
+                    foreach ( $meta as $meta_label => $meta_value ) {
235
+                        if ( getpaid_strtolower( wpinv_clean( wp_unslash( $meta_label ) ) ) == getpaid_strtolower( $field_label ) ) {
236
+                            $value = $meta_value;
237
+                        }
238
+                    }
239
+                }
240
+
241
+                $field_values[ $field_key ] = $value;
242
+            }
243
+        }
244
+
245
+        return $field_values;
246
+    }
247 247
 }
Please login to merge, or discard this patch.
includes/gateways/class-getpaid-paypal-api.php 1 patch
Indentation   +214 added lines, -214 removed lines patch added patch discarded remove patch
@@ -10,227 +10,227 @@
 block discarded – undo
10 10
  */
11 11
 class GetPaid_PayPal_API {
12 12
 
13
-	/**
14
-	 * Retrieves the bearer token.
15
-	 *
13
+    /**
14
+     * Retrieves the bearer token.
15
+     *
16 16
      * @return string|\WP_Error
17
-	 */
18
-	public static function get_token( $mode = 'live' ) {
17
+     */
18
+    public static function get_token( $mode = 'live' ) {
19 19
 
20
-		$token = get_transient( 'getpaid_paypal_' . $mode . '_token' );
20
+        $token = get_transient( 'getpaid_paypal_' . $mode . '_token' );
21 21
 
22
-		if ( $token ) {
23
-			return $token;
24
-		}
22
+        if ( $token ) {
23
+            return $token;
24
+        }
25 25
 
26
-		$client_id  = 'live' === $mode ? wpinv_get_option( 'paypal_client_id' ) : wpinv_get_option( 'paypal_sandbox_client_id' );
27
-		$secret_key = 'live' === $mode ? wpinv_get_option( 'paypal_secret' ) : wpinv_get_option( 'paypal_sandbox_secret' );
28
-		$url        = self::get_api_url( 'v1/oauth2/token?grant_type=client_credentials', $mode );
26
+        $client_id  = 'live' === $mode ? wpinv_get_option( 'paypal_client_id' ) : wpinv_get_option( 'paypal_sandbox_client_id' );
27
+        $secret_key = 'live' === $mode ? wpinv_get_option( 'paypal_secret' ) : wpinv_get_option( 'paypal_sandbox_secret' );
28
+        $url        = self::get_api_url( 'v1/oauth2/token?grant_type=client_credentials', $mode );
29 29
 
30 30
         if ( empty( $client_id ) || empty( $secret_key ) ) {
31 31
             return new \WP_Error( 'invalid_request', 'Missing client id or secret key.', array( 'status' => 400 ) );
32 32
         }
33 33
 
34
-		$args   = array(
35
-			'method'  => 'POST',
36
-			'timeout' => 30,
37
-			'headers' => array(
38
-				'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
39
-				'Accept'        => 'application/json',
40
-				'Content-Type'  => 'application/x-www-form-urlencoded',
41
-			),
42
-		);
43
-
44
-		$response = self::response_or_error( wp_remote_post( $url, $args ) );
45
-
46
-		if ( is_wp_error( $response ) ) {
47
-			return $response;
48
-		}
49
-
50
-		if ( ! isset( $response->access_token ) ) {
51
-			return new \WP_Error( 'invalid_request', 'Could not create token.', array( 'status' => 400 ) );
52
-		}
53
-
54
-		set_transient( 'getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600 );
55
-		return $response->access_token;
56
-	}
57
-
58
-	/**
59
-	 * Retrieves the PayPal API URL.
60
-	 *
61
-	 * @param string $endpoint
62
-	 * @return string
63
-	 */
64
-	public static function get_api_url( $endpoint = '', $mode = 'live'  ) {
65
-		$endpoint = ltrim( $endpoint, '/' );
66
-		return 'live' === $mode ? 'https://api-m.paypal.com/' . $endpoint : 'https://api-m.sandbox.paypal.com/' . $endpoint;
67
-	}
68
-
69
-	/**
70
-	 * Handles a post request.
71
-	 *
72
-	 * @param string $path The path to the endpoint.
73
-	 * @param mixed $data The data to send.
74
-	 * @param string $method The method to use.
75
-	 *
76
-	 * @return true|\WP_Error
77
-	 */
78
-	public static function post( $path, $data, $mode = 'live', $method = 'POST' ) {
79
-
80
-		$access_token = self::get_token( $mode );
81
-
82
-		if ( is_wp_error( $access_token ) ) {
83
-			return $access_token;
84
-		}
85
-
86
-		$url  = self::get_api_url( $path, $mode );
87
-		$args = array(
88
-			'method'  => $method,
89
-			'headers' => array(
90
-				'Authorization' => 'Bearer ' . $access_token,
91
-				'Content-Type'  => 'application/json',
92
-			),
93
-		);
94
-
95
-		if( ! empty( $data )) {
96
-			$args['body'] = wp_json_encode( $data );
97
-		}
98
-
99
-		return self::response_or_error( wp_remote_post( $url, $args ) );
100
-	}
101
-
102
-	/**
103
-	 * Handles a get request.
104
-	 *
105
-	 * @param string $path The path to the endpoint.
106
-	 * @param string $method
107
-	 * @return object|\WP_Error
108
-	 */
109
-	public static function get( $path, $mode = 'live', $method = 'GET' ) {
110
-
111
-		$access_token = self::get_token( $mode );
112
-
113
-		if ( is_wp_error( $access_token ) ) {
114
-			return $access_token;
115
-		}
116
-
117
-		$url  = self::get_api_url( $path, $mode );
118
-		$args = array(
119
-			'method'  => $method,
120
-			'headers' => array(
121
-				'Authorization' => 'Bearer ' . $access_token,
122
-			),
123
-		);
124
-
125
-		return self::response_or_error( wp_remote_get( $url, $args ) );
126
-	}
127
-
128
-	/**
129
-	 * Returns the response body
130
-	 *
131
-	 * @since 1.0.0
132
-	 * @version 1.0.0
133
-	 * @param \WP_Error|array $response
134
-	 * @return \WP_Error|object
135
-	 */
136
-	public static function response_or_error( $response ) {
137
-
138
-		if ( is_wp_error( $response ) ) {
139
-			return new \WP_Error( 'paypal_error', __( 'There was a problem connecting to the PayPal API endpoint.', 'invoicing' ) );
140
-		}
141
-
142
-		if ( empty( $response['body'] ) ) {
143
-			return true;
144
-		}
145
-
146
-		$response_body = json_decode( wp_remote_retrieve_body( $response ) );
147
-
148
-		if ( wp_remote_retrieve_response_code( $response ) > 299 ) {
149
-
150
-			// Normal errors.
151
-			if ( $response_body && isset( $response_body->message ) ) {
152
-				$error_message = $response_body->message;
153
-
154
-			// Identity errors.
155
-			} elseif ( $response_body && isset( $response_body->error_description ) ) {
156
-				$error_message = $response_body->error_description;
157
-				return new \WP_Error( 'paypal_error', wp_kses_post( $response_body->error_description ) );
158
-			} else {
159
-				$error_message = __( 'There was an error connecting to the PayPal API endpoint.', 'invoicing' );
160
-			}
161
-
162
-			return new \WP_Error( 'paypal_error', $error_message );
163
-		}
164
-
165
-		return $response_body;
166
-	}
167
-
168
-	/**
169
-	 * Fetches an order.
170
-	 *
171
-	 * @since 1.0.0
172
-	 * @version 1.0.0
173
-	 * @param string $order_id
174
-	 * @link https://developer.paypal.com/docs/api/orders/v2/#orders_get
175
-	 * @return \WP_Error|object
176
-	 */
177
-	public static function get_order( $order_id, $mode = 'live' ) {
178
-		return self::get( '/v2/checkout/orders/' . $order_id, $mode );
179
-	}
180
-
181
-	/**
182
-	 * Fetches a subscription.
183
-	 *
184
-	 * @since 1.0.0
185
-	 * @version 1.0.0
186
-	 * @param string $subscription_id
187
-	 * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get
188
-	 * @return \WP_Error|object
189
-	 */
190
-	public static function get_subscription( $subscription_id, $mode = 'live' ) {
191
-		return self::get( '/v1/billing/subscriptions/' . $subscription_id, $mode );
192
-	}
193
-
194
-	/**
195
-	 * Fetches a subscription's latest transactions (limits search to last one day).
196
-	 *
197
-	 * @since 1.0.0
198
-	 * @version 1.0.0
199
-	 * @param string $subscription_id
200
-	 * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions
201
-	 * @return \WP_Error|object
202
-	 */
203
-	public static function get_subscription_transaction( $subscription_id, $mode = 'live' ) {
204
-		$start_time = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '-1 day' ) );
205
-		$end_time   = gmdate( 'Y-m-d\TH:i:s\Z' );
206
-		return self::get( "/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode );
207
-	}
208
-
209
-	/**
210
-	 * Refunds a capture.
211
-	 *
212
-	 * @since 1.0.0
213
-	 * @version 1.0.0
214
-	 * @param string $capture_id
215
-	 * @param array  $args
216
-	 * @link https://developer.paypal.com/docs/api/payments/v2/#captures_refund
217
-	 * @return \WP_Error|object
218
-	 */
219
-	public static function refund_capture( $capture_id, $args = array(), $mode = 'live' ) {
220
-		return self::post( '/v2/payments/captures/' . $capture_id . '/refund', $args, $mode );
221
-	}
222
-
223
-	/**
224
-	 * Cancels a subscription.
225
-	 *
226
-	 * @since 2.8.24
227
-	 * @version 2.8.24
228
-	 * @param string $subscription_id
229
-	 * @param array  $args
230
-	 * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel
231
-	 * @return \WP_Error|object
232
-	 */
233
-	public static function cancel_subscription( $subscription_id, $args = array(), $mode = 'live' ) {
234
-		return self::post( '/v1/billing/subscriptions/' . $subscription_id . '/cancel', $args, $mode );
235
-	}
34
+        $args   = array(
35
+            'method'  => 'POST',
36
+            'timeout' => 30,
37
+            'headers' => array(
38
+                'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
39
+                'Accept'        => 'application/json',
40
+                'Content-Type'  => 'application/x-www-form-urlencoded',
41
+            ),
42
+        );
43
+
44
+        $response = self::response_or_error( wp_remote_post( $url, $args ) );
45
+
46
+        if ( is_wp_error( $response ) ) {
47
+            return $response;
48
+        }
49
+
50
+        if ( ! isset( $response->access_token ) ) {
51
+            return new \WP_Error( 'invalid_request', 'Could not create token.', array( 'status' => 400 ) );
52
+        }
53
+
54
+        set_transient( 'getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600 );
55
+        return $response->access_token;
56
+    }
57
+
58
+    /**
59
+     * Retrieves the PayPal API URL.
60
+     *
61
+     * @param string $endpoint
62
+     * @return string
63
+     */
64
+    public static function get_api_url( $endpoint = '', $mode = 'live'  ) {
65
+        $endpoint = ltrim( $endpoint, '/' );
66
+        return 'live' === $mode ? 'https://api-m.paypal.com/' . $endpoint : 'https://api-m.sandbox.paypal.com/' . $endpoint;
67
+    }
68
+
69
+    /**
70
+     * Handles a post request.
71
+     *
72
+     * @param string $path The path to the endpoint.
73
+     * @param mixed $data The data to send.
74
+     * @param string $method The method to use.
75
+     *
76
+     * @return true|\WP_Error
77
+     */
78
+    public static function post( $path, $data, $mode = 'live', $method = 'POST' ) {
79
+
80
+        $access_token = self::get_token( $mode );
81
+
82
+        if ( is_wp_error( $access_token ) ) {
83
+            return $access_token;
84
+        }
85
+
86
+        $url  = self::get_api_url( $path, $mode );
87
+        $args = array(
88
+            'method'  => $method,
89
+            'headers' => array(
90
+                'Authorization' => 'Bearer ' . $access_token,
91
+                'Content-Type'  => 'application/json',
92
+            ),
93
+        );
94
+
95
+        if( ! empty( $data )) {
96
+            $args['body'] = wp_json_encode( $data );
97
+        }
98
+
99
+        return self::response_or_error( wp_remote_post( $url, $args ) );
100
+    }
101
+
102
+    /**
103
+     * Handles a get request.
104
+     *
105
+     * @param string $path The path to the endpoint.
106
+     * @param string $method
107
+     * @return object|\WP_Error
108
+     */
109
+    public static function get( $path, $mode = 'live', $method = 'GET' ) {
110
+
111
+        $access_token = self::get_token( $mode );
112
+
113
+        if ( is_wp_error( $access_token ) ) {
114
+            return $access_token;
115
+        }
116
+
117
+        $url  = self::get_api_url( $path, $mode );
118
+        $args = array(
119
+            'method'  => $method,
120
+            'headers' => array(
121
+                'Authorization' => 'Bearer ' . $access_token,
122
+            ),
123
+        );
124
+
125
+        return self::response_or_error( wp_remote_get( $url, $args ) );
126
+    }
127
+
128
+    /**
129
+     * Returns the response body
130
+     *
131
+     * @since 1.0.0
132
+     * @version 1.0.0
133
+     * @param \WP_Error|array $response
134
+     * @return \WP_Error|object
135
+     */
136
+    public static function response_or_error( $response ) {
137
+
138
+        if ( is_wp_error( $response ) ) {
139
+            return new \WP_Error( 'paypal_error', __( 'There was a problem connecting to the PayPal API endpoint.', 'invoicing' ) );
140
+        }
141
+
142
+        if ( empty( $response['body'] ) ) {
143
+            return true;
144
+        }
145
+
146
+        $response_body = json_decode( wp_remote_retrieve_body( $response ) );
147
+
148
+        if ( wp_remote_retrieve_response_code( $response ) > 299 ) {
149
+
150
+            // Normal errors.
151
+            if ( $response_body && isset( $response_body->message ) ) {
152
+                $error_message = $response_body->message;
153
+
154
+            // Identity errors.
155
+            } elseif ( $response_body && isset( $response_body->error_description ) ) {
156
+                $error_message = $response_body->error_description;
157
+                return new \WP_Error( 'paypal_error', wp_kses_post( $response_body->error_description ) );
158
+            } else {
159
+                $error_message = __( 'There was an error connecting to the PayPal API endpoint.', 'invoicing' );
160
+            }
161
+
162
+            return new \WP_Error( 'paypal_error', $error_message );
163
+        }
164
+
165
+        return $response_body;
166
+    }
167
+
168
+    /**
169
+     * Fetches an order.
170
+     *
171
+     * @since 1.0.0
172
+     * @version 1.0.0
173
+     * @param string $order_id
174
+     * @link https://developer.paypal.com/docs/api/orders/v2/#orders_get
175
+     * @return \WP_Error|object
176
+     */
177
+    public static function get_order( $order_id, $mode = 'live' ) {
178
+        return self::get( '/v2/checkout/orders/' . $order_id, $mode );
179
+    }
180
+
181
+    /**
182
+     * Fetches a subscription.
183
+     *
184
+     * @since 1.0.0
185
+     * @version 1.0.0
186
+     * @param string $subscription_id
187
+     * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get
188
+     * @return \WP_Error|object
189
+     */
190
+    public static function get_subscription( $subscription_id, $mode = 'live' ) {
191
+        return self::get( '/v1/billing/subscriptions/' . $subscription_id, $mode );
192
+    }
193
+
194
+    /**
195
+     * Fetches a subscription's latest transactions (limits search to last one day).
196
+     *
197
+     * @since 1.0.0
198
+     * @version 1.0.0
199
+     * @param string $subscription_id
200
+     * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions
201
+     * @return \WP_Error|object
202
+     */
203
+    public static function get_subscription_transaction( $subscription_id, $mode = 'live' ) {
204
+        $start_time = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '-1 day' ) );
205
+        $end_time   = gmdate( 'Y-m-d\TH:i:s\Z' );
206
+        return self::get( "/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode );
207
+    }
208
+
209
+    /**
210
+     * Refunds a capture.
211
+     *
212
+     * @since 1.0.0
213
+     * @version 1.0.0
214
+     * @param string $capture_id
215
+     * @param array  $args
216
+     * @link https://developer.paypal.com/docs/api/payments/v2/#captures_refund
217
+     * @return \WP_Error|object
218
+     */
219
+    public static function refund_capture( $capture_id, $args = array(), $mode = 'live' ) {
220
+        return self::post( '/v2/payments/captures/' . $capture_id . '/refund', $args, $mode );
221
+    }
222
+
223
+    /**
224
+     * Cancels a subscription.
225
+     *
226
+     * @since 2.8.24
227
+     * @version 2.8.24
228
+     * @param string $subscription_id
229
+     * @param array  $args
230
+     * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel
231
+     * @return \WP_Error|object
232
+     */
233
+    public static function cancel_subscription( $subscription_id, $args = array(), $mode = 'live' ) {
234
+        return self::post( '/v1/billing/subscriptions/' . $subscription_id . '/cancel', $args, $mode );
235
+    }
236 236
 }
Please login to merge, or discard this patch.
includes/gateways/class-getpaid-paypal-gateway.php 1 patch
Indentation   +508 added lines, -508 removed lines patch added patch discarded remove patch
@@ -13,103 +13,103 @@  discard block
 block discarded – undo
13 13
 class GetPaid_Paypal_Gateway extends GetPaid_Payment_Gateway {
14 14
 
15 15
     /**
16
-	 * Payment method id.
17
-	 *
18
-	 * @var string
19
-	 */
16
+     * Payment method id.
17
+     *
18
+     * @var string
19
+     */
20 20
     public $id = 'paypal';
21 21
 
22 22
     /**
23
-	 * An array of features that this gateway supports.
24
-	 *
25
-	 * @var array
26
-	 */
23
+     * An array of features that this gateway supports.
24
+     *
25
+     * @var array
26
+     */
27 27
     protected $supports = array( 'subscription', 'sandbox', 'single_subscription_group', 'refunds' );
28 28
 
29 29
     /**
30
-	 * Payment method order.
31
-	 *
32
-	 * @var int
33
-	 */
30
+     * Payment method order.
31
+     *
32
+     * @var int
33
+     */
34 34
     public $order = 1;
35 35
 
36 36
     /**
37
-	 * Stores line items to send to PayPal.
38
-	 *
39
-	 * @var array
40
-	 */
37
+     * Stores line items to send to PayPal.
38
+     *
39
+     * @var array
40
+     */
41 41
     protected $line_items = array();
42 42
 
43 43
     /**
44
-	 * Endpoint for requests from PayPal.
45
-	 *
46
-	 * @var string
47
-	 */
48
-	protected $notify_url;
44
+     * Endpoint for requests from PayPal.
45
+     *
46
+     * @var string
47
+     */
48
+    protected $notify_url;
49 49
 
50
-	/**
51
-	 * Endpoint for requests to PayPal.
52
-	 *
53
-	 * @var string
54
-	 */
50
+    /**
51
+     * Endpoint for requests to PayPal.
52
+     *
53
+     * @var string
54
+     */
55 55
     protected $endpoint;
56 56
 
57 57
     /**
58
-	 * Currencies this gateway is allowed for.
59
-	 *
60
-	 * @var array
61
-	 */
62
-	public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' );
58
+     * Currencies this gateway is allowed for.
59
+     *
60
+     * @var array
61
+     */
62
+    public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' );
63 63
 
64 64
     /**
65
-	 * URL to view a transaction.
66
-	 *
67
-	 * @var string
68
-	 */
65
+     * URL to view a transaction.
66
+     *
67
+     * @var string
68
+     */
69 69
     public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s';
70 70
 
71 71
     /**
72
-	 * URL to view a subscription.
73
-	 *
74
-	 * @var string
75
-	 */
76
-	public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s';
72
+     * URL to view a subscription.
73
+     *
74
+     * @var string
75
+     */
76
+    public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s';
77 77
 
78 78
     /**
79
-	 * Class constructor.
80
-	 */
81
-	public function __construct() {
82
-		parent::__construct();
79
+     * Class constructor.
80
+     */
81
+    public function __construct() {
82
+        parent::__construct();
83 83
 
84 84
         $this->title                = __( 'PayPal Standard', 'invoicing' );
85 85
         $this->method_title         = __( 'PayPal Standard', 'invoicing' );
86 86
         $this->checkout_button_text = __( 'Proceed to PayPal', 'invoicing' );
87 87
         $this->notify_url           = wpinv_get_ipn_url( $this->id );
88 88
 
89
-		add_filter( 'wpinv_subscription_cancel_url', array( $this, 'filter_cancel_subscription_url' ), 10, 2 );
90
-		add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 );
91
-		add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 );
92
-		add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
93
-		add_action( 'wpinv_paypal_connect', array( $this, 'display_connect_buttons' ) );
94
-
95
-		if ( $this->enabled ) {
96
-			add_filter( 'getpaid_paypal_sandbox_notice', array( $this, 'sandbox_notice' ) );
97
-			add_action( 'getpaid_paypal_subscription_cancelled', array( $this, 'subscription_cancelled' ) );
98
-			add_action( 'getpaid_delete_subscription', array( $this, 'subscription_cancelled' ) );
99
-			add_action( 'getpaid_refund_invoice_remotely', array( $this, 'refund_invoice' ) );
100
-		}
89
+        add_filter( 'wpinv_subscription_cancel_url', array( $this, 'filter_cancel_subscription_url' ), 10, 2 );
90
+        add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 );
91
+        add_filter( 'getpaid_get_paypal_connect_url', array( $this, 'maybe_get_connect_url' ), 10, 2 );
92
+        add_action( 'getpaid_authenticated_admin_action_connect_paypal', array( $this, 'connect_paypal' ) );
93
+        add_action( 'wpinv_paypal_connect', array( $this, 'display_connect_buttons' ) );
94
+
95
+        if ( $this->enabled ) {
96
+            add_filter( 'getpaid_paypal_sandbox_notice', array( $this, 'sandbox_notice' ) );
97
+            add_action( 'getpaid_paypal_subscription_cancelled', array( $this, 'subscription_cancelled' ) );
98
+            add_action( 'getpaid_delete_subscription', array( $this, 'subscription_cancelled' ) );
99
+            add_action( 'getpaid_refund_invoice_remotely', array( $this, 'refund_invoice' ) );
100
+        }
101 101
     }
102 102
 
103 103
     /**
104
-	 * Process Payment.
105
-	 *
106
-	 *
107
-	 * @param WPInv_Invoice $invoice Invoice.
108
-	 * @param array $submission_data Posted checkout fields.
109
-	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
110
-	 * @return array
111
-	 */
112
-	public function process_payment( $invoice, $submission_data, $submission ) {
104
+     * Process Payment.
105
+     *
106
+     *
107
+     * @param WPInv_Invoice $invoice Invoice.
108
+     * @param array $submission_data Posted checkout fields.
109
+     * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
110
+     * @return array
111
+     */
112
+    public function process_payment( $invoice, $submission_data, $submission ) {
113 113
 
114 114
         // Get redirect url.
115 115
         $paypal_redirect = $this->get_request_url( $invoice );
@@ -132,15 +132,15 @@  discard block
 block discarded – undo
132 132
     }
133 133
 
134 134
     /**
135
-	 * Get the PayPal request URL for an invoice.
136
-	 *
137
-	 * @param  WPInv_Invoice $invoice Invoice object.
138
-	 * @return string
139
-	 */
140
-	public function get_request_url( $invoice ) {
135
+     * Get the PayPal request URL for an invoice.
136
+     *
137
+     * @param  WPInv_Invoice $invoice Invoice object.
138
+     * @return string
139
+     */
140
+    public function get_request_url( $invoice ) {
141 141
 
142 142
         // Endpoint for this request
143
-		$this->endpoint    = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?';
143
+        $this->endpoint    = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?';
144 144
 
145 145
         // Retrieve paypal args.
146 146
         $paypal_args       = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' );
@@ -153,45 +153,45 @@  discard block
 block discarded – undo
153 153
 
154 154
         return add_query_arg( $paypal_args, $this->endpoint );
155 155
 
156
-	}
156
+    }
157 157
 
158 158
     /**
159
-	 * Get PayPal Args for passing to PP.
160
-	 *
161
-	 * @param  WPInv_Invoice $invoice Invoice object.
162
-	 * @return array
163
-	 */
164
-	protected function get_paypal_args( $invoice ) {
159
+     * Get PayPal Args for passing to PP.
160
+     *
161
+     * @param  WPInv_Invoice $invoice Invoice object.
162
+     * @return array
163
+     */
164
+    protected function get_paypal_args( $invoice ) {
165 165
 
166 166
         // Whether or not to send the line items as one item.
167
-		$force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
168
-
169
-		if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
170
-			$force_one_line_item = true;
171
-		}
172
-
173
-		$paypal_args = apply_filters(
174
-			'getpaid_paypal_args',
175
-			array_merge(
176
-				$this->get_transaction_args( $invoice ),
177
-				$this->get_line_item_args( $invoice, $force_one_line_item )
178
-			),
179
-			$invoice
180
-		);
181
-
182
-		return $this->fix_request_length( $invoice, $paypal_args );
167
+        $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', true, $invoice );
168
+
169
+        if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) {
170
+            $force_one_line_item = true;
171
+        }
172
+
173
+        $paypal_args = apply_filters(
174
+            'getpaid_paypal_args',
175
+            array_merge(
176
+                $this->get_transaction_args( $invoice ),
177
+                $this->get_line_item_args( $invoice, $force_one_line_item )
178
+            ),
179
+            $invoice
180
+        );
181
+
182
+        return $this->fix_request_length( $invoice, $paypal_args );
183 183
     }
184 184
 
185 185
     /**
186
-	 * Get transaction args for paypal request.
187
-	 *
188
-	 * @param WPInv_Invoice $invoice Invoice object.
189
-	 * @return array
190
-	 */
191
-	protected function get_transaction_args( $invoice ) {
186
+     * Get transaction args for paypal request.
187
+     *
188
+     * @param WPInv_Invoice $invoice Invoice object.
189
+     * @return array
190
+     */
191
+    protected function get_transaction_args( $invoice ) {
192 192
 
193
-		$email = $this->is_sandbox( $invoice ) ? wpinv_get_option( 'paypal_sandbox_email', wpinv_get_option( 'paypal_email', '' ) ) : wpinv_get_option( 'paypal_email', '' );
194
-		return array(
193
+        $email = $this->is_sandbox( $invoice ) ? wpinv_get_option( 'paypal_sandbox_email', wpinv_get_option( 'paypal_email', '' ) ) : wpinv_get_option( 'paypal_email', '' );
194
+        return array(
195 195
             'cmd'           => '_cart',
196 196
             'business'      => $email,
197 197
             'no_shipping'   => '1',
@@ -216,16 +216,16 @@  discard block
 block discarded – undo
216 216
     }
217 217
 
218 218
     /**
219
-	 * Get line item args for paypal request.
220
-	 *
221
-	 * @param  WPInv_Invoice $invoice Invoice object.
222
-	 * @param  bool     $force_one_line_item Create only one item for this invoice.
223
-	 * @return array
224
-	 */
225
-	protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
219
+     * Get line item args for paypal request.
220
+     *
221
+     * @param  WPInv_Invoice $invoice Invoice object.
222
+     * @param  bool     $force_one_line_item Create only one item for this invoice.
223
+     * @return array
224
+     */
225
+    protected function get_line_item_args( $invoice, $force_one_line_item = false ) {
226 226
 
227 227
         // Maybe send invoice as a single item.
228
-		if ( $force_one_line_item ) {
228
+        if ( $force_one_line_item ) {
229 229
             return $this->get_line_item_args_single_item( $invoice );
230 230
         }
231 231
 
@@ -245,134 +245,134 @@  discard block
 block discarded – undo
245 245
             $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 );
246 246
         }
247 247
 
248
-		return array_merge( $line_item_args, $this->get_line_items() );
248
+        return array_merge( $line_item_args, $this->get_line_items() );
249 249
 
250 250
     }
251 251
 
252 252
     /**
253
-	 * Get line item args for paypal request as a single line item.
254
-	 *
255
-	 * @param  WPInv_Invoice $invoice Invoice object.
256
-	 * @return array
257
-	 */
258
-	protected function get_line_item_args_single_item( $invoice ) {
259
-		$this->delete_line_items();
253
+     * Get line item args for paypal request as a single line item.
254
+     *
255
+     * @param  WPInv_Invoice $invoice Invoice object.
256
+     * @return array
257
+     */
258
+    protected function get_line_item_args_single_item( $invoice ) {
259
+        $this->delete_line_items();
260 260
 
261 261
         $item_name = wp_sprintf( __( 'Invoice %s', 'invoicing' ), $invoice->get_number() );
262
-		$this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
262
+        $this->add_line_item( $item_name, 1, wpinv_round_amount( (float) $invoice->get_total(), 2, true ), $invoice->get_id() );
263 263
 
264
-		return $this->get_line_items();
264
+        return $this->get_line_items();
265 265
     }
266 266
 
267 267
     /**
268
-	 * Return all line items.
269
-	 */
270
-	protected function get_line_items() {
271
-		return $this->line_items;
272
-	}
268
+     * Return all line items.
269
+     */
270
+    protected function get_line_items() {
271
+        return $this->line_items;
272
+    }
273 273
 
274 274
     /**
275
-	 * Remove all line items.
276
-	 */
277
-	protected function delete_line_items() {
278
-		$this->line_items = array();
275
+     * Remove all line items.
276
+     */
277
+    protected function delete_line_items() {
278
+        $this->line_items = array();
279 279
     }
280 280
 
281 281
     /**
282
-	 * Prepare line items to send to paypal.
283
-	 *
284
-	 * @param  WPInv_Invoice $invoice Invoice object.
285
-	 */
286
-	protected function prepare_line_items( $invoice ) {
287
-		$this->delete_line_items();
288
-
289
-		// Items.
290
-		foreach ( $invoice->get_items() as $item ) {
291
-			$amount   = $item->get_price();
292
-			$quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
293
-			$this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
282
+     * Prepare line items to send to paypal.
283
+     *
284
+     * @param  WPInv_Invoice $invoice Invoice object.
285
+     */
286
+    protected function prepare_line_items( $invoice ) {
287
+        $this->delete_line_items();
288
+
289
+        // Items.
290
+        foreach ( $invoice->get_items() as $item ) {
291
+            $amount   = $item->get_price();
292
+            $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity();
293
+            $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() );
294 294
         }
295 295
 
296 296
         // Fees.
297
-		foreach ( $invoice->get_fees() as $fee => $data ) {
297
+        foreach ( $invoice->get_fees() as $fee => $data ) {
298 298
             $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) );
299 299
         }
300 300
 
301 301
     }
302 302
 
303 303
     /**
304
-	 * Add PayPal Line Item.
305
-	 *
306
-	 * @param  string $item_name Item name.
307
-	 * @param  float    $quantity Item quantity.
308
-	 * @param  float  $amount Amount.
309
-	 * @param  string $item_number Item number.
310
-	 */
311
-	protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
312
-		$index = ( count( $this->line_items ) / 4 ) + 1;
313
-
314
-		/**
315
-		 * Prevent error "Things don't appear to be working at the moment. (https://www.sandbox.paypal.com/webapps/hermes/error)"
316
-		 */
317
-		$item_name = str_replace( "#", "", $item_name );
318
-
319
-		$item = apply_filters(
320
-			'getpaid_paypal_line_item',
321
-			array(
322
-				'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
323
-				'quantity'    => (float) $quantity,
324
-				'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
325
-				'item_number' => $item_number,
326
-			),
327
-			$item_name,
328
-			$quantity,
329
-			$amount,
330
-			$item_number
331
-		);
332
-
333
-		$this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
304
+     * Add PayPal Line Item.
305
+     *
306
+     * @param  string $item_name Item name.
307
+     * @param  float    $quantity Item quantity.
308
+     * @param  float  $amount Amount.
309
+     * @param  string $item_number Item number.
310
+     */
311
+    protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) {
312
+        $index = ( count( $this->line_items ) / 4 ) + 1;
313
+
314
+        /**
315
+         * Prevent error "Things don't appear to be working at the moment. (https://www.sandbox.paypal.com/webapps/hermes/error)"
316
+         */
317
+        $item_name = str_replace( "#", "", $item_name );
318
+
319
+        $item = apply_filters(
320
+            'getpaid_paypal_line_item',
321
+            array(
322
+                'item_name'   => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ),
323
+                'quantity'    => (float) $quantity,
324
+                'amount'      => wpinv_sanitize_amount( (float) $amount, 2 ),
325
+                'item_number' => $item_number,
326
+            ),
327
+            $item_name,
328
+            $quantity,
329
+            $amount,
330
+            $item_number
331
+        );
332
+
333
+        $this->line_items[ 'item_name_' . $index ]   = getpaid_limit_length( $item['item_name'], 127 );
334 334
         $this->line_items[ 'quantity_' . $index ]    = $item['quantity'];
335 335
 
336 336
         // The price or amount of the product, service, or contribution, not including shipping, handling, or tax.
337
-		$this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
338
-		$this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
337
+        $this->line_items[ 'amount_' . $index ]      = $item['amount'] * $item['quantity'];
338
+        $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 );
339 339
     }
340 340
 
341 341
     /**
342
-	 * If the default request with line items is too long, generate a new one with only one line item.
343
-	 *
344
-	 * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
345
-	 *
346
-	 * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
347
-	 * @param array    $paypal_args Arguments sent to Paypal in the request.
348
-	 * @return array
349
-	 */
350
-	protected function fix_request_length( $invoice, $paypal_args ) {
351
-		$max_paypal_length = 2083;
352
-		$query_candidate   = http_build_query( $paypal_args, '', '&' );
353
-
354
-		if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
355
-			return $paypal_args;
356
-		}
357
-
358
-		return apply_filters(
359
-			'getpaid_paypal_args',
360
-			array_merge(
361
-				$this->get_transaction_args( $invoice ),
362
-				$this->get_line_item_args( $invoice, true )
363
-			),
364
-			$invoice
365
-		);
342
+     * If the default request with line items is too long, generate a new one with only one line item.
343
+     *
344
+     * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.
345
+     *
346
+     * @param WPInv_Invoice $invoice Invoice to be sent to Paypal.
347
+     * @param array    $paypal_args Arguments sent to Paypal in the request.
348
+     * @return array
349
+     */
350
+    protected function fix_request_length( $invoice, $paypal_args ) {
351
+        $max_paypal_length = 2083;
352
+        $query_candidate   = http_build_query( $paypal_args, '', '&' );
353
+
354
+        if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) {
355
+            return $paypal_args;
356
+        }
357
+
358
+        return apply_filters(
359
+            'getpaid_paypal_args',
360
+            array_merge(
361
+                $this->get_transaction_args( $invoice ),
362
+                $this->get_line_item_args( $invoice, true )
363
+            ),
364
+            $invoice
365
+        );
366 366
 
367 367
     }
368 368
 
369 369
     /**
370
-	 * Processes recurring invoices.
371
-	 *
372
-	 * @param  array $paypal_args PayPal args.
373
-	 * @param  WPInv_Invoice    $invoice Invoice object.
374
-	 */
375
-	public function process_subscription( $paypal_args, $invoice ) {
370
+     * Processes recurring invoices.
371
+     *
372
+     * @param  array $paypal_args PayPal args.
373
+     * @param  WPInv_Invoice    $invoice Invoice object.
374
+     */
375
+    public function process_subscription( $paypal_args, $invoice ) {
376 376
 
377 377
         // Make sure this is a subscription.
378 378
         if ( ! $invoice->is_recurring() || ! $subscription = getpaid_get_invoice_subscription( $invoice ) ) {
@@ -393,21 +393,21 @@  discard block
 block discarded – undo
393 393
         $recurring_amount       = (float) wpinv_sanitize_amount( $invoice->get_recurring_total(), 2 );
394 394
         $subscription_item      = $invoice->get_recurring( true );
395 395
 
396
-		// Convert 365 days to 1 year.
397
-		if ( 'D' == $period && 365 == $interval ) {
398
-			$period = 'Y';
399
-			$interval = 1;
400
-		}
396
+        // Convert 365 days to 1 year.
397
+        if ( 'D' == $period && 365 == $interval ) {
398
+            $period = 'Y';
399
+            $interval = 1;
400
+        }
401 401
 
402 402
         if ( $subscription_item->has_free_trial() ) {
403 403
 
404 404
             $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount;
405 405
 
406
-			// Trial period length.
407
-			$paypal_args['p1'] = $subscription_item->get_trial_interval();
406
+            // Trial period length.
407
+            $paypal_args['p1'] = $subscription_item->get_trial_interval();
408 408
 
409
-			// Trial period.
410
-			$paypal_args['t1'] = $subscription_item->get_trial_period();
409
+            // Trial period.
410
+            $paypal_args['t1'] = $subscription_item->get_trial_period();
411 411
 
412 412
         } elseif ( $initial_amount != $recurring_amount ) {
413 413
 
@@ -430,40 +430,40 @@  discard block
 block discarded – undo
430 430
         }
431 431
 
432 432
         // We have a recurring payment
433
-		if ( ! isset( $param_number ) || 1 == $param_number ) {
433
+        if ( ! isset( $param_number ) || 1 == $param_number ) {
434 434
 
435
-			// Subscription price
436
-			$paypal_args['a3'] = $recurring_amount;
435
+            // Subscription price
436
+            $paypal_args['a3'] = $recurring_amount;
437 437
 
438
-			// Subscription duration
439
-			$paypal_args['p3'] = $interval;
438
+            // Subscription duration
439
+            $paypal_args['p3'] = $interval;
440 440
 
441
-			// Subscription period
442
-			$paypal_args['t3'] = $period;
441
+            // Subscription period
442
+            $paypal_args['t3'] = $period;
443 443
 
444 444
         }
445 445
 
446 446
         // Recurring payments
447
-		if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
447
+        if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) {
448 448
 
449
-			// Non-recurring payments
450
-			$paypal_args['src'] = 0;
449
+            // Non-recurring payments
450
+            $paypal_args['src'] = 0;
451 451
 
452
-		} else {
452
+        } else {
453 453
 
454
-			$paypal_args['src'] = 1;
454
+            $paypal_args['src'] = 1;
455 455
 
456
-			if ( $bill_times > 0 ) {
456
+            if ( $bill_times > 0 ) {
457 457
 
458
-				// An initial period is being used to charge a sign-up fee
459
-				if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
460
-					$bill_times--;
461
-				}
458
+                // An initial period is being used to charge a sign-up fee
459
+                if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) {
460
+                    $bill_times--;
461
+                }
462 462
 
463 463
                 // Make sure it's not over the max of 52
464 464
                 $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 );
465 465
 
466
-			}
466
+            }
467 467
         }
468 468
 
469 469
         // Force return URL so that order description & instructions display
@@ -475,115 +475,115 @@  discard block
 block discarded – undo
475 475
             if ( isset( $paypal_args[ $arg ] ) ) {
476 476
                 unset( $paypal_args[ $arg ] );
477 477
             }
478
-		}
478
+        }
479 479
 
480 480
         return apply_filters(
481
-			'getpaid_paypal_subscription_args',
482
-			$paypal_args,
483
-			$invoice
481
+            'getpaid_paypal_subscription_args',
482
+            $paypal_args,
483
+            $invoice
484
+        );
485
+
486
+    }
487
+
488
+    /**
489
+     * Refunds an invoice remotely.
490
+     * 
491
+     * @since 2.8.24
492
+     * @param WPInv_Invoice $invoice Invoice object.
493
+     */
494
+    public function refund_invoice( $invoice ) {
495
+
496
+        if ( $invoice->get_gateway() !== $this->id ) {
497
+            return;
498
+        }
499
+
500
+        $mode	= $this->is_sandbox( $invoice ) ? 'sandbox' : 'live';
501
+        $result = GetPaid_PayPal_API::refund_capture( $invoice->get_transaction_id(), array(), $mode );
502
+
503
+        if ( is_wp_error( $result ) ) {
504
+            $invoice->add_system_note(
505
+                sprintf(
506
+                    // translators: %s is the error message.
507
+                    __( 'An error occured while trying to refund invoice #%1$s in PayPal: %2$s', 'invoicing' ),
508
+                    $invoice->get_id(),
509
+                    $result->get_error_message()
510
+                )
511
+            );
512
+        } else {
513
+            $invoice->add_system_note(
514
+                sprintf(
515
+                    // translators: %s is the refund ID.
516
+                    __( 'Successfully refunded invoice #%1$s in PayPal. Refund ID: %2$s', 'invoicing' ),
517
+                    $invoice->get_id(),
518
+                    $result->id
519
+                )
520
+            );
521
+        }
522
+    }
523
+
524
+    /**
525
+     * Cancels a subscription remotely.
526
+     * 
527
+     * @since 2.8.24
528
+     * @param WPInv_Subscription $subscription Subscription object.
529
+     */
530
+    public function subscription_cancelled( $subscription ) {
531
+
532
+        if ( $subscription->get_gateway() != $this->id ) {
533
+            return;
534
+        }
535
+
536
+        $invoice = $subscription->get_parent_invoice();
537
+
538
+        // Abort if the parent invoice does not exist.
539
+        if ( ! $invoice->exists() ) {
540
+            return;
541
+        }
542
+
543
+        $mode	= $this->is_sandbox( $invoice ) ? 'sandbox' : 'live';
544
+        $result = GetPaid_PayPal_API::cancel_subscription( 
545
+            $invoice->get_remote_subscription_id(), 
546
+            array(
547
+                'reason' => __(' Customer requested cancellation', 'invoicing' ),
548
+            ), 
549
+            $mode 
484 550
         );
485 551
 
552
+        if ( is_wp_error( $result ) ) {
553
+
554
+            $error = sprintf(
555
+                // translators: %s is the subscription ID.
556
+                __( 'An error occured while trying to cancel subscription #%s in PayPal.', 'invoicing' ),
557
+                $subscription->get_id()
558
+            );
559
+
560
+            getpaid_admin()->show_error( $error . ' ' . $result->get_error_message() );
561
+
562
+            if ( ! is_admin() ) {
563
+                wpinv_set_error( $result->get_error_code(), $error );
564
+            }
565
+
566
+            return;
567
+        }
568
+
569
+        if ( is_admin() ) {
570
+            getpaid_admin()->show_success(
571
+                sprintf(
572
+                    // translators: %s is the subscription ID.
573
+                    __( 'Successfully cancelled subscription #%s in PayPal.', 'invoicing' ),
574
+                    $subscription->get_id()
575
+                )
576
+            );
577
+        }
578
+
486 579
     }
487 580
 
488
-	/**
489
-	 * Refunds an invoice remotely.
490
-	 * 
491
-	 * @since 2.8.24
492
-	 * @param WPInv_Invoice $invoice Invoice object.
493
-	 */
494
-	public function refund_invoice( $invoice ) {
495
-
496
-		if ( $invoice->get_gateway() !== $this->id ) {
497
-			return;
498
-		}
499
-
500
-		$mode	= $this->is_sandbox( $invoice ) ? 'sandbox' : 'live';
501
-		$result = GetPaid_PayPal_API::refund_capture( $invoice->get_transaction_id(), array(), $mode );
502
-
503
-		if ( is_wp_error( $result ) ) {
504
-			$invoice->add_system_note(
505
-				sprintf(
506
-					// translators: %s is the error message.
507
-					__( 'An error occured while trying to refund invoice #%1$s in PayPal: %2$s', 'invoicing' ),
508
-					$invoice->get_id(),
509
-					$result->get_error_message()
510
-				)
511
-			);
512
-		} else {
513
-			$invoice->add_system_note(
514
-				sprintf(
515
-					// translators: %s is the refund ID.
516
-					__( 'Successfully refunded invoice #%1$s in PayPal. Refund ID: %2$s', 'invoicing' ),
517
-					$invoice->get_id(),
518
-					$result->id
519
-				)
520
-			);
521
-		}
522
-	}
523
-
524
-	/**
525
-	 * Cancels a subscription remotely.
526
-	 * 
527
-	 * @since 2.8.24
528
-	 * @param WPInv_Subscription $subscription Subscription object.
529
-	 */
530
-	public function subscription_cancelled( $subscription ) {
531
-
532
-		if ( $subscription->get_gateway() != $this->id ) {
533
-			return;
534
-		}
535
-
536
-		$invoice = $subscription->get_parent_invoice();
537
-
538
-		// Abort if the parent invoice does not exist.
539
-		if ( ! $invoice->exists() ) {
540
-			return;
541
-		}
542
-
543
-		$mode	= $this->is_sandbox( $invoice ) ? 'sandbox' : 'live';
544
-		$result = GetPaid_PayPal_API::cancel_subscription( 
545
-			$invoice->get_remote_subscription_id(), 
546
-			array(
547
-				'reason' => __(' Customer requested cancellation', 'invoicing' ),
548
-			), 
549
-			$mode 
550
-		);
551
-
552
-		if ( is_wp_error( $result ) ) {
553
-
554
-			$error = sprintf(
555
-				// translators: %s is the subscription ID.
556
-				__( 'An error occured while trying to cancel subscription #%s in PayPal.', 'invoicing' ),
557
-				$subscription->get_id()
558
-			);
559
-
560
-			getpaid_admin()->show_error( $error . ' ' . $result->get_error_message() );
561
-
562
-			if ( ! is_admin() ) {
563
-				wpinv_set_error( $result->get_error_code(), $error );
564
-			}
565
-
566
-			return;
567
-		}
568
-
569
-		if ( is_admin() ) {
570
-			getpaid_admin()->show_success(
571
-				sprintf(
572
-					// translators: %s is the subscription ID.
573
-					__( 'Successfully cancelled subscription #%s in PayPal.', 'invoicing' ),
574
-					$subscription->get_id()
575
-				)
576
-			);
577
-		}
578
-
579
-	}
580
-
581
-    /**
582
-	 * Processes ipns and marks payments as complete.
583
-	 *
584
-	 * @return void
585
-	 */
586
-	public function verify_ipn() {
581
+    /**
582
+     * Processes ipns and marks payments as complete.
583
+     *
584
+     * @return void
585
+     */
586
+    public function verify_ipn() {
587 587
         new GetPaid_Paypal_Gateway_IPN_Handler( $this );
588 588
     }
589 589
 
@@ -593,19 +593,19 @@  discard block
 block discarded – undo
593 593
     public function sandbox_notice() {
594 594
 
595 595
         return sprintf(
596
-			__( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %1$sPayPal Sandbox Testing Guide%2$s for more details.', 'invoicing' ),
597
-			'<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
598
-			'</a>'
599
-		);
596
+            __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %1$sPayPal Sandbox Testing Guide%2$s for more details.', 'invoicing' ),
597
+            '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">',
598
+            '</a>'
599
+        );
600 600
 
601 601
     }
602 602
 
603
-	/**
604
-	 * Filters the gateway settings.
605
-	 *
606
-	 * @param array $admin_settings
607
-	 */
608
-	public function admin_settings( $admin_settings ) {
603
+    /**
604
+     * Filters the gateway settings.
605
+     *
606
+     * @param array $admin_settings
607
+     */
608
+    public function admin_settings( $admin_settings ) {
609 609
 
610 610
         $currencies = sprintf(
611 611
             __( 'Supported Currencies: %s', 'invoicing' ),
@@ -615,66 +615,66 @@  discard block
 block discarded – undo
615 615
         $admin_settings['paypal_active']['desc'] .= " ($currencies)";
616 616
         $admin_settings['paypal_desc']['std']     = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
617 617
 
618
-		// Access tokens.
619
-		$live_email      = wpinv_get_option( 'paypal_email' );
620
-		$sandbox_email   = wpinv_get_option( 'paypal_sandbox_email' );
618
+        // Access tokens.
619
+        $live_email      = wpinv_get_option( 'paypal_email' );
620
+        $sandbox_email   = wpinv_get_option( 'paypal_sandbox_email' );
621 621
 
622
-		$admin_settings['paypal_connect'] = array(
623
-			'type' => 'hook',
624
-			'id'   => 'paypal_connect',
625
-			'name' => __( 'Connect to PayPal', 'invoicing' ),
626
-		);
622
+        $admin_settings['paypal_connect'] = array(
623
+            'type' => 'hook',
624
+            'id'   => 'paypal_connect',
625
+            'name' => __( 'Connect to PayPal', 'invoicing' ),
626
+        );
627 627
 
628 628
         $admin_settings['paypal_email'] = array(
629 629
             'type'  => 'text',
630
-			'class' => 'live-auth-data',
630
+            'class' => 'live-auth-data',
631 631
             'id'    => 'paypal_email',
632 632
             'name'  => __( 'Live Email Address', 'invoicing' ),
633 633
             'desc'  => __( 'The email address of your PayPal account.', 'invoicing' ),
634 634
         );
635 635
 
636
-		$admin_settings['paypal_sandbox_email'] = array(
636
+        $admin_settings['paypal_sandbox_email'] = array(
637 637
             'type'  => 'text',
638
-			'class' => 'sandbox-auth-data',
638
+            'class' => 'sandbox-auth-data',
639 639
             'id'    => 'paypal_sandbox_email',
640 640
             'name'  => __( 'Sandbox Email Address', 'invoicing' ),
641 641
             'desc'  => __( 'The email address of your sandbox PayPal account.', 'invoicing' ),
642
-			'std'   => wpinv_get_option( 'paypal_email', '' ),
642
+            'std'   => wpinv_get_option( 'paypal_email', '' ),
643
+        );
644
+
645
+        // Client ID and secret.
646
+        $admin_settings['paypal_client_id'] = array(
647
+            'type'  => 'text',
648
+            'class' => 'live-auth-data',
649
+            'id'    => 'paypal_client_id',
650
+            'name'  => __( 'Live Client ID', 'invoicing' ),
651
+            'desc'  => __( 'The client ID of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
652
+        );
653
+
654
+        $admin_settings['paypal_sandbox_client_id'] = array(
655
+            'type'  => 'text',
656
+            'class' => 'sandbox-auth-data',
657
+            'id'    => 'paypal_sandbox_client_id',
658
+            'name'  => __( 'Sandbox Client ID', 'invoicing' ),
659
+            'desc'  => __( 'The client ID of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
660
+            'std'   => wpinv_get_option( 'paypal_client_id', '' ),
643 661
         );
644 662
 
645
-		// Client ID and secret.
646
-		$admin_settings['paypal_client_id'] = array(
647
-			'type'  => 'text',
648
-			'class' => 'live-auth-data',
649
-			'id'    => 'paypal_client_id',
650
-			'name'  => __( 'Live Client ID', 'invoicing' ),
651
-			'desc'  => __( 'The client ID of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
652
-		);
653
-
654
-		$admin_settings['paypal_sandbox_client_id'] = array(
655
-			'type'  => 'text',
656
-			'class' => 'sandbox-auth-data',
657
-			'id'    => 'paypal_sandbox_client_id',
658
-			'name'  => __( 'Sandbox Client ID', 'invoicing' ),
659
-			'desc'  => __( 'The client ID of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
660
-			'std'   => wpinv_get_option( 'paypal_client_id', '' ),
661
-		);
662
-
663
-		$admin_settings['paypal_secret'] = array(
664
-			'type'  => 'text',
665
-			'class' => 'live-auth-data',
666
-			'id'    => 'paypal_secret',
667
-			'name'  => __( 'Live Secret', 'invoicing' ),
668
-			'desc'  => __( 'The secret of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
669
-		);
670
-
671
-		$admin_settings['paypal_sandbox_secret'] = array(
672
-			'type'  => 'text',
673
-			'class' => 'sandbox-auth-data',
674
-			'id'    => 'paypal_sandbox_secret',
675
-			'name'  => __( 'Sandbox Secret', 'invoicing' ),
676
-			'desc'  => __( 'The secret of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
677
-		);
663
+        $admin_settings['paypal_secret'] = array(
664
+            'type'  => 'text',
665
+            'class' => 'live-auth-data',
666
+            'id'    => 'paypal_secret',
667
+            'name'  => __( 'Live Secret', 'invoicing' ),
668
+            'desc'  => __( 'The secret of your PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
669
+        );
670
+
671
+        $admin_settings['paypal_sandbox_secret'] = array(
672
+            'type'  => 'text',
673
+            'class' => 'sandbox-auth-data',
674
+            'id'    => 'paypal_sandbox_secret',
675
+            'name'  => __( 'Sandbox Secret', 'invoicing' ),
676
+            'desc'  => __( 'The secret of your sandbox PayPal account. You can retrieve this from your PayPal developer account.', 'invoicing' ),
677
+        );
678 678
 
679 679
         $admin_settings['paypal_ipn_url'] = array(
680 680
             'type'     => 'ipn_url',
@@ -685,57 +685,57 @@  discard block
 block discarded – undo
685 685
             'readonly' => true,
686 686
         );
687 687
 
688
-		return $admin_settings;
689
-	}
690
-
691
-	/**
692
-	 * Retrieves the URL to cancel a subscription.
693
-	 *
694
-	 * @param string $url
695
-	 * @param WPInv_Subscription $subscription
696
-	 */
697
-	public function filter_cancel_subscription_url( $url, $subscription ) {
698
-
699
-		if ( $this->id !== $subscription->get_gateway() ) {
700
-			return $url;
701
-		}
702
-
703
-		// Get the PayPal profile ID.
704
-		$profile_id = $subscription->get_profile_id();
705
-
706
-		// Bail if no profile ID.
707
-		if ( empty( $profile_id ) ) {
708
-			return $url;
709
-		}
710
-
711
-		$cancel_url = 'https://www.paypal.com/myaccount/autopay/connect/%s/cancel';
712
-		if ( $this->is_sandbox( $subscription->get_parent_payment() ) ) {
713
-			$cancel_url = 'https://www.sandbox.paypal.com/myaccount/autopay/connect/%s/cancel';
714
-		}
715
-
716
-		return sprintf( $cancel_url, $profile_id );
717
-	}
718
-
719
-	/**
720
-	 * Retrieves the PayPal connect URL when using the setup wizzard.
721
-	 *
722
-	 *
688
+        return $admin_settings;
689
+    }
690
+
691
+    /**
692
+     * Retrieves the URL to cancel a subscription.
693
+     *
694
+     * @param string $url
695
+     * @param WPInv_Subscription $subscription
696
+     */
697
+    public function filter_cancel_subscription_url( $url, $subscription ) {
698
+
699
+        if ( $this->id !== $subscription->get_gateway() ) {
700
+            return $url;
701
+        }
702
+
703
+        // Get the PayPal profile ID.
704
+        $profile_id = $subscription->get_profile_id();
705
+
706
+        // Bail if no profile ID.
707
+        if ( empty( $profile_id ) ) {
708
+            return $url;
709
+        }
710
+
711
+        $cancel_url = 'https://www.paypal.com/myaccount/autopay/connect/%s/cancel';
712
+        if ( $this->is_sandbox( $subscription->get_parent_payment() ) ) {
713
+            $cancel_url = 'https://www.sandbox.paypal.com/myaccount/autopay/connect/%s/cancel';
714
+        }
715
+
716
+        return sprintf( $cancel_url, $profile_id );
717
+    }
718
+
719
+    /**
720
+     * Retrieves the PayPal connect URL when using the setup wizzard.
721
+     *
722
+     *
723 723
      * @param array $data
724 724
      * @return string
725
-	 */
726
-	public static function maybe_get_connect_url( $url = '', $data = array() ) {
727
-		return self::get_connect_url( false, urldecode( $data['redirect'] ) );
728
-	}
729
-
730
-	/**
731
-	 * Retrieves the PayPal connect URL.
732
-	 *
733
-	 *
725
+     */
726
+    public static function maybe_get_connect_url( $url = '', $data = array() ) {
727
+        return self::get_connect_url( false, urldecode( $data['redirect'] ) );
728
+    }
729
+
730
+    /**
731
+     * Retrieves the PayPal connect URL.
732
+     *
733
+     *
734 734
      * @param bool $is_sandbox
735
-	 * @param string $redirect
735
+     * @param string $redirect
736 736
      * @return string
737
-	 */
738
-	public static function get_connect_url( $is_sandbox, $redirect = '' ) {
737
+     */
738
+    public static function get_connect_url( $is_sandbox, $redirect = '' ) {
739 739
 
740 740
         $redirect_url = add_query_arg(
741 741
             array(
@@ -745,7 +745,7 @@  discard block
 block discarded – undo
745 745
                 'tab'                  => 'gateways',
746 746
                 'section'              => 'paypal',
747 747
                 'getpaid-nonce'        => wp_create_nonce( 'getpaid-nonce' ),
748
-				'redirect'             => urlencode( $redirect ),
748
+                'redirect'             => urlencode( $redirect ),
749 749
             ),
750 750
             admin_url( 'admin.php' )
751 751
         );
@@ -760,12 +760,12 @@  discard block
 block discarded – undo
760 760
 
761 761
     }
762 762
 
763
-	/**
764
-	 * Generates settings page js.
765
-	 *
763
+    /**
764
+     * Generates settings page js.
765
+     *
766 766
      * @return void
767
-	 */
768
-	public static function display_connect_buttons() {
767
+     */
768
+    public static function display_connect_buttons() {
769 769
 
770 770
         ?>
771 771
 			<div class="wpinv-paypal-connect-live">
@@ -807,70 +807,70 @@  discard block
 block discarded – undo
807 807
         <?php
808 808
     }
809 809
 
810
-	/**
811
-	 * Connects to PayPal.
812
-	 *
813
-	 * @param array $data Connection data.
814
-	 * @return void
815
-	 */
816
-	public function connect_paypal( $data ) {
817
-
818
-		$sandbox      = $this->is_sandbox();
819
-		$data         = wp_unslash( $data );
820
-		$access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
821
-
822
-		if ( isset( $data['live_mode'] ) ) {
823
-			$sandbox = empty( $data['live_mode'] );
824
-		}
825
-
826
-		wpinv_update_option( 'paypal_sandbox', (int) $sandbox );
827
-		wpinv_update_option( 'paypal_active', 1 );
828
-
829
-		if ( ! empty( $data['error_description'] ) ) {
830
-			getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
831
-		} else {
832
-
833
-			// Retrieve the user info.
834
-			$user_info = wp_remote_get(
835
-				! $sandbox ? 'https://api-m.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1' : 'https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1',
836
-				array(
837
-
838
-					'headers' => array(
839
-						'Authorization' => 'Bearer ' . $access_token,
840
-						'Content-type'  => 'application/json',
841
-					),
842
-
843
-				)
844
-			);
845
-
846
-			if ( is_wp_error( $user_info ) ) {
847
-				getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
848
-			} else {
849
-
850
-				// Create application.
851
-				$user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
852
-
853
-				if ( $sandbox ) {
854
-					wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
855
-					wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
856
-					set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
857
-					getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) );
858
-				} else {
859
-					wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
860
-					wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
861
-					set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
862
-					getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) );
863
-				}
864
-			}
865
-		}
866
-
867
-		$redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) : urldecode( $data['redirect'] );
868
-
869
-		if ( isset( $data['step'] ) ) {
870
-			$redirect = add_query_arg( 'step', $data['step'], $redirect );
871
-		}
872
-		wp_redirect( $redirect );
873
-		exit;
874
-	}
810
+    /**
811
+     * Connects to PayPal.
812
+     *
813
+     * @param array $data Connection data.
814
+     * @return void
815
+     */
816
+    public function connect_paypal( $data ) {
817
+
818
+        $sandbox      = $this->is_sandbox();
819
+        $data         = wp_unslash( $data );
820
+        $access_token = empty( $data['access_token'] ) ? '' : sanitize_text_field( $data['access_token'] );
821
+
822
+        if ( isset( $data['live_mode'] ) ) {
823
+            $sandbox = empty( $data['live_mode'] );
824
+        }
825
+
826
+        wpinv_update_option( 'paypal_sandbox', (int) $sandbox );
827
+        wpinv_update_option( 'paypal_active', 1 );
828
+
829
+        if ( ! empty( $data['error_description'] ) ) {
830
+            getpaid_admin()->show_error( wp_kses_post( urldecode( $data['error_description'] ) ) );
831
+        } else {
832
+
833
+            // Retrieve the user info.
834
+            $user_info = wp_remote_get(
835
+                ! $sandbox ? 'https://api-m.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1' : 'https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo?schema=paypalv1.1',
836
+                array(
837
+
838
+                    'headers' => array(
839
+                        'Authorization' => 'Bearer ' . $access_token,
840
+                        'Content-type'  => 'application/json',
841
+                    ),
842
+
843
+                )
844
+            );
845
+
846
+            if ( is_wp_error( $user_info ) ) {
847
+                getpaid_admin()->show_error( wp_kses_post( $user_info->get_error_message() ) );
848
+            } else {
849
+
850
+                // Create application.
851
+                $user_info = json_decode( wp_remote_retrieve_body( $user_info ) );
852
+
853
+                if ( $sandbox ) {
854
+                    wpinv_update_option( 'paypal_sandbox_email', sanitize_email( $user_info->emails[0]->value ) );
855
+                    wpinv_update_option( 'paypal_sandbox_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
856
+                    set_transient( 'getpaid_paypal_sandbox_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
857
+                    getpaid_admin()->show_success( __( 'Successfully connected your PayPal sandbox account', 'invoicing' ) );
858
+                } else {
859
+                    wpinv_update_option( 'paypal_email', sanitize_email( $user_info->emails[0]->value ) );
860
+                    wpinv_update_option( 'paypal_refresh_token', sanitize_text_field( urldecode( $data['refresh_token'] ) ) );
861
+                    set_transient( 'getpaid_paypal_access_token', sanitize_text_field( urldecode( $data['access_token'] ) ), (int) $data['expires_in'] );
862
+                    getpaid_admin()->show_success( __( 'Successfully connected your PayPal account', 'invoicing' ) );
863
+                }
864
+            }
865
+        }
866
+
867
+        $redirect = empty( $data['redirect'] ) ? admin_url( 'admin.php?page=wpinv-settings&tab=gateways&section=paypal' ) : urldecode( $data['redirect'] );
868
+
869
+        if ( isset( $data['step'] ) ) {
870
+            $redirect = add_query_arg( 'step', $data['step'], $redirect );
871
+        }
872
+        wp_redirect( $redirect );
873
+        exit;
874
+    }
875 875
 
876 876
 }
Please login to merge, or discard this patch.
templates/payment-forms/elements/checkbox.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -13,19 +13,19 @@
 block discarded – undo
13 13
 $label_class = sanitize_key( preg_replace( '/[^A-Za-z0-9_-]/', '-', $label ) );
14 14
 
15 15
 if ( ! empty( $required ) ) {
16
-	$label .= "<span class='text-danger'> *</span>";
16
+    $label .= "<span class='text-danger'> *</span>";
17 17
 }
18 18
 
19 19
 aui()->input(
20
-	array(
21
-		'type'      => 'checkbox',
22
-		'name'      => esc_attr( $id ),
23
-		'id'        => esc_attr( $element_id ),
24
-		'required'  => ! empty( $required ),
25
-		'label'     => $label,
26
-		'value'     => esc_attr__( 'Yes', 'invoicing' ),
27
-		'help_text' => empty( $description ) ? '' : wp_kses_post( $description ),
28
-		'class'     => $label_class
29
-	),
30
-	true
20
+    array(
21
+        'type'      => 'checkbox',
22
+        'name'      => esc_attr( $id ),
23
+        'id'        => esc_attr( $element_id ),
24
+        'required'  => ! empty( $required ),
25
+        'label'     => $label,
26
+        'value'     => esc_attr__( 'Yes', 'invoicing' ),
27
+        'help_text' => empty( $description ) ? '' : wp_kses_post( $description ),
28
+        'class'     => $label_class
29
+    ),
30
+    true
31 31
 );
Please login to merge, or discard this patch.
templates/payment-forms/elements/address.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
 defined( 'ABSPATH' ) || exit;
11 11
 
12 12
 if ( empty( $fields ) ) {
13
-	return;
13
+    return;
14 14
 }
15 15
 
16 16
 // A prefix for all ids (so that a form can be included in the same page multiple times).
@@ -18,12 +18,12 @@  discard block
 block discarded – undo
18 18
 
19 19
 // Prepare the user's country.
20 20
 if ( ! empty( $form->invoice ) ) {
21
-	$country = $form->invoice->get_country();
21
+    $country = $form->invoice->get_country();
22 22
 }
23 23
 
24 24
 if ( empty( $country ) ) {
25
-	$country = empty( $country ) ? getpaid_get_ip_country() : $country;
26
-	$country = empty( $country ) ? wpinv_get_default_country() : $country;
25
+    $country = empty( $country ) ? getpaid_get_ip_country() : $country;
26
+    $country = empty( $country ) ? wpinv_get_default_country() : $country;
27 27
 }
28 28
 
29 29
 // A prefix for all ids (so that a form can be included in the same page multiple times).
@@ -50,32 +50,32 @@  discard block
 block discarded – undo
50 50
 	<!-- Start Billing Address -->
51 51
 	<div class="getpaid-billing-address-wrapper">
52 52
 		<?php
53
-			$field_type = 'billing';
53
+            $field_type = 'billing';
54 54
 
55
-			wpinv_get_template( 'payment-forms/elements/address-fields.php', array( 'form' => $form, 'fields' => $fields, 'address_type' => $address_type, 'field_type' => $field_type, 'uniqid' => $uniqid, 'country' => $country ) );
55
+            wpinv_get_template( 'payment-forms/elements/address-fields.php', array( 'form' => $form, 'fields' => $fields, 'address_type' => $address_type, 'field_type' => $field_type, 'uniqid' => $uniqid, 'country' => $country ) );
56 56
 
57
-			do_action( 'getpaid_after_payment_form_billing_fields', $form );
58
-		?>
57
+            do_action( 'getpaid_after_payment_form_billing_fields', $form );
58
+        ?>
59 59
 	</div>
60 60
 	<!-- End Billing Address -->
61 61
 <?php endif; ?>
62 62
 
63 63
 <?php if ( 'both' === $address_type ) : ?>
64 64
 	<?php
65
-		aui()->input(
66
-			array(
67
-				'type'     => 'checkbox',
68
-				'name'     => 'same-shipping-address',
69
-				'id'       => "shipping-toggle$uniqid",
70
-				'required' => false,
71
-				'label'    => empty( $shipping_address_toggle ) ? esc_html__( 'Same billing & shipping address.', 'invoicing' ) : wp_kses_post( $shipping_address_toggle ),
72
-				'value'    => 1,
73
-				'checked'  => true,
74
-				'class'    => 'chkbox-same-shipping-address'
75
-			),
76
-			true
77
-		);
78
-	?>
65
+        aui()->input(
66
+            array(
67
+                'type'     => 'checkbox',
68
+                'name'     => 'same-shipping-address',
69
+                'id'       => "shipping-toggle$uniqid",
70
+                'required' => false,
71
+                'label'    => empty( $shipping_address_toggle ) ? esc_html__( 'Same billing & shipping address.', 'invoicing' ) : wp_kses_post( $shipping_address_toggle ),
72
+                'value'    => 1,
73
+                'checked'  => true,
74
+                'class'    => 'chkbox-same-shipping-address'
75
+            ),
76
+            true
77
+        );
78
+    ?>
79 79
 	<!-- Start Shipping Address Title -->
80 80
 	<h4 class="mb-3 getpaid-shipping-address-title">
81 81
 		<?php esc_html_e( 'Shipping Address', 'invoicing' ); ?>
@@ -87,12 +87,12 @@  discard block
 block discarded – undo
87 87
 	<!-- Start Shipping Address -->
88 88
 	<div class="getpaid-shipping-address-wrapper">
89 89
 		<?php
90
-			$field_type = 'shipping';
90
+            $field_type = 'shipping';
91 91
 
92
-			wpinv_get_template( 'payment-forms/elements/address-fields.php', array( 'form' => $form, 'fields' => $fields, 'address_type' => $address_type, 'field_type' => $field_type, 'uniqid' => $uniqid, 'country' => $country ) );
92
+            wpinv_get_template( 'payment-forms/elements/address-fields.php', array( 'form' => $form, 'fields' => $fields, 'address_type' => $address_type, 'field_type' => $field_type, 'uniqid' => $uniqid, 'country' => $country ) );
93 93
 
94
-			do_action( 'getpaid_after_payment_form_shipping_fields', $form );
95
-		?>
94
+            do_action( 'getpaid_after_payment_form_shipping_fields', $form );
95
+        ?>
96 96
 	</div>
97 97
 	<!-- End Shipping Address -->
98 98
 <?php endif; ?>
Please login to merge, or discard this patch.
includes/class-wpinv.php 1 patch
Indentation   +616 added lines, -616 removed lines patch added patch discarded remove patch
@@ -14,620 +14,620 @@
 block discarded – undo
14 14
  */
15 15
 class WPInv_Plugin {
16 16
 
17
-	/**
18
-	 * GetPaid version.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	public $version;
23
-
24
-	/**
25
-	 * Data container.
26
-	 *
27
-	 * @var array
28
-	 */
29
-	protected $data = array();
30
-
31
-	/**
32
-	 * Form elements instance.
33
-	 *
34
-	 * @var WPInv_Payment_Form_Elements
35
-	 */
36
-	public $form_elements;
37
-
38
-	/**
39
-	 * @var array An array of payment gateways.
40
-	 */
41
-	public $gateways;
42
-
43
-	/**
44
-	 * Class constructor.
45
-	 */
46
-	public function __construct() {
47
-		$this->define_constants();
48
-		$this->includes();
49
-		$this->init_hooks();
50
-		$this->set_properties();
51
-	}
52
-
53
-	/**
54
-	 * Sets a custom data property.
55
-	 *
56
-	 * @param string $prop The prop to set.
57
-	 * @param mixed $value The value to retrieve.
58
-	 */
59
-	public function set( $prop, $value ) {
60
-		$this->data[ $prop ] = $value;
61
-	}
62
-
63
-	/**
64
-	 * Gets a custom data property.
65
-	 *
66
-	 * @param string $prop The prop to set.
67
-	 * @return mixed The value.
68
-	 */
69
-	public function get( $prop ) {
70
-		if ( isset( $this->data[ $prop ] ) ) {
71
-			return $this->data[ $prop ];
72
-		}
73
-
74
-		return null;
75
-	}
76
-
77
-	/**
78
-	 * Define class properties.
79
-	 */
80
-	public function set_properties() {
81
-		// Sessions.
82
-		$this->set( 'session', new WPInv_Session_Handler() );
83
-		$GLOBALS['wpi_session'] = $this->get( 'session' ); // Backwards compatibility.
84
-		$GLOBALS['wpinv_euvat'] = new WPInv_EUVat(); // Backwards compatibility.
85
-
86
-		// Init other objects.
87
-		$this->set( 'notes', new WPInv_Notes() );
88
-		$this->set( 'api', new WPInv_API() );
89
-		$this->set( 'post_types', new GetPaid_Post_Types() );
90
-		$this->set( 'template', new GetPaid_Template() );
91
-		$this->set( 'admin', new GetPaid_Admin() );
92
-		$this->set( 'subscriptions', new WPInv_Subscriptions() );
93
-		$this->set( 'invoice_emails', new GetPaid_Invoice_Notification_Emails() );
94
-		$this->set( 'subscription_emails', new GetPaid_Subscription_Notification_Emails() );
95
-		$this->set( 'daily_maintenace', new GetPaid_Daily_Maintenance() );
96
-		$this->set( 'payment_forms', new GetPaid_Payment_Forms() );
97
-		$this->set( 'maxmind', new GetPaid_MaxMind_Geolocation() );
98
-		$this->set( 'data_retention', new WPInv_Data_Retention() );
99
-	}
100
-
101
-	 /**
102
-	 * Define plugin constants.
103
-	 */
104
-	public function define_constants() {
105
-		define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) );
106
-		define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) );
107
-		$this->version = WPINV_VERSION;
108
-	}
109
-
110
-	/**
111
-	 * Hook into actions and filters.
112
-	 *
113
-	 * @since 1.0.19
114
-	 */
115
-	protected function init_hooks() {
116
-		/* Internationalize the text strings used. */
117
-		add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
118
-
119
-		// Init the plugin after WordPress inits.
120
-		add_action( 'init', array( $this, 'init' ), 1 );
121
-		add_action( 'init', array( $this, 'maybe_process_ipn' ), 100 );
122
-		add_action( 'init', array( $this, 'wpinv_actions' ) );
123
-		add_action( 'init', array( $this, 'maybe_do_authenticated_action' ), 100 );
124
-		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 11 );
125
-		add_action( 'wp_footer', array( $this, 'wp_footer' ) );
126
-		add_action( 'wp_head', array( $this, 'wp_head' ) );
127
-		add_action( 'widgets_init', array( $this, 'register_widgets' ) );
128
-		add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) );
129
-		add_filter( 'the_seo_framework_sitemap_supported_post_types', array( $this, 'exclude_invoicing_post_types' ) );
130
-		add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
131
-
132
-		add_filter( 'query_vars', array( $this, 'custom_query_vars' ) );
133
-		add_action( 'init', array( $this, 'add_rewrite_rule' ), 10, 0 );
134
-		add_action( 'pre_get_posts', array( $this, 'maybe_process_new_ipn' ), 1 );
135
-
136
-		// Fires after registering actions.
137
-		do_action( 'wpinv_actions', $this );
138
-		do_action( 'getpaid_actions', $this );
139
-	}
140
-
141
-	public function plugins_loaded() {
142
-		/* Internationalize the text strings used. */
143
-		$this->load_textdomain();
144
-
145
-		do_action( 'wpinv_loaded' );
146
-
147
-		// Fix oxygen page builder conflict
148
-		if ( function_exists( 'ct_css_output' ) ) {
149
-			wpinv_oxygen_fix_conflict();
150
-		}
151
-	}
152
-
153
-	/**
154
-	 * Load Localisation files.
155
-	 *
156
-	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
157
-	 *
158
-	 * Locales found in:
159
-	 *      - WP_LANG_DIR/plugins/invoicing-LOCALE.mo
160
-	 *      - WP_PLUGIN_DIR/invoicing/languages/invoicing-LOCALE.mo
161
-	 *
162
-	 * @since 1.0.0
163
-	 */
164
-	public function load_textdomain() {
165
-		// Determines the current locale.
166
-		if ( function_exists( 'determine_locale' ) ) {
167
-			$locale = determine_locale();
168
-		} else if ( function_exists( 'get_user_locale' ) ) {
169
-			$locale = get_user_locale();
170
-		} else {
171
-			$locale = get_locale();
172
-		}
173
-
174
-		/**
175
-		 * Filter the locale to use for translations.
176
-		 */
177
-		$locale = apply_filters( 'plugin_locale', $locale, 'invoicing' );
178
-
179
-		unload_textdomain( 'invoicing', true );
180
-		load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' );
181
-		load_plugin_textdomain( 'invoicing', false, plugin_basename( dirname( WPINV_PLUGIN_FILE ) ) . '/languages/' );
182
-	}
183
-
184
-	/**
185
-	 * Include required core files used in admin and on the frontend.
186
-	 */
187
-	public function includes() {
188
-		// Start with the settings.
189
-		require_once WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php';
190
-
191
-		// Packages/libraries.
192
-		require_once WPINV_PLUGIN_DIR . 'vendor/autoload.php';
193
-		require_once WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php';
194
-
195
-		// Load functions.
196
-		require_once WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php';
197
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php';
198
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php';
199
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php';
200
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php';
201
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php';
202
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php';
203
-		require_once WPINV_PLUGIN_DIR . 'includes/invoice-functions.php';
204
-		require_once WPINV_PLUGIN_DIR . 'includes/subscription-functions.php';
205
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php';
206
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php';
207
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php';
208
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php';
209
-		require_once WPINV_PLUGIN_DIR . 'includes/user-functions.php';
210
-		require_once WPINV_PLUGIN_DIR . 'includes/error-functions.php';
211
-
212
-		// Register autoloader.
213
-		try {
214
-			spl_autoload_register( array( $this, 'autoload' ), true );
215
-		} catch ( Exception $e ) {
216
-			wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
217
-		}
218
-
219
-		require_once WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php';
220
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php';
221
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php';
222
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php';
223
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php';
224
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php';
225
-		require_once WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php';
226
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php';
227
-		require_once WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php';
228
-		require_once WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php';
229
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php';
230
-		require_once WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php';
231
-		require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php';
232
-		require_once WPINV_PLUGIN_DIR . 'widgets/checkout.php';
233
-		require_once WPINV_PLUGIN_DIR . 'widgets/invoice-history.php';
234
-		require_once WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php';
235
-		require_once WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php';
236
-		require_once WPINV_PLUGIN_DIR . 'widgets/subscriptions.php';
237
-		require_once WPINV_PLUGIN_DIR . 'widgets/buy-item.php';
238
-		require_once WPINV_PLUGIN_DIR . 'widgets/getpaid.php';
239
-		require_once WPINV_PLUGIN_DIR . 'widgets/invoice.php';
240
-		require_once WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php';
241
-
242
-		if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
243
-			GetPaid_Post_Types_Admin::init();
244
-
245
-			require_once WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php';
246
-			require_once WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php';
247
-			require_once WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php';
248
-			require_once WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php';
249
-			require_once WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php';
250
-			require_once WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php';
251
-			// load the user class only on the users.php page
252
-			global $pagenow;
253
-			if ( $pagenow == 'users.php' ) {
254
-				new WPInv_Admin_Users();
255
-			}
256
-		}
257
-
258
-		// Register cli commands
259
-		if ( defined( 'WP_CLI' ) && WP_CLI ) {
260
-			require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php';
261
-			WP_CLI::add_command( 'invoicing', 'WPInv_CLI' );
262
-		}
263
-	}
264
-
265
-	/**
266
-	 * Class autoloader
267
-	 *
268
-	 * @param       string $class_name The name of the class to load.
269
-	 * @access      public
270
-	 * @since       1.0.19
271
-	 * @return      void
272
-	 */
273
-	public function autoload( $class_name ) {
274
-		// Normalize the class name...
275
-		$class_name  = strtolower( $class_name );
276
-
277
-		// ... and make sure it is our class.
278
-		if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
279
-			return;
280
-		}
281
-
282
-		// Next, prepare the file name from the class.
283
-		$file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
284
-
285
-		// Base path of the classes.
286
-		$plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
287
-
288
-		// And an array of possible locations in order of importance.
289
-		$locations = array(
290
-			"$plugin_path/includes",
291
-			"$plugin_path/includes/data-stores",
292
-			"$plugin_path/includes/gateways",
293
-			"$plugin_path/includes/payments",
294
-			"$plugin_path/includes/geolocation",
295
-			"$plugin_path/includes/reports",
296
-			"$plugin_path/includes/api",
297
-			"$plugin_path/includes/admin",
298
-			"$plugin_path/includes/admin/meta-boxes",
299
-		);
300
-
301
-		foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) {
302
-			if ( file_exists( trailingslashit( $location ) . $file_name ) ) {
303
-				include trailingslashit( $location ) . $file_name;
304
-				break;
305
-			}
306
-		}
307
-	}
308
-
309
-	/**
310
-	 * Inits hooks etc.
311
-	 */
312
-	public function init() {
313
-		// Fires before getpaid inits.
314
-		do_action( 'before_getpaid_init', $this );
315
-
316
-		// Maybe upgrade.
317
-		$this->maybe_upgrade_database();
318
-
319
-		// Load default gateways.
320
-		$gateways = apply_filters(
321
-			'getpaid_default_gateways',
322
-			array(
323
-				'manual'        => 'GetPaid_Manual_Gateway',
324
-				'paypal'        => 'GetPaid_Paypal_Gateway',
325
-				'worldpay'      => 'GetPaid_Worldpay_Gateway',
326
-				'bank_transfer' => 'GetPaid_Bank_Transfer_Gateway',
327
-				'authorizenet'  => 'GetPaid_Authorize_Net_Gateway',
328
-			)
329
-		);
330
-
331
-		foreach ( $gateways as $id => $class ) {
332
-			$this->gateways[ $id ] = new $class();
333
-		}
334
-
335
-		if ( 'yes' != get_option( 'wpinv_renamed_gateways' ) ) {
336
-			GetPaid_Installer::rename_gateways_label();
337
-			update_option( 'wpinv_renamed_gateways', 'yes' );
338
-		}
339
-
340
-		// Fires after getpaid inits.
341
-		do_action( 'getpaid_init', $this );
342
-	}
343
-
344
-	/**
345
-	 * Checks if this is an IPN request and processes it.
346
-	 */
347
-	public function maybe_process_ipn() {
348
-		// Ensure that this is an IPN request.
349
-		if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) {
350
-			return;
351
-		}
352
-
353
-		$gateway = sanitize_text_field( $_GET['wpi-gateway'] );
354
-
355
-		do_action( 'wpinv_verify_payment_ipn', $gateway );
356
-		do_action( "wpinv_verify_{$gateway}_ipn" );
357
-		exit;
358
-	}
359
-
360
-	public function enqueue_scripts() {
361
-		// Fires before adding scripts.
362
-		do_action( 'getpaid_enqueue_scripts' );
363
-
364
-		$localize                         = array();
365
-		$localize['ajax_url']             = admin_url( 'admin-ajax.php' );
366
-		$localize['thousands']            = wpinv_thousands_separator();
367
-		$localize['decimals']             = wpinv_decimal_separator();
368
-		$localize['nonce']                = wp_create_nonce( 'wpinv-nonce' );
369
-		$localize['txtComplete']          = __( 'Continue', 'invoicing' );
370
-		$localize['UseTaxes']             = wpinv_use_taxes();
371
-		$localize['formNonce']            = wp_create_nonce( 'getpaid_form_nonce' );
372
-		$localize['loading']              = __( 'Loading...', 'invoicing' );
373
-		$localize['connectionError']      = __( 'Could not establish a connection to the server.', 'invoicing' );
374
-		$localize['recaptchaSettings']    = getpaid_get_recaptcha_settings();
375
-
376
-		$localize = apply_filters( 'wpinv_front_js_localize', $localize );
377
-
378
-		// reCaptcha.
379
-		if ( getpaid_is_recaptcha_enabled() && ( $recaptcha_js = getpaid_recaptcha_api_url() ) ) {
380
-			wp_enqueue_script( 'recaptcha', $recaptcha_js, array(), null, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
381
-		}
382
-
383
-		wp_enqueue_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.min.js', array( 'jquery' ), WPINV_VERSION, true );
384
-		wp_localize_script( 'wpinv-front-script', 'WPInv', $localize );
385
-	}
386
-
387
-	public function wpinv_actions() {
388
-		if ( isset( $_REQUEST['wpi_action'] ) ) {
389
-			do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST );
390
-		}
391
-
392
-		if ( defined( 'WP_ALL_IMPORT_ROOT_DIR' ) ) {
393
-			include plugin_dir_path( __FILE__ ) . 'libraries/wp-all-import/class-getpaid-wp-all-import.php';
394
-		}
395
-	}
396
-
397
-	/**
398
-	 * Fires an action after verifying that a user can fire them.
399
-	 *
400
-	 * Note: If the action is on an invoice, subscription etc, esure that the
401
-	 * current user owns the invoice/subscription.
402
-	 */
403
-	public function maybe_do_authenticated_action() {
404
-		if ( isset( $_REQUEST['getpaid-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
405
-			$key  = sanitize_key( $_REQUEST['getpaid-action'] );
406
-			$data = wp_unslash( $_REQUEST );
407
-
408
-			if ( is_user_logged_in() ) {
409
-				do_action( "getpaid_authenticated_action_$key", $data );
410
-			}
411
-
412
-			do_action( "getpaid_unauthenticated_action_$key", $data );
413
-		}
414
-	}
415
-
416
-	public function pre_get_posts( $wp_query ) {
417
-		if ( ! is_admin() && ! empty( $wp_query->query_vars['post_type'] ) && getpaid_is_invoice_post_type( $wp_query->query_vars['post_type'] ) && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) {
418
-			$wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses( false, false, $wp_query->query_vars['post_type'] ) );
419
-		}
420
-
421
-		return $wp_query;
422
-	}
423
-
424
-	/**
425
-	 * Register widgets
426
-	 *
427
-	 */
428
-	public function register_widgets() {
429
-		global $pagenow;
430
-
431
-		// Currently, UX Builder does not work particulaly well with SuperDuper.
432
-		// So we disable our widgets when editing a page with UX Builder.
433
-		if ( function_exists( 'ux_builder_is_active' ) && ux_builder_is_active() ) {
434
-			return;
435
-		}
436
-
437
-		$block_widget_init_screens = function_exists( 'sd_pagenow_exclude' ) ? sd_pagenow_exclude() : array();
438
-
439
-		if ( is_admin() && $pagenow && in_array( $pagenow, $block_widget_init_screens ) ) {
440
-			// don't initiate in these conditions.
441
-		} else {
442
-			// Only load allowed widgets.
443
-			$exclude = function_exists( 'sd_widget_exclude' ) ? sd_widget_exclude() : array();
444
-			$widgets = apply_filters(
445
-				'getpaid_widget_classes',
446
-				array(
447
-					'WPInv_Checkout_Widget',
448
-					'WPInv_History_Widget',
449
-					'WPInv_Receipt_Widget',
450
-					'WPInv_Subscriptions_Widget',
451
-					'WPInv_Buy_Item_Widget',
452
-					'WPInv_Messages_Widget',
453
-					'WPInv_GetPaid_Widget',
454
-					'WPInv_Invoice_Widget',
455
-				)
456
-			);
457
-
458
-			// For each widget...
459
-			foreach ( $widgets as $widget ) {
460
-				// Abort early if it is excluded for this page.
461
-				if ( in_array( $widget, $exclude ) ) {
462
-					continue;
463
-				}
464
-
465
-				// SD V1 used to extend the widget class. V2 does not, so we cannot call register widget on it.
466
-				if ( is_subclass_of( $widget, 'WP_Widget' ) ) {
467
-					register_widget( $widget );
468
-				} else {
469
-					new $widget();
470
-				}
471
-			}
472
-		}
473
-	}
474
-
475
-	/**
476
-	 * Upgrades the database.
477
-	 *
478
-	 * @since 2.0.2
479
-	 */
480
-	public function maybe_upgrade_database() {
481
-		// Ensure the database tables are up to date.
482
-		GetPaid_Installer::maybe_create_db_tables();
483
-
484
-		$wpi_version = get_option( 'wpinv_version', 0 );
485
-
486
-		if ( $wpi_version == WPINV_VERSION ) {
487
-			return;
488
-		}
489
-
490
-		$installer = new GetPaid_Installer();
491
-
492
-		if ( empty( $wpi_version ) ) {
493
-			return $installer->upgrade_db( 0 );
494
-		}
495
-
496
-		$upgrades  = array(
497
-			'0.0.5' => '004',
498
-			'1.0.3' => '102',
499
-			'2.0.0' => '118',
500
-			'2.8.0' => '279',
501
-		);
502
-
503
-		foreach ( $upgrades as $key => $method ) {
504
-			if ( version_compare( $wpi_version, $key, '<' ) ) {
505
-				return $installer->upgrade_db( $method );
506
-			}
507
-		}
508
-	}
509
-
510
-	/**
511
-	 * Flushes the permalinks if needed.
512
-	 *
513
-	 * @since 2.0.8
514
-	 */
515
-	public function maybe_flush_permalinks() {
516
-		$flush = get_option( 'wpinv_flush_permalinks', 0 );
517
-
518
-		if ( ! empty( $flush ) ) {
519
-			flush_rewrite_rules();
520
-			delete_option( 'wpinv_flush_permalinks' );
521
-		}
522
-	}
523
-
524
-	/**
525
-	 * Remove our pages from yoast sitemaps.
526
-	 *
527
-	 * @since 1.0.19
528
-	 * @param int[] $excluded_posts_ids
529
-	 */
530
-	public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ) {
531
-		// Ensure that we have an array.
532
-		if ( ! is_array( $excluded_posts_ids ) ) {
533
-			$excluded_posts_ids = array();
534
-		}
535
-
536
-		// Prepare our pages.
537
-		$our_pages = array();
538
-
539
-		// Checkout page.
540
-		$our_pages[] = wpinv_get_option( 'checkout_page', false );
541
-
542
-		// Success page.
543
-		$our_pages[] = wpinv_get_option( 'success_page', false );
544
-
545
-		// Failure page.
546
-		$our_pages[] = wpinv_get_option( 'failure_page', false );
547
-
548
-		// History page.
549
-		$our_pages[] = wpinv_get_option( 'invoice_history_page', false );
550
-
551
-		// Subscriptions page.
552
-		$our_pages[] = wpinv_get_option( 'invoice_subscription_page', false );
553
-
554
-		$our_pages   = array_map( 'intval', array_filter( $our_pages ) );
555
-
556
-		$excluded_posts_ids = $excluded_posts_ids + $our_pages;
557
-
558
-		return array_unique( $excluded_posts_ids );
559
-	}
560
-
561
-	/**
562
-	 * Remove our pages from yoast sitemaps.
563
-	 *
564
-	 * @since 1.0.19
565
-	 * @param string[] $post_types
566
-	 */
567
-	public function exclude_invoicing_post_types( $post_types ) {
568
-		// Ensure that we have an array.
569
-		if ( ! is_array( $post_types ) ) {
570
-			$post_types = array();
571
-		}
572
-
573
-		// Remove our post types.
574
-		return array_diff( $post_types, array_keys( getpaid_get_invoice_post_types() ) );
575
-	}
576
-
577
-	/**
578
-	 * Displays additional footer code.
579
-	 *
580
-	 * @since 2.0.0
581
-	 */
582
-	public function wp_footer() {
583
-		wpinv_get_template( 'frontend-footer.php' );
584
-	}
585
-
586
-	/**
587
-	 * Displays additional header code.
588
-	 *
589
-	 * @since 2.0.0
590
-	 */
591
-	public function wp_head() {
592
-		wpinv_get_template( 'frontend-head.php' );
593
-	}
594
-
595
-	/**
596
-	 * Custom query vars.
597
-	 *
598
-	 */
599
-	public function custom_query_vars( $vars ) {
600
-		$vars[] = 'getpaid-ipn';
601
-		return $vars;
602
-	}
603
-
604
-	/**
605
-	 * Add rewrite tags and rules.
606
-	 *
607
-	 */
608
-	public function add_rewrite_rule() {
609
-		$tag = 'getpaid-ipn';
610
-		add_rewrite_tag( "%$tag%", '([^&]+)' );
611
-		add_rewrite_rule( "^$tag/([^/]*)/?", "index.php?$tag=\$matches[1]", 'top' );
612
-	}
613
-
614
-	/**
615
-	 * Processes non-query string ipns.
616
-	 *
617
-	 */
618
-	public function maybe_process_new_ipn( $query ) {
619
-		if ( is_admin() || ! $query->is_main_query() ) {
620
-			return;
621
-		}
622
-
623
-		$gateway = get_query_var( 'getpaid-ipn' );
624
-
625
-		if ( ! empty( $gateway ) ) {
626
-			$gateway = sanitize_text_field( $gateway );
627
-			nocache_headers();
628
-			do_action( 'wpinv_verify_payment_ipn', $gateway );
629
-			do_action( "wpinv_verify_{$gateway}_ipn" );
630
-			exit;
631
-		}
632
-	}
17
+    /**
18
+     * GetPaid version.
19
+     *
20
+     * @var string
21
+     */
22
+    public $version;
23
+
24
+    /**
25
+     * Data container.
26
+     *
27
+     * @var array
28
+     */
29
+    protected $data = array();
30
+
31
+    /**
32
+     * Form elements instance.
33
+     *
34
+     * @var WPInv_Payment_Form_Elements
35
+     */
36
+    public $form_elements;
37
+
38
+    /**
39
+     * @var array An array of payment gateways.
40
+     */
41
+    public $gateways;
42
+
43
+    /**
44
+     * Class constructor.
45
+     */
46
+    public function __construct() {
47
+        $this->define_constants();
48
+        $this->includes();
49
+        $this->init_hooks();
50
+        $this->set_properties();
51
+    }
52
+
53
+    /**
54
+     * Sets a custom data property.
55
+     *
56
+     * @param string $prop The prop to set.
57
+     * @param mixed $value The value to retrieve.
58
+     */
59
+    public function set( $prop, $value ) {
60
+        $this->data[ $prop ] = $value;
61
+    }
62
+
63
+    /**
64
+     * Gets a custom data property.
65
+     *
66
+     * @param string $prop The prop to set.
67
+     * @return mixed The value.
68
+     */
69
+    public function get( $prop ) {
70
+        if ( isset( $this->data[ $prop ] ) ) {
71
+            return $this->data[ $prop ];
72
+        }
73
+
74
+        return null;
75
+    }
76
+
77
+    /**
78
+     * Define class properties.
79
+     */
80
+    public function set_properties() {
81
+        // Sessions.
82
+        $this->set( 'session', new WPInv_Session_Handler() );
83
+        $GLOBALS['wpi_session'] = $this->get( 'session' ); // Backwards compatibility.
84
+        $GLOBALS['wpinv_euvat'] = new WPInv_EUVat(); // Backwards compatibility.
85
+
86
+        // Init other objects.
87
+        $this->set( 'notes', new WPInv_Notes() );
88
+        $this->set( 'api', new WPInv_API() );
89
+        $this->set( 'post_types', new GetPaid_Post_Types() );
90
+        $this->set( 'template', new GetPaid_Template() );
91
+        $this->set( 'admin', new GetPaid_Admin() );
92
+        $this->set( 'subscriptions', new WPInv_Subscriptions() );
93
+        $this->set( 'invoice_emails', new GetPaid_Invoice_Notification_Emails() );
94
+        $this->set( 'subscription_emails', new GetPaid_Subscription_Notification_Emails() );
95
+        $this->set( 'daily_maintenace', new GetPaid_Daily_Maintenance() );
96
+        $this->set( 'payment_forms', new GetPaid_Payment_Forms() );
97
+        $this->set( 'maxmind', new GetPaid_MaxMind_Geolocation() );
98
+        $this->set( 'data_retention', new WPInv_Data_Retention() );
99
+    }
100
+
101
+        /**
102
+         * Define plugin constants.
103
+         */
104
+    public function define_constants() {
105
+        define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) );
106
+        define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) );
107
+        $this->version = WPINV_VERSION;
108
+    }
109
+
110
+    /**
111
+     * Hook into actions and filters.
112
+     *
113
+     * @since 1.0.19
114
+     */
115
+    protected function init_hooks() {
116
+        /* Internationalize the text strings used. */
117
+        add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
118
+
119
+        // Init the plugin after WordPress inits.
120
+        add_action( 'init', array( $this, 'init' ), 1 );
121
+        add_action( 'init', array( $this, 'maybe_process_ipn' ), 100 );
122
+        add_action( 'init', array( $this, 'wpinv_actions' ) );
123
+        add_action( 'init', array( $this, 'maybe_do_authenticated_action' ), 100 );
124
+        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 11 );
125
+        add_action( 'wp_footer', array( $this, 'wp_footer' ) );
126
+        add_action( 'wp_head', array( $this, 'wp_head' ) );
127
+        add_action( 'widgets_init', array( $this, 'register_widgets' ) );
128
+        add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) );
129
+        add_filter( 'the_seo_framework_sitemap_supported_post_types', array( $this, 'exclude_invoicing_post_types' ) );
130
+        add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
131
+
132
+        add_filter( 'query_vars', array( $this, 'custom_query_vars' ) );
133
+        add_action( 'init', array( $this, 'add_rewrite_rule' ), 10, 0 );
134
+        add_action( 'pre_get_posts', array( $this, 'maybe_process_new_ipn' ), 1 );
135
+
136
+        // Fires after registering actions.
137
+        do_action( 'wpinv_actions', $this );
138
+        do_action( 'getpaid_actions', $this );
139
+    }
140
+
141
+    public function plugins_loaded() {
142
+        /* Internationalize the text strings used. */
143
+        $this->load_textdomain();
144
+
145
+        do_action( 'wpinv_loaded' );
146
+
147
+        // Fix oxygen page builder conflict
148
+        if ( function_exists( 'ct_css_output' ) ) {
149
+            wpinv_oxygen_fix_conflict();
150
+        }
151
+    }
152
+
153
+    /**
154
+     * Load Localisation files.
155
+     *
156
+     * Note: the first-loaded translation file overrides any following ones if the same translation is present.
157
+     *
158
+     * Locales found in:
159
+     *      - WP_LANG_DIR/plugins/invoicing-LOCALE.mo
160
+     *      - WP_PLUGIN_DIR/invoicing/languages/invoicing-LOCALE.mo
161
+     *
162
+     * @since 1.0.0
163
+     */
164
+    public function load_textdomain() {
165
+        // Determines the current locale.
166
+        if ( function_exists( 'determine_locale' ) ) {
167
+            $locale = determine_locale();
168
+        } else if ( function_exists( 'get_user_locale' ) ) {
169
+            $locale = get_user_locale();
170
+        } else {
171
+            $locale = get_locale();
172
+        }
173
+
174
+        /**
175
+         * Filter the locale to use for translations.
176
+         */
177
+        $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' );
178
+
179
+        unload_textdomain( 'invoicing', true );
180
+        load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' );
181
+        load_plugin_textdomain( 'invoicing', false, plugin_basename( dirname( WPINV_PLUGIN_FILE ) ) . '/languages/' );
182
+    }
183
+
184
+    /**
185
+     * Include required core files used in admin and on the frontend.
186
+     */
187
+    public function includes() {
188
+        // Start with the settings.
189
+        require_once WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php';
190
+
191
+        // Packages/libraries.
192
+        require_once WPINV_PLUGIN_DIR . 'vendor/autoload.php';
193
+        require_once WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php';
194
+
195
+        // Load functions.
196
+        require_once WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php';
197
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php';
198
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php';
199
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php';
200
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php';
201
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php';
202
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php';
203
+        require_once WPINV_PLUGIN_DIR . 'includes/invoice-functions.php';
204
+        require_once WPINV_PLUGIN_DIR . 'includes/subscription-functions.php';
205
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php';
206
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php';
207
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php';
208
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php';
209
+        require_once WPINV_PLUGIN_DIR . 'includes/user-functions.php';
210
+        require_once WPINV_PLUGIN_DIR . 'includes/error-functions.php';
211
+
212
+        // Register autoloader.
213
+        try {
214
+            spl_autoload_register( array( $this, 'autoload' ), true );
215
+        } catch ( Exception $e ) {
216
+            wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
217
+        }
218
+
219
+        require_once WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php';
220
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php';
221
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php';
222
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php';
223
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php';
224
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php';
225
+        require_once WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php';
226
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php';
227
+        require_once WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php';
228
+        require_once WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php';
229
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php';
230
+        require_once WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php';
231
+        require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php';
232
+        require_once WPINV_PLUGIN_DIR . 'widgets/checkout.php';
233
+        require_once WPINV_PLUGIN_DIR . 'widgets/invoice-history.php';
234
+        require_once WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php';
235
+        require_once WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php';
236
+        require_once WPINV_PLUGIN_DIR . 'widgets/subscriptions.php';
237
+        require_once WPINV_PLUGIN_DIR . 'widgets/buy-item.php';
238
+        require_once WPINV_PLUGIN_DIR . 'widgets/getpaid.php';
239
+        require_once WPINV_PLUGIN_DIR . 'widgets/invoice.php';
240
+        require_once WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php';
241
+
242
+        if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
243
+            GetPaid_Post_Types_Admin::init();
244
+
245
+            require_once WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php';
246
+            require_once WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php';
247
+            require_once WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php';
248
+            require_once WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php';
249
+            require_once WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php';
250
+            require_once WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php';
251
+            // load the user class only on the users.php page
252
+            global $pagenow;
253
+            if ( $pagenow == 'users.php' ) {
254
+                new WPInv_Admin_Users();
255
+            }
256
+        }
257
+
258
+        // Register cli commands
259
+        if ( defined( 'WP_CLI' ) && WP_CLI ) {
260
+            require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php';
261
+            WP_CLI::add_command( 'invoicing', 'WPInv_CLI' );
262
+        }
263
+    }
264
+
265
+    /**
266
+     * Class autoloader
267
+     *
268
+     * @param       string $class_name The name of the class to load.
269
+     * @access      public
270
+     * @since       1.0.19
271
+     * @return      void
272
+     */
273
+    public function autoload( $class_name ) {
274
+        // Normalize the class name...
275
+        $class_name  = strtolower( $class_name );
276
+
277
+        // ... and make sure it is our class.
278
+        if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
279
+            return;
280
+        }
281
+
282
+        // Next, prepare the file name from the class.
283
+        $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
284
+
285
+        // Base path of the classes.
286
+        $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
287
+
288
+        // And an array of possible locations in order of importance.
289
+        $locations = array(
290
+            "$plugin_path/includes",
291
+            "$plugin_path/includes/data-stores",
292
+            "$plugin_path/includes/gateways",
293
+            "$plugin_path/includes/payments",
294
+            "$plugin_path/includes/geolocation",
295
+            "$plugin_path/includes/reports",
296
+            "$plugin_path/includes/api",
297
+            "$plugin_path/includes/admin",
298
+            "$plugin_path/includes/admin/meta-boxes",
299
+        );
300
+
301
+        foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) {
302
+            if ( file_exists( trailingslashit( $location ) . $file_name ) ) {
303
+                include trailingslashit( $location ) . $file_name;
304
+                break;
305
+            }
306
+        }
307
+    }
308
+
309
+    /**
310
+     * Inits hooks etc.
311
+     */
312
+    public function init() {
313
+        // Fires before getpaid inits.
314
+        do_action( 'before_getpaid_init', $this );
315
+
316
+        // Maybe upgrade.
317
+        $this->maybe_upgrade_database();
318
+
319
+        // Load default gateways.
320
+        $gateways = apply_filters(
321
+            'getpaid_default_gateways',
322
+            array(
323
+                'manual'        => 'GetPaid_Manual_Gateway',
324
+                'paypal'        => 'GetPaid_Paypal_Gateway',
325
+                'worldpay'      => 'GetPaid_Worldpay_Gateway',
326
+                'bank_transfer' => 'GetPaid_Bank_Transfer_Gateway',
327
+                'authorizenet'  => 'GetPaid_Authorize_Net_Gateway',
328
+            )
329
+        );
330
+
331
+        foreach ( $gateways as $id => $class ) {
332
+            $this->gateways[ $id ] = new $class();
333
+        }
334
+
335
+        if ( 'yes' != get_option( 'wpinv_renamed_gateways' ) ) {
336
+            GetPaid_Installer::rename_gateways_label();
337
+            update_option( 'wpinv_renamed_gateways', 'yes' );
338
+        }
339
+
340
+        // Fires after getpaid inits.
341
+        do_action( 'getpaid_init', $this );
342
+    }
343
+
344
+    /**
345
+     * Checks if this is an IPN request and processes it.
346
+     */
347
+    public function maybe_process_ipn() {
348
+        // Ensure that this is an IPN request.
349
+        if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) {
350
+            return;
351
+        }
352
+
353
+        $gateway = sanitize_text_field( $_GET['wpi-gateway'] );
354
+
355
+        do_action( 'wpinv_verify_payment_ipn', $gateway );
356
+        do_action( "wpinv_verify_{$gateway}_ipn" );
357
+        exit;
358
+    }
359
+
360
+    public function enqueue_scripts() {
361
+        // Fires before adding scripts.
362
+        do_action( 'getpaid_enqueue_scripts' );
363
+
364
+        $localize                         = array();
365
+        $localize['ajax_url']             = admin_url( 'admin-ajax.php' );
366
+        $localize['thousands']            = wpinv_thousands_separator();
367
+        $localize['decimals']             = wpinv_decimal_separator();
368
+        $localize['nonce']                = wp_create_nonce( 'wpinv-nonce' );
369
+        $localize['txtComplete']          = __( 'Continue', 'invoicing' );
370
+        $localize['UseTaxes']             = wpinv_use_taxes();
371
+        $localize['formNonce']            = wp_create_nonce( 'getpaid_form_nonce' );
372
+        $localize['loading']              = __( 'Loading...', 'invoicing' );
373
+        $localize['connectionError']      = __( 'Could not establish a connection to the server.', 'invoicing' );
374
+        $localize['recaptchaSettings']    = getpaid_get_recaptcha_settings();
375
+
376
+        $localize = apply_filters( 'wpinv_front_js_localize', $localize );
377
+
378
+        // reCaptcha.
379
+        if ( getpaid_is_recaptcha_enabled() && ( $recaptcha_js = getpaid_recaptcha_api_url() ) ) {
380
+            wp_enqueue_script( 'recaptcha', $recaptcha_js, array(), null, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
381
+        }
382
+
383
+        wp_enqueue_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.min.js', array( 'jquery' ), WPINV_VERSION, true );
384
+        wp_localize_script( 'wpinv-front-script', 'WPInv', $localize );
385
+    }
386
+
387
+    public function wpinv_actions() {
388
+        if ( isset( $_REQUEST['wpi_action'] ) ) {
389
+            do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST );
390
+        }
391
+
392
+        if ( defined( 'WP_ALL_IMPORT_ROOT_DIR' ) ) {
393
+            include plugin_dir_path( __FILE__ ) . 'libraries/wp-all-import/class-getpaid-wp-all-import.php';
394
+        }
395
+    }
396
+
397
+    /**
398
+     * Fires an action after verifying that a user can fire them.
399
+     *
400
+     * Note: If the action is on an invoice, subscription etc, esure that the
401
+     * current user owns the invoice/subscription.
402
+     */
403
+    public function maybe_do_authenticated_action() {
404
+        if ( isset( $_REQUEST['getpaid-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
405
+            $key  = sanitize_key( $_REQUEST['getpaid-action'] );
406
+            $data = wp_unslash( $_REQUEST );
407
+
408
+            if ( is_user_logged_in() ) {
409
+                do_action( "getpaid_authenticated_action_$key", $data );
410
+            }
411
+
412
+            do_action( "getpaid_unauthenticated_action_$key", $data );
413
+        }
414
+    }
415
+
416
+    public function pre_get_posts( $wp_query ) {
417
+        if ( ! is_admin() && ! empty( $wp_query->query_vars['post_type'] ) && getpaid_is_invoice_post_type( $wp_query->query_vars['post_type'] ) && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) {
418
+            $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses( false, false, $wp_query->query_vars['post_type'] ) );
419
+        }
420
+
421
+        return $wp_query;
422
+    }
423
+
424
+    /**
425
+     * Register widgets
426
+     *
427
+     */
428
+    public function register_widgets() {
429
+        global $pagenow;
430
+
431
+        // Currently, UX Builder does not work particulaly well with SuperDuper.
432
+        // So we disable our widgets when editing a page with UX Builder.
433
+        if ( function_exists( 'ux_builder_is_active' ) && ux_builder_is_active() ) {
434
+            return;
435
+        }
436
+
437
+        $block_widget_init_screens = function_exists( 'sd_pagenow_exclude' ) ? sd_pagenow_exclude() : array();
438
+
439
+        if ( is_admin() && $pagenow && in_array( $pagenow, $block_widget_init_screens ) ) {
440
+            // don't initiate in these conditions.
441
+        } else {
442
+            // Only load allowed widgets.
443
+            $exclude = function_exists( 'sd_widget_exclude' ) ? sd_widget_exclude() : array();
444
+            $widgets = apply_filters(
445
+                'getpaid_widget_classes',
446
+                array(
447
+                    'WPInv_Checkout_Widget',
448
+                    'WPInv_History_Widget',
449
+                    'WPInv_Receipt_Widget',
450
+                    'WPInv_Subscriptions_Widget',
451
+                    'WPInv_Buy_Item_Widget',
452
+                    'WPInv_Messages_Widget',
453
+                    'WPInv_GetPaid_Widget',
454
+                    'WPInv_Invoice_Widget',
455
+                )
456
+            );
457
+
458
+            // For each widget...
459
+            foreach ( $widgets as $widget ) {
460
+                // Abort early if it is excluded for this page.
461
+                if ( in_array( $widget, $exclude ) ) {
462
+                    continue;
463
+                }
464
+
465
+                // SD V1 used to extend the widget class. V2 does not, so we cannot call register widget on it.
466
+                if ( is_subclass_of( $widget, 'WP_Widget' ) ) {
467
+                    register_widget( $widget );
468
+                } else {
469
+                    new $widget();
470
+                }
471
+            }
472
+        }
473
+    }
474
+
475
+    /**
476
+     * Upgrades the database.
477
+     *
478
+     * @since 2.0.2
479
+     */
480
+    public function maybe_upgrade_database() {
481
+        // Ensure the database tables are up to date.
482
+        GetPaid_Installer::maybe_create_db_tables();
483
+
484
+        $wpi_version = get_option( 'wpinv_version', 0 );
485
+
486
+        if ( $wpi_version == WPINV_VERSION ) {
487
+            return;
488
+        }
489
+
490
+        $installer = new GetPaid_Installer();
491
+
492
+        if ( empty( $wpi_version ) ) {
493
+            return $installer->upgrade_db( 0 );
494
+        }
495
+
496
+        $upgrades  = array(
497
+            '0.0.5' => '004',
498
+            '1.0.3' => '102',
499
+            '2.0.0' => '118',
500
+            '2.8.0' => '279',
501
+        );
502
+
503
+        foreach ( $upgrades as $key => $method ) {
504
+            if ( version_compare( $wpi_version, $key, '<' ) ) {
505
+                return $installer->upgrade_db( $method );
506
+            }
507
+        }
508
+    }
509
+
510
+    /**
511
+     * Flushes the permalinks if needed.
512
+     *
513
+     * @since 2.0.8
514
+     */
515
+    public function maybe_flush_permalinks() {
516
+        $flush = get_option( 'wpinv_flush_permalinks', 0 );
517
+
518
+        if ( ! empty( $flush ) ) {
519
+            flush_rewrite_rules();
520
+            delete_option( 'wpinv_flush_permalinks' );
521
+        }
522
+    }
523
+
524
+    /**
525
+     * Remove our pages from yoast sitemaps.
526
+     *
527
+     * @since 1.0.19
528
+     * @param int[] $excluded_posts_ids
529
+     */
530
+    public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ) {
531
+        // Ensure that we have an array.
532
+        if ( ! is_array( $excluded_posts_ids ) ) {
533
+            $excluded_posts_ids = array();
534
+        }
535
+
536
+        // Prepare our pages.
537
+        $our_pages = array();
538
+
539
+        // Checkout page.
540
+        $our_pages[] = wpinv_get_option( 'checkout_page', false );
541
+
542
+        // Success page.
543
+        $our_pages[] = wpinv_get_option( 'success_page', false );
544
+
545
+        // Failure page.
546
+        $our_pages[] = wpinv_get_option( 'failure_page', false );
547
+
548
+        // History page.
549
+        $our_pages[] = wpinv_get_option( 'invoice_history_page', false );
550
+
551
+        // Subscriptions page.
552
+        $our_pages[] = wpinv_get_option( 'invoice_subscription_page', false );
553
+
554
+        $our_pages   = array_map( 'intval', array_filter( $our_pages ) );
555
+
556
+        $excluded_posts_ids = $excluded_posts_ids + $our_pages;
557
+
558
+        return array_unique( $excluded_posts_ids );
559
+    }
560
+
561
+    /**
562
+     * Remove our pages from yoast sitemaps.
563
+     *
564
+     * @since 1.0.19
565
+     * @param string[] $post_types
566
+     */
567
+    public function exclude_invoicing_post_types( $post_types ) {
568
+        // Ensure that we have an array.
569
+        if ( ! is_array( $post_types ) ) {
570
+            $post_types = array();
571
+        }
572
+
573
+        // Remove our post types.
574
+        return array_diff( $post_types, array_keys( getpaid_get_invoice_post_types() ) );
575
+    }
576
+
577
+    /**
578
+     * Displays additional footer code.
579
+     *
580
+     * @since 2.0.0
581
+     */
582
+    public function wp_footer() {
583
+        wpinv_get_template( 'frontend-footer.php' );
584
+    }
585
+
586
+    /**
587
+     * Displays additional header code.
588
+     *
589
+     * @since 2.0.0
590
+     */
591
+    public function wp_head() {
592
+        wpinv_get_template( 'frontend-head.php' );
593
+    }
594
+
595
+    /**
596
+     * Custom query vars.
597
+     *
598
+     */
599
+    public function custom_query_vars( $vars ) {
600
+        $vars[] = 'getpaid-ipn';
601
+        return $vars;
602
+    }
603
+
604
+    /**
605
+     * Add rewrite tags and rules.
606
+     *
607
+     */
608
+    public function add_rewrite_rule() {
609
+        $tag = 'getpaid-ipn';
610
+        add_rewrite_tag( "%$tag%", '([^&]+)' );
611
+        add_rewrite_rule( "^$tag/([^/]*)/?", "index.php?$tag=\$matches[1]", 'top' );
612
+    }
613
+
614
+    /**
615
+     * Processes non-query string ipns.
616
+     *
617
+     */
618
+    public function maybe_process_new_ipn( $query ) {
619
+        if ( is_admin() || ! $query->is_main_query() ) {
620
+            return;
621
+        }
622
+
623
+        $gateway = get_query_var( 'getpaid-ipn' );
624
+
625
+        if ( ! empty( $gateway ) ) {
626
+            $gateway = sanitize_text_field( $gateway );
627
+            nocache_headers();
628
+            do_action( 'wpinv_verify_payment_ipn', $gateway );
629
+            do_action( "wpinv_verify_{$gateway}_ipn" );
630
+            exit;
631
+        }
632
+    }
633 633
 }
Please login to merge, or discard this patch.
includes/admin/class-getpaid-admin-setup-wizard.php 1 patch
Indentation   +388 added lines, -388 removed lines patch added patch discarded remove patch
@@ -17,402 +17,402 @@
 block discarded – undo
17 17
  */
18 18
 class GetPaid_Admin_Setup_Wizard {
19 19
 
20
-	/**
21
-	 * @var string Current Step
22
-	 */
23
-	protected $step = '';
24
-
25
-	/**
26
-	 * @var string|false Previous Step
27
-	 */
28
-	protected $previous_step = '';
29
-
30
-	/**
31
-	 * @var string|false Next Step
32
-	 */
33
-	protected $next_step = '';
34
-
35
-	/**
36
-	 * @var array All available steps for the setup wizard
37
-	 */
38
-	protected $steps = array();
39
-
40
-	/**
41
-	 * Class constructor.
42
-	 *
43
-	 * @since 2.4.0
44
-	 */
45
-	public function __construct() {
46
-
47
-		if ( apply_filters( 'getpaid_enable_setup_wizard', true ) && wpinv_current_user_can_manage_invoicing() ) {
48
-			add_action( 'admin_menu', array( $this, 'add_menu' ) );
49
-			add_action( 'current_screen', array( $this, 'setup_wizard' ) );
50
-			add_action( 'admin_init', array( $this, 'remove_deprecated_functions' ) );
51
-		}
52
-
53
-	}
54
-
55
-	/**
56
-	 * Add admin menus/screens.
57
-	 *
58
-	 * @since 2.4.0
59
-	 */
60
-	public function add_menu() {
61
-		add_dashboard_page( '', '', wpinv_get_capability(), 'gp-setup', '' );
62
-	}
63
-
64
-	/**
65
-	 * Sets up the setup wizard.
66
-	 *
67
-	 * @since 2.4.0
68
-	 */
69
-	public function setup_wizard() {
70
-
71
-		if ( isset( $_GET['page'] ) && 'gp-setup' === $_GET['page'] ) {
72
-			$this->setup_globals();
73
-			$this->maybe_save_current_step();
74
-			$this->display_wizard();
75
-			exit;
76
-		}
77
-
78
-	}
79
-
80
-	public function remove_deprecated_functions() {
81
-		// removes deprecated warnings from page
82
-		remove_action('admin_print_styles', 'print_emoji_styles');
83
-		remove_action( 'admin_head', 'wp_admin_bar_header' );
84
-	}
85
-
86
-	/**
87
-	 * Sets up class variables.
88
-	 *
89
-	 * @since 2.4.0
90
-	 */
91
-	protected function setup_globals() {
92
-		$this->steps         = $this->get_setup_steps();
93
-		$this->step          = $this->get_current_step();
94
-		$this->previous_step = $this->get_previous_step();
95
-		$this->next_step     = $this->get_next_step();
96
-	}
97
-
98
-	/**
99
-	 * Saves the current step.
100
-	 *
101
-	 * @since 2.4.0
102
-	 */
103
-	protected function maybe_save_current_step() {
104
-		if ( ! empty( $_POST['save_step'] ) && is_callable( $this->steps[ $this->step ]['handler'] ) ) {
105
-			call_user_func( $this->steps[ $this->step ]['handler'], $this );
106
-		}
107
-	}
108
-
109
-	/**
110
-	 * Returns the setup steps.
111
-	 *
112
-	 * @since 2.4.0
113
-	 * @return array
114
-	 */
115
-	protected function get_setup_steps() {
116
-
117
-		$steps = array(
118
-
119
-			'introduction'     => array(
120
-				'name'    => __( 'Introduction', 'invoicing' ),
121
-				'view'    => array( $this, 'setup_introduction' ),
122
-				'handler' => '',
123
-			),
124
-
125
-			'business_details' => array(
126
-				'name'    => __( 'Business Details', 'invoicing' ),
127
-				'view'    => array( $this, 'setup_business' ),
128
-				'handler' => '',
129
-			),
130
-
131
-			'currency'         => array(
132
-				'name'    => __( 'Currency', 'invoicing' ),
133
-				'view'    => array( $this, 'setup_currency' ),
134
-				'handler' => '',
135
-			),
136
-
137
-			'payments'         => array(
138
-				'name'    => __( 'Payment Gateways', 'invoicing' ),
139
-				'view'    => array( $this, 'setup_payments' ),
140
-				'handler' => array( $this, 'setup_payments_save' ),
141
-			),
142
-
143
-			'recommend'        => array(
144
-				'name'    => __( 'Recommend', 'invoicing' ),
145
-				'view'    => array( $this, 'setup_recommend' ),
146
-				'handler' => '',
147
-			),
148
-
149
-			'next_steps'       => array(
150
-				'name'    => __( 'Get Paid', 'invoicing' ),
151
-				'view'    => array( $this, 'setup_ready' ),
152
-				'handler' => '',
153
-			),
154
-
155
-		);
156
-
157
-		return apply_filters( 'getpaid_setup_wizard_steps', $steps );
158
-
159
-	}
160
-
161
-	/**
162
-	 * Returns the current step.
163
-	 *
164
-	 * @since 2.4.0
165
-	 * @return string
166
-	 */
167
-	protected function get_current_step() {
168
-		$step = isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : '';
169
-		return ! empty( $step ) && in_array( $step, array_keys( $this->steps ) ) ? $step : current( array_keys( $this->steps ) );
170
-	}
171
-
172
-	/**
173
-	 * Returns the previous step.
174
-	 *
175
-	 * @since 2.4.0
176
-	 * @return string|false
177
-	 */
178
-	protected function get_previous_step() {
179
-
180
-		$previous = false;
181
-		$current  = $this->step;
182
-		foreach ( array_keys( $this->steps ) as $step ) {
183
-			if ( $current === $step ) {
184
-				return $previous;
185
-			}
186
-
187
-			$previous = $step;
188
-		}
189
-
190
-		return false;
191
-	}
192
-
193
-	/**
194
-	 * Returns the next step.
195
-	 *
196
-	 * @since 2.4.0
197
-	 * @return string|false
198
-	 */
199
-	protected function get_next_step() {
200
-
201
-		$on_current = false;
202
-		$current    = $this->step;
203
-		foreach ( array_keys( $this->steps ) as $step ) {
204
-
205
-			if ( $on_current ) {
206
-				return $step;
207
-			}
208
-
209
-			if ( $current === $step ) {
210
-				return $on_current = true;
211
-			}
20
+    /**
21
+     * @var string Current Step
22
+     */
23
+    protected $step = '';
24
+
25
+    /**
26
+     * @var string|false Previous Step
27
+     */
28
+    protected $previous_step = '';
29
+
30
+    /**
31
+     * @var string|false Next Step
32
+     */
33
+    protected $next_step = '';
34
+
35
+    /**
36
+     * @var array All available steps for the setup wizard
37
+     */
38
+    protected $steps = array();
39
+
40
+    /**
41
+     * Class constructor.
42
+     *
43
+     * @since 2.4.0
44
+     */
45
+    public function __construct() {
46
+
47
+        if ( apply_filters( 'getpaid_enable_setup_wizard', true ) && wpinv_current_user_can_manage_invoicing() ) {
48
+            add_action( 'admin_menu', array( $this, 'add_menu' ) );
49
+            add_action( 'current_screen', array( $this, 'setup_wizard' ) );
50
+            add_action( 'admin_init', array( $this, 'remove_deprecated_functions' ) );
51
+        }
52
+
53
+    }
54
+
55
+    /**
56
+     * Add admin menus/screens.
57
+     *
58
+     * @since 2.4.0
59
+     */
60
+    public function add_menu() {
61
+        add_dashboard_page( '', '', wpinv_get_capability(), 'gp-setup', '' );
62
+    }
63
+
64
+    /**
65
+     * Sets up the setup wizard.
66
+     *
67
+     * @since 2.4.0
68
+     */
69
+    public function setup_wizard() {
70
+
71
+        if ( isset( $_GET['page'] ) && 'gp-setup' === $_GET['page'] ) {
72
+            $this->setup_globals();
73
+            $this->maybe_save_current_step();
74
+            $this->display_wizard();
75
+            exit;
76
+        }
77
+
78
+    }
79
+
80
+    public function remove_deprecated_functions() {
81
+        // removes deprecated warnings from page
82
+        remove_action('admin_print_styles', 'print_emoji_styles');
83
+        remove_action( 'admin_head', 'wp_admin_bar_header' );
84
+    }
85
+
86
+    /**
87
+     * Sets up class variables.
88
+     *
89
+     * @since 2.4.0
90
+     */
91
+    protected function setup_globals() {
92
+        $this->steps         = $this->get_setup_steps();
93
+        $this->step          = $this->get_current_step();
94
+        $this->previous_step = $this->get_previous_step();
95
+        $this->next_step     = $this->get_next_step();
96
+    }
97
+
98
+    /**
99
+     * Saves the current step.
100
+     *
101
+     * @since 2.4.0
102
+     */
103
+    protected function maybe_save_current_step() {
104
+        if ( ! empty( $_POST['save_step'] ) && is_callable( $this->steps[ $this->step ]['handler'] ) ) {
105
+            call_user_func( $this->steps[ $this->step ]['handler'], $this );
106
+        }
107
+    }
108
+
109
+    /**
110
+     * Returns the setup steps.
111
+     *
112
+     * @since 2.4.0
113
+     * @return array
114
+     */
115
+    protected function get_setup_steps() {
116
+
117
+        $steps = array(
118
+
119
+            'introduction'     => array(
120
+                'name'    => __( 'Introduction', 'invoicing' ),
121
+                'view'    => array( $this, 'setup_introduction' ),
122
+                'handler' => '',
123
+            ),
124
+
125
+            'business_details' => array(
126
+                'name'    => __( 'Business Details', 'invoicing' ),
127
+                'view'    => array( $this, 'setup_business' ),
128
+                'handler' => '',
129
+            ),
130
+
131
+            'currency'         => array(
132
+                'name'    => __( 'Currency', 'invoicing' ),
133
+                'view'    => array( $this, 'setup_currency' ),
134
+                'handler' => '',
135
+            ),
136
+
137
+            'payments'         => array(
138
+                'name'    => __( 'Payment Gateways', 'invoicing' ),
139
+                'view'    => array( $this, 'setup_payments' ),
140
+                'handler' => array( $this, 'setup_payments_save' ),
141
+            ),
142
+
143
+            'recommend'        => array(
144
+                'name'    => __( 'Recommend', 'invoicing' ),
145
+                'view'    => array( $this, 'setup_recommend' ),
146
+                'handler' => '',
147
+            ),
148
+
149
+            'next_steps'       => array(
150
+                'name'    => __( 'Get Paid', 'invoicing' ),
151
+                'view'    => array( $this, 'setup_ready' ),
152
+                'handler' => '',
153
+            ),
154
+
155
+        );
156
+
157
+        return apply_filters( 'getpaid_setup_wizard_steps', $steps );
158
+
159
+    }
160
+
161
+    /**
162
+     * Returns the current step.
163
+     *
164
+     * @since 2.4.0
165
+     * @return string
166
+     */
167
+    protected function get_current_step() {
168
+        $step = isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : '';
169
+        return ! empty( $step ) && in_array( $step, array_keys( $this->steps ) ) ? $step : current( array_keys( $this->steps ) );
170
+    }
171
+
172
+    /**
173
+     * Returns the previous step.
174
+     *
175
+     * @since 2.4.0
176
+     * @return string|false
177
+     */
178
+    protected function get_previous_step() {
179
+
180
+        $previous = false;
181
+        $current  = $this->step;
182
+        foreach ( array_keys( $this->steps ) as $step ) {
183
+            if ( $current === $step ) {
184
+                return $previous;
185
+            }
186
+
187
+            $previous = $step;
188
+        }
189
+
190
+        return false;
191
+    }
192
+
193
+    /**
194
+     * Returns the next step.
195
+     *
196
+     * @since 2.4.0
197
+     * @return string|false
198
+     */
199
+    protected function get_next_step() {
200
+
201
+        $on_current = false;
202
+        $current    = $this->step;
203
+        foreach ( array_keys( $this->steps ) as $step ) {
204
+
205
+            if ( $on_current ) {
206
+                return $step;
207
+            }
208
+
209
+            if ( $current === $step ) {
210
+                return $on_current = true;
211
+            }
212 212
 }
213 213
 
214
-		return false;
215
-	}
216
-
217
-	/**
218
-	 * Displays the setup wizard.
219
-	 *
220
-	 * @since 2.4.0
221
-	 */
222
-	public function display_wizard() {
223
-		$this->display_header();
224
-		$this->display_current_step();
225
-		$this->display_footer();
226
-	}
227
-
228
-	/**
229
-	 * Displays the Wizard Header.
230
-	 *
231
-	 * @since 2.0.0
232
-	 */
233
-	public function display_header() {
234
-		$steps     = $this->steps;
235
-		$current   = $this->step;
236
-		$next_step = $this->next_step;
237
-		array_shift( $steps );
238
-		include plugin_dir_path( __FILE__ ) . 'views/wizard-header.php';
239
-	}
240
-
241
-	/**
242
-	 * Displays the content for the current step.
243
-	 *
244
-	 * @since 2.4.0
245
-	 */
246
-	public function display_current_step() {
247
-		?>
214
+        return false;
215
+    }
216
+
217
+    /**
218
+     * Displays the setup wizard.
219
+     *
220
+     * @since 2.4.0
221
+     */
222
+    public function display_wizard() {
223
+        $this->display_header();
224
+        $this->display_current_step();
225
+        $this->display_footer();
226
+    }
227
+
228
+    /**
229
+     * Displays the Wizard Header.
230
+     *
231
+     * @since 2.0.0
232
+     */
233
+    public function display_header() {
234
+        $steps     = $this->steps;
235
+        $current   = $this->step;
236
+        $next_step = $this->next_step;
237
+        array_shift( $steps );
238
+        include plugin_dir_path( __FILE__ ) . 'views/wizard-header.php';
239
+    }
240
+
241
+    /**
242
+     * Displays the content for the current step.
243
+     *
244
+     * @since 2.4.0
245
+     */
246
+    public function display_current_step() {
247
+        ?>
248 248
 			<div class="gp-setup-content rowx mw-100 text-center mb-3">
249 249
 				<div class="col-12 col-md-5 m-auto">
250 250
 					<?php call_user_func( $this->steps[ $this->step ]['view'], $this ); ?>
251 251
 				</div>
252 252
 			</div>
253 253
 		<?php
254
-	}
255
-
256
-	/**
257
-	 * Setup Wizard Footer.
258
-	 *
259
-	 * @since 2.4.0
260
-	 */
261
-	public function display_footer() {
262
-
263
-		if ( isset( $_GET['step'] ) ) {
264
-			$label    = $this->step == 'next_steps' ? __( 'Return to the WordPress Dashboard', 'invoicing' ) : __( 'Skip this step', 'invoicing' );
265
-
266
-			echo '<p class="gd-return-to-dashboard-wrap"> <a href="' . esc_url( $this->get_next_step_link() ) . '" class="gd-return-to-dashboard btn btn-link d-block text-muted">' . esc_html( $label ) . '</a></p>';
267
-		}
268
-
269
-		echo '</body></html>';
270
-	}
271
-
272
-	/**
273
-	 * Introduction step.
274
-	 *
275
-	 * @since 2.0.0
276
-	 */
277
-	public function setup_introduction() {
278
-		$next_url = $this->get_next_step_link();
279
-		include plugin_dir_path( __FILE__ ) . 'views/wizard-introduction.php';
280
-	}
281
-
282
-	/**
283
-	 * Get the URL for the next step's screen.
284
-	 *
285
-	 * @param string step   slug (default: current step)
286
-	 *
287
-	 * @return string       URL for next step if a next step exists.
288
-	 *                      Admin URL if it's the last step.
289
-	 *                      Empty string on failure.
290
-	 * @since 3.0.0
291
-	 */
292
-	public function get_next_step_link( $step = '' ) {
293
-		if ( ! $step ) {
294
-			$step = $this->step;
295
-		}
296
-
297
-		$keys = array_keys( $this->steps );
298
-		if ( end( $keys ) === $step ) {
299
-			return admin_url();
300
-		}
301
-
302
-		$step_index = array_search( $step, $keys );
303
-		if ( false === $step_index ) {
304
-			return '';
305
-		}
306
-
307
-		return remove_query_arg( 'settings-updated', add_query_arg( 'step', $keys[ $step_index + 1 ] ) );
308
-	}
309
-
310
-	/**
311
-	 * Setup maps api.
312
-	 *
313
-	 * @since 2.0.0
314
-	 */
315
-	public function setup_business() {
316
-		$next_url = $this->get_next_step_link();
317
-		$wizard   = $this;
318
-		$page     = 'wpinv_settings_general_main';
319
-		$section  = 'wpinv_settings_general_main';
320
-		include plugin_dir_path( __FILE__ ) . 'views/wizard-settings.php';
321
-	}
322
-
323
-	/**
324
-	 * Default Location settings.
325
-	 *
326
-	 * @since 2.0.0
327
-	 */
328
-	public function setup_currency() {
329
-		$next_url = $this->get_next_step_link();
330
-		$wizard   = $this;
331
-		$page     = 'wpinv_settings_general_currency_section';
332
-		$section  = 'wpinv_settings_general_currency_section';
333
-		include plugin_dir_path( __FILE__ ) . 'views/wizard-settings.php';
334
-	}
335
-
336
-	/**
337
-	 * Installation of recommended plugins.
338
-	 *
339
-	 * @since 1.0.0
340
-	 */
341
-	public function setup_recommend() {
342
-		$next_url            = $this->get_next_step_link();
343
-		$recommended_plugins = self::get_recommend_wp_plugins();
344
-		include plugin_dir_path( __FILE__ ) . 'views/wizard-plugins.php';
345
-	}
346
-
347
-	/**
348
-	 * A list of recommended wp.org plugins.
349
-	 * @return array
350
-	 */
351
-	public static function get_recommend_wp_plugins() {
352
-		return array(
353
-			'ayecode-connect'  => array(
354
-				'file' => 'ayecode-connect/ayecode-connect.php',
355
-				'url'  => 'https://wordpress.org/plugins/ayecode-connect/',
356
-				'slug' => 'ayecode-connect',
357
-				'name' => 'AyeCode Connect',
358
-				'desc' => __( 'Documentation and Support from within your WordPress admin.', 'invoicing' ),
359
-			),
360
-			'invoicing-quotes' => array(
361
-				'file' => 'invoicing-quotes/wpinv-quote.php',
362
-				'url'  => 'https://wordpress.org/plugins/invoicing-quotes/',
363
-				'slug' => 'invoicing-quotes',
364
-				'name' => 'Customer Quotes',
365
-				'desc' => __( 'Create & Send Quotes to Customers and have them accept and pay.', 'invoicing' ),
366
-			),
367
-			'userswp'          => array(
368
-				'file' => 'userswp/userswp.php',
369
-				'url'  => 'https://wordpress.org/plugins/userswp/',
370
-				'slug' => 'userswp',
371
-				'name' => 'UsersWP',
372
-				'desc' => __( 'Frontend user login and registration as well as slick profile pages.', 'invoicing' ),
373
-			),
374
-		);
375
-	}
376
-
377
-	/**
378
-	 * Dummy Data setup.
379
-	 *
380
-	 * @since 2.4.0
381
-	 */
382
-	public function setup_payments() {
383
-		$next_url = $this->get_next_step_link();
384
-		include plugin_dir_path( __FILE__ ) . 'views/wizard-gateways.php';
385
-	}
386
-
387
-	/**
388
-	 * Dummy data save.
389
-	 *
390
-	 * This is done via ajax so we just pass onto the next step.
391
-	 *
392
-	 * @since 2.0.0
393
-	 */
394
-	public function setup_payments_save() {
395
-		check_admin_referer( 'getpaid-setup-wizard', 'getpaid-setup-wizard' );
396
-		wpinv_update_option( 'manual_active', ! empty( $_POST['enable-manual-gateway'] ) );
397
-
398
-		if ( ! empty( $_POST['paypal-email'] ) ) {
399
-			wpinv_update_option( 'paypal_email', sanitize_email( $_POST['paypal-email'] ) );
400
-			wpinv_update_option( 'paypal_active', 1 );
401
-			wpinv_update_option( 'paypal_sandbox', 0 );
402
-		}
403
-
404
-		wp_redirect( esc_url_raw( $this->get_next_step_link() ) );
405
-		exit;
406
-	}
407
-
408
-	/**
409
-	 * Final step.
410
-	 *
411
-	 * @since 2.0.0
412
-	 */
413
-	public function setup_ready() {
414
-		include plugin_dir_path( __FILE__ ) . 'views/wizard-thank-you.php';
415
-	}
254
+    }
255
+
256
+    /**
257
+     * Setup Wizard Footer.
258
+     *
259
+     * @since 2.4.0
260
+     */
261
+    public function display_footer() {
262
+
263
+        if ( isset( $_GET['step'] ) ) {
264
+            $label    = $this->step == 'next_steps' ? __( 'Return to the WordPress Dashboard', 'invoicing' ) : __( 'Skip this step', 'invoicing' );
265
+
266
+            echo '<p class="gd-return-to-dashboard-wrap"> <a href="' . esc_url( $this->get_next_step_link() ) . '" class="gd-return-to-dashboard btn btn-link d-block text-muted">' . esc_html( $label ) . '</a></p>';
267
+        }
268
+
269
+        echo '</body></html>';
270
+    }
271
+
272
+    /**
273
+     * Introduction step.
274
+     *
275
+     * @since 2.0.0
276
+     */
277
+    public function setup_introduction() {
278
+        $next_url = $this->get_next_step_link();
279
+        include plugin_dir_path( __FILE__ ) . 'views/wizard-introduction.php';
280
+    }
281
+
282
+    /**
283
+     * Get the URL for the next step's screen.
284
+     *
285
+     * @param string step   slug (default: current step)
286
+     *
287
+     * @return string       URL for next step if a next step exists.
288
+     *                      Admin URL if it's the last step.
289
+     *                      Empty string on failure.
290
+     * @since 3.0.0
291
+     */
292
+    public function get_next_step_link( $step = '' ) {
293
+        if ( ! $step ) {
294
+            $step = $this->step;
295
+        }
296
+
297
+        $keys = array_keys( $this->steps );
298
+        if ( end( $keys ) === $step ) {
299
+            return admin_url();
300
+        }
301
+
302
+        $step_index = array_search( $step, $keys );
303
+        if ( false === $step_index ) {
304
+            return '';
305
+        }
306
+
307
+        return remove_query_arg( 'settings-updated', add_query_arg( 'step', $keys[ $step_index + 1 ] ) );
308
+    }
309
+
310
+    /**
311
+     * Setup maps api.
312
+     *
313
+     * @since 2.0.0
314
+     */
315
+    public function setup_business() {
316
+        $next_url = $this->get_next_step_link();
317
+        $wizard   = $this;
318
+        $page     = 'wpinv_settings_general_main';
319
+        $section  = 'wpinv_settings_general_main';
320
+        include plugin_dir_path( __FILE__ ) . 'views/wizard-settings.php';
321
+    }
322
+
323
+    /**
324
+     * Default Location settings.
325
+     *
326
+     * @since 2.0.0
327
+     */
328
+    public function setup_currency() {
329
+        $next_url = $this->get_next_step_link();
330
+        $wizard   = $this;
331
+        $page     = 'wpinv_settings_general_currency_section';
332
+        $section  = 'wpinv_settings_general_currency_section';
333
+        include plugin_dir_path( __FILE__ ) . 'views/wizard-settings.php';
334
+    }
335
+
336
+    /**
337
+     * Installation of recommended plugins.
338
+     *
339
+     * @since 1.0.0
340
+     */
341
+    public function setup_recommend() {
342
+        $next_url            = $this->get_next_step_link();
343
+        $recommended_plugins = self::get_recommend_wp_plugins();
344
+        include plugin_dir_path( __FILE__ ) . 'views/wizard-plugins.php';
345
+    }
346
+
347
+    /**
348
+     * A list of recommended wp.org plugins.
349
+     * @return array
350
+     */
351
+    public static function get_recommend_wp_plugins() {
352
+        return array(
353
+            'ayecode-connect'  => array(
354
+                'file' => 'ayecode-connect/ayecode-connect.php',
355
+                'url'  => 'https://wordpress.org/plugins/ayecode-connect/',
356
+                'slug' => 'ayecode-connect',
357
+                'name' => 'AyeCode Connect',
358
+                'desc' => __( 'Documentation and Support from within your WordPress admin.', 'invoicing' ),
359
+            ),
360
+            'invoicing-quotes' => array(
361
+                'file' => 'invoicing-quotes/wpinv-quote.php',
362
+                'url'  => 'https://wordpress.org/plugins/invoicing-quotes/',
363
+                'slug' => 'invoicing-quotes',
364
+                'name' => 'Customer Quotes',
365
+                'desc' => __( 'Create & Send Quotes to Customers and have them accept and pay.', 'invoicing' ),
366
+            ),
367
+            'userswp'          => array(
368
+                'file' => 'userswp/userswp.php',
369
+                'url'  => 'https://wordpress.org/plugins/userswp/',
370
+                'slug' => 'userswp',
371
+                'name' => 'UsersWP',
372
+                'desc' => __( 'Frontend user login and registration as well as slick profile pages.', 'invoicing' ),
373
+            ),
374
+        );
375
+    }
376
+
377
+    /**
378
+     * Dummy Data setup.
379
+     *
380
+     * @since 2.4.0
381
+     */
382
+    public function setup_payments() {
383
+        $next_url = $this->get_next_step_link();
384
+        include plugin_dir_path( __FILE__ ) . 'views/wizard-gateways.php';
385
+    }
386
+
387
+    /**
388
+     * Dummy data save.
389
+     *
390
+     * This is done via ajax so we just pass onto the next step.
391
+     *
392
+     * @since 2.0.0
393
+     */
394
+    public function setup_payments_save() {
395
+        check_admin_referer( 'getpaid-setup-wizard', 'getpaid-setup-wizard' );
396
+        wpinv_update_option( 'manual_active', ! empty( $_POST['enable-manual-gateway'] ) );
397
+
398
+        if ( ! empty( $_POST['paypal-email'] ) ) {
399
+            wpinv_update_option( 'paypal_email', sanitize_email( $_POST['paypal-email'] ) );
400
+            wpinv_update_option( 'paypal_active', 1 );
401
+            wpinv_update_option( 'paypal_sandbox', 0 );
402
+        }
403
+
404
+        wp_redirect( esc_url_raw( $this->get_next_step_link() ) );
405
+        exit;
406
+    }
407
+
408
+    /**
409
+     * Final step.
410
+     *
411
+     * @since 2.0.0
412
+     */
413
+    public function setup_ready() {
414
+        include plugin_dir_path( __FILE__ ) . 'views/wizard-thank-you.php';
415
+    }
416 416
 
417 417
 }
418 418
 
Please login to merge, or discard this patch.
vendor/ayecode/wp-super-duper/includes/class-super-duper-bricks-element.php 1 patch
Indentation   +259 added lines, -259 removed lines patch added patch discarded remove patch
@@ -6,259 +6,259 @@  discard block
 block discarded – undo
6 6
 
7 7
 class Super_Duper_Bricks_Element extends \Bricks\Element {
8 8
 
9
-	public $widget;
10
-
11
-	public function __construct( $element = null ) {
12
-
13
-
14
-		$block_icon = !empty($this->widget->options['block-icon']) ? $this->widget->options['block-icon'] : '';
9
+    public $widget;
15 10
 
11
+    public function __construct( $element = null ) {
12
+
13
+
14
+        $block_icon = !empty($this->widget->options['block-icon']) ? $this->widget->options['block-icon'] : '';
15
+
16
+
17
+        $this->category = !empty($this->widget->options['textdomain']) ? esc_attr( $this->widget->options['textdomain'] ) : 'Super Duper';
18
+        $this->name     = $this->widget->id_base;
19
+        $this->icon     = (strpos($block_icon, 'fa') === 0) ? esc_attr($this->widget->options['block-icon']) : 'fas fa-globe-americas';
20
+
21
+        parent::__construct($element);
22
+    }
23
+
24
+    /**
25
+     * Set the element name.
26
+     *
27
+     * @return array|string|string[]|null
28
+     */
29
+    public function get_label() {
30
+        $escaped_text = esc_attr( $this->widget->name );
31
+        return str_replace( ' &gt; ', ' > ', $escaped_text ); // keep our > but have it safe
32
+    }
33
+
34
+    /**
35
+     * Bricks function to set the controls
36
+     *
37
+     * @return void
38
+     */
39
+    public function set_controls() {
40
+        $args = $this->sd_convert_arguments($this->widget);
41
+
42
+        if (!empty($args)) {
43
+            $this->controls = $this->controls + $args;
44
+        }
45
+
46
+    }
47
+
48
+    /**
49
+     * Set the bricks control groups from the GD ones.
50
+     *
51
+     * @return void
52
+     */
53
+    public function set_control_groups() {
54
+        $args = $this->sd_get_arguments();
55
+
56
+        $groups = array();
57
+        if(!empty($args)) {
58
+            foreach ($args as $k => $v) {
59
+                $g_slug = !empty($v['group']) ? sanitize_title( $v['group'] ) : '';
60
+                if($g_slug && empty($groups[$g_slug])) {
61
+                    $groups[$g_slug] = array(
62
+                        'title' => esc_html( $v['group'] ),
63
+                        'tab' => 'content',
64
+                    );
65
+                }
66
+            }
67
+        }
68
+
69
+        if(!empty($groups)) {
70
+            $this->control_groups = $this->control_groups + $groups;
71
+        }
72
+
73
+    }
74
+
75
+    /**
76
+     * Get the setting input arguments.
77
+     *
78
+     * @return mixed
79
+     */
80
+    public function sd_get_arguments() {
81
+        $args = $this->widget->set_arguments();
82
+
83
+        $widget_options = ! empty( $this->widget->options ) ? $this->widget->options : array();
84
+        $widget_instance = ! empty( $this->widget->instance ) ? $this->widget->instance : array();
85
+
86
+        $args = apply_filters( 'wp_super_duper_arguments', $args, $widget_options, $widget_instance );
87
+
88
+        $arg_keys_subtract = $this->sd_remove_arguments();
89
+
90
+        if ( ! empty( $arg_keys_subtract ) ) {
91
+            foreach($arg_keys_subtract as $key ){
92
+                unset($args[$key]);
93
+            }
94
+        }
95
+
96
+        return $args;
97
+    }
98
+
99
+
100
+    /**
101
+     * Simply use our own render function for the output.
102
+     *
103
+     * @return void
104
+     */
105
+    public function render() {
106
+        $settings = $this->sd_maybe_convert_values( $this->settings );
107
+
108
+        // Set the AyeCode UI calss on the wrapper
109
+        $this->set_attribute( '_root', 'class', 'bsui' );
110
+
111
+        // We might need to add a placeholder here for previews.
112
+
113
+        do_action( 'super_duper_before_render_bricks_element', $settings, $this->widget, $this );
114
+
115
+        // Add the bricks attributes to wrapper
116
+        echo "<div {$this->render_attributes( '_root' )}>";
117
+        echo $this->widget->output( $settings );
118
+        echo '</div>';
119
+    }
120
+
121
+    /**
122
+     * Values can never be arrays so convert if bricks setting make it an array.
123
+     *
124
+     * @param $settings
125
+     * @return mixed
126
+     */
127
+    public function sd_maybe_convert_values( $settings ) {
128
+
129
+
130
+        if (!empty($settings)) {
131
+            foreach( $settings as $k => $v ) {
132
+                if(is_array($v)) {
133
+                    $value = '';
134
+                    // is color
135
+                    if (isset($v['hex'])) {
136
+                        $value = $v['hex'];
137
+                    } elseif (isset($v['icon'])) {
138
+                        $value = $v['icon'];
139
+                    }
140
+
141
+
142
+                    // set the value
143
+                    $settings[$k] = $value;
144
+                }
145
+
146
+            }
147
+        }
148
+
149
+        return $settings;
150
+    }
151
+
152
+    /**
153
+     * Convert SD arguments to Bricks arguments.
154
+     *
155
+     * @param $widget
156
+     *
157
+     * @return array
158
+     */
159
+    public function sd_convert_arguments() {
160
+        $bricks_args = array();
161
+
162
+        $args = $this->sd_get_arguments();
163
+
164
+        if ( ! empty( $args ) ) {
165
+            foreach ( $args as $key => $arg ) {
166
+                // convert title
167
+                if ( ! empty( $arg['title'] ) ) {
168
+                    $arg['label'] = $arg['title'];
169
+                    unset( $arg['title'] );
170
+                }
171
+
172
+                // set fields not to use dynamic data
173
+                $arg['hasDynamicData'] = false;
174
+
175
+                if ( ! empty( $arg['group'] ) ) {
176
+                    $arg['group'] =  sanitize_title( $arg['group'] );
177
+                }
178
+
179
+                $arg['rerender'] = true;
180
+
181
+                // required
182
+                if( ! empty( $arg['element_require'] ) ) {
183
+                    $arg['required'] = $this->sd_convert_required( $arg['element_require'] );
184
+                    unset( $arg['element_require'] );
185
+                }
186
+
187
+                // icons
188
+                if ( 'icon' === $key ) {
189
+                    $arg['type'] = 'icon';
190
+                }
191
+
192
+                // Bricks don't render dropdown when first option key is 0.
193
+                if ( in_array( $key, array( 'zoom', 'mapzoom' ) ) && ! empty( $arg['options'] ) && is_array( $arg['options'] ) && ( $option_keys = array_keys( $arg['options'] ) ) ) {
194
+                    // Move first element to last.
195
+                    if ( $option_keys[0] === 0 || $option_keys[0] === '0' ) {
196
+                        $options = $arg['options'];
197
+                        unset( $arg['options'][0] );
198
+                        $arg['options'][0] = $options[0];
199
+                    }
200
+                }
201
+
202
+                $bricks_args[$key] = $arg;
203
+            }
204
+        }
205
+
206
+        return $bricks_args;
207
+    }
208
+
209
+    /**
210
+     * Convert the SD element_required to the Bricks required syntax.
211
+     *
212
+     * @param $element_require
213
+     * @return array
214
+     */
215
+    public function sd_convert_required($element_require) {
216
+        $bricks_required = [];
217
+
218
+        // Handle logical OR (||) for multiple values
219
+        if (strpos($element_require, '||') !== false) {
220
+            preg_match('/\[%(.+?)%\] *== *"(.*?)"/', $element_require, $matches);
221
+            if ($matches) {
222
+                $control_id = $matches[1];
223
+                preg_match_all('/\[%.*?%\] *== *"(.*?)"/', $element_require, $value_matches);
224
+                $values = $value_matches[1];
225
+                $bricks_required[] = [$control_id, '=', $values];
226
+            }
227
+            return $bricks_required;
228
+        }
229
+
230
+        // Match individual conditions
231
+        preg_match_all('/(!)?\[%(.*?)%\](?:\s*([!=<>]=?)\s*(".*?"|\'.*?\'|\d+))?/', $element_require, $matches, PREG_SET_ORDER);
232
+
233
+        foreach ($matches as $match) {
234
+            $is_negation = isset($match[1]) && $match[1] === '!';
235
+            $control_id = $match[2];
236
+            $operator = isset($match[3]) ? str_replace('==', '=', $match[3]) : ($is_negation ? '=' : '!=');
237
+            $value = isset($match[4]) ? trim($match[4], '"\'') : ($is_negation ? '' : '');
238
+
239
+            // Adjust for negation without explicit operator
240
+            if ($is_negation && !isset($match[3])) {
241
+                $operator = '=';
242
+                $value = '';
243
+            }
244
+
245
+            $bricks_required[] = [$control_id, $operator, $value];
246
+        }
247
+
248
+        return $bricks_required;
249
+    }
250
+
251
+
252
+    /**
253
+     * A way to remove some settings by keys.
254
+     *
255
+     * @return array
256
+     */
257
+    public function sd_remove_arguments()
258
+    {
259
+        return array();
260
+    }
16 261
 
17
-		$this->category = !empty($this->widget->options['textdomain']) ? esc_attr( $this->widget->options['textdomain'] ) : 'Super Duper';
18
-		$this->name     = $this->widget->id_base;
19
-		$this->icon     = (strpos($block_icon, 'fa') === 0) ? esc_attr($this->widget->options['block-icon']) : 'fas fa-globe-americas';
20
-
21
-		parent::__construct($element);
22
-	}
23
-
24
-	/**
25
-	 * Set the element name.
26
-	 *
27
-	 * @return array|string|string[]|null
28
-	 */
29
-	public function get_label() {
30
-		$escaped_text = esc_attr( $this->widget->name );
31
-		return str_replace( ' &gt; ', ' > ', $escaped_text ); // keep our > but have it safe
32
-	}
33
-
34
-	/**
35
-	 * Bricks function to set the controls
36
-	 *
37
-	 * @return void
38
-	 */
39
-	public function set_controls() {
40
-		$args = $this->sd_convert_arguments($this->widget);
41
-
42
-		if (!empty($args)) {
43
-			$this->controls = $this->controls + $args;
44
-		}
45
-
46
-	}
47
-
48
-	/**
49
-	 * Set the bricks control groups from the GD ones.
50
-	 *
51
-	 * @return void
52
-	 */
53
-	public function set_control_groups() {
54
-		$args = $this->sd_get_arguments();
55
-
56
-		$groups = array();
57
-		if(!empty($args)) {
58
-			foreach ($args as $k => $v) {
59
-				$g_slug = !empty($v['group']) ? sanitize_title( $v['group'] ) : '';
60
-				if($g_slug && empty($groups[$g_slug])) {
61
-					$groups[$g_slug] = array(
62
-						'title' => esc_html( $v['group'] ),
63
-						'tab' => 'content',
64
-					);
65
-				}
66
-			}
67
-		}
68
-
69
-		if(!empty($groups)) {
70
-			$this->control_groups = $this->control_groups + $groups;
71
-		}
72
-
73
-	}
74
-
75
-	/**
76
-	 * Get the setting input arguments.
77
-	 *
78
-	 * @return mixed
79
-	 */
80
-	public function sd_get_arguments() {
81
-		$args = $this->widget->set_arguments();
82
-
83
-		$widget_options = ! empty( $this->widget->options ) ? $this->widget->options : array();
84
-		$widget_instance = ! empty( $this->widget->instance ) ? $this->widget->instance : array();
85
-
86
-		$args = apply_filters( 'wp_super_duper_arguments', $args, $widget_options, $widget_instance );
87
-
88
-		$arg_keys_subtract = $this->sd_remove_arguments();
89
-
90
-		if ( ! empty( $arg_keys_subtract ) ) {
91
-			foreach($arg_keys_subtract as $key ){
92
-				unset($args[$key]);
93
-			}
94
-		}
95
-
96
-		return $args;
97
-	}
98
-
99
-
100
-	/**
101
-	 * Simply use our own render function for the output.
102
-	 *
103
-	 * @return void
104
-	 */
105
-	public function render() {
106
-		$settings = $this->sd_maybe_convert_values( $this->settings );
107
-
108
-		// Set the AyeCode UI calss on the wrapper
109
-		$this->set_attribute( '_root', 'class', 'bsui' );
110
-
111
-		// We might need to add a placeholder here for previews.
112
-
113
-		do_action( 'super_duper_before_render_bricks_element', $settings, $this->widget, $this );
114
-
115
-		// Add the bricks attributes to wrapper
116
-		echo "<div {$this->render_attributes( '_root' )}>";
117
-		echo $this->widget->output( $settings );
118
-		echo '</div>';
119
-	}
120
-
121
-	/**
122
-	 * Values can never be arrays so convert if bricks setting make it an array.
123
-	 *
124
-	 * @param $settings
125
-	 * @return mixed
126
-	 */
127
-	public function sd_maybe_convert_values( $settings ) {
128
-
129
-
130
-		if (!empty($settings)) {
131
-			foreach( $settings as $k => $v ) {
132
-				if(is_array($v)) {
133
-					$value = '';
134
-					// is color
135
-					if (isset($v['hex'])) {
136
-						$value = $v['hex'];
137
-					} elseif (isset($v['icon'])) {
138
-						$value = $v['icon'];
139
-					}
140
-
141
-
142
-					// set the value
143
-					$settings[$k] = $value;
144
-				}
145
-
146
-			}
147
-		}
148
-
149
-		return $settings;
150
-	}
151
-
152
-	/**
153
-	 * Convert SD arguments to Bricks arguments.
154
-	 *
155
-	 * @param $widget
156
-	 *
157
-	 * @return array
158
-	 */
159
-	public function sd_convert_arguments() {
160
-		$bricks_args = array();
161
-
162
-		$args = $this->sd_get_arguments();
163
-
164
-		if ( ! empty( $args ) ) {
165
-			foreach ( $args as $key => $arg ) {
166
-				// convert title
167
-				if ( ! empty( $arg['title'] ) ) {
168
-					$arg['label'] = $arg['title'];
169
-					unset( $arg['title'] );
170
-				}
171
-
172
-				// set fields not to use dynamic data
173
-				$arg['hasDynamicData'] = false;
174
-
175
-				if ( ! empty( $arg['group'] ) ) {
176
-					$arg['group'] =  sanitize_title( $arg['group'] );
177
-				}
178
-
179
-				$arg['rerender'] = true;
180
-
181
-				// required
182
-				if( ! empty( $arg['element_require'] ) ) {
183
-					$arg['required'] = $this->sd_convert_required( $arg['element_require'] );
184
-					unset( $arg['element_require'] );
185
-				}
186
-
187
-				// icons
188
-				if ( 'icon' === $key ) {
189
-					$arg['type'] = 'icon';
190
-				}
191
-
192
-				// Bricks don't render dropdown when first option key is 0.
193
-				if ( in_array( $key, array( 'zoom', 'mapzoom' ) ) && ! empty( $arg['options'] ) && is_array( $arg['options'] ) && ( $option_keys = array_keys( $arg['options'] ) ) ) {
194
-					// Move first element to last.
195
-					if ( $option_keys[0] === 0 || $option_keys[0] === '0' ) {
196
-						$options = $arg['options'];
197
-						unset( $arg['options'][0] );
198
-						$arg['options'][0] = $options[0];
199
-					}
200
-				}
201
-
202
-				$bricks_args[$key] = $arg;
203
-			}
204
-		}
205
-
206
-		return $bricks_args;
207
-	}
208
-
209
-	/**
210
-	 * Convert the SD element_required to the Bricks required syntax.
211
-	 *
212
-	 * @param $element_require
213
-	 * @return array
214
-	 */
215
-	public function sd_convert_required($element_require) {
216
-		$bricks_required = [];
217
-
218
-		// Handle logical OR (||) for multiple values
219
-		if (strpos($element_require, '||') !== false) {
220
-			preg_match('/\[%(.+?)%\] *== *"(.*?)"/', $element_require, $matches);
221
-			if ($matches) {
222
-				$control_id = $matches[1];
223
-				preg_match_all('/\[%.*?%\] *== *"(.*?)"/', $element_require, $value_matches);
224
-				$values = $value_matches[1];
225
-				$bricks_required[] = [$control_id, '=', $values];
226
-			}
227
-			return $bricks_required;
228
-		}
229
-
230
-		// Match individual conditions
231
-		preg_match_all('/(!)?\[%(.*?)%\](?:\s*([!=<>]=?)\s*(".*?"|\'.*?\'|\d+))?/', $element_require, $matches, PREG_SET_ORDER);
232
-
233
-		foreach ($matches as $match) {
234
-			$is_negation = isset($match[1]) && $match[1] === '!';
235
-			$control_id = $match[2];
236
-			$operator = isset($match[3]) ? str_replace('==', '=', $match[3]) : ($is_negation ? '=' : '!=');
237
-			$value = isset($match[4]) ? trim($match[4], '"\'') : ($is_negation ? '' : '');
238
-
239
-			// Adjust for negation without explicit operator
240
-			if ($is_negation && !isset($match[3])) {
241
-				$operator = '=';
242
-				$value = '';
243
-			}
244
-
245
-			$bricks_required[] = [$control_id, $operator, $value];
246
-		}
247
-
248
-		return $bricks_required;
249
-	}
250
-
251
-
252
-	/**
253
-	 * A way to remove some settings by keys.
254
-	 *
255
-	 * @return array
256
-	 */
257
-	public function sd_remove_arguments()
258
-	{
259
-		return array();
260
-	}
261
-
262 262
 }
263 263
 
264 264
 
@@ -267,12 +267,12 @@  discard block
 block discarded – undo
267 267
  */
268 268
 add_action( 'wp_enqueue_scripts', function() {
269 269
 
270
-	// Check if we're in the Bricks Editor
271
-	if ( isset( $_GET['bricks'] ) && $_GET['bricks'] && bricks_is_builder_main() ) {
272
-		// Add inline script to the 'bricks-builder' script
273
-		wp_add_inline_script(
274
-			'bricks-builder',
275
-			"
270
+    // Check if we're in the Bricks Editor
271
+    if ( isset( $_GET['bricks'] ) && $_GET['bricks'] && bricks_is_builder_main() ) {
272
+        // Add inline script to the 'bricks-builder' script
273
+        wp_add_inline_script(
274
+            'bricks-builder',
275
+            "
276 276
 
277 277
 (function () {
278 278
     // Function to get the current breakpoint from the #bricks-preview class
@@ -479,6 +479,6 @@  discard block
 block discarded – undo
479 479
     addIconsToLabels();
480 480
 })();
481 481
 "
482
-		);
483
-	}
482
+        );
483
+    }
484 484
 });
Please login to merge, or discard this patch.