| Total Complexity | 112 |
| Total Lines | 876 |
| 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_duplicate_form', array( $this, 'duplicate_payment_form' ) ); |
||
| 65 | add_action( 'getpaid_authenticated_admin_action_send_invoice', array( $this, 'send_customer_invoice' ) ); |
||
| 66 | add_action( 'getpaid_authenticated_admin_action_send_invoice_reminder', array( $this, 'send_customer_payment_reminder' ) ); |
||
| 67 | add_action( 'getpaid_authenticated_admin_action_reset_tax_rates', array( $this, 'admin_reset_tax_rates' ) ); |
||
| 68 | add_action( 'getpaid_authenticated_admin_action_create_missing_pages', array( $this, 'admin_create_missing_pages' ) ); |
||
| 69 | add_action( 'getpaid_authenticated_admin_action_create_missing_tables', array( $this, 'admin_create_missing_tables' ) ); |
||
| 70 | add_action( 'getpaid_authenticated_admin_action_migrate_old_invoices', array( $this, 'admin_migrate_old_invoices' ) ); |
||
| 71 | add_action( 'getpaid_authenticated_admin_action_download_customers', array( $this, 'admin_download_customers' ) ); |
||
| 72 | add_action( 'getpaid_authenticated_admin_action_recalculate_discounts', array( $this, 'admin_recalculate_discounts' ) ); |
||
| 73 | add_action( 'getpaid_authenticated_admin_action_install_plugin', array( $this, 'admin_install_plugin' ) ); |
||
| 74 | add_action( 'getpaid_authenticated_admin_action_connect_gateway', array( $this, 'admin_connect_gateway' ) ); |
||
| 75 | add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) ); |
||
| 76 | do_action( 'getpaid_init_admin_hooks', $this ); |
||
| 77 | |||
| 78 | // Setup/welcome |
||
| 79 | if ( ! empty( $_GET['page'] ) ) { |
||
| 80 | switch ( $_GET['page'] ) { |
||
| 81 | case 'gp-setup' : |
||
| 82 | include_once( dirname( __FILE__ ) . '/class-getpaid-admin-setup-wizard.php' ); |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Register admin scripts |
||
| 91 | * |
||
| 92 | */ |
||
| 93 | public function enqeue_scripts() { |
||
| 94 | global $current_screen, $pagenow; |
||
| 95 | |||
| 96 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
| 97 | $editing = $pagenow == 'post.php' || $pagenow == 'post-new.php'; |
||
| 98 | |||
| 99 | if ( ! empty( $current_screen->post_type ) ) { |
||
| 100 | $page = $current_screen->post_type; |
||
| 101 | } |
||
| 102 | |||
| 103 | // General styles. |
||
| 104 | if ( false !== stripos( $page, 'wpi' ) || 'gp-setup' == $page ) { |
||
| 105 | |||
| 106 | // Styles. |
||
| 107 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' ); |
||
| 108 | wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version ); |
||
| 109 | wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' ); |
||
| 110 | |||
| 111 | // Scripts. |
||
| 112 | wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION ); |
||
| 113 | |||
| 114 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' ); |
||
| 115 | wp_enqueue_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'wp-color-picker', 'jquery-ui-tooltip' ), $version ); |
||
| 116 | wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) ); |
||
| 117 | |||
| 118 | } |
||
| 119 | |||
| 120 | // Payment form scripts. |
||
| 121 | if ( 'wpi_payment_form' == $page && $editing ) { |
||
| 122 | $this->load_payment_form_scripts(); |
||
| 123 | } |
||
| 124 | |||
| 125 | if ( $page == 'wpinv-subscriptions' ) { |
||
| 126 | wp_enqueue_script( 'postbox' ); |
||
| 127 | } |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns admin js translations. |
||
| 133 | * |
||
| 134 | */ |
||
| 135 | protected function get_admin_i18() { |
||
| 136 | global $post; |
||
| 137 | |||
| 138 | $date_range = array( |
||
| 139 | 'period' => isset( $_GET['date_range'] ) ? sanitize_text_field( $_GET['date_range'] ) : '7_days' |
||
| 140 | ); |
||
| 141 | |||
| 142 | if ( $date_range['period'] == 'custom' ) { |
||
| 143 | |||
| 144 | if ( isset( $_GET['from'] ) ) { |
||
| 145 | $date_range[ 'after' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['from'] ), current_time( 'timestamp' ) ) - DAY_IN_SECONDS ); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ( isset( $_GET['to'] ) ) { |
||
| 149 | $date_range[ 'before' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['to'] ), current_time( 'timestamp' ) ) + DAY_IN_SECONDS ); |
||
| 150 | } |
||
| 151 | |||
| 152 | } |
||
| 153 | |||
| 154 | $i18n = array( |
||
| 155 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
| 156 | 'post_ID' => isset( $post->ID ) ? $post->ID : '', |
||
| 157 | 'wpinv_nonce' => wp_create_nonce( 'wpinv-nonce' ), |
||
| 158 | 'rest_nonce' => wp_create_nonce( 'wp_rest' ), |
||
| 159 | 'rest_root' => esc_url_raw( rest_url() ), |
||
| 160 | 'date_range' => $date_range, |
||
| 161 | 'add_invoice_note_nonce' => wp_create_nonce( 'add-invoice-note' ), |
||
| 162 | 'delete_invoice_note_nonce' => wp_create_nonce( 'delete-invoice-note' ), |
||
| 163 | 'invoice_item_nonce' => wp_create_nonce( 'invoice-item' ), |
||
| 164 | 'billing_details_nonce' => wp_create_nonce( 'get-billing-details' ), |
||
| 165 | 'tax' => wpinv_tax_amount(), |
||
| 166 | 'discount' => 0, |
||
| 167 | 'currency_symbol' => wpinv_currency_symbol(), |
||
| 168 | 'currency' => wpinv_get_currency(), |
||
| 169 | 'currency_pos' => wpinv_currency_position(), |
||
| 170 | 'thousand_sep' => wpinv_thousands_separator(), |
||
| 171 | 'decimal_sep' => wpinv_decimal_separator(), |
||
| 172 | 'decimals' => wpinv_decimals(), |
||
| 173 | 'save_invoice' => __( 'Save Invoice', 'invoicing' ), |
||
| 174 | 'status_publish' => wpinv_status_nicename( 'publish' ), |
||
| 175 | 'status_pending' => wpinv_status_nicename( 'wpi-pending' ), |
||
| 176 | 'delete_tax_rate' => __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ), |
||
| 177 | 'status_pending' => wpinv_status_nicename( 'wpi-pending' ), |
||
| 178 | 'FillBillingDetails' => __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ), |
||
| 179 | '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' ), |
||
| 180 | 'AreYouSure' => __( 'Are you sure?', 'invoicing' ), |
||
| 181 | 'errDeleteItem' => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ), |
||
| 182 | 'delete_subscription' => __( 'Are you sure you want to delete this subscription?', 'invoicing' ), |
||
| 183 | 'action_edit' => __( 'Edit', 'invoicing' ), |
||
| 184 | 'action_cancel' => __( 'Cancel', 'invoicing' ), |
||
| 185 | 'item_description' => __( 'Item Description', 'invoicing' ), |
||
| 186 | 'invoice_description' => __( 'Invoice Description', 'invoicing' ), |
||
| 187 | 'discount_description' => __( 'Discount Description', 'invoicing' ), |
||
| 188 | 'searching' => __( 'Searching', 'invoicing' ), |
||
| 189 | 'loading' => __( 'Loading...', 'invoicing' ), |
||
| 190 | 'search_customers' => __( 'Enter customer name or email', 'invoicing' ), |
||
| 191 | 'search_items' => __( 'Enter item name', 'invoicing' ), |
||
| 192 | ); |
||
| 193 | |||
| 194 | if ( ! empty( $post ) && getpaid_is_invoice_post_type( $post->post_type ) ) { |
||
| 195 | |||
| 196 | $invoice = new WPInv_Invoice( $post ); |
||
| 197 | $i18n['save_invoice'] = sprintf( |
||
| 198 | __( 'Save %s', 'invoicing' ), |
||
| 199 | ucfirst( $invoice->get_invoice_quote_type() ) |
||
| 200 | ); |
||
| 201 | |||
| 202 | $i18n['invoice_description'] = sprintf( |
||
| 203 | __( '%s Description', 'invoicing' ), |
||
| 204 | ucfirst( $invoice->get_invoice_quote_type() ) |
||
| 205 | ); |
||
| 206 | |||
| 207 | } |
||
| 208 | return $i18n; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Change the admin footer text on GetPaid admin pages. |
||
| 213 | * |
||
| 214 | * @since 2.0.0 |
||
| 215 | * @param string $footer_text |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function admin_footer_text( $footer_text ) { |
||
| 219 | global $current_screen; |
||
| 220 | |||
| 221 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
| 222 | |||
| 223 | if ( ! empty( $current_screen->post_type ) ) { |
||
| 224 | $page = $current_screen->post_type; |
||
| 225 | } |
||
| 226 | |||
| 227 | // General styles. |
||
| 228 | if ( apply_filters( 'getpaid_display_admin_footer_text', wpinv_current_user_can_manage_invoicing() ) && false !== stripos( $page, 'wpi' ) ) { |
||
| 229 | |||
| 230 | // Change the footer text |
||
| 231 | if ( ! get_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', true ) ) { |
||
| 232 | |||
| 233 | $rating_url = esc_url( |
||
| 234 | wp_nonce_url( |
||
| 235 | admin_url( 'admin.php?page=wpinv-reports&getpaid-admin-action=rate_plugin' ), |
||
| 236 | 'getpaid-nonce', |
||
| 237 | 'getpaid-nonce' |
||
| 238 | ) |
||
| 239 | ); |
||
| 240 | |||
| 241 | $footer_text = sprintf( |
||
| 242 | /* translators: %s: five stars */ |
||
| 243 | __( 'If you like <strong>GetPaid</strong>, please leave us a %s rating. A huge thanks in advance!', 'invoicing' ), |
||
| 244 | "<a href='$rating_url'>★★★★★</a>" |
||
| 245 | ); |
||
| 246 | |||
| 247 | } else { |
||
| 248 | |||
| 249 | $footer_text = sprintf( |
||
| 250 | /* translators: %s: GetPaid */ |
||
| 251 | __( 'Thank you for using %s!', 'invoicing' ), |
||
| 252 | "<a href='https://wpgetpaid.com/' target='_blank'><strong>GetPaid</strong></a>" |
||
| 253 | ); |
||
| 254 | |||
| 255 | } |
||
| 256 | |||
| 257 | } |
||
| 258 | |||
| 259 | return $footer_text; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Redirects to wp.org to rate the plugin. |
||
| 264 | * |
||
| 265 | * @since 2.0.0 |
||
| 266 | */ |
||
| 267 | public function redirect_to_wordpress_rating_page() { |
||
| 268 | update_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', 1 ); |
||
| 269 | wp_redirect( 'https://wordpress.org/support/plugin/invoicing/reviews?rate=5#new-post' ); |
||
| 270 | exit; |
||
|
|
|||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Loads payment form js. |
||
| 275 | * |
||
| 276 | */ |
||
| 277 | protected function load_payment_form_scripts() { |
||
| 278 | global $post; |
||
| 279 | |||
| 280 | wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.min.js', array(), WPINV_VERSION ); |
||
| 281 | wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION ); |
||
| 282 | wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION ); |
||
| 283 | |||
| 284 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' ); |
||
| 285 | wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ), $version ); |
||
| 286 | |||
| 287 | wp_localize_script( |
||
| 288 | 'wpinv-admin-payment-form-script', |
||
| 289 | 'wpinvPaymentFormAdmin', |
||
| 290 | array( |
||
| 291 | 'elements' => wpinv_get_data( 'payment-form-elements' ), |
||
| 292 | 'form_elements' => getpaid_get_payment_form_elements( $post->ID ), |
||
| 293 | 'currency' => wpinv_currency_symbol(), |
||
| 294 | 'position' => wpinv_currency_position(), |
||
| 295 | 'decimals' => (int) wpinv_decimals(), |
||
| 296 | 'thousands_sep' => wpinv_thousands_separator(), |
||
| 297 | 'decimals_sep' => wpinv_decimal_separator(), |
||
| 298 | 'form_items' => gepaid_get_form_items( $post->ID ), |
||
| 299 | 'is_default' => $post->ID == wpinv_get_default_payment_form(), |
||
| 300 | ) |
||
| 301 | ); |
||
| 302 | |||
| 303 | wp_enqueue_script( 'wpinv-admin-payment-form-script' ); |
||
| 304 | |||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Add our classes to admin pages. |
||
| 309 | * |
||
| 310 | * @param string $classes |
||
| 311 | * @return string |
||
| 312 | * |
||
| 313 | */ |
||
| 314 | public function admin_body_class( $classes ) { |
||
| 315 | global $pagenow, $post, $current_screen; |
||
| 316 | |||
| 317 | |||
| 318 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
| 319 | |||
| 320 | if ( ! empty( $current_screen->post_type ) ) { |
||
| 321 | $page = $current_screen->post_type; |
||
| 322 | } |
||
| 323 | |||
| 324 | if ( false !== stripos( $page, 'wpi' ) ) { |
||
| 325 | $classes .= ' wpi-' . sanitize_key( $page ); |
||
| 326 | } |
||
| 327 | |||
| 328 | if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) { |
||
| 329 | $classes .= ' wpinv-cpt wpinv'; |
||
| 330 | } |
||
| 331 | |||
| 332 | if ( getpaid_is_invoice_post_type( $page ) ) { |
||
| 333 | $classes .= ' getpaid-is-invoice-cpt'; |
||
| 334 | } |
||
| 335 | |||
| 336 | return $classes; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Maybe show the AyeCode Connect Notice. |
||
| 341 | */ |
||
| 342 | public function init_ayecode_connect_helper(){ |
||
| 343 | |||
| 344 | // Register with the deactivation survey class. |
||
| 345 | AyeCode_Deactivation_Survey::instance(array( |
||
| 346 | 'slug' => 'invoicing', |
||
| 347 | 'version' => WPINV_VERSION, |
||
| 348 | 'support_url' => 'https://wpgetpaid.com/support/', |
||
| 349 | 'documentation_url' => 'https://docs.wpgetpaid.com/', |
||
| 350 | 'activated' => (int) get_option( 'gepaid_installed_on' ), |
||
| 351 | )); |
||
| 352 | |||
| 353 | new AyeCode_Connect_Helper( |
||
| 354 | array( |
||
| 355 | 'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"), |
||
| 356 | 'connect_external' => __( "Please confirm you wish to connect your site?","invoicing" ), |
||
| 357 | '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>" ), |
||
| 358 | 'connect_button' => __("Connect Site","invoicing"), |
||
| 359 | 'connecting_button' => __("Connecting...","invoicing"), |
||
| 360 | 'error_localhost' => __( "This service will only work with a live domain, not a localhost.","invoicing" ), |
||
| 361 | 'error' => __( "Something went wrong, please refresh and try again.","invoicing" ), |
||
| 362 | ), |
||
| 363 | array( 'wpi-addons' ) |
||
| 364 | ); |
||
| 365 | |||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Redirect users to settings on activation. |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function activation_redirect() { |
||
| 374 | |||
| 375 | $redirected = get_option( 'wpinv_redirected_to_settings' ); |
||
| 376 | |||
| 377 | if ( ! empty( $redirected ) || wp_doing_ajax() || ! current_user_can( 'manage_options' ) ) { |
||
| 378 | return; |
||
| 379 | } |
||
| 380 | |||
| 381 | // Bail if activating from network, or bulk |
||
| 382 | if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { |
||
| 383 | return; |
||
| 384 | } |
||
| 385 | |||
| 386 | update_option( 'wpinv_redirected_to_settings', 1 ); |
||
| 387 | |||
| 388 | wp_safe_redirect( admin_url( 'index.php?page=gp-setup' ) ); |
||
| 389 | exit; |
||
| 390 | |||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Fires an admin action after verifying that a user can fire them. |
||
| 395 | */ |
||
| 396 | public function maybe_do_admin_action() { |
||
| 397 | |||
| 398 | if ( wpinv_current_user_can_manage_invoicing() && isset( $_REQUEST['getpaid-admin-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) { |
||
| 399 | $key = sanitize_key( $_REQUEST['getpaid-admin-action'] ); |
||
| 400 | do_action( "getpaid_authenticated_admin_action_$key", $_REQUEST ); |
||
| 401 | } |
||
| 402 | |||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Sends a payment reminder to a customer. |
||
| 407 | * |
||
| 408 | * @param array $args |
||
| 409 | */ |
||
| 410 | public function duplicate_payment_form( $args ) { |
||
| 411 | |||
| 412 | if ( empty( $args['form_id'] ) ) { |
||
| 413 | return; |
||
| 414 | } |
||
| 415 | |||
| 416 | $form = new GetPaid_Payment_Form( $args['form_id'] ); |
||
| 417 | |||
| 418 | if ( ! $form->exists() ) { |
||
| 419 | return; |
||
| 420 | } |
||
| 421 | |||
| 422 | $new_form = new GetPaid_Payment_Form(); |
||
| 423 | $new_form->set_author( $form->get_author( 'edit' ) ); |
||
| 424 | $new_form->set_name( $form->get_name( 'edit' ) . __( '(copy)', 'invoicing' ) ); |
||
| 425 | $new_form->set_elements( $form->get_elements( 'edit' ) ); |
||
| 426 | $new_form->set_items( $form->get_items( 'edit' ) ); |
||
| 427 | $new_form->save(); |
||
| 428 | |||
| 429 | if ( $new_form->exists() ) { |
||
| 430 | $this->show_success( __( 'Form duplicated successfully', 'invoicing' ) ); |
||
| 431 | $url = get_edit_post_link( $new_form->get_id(), 'edit' ); |
||
| 432 | } else { |
||
| 433 | $this->show_error( __( 'Unable to duplicate form', 'invoicing' ) ); |
||
| 434 | $url = remove_query_arg( array( 'getpaid-admin-action', 'form_id', 'getpaid-nonce' ) ); |
||
| 435 | } |
||
| 436 | |||
| 437 | wp_redirect( $url ); |
||
| 438 | exit; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Sends a payment reminder to a customer. |
||
| 443 | * |
||
| 444 | * @param array $args |
||
| 445 | */ |
||
| 446 | public function send_customer_invoice( $args ) { |
||
| 447 | $sent = getpaid()->get( 'invoice_emails' )->user_invoice( new WPInv_Invoice( $args['invoice_id'] ), true ); |
||
| 448 | |||
| 449 | if ( $sent ) { |
||
| 450 | $this->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) ); |
||
| 451 | } else { |
||
| 452 | $this->show_error( __( 'Could not send the invoice to the customer', 'invoicing' ) ); |
||
| 453 | } |
||
| 454 | |||
| 455 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) ); |
||
| 456 | exit; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Sends a payment reminder to a customer. |
||
| 461 | * |
||
| 462 | * @param array $args |
||
| 463 | */ |
||
| 464 | public function send_customer_payment_reminder( $args ) { |
||
| 465 | $sent = getpaid()->get( 'invoice_emails' )->force_send_overdue_notice( new WPInv_Invoice( $args['invoice_id'] ) ); |
||
| 466 | |||
| 467 | if ( $sent ) { |
||
| 468 | $this->show_success( __( 'Payment reminder was successfully sent to the customer', 'invoicing' ) ); |
||
| 469 | } else { |
||
| 470 | $this->show_error( __( 'Could not sent payment reminder to the customer', 'invoicing' ) ); |
||
| 471 | } |
||
| 472 | |||
| 473 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) ); |
||
| 474 | exit; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Resets tax rates. |
||
| 479 | * |
||
| 480 | */ |
||
| 481 | public function admin_reset_tax_rates() { |
||
| 482 | |||
| 483 | update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) ); |
||
| 484 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 485 | exit; |
||
| 486 | |||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Resets admin pages. |
||
| 491 | * |
||
| 492 | */ |
||
| 493 | public function admin_create_missing_pages() { |
||
| 494 | $installer = new GetPaid_Installer(); |
||
| 495 | $installer->create_pages(); |
||
| 496 | $this->show_success( __( 'GetPaid pages updated.', 'invoicing' ) ); |
||
| 497 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 498 | exit; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Creates an missing admin tables. |
||
| 503 | * |
||
| 504 | */ |
||
| 505 | public function admin_create_missing_tables() { |
||
| 506 | global $wpdb; |
||
| 507 | $installer = new GetPaid_Installer(); |
||
| 508 | |||
| 509 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}wpinv_subscriptions'" ) != $wpdb->prefix . 'wpinv_subscriptions' ) { |
||
| 510 | $installer->create_subscriptions_table(); |
||
| 511 | |||
| 512 | if ( $wpdb->last_error !== '' ) { |
||
| 513 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | |||
| 517 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoices'" ) != $wpdb->prefix . 'getpaid_invoices' ) { |
||
| 518 | $installer->create_invoices_table(); |
||
| 519 | |||
| 520 | if ( $wpdb->last_error !== '' ) { |
||
| 521 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoice_items'" ) != $wpdb->prefix . 'getpaid_invoice_items' ) { |
||
| 526 | $installer->create_invoice_items_table(); |
||
| 527 | |||
| 528 | if ( $wpdb->last_error !== '' ) { |
||
| 529 | $this->show_error( __( 'Your GetPaid tables have been updated:', 'invoicing' ) . ' ' . $wpdb->last_error ); |
||
| 530 | } |
||
| 531 | } |
||
| 532 | |||
| 533 | if ( ! $this->has_notices() ) { |
||
| 534 | $this->show_success( __( 'Your GetPaid tables have been updated.', 'invoicing' ) ); |
||
| 535 | } |
||
| 536 | |||
| 537 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 538 | exit; |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Migrates old invoices to the new database tables. |
||
| 543 | * |
||
| 544 | */ |
||
| 545 | public function admin_migrate_old_invoices() { |
||
| 546 | |||
| 547 | // Migrate the invoices. |
||
| 548 | $installer = new GetPaid_Installer(); |
||
| 549 | $installer->migrate_old_invoices(); |
||
| 550 | |||
| 551 | // Show an admin message. |
||
| 552 | $this->show_success( __( 'Your invoices have been migrated.', 'invoicing' ) ); |
||
| 553 | |||
| 554 | // Redirect the admin. |
||
| 555 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 556 | exit; |
||
| 557 | |||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Download customers. |
||
| 562 | * |
||
| 563 | */ |
||
| 564 | public function admin_download_customers() { |
||
| 565 | global $wpdb; |
||
| 566 | |||
| 567 | $output = fopen( 'php://output', 'w' ) or die( __( 'Unsupported server', 'invoicing' ) ); |
||
| 568 | |||
| 569 | header( "Content-Type:text/csv" ); |
||
| 570 | header( "Content-Disposition:attachment;filename=customers.csv" ); |
||
| 571 | |||
| 572 | $post_types = ''; |
||
| 573 | |||
| 574 | foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) { |
||
| 575 | $post_types .= $wpdb->prepare( "post_type=%s OR ", $post_type ); |
||
| 576 | } |
||
| 577 | |||
| 578 | $post_types = rtrim( $post_types, ' OR' ); |
||
| 579 | |||
| 580 | $customers = $wpdb->get_col( |
||
| 581 | $wpdb->prepare( |
||
| 582 | "SELECT DISTINCT( post_author ) FROM $wpdb->posts WHERE $post_types" |
||
| 583 | ) |
||
| 584 | ); |
||
| 585 | |||
| 586 | $columns = array( |
||
| 587 | 'name' => __( 'Name', 'invoicing' ), |
||
| 588 | 'email' => __( 'Email', 'invoicing' ), |
||
| 589 | 'country' => __( 'Country', 'invoicing' ), |
||
| 590 | 'state' => __( 'State', 'invoicing' ), |
||
| 591 | 'city' => __( 'City', 'invoicing' ), |
||
| 592 | 'zip' => __( 'ZIP', 'invoicing' ), |
||
| 593 | 'address' => __( 'Address', 'invoicing' ), |
||
| 594 | 'phone' => __( 'Phone', 'invoicing' ), |
||
| 595 | 'company' => __( 'Company', 'invoicing' ), |
||
| 596 | 'company_id' => __( 'Company ID', 'invoicing' ), |
||
| 597 | 'invoices' => __( 'Invoices', 'invoicing' ), |
||
| 598 | 'total_raw' => __( 'Total Spend', 'invoicing' ), |
||
| 599 | 'signup' => __( 'Date created', 'invoicing' ), |
||
| 600 | ); |
||
| 601 | |||
| 602 | // Output the csv column headers. |
||
| 603 | fputcsv( $output, array_values( $columns ) ); |
||
| 604 | |||
| 605 | // Loop through |
||
| 606 | $table = new WPInv_Customers_Table(); |
||
| 607 | foreach ( $customers as $customer_id ) { |
||
| 608 | |||
| 609 | $user = get_user_by( 'id', $customer_id ); |
||
| 610 | $row = array(); |
||
| 611 | if ( empty( $user ) ) { |
||
| 612 | continue; |
||
| 613 | } |
||
| 614 | |||
| 615 | foreach ( array_keys( $columns ) as $column ) { |
||
| 616 | |||
| 617 | $method = 'column_' . $column; |
||
| 618 | |||
| 619 | if ( 'name' == $column ) { |
||
| 620 | $value = sanitize_text_field( $user->display_name ); |
||
| 621 | } else if( 'email' == $column ) { |
||
| 622 | $value = sanitize_email( $user->user_email ); |
||
| 623 | } else if ( is_callable( array( $table, $method ) ) ) { |
||
| 624 | $value = strip_tags( $table->$method( $user ) ); |
||
| 625 | } |
||
| 626 | |||
| 627 | if ( empty( $value ) ) { |
||
| 628 | $value = sanitize_text_field( get_user_meta( $user->ID, '_wpinv_' . $column, true ) ); |
||
| 629 | } |
||
| 630 | |||
| 631 | $row[] = $value; |
||
| 632 | |||
| 633 | } |
||
| 634 | |||
| 635 | fputcsv( $output, $row ); |
||
| 636 | } |
||
| 637 | |||
| 638 | fclose( $output ); |
||
| 639 | exit; |
||
| 640 | |||
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Installs a plugin. |
||
| 645 | * |
||
| 646 | * @param array $data |
||
| 647 | */ |
||
| 648 | public function admin_install_plugin( $data ) { |
||
| 649 | |||
| 650 | if ( ! empty( $data['plugins'] ) ) { |
||
| 651 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 652 | wp_cache_flush(); |
||
| 653 | |||
| 654 | foreach ( $data['plugins'] as $slug => $file ) { |
||
| 655 | $plugin_zip = esc_url( 'https://downloads.wordpress.org/plugin/' . $slug . '.latest-stable.zip' ); |
||
| 656 | $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); |
||
| 657 | $installed = $upgrader->install( $plugin_zip ); |
||
| 658 | |||
| 659 | if ( ! is_wp_error( $installed ) && $installed ) { |
||
| 660 | activate_plugin( $file, '', false, true ); |
||
| 661 | } else { |
||
| 662 | wpinv_error_log( $upgrader->skin->get_upgrade_messages(), false ); |
||
| 663 | } |
||
| 664 | |||
| 665 | } |
||
| 666 | |||
| 667 | } |
||
| 668 | |||
| 669 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( $data['redirect'] ) : admin_url( 'plugins.php' ); |
||
| 670 | wp_safe_redirect( $redirect ); |
||
| 671 | exit; |
||
| 672 | |||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Connects a gateway. |
||
| 677 | * |
||
| 678 | * @param array $data |
||
| 679 | */ |
||
| 680 | public function admin_connect_gateway( $data ) { |
||
| 681 | |||
| 682 | if ( ! empty( $data['plugin'] ) ) { |
||
| 683 | |||
| 684 | $gateway = sanitize_key( $data['plugin'] ); |
||
| 685 | $connect_url = apply_filters( "getpaid_get_{$gateway}_connect_url", false, $data ); |
||
| 686 | |||
| 687 | if ( ! empty( $connect_url ) ) { |
||
| 688 | wp_redirect( $connect_url ); |
||
| 689 | exit; |
||
| 690 | } |
||
| 691 | |||
| 692 | if ( 'stripe' == $data['plugin'] ) { |
||
| 693 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
| 694 | |||
| 695 | if ( ! array_key_exists( 'getpaid-stripe-payments/getpaid-stripe-payments.php', get_plugins() ) ) { |
||
| 696 | $plugin_zip = esc_url( 'https://downloads.wordpress.org/plugin/getpaid-stripe-payments.latest-stable.zip' ); |
||
| 697 | $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); |
||
| 698 | $upgrader->install( $plugin_zip ); |
||
| 699 | } |
||
| 700 | |||
| 701 | activate_plugin( 'getpaid-stripe-payments/getpaid-stripe-payments.php', '', false, true ); |
||
| 702 | } |
||
| 703 | |||
| 704 | if ( ! empty( $connect_url ) ) { |
||
| 705 | wp_redirect( $connect_url ); |
||
| 706 | exit; |
||
| 707 | } |
||
| 708 | |||
| 709 | } |
||
| 710 | |||
| 711 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( urldecode( $data['redirect'] ) ) : admin_url( 'admin.php?page=wpinv-settings&tab=gateways' ); |
||
| 712 | wp_safe_redirect( $redirect ); |
||
| 713 | exit; |
||
| 714 | |||
| 715 | } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Recalculates discounts. |
||
| 719 | * |
||
| 720 | */ |
||
| 721 | public function admin_recalculate_discounts() { |
||
| 722 | global $wpdb; |
||
| 723 | |||
| 724 | // Fetch all invoices that have discount codes. |
||
| 725 | $table = $wpdb->prefix . 'getpaid_invoices'; |
||
| 726 | $invoices = $wpdb->get_col( "SELECT `post_id` FROM `$table` WHERE `discount` = 0 && `discount_code` <> ''" ); |
||
| 727 | |||
| 728 | foreach ( $invoices as $invoice ) { |
||
| 729 | |||
| 730 | $invoice = new WPInv_Invoice( $invoice ); |
||
| 731 | |||
| 732 | if ( ! $invoice->exists() ) { |
||
| 733 | continue; |
||
| 734 | } |
||
| 735 | |||
| 736 | // Abort if the discount does not exist or does not apply here. |
||
| 737 | $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
||
| 738 | if ( ! $discount->exists() ) { |
||
| 739 | continue; |
||
| 740 | } |
||
| 741 | |||
| 742 | $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
||
| 743 | $invoice->recalculate_total(); |
||
| 744 | |||
| 745 | if ( $invoice->get_total_discount() > 0 ) { |
||
| 746 | $invoice->save(); |
||
| 747 | } |
||
| 748 | |||
| 749 | } |
||
| 750 | |||
| 751 | // Show an admin message. |
||
| 752 | $this->show_success( __( 'Discounts have been recalculated.', 'invoicing' ) ); |
||
| 753 | |||
| 754 | // Redirect the admin. |
||
| 755 | wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) ); |
||
| 756 | exit; |
||
| 757 | |||
| 758 | } |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Returns an array of admin notices. |
||
| 762 | * |
||
| 763 | * @since 1.0.19 |
||
| 764 | * @return array |
||
| 765 | */ |
||
| 766 | public function get_notices() { |
||
| 767 | $notices = get_option( 'wpinv_admin_notices' ); |
||
| 768 | return is_array( $notices ) ? $notices : array(); |
||
| 769 | } |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Checks if we have any admin notices. |
||
| 773 | * |
||
| 774 | * @since 2.0.4 |
||
| 775 | * @return array |
||
| 776 | */ |
||
| 777 | public function has_notices() { |
||
| 778 | return count( $this->get_notices() ) > 0; |
||
| 779 | } |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Clears all admin notices |
||
| 783 | * |
||
| 784 | * @access public |
||
| 785 | * @since 1.0.19 |
||
| 786 | */ |
||
| 787 | public function clear_notices() { |
||
| 788 | delete_option( 'wpinv_admin_notices' ); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Saves a new admin notice |
||
| 793 | * |
||
| 794 | * @access public |
||
| 795 | * @since 1.0.19 |
||
| 796 | */ |
||
| 797 | public function save_notice( $type, $message ) { |
||
| 798 | $notices = $this->get_notices(); |
||
| 799 | |||
| 800 | if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) { |
||
| 801 | $notices[ $type ] = array(); |
||
| 802 | } |
||
| 803 | |||
| 804 | $notices[ $type ][] = $message; |
||
| 805 | |||
| 806 | update_option( 'wpinv_admin_notices', $notices ); |
||
| 807 | } |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Displays a success notice |
||
| 811 | * |
||
| 812 | * @param string $msg The message to qeue. |
||
| 813 | * @access public |
||
| 814 | * @since 1.0.19 |
||
| 815 | */ |
||
| 816 | public function show_success( $msg ) { |
||
| 817 | $this->save_notice( 'success', $msg ); |
||
| 818 | } |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Displays a error notice |
||
| 822 | * |
||
| 823 | * @access public |
||
| 824 | * @param string $msg The message to qeue. |
||
| 825 | * @since 1.0.19 |
||
| 826 | */ |
||
| 827 | public function show_error( $msg ) { |
||
| 829 | } |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Displays a warning notice |
||
| 833 | * |
||
| 834 | * @access public |
||
| 835 | * @param string $msg The message to qeue. |
||
| 836 | * @since 1.0.19 |
||
| 837 | */ |
||
| 838 | public function show_warning( $msg ) { |
||
| 840 | } |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Displays a info notice |
||
| 844 | * |
||
| 845 | * @access public |
||
| 846 | * @param string $msg The message to qeue. |
||
| 847 | * @since 1.0.19 |
||
| 848 | */ |
||
| 849 | public function show_info( $msg ) { |
||
| 851 | } |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Show notices |
||
| 855 | * |
||
| 856 | * @access public |
||
| 857 | * @since 1.0.19 |
||
| 858 | */ |
||
| 859 | public function show_notices() { |
||
| 860 | |||
| 861 | $notices = $this->get_notices(); |
||
| 862 | $this->clear_notices(); |
||
| 890 | } |
||
| 891 | |||
| 892 | } |
||
| 893 | |||
| 894 | } |
||
| 895 | |||
| 896 | } |
||
| 897 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.