@@ -12,276 +12,276 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Metaboxes { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Only save metaboxes once. |
|
| 17 | - * |
|
| 18 | - * @var boolean |
|
| 19 | - */ |
|
| 20 | - private static $saved_meta_boxes = false; |
|
| 21 | - |
|
| 22 | 15 | /** |
| 23 | - * Hook in methods. |
|
| 24 | - */ |
|
| 25 | - public static function init() { |
|
| 26 | - |
|
| 27 | - // Register metaboxes. |
|
| 28 | - add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::add_meta_boxes', 5, 2 ); |
|
| 29 | - |
|
| 30 | - // Remove metaboxes. |
|
| 31 | - add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::remove_meta_boxes', 30 ); |
|
| 32 | - |
|
| 33 | - // Rename metaboxes. |
|
| 34 | - add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::rename_meta_boxes', 45 ); |
|
| 35 | - |
|
| 36 | - // Save metaboxes. |
|
| 37 | - add_action( 'save_post', 'GetPaid_Metaboxes::save_meta_boxes', 1, 2 ); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Register core metaboxes. |
|
| 42 | - */ |
|
| 43 | - public static function add_meta_boxes( $post_type, $post ) { |
|
| 44 | - |
|
| 45 | - // For invoices... |
|
| 46 | - self::add_invoice_meta_boxes( $post_type, $post ); |
|
| 47 | - |
|
| 48 | - // For payment forms. |
|
| 49 | - self::add_payment_form_meta_boxes( $post_type, $post ); |
|
| 50 | - |
|
| 51 | - // For invoice items. |
|
| 52 | - self::add_item_meta_boxes( $post_type ); |
|
| 53 | - |
|
| 54 | - // For invoice discounts. |
|
| 55 | - if ( $post_type == 'wpi_discount' ) { |
|
| 56 | - add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' ); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - } |
|
| 16 | + * Only save metaboxes once. |
|
| 17 | + * |
|
| 18 | + * @var boolean |
|
| 19 | + */ |
|
| 20 | + private static $saved_meta_boxes = false; |
|
| 60 | 21 | |
| 61 | - /** |
|
| 62 | - * Register core metaboxes. |
|
| 63 | - */ |
|
| 64 | - protected static function add_payment_form_meta_boxes( $post_type, $post ) { |
|
| 22 | + /** |
|
| 23 | + * Hook in methods. |
|
| 24 | + */ |
|
| 25 | + public static function init() { |
|
| 65 | 26 | |
| 66 | - // For payment forms. |
|
| 67 | - if ( $post_type == 'wpi_payment_form' ) { |
|
| 27 | + // Register metaboxes. |
|
| 28 | + add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::add_meta_boxes', 5, 2 ); |
|
| 68 | 29 | |
| 69 | - // Design payment form. |
|
| 70 | - add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' ); |
|
| 30 | + // Remove metaboxes. |
|
| 31 | + add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::remove_meta_boxes', 30 ); |
|
| 71 | 32 | |
| 72 | - // Payment form information. |
|
| 73 | - if ( $post->ID != wpinv_get_default_payment_form() ) { |
|
| 74 | - add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' ); |
|
| 75 | - } |
|
| 33 | + // Rename metaboxes. |
|
| 34 | + add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::rename_meta_boxes', 45 ); |
|
| 76 | 35 | |
| 77 | - } |
|
| 36 | + // Save metaboxes. |
|
| 37 | + add_action( 'save_post', 'GetPaid_Metaboxes::save_meta_boxes', 1, 2 ); |
|
| 38 | + } |
|
| 78 | 39 | |
| 79 | - } |
|
| 40 | + /** |
|
| 41 | + * Register core metaboxes. |
|
| 42 | + */ |
|
| 43 | + public static function add_meta_boxes( $post_type, $post ) { |
|
| 80 | 44 | |
| 81 | - /** |
|
| 82 | - * Register core metaboxes. |
|
| 83 | - */ |
|
| 84 | - protected static function add_item_meta_boxes( $post_type ) { |
|
| 45 | + // For invoices... |
|
| 46 | + self::add_invoice_meta_boxes( $post_type, $post ); |
|
| 85 | 47 | |
| 86 | - if ( $post_type == 'wpi_item' ) { |
|
| 48 | + // For payment forms. |
|
| 49 | + self::add_payment_form_meta_boxes( $post_type, $post ); |
|
| 87 | 50 | |
| 88 | - // Item details. |
|
| 89 | - add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' ); |
|
| 51 | + // For invoice items. |
|
| 52 | + self::add_item_meta_boxes( $post_type ); |
|
| 90 | 53 | |
| 91 | - // If taxes are enabled, register the tax metabox. |
|
| 92 | - if ( wpinv_use_taxes() ) { |
|
| 93 | - add_meta_box( 'wpinv_item_vat', __( 'Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' ); |
|
| 94 | - } |
|
| 54 | + // For invoice discounts. |
|
| 55 | + if ( $post_type == 'wpi_discount' ) { |
|
| 56 | + add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' ); |
|
| 57 | + } |
|
| 95 | 58 | |
| 96 | - // Item info. |
|
| 97 | - add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' ); |
|
| 59 | + } |
|
| 98 | 60 | |
| 99 | - } |
|
| 61 | + /** |
|
| 62 | + * Register core metaboxes. |
|
| 63 | + */ |
|
| 64 | + protected static function add_payment_form_meta_boxes( $post_type, $post ) { |
|
| 100 | 65 | |
| 101 | - } |
|
| 66 | + // For payment forms. |
|
| 67 | + if ( $post_type == 'wpi_payment_form' ) { |
|
| 102 | 68 | |
| 103 | - /** |
|
| 104 | - * Register invoice metaboxes. |
|
| 105 | - */ |
|
| 106 | - protected static function add_invoice_meta_boxes( $post_type, $post ) { |
|
| 69 | + // Design payment form. |
|
| 70 | + add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' ); |
|
| 107 | 71 | |
| 108 | - // For invoices... |
|
| 109 | - if ( getpaid_is_invoice_post_type( $post_type ) ) { |
|
| 110 | - $invoice = new WPInv_Invoice( $post ); |
|
| 72 | + // Payment form information. |
|
| 73 | + if ( $post->ID != wpinv_get_default_payment_form() ) { |
|
| 74 | + add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' ); |
|
| 75 | + } |
|
| 111 | 76 | |
| 112 | - // Resend invoice. |
|
| 113 | - if ( ! $invoice->is_draft() ) { |
|
| 77 | + } |
|
| 114 | 78 | |
| 115 | - add_meta_box( |
|
| 116 | - 'wpinv-mb-resend-invoice', |
|
| 117 | - sprintf( |
|
| 118 | - __( 'Resend %s', 'invoicing' ), |
|
| 119 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 120 | - ), |
|
| 121 | - 'GetPaid_Meta_Box_Resend_Invoice::output', |
|
| 122 | - $post_type, |
|
| 123 | - 'side', |
|
| 124 | - 'low' |
|
| 125 | - ); |
|
| 79 | + } |
|
| 126 | 80 | |
| 127 | - } |
|
| 81 | + /** |
|
| 82 | + * Register core metaboxes. |
|
| 83 | + */ |
|
| 84 | + protected static function add_item_meta_boxes( $post_type ) { |
|
| 128 | 85 | |
| 129 | - // Subscriptions. |
|
| 130 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
| 131 | - if ( ! empty( $subscriptions ) ) { |
|
| 86 | + if ( $post_type == 'wpi_item' ) { |
|
| 132 | 87 | |
| 133 | - if ( is_array( $subscriptions ) ) { |
|
| 134 | - add_meta_box( 'wpinv-mb-subscriptions', __( 'Related Subscriptions', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_related', $post_type, 'advanced' ); |
|
| 135 | - } else { |
|
| 136 | - add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', $post_type, 'advanced' ); |
|
| 137 | - } |
|
| 88 | + // Item details. |
|
| 89 | + add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' ); |
|
| 138 | 90 | |
| 139 | - if ( getpaid_count_subscription_invoices( $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id() ) > 1 ) { |
|
| 140 | - add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', $post_type, 'advanced' ); |
|
| 141 | - } |
|
| 91 | + // If taxes are enabled, register the tax metabox. |
|
| 92 | + if ( wpinv_use_taxes() ) { |
|
| 93 | + add_meta_box( 'wpinv_item_vat', __( 'Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' ); |
|
| 94 | + } |
|
| 142 | 95 | |
| 143 | - } |
|
| 96 | + // Item info. |
|
| 97 | + add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' ); |
|
| 144 | 98 | |
| 145 | - // Invoice details. |
|
| 146 | - add_meta_box( |
|
| 147 | - 'wpinv-details', |
|
| 148 | - sprintf( |
|
| 149 | - __( '%s Details', 'invoicing' ), |
|
| 150 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 151 | - ), |
|
| 152 | - 'GetPaid_Meta_Box_Invoice_Details::output', |
|
| 153 | - $post_type, |
|
| 154 | - 'side' |
|
| 155 | - ); |
|
| 99 | + } |
|
| 156 | 100 | |
| 157 | - // Payment details. |
|
| 158 | - add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', $post_type, 'side', 'default' ); |
|
| 101 | + } |
|
| 159 | 102 | |
| 160 | - // Billing details. |
|
| 161 | - add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', $post_type, 'normal', 'high' ); |
|
| 103 | + /** |
|
| 104 | + * Register invoice metaboxes. |
|
| 105 | + */ |
|
| 106 | + protected static function add_invoice_meta_boxes( $post_type, $post ) { |
|
| 107 | + |
|
| 108 | + // For invoices... |
|
| 109 | + if ( getpaid_is_invoice_post_type( $post_type ) ) { |
|
| 110 | + $invoice = new WPInv_Invoice( $post ); |
|
| 111 | + |
|
| 112 | + // Resend invoice. |
|
| 113 | + if ( ! $invoice->is_draft() ) { |
|
| 114 | + |
|
| 115 | + add_meta_box( |
|
| 116 | + 'wpinv-mb-resend-invoice', |
|
| 117 | + sprintf( |
|
| 118 | + __( 'Resend %s', 'invoicing' ), |
|
| 119 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 120 | + ), |
|
| 121 | + 'GetPaid_Meta_Box_Resend_Invoice::output', |
|
| 122 | + $post_type, |
|
| 123 | + 'side', |
|
| 124 | + 'low' |
|
| 125 | + ); |
|
| 126 | + |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // Subscriptions. |
|
| 130 | + $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
| 131 | + if ( ! empty( $subscriptions ) ) { |
|
| 132 | + |
|
| 133 | + if ( is_array( $subscriptions ) ) { |
|
| 134 | + add_meta_box( 'wpinv-mb-subscriptions', __( 'Related Subscriptions', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_related', $post_type, 'advanced' ); |
|
| 135 | + } else { |
|
| 136 | + add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', $post_type, 'advanced' ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ( getpaid_count_subscription_invoices( $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id() ) > 1 ) { |
|
| 140 | + add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', $post_type, 'advanced' ); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // Invoice details. |
|
| 146 | + add_meta_box( |
|
| 147 | + 'wpinv-details', |
|
| 148 | + sprintf( |
|
| 149 | + __( '%s Details', 'invoicing' ), |
|
| 150 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 151 | + ), |
|
| 152 | + 'GetPaid_Meta_Box_Invoice_Details::output', |
|
| 153 | + $post_type, |
|
| 154 | + 'side' |
|
| 155 | + ); |
|
| 156 | + |
|
| 157 | + // Payment details. |
|
| 158 | + add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', $post_type, 'side', 'default' ); |
|
| 159 | + |
|
| 160 | + // Billing details. |
|
| 161 | + add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', $post_type, 'normal', 'high' ); |
|
| 162 | 162 | |
| 163 | - // Invoice items. |
|
| 164 | - add_meta_box( |
|
| 165 | - 'wpinv-items', |
|
| 166 | - sprintf( |
|
| 167 | - __( '%s Items', 'invoicing' ), |
|
| 168 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 169 | - ), |
|
| 170 | - 'GetPaid_Meta_Box_Invoice_Items::output', |
|
| 171 | - $post_type, |
|
| 172 | - 'normal', |
|
| 173 | - 'high' |
|
| 174 | - ); |
|
| 163 | + // Invoice items. |
|
| 164 | + add_meta_box( |
|
| 165 | + 'wpinv-items', |
|
| 166 | + sprintf( |
|
| 167 | + __( '%s Items', 'invoicing' ), |
|
| 168 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 169 | + ), |
|
| 170 | + 'GetPaid_Meta_Box_Invoice_Items::output', |
|
| 171 | + $post_type, |
|
| 172 | + 'normal', |
|
| 173 | + 'high' |
|
| 174 | + ); |
|
| 175 | 175 | |
| 176 | - // Invoice notes. |
|
| 177 | - add_meta_box( |
|
| 178 | - 'wpinv-notes', |
|
| 179 | - sprintf( |
|
| 180 | - __( '%s Notes', 'invoicing' ), |
|
| 181 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 182 | - ), |
|
| 183 | - 'WPInv_Meta_Box_Notes::output', |
|
| 184 | - $post_type, |
|
| 185 | - 'side', |
|
| 186 | - 'low' |
|
| 187 | - ); |
|
| 188 | - |
|
| 189 | - // Shipping Address. |
|
| 190 | - if ( get_post_meta( $invoice->get_id(), 'shipping_address', true ) ) { |
|
| 191 | - add_meta_box( 'wpinv-invoice-shipping-details', __( 'Shipping Address', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Shipping_Address::output', $post_type, 'side', 'high' ); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // Payment form information. |
|
| 195 | - if ( get_post_meta( $invoice->get_id(), 'payment_form_data', true ) ) { |
|
| 196 | - add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', $post_type, 'side', 'high' ); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Remove some metaboxes. |
|
| 205 | - */ |
|
| 206 | - public static function remove_meta_boxes() { |
|
| 207 | - remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' ); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Rename other metaboxes. |
|
| 212 | - */ |
|
| 213 | - public static function rename_meta_boxes() { |
|
| 176 | + // Invoice notes. |
|
| 177 | + add_meta_box( |
|
| 178 | + 'wpinv-notes', |
|
| 179 | + sprintf( |
|
| 180 | + __( '%s Notes', 'invoicing' ), |
|
| 181 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
| 182 | + ), |
|
| 183 | + 'WPInv_Meta_Box_Notes::output', |
|
| 184 | + $post_type, |
|
| 185 | + 'side', |
|
| 186 | + 'low' |
|
| 187 | + ); |
|
| 188 | + |
|
| 189 | + // Shipping Address. |
|
| 190 | + if ( get_post_meta( $invoice->get_id(), 'shipping_address', true ) ) { |
|
| 191 | + add_meta_box( 'wpinv-invoice-shipping-details', __( 'Shipping Address', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Shipping_Address::output', $post_type, 'side', 'high' ); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // Payment form information. |
|
| 195 | + if ( get_post_meta( $invoice->get_id(), 'payment_form_data', true ) ) { |
|
| 196 | + add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', $post_type, 'side', 'high' ); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Remove some metaboxes. |
|
| 205 | + */ |
|
| 206 | + public static function remove_meta_boxes() { |
|
| 207 | + remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' ); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Rename other metaboxes. |
|
| 212 | + */ |
|
| 213 | + public static function rename_meta_boxes() { |
|
| 214 | 214 | |
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Check if we're saving, then trigger an action based on the post type. |
|
| 219 | - * |
|
| 220 | - * @param int $post_id Post ID. |
|
| 221 | - * @param object $post Post object. |
|
| 222 | - */ |
|
| 223 | - public static function save_meta_boxes( $post_id, $post ) { |
|
| 224 | - $post_id = absint( $post_id ); |
|
| 225 | - $data = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
| 226 | - |
|
| 227 | - // Do not save for ajax requests. |
|
| 228 | - if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) { |
|
| 229 | - return; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - // $post_id and $post are required |
|
| 233 | - if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) { |
|
| 234 | - return; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - // Dont' save meta boxes for revisions or autosaves. |
|
| 238 | - if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { |
|
| 239 | - return; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // Check the nonce. |
|
| 243 | - if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) { |
|
| 244 | - return; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - // Check the post being saved == the $post_id to prevent triggering this call for other save_post events. |
|
| 248 | - if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) { |
|
| 249 | - return; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - // Check user has permission to edit. |
|
| 253 | - if ( ! current_user_can( 'edit_post', $post_id ) ) { |
|
| 254 | - return; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - if ( getpaid_is_invoice_post_type( $post->post_type ) ) { |
|
| 258 | - |
|
| 259 | - // We need this save event to run once to avoid potential endless loops. |
|
| 260 | - self::$saved_meta_boxes = true; |
|
| 261 | - |
|
| 262 | - return GetPaid_Meta_Box_Invoice_Address::save( $post_id ); |
|
| 263 | - |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - // Ensure this is our post type. |
|
| 267 | - $post_types_map = array( |
|
| 268 | - 'wpi_item' => 'GetPaid_Meta_Box_Item_Details', |
|
| 269 | - 'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form', |
|
| 270 | - 'wpi_discount' => 'GetPaid_Meta_Box_Discount_Details', |
|
| 271 | - ); |
|
| 272 | - |
|
| 273 | - // Is this our post type? |
|
| 274 | - if ( ! isset( $post_types_map[ $post->post_type ] ) ) { |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // We need this save event to run once to avoid potential endless loops. |
|
| 279 | - self::$saved_meta_boxes = true; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Check if we're saving, then trigger an action based on the post type. |
|
| 219 | + * |
|
| 220 | + * @param int $post_id Post ID. |
|
| 221 | + * @param object $post Post object. |
|
| 222 | + */ |
|
| 223 | + public static function save_meta_boxes( $post_id, $post ) { |
|
| 224 | + $post_id = absint( $post_id ); |
|
| 225 | + $data = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
| 226 | + |
|
| 227 | + // Do not save for ajax requests. |
|
| 228 | + if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) { |
|
| 229 | + return; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + // $post_id and $post are required |
|
| 233 | + if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) { |
|
| 234 | + return; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + // Dont' save meta boxes for revisions or autosaves. |
|
| 238 | + if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { |
|
| 239 | + return; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // Check the nonce. |
|
| 243 | + if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) { |
|
| 244 | + return; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + // Check the post being saved == the $post_id to prevent triggering this call for other save_post events. |
|
| 248 | + if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) { |
|
| 249 | + return; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + // Check user has permission to edit. |
|
| 253 | + if ( ! current_user_can( 'edit_post', $post_id ) ) { |
|
| 254 | + return; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + if ( getpaid_is_invoice_post_type( $post->post_type ) ) { |
|
| 258 | + |
|
| 259 | + // We need this save event to run once to avoid potential endless loops. |
|
| 260 | + self::$saved_meta_boxes = true; |
|
| 261 | + |
|
| 262 | + return GetPaid_Meta_Box_Invoice_Address::save( $post_id ); |
|
| 263 | + |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + // Ensure this is our post type. |
|
| 267 | + $post_types_map = array( |
|
| 268 | + 'wpi_item' => 'GetPaid_Meta_Box_Item_Details', |
|
| 269 | + 'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form', |
|
| 270 | + 'wpi_discount' => 'GetPaid_Meta_Box_Discount_Details', |
|
| 271 | + ); |
|
| 272 | + |
|
| 273 | + // Is this our post type? |
|
| 274 | + if ( ! isset( $post_types_map[ $post->post_type ] ) ) { |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // We need this save event to run once to avoid potential endless loops. |
|
| 279 | + self::$saved_meta_boxes = true; |
|
| 280 | 280 | |
| 281 | - // Save the post. |
|
| 282 | - $class = $post_types_map[ $post->post_type ]; |
|
| 283 | - $class::save( $post_id, $_POST, $post ); |
|
| 281 | + // Save the post. |
|
| 282 | + $class = $post_types_map[ $post->post_type ]; |
|
| 283 | + $class::save( $post_id, $_POST, $post ); |
|
| 284 | 284 | |
| 285 | - } |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | 287 | } |