Passed
Push — master ( 81484a...8aa7be )
by Brian
11:50
created
includes/error-functions.php 1 patch
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
  * @package Invoicing
7 7
  */
8 8
  
9
-defined( 'ABSPATH' ) || exit;
9
+defined('ABSPATH') || exit;
10 10
 
11 11
 /**
12 12
  * Returns the errors as html
@@ -14,27 +14,27 @@  discard block
 block discarded – undo
14 14
  * @param clear whether or not to clear the errors.
15 15
  * @since  1.0.19
16 16
  */
17
-function getpaid_get_errors_html( $clear = true ) {
17
+function getpaid_get_errors_html($clear = true) {
18 18
 
19 19
     $errors = '';
20
-    foreach ( wpinv_get_errors() as $error ) {
21
-        $type     = 'error';
20
+    foreach (wpinv_get_errors() as $error) {
21
+        $type = 'error';
22 22
 
23
-        if ( is_array( $error ) ) {
23
+        if (is_array($error)) {
24 24
             $type  = $error['type'];
25 25
             $error = $error['text'];
26 26
         }
27 27
 
28 28
         $errors .= aui()->alert(
29 29
             array(
30
-                'content'     => wp_kses_post( $error ),
30
+                'content'     => wp_kses_post($error),
31 31
                 'type'        => $type,
32 32
             )
33 33
         );
34 34
 
35 35
     }
36 36
 
37
-    if ( $clear ){
37
+    if ($clear) {
38 38
         wpinv_clear_errors();
39 39
     }
40 40
 
@@ -55,8 +55,8 @@  discard block
 block discarded – undo
55 55
  * @return array
56 56
  */
57 57
 function wpinv_get_errors() {
58
-    $errors = getpaid_session()->get( 'wpinv_errors' );
59
-    return is_array( $errors ) ? $errors : array();
58
+    $errors = getpaid_session()->get('wpinv_errors');
59
+    return is_array($errors) ? $errors : array();
60 60
 }
61 61
 
62 62
 /**
@@ -66,15 +66,15 @@  discard block
 block discarded – undo
66 66
  * @param string $error_message The error message.
67 67
  * @param string $type Either error, info, warning, primary, dark, light or success.
68 68
  */
69
-function wpinv_set_error( $error_id, $error_message, $type = 'error' ) {
69
+function wpinv_set_error($error_id, $error_message, $type = 'error') {
70 70
 
71 71
     $errors              = wpinv_get_errors();
72
-    $errors[ $error_id ] = array(
72
+    $errors[$error_id] = array(
73 73
         'type' =>  $type,
74 74
         'text' =>  $error_message,
75 75
     );
76 76
 
77
-    getpaid_session()->set( 'wpinv_errors', $errors );
77
+    getpaid_session()->set('wpinv_errors', $errors);
78 78
 }
79 79
 
80 80
 /**
@@ -82,7 +82,7 @@  discard block
 block discarded – undo
82 82
  * 
83 83
  */
84 84
 function wpinv_has_errors() {
85
-    return count( wpinv_get_errors() ) > 0;
85
+    return count(wpinv_get_errors()) > 0;
86 86
 }
87 87
 
88 88
 /**
@@ -90,21 +90,21 @@  discard block
 block discarded – undo
90 90
  * 
91 91
  */
92 92
 function wpinv_clear_errors() {
93
-    getpaid_session()->set( 'wpinv_errors', null );
93
+    getpaid_session()->set('wpinv_errors', null);
94 94
 }
95 95
 
96 96
 /**
97 97
  * Clears a single error.
98 98
  * 
99 99
  */
100
-function wpinv_unset_error( $error_id ) {
100
+function wpinv_unset_error($error_id) {
101 101
     $errors = wpinv_get_errors();
102 102
 
103
-    if ( isset( $errors[ $error_id ] ) ) {
104
-        unset( $errors[ $error_id ] );
103
+    if (isset($errors[$error_id])) {
104
+        unset($errors[$error_id]);
105 105
     }
106 106
 
107
-    getpaid_session()->set( 'wpinv_errors', $errors );
107
+    getpaid_session()->set('wpinv_errors', $errors);
108 108
 }
109 109
 
110 110
 /**
@@ -115,15 +115,15 @@  discard block
 block discarded – undo
115 115
  * @param string $message Message to log.
116 116
  * @param string $version Version the message was added in.
117 117
  */
118
-function getpaid_doing_it_wrong( $function, $message, $version ) {
118
+function getpaid_doing_it_wrong($function, $message, $version) {
119 119
 
120 120
 	$message .= ' Backtrace: ' . wp_debug_backtrace_summary();
121 121
 
122
-	if ( is_ajax() || defined( 'REST_REQUEST' ) ) {
123
-		do_action( 'doing_it_wrong_run', $function, $message, $version );
124
-		error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." );
122
+	if (is_ajax() || defined('REST_REQUEST')) {
123
+		do_action('doing_it_wrong_run', $function, $message, $version);
124
+		error_log("{$function} was called incorrectly. {$message}. This message was added in version {$version}.");
125 125
 	} else {
126
-		_doing_it_wrong( $function, $message, $version );
126
+		_doing_it_wrong($function, $message, $version);
127 127
 	}
128 128
 
129 129
 }
Please login to merge, or discard this patch.
includes/class-wpinv.php 2 patches
Indentation   +495 added lines, -495 removed lines patch added patch discarded remove patch
@@ -14,438 +14,438 @@  discard block
 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
-	 * Tax instance.
40
-	 *
41
-	 * @var WPInv_EUVat
42
-	 */
43
-	public $tax;
44
-
45
-	/**
46
-	 * @param array An array of payment gateways.
47
-	 */
48
-	public $gateways;
49
-
50
-	/**
51
-	 * Class constructor.
52
-	 */
53
-	public function __construct() {
54
-		$this->define_constants();
55
-		$this->includes();
56
-		$this->init_hooks();
57
-		$this->set_properties();
58
-	}
59
-
60
-	/**
61
-	 * Sets a custom data property.
62
-	 * 
63
-	 * @param string $prop The prop to set.
64
-	 * @param mixed $value The value to retrieve.
65
-	 */
66
-	public function set( $prop, $value ) {
67
-		$this->data[ $prop ] = $value;
68
-	}
69
-
70
-	/**
71
-	 * Gets a custom data property.
72
-	 * 
73
-	 * @param string $prop The prop to set.
74
-	 * @return mixed The value.
75
-	 */
76
-	public function get( $prop ) {
77
-
78
-		if ( isset( $this->data[ $prop ] ) ) {
79
-			return $this->data[ $prop ];
80
-		}
81
-
82
-		return null;
83
-	}
84
-
85
-	/**
86
-	 * Define class properties.
87
-	 */
88
-	public function set_properties() {
89
-
90
-		// Sessions.
91
-		$this->set( 'session', new WPInv_Session_Handler() );
92
-		$GLOBALS['wpi_session'] = $this->get( 'session' ); // Backwards compatibility.
93
-		$this->form_elements = new WPInv_Payment_Form_Elements();
94
-		$this->tax           = new WPInv_EUVat();
95
-		$this->tax->init();
96
-		$GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility.
97
-
98
-		// Init other objects.
99
-		$this->set( 'reports', new WPInv_Reports() ); // TODO: Refactor.
100
-		$this->set( 'session', new WPInv_Session_Handler() );
101
-		$this->set( 'notes', new WPInv_Notes() );
102
-		$this->set( 'api', new WPInv_API() );
103
-		$this->set( 'post_types', new GetPaid_Post_Types() );
104
-		$this->set( 'template', new GetPaid_Template() );
105
-		$this->set( 'admin', new GetPaid_Admin() );
106
-		$this->set( 'subscriptions', new WPInv_Subscriptions() );
107
-		$this->set( 'invoice_emails', new GetPaid_Invoice_Notification_Emails() );
108
-
109
-	}
110
-
111
-	 /**
112
-	 * Define plugin constants.
113
-	 */
114
-	public function define_constants() {
115
-		define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) );
116
-		define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) );
117
-		$this->version = WPINV_VERSION;
118
-	}
119
-
120
-	/**
121
-	 * Hook into actions and filters.
122
-	 *
123
-	 * @since 1.0.19
124
-	 */
125
-	protected function init_hooks() {
126
-		/* Internationalize the text strings used. */
127
-		add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
128
-
129
-		// Init the plugin after WordPress inits.
130
-		add_action( 'init', array( $this, 'init' ), 1 );
131
-		add_action( 'getpaid_init', array( $this, 'maybe_process_ipn' ), 5 );
132
-		add_action( 'init', array( &$this, 'wpinv_actions' ) );
133
-		add_action( 'init', array( $this, 'maybe_do_authenticated_action' ), 100 );
134
-
135
-		if ( class_exists( 'BuddyPress' ) ) {
136
-			add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) );
137
-		}
138
-
139
-		add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
140
-		add_action( 'wp_footer', array( &$this, 'wp_footer' ) );
141
-		add_action( 'widgets_init', array( &$this, 'register_widgets' ) );
142
-		add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) );
143
-		add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
144
-
145
-		// Fires after registering actions.
146
-		do_action( 'wpinv_actions', $this );
147
-		do_action( 'getpaid_actions', $this );
148
-
149
-	}
150
-
151
-	public function plugins_loaded() {
152
-		/* Internationalize the text strings used. */
153
-		$this->load_textdomain();
154
-
155
-		do_action( 'wpinv_loaded' );
156
-
157
-		// Fix oxygen page builder conflict
158
-		if ( function_exists( 'ct_css_output' ) ) {
159
-			wpinv_oxygen_fix_conflict();
160
-		}
161
-	}
162
-
163
-	/**
164
-	 * Load the translation of the plugin.
165
-	 *
166
-	 * @since 1.0
167
-	 */
168
-	public function load_textdomain( $locale = NULL ) {
169
-		if ( empty( $locale ) ) {
170
-			$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
171
-		}
172
-
173
-		$locale = apply_filters( 'plugin_locale', $locale, 'invoicing' );
174
-
175
-		unload_textdomain( 'invoicing' );
176
-		load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' );
177
-		load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' );
178
-
179
-		/**
180
-		 * Define language constants.
181
-		 */
182
-		require_once( WPINV_PLUGIN_DIR . 'language.php' );
183
-	}
184
-
185
-	/**
186
-	 * Include required core files used in admin and on the frontend.
187
-	 */
188
-	public function includes() {
189
-
190
-		// Start with the settings.
191
-		require_once( WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php' );
192
-
193
-		// Packages/libraries.
194
-		require_once( WPINV_PLUGIN_DIR . 'vendor/autoload.php' );
195
-		require_once( WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php' );
196
-		require_once( WPINV_PLUGIN_DIR . 'includes/libraries/action-scheduler/action-scheduler.php' );
197
-
198
-		// Load functions.
199
-		require_once( WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php' );
200
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php' );
201
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php' );
202
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php' );
203
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php' );
204
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php' );
205
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php' );
206
-		require_once( WPINV_PLUGIN_DIR . 'includes/invoice-functions.php' );
207
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php' );
208
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php' );
209
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php' );
210
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php' );
211
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php' );
212
-		require_once( WPINV_PLUGIN_DIR . 'includes/error-functions.php' );
213
-
214
-		// Register autoloader.
215
-		try {
216
-			spl_autoload_register( array( $this, 'autoload' ), true );
217
-		} catch ( Exception $e ) {
218
-			wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
219
-		}
220
-
221
-		require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php' );
222
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php' );
223
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php' );
224
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php' );
225
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php' );
226
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php' );
227
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php' );
228
-		require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' );
229
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php' );
230
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php' );
231
-		require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php' );
232
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' );
233
-		require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' );
234
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' );
235
-		require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' );
236
-		require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' );
237
-		require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' );
238
-		require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' );
239
-		require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' );
240
-		require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' );
241
-		require_once( WPINV_PLUGIN_DIR . 'widgets/getpaid.php' );
242
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php' );
243
-
244
-		/**
245
-		 * Load the tax class.
246
-		 */
247
-		if ( ! class_exists( 'WPInv_EUVat' ) ) {
248
-			require_once( WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php' );
249
-		}
250
-
251
-		$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
252
-		if ( !empty( $gateways ) ) {
253
-			foreach ( $gateways as $gateway ) {
254
-				if ( $gateway == 'manual' ) {
255
-					continue;
256
-				}
257
-
258
-				$gateway_file = WPINV_PLUGIN_DIR . 'includes/gateways/' . $gateway . '.php';
259
-
260
-				if ( file_exists( $gateway_file ) ) {
261
-					require_once( $gateway_file );
262
-				}
263
-			}
264
-		}
265
-
266
-		if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
267
-			GetPaid_Post_Types_Admin::init();
268
-
269
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php' );
270
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php' );
271
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php' );
272
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php' );
273
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php' );
274
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php' );
275
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php' );
276
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php' );
277
-			// load the user class only on the users.php page
278
-			global $pagenow;
279
-			if($pagenow=='users.php'){
280
-				new WPInv_Admin_Users();
281
-			}
282
-		}
283
-
284
-		// Register cli commands
285
-		if ( defined( 'WP_CLI' ) && WP_CLI ) {
286
-			require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php' );
287
-			WP_CLI::add_command( 'invoicing', 'WPInv_CLI' );
288
-		}
289
-
290
-		// include css inliner
291
-		if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) {
292
-			include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' );
293
-		}
294
-
295
-		require_once( WPINV_PLUGIN_DIR . 'includes/admin/install.php' );
296
-	}
297
-
298
-	/**
299
-	 * Class autoloader
300
-	 *
301
-	 * @param       string $class_name The name of the class to load.
302
-	 * @access      public
303
-	 * @since       1.0.19
304
-	 * @return      void
305
-	 */
306
-	public function autoload( $class_name ) {
307
-
308
-		// Normalize the class name...
309
-		$class_name  = strtolower( $class_name );
310
-
311
-		// ... and make sure it is our class.
312
-		if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
313
-			return;
314
-		}
315
-
316
-		// Next, prepare the file name from the class.
317
-		$file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
318
-
319
-		// Base path of the classes.
320
-		$plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
321
-
322
-		// And an array of possible locations in order of importance.
323
-		$locations = array(
324
-			"$plugin_path/includes",
325
-			"$plugin_path/includes/data-stores",
326
-			"$plugin_path/includes/gateways",
327
-			"$plugin_path/includes/api",
328
-			"$plugin_path/includes/admin",
329
-			"$plugin_path/includes/admin/meta-boxes",
330
-		);
331
-
332
-		foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) {
333
-
334
-			if ( file_exists( trailingslashit( $location ) . $file_name ) ) {
335
-				include trailingslashit( $location ) . $file_name;
336
-				break;
337
-			}
338
-
339
-		}
340
-
341
-	}
342
-
343
-	/**
344
-	 * Inits hooks etc.
345
-	 */
346
-	public function init() {
347
-
348
-		// Fires before getpaid inits.
349
-		do_action( 'before_getpaid_init', $this );
350
-
351
-		// Load default gateways.
352
-		$gateways = apply_filters(
353
-			'getpaid_default_gateways',
354
-			array(
355
-				'manual'        => 'GetPaid_Manual_Gateway',
356
-				'paypal'        => 'GetPaid_Paypal_Gateway',
357
-				'worldpay'      => 'GetPaid_Worldpay_Gateway',
358
-				'bank_transfer' => 'GetPaid_Bank_Transfer_Gateway',
359
-				'authorizenet'  => 'GetPaid_Authorize_Net_Gateway',
360
-			)
361
-		);
362
-
363
-		foreach ( $gateways as $id => $class ) {
364
-			$this->gateways[ $id ] = new $class();
365
-		}
366
-
367
-		// Fires after getpaid inits.
368
-		do_action( 'getpaid_init', $this );
369
-
370
-	}
371
-
372
-	/**
373
-	 * Checks if this is an IPN request and processes it.
374
-	 */
375
-	public function maybe_process_ipn() {
376
-
377
-		// Ensure that this is an IPN request.
378
-		if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) {
379
-			return;
380
-		}
381
-
382
-		$gateway = wpinv_clean( $_GET['wpi-gateway'] );
383
-
384
-		do_action( 'wpinv_verify_payment_ipn', $gateway );
385
-		do_action( "wpinv_verify_{$gateway}_ipn" );
386
-		exit;
387
-
388
-	}
389
-
390
-	public function enqueue_scripts() {
391
-		$suffix       = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
392
-
393
-		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' );
394
-		wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version );
395
-		wp_enqueue_style( 'wpinv_front_style' );
396
-
397
-		// Register scripts
398
-		wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true );
399
-		wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ),  filemtime( WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js' ) );
400
-
401
-		$localize                         = array();
402
-		$localize['ajax_url']             = admin_url( 'admin-ajax.php' );
403
-		$localize['nonce']                = wp_create_nonce( 'wpinv-nonce' );
404
-		$localize['currency_symbol']      = wpinv_currency_symbol();
405
-		$localize['currency_pos']         = wpinv_currency_position();
406
-		$localize['thousand_sep']         = wpinv_thousands_separator();
407
-		$localize['decimal_sep']          = wpinv_decimal_separator();
408
-		$localize['decimals']             = wpinv_decimals();
409
-		$localize['txtComplete']          = __( 'Continue', 'invoicing' );
410
-		$localize['UseTaxes']             = wpinv_use_taxes();
411
-		$localize['checkoutNonce']        = wp_create_nonce( 'wpinv_checkout_nonce' );
412
-		$localize['formNonce']            = wp_create_nonce( 'getpaid_form_nonce' );
413
-		$localize['connectionError']      = __( 'Could not establish a connection to the server.', 'invoicing' );
414
-
415
-		$localize = apply_filters( 'wpinv_front_js_localize', $localize );
416
-
417
-		wp_enqueue_script( 'jquery-blockui' );
418
-		$autofill_api = wpinv_get_option('address_autofill_api');
419
-		$autofill_active = wpinv_get_option('address_autofill_active');
420
-		if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) {
421
-			if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) {
422
-				wp_dequeue_script( 'google-maps-api' );
423
-			}
424
-			wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false );
425
-			wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true );
426
-		}
427
-
428
-		wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' );
429
-		wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION );
430
-
431
-		wp_enqueue_script( 'wpinv-front-script' );
432
-		wp_localize_script( 'wpinv-front-script', 'WPInv', $localize );
433
-
434
-		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' );
435
-		wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ),  $version, true );
436
-	}
437
-
438
-	public function wpinv_actions() {
439
-		if ( isset( $_REQUEST['wpi_action'] ) ) {
440
-			do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST );
441
-		}
442
-	}
443
-
444
-	/**
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
+     * Tax instance.
40
+     *
41
+     * @var WPInv_EUVat
42
+     */
43
+    public $tax;
44
+
45
+    /**
46
+     * @param array An array of payment gateways.
47
+     */
48
+    public $gateways;
49
+
50
+    /**
51
+     * Class constructor.
52
+     */
53
+    public function __construct() {
54
+        $this->define_constants();
55
+        $this->includes();
56
+        $this->init_hooks();
57
+        $this->set_properties();
58
+    }
59
+
60
+    /**
61
+     * Sets a custom data property.
62
+     * 
63
+     * @param string $prop The prop to set.
64
+     * @param mixed $value The value to retrieve.
65
+     */
66
+    public function set( $prop, $value ) {
67
+        $this->data[ $prop ] = $value;
68
+    }
69
+
70
+    /**
71
+     * Gets a custom data property.
72
+     * 
73
+     * @param string $prop The prop to set.
74
+     * @return mixed The value.
75
+     */
76
+    public function get( $prop ) {
77
+
78
+        if ( isset( $this->data[ $prop ] ) ) {
79
+            return $this->data[ $prop ];
80
+        }
81
+
82
+        return null;
83
+    }
84
+
85
+    /**
86
+     * Define class properties.
87
+     */
88
+    public function set_properties() {
89
+
90
+        // Sessions.
91
+        $this->set( 'session', new WPInv_Session_Handler() );
92
+        $GLOBALS['wpi_session'] = $this->get( 'session' ); // Backwards compatibility.
93
+        $this->form_elements = new WPInv_Payment_Form_Elements();
94
+        $this->tax           = new WPInv_EUVat();
95
+        $this->tax->init();
96
+        $GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility.
97
+
98
+        // Init other objects.
99
+        $this->set( 'reports', new WPInv_Reports() ); // TODO: Refactor.
100
+        $this->set( 'session', new WPInv_Session_Handler() );
101
+        $this->set( 'notes', new WPInv_Notes() );
102
+        $this->set( 'api', new WPInv_API() );
103
+        $this->set( 'post_types', new GetPaid_Post_Types() );
104
+        $this->set( 'template', new GetPaid_Template() );
105
+        $this->set( 'admin', new GetPaid_Admin() );
106
+        $this->set( 'subscriptions', new WPInv_Subscriptions() );
107
+        $this->set( 'invoice_emails', new GetPaid_Invoice_Notification_Emails() );
108
+
109
+    }
110
+
111
+        /**
112
+         * Define plugin constants.
113
+         */
114
+    public function define_constants() {
115
+        define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) );
116
+        define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) );
117
+        $this->version = WPINV_VERSION;
118
+    }
119
+
120
+    /**
121
+     * Hook into actions and filters.
122
+     *
123
+     * @since 1.0.19
124
+     */
125
+    protected function init_hooks() {
126
+        /* Internationalize the text strings used. */
127
+        add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
128
+
129
+        // Init the plugin after WordPress inits.
130
+        add_action( 'init', array( $this, 'init' ), 1 );
131
+        add_action( 'getpaid_init', array( $this, 'maybe_process_ipn' ), 5 );
132
+        add_action( 'init', array( &$this, 'wpinv_actions' ) );
133
+        add_action( 'init', array( $this, 'maybe_do_authenticated_action' ), 100 );
134
+
135
+        if ( class_exists( 'BuddyPress' ) ) {
136
+            add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) );
137
+        }
138
+
139
+        add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
140
+        add_action( 'wp_footer', array( &$this, 'wp_footer' ) );
141
+        add_action( 'widgets_init', array( &$this, 'register_widgets' ) );
142
+        add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) );
143
+        add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
144
+
145
+        // Fires after registering actions.
146
+        do_action( 'wpinv_actions', $this );
147
+        do_action( 'getpaid_actions', $this );
148
+
149
+    }
150
+
151
+    public function plugins_loaded() {
152
+        /* Internationalize the text strings used. */
153
+        $this->load_textdomain();
154
+
155
+        do_action( 'wpinv_loaded' );
156
+
157
+        // Fix oxygen page builder conflict
158
+        if ( function_exists( 'ct_css_output' ) ) {
159
+            wpinv_oxygen_fix_conflict();
160
+        }
161
+    }
162
+
163
+    /**
164
+     * Load the translation of the plugin.
165
+     *
166
+     * @since 1.0
167
+     */
168
+    public function load_textdomain( $locale = NULL ) {
169
+        if ( empty( $locale ) ) {
170
+            $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
171
+        }
172
+
173
+        $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' );
174
+
175
+        unload_textdomain( 'invoicing' );
176
+        load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' );
177
+        load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' );
178
+
179
+        /**
180
+         * Define language constants.
181
+         */
182
+        require_once( WPINV_PLUGIN_DIR . 'language.php' );
183
+    }
184
+
185
+    /**
186
+     * Include required core files used in admin and on the frontend.
187
+     */
188
+    public function includes() {
189
+
190
+        // Start with the settings.
191
+        require_once( WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php' );
192
+
193
+        // Packages/libraries.
194
+        require_once( WPINV_PLUGIN_DIR . 'vendor/autoload.php' );
195
+        require_once( WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php' );
196
+        require_once( WPINV_PLUGIN_DIR . 'includes/libraries/action-scheduler/action-scheduler.php' );
197
+
198
+        // Load functions.
199
+        require_once( WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php' );
200
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php' );
201
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php' );
202
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php' );
203
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php' );
204
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php' );
205
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php' );
206
+        require_once( WPINV_PLUGIN_DIR . 'includes/invoice-functions.php' );
207
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php' );
208
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php' );
209
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php' );
210
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php' );
211
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php' );
212
+        require_once( WPINV_PLUGIN_DIR . 'includes/error-functions.php' );
213
+
214
+        // Register autoloader.
215
+        try {
216
+            spl_autoload_register( array( $this, 'autoload' ), true );
217
+        } catch ( Exception $e ) {
218
+            wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
219
+        }
220
+
221
+        require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php' );
222
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php' );
223
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php' );
224
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php' );
225
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php' );
226
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php' );
227
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php' );
228
+        require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' );
229
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php' );
230
+        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php' );
231
+        require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php' );
232
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' );
233
+        require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' );
234
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' );
235
+        require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' );
236
+        require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' );
237
+        require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' );
238
+        require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' );
239
+        require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' );
240
+        require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' );
241
+        require_once( WPINV_PLUGIN_DIR . 'widgets/getpaid.php' );
242
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php' );
243
+
244
+        /**
245
+         * Load the tax class.
246
+         */
247
+        if ( ! class_exists( 'WPInv_EUVat' ) ) {
248
+            require_once( WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php' );
249
+        }
250
+
251
+        $gateways = array_keys( wpinv_get_enabled_payment_gateways() );
252
+        if ( !empty( $gateways ) ) {
253
+            foreach ( $gateways as $gateway ) {
254
+                if ( $gateway == 'manual' ) {
255
+                    continue;
256
+                }
257
+
258
+                $gateway_file = WPINV_PLUGIN_DIR . 'includes/gateways/' . $gateway . '.php';
259
+
260
+                if ( file_exists( $gateway_file ) ) {
261
+                    require_once( $gateway_file );
262
+                }
263
+            }
264
+        }
265
+
266
+        if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
267
+            GetPaid_Post_Types_Admin::init();
268
+
269
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php' );
270
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php' );
271
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php' );
272
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php' );
273
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php' );
274
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php' );
275
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php' );
276
+            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php' );
277
+            // load the user class only on the users.php page
278
+            global $pagenow;
279
+            if($pagenow=='users.php'){
280
+                new WPInv_Admin_Users();
281
+            }
282
+        }
283
+
284
+        // Register cli commands
285
+        if ( defined( 'WP_CLI' ) && WP_CLI ) {
286
+            require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php' );
287
+            WP_CLI::add_command( 'invoicing', 'WPInv_CLI' );
288
+        }
289
+
290
+        // include css inliner
291
+        if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) {
292
+            include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' );
293
+        }
294
+
295
+        require_once( WPINV_PLUGIN_DIR . 'includes/admin/install.php' );
296
+    }
297
+
298
+    /**
299
+     * Class autoloader
300
+     *
301
+     * @param       string $class_name The name of the class to load.
302
+     * @access      public
303
+     * @since       1.0.19
304
+     * @return      void
305
+     */
306
+    public function autoload( $class_name ) {
307
+
308
+        // Normalize the class name...
309
+        $class_name  = strtolower( $class_name );
310
+
311
+        // ... and make sure it is our class.
312
+        if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
313
+            return;
314
+        }
315
+
316
+        // Next, prepare the file name from the class.
317
+        $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
318
+
319
+        // Base path of the classes.
320
+        $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
321
+
322
+        // And an array of possible locations in order of importance.
323
+        $locations = array(
324
+            "$plugin_path/includes",
325
+            "$plugin_path/includes/data-stores",
326
+            "$plugin_path/includes/gateways",
327
+            "$plugin_path/includes/api",
328
+            "$plugin_path/includes/admin",
329
+            "$plugin_path/includes/admin/meta-boxes",
330
+        );
331
+
332
+        foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) {
333
+
334
+            if ( file_exists( trailingslashit( $location ) . $file_name ) ) {
335
+                include trailingslashit( $location ) . $file_name;
336
+                break;
337
+            }
338
+
339
+        }
340
+
341
+    }
342
+
343
+    /**
344
+     * Inits hooks etc.
345
+     */
346
+    public function init() {
347
+
348
+        // Fires before getpaid inits.
349
+        do_action( 'before_getpaid_init', $this );
350
+
351
+        // Load default gateways.
352
+        $gateways = apply_filters(
353
+            'getpaid_default_gateways',
354
+            array(
355
+                'manual'        => 'GetPaid_Manual_Gateway',
356
+                'paypal'        => 'GetPaid_Paypal_Gateway',
357
+                'worldpay'      => 'GetPaid_Worldpay_Gateway',
358
+                'bank_transfer' => 'GetPaid_Bank_Transfer_Gateway',
359
+                'authorizenet'  => 'GetPaid_Authorize_Net_Gateway',
360
+            )
361
+        );
362
+
363
+        foreach ( $gateways as $id => $class ) {
364
+            $this->gateways[ $id ] = new $class();
365
+        }
366
+
367
+        // Fires after getpaid inits.
368
+        do_action( 'getpaid_init', $this );
369
+
370
+    }
371
+
372
+    /**
373
+     * Checks if this is an IPN request and processes it.
374
+     */
375
+    public function maybe_process_ipn() {
376
+
377
+        // Ensure that this is an IPN request.
378
+        if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) {
379
+            return;
380
+        }
381
+
382
+        $gateway = wpinv_clean( $_GET['wpi-gateway'] );
383
+
384
+        do_action( 'wpinv_verify_payment_ipn', $gateway );
385
+        do_action( "wpinv_verify_{$gateway}_ipn" );
386
+        exit;
387
+
388
+    }
389
+
390
+    public function enqueue_scripts() {
391
+        $suffix       = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
392
+
393
+        $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' );
394
+        wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version );
395
+        wp_enqueue_style( 'wpinv_front_style' );
396
+
397
+        // Register scripts
398
+        wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true );
399
+        wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ),  filemtime( WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js' ) );
400
+
401
+        $localize                         = array();
402
+        $localize['ajax_url']             = admin_url( 'admin-ajax.php' );
403
+        $localize['nonce']                = wp_create_nonce( 'wpinv-nonce' );
404
+        $localize['currency_symbol']      = wpinv_currency_symbol();
405
+        $localize['currency_pos']         = wpinv_currency_position();
406
+        $localize['thousand_sep']         = wpinv_thousands_separator();
407
+        $localize['decimal_sep']          = wpinv_decimal_separator();
408
+        $localize['decimals']             = wpinv_decimals();
409
+        $localize['txtComplete']          = __( 'Continue', 'invoicing' );
410
+        $localize['UseTaxes']             = wpinv_use_taxes();
411
+        $localize['checkoutNonce']        = wp_create_nonce( 'wpinv_checkout_nonce' );
412
+        $localize['formNonce']            = wp_create_nonce( 'getpaid_form_nonce' );
413
+        $localize['connectionError']      = __( 'Could not establish a connection to the server.', 'invoicing' );
414
+
415
+        $localize = apply_filters( 'wpinv_front_js_localize', $localize );
416
+
417
+        wp_enqueue_script( 'jquery-blockui' );
418
+        $autofill_api = wpinv_get_option('address_autofill_api');
419
+        $autofill_active = wpinv_get_option('address_autofill_active');
420
+        if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) {
421
+            if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) {
422
+                wp_dequeue_script( 'google-maps-api' );
423
+            }
424
+            wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false );
425
+            wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true );
426
+        }
427
+
428
+        wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' );
429
+        wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION );
430
+
431
+        wp_enqueue_script( 'wpinv-front-script' );
432
+        wp_localize_script( 'wpinv-front-script', 'WPInv', $localize );
433
+
434
+        $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' );
435
+        wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ),  $version, true );
436
+    }
437
+
438
+    public function wpinv_actions() {
439
+        if ( isset( $_REQUEST['wpi_action'] ) ) {
440
+            do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST );
441
+        }
442
+    }
443
+
444
+    /**
445 445
      * Fires an action after verifying that a user can fire them.
446
-	 *
447
-	 * Note: If the action is on an invoice, subscription etc, esure that the
448
-	 * current user owns the invoice/subscription.
446
+     *
447
+     * Note: If the action is on an invoice, subscription etc, esure that the
448
+     * current user owns the invoice/subscription.
449 449
      */
450 450
     public function maybe_do_authenticated_action() {
451 451
 
@@ -456,82 +456,82 @@  discard block
 block discarded – undo
456 456
 
457 457
     }
458 458
 
459
-	public function pre_get_posts( $wp_query ) {
460
-		if ( ! is_admin() && !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) {
461
-			$wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() );
462
-		}
463
-
464
-		return $wp_query;
465
-	}
466
-
467
-	public function bp_invoicing_init() {
468
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' );
469
-	}
470
-
471
-	/**
472
-	 * Register widgets
473
-	 *
474
-	 */
475
-	public function register_widgets() {
476
-		$widgets = apply_filters(
477
-			'getpaid_widget_classes',
478
-			array(
479
-				'WPInv_Checkout_Widget',
480
-				'WPInv_History_Widget',
481
-				'WPInv_Receipt_Widget',
482
-				'WPInv_Subscriptions_Widget',
483
-				'WPInv_Buy_Item_Widget',
484
-				'WPInv_Messages_Widget',
485
-				'WPInv_GetPaid_Widget'
486
-			)
487
-		);
488
-
489
-		foreach ( $widgets as $widget ) {
490
-			register_widget( $widget );
491
-		}
459
+    public function pre_get_posts( $wp_query ) {
460
+        if ( ! is_admin() && !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) {
461
+            $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() );
462
+        }
463
+
464
+        return $wp_query;
465
+    }
466
+
467
+    public function bp_invoicing_init() {
468
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' );
469
+    }
470
+
471
+    /**
472
+     * Register widgets
473
+     *
474
+     */
475
+    public function register_widgets() {
476
+        $widgets = apply_filters(
477
+            'getpaid_widget_classes',
478
+            array(
479
+                'WPInv_Checkout_Widget',
480
+                'WPInv_History_Widget',
481
+                'WPInv_Receipt_Widget',
482
+                'WPInv_Subscriptions_Widget',
483
+                'WPInv_Buy_Item_Widget',
484
+                'WPInv_Messages_Widget',
485
+                'WPInv_GetPaid_Widget'
486
+            )
487
+        );
488
+
489
+        foreach ( $widgets as $widget ) {
490
+            register_widget( $widget );
491
+        }
492 492
 		
493
-	}
493
+    }
494 494
 
495
-	/**
496
-	 * Remove our pages from yoast sitemaps.
497
-	 *
498
-	 * @since 1.0.19
499
-	 * @param int[] $excluded_posts_ids
500
-	 */
501
-	public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){
495
+    /**
496
+     * Remove our pages from yoast sitemaps.
497
+     *
498
+     * @since 1.0.19
499
+     * @param int[] $excluded_posts_ids
500
+     */
501
+    public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){
502 502
 
503
-		// Ensure that we have an array.
504
-		if ( ! is_array( $excluded_posts_ids ) ) {
505
-			$excluded_posts_ids = array();
506
-		}
503
+        // Ensure that we have an array.
504
+        if ( ! is_array( $excluded_posts_ids ) ) {
505
+            $excluded_posts_ids = array();
506
+        }
507 507
 
508
-		// Prepare our pages.
509
-		$our_pages = array();
508
+        // Prepare our pages.
509
+        $our_pages = array();
510 510
 
511
-		// Checkout page.
512
-		$our_pages[] = wpinv_get_option( 'checkout_page', false );
511
+        // Checkout page.
512
+        $our_pages[] = wpinv_get_option( 'checkout_page', false );
513 513
 
514
-		// Success page.
515
-		$our_pages[] = wpinv_get_option( 'success_page', false );
514
+        // Success page.
515
+        $our_pages[] = wpinv_get_option( 'success_page', false );
516 516
 
517
-		// Failure page.
518
-		$our_pages[] = wpinv_get_option( 'failure_page', false );
517
+        // Failure page.
518
+        $our_pages[] = wpinv_get_option( 'failure_page', false );
519 519
 
520
-		// History page.
521
-		$our_pages[] = wpinv_get_option( 'invoice_history_page', false );
520
+        // History page.
521
+        $our_pages[] = wpinv_get_option( 'invoice_history_page', false );
522 522
 
523
-		// Subscriptions page.
524
-		$our_pages[] = wpinv_get_option( 'invoice_subscription_page', false );
523
+        // Subscriptions page.
524
+        $our_pages[] = wpinv_get_option( 'invoice_subscription_page', false );
525 525
 
526
-		$our_pages   = array_map( 'intval', array_filter( $our_pages ) );
526
+        $our_pages   = array_map( 'intval', array_filter( $our_pages ) );
527 527
 
528
-		$excluded_posts_ids = $excluded_posts_ids + $our_pages;
529
-		return array_unique( $excluded_posts_ids );
528
+        $excluded_posts_ids = $excluded_posts_ids + $our_pages;
529
+        return array_unique( $excluded_posts_ids );
530 530
 
531
-	}
531
+    }
532 532
 
533
-	public function wp_footer() {
534
-		echo '
533
+    public function wp_footer() {
534
+        echo '
535 535
 			<div class="bsui">
536 536
 				<div  id="getpaid-payment-modal" class="modal" tabindex="-1" role="dialog">
537 537
 					<div class="modal-dialog modal-dialog-centered modal-lg" role="checkout" style="max-width: 650px;">
@@ -542,6 +542,6 @@  discard block
 block discarded – undo
542 542
 				</div>
543 543
 			</div>
544 544
 		';
545
-	}
545
+    }
546 546
 
547 547
 }
Please login to merge, or discard this patch.
Spacing   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
  * @since   1.0.0
7 7
  */
8 8
 
9
-defined( 'ABSPATH' ) || exit;
9
+defined('ABSPATH') || exit;
10 10
 
11 11
 /**
12 12
  * Main Invoicing class.
@@ -63,8 +63,8 @@  discard block
 block discarded – undo
63 63
 	 * @param string $prop The prop to set.
64 64
 	 * @param mixed $value The value to retrieve.
65 65
 	 */
66
-	public function set( $prop, $value ) {
67
-		$this->data[ $prop ] = $value;
66
+	public function set($prop, $value) {
67
+		$this->data[$prop] = $value;
68 68
 	}
69 69
 
70 70
 	/**
@@ -73,10 +73,10 @@  discard block
 block discarded – undo
73 73
 	 * @param string $prop The prop to set.
74 74
 	 * @return mixed The value.
75 75
 	 */
76
-	public function get( $prop ) {
76
+	public function get($prop) {
77 77
 
78
-		if ( isset( $this->data[ $prop ] ) ) {
79
-			return $this->data[ $prop ];
78
+		if (isset($this->data[$prop])) {
79
+			return $this->data[$prop];
80 80
 		}
81 81
 
82 82
 		return null;
@@ -88,23 +88,23 @@  discard block
 block discarded – undo
88 88
 	public function set_properties() {
89 89
 
90 90
 		// Sessions.
91
-		$this->set( 'session', new WPInv_Session_Handler() );
92
-		$GLOBALS['wpi_session'] = $this->get( 'session' ); // Backwards compatibility.
91
+		$this->set('session', new WPInv_Session_Handler());
92
+		$GLOBALS['wpi_session'] = $this->get('session'); // Backwards compatibility.
93 93
 		$this->form_elements = new WPInv_Payment_Form_Elements();
94 94
 		$this->tax           = new WPInv_EUVat();
95 95
 		$this->tax->init();
96 96
 		$GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility.
97 97
 
98 98
 		// Init other objects.
99
-		$this->set( 'reports', new WPInv_Reports() ); // TODO: Refactor.
100
-		$this->set( 'session', new WPInv_Session_Handler() );
101
-		$this->set( 'notes', new WPInv_Notes() );
102
-		$this->set( 'api', new WPInv_API() );
103
-		$this->set( 'post_types', new GetPaid_Post_Types() );
104
-		$this->set( 'template', new GetPaid_Template() );
105
-		$this->set( 'admin', new GetPaid_Admin() );
106
-		$this->set( 'subscriptions', new WPInv_Subscriptions() );
107
-		$this->set( 'invoice_emails', new GetPaid_Invoice_Notification_Emails() );
99
+		$this->set('reports', new WPInv_Reports()); // TODO: Refactor.
100
+		$this->set('session', new WPInv_Session_Handler());
101
+		$this->set('notes', new WPInv_Notes());
102
+		$this->set('api', new WPInv_API());
103
+		$this->set('post_types', new GetPaid_Post_Types());
104
+		$this->set('template', new GetPaid_Template());
105
+		$this->set('admin', new GetPaid_Admin());
106
+		$this->set('subscriptions', new WPInv_Subscriptions());
107
+		$this->set('invoice_emails', new GetPaid_Invoice_Notification_Emails());
108 108
 
109 109
 	}
110 110
 
@@ -112,8 +112,8 @@  discard block
 block discarded – undo
112 112
 	 * Define plugin constants.
113 113
 	 */
114 114
 	public function define_constants() {
115
-		define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) );
116
-		define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) );
115
+		define('WPINV_PLUGIN_DIR', plugin_dir_path(WPINV_PLUGIN_FILE));
116
+		define('WPINV_PLUGIN_URL', plugin_dir_url(WPINV_PLUGIN_FILE));
117 117
 		$this->version = WPINV_VERSION;
118 118
 	}
119 119
 
@@ -124,27 +124,27 @@  discard block
 block discarded – undo
124 124
 	 */
125 125
 	protected function init_hooks() {
126 126
 		/* Internationalize the text strings used. */
127
-		add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
127
+		add_action('plugins_loaded', array(&$this, 'plugins_loaded'));
128 128
 
129 129
 		// Init the plugin after WordPress inits.
130
-		add_action( 'init', array( $this, 'init' ), 1 );
131
-		add_action( 'getpaid_init', array( $this, 'maybe_process_ipn' ), 5 );
132
-		add_action( 'init', array( &$this, 'wpinv_actions' ) );
133
-		add_action( 'init', array( $this, 'maybe_do_authenticated_action' ), 100 );
130
+		add_action('init', array($this, 'init'), 1);
131
+		add_action('getpaid_init', array($this, 'maybe_process_ipn'), 5);
132
+		add_action('init', array(&$this, 'wpinv_actions'));
133
+		add_action('init', array($this, 'maybe_do_authenticated_action'), 100);
134 134
 
135
-		if ( class_exists( 'BuddyPress' ) ) {
136
-			add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) );
135
+		if (class_exists('BuddyPress')) {
136
+			add_action('bp_include', array(&$this, 'bp_invoicing_init'));
137 137
 		}
138 138
 
139
-		add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
140
-		add_action( 'wp_footer', array( &$this, 'wp_footer' ) );
141
-		add_action( 'widgets_init', array( &$this, 'register_widgets' ) );
142
-		add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) );
143
-		add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
139
+		add_action('wp_enqueue_scripts', array(&$this, 'enqueue_scripts'));
140
+		add_action('wp_footer', array(&$this, 'wp_footer'));
141
+		add_action('widgets_init', array(&$this, 'register_widgets'));
142
+		add_filter('wpseo_exclude_from_sitemap_by_post_ids', array($this, 'wpseo_exclude_from_sitemap_by_post_ids'));
143
+		add_filter('pre_get_posts', array(&$this, 'pre_get_posts'));
144 144
 
145 145
 		// Fires after registering actions.
146
-		do_action( 'wpinv_actions', $this );
147
-		do_action( 'getpaid_actions', $this );
146
+		do_action('wpinv_actions', $this);
147
+		do_action('getpaid_actions', $this);
148 148
 
149 149
 	}
150 150
 
@@ -152,10 +152,10 @@  discard block
 block discarded – undo
152 152
 		/* Internationalize the text strings used. */
153 153
 		$this->load_textdomain();
154 154
 
155
-		do_action( 'wpinv_loaded' );
155
+		do_action('wpinv_loaded');
156 156
 
157 157
 		// Fix oxygen page builder conflict
158
-		if ( function_exists( 'ct_css_output' ) ) {
158
+		if (function_exists('ct_css_output')) {
159 159
 			wpinv_oxygen_fix_conflict();
160 160
 		}
161 161
 	}
@@ -165,21 +165,21 @@  discard block
 block discarded – undo
165 165
 	 *
166 166
 	 * @since 1.0
167 167
 	 */
168
-	public function load_textdomain( $locale = NULL ) {
169
-		if ( empty( $locale ) ) {
170
-			$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
168
+	public function load_textdomain($locale = NULL) {
169
+		if (empty($locale)) {
170
+			$locale = is_admin() && function_exists('get_user_locale') ? get_user_locale() : get_locale();
171 171
 		}
172 172
 
173
-		$locale = apply_filters( 'plugin_locale', $locale, 'invoicing' );
173
+		$locale = apply_filters('plugin_locale', $locale, 'invoicing');
174 174
 
175
-		unload_textdomain( 'invoicing' );
176
-		load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' );
177
-		load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' );
175
+		unload_textdomain('invoicing');
176
+		load_textdomain('invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo');
177
+		load_plugin_textdomain('invoicing', false, WPINV_PLUGIN_DIR . 'languages');
178 178
 
179 179
 		/**
180 180
 		 * Define language constants.
181 181
 		 */
182
-		require_once( WPINV_PLUGIN_DIR . 'language.php' );
182
+		require_once(WPINV_PLUGIN_DIR . 'language.php');
183 183
 	}
184 184
 
185 185
 	/**
@@ -188,111 +188,111 @@  discard block
 block discarded – undo
188 188
 	public function includes() {
189 189
 
190 190
 		// Start with the settings.
191
-		require_once( WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php' );
191
+		require_once(WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php');
192 192
 
193 193
 		// Packages/libraries.
194
-		require_once( WPINV_PLUGIN_DIR . 'vendor/autoload.php' );
195
-		require_once( WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php' );
196
-		require_once( WPINV_PLUGIN_DIR . 'includes/libraries/action-scheduler/action-scheduler.php' );
194
+		require_once(WPINV_PLUGIN_DIR . 'vendor/autoload.php');
195
+		require_once(WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php');
196
+		require_once(WPINV_PLUGIN_DIR . 'includes/libraries/action-scheduler/action-scheduler.php');
197 197
 
198 198
 		// Load functions.
199
-		require_once( WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php' );
200
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php' );
201
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php' );
202
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php' );
203
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php' );
204
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php' );
205
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php' );
206
-		require_once( WPINV_PLUGIN_DIR . 'includes/invoice-functions.php' );
207
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php' );
208
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php' );
209
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php' );
210
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php' );
211
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php' );
212
-		require_once( WPINV_PLUGIN_DIR . 'includes/error-functions.php' );
199
+		require_once(WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php');
200
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php');
201
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php');
202
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php');
203
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php');
204
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php');
205
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php');
206
+		require_once(WPINV_PLUGIN_DIR . 'includes/invoice-functions.php');
207
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php');
208
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php');
209
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php');
210
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php');
211
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php');
212
+		require_once(WPINV_PLUGIN_DIR . 'includes/error-functions.php');
213 213
 
214 214
 		// Register autoloader.
215 215
 		try {
216
-			spl_autoload_register( array( $this, 'autoload' ), true );
217
-		} catch ( Exception $e ) {
218
-			wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
216
+			spl_autoload_register(array($this, 'autoload'), true);
217
+		} catch (Exception $e) {
218
+			wpinv_error_log($e->getMessage(), '', __FILE__, 149, true);
219 219
 		}
220 220
 
221
-		require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php' );
222
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php' );
223
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php' );
224
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php' );
225
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php' );
226
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php' );
227
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php' );
228
-		require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' );
229
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php' );
230
-		require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php' );
231
-		require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php' );
232
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' );
233
-		require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' );
234
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' );
235
-		require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' );
236
-		require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' );
237
-		require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' );
238
-		require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' );
239
-		require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' );
240
-		require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' );
241
-		require_once( WPINV_PLUGIN_DIR . 'widgets/getpaid.php' );
242
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php' );
221
+		require_once(WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php');
222
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php');
223
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php');
224
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php');
225
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php');
226
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php');
227
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php');
228
+		require_once(WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php');
229
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php');
230
+		require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php');
231
+		require_once(WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php');
232
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php');
233
+		require_once(WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php');
234
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php');
235
+		require_once(WPINV_PLUGIN_DIR . 'widgets/checkout.php');
236
+		require_once(WPINV_PLUGIN_DIR . 'widgets/invoice-history.php');
237
+		require_once(WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php');
238
+		require_once(WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php');
239
+		require_once(WPINV_PLUGIN_DIR . 'widgets/subscriptions.php');
240
+		require_once(WPINV_PLUGIN_DIR . 'widgets/buy-item.php');
241
+		require_once(WPINV_PLUGIN_DIR . 'widgets/getpaid.php');
242
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php');
243 243
 
244 244
 		/**
245 245
 		 * Load the tax class.
246 246
 		 */
247
-		if ( ! class_exists( 'WPInv_EUVat' ) ) {
248
-			require_once( WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php' );
247
+		if (!class_exists('WPInv_EUVat')) {
248
+			require_once(WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php');
249 249
 		}
250 250
 
251
-		$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
252
-		if ( !empty( $gateways ) ) {
253
-			foreach ( $gateways as $gateway ) {
254
-				if ( $gateway == 'manual' ) {
251
+		$gateways = array_keys(wpinv_get_enabled_payment_gateways());
252
+		if (!empty($gateways)) {
253
+			foreach ($gateways as $gateway) {
254
+				if ($gateway == 'manual') {
255 255
 					continue;
256 256
 				}
257 257
 
258 258
 				$gateway_file = WPINV_PLUGIN_DIR . 'includes/gateways/' . $gateway . '.php';
259 259
 
260
-				if ( file_exists( $gateway_file ) ) {
261
-					require_once( $gateway_file );
260
+				if (file_exists($gateway_file)) {
261
+					require_once($gateway_file);
262 262
 				}
263 263
 			}
264 264
 		}
265 265
 
266
-		if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
266
+		if (is_admin() || (defined('WP_CLI') && WP_CLI)) {
267 267
 			GetPaid_Post_Types_Admin::init();
268 268
 
269
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php' );
270
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php' );
271
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php' );
272
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php' );
273
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php' );
274
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php' );
275
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php' );
276
-			require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php' );
269
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php');
270
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php');
271
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php');
272
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php');
273
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php');
274
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php');
275
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php');
276
+			require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php');
277 277
 			// load the user class only on the users.php page
278 278
 			global $pagenow;
279
-			if($pagenow=='users.php'){
279
+			if ($pagenow == 'users.php') {
280 280
 				new WPInv_Admin_Users();
281 281
 			}
282 282
 		}
283 283
 
284 284
 		// Register cli commands
285
-		if ( defined( 'WP_CLI' ) && WP_CLI ) {
286
-			require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php' );
287
-			WP_CLI::add_command( 'invoicing', 'WPInv_CLI' );
285
+		if (defined('WP_CLI') && WP_CLI) {
286
+			require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php');
287
+			WP_CLI::add_command('invoicing', 'WPInv_CLI');
288 288
 		}
289 289
 
290 290
 		// include css inliner
291
-		if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) {
292
-			include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' );
291
+		if (!class_exists('Emogrifier') && class_exists('DOMDocument')) {
292
+			include_once(WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php');
293 293
 		}
294 294
 
295
-		require_once( WPINV_PLUGIN_DIR . 'includes/admin/install.php' );
295
+		require_once(WPINV_PLUGIN_DIR . 'includes/admin/install.php');
296 296
 	}
297 297
 
298 298
 	/**
@@ -303,21 +303,21 @@  discard block
 block discarded – undo
303 303
 	 * @since       1.0.19
304 304
 	 * @return      void
305 305
 	 */
306
-	public function autoload( $class_name ) {
306
+	public function autoload($class_name) {
307 307
 
308 308
 		// Normalize the class name...
309
-		$class_name  = strtolower( $class_name );
309
+		$class_name = strtolower($class_name);
310 310
 
311 311
 		// ... and make sure it is our class.
312
-		if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
312
+		if (false === strpos($class_name, 'getpaid_') && false === strpos($class_name, 'wpinv_')) {
313 313
 			return;
314 314
 		}
315 315
 
316 316
 		// Next, prepare the file name from the class.
317
-		$file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
317
+		$file_name = 'class-' . str_replace('_', '-', $class_name) . '.php';
318 318
 
319 319
 		// Base path of the classes.
320
-		$plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
320
+		$plugin_path = untrailingslashit(WPINV_PLUGIN_DIR);
321 321
 
322 322
 		// And an array of possible locations in order of importance.
323 323
 		$locations = array(
@@ -329,10 +329,10 @@  discard block
 block discarded – undo
329 329
 			"$plugin_path/includes/admin/meta-boxes",
330 330
 		);
331 331
 
332
-		foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) {
332
+		foreach (apply_filters('getpaid_autoload_locations', $locations) as $location) {
333 333
 
334
-			if ( file_exists( trailingslashit( $location ) . $file_name ) ) {
335
-				include trailingslashit( $location ) . $file_name;
334
+			if (file_exists(trailingslashit($location) . $file_name)) {
335
+				include trailingslashit($location) . $file_name;
336 336
 				break;
337 337
 			}
338 338
 
@@ -346,7 +346,7 @@  discard block
 block discarded – undo
346 346
 	public function init() {
347 347
 
348 348
 		// Fires before getpaid inits.
349
-		do_action( 'before_getpaid_init', $this );
349
+		do_action('before_getpaid_init', $this);
350 350
 
351 351
 		// Load default gateways.
352 352
 		$gateways = apply_filters(
@@ -360,12 +360,12 @@  discard block
 block discarded – undo
360 360
 			)
361 361
 		);
362 362
 
363
-		foreach ( $gateways as $id => $class ) {
364
-			$this->gateways[ $id ] = new $class();
363
+		foreach ($gateways as $id => $class) {
364
+			$this->gateways[$id] = new $class();
365 365
 		}
366 366
 
367 367
 		// Fires after getpaid inits.
368
-		do_action( 'getpaid_init', $this );
368
+		do_action('getpaid_init', $this);
369 369
 
370 370
 	}
371 371
 
@@ -375,69 +375,69 @@  discard block
 block discarded – undo
375 375
 	public function maybe_process_ipn() {
376 376
 
377 377
 		// Ensure that this is an IPN request.
378
-		if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) {
378
+		if (empty($_GET['wpi-listener']) || 'IPN' !== $_GET['wpi-listener'] || empty($_GET['wpi-gateway'])) {
379 379
 			return;
380 380
 		}
381 381
 
382
-		$gateway = wpinv_clean( $_GET['wpi-gateway'] );
382
+		$gateway = wpinv_clean($_GET['wpi-gateway']);
383 383
 
384
-		do_action( 'wpinv_verify_payment_ipn', $gateway );
385
-		do_action( "wpinv_verify_{$gateway}_ipn" );
384
+		do_action('wpinv_verify_payment_ipn', $gateway);
385
+		do_action("wpinv_verify_{$gateway}_ipn");
386 386
 		exit;
387 387
 
388 388
 	}
389 389
 
390 390
 	public function enqueue_scripts() {
391
-		$suffix       = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
391
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
392 392
 
393
-		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' );
394
-		wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version );
395
-		wp_enqueue_style( 'wpinv_front_style' );
393
+		$version = filemtime(WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css');
394
+		wp_register_style('wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version);
395
+		wp_enqueue_style('wpinv_front_style');
396 396
 
397 397
 		// Register scripts
398
-		wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true );
399
-		wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ),  filemtime( WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js' ) );
398
+		wp_register_script('jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array('jquery'), '2.70', true);
399
+		wp_register_script('wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array('jquery'), filemtime(WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js'));
400 400
 
401 401
 		$localize                         = array();
402
-		$localize['ajax_url']             = admin_url( 'admin-ajax.php' );
403
-		$localize['nonce']                = wp_create_nonce( 'wpinv-nonce' );
402
+		$localize['ajax_url']             = admin_url('admin-ajax.php');
403
+		$localize['nonce']                = wp_create_nonce('wpinv-nonce');
404 404
 		$localize['currency_symbol']      = wpinv_currency_symbol();
405 405
 		$localize['currency_pos']         = wpinv_currency_position();
406 406
 		$localize['thousand_sep']         = wpinv_thousands_separator();
407 407
 		$localize['decimal_sep']          = wpinv_decimal_separator();
408 408
 		$localize['decimals']             = wpinv_decimals();
409
-		$localize['txtComplete']          = __( 'Continue', 'invoicing' );
409
+		$localize['txtComplete']          = __('Continue', 'invoicing');
410 410
 		$localize['UseTaxes']             = wpinv_use_taxes();
411
-		$localize['checkoutNonce']        = wp_create_nonce( 'wpinv_checkout_nonce' );
412
-		$localize['formNonce']            = wp_create_nonce( 'getpaid_form_nonce' );
413
-		$localize['connectionError']      = __( 'Could not establish a connection to the server.', 'invoicing' );
411
+		$localize['checkoutNonce']        = wp_create_nonce('wpinv_checkout_nonce');
412
+		$localize['formNonce']            = wp_create_nonce('getpaid_form_nonce');
413
+		$localize['connectionError']      = __('Could not establish a connection to the server.', 'invoicing');
414 414
 
415
-		$localize = apply_filters( 'wpinv_front_js_localize', $localize );
415
+		$localize = apply_filters('wpinv_front_js_localize', $localize);
416 416
 
417
-		wp_enqueue_script( 'jquery-blockui' );
417
+		wp_enqueue_script('jquery-blockui');
418 418
 		$autofill_api = wpinv_get_option('address_autofill_api');
419 419
 		$autofill_active = wpinv_get_option('address_autofill_active');
420
-		if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) {
421
-			if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) {
422
-				wp_dequeue_script( 'google-maps-api' );
420
+		if (isset($autofill_active) && 1 == $autofill_active && !empty($autofill_api) && wpinv_is_checkout()) {
421
+			if (wp_script_is('google-maps-api', 'enqueued')) {
422
+				wp_dequeue_script('google-maps-api');
423 423
 			}
424
-			wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false );
425
-			wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true );
424
+			wp_enqueue_script('google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array('jquery'), '', false);
425
+			wp_enqueue_script('google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array('jquery', 'google-maps-api'), '', true);
426 426
 		}
427 427
 
428
-		wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' );
429
-		wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION );
428
+		wp_enqueue_style("select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all');
429
+		wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array('jquery'), WPINV_VERSION);
430 430
 
431
-		wp_enqueue_script( 'wpinv-front-script' );
432
-		wp_localize_script( 'wpinv-front-script', 'WPInv', $localize );
431
+		wp_enqueue_script('wpinv-front-script');
432
+		wp_localize_script('wpinv-front-script', 'WPInv', $localize);
433 433
 
434
-		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' );
435
-		wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ),  $version, true );
434
+		$version = filemtime(WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js');
435
+		wp_enqueue_script('wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array('wpinv-front-script', 'wp-hooks'), $version, true);
436 436
 	}
437 437
 
438 438
 	public function wpinv_actions() {
439
-		if ( isset( $_REQUEST['wpi_action'] ) ) {
440
-			do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST );
439
+		if (isset($_REQUEST['wpi_action'])) {
440
+			do_action('wpinv_' . wpinv_sanitize_key($_REQUEST['wpi_action']), $_REQUEST);
441 441
 		}
442 442
 	}
443 443
 
@@ -449,23 +449,23 @@  discard block
 block discarded – undo
449 449
      */
450 450
     public function maybe_do_authenticated_action() {
451 451
 
452
-        if ( is_user_logged_in() && isset( $_REQUEST['getpaid-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
453
-            $key = sanitize_key( $_REQUEST['getpaid-action'] );
454
-            do_action( "getpaid_authenticated_action_$key", $_REQUEST );
452
+        if (is_user_logged_in() && isset($_REQUEST['getpaid-action']) && isset($_REQUEST['getpaid-nonce']) && wp_verify_nonce($_REQUEST['getpaid-nonce'], 'getpaid-nonce')) {
453
+            $key = sanitize_key($_REQUEST['getpaid-action']);
454
+            do_action("getpaid_authenticated_action_$key", $_REQUEST);
455 455
         }
456 456
 
457 457
     }
458 458
 
459
-	public function pre_get_posts( $wp_query ) {
460
-		if ( ! is_admin() && !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) {
461
-			$wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() );
459
+	public function pre_get_posts($wp_query) {
460
+		if (!is_admin() && !empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query()) {
461
+			$wp_query->query_vars['post_status'] = array_keys(wpinv_get_invoice_statuses());
462 462
 		}
463 463
 
464 464
 		return $wp_query;
465 465
 	}
466 466
 
467 467
 	public function bp_invoicing_init() {
468
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' );
468
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php');
469 469
 	}
470 470
 
471 471
 	/**
@@ -486,8 +486,8 @@  discard block
 block discarded – undo
486 486
 			)
487 487
 		);
488 488
 
489
-		foreach ( $widgets as $widget ) {
490
-			register_widget( $widget );
489
+		foreach ($widgets as $widget) {
490
+			register_widget($widget);
491 491
 		}
492 492
 		
493 493
 	}
@@ -498,10 +498,10 @@  discard block
 block discarded – undo
498 498
 	 * @since 1.0.19
499 499
 	 * @param int[] $excluded_posts_ids
500 500
 	 */
501
-	public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){
501
+	public function wpseo_exclude_from_sitemap_by_post_ids($excluded_posts_ids) {
502 502
 
503 503
 		// Ensure that we have an array.
504
-		if ( ! is_array( $excluded_posts_ids ) ) {
504
+		if (!is_array($excluded_posts_ids)) {
505 505
 			$excluded_posts_ids = array();
506 506
 		}
507 507
 
@@ -509,24 +509,24 @@  discard block
 block discarded – undo
509 509
 		$our_pages = array();
510 510
 
511 511
 		// Checkout page.
512
-		$our_pages[] = wpinv_get_option( 'checkout_page', false );
512
+		$our_pages[] = wpinv_get_option('checkout_page', false);
513 513
 
514 514
 		// Success page.
515
-		$our_pages[] = wpinv_get_option( 'success_page', false );
515
+		$our_pages[] = wpinv_get_option('success_page', false);
516 516
 
517 517
 		// Failure page.
518
-		$our_pages[] = wpinv_get_option( 'failure_page', false );
518
+		$our_pages[] = wpinv_get_option('failure_page', false);
519 519
 
520 520
 		// History page.
521
-		$our_pages[] = wpinv_get_option( 'invoice_history_page', false );
521
+		$our_pages[] = wpinv_get_option('invoice_history_page', false);
522 522
 
523 523
 		// Subscriptions page.
524
-		$our_pages[] = wpinv_get_option( 'invoice_subscription_page', false );
524
+		$our_pages[] = wpinv_get_option('invoice_subscription_page', false);
525 525
 
526
-		$our_pages   = array_map( 'intval', array_filter( $our_pages ) );
526
+		$our_pages   = array_map('intval', array_filter($our_pages));
527 527
 
528 528
 		$excluded_posts_ids = $excluded_posts_ids + $our_pages;
529
-		return array_unique( $excluded_posts_ids );
529
+		return array_unique($excluded_posts_ids);
530 530
 
531 531
 	}
532 532
 
Please login to merge, or discard this patch.
includes/api/class-getpaid-rest-crud-controller.php 2 patches
Indentation   +471 added lines, -471 removed lines patch added patch discarded remove patch
@@ -17,476 +17,476 @@
 block discarded – undo
17 17
  */
18 18
 class GetPaid_REST_CRUD_Controller extends GetPaid_REST_Controller {
19 19
 
20
-	/**
21
-	 * Contains this controller's class name.
22
-	 *
23
-	 * @var string
24
-	 */
25
-	public $crud_class;
26
-
27
-	/**
28
-	 * Contains the current CRUD object.
29
-	 *
30
-	 * @var GetPaid_Data
31
-	 */
32
-	protected $data_object;
33
-
34
-	/**
35
-	 * Registers the routes for the objects of the controller.
36
-	 *
37
-	 * @since 1.0.19
38
-	 *
39
-	 * @see register_rest_route()
40
-	 */
41
-	public function register_namespace_routes( $namespace ) {
42
-
43
-		register_rest_route(
44
-			$namespace,
45
-			'/' . $this->rest_base,
46
-			array(
47
-				array(
48
-					'methods'             => WP_REST_Server::READABLE,
49
-					'callback'            => array( $this, 'get_items' ),
50
-					'permission_callback' => array( $this, 'get_items_permissions_check' ),
51
-					'args'                => $this->get_collection_params(),
52
-				),
53
-				array(
54
-					'methods'             => WP_REST_Server::CREATABLE,
55
-					'callback'            => array( $this, 'create_item' ),
56
-					'permission_callback' => array( $this, 'create_item_permissions_check' ),
57
-					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
58
-				),
59
-				'schema' => array( $this, 'get_public_item_schema' ),
60
-			)
61
-		);
62
-
63
-		$get_item_args = array(
64
-			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
65
-		);
66
-
67
-		register_rest_route(
68
-			$namespace,
69
-			'/' . $this->rest_base . '/(?P<id>[\d]+)',
70
-			array(
71
-				'args'   => array(
72
-					'id' => array(
73
-						'description' => __( 'Unique identifier for the object.', 'invoicing' ),
74
-						'type'        => 'integer',
75
-					),
76
-				),
77
-				array(
78
-					'methods'             => WP_REST_Server::READABLE,
79
-					'callback'            => array( $this, 'get_item' ),
80
-					'permission_callback' => array( $this, 'get_item_permissions_check' ),
81
-					'args'                => $get_item_args,
82
-				),
83
-				array(
84
-					'methods'             => WP_REST_Server::EDITABLE,
85
-					'callback'            => array( $this, 'update_item' ),
86
-					'permission_callback' => array( $this, 'update_item_permissions_check' ),
87
-					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
88
-				),
89
-				array(
90
-					'methods'             => WP_REST_Server::DELETABLE,
91
-					'callback'            => array( $this, 'delete_item' ),
92
-					'permission_callback' => array( $this, 'delete_item_permissions_check' ),
93
-					'args'                => array(
94
-						'force' => array(
95
-							'type'        => 'boolean',
96
-							'default'     => false,
97
-							'description' => __( 'Whether to bypass Trash and force deletion.', 'invoicing' ),
98
-						),
99
-					),
100
-				),
101
-				'schema' => array( $this, 'get_public_item_schema' ),
102
-			)
103
-		);
104
-
105
-	}
106
-
107
-	/**
108
-	 * Saves a single object.
109
-	 *
110
-	 * @param GetPaid_Data $object Object to save.
111
-	 * @return WP_Error|GetPaid_Data
112
-	 */
113
-	protected function save_object( $object ) {
114
-		$object->save();
115
-
116
-		if ( ! empty( $object->last_error ) ) {
117
-			return new WP_Error( 'rest_cannot_save', $object->last_error, array( 'status' => 400 ) );
118
-		}
119
-
120
-		return new $this->crud_class( $object->get_id() );
121
-	}
122
-
123
-	/**
124
-	 * Retrieves a single object.
125
-	 *
126
-	 * @since 1.0.13
127
-	 *
128
-	 * @param int|WP_Post $object_id Supplied ID.
129
-	 * @return GetPaid_Data|WP_Error GetPaid_Data object if ID is valid, WP_Error otherwise.
130
-	 */
131
-	protected function get_object( $object_id ) {
132
-
133
-		// Do we have an object?
134
-		if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
135
-			return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
136
-		}
137
-
138
-		// Fetch the object.
139
-		$object = new $this->crud_class( $object_id );
140
-		if ( ! empty( $object->last_error ) ) {
141
-			return new WP_Error( 'rest_object_invalid_id', $object->last_error, array( 'status' => 404 ) );
142
-		}
143
-
144
-		$this->data_object = $object;
145
-		return $object->get_id() ? $object : new WP_Error( 'rest_object_invalid_id', __( 'Invalid ID.', 'invoicing' ), array( 'status' => 404 ) );
146
-
147
-	}
148
-
149
-	/**
150
-	 * Get a single object.
151
-	 *
152
-	 * @param WP_REST_Request $request Full details about the request.
153
-	 * @return WP_Error|WP_REST_Response
154
-	 */
155
-	public function get_item( $request ) {
156
-
157
-		// Fetch the item.
158
-		$object = $this->get_object( $request['id'] );
159
-
160
-		if ( is_wp_error( $object ) ) {
161
-			return $object;
162
-		}
163
-
164
-		// Generate a response.
165
-		return rest_ensure_response( $this->prepare_item_for_response( $object, $request ) );
166
-
167
-	}
168
-
169
-	/**
170
-	 * Create a single object.
171
-	 *
172
-	 * @param WP_REST_Request $request Full details about the request.
173
-	 * @return WP_Error|WP_REST_Response
174
-	 */
175
-	public function create_item( $request ) {
176
-
177
-		// Can not create an existing item.
178
-		if ( ! empty( $request['id'] ) ) {
179
-			/* translators: %s: post type */
180
-			return new WP_Error( "getpaid_rest_{$this->rest_base}_exists", __( 'Cannot create existing resource.', 'invoicing' ), array( 'status' => 400 ) );
181
-		}
182
-
183
-		// Generate a GetPaid_Data object from the request.
184
-		$object = $this->prepare_item_for_database( $request );
185
-		if ( is_wp_error( $object ) ) {
186
-			return $object;
187
-		}
188
-
189
-		// Save the object.
190
-		$object = $this->save_object( $object );
191
-		if ( is_wp_error( $object ) ) {
192
-			return $object;
193
-		}
194
-
195
-		// Save special fields.
196
-		$save_special = $this->update_additional_fields_for_object( $object, $request );
197
-		if ( is_wp_error( $save_special ) ) {
198
-			$object->delete( true );
199
-			return $save_special;
200
-		}
201
-
202
-		$request->set_param( 'context', 'edit' );
203
-		$response = $this->prepare_item_for_response( $object, $request );
204
-		$response = rest_ensure_response( $response );
205
-		$response->set_status( 201 );
206
-		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) );
207
-
208
-		return $response;
209
-	}
210
-
211
-	/**
212
-	 * Update a single object.
213
-	 *
214
-	 * @param WP_REST_Request $request Full details about the request.
215
-	 * @return WP_Error|WP_REST_Response
216
-	 */
217
-	public function update_item( $request ) {
218
-
219
-		// Fetch the item.
220
-		$object = $this->get_object( $request['id'] );
221
-		if ( is_wp_error( $object ) ) {
222
-			return $object;
223
-		}
224
-
225
-		// Prepare the item for saving.
226
-		$object = $this->prepare_item_for_database( $request );
227
-		if ( is_wp_error( $object ) ) {
228
-			return $object;
229
-		}
230
-
231
-		// Save the item.
232
-		$object = $this->save_object( $object );
233
-		if ( is_wp_error( $object ) ) {
234
-			return $object;
235
-		}
236
-
237
-		// Save special fields (those added via hooks).
238
-		$save_special = $this->update_additional_fields_for_object( $object, $request );
239
-		if ( is_wp_error( $save_special ) ) {
240
-			return $save_special;
241
-		}
242
-
243
-		$request->set_param( 'context', 'edit' );
244
-		$response = $this->prepare_item_for_response( $object, $request );
245
-		return rest_ensure_response( $response );
246
-	}
247
-
248
-	/**
249
-	 * Prepare links for the request.
250
-	 *
251
-	 * @param GetPaid_Data    $object GetPaid_Data object.
252
-	 * @return array Links for the given object.
253
-	 */
254
-	protected function prepare_links( $object ) {
255
-
256
-		$links = array(
257
-			'self'       => array(
258
-				'href'   => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
259
-			),
260
-			'collection' => array(
261
-				'href'   => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
262
-			),
263
-		);
264
-
265
-		return $links;
266
-	}
267
-
268
-	/**
269
-	 * Get the query params for collections of attachments.
270
-	 *
271
-	 * @return array
272
-	 */
273
-	public function get_collection_params() {
274
-		$params = parent::get_collection_params();
275
-		$params['context']['default'] = 'view';
276
-		return $params;
277
-	}
278
-
279
-	/**
280
-	 * Only return writable props from schema.
281
-	 *
282
-	 * @param  array $schema Schema.
283
-	 * @return bool
284
-	 */
285
-	public function filter_writable_props( $schema ) {
286
-		return empty( $schema['readonly'] );
287
-	}
288
-
289
-	/**
290
-	 * Prepare a single object for create or update.
291
-	 *
292
-	 * @since 1.0.19
293
-	 * @param  WP_REST_Request $request Request object.
294
-	 * @return GetPaid_Data|WP_Error Data object or WP_Error.
295
-	 */
296
-	protected function prepare_item_for_database( $request ) {
297
-
298
-		// Do we have an object?
299
-		if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
300
-			return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
301
-		}
302
-
303
-		// Prepare the object.
304
-		$id        = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
305
-		$object    = new $this->crud_class( $id );
306
-
307
-		// Abort if an error exists.
308
-		if ( ! empty( $object->last_error ) ) {
309
-			return new WP_Error( 'invalid_item', $object->last_error );
310
-		}
311
-
312
-		$schema    = $this->get_item_schema();
313
-		$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
314
-
315
-		// Handle all writable props.
316
-		foreach ( $data_keys as $key ) {
317
-			$value = $request[ $key ];
318
-
319
-			if ( ! is_null( $value ) ) {
320
-				switch ( $key ) {
321
-
322
-					case 'meta_data':
323
-						if ( is_array( $value ) ) {
324
-							foreach ( $value as $meta ) {
325
-								$object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
326
-							}
327
-						}
328
-						break;
329
-
330
-					default:
331
-						if ( is_callable( array( $object, "set_{$key}" ) ) ) {
332
-							$object->{"set_{$key}"}( $value );
333
-						}
334
-						break;
335
-				}
336
-			}
337
-
338
-		}
339
-
340
-		// Filters an object before it is inserted via the REST API..
341
-		return apply_filters( "getpaid_rest_pre_insert_{$this->rest_base}_object", $object, $request );
342
-	}
343
-
344
-	/**
345
-	 * Retrieves data from a GetPaid class.
346
-	 *
347
-	 * @since  1.0.19
348
-	 * @param  GetPaid_Meta_Data[]    $meta_data  meta data objects.
349
-	 * @return array
350
-	 */
351
-	protected function prepare_object_meta_data( $meta_data ) {
352
-		$meta = array();
353
-
354
-		foreach( $meta_data as $object ) {
355
-			$meta[] = $object->get_data();
356
-		}
357
-
358
-		return $meta;
359
-	}
360
-
361
-	/**
362
-	 * Retrieves invoice items.
363
-	 *
364
-	 * @since  1.0.19
365
-	 * @param  WPInv_Invoice $invoice  Invoice items.
366
-	 * @param array            $fields Fields to include.
367
-	 * @return array
368
-	 */
369
-	protected function prepare_invoice_items( $invoice ) {
370
-		$items = array();
371
-
372
-		foreach( $invoice->get_items() as $item ) {
373
-
374
-			$item_data = $item->prepare_data_for_saving();
375
-
376
-			if ( 'amount' == $invoice->get_template() ) {
377
-				$item_data['quantity'] = 1;
378
-			}
379
-
380
-			$items[] = $item_data;
381
-		}
382
-
383
-		return $items;
384
-	}
385
-
386
-	/**
387
-	 * Retrieves data from a GetPaid class.
388
-	 *
389
-	 * @since  1.0.19
390
-	 * @param  GetPaid_Data    $object  Data object.
391
-	 * @param array            $fields Fields to include.
392
-	 * @param string           $context either view or edit.
393
-	 * @return array
394
-	 */
395
-	protected function prepare_object_data( $object, $fields, $context = 'view' ) {
396
-
397
-		$data = array();
398
-
399
-		// Handle all writable props.
400
-		foreach ( array_keys( $this->get_schema_properties() ) as $key ) {
401
-
402
-			// Abort if it is not included.
403
-			if ( ! empty( $fields ) && ! $this->is_field_included( $key, $fields ) ) {
404
-				continue;
405
-			}
406
-
407
-			// Or this current object does not support the field.
408
-			if ( ! $this->object_supports_field( $object, $key ) ) {
409
-				continue;
410
-			}
411
-
412
-			// Handle meta data.
413
-			if ( $key == 'meta_data' ) {
414
-				$data['meta_data'] = $this->prepare_object_meta_data( $object->get_meta_data() );
415
-				continue;
416
-			}
417
-
418
-			// Handle items.
419
-			if ( $key == 'items' && is_a( $object, 'WPInv_Invoice' )  ) {
420
-				$data['items'] = $this->prepare_invoice_items( $object );
421
-				continue;
422
-			}
423
-
424
-			// Booleans.
425
-			if ( is_callable( array( $object, $key ) ) ) {
426
-				$data[ $key ] = $object->$key( $context );
427
-				continue;
428
-			}
429
-
430
-			// Get object value.
431
-			if ( is_callable( array( $object, "get_{$key}" ) ) ) {
432
-				$value = $object->{"get_{$key}"}( $context );
433
-
434
-				// If the value is an instance of GetPaid_Data...
435
-				if ( is_a( $value, 'GetPaid_Data' ) ) {
436
-					$value = $value->get_data( $context );
437
-				}
438
-
439
-				// For objects, retrieves it's properties.
440
-				$data[ $key ] = is_object( $value ) ? get_object_vars( $value ) :  $value ;
441
-				continue;
442
-			}
443
-
444
-		}
445
-
446
-		return $data;
447
-	}
448
-
449
-	/**
450
-	 * Checks if a key should be included in a response.
451
-	 *
452
-	 * @since  1.0.19
453
-	 * @param  GetPaid_Data $object  Data object.
454
-	 * @param  string       $field_key The key to check for.
455
-	 * @return bool
456
-	 */
457
-	public function object_supports_field( $object, $field_key ) {
458
-		return apply_filters( 'getpaid_rest_object_supports_key', true, $object, $field_key );
459
-	}
460
-
461
-	/**
462
-	 * Prepare a single object output for response.
463
-	 *
464
-	 * @since  1.0.19
465
-	 * @param  GetPaid_Data    $object  Data object.
466
-	 * @param  WP_REST_Request $request Request object.
467
-	 * @return WP_REST_Response
468
-	 */
469
-	public function prepare_item_for_response( $object, $request ) {
470
-		remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
471
-
472
-		$this->data_object = $object;
473
-
474
-		// Fetch the fields to include in this response.
475
-		$fields = $this->get_fields_for_response( $request );
476
-
477
-		// Prepare object data.
478
-		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
479
-		$data    = $this->prepare_object_data( $object, $fields, $context );
480
-		$data    = $this->add_additional_fields_to_object( $data, $request );
481
-		$data    = $this->limit_object_to_requested_fields( $data, $fields );
482
-		$data    = $this->filter_response_by_context( $data, $context );
483
-
484
-		// Prepare the response.
485
-		$response = rest_ensure_response( $data );
486
-		$response->add_links( $this->prepare_links( $object, $request ) );
487
-
488
-		// Filter item response.
489
-		return apply_filters( "getpaid_rest_prepare_{$this->rest_base}_object", $response, $object, $request );
490
-	}
20
+    /**
21
+     * Contains this controller's class name.
22
+     *
23
+     * @var string
24
+     */
25
+    public $crud_class;
26
+
27
+    /**
28
+     * Contains the current CRUD object.
29
+     *
30
+     * @var GetPaid_Data
31
+     */
32
+    protected $data_object;
33
+
34
+    /**
35
+     * Registers the routes for the objects of the controller.
36
+     *
37
+     * @since 1.0.19
38
+     *
39
+     * @see register_rest_route()
40
+     */
41
+    public function register_namespace_routes( $namespace ) {
42
+
43
+        register_rest_route(
44
+            $namespace,
45
+            '/' . $this->rest_base,
46
+            array(
47
+                array(
48
+                    'methods'             => WP_REST_Server::READABLE,
49
+                    'callback'            => array( $this, 'get_items' ),
50
+                    'permission_callback' => array( $this, 'get_items_permissions_check' ),
51
+                    'args'                => $this->get_collection_params(),
52
+                ),
53
+                array(
54
+                    'methods'             => WP_REST_Server::CREATABLE,
55
+                    'callback'            => array( $this, 'create_item' ),
56
+                    'permission_callback' => array( $this, 'create_item_permissions_check' ),
57
+                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
58
+                ),
59
+                'schema' => array( $this, 'get_public_item_schema' ),
60
+            )
61
+        );
62
+
63
+        $get_item_args = array(
64
+            'context' => $this->get_context_param( array( 'default' => 'view' ) ),
65
+        );
66
+
67
+        register_rest_route(
68
+            $namespace,
69
+            '/' . $this->rest_base . '/(?P<id>[\d]+)',
70
+            array(
71
+                'args'   => array(
72
+                    'id' => array(
73
+                        'description' => __( 'Unique identifier for the object.', 'invoicing' ),
74
+                        'type'        => 'integer',
75
+                    ),
76
+                ),
77
+                array(
78
+                    'methods'             => WP_REST_Server::READABLE,
79
+                    'callback'            => array( $this, 'get_item' ),
80
+                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
81
+                    'args'                => $get_item_args,
82
+                ),
83
+                array(
84
+                    'methods'             => WP_REST_Server::EDITABLE,
85
+                    'callback'            => array( $this, 'update_item' ),
86
+                    'permission_callback' => array( $this, 'update_item_permissions_check' ),
87
+                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
88
+                ),
89
+                array(
90
+                    'methods'             => WP_REST_Server::DELETABLE,
91
+                    'callback'            => array( $this, 'delete_item' ),
92
+                    'permission_callback' => array( $this, 'delete_item_permissions_check' ),
93
+                    'args'                => array(
94
+                        'force' => array(
95
+                            'type'        => 'boolean',
96
+                            'default'     => false,
97
+                            'description' => __( 'Whether to bypass Trash and force deletion.', 'invoicing' ),
98
+                        ),
99
+                    ),
100
+                ),
101
+                'schema' => array( $this, 'get_public_item_schema' ),
102
+            )
103
+        );
104
+
105
+    }
106
+
107
+    /**
108
+     * Saves a single object.
109
+     *
110
+     * @param GetPaid_Data $object Object to save.
111
+     * @return WP_Error|GetPaid_Data
112
+     */
113
+    protected function save_object( $object ) {
114
+        $object->save();
115
+
116
+        if ( ! empty( $object->last_error ) ) {
117
+            return new WP_Error( 'rest_cannot_save', $object->last_error, array( 'status' => 400 ) );
118
+        }
119
+
120
+        return new $this->crud_class( $object->get_id() );
121
+    }
122
+
123
+    /**
124
+     * Retrieves a single object.
125
+     *
126
+     * @since 1.0.13
127
+     *
128
+     * @param int|WP_Post $object_id Supplied ID.
129
+     * @return GetPaid_Data|WP_Error GetPaid_Data object if ID is valid, WP_Error otherwise.
130
+     */
131
+    protected function get_object( $object_id ) {
132
+
133
+        // Do we have an object?
134
+        if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
135
+            return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
136
+        }
137
+
138
+        // Fetch the object.
139
+        $object = new $this->crud_class( $object_id );
140
+        if ( ! empty( $object->last_error ) ) {
141
+            return new WP_Error( 'rest_object_invalid_id', $object->last_error, array( 'status' => 404 ) );
142
+        }
143
+
144
+        $this->data_object = $object;
145
+        return $object->get_id() ? $object : new WP_Error( 'rest_object_invalid_id', __( 'Invalid ID.', 'invoicing' ), array( 'status' => 404 ) );
146
+
147
+    }
148
+
149
+    /**
150
+     * Get a single object.
151
+     *
152
+     * @param WP_REST_Request $request Full details about the request.
153
+     * @return WP_Error|WP_REST_Response
154
+     */
155
+    public function get_item( $request ) {
156
+
157
+        // Fetch the item.
158
+        $object = $this->get_object( $request['id'] );
159
+
160
+        if ( is_wp_error( $object ) ) {
161
+            return $object;
162
+        }
163
+
164
+        // Generate a response.
165
+        return rest_ensure_response( $this->prepare_item_for_response( $object, $request ) );
166
+
167
+    }
168
+
169
+    /**
170
+     * Create a single object.
171
+     *
172
+     * @param WP_REST_Request $request Full details about the request.
173
+     * @return WP_Error|WP_REST_Response
174
+     */
175
+    public function create_item( $request ) {
176
+
177
+        // Can not create an existing item.
178
+        if ( ! empty( $request['id'] ) ) {
179
+            /* translators: %s: post type */
180
+            return new WP_Error( "getpaid_rest_{$this->rest_base}_exists", __( 'Cannot create existing resource.', 'invoicing' ), array( 'status' => 400 ) );
181
+        }
182
+
183
+        // Generate a GetPaid_Data object from the request.
184
+        $object = $this->prepare_item_for_database( $request );
185
+        if ( is_wp_error( $object ) ) {
186
+            return $object;
187
+        }
188
+
189
+        // Save the object.
190
+        $object = $this->save_object( $object );
191
+        if ( is_wp_error( $object ) ) {
192
+            return $object;
193
+        }
194
+
195
+        // Save special fields.
196
+        $save_special = $this->update_additional_fields_for_object( $object, $request );
197
+        if ( is_wp_error( $save_special ) ) {
198
+            $object->delete( true );
199
+            return $save_special;
200
+        }
201
+
202
+        $request->set_param( 'context', 'edit' );
203
+        $response = $this->prepare_item_for_response( $object, $request );
204
+        $response = rest_ensure_response( $response );
205
+        $response->set_status( 201 );
206
+        $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) );
207
+
208
+        return $response;
209
+    }
210
+
211
+    /**
212
+     * Update a single object.
213
+     *
214
+     * @param WP_REST_Request $request Full details about the request.
215
+     * @return WP_Error|WP_REST_Response
216
+     */
217
+    public function update_item( $request ) {
218
+
219
+        // Fetch the item.
220
+        $object = $this->get_object( $request['id'] );
221
+        if ( is_wp_error( $object ) ) {
222
+            return $object;
223
+        }
224
+
225
+        // Prepare the item for saving.
226
+        $object = $this->prepare_item_for_database( $request );
227
+        if ( is_wp_error( $object ) ) {
228
+            return $object;
229
+        }
230
+
231
+        // Save the item.
232
+        $object = $this->save_object( $object );
233
+        if ( is_wp_error( $object ) ) {
234
+            return $object;
235
+        }
236
+
237
+        // Save special fields (those added via hooks).
238
+        $save_special = $this->update_additional_fields_for_object( $object, $request );
239
+        if ( is_wp_error( $save_special ) ) {
240
+            return $save_special;
241
+        }
242
+
243
+        $request->set_param( 'context', 'edit' );
244
+        $response = $this->prepare_item_for_response( $object, $request );
245
+        return rest_ensure_response( $response );
246
+    }
247
+
248
+    /**
249
+     * Prepare links for the request.
250
+     *
251
+     * @param GetPaid_Data    $object GetPaid_Data object.
252
+     * @return array Links for the given object.
253
+     */
254
+    protected function prepare_links( $object ) {
255
+
256
+        $links = array(
257
+            'self'       => array(
258
+                'href'   => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
259
+            ),
260
+            'collection' => array(
261
+                'href'   => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
262
+            ),
263
+        );
264
+
265
+        return $links;
266
+    }
267
+
268
+    /**
269
+     * Get the query params for collections of attachments.
270
+     *
271
+     * @return array
272
+     */
273
+    public function get_collection_params() {
274
+        $params = parent::get_collection_params();
275
+        $params['context']['default'] = 'view';
276
+        return $params;
277
+    }
278
+
279
+    /**
280
+     * Only return writable props from schema.
281
+     *
282
+     * @param  array $schema Schema.
283
+     * @return bool
284
+     */
285
+    public function filter_writable_props( $schema ) {
286
+        return empty( $schema['readonly'] );
287
+    }
288
+
289
+    /**
290
+     * Prepare a single object for create or update.
291
+     *
292
+     * @since 1.0.19
293
+     * @param  WP_REST_Request $request Request object.
294
+     * @return GetPaid_Data|WP_Error Data object or WP_Error.
295
+     */
296
+    protected function prepare_item_for_database( $request ) {
297
+
298
+        // Do we have an object?
299
+        if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
300
+            return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
301
+        }
302
+
303
+        // Prepare the object.
304
+        $id        = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
305
+        $object    = new $this->crud_class( $id );
306
+
307
+        // Abort if an error exists.
308
+        if ( ! empty( $object->last_error ) ) {
309
+            return new WP_Error( 'invalid_item', $object->last_error );
310
+        }
311
+
312
+        $schema    = $this->get_item_schema();
313
+        $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
314
+
315
+        // Handle all writable props.
316
+        foreach ( $data_keys as $key ) {
317
+            $value = $request[ $key ];
318
+
319
+            if ( ! is_null( $value ) ) {
320
+                switch ( $key ) {
321
+
322
+                    case 'meta_data':
323
+                        if ( is_array( $value ) ) {
324
+                            foreach ( $value as $meta ) {
325
+                                $object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
326
+                            }
327
+                        }
328
+                        break;
329
+
330
+                    default:
331
+                        if ( is_callable( array( $object, "set_{$key}" ) ) ) {
332
+                            $object->{"set_{$key}"}( $value );
333
+                        }
334
+                        break;
335
+                }
336
+            }
337
+
338
+        }
339
+
340
+        // Filters an object before it is inserted via the REST API..
341
+        return apply_filters( "getpaid_rest_pre_insert_{$this->rest_base}_object", $object, $request );
342
+    }
343
+
344
+    /**
345
+     * Retrieves data from a GetPaid class.
346
+     *
347
+     * @since  1.0.19
348
+     * @param  GetPaid_Meta_Data[]    $meta_data  meta data objects.
349
+     * @return array
350
+     */
351
+    protected function prepare_object_meta_data( $meta_data ) {
352
+        $meta = array();
353
+
354
+        foreach( $meta_data as $object ) {
355
+            $meta[] = $object->get_data();
356
+        }
357
+
358
+        return $meta;
359
+    }
360
+
361
+    /**
362
+     * Retrieves invoice items.
363
+     *
364
+     * @since  1.0.19
365
+     * @param  WPInv_Invoice $invoice  Invoice items.
366
+     * @param array            $fields Fields to include.
367
+     * @return array
368
+     */
369
+    protected function prepare_invoice_items( $invoice ) {
370
+        $items = array();
371
+
372
+        foreach( $invoice->get_items() as $item ) {
373
+
374
+            $item_data = $item->prepare_data_for_saving();
375
+
376
+            if ( 'amount' == $invoice->get_template() ) {
377
+                $item_data['quantity'] = 1;
378
+            }
379
+
380
+            $items[] = $item_data;
381
+        }
382
+
383
+        return $items;
384
+    }
385
+
386
+    /**
387
+     * Retrieves data from a GetPaid class.
388
+     *
389
+     * @since  1.0.19
390
+     * @param  GetPaid_Data    $object  Data object.
391
+     * @param array            $fields Fields to include.
392
+     * @param string           $context either view or edit.
393
+     * @return array
394
+     */
395
+    protected function prepare_object_data( $object, $fields, $context = 'view' ) {
396
+
397
+        $data = array();
398
+
399
+        // Handle all writable props.
400
+        foreach ( array_keys( $this->get_schema_properties() ) as $key ) {
401
+
402
+            // Abort if it is not included.
403
+            if ( ! empty( $fields ) && ! $this->is_field_included( $key, $fields ) ) {
404
+                continue;
405
+            }
406
+
407
+            // Or this current object does not support the field.
408
+            if ( ! $this->object_supports_field( $object, $key ) ) {
409
+                continue;
410
+            }
411
+
412
+            // Handle meta data.
413
+            if ( $key == 'meta_data' ) {
414
+                $data['meta_data'] = $this->prepare_object_meta_data( $object->get_meta_data() );
415
+                continue;
416
+            }
417
+
418
+            // Handle items.
419
+            if ( $key == 'items' && is_a( $object, 'WPInv_Invoice' )  ) {
420
+                $data['items'] = $this->prepare_invoice_items( $object );
421
+                continue;
422
+            }
423
+
424
+            // Booleans.
425
+            if ( is_callable( array( $object, $key ) ) ) {
426
+                $data[ $key ] = $object->$key( $context );
427
+                continue;
428
+            }
429
+
430
+            // Get object value.
431
+            if ( is_callable( array( $object, "get_{$key}" ) ) ) {
432
+                $value = $object->{"get_{$key}"}( $context );
433
+
434
+                // If the value is an instance of GetPaid_Data...
435
+                if ( is_a( $value, 'GetPaid_Data' ) ) {
436
+                    $value = $value->get_data( $context );
437
+                }
438
+
439
+                // For objects, retrieves it's properties.
440
+                $data[ $key ] = is_object( $value ) ? get_object_vars( $value ) :  $value ;
441
+                continue;
442
+            }
443
+
444
+        }
445
+
446
+        return $data;
447
+    }
448
+
449
+    /**
450
+     * Checks if a key should be included in a response.
451
+     *
452
+     * @since  1.0.19
453
+     * @param  GetPaid_Data $object  Data object.
454
+     * @param  string       $field_key The key to check for.
455
+     * @return bool
456
+     */
457
+    public function object_supports_field( $object, $field_key ) {
458
+        return apply_filters( 'getpaid_rest_object_supports_key', true, $object, $field_key );
459
+    }
460
+
461
+    /**
462
+     * Prepare a single object output for response.
463
+     *
464
+     * @since  1.0.19
465
+     * @param  GetPaid_Data    $object  Data object.
466
+     * @param  WP_REST_Request $request Request object.
467
+     * @return WP_REST_Response
468
+     */
469
+    public function prepare_item_for_response( $object, $request ) {
470
+        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
471
+
472
+        $this->data_object = $object;
473
+
474
+        // Fetch the fields to include in this response.
475
+        $fields = $this->get_fields_for_response( $request );
476
+
477
+        // Prepare object data.
478
+        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
479
+        $data    = $this->prepare_object_data( $object, $fields, $context );
480
+        $data    = $this->add_additional_fields_to_object( $data, $request );
481
+        $data    = $this->limit_object_to_requested_fields( $data, $fields );
482
+        $data    = $this->filter_response_by_context( $data, $context );
483
+
484
+        // Prepare the response.
485
+        $response = rest_ensure_response( $data );
486
+        $response->add_links( $this->prepare_links( $object, $request ) );
487
+
488
+        // Filter item response.
489
+        return apply_filters( "getpaid_rest_prepare_{$this->rest_base}_object", $response, $object, $request );
490
+    }
491 491
 
492 492
 }
Please login to merge, or discard this patch.
Spacing   +117 added lines, -117 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  * @version 1.0.19
9 9
  */
10 10
 
11
-defined( 'ABSPATH' ) || exit;
11
+defined('ABSPATH') || exit;
12 12
 
13 13
 /**
14 14
  * GetPaid REST CRUD controller class.
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
38 38
 	 *
39 39
 	 * @see register_rest_route()
40 40
 	 */
41
-	public function register_namespace_routes( $namespace ) {
41
+	public function register_namespace_routes($namespace) {
42 42
 
43 43
 		register_rest_route(
44 44
 			$namespace,
@@ -46,22 +46,22 @@  discard block
 block discarded – undo
46 46
 			array(
47 47
 				array(
48 48
 					'methods'             => WP_REST_Server::READABLE,
49
-					'callback'            => array( $this, 'get_items' ),
50
-					'permission_callback' => array( $this, 'get_items_permissions_check' ),
49
+					'callback'            => array($this, 'get_items'),
50
+					'permission_callback' => array($this, 'get_items_permissions_check'),
51 51
 					'args'                => $this->get_collection_params(),
52 52
 				),
53 53
 				array(
54 54
 					'methods'             => WP_REST_Server::CREATABLE,
55
-					'callback'            => array( $this, 'create_item' ),
56
-					'permission_callback' => array( $this, 'create_item_permissions_check' ),
57
-					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
55
+					'callback'            => array($this, 'create_item'),
56
+					'permission_callback' => array($this, 'create_item_permissions_check'),
57
+					'args'                => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
58 58
 				),
59
-				'schema' => array( $this, 'get_public_item_schema' ),
59
+				'schema' => array($this, 'get_public_item_schema'),
60 60
 			)
61 61
 		);
62 62
 
63 63
 		$get_item_args = array(
64
-			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
64
+			'context' => $this->get_context_param(array('default' => 'view')),
65 65
 		);
66 66
 
67 67
 		register_rest_route(
@@ -70,35 +70,35 @@  discard block
 block discarded – undo
70 70
 			array(
71 71
 				'args'   => array(
72 72
 					'id' => array(
73
-						'description' => __( 'Unique identifier for the object.', 'invoicing' ),
73
+						'description' => __('Unique identifier for the object.', 'invoicing'),
74 74
 						'type'        => 'integer',
75 75
 					),
76 76
 				),
77 77
 				array(
78 78
 					'methods'             => WP_REST_Server::READABLE,
79
-					'callback'            => array( $this, 'get_item' ),
80
-					'permission_callback' => array( $this, 'get_item_permissions_check' ),
79
+					'callback'            => array($this, 'get_item'),
80
+					'permission_callback' => array($this, 'get_item_permissions_check'),
81 81
 					'args'                => $get_item_args,
82 82
 				),
83 83
 				array(
84 84
 					'methods'             => WP_REST_Server::EDITABLE,
85
-					'callback'            => array( $this, 'update_item' ),
86
-					'permission_callback' => array( $this, 'update_item_permissions_check' ),
87
-					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
85
+					'callback'            => array($this, 'update_item'),
86
+					'permission_callback' => array($this, 'update_item_permissions_check'),
87
+					'args'                => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE),
88 88
 				),
89 89
 				array(
90 90
 					'methods'             => WP_REST_Server::DELETABLE,
91
-					'callback'            => array( $this, 'delete_item' ),
92
-					'permission_callback' => array( $this, 'delete_item_permissions_check' ),
91
+					'callback'            => array($this, 'delete_item'),
92
+					'permission_callback' => array($this, 'delete_item_permissions_check'),
93 93
 					'args'                => array(
94 94
 						'force' => array(
95 95
 							'type'        => 'boolean',
96 96
 							'default'     => false,
97
-							'description' => __( 'Whether to bypass Trash and force deletion.', 'invoicing' ),
97
+							'description' => __('Whether to bypass Trash and force deletion.', 'invoicing'),
98 98
 						),
99 99
 					),
100 100
 				),
101
-				'schema' => array( $this, 'get_public_item_schema' ),
101
+				'schema' => array($this, 'get_public_item_schema'),
102 102
 			)
103 103
 		);
104 104
 
@@ -110,14 +110,14 @@  discard block
 block discarded – undo
110 110
 	 * @param GetPaid_Data $object Object to save.
111 111
 	 * @return WP_Error|GetPaid_Data
112 112
 	 */
113
-	protected function save_object( $object ) {
113
+	protected function save_object($object) {
114 114
 		$object->save();
115 115
 
116
-		if ( ! empty( $object->last_error ) ) {
117
-			return new WP_Error( 'rest_cannot_save', $object->last_error, array( 'status' => 400 ) );
116
+		if (!empty($object->last_error)) {
117
+			return new WP_Error('rest_cannot_save', $object->last_error, array('status' => 400));
118 118
 		}
119 119
 
120
-		return new $this->crud_class( $object->get_id() );
120
+		return new $this->crud_class($object->get_id());
121 121
 	}
122 122
 
123 123
 	/**
@@ -128,21 +128,21 @@  discard block
 block discarded – undo
128 128
 	 * @param int|WP_Post $object_id Supplied ID.
129 129
 	 * @return GetPaid_Data|WP_Error GetPaid_Data object if ID is valid, WP_Error otherwise.
130 130
 	 */
131
-	protected function get_object( $object_id ) {
131
+	protected function get_object($object_id) {
132 132
 
133 133
 		// Do we have an object?
134
-		if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
135
-			return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
134
+		if (empty($this->crud_class) || !class_exists($this->crud_class)) {
135
+			return new WP_Error('no_crud_class', __('You need to specify a CRUD class for this controller', 'invoicing'));
136 136
 		}
137 137
 
138 138
 		// Fetch the object.
139
-		$object = new $this->crud_class( $object_id );
140
-		if ( ! empty( $object->last_error ) ) {
141
-			return new WP_Error( 'rest_object_invalid_id', $object->last_error, array( 'status' => 404 ) );
139
+		$object = new $this->crud_class($object_id);
140
+		if (!empty($object->last_error)) {
141
+			return new WP_Error('rest_object_invalid_id', $object->last_error, array('status' => 404));
142 142
 		}
143 143
 
144 144
 		$this->data_object = $object;
145
-		return $object->get_id() ? $object : new WP_Error( 'rest_object_invalid_id', __( 'Invalid ID.', 'invoicing' ), array( 'status' => 404 ) );
145
+		return $object->get_id() ? $object : new WP_Error('rest_object_invalid_id', __('Invalid ID.', 'invoicing'), array('status' => 404));
146 146
 
147 147
 	}
148 148
 
@@ -152,17 +152,17 @@  discard block
 block discarded – undo
152 152
 	 * @param WP_REST_Request $request Full details about the request.
153 153
 	 * @return WP_Error|WP_REST_Response
154 154
 	 */
155
-	public function get_item( $request ) {
155
+	public function get_item($request) {
156 156
 
157 157
 		// Fetch the item.
158
-		$object = $this->get_object( $request['id'] );
158
+		$object = $this->get_object($request['id']);
159 159
 
160
-		if ( is_wp_error( $object ) ) {
160
+		if (is_wp_error($object)) {
161 161
 			return $object;
162 162
 		}
163 163
 
164 164
 		// Generate a response.
165
-		return rest_ensure_response( $this->prepare_item_for_response( $object, $request ) );
165
+		return rest_ensure_response($this->prepare_item_for_response($object, $request));
166 166
 
167 167
 	}
168 168
 
@@ -172,38 +172,38 @@  discard block
 block discarded – undo
172 172
 	 * @param WP_REST_Request $request Full details about the request.
173 173
 	 * @return WP_Error|WP_REST_Response
174 174
 	 */
175
-	public function create_item( $request ) {
175
+	public function create_item($request) {
176 176
 
177 177
 		// Can not create an existing item.
178
-		if ( ! empty( $request['id'] ) ) {
178
+		if (!empty($request['id'])) {
179 179
 			/* translators: %s: post type */
180
-			return new WP_Error( "getpaid_rest_{$this->rest_base}_exists", __( 'Cannot create existing resource.', 'invoicing' ), array( 'status' => 400 ) );
180
+			return new WP_Error("getpaid_rest_{$this->rest_base}_exists", __('Cannot create existing resource.', 'invoicing'), array('status' => 400));
181 181
 		}
182 182
 
183 183
 		// Generate a GetPaid_Data object from the request.
184
-		$object = $this->prepare_item_for_database( $request );
185
-		if ( is_wp_error( $object ) ) {
184
+		$object = $this->prepare_item_for_database($request);
185
+		if (is_wp_error($object)) {
186 186
 			return $object;
187 187
 		}
188 188
 
189 189
 		// Save the object.
190
-		$object = $this->save_object( $object );
191
-		if ( is_wp_error( $object ) ) {
190
+		$object = $this->save_object($object);
191
+		if (is_wp_error($object)) {
192 192
 			return $object;
193 193
 		}
194 194
 
195 195
 		// Save special fields.
196
-		$save_special = $this->update_additional_fields_for_object( $object, $request );
197
-		if ( is_wp_error( $save_special ) ) {
198
-			$object->delete( true );
196
+		$save_special = $this->update_additional_fields_for_object($object, $request);
197
+		if (is_wp_error($save_special)) {
198
+			$object->delete(true);
199 199
 			return $save_special;
200 200
 		}
201 201
 
202
-		$request->set_param( 'context', 'edit' );
203
-		$response = $this->prepare_item_for_response( $object, $request );
204
-		$response = rest_ensure_response( $response );
205
-		$response->set_status( 201 );
206
-		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) );
202
+		$request->set_param('context', 'edit');
203
+		$response = $this->prepare_item_for_response($object, $request);
204
+		$response = rest_ensure_response($response);
205
+		$response->set_status(201);
206
+		$response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id())));
207 207
 
208 208
 		return $response;
209 209
 	}
@@ -214,35 +214,35 @@  discard block
 block discarded – undo
214 214
 	 * @param WP_REST_Request $request Full details about the request.
215 215
 	 * @return WP_Error|WP_REST_Response
216 216
 	 */
217
-	public function update_item( $request ) {
217
+	public function update_item($request) {
218 218
 
219 219
 		// Fetch the item.
220
-		$object = $this->get_object( $request['id'] );
221
-		if ( is_wp_error( $object ) ) {
220
+		$object = $this->get_object($request['id']);
221
+		if (is_wp_error($object)) {
222 222
 			return $object;
223 223
 		}
224 224
 
225 225
 		// Prepare the item for saving.
226
-		$object = $this->prepare_item_for_database( $request );
227
-		if ( is_wp_error( $object ) ) {
226
+		$object = $this->prepare_item_for_database($request);
227
+		if (is_wp_error($object)) {
228 228
 			return $object;
229 229
 		}
230 230
 
231 231
 		// Save the item.
232
-		$object = $this->save_object( $object );
233
-		if ( is_wp_error( $object ) ) {
232
+		$object = $this->save_object($object);
233
+		if (is_wp_error($object)) {
234 234
 			return $object;
235 235
 		}
236 236
 
237 237
 		// Save special fields (those added via hooks).
238
-		$save_special = $this->update_additional_fields_for_object( $object, $request );
239
-		if ( is_wp_error( $save_special ) ) {
238
+		$save_special = $this->update_additional_fields_for_object($object, $request);
239
+		if (is_wp_error($save_special)) {
240 240
 			return $save_special;
241 241
 		}
242 242
 
243
-		$request->set_param( 'context', 'edit' );
244
-		$response = $this->prepare_item_for_response( $object, $request );
245
-		return rest_ensure_response( $response );
243
+		$request->set_param('context', 'edit');
244
+		$response = $this->prepare_item_for_response($object, $request);
245
+		return rest_ensure_response($response);
246 246
 	}
247 247
 
248 248
 	/**
@@ -251,14 +251,14 @@  discard block
 block discarded – undo
251 251
 	 * @param GetPaid_Data    $object GetPaid_Data object.
252 252
 	 * @return array Links for the given object.
253 253
 	 */
254
-	protected function prepare_links( $object ) {
254
+	protected function prepare_links($object) {
255 255
 
256 256
 		$links = array(
257 257
 			'self'       => array(
258
-				'href'   => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
258
+				'href'   => rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id())),
259 259
 			),
260 260
 			'collection' => array(
261
-				'href'   => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
261
+				'href'   => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)),
262 262
 			),
263 263
 		);
264 264
 
@@ -282,8 +282,8 @@  discard block
 block discarded – undo
282 282
 	 * @param  array $schema Schema.
283 283
 	 * @return bool
284 284
 	 */
285
-	public function filter_writable_props( $schema ) {
286
-		return empty( $schema['readonly'] );
285
+	public function filter_writable_props($schema) {
286
+		return empty($schema['readonly']);
287 287
 	}
288 288
 
289 289
 	/**
@@ -293,43 +293,43 @@  discard block
 block discarded – undo
293 293
 	 * @param  WP_REST_Request $request Request object.
294 294
 	 * @return GetPaid_Data|WP_Error Data object or WP_Error.
295 295
 	 */
296
-	protected function prepare_item_for_database( $request ) {
296
+	protected function prepare_item_for_database($request) {
297 297
 
298 298
 		// Do we have an object?
299
-		if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
300
-			return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
299
+		if (empty($this->crud_class) || !class_exists($this->crud_class)) {
300
+			return new WP_Error('no_crud_class', __('You need to specify a CRUD class for this controller', 'invoicing'));
301 301
 		}
302 302
 
303 303
 		// Prepare the object.
304
-		$id        = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
305
-		$object    = new $this->crud_class( $id );
304
+		$id        = isset($request['id']) ? absint($request['id']) : 0;
305
+		$object    = new $this->crud_class($id);
306 306
 
307 307
 		// Abort if an error exists.
308
-		if ( ! empty( $object->last_error ) ) {
309
-			return new WP_Error( 'invalid_item', $object->last_error );
308
+		if (!empty($object->last_error)) {
309
+			return new WP_Error('invalid_item', $object->last_error);
310 310
 		}
311 311
 
312 312
 		$schema    = $this->get_item_schema();
313
-		$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
313
+		$data_keys = array_keys(array_filter($schema['properties'], array($this, 'filter_writable_props')));
314 314
 
315 315
 		// Handle all writable props.
316
-		foreach ( $data_keys as $key ) {
317
-			$value = $request[ $key ];
316
+		foreach ($data_keys as $key) {
317
+			$value = $request[$key];
318 318
 
319
-			if ( ! is_null( $value ) ) {
320
-				switch ( $key ) {
319
+			if (!is_null($value)) {
320
+				switch ($key) {
321 321
 
322 322
 					case 'meta_data':
323
-						if ( is_array( $value ) ) {
324
-							foreach ( $value as $meta ) {
325
-								$object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
323
+						if (is_array($value)) {
324
+							foreach ($value as $meta) {
325
+								$object->update_meta_data($meta['key'], $meta['value'], isset($meta['id']) ? $meta['id'] : '');
326 326
 							}
327 327
 						}
328 328
 						break;
329 329
 
330 330
 					default:
331
-						if ( is_callable( array( $object, "set_{$key}" ) ) ) {
332
-							$object->{"set_{$key}"}( $value );
331
+						if (is_callable(array($object, "set_{$key}"))) {
332
+							$object->{"set_{$key}"}($value);
333 333
 						}
334 334
 						break;
335 335
 				}
@@ -338,7 +338,7 @@  discard block
 block discarded – undo
338 338
 		}
339 339
 
340 340
 		// Filters an object before it is inserted via the REST API..
341
-		return apply_filters( "getpaid_rest_pre_insert_{$this->rest_base}_object", $object, $request );
341
+		return apply_filters("getpaid_rest_pre_insert_{$this->rest_base}_object", $object, $request);
342 342
 	}
343 343
 
344 344
 	/**
@@ -348,10 +348,10 @@  discard block
 block discarded – undo
348 348
 	 * @param  GetPaid_Meta_Data[]    $meta_data  meta data objects.
349 349
 	 * @return array
350 350
 	 */
351
-	protected function prepare_object_meta_data( $meta_data ) {
351
+	protected function prepare_object_meta_data($meta_data) {
352 352
 		$meta = array();
353 353
 
354
-		foreach( $meta_data as $object ) {
354
+		foreach ($meta_data as $object) {
355 355
 			$meta[] = $object->get_data();
356 356
 		}
357 357
 
@@ -366,14 +366,14 @@  discard block
 block discarded – undo
366 366
 	 * @param array            $fields Fields to include.
367 367
 	 * @return array
368 368
 	 */
369
-	protected function prepare_invoice_items( $invoice ) {
369
+	protected function prepare_invoice_items($invoice) {
370 370
 		$items = array();
371 371
 
372
-		foreach( $invoice->get_items() as $item ) {
372
+		foreach ($invoice->get_items() as $item) {
373 373
 
374 374
 			$item_data = $item->prepare_data_for_saving();
375 375
 
376
-			if ( 'amount' == $invoice->get_template() ) {
376
+			if ('amount' == $invoice->get_template()) {
377 377
 				$item_data['quantity'] = 1;
378 378
 			}
379 379
 
@@ -392,52 +392,52 @@  discard block
 block discarded – undo
392 392
 	 * @param string           $context either view or edit.
393 393
 	 * @return array
394 394
 	 */
395
-	protected function prepare_object_data( $object, $fields, $context = 'view' ) {
395
+	protected function prepare_object_data($object, $fields, $context = 'view') {
396 396
 
397 397
 		$data = array();
398 398
 
399 399
 		// Handle all writable props.
400
-		foreach ( array_keys( $this->get_schema_properties() ) as $key ) {
400
+		foreach (array_keys($this->get_schema_properties()) as $key) {
401 401
 
402 402
 			// Abort if it is not included.
403
-			if ( ! empty( $fields ) && ! $this->is_field_included( $key, $fields ) ) {
403
+			if (!empty($fields) && !$this->is_field_included($key, $fields)) {
404 404
 				continue;
405 405
 			}
406 406
 
407 407
 			// Or this current object does not support the field.
408
-			if ( ! $this->object_supports_field( $object, $key ) ) {
408
+			if (!$this->object_supports_field($object, $key)) {
409 409
 				continue;
410 410
 			}
411 411
 
412 412
 			// Handle meta data.
413
-			if ( $key == 'meta_data' ) {
414
-				$data['meta_data'] = $this->prepare_object_meta_data( $object->get_meta_data() );
413
+			if ($key == 'meta_data') {
414
+				$data['meta_data'] = $this->prepare_object_meta_data($object->get_meta_data());
415 415
 				continue;
416 416
 			}
417 417
 
418 418
 			// Handle items.
419
-			if ( $key == 'items' && is_a( $object, 'WPInv_Invoice' )  ) {
420
-				$data['items'] = $this->prepare_invoice_items( $object );
419
+			if ($key == 'items' && is_a($object, 'WPInv_Invoice')) {
420
+				$data['items'] = $this->prepare_invoice_items($object);
421 421
 				continue;
422 422
 			}
423 423
 
424 424
 			// Booleans.
425
-			if ( is_callable( array( $object, $key ) ) ) {
426
-				$data[ $key ] = $object->$key( $context );
425
+			if (is_callable(array($object, $key))) {
426
+				$data[$key] = $object->$key($context);
427 427
 				continue;
428 428
 			}
429 429
 
430 430
 			// Get object value.
431
-			if ( is_callable( array( $object, "get_{$key}" ) ) ) {
432
-				$value = $object->{"get_{$key}"}( $context );
431
+			if (is_callable(array($object, "get_{$key}"))) {
432
+				$value = $object->{"get_{$key}"}($context);
433 433
 
434 434
 				// If the value is an instance of GetPaid_Data...
435
-				if ( is_a( $value, 'GetPaid_Data' ) ) {
436
-					$value = $value->get_data( $context );
435
+				if (is_a($value, 'GetPaid_Data')) {
436
+					$value = $value->get_data($context);
437 437
 				}
438 438
 
439 439
 				// For objects, retrieves it's properties.
440
-				$data[ $key ] = is_object( $value ) ? get_object_vars( $value ) :  $value ;
440
+				$data[$key] = is_object($value) ? get_object_vars($value) : $value;
441 441
 				continue;
442 442
 			}
443 443
 
@@ -454,8 +454,8 @@  discard block
 block discarded – undo
454 454
 	 * @param  string       $field_key The key to check for.
455 455
 	 * @return bool
456 456
 	 */
457
-	public function object_supports_field( $object, $field_key ) {
458
-		return apply_filters( 'getpaid_rest_object_supports_key', true, $object, $field_key );
457
+	public function object_supports_field($object, $field_key) {
458
+		return apply_filters('getpaid_rest_object_supports_key', true, $object, $field_key);
459 459
 	}
460 460
 
461 461
 	/**
@@ -466,27 +466,27 @@  discard block
 block discarded – undo
466 466
 	 * @param  WP_REST_Request $request Request object.
467 467
 	 * @return WP_REST_Response
468 468
 	 */
469
-	public function prepare_item_for_response( $object, $request ) {
470
-		remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
469
+	public function prepare_item_for_response($object, $request) {
470
+		remove_filter('rest_post_dispatch', 'rest_filter_response_fields', 10);
471 471
 
472 472
 		$this->data_object = $object;
473 473
 
474 474
 		// Fetch the fields to include in this response.
475
-		$fields = $this->get_fields_for_response( $request );
475
+		$fields = $this->get_fields_for_response($request);
476 476
 
477 477
 		// Prepare object data.
478
-		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
479
-		$data    = $this->prepare_object_data( $object, $fields, $context );
480
-		$data    = $this->add_additional_fields_to_object( $data, $request );
481
-		$data    = $this->limit_object_to_requested_fields( $data, $fields );
482
-		$data    = $this->filter_response_by_context( $data, $context );
478
+		$context = !empty($request['context']) ? $request['context'] : 'view';
479
+		$data    = $this->prepare_object_data($object, $fields, $context);
480
+		$data    = $this->add_additional_fields_to_object($data, $request);
481
+		$data    = $this->limit_object_to_requested_fields($data, $fields);
482
+		$data    = $this->filter_response_by_context($data, $context);
483 483
 
484 484
 		// Prepare the response.
485
-		$response = rest_ensure_response( $data );
486
-		$response->add_links( $this->prepare_links( $object, $request ) );
485
+		$response = rest_ensure_response($data);
486
+		$response->add_links($this->prepare_links($object, $request));
487 487
 
488 488
 		// Filter item response.
489
-		return apply_filters( "getpaid_rest_prepare_{$this->rest_base}_object", $response, $object, $request );
489
+		return apply_filters("getpaid_rest_prepare_{$this->rest_base}_object", $response, $object, $request);
490 490
 	}
491 491
 
492 492
 }
Please login to merge, or discard this patch.
templates/wpinv-subscriptions-history.php 1 patch
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 global $wpdb;
14 14
 
15 15
 $db = new WPInv_Subscriptions_DB;
16
-$page = isset($_GET['cpage']) ? abs((int)$_GET['cpage']) : 1;
16
+$page = isset($_GET['cpage']) ? abs((int) $_GET['cpage']) : 1;
17 17
 $items_per_page = get_option('posts_per_page');
18 18
 $offset = ($page * $items_per_page) - $items_per_page;
19 19
 $args = array('customer_id' => $user_id, 'offset' => $offset);
@@ -30,16 +30,16 @@  discard block
 block discarded – undo
30 30
     <table class="table table-bordered table-hover table-responsive wpi-user-subscriptions">
31 31
         <thead>
32 32
         <tr>
33
-            <th class="sub-no"><span class="nobr"><?php _e( 'No.', 'invoicing' ); ?></span></th>
34
-            <th class="sub-amount"><span class="nobr"><?php _e( 'Initial Amount', 'invoicing' ); ?></span></th>
35
-            <th class="sub-cycle"><span class="nobr"><?php _e( 'Billing Cycle', 'invoicing' ); ?></span></th>
36
-            <th class="sub-billed"><span class="nobr"><?php _e( 'Times Billed', 'invoicing' ); ?></span></th>
37
-            <th class="sub-status"><span class="nobr"><?php _e( 'Status', 'invoicing' ); ?></span></th>
38
-            <th class="sub-invoice"><span class="nobr"><?php _e( 'Invoice', 'invoicing' ); ?></span></th>
39
-            <th class="sub-item"><span class="nobr"><?php _e( 'Item', 'invoicing' ); ?></span></th>
40
-            <th class="sub-gateway"><span class="nobr"><?php _e( 'Gateway', 'invoicing' ); ?></span></th>
41
-            <th class="sub-expiry"><span class="nobr"><?php _e( 'Expires On', 'invoicing' ); ?></span></th>
42
-            <th class="sub-actions"><span class="nobr"><?php _e( 'Actions', 'invoicing' ); ?></span></th>
33
+            <th class="sub-no"><span class="nobr"><?php _e('No.', 'invoicing'); ?></span></th>
34
+            <th class="sub-amount"><span class="nobr"><?php _e('Initial Amount', 'invoicing'); ?></span></th>
35
+            <th class="sub-cycle"><span class="nobr"><?php _e('Billing Cycle', 'invoicing'); ?></span></th>
36
+            <th class="sub-billed"><span class="nobr"><?php _e('Times Billed', 'invoicing'); ?></span></th>
37
+            <th class="sub-status"><span class="nobr"><?php _e('Status', 'invoicing'); ?></span></th>
38
+            <th class="sub-invoice"><span class="nobr"><?php _e('Invoice', 'invoicing'); ?></span></th>
39
+            <th class="sub-item"><span class="nobr"><?php _e('Item', 'invoicing'); ?></span></th>
40
+            <th class="sub-gateway"><span class="nobr"><?php _e('Gateway', 'invoicing'); ?></span></th>
41
+            <th class="sub-expiry"><span class="nobr"><?php _e('Expires On', 'invoicing'); ?></span></th>
42
+            <th class="sub-actions"><span class="nobr"><?php _e('Actions', 'invoicing'); ?></span></th>
43 43
         </tr>
44 44
         </thead>
45 45
 
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
                     $initial = wpinv_price(wpinv_format_amount($sub->initial_amount), wpinv_get_invoice_currency_code($sub->parent_payment_id));
58 58
                     printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?>
59 59
                 </td>
60
-                <td><?php echo $sub->get_times_billed() . ' / ' . (($sub->bill_times == 0) ? __( 'Until Cancelled', 'invoicing' ) : $sub->bill_times); ?></td>
60
+                <td><?php echo $sub->get_times_billed() . ' / ' . (($sub->bill_times == 0) ? __('Until Cancelled', 'invoicing') : $sub->bill_times); ?></td>
61 61
                 <td><?php echo $sub->get_status_label_html(); ?></td>
62 62
                 <td><a href="<?php echo get_permalink($invoice); ?>" target="_blank">#<?php echo $invoice; ?></a></td>
63 63
                 <td><?php echo get_the_title($sub->product_id); ?></td>
@@ -65,7 +65,7 @@  discard block
 block discarded – undo
65 65
                 <td><?php echo date_i18n(get_option('date_format'), strtotime($sub->expiration, current_time('timestamp'))); ?></td>
66 66
                 <td><?php
67 67
                     if ($sub->can_cancel()) {
68
-                        $message = esc_attr__( 'Are you sure you want to cancel this subscription?', 'invoicing' );
68
+                        $message = esc_attr__('Are you sure you want to cancel this subscription?', 'invoicing');
69 69
                         echo '<a class="btn btn-sm btn-primary" onclick="return confirm(\'' . $message . '\')" href="' . $sub->get_cancel_url() . '" >' . __("Cancel", "invoicing") . '</a>';
70 70
                     }
71 71
                     do_action('wpinv_subscription_item_actions', $sub);
Please login to merge, or discard this patch.
templates/invoice/line-item.php 1 patch
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -7,52 +7,52 @@  discard block
 block discarded – undo
7 7
  * @version 1.0.19
8 8
  */
9 9
 
10
-defined( 'ABSPATH' ) || exit;
10
+defined('ABSPATH') || exit;
11 11
 
12
-do_action( 'getpaid_before_invoice_line_item', $invoice, $item );
12
+do_action('getpaid_before_invoice_line_item', $invoice, $item);
13 13
 
14 14
 ?>
15 15
 
16
-<div class='getpaid-invoice-item item-<?php echo (int) $item->get_id(); ?> item-type-<?php echo sanitize_html_class( $item->get_type() ); ?>'>
16
+<div class='getpaid-invoice-item item-<?php echo (int) $item->get_id(); ?> item-type-<?php echo sanitize_html_class($item->get_type()); ?>'>
17 17
 
18 18
     <div class="form-row">
19 19
 
20
-        <?php foreach ( array_keys( $columns ) as $column ): ?>
20
+        <?php foreach (array_keys($columns) as $column): ?>
21 21
 
22
-            <div class="<?php echo 'name' == $column ? 'col-12 col-sm-6' : 'col-12 col-sm' ?> getpaid-invoice-item-<?php echo sanitize_html_class( $column ); ?>">
22
+            <div class="<?php echo 'name' == $column ? 'col-12 col-sm-6' : 'col-12 col-sm' ?> getpaid-invoice-item-<?php echo sanitize_html_class($column); ?>">
23 23
 
24 24
                 <?php
25 25
 
26 26
                     // Fires before printing a line item column.
27
-                    do_action( "getpaid_invoice_line_item_before_$column", $item, $invoice );
27
+                    do_action("getpaid_invoice_line_item_before_$column", $item, $invoice);
28 28
 
29 29
                     // Item name.
30
-                    if ( 'name' == $column ) {
30
+                    if ('name' == $column) {
31 31
 
32 32
                         // Display the name.
33
-                        echo '<div class="mb-1">' . sanitize_text_field( $item->get_name() ) . '</div>';
33
+                        echo '<div class="mb-1">' . sanitize_text_field($item->get_name()) . '</div>';
34 34
 
35 35
                         // And an optional description.
36 36
                         $description = $item->get_description();
37 37
 
38
-                        if ( ! empty( $description ) ) {
39
-                            $description = wp_kses_post( $description );
38
+                        if (!empty($description)) {
39
+                            $description = wp_kses_post($description);
40 40
                             echo "<small class='form-text text-muted pr-2 m-0'>$description</small>";
41 41
                         }
42 42
 
43
-                        $actions = apply_filters( 'getpaid-invoice-page-line-item-actions', array(), $item, $invoice );
43
+                        $actions = apply_filters('getpaid-invoice-page-line-item-actions', array(), $item, $invoice);
44 44
 
45
-                        if ( ! empty( $actions ) ) {
45
+                        if (!empty($actions)) {
46 46
 
47
-                            $sanitized  = array();
48
-                            foreach ( $actions as $key => $action ) {
49
-                                $key         = sanitize_html_class( $key );
50
-                                $action      = wp_kses_post( $action );
47
+                            $sanitized = array();
48
+                            foreach ($actions as $key => $action) {
49
+                                $key         = sanitize_html_class($key);
50
+                                $action      = wp_kses_post($action);
51 51
                                 $sanitized[] = "<span class='$key'>$action</span>";
52 52
                             }
53 53
 
54 54
                             echo "<small class='form-text getpaid-line-item-actions'>";
55
-                            echo implode( ' | ', $sanitized );
55
+                            echo implode(' | ', $sanitized);
56 56
                             echo '</small>';
57 57
 
58 58
                         }
@@ -60,32 +60,32 @@  discard block
 block discarded – undo
60 60
                     }
61 61
 
62 62
                     // Item price.
63
-                    if ( 'price' == $column ) {
63
+                    if ('price' == $column) {
64 64
 
65 65
                         // Display the item price (or recurring price if this is a renewal invoice)
66
-                        if ( $invoice->is_recurring() && $invoice->is_renewal() ) {
67
-                            echo wpinv_price( wpinv_format_amount( $item->get_price() ), $invoice->get_currency() );
66
+                        if ($invoice->is_recurring() && $invoice->is_renewal()) {
67
+                            echo wpinv_price(wpinv_format_amount($item->get_price()), $invoice->get_currency());
68 68
                         } else {
69
-                            echo wpinv_price( wpinv_format_amount( $item->get_initial_price() ), $invoice->get_currency() );
69
+                            echo wpinv_price(wpinv_format_amount($item->get_initial_price()), $invoice->get_currency());
70 70
                         }
71 71
 
72 72
                     }
73 73
 
74 74
                     // Item quantity.
75
-                    if ( 'quantity' == $column ) {
75
+                    if ('quantity' == $column) {
76 76
                         echo (int) $item->get_qantity();
77 77
                     }
78 78
 
79 79
                     // Item sub total.
80
-                    if ( 'subtotal' == $column ) {
81
-                        echo wpinv_price( wpinv_format_amount( $item->get_sub_total() ), $invoice->get_currency() );
80
+                    if ('subtotal' == $column) {
81
+                        echo wpinv_price(wpinv_format_amount($item->get_sub_total()), $invoice->get_currency());
82 82
                     }
83 83
 
84 84
                     // Fires when printing a line item column.
85
-                    do_action( "getpaid_invoice_line_item_$column", $item, $invoice );
85
+                    do_action("getpaid_invoice_line_item_$column", $item, $invoice);
86 86
 
87 87
                     // Fires after printing a line item column.
88
-                    do_action( "getpaid_invoice_line_item_after_$column", $item, $invoice );
88
+                    do_action("getpaid_invoice_line_item_after_$column", $item, $invoice);
89 89
 
90 90
                 ?>
91 91
 
Please login to merge, or discard this patch.