Total Complexity | 92 |
Total Lines | 744 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Admin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Admin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class GetPaid_Admin { |
||
15 | |||
16 | /** |
||
17 | * Local path to this plugins admin directory |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | public $admin_path; |
||
22 | |||
23 | /** |
||
24 | * Web path to this plugins admin directory |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | public $admin_url; |
||
29 | |||
30 | /** |
||
31 | * Reports components. |
||
32 | * |
||
33 | * @var GetPaid_Reports |
||
34 | */ |
||
35 | public $reports; |
||
36 | |||
37 | /** |
||
38 | * Class constructor. |
||
39 | */ |
||
40 | public function __construct(){ |
||
48 | } |
||
49 | |||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Init action and filter hooks |
||
54 | * |
||
55 | */ |
||
56 | private function init_admin_hooks() { |
||
57 | add_action( 'admin_enqueue_scripts', array( $this, 'enqeue_scripts' ) ); |
||
58 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
59 | add_action( 'admin_init', array( $this, 'init_ayecode_connect_helper' ) ); |
||
60 | add_action( 'admin_init', array( $this, 'activation_redirect') ); |
||
61 | add_action( 'admin_init', array( $this, 'maybe_do_admin_action') ); |
||
62 | add_action( 'admin_notices', array( $this, 'show_notices' ) ); |
||
63 | add_action( 'getpaid_authenticated_admin_action_rate_plugin', array( $this, 'redirect_to_wordpress_rating_page' ) ); |
||
64 | add_action( 'getpaid_authenticated_admin_action_send_invoice', array( $this, 'send_customer_invoice' ) ); |
||
65 | add_action( 'getpaid_authenticated_admin_action_send_invoice_reminder', array( $this, 'send_customer_payment_reminder' ) ); |
||
66 | add_action( 'getpaid_authenticated_admin_action_reset_tax_rates', array( $this, 'admin_reset_tax_rates' ) ); |
||
67 | add_action( 'getpaid_authenticated_admin_action_create_missing_pages', array( $this, 'admin_create_missing_pages' ) ); |
||
68 | add_action( 'getpaid_authenticated_admin_action_create_missing_tables', array( $this, 'admin_create_missing_tables' ) ); |
||
69 | add_action( 'getpaid_authenticated_admin_action_migrate_old_invoices', array( $this, 'admin_migrate_old_invoices' ) ); |
||
70 | add_action( 'getpaid_authenticated_admin_action_download_customers', array( $this, 'admin_download_customers' ) ); |
||
71 | add_action( 'getpaid_authenticated_admin_action_recalculate_discounts', array( $this, 'admin_recalculate_discounts' ) ); |
||
72 | add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) ); |
||
73 | do_action( 'getpaid_init_admin_hooks', $this ); |
||
74 | |||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Register admin scripts |
||
79 | * |
||
80 | */ |
||
81 | public function enqeue_scripts() { |
||
82 | global $current_screen, $pagenow; |
||
83 | |||
84 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
85 | $editing = $pagenow == 'post.php' || $pagenow == 'post-new.php'; |
||
86 | |||
87 | if ( ! empty( $current_screen->post_type ) ) { |
||
88 | $page = $current_screen->post_type; |
||
89 | } |
||
90 | |||
91 | // General styles. |
||
92 | if ( false !== stripos( $page, 'wpi' ) ) { |
||
93 | |||
94 | // Styles. |
||
95 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' ); |
||
96 | wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version ); |
||
97 | wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' ); |
||
98 | |||
99 | // Scripts. |
||
100 | wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION ); |
||
101 | |||
102 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' ); |
||
103 | wp_enqueue_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'wp-color-picker' ), $version ); |
||
104 | wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) ); |
||
105 | |||
106 | } |
||
107 | |||
108 | // Payment form scripts. |
||
109 | if ( 'wpi_payment_form' == $page && $editing ) { |
||
110 | $this->load_payment_form_scripts(); |
||
111 | } |
||
112 | |||
113 | if ( $page == 'wpinv-subscriptions' ) { |
||
114 | wp_enqueue_script( 'postbox' ); |
||
115 | } |
||
116 | |||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns admin js translations. |
||
121 | * |
||
122 | */ |
||
123 | protected function get_admin_i18() { |
||
124 | global $post; |
||
125 | |||
126 | $date_range = array( |
||
127 | 'period' => isset( $_GET['date_range'] ) ? sanitize_text_field( $_GET['date_range'] ) : '7_days' |
||
128 | ); |
||
129 | |||
130 | if ( $date_range['period'] == 'custom' ) { |
||
131 | |||
132 | if ( isset( $_GET['from'] ) ) { |
||
133 | $date_range[ 'after' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['from'] ), current_time( 'timestamp' ) ) - DAY_IN_SECONDS ); |
||
134 | } |
||
135 | |||
136 | if ( isset( $_GET['to'] ) ) { |
||
137 | $date_range[ 'before' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['to'] ), current_time( 'timestamp' ) ) + DAY_IN_SECONDS ); |
||
138 | } |
||
139 | |||
140 | } |
||
141 | |||
142 | $i18n = array( |
||
143 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
144 | 'post_ID' => isset( $post->ID ) ? $post->ID : '', |
||
145 | 'wpinv_nonce' => wp_create_nonce( 'wpinv-nonce' ), |
||
146 | 'rest_nonce' => wp_create_nonce( 'wp_rest' ), |
||
147 | 'rest_root' => esc_url_raw( rest_url() ), |
||
148 | 'date_range' => $date_range, |
||
149 | 'add_invoice_note_nonce' => wp_create_nonce( 'add-invoice-note' ), |
||
150 | 'delete_invoice_note_nonce' => wp_create_nonce( 'delete-invoice-note' ), |
||
151 | 'invoice_item_nonce' => wp_create_nonce( 'invoice-item' ), |
||
152 | 'billing_details_nonce' => wp_create_nonce( 'get-billing-details' ), |
||
153 | 'tax' => wpinv_tax_amount(), |
||
154 | 'discount' => 0, |
||
155 | 'currency_symbol' => wpinv_currency_symbol(), |
||
156 | 'currency' => wpinv_get_currency(), |
||
157 | 'currency_pos' => wpinv_currency_position(), |
||
158 | 'thousand_sep' => wpinv_thousands_separator(), |
||
159 | 'decimal_sep' => wpinv_decimal_separator(), |
||
160 | 'decimals' => wpinv_decimals(), |
||
161 | 'save_invoice' => __( 'Save Invoice', 'invoicing' ), |
||
162 | 'status_publish' => wpinv_status_nicename( 'publish' ), |
||
163 | 'status_pending' => wpinv_status_nicename( 'wpi-pending' ), |
||
164 | 'delete_tax_rate' => __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ), |
||
165 | 'status_pending' => wpinv_status_nicename( 'wpi-pending' ), |
||
166 | 'FillBillingDetails' => __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ), |
||
167 | 'confirmCalcTotals' => __( 'Recalculate totals? This will recalculate totals based on the user billing country. If no billing country is set it will use the base country.', 'invoicing' ), |
||
168 | 'AreYouSure' => __( 'Are you sure?', 'invoicing' ), |
||
169 | 'errDeleteItem' => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ), |
||
170 | 'delete_subscription' => __( 'Are you sure you want to delete this subscription?', 'invoicing' ), |
||
171 | 'action_edit' => __( 'Edit', 'invoicing' ), |
||
172 | 'action_cancel' => __( 'Cancel', 'invoicing' ), |
||
173 | 'item_description' => __( 'Item Description', 'invoicing' ), |
||
174 | 'invoice_description' => __( 'Invoice Description', 'invoicing' ), |
||
175 | 'discount_description' => __( 'Discount Description', 'invoicing' ), |
||
176 | 'searching' => __( 'Searching', 'invoicing' ), |
||
177 | 'loading' => __( 'Loading...', 'invoicing' ), |
||
178 | 'search_customers' => __( 'Enter customer name or email', 'invoicing' ), |
||
179 | 'search_items' => __( 'Enter item name', 'invoicing' ), |
||
180 | ); |
||
181 | |||
182 | if ( ! empty( $post ) && getpaid_is_invoice_post_type( $post->post_type ) ) { |
||
183 | |||
184 | $invoice = new WPInv_Invoice( $post ); |
||
185 | $i18n['save_invoice'] = sprintf( |
||
186 | __( 'Save %s', 'invoicing' ), |
||
187 | ucfirst( $invoice->get_invoice_quote_type() ) |
||
188 | ); |
||
189 | |||
190 | $i18n['invoice_description'] = sprintf( |
||
191 | __( '%s Description', 'invoicing' ), |
||
192 | ucfirst( $invoice->get_invoice_quote_type() ) |
||
193 | ); |
||
194 | |||
195 | } |
||
196 | return $i18n; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Change the admin footer text on GetPaid admin pages. |
||
201 | * |
||
202 | * @since 2.0.0 |
||
203 | * @param string $footer_text |
||
204 | * @return string |
||
205 | */ |
||
206 | public function admin_footer_text( $footer_text ) { |
||
207 | global $current_screen; |
||
208 | |||
209 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
210 | |||
211 | if ( ! empty( $current_screen->post_type ) ) { |
||
212 | $page = $current_screen->post_type; |
||
213 | } |
||
214 | |||
215 | // General styles. |
||
216 | if ( apply_filters( 'getpaid_display_admin_footer_text', wpinv_current_user_can_manage_invoicing() ) && false !== stripos( $page, 'wpi' ) ) { |
||
217 | |||
218 | // Change the footer text |
||
219 | if ( ! get_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', true ) ) { |
||
220 | |||
221 | $rating_url = esc_url( |
||
222 | wp_nonce_url( |
||
223 | admin_url( 'admin.php?page=wpinv-reports&getpaid-admin-action=rate_plugin' ), |
||
224 | 'getpaid-nonce', |
||
225 | 'getpaid-nonce' |
||
226 | ) |
||
227 | ); |
||
228 | |||
229 | $footer_text = sprintf( |
||
230 | /* translators: %s: five stars */ |
||
231 | __( 'If you like <strong>GetPaid</strong>, please leave us a %s rating. A huge thanks in advance!', 'invoicing' ), |
||
232 | "<a href='$rating_url'>★★★★★</a>" |
||
233 | ); |
||
234 | |||
235 | } else { |
||
236 | |||
237 | $footer_text = sprintf( |
||
238 | /* translators: %s: GetPaid */ |
||
239 | __( 'Thank you for using %s!', 'invoicing' ), |
||
240 | "<a href='https://wpgetpaid.com/' target='_blank'><strong>GetPaid</strong></a>" |
||
241 | ); |
||
242 | |||
243 | } |
||
244 | |||
245 | } |
||
246 | |||
247 | return $footer_text; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Redirects to wp.org to rate the plugin. |
||
252 | * |
||
253 | * @since 2.0.0 |
||
254 | */ |
||
255 | public function redirect_to_wordpress_rating_page() { |
||
256 | update_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', 1 ); |
||
257 | wp_redirect( 'https://wordpress.org/support/plugin/invoicing/reviews?rate=5#new-post' ); |
||
258 | exit; |
||
|
|||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Loads payment form js. |
||
263 | * |
||
264 | */ |
||
265 | protected function load_payment_form_scripts() { |
||
266 | global $post; |
||
267 | |||
268 | wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION ); |
||
269 | wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION ); |
||
270 | wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION ); |
||
271 | |||
272 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' ); |
||
273 | wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ), $version ); |
||
274 | |||
275 | wp_localize_script( |
||
276 | 'wpinv-admin-payment-form-script', |
||
277 | 'wpinvPaymentFormAdmin', |
||
278 | array( |
||
279 | 'elements' => wpinv_get_data( 'payment-form-elements' ), |
||
280 | 'form_elements' => getpaid_get_payment_form_elements( $post->ID ), |
||
281 | 'currency' => wpinv_currency_symbol(), |
||
282 | 'position' => wpinv_currency_position(), |
||
283 | 'decimals' => (int) wpinv_decimals(), |
||
284 | 'thousands_sep' => wpinv_thousands_separator(), |
||
285 | 'decimals_sep' => wpinv_decimal_separator(), |
||
286 | 'form_items' => gepaid_get_form_items( $post->ID ), |
||
287 | 'is_default' => $post->ID == wpinv_get_default_payment_form(), |
||
288 | ) |
||
289 | ); |
||
290 | |||
291 | wp_enqueue_script( 'wpinv-admin-payment-form-script' ); |
||
292 | |||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Add our classes to admin pages. |
||
297 | * |
||
298 | * @param string $classes |
||
299 | * @return string |
||
300 | * |
||
301 | */ |
||
302 | public function admin_body_class( $classes ) { |
||
303 | global $pagenow, $post, $current_screen; |
||
304 | |||
305 | |||
306 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
307 | |||
308 | if ( ! empty( $current_screen->post_type ) ) { |
||
309 | $page = $current_screen->post_type; |
||
310 | } |
||
311 | |||
312 | if ( false !== stripos( $page, 'wpi' ) ) { |
||
313 | $classes .= ' wpi-' . sanitize_key( $page ); |
||
314 | } |
||
315 | |||
316 | if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) { |
||
317 | $classes .= ' wpinv-cpt wpinv'; |
||
318 | } |
||
319 | |||
320 | if ( getpaid_is_invoice_post_type( $page ) ) { |
||
321 | $classes .= ' getpaid-is-invoice-cpt'; |
||
322 | } |
||
323 | |||
324 | return $classes; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Maybe show the AyeCode Connect Notice. |
||
329 | */ |
||
330 | public function init_ayecode_connect_helper(){ |
||
331 | |||
332 | new AyeCode_Connect_Helper( |
||
333 | array( |
||
334 | 'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"), |
||
335 | 'connect_external' => __( "Please confirm you wish to connect your site?","invoicing" ), |
||
336 | 'connect' => sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s","invoicing" ),"<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>","</a>" ), |
||
337 | 'connect_button' => __("Connect Site","invoicing"), |
||
338 | 'connecting_button' => __("Connecting...","invoicing"), |
||
339 | 'error_localhost' => __( "This service will only work with a live domain, not a localhost.","invoicing" ), |
||
340 | 'error' => __( "Something went wrong, please refresh and try again.","invoicing" ), |
||
341 | ), |
||
342 | array( 'wpi-addons' ) |
||
343 | ); |
||
344 | |||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Redirect users to settings on activation. |
||
349 | * |
||
350 | * @return void |
||
351 | */ |
||
352 | public function activation_redirect() { |
||
353 | |||
354 | $redirected = get_option( 'wpinv_redirected_to_settings' ); |
||
355 | |||
356 | if ( ! empty( $redirected ) || wp_doing_ajax() || ! current_user_can( 'manage_options' ) ) { |
||
357 | return; |
||
358 | } |
||
359 | |||
360 | // Bail if activating from network, or bulk |
||
361 | if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { |
||
362 | return; |
||
363 | } |
||
364 | |||
365 | update_option( 'wpinv_redirected_to_settings', 1 ); |
||
366 | |||
367 | wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) ); |
||
368 | exit; |
||
369 | |||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Fires an admin action after verifying that a user can fire them. |
||
374 | */ |
||
375 | public function maybe_do_admin_action() { |
||
376 | |||
377 | if ( wpinv_current_user_can_manage_invoicing() && isset( $_REQUEST['getpaid-admin-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) { |
||
378 | $key = sanitize_key( $_REQUEST['getpaid-admin-action'] ); |
||
379 | do_action( "getpaid_authenticated_admin_action_$key", $_REQUEST ); |
||
380 | } |
||
381 | |||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Sends a payment reminder to a customer. |
||
386 | * |
||
387 | * @param array $args |
||
388 | */ |
||
389 | public function send_customer_invoice( $args ) { |
||
390 | $sent = getpaid()->get( 'invoice_emails' )->user_invoice( new WPInv_Invoice( $args['invoice_id'] ), true ); |
||
391 | |||
392 | if ( $sent ) { |
||
393 | $this->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) ); |
||
394 | } else { |
||
395 | $this->show_error( __( 'Could not send the invoice to the customer', 'invoicing' ) ); |
||
396 | } |
||
397 | |||
398 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) ); |
||
399 | exit; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Sends a payment reminder to a customer. |
||
404 | * |
||
405 | * @param array $args |
||
406 | */ |
||
407 | public function send_customer_payment_reminder( $args ) { |
||
408 | $sent = getpaid()->get( 'invoice_emails' )->force_send_overdue_notice( new WPInv_Invoice( $args['invoice_id'] ) ); |
||
409 | |||
410 | if ( $sent ) { |
||
411 | $this->show_success( __( 'Payment reminder was successfully sent to the customer', 'invoicing' ) ); |
||
412 | } else { |
||
413 | $this->show_error( __( 'Could not sent payment reminder to the customer', 'invoicing' ) ); |
||
414 | } |
||
415 | |||
416 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) ); |
||
417 | exit; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Resets tax rates. |
||
422 | * |
||
423 | */ |
||
424 | public function admin_reset_tax_rates() { |
||
425 | |||
426 | update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) ); |
||
427 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
428 | exit; |
||
429 | |||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Resets admin pages. |
||
434 | * |
||
435 | */ |
||
436 | public function admin_create_missing_pages() { |
||
437 | $installer = new GetPaid_Installer(); |
||
438 | $installer->create_pages(); |
||
439 | $this->show_success( __( 'GetPaid pages updated.', 'invoicing' ) ); |
||
440 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
441 | exit; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Creates an missing admin tables. |
||
446 | * |
||
447 | */ |
||
448 | public function admin_create_missing_tables() { |
||
449 | global $wpdb; |
||
450 | $installer = new GetPaid_Installer(); |
||
451 | |||
452 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}wpinv_subscriptions'" ) != $wpdb->prefix . 'wpinv_subscriptions' ) { |
||
453 | $installer->create_subscriptions_table(); |
||
454 | |||
455 | if ( $wpdb->last_error !== '' ) { |
||
456 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
457 | } |
||
458 | } |
||
459 | |||
460 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoices'" ) != $wpdb->prefix . 'getpaid_invoices' ) { |
||
461 | $installer->create_invoices_table(); |
||
462 | |||
463 | if ( $wpdb->last_error !== '' ) { |
||
464 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
465 | } |
||
466 | } |
||
467 | |||
468 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoice_items'" ) != $wpdb->prefix . 'getpaid_invoice_items' ) { |
||
469 | $installer->create_invoice_items_table(); |
||
470 | |||
471 | if ( $wpdb->last_error !== '' ) { |
||
472 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
473 | } |
||
474 | } |
||
475 | |||
476 | if ( ! $this->has_notices() ) { |
||
477 | $this->show_success( __( 'Your GetPaid tables have been updated.', 'invoicing' ) ); |
||
478 | } |
||
479 | |||
480 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
481 | exit; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Migrates old invoices to the new database tables. |
||
486 | * |
||
487 | */ |
||
488 | public function admin_migrate_old_invoices() { |
||
489 | |||
490 | // Migrate the invoices. |
||
491 | $installer = new GetPaid_Installer(); |
||
492 | $installer->migrate_old_invoices(); |
||
493 | |||
494 | // Show an admin message. |
||
495 | $this->show_success( __( 'Your invoices have been migrated.', 'invoicing' ) ); |
||
496 | |||
497 | // Redirect the admin. |
||
498 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
499 | exit; |
||
500 | |||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Download customers. |
||
505 | * |
||
506 | */ |
||
507 | public function admin_download_customers() { |
||
508 | global $wpdb; |
||
509 | |||
510 | $output = fopen( 'php://output', 'w' ) or die( __( 'Unsupported server', 'invoicing' ) ); |
||
511 | |||
512 | header( "Content-Type:text/csv" ); |
||
513 | header( "Content-Disposition:attachment;filename=customers.csv" ); |
||
514 | |||
515 | $post_types = ''; |
||
516 | |||
517 | foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) { |
||
518 | $post_types .= $wpdb->prepare( "post_type=%s OR ", $post_type ); |
||
519 | } |
||
520 | |||
521 | $post_types = rtrim( $post_types, ' OR' ); |
||
522 | |||
523 | $customers = $wpdb->get_col( |
||
524 | $wpdb->prepare( |
||
525 | "SELECT DISTINCT( post_author ) FROM $wpdb->posts WHERE $post_types" |
||
526 | ) |
||
527 | ); |
||
528 | |||
529 | $columns = array( |
||
530 | 'name' => __( 'Name', 'invoicing' ), |
||
531 | 'email' => __( 'Email', 'invoicing' ), |
||
532 | 'country' => __( 'Country', 'invoicing' ), |
||
533 | 'state' => __( 'State', 'invoicing' ), |
||
534 | 'city' => __( 'City', 'invoicing' ), |
||
535 | 'zip' => __( 'ZIP', 'invoicing' ), |
||
536 | 'address' => __( 'Address', 'invoicing' ), |
||
537 | 'phone' => __( 'Phone', 'invoicing' ), |
||
538 | 'company' => __( 'Company', 'invoicing' ), |
||
539 | 'invoices' => __( 'Invoices', 'invoicing' ), |
||
540 | 'total' => __( 'Total Spend', 'invoicing' ), |
||
541 | 'signup' => __( 'Date created', 'invoicing' ), |
||
542 | ); |
||
543 | |||
544 | // Output the csv column headers. |
||
545 | fputcsv( $output, array_values( $columns ) ); |
||
546 | |||
547 | // Loop through |
||
548 | $table = new WPInv_Customers_Table(); |
||
549 | foreach ( $customers as $customer_id ) { |
||
550 | |||
551 | $user = get_user_by( 'id', $customer_id ); |
||
552 | $row = array(); |
||
553 | if ( empty( $user ) ) { |
||
554 | continue; |
||
555 | } |
||
556 | |||
557 | foreach ( array_keys( $columns ) as $column ) { |
||
558 | |||
559 | $method = 'column_' . $column; |
||
560 | |||
561 | if ( 'name' == $column ) { |
||
562 | $value = sanitize_text_field( $user->display_name ); |
||
563 | } else if( 'email' == $column ) { |
||
564 | $value = sanitize_email( $user->user_email ); |
||
565 | } else if ( is_callable( array( $table, $method ) ) ) { |
||
566 | $value = strip_tags( $table->$method( $user ) ); |
||
567 | } |
||
568 | |||
569 | if ( empty( $value ) ) { |
||
570 | $value = sanitize_text_field( get_user_meta( $user->ID, '_wpinv_' . $column, true ) ); |
||
571 | } |
||
572 | |||
573 | $row[] = $value; |
||
574 | |||
575 | } |
||
576 | |||
577 | fputcsv( $output, $row ); |
||
578 | } |
||
579 | |||
580 | fclose( $output ); |
||
581 | exit; |
||
582 | |||
583 | } |
||
584 | |||
585 | /** |
||
586 | * Recalculates discounts. |
||
587 | * |
||
588 | */ |
||
589 | public function admin_recalculate_discounts() { |
||
590 | global $wpdb; |
||
591 | |||
592 | // Fetch all invoices that have discount codes. |
||
593 | $table = $wpdb->prefix . 'getpaid_invoices'; |
||
594 | $invoices = $wpdb->get_col( "SELECT `post_id` FROM `$table` WHERE `discount` = 0 && `discount_code` <> ''" ); |
||
595 | |||
596 | foreach ( $invoices as $invoice ) { |
||
597 | |||
598 | $invoice = new WPInv_Invoice( $invoice ); |
||
599 | |||
600 | if ( ! $invoice->exists() ) { |
||
601 | continue; |
||
602 | } |
||
603 | |||
604 | // Abort if the discount does not exist or does not apply here. |
||
605 | $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
||
606 | if ( ! $discount->exists() ) { |
||
607 | continue; |
||
608 | } |
||
609 | |||
610 | $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
||
611 | $invoice->recalculate_total(); |
||
612 | |||
613 | if ( $invoice->get_total_discount() > 0 ) { |
||
614 | $invoice->save(); |
||
615 | } |
||
616 | |||
617 | } |
||
618 | |||
619 | // Show an admin message. |
||
620 | $this->show_success( __( 'Discounts have been recalculated.', 'invoicing' ) ); |
||
621 | |||
622 | // Redirect the admin. |
||
623 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
624 | exit; |
||
625 | |||
626 | } |
||
627 | |||
628 | /** |
||
629 | * Returns an array of admin notices. |
||
630 | * |
||
631 | * @since 1.0.19 |
||
632 | * @return array |
||
633 | */ |
||
634 | public function get_notices() { |
||
635 | $notices = get_option( 'wpinv_admin_notices' ); |
||
636 | return is_array( $notices ) ? $notices : array(); |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * Checks if we have any admin notices. |
||
641 | * |
||
642 | * @since 2.0.4 |
||
643 | * @return array |
||
644 | */ |
||
645 | public function has_notices() { |
||
646 | return count( $this->get_notices() ) > 0; |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * Clears all admin notices |
||
651 | * |
||
652 | * @access public |
||
653 | * @since 1.0.19 |
||
654 | */ |
||
655 | public function clear_notices() { |
||
656 | delete_option( 'wpinv_admin_notices' ); |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * Saves a new admin notice |
||
661 | * |
||
662 | * @access public |
||
663 | * @since 1.0.19 |
||
664 | */ |
||
665 | public function save_notice( $type, $message ) { |
||
666 | $notices = $this->get_notices(); |
||
667 | |||
668 | if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) { |
||
669 | $notices[ $type ] = array(); |
||
670 | } |
||
671 | |||
672 | $notices[ $type ][] = $message; |
||
673 | |||
674 | update_option( 'wpinv_admin_notices', $notices ); |
||
675 | } |
||
676 | |||
677 | /** |
||
678 | * Displays a success notice |
||
679 | * |
||
680 | * @param string $msg The message to qeue. |
||
681 | * @access public |
||
682 | * @since 1.0.19 |
||
683 | */ |
||
684 | public function show_success( $msg ) { |
||
685 | $this->save_notice( 'success', $msg ); |
||
686 | } |
||
687 | |||
688 | /** |
||
689 | * Displays a error notice |
||
690 | * |
||
691 | * @access public |
||
692 | * @param string $msg The message to qeue. |
||
693 | * @since 1.0.19 |
||
694 | */ |
||
695 | public function show_error( $msg ) { |
||
697 | } |
||
698 | |||
699 | /** |
||
700 | * Displays a warning notice |
||
701 | * |
||
702 | * @access public |
||
703 | * @param string $msg The message to qeue. |
||
704 | * @since 1.0.19 |
||
705 | */ |
||
706 | public function show_warning( $msg ) { |
||
708 | } |
||
709 | |||
710 | /** |
||
711 | * Displays a info notice |
||
712 | * |
||
713 | * @access public |
||
714 | * @param string $msg The message to qeue. |
||
715 | * @since 1.0.19 |
||
716 | */ |
||
717 | public function show_info( $msg ) { |
||
719 | } |
||
720 | |||
721 | /** |
||
722 | * Show notices |
||
723 | * |
||
724 | * @access public |
||
725 | * @since 1.0.19 |
||
726 | */ |
||
727 | public function show_notices() { |
||
728 | |||
758 | } |
||
759 | |||
760 | } |
||
761 | |||
762 | } |
||
763 | |||
764 | } |
||
765 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.