| Total Complexity | 82 |
| Total Lines | 661 |
| 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_recalculate_discounts', array( $this, 'admin_recalculate_discounts' ) ); |
||
| 71 | add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) ); |
||
| 72 | do_action( 'getpaid_init_admin_hooks', $this ); |
||
| 73 | |||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Register admin scripts |
||
| 78 | * |
||
| 79 | */ |
||
| 80 | public function enqeue_scripts() { |
||
| 81 | global $current_screen, $pagenow; |
||
| 82 | |||
| 83 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
| 84 | $editing = $pagenow == 'post.php' || $pagenow == 'post-new.php'; |
||
| 85 | |||
| 86 | if ( ! empty( $current_screen->post_type ) ) { |
||
| 87 | $page = $current_screen->post_type; |
||
| 88 | } |
||
| 89 | |||
| 90 | // General styles. |
||
| 91 | if ( false !== stripos( $page, 'wpi' ) ) { |
||
| 92 | |||
| 93 | // Styles. |
||
| 94 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' ); |
||
| 95 | wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version ); |
||
| 96 | wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' ); |
||
| 97 | |||
| 98 | // Scripts. |
||
| 99 | wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION ); |
||
| 100 | |||
| 101 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' ); |
||
| 102 | wp_enqueue_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'wp-color-picker' ), $version ); |
||
| 103 | wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) ); |
||
| 104 | |||
| 105 | } |
||
| 106 | |||
| 107 | // Payment form scripts. |
||
| 108 | if ( 'wpi_payment_form' == $page && $editing ) { |
||
| 109 | $this->load_payment_form_scripts(); |
||
| 110 | } |
||
| 111 | |||
| 112 | if ( $page == 'wpinv-subscriptions' ) { |
||
| 113 | wp_enqueue_script( 'postbox' ); |
||
| 114 | } |
||
| 115 | |||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns admin js translations. |
||
| 120 | * |
||
| 121 | */ |
||
| 122 | protected function get_admin_i18() { |
||
| 123 | global $post; |
||
| 124 | |||
| 125 | $date_range = array( |
||
| 126 | 'period' => isset( $_GET['date_range'] ) ? sanitize_text_field( $_GET['date_range'] ) : '7_days' |
||
| 127 | ); |
||
| 128 | |||
| 129 | if ( $date_range['period'] == 'custom' ) { |
||
| 130 | |||
| 131 | if ( isset( $_GET['from'] ) ) { |
||
| 132 | $date_range[ 'after' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['from'] ), current_time( 'timestamp' ) ) - DAY_IN_SECONDS ); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ( isset( $_GET['to'] ) ) { |
||
| 136 | $date_range[ 'before' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['to'] ), current_time( 'timestamp' ) ) + DAY_IN_SECONDS ); |
||
| 137 | } |
||
| 138 | |||
| 139 | } |
||
| 140 | |||
| 141 | $i18n = array( |
||
| 142 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
| 143 | 'post_ID' => isset( $post->ID ) ? $post->ID : '', |
||
| 144 | 'wpinv_nonce' => wp_create_nonce( 'wpinv-nonce' ), |
||
| 145 | 'rest_nonce' => wp_create_nonce( 'wp_rest' ), |
||
| 146 | 'rest_root' => esc_url_raw( rest_url() ), |
||
| 147 | 'date_range' => $date_range, |
||
| 148 | 'add_invoice_note_nonce' => wp_create_nonce( 'add-invoice-note' ), |
||
| 149 | 'delete_invoice_note_nonce' => wp_create_nonce( 'delete-invoice-note' ), |
||
| 150 | 'invoice_item_nonce' => wp_create_nonce( 'invoice-item' ), |
||
| 151 | 'billing_details_nonce' => wp_create_nonce( 'get-billing-details' ), |
||
| 152 | 'tax' => wpinv_tax_amount(), |
||
| 153 | 'discount' => 0, |
||
| 154 | 'currency_symbol' => wpinv_currency_symbol(), |
||
| 155 | 'currency' => wpinv_get_currency(), |
||
| 156 | 'currency_pos' => wpinv_currency_position(), |
||
| 157 | 'thousand_sep' => wpinv_thousands_separator(), |
||
| 158 | 'decimal_sep' => wpinv_decimal_separator(), |
||
| 159 | 'decimals' => wpinv_decimals(), |
||
| 160 | 'save_invoice' => __( 'Save Invoice', 'invoicing' ), |
||
| 161 | 'status_publish' => wpinv_status_nicename( 'publish' ), |
||
| 162 | 'status_pending' => wpinv_status_nicename( 'wpi-pending' ), |
||
| 163 | 'delete_tax_rate' => __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ), |
||
| 164 | 'status_pending' => wpinv_status_nicename( 'wpi-pending' ), |
||
| 165 | 'FillBillingDetails' => __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ), |
||
| 166 | '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' ), |
||
| 167 | 'AreYouSure' => __( 'Are you sure?', 'invoicing' ), |
||
| 168 | 'errDeleteItem' => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ), |
||
| 169 | 'delete_subscription' => __( 'Are you sure you want to delete this subscription?', 'invoicing' ), |
||
| 170 | 'action_edit' => __( 'Edit', 'invoicing' ), |
||
| 171 | 'action_cancel' => __( 'Cancel', 'invoicing' ), |
||
| 172 | 'item_description' => __( 'Item Description', 'invoicing' ), |
||
| 173 | 'invoice_description' => __( 'Invoice Description', 'invoicing' ), |
||
| 174 | 'discount_description' => __( 'Discount Description', 'invoicing' ), |
||
| 175 | 'searching' => __( 'Searching', 'invoicing' ), |
||
| 176 | 'loading' => __( 'Loading...', 'invoicing' ), |
||
| 177 | ); |
||
| 178 | |||
| 179 | if ( ! empty( $post ) && getpaid_is_invoice_post_type( $post->post_type ) ) { |
||
| 180 | |||
| 181 | $invoice = new WPInv_Invoice( $post ); |
||
| 182 | $i18n['save_invoice'] = sprintf( |
||
| 183 | __( 'Save %s', 'invoicing' ), |
||
| 184 | ucfirst( $invoice->get_invoice_quote_type() ) |
||
| 185 | ); |
||
| 186 | |||
| 187 | $i18n['invoice_description'] = sprintf( |
||
| 188 | __( '%s Description', 'invoicing' ), |
||
| 189 | ucfirst( $invoice->get_invoice_quote_type() ) |
||
| 190 | ); |
||
| 191 | |||
| 192 | } |
||
| 193 | return $i18n; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Change the admin footer text on GetPaid admin pages. |
||
| 198 | * |
||
| 199 | * @since 2.0.0 |
||
| 200 | * @param string $footer_text |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function admin_footer_text( $footer_text ) { |
||
| 204 | global $current_screen; |
||
| 205 | |||
| 206 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
| 207 | |||
| 208 | if ( ! empty( $current_screen->post_type ) ) { |
||
| 209 | $page = $current_screen->post_type; |
||
| 210 | } |
||
| 211 | |||
| 212 | // General styles. |
||
| 213 | if ( apply_filters( 'getpaid_display_admin_footer_text', wpinv_current_user_can_manage_invoicing() ) && false !== stripos( $page, 'wpi' ) ) { |
||
| 214 | |||
| 215 | // Change the footer text |
||
| 216 | if ( ! get_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', true ) ) { |
||
| 217 | |||
| 218 | $rating_url = esc_url( |
||
| 219 | wp_nonce_url( |
||
| 220 | admin_url( 'admin.php?page=wpinv-reports&getpaid-admin-action=rate_plugin' ), |
||
| 221 | 'getpaid-nonce', |
||
| 222 | 'getpaid-nonce' |
||
| 223 | ) |
||
| 224 | ); |
||
| 225 | |||
| 226 | $footer_text = sprintf( |
||
| 227 | /* translators: %s: five stars */ |
||
| 228 | __( 'If you like <strong>GetPaid</strong>, please leave us a %s rating. A huge thanks in advance!', 'invoicing' ), |
||
| 229 | "<a href='$rating_url'>★★★★★</a>" |
||
| 230 | ); |
||
| 231 | |||
| 232 | } else { |
||
| 233 | |||
| 234 | $footer_text = sprintf( |
||
| 235 | /* translators: %s: GetPaid */ |
||
| 236 | __( 'Thank you for using %s!', 'invoicing' ), |
||
| 237 | "<a href='https://wpgetpaid.com/' target='_blank'><strong>GetPaid</strong></a>" |
||
| 238 | ); |
||
| 239 | |||
| 240 | } |
||
| 241 | |||
| 242 | } |
||
| 243 | |||
| 244 | return $footer_text; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Redirects to wp.org to rate the plugin. |
||
| 249 | * |
||
| 250 | * @since 2.0.0 |
||
| 251 | */ |
||
| 252 | public function redirect_to_wordpress_rating_page() { |
||
| 253 | update_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', 1 ); |
||
| 254 | wp_redirect( 'https://wordpress.org/support/plugin/invoicing/reviews?rate=5#new-post' ); |
||
| 255 | exit; |
||
|
|
|||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Loads payment form js. |
||
| 260 | * |
||
| 261 | */ |
||
| 262 | protected function load_payment_form_scripts() { |
||
| 263 | global $post; |
||
| 264 | |||
| 265 | wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION ); |
||
| 266 | wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION ); |
||
| 267 | wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION ); |
||
| 268 | |||
| 269 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' ); |
||
| 270 | wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ), $version ); |
||
| 271 | |||
| 272 | wp_localize_script( |
||
| 273 | 'wpinv-admin-payment-form-script', |
||
| 274 | 'wpinvPaymentFormAdmin', |
||
| 275 | array( |
||
| 276 | 'elements' => wpinv_get_data( 'payment-form-elements' ), |
||
| 277 | 'form_elements' => getpaid_get_payment_form_elements( $post->ID ), |
||
| 278 | 'currency' => wpinv_currency_symbol(), |
||
| 279 | 'position' => wpinv_currency_position(), |
||
| 280 | 'decimals' => (int) wpinv_decimals(), |
||
| 281 | 'thousands_sep' => wpinv_thousands_separator(), |
||
| 282 | 'decimals_sep' => wpinv_decimal_separator(), |
||
| 283 | 'form_items' => gepaid_get_form_items( $post->ID ), |
||
| 284 | 'is_default' => $post->ID == wpinv_get_default_payment_form(), |
||
| 285 | ) |
||
| 286 | ); |
||
| 287 | |||
| 288 | wp_enqueue_script( 'wpinv-admin-payment-form-script' ); |
||
| 289 | |||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Add our classes to admin pages. |
||
| 294 | * |
||
| 295 | * @param string $classes |
||
| 296 | * @return string |
||
| 297 | * |
||
| 298 | */ |
||
| 299 | public function admin_body_class( $classes ) { |
||
| 300 | global $pagenow, $post, $current_screen; |
||
| 301 | |||
| 302 | |||
| 303 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
| 304 | |||
| 305 | if ( ! empty( $current_screen->post_type ) ) { |
||
| 306 | $page = $current_screen->post_type; |
||
| 307 | } |
||
| 308 | |||
| 309 | if ( false !== stripos( $page, 'wpi' ) ) { |
||
| 310 | $classes .= ' wpi-' . sanitize_key( $page ); |
||
| 311 | } |
||
| 312 | |||
| 313 | if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) { |
||
| 314 | $classes .= ' wpinv-cpt wpinv'; |
||
| 315 | } |
||
| 316 | |||
| 317 | if ( getpaid_is_invoice_post_type( $page ) ) { |
||
| 318 | $classes .= ' getpaid-is-invoice-cpt'; |
||
| 319 | } |
||
| 320 | |||
| 321 | return $classes; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Maybe show the AyeCode Connect Notice. |
||
| 326 | */ |
||
| 327 | public function init_ayecode_connect_helper(){ |
||
| 328 | |||
| 329 | new AyeCode_Connect_Helper( |
||
| 330 | array( |
||
| 331 | 'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"), |
||
| 332 | 'connect_external' => __( "Please confirm you wish to connect your site?","invoicing" ), |
||
| 333 | '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>" ), |
||
| 334 | 'connect_button' => __("Connect Site","invoicing"), |
||
| 335 | 'connecting_button' => __("Connecting...","invoicing"), |
||
| 336 | 'error_localhost' => __( "This service will only work with a live domain, not a localhost.","invoicing" ), |
||
| 337 | 'error' => __( "Something went wrong, please refresh and try again.","invoicing" ), |
||
| 338 | ), |
||
| 339 | array( 'wpi-addons' ) |
||
| 340 | ); |
||
| 341 | |||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Redirect users to settings on activation. |
||
| 346 | * |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function activation_redirect() { |
||
| 350 | |||
| 351 | $redirected = get_option( 'wpinv_redirected_to_settings' ); |
||
| 352 | |||
| 353 | if ( ! empty( $redirected ) || wp_doing_ajax() || ! current_user_can( 'manage_options' ) ) { |
||
| 354 | return; |
||
| 355 | } |
||
| 356 | |||
| 357 | // Bail if activating from network, or bulk |
||
| 358 | if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { |
||
| 359 | return; |
||
| 360 | } |
||
| 361 | |||
| 362 | update_option( 'wpinv_redirected_to_settings', 1 ); |
||
| 363 | |||
| 364 | wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) ); |
||
| 365 | exit; |
||
| 366 | |||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Fires an admin action after verifying that a user can fire them. |
||
| 371 | */ |
||
| 372 | public function maybe_do_admin_action() { |
||
| 377 | } |
||
| 378 | |||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Sends a payment reminder to a customer. |
||
| 383 | * |
||
| 384 | * @param array $args |
||
| 385 | */ |
||
| 386 | public function send_customer_invoice( $args ) { |
||
| 387 | $sent = getpaid()->get( 'invoice_emails' )->user_invoice( new WPInv_Invoice( $args['invoice_id'] ) ); |
||
| 388 | |||
| 389 | if ( $sent ) { |
||
| 390 | $this->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) ); |
||
| 391 | } else { |
||
| 392 | $this->show_error( __( 'Could not sent the invoice to the customer', 'invoicing' ) ); |
||
| 393 | } |
||
| 394 | |||
| 395 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) ); |
||
| 396 | exit; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Sends a payment reminder to a customer. |
||
| 401 | * |
||
| 402 | * @param array $args |
||
| 403 | */ |
||
| 404 | public function send_customer_payment_reminder( $args ) { |
||
| 405 | $sent = getpaid()->get( 'invoice_emails' )->force_send_overdue_notice( new WPInv_Invoice( $args['invoice_id'] ) ); |
||
| 406 | |||
| 407 | if ( $sent ) { |
||
| 408 | $this->show_success( __( 'Payment reminder was successfully sent to the customer', 'invoicing' ) ); |
||
| 409 | } else { |
||
| 410 | $this->show_error( __( 'Could not sent payment reminder to the customer', 'invoicing' ) ); |
||
| 411 | } |
||
| 412 | |||
| 413 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) ); |
||
| 414 | exit; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Resets tax rates. |
||
| 419 | * |
||
| 420 | */ |
||
| 421 | public function admin_reset_tax_rates() { |
||
| 422 | |||
| 423 | update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) ); |
||
| 424 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 425 | exit; |
||
| 426 | |||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Resets admin pages. |
||
| 431 | * |
||
| 432 | */ |
||
| 433 | public function admin_create_missing_pages() { |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Creates an missing admin tables. |
||
| 443 | * |
||
| 444 | */ |
||
| 445 | public function admin_create_missing_tables() { |
||
| 446 | global $wpdb; |
||
| 447 | $installer = new GetPaid_Installer(); |
||
| 448 | |||
| 449 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}wpinv_subscriptions'" ) != $wpdb->prefix . 'wpinv_subscriptions' ) { |
||
| 450 | $installer->create_subscriptions_table(); |
||
| 451 | |||
| 452 | if ( $wpdb->last_error !== '' ) { |
||
| 453 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoices'" ) != $wpdb->prefix . 'getpaid_invoices' ) { |
||
| 458 | $installer->create_invoices_table(); |
||
| 459 | |||
| 460 | if ( $wpdb->last_error !== '' ) { |
||
| 461 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoice_items'" ) != $wpdb->prefix . 'getpaid_invoice_items' ) { |
||
| 466 | $installer->create_invoice_items_table(); |
||
| 467 | |||
| 468 | if ( $wpdb->last_error !== '' ) { |
||
| 469 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | if ( ! $this->has_notices() ) { |
||
| 474 | $this->show_success( __( 'Your GetPaid tables have been updated.', 'invoicing' ) ); |
||
| 475 | } |
||
| 476 | |||
| 477 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 478 | exit; |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Migrates old invoices to the new database tables. |
||
| 483 | * |
||
| 484 | */ |
||
| 485 | public function admin_migrate_old_invoices() { |
||
| 497 | |||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Recalculates discounts. |
||
| 502 | * |
||
| 503 | */ |
||
| 504 | public function admin_recalculate_discounts() { |
||
| 505 | global $wpdb; |
||
| 506 | |||
| 507 | // Fetch all invoices that have discount codes. |
||
| 508 | $table = $wpdb->prefix . 'getpaid_invoices'; |
||
| 509 | $invoices = $wpdb->get_col( "SELECT `post_id` FROM `$table` WHERE `discount` = 0 && `discount_code` <> ''" ); |
||
| 510 | |||
| 511 | foreach ( $invoices as $invoice ) { |
||
| 512 | |||
| 513 | $invoice = new WPInv_Invoice( $invoice ); |
||
| 514 | |||
| 515 | if ( ! $invoice->exists() ) { |
||
| 516 | continue; |
||
| 517 | } |
||
| 518 | |||
| 519 | // Abort if the discount does not exist or does not apply here. |
||
| 520 | $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
||
| 521 | if ( ! $discount->exists() ) { |
||
| 522 | continue; |
||
| 523 | } |
||
| 524 | |||
| 525 | $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
||
| 526 | $invoice->recalculate_total(); |
||
| 527 | |||
| 528 | if ( $invoice->get_total_discount() > 0 ) { |
||
| 529 | $invoice->save(); |
||
| 530 | } |
||
| 531 | |||
| 532 | } |
||
| 533 | |||
| 534 | // Show an admin message. |
||
| 535 | $this->show_success( __( 'Discounts have been recalculated.', 'invoicing' ) ); |
||
| 536 | |||
| 537 | // Redirect the admin. |
||
| 538 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 539 | exit; |
||
| 540 | |||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns an array of admin notices. |
||
| 545 | * |
||
| 546 | * @since 1.0.19 |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | public function get_notices() { |
||
| 550 | $notices = get_option( 'wpinv_admin_notices' ); |
||
| 551 | return is_array( $notices ) ? $notices : array(); |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Checks if we have any admin notices. |
||
| 556 | * |
||
| 557 | * @since 2.0.4 |
||
| 558 | * @return array |
||
| 559 | */ |
||
| 560 | public function has_notices() { |
||
| 561 | return count( $this->get_notices() ) > 0; |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Clears all admin notices |
||
| 566 | * |
||
| 567 | * @access public |
||
| 568 | * @since 1.0.19 |
||
| 569 | */ |
||
| 570 | public function clear_notices() { |
||
| 571 | delete_option( 'wpinv_admin_notices' ); |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Saves a new admin notice |
||
| 576 | * |
||
| 577 | * @access public |
||
| 578 | * @since 1.0.19 |
||
| 579 | */ |
||
| 580 | public function save_notice( $type, $message ) { |
||
| 581 | $notices = $this->get_notices(); |
||
| 582 | |||
| 583 | if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) { |
||
| 584 | $notices[ $type ] = array(); |
||
| 585 | } |
||
| 586 | |||
| 587 | $notices[ $type ][] = $message; |
||
| 588 | |||
| 589 | update_option( 'wpinv_admin_notices', $notices ); |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Displays a success notice |
||
| 594 | * |
||
| 595 | * @param string $msg The message to qeue. |
||
| 596 | * @access public |
||
| 597 | * @since 1.0.19 |
||
| 598 | */ |
||
| 599 | public function show_success( $msg ) { |
||
| 600 | $this->save_notice( 'success', $msg ); |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Displays a error notice |
||
| 605 | * |
||
| 606 | * @access public |
||
| 607 | * @param string $msg The message to qeue. |
||
| 608 | * @since 1.0.19 |
||
| 609 | */ |
||
| 610 | public function show_error( $msg ) { |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Displays a warning notice |
||
| 616 | * |
||
| 617 | * @access public |
||
| 618 | * @param string $msg The message to qeue. |
||
| 619 | * @since 1.0.19 |
||
| 620 | */ |
||
| 621 | public function show_warning( $msg ) { |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Displays a info notice |
||
| 627 | * |
||
| 628 | * @access public |
||
| 629 | * @param string $msg The message to qeue. |
||
| 630 | * @since 1.0.19 |
||
| 631 | */ |
||
| 632 | public function show_info( $msg ) { |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Show notices |
||
| 638 | * |
||
| 639 | * @access public |
||
| 640 | * @since 1.0.19 |
||
| 641 | */ |
||
| 642 | public function show_notices() { |
||
| 675 | } |
||
| 676 | |||
| 677 | } |
||
| 678 | |||
| 682 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.