@@ -13,449 +13,449 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class GetPaid_Payment_Gateway { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set if the place checkout button should be renamed on selection. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - public $checkout_button_text; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Boolean whether the method is enabled. |
|
| 25 | - * |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - public $enabled = true; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Payment method id. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - public $id; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Payment method order. |
|
| 39 | - * |
|
| 40 | - * @var int |
|
| 41 | - */ |
|
| 42 | - public $order = 10; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Payment method title for the frontend. |
|
| 46 | - * |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $title; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Payment method description for the frontend. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - public $description; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Gateway title. |
|
| 60 | - * |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - public $method_title = ''; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Gateway description. |
|
| 67 | - * |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - public $method_description = ''; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Countries this gateway is allowed for. |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - public $countries; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Currencies this gateway is allowed for. |
|
| 81 | - * |
|
| 82 | - * @var array |
|
| 83 | - */ |
|
| 84 | - public $currencies; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Currencies this gateway is not allowed for. |
|
| 88 | - * |
|
| 89 | - * @var array |
|
| 90 | - */ |
|
| 91 | - public $exclude_currencies; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | - * |
|
| 96 | - * @var int |
|
| 97 | - */ |
|
| 98 | - public $max_amount = 0; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Optional URL to view a transaction. |
|
| 102 | - * |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $view_transaction_url = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Optional URL to view a subscription. |
|
| 109 | - * |
|
| 110 | - * @var string |
|
| 111 | - */ |
|
| 112 | - public $view_subscription_url = ''; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Optional label to show for "new payment method" in the payment |
|
| 116 | - * method/token selection radio selection. |
|
| 117 | - * |
|
| 118 | - * @var string |
|
| 119 | - */ |
|
| 120 | - public $new_method_label = ''; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Contains a user's saved tokens for this gateway. |
|
| 124 | - * |
|
| 125 | - * @var array |
|
| 126 | - */ |
|
| 127 | - protected $tokens = array(); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * An array of features that this gateway supports. |
|
| 131 | - * |
|
| 132 | - * @var array |
|
| 133 | - */ |
|
| 134 | - protected $supports = array(); |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Class constructor. |
|
| 138 | - */ |
|
| 139 | - public function __construct() { |
|
| 140 | - $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 141 | - |
|
| 142 | - // Register gateway. |
|
| 143 | - add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 144 | - |
|
| 145 | - // Enable Subscriptions. |
|
| 146 | - if ( $this->supports( 'subscription' ) ) { |
|
| 147 | - add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // Enable sandbox. |
|
| 151 | - if ( $this->supports( 'sandbox' ) ) { |
|
| 152 | - add_filter( "wpinv_{$this->id}_supports_sandbox", '__return_true' ); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // Gateway settings. |
|
| 156 | - add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 16 | + /** |
|
| 17 | + * Set if the place checkout button should be renamed on selection. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + public $checkout_button_text; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Boolean whether the method is enabled. |
|
| 25 | + * |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + public $enabled = true; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Payment method id. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + public $id; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Payment method order. |
|
| 39 | + * |
|
| 40 | + * @var int |
|
| 41 | + */ |
|
| 42 | + public $order = 10; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Payment method title for the frontend. |
|
| 46 | + * |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $title; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Payment method description for the frontend. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + public $description; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Gateway title. |
|
| 60 | + * |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + public $method_title = ''; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Gateway description. |
|
| 67 | + * |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + public $method_description = ''; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Countries this gateway is allowed for. |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + public $countries; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Currencies this gateway is allowed for. |
|
| 81 | + * |
|
| 82 | + * @var array |
|
| 83 | + */ |
|
| 84 | + public $currencies; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Currencies this gateway is not allowed for. |
|
| 88 | + * |
|
| 89 | + * @var array |
|
| 90 | + */ |
|
| 91 | + public $exclude_currencies; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | + * |
|
| 96 | + * @var int |
|
| 97 | + */ |
|
| 98 | + public $max_amount = 0; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Optional URL to view a transaction. |
|
| 102 | + * |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $view_transaction_url = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Optional URL to view a subscription. |
|
| 109 | + * |
|
| 110 | + * @var string |
|
| 111 | + */ |
|
| 112 | + public $view_subscription_url = ''; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Optional label to show for "new payment method" in the payment |
|
| 116 | + * method/token selection radio selection. |
|
| 117 | + * |
|
| 118 | + * @var string |
|
| 119 | + */ |
|
| 120 | + public $new_method_label = ''; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Contains a user's saved tokens for this gateway. |
|
| 124 | + * |
|
| 125 | + * @var array |
|
| 126 | + */ |
|
| 127 | + protected $tokens = array(); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * An array of features that this gateway supports. |
|
| 131 | + * |
|
| 132 | + * @var array |
|
| 133 | + */ |
|
| 134 | + protected $supports = array(); |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Class constructor. |
|
| 138 | + */ |
|
| 139 | + public function __construct() { |
|
| 140 | + $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 141 | + |
|
| 142 | + // Register gateway. |
|
| 143 | + add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 144 | + |
|
| 145 | + // Enable Subscriptions. |
|
| 146 | + if ( $this->supports( 'subscription' ) ) { |
|
| 147 | + add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // Enable sandbox. |
|
| 151 | + if ( $this->supports( 'sandbox' ) ) { |
|
| 152 | + add_filter( "wpinv_{$this->id}_supports_sandbox", '__return_true' ); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // Gateway settings. |
|
| 156 | + add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 157 | 157 | |
| 158 | 158 | |
| 159 | - // Gateway checkout fiellds. |
|
| 160 | - add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 161 | - |
|
| 162 | - // Process payment. |
|
| 163 | - add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 159 | + // Gateway checkout fiellds. |
|
| 160 | + add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 161 | + |
|
| 162 | + // Process payment. |
|
| 163 | + add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 164 | + |
|
| 165 | + // Change the checkout button text. |
|
| 166 | + if ( ! empty( $this->checkout_button_text ) ) { |
|
| 167 | + add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + // Check if a gateway is valid for a given currency. |
|
| 171 | + add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 172 | + |
|
| 173 | + // Generate the transaction url. |
|
| 174 | + add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 175 | + |
|
| 176 | + // Generate the subscription url. |
|
| 177 | + add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 ); |
|
| 178 | + |
|
| 179 | + // Confirm payments. |
|
| 180 | + add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 181 | + |
|
| 182 | + // Verify IPNs. |
|
| 183 | + add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 184 | + |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Checks if this gateway is a given gateway. |
|
| 189 | + * |
|
| 190 | + * @since 1.0.19 |
|
| 191 | + * @return bool |
|
| 192 | + */ |
|
| 193 | + public function is( $gateway ) { |
|
| 194 | + return $gateway == $this->id; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Returns a users saved tokens for this gateway. |
|
| 199 | + * |
|
| 200 | + * @since 1.0.19 |
|
| 201 | + * @return array |
|
| 202 | + */ |
|
| 203 | + public function get_tokens( $sandbox = null ) { |
|
| 204 | + |
|
| 205 | + if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 206 | + $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 207 | + |
|
| 208 | + if ( is_array( $tokens ) ) { |
|
| 209 | + $this->tokens = $tokens; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + if ( ! is_bool( $sandbox ) ) { |
|
| 215 | + return $this->tokens; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 219 | + return wp_list_filter( $this->tokens, $args ); |
|
| 220 | + |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Saves a token for this gateway. |
|
| 225 | + * |
|
| 226 | + * @since 1.0.19 |
|
| 227 | + */ |
|
| 228 | + public function save_token( $token ) { |
|
| 164 | 229 | |
| 165 | - // Change the checkout button text. |
|
| 166 | - if ( ! empty( $this->checkout_button_text ) ) { |
|
| 167 | - add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - // Check if a gateway is valid for a given currency. |
|
| 171 | - add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 172 | - |
|
| 173 | - // Generate the transaction url. |
|
| 174 | - add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 175 | - |
|
| 176 | - // Generate the subscription url. |
|
| 177 | - add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 ); |
|
| 178 | - |
|
| 179 | - // Confirm payments. |
|
| 180 | - add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 181 | - |
|
| 182 | - // Verify IPNs. |
|
| 183 | - add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 184 | - |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Checks if this gateway is a given gateway. |
|
| 189 | - * |
|
| 190 | - * @since 1.0.19 |
|
| 191 | - * @return bool |
|
| 192 | - */ |
|
| 193 | - public function is( $gateway ) { |
|
| 194 | - return $gateway == $this->id; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Returns a users saved tokens for this gateway. |
|
| 199 | - * |
|
| 200 | - * @since 1.0.19 |
|
| 201 | - * @return array |
|
| 202 | - */ |
|
| 203 | - public function get_tokens( $sandbox = null ) { |
|
| 204 | - |
|
| 205 | - if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 206 | - $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 207 | - |
|
| 208 | - if ( is_array( $tokens ) ) { |
|
| 209 | - $this->tokens = $tokens; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - if ( ! is_bool( $sandbox ) ) { |
|
| 215 | - return $this->tokens; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 219 | - return wp_list_filter( $this->tokens, $args ); |
|
| 220 | - |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Saves a token for this gateway. |
|
| 225 | - * |
|
| 226 | - * @since 1.0.19 |
|
| 227 | - */ |
|
| 228 | - public function save_token( $token ) { |
|
| 229 | - |
|
| 230 | - $tokens = $this->get_tokens(); |
|
| 231 | - $tokens[] = $token; |
|
| 232 | - |
|
| 233 | - update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 234 | - |
|
| 235 | - $this->tokens = $tokens; |
|
| 236 | - |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Return the title for admin screens. |
|
| 241 | - * |
|
| 242 | - * @return string |
|
| 243 | - */ |
|
| 244 | - public function get_method_title() { |
|
| 245 | - return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Return the description for admin screens. |
|
| 250 | - * |
|
| 251 | - * @return string |
|
| 252 | - */ |
|
| 253 | - public function get_method_description() { |
|
| 254 | - return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Get the success url. |
|
| 259 | - * |
|
| 260 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 261 | - * @return string |
|
| 262 | - */ |
|
| 263 | - public function get_return_url( $invoice ) { |
|
| 264 | - |
|
| 265 | - // Payment success url |
|
| 266 | - $return_url = add_query_arg( |
|
| 267 | - array( |
|
| 268 | - 'payment-confirm' => $this->id, |
|
| 269 | - 'invoice_key' => $invoice->get_key(), |
|
| 270 | - 'utm_nooverride' => 1 |
|
| 271 | - ), |
|
| 272 | - wpinv_get_success_page_uri() |
|
| 273 | - ); |
|
| 274 | - |
|
| 275 | - return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Confirms payments when rendering the success page. |
|
| 280 | - * |
|
| 281 | - * @param string $content Success page content. |
|
| 282 | - * @return string |
|
| 283 | - */ |
|
| 284 | - public function get_confirm_payment( $content ) { |
|
| 230 | + $tokens = $this->get_tokens(); |
|
| 231 | + $tokens[] = $token; |
|
| 232 | + |
|
| 233 | + update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 234 | + |
|
| 235 | + $this->tokens = $tokens; |
|
| 236 | + |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Return the title for admin screens. |
|
| 241 | + * |
|
| 242 | + * @return string |
|
| 243 | + */ |
|
| 244 | + public function get_method_title() { |
|
| 245 | + return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Return the description for admin screens. |
|
| 250 | + * |
|
| 251 | + * @return string |
|
| 252 | + */ |
|
| 253 | + public function get_method_description() { |
|
| 254 | + return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Get the success url. |
|
| 259 | + * |
|
| 260 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 261 | + * @return string |
|
| 262 | + */ |
|
| 263 | + public function get_return_url( $invoice ) { |
|
| 264 | + |
|
| 265 | + // Payment success url |
|
| 266 | + $return_url = add_query_arg( |
|
| 267 | + array( |
|
| 268 | + 'payment-confirm' => $this->id, |
|
| 269 | + 'invoice_key' => $invoice->get_key(), |
|
| 270 | + 'utm_nooverride' => 1 |
|
| 271 | + ), |
|
| 272 | + wpinv_get_success_page_uri() |
|
| 273 | + ); |
|
| 274 | + |
|
| 275 | + return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Confirms payments when rendering the success page. |
|
| 280 | + * |
|
| 281 | + * @param string $content Success page content. |
|
| 282 | + * @return string |
|
| 283 | + */ |
|
| 284 | + public function get_confirm_payment( $content ) { |
|
| 285 | 285 | |
| 286 | - // Retrieve the invoice. |
|
| 287 | - $invoice_id = getpaid_get_current_invoice_id(); |
|
| 288 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 286 | + // Retrieve the invoice. |
|
| 287 | + $invoice_id = getpaid_get_current_invoice_id(); |
|
| 288 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 289 | 289 | |
| 290 | - // Ensure that it exists and that it is pending payment. |
|
| 291 | - if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 292 | - return $content; |
|
| 293 | - } |
|
| 290 | + // Ensure that it exists and that it is pending payment. |
|
| 291 | + if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 292 | + return $content; |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - // Can the user view this invoice?? |
|
| 296 | - if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 297 | - return $content; |
|
| 298 | - } |
|
| 295 | + // Can the user view this invoice?? |
|
| 296 | + if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 297 | + return $content; |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - // Show payment processing indicator. |
|
| 301 | - return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Processes ipns and marks payments as complete. |
|
| 306 | - * |
|
| 307 | - * @return void |
|
| 308 | - */ |
|
| 309 | - public function verify_ipn() {} |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 313 | - * |
|
| 314 | - * @param string $transaction_url transaction url. |
|
| 315 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 316 | - * @return string transaction URL, or empty string. |
|
| 317 | - */ |
|
| 318 | - public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 319 | - |
|
| 320 | - $transaction_id = $invoice->get_transaction_id(); |
|
| 321 | - |
|
| 322 | - if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 323 | - $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 324 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 325 | - $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return $transaction_url; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 333 | - * |
|
| 334 | - * @param string $subscription_url transaction url. |
|
| 335 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 336 | - * @return string subscription URL, or empty string. |
|
| 337 | - */ |
|
| 338 | - public function filter_subscription_url( $subscription_url, $invoice ) { |
|
| 339 | - |
|
| 340 | - $profile_id = $invoice->get_subscription_id(); |
|
| 341 | - |
|
| 342 | - if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 343 | - |
|
| 344 | - $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 345 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 346 | - $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 347 | - |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - return $subscription_url; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Check if the gateway is available for use. |
|
| 355 | - * |
|
| 356 | - * @return bool |
|
| 357 | - */ |
|
| 358 | - public function is_available() { |
|
| 359 | - return ! empty( $this->enabled ); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Return the gateway's title. |
|
| 364 | - * |
|
| 365 | - * @return string |
|
| 366 | - */ |
|
| 367 | - public function get_title() { |
|
| 368 | - return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Return the gateway's description. |
|
| 373 | - * |
|
| 374 | - * @return string |
|
| 375 | - */ |
|
| 376 | - public function get_description() { |
|
| 377 | - return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Process Payment. |
|
| 382 | - * |
|
| 383 | - * |
|
| 384 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 385 | - * @param array $submission_data Posted checkout fields. |
|
| 386 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 387 | - * @return void |
|
| 388 | - */ |
|
| 389 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 390 | - // Process the payment then either redirect to the success page or the gateway. |
|
| 391 | - do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * Process refund. |
|
| 396 | - * |
|
| 397 | - * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 398 | - * a passed in amount. |
|
| 399 | - * |
|
| 400 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 401 | - * @param float $amount Refund amount. |
|
| 402 | - * @param string $reason Refund reason. |
|
| 403 | - * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 404 | - */ |
|
| 405 | - public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 406 | - return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Displays the payment fields, credit cards etc. |
|
| 411 | - * |
|
| 412 | - * @param int $invoice_id 0 or invoice id. |
|
| 413 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
| 414 | - */ |
|
| 415 | - public function payment_fields( $invoice_id, $form ) { |
|
| 416 | - do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Filters the gateway settings. |
|
| 421 | - * |
|
| 422 | - * @param array $admin_settings |
|
| 423 | - */ |
|
| 424 | - public function admin_settings( $admin_settings ) { |
|
| 425 | - return $admin_settings; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Retrieves the value of a gateway setting. |
|
| 430 | - * |
|
| 431 | - * @param string $option |
|
| 432 | - */ |
|
| 433 | - public function get_option( $option, $default = false ) { |
|
| 434 | - return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Check if a gateway supports a given feature. |
|
| 439 | - * |
|
| 440 | - * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 441 | - * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 442 | - * |
|
| 443 | - * @param string $feature string The name of a feature to test support for. |
|
| 444 | - * @return bool True if the gateway supports the feature, false otherwise. |
|
| 445 | - * @since 1.0.19 |
|
| 446 | - */ |
|
| 447 | - public function supports( $feature ) { |
|
| 448 | - return apply_filters( 'getpaid_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this ); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Returns the credit card form html. |
|
| 453 | - * |
|
| 454 | - * @param bool $save whether or not to display the save button. |
|
| 455 | - */ |
|
| 300 | + // Show payment processing indicator. |
|
| 301 | + return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Processes ipns and marks payments as complete. |
|
| 306 | + * |
|
| 307 | + * @return void |
|
| 308 | + */ |
|
| 309 | + public function verify_ipn() {} |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 313 | + * |
|
| 314 | + * @param string $transaction_url transaction url. |
|
| 315 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 316 | + * @return string transaction URL, or empty string. |
|
| 317 | + */ |
|
| 318 | + public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 319 | + |
|
| 320 | + $transaction_id = $invoice->get_transaction_id(); |
|
| 321 | + |
|
| 322 | + if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 323 | + $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 324 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 325 | + $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return $transaction_url; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 333 | + * |
|
| 334 | + * @param string $subscription_url transaction url. |
|
| 335 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 336 | + * @return string subscription URL, or empty string. |
|
| 337 | + */ |
|
| 338 | + public function filter_subscription_url( $subscription_url, $invoice ) { |
|
| 339 | + |
|
| 340 | + $profile_id = $invoice->get_subscription_id(); |
|
| 341 | + |
|
| 342 | + if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 343 | + |
|
| 344 | + $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 345 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 346 | + $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 347 | + |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + return $subscription_url; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Check if the gateway is available for use. |
|
| 355 | + * |
|
| 356 | + * @return bool |
|
| 357 | + */ |
|
| 358 | + public function is_available() { |
|
| 359 | + return ! empty( $this->enabled ); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Return the gateway's title. |
|
| 364 | + * |
|
| 365 | + * @return string |
|
| 366 | + */ |
|
| 367 | + public function get_title() { |
|
| 368 | + return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Return the gateway's description. |
|
| 373 | + * |
|
| 374 | + * @return string |
|
| 375 | + */ |
|
| 376 | + public function get_description() { |
|
| 377 | + return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Process Payment. |
|
| 382 | + * |
|
| 383 | + * |
|
| 384 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 385 | + * @param array $submission_data Posted checkout fields. |
|
| 386 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 387 | + * @return void |
|
| 388 | + */ |
|
| 389 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 390 | + // Process the payment then either redirect to the success page or the gateway. |
|
| 391 | + do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * Process refund. |
|
| 396 | + * |
|
| 397 | + * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 398 | + * a passed in amount. |
|
| 399 | + * |
|
| 400 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 401 | + * @param float $amount Refund amount. |
|
| 402 | + * @param string $reason Refund reason. |
|
| 403 | + * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 404 | + */ |
|
| 405 | + public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 406 | + return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Displays the payment fields, credit cards etc. |
|
| 411 | + * |
|
| 412 | + * @param int $invoice_id 0 or invoice id. |
|
| 413 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
| 414 | + */ |
|
| 415 | + public function payment_fields( $invoice_id, $form ) { |
|
| 416 | + do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Filters the gateway settings. |
|
| 421 | + * |
|
| 422 | + * @param array $admin_settings |
|
| 423 | + */ |
|
| 424 | + public function admin_settings( $admin_settings ) { |
|
| 425 | + return $admin_settings; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Retrieves the value of a gateway setting. |
|
| 430 | + * |
|
| 431 | + * @param string $option |
|
| 432 | + */ |
|
| 433 | + public function get_option( $option, $default = false ) { |
|
| 434 | + return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Check if a gateway supports a given feature. |
|
| 439 | + * |
|
| 440 | + * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 441 | + * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 442 | + * |
|
| 443 | + * @param string $feature string The name of a feature to test support for. |
|
| 444 | + * @return bool True if the gateway supports the feature, false otherwise. |
|
| 445 | + * @since 1.0.19 |
|
| 446 | + */ |
|
| 447 | + public function supports( $feature ) { |
|
| 448 | + return apply_filters( 'getpaid_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this ); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Returns the credit card form html. |
|
| 453 | + * |
|
| 454 | + * @param bool $save whether or not to display the save button. |
|
| 455 | + */ |
|
| 456 | 456 | public function get_cc_form( $save = false ) { |
| 457 | 457 | |
| 458 | - ob_start(); |
|
| 458 | + ob_start(); |
|
| 459 | 459 | |
| 460 | 460 | $id_prefix = esc_attr( uniqid( $this->id ) ); |
| 461 | 461 | |
@@ -561,175 +561,175 @@ discard block |
||
| 561 | 561 | |
| 562 | 562 | <?php |
| 563 | 563 | |
| 564 | - if ( $save ) { |
|
| 565 | - echo $this->save_payment_method_checkbox(); |
|
| 566 | - } |
|
| 564 | + if ( $save ) { |
|
| 565 | + echo $this->save_payment_method_checkbox(); |
|
| 566 | + } |
|
| 567 | 567 | |
| 568 | - ?> |
|
| 568 | + ?> |
|
| 569 | 569 | </div> |
| 570 | 570 | |
| 571 | 571 | </div> |
| 572 | 572 | <?php |
| 573 | 573 | |
| 574 | - return ob_get_clean(); |
|
| 574 | + return ob_get_clean(); |
|
| 575 | + |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Displays a new payment method entry form. |
|
| 580 | + * |
|
| 581 | + * @since 1.0.19 |
|
| 582 | + */ |
|
| 583 | + public function new_payment_method_entry( $form ) { |
|
| 584 | + echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Grab and display our saved payment methods. |
|
| 589 | + * |
|
| 590 | + * @since 1.0.19 |
|
| 591 | + */ |
|
| 592 | + public function saved_payment_methods() { |
|
| 593 | + $html = '<ul class="getpaid-saved-payment-methods m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 594 | + |
|
| 595 | + foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 596 | + $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 597 | + } |
|
| 575 | 598 | |
| 599 | + $html .= $this->get_new_payment_method_option_html(); |
|
| 600 | + $html .= '</ul>'; |
|
| 601 | + |
|
| 602 | + echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 576 | 603 | } |
| 577 | 604 | |
| 578 | - /** |
|
| 579 | - * Displays a new payment method entry form. |
|
| 580 | - * |
|
| 581 | - * @since 1.0.19 |
|
| 582 | - */ |
|
| 583 | - public function new_payment_method_entry( $form ) { |
|
| 584 | - echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Grab and display our saved payment methods. |
|
| 589 | - * |
|
| 590 | - * @since 1.0.19 |
|
| 591 | - */ |
|
| 592 | - public function saved_payment_methods() { |
|
| 593 | - $html = '<ul class="getpaid-saved-payment-methods m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 594 | - |
|
| 595 | - foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 596 | - $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - $html .= $this->get_new_payment_method_option_html(); |
|
| 600 | - $html .= '</ul>'; |
|
| 601 | - |
|
| 602 | - echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * Gets saved payment method HTML from a token. |
|
| 607 | - * |
|
| 608 | - * @since 1.0.19 |
|
| 609 | - * @param array $token Payment Token. |
|
| 610 | - * @return string Generated payment method HTML |
|
| 611 | - */ |
|
| 612 | - public function get_saved_payment_method_option_html( $token ) { |
|
| 613 | - |
|
| 614 | - return sprintf( |
|
| 615 | - '<li class="getpaid-payment-method form-group"> |
|
| 605 | + /** |
|
| 606 | + * Gets saved payment method HTML from a token. |
|
| 607 | + * |
|
| 608 | + * @since 1.0.19 |
|
| 609 | + * @param array $token Payment Token. |
|
| 610 | + * @return string Generated payment method HTML |
|
| 611 | + */ |
|
| 612 | + public function get_saved_payment_method_option_html( $token ) { |
|
| 613 | + |
|
| 614 | + return sprintf( |
|
| 615 | + '<li class="getpaid-payment-method form-group"> |
|
| 616 | 616 | <label> |
| 617 | 617 | <input name="getpaid-%1$s-payment-method" type="radio" value="%2$s" style="width:auto;" class="getpaid-saved-payment-method-token-input" %4$s /> |
| 618 | 618 | <span>%3$s</span> |
| 619 | 619 | </label> |
| 620 | 620 | </li>', |
| 621 | - esc_attr( $this->id ), |
|
| 622 | - esc_attr( $token['id'] ), |
|
| 623 | - esc_html( $token['name'] ), |
|
| 624 | - checked( empty( $token['default'] ), false, false ) |
|
| 625 | - ); |
|
| 621 | + esc_attr( $this->id ), |
|
| 622 | + esc_attr( $token['id'] ), |
|
| 623 | + esc_html( $token['name'] ), |
|
| 624 | + checked( empty( $token['default'] ), false, false ) |
|
| 625 | + ); |
|
| 626 | 626 | |
| 627 | - } |
|
| 627 | + } |
|
| 628 | 628 | |
| 629 | - /** |
|
| 630 | - * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 631 | - * |
|
| 632 | - * @since 1.0.19 |
|
| 633 | - */ |
|
| 634 | - public function get_new_payment_method_option_html() { |
|
| 629 | + /** |
|
| 630 | + * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 631 | + * |
|
| 632 | + * @since 1.0.19 |
|
| 633 | + */ |
|
| 634 | + public function get_new_payment_method_option_html() { |
|
| 635 | 635 | |
| 636 | - $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 636 | + $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 637 | 637 | |
| 638 | - return sprintf( |
|
| 639 | - '<li class="getpaid-new-payment-method"> |
|
| 638 | + return sprintf( |
|
| 639 | + '<li class="getpaid-new-payment-method"> |
|
| 640 | 640 | <label> |
| 641 | 641 | <input name="getpaid-%1$s-payment-method" type="radio" value="new" style="width:auto;" /> |
| 642 | 642 | <span>%2$s</span> |
| 643 | 643 | </label> |
| 644 | 644 | </li>', |
| 645 | - esc_attr( $this->id ), |
|
| 646 | - esc_html( $label ) |
|
| 647 | - ); |
|
| 645 | + esc_attr( $this->id ), |
|
| 646 | + esc_html( $label ) |
|
| 647 | + ); |
|
| 648 | 648 | |
| 649 | - } |
|
| 649 | + } |
|
| 650 | 650 | |
| 651 | - /** |
|
| 652 | - * Outputs a checkbox for saving a new payment method to the database. |
|
| 653 | - * |
|
| 654 | - * @since 1.0.19 |
|
| 655 | - */ |
|
| 656 | - public function save_payment_method_checkbox() { |
|
| 651 | + /** |
|
| 652 | + * Outputs a checkbox for saving a new payment method to the database. |
|
| 653 | + * |
|
| 654 | + * @since 1.0.19 |
|
| 655 | + */ |
|
| 656 | + public function save_payment_method_checkbox() { |
|
| 657 | 657 | |
| 658 | - return sprintf( |
|
| 659 | - '<p class="form-group getpaid-save-payment-method"> |
|
| 658 | + return sprintf( |
|
| 659 | + '<p class="form-group getpaid-save-payment-method"> |
|
| 660 | 660 | <label> |
| 661 | 661 | <input name="getpaid-%1$s-new-payment-method" type="checkbox" value="true" style="width:auto;" /> |
| 662 | 662 | <span>%2$s</span> |
| 663 | 663 | </label> |
| 664 | 664 | </p>', |
| 665 | - esc_attr( $this->id ), |
|
| 666 | - esc_html__( 'Save payment method', 'invoicing' ) |
|
| 667 | - ); |
|
| 665 | + esc_attr( $this->id ), |
|
| 666 | + esc_html__( 'Save payment method', 'invoicing' ) |
|
| 667 | + ); |
|
| 668 | 668 | |
| 669 | - } |
|
| 669 | + } |
|
| 670 | 670 | |
| 671 | - /** |
|
| 672 | - * Registers the gateway. |
|
| 673 | - * |
|
| 674 | - * @return array |
|
| 675 | - */ |
|
| 676 | - public function register_gateway( $gateways ) { |
|
| 671 | + /** |
|
| 672 | + * Registers the gateway. |
|
| 673 | + * |
|
| 674 | + * @return array |
|
| 675 | + */ |
|
| 676 | + public function register_gateway( $gateways ) { |
|
| 677 | 677 | |
| 678 | - $gateways[ $this->id ] = array( |
|
| 678 | + $gateways[ $this->id ] = array( |
|
| 679 | 679 | |
| 680 | - 'admin_label' => $this->method_title, |
|
| 680 | + 'admin_label' => $this->method_title, |
|
| 681 | 681 | 'checkout_label' => $this->title, |
| 682 | - 'ordering' => $this->order, |
|
| 683 | - |
|
| 684 | - ); |
|
| 685 | - |
|
| 686 | - return $gateways; |
|
| 687 | - |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Checks whether or not this is a sandbox request. |
|
| 692 | - * |
|
| 693 | - * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 694 | - * @return bool |
|
| 695 | - */ |
|
| 696 | - public function is_sandbox( $invoice = null ) { |
|
| 697 | - |
|
| 698 | - if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 699 | - return $invoice->get_mode() == 'test'; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - return wpinv_is_test_mode( $this->id ); |
|
| 703 | - |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * Renames the checkout button |
|
| 708 | - * |
|
| 709 | - * @return string |
|
| 710 | - */ |
|
| 711 | - public function rename_checkout_button() { |
|
| 712 | - return $this->checkout_button_text; |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - /** |
|
| 716 | - * Validate gateway currency |
|
| 717 | - * |
|
| 718 | - * @return bool |
|
| 719 | - */ |
|
| 720 | - public function validate_currency( $validation, $currency ) { |
|
| 721 | - |
|
| 722 | - // Required currencies. |
|
| 723 | - if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 724 | - return false; |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - // Excluded currencies. |
|
| 728 | - if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 729 | - return false; |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - return $validation; |
|
| 733 | - } |
|
| 682 | + 'ordering' => $this->order, |
|
| 683 | + |
|
| 684 | + ); |
|
| 685 | + |
|
| 686 | + return $gateways; |
|
| 687 | + |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Checks whether or not this is a sandbox request. |
|
| 692 | + * |
|
| 693 | + * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 694 | + * @return bool |
|
| 695 | + */ |
|
| 696 | + public function is_sandbox( $invoice = null ) { |
|
| 697 | + |
|
| 698 | + if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 699 | + return $invoice->get_mode() == 'test'; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + return wpinv_is_test_mode( $this->id ); |
|
| 703 | + |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * Renames the checkout button |
|
| 708 | + * |
|
| 709 | + * @return string |
|
| 710 | + */ |
|
| 711 | + public function rename_checkout_button() { |
|
| 712 | + return $this->checkout_button_text; |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + /** |
|
| 716 | + * Validate gateway currency |
|
| 717 | + * |
|
| 718 | + * @return bool |
|
| 719 | + */ |
|
| 720 | + public function validate_currency( $validation, $currency ) { |
|
| 721 | + |
|
| 722 | + // Required currencies. |
|
| 723 | + if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 724 | + return false; |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + // Excluded currencies. |
|
| 728 | + if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 729 | + return false; |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + return $validation; |
|
| 733 | + } |
|
| 734 | 734 | |
| 735 | 735 | } |
@@ -12,8 +12,8 @@ discard block |
||
| 12 | 12 | class WPInv_Subscriptions { |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | - * Class constructor. |
|
| 16 | - */ |
|
| 15 | + * Class constructor. |
|
| 16 | + */ |
|
| 17 | 17 | public function __construct(){ |
| 18 | 18 | |
| 19 | 19 | // Fire gateway specific hooks when a subscription changes. |
@@ -33,12 +33,12 @@ discard block |
||
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | - * Processes subscription status changes. |
|
| 36 | + * Processes subscription status changes. |
|
| 37 | 37 | * |
| 38 | 38 | * @param WPInv_Subscription $subscription |
| 39 | 39 | * @param string $from |
| 40 | 40 | * @param string $to |
| 41 | - */ |
|
| 41 | + */ |
|
| 42 | 42 | public function process_subscription_status_change( $subscription, $from, $to ) { |
| 43 | 43 | |
| 44 | 44 | $gateway = $subscription->get_gateway(); |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | - exit; |
|
| 7 | + exit; |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | /** |
@@ -14,228 +14,228 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | class GetPaid_Item_Data_Store extends GetPaid_Data_Store_WP { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Data stored in meta keys, but not considered "meta" for an item. |
|
| 19 | - * |
|
| 20 | - * @since 1.0.19 |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $internal_meta_keys = array( |
|
| 24 | - '_wpinv_price', |
|
| 25 | - '_wpinv_vat_rule', |
|
| 26 | - '_wpinv_vat_class', |
|
| 27 | - '_wpinv_type', |
|
| 28 | - '_wpinv_custom_id', |
|
| 29 | - '_wpinv_custom_name', |
|
| 30 | - '_wpinv_custom_singular_name', |
|
| 31 | - '_wpinv_editable', |
|
| 32 | - '_wpinv_dynamic_pricing', |
|
| 33 | - '_minimum_price', |
|
| 34 | - '_wpinv_is_recurring', |
|
| 35 | - '_wpinv_recurring_period', |
|
| 36 | - '_wpinv_recurring_interval', |
|
| 37 | - '_wpinv_recurring_limit', |
|
| 38 | - '_wpinv_free_trial', |
|
| 39 | - '_wpinv_trial_period', |
|
| 40 | - '_wpinv_trial_interval' |
|
| 41 | - ); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * A map of meta keys to data props. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.19 |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - protected $meta_key_to_props = array( |
|
| 51 | - '_wpinv_price' => 'price', |
|
| 52 | - '_wpinv_vat_rule' => 'vat_rule', |
|
| 53 | - '_wpinv_vat_class' => 'vat_class', |
|
| 54 | - '_wpinv_type' => 'type', |
|
| 55 | - '_wpinv_custom_id' => 'custom_id', |
|
| 56 | - '_wpinv_custom_name' => 'custom_name', |
|
| 57 | - '_wpinv_custom_singular_name' => 'custom_singular_name', |
|
| 58 | - '_wpinv_editable' => 'is_editable', |
|
| 59 | - '_wpinv_dynamic_pricing' => 'is_dynamic_pricing', |
|
| 60 | - '_minimum_price' => 'minimum_price', |
|
| 61 | - '_wpinv_custom_name' => 'custom_name', |
|
| 62 | - '_wpinv_is_recurring' => 'is_recurring', |
|
| 63 | - '_wpinv_recurring_period' => 'recurring_period', |
|
| 64 | - '_wpinv_recurring_interval' => 'recurring_interval', |
|
| 65 | - '_wpinv_recurring_limit' => 'recurring_limit', |
|
| 66 | - '_wpinv_free_trial' => 'is_free_trial', |
|
| 67 | - '_wpinv_trial_period' => 'trial_period', |
|
| 68 | - '_wpinv_trial_interval' => 'trial_interval', |
|
| 69 | - '_wpinv_version' => 'version', |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - /* |
|
| 17 | + /** |
|
| 18 | + * Data stored in meta keys, but not considered "meta" for an item. |
|
| 19 | + * |
|
| 20 | + * @since 1.0.19 |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $internal_meta_keys = array( |
|
| 24 | + '_wpinv_price', |
|
| 25 | + '_wpinv_vat_rule', |
|
| 26 | + '_wpinv_vat_class', |
|
| 27 | + '_wpinv_type', |
|
| 28 | + '_wpinv_custom_id', |
|
| 29 | + '_wpinv_custom_name', |
|
| 30 | + '_wpinv_custom_singular_name', |
|
| 31 | + '_wpinv_editable', |
|
| 32 | + '_wpinv_dynamic_pricing', |
|
| 33 | + '_minimum_price', |
|
| 34 | + '_wpinv_is_recurring', |
|
| 35 | + '_wpinv_recurring_period', |
|
| 36 | + '_wpinv_recurring_interval', |
|
| 37 | + '_wpinv_recurring_limit', |
|
| 38 | + '_wpinv_free_trial', |
|
| 39 | + '_wpinv_trial_period', |
|
| 40 | + '_wpinv_trial_interval' |
|
| 41 | + ); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * A map of meta keys to data props. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.19 |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + protected $meta_key_to_props = array( |
|
| 51 | + '_wpinv_price' => 'price', |
|
| 52 | + '_wpinv_vat_rule' => 'vat_rule', |
|
| 53 | + '_wpinv_vat_class' => 'vat_class', |
|
| 54 | + '_wpinv_type' => 'type', |
|
| 55 | + '_wpinv_custom_id' => 'custom_id', |
|
| 56 | + '_wpinv_custom_name' => 'custom_name', |
|
| 57 | + '_wpinv_custom_singular_name' => 'custom_singular_name', |
|
| 58 | + '_wpinv_editable' => 'is_editable', |
|
| 59 | + '_wpinv_dynamic_pricing' => 'is_dynamic_pricing', |
|
| 60 | + '_minimum_price' => 'minimum_price', |
|
| 61 | + '_wpinv_custom_name' => 'custom_name', |
|
| 62 | + '_wpinv_is_recurring' => 'is_recurring', |
|
| 63 | + '_wpinv_recurring_period' => 'recurring_period', |
|
| 64 | + '_wpinv_recurring_interval' => 'recurring_interval', |
|
| 65 | + '_wpinv_recurring_limit' => 'recurring_limit', |
|
| 66 | + '_wpinv_free_trial' => 'is_free_trial', |
|
| 67 | + '_wpinv_trial_period' => 'trial_period', |
|
| 68 | + '_wpinv_trial_interval' => 'trial_interval', |
|
| 69 | + '_wpinv_version' => 'version', |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + /* |
|
| 73 | 73 | |-------------------------------------------------------------------------- |
| 74 | 74 | | CRUD Methods |
| 75 | 75 | |-------------------------------------------------------------------------- |
| 76 | 76 | */ |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Method to create a new item in the database. |
|
| 80 | - * |
|
| 81 | - * @param WPInv_Item $item Item object. |
|
| 82 | - */ |
|
| 83 | - public function create( &$item ) { |
|
| 84 | - $item->set_version( WPINV_VERSION ); |
|
| 85 | - $item->set_date_created( current_time('mysql') ); |
|
| 86 | - |
|
| 87 | - // Create a new post. |
|
| 88 | - $id = wp_insert_post( |
|
| 89 | - apply_filters( |
|
| 90 | - 'getpaid_new_item_data', |
|
| 91 | - array( |
|
| 92 | - 'post_date' => $item->get_date_created( 'edit' ), |
|
| 93 | - 'post_type' => 'wpi_item', |
|
| 94 | - 'post_status' => $this->get_post_status( $item ), |
|
| 95 | - 'ping_status' => 'closed', |
|
| 96 | - 'post_author' => $item->get_author( 'edit' ), |
|
| 97 | - 'post_title' => $item->get_name( 'edit' ), |
|
| 98 | - 'post_parent' => 0, |
|
| 99 | - 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 100 | - ) |
|
| 101 | - ), |
|
| 102 | - true |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 106 | - $item->set_id( $id ); |
|
| 107 | - $this->update_post_meta( $item ); |
|
| 108 | - $item->save_meta_data(); |
|
| 109 | - $item->apply_changes(); |
|
| 110 | - $this->clear_caches( $item ); |
|
| 111 | - do_action( 'getpaid_new_item', $item ); |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if ( is_wp_error( $id ) ) { |
|
| 116 | - $item->last_error = $id->get_error_message(); |
|
| 117 | - } |
|
| 78 | + /** |
|
| 79 | + * Method to create a new item in the database. |
|
| 80 | + * |
|
| 81 | + * @param WPInv_Item $item Item object. |
|
| 82 | + */ |
|
| 83 | + public function create( &$item ) { |
|
| 84 | + $item->set_version( WPINV_VERSION ); |
|
| 85 | + $item->set_date_created( current_time('mysql') ); |
|
| 86 | + |
|
| 87 | + // Create a new post. |
|
| 88 | + $id = wp_insert_post( |
|
| 89 | + apply_filters( |
|
| 90 | + 'getpaid_new_item_data', |
|
| 91 | + array( |
|
| 92 | + 'post_date' => $item->get_date_created( 'edit' ), |
|
| 93 | + 'post_type' => 'wpi_item', |
|
| 94 | + 'post_status' => $this->get_post_status( $item ), |
|
| 95 | + 'ping_status' => 'closed', |
|
| 96 | + 'post_author' => $item->get_author( 'edit' ), |
|
| 97 | + 'post_title' => $item->get_name( 'edit' ), |
|
| 98 | + 'post_parent' => 0, |
|
| 99 | + 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 100 | + ) |
|
| 101 | + ), |
|
| 102 | + true |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + if ( $id && ! is_wp_error( $id ) ) { |
|
| 106 | + $item->set_id( $id ); |
|
| 107 | + $this->update_post_meta( $item ); |
|
| 108 | + $item->save_meta_data(); |
|
| 109 | + $item->apply_changes(); |
|
| 110 | + $this->clear_caches( $item ); |
|
| 111 | + do_action( 'getpaid_new_item', $item ); |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if ( is_wp_error( $id ) ) { |
|
| 116 | + $item->last_error = $id->get_error_message(); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Method to read an item from the database. |
|
| 124 | - * |
|
| 125 | - * @param WPInv_Item $item Item object. |
|
| 126 | - * |
|
| 127 | - */ |
|
| 128 | - public function read( &$item ) { |
|
| 129 | - |
|
| 130 | - $item->set_defaults(); |
|
| 131 | - $item_object = get_post( $item->get_id() ); |
|
| 132 | - |
|
| 133 | - if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) { |
|
| 134 | - $item->last_error = __( 'Invalid item.', 'invoicing' ); |
|
| 135 | - $item->set_id( 0 ); |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $item->set_props( |
|
| 140 | - array( |
|
| 141 | - 'parent_id' => $item_object->post_parent, |
|
| 142 | - 'date_created' => 0 < $item_object->post_date ? $item_object->post_date : null, |
|
| 143 | - 'date_modified' => 0 < $item_object->post_modified ? $item_object->post_modified : null, |
|
| 144 | - 'status' => $item_object->post_status, |
|
| 145 | - 'name' => $item_object->post_title, |
|
| 146 | - 'description' => $item_object->post_excerpt, |
|
| 147 | - 'author' => $item_object->post_author, |
|
| 148 | - ) |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - $this->read_object_data( $item, $item_object ); |
|
| 152 | - $item->read_meta_data(); |
|
| 153 | - $item->set_object_read( true ); |
|
| 154 | - do_action( 'getpaid_read_item', $item ); |
|
| 155 | - |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Method to update an item in the database. |
|
| 160 | - * |
|
| 161 | - * @param WPInv_Item $item Item object. |
|
| 162 | - */ |
|
| 163 | - public function update( &$item ) { |
|
| 164 | - $item->save_meta_data(); |
|
| 165 | - $item->set_version( WPINV_VERSION ); |
|
| 166 | - |
|
| 167 | - if ( null === $item->get_date_created( 'edit' ) ) { |
|
| 168 | - $item->set_date_created( current_time('mysql') ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // Grab the current status so we can compare. |
|
| 172 | - $previous_status = get_post_status( $item->get_id() ); |
|
| 173 | - |
|
| 174 | - $changes = $item->get_changes(); |
|
| 175 | - |
|
| 176 | - // Only update the post when the post data changes. |
|
| 177 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
| 178 | - $post_data = array( |
|
| 179 | - 'post_date' => $item->get_date_created( 'edit' ), |
|
| 180 | - 'post_status' => $item->get_status( 'edit' ), |
|
| 181 | - 'post_parent' => $item->get_parent_id( 'edit' ), |
|
| 182 | - 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 183 | - 'post_modified' => $item->get_date_modified( 'edit' ), |
|
| 184 | - 'post_title' => $item->get_name( 'edit' ), |
|
| 185 | - 'post_author' => $item->get_author( 'edit' ), |
|
| 186 | - ); |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
| 190 | - * to update data, since wp_update_post spawns more calls to the |
|
| 191 | - * save_post action. |
|
| 192 | - * |
|
| 193 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 194 | - * or an update purely from CRUD. |
|
| 195 | - */ |
|
| 196 | - if ( doing_action( 'save_post' ) ) { |
|
| 197 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) ); |
|
| 198 | - clean_post_cache( $item->get_id() ); |
|
| 199 | - } else { |
|
| 200 | - wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) ); |
|
| 201 | - } |
|
| 202 | - $item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 203 | - } |
|
| 204 | - $this->update_post_meta( $item ); |
|
| 205 | - $item->apply_changes(); |
|
| 206 | - $this->clear_caches( $item ); |
|
| 207 | - |
|
| 208 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 209 | - $new_status = $item->get_status( 'edit' ); |
|
| 210 | - |
|
| 211 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 212 | - do_action( 'getpaid_new_item', $item ); |
|
| 213 | - } else { |
|
| 214 | - do_action( 'getpaid_update_item', $item ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /* |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Method to read an item from the database. |
|
| 124 | + * |
|
| 125 | + * @param WPInv_Item $item Item object. |
|
| 126 | + * |
|
| 127 | + */ |
|
| 128 | + public function read( &$item ) { |
|
| 129 | + |
|
| 130 | + $item->set_defaults(); |
|
| 131 | + $item_object = get_post( $item->get_id() ); |
|
| 132 | + |
|
| 133 | + if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) { |
|
| 134 | + $item->last_error = __( 'Invalid item.', 'invoicing' ); |
|
| 135 | + $item->set_id( 0 ); |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $item->set_props( |
|
| 140 | + array( |
|
| 141 | + 'parent_id' => $item_object->post_parent, |
|
| 142 | + 'date_created' => 0 < $item_object->post_date ? $item_object->post_date : null, |
|
| 143 | + 'date_modified' => 0 < $item_object->post_modified ? $item_object->post_modified : null, |
|
| 144 | + 'status' => $item_object->post_status, |
|
| 145 | + 'name' => $item_object->post_title, |
|
| 146 | + 'description' => $item_object->post_excerpt, |
|
| 147 | + 'author' => $item_object->post_author, |
|
| 148 | + ) |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + $this->read_object_data( $item, $item_object ); |
|
| 152 | + $item->read_meta_data(); |
|
| 153 | + $item->set_object_read( true ); |
|
| 154 | + do_action( 'getpaid_read_item', $item ); |
|
| 155 | + |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Method to update an item in the database. |
|
| 160 | + * |
|
| 161 | + * @param WPInv_Item $item Item object. |
|
| 162 | + */ |
|
| 163 | + public function update( &$item ) { |
|
| 164 | + $item->save_meta_data(); |
|
| 165 | + $item->set_version( WPINV_VERSION ); |
|
| 166 | + |
|
| 167 | + if ( null === $item->get_date_created( 'edit' ) ) { |
|
| 168 | + $item->set_date_created( current_time('mysql') ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // Grab the current status so we can compare. |
|
| 172 | + $previous_status = get_post_status( $item->get_id() ); |
|
| 173 | + |
|
| 174 | + $changes = $item->get_changes(); |
|
| 175 | + |
|
| 176 | + // Only update the post when the post data changes. |
|
| 177 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
| 178 | + $post_data = array( |
|
| 179 | + 'post_date' => $item->get_date_created( 'edit' ), |
|
| 180 | + 'post_status' => $item->get_status( 'edit' ), |
|
| 181 | + 'post_parent' => $item->get_parent_id( 'edit' ), |
|
| 182 | + 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 183 | + 'post_modified' => $item->get_date_modified( 'edit' ), |
|
| 184 | + 'post_title' => $item->get_name( 'edit' ), |
|
| 185 | + 'post_author' => $item->get_author( 'edit' ), |
|
| 186 | + ); |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
| 190 | + * to update data, since wp_update_post spawns more calls to the |
|
| 191 | + * save_post action. |
|
| 192 | + * |
|
| 193 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 194 | + * or an update purely from CRUD. |
|
| 195 | + */ |
|
| 196 | + if ( doing_action( 'save_post' ) ) { |
|
| 197 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) ); |
|
| 198 | + clean_post_cache( $item->get_id() ); |
|
| 199 | + } else { |
|
| 200 | + wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) ); |
|
| 201 | + } |
|
| 202 | + $item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 203 | + } |
|
| 204 | + $this->update_post_meta( $item ); |
|
| 205 | + $item->apply_changes(); |
|
| 206 | + $this->clear_caches( $item ); |
|
| 207 | + |
|
| 208 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 209 | + $new_status = $item->get_status( 'edit' ); |
|
| 210 | + |
|
| 211 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 212 | + do_action( 'getpaid_new_item', $item ); |
|
| 213 | + } else { |
|
| 214 | + do_action( 'getpaid_update_item', $item ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /* |
|
| 220 | 220 | |-------------------------------------------------------------------------- |
| 221 | 221 | | Additional Methods |
| 222 | 222 | |-------------------------------------------------------------------------- |
| 223 | 223 | */ |
| 224 | 224 | |
| 225 | - /** |
|
| 226 | - * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class. |
|
| 227 | - * |
|
| 228 | - * @param WPInv_Item $item WPInv_Item object. |
|
| 229 | - * @since 1.0.19 |
|
| 230 | - */ |
|
| 231 | - protected function update_post_meta( &$item ) { |
|
| 225 | + /** |
|
| 226 | + * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class. |
|
| 227 | + * |
|
| 228 | + * @param WPInv_Item $item WPInv_Item object. |
|
| 229 | + * @since 1.0.19 |
|
| 230 | + */ |
|
| 231 | + protected function update_post_meta( &$item ) { |
|
| 232 | 232 | |
| 233 | - // Ensure that we have a custom id. |
|
| 233 | + // Ensure that we have a custom id. |
|
| 234 | 234 | if ( ! $item->get_custom_id() ) { |
| 235 | 235 | $item->set_custom_id( $item->get_id() ); |
| 236 | - } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - parent::update_post_meta( $item ); |
|
| 239 | - } |
|
| 238 | + parent::update_post_meta( $item ); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | 241 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | - exit; |
|
| 7 | + exit; |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | /** |
@@ -14,179 +14,179 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | class GetPaid_Payment_Form_Data_Store extends GetPaid_Data_Store_WP { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Data stored in meta keys, but not considered "meta" for a form. |
|
| 19 | - * |
|
| 20 | - * @since 1.0.19 |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $internal_meta_keys = array( |
|
| 24 | - 'wpinv_form_elements', |
|
| 25 | - 'wpinv_form_items', |
|
| 26 | - 'wpinv_form_earned', |
|
| 27 | - 'wpinv_form_refunded', |
|
| 28 | - 'wpinv_form_cancelled', |
|
| 29 | - 'wpinv_form_failed' |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * A map of meta keys to data props. |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - protected $meta_key_to_props = array( |
|
| 40 | - 'wpinv_form_elements' => 'elements', |
|
| 41 | - 'wpinv_form_items' => 'items', |
|
| 42 | - 'wpinv_form_earned' => 'earned', |
|
| 43 | - 'wpinv_form_refunded' => 'refunded', |
|
| 44 | - 'wpinv_form_cancelled' => 'cancelled', |
|
| 45 | - 'wpinv_form_failed' => 'failed', |
|
| 46 | - ); |
|
| 47 | - |
|
| 48 | - /* |
|
| 17 | + /** |
|
| 18 | + * Data stored in meta keys, but not considered "meta" for a form. |
|
| 19 | + * |
|
| 20 | + * @since 1.0.19 |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $internal_meta_keys = array( |
|
| 24 | + 'wpinv_form_elements', |
|
| 25 | + 'wpinv_form_items', |
|
| 26 | + 'wpinv_form_earned', |
|
| 27 | + 'wpinv_form_refunded', |
|
| 28 | + 'wpinv_form_cancelled', |
|
| 29 | + 'wpinv_form_failed' |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * A map of meta keys to data props. |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + protected $meta_key_to_props = array( |
|
| 40 | + 'wpinv_form_elements' => 'elements', |
|
| 41 | + 'wpinv_form_items' => 'items', |
|
| 42 | + 'wpinv_form_earned' => 'earned', |
|
| 43 | + 'wpinv_form_refunded' => 'refunded', |
|
| 44 | + 'wpinv_form_cancelled' => 'cancelled', |
|
| 45 | + 'wpinv_form_failed' => 'failed', |
|
| 46 | + ); |
|
| 47 | + |
|
| 48 | + /* |
|
| 49 | 49 | |-------------------------------------------------------------------------- |
| 50 | 50 | | CRUD Methods |
| 51 | 51 | |-------------------------------------------------------------------------- |
| 52 | 52 | */ |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Method to create a new form in the database. |
|
| 56 | - * |
|
| 57 | - * @param GetPaid_Payment_Form $form Form object. |
|
| 58 | - */ |
|
| 59 | - public function create( &$form ) { |
|
| 60 | - $form->set_version( WPINV_VERSION ); |
|
| 61 | - $form->set_date_created( current_time('mysql') ); |
|
| 62 | - |
|
| 63 | - // Create a new post. |
|
| 64 | - $id = wp_insert_post( |
|
| 65 | - apply_filters( |
|
| 66 | - 'getpaid_new_payment_form_data', |
|
| 67 | - array( |
|
| 68 | - 'post_date' => $form->get_date_created( 'edit' ), |
|
| 69 | - 'post_type' => 'wpi_payment_form', |
|
| 70 | - 'post_status' => $this->get_post_status( $form ), |
|
| 71 | - 'ping_status' => 'closed', |
|
| 72 | - 'post_author' => $form->get_author( 'edit' ), |
|
| 73 | - 'post_title' => $form->get_name( 'edit' ), |
|
| 74 | - ) |
|
| 75 | - ), |
|
| 76 | - true |
|
| 77 | - ); |
|
| 78 | - |
|
| 79 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 80 | - $form->set_id( $id ); |
|
| 81 | - $this->update_post_meta( $form ); |
|
| 82 | - $form->save_meta_data(); |
|
| 83 | - $form->apply_changes(); |
|
| 84 | - $this->clear_caches( $form ); |
|
| 85 | - do_action( 'getpaid_create_payment_form', $form ); |
|
| 86 | - return true; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - if ( is_wp_error( $id ) ) { |
|
| 90 | - $form->last_error = $id->get_error_message(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return false; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Method to read a form from the database. |
|
| 98 | - * |
|
| 99 | - * @param GetPaid_Payment_Form $form Form object. |
|
| 100 | - * |
|
| 101 | - */ |
|
| 102 | - public function read( &$form ) { |
|
| 103 | - |
|
| 104 | - $form->set_defaults(); |
|
| 105 | - $form_object = get_post( $form->get_id() ); |
|
| 106 | - |
|
| 107 | - if ( ! $form->get_id() || ! $form_object || $form_object->post_type != 'wpi_payment_form' ) { |
|
| 108 | - $form->last_error = __( 'Invalid form.', 'invoicing' ); |
|
| 109 | - $form->set_id( 0 ); |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $form->set_props( |
|
| 114 | - array( |
|
| 115 | - 'date_created' => 0 < $form_object->post_date ? $form_object->post_date : null, |
|
| 116 | - 'date_modified' => 0 < $form_object->post_modified ? $form_object->post_modified : null, |
|
| 117 | - 'status' => $form_object->post_status, |
|
| 118 | - 'name' => $form_object->post_title, |
|
| 119 | - 'author' => $form_object->post_author, |
|
| 120 | - ) |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - $this->read_object_data( $form, $form_object ); |
|
| 124 | - $form->read_meta_data(); |
|
| 125 | - $form->set_object_read( true ); |
|
| 126 | - do_action( 'getpaid_read_payment_form', $form ); |
|
| 127 | - |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Method to update a form in the database. |
|
| 132 | - * |
|
| 133 | - * @param GetPaid_Payment_Form $form Form object. |
|
| 134 | - */ |
|
| 135 | - public function update( &$form ) { |
|
| 136 | - $form->save_meta_data(); |
|
| 137 | - $form->set_version( WPINV_VERSION ); |
|
| 138 | - |
|
| 139 | - if ( null === $form->get_date_created( 'edit' ) ) { |
|
| 140 | - $form->set_date_created( current_time('mysql') ); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Grab the current status so we can compare. |
|
| 144 | - $previous_status = get_post_status( $form->get_id() ); |
|
| 145 | - |
|
| 146 | - $changes = $form->get_changes(); |
|
| 147 | - |
|
| 148 | - // Only update the post when the post data changes. |
|
| 149 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
| 150 | - $post_data = array( |
|
| 151 | - 'post_date' => $form->get_date_created( 'edit' ), |
|
| 152 | - 'post_status' => $form->get_status( 'edit' ), |
|
| 153 | - 'post_title' => $form->get_name( 'edit' ), |
|
| 154 | - 'post_author' => $form->get_author( 'edit' ), |
|
| 155 | - 'post_modified' => $form->get_date_modified( 'edit' ), |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
| 160 | - * to update data, since wp_update_post spawns more calls to the |
|
| 161 | - * save_post action. |
|
| 162 | - * |
|
| 163 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 164 | - * or an update purely from CRUD. |
|
| 165 | - */ |
|
| 166 | - if ( doing_action( 'save_post' ) ) { |
|
| 167 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $form->get_id() ) ); |
|
| 168 | - clean_post_cache( $form->get_id() ); |
|
| 169 | - } else { |
|
| 170 | - wp_update_post( array_merge( array( 'ID' => $form->get_id() ), $post_data ) ); |
|
| 171 | - } |
|
| 172 | - $form->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 173 | - } |
|
| 174 | - $this->update_post_meta( $form ); |
|
| 175 | - $form->apply_changes(); |
|
| 176 | - $this->clear_caches( $form ); |
|
| 177 | - |
|
| 178 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 179 | - $new_status = $form->get_status( 'edit' ); |
|
| 180 | - |
|
| 181 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 182 | - do_action( 'getpaid_new_payment_form', $form ); |
|
| 183 | - } else { |
|
| 184 | - do_action( 'getpaid_update_payment_form', $form ); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /* |
|
| 54 | + /** |
|
| 55 | + * Method to create a new form in the database. |
|
| 56 | + * |
|
| 57 | + * @param GetPaid_Payment_Form $form Form object. |
|
| 58 | + */ |
|
| 59 | + public function create( &$form ) { |
|
| 60 | + $form->set_version( WPINV_VERSION ); |
|
| 61 | + $form->set_date_created( current_time('mysql') ); |
|
| 62 | + |
|
| 63 | + // Create a new post. |
|
| 64 | + $id = wp_insert_post( |
|
| 65 | + apply_filters( |
|
| 66 | + 'getpaid_new_payment_form_data', |
|
| 67 | + array( |
|
| 68 | + 'post_date' => $form->get_date_created( 'edit' ), |
|
| 69 | + 'post_type' => 'wpi_payment_form', |
|
| 70 | + 'post_status' => $this->get_post_status( $form ), |
|
| 71 | + 'ping_status' => 'closed', |
|
| 72 | + 'post_author' => $form->get_author( 'edit' ), |
|
| 73 | + 'post_title' => $form->get_name( 'edit' ), |
|
| 74 | + ) |
|
| 75 | + ), |
|
| 76 | + true |
|
| 77 | + ); |
|
| 78 | + |
|
| 79 | + if ( $id && ! is_wp_error( $id ) ) { |
|
| 80 | + $form->set_id( $id ); |
|
| 81 | + $this->update_post_meta( $form ); |
|
| 82 | + $form->save_meta_data(); |
|
| 83 | + $form->apply_changes(); |
|
| 84 | + $this->clear_caches( $form ); |
|
| 85 | + do_action( 'getpaid_create_payment_form', $form ); |
|
| 86 | + return true; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + if ( is_wp_error( $id ) ) { |
|
| 90 | + $form->last_error = $id->get_error_message(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return false; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Method to read a form from the database. |
|
| 98 | + * |
|
| 99 | + * @param GetPaid_Payment_Form $form Form object. |
|
| 100 | + * |
|
| 101 | + */ |
|
| 102 | + public function read( &$form ) { |
|
| 103 | + |
|
| 104 | + $form->set_defaults(); |
|
| 105 | + $form_object = get_post( $form->get_id() ); |
|
| 106 | + |
|
| 107 | + if ( ! $form->get_id() || ! $form_object || $form_object->post_type != 'wpi_payment_form' ) { |
|
| 108 | + $form->last_error = __( 'Invalid form.', 'invoicing' ); |
|
| 109 | + $form->set_id( 0 ); |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $form->set_props( |
|
| 114 | + array( |
|
| 115 | + 'date_created' => 0 < $form_object->post_date ? $form_object->post_date : null, |
|
| 116 | + 'date_modified' => 0 < $form_object->post_modified ? $form_object->post_modified : null, |
|
| 117 | + 'status' => $form_object->post_status, |
|
| 118 | + 'name' => $form_object->post_title, |
|
| 119 | + 'author' => $form_object->post_author, |
|
| 120 | + ) |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + $this->read_object_data( $form, $form_object ); |
|
| 124 | + $form->read_meta_data(); |
|
| 125 | + $form->set_object_read( true ); |
|
| 126 | + do_action( 'getpaid_read_payment_form', $form ); |
|
| 127 | + |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Method to update a form in the database. |
|
| 132 | + * |
|
| 133 | + * @param GetPaid_Payment_Form $form Form object. |
|
| 134 | + */ |
|
| 135 | + public function update( &$form ) { |
|
| 136 | + $form->save_meta_data(); |
|
| 137 | + $form->set_version( WPINV_VERSION ); |
|
| 138 | + |
|
| 139 | + if ( null === $form->get_date_created( 'edit' ) ) { |
|
| 140 | + $form->set_date_created( current_time('mysql') ); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Grab the current status so we can compare. |
|
| 144 | + $previous_status = get_post_status( $form->get_id() ); |
|
| 145 | + |
|
| 146 | + $changes = $form->get_changes(); |
|
| 147 | + |
|
| 148 | + // Only update the post when the post data changes. |
|
| 149 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
| 150 | + $post_data = array( |
|
| 151 | + 'post_date' => $form->get_date_created( 'edit' ), |
|
| 152 | + 'post_status' => $form->get_status( 'edit' ), |
|
| 153 | + 'post_title' => $form->get_name( 'edit' ), |
|
| 154 | + 'post_author' => $form->get_author( 'edit' ), |
|
| 155 | + 'post_modified' => $form->get_date_modified( 'edit' ), |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
| 160 | + * to update data, since wp_update_post spawns more calls to the |
|
| 161 | + * save_post action. |
|
| 162 | + * |
|
| 163 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 164 | + * or an update purely from CRUD. |
|
| 165 | + */ |
|
| 166 | + if ( doing_action( 'save_post' ) ) { |
|
| 167 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $form->get_id() ) ); |
|
| 168 | + clean_post_cache( $form->get_id() ); |
|
| 169 | + } else { |
|
| 170 | + wp_update_post( array_merge( array( 'ID' => $form->get_id() ), $post_data ) ); |
|
| 171 | + } |
|
| 172 | + $form->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 173 | + } |
|
| 174 | + $this->update_post_meta( $form ); |
|
| 175 | + $form->apply_changes(); |
|
| 176 | + $this->clear_caches( $form ); |
|
| 177 | + |
|
| 178 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 179 | + $new_status = $form->get_status( 'edit' ); |
|
| 180 | + |
|
| 181 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 182 | + do_action( 'getpaid_new_payment_form', $form ); |
|
| 183 | + } else { |
|
| 184 | + do_action( 'getpaid_update_payment_form', $form ); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /* |
|
| 190 | 190 | |-------------------------------------------------------------------------- |
| 191 | 191 | | Additional Methods |
| 192 | 192 | |-------------------------------------------------------------------------- |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | - exit; |
|
| 8 | + exit; |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | /** |
@@ -15,196 +15,196 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class GetPaid_Discount_Data_Store extends GetPaid_Data_Store_WP { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Data stored in meta keys, but not considered "meta" for a discount. |
|
| 20 | - * |
|
| 21 | - * @since 1.0.19 |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $internal_meta_keys = array( |
|
| 25 | - '_wpi_discount_code', |
|
| 26 | - '_wpi_discount_amount', |
|
| 27 | - '_wpi_discount_start', |
|
| 28 | - '_wpi_discount_expiration', |
|
| 29 | - '_wpi_discount_type', |
|
| 30 | - '_wpi_discount_uses', |
|
| 31 | - '_wpi_discount_is_single_use', |
|
| 32 | - '_wpi_discount_items', |
|
| 33 | - '_wpi_discount_excluded_items', |
|
| 34 | - '_wpi_discount_max_uses', |
|
| 35 | - '_wpi_discount_is_recurring', |
|
| 36 | - '_wpi_discount_min_total', |
|
| 37 | - '_wpi_discount_max_total', |
|
| 38 | - ); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * A map of meta keys to data props. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.19 |
|
| 44 | - * |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - protected $meta_key_to_props = array( |
|
| 48 | - '_wpi_discount_code' => 'code', |
|
| 49 | - '_wpi_discount_amount' => 'amount', |
|
| 50 | - '_wpi_discount_start' => 'start', |
|
| 51 | - '_wpi_discount_expiration' => 'expiration', |
|
| 52 | - '_wpi_discount_type' => 'type', |
|
| 53 | - '_wpi_discount_uses' => 'uses', |
|
| 54 | - '_wpi_discount_is_single_use' => 'is_single_use', |
|
| 55 | - '_wpi_discount_items' => 'items', |
|
| 56 | - '_wpi_discount_excluded_items' => 'excluded_items', |
|
| 57 | - '_wpi_discount_max_uses' => 'max_uses', |
|
| 58 | - '_wpi_discount_is_recurring' => 'is_recurring', |
|
| 59 | - '_wpi_discount_min_total' => 'min_total', |
|
| 60 | - '_wpi_discount_max_total' => 'max_total', |
|
| 61 | - ); |
|
| 62 | - |
|
| 63 | - /* |
|
| 18 | + /** |
|
| 19 | + * Data stored in meta keys, but not considered "meta" for a discount. |
|
| 20 | + * |
|
| 21 | + * @since 1.0.19 |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $internal_meta_keys = array( |
|
| 25 | + '_wpi_discount_code', |
|
| 26 | + '_wpi_discount_amount', |
|
| 27 | + '_wpi_discount_start', |
|
| 28 | + '_wpi_discount_expiration', |
|
| 29 | + '_wpi_discount_type', |
|
| 30 | + '_wpi_discount_uses', |
|
| 31 | + '_wpi_discount_is_single_use', |
|
| 32 | + '_wpi_discount_items', |
|
| 33 | + '_wpi_discount_excluded_items', |
|
| 34 | + '_wpi_discount_max_uses', |
|
| 35 | + '_wpi_discount_is_recurring', |
|
| 36 | + '_wpi_discount_min_total', |
|
| 37 | + '_wpi_discount_max_total', |
|
| 38 | + ); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * A map of meta keys to data props. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.19 |
|
| 44 | + * |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + protected $meta_key_to_props = array( |
|
| 48 | + '_wpi_discount_code' => 'code', |
|
| 49 | + '_wpi_discount_amount' => 'amount', |
|
| 50 | + '_wpi_discount_start' => 'start', |
|
| 51 | + '_wpi_discount_expiration' => 'expiration', |
|
| 52 | + '_wpi_discount_type' => 'type', |
|
| 53 | + '_wpi_discount_uses' => 'uses', |
|
| 54 | + '_wpi_discount_is_single_use' => 'is_single_use', |
|
| 55 | + '_wpi_discount_items' => 'items', |
|
| 56 | + '_wpi_discount_excluded_items' => 'excluded_items', |
|
| 57 | + '_wpi_discount_max_uses' => 'max_uses', |
|
| 58 | + '_wpi_discount_is_recurring' => 'is_recurring', |
|
| 59 | + '_wpi_discount_min_total' => 'min_total', |
|
| 60 | + '_wpi_discount_max_total' => 'max_total', |
|
| 61 | + ); |
|
| 62 | + |
|
| 63 | + /* |
|
| 64 | 64 | |-------------------------------------------------------------------------- |
| 65 | 65 | | CRUD Methods |
| 66 | 66 | |-------------------------------------------------------------------------- |
| 67 | 67 | */ |
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Method to create a new discount in the database. |
|
| 71 | - * |
|
| 72 | - * @param WPInv_Discount $discount Discount object. |
|
| 73 | - */ |
|
| 74 | - public function create( &$discount ) { |
|
| 75 | - $discount->set_version( WPINV_VERSION ); |
|
| 76 | - $discount->set_date_created( current_time('mysql') ); |
|
| 77 | - |
|
| 78 | - // Create a new post. |
|
| 79 | - $id = wp_insert_post( |
|
| 80 | - apply_filters( |
|
| 81 | - 'getpaid_new_discount_data', |
|
| 82 | - array( |
|
| 83 | - 'post_date' => $discount->get_date_created( 'edit' ), |
|
| 84 | - 'post_type' => 'wpi_discount', |
|
| 85 | - 'post_status' => $this->get_post_status( $discount ), |
|
| 86 | - 'ping_status' => 'closed', |
|
| 87 | - 'post_author' => $discount->get_author( 'edit' ), |
|
| 88 | - 'post_title' => $discount->get_name( 'edit' ), |
|
| 89 | - 'post_excerpt' => $discount->get_description( 'edit' ), |
|
| 90 | - ) |
|
| 91 | - ), |
|
| 92 | - true |
|
| 93 | - ); |
|
| 94 | - |
|
| 95 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 96 | - $discount->set_id( $id ); |
|
| 97 | - $this->update_post_meta( $discount ); |
|
| 98 | - $discount->save_meta_data(); |
|
| 99 | - $discount->apply_changes(); |
|
| 100 | - $this->clear_caches( $discount ); |
|
| 101 | - do_action( 'getpaid_new_discount', $discount ); |
|
| 102 | - return true; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if ( is_wp_error( $id ) ) { |
|
| 106 | - $discount->last_error = $id->get_error_message(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Method to read a discount from the database. |
|
| 114 | - * |
|
| 115 | - * @param WPInv_Discount $discount Discount object. |
|
| 116 | - * |
|
| 117 | - */ |
|
| 118 | - public function read( &$discount ) { |
|
| 119 | - |
|
| 120 | - $discount->set_defaults(); |
|
| 121 | - $discount_object = get_post( $discount->get_id() ); |
|
| 122 | - |
|
| 123 | - if ( ! $discount->get_id() || ! $discount_object || $discount_object->post_type != 'wpi_discount' ) { |
|
| 124 | - $discount->last_error = __( 'Invalid discount.', 'invoicing' ); |
|
| 125 | - $discount->set_id( 0 ); |
|
| 126 | - return false; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $discount->set_props( |
|
| 130 | - array( |
|
| 131 | - 'date_created' => 0 < $discount_object->post_date ? $discount_object->post_date : null, |
|
| 132 | - 'date_modified' => 0 < $discount_object->post_modified ? $discount_object->post_modified : null, |
|
| 133 | - 'status' => $discount_object->post_status, |
|
| 134 | - 'name' => $discount_object->post_title, |
|
| 135 | - 'author' => $discount_object->post_author, |
|
| 136 | - 'description' => $discount_object->post_excerpt, |
|
| 137 | - ) |
|
| 138 | - ); |
|
| 139 | - |
|
| 140 | - $this->read_object_data( $discount, $discount_object ); |
|
| 141 | - $discount->read_meta_data(); |
|
| 142 | - $discount->set_object_read( true ); |
|
| 143 | - do_action( 'getpaid_read_discount', $discount ); |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Method to update a discount in the database. |
|
| 149 | - * |
|
| 150 | - * @param WPInv_Discount $discount Discount object. |
|
| 151 | - */ |
|
| 152 | - public function update( &$discount ) { |
|
| 153 | - $discount->save_meta_data(); |
|
| 154 | - $discount->set_version( WPINV_VERSION ); |
|
| 155 | - |
|
| 156 | - if ( null === $discount->get_date_created( 'edit' ) ) { |
|
| 157 | - $discount->set_date_created( current_time('mysql') ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // Grab the current status so we can compare. |
|
| 161 | - $previous_status = get_post_status( $discount->get_id() ); |
|
| 162 | - |
|
| 163 | - $changes = $discount->get_changes(); |
|
| 164 | - |
|
| 165 | - // Only update the post when the post data changes. |
|
| 166 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) { |
|
| 167 | - $post_data = array( |
|
| 168 | - 'post_date' => $discount->get_date_created( 'edit' ), |
|
| 169 | - 'post_status' => $discount->get_status( 'edit' ), |
|
| 170 | - 'post_title' => $discount->get_name( 'edit' ), |
|
| 171 | - 'post_author' => $discount->get_author( 'edit' ), |
|
| 172 | - 'post_modified' => $discount->get_date_modified( 'edit' ), |
|
| 173 | - 'post_excerpt' => $discount->get_description( 'edit' ), |
|
| 174 | - ); |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
| 178 | - * to update data, since wp_update_post spawns more calls to the |
|
| 179 | - * save_post action. |
|
| 180 | - * |
|
| 181 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 182 | - * or an update purely from CRUD. |
|
| 183 | - */ |
|
| 184 | - if ( doing_action( 'save_post' ) ) { |
|
| 185 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) ); |
|
| 186 | - clean_post_cache( $discount->get_id() ); |
|
| 187 | - } else { |
|
| 188 | - wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) ); |
|
| 189 | - } |
|
| 190 | - $discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 191 | - } |
|
| 192 | - $this->update_post_meta( $discount ); |
|
| 193 | - $discount->apply_changes(); |
|
| 194 | - $this->clear_caches( $discount ); |
|
| 195 | - |
|
| 196 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 197 | - $new_status = $discount->get_status( 'edit' ); |
|
| 198 | - |
|
| 199 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 200 | - do_action( 'getpaid_new_discount', $discount ); |
|
| 201 | - } else { |
|
| 202 | - do_action( 'getpaid_update_discount', $discount ); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /* |
|
| 69 | + /** |
|
| 70 | + * Method to create a new discount in the database. |
|
| 71 | + * |
|
| 72 | + * @param WPInv_Discount $discount Discount object. |
|
| 73 | + */ |
|
| 74 | + public function create( &$discount ) { |
|
| 75 | + $discount->set_version( WPINV_VERSION ); |
|
| 76 | + $discount->set_date_created( current_time('mysql') ); |
|
| 77 | + |
|
| 78 | + // Create a new post. |
|
| 79 | + $id = wp_insert_post( |
|
| 80 | + apply_filters( |
|
| 81 | + 'getpaid_new_discount_data', |
|
| 82 | + array( |
|
| 83 | + 'post_date' => $discount->get_date_created( 'edit' ), |
|
| 84 | + 'post_type' => 'wpi_discount', |
|
| 85 | + 'post_status' => $this->get_post_status( $discount ), |
|
| 86 | + 'ping_status' => 'closed', |
|
| 87 | + 'post_author' => $discount->get_author( 'edit' ), |
|
| 88 | + 'post_title' => $discount->get_name( 'edit' ), |
|
| 89 | + 'post_excerpt' => $discount->get_description( 'edit' ), |
|
| 90 | + ) |
|
| 91 | + ), |
|
| 92 | + true |
|
| 93 | + ); |
|
| 94 | + |
|
| 95 | + if ( $id && ! is_wp_error( $id ) ) { |
|
| 96 | + $discount->set_id( $id ); |
|
| 97 | + $this->update_post_meta( $discount ); |
|
| 98 | + $discount->save_meta_data(); |
|
| 99 | + $discount->apply_changes(); |
|
| 100 | + $this->clear_caches( $discount ); |
|
| 101 | + do_action( 'getpaid_new_discount', $discount ); |
|
| 102 | + return true; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if ( is_wp_error( $id ) ) { |
|
| 106 | + $discount->last_error = $id->get_error_message(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Method to read a discount from the database. |
|
| 114 | + * |
|
| 115 | + * @param WPInv_Discount $discount Discount object. |
|
| 116 | + * |
|
| 117 | + */ |
|
| 118 | + public function read( &$discount ) { |
|
| 119 | + |
|
| 120 | + $discount->set_defaults(); |
|
| 121 | + $discount_object = get_post( $discount->get_id() ); |
|
| 122 | + |
|
| 123 | + if ( ! $discount->get_id() || ! $discount_object || $discount_object->post_type != 'wpi_discount' ) { |
|
| 124 | + $discount->last_error = __( 'Invalid discount.', 'invoicing' ); |
|
| 125 | + $discount->set_id( 0 ); |
|
| 126 | + return false; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $discount->set_props( |
|
| 130 | + array( |
|
| 131 | + 'date_created' => 0 < $discount_object->post_date ? $discount_object->post_date : null, |
|
| 132 | + 'date_modified' => 0 < $discount_object->post_modified ? $discount_object->post_modified : null, |
|
| 133 | + 'status' => $discount_object->post_status, |
|
| 134 | + 'name' => $discount_object->post_title, |
|
| 135 | + 'author' => $discount_object->post_author, |
|
| 136 | + 'description' => $discount_object->post_excerpt, |
|
| 137 | + ) |
|
| 138 | + ); |
|
| 139 | + |
|
| 140 | + $this->read_object_data( $discount, $discount_object ); |
|
| 141 | + $discount->read_meta_data(); |
|
| 142 | + $discount->set_object_read( true ); |
|
| 143 | + do_action( 'getpaid_read_discount', $discount ); |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Method to update a discount in the database. |
|
| 149 | + * |
|
| 150 | + * @param WPInv_Discount $discount Discount object. |
|
| 151 | + */ |
|
| 152 | + public function update( &$discount ) { |
|
| 153 | + $discount->save_meta_data(); |
|
| 154 | + $discount->set_version( WPINV_VERSION ); |
|
| 155 | + |
|
| 156 | + if ( null === $discount->get_date_created( 'edit' ) ) { |
|
| 157 | + $discount->set_date_created( current_time('mysql') ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // Grab the current status so we can compare. |
|
| 161 | + $previous_status = get_post_status( $discount->get_id() ); |
|
| 162 | + |
|
| 163 | + $changes = $discount->get_changes(); |
|
| 164 | + |
|
| 165 | + // Only update the post when the post data changes. |
|
| 166 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) { |
|
| 167 | + $post_data = array( |
|
| 168 | + 'post_date' => $discount->get_date_created( 'edit' ), |
|
| 169 | + 'post_status' => $discount->get_status( 'edit' ), |
|
| 170 | + 'post_title' => $discount->get_name( 'edit' ), |
|
| 171 | + 'post_author' => $discount->get_author( 'edit' ), |
|
| 172 | + 'post_modified' => $discount->get_date_modified( 'edit' ), |
|
| 173 | + 'post_excerpt' => $discount->get_description( 'edit' ), |
|
| 174 | + ); |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
| 178 | + * to update data, since wp_update_post spawns more calls to the |
|
| 179 | + * save_post action. |
|
| 180 | + * |
|
| 181 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 182 | + * or an update purely from CRUD. |
|
| 183 | + */ |
|
| 184 | + if ( doing_action( 'save_post' ) ) { |
|
| 185 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) ); |
|
| 186 | + clean_post_cache( $discount->get_id() ); |
|
| 187 | + } else { |
|
| 188 | + wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) ); |
|
| 189 | + } |
|
| 190 | + $discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 191 | + } |
|
| 192 | + $this->update_post_meta( $discount ); |
|
| 193 | + $discount->apply_changes(); |
|
| 194 | + $this->clear_caches( $discount ); |
|
| 195 | + |
|
| 196 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 197 | + $new_status = $discount->get_status( 'edit' ); |
|
| 198 | + |
|
| 199 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 200 | + do_action( 'getpaid_new_discount', $discount ); |
|
| 201 | + } else { |
|
| 202 | + do_action( 'getpaid_update_discount', $discount ); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /* |
|
| 208 | 208 | |-------------------------------------------------------------------------- |
| 209 | 209 | | Additional Methods |
| 210 | 210 | |-------------------------------------------------------------------------- |
@@ -14,346 +14,346 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class GetPaid_Data_Store_WP { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Meta type. This should match up with |
|
| 19 | - * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
| 20 | - * WP defines 'post', 'user', 'comment', and 'term'. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $meta_type = 'post'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * This only needs set if you are using a custom metadata type. |
|
| 28 | - * |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - protected $object_id_field_for_meta = ''; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Data stored in meta keys, but not considered "meta" for an object. |
|
| 35 | - * |
|
| 36 | - * @since 1.0.19 |
|
| 37 | - * |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - protected $internal_meta_keys = array(); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Meta data which should exist in the DB, even if empty. |
|
| 44 | - * |
|
| 45 | - * @since 1.0.19 |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - protected $must_exist_meta_keys = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * A map of meta keys to data props. |
|
| 53 | - * |
|
| 54 | - * @since 1.0.19 |
|
| 55 | - * |
|
| 56 | - * @var array |
|
| 57 | - */ |
|
| 58 | - protected $meta_key_to_props = array(); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Returns an array of meta for an object. |
|
| 62 | - * |
|
| 63 | - * @since 1.0.19 |
|
| 64 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - public function read_meta( &$object ) { |
|
| 68 | - global $wpdb; |
|
| 69 | - $db_info = $this->get_db_info(); |
|
| 70 | - $raw_meta_data = $wpdb->get_results( |
|
| 71 | - $wpdb->prepare( |
|
| 72 | - "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
| 17 | + /** |
|
| 18 | + * Meta type. This should match up with |
|
| 19 | + * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
| 20 | + * WP defines 'post', 'user', 'comment', and 'term'. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $meta_type = 'post'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * This only needs set if you are using a custom metadata type. |
|
| 28 | + * |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + protected $object_id_field_for_meta = ''; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Data stored in meta keys, but not considered "meta" for an object. |
|
| 35 | + * |
|
| 36 | + * @since 1.0.19 |
|
| 37 | + * |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + protected $internal_meta_keys = array(); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Meta data which should exist in the DB, even if empty. |
|
| 44 | + * |
|
| 45 | + * @since 1.0.19 |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + protected $must_exist_meta_keys = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * A map of meta keys to data props. |
|
| 53 | + * |
|
| 54 | + * @since 1.0.19 |
|
| 55 | + * |
|
| 56 | + * @var array |
|
| 57 | + */ |
|
| 58 | + protected $meta_key_to_props = array(); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Returns an array of meta for an object. |
|
| 62 | + * |
|
| 63 | + * @since 1.0.19 |
|
| 64 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + public function read_meta( &$object ) { |
|
| 68 | + global $wpdb; |
|
| 69 | + $db_info = $this->get_db_info(); |
|
| 70 | + $raw_meta_data = $wpdb->get_results( |
|
| 71 | + $wpdb->prepare( |
|
| 72 | + "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
| 73 | 73 | FROM {$db_info['table']} |
| 74 | 74 | WHERE {$db_info['object_id_field']} = %d |
| 75 | 75 | ORDER BY {$db_info['meta_id_field']}", |
| 76 | - $object->get_id() |
|
| 77 | - ) |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
| 81 | - $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
| 82 | - return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Deletes meta based on meta ID. |
|
| 87 | - * |
|
| 88 | - * @since 1.0.19 |
|
| 89 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 90 | - * @param stdClass $meta (containing at least ->id). |
|
| 91 | - */ |
|
| 92 | - public function delete_meta( &$object, $meta ) { |
|
| 93 | - delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Add new piece of meta. |
|
| 98 | - * |
|
| 99 | - * @since 1.0.19 |
|
| 100 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 101 | - * @param stdClass $meta (containing ->key and ->value). |
|
| 102 | - * @return int meta ID |
|
| 103 | - */ |
|
| 104 | - public function add_meta( &$object, $meta ) { |
|
| 105 | - return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Update meta. |
|
| 110 | - * |
|
| 111 | - * @since 1.0.19 |
|
| 112 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 113 | - * @param stdClass $meta (containing ->id, ->key and ->value). |
|
| 114 | - */ |
|
| 115 | - public function update_meta( &$object, $meta ) { |
|
| 116 | - update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Table structure is slightly different between meta types, this function will return what we need to know. |
|
| 121 | - * |
|
| 122 | - * @since 1.0.19 |
|
| 123 | - * @return array Array elements: table, object_id_field, meta_id_field |
|
| 124 | - */ |
|
| 125 | - protected function get_db_info() { |
|
| 126 | - global $wpdb; |
|
| 127 | - |
|
| 128 | - $meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well. |
|
| 129 | - $table = $wpdb->prefix; |
|
| 130 | - |
|
| 131 | - // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
| 132 | - if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
| 133 | - $table .= 'getpaid_'; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $table .= $this->meta_type . 'meta'; |
|
| 137 | - $object_id_field = $this->meta_type . '_id'; |
|
| 138 | - |
|
| 139 | - // Figure out our field names. |
|
| 140 | - if ( 'user' === $this->meta_type ) { |
|
| 141 | - $meta_id_field = 'umeta_id'; |
|
| 142 | - $table = $wpdb->usermeta; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
| 146 | - $object_id_field = $this->object_id_field_for_meta; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return array( |
|
| 150 | - 'table' => $table, |
|
| 151 | - 'object_id_field' => $object_id_field, |
|
| 152 | - 'meta_id_field' => $meta_id_field, |
|
| 153 | - ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
| 158 | - * addition to all data props with _ prefix. |
|
| 159 | - * |
|
| 160 | - * @since 1.0.19 |
|
| 161 | - * |
|
| 162 | - * @param string $key Prefix to be added to meta keys. |
|
| 163 | - * @return string |
|
| 164 | - */ |
|
| 165 | - protected function prefix_key( $key ) { |
|
| 166 | - return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Callback to remove unwanted meta data. |
|
| 171 | - * |
|
| 172 | - * @param object $meta Meta object to check if it should be excluded or not. |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - protected function exclude_internal_meta_keys( $meta ) { |
|
| 176 | - return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Gets a list of props and meta keys that need updated based on change state |
|
| 181 | - * or if they are present in the database or not. |
|
| 182 | - * |
|
| 183 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 184 | - * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
| 185 | - * @param string $meta_type The internal WP meta type (post, user, etc). |
|
| 186 | - * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
| 187 | - */ |
|
| 188 | - protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
| 189 | - $props_to_update = array(); |
|
| 190 | - $changed_props = $object->get_changes(); |
|
| 191 | - |
|
| 192 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 193 | - foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
| 194 | - if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
| 195 | - $props_to_update[ $meta_key ] = $prop; |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $props_to_update; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Read object data. |
|
| 204 | - * |
|
| 205 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 206 | - * @param WP_Post $post_object Post object. |
|
| 207 | - * @since 1.0.19 |
|
| 208 | - */ |
|
| 209 | - protected function read_object_data( &$object, $post_object ) { |
|
| 210 | - $id = $object->get_id(); |
|
| 211 | - $props = array(); |
|
| 212 | - |
|
| 213 | - foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
| 214 | - $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Set object properties. |
|
| 218 | - $object->set_props( $props ); |
|
| 219 | - |
|
| 220 | - // Gets extra data associated with the object if needed. |
|
| 221 | - foreach ( $object->get_extra_data_keys() as $key ) { |
|
| 222 | - $function = 'set_' . $key; |
|
| 223 | - if ( is_callable( array( $object, $function ) ) ) { |
|
| 224 | - $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class. |
|
| 231 | - * |
|
| 232 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 233 | - * @since 1.0.19 |
|
| 234 | - */ |
|
| 235 | - protected function update_post_meta( &$object ) { |
|
| 236 | - |
|
| 237 | - $updated_props = array(); |
|
| 238 | - $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
| 239 | - $object_type = $object->get_object_type(); |
|
| 240 | - |
|
| 241 | - foreach ( $props_to_update as $meta_key => $prop ) { |
|
| 242 | - $value = $object->{"get_$prop"}( 'edit' ); |
|
| 243 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 244 | - |
|
| 245 | - $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
| 246 | - |
|
| 247 | - if ( $updated ) { |
|
| 248 | - $updated_props[] = $prop; |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Update meta data in, or delete it from, the database. |
|
| 257 | - * |
|
| 258 | - * Avoids storing meta when it's either an empty string or empty array or null. |
|
| 259 | - * Other empty values such as numeric 0 should still be stored. |
|
| 260 | - * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
| 261 | - * |
|
| 262 | - * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
| 263 | - * |
|
| 264 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 265 | - * @param string $meta_key Meta key to update. |
|
| 266 | - * @param mixed $meta_value Value to save. |
|
| 267 | - * |
|
| 268 | - * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
| 269 | - * |
|
| 270 | - * @return bool True if updated/deleted. |
|
| 271 | - */ |
|
| 272 | - protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
| 273 | - if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
| 274 | - $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
| 275 | - } else { |
|
| 276 | - $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - return (bool) $updated; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Return list of internal meta keys. |
|
| 284 | - * |
|
| 285 | - * @since 1.0.19 |
|
| 286 | - * @return array |
|
| 287 | - */ |
|
| 288 | - public function get_internal_meta_keys() { |
|
| 289 | - return $this->internal_meta_keys; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Clear any caches. |
|
| 294 | - * |
|
| 295 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 296 | - * @since 1.0.19 |
|
| 297 | - */ |
|
| 298 | - protected function clear_caches( &$object ) { |
|
| 299 | - clean_post_cache( $object->get_id() ); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Method to delete a data object from the database. |
|
| 304 | - * |
|
| 305 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 306 | - * @param array $args Array of args to pass to the delete method. |
|
| 307 | - * |
|
| 308 | - * @return void |
|
| 309 | - */ |
|
| 310 | - public function delete( &$object, $args = array() ) { |
|
| 311 | - $id = $object->get_id(); |
|
| 312 | - $object_type = $object->get_object_type(); |
|
| 313 | - |
|
| 314 | - if ( 'invoice' == $object_type ) { |
|
| 315 | - $object_type = $object->get_type(); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $args = wp_parse_args( |
|
| 319 | - $args, |
|
| 320 | - array( |
|
| 321 | - 'force_delete' => false, |
|
| 322 | - ) |
|
| 323 | - ); |
|
| 324 | - |
|
| 325 | - if ( ! $id ) { |
|
| 326 | - return; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - if ( $args['force_delete'] ) { |
|
| 330 | - do_action( "getpaid_delete_$object_type", $object ); |
|
| 331 | - wp_delete_post( $id, true ); |
|
| 332 | - $object->set_id( 0 ); |
|
| 333 | - } else { |
|
| 334 | - do_action( "getpaid_trash_$object_type", $object ); |
|
| 335 | - wp_trash_post( $id ); |
|
| 336 | - $object->set_status( 'trash' ); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Get the status to save to the post object. |
|
| 342 | - * |
|
| 343 | - * |
|
| 344 | - * @since 1.0.19 |
|
| 345 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 346 | - * @return string |
|
| 347 | - */ |
|
| 348 | - protected function get_post_status( $object ) { |
|
| 349 | - $object_status = $object->get_status( 'edit' ); |
|
| 350 | - $object_type = $object->get_object_type(); |
|
| 351 | - |
|
| 352 | - if ( ! $object_status ) { |
|
| 353 | - $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - return $object_status; |
|
| 357 | - } |
|
| 76 | + $object->get_id() |
|
| 77 | + ) |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
| 81 | + $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
| 82 | + return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Deletes meta based on meta ID. |
|
| 87 | + * |
|
| 88 | + * @since 1.0.19 |
|
| 89 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 90 | + * @param stdClass $meta (containing at least ->id). |
|
| 91 | + */ |
|
| 92 | + public function delete_meta( &$object, $meta ) { |
|
| 93 | + delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Add new piece of meta. |
|
| 98 | + * |
|
| 99 | + * @since 1.0.19 |
|
| 100 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 101 | + * @param stdClass $meta (containing ->key and ->value). |
|
| 102 | + * @return int meta ID |
|
| 103 | + */ |
|
| 104 | + public function add_meta( &$object, $meta ) { |
|
| 105 | + return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Update meta. |
|
| 110 | + * |
|
| 111 | + * @since 1.0.19 |
|
| 112 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 113 | + * @param stdClass $meta (containing ->id, ->key and ->value). |
|
| 114 | + */ |
|
| 115 | + public function update_meta( &$object, $meta ) { |
|
| 116 | + update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Table structure is slightly different between meta types, this function will return what we need to know. |
|
| 121 | + * |
|
| 122 | + * @since 1.0.19 |
|
| 123 | + * @return array Array elements: table, object_id_field, meta_id_field |
|
| 124 | + */ |
|
| 125 | + protected function get_db_info() { |
|
| 126 | + global $wpdb; |
|
| 127 | + |
|
| 128 | + $meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well. |
|
| 129 | + $table = $wpdb->prefix; |
|
| 130 | + |
|
| 131 | + // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
| 132 | + if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
| 133 | + $table .= 'getpaid_'; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $table .= $this->meta_type . 'meta'; |
|
| 137 | + $object_id_field = $this->meta_type . '_id'; |
|
| 138 | + |
|
| 139 | + // Figure out our field names. |
|
| 140 | + if ( 'user' === $this->meta_type ) { |
|
| 141 | + $meta_id_field = 'umeta_id'; |
|
| 142 | + $table = $wpdb->usermeta; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
| 146 | + $object_id_field = $this->object_id_field_for_meta; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return array( |
|
| 150 | + 'table' => $table, |
|
| 151 | + 'object_id_field' => $object_id_field, |
|
| 152 | + 'meta_id_field' => $meta_id_field, |
|
| 153 | + ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
| 158 | + * addition to all data props with _ prefix. |
|
| 159 | + * |
|
| 160 | + * @since 1.0.19 |
|
| 161 | + * |
|
| 162 | + * @param string $key Prefix to be added to meta keys. |
|
| 163 | + * @return string |
|
| 164 | + */ |
|
| 165 | + protected function prefix_key( $key ) { |
|
| 166 | + return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Callback to remove unwanted meta data. |
|
| 171 | + * |
|
| 172 | + * @param object $meta Meta object to check if it should be excluded or not. |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + protected function exclude_internal_meta_keys( $meta ) { |
|
| 176 | + return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Gets a list of props and meta keys that need updated based on change state |
|
| 181 | + * or if they are present in the database or not. |
|
| 182 | + * |
|
| 183 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 184 | + * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
| 185 | + * @param string $meta_type The internal WP meta type (post, user, etc). |
|
| 186 | + * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
| 187 | + */ |
|
| 188 | + protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
| 189 | + $props_to_update = array(); |
|
| 190 | + $changed_props = $object->get_changes(); |
|
| 191 | + |
|
| 192 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 193 | + foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
| 194 | + if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
| 195 | + $props_to_update[ $meta_key ] = $prop; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $props_to_update; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Read object data. |
|
| 204 | + * |
|
| 205 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 206 | + * @param WP_Post $post_object Post object. |
|
| 207 | + * @since 1.0.19 |
|
| 208 | + */ |
|
| 209 | + protected function read_object_data( &$object, $post_object ) { |
|
| 210 | + $id = $object->get_id(); |
|
| 211 | + $props = array(); |
|
| 212 | + |
|
| 213 | + foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
| 214 | + $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Set object properties. |
|
| 218 | + $object->set_props( $props ); |
|
| 219 | + |
|
| 220 | + // Gets extra data associated with the object if needed. |
|
| 221 | + foreach ( $object->get_extra_data_keys() as $key ) { |
|
| 222 | + $function = 'set_' . $key; |
|
| 223 | + if ( is_callable( array( $object, $function ) ) ) { |
|
| 224 | + $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class. |
|
| 231 | + * |
|
| 232 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 233 | + * @since 1.0.19 |
|
| 234 | + */ |
|
| 235 | + protected function update_post_meta( &$object ) { |
|
| 236 | + |
|
| 237 | + $updated_props = array(); |
|
| 238 | + $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
| 239 | + $object_type = $object->get_object_type(); |
|
| 240 | + |
|
| 241 | + foreach ( $props_to_update as $meta_key => $prop ) { |
|
| 242 | + $value = $object->{"get_$prop"}( 'edit' ); |
|
| 243 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 244 | + |
|
| 245 | + $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
| 246 | + |
|
| 247 | + if ( $updated ) { |
|
| 248 | + $updated_props[] = $prop; |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Update meta data in, or delete it from, the database. |
|
| 257 | + * |
|
| 258 | + * Avoids storing meta when it's either an empty string or empty array or null. |
|
| 259 | + * Other empty values such as numeric 0 should still be stored. |
|
| 260 | + * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
| 261 | + * |
|
| 262 | + * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
| 263 | + * |
|
| 264 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 265 | + * @param string $meta_key Meta key to update. |
|
| 266 | + * @param mixed $meta_value Value to save. |
|
| 267 | + * |
|
| 268 | + * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
| 269 | + * |
|
| 270 | + * @return bool True if updated/deleted. |
|
| 271 | + */ |
|
| 272 | + protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
| 273 | + if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
| 274 | + $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
| 275 | + } else { |
|
| 276 | + $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + return (bool) $updated; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Return list of internal meta keys. |
|
| 284 | + * |
|
| 285 | + * @since 1.0.19 |
|
| 286 | + * @return array |
|
| 287 | + */ |
|
| 288 | + public function get_internal_meta_keys() { |
|
| 289 | + return $this->internal_meta_keys; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Clear any caches. |
|
| 294 | + * |
|
| 295 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 296 | + * @since 1.0.19 |
|
| 297 | + */ |
|
| 298 | + protected function clear_caches( &$object ) { |
|
| 299 | + clean_post_cache( $object->get_id() ); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Method to delete a data object from the database. |
|
| 304 | + * |
|
| 305 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 306 | + * @param array $args Array of args to pass to the delete method. |
|
| 307 | + * |
|
| 308 | + * @return void |
|
| 309 | + */ |
|
| 310 | + public function delete( &$object, $args = array() ) { |
|
| 311 | + $id = $object->get_id(); |
|
| 312 | + $object_type = $object->get_object_type(); |
|
| 313 | + |
|
| 314 | + if ( 'invoice' == $object_type ) { |
|
| 315 | + $object_type = $object->get_type(); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $args = wp_parse_args( |
|
| 319 | + $args, |
|
| 320 | + array( |
|
| 321 | + 'force_delete' => false, |
|
| 322 | + ) |
|
| 323 | + ); |
|
| 324 | + |
|
| 325 | + if ( ! $id ) { |
|
| 326 | + return; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + if ( $args['force_delete'] ) { |
|
| 330 | + do_action( "getpaid_delete_$object_type", $object ); |
|
| 331 | + wp_delete_post( $id, true ); |
|
| 332 | + $object->set_id( 0 ); |
|
| 333 | + } else { |
|
| 334 | + do_action( "getpaid_trash_$object_type", $object ); |
|
| 335 | + wp_trash_post( $id ); |
|
| 336 | + $object->set_status( 'trash' ); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Get the status to save to the post object. |
|
| 342 | + * |
|
| 343 | + * |
|
| 344 | + * @since 1.0.19 |
|
| 345 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 346 | + * @return string |
|
| 347 | + */ |
|
| 348 | + protected function get_post_status( $object ) { |
|
| 349 | + $object_status = $object->get_status( 'edit' ); |
|
| 350 | + $object_type = $object->get_object_type(); |
|
| 351 | + |
|
| 352 | + if ( ! $object_status ) { |
|
| 353 | + $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + return $object_status; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | 359 | } |
@@ -12,119 +12,119 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class WPInv_Notes { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Class constructor. |
|
| 17 | - */ |
|
| 18 | - public function __construct() { |
|
| 19 | - |
|
| 20 | - // Filter inovice notes. |
|
| 21 | - add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
| 22 | - add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
| 23 | - |
|
| 24 | - // Fires after notes are loaded. |
|
| 25 | - do_action( 'wpinv_notes_init', $this ); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Filters invoice notes query to only include our notes. |
|
| 30 | - * |
|
| 31 | - * @param WP_Comment_Query $query |
|
| 32 | - */ |
|
| 33 | - public function set_invoice_note_type( $query ) { |
|
| 34 | - $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
| 35 | - |
|
| 36 | - if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
| 37 | - $query->query_vars['type'] = 'wpinv_note'; |
|
| 38 | - } else { |
|
| 39 | - |
|
| 40 | - if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
| 41 | - $query->query_vars['type__not_in'] = array(); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
| 45 | - $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - return $query; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Exclude notes from the comments feed. |
|
| 53 | - */ |
|
| 54 | - function wpinv_comment_feed_where( $where ){ |
|
| 55 | - return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Returns an array of invoice notes. |
|
| 60 | - * |
|
| 61 | - * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
| 62 | - * @param string $type Optional. Pass in customer to only return customer notes. |
|
| 63 | - * @return WP_Comment[] |
|
| 64 | - */ |
|
| 65 | - public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
| 66 | - |
|
| 67 | - // Default comment args. |
|
| 68 | - $args = array( |
|
| 69 | - 'post_id' => $invoice_id, |
|
| 70 | - 'orderby' => 'comment_ID', |
|
| 71 | - 'order' => 'ASC', |
|
| 72 | - ); |
|
| 73 | - |
|
| 74 | - // Maybe only show customer comments. |
|
| 75 | - if ( $type == 'customer' ) { |
|
| 76 | - $args['meta_key'] = '_wpi_customer_note'; |
|
| 77 | - $args['meta_value'] = 1; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
| 81 | - |
|
| 82 | - return get_comments( $args ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Saves an invoice comment. |
|
| 87 | - * |
|
| 88 | - * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
| 89 | - * @param string $note The note content. |
|
| 90 | - * @param string $note_author The name of the author of the note. |
|
| 91 | - * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
| 92 | - * @return int|false The new note's ID on success, false on failure. |
|
| 93 | - */ |
|
| 94 | - function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
| 95 | - |
|
| 96 | - do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Insert the comment. |
|
| 100 | - */ |
|
| 101 | - $note_id = wp_insert_comment( |
|
| 102 | - wp_filter_comment( |
|
| 103 | - array( |
|
| 104 | - 'comment_post_ID' => $invoice->get_id(), |
|
| 105 | - 'comment_content' => $note, |
|
| 106 | - 'comment_agent' => 'Invoicing', |
|
| 107 | - 'user_id' => get_current_user_id(), |
|
| 108 | - 'comment_author' => $note_author, |
|
| 109 | - 'comment_author_IP' => wpinv_get_ip(), |
|
| 110 | - 'comment_author_email' => $author_email, |
|
| 111 | - 'comment_author_url' => $invoice->get_view_url(), |
|
| 112 | - 'comment_type' => 'wpinv_note', |
|
| 113 | - ) |
|
| 114 | - ) |
|
| 115 | - ); |
|
| 116 | - |
|
| 117 | - do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
| 118 | - |
|
| 119 | - // Are we notifying the customer? |
|
| 120 | - if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
| 121 | - return $note_id; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
| 125 | - do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
| 126 | - do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
| 127 | - return $note_id; |
|
| 128 | - } |
|
| 15 | + /** |
|
| 16 | + * Class constructor. |
|
| 17 | + */ |
|
| 18 | + public function __construct() { |
|
| 19 | + |
|
| 20 | + // Filter inovice notes. |
|
| 21 | + add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
| 22 | + add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
| 23 | + |
|
| 24 | + // Fires after notes are loaded. |
|
| 25 | + do_action( 'wpinv_notes_init', $this ); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Filters invoice notes query to only include our notes. |
|
| 30 | + * |
|
| 31 | + * @param WP_Comment_Query $query |
|
| 32 | + */ |
|
| 33 | + public function set_invoice_note_type( $query ) { |
|
| 34 | + $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
| 35 | + |
|
| 36 | + if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
| 37 | + $query->query_vars['type'] = 'wpinv_note'; |
|
| 38 | + } else { |
|
| 39 | + |
|
| 40 | + if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
| 41 | + $query->query_vars['type__not_in'] = array(); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
| 45 | + $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + return $query; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Exclude notes from the comments feed. |
|
| 53 | + */ |
|
| 54 | + function wpinv_comment_feed_where( $where ){ |
|
| 55 | + return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Returns an array of invoice notes. |
|
| 60 | + * |
|
| 61 | + * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
| 62 | + * @param string $type Optional. Pass in customer to only return customer notes. |
|
| 63 | + * @return WP_Comment[] |
|
| 64 | + */ |
|
| 65 | + public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
| 66 | + |
|
| 67 | + // Default comment args. |
|
| 68 | + $args = array( |
|
| 69 | + 'post_id' => $invoice_id, |
|
| 70 | + 'orderby' => 'comment_ID', |
|
| 71 | + 'order' => 'ASC', |
|
| 72 | + ); |
|
| 73 | + |
|
| 74 | + // Maybe only show customer comments. |
|
| 75 | + if ( $type == 'customer' ) { |
|
| 76 | + $args['meta_key'] = '_wpi_customer_note'; |
|
| 77 | + $args['meta_value'] = 1; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
| 81 | + |
|
| 82 | + return get_comments( $args ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Saves an invoice comment. |
|
| 87 | + * |
|
| 88 | + * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
| 89 | + * @param string $note The note content. |
|
| 90 | + * @param string $note_author The name of the author of the note. |
|
| 91 | + * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
| 92 | + * @return int|false The new note's ID on success, false on failure. |
|
| 93 | + */ |
|
| 94 | + function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
| 95 | + |
|
| 96 | + do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Insert the comment. |
|
| 100 | + */ |
|
| 101 | + $note_id = wp_insert_comment( |
|
| 102 | + wp_filter_comment( |
|
| 103 | + array( |
|
| 104 | + 'comment_post_ID' => $invoice->get_id(), |
|
| 105 | + 'comment_content' => $note, |
|
| 106 | + 'comment_agent' => 'Invoicing', |
|
| 107 | + 'user_id' => get_current_user_id(), |
|
| 108 | + 'comment_author' => $note_author, |
|
| 109 | + 'comment_author_IP' => wpinv_get_ip(), |
|
| 110 | + 'comment_author_email' => $author_email, |
|
| 111 | + 'comment_author_url' => $invoice->get_view_url(), |
|
| 112 | + 'comment_type' => 'wpinv_note', |
|
| 113 | + ) |
|
| 114 | + ) |
|
| 115 | + ); |
|
| 116 | + |
|
| 117 | + do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
| 118 | + |
|
| 119 | + // Are we notifying the customer? |
|
| 120 | + if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
| 121 | + return $note_id; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
| 125 | + do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
| 126 | + do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
| 127 | + return $note_id; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | } |
@@ -196,11 +196,11 @@ discard block |
||
| 196 | 196 | $cb = "wpinv_{$option['type']}_callback"; |
| 197 | 197 | $section = "wpinv_settings_{$tab}_$section"; |
| 198 | 198 | |
| 199 | - if ( isset( $option['desc'] ) && ! empty( $option['help-tip'] ) ) { |
|
| 200 | - $tip = esc_attr( $option['desc'] ); |
|
| 201 | - $name .= "<span class='dashicons dashicons-editor-help wpi-help-tip' title='$tip'></span>"; |
|
| 202 | - unset( $option['desc'] ); |
|
| 203 | - } |
|
| 199 | + if ( isset( $option['desc'] ) && ! empty( $option['help-tip'] ) ) { |
|
| 200 | + $tip = esc_attr( $option['desc'] ); |
|
| 201 | + $name .= "<span class='dashicons dashicons-editor-help wpi-help-tip' title='$tip'></span>"; |
|
| 202 | + unset( $option['desc'] ); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | 205 | // Loop through all tabs. |
| 206 | 206 | add_settings_field( |
@@ -425,333 +425,333 @@ discard block |
||
| 425 | 425 | } |
| 426 | 426 | |
| 427 | 427 | function wpinv_get_pages( $with_slug = false, $default_label = NULL ) { |
| 428 | - $pages_options = array(); |
|
| 428 | + $pages_options = array(); |
|
| 429 | 429 | |
| 430 | - if( $default_label !== NULL && $default_label !== false ) { |
|
| 431 | - $pages_options = array( '' => $default_label ); // Blank option |
|
| 432 | - } |
|
| 430 | + if( $default_label !== NULL && $default_label !== false ) { |
|
| 431 | + $pages_options = array( '' => $default_label ); // Blank option |
|
| 432 | + } |
|
| 433 | 433 | |
| 434 | - $pages = get_pages(); |
|
| 435 | - if ( $pages ) { |
|
| 436 | - foreach ( $pages as $page ) { |
|
| 437 | - $title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title; |
|
| 434 | + $pages = get_pages(); |
|
| 435 | + if ( $pages ) { |
|
| 436 | + foreach ( $pages as $page ) { |
|
| 437 | + $title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title; |
|
| 438 | 438 | $pages_options[ $page->ID ] = $title; |
| 439 | - } |
|
| 440 | - } |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | 441 | |
| 442 | - return $pages_options; |
|
| 442 | + return $pages_options; |
|
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | function wpinv_header_callback( $args ) { |
| 446 | - if ( !empty( $args['desc'] ) ) { |
|
| 446 | + if ( !empty( $args['desc'] ) ) { |
|
| 447 | 447 | echo $args['desc']; |
| 448 | 448 | } |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | function wpinv_hidden_callback( $args ) { |
| 452 | - global $wpinv_options; |
|
| 453 | - |
|
| 454 | - if ( isset( $args['set_value'] ) ) { |
|
| 455 | - $value = $args['set_value']; |
|
| 456 | - } elseif ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 457 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 458 | - } else { |
|
| 459 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 463 | - $args['readonly'] = true; |
|
| 464 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 465 | - $name = ''; |
|
| 466 | - } else { |
|
| 467 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
| 452 | + global $wpinv_options; |
|
| 453 | + |
|
| 454 | + if ( isset( $args['set_value'] ) ) { |
|
| 455 | + $value = $args['set_value']; |
|
| 456 | + } elseif ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 457 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 458 | + } else { |
|
| 459 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 463 | + $args['readonly'] = true; |
|
| 464 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 465 | + $name = ''; |
|
| 466 | + } else { |
|
| 467 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
| 471 | 471 | |
| 472 | - echo $html; |
|
| 472 | + echo $html; |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | function wpinv_checkbox_callback( $args ) { |
| 476 | - global $wpinv_options; |
|
| 476 | + global $wpinv_options; |
|
| 477 | 477 | |
| 478 | 478 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 479 | 479 | |
| 480 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 481 | - $name = ''; |
|
| 482 | - } else { |
|
| 483 | - $name = 'name="wpinv_settings[' . $sanitize_id . ']"'; |
|
| 484 | - } |
|
| 480 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 481 | + $name = ''; |
|
| 482 | + } else { |
|
| 483 | + $name = 'name="wpinv_settings[' . $sanitize_id . ']"'; |
|
| 484 | + } |
|
| 485 | 485 | |
| 486 | - $std = isset( $args['std'] ) ? $args['std'] : 0; |
|
| 487 | - $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 488 | - $checked = checked( empty( $value ), false, false ); |
|
| 486 | + $std = isset( $args['std'] ) ? $args['std'] : 0; |
|
| 487 | + $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 488 | + $checked = checked( empty( $value ), false, false ); |
|
| 489 | 489 | |
| 490 | - $html = '<input type="checkbox" id="wpinv_settings[' . $sanitize_id . ']"' . $name . ' value="1" ' . $checked . '/>'; |
|
| 491 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 490 | + $html = '<input type="checkbox" id="wpinv_settings[' . $sanitize_id . ']"' . $name . ' value="1" ' . $checked . '/>'; |
|
| 491 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 492 | 492 | |
| 493 | - echo $html; |
|
| 493 | + echo $html; |
|
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | function wpinv_multicheck_callback( $args ) { |
| 497 | - global $wpinv_options; |
|
| 497 | + global $wpinv_options; |
|
| 498 | 498 | |
| 499 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 500 | - $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
| 499 | + $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 500 | + $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
| 501 | 501 | |
| 502 | - if ( ! empty( $args['options'] ) ) { |
|
| 502 | + if ( ! empty( $args['options'] ) ) { |
|
| 503 | 503 | |
| 504 | - $std = isset( $args['std'] ) ? $args['std'] : array(); |
|
| 505 | - $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 504 | + $std = isset( $args['std'] ) ? $args['std'] : array(); |
|
| 505 | + $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 506 | 506 | |
| 507 | - echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">'; |
|
| 507 | + echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">'; |
|
| 508 | 508 | foreach( $args['options'] as $key => $option ): |
| 509 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 510 | - if ( in_array( $sanitize_key, $value ) ) { |
|
| 511 | - $enabled = $sanitize_key; |
|
| 512 | - } else { |
|
| 513 | - $enabled = NULL; |
|
| 514 | - } |
|
| 515 | - echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/> '; |
|
| 516 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>'; |
|
| 517 | - endforeach; |
|
| 518 | - echo '</div>'; |
|
| 519 | - echo '<p class="description">' . $args['desc'] . '</p>'; |
|
| 520 | - } |
|
| 509 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 510 | + if ( in_array( $sanitize_key, $value ) ) { |
|
| 511 | + $enabled = $sanitize_key; |
|
| 512 | + } else { |
|
| 513 | + $enabled = NULL; |
|
| 514 | + } |
|
| 515 | + echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/> '; |
|
| 516 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>'; |
|
| 517 | + endforeach; |
|
| 518 | + echo '</div>'; |
|
| 519 | + echo '<p class="description">' . $args['desc'] . '</p>'; |
|
| 520 | + } |
|
| 521 | 521 | } |
| 522 | 522 | |
| 523 | 523 | function wpinv_payment_icons_callback( $args ) { |
| 524 | - global $wpinv_options; |
|
| 524 | + global $wpinv_options; |
|
| 525 | 525 | |
| 526 | 526 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 527 | 527 | |
| 528 | - if ( ! empty( $args['options'] ) ) { |
|
| 529 | - foreach( $args['options'] as $key => $option ) { |
|
| 528 | + if ( ! empty( $args['options'] ) ) { |
|
| 529 | + foreach( $args['options'] as $key => $option ) { |
|
| 530 | 530 | $sanitize_key = wpinv_sanitize_key( $key ); |
| 531 | 531 | |
| 532 | - if( isset( $wpinv_options[$args['id']][$key] ) ) { |
|
| 533 | - $enabled = $option; |
|
| 534 | - } else { |
|
| 535 | - $enabled = NULL; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">'; |
|
| 539 | - |
|
| 540 | - echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/> '; |
|
| 541 | - |
|
| 542 | - if ( wpinv_string_is_image_url( $key ) ) { |
|
| 543 | - echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 544 | - } else { |
|
| 545 | - $card = strtolower( str_replace( ' ', '', $option ) ); |
|
| 546 | - |
|
| 547 | - if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) { |
|
| 548 | - $image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' ); |
|
| 549 | - } else { |
|
| 550 | - $image = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false ); |
|
| 551 | - $content_dir = WP_CONTENT_DIR; |
|
| 552 | - |
|
| 553 | - if ( function_exists( 'wp_normalize_path' ) ) { |
|
| 554 | - // Replaces backslashes with forward slashes for Windows systems |
|
| 555 | - $image = wp_normalize_path( $image ); |
|
| 556 | - $content_dir = wp_normalize_path( $content_dir ); |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - $image = str_replace( $content_dir, content_url(), $image ); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 563 | - } |
|
| 564 | - echo $option . '</label>'; |
|
| 565 | - } |
|
| 566 | - echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 567 | - } |
|
| 532 | + if( isset( $wpinv_options[$args['id']][$key] ) ) { |
|
| 533 | + $enabled = $option; |
|
| 534 | + } else { |
|
| 535 | + $enabled = NULL; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">'; |
|
| 539 | + |
|
| 540 | + echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/> '; |
|
| 541 | + |
|
| 542 | + if ( wpinv_string_is_image_url( $key ) ) { |
|
| 543 | + echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 544 | + } else { |
|
| 545 | + $card = strtolower( str_replace( ' ', '', $option ) ); |
|
| 546 | + |
|
| 547 | + if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) { |
|
| 548 | + $image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' ); |
|
| 549 | + } else { |
|
| 550 | + $image = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false ); |
|
| 551 | + $content_dir = WP_CONTENT_DIR; |
|
| 552 | + |
|
| 553 | + if ( function_exists( 'wp_normalize_path' ) ) { |
|
| 554 | + // Replaces backslashes with forward slashes for Windows systems |
|
| 555 | + $image = wp_normalize_path( $image ); |
|
| 556 | + $content_dir = wp_normalize_path( $content_dir ); |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + $image = str_replace( $content_dir, content_url(), $image ); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 563 | + } |
|
| 564 | + echo $option . '</label>'; |
|
| 565 | + } |
|
| 566 | + echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 567 | + } |
|
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | function wpinv_radio_callback( $args ) { |
| 571 | - global $wpinv_options; |
|
| 571 | + global $wpinv_options; |
|
| 572 | 572 | |
| 573 | 573 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 574 | 574 | |
| 575 | 575 | foreach ( $args['options'] as $key => $option ) : |
| 576 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 576 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 577 | 577 | |
| 578 | 578 | $checked = false; |
| 579 | 579 | |
| 580 | - if ( isset( $wpinv_options[ $args['id'] ] ) && $wpinv_options[ $args['id'] ] == $key ) |
|
| 581 | - $checked = true; |
|
| 582 | - elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wpinv_options[ $args['id'] ] ) ) |
|
| 583 | - $checked = true; |
|
| 580 | + if ( isset( $wpinv_options[ $args['id'] ] ) && $wpinv_options[ $args['id'] ] == $key ) |
|
| 581 | + $checked = true; |
|
| 582 | + elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wpinv_options[ $args['id'] ] ) ) |
|
| 583 | + $checked = true; |
|
| 584 | 584 | |
| 585 | - echo '<input name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="radio" value="' . $sanitize_key . '" ' . checked(true, $checked, false) . '/> '; |
|
| 586 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option ) . '</label><br/>'; |
|
| 587 | - endforeach; |
|
| 585 | + echo '<input name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="radio" value="' . $sanitize_key . '" ' . checked(true, $checked, false) . '/> '; |
|
| 586 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option ) . '</label><br/>'; |
|
| 587 | + endforeach; |
|
| 588 | 588 | |
| 589 | - echo '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 589 | + echo '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function wpinv_gateways_callback( $args ) { |
| 593 | - global $wpinv_options; |
|
| 593 | + global $wpinv_options; |
|
| 594 | 594 | |
| 595 | 595 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 596 | 596 | |
| 597 | - foreach ( $args['options'] as $key => $option ) : |
|
| 598 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 597 | + foreach ( $args['options'] as $key => $option ) : |
|
| 598 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 599 | 599 | |
| 600 | 600 | if ( isset( $wpinv_options['gateways'][ $key ] ) ) |
| 601 | - $enabled = '1'; |
|
| 602 | - else |
|
| 603 | - $enabled = null; |
|
| 601 | + $enabled = '1'; |
|
| 602 | + else |
|
| 603 | + $enabled = null; |
|
| 604 | 604 | |
| 605 | - echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
| 606 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>'; |
|
| 607 | - endforeach; |
|
| 605 | + echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
| 606 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>'; |
|
| 607 | + endforeach; |
|
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | function wpinv_gateway_select_callback($args) { |
| 611 | - global $wpinv_options; |
|
| 611 | + global $wpinv_options; |
|
| 612 | 612 | |
| 613 | 613 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 614 | 614 | $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
| 615 | 615 | |
| 616 | - echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >'; |
|
| 616 | + echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >'; |
|
| 617 | 617 | |
| 618 | - foreach ( $args['options'] as $key => $option ) : |
|
| 619 | - if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
|
| 618 | + foreach ( $args['options'] as $key => $option ) : |
|
| 619 | + if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
|
| 620 | 620 | $selected = selected( $key, $args['selected'], false ); |
| 621 | 621 | } else { |
| 622 | 622 | $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $key, $wpinv_options[$args['id']], false ) : ''; |
| 623 | 623 | } |
| 624 | - echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>'; |
|
| 625 | - endforeach; |
|
| 624 | + echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>'; |
|
| 625 | + endforeach; |
|
| 626 | 626 | |
| 627 | - echo '</select>'; |
|
| 628 | - echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 627 | + echo '</select>'; |
|
| 628 | + echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 629 | 629 | } |
| 630 | 630 | |
| 631 | 631 | function wpinv_text_callback( $args ) { |
| 632 | - global $wpinv_options; |
|
| 632 | + global $wpinv_options; |
|
| 633 | 633 | |
| 634 | 634 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 635 | 635 | |
| 636 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 637 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 638 | - } else { |
|
| 639 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 643 | - $args['readonly'] = true; |
|
| 644 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 645 | - $name = ''; |
|
| 646 | - } else { |
|
| 647 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 648 | - } |
|
| 649 | - $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 650 | - |
|
| 651 | - $readonly = $args['readonly'] === true ? ' readonly="readonly"' : ''; |
|
| 652 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 653 | - $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>'; |
|
| 654 | - $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 655 | - |
|
| 656 | - echo $html; |
|
| 636 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 637 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 638 | + } else { |
|
| 639 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 643 | + $args['readonly'] = true; |
|
| 644 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 645 | + $name = ''; |
|
| 646 | + } else { |
|
| 647 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 648 | + } |
|
| 649 | + $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 650 | + |
|
| 651 | + $readonly = $args['readonly'] === true ? ' readonly="readonly"' : ''; |
|
| 652 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 653 | + $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>'; |
|
| 654 | + $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 655 | + |
|
| 656 | + echo $html; |
|
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | function wpinv_number_callback( $args ) { |
| 660 | - global $wpinv_options; |
|
| 660 | + global $wpinv_options; |
|
| 661 | 661 | |
| 662 | 662 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 663 | 663 | |
| 664 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 665 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 666 | - } else { |
|
| 667 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 671 | - $args['readonly'] = true; |
|
| 672 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 673 | - $name = ''; |
|
| 674 | - } else { |
|
| 675 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - $max = isset( $args['max'] ) ? $args['max'] : 999999; |
|
| 679 | - $min = isset( $args['min'] ) ? $args['min'] : 0; |
|
| 680 | - $step = isset( $args['step'] ) ? $args['step'] : 1; |
|
| 681 | - $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 682 | - |
|
| 683 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 684 | - $html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 685 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 686 | - |
|
| 687 | - echo $html; |
|
| 664 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 665 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 666 | + } else { |
|
| 667 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 671 | + $args['readonly'] = true; |
|
| 672 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 673 | + $name = ''; |
|
| 674 | + } else { |
|
| 675 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + $max = isset( $args['max'] ) ? $args['max'] : 999999; |
|
| 679 | + $min = isset( $args['min'] ) ? $args['min'] : 0; |
|
| 680 | + $step = isset( $args['step'] ) ? $args['step'] : 1; |
|
| 681 | + $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 682 | + |
|
| 683 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 684 | + $html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 685 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 686 | + |
|
| 687 | + echo $html; |
|
| 688 | 688 | } |
| 689 | 689 | |
| 690 | 690 | function wpinv_textarea_callback( $args ) { |
| 691 | - global $wpinv_options; |
|
| 691 | + global $wpinv_options; |
|
| 692 | 692 | |
| 693 | 693 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 694 | 694 | |
| 695 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 696 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 697 | - } else { |
|
| 698 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 699 | - } |
|
| 695 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 696 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 697 | + } else { |
|
| 698 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 699 | + } |
|
| 700 | 700 | |
| 701 | 701 | $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
| 702 | 702 | $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text'; |
| 703 | 703 | |
| 704 | - $html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 705 | - $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 704 | + $html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 705 | + $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 706 | 706 | |
| 707 | - echo $html; |
|
| 707 | + echo $html; |
|
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | function wpinv_password_callback( $args ) { |
| 711 | - global $wpinv_options; |
|
| 711 | + global $wpinv_options; |
|
| 712 | 712 | |
| 713 | 713 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 714 | 714 | |
| 715 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 716 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 717 | - } else { |
|
| 718 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 719 | - } |
|
| 715 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 716 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 717 | + } else { |
|
| 718 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 719 | + } |
|
| 720 | 720 | |
| 721 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 722 | - $html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>'; |
|
| 723 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 721 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 722 | + $html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>'; |
|
| 723 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 724 | 724 | |
| 725 | - echo $html; |
|
| 725 | + echo $html; |
|
| 726 | 726 | } |
| 727 | 727 | |
| 728 | 728 | function wpinv_missing_callback($args) { |
| 729 | - printf( |
|
| 730 | - __( 'The callback function used for the %s setting is missing.', 'invoicing' ), |
|
| 731 | - '<strong>' . $args['id'] . '</strong>' |
|
| 732 | - ); |
|
| 729 | + printf( |
|
| 730 | + __( 'The callback function used for the %s setting is missing.', 'invoicing' ), |
|
| 731 | + '<strong>' . $args['id'] . '</strong>' |
|
| 732 | + ); |
|
| 733 | 733 | } |
| 734 | 734 | |
| 735 | 735 | function wpinv_select_callback($args) { |
| 736 | - global $wpinv_options; |
|
| 736 | + global $wpinv_options; |
|
| 737 | 737 | |
| 738 | 738 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 739 | 739 | |
| 740 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 741 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 742 | - } else { |
|
| 743 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 744 | - } |
|
| 740 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 741 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 742 | + } else { |
|
| 743 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 744 | + } |
|
| 745 | 745 | |
| 746 | 746 | if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
| 747 | 747 | $value = $args['selected']; |
| 748 | 748 | } |
| 749 | 749 | |
| 750 | - if ( isset( $args['placeholder'] ) ) { |
|
| 751 | - $placeholder = $args['placeholder']; |
|
| 752 | - } else { |
|
| 753 | - $placeholder = ''; |
|
| 754 | - } |
|
| 750 | + if ( isset( $args['placeholder'] ) ) { |
|
| 751 | + $placeholder = $args['placeholder']; |
|
| 752 | + } else { |
|
| 753 | + $placeholder = ''; |
|
| 754 | + } |
|
| 755 | 755 | |
| 756 | 756 | if( !empty( $args['onchange'] ) ) { |
| 757 | 757 | $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"'; |
@@ -761,143 +761,143 @@ discard block |
||
| 761 | 761 | |
| 762 | 762 | $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
| 763 | 763 | |
| 764 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />'; |
|
| 764 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />'; |
|
| 765 | 765 | |
| 766 | - foreach ( $args['options'] as $option => $name ) { |
|
| 767 | - $selected = selected( $option, $value, false ); |
|
| 768 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 769 | - } |
|
| 766 | + foreach ( $args['options'] as $option => $name ) { |
|
| 767 | + $selected = selected( $option, $value, false ); |
|
| 768 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 769 | + } |
|
| 770 | 770 | |
| 771 | - $html .= '</select>'; |
|
| 772 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 771 | + $html .= '</select>'; |
|
| 772 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 773 | 773 | |
| 774 | - echo $html; |
|
| 774 | + echo $html; |
|
| 775 | 775 | } |
| 776 | 776 | |
| 777 | 777 | function wpinv_color_select_callback( $args ) { |
| 778 | - global $wpinv_options; |
|
| 778 | + global $wpinv_options; |
|
| 779 | 779 | |
| 780 | 780 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 781 | 781 | |
| 782 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 783 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 784 | - } else { |
|
| 785 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 786 | - } |
|
| 782 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 783 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 784 | + } else { |
|
| 785 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 786 | + } |
|
| 787 | 787 | |
| 788 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>'; |
|
| 788 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>'; |
|
| 789 | 789 | |
| 790 | - foreach ( $args['options'] as $option => $color ) { |
|
| 791 | - $selected = selected( $option, $value, false ); |
|
| 792 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>'; |
|
| 793 | - } |
|
| 790 | + foreach ( $args['options'] as $option => $color ) { |
|
| 791 | + $selected = selected( $option, $value, false ); |
|
| 792 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>'; |
|
| 793 | + } |
|
| 794 | 794 | |
| 795 | - $html .= '</select>'; |
|
| 796 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 795 | + $html .= '</select>'; |
|
| 796 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 797 | 797 | |
| 798 | - echo $html; |
|
| 798 | + echo $html; |
|
| 799 | 799 | } |
| 800 | 800 | |
| 801 | 801 | function wpinv_rich_editor_callback( $args ) { |
| 802 | - global $wpinv_options, $wp_version; |
|
| 802 | + global $wpinv_options, $wp_version; |
|
| 803 | 803 | |
| 804 | 804 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 805 | 805 | |
| 806 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 807 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 806 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 807 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 808 | 808 | |
| 809 | - if( empty( $args['allow_blank'] ) && empty( $value ) ) { |
|
| 810 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 811 | - } |
|
| 812 | - } else { |
|
| 813 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 814 | - } |
|
| 809 | + if( empty( $args['allow_blank'] ) && empty( $value ) ) { |
|
| 810 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 811 | + } |
|
| 812 | + } else { |
|
| 813 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 814 | + } |
|
| 815 | 815 | |
| 816 | - $rows = isset( $args['size'] ) ? $args['size'] : 20; |
|
| 816 | + $rows = isset( $args['size'] ) ? $args['size'] : 20; |
|
| 817 | 817 | |
| 818 | - $html = '<div class="getpaid-settings-editor-input">'; |
|
| 819 | - if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) { |
|
| 820 | - ob_start(); |
|
| 821 | - wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) ); |
|
| 822 | - $html .= ob_get_clean(); |
|
| 823 | - } else { |
|
| 824 | - $html .= '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 825 | - } |
|
| 818 | + $html = '<div class="getpaid-settings-editor-input">'; |
|
| 819 | + if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) { |
|
| 820 | + ob_start(); |
|
| 821 | + wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) ); |
|
| 822 | + $html .= ob_get_clean(); |
|
| 823 | + } else { |
|
| 824 | + $html .= '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 825 | + } |
|
| 826 | 826 | |
| 827 | - $html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 827 | + $html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 828 | 828 | |
| 829 | - echo $html; |
|
| 829 | + echo $html; |
|
| 830 | 830 | } |
| 831 | 831 | |
| 832 | 832 | function wpinv_upload_callback( $args ) { |
| 833 | - global $wpinv_options; |
|
| 833 | + global $wpinv_options; |
|
| 834 | 834 | |
| 835 | 835 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 836 | 836 | |
| 837 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 838 | - $value = $wpinv_options[$args['id']]; |
|
| 839 | - } else { |
|
| 840 | - $value = isset($args['std']) ? $args['std'] : ''; |
|
| 841 | - } |
|
| 837 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 838 | + $value = $wpinv_options[$args['id']]; |
|
| 839 | + } else { |
|
| 840 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 841 | + } |
|
| 842 | 842 | |
| 843 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 844 | - $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 845 | - $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>'; |
|
| 846 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 843 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 844 | + $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 845 | + $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>'; |
|
| 846 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 847 | 847 | |
| 848 | - echo $html; |
|
| 848 | + echo $html; |
|
| 849 | 849 | } |
| 850 | 850 | |
| 851 | 851 | function wpinv_color_callback( $args ) { |
| 852 | - global $wpinv_options; |
|
| 852 | + global $wpinv_options; |
|
| 853 | 853 | |
| 854 | 854 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 855 | 855 | |
| 856 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 857 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 858 | - } else { |
|
| 859 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 860 | - } |
|
| 856 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 857 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 858 | + } else { |
|
| 859 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 860 | + } |
|
| 861 | 861 | |
| 862 | - $default = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 862 | + $default = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 863 | 863 | |
| 864 | - $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />'; |
|
| 865 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 864 | + $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />'; |
|
| 865 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 866 | 866 | |
| 867 | - echo $html; |
|
| 867 | + echo $html; |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | function wpinv_country_states_callback($args) { |
| 871 | - global $wpinv_options; |
|
| 871 | + global $wpinv_options; |
|
| 872 | 872 | |
| 873 | 873 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 874 | 874 | |
| 875 | - if ( isset( $args['placeholder'] ) ) { |
|
| 876 | - $placeholder = $args['placeholder']; |
|
| 877 | - } else { |
|
| 878 | - $placeholder = ''; |
|
| 879 | - } |
|
| 875 | + if ( isset( $args['placeholder'] ) ) { |
|
| 876 | + $placeholder = $args['placeholder']; |
|
| 877 | + } else { |
|
| 878 | + $placeholder = ''; |
|
| 879 | + } |
|
| 880 | 880 | |
| 881 | - $states = wpinv_get_country_states(); |
|
| 881 | + $states = wpinv_get_country_states(); |
|
| 882 | 882 | |
| 883 | - $class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
| 884 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>'; |
|
| 883 | + $class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
| 884 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>'; |
|
| 885 | 885 | |
| 886 | - foreach ( $states as $option => $name ) { |
|
| 887 | - $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : ''; |
|
| 888 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 889 | - } |
|
| 886 | + foreach ( $states as $option => $name ) { |
|
| 887 | + $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : ''; |
|
| 888 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 889 | + } |
|
| 890 | 890 | |
| 891 | - $html .= '</select>'; |
|
| 892 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 891 | + $html .= '</select>'; |
|
| 892 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 893 | 893 | |
| 894 | - echo $html; |
|
| 894 | + echo $html; |
|
| 895 | 895 | } |
| 896 | 896 | |
| 897 | 897 | function wpinv_tax_rates_callback($args) { |
| 898 | - global $wpinv_options; |
|
| 899 | - $rates = wpinv_get_tax_rates(); |
|
| 900 | - ob_start(); ?> |
|
| 898 | + global $wpinv_options; |
|
| 899 | + $rates = wpinv_get_tax_rates(); |
|
| 900 | + ob_start(); ?> |
|
| 901 | 901 | </td><tr> |
| 902 | 902 | <td colspan="2" class="wpinv_tax_tdbox"> |
| 903 | 903 | <p><?php echo $args['desc']; ?></p> |
@@ -921,40 +921,40 @@ discard block |
||
| 921 | 921 | <tr> |
| 922 | 922 | <td class="wpinv_tax_country"> |
| 923 | 923 | <?php |
| 924 | - echo wpinv_html_select( array( |
|
| 925 | - 'options' => wpinv_get_country_list( true ), |
|
| 926 | - 'name' => 'tax_rates[' . $sanitized_key . '][country]', |
|
| 924 | + echo wpinv_html_select( array( |
|
| 925 | + 'options' => wpinv_get_country_list( true ), |
|
| 926 | + 'name' => 'tax_rates[' . $sanitized_key . '][country]', |
|
| 927 | 927 | 'id' => 'tax_rates[' . $sanitized_key . '][country]', |
| 928 | - 'selected' => $rate['country'], |
|
| 929 | - 'show_option_all' => false, |
|
| 930 | - 'show_option_none' => false, |
|
| 931 | - 'class' => 'wpinv-tax-country wpi_select2', |
|
| 932 | - 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 933 | - ) ); |
|
| 934 | - ?> |
|
| 928 | + 'selected' => $rate['country'], |
|
| 929 | + 'show_option_all' => false, |
|
| 930 | + 'show_option_none' => false, |
|
| 931 | + 'class' => 'wpinv-tax-country wpi_select2', |
|
| 932 | + 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 933 | + ) ); |
|
| 934 | + ?> |
|
| 935 | 935 | </td> |
| 936 | 936 | <td class="wpinv_tax_state"> |
| 937 | 937 | <?php |
| 938 | - $states = wpinv_get_country_states( $rate['country'] ); |
|
| 939 | - if( !empty( $states ) ) { |
|
| 940 | - echo wpinv_html_select( array( |
|
| 941 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
| 942 | - 'name' => 'tax_rates[' . $sanitized_key . '][state]', |
|
| 938 | + $states = wpinv_get_country_states( $rate['country'] ); |
|
| 939 | + if( !empty( $states ) ) { |
|
| 940 | + echo wpinv_html_select( array( |
|
| 941 | + 'options' => array_merge( array( '' => '' ), $states ), |
|
| 942 | + 'name' => 'tax_rates[' . $sanitized_key . '][state]', |
|
| 943 | 943 | 'id' => 'tax_rates[' . $sanitized_key . '][state]', |
| 944 | - 'selected' => $rate['state'], |
|
| 945 | - 'show_option_all' => false, |
|
| 946 | - 'show_option_none' => false, |
|
| 944 | + 'selected' => $rate['state'], |
|
| 945 | + 'show_option_all' => false, |
|
| 946 | + 'show_option_none' => false, |
|
| 947 | 947 | 'class' => 'wpi_select2', |
| 948 | - 'placeholder' => __( 'Choose a state', 'invoicing' ) |
|
| 949 | - ) ); |
|
| 950 | - } else { |
|
| 951 | - echo wpinv_html_text( array( |
|
| 952 | - 'name' => 'tax_rates[' . $sanitized_key . '][state]', $rate['state'], |
|
| 953 | - 'value' => ! empty( $rate['state'] ) ? $rate['state'] : '', |
|
| 948 | + 'placeholder' => __( 'Choose a state', 'invoicing' ) |
|
| 949 | + ) ); |
|
| 950 | + } else { |
|
| 951 | + echo wpinv_html_text( array( |
|
| 952 | + 'name' => 'tax_rates[' . $sanitized_key . '][state]', $rate['state'], |
|
| 953 | + 'value' => ! empty( $rate['state'] ) ? $rate['state'] : '', |
|
| 954 | 954 | 'id' => 'tax_rates[' . $sanitized_key . '][state]', |
| 955 | - ) ); |
|
| 956 | - } |
|
| 957 | - ?> |
|
| 955 | + ) ); |
|
| 956 | + } |
|
| 957 | + ?> |
|
| 958 | 958 | </td> |
| 959 | 959 | <td class="wpinv_tax_global"> |
| 960 | 960 | <input type="checkbox" name="tax_rates[<?php echo $sanitized_key; ?>][global]" id="tax_rates[<?php echo $sanitized_key; ?>][global]" value="1"<?php checked( true, ! empty( $rate['global'] ) ); ?>/> |
@@ -969,19 +969,19 @@ discard block |
||
| 969 | 969 | <tr> |
| 970 | 970 | <td class="wpinv_tax_country"> |
| 971 | 971 | <?php |
| 972 | - echo wpinv_html_select( array( |
|
| 973 | - 'options' => wpinv_get_country_list( true ), |
|
| 974 | - 'name' => 'tax_rates[0][country]', |
|
| 975 | - 'show_option_all' => false, |
|
| 976 | - 'show_option_none' => false, |
|
| 977 | - 'class' => 'wpinv-tax-country wpi_select2', |
|
| 978 | - 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 979 | - ) ); ?> |
|
| 972 | + echo wpinv_html_select( array( |
|
| 973 | + 'options' => wpinv_get_country_list( true ), |
|
| 974 | + 'name' => 'tax_rates[0][country]', |
|
| 975 | + 'show_option_all' => false, |
|
| 976 | + 'show_option_none' => false, |
|
| 977 | + 'class' => 'wpinv-tax-country wpi_select2', |
|
| 978 | + 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 979 | + ) ); ?> |
|
| 980 | 980 | </td> |
| 981 | 981 | <td class="wpinv_tax_state"> |
| 982 | 982 | <?php echo wpinv_html_text( array( |
| 983 | - 'name' => 'tax_rates[0][state]' |
|
| 984 | - ) ); ?> |
|
| 983 | + 'name' => 'tax_rates[0][state]' |
|
| 984 | + ) ); ?> |
|
| 985 | 985 | </td> |
| 986 | 986 | <td class="wpinv_tax_global"> |
| 987 | 987 | <input type="checkbox" name="tax_rates[0][global]" id="tax_rates[0][global]" value="1"/> |
@@ -996,7 +996,7 @@ discard block |
||
| 996 | 996 | <tfoot><tr><td colspan="5"></td><td class="wpinv_tax_action"><span class="button-secondary" id="wpinv_add_tax_rate"><?php _e( 'Add Tax Rate', 'invoicing' ); ?></span></td></tr></tfoot> |
| 997 | 997 | </table> |
| 998 | 998 | <?php |
| 999 | - echo ob_get_clean(); |
|
| 999 | + echo ob_get_clean(); |
|
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | 1002 | function wpinv_tools_callback($args) { |
@@ -1024,15 +1024,15 @@ discard block |
||
| 1024 | 1024 | } |
| 1025 | 1025 | |
| 1026 | 1026 | function wpinv_descriptive_text_callback( $args ) { |
| 1027 | - echo wp_kses_post( $args['desc'] ); |
|
| 1027 | + echo wp_kses_post( $args['desc'] ); |
|
| 1028 | 1028 | } |
| 1029 | 1029 | |
| 1030 | 1030 | function wpinv_hook_callback( $args ) { |
| 1031 | - do_action( 'wpinv_' . $args['id'], $args ); |
|
| 1031 | + do_action( 'wpinv_' . $args['id'], $args ); |
|
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | function wpinv_set_settings_cap() { |
| 1035 | - return wpinv_get_capability(); |
|
| 1035 | + return wpinv_get_capability(); |
|
| 1036 | 1036 | } |
| 1037 | 1037 | add_filter( 'option_page_capability_wpinv_settings', 'wpinv_set_settings_cap' ); |
| 1038 | 1038 | |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | - exit; |
|
| 12 | + exit; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,356 +21,356 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class GetPaid_Data { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * ID for this object. |
|
| 26 | - * |
|
| 27 | - * @since 1.0.19 |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - protected $id = 0; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Core data for this object. Name value pairs (name + default value). |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $data = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Core data changes for this object. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.19 |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - protected $changes = array(); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * This is false until the object is read from the DB. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @var bool |
|
| 53 | - */ |
|
| 54 | - protected $object_read = false; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * This is the name of this object type. |
|
| 58 | - * |
|
| 59 | - * @since 1.0.19 |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - protected $object_type = 'data'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | - * Used as a standard way for sub classes (like item types) to add |
|
| 67 | - * additional information to an inherited class. |
|
| 68 | - * |
|
| 69 | - * @since 1.0.19 |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $extra_data = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $default_data = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Contains a reference to the data store for this class. |
|
| 84 | - * |
|
| 85 | - * @since 1.0.19 |
|
| 86 | - * @var GetPaid_Data_Store |
|
| 87 | - */ |
|
| 88 | - protected $data_store; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Stores meta in cache for future reads. |
|
| 92 | - * A group must be set to to enable caching. |
|
| 93 | - * |
|
| 94 | - * @since 1.0.19 |
|
| 95 | - * @var string |
|
| 96 | - */ |
|
| 97 | - protected $cache_group = ''; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Stores the last error. |
|
| 101 | - * |
|
| 102 | - * @since 1.0.19 |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $last_error = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Stores additional meta data. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * @var array |
|
| 112 | - */ |
|
| 113 | - protected $meta_data = null; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Default constructor. |
|
| 117 | - * |
|
| 118 | - * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
|
| 119 | - */ |
|
| 120 | - public function __construct( $read = 0 ) { |
|
| 121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | - $this->default_data = $this->data; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public function __sleep() { |
|
| 131 | - return array( 'id' ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Re-run the constructor with the object ID. |
|
| 136 | - * |
|
| 137 | - * If the object no longer exists, remove the ID. |
|
| 138 | - */ |
|
| 139 | - public function __wakeup() { |
|
| 140 | - $this->__construct( absint( $this->id ) ); |
|
| 141 | - |
|
| 142 | - if ( ! empty( $this->last_error ) ) { |
|
| 143 | - $this->set_id( 0 ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - */ |
|
| 153 | - public function __clone() { |
|
| 154 | - $this->maybe_read_meta_data(); |
|
| 155 | - if ( ! empty( $this->meta_data ) ) { |
|
| 156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | - if ( ! empty( $meta->id ) ) { |
|
| 159 | - $this->meta_data[ $array_key ]->id = null; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the data store. |
|
| 167 | - * |
|
| 168 | - * @since 1.0.19 |
|
| 169 | - * @return object |
|
| 170 | - */ |
|
| 171 | - public function get_data_store() { |
|
| 172 | - return $this->data_store; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Get the object type. |
|
| 177 | - * |
|
| 178 | - * @since 1.0.19 |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function get_object_type() { |
|
| 182 | - return $this->object_type; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Returns the unique ID for this object. |
|
| 187 | - * |
|
| 188 | - * @since 1.0.19 |
|
| 189 | - * @return int |
|
| 190 | - */ |
|
| 191 | - public function get_id() { |
|
| 192 | - return $this->id; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get form status. |
|
| 197 | - * |
|
| 198 | - * @since 1.0.19 |
|
| 199 | - * @param string $context View or edit context. |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function get_status( $context = 'view' ) { |
|
| 203 | - return $this->get_prop( 'status', $context ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Delete an object, set the ID to 0, and return result. |
|
| 208 | - * |
|
| 209 | - * @since 1.0.19 |
|
| 210 | - * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | - * @return bool result |
|
| 212 | - */ |
|
| 213 | - public function delete( $force_delete = false ) { |
|
| 214 | - if ( $this->data_store && $this->get_id() ) { |
|
| 215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | - $this->set_id( 0 ); |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Save should create or update based on object existence. |
|
| 224 | - * |
|
| 225 | - * @since 1.0.19 |
|
| 226 | - * @return int |
|
| 227 | - */ |
|
| 228 | - public function save() { |
|
| 229 | - if ( ! $this->data_store ) { |
|
| 230 | - return $this->get_id(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | - * |
|
| 236 | - * @param GetPaid_Data $this The object being saved. |
|
| 237 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | - */ |
|
| 239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | - |
|
| 241 | - if ( $this->get_id() ) { |
|
| 242 | - $this->data_store->update( $this ); |
|
| 243 | - } else { |
|
| 244 | - $this->data_store->create( $this ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Trigger action after saving to the DB. |
|
| 249 | - * |
|
| 250 | - * @param GetPaid_Data $this The object being saved. |
|
| 251 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | - */ |
|
| 253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | - |
|
| 255 | - return $this->get_id(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Change data to JSON format. |
|
| 260 | - * |
|
| 261 | - * @since 1.0.19 |
|
| 262 | - * @return string Data in JSON format. |
|
| 263 | - */ |
|
| 264 | - public function __toString() { |
|
| 265 | - return wp_json_encode( $this->get_data() ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Returns all data for this object. |
|
| 270 | - * |
|
| 271 | - * @since 1.0.19 |
|
| 272 | - * @return array |
|
| 273 | - */ |
|
| 274 | - public function get_data() { |
|
| 275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Returns array of expected data keys for this object. |
|
| 280 | - * |
|
| 281 | - * @since 1.0.19 |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - public function get_data_keys() { |
|
| 285 | - return array_keys( $this->data ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | - * |
|
| 291 | - * @since 1.0.19 |
|
| 292 | - * @return array |
|
| 293 | - */ |
|
| 294 | - public function get_extra_data_keys() { |
|
| 295 | - return array_keys( $this->extra_data ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Filter null meta values from array. |
|
| 300 | - * |
|
| 301 | - * @since 1.0.19 |
|
| 302 | - * @param mixed $meta Meta value to check. |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - protected function filter_null_meta( $meta ) { |
|
| 306 | - return ! is_null( $meta->value ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get All Meta Data. |
|
| 311 | - * |
|
| 312 | - * @since 1.0.19 |
|
| 313 | - * @return array of objects. |
|
| 314 | - */ |
|
| 315 | - public function get_meta_data() { |
|
| 316 | - $this->maybe_read_meta_data(); |
|
| 317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Check if the key is an internal one. |
|
| 322 | - * |
|
| 323 | - * @since 1.0.19 |
|
| 324 | - * @param string $key Key to check. |
|
| 325 | - * @return bool true if it's an internal key, false otherwise |
|
| 326 | - */ |
|
| 327 | - protected function is_internal_meta_key( $key ) { |
|
| 328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | - |
|
| 330 | - if ( ! $internal_meta_key ) { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | - |
|
| 336 | - if ( ! $has_setter_or_getter ) { |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /* translators: %s: $key Key to check */ |
|
| 341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 342 | - |
|
| 343 | - return true; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Magic method for setting data fields. |
|
| 348 | - * |
|
| 349 | - * This method does not update custom fields in the database. |
|
| 350 | - * |
|
| 351 | - * @since 1.0.19 |
|
| 352 | - * @access public |
|
| 353 | - * |
|
| 354 | - */ |
|
| 355 | - public function __set( $key, $value ) { |
|
| 356 | - |
|
| 357 | - if ( 'id' == strtolower( $key ) ) { |
|
| 358 | - return $this->set_id( $value ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - if ( method_exists( $this, "set_$key") ) { |
|
| 362 | - |
|
| 363 | - /* translators: %s: $key Key to set */ |
|
| 364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 365 | - |
|
| 366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | - } else { |
|
| 368 | - $this->set_prop( $key, $value ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 24 | + /** |
|
| 25 | + * ID for this object. |
|
| 26 | + * |
|
| 27 | + * @since 1.0.19 |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + protected $id = 0; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Core data for this object. Name value pairs (name + default value). |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $data = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Core data changes for this object. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.19 |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + protected $changes = array(); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * This is false until the object is read from the DB. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @var bool |
|
| 53 | + */ |
|
| 54 | + protected $object_read = false; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * This is the name of this object type. |
|
| 58 | + * |
|
| 59 | + * @since 1.0.19 |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + protected $object_type = 'data'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | + * Used as a standard way for sub classes (like item types) to add |
|
| 67 | + * additional information to an inherited class. |
|
| 68 | + * |
|
| 69 | + * @since 1.0.19 |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $extra_data = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $default_data = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Contains a reference to the data store for this class. |
|
| 84 | + * |
|
| 85 | + * @since 1.0.19 |
|
| 86 | + * @var GetPaid_Data_Store |
|
| 87 | + */ |
|
| 88 | + protected $data_store; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Stores meta in cache for future reads. |
|
| 92 | + * A group must be set to to enable caching. |
|
| 93 | + * |
|
| 94 | + * @since 1.0.19 |
|
| 95 | + * @var string |
|
| 96 | + */ |
|
| 97 | + protected $cache_group = ''; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Stores the last error. |
|
| 101 | + * |
|
| 102 | + * @since 1.0.19 |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $last_error = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Stores additional meta data. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * @var array |
|
| 112 | + */ |
|
| 113 | + protected $meta_data = null; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Default constructor. |
|
| 117 | + * |
|
| 118 | + * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
|
| 119 | + */ |
|
| 120 | + public function __construct( $read = 0 ) { |
|
| 121 | + $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | + $this->default_data = $this->data; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public function __sleep() { |
|
| 131 | + return array( 'id' ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Re-run the constructor with the object ID. |
|
| 136 | + * |
|
| 137 | + * If the object no longer exists, remove the ID. |
|
| 138 | + */ |
|
| 139 | + public function __wakeup() { |
|
| 140 | + $this->__construct( absint( $this->id ) ); |
|
| 141 | + |
|
| 142 | + if ( ! empty( $this->last_error ) ) { |
|
| 143 | + $this->set_id( 0 ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + */ |
|
| 153 | + public function __clone() { |
|
| 154 | + $this->maybe_read_meta_data(); |
|
| 155 | + if ( ! empty( $this->meta_data ) ) { |
|
| 156 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | + $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | + if ( ! empty( $meta->id ) ) { |
|
| 159 | + $this->meta_data[ $array_key ]->id = null; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the data store. |
|
| 167 | + * |
|
| 168 | + * @since 1.0.19 |
|
| 169 | + * @return object |
|
| 170 | + */ |
|
| 171 | + public function get_data_store() { |
|
| 172 | + return $this->data_store; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Get the object type. |
|
| 177 | + * |
|
| 178 | + * @since 1.0.19 |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function get_object_type() { |
|
| 182 | + return $this->object_type; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Returns the unique ID for this object. |
|
| 187 | + * |
|
| 188 | + * @since 1.0.19 |
|
| 189 | + * @return int |
|
| 190 | + */ |
|
| 191 | + public function get_id() { |
|
| 192 | + return $this->id; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get form status. |
|
| 197 | + * |
|
| 198 | + * @since 1.0.19 |
|
| 199 | + * @param string $context View or edit context. |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function get_status( $context = 'view' ) { |
|
| 203 | + return $this->get_prop( 'status', $context ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Delete an object, set the ID to 0, and return result. |
|
| 208 | + * |
|
| 209 | + * @since 1.0.19 |
|
| 210 | + * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | + * @return bool result |
|
| 212 | + */ |
|
| 213 | + public function delete( $force_delete = false ) { |
|
| 214 | + if ( $this->data_store && $this->get_id() ) { |
|
| 215 | + $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | + $this->set_id( 0 ); |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Save should create or update based on object existence. |
|
| 224 | + * |
|
| 225 | + * @since 1.0.19 |
|
| 226 | + * @return int |
|
| 227 | + */ |
|
| 228 | + public function save() { |
|
| 229 | + if ( ! $this->data_store ) { |
|
| 230 | + return $this->get_id(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | + * |
|
| 236 | + * @param GetPaid_Data $this The object being saved. |
|
| 237 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | + */ |
|
| 239 | + do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | + |
|
| 241 | + if ( $this->get_id() ) { |
|
| 242 | + $this->data_store->update( $this ); |
|
| 243 | + } else { |
|
| 244 | + $this->data_store->create( $this ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Trigger action after saving to the DB. |
|
| 249 | + * |
|
| 250 | + * @param GetPaid_Data $this The object being saved. |
|
| 251 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | + */ |
|
| 253 | + do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | + |
|
| 255 | + return $this->get_id(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Change data to JSON format. |
|
| 260 | + * |
|
| 261 | + * @since 1.0.19 |
|
| 262 | + * @return string Data in JSON format. |
|
| 263 | + */ |
|
| 264 | + public function __toString() { |
|
| 265 | + return wp_json_encode( $this->get_data() ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Returns all data for this object. |
|
| 270 | + * |
|
| 271 | + * @since 1.0.19 |
|
| 272 | + * @return array |
|
| 273 | + */ |
|
| 274 | + public function get_data() { |
|
| 275 | + return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Returns array of expected data keys for this object. |
|
| 280 | + * |
|
| 281 | + * @since 1.0.19 |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + public function get_data_keys() { |
|
| 285 | + return array_keys( $this->data ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | + * |
|
| 291 | + * @since 1.0.19 |
|
| 292 | + * @return array |
|
| 293 | + */ |
|
| 294 | + public function get_extra_data_keys() { |
|
| 295 | + return array_keys( $this->extra_data ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Filter null meta values from array. |
|
| 300 | + * |
|
| 301 | + * @since 1.0.19 |
|
| 302 | + * @param mixed $meta Meta value to check. |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + protected function filter_null_meta( $meta ) { |
|
| 306 | + return ! is_null( $meta->value ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get All Meta Data. |
|
| 311 | + * |
|
| 312 | + * @since 1.0.19 |
|
| 313 | + * @return array of objects. |
|
| 314 | + */ |
|
| 315 | + public function get_meta_data() { |
|
| 316 | + $this->maybe_read_meta_data(); |
|
| 317 | + return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Check if the key is an internal one. |
|
| 322 | + * |
|
| 323 | + * @since 1.0.19 |
|
| 324 | + * @param string $key Key to check. |
|
| 325 | + * @return bool true if it's an internal key, false otherwise |
|
| 326 | + */ |
|
| 327 | + protected function is_internal_meta_key( $key ) { |
|
| 328 | + $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | + |
|
| 330 | + if ( ! $internal_meta_key ) { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | + |
|
| 336 | + if ( ! $has_setter_or_getter ) { |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /* translators: %s: $key Key to check */ |
|
| 341 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 342 | + |
|
| 343 | + return true; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Magic method for setting data fields. |
|
| 348 | + * |
|
| 349 | + * This method does not update custom fields in the database. |
|
| 350 | + * |
|
| 351 | + * @since 1.0.19 |
|
| 352 | + * @access public |
|
| 353 | + * |
|
| 354 | + */ |
|
| 355 | + public function __set( $key, $value ) { |
|
| 356 | + |
|
| 357 | + if ( 'id' == strtolower( $key ) ) { |
|
| 358 | + return $this->set_id( $value ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + if ( method_exists( $this, "set_$key") ) { |
|
| 362 | + |
|
| 363 | + /* translators: %s: $key Key to set */ |
|
| 364 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 365 | + |
|
| 366 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | + } else { |
|
| 368 | + $this->set_prop( $key, $value ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | 374 | * Margic method for retrieving a property. |
| 375 | 375 | */ |
| 376 | 376 | public function __get( $key ) { |
@@ -378,10 +378,10 @@ discard block |
||
| 378 | 378 | // Check if we have a helper method for that. |
| 379 | 379 | if ( method_exists( $this, 'get_' . $key ) ) { |
| 380 | 380 | |
| 381 | - if ( 'post_type' != $key ) { |
|
| 382 | - /* translators: %s: $key Key to set */ |
|
| 383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 384 | - } |
|
| 381 | + if ( 'post_type' != $key ) { |
|
| 382 | + /* translators: %s: $key Key to set */ |
|
| 383 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | 386 | return call_user_func( array( $this, 'get_' . $key ) ); |
| 387 | 387 | } |
@@ -391,512 +391,512 @@ discard block |
||
| 391 | 391 | return $this->post->$key; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return $this->get_prop( $key ); |
|
| 395 | - |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get Meta Data by Key. |
|
| 400 | - * |
|
| 401 | - * @since 1.0.19 |
|
| 402 | - * @param string $key Meta Key. |
|
| 403 | - * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | - * @return mixed |
|
| 406 | - */ |
|
| 407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | - |
|
| 409 | - // Check if this is an internal meta key. |
|
| 410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | - if ( $this->is_internal_meta_key( $_key ) ) { |
|
| 413 | - $function = 'get_' . $_key; |
|
| 414 | - |
|
| 415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | - return $this->{$function}(); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // Read the meta data if not yet read. |
|
| 421 | - $this->maybe_read_meta_data(); |
|
| 422 | - $meta_data = $this->get_meta_data(); |
|
| 423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | - $value = $single ? '' : array(); |
|
| 425 | - |
|
| 426 | - if ( ! empty( $array_keys ) ) { |
|
| 427 | - // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | - if ( $single ) { |
|
| 429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | - } else { |
|
| 431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - if ( 'view' === $context ) { |
|
| 436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return $value; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | - * |
|
| 445 | - * @since 1.0.19 |
|
| 446 | - * @param string $key Meta Key. |
|
| 447 | - * @return boolean |
|
| 448 | - */ |
|
| 449 | - public function meta_exists( $key = '' ) { |
|
| 450 | - $this->maybe_read_meta_data(); |
|
| 451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | - return in_array( $key, $array_keys, true ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set all meta data from array. |
|
| 457 | - * |
|
| 458 | - * @since 1.0.19 |
|
| 459 | - * @param array $data Key/Value pairs. |
|
| 460 | - */ |
|
| 461 | - public function set_meta_data( $data ) { |
|
| 462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | - $this->maybe_read_meta_data(); |
|
| 464 | - foreach ( $data as $meta ) { |
|
| 465 | - $meta = (array) $meta; |
|
| 466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | - array( |
|
| 469 | - 'id' => $meta['id'], |
|
| 470 | - 'key' => $meta['key'], |
|
| 471 | - 'value' => $meta['value'], |
|
| 472 | - ) |
|
| 473 | - ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Add meta data. |
|
| 481 | - * |
|
| 482 | - * @since 1.0.19 |
|
| 483 | - * |
|
| 484 | - * @param string $key Meta key. |
|
| 485 | - * @param string|array $value Meta value. |
|
| 486 | - * @param bool $unique Should this be a unique key?. |
|
| 487 | - */ |
|
| 488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | - $function = 'set_' . $key; |
|
| 491 | - |
|
| 492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | - return $this->{$function}( $value ); |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $this->maybe_read_meta_data(); |
|
| 498 | - if ( $unique ) { |
|
| 499 | - $this->delete_meta_data( $key ); |
|
| 500 | - } |
|
| 501 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | - array( |
|
| 503 | - 'key' => $key, |
|
| 504 | - 'value' => $value, |
|
| 505 | - ) |
|
| 506 | - ); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Update meta data by key or ID, if provided. |
|
| 511 | - * |
|
| 512 | - * @since 1.0.19 |
|
| 513 | - * |
|
| 514 | - * @param string $key Meta key. |
|
| 515 | - * @param string|array $value Meta value. |
|
| 516 | - * @param int $meta_id Meta ID. |
|
| 517 | - */ |
|
| 518 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 519 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 520 | - $function = 'set_' . $key; |
|
| 521 | - |
|
| 522 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 523 | - return $this->{$function}( $value ); |
|
| 524 | - } |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - $this->maybe_read_meta_data(); |
|
| 528 | - |
|
| 529 | - $array_key = false; |
|
| 530 | - |
|
| 531 | - if ( $meta_id ) { |
|
| 532 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 533 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 534 | - } else { |
|
| 535 | - // Find matches by key. |
|
| 536 | - $matches = array(); |
|
| 537 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 538 | - if ( $meta->key === $key ) { |
|
| 539 | - $matches[] = $meta_data_array_key; |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - if ( ! empty( $matches ) ) { |
|
| 544 | - // Set matches to null so only one key gets the new value. |
|
| 545 | - foreach ( $matches as $meta_data_array_key ) { |
|
| 546 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 547 | - } |
|
| 548 | - $array_key = current( $matches ); |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - if ( false !== $array_key ) { |
|
| 553 | - $meta = $this->meta_data[ $array_key ]; |
|
| 554 | - $meta->key = $key; |
|
| 555 | - $meta->value = $value; |
|
| 556 | - } else { |
|
| 557 | - $this->add_meta_data( $key, $value, true ); |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Delete meta data. |
|
| 563 | - * |
|
| 564 | - * @since 1.0.19 |
|
| 565 | - * @param string $key Meta key. |
|
| 566 | - */ |
|
| 567 | - public function delete_meta_data( $key ) { |
|
| 568 | - $this->maybe_read_meta_data(); |
|
| 569 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 570 | - |
|
| 571 | - if ( $array_keys ) { |
|
| 572 | - foreach ( $array_keys as $array_key ) { |
|
| 573 | - $this->meta_data[ $array_key ]->value = null; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Delete meta data. |
|
| 580 | - * |
|
| 581 | - * @since 1.0.19 |
|
| 582 | - * @param int $mid Meta ID. |
|
| 583 | - */ |
|
| 584 | - public function delete_meta_data_by_mid( $mid ) { |
|
| 585 | - $this->maybe_read_meta_data(); |
|
| 586 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 587 | - |
|
| 588 | - if ( $array_keys ) { |
|
| 589 | - foreach ( $array_keys as $array_key ) { |
|
| 590 | - $this->meta_data[ $array_key ]->value = null; |
|
| 591 | - } |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Read meta data if null. |
|
| 597 | - * |
|
| 598 | - * @since 1.0.19 |
|
| 599 | - */ |
|
| 600 | - protected function maybe_read_meta_data() { |
|
| 601 | - if ( is_null( $this->meta_data ) ) { |
|
| 602 | - $this->read_meta_data(); |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Read Meta Data from the database. Ignore any internal properties. |
|
| 608 | - * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 609 | - * |
|
| 610 | - * @since 1.0.19 |
|
| 611 | - * @param bool $force_read True to force a new DB read (and update cache). |
|
| 612 | - */ |
|
| 613 | - public function read_meta_data( $force_read = false ) { |
|
| 614 | - |
|
| 615 | - // Reset meta data. |
|
| 616 | - $this->meta_data = array(); |
|
| 617 | - |
|
| 618 | - // Maybe abort early. |
|
| 619 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 620 | - return; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - // Only read from cache if the cache key is set. |
|
| 624 | - $cache_key = null; |
|
| 625 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 626 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 627 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - // Should we force read? |
|
| 631 | - if ( empty( $raw_meta_data ) ) { |
|
| 632 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 633 | - |
|
| 634 | - if ( ! empty( $cache_key ) ) { |
|
| 635 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - // Set meta data. |
|
| 641 | - if ( is_array( $raw_meta_data ) ) { |
|
| 642 | - |
|
| 643 | - foreach ( $raw_meta_data as $meta ) { |
|
| 644 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 645 | - array( |
|
| 646 | - 'id' => (int) $meta->meta_id, |
|
| 647 | - 'key' => $meta->meta_key, |
|
| 648 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 649 | - ) |
|
| 650 | - ); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * Update Meta Data in the database. |
|
| 659 | - * |
|
| 660 | - * @since 1.0.19 |
|
| 661 | - */ |
|
| 662 | - public function save_meta_data() { |
|
| 663 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 664 | - return; |
|
| 665 | - } |
|
| 666 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 667 | - if ( is_null( $meta->value ) ) { |
|
| 668 | - if ( ! empty( $meta->id ) ) { |
|
| 669 | - $this->data_store->delete_meta( $this, $meta ); |
|
| 670 | - unset( $this->meta_data[ $array_key ] ); |
|
| 671 | - } |
|
| 672 | - } elseif ( empty( $meta->id ) ) { |
|
| 673 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 674 | - $meta->apply_changes(); |
|
| 675 | - } else { |
|
| 676 | - if ( $meta->get_changes() ) { |
|
| 677 | - $this->data_store->update_meta( $this, $meta ); |
|
| 678 | - $meta->apply_changes(); |
|
| 679 | - } |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - if ( ! empty( $this->cache_group ) ) { |
|
| 683 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 684 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 685 | - } |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * Set ID. |
|
| 690 | - * |
|
| 691 | - * @since 1.0.19 |
|
| 692 | - * @param int $id ID. |
|
| 693 | - */ |
|
| 694 | - public function set_id( $id ) { |
|
| 695 | - $this->id = absint( $id ); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Sets item status. |
|
| 700 | - * |
|
| 701 | - * @since 1.0.19 |
|
| 702 | - * @param string $status New status. |
|
| 703 | - * @return array details of change. |
|
| 704 | - */ |
|
| 705 | - public function set_status( $status ) { |
|
| 394 | + return $this->get_prop( $key ); |
|
| 395 | + |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get Meta Data by Key. |
|
| 400 | + * |
|
| 401 | + * @since 1.0.19 |
|
| 402 | + * @param string $key Meta Key. |
|
| 403 | + * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | + * @return mixed |
|
| 406 | + */ |
|
| 407 | + public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | + |
|
| 409 | + // Check if this is an internal meta key. |
|
| 410 | + $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | + $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | + if ( $this->is_internal_meta_key( $_key ) ) { |
|
| 413 | + $function = 'get_' . $_key; |
|
| 414 | + |
|
| 415 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | + return $this->{$function}(); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // Read the meta data if not yet read. |
|
| 421 | + $this->maybe_read_meta_data(); |
|
| 422 | + $meta_data = $this->get_meta_data(); |
|
| 423 | + $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | + $value = $single ? '' : array(); |
|
| 425 | + |
|
| 426 | + if ( ! empty( $array_keys ) ) { |
|
| 427 | + // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | + if ( $single ) { |
|
| 429 | + $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | + } else { |
|
| 431 | + $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + if ( 'view' === $context ) { |
|
| 436 | + $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return $value; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | + * |
|
| 445 | + * @since 1.0.19 |
|
| 446 | + * @param string $key Meta Key. |
|
| 447 | + * @return boolean |
|
| 448 | + */ |
|
| 449 | + public function meta_exists( $key = '' ) { |
|
| 450 | + $this->maybe_read_meta_data(); |
|
| 451 | + $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | + return in_array( $key, $array_keys, true ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set all meta data from array. |
|
| 457 | + * |
|
| 458 | + * @since 1.0.19 |
|
| 459 | + * @param array $data Key/Value pairs. |
|
| 460 | + */ |
|
| 461 | + public function set_meta_data( $data ) { |
|
| 462 | + if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | + $this->maybe_read_meta_data(); |
|
| 464 | + foreach ( $data as $meta ) { |
|
| 465 | + $meta = (array) $meta; |
|
| 466 | + if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | + array( |
|
| 469 | + 'id' => $meta['id'], |
|
| 470 | + 'key' => $meta['key'], |
|
| 471 | + 'value' => $meta['value'], |
|
| 472 | + ) |
|
| 473 | + ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Add meta data. |
|
| 481 | + * |
|
| 482 | + * @since 1.0.19 |
|
| 483 | + * |
|
| 484 | + * @param string $key Meta key. |
|
| 485 | + * @param string|array $value Meta value. |
|
| 486 | + * @param bool $unique Should this be a unique key?. |
|
| 487 | + */ |
|
| 488 | + public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | + $function = 'set_' . $key; |
|
| 491 | + |
|
| 492 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | + return $this->{$function}( $value ); |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $this->maybe_read_meta_data(); |
|
| 498 | + if ( $unique ) { |
|
| 499 | + $this->delete_meta_data( $key ); |
|
| 500 | + } |
|
| 501 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | + array( |
|
| 503 | + 'key' => $key, |
|
| 504 | + 'value' => $value, |
|
| 505 | + ) |
|
| 506 | + ); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Update meta data by key or ID, if provided. |
|
| 511 | + * |
|
| 512 | + * @since 1.0.19 |
|
| 513 | + * |
|
| 514 | + * @param string $key Meta key. |
|
| 515 | + * @param string|array $value Meta value. |
|
| 516 | + * @param int $meta_id Meta ID. |
|
| 517 | + */ |
|
| 518 | + public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 519 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 520 | + $function = 'set_' . $key; |
|
| 521 | + |
|
| 522 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 523 | + return $this->{$function}( $value ); |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + $this->maybe_read_meta_data(); |
|
| 528 | + |
|
| 529 | + $array_key = false; |
|
| 530 | + |
|
| 531 | + if ( $meta_id ) { |
|
| 532 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 533 | + $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 534 | + } else { |
|
| 535 | + // Find matches by key. |
|
| 536 | + $matches = array(); |
|
| 537 | + foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 538 | + if ( $meta->key === $key ) { |
|
| 539 | + $matches[] = $meta_data_array_key; |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + if ( ! empty( $matches ) ) { |
|
| 544 | + // Set matches to null so only one key gets the new value. |
|
| 545 | + foreach ( $matches as $meta_data_array_key ) { |
|
| 546 | + $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 547 | + } |
|
| 548 | + $array_key = current( $matches ); |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + if ( false !== $array_key ) { |
|
| 553 | + $meta = $this->meta_data[ $array_key ]; |
|
| 554 | + $meta->key = $key; |
|
| 555 | + $meta->value = $value; |
|
| 556 | + } else { |
|
| 557 | + $this->add_meta_data( $key, $value, true ); |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Delete meta data. |
|
| 563 | + * |
|
| 564 | + * @since 1.0.19 |
|
| 565 | + * @param string $key Meta key. |
|
| 566 | + */ |
|
| 567 | + public function delete_meta_data( $key ) { |
|
| 568 | + $this->maybe_read_meta_data(); |
|
| 569 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 570 | + |
|
| 571 | + if ( $array_keys ) { |
|
| 572 | + foreach ( $array_keys as $array_key ) { |
|
| 573 | + $this->meta_data[ $array_key ]->value = null; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Delete meta data. |
|
| 580 | + * |
|
| 581 | + * @since 1.0.19 |
|
| 582 | + * @param int $mid Meta ID. |
|
| 583 | + */ |
|
| 584 | + public function delete_meta_data_by_mid( $mid ) { |
|
| 585 | + $this->maybe_read_meta_data(); |
|
| 586 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 587 | + |
|
| 588 | + if ( $array_keys ) { |
|
| 589 | + foreach ( $array_keys as $array_key ) { |
|
| 590 | + $this->meta_data[ $array_key ]->value = null; |
|
| 591 | + } |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Read meta data if null. |
|
| 597 | + * |
|
| 598 | + * @since 1.0.19 |
|
| 599 | + */ |
|
| 600 | + protected function maybe_read_meta_data() { |
|
| 601 | + if ( is_null( $this->meta_data ) ) { |
|
| 602 | + $this->read_meta_data(); |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Read Meta Data from the database. Ignore any internal properties. |
|
| 608 | + * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 609 | + * |
|
| 610 | + * @since 1.0.19 |
|
| 611 | + * @param bool $force_read True to force a new DB read (and update cache). |
|
| 612 | + */ |
|
| 613 | + public function read_meta_data( $force_read = false ) { |
|
| 614 | + |
|
| 615 | + // Reset meta data. |
|
| 616 | + $this->meta_data = array(); |
|
| 617 | + |
|
| 618 | + // Maybe abort early. |
|
| 619 | + if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 620 | + return; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + // Only read from cache if the cache key is set. |
|
| 624 | + $cache_key = null; |
|
| 625 | + if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 626 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 627 | + $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + // Should we force read? |
|
| 631 | + if ( empty( $raw_meta_data ) ) { |
|
| 632 | + $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 633 | + |
|
| 634 | + if ( ! empty( $cache_key ) ) { |
|
| 635 | + wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + // Set meta data. |
|
| 641 | + if ( is_array( $raw_meta_data ) ) { |
|
| 642 | + |
|
| 643 | + foreach ( $raw_meta_data as $meta ) { |
|
| 644 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 645 | + array( |
|
| 646 | + 'id' => (int) $meta->meta_id, |
|
| 647 | + 'key' => $meta->meta_key, |
|
| 648 | + 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 649 | + ) |
|
| 650 | + ); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * Update Meta Data in the database. |
|
| 659 | + * |
|
| 660 | + * @since 1.0.19 |
|
| 661 | + */ |
|
| 662 | + public function save_meta_data() { |
|
| 663 | + if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 664 | + return; |
|
| 665 | + } |
|
| 666 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 667 | + if ( is_null( $meta->value ) ) { |
|
| 668 | + if ( ! empty( $meta->id ) ) { |
|
| 669 | + $this->data_store->delete_meta( $this, $meta ); |
|
| 670 | + unset( $this->meta_data[ $array_key ] ); |
|
| 671 | + } |
|
| 672 | + } elseif ( empty( $meta->id ) ) { |
|
| 673 | + $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 674 | + $meta->apply_changes(); |
|
| 675 | + } else { |
|
| 676 | + if ( $meta->get_changes() ) { |
|
| 677 | + $this->data_store->update_meta( $this, $meta ); |
|
| 678 | + $meta->apply_changes(); |
|
| 679 | + } |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + if ( ! empty( $this->cache_group ) ) { |
|
| 683 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 684 | + wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 685 | + } |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * Set ID. |
|
| 690 | + * |
|
| 691 | + * @since 1.0.19 |
|
| 692 | + * @param int $id ID. |
|
| 693 | + */ |
|
| 694 | + public function set_id( $id ) { |
|
| 695 | + $this->id = absint( $id ); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Sets item status. |
|
| 700 | + * |
|
| 701 | + * @since 1.0.19 |
|
| 702 | + * @param string $status New status. |
|
| 703 | + * @return array details of change. |
|
| 704 | + */ |
|
| 705 | + public function set_status( $status ) { |
|
| 706 | 706 | $old_status = $this->get_status(); |
| 707 | 707 | |
| 708 | - $this->set_prop( 'status', $status ); |
|
| 709 | - |
|
| 710 | - return array( |
|
| 711 | - 'from' => $old_status, |
|
| 712 | - 'to' => $status, |
|
| 713 | - ); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * Set all props to default values. |
|
| 718 | - * |
|
| 719 | - * @since 1.0.19 |
|
| 720 | - */ |
|
| 721 | - public function set_defaults() { |
|
| 722 | - $this->data = $this->default_data; |
|
| 723 | - $this->changes = array(); |
|
| 724 | - $this->set_object_read( false ); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Set object read property. |
|
| 729 | - * |
|
| 730 | - * @since 1.0.19 |
|
| 731 | - * @param boolean $read Should read?. |
|
| 732 | - */ |
|
| 733 | - public function set_object_read( $read = true ) { |
|
| 734 | - $this->object_read = (bool) $read; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Get object read property. |
|
| 739 | - * |
|
| 740 | - * @since 1.0.19 |
|
| 741 | - * @return boolean |
|
| 742 | - */ |
|
| 743 | - public function get_object_read() { |
|
| 744 | - return (bool) $this->object_read; |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * Set a collection of props in one go, collect any errors, and return the result. |
|
| 749 | - * Only sets using public methods. |
|
| 750 | - * |
|
| 751 | - * @since 1.0.19 |
|
| 752 | - * |
|
| 753 | - * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 754 | - * @param string $context In what context to run this. |
|
| 755 | - * |
|
| 756 | - * @return bool|WP_Error |
|
| 757 | - */ |
|
| 758 | - public function set_props( $props, $context = 'set' ) { |
|
| 759 | - $errors = false; |
|
| 760 | - |
|
| 761 | - foreach ( $props as $prop => $value ) { |
|
| 762 | - try { |
|
| 763 | - /** |
|
| 764 | - * Checks if the prop being set is allowed, and the value is not null. |
|
| 765 | - */ |
|
| 766 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 767 | - continue; |
|
| 768 | - } |
|
| 769 | - $setter = "set_$prop"; |
|
| 770 | - |
|
| 771 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
| 772 | - $this->{$setter}( $value ); |
|
| 773 | - } |
|
| 774 | - } catch ( Exception $e ) { |
|
| 775 | - if ( ! $errors ) { |
|
| 776 | - $errors = new WP_Error(); |
|
| 777 | - } |
|
| 778 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 779 | - $this->last_error = $e->getMessage(); |
|
| 780 | - } |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Sets a prop for a setter method. |
|
| 788 | - * |
|
| 789 | - * This stores changes in a special array so we can track what needs saving |
|
| 790 | - * the the DB later. |
|
| 791 | - * |
|
| 792 | - * @since 1.0.19 |
|
| 793 | - * @param string $prop Name of prop to set. |
|
| 794 | - * @param mixed $value Value of the prop. |
|
| 795 | - */ |
|
| 796 | - protected function set_prop( $prop, $value ) { |
|
| 797 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 798 | - if ( true === $this->object_read ) { |
|
| 799 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 800 | - $this->changes[ $prop ] = $value; |
|
| 801 | - } |
|
| 802 | - } else { |
|
| 803 | - $this->data[ $prop ] = $value; |
|
| 804 | - } |
|
| 805 | - } |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - /** |
|
| 809 | - * Return data changes only. |
|
| 810 | - * |
|
| 811 | - * @since 1.0.19 |
|
| 812 | - * @return array |
|
| 813 | - */ |
|
| 814 | - public function get_changes() { |
|
| 815 | - return $this->changes; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * Merge changes with data and clear. |
|
| 820 | - * |
|
| 821 | - * @since 1.0.19 |
|
| 822 | - */ |
|
| 823 | - public function apply_changes() { |
|
| 824 | - $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
| 825 | - $this->changes = array(); |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * Prefix for action and filter hooks on data. |
|
| 830 | - * |
|
| 831 | - * @since 1.0.19 |
|
| 832 | - * @return string |
|
| 833 | - */ |
|
| 834 | - protected function get_hook_prefix() { |
|
| 835 | - return 'wpinv_get_' . $this->object_type . '_'; |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - /** |
|
| 839 | - * Gets a prop for a getter method. |
|
| 840 | - * |
|
| 841 | - * Gets the value from either current pending changes, or the data itself. |
|
| 842 | - * Context controls what happens to the value before it's returned. |
|
| 843 | - * |
|
| 844 | - * @since 1.0.19 |
|
| 845 | - * @param string $prop Name of prop to get. |
|
| 846 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 847 | - * @return mixed |
|
| 848 | - */ |
|
| 849 | - protected function get_prop( $prop, $context = 'view' ) { |
|
| 850 | - $value = null; |
|
| 851 | - |
|
| 852 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 853 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 854 | - |
|
| 855 | - if ( 'view' === $context ) { |
|
| 856 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 857 | - } |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - return $value; |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * Sets a date prop whilst handling formatting and datetime objects. |
|
| 865 | - * |
|
| 866 | - * @since 1.0.19 |
|
| 867 | - * @param string $prop Name of prop to set. |
|
| 868 | - * @param string|integer $value Value of the prop. |
|
| 869 | - */ |
|
| 870 | - protected function set_date_prop( $prop, $value ) { |
|
| 871 | - |
|
| 872 | - if ( empty( $value ) ) { |
|
| 873 | - $this->set_prop( $prop, null ); |
|
| 874 | - return; |
|
| 875 | - } |
|
| 876 | - $this->set_prop( $prop, $value ); |
|
| 877 | - |
|
| 878 | - } |
|
| 879 | - |
|
| 880 | - /** |
|
| 881 | - * When invalid data is found, throw an exception unless reading from the DB. |
|
| 882 | - * |
|
| 883 | - * @since 1.0.19 |
|
| 884 | - * @param string $code Error code. |
|
| 885 | - * @param string $message Error message. |
|
| 886 | - */ |
|
| 887 | - protected function error( $code, $message ) { |
|
| 888 | - $this->last_error = $message; |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * Checks if the object is saved in the database |
|
| 893 | - * |
|
| 894 | - * @since 1.0.19 |
|
| 895 | - * @return bool |
|
| 896 | - */ |
|
| 897 | - public function exists() { |
|
| 898 | - $id = $this->get_id(); |
|
| 899 | - return ! empty( $id ); |
|
| 900 | - } |
|
| 708 | + $this->set_prop( 'status', $status ); |
|
| 709 | + |
|
| 710 | + return array( |
|
| 711 | + 'from' => $old_status, |
|
| 712 | + 'to' => $status, |
|
| 713 | + ); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * Set all props to default values. |
|
| 718 | + * |
|
| 719 | + * @since 1.0.19 |
|
| 720 | + */ |
|
| 721 | + public function set_defaults() { |
|
| 722 | + $this->data = $this->default_data; |
|
| 723 | + $this->changes = array(); |
|
| 724 | + $this->set_object_read( false ); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Set object read property. |
|
| 729 | + * |
|
| 730 | + * @since 1.0.19 |
|
| 731 | + * @param boolean $read Should read?. |
|
| 732 | + */ |
|
| 733 | + public function set_object_read( $read = true ) { |
|
| 734 | + $this->object_read = (bool) $read; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Get object read property. |
|
| 739 | + * |
|
| 740 | + * @since 1.0.19 |
|
| 741 | + * @return boolean |
|
| 742 | + */ |
|
| 743 | + public function get_object_read() { |
|
| 744 | + return (bool) $this->object_read; |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * Set a collection of props in one go, collect any errors, and return the result. |
|
| 749 | + * Only sets using public methods. |
|
| 750 | + * |
|
| 751 | + * @since 1.0.19 |
|
| 752 | + * |
|
| 753 | + * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 754 | + * @param string $context In what context to run this. |
|
| 755 | + * |
|
| 756 | + * @return bool|WP_Error |
|
| 757 | + */ |
|
| 758 | + public function set_props( $props, $context = 'set' ) { |
|
| 759 | + $errors = false; |
|
| 760 | + |
|
| 761 | + foreach ( $props as $prop => $value ) { |
|
| 762 | + try { |
|
| 763 | + /** |
|
| 764 | + * Checks if the prop being set is allowed, and the value is not null. |
|
| 765 | + */ |
|
| 766 | + if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 767 | + continue; |
|
| 768 | + } |
|
| 769 | + $setter = "set_$prop"; |
|
| 770 | + |
|
| 771 | + if ( is_callable( array( $this, $setter ) ) ) { |
|
| 772 | + $this->{$setter}( $value ); |
|
| 773 | + } |
|
| 774 | + } catch ( Exception $e ) { |
|
| 775 | + if ( ! $errors ) { |
|
| 776 | + $errors = new WP_Error(); |
|
| 777 | + } |
|
| 778 | + $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 779 | + $this->last_error = $e->getMessage(); |
|
| 780 | + } |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Sets a prop for a setter method. |
|
| 788 | + * |
|
| 789 | + * This stores changes in a special array so we can track what needs saving |
|
| 790 | + * the the DB later. |
|
| 791 | + * |
|
| 792 | + * @since 1.0.19 |
|
| 793 | + * @param string $prop Name of prop to set. |
|
| 794 | + * @param mixed $value Value of the prop. |
|
| 795 | + */ |
|
| 796 | + protected function set_prop( $prop, $value ) { |
|
| 797 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 798 | + if ( true === $this->object_read ) { |
|
| 799 | + if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 800 | + $this->changes[ $prop ] = $value; |
|
| 801 | + } |
|
| 802 | + } else { |
|
| 803 | + $this->data[ $prop ] = $value; |
|
| 804 | + } |
|
| 805 | + } |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + /** |
|
| 809 | + * Return data changes only. |
|
| 810 | + * |
|
| 811 | + * @since 1.0.19 |
|
| 812 | + * @return array |
|
| 813 | + */ |
|
| 814 | + public function get_changes() { |
|
| 815 | + return $this->changes; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * Merge changes with data and clear. |
|
| 820 | + * |
|
| 821 | + * @since 1.0.19 |
|
| 822 | + */ |
|
| 823 | + public function apply_changes() { |
|
| 824 | + $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
| 825 | + $this->changes = array(); |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * Prefix for action and filter hooks on data. |
|
| 830 | + * |
|
| 831 | + * @since 1.0.19 |
|
| 832 | + * @return string |
|
| 833 | + */ |
|
| 834 | + protected function get_hook_prefix() { |
|
| 835 | + return 'wpinv_get_' . $this->object_type . '_'; |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + /** |
|
| 839 | + * Gets a prop for a getter method. |
|
| 840 | + * |
|
| 841 | + * Gets the value from either current pending changes, or the data itself. |
|
| 842 | + * Context controls what happens to the value before it's returned. |
|
| 843 | + * |
|
| 844 | + * @since 1.0.19 |
|
| 845 | + * @param string $prop Name of prop to get. |
|
| 846 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 847 | + * @return mixed |
|
| 848 | + */ |
|
| 849 | + protected function get_prop( $prop, $context = 'view' ) { |
|
| 850 | + $value = null; |
|
| 851 | + |
|
| 852 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 853 | + $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 854 | + |
|
| 855 | + if ( 'view' === $context ) { |
|
| 856 | + $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 857 | + } |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + return $value; |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * Sets a date prop whilst handling formatting and datetime objects. |
|
| 865 | + * |
|
| 866 | + * @since 1.0.19 |
|
| 867 | + * @param string $prop Name of prop to set. |
|
| 868 | + * @param string|integer $value Value of the prop. |
|
| 869 | + */ |
|
| 870 | + protected function set_date_prop( $prop, $value ) { |
|
| 871 | + |
|
| 872 | + if ( empty( $value ) ) { |
|
| 873 | + $this->set_prop( $prop, null ); |
|
| 874 | + return; |
|
| 875 | + } |
|
| 876 | + $this->set_prop( $prop, $value ); |
|
| 877 | + |
|
| 878 | + } |
|
| 879 | + |
|
| 880 | + /** |
|
| 881 | + * When invalid data is found, throw an exception unless reading from the DB. |
|
| 882 | + * |
|
| 883 | + * @since 1.0.19 |
|
| 884 | + * @param string $code Error code. |
|
| 885 | + * @param string $message Error message. |
|
| 886 | + */ |
|
| 887 | + protected function error( $code, $message ) { |
|
| 888 | + $this->last_error = $message; |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * Checks if the object is saved in the database |
|
| 893 | + * |
|
| 894 | + * @since 1.0.19 |
|
| 895 | + * @return bool |
|
| 896 | + */ |
|
| 897 | + public function exists() { |
|
| 898 | + $id = $this->get_id(); |
|
| 899 | + return ! empty( $id ); |
|
| 900 | + } |
|
| 901 | 901 | |
| 902 | 902 | } |