@@ -16,483 +16,483 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class GetPaid_Subscriptions_Query { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Query vars, after parsing |
|
| 21 | - * |
|
| 22 | - * @since 1.0.19 |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $query_vars = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * List of found subscriptions. |
|
| 29 | - * |
|
| 30 | - * @since 1.0.19 |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $results; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Total number of found subscriptions for the current query |
|
| 37 | - * |
|
| 38 | - * @since 1.0.19 |
|
| 39 | - * @var int |
|
| 40 | - */ |
|
| 41 | - private $total_subscriptions = 0; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The SQL query used to fetch matching subscriptions. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.19 |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $request; |
|
| 50 | - |
|
| 51 | - // SQL clauses |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Contains the 'FIELDS' sql clause |
|
| 55 | - * |
|
| 56 | - * @since 1.0.19 |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - public $query_fields; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Contains the 'FROM' sql clause |
|
| 63 | - * |
|
| 64 | - * @since 1.0.19 |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - public $query_from; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Contains the 'WHERE' sql clause |
|
| 71 | - * |
|
| 72 | - * @since 1.0.19 |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - public $query_where; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Contains the 'ORDER BY' sql clause |
|
| 79 | - * |
|
| 80 | - * @since 1.0.19 |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - public $query_orderby; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Contains the 'LIMIT' sql clause |
|
| 87 | - * |
|
| 88 | - * @since 1.0.19 |
|
| 89 | - * @var string |
|
| 90 | - */ |
|
| 91 | - public $query_limit; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Class constructor. |
|
| 95 | - * |
|
| 96 | - * @since 1.0.19 |
|
| 97 | - * |
|
| 98 | - * @param null|string|array $query Optional. The query variables. |
|
| 99 | - */ |
|
| 100 | - public function __construct( $query = null ) { |
|
| 101 | - if ( ! is_null( $query ) ) { |
|
| 102 | - $this->prepare_query( $query ); |
|
| 103 | - $this->query(); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Fills in missing query variables with default values. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * |
|
| 112 | - * @param array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | - * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | - */ |
|
| 115 | - public static function fill_query_vars( $args ) { |
|
| 116 | - $defaults = array( |
|
| 117 | - 'status' => 'all', |
|
| 118 | - 'customer_in' => array(), |
|
| 119 | - 'customer_not_in' => array(), |
|
| 120 | - 'product_in' => array(), |
|
| 121 | - 'product_not_in' => array(), |
|
| 122 | - 'include' => array(), |
|
| 123 | - 'exclude' => array(), |
|
| 124 | - 'orderby' => 'id', |
|
| 125 | - 'order' => 'DESC', |
|
| 126 | - 'offset' => '', |
|
| 127 | - 'number' => 10, |
|
| 128 | - 'paged' => 1, |
|
| 129 | - 'count_total' => true, |
|
| 130 | - 'fields' => 'all', |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - return wp_parse_args( $args, $defaults ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Prepare the query variables. |
|
| 138 | - * |
|
| 139 | - * @since 1.0.19 |
|
| 140 | - * |
|
| 141 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 142 | - * |
|
| 143 | - * @param string|array $query { |
|
| 144 | - * Optional. Array or string of Query parameters. |
|
| 145 | - * |
|
| 146 | - * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
| 147 | - * Default is all. |
|
| 148 | - * @type int[] $customer_in An array of customer ids to filter by. |
|
| 149 | - * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
| 150 | - * @type int[] $product_in An array of product ids to filter by. |
|
| 151 | - * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
| 152 | - * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
| 153 | - * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
| 154 | - * @type array $include An array of subscription IDs to include. Default empty array. |
|
| 155 | - * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
| 156 | - * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
| 157 | - * an array of values, or a multi-dimensional array with fields as |
|
| 158 | - * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
| 159 | - * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 160 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 161 | - * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
| 162 | - * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
| 163 | - * passed as part of an `$orderby` array take precedence over this |
|
| 164 | - * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
| 165 | - * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
| 166 | - * conjunction with pagination. Default 0. |
|
| 167 | - * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
| 168 | - * conjunction with pagination. Value -1 (all) is supported, but |
|
| 169 | - * should be used with caution on larger sites. |
|
| 170 | - * Default 10. |
|
| 171 | - * @type int $paged When used with number, defines the page of results to return. |
|
| 172 | - * Default 1. |
|
| 173 | - * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
| 174 | - * is not needed, setting this to false can improve performance. |
|
| 175 | - * Default true. |
|
| 176 | - * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
| 177 | - * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 178 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 179 | - * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
| 180 | - * Use 'all' for all fields. Default 'all'. |
|
| 181 | - * } |
|
| 182 | - */ |
|
| 183 | - public function prepare_query( $query = array() ) { |
|
| 184 | - global $wpdb; |
|
| 185 | - |
|
| 186 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 187 | - $this->query_limit = null; |
|
| 188 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 192 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
| 196 | - |
|
| 197 | - // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
| 198 | - $qv =& $this->query_vars; |
|
| 199 | - $qv = $this->fill_query_vars( $qv ); |
|
| 200 | - $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
| 201 | - $this->query_from = "FROM $table"; |
|
| 202 | - |
|
| 203 | - // Prepare query fields. |
|
| 204 | - $this->prepare_query_fields( $qv, $table ); |
|
| 205 | - |
|
| 206 | - // Prepare query where. |
|
| 207 | - $this->prepare_query_where( $qv, $table ); |
|
| 208 | - |
|
| 209 | - // Prepare query order. |
|
| 210 | - $this->prepare_query_order( $qv, $table ); |
|
| 211 | - |
|
| 212 | - // limit |
|
| 213 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 214 | - if ( $qv['offset'] ) { |
|
| 215 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 216 | - } else { |
|
| 217 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Prepares the query fields. |
|
| 226 | - * |
|
| 227 | - * @since 1.0.19 |
|
| 228 | - * |
|
| 229 | - * @param array $qv Query vars. |
|
| 230 | - * @param string $table Table name. |
|
| 231 | - */ |
|
| 232 | - protected function prepare_query_fields( &$qv, $table ) { |
|
| 233 | - |
|
| 234 | - if ( is_array( $qv['fields'] ) ) { |
|
| 235 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 236 | - |
|
| 237 | - $this->query_fields = array(); |
|
| 238 | - foreach ( $qv['fields'] as $field ) { |
|
| 239 | - $field = 'id' === strtolower( $field ) ? 'id' : sanitize_key( $field ); |
|
| 240 | - $this->query_fields[] = "$table.`$field`"; |
|
| 241 | - } |
|
| 242 | - $this->query_fields = implode( ',', $this->query_fields ); |
|
| 243 | - } elseif ( 'all' === $qv['fields'] ) { |
|
| 244 | - $this->query_fields = "$table.*"; |
|
| 245 | - } else { |
|
| 246 | - $this->query_fields = "$table.id"; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 250 | - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Prepares the query where. |
|
| 257 | - * |
|
| 258 | - * @since 1.0.19 |
|
| 259 | - * |
|
| 260 | - * @param array $qv Query vars. |
|
| 261 | - * @param string $table Table name. |
|
| 262 | - */ |
|
| 263 | - protected function prepare_query_where( &$qv, $table ) { |
|
| 264 | - global $wpdb; |
|
| 265 | - $this->query_where = 'WHERE 1=1'; |
|
| 266 | - |
|
| 267 | - // Status. |
|
| 268 | - if ( 'all' !== $qv['status'] ) { |
|
| 269 | - $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
| 270 | - $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
| 271 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - if ( ! empty( $qv['customer_in'] ) ) { |
|
| 275 | - $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
| 276 | - $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
| 277 | - } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
| 278 | - $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
| 279 | - $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - if ( ! empty( $qv['product_in'] ) ) { |
|
| 283 | - $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
| 284 | - $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
| 285 | - } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
| 286 | - $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
| 287 | - $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ( ! empty( $qv['include'] ) ) { |
|
| 291 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 292 | - $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 293 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 294 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 295 | - $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // Date queries are allowed for the subscription creation date. |
|
| 299 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 300 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
| 301 | - $this->query_where .= $date_created_query->get_sql(); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // Date queries are also allowed for the subscription expiration date. |
|
| 305 | - if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
| 306 | - $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
| 307 | - $this->query_where .= $date_expires_query->get_sql(); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Prepares the query order. |
|
| 314 | - * |
|
| 315 | - * @since 1.0.19 |
|
| 316 | - * |
|
| 317 | - * @param array $qv Query vars. |
|
| 318 | - * @param string $table Table name. |
|
| 319 | - */ |
|
| 320 | - protected function prepare_query_order( &$qv, $table ) { |
|
| 321 | - |
|
| 322 | - // sorting. |
|
| 323 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 324 | - $order = $this->parse_order( $qv['order'] ); |
|
| 325 | - |
|
| 326 | - // Default order is by 'id' (latest subscriptions). |
|
| 327 | - if ( empty( $qv['orderby'] ) ) { |
|
| 328 | - $ordersby = array( 'id' ); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - // 'orderby' values may be an array, comma- or space-separated list. |
|
| 332 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 333 | - |
|
| 334 | - $orderby_array = array(); |
|
| 335 | - foreach ( $ordersby as $_key => $_value ) { |
|
| 336 | - |
|
| 337 | - if ( is_int( $_key ) ) { |
|
| 338 | - // Integer key means this is a flat array of 'orderby' fields. |
|
| 339 | - $_orderby = $_value; |
|
| 340 | - $_order = $order; |
|
| 341 | - } else { |
|
| 342 | - // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 343 | - $_orderby = $_key; |
|
| 344 | - $_order = $_value; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 348 | - |
|
| 349 | - if ( $parsed ) { |
|
| 350 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - // If no valid clauses were found, order by id. |
|
| 356 | - if ( empty( $orderby_array ) ) { |
|
| 357 | - $orderby_array[] = "id $order"; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 361 | - |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Execute the query, with the current variables. |
|
| 366 | - * |
|
| 367 | - * @since 1.0.19 |
|
| 368 | - * |
|
| 369 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 370 | - */ |
|
| 371 | - public function query() { |
|
| 372 | - global $wpdb; |
|
| 373 | - |
|
| 374 | - $qv =& $this->query_vars; |
|
| 375 | - |
|
| 376 | - // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
| 377 | - // total_subscriptions property. |
|
| 378 | - $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
| 379 | - |
|
| 380 | - if ( null === $this->results ) { |
|
| 381 | - $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 382 | - |
|
| 383 | - if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
| 384 | - $this->results = $wpdb->get_results( $this->request ); |
|
| 385 | - } else { |
|
| 386 | - $this->results = $wpdb->get_col( $this->request ); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 390 | - $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 391 | - $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - if ( 'all' == $qv['fields'] ) { |
|
| 396 | - foreach ( $this->results as $key => $subscription ) { |
|
| 397 | - $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Retrieve query variable. |
|
| 405 | - * |
|
| 406 | - * @since 1.0.19 |
|
| 407 | - * |
|
| 408 | - * @param string $query_var Query variable key. |
|
| 409 | - * @return mixed |
|
| 410 | - */ |
|
| 411 | - public function get( $query_var ) { |
|
| 412 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 413 | - return $this->query_vars[ $query_var ]; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - return null; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Set query variable. |
|
| 421 | - * |
|
| 422 | - * @since 1.0.19 |
|
| 423 | - * |
|
| 424 | - * @param string $query_var Query variable key. |
|
| 425 | - * @param mixed $value Query variable value. |
|
| 426 | - */ |
|
| 427 | - public function set( $query_var, $value ) { |
|
| 428 | - $this->query_vars[ $query_var ] = $value; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Return the list of subscriptions. |
|
| 433 | - * |
|
| 434 | - * @since 1.0.19 |
|
| 435 | - * |
|
| 436 | - * @return WPInv_Subscription[]|array Found subscriptions. |
|
| 437 | - */ |
|
| 438 | - public function get_results() { |
|
| 439 | - return $this->results; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Return the total number of subscriptions for the current query. |
|
| 444 | - * |
|
| 445 | - * @since 1.0.19 |
|
| 446 | - * |
|
| 447 | - * @return int Number of total subscriptions. |
|
| 448 | - */ |
|
| 449 | - public function get_total() { |
|
| 450 | - return $this->total_subscriptions; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
| 455 | - * |
|
| 456 | - * @since 1.0.19 |
|
| 457 | - * |
|
| 458 | - * @param string $orderby Alias for the field to order by. |
|
| 459 | - * @param string $table The current table. |
|
| 460 | - * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 461 | - */ |
|
| 462 | - protected function parse_orderby( $orderby, $table ) { |
|
| 463 | - |
|
| 464 | - $_orderby = ''; |
|
| 465 | - if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
| 466 | - $_orderby = "$table.`$orderby`"; |
|
| 467 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 468 | - $_orderby = "$table.id"; |
|
| 469 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 470 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 471 | - $include_sql = implode( ',', $include ); |
|
| 472 | - $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - return $_orderby; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 480 | - * |
|
| 481 | - * @since 1.0.19 |
|
| 482 | - * |
|
| 483 | - * @param string $order The 'order' query variable. |
|
| 484 | - * @return string The sanitized 'order' query variable. |
|
| 485 | - */ |
|
| 486 | - protected function parse_order( $order ) { |
|
| 487 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 488 | - return 'DESC'; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
| 492 | - return 'ASC'; |
|
| 493 | - } else { |
|
| 494 | - return 'DESC'; |
|
| 495 | - } |
|
| 496 | - } |
|
| 19 | + /** |
|
| 20 | + * Query vars, after parsing |
|
| 21 | + * |
|
| 22 | + * @since 1.0.19 |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $query_vars = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * List of found subscriptions. |
|
| 29 | + * |
|
| 30 | + * @since 1.0.19 |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $results; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Total number of found subscriptions for the current query |
|
| 37 | + * |
|
| 38 | + * @since 1.0.19 |
|
| 39 | + * @var int |
|
| 40 | + */ |
|
| 41 | + private $total_subscriptions = 0; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The SQL query used to fetch matching subscriptions. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.19 |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $request; |
|
| 50 | + |
|
| 51 | + // SQL clauses |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Contains the 'FIELDS' sql clause |
|
| 55 | + * |
|
| 56 | + * @since 1.0.19 |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + public $query_fields; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Contains the 'FROM' sql clause |
|
| 63 | + * |
|
| 64 | + * @since 1.0.19 |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + public $query_from; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Contains the 'WHERE' sql clause |
|
| 71 | + * |
|
| 72 | + * @since 1.0.19 |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + public $query_where; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Contains the 'ORDER BY' sql clause |
|
| 79 | + * |
|
| 80 | + * @since 1.0.19 |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + public $query_orderby; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Contains the 'LIMIT' sql clause |
|
| 87 | + * |
|
| 88 | + * @since 1.0.19 |
|
| 89 | + * @var string |
|
| 90 | + */ |
|
| 91 | + public $query_limit; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Class constructor. |
|
| 95 | + * |
|
| 96 | + * @since 1.0.19 |
|
| 97 | + * |
|
| 98 | + * @param null|string|array $query Optional. The query variables. |
|
| 99 | + */ |
|
| 100 | + public function __construct( $query = null ) { |
|
| 101 | + if ( ! is_null( $query ) ) { |
|
| 102 | + $this->prepare_query( $query ); |
|
| 103 | + $this->query(); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Fills in missing query variables with default values. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * |
|
| 112 | + * @param array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | + * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | + */ |
|
| 115 | + public static function fill_query_vars( $args ) { |
|
| 116 | + $defaults = array( |
|
| 117 | + 'status' => 'all', |
|
| 118 | + 'customer_in' => array(), |
|
| 119 | + 'customer_not_in' => array(), |
|
| 120 | + 'product_in' => array(), |
|
| 121 | + 'product_not_in' => array(), |
|
| 122 | + 'include' => array(), |
|
| 123 | + 'exclude' => array(), |
|
| 124 | + 'orderby' => 'id', |
|
| 125 | + 'order' => 'DESC', |
|
| 126 | + 'offset' => '', |
|
| 127 | + 'number' => 10, |
|
| 128 | + 'paged' => 1, |
|
| 129 | + 'count_total' => true, |
|
| 130 | + 'fields' => 'all', |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + return wp_parse_args( $args, $defaults ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Prepare the query variables. |
|
| 138 | + * |
|
| 139 | + * @since 1.0.19 |
|
| 140 | + * |
|
| 141 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 142 | + * |
|
| 143 | + * @param string|array $query { |
|
| 144 | + * Optional. Array or string of Query parameters. |
|
| 145 | + * |
|
| 146 | + * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
| 147 | + * Default is all. |
|
| 148 | + * @type int[] $customer_in An array of customer ids to filter by. |
|
| 149 | + * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
| 150 | + * @type int[] $product_in An array of product ids to filter by. |
|
| 151 | + * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
| 152 | + * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
| 153 | + * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
| 154 | + * @type array $include An array of subscription IDs to include. Default empty array. |
|
| 155 | + * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
| 156 | + * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
| 157 | + * an array of values, or a multi-dimensional array with fields as |
|
| 158 | + * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
| 159 | + * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 160 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 161 | + * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
| 162 | + * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
| 163 | + * passed as part of an `$orderby` array take precedence over this |
|
| 164 | + * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
| 165 | + * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
| 166 | + * conjunction with pagination. Default 0. |
|
| 167 | + * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
| 168 | + * conjunction with pagination. Value -1 (all) is supported, but |
|
| 169 | + * should be used with caution on larger sites. |
|
| 170 | + * Default 10. |
|
| 171 | + * @type int $paged When used with number, defines the page of results to return. |
|
| 172 | + * Default 1. |
|
| 173 | + * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
| 174 | + * is not needed, setting this to false can improve performance. |
|
| 175 | + * Default true. |
|
| 176 | + * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
| 177 | + * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 178 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 179 | + * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
| 180 | + * Use 'all' for all fields. Default 'all'. |
|
| 181 | + * } |
|
| 182 | + */ |
|
| 183 | + public function prepare_query( $query = array() ) { |
|
| 184 | + global $wpdb; |
|
| 185 | + |
|
| 186 | + if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 187 | + $this->query_limit = null; |
|
| 188 | + $this->query_vars = $this->fill_query_vars( $query ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 192 | + $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
| 196 | + |
|
| 197 | + // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
| 198 | + $qv =& $this->query_vars; |
|
| 199 | + $qv = $this->fill_query_vars( $qv ); |
|
| 200 | + $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
| 201 | + $this->query_from = "FROM $table"; |
|
| 202 | + |
|
| 203 | + // Prepare query fields. |
|
| 204 | + $this->prepare_query_fields( $qv, $table ); |
|
| 205 | + |
|
| 206 | + // Prepare query where. |
|
| 207 | + $this->prepare_query_where( $qv, $table ); |
|
| 208 | + |
|
| 209 | + // Prepare query order. |
|
| 210 | + $this->prepare_query_order( $qv, $table ); |
|
| 211 | + |
|
| 212 | + // limit |
|
| 213 | + if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 214 | + if ( $qv['offset'] ) { |
|
| 215 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 216 | + } else { |
|
| 217 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Prepares the query fields. |
|
| 226 | + * |
|
| 227 | + * @since 1.0.19 |
|
| 228 | + * |
|
| 229 | + * @param array $qv Query vars. |
|
| 230 | + * @param string $table Table name. |
|
| 231 | + */ |
|
| 232 | + protected function prepare_query_fields( &$qv, $table ) { |
|
| 233 | + |
|
| 234 | + if ( is_array( $qv['fields'] ) ) { |
|
| 235 | + $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 236 | + |
|
| 237 | + $this->query_fields = array(); |
|
| 238 | + foreach ( $qv['fields'] as $field ) { |
|
| 239 | + $field = 'id' === strtolower( $field ) ? 'id' : sanitize_key( $field ); |
|
| 240 | + $this->query_fields[] = "$table.`$field`"; |
|
| 241 | + } |
|
| 242 | + $this->query_fields = implode( ',', $this->query_fields ); |
|
| 243 | + } elseif ( 'all' === $qv['fields'] ) { |
|
| 244 | + $this->query_fields = "$table.*"; |
|
| 245 | + } else { |
|
| 246 | + $this->query_fields = "$table.id"; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 250 | + $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Prepares the query where. |
|
| 257 | + * |
|
| 258 | + * @since 1.0.19 |
|
| 259 | + * |
|
| 260 | + * @param array $qv Query vars. |
|
| 261 | + * @param string $table Table name. |
|
| 262 | + */ |
|
| 263 | + protected function prepare_query_where( &$qv, $table ) { |
|
| 264 | + global $wpdb; |
|
| 265 | + $this->query_where = 'WHERE 1=1'; |
|
| 266 | + |
|
| 267 | + // Status. |
|
| 268 | + if ( 'all' !== $qv['status'] ) { |
|
| 269 | + $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
| 270 | + $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
| 271 | + $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + if ( ! empty( $qv['customer_in'] ) ) { |
|
| 275 | + $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
| 276 | + $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
| 277 | + } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
| 278 | + $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
| 279 | + $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + if ( ! empty( $qv['product_in'] ) ) { |
|
| 283 | + $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
| 284 | + $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
| 285 | + } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
| 286 | + $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
| 287 | + $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ( ! empty( $qv['include'] ) ) { |
|
| 291 | + $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 292 | + $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 293 | + } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 294 | + $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 295 | + $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // Date queries are allowed for the subscription creation date. |
|
| 299 | + if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 300 | + $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
| 301 | + $this->query_where .= $date_created_query->get_sql(); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // Date queries are also allowed for the subscription expiration date. |
|
| 305 | + if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
| 306 | + $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
| 307 | + $this->query_where .= $date_expires_query->get_sql(); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Prepares the query order. |
|
| 314 | + * |
|
| 315 | + * @since 1.0.19 |
|
| 316 | + * |
|
| 317 | + * @param array $qv Query vars. |
|
| 318 | + * @param string $table Table name. |
|
| 319 | + */ |
|
| 320 | + protected function prepare_query_order( &$qv, $table ) { |
|
| 321 | + |
|
| 322 | + // sorting. |
|
| 323 | + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 324 | + $order = $this->parse_order( $qv['order'] ); |
|
| 325 | + |
|
| 326 | + // Default order is by 'id' (latest subscriptions). |
|
| 327 | + if ( empty( $qv['orderby'] ) ) { |
|
| 328 | + $ordersby = array( 'id' ); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + // 'orderby' values may be an array, comma- or space-separated list. |
|
| 332 | + $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 333 | + |
|
| 334 | + $orderby_array = array(); |
|
| 335 | + foreach ( $ordersby as $_key => $_value ) { |
|
| 336 | + |
|
| 337 | + if ( is_int( $_key ) ) { |
|
| 338 | + // Integer key means this is a flat array of 'orderby' fields. |
|
| 339 | + $_orderby = $_value; |
|
| 340 | + $_order = $order; |
|
| 341 | + } else { |
|
| 342 | + // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 343 | + $_orderby = $_key; |
|
| 344 | + $_order = $_value; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 348 | + |
|
| 349 | + if ( $parsed ) { |
|
| 350 | + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + // If no valid clauses were found, order by id. |
|
| 356 | + if ( empty( $orderby_array ) ) { |
|
| 357 | + $orderby_array[] = "id $order"; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 361 | + |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Execute the query, with the current variables. |
|
| 366 | + * |
|
| 367 | + * @since 1.0.19 |
|
| 368 | + * |
|
| 369 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 370 | + */ |
|
| 371 | + public function query() { |
|
| 372 | + global $wpdb; |
|
| 373 | + |
|
| 374 | + $qv =& $this->query_vars; |
|
| 375 | + |
|
| 376 | + // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
| 377 | + // total_subscriptions property. |
|
| 378 | + $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
| 379 | + |
|
| 380 | + if ( null === $this->results ) { |
|
| 381 | + $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 382 | + |
|
| 383 | + if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
| 384 | + $this->results = $wpdb->get_results( $this->request ); |
|
| 385 | + } else { |
|
| 386 | + $this->results = $wpdb->get_col( $this->request ); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 390 | + $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 391 | + $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + if ( 'all' == $qv['fields'] ) { |
|
| 396 | + foreach ( $this->results as $key => $subscription ) { |
|
| 397 | + $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Retrieve query variable. |
|
| 405 | + * |
|
| 406 | + * @since 1.0.19 |
|
| 407 | + * |
|
| 408 | + * @param string $query_var Query variable key. |
|
| 409 | + * @return mixed |
|
| 410 | + */ |
|
| 411 | + public function get( $query_var ) { |
|
| 412 | + if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 413 | + return $this->query_vars[ $query_var ]; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + return null; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Set query variable. |
|
| 421 | + * |
|
| 422 | + * @since 1.0.19 |
|
| 423 | + * |
|
| 424 | + * @param string $query_var Query variable key. |
|
| 425 | + * @param mixed $value Query variable value. |
|
| 426 | + */ |
|
| 427 | + public function set( $query_var, $value ) { |
|
| 428 | + $this->query_vars[ $query_var ] = $value; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Return the list of subscriptions. |
|
| 433 | + * |
|
| 434 | + * @since 1.0.19 |
|
| 435 | + * |
|
| 436 | + * @return WPInv_Subscription[]|array Found subscriptions. |
|
| 437 | + */ |
|
| 438 | + public function get_results() { |
|
| 439 | + return $this->results; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Return the total number of subscriptions for the current query. |
|
| 444 | + * |
|
| 445 | + * @since 1.0.19 |
|
| 446 | + * |
|
| 447 | + * @return int Number of total subscriptions. |
|
| 448 | + */ |
|
| 449 | + public function get_total() { |
|
| 450 | + return $this->total_subscriptions; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
| 455 | + * |
|
| 456 | + * @since 1.0.19 |
|
| 457 | + * |
|
| 458 | + * @param string $orderby Alias for the field to order by. |
|
| 459 | + * @param string $table The current table. |
|
| 460 | + * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 461 | + */ |
|
| 462 | + protected function parse_orderby( $orderby, $table ) { |
|
| 463 | + |
|
| 464 | + $_orderby = ''; |
|
| 465 | + if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
| 466 | + $_orderby = "$table.`$orderby`"; |
|
| 467 | + } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 468 | + $_orderby = "$table.id"; |
|
| 469 | + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 470 | + $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 471 | + $include_sql = implode( ',', $include ); |
|
| 472 | + $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + return $_orderby; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 480 | + * |
|
| 481 | + * @since 1.0.19 |
|
| 482 | + * |
|
| 483 | + * @param string $order The 'order' query variable. |
|
| 484 | + * @return string The sanitized 'order' query variable. |
|
| 485 | + */ |
|
| 486 | + protected function parse_order( $order ) { |
|
| 487 | + if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 488 | + return 'DESC'; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + if ( 'ASC' === strtoupper( $order ) ) { |
|
| 492 | + return 'ASC'; |
|
| 493 | + } else { |
|
| 494 | + return 'DESC'; |
|
| 495 | + } |
|
| 496 | + } |
|
| 497 | 497 | |
| 498 | 498 | } |
@@ -97,9 +97,9 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @param null|string|array $query Optional. The query variables. |
| 99 | 99 | */ |
| 100 | - public function __construct( $query = null ) { |
|
| 101 | - if ( ! is_null( $query ) ) { |
|
| 102 | - $this->prepare_query( $query ); |
|
| 100 | + public function __construct($query = null) { |
|
| 101 | + if (!is_null($query)) { |
|
| 102 | + $this->prepare_query($query); |
|
| 103 | 103 | $this->query(); |
| 104 | 104 | } |
| 105 | 105 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | * @param array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
| 113 | 113 | * @return array Complete query variables with undefined ones filled in with defaults. |
| 114 | 114 | */ |
| 115 | - public static function fill_query_vars( $args ) { |
|
| 115 | + public static function fill_query_vars($args) { |
|
| 116 | 116 | $defaults = array( |
| 117 | 117 | 'status' => 'all', |
| 118 | 118 | 'customer_in' => array(), |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | 'fields' => 'all', |
| 131 | 131 | ); |
| 132 | 132 | |
| 133 | - return wp_parse_args( $args, $defaults ); |
|
| 133 | + return wp_parse_args($args, $defaults); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -180,45 +180,45 @@ discard block |
||
| 180 | 180 | * Use 'all' for all fields. Default 'all'. |
| 181 | 181 | * } |
| 182 | 182 | */ |
| 183 | - public function prepare_query( $query = array() ) { |
|
| 183 | + public function prepare_query($query = array()) { |
|
| 184 | 184 | global $wpdb; |
| 185 | 185 | |
| 186 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 186 | + if (empty($this->query_vars) || !empty($query)) { |
|
| 187 | 187 | $this->query_limit = null; |
| 188 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
| 188 | + $this->query_vars = $this->fill_query_vars($query); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 192 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 191 | + if (!empty($this->query_vars['fields']) && 'all' !== $this->query_vars['fields']) { |
|
| 192 | + $this->query_vars['fields'] = wpinv_parse_list($this->query_vars['fields']); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
| 195 | + do_action('getpaid_pre_get_subscriptions', array(&$this)); |
|
| 196 | 196 | |
| 197 | 197 | // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
| 198 | - $qv =& $this->query_vars; |
|
| 199 | - $qv = $this->fill_query_vars( $qv ); |
|
| 198 | + $qv = & $this->query_vars; |
|
| 199 | + $qv = $this->fill_query_vars($qv); |
|
| 200 | 200 | $table = $wpdb->prefix . 'wpinv_subscriptions'; |
| 201 | 201 | $this->query_from = "FROM $table"; |
| 202 | 202 | |
| 203 | 203 | // Prepare query fields. |
| 204 | - $this->prepare_query_fields( $qv, $table ); |
|
| 204 | + $this->prepare_query_fields($qv, $table); |
|
| 205 | 205 | |
| 206 | 206 | // Prepare query where. |
| 207 | - $this->prepare_query_where( $qv, $table ); |
|
| 207 | + $this->prepare_query_where($qv, $table); |
|
| 208 | 208 | |
| 209 | 209 | // Prepare query order. |
| 210 | - $this->prepare_query_order( $qv, $table ); |
|
| 210 | + $this->prepare_query_order($qv, $table); |
|
| 211 | 211 | |
| 212 | 212 | // limit |
| 213 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 214 | - if ( $qv['offset'] ) { |
|
| 215 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 213 | + if (isset($qv['number']) && $qv['number'] > 0) { |
|
| 214 | + if ($qv['offset']) { |
|
| 215 | + $this->query_limit = $wpdb->prepare('LIMIT %d, %d', $qv['offset'], $qv['number']); |
|
| 216 | 216 | } else { |
| 217 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 217 | + $this->query_limit = $wpdb->prepare('LIMIT %d, %d', $qv['number'] * ($qv['paged'] - 1), $qv['number']); |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
| 221 | + do_action_ref_array('getpaid_after_subscriptions_query', array(&$this)); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -229,24 +229,24 @@ discard block |
||
| 229 | 229 | * @param array $qv Query vars. |
| 230 | 230 | * @param string $table Table name. |
| 231 | 231 | */ |
| 232 | - protected function prepare_query_fields( &$qv, $table ) { |
|
| 232 | + protected function prepare_query_fields(&$qv, $table) { |
|
| 233 | 233 | |
| 234 | - if ( is_array( $qv['fields'] ) ) { |
|
| 235 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 234 | + if (is_array($qv['fields'])) { |
|
| 235 | + $qv['fields'] = array_unique($qv['fields']); |
|
| 236 | 236 | |
| 237 | 237 | $this->query_fields = array(); |
| 238 | - foreach ( $qv['fields'] as $field ) { |
|
| 239 | - $field = 'id' === strtolower( $field ) ? 'id' : sanitize_key( $field ); |
|
| 238 | + foreach ($qv['fields'] as $field) { |
|
| 239 | + $field = 'id' === strtolower($field) ? 'id' : sanitize_key($field); |
|
| 240 | 240 | $this->query_fields[] = "$table.`$field`"; |
| 241 | 241 | } |
| 242 | - $this->query_fields = implode( ',', $this->query_fields ); |
|
| 243 | - } elseif ( 'all' === $qv['fields'] ) { |
|
| 242 | + $this->query_fields = implode(',', $this->query_fields); |
|
| 243 | + } elseif ('all' === $qv['fields']) { |
|
| 244 | 244 | $this->query_fields = "$table.*"; |
| 245 | 245 | } else { |
| 246 | 246 | $this->query_fields = "$table.id"; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 249 | + if (isset($qv['count_total']) && $qv['count_total']) { |
|
| 250 | 250 | $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
| 251 | 251 | } |
| 252 | 252 | |
@@ -260,50 +260,50 @@ discard block |
||
| 260 | 260 | * @param array $qv Query vars. |
| 261 | 261 | * @param string $table Table name. |
| 262 | 262 | */ |
| 263 | - protected function prepare_query_where( &$qv, $table ) { |
|
| 263 | + protected function prepare_query_where(&$qv, $table) { |
|
| 264 | 264 | global $wpdb; |
| 265 | 265 | $this->query_where = 'WHERE 1=1'; |
| 266 | 266 | |
| 267 | 267 | // Status. |
| 268 | - if ( 'all' !== $qv['status'] ) { |
|
| 269 | - $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
| 270 | - $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
| 271 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
| 268 | + if ('all' !== $qv['status']) { |
|
| 269 | + $statuses = wpinv_clean(wpinv_parse_list($qv['status'])); |
|
| 270 | + $prepared_statuses = join(',', array_fill(0, count($statuses), '%s')); |
|
| 271 | + $this->query_where .= $wpdb->prepare(" AND $table.`status` IN ( $prepared_statuses )", $statuses); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - if ( ! empty( $qv['customer_in'] ) ) { |
|
| 275 | - $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
| 274 | + if (!empty($qv['customer_in'])) { |
|
| 275 | + $customer_in = implode(',', wp_parse_id_list($qv['customer_in'])); |
|
| 276 | 276 | $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
| 277 | - } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
| 278 | - $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
| 277 | + } elseif (!empty($qv['customer_not_in'])) { |
|
| 278 | + $customer_not_in = implode(',', wp_parse_id_list($qv['customer_not_in'])); |
|
| 279 | 279 | $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - if ( ! empty( $qv['product_in'] ) ) { |
|
| 283 | - $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
| 282 | + if (!empty($qv['product_in'])) { |
|
| 283 | + $product_in = implode(',', wp_parse_id_list($qv['product_in'])); |
|
| 284 | 284 | $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
| 285 | - } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
| 286 | - $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
| 285 | + } elseif (!empty($qv['product_not_in'])) { |
|
| 286 | + $product_not_in = implode(',', wp_parse_id_list($qv['product_not_in'])); |
|
| 287 | 287 | $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
| 288 | 288 | } |
| 289 | 289 | |
| 290 | - if ( ! empty( $qv['include'] ) ) { |
|
| 291 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 290 | + if (!empty($qv['include'])) { |
|
| 291 | + $include = implode(',', wp_parse_id_list($qv['include'])); |
|
| 292 | 292 | $this->query_where .= " AND $table.`id` IN ($include)"; |
| 293 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 294 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 293 | + } elseif (!empty($qv['exclude'])) { |
|
| 294 | + $exclude = implode(',', wp_parse_id_list($qv['exclude'])); |
|
| 295 | 295 | $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | // Date queries are allowed for the subscription creation date. |
| 299 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 300 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
| 299 | + if (!empty($qv['date_created_query']) && is_array($qv['date_created_query'])) { |
|
| 300 | + $date_created_query = new WP_Date_Query($qv['date_created_query'], "$table.created"); |
|
| 301 | 301 | $this->query_where .= $date_created_query->get_sql(); |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | // Date queries are also allowed for the subscription expiration date. |
| 305 | - if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
| 306 | - $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
| 305 | + if (!empty($qv['date_expires_query']) && is_array($qv['date_expires_query'])) { |
|
| 306 | + $date_expires_query = new WP_Date_Query($qv['date_expires_query'], "$table.expiration"); |
|
| 307 | 307 | $this->query_where .= $date_expires_query->get_sql(); |
| 308 | 308 | } |
| 309 | 309 | |
@@ -317,24 +317,24 @@ discard block |
||
| 317 | 317 | * @param array $qv Query vars. |
| 318 | 318 | * @param string $table Table name. |
| 319 | 319 | */ |
| 320 | - protected function prepare_query_order( &$qv, $table ) { |
|
| 320 | + protected function prepare_query_order(&$qv, $table) { |
|
| 321 | 321 | |
| 322 | 322 | // sorting. |
| 323 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 324 | - $order = $this->parse_order( $qv['order'] ); |
|
| 323 | + $qv['order'] = isset($qv['order']) ? strtoupper($qv['order']) : ''; |
|
| 324 | + $order = $this->parse_order($qv['order']); |
|
| 325 | 325 | |
| 326 | 326 | // Default order is by 'id' (latest subscriptions). |
| 327 | - if ( empty( $qv['orderby'] ) ) { |
|
| 328 | - $ordersby = array( 'id' ); |
|
| 327 | + if (empty($qv['orderby'])) { |
|
| 328 | + $ordersby = array('id'); |
|
| 329 | 329 | } |
| 330 | 330 | |
| 331 | 331 | // 'orderby' values may be an array, comma- or space-separated list. |
| 332 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 332 | + $ordersby = array_filter(wpinv_parse_list($qv['orderby'])); |
|
| 333 | 333 | |
| 334 | 334 | $orderby_array = array(); |
| 335 | - foreach ( $ordersby as $_key => $_value ) { |
|
| 335 | + foreach ($ordersby as $_key => $_value) { |
|
| 336 | 336 | |
| 337 | - if ( is_int( $_key ) ) { |
|
| 337 | + if (is_int($_key)) { |
|
| 338 | 338 | // Integer key means this is a flat array of 'orderby' fields. |
| 339 | 339 | $_orderby = $_value; |
| 340 | 340 | $_order = $order; |
@@ -344,20 +344,20 @@ discard block |
||
| 344 | 344 | $_order = $_value; |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 347 | + $parsed = $this->parse_orderby($_orderby, $table); |
|
| 348 | 348 | |
| 349 | - if ( $parsed ) { |
|
| 350 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 349 | + if ($parsed) { |
|
| 350 | + $orderby_array[] = $parsed . ' ' . $this->parse_order($_order); |
|
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | // If no valid clauses were found, order by id. |
| 356 | - if ( empty( $orderby_array ) ) { |
|
| 356 | + if (empty($orderby_array)) { |
|
| 357 | 357 | $orderby_array[] = "id $order"; |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 360 | + $this->query_orderby = 'ORDER BY ' . implode(', ', $orderby_array); |
|
| 361 | 361 | |
| 362 | 362 | } |
| 363 | 363 | |
@@ -371,30 +371,30 @@ discard block |
||
| 371 | 371 | public function query() { |
| 372 | 372 | global $wpdb; |
| 373 | 373 | |
| 374 | - $qv =& $this->query_vars; |
|
| 374 | + $qv = & $this->query_vars; |
|
| 375 | 375 | |
| 376 | 376 | // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
| 377 | 377 | // total_subscriptions property. |
| 378 | - $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
| 378 | + $this->results = apply_filters_ref_array('getpaid_subscriptions_pre_query', array(null, &$this)); |
|
| 379 | 379 | |
| 380 | - if ( null === $this->results ) { |
|
| 380 | + if (null === $this->results) { |
|
| 381 | 381 | $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
| 382 | 382 | |
| 383 | - if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
| 384 | - $this->results = $wpdb->get_results( $this->request ); |
|
| 383 | + if ((is_array($qv['fields']) && 1 != count($qv['fields'])) || 'all' == $qv['fields']) { |
|
| 384 | + $this->results = $wpdb->get_results($this->request); |
|
| 385 | 385 | } else { |
| 386 | - $this->results = $wpdb->get_col( $this->request ); |
|
| 386 | + $this->results = $wpdb->get_col($this->request); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 390 | - $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 391 | - $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
| 389 | + if (isset($qv['count_total']) && $qv['count_total']) { |
|
| 390 | + $found_subscriptions_query = apply_filters('getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this); |
|
| 391 | + $this->total_subscriptions = (int) $wpdb->get_var($found_subscriptions_query); |
|
| 392 | 392 | } |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - if ( 'all' == $qv['fields'] ) { |
|
| 396 | - foreach ( $this->results as $key => $subscription ) { |
|
| 397 | - $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
| 395 | + if ('all' == $qv['fields']) { |
|
| 396 | + foreach ($this->results as $key => $subscription) { |
|
| 397 | + $this->results[$key] = new WPInv_Subscription($subscription); |
|
| 398 | 398 | } |
| 399 | 399 | } |
| 400 | 400 | |
@@ -408,9 +408,9 @@ discard block |
||
| 408 | 408 | * @param string $query_var Query variable key. |
| 409 | 409 | * @return mixed |
| 410 | 410 | */ |
| 411 | - public function get( $query_var ) { |
|
| 412 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 413 | - return $this->query_vars[ $query_var ]; |
|
| 411 | + public function get($query_var) { |
|
| 412 | + if (isset($this->query_vars[$query_var])) { |
|
| 413 | + return $this->query_vars[$query_var]; |
|
| 414 | 414 | } |
| 415 | 415 | |
| 416 | 416 | return null; |
@@ -424,8 +424,8 @@ discard block |
||
| 424 | 424 | * @param string $query_var Query variable key. |
| 425 | 425 | * @param mixed $value Query variable value. |
| 426 | 426 | */ |
| 427 | - public function set( $query_var, $value ) { |
|
| 428 | - $this->query_vars[ $query_var ] = $value; |
|
| 427 | + public function set($query_var, $value) { |
|
| 428 | + $this->query_vars[$query_var] = $value; |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | /** |
@@ -459,16 +459,16 @@ discard block |
||
| 459 | 459 | * @param string $table The current table. |
| 460 | 460 | * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
| 461 | 461 | */ |
| 462 | - protected function parse_orderby( $orderby, $table ) { |
|
| 462 | + protected function parse_orderby($orderby, $table) { |
|
| 463 | 463 | |
| 464 | 464 | $_orderby = ''; |
| 465 | - if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
| 465 | + if (in_array($orderby, array('customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id'))) { |
|
| 466 | 466 | $_orderby = "$table.`$orderby`"; |
| 467 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 467 | + } elseif ('id' === strtolower($orderby)) { |
|
| 468 | 468 | $_orderby = "$table.id"; |
| 469 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 470 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 471 | - $include_sql = implode( ',', $include ); |
|
| 469 | + } elseif ('include' === $orderby && !empty($this->query_vars['include'])) { |
|
| 470 | + $include = wp_parse_id_list($this->query_vars['include']); |
|
| 471 | + $include_sql = implode(',', $include); |
|
| 472 | 472 | $_orderby = "FIELD( $table.id, $include_sql )"; |
| 473 | 473 | } |
| 474 | 474 | |
@@ -483,12 +483,12 @@ discard block |
||
| 483 | 483 | * @param string $order The 'order' query variable. |
| 484 | 484 | * @return string The sanitized 'order' query variable. |
| 485 | 485 | */ |
| 486 | - protected function parse_order( $order ) { |
|
| 487 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 486 | + protected function parse_order($order) { |
|
| 487 | + if (!is_string($order) || empty($order)) { |
|
| 488 | 488 | return 'DESC'; |
| 489 | 489 | } |
| 490 | 490 | |
| 491 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
| 491 | + if ('ASC' === strtoupper($order)) { |
|
| 492 | 492 | return 'ASC'; |
| 493 | 493 | } else { |
| 494 | 494 | return 'DESC'; |
@@ -7,86 +7,86 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | // MUST have WordPress. |
| 10 | -if ( !defined( 'WPINC' ) ) { |
|
| 11 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
| 10 | +if (!defined('WPINC')) { |
|
| 11 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | function wpinv_item_quantities_enabled() { |
| 15 | - $ret = wpinv_get_option( 'item_quantities', true ); |
|
| 15 | + $ret = wpinv_get_option('item_quantities', true); |
|
| 16 | 16 | |
| 17 | - return (bool) apply_filters( 'wpinv_item_quantities_enabled', $ret ); |
|
| 17 | + return (bool) apply_filters('wpinv_item_quantities_enabled', $ret); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | function wpinv_get_ip() { |
| 21 | 21 | $ip = '127.0.0.1'; |
| 22 | 22 | |
| 23 | - if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
|
| 24 | - $ip = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); |
|
| 25 | - } elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
|
| 26 | - $ip = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); |
|
| 27 | - } elseif( !empty( $_SERVER['REMOTE_ADDR'] ) ) { |
|
| 28 | - $ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); |
|
| 23 | + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
|
| 24 | + $ip = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']); |
|
| 25 | + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
| 26 | + $ip = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']); |
|
| 27 | + } elseif (!empty($_SERVER['REMOTE_ADDR'])) { |
|
| 28 | + $ip = sanitize_text_field($_SERVER['REMOTE_ADDR']); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - return apply_filters( 'wpinv_get_ip', $ip ); |
|
| 31 | + return apply_filters('wpinv_get_ip', $ip); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | function wpinv_get_user_agent() { |
| 35 | - if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
|
| 36 | - $user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); |
|
| 35 | + if (!empty($_SERVER['HTTP_USER_AGENT'])) { |
|
| 36 | + $user_agent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']); |
|
| 37 | 37 | } else { |
| 38 | 38 | $user_agent = ''; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - return apply_filters( 'wpinv_get_user_agent', $user_agent ); |
|
| 41 | + return apply_filters('wpinv_get_user_agent', $user_agent); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | -function wpinv_sanitize_amount( $amount, $decimals = NULL ) { |
|
| 44 | +function wpinv_sanitize_amount($amount, $decimals = NULL) { |
|
| 45 | 45 | $is_negative = false; |
| 46 | 46 | $thousands_sep = wpinv_thousands_separator(); |
| 47 | 47 | $decimal_sep = wpinv_decimal_separator(); |
| 48 | - if ( $decimals === NULL ) { |
|
| 48 | + if ($decimals === NULL) { |
|
| 49 | 49 | $decimals = wpinv_decimals(); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | // Sanitize the amount |
| 53 | - if ( $decimal_sep == ',' && false !== ( $found = strpos( $amount, $decimal_sep ) ) ) { |
|
| 54 | - if ( ( $thousands_sep == '.' || $thousands_sep == ' ' ) && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 55 | - $amount = str_replace( $thousands_sep, '', $amount ); |
|
| 56 | - } elseif( empty( $thousands_sep ) && false !== ( $found = strpos( $amount, '.' ) ) ) { |
|
| 57 | - $amount = str_replace( '.', '', $amount ); |
|
| 53 | + if ($decimal_sep == ',' && false !== ($found = strpos($amount, $decimal_sep))) { |
|
| 54 | + if (($thousands_sep == '.' || $thousands_sep == ' ') && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 55 | + $amount = str_replace($thousands_sep, '', $amount); |
|
| 56 | + } elseif (empty($thousands_sep) && false !== ($found = strpos($amount, '.'))) { |
|
| 57 | + $amount = str_replace('.', '', $amount); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - $amount = str_replace( $decimal_sep, '.', $amount ); |
|
| 61 | - } elseif( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 62 | - $amount = str_replace( $thousands_sep, '', $amount ); |
|
| 60 | + $amount = str_replace($decimal_sep, '.', $amount); |
|
| 61 | + } elseif ($thousands_sep == ',' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 62 | + $amount = str_replace($thousands_sep, '', $amount); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if( $amount < 0 ) { |
|
| 65 | + if ($amount < 0) { |
|
| 66 | 66 | $is_negative = true; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - $amount = preg_replace( '/[^0-9\.]/', '', $amount ); |
|
| 69 | + $amount = preg_replace('/[^0-9\.]/', '', $amount); |
|
| 70 | 70 | |
| 71 | - $decimals = apply_filters( 'wpinv_sanitize_amount_decimals', absint( $decimals ), $amount ); |
|
| 72 | - $amount = number_format( (double) $amount, absint( $decimals ), '.', '' ); |
|
| 71 | + $decimals = apply_filters('wpinv_sanitize_amount_decimals', absint($decimals), $amount); |
|
| 72 | + $amount = number_format((double) $amount, absint($decimals), '.', ''); |
|
| 73 | 73 | |
| 74 | - if( $is_negative ) { |
|
| 74 | + if ($is_negative) { |
|
| 75 | 75 | $amount *= -1; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - return apply_filters( 'wpinv_sanitize_amount', $amount, $decimals ); |
|
| 78 | + return apply_filters('wpinv_sanitize_amount', $amount, $decimals); |
|
| 79 | 79 | } |
| 80 | -add_filter( 'wpinv_sanitize_amount_decimals', 'wpinv_currency_decimal_filter', 10, 1 ); |
|
| 80 | +add_filter('wpinv_sanitize_amount_decimals', 'wpinv_currency_decimal_filter', 10, 1); |
|
| 81 | 81 | |
| 82 | -function wpinv_round_amount( $amount, $decimals = NULL ) { |
|
| 83 | - if ( $decimals === NULL ) { |
|
| 82 | +function wpinv_round_amount($amount, $decimals = NULL) { |
|
| 83 | + if ($decimals === NULL) { |
|
| 84 | 84 | $decimals = wpinv_decimals(); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - $amount = round( (double)$amount, wpinv_currency_decimal_filter( absint( $decimals ) ) ); |
|
| 87 | + $amount = round((double) $amount, wpinv_currency_decimal_filter(absint($decimals))); |
|
| 88 | 88 | |
| 89 | - return apply_filters( 'wpinv_round_amount', $amount, $decimals ); |
|
| 89 | + return apply_filters('wpinv_round_amount', $amount, $decimals); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
@@ -95,32 +95,32 @@ discard block |
||
| 95 | 95 | * @since 1.0.19 |
| 96 | 96 | * @return array |
| 97 | 97 | */ |
| 98 | -function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
|
| 98 | +function wpinv_get_invoice_statuses($draft = false, $trashed = false, $invoice = false) { |
|
| 99 | 99 | $invoice_statuses = array( |
| 100 | - 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
| 101 | - 'publish' => _x( 'Paid', 'Invoice status', 'invoicing' ), |
|
| 102 | - 'wpi-processing' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
|
| 103 | - 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
| 104 | - 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
| 105 | - 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
| 106 | - 'wpi-failed' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
|
| 107 | - 'wpi-renewal' => _x( 'Renewal Payment', 'Invoice status', 'invoicing' ), |
|
| 100 | + 'wpi-pending' => _x('Pending payment', 'Invoice status', 'invoicing'), |
|
| 101 | + 'publish' => _x('Paid', 'Invoice status', 'invoicing'), |
|
| 102 | + 'wpi-processing' => _x('Processing', 'Invoice status', 'invoicing'), |
|
| 103 | + 'wpi-onhold' => _x('On hold', 'Invoice status', 'invoicing'), |
|
| 104 | + 'wpi-cancelled' => _x('Cancelled', 'Invoice status', 'invoicing'), |
|
| 105 | + 'wpi-refunded' => _x('Refunded', 'Invoice status', 'invoicing'), |
|
| 106 | + 'wpi-failed' => _x('Failed', 'Invoice status', 'invoicing'), |
|
| 107 | + 'wpi-renewal' => _x('Renewal Payment', 'Invoice status', 'invoicing'), |
|
| 108 | 108 | ); |
| 109 | 109 | |
| 110 | - if ( $draft ) { |
|
| 111 | - $invoice_statuses['draft'] = __( 'Draft', 'invoicing' ); |
|
| 110 | + if ($draft) { |
|
| 111 | + $invoice_statuses['draft'] = __('Draft', 'invoicing'); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - if ( $trashed ) { |
|
| 115 | - $invoice_statuses['trash'] = __( 'Trash', 'invoicing' ); |
|
| 114 | + if ($trashed) { |
|
| 115 | + $invoice_statuses['trash'] = __('Trash', 'invoicing'); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 118 | + return apply_filters('wpinv_statuses', $invoice_statuses, $invoice); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | -function wpinv_status_nicename( $status ) { |
|
| 122 | - $statuses = wpinv_get_invoice_statuses( true, true ); |
|
| 123 | - $status = isset( $statuses[$status] ) ? $statuses[$status] : __( $status, 'invoicing' ); |
|
| 121 | +function wpinv_status_nicename($status) { |
|
| 122 | + $statuses = wpinv_get_invoice_statuses(true, true); |
|
| 123 | + $status = isset($statuses[$status]) ? $statuses[$status] : __($status, 'invoicing'); |
|
| 124 | 124 | |
| 125 | 125 | return $status; |
| 126 | 126 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | * Retrieves the default currency code. |
| 130 | 130 | */ |
| 131 | 131 | function wpinv_get_currency() { |
| 132 | - return apply_filters( 'wpinv_currency', wpinv_get_option( 'currency', 'USD' ) ); |
|
| 132 | + return apply_filters('wpinv_currency', wpinv_get_option('currency', 'USD')); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /** |
@@ -137,61 +137,61 @@ discard block |
||
| 137 | 137 | * |
| 138 | 138 | * @param string|null $currency The currency code. Defaults to the default currency. |
| 139 | 139 | */ |
| 140 | -function wpinv_currency_symbol( $currency = null ) { |
|
| 140 | +function wpinv_currency_symbol($currency = null) { |
|
| 141 | 141 | |
| 142 | 142 | // Prepare the currency. |
| 143 | - $currency = empty( $currency ) ? wpinv_get_currency() : wpinv_clean( $currency ); |
|
| 143 | + $currency = empty($currency) ? wpinv_get_currency() : wpinv_clean($currency); |
|
| 144 | 144 | |
| 145 | 145 | // Fetch all symbols. |
| 146 | 146 | $symbols = wpinv_get_currency_symbols(); |
| 147 | 147 | |
| 148 | 148 | // Fetch this currencies symbol. |
| 149 | - $currency_symbol = isset( $symbols[$currency] ) ? $symbols[$currency] : $currency; |
|
| 149 | + $currency_symbol = isset($symbols[$currency]) ? $symbols[$currency] : $currency; |
|
| 150 | 150 | |
| 151 | 151 | // Filter the symbol. |
| 152 | - return apply_filters( 'wpinv_currency_symbol', $currency_symbol, $currency ); |
|
| 152 | + return apply_filters('wpinv_currency_symbol', $currency_symbol, $currency); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | function wpinv_currency_position() { |
| 156 | - $position = wpinv_get_option( 'currency_position', 'left' ); |
|
| 156 | + $position = wpinv_get_option('currency_position', 'left'); |
|
| 157 | 157 | |
| 158 | - return apply_filters( 'wpinv_currency_position', $position ); |
|
| 158 | + return apply_filters('wpinv_currency_position', $position); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | function wpinv_thousands_separator() { |
| 162 | - $thousand_sep = wpinv_get_option( 'thousands_separator', ',' ); |
|
| 162 | + $thousand_sep = wpinv_get_option('thousands_separator', ','); |
|
| 163 | 163 | |
| 164 | - return apply_filters( 'wpinv_thousands_separator', $thousand_sep ); |
|
| 164 | + return apply_filters('wpinv_thousands_separator', $thousand_sep); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | function wpinv_decimal_separator() { |
| 168 | - $decimal_sep = wpinv_get_option( 'decimal_separator', '.' ); |
|
| 168 | + $decimal_sep = wpinv_get_option('decimal_separator', '.'); |
|
| 169 | 169 | |
| 170 | - return apply_filters( 'wpinv_decimal_separator', $decimal_sep ); |
|
| 170 | + return apply_filters('wpinv_decimal_separator', $decimal_sep); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | function wpinv_decimals() { |
| 174 | - $decimals = apply_filters( 'wpinv_decimals', wpinv_get_option( 'decimals', 2 ) ); |
|
| 174 | + $decimals = apply_filters('wpinv_decimals', wpinv_get_option('decimals', 2)); |
|
| 175 | 175 | |
| 176 | - return absint( $decimals ); |
|
| 176 | + return absint($decimals); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
| 180 | 180 | * Retrieves a list of all supported currencies. |
| 181 | 181 | */ |
| 182 | 182 | function wpinv_get_currencies() { |
| 183 | - return apply_filters( 'wpinv_currencies', wpinv_get_data( 'currencies' ) ); |
|
| 183 | + return apply_filters('wpinv_currencies', wpinv_get_data('currencies')); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | /** |
| 187 | 187 | * Retrieves a list of all currency symbols. |
| 188 | 188 | */ |
| 189 | 189 | function wpinv_get_currency_symbols() { |
| 190 | - return apply_filters( 'wpinv_currency_symbols', wpinv_get_data( 'currency-symbols' ) ); |
|
| 190 | + return apply_filters('wpinv_currency_symbols', wpinv_get_data('currency-symbols')); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | -function wpinv_price( $amount = '', $currency = '' ) { |
|
| 194 | - if( empty( $currency ) ) { |
|
| 193 | +function wpinv_price($amount = '', $currency = '') { |
|
| 194 | + if (empty($currency)) { |
|
| 195 | 195 | $currency = wpinv_get_currency(); |
| 196 | 196 | } |
| 197 | 197 | |
@@ -199,14 +199,14 @@ discard block |
||
| 199 | 199 | |
| 200 | 200 | $negative = $amount < 0; |
| 201 | 201 | |
| 202 | - if ( $negative ) { |
|
| 203 | - $amount = substr( $amount, 1 ); |
|
| 202 | + if ($negative) { |
|
| 203 | + $amount = substr($amount, 1); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - $symbol = wpinv_currency_symbol( $currency ); |
|
| 206 | + $symbol = wpinv_currency_symbol($currency); |
|
| 207 | 207 | |
| 208 | - if ( $position == 'left' || $position == 'left_space' ) { |
|
| 209 | - switch ( $currency ) { |
|
| 208 | + if ($position == 'left' || $position == 'left_space') { |
|
| 209 | + switch ($currency) { |
|
| 210 | 210 | case "GBP" : |
| 211 | 211 | case "BRL" : |
| 212 | 212 | case "EUR" : |
@@ -218,15 +218,15 @@ discard block |
||
| 218 | 218 | case "NZD" : |
| 219 | 219 | case "SGD" : |
| 220 | 220 | case "JPY" : |
| 221 | - $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 221 | + $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 222 | 222 | break; |
| 223 | 223 | default : |
| 224 | 224 | //$price = $currency . ' ' . $amount; |
| 225 | - $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 225 | + $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 226 | 226 | break; |
| 227 | 227 | } |
| 228 | 228 | } else { |
| 229 | - switch ( $currency ) { |
|
| 229 | + switch ($currency) { |
|
| 230 | 230 | case "GBP" : |
| 231 | 231 | case "BRL" : |
| 232 | 232 | case "EUR" : |
@@ -237,83 +237,83 @@ discard block |
||
| 237 | 237 | case "MXN" : |
| 238 | 238 | case "SGD" : |
| 239 | 239 | case "JPY" : |
| 240 | - $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 240 | + $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 241 | 241 | break; |
| 242 | 242 | default : |
| 243 | 243 | //$price = $amount . ' ' . $currency; |
| 244 | - $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 244 | + $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 245 | 245 | break; |
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if ( $negative ) { |
|
| 249 | + if ($negative) { |
|
| 250 | 250 | $price = '-' . $price; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - $price = apply_filters( 'wpinv_' . strtolower( $currency ) . '_currency_filter_' . $position, $price, $currency, $amount ); |
|
| 253 | + $price = apply_filters('wpinv_' . strtolower($currency) . '_currency_filter_' . $position, $price, $currency, $amount); |
|
| 254 | 254 | |
| 255 | 255 | return $price; |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | -function wpinv_format_amount( $amount, $decimals = NULL, $calculate = false ) { |
|
| 258 | +function wpinv_format_amount($amount, $decimals = NULL, $calculate = false) { |
|
| 259 | 259 | $thousands_sep = wpinv_thousands_separator(); |
| 260 | 260 | $decimal_sep = wpinv_decimal_separator(); |
| 261 | 261 | |
| 262 | - if ( $decimals === NULL ) { |
|
| 262 | + if ($decimals === NULL) { |
|
| 263 | 263 | $decimals = wpinv_decimals(); |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - if ( $decimal_sep == ',' && false !== ( $sep_found = strpos( $amount, $decimal_sep ) ) ) { |
|
| 267 | - $whole = substr( $amount, 0, $sep_found ); |
|
| 268 | - $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) ); |
|
| 266 | + if ($decimal_sep == ',' && false !== ($sep_found = strpos($amount, $decimal_sep))) { |
|
| 267 | + $whole = substr($amount, 0, $sep_found); |
|
| 268 | + $part = substr($amount, $sep_found + 1, (strlen($amount) - 1)); |
|
| 269 | 269 | $amount = $whole . '.' . $part; |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - if ( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 273 | - $amount = str_replace( ',', '', $amount ); |
|
| 272 | + if ($thousands_sep == ',' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 273 | + $amount = str_replace(',', '', $amount); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | - if ( $thousands_sep == ' ' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 277 | - $amount = str_replace( ' ', '', $amount ); |
|
| 276 | + if ($thousands_sep == ' ' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 277 | + $amount = str_replace(' ', '', $amount); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - if ( empty( $amount ) ) { |
|
| 280 | + if (empty($amount)) { |
|
| 281 | 281 | $amount = 0; |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | - $decimals = apply_filters( 'wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate ); |
|
| 285 | - $formatted = number_format( (float)$amount, $decimals, $decimal_sep, $thousands_sep ); |
|
| 284 | + $decimals = apply_filters('wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate); |
|
| 285 | + $formatted = number_format((float) $amount, $decimals, $decimal_sep, $thousands_sep); |
|
| 286 | 286 | |
| 287 | - if ( $calculate ) { |
|
| 288 | - if ( $thousands_sep === "," ) { |
|
| 289 | - $formatted = str_replace( ",", "", $formatted ); |
|
| 287 | + if ($calculate) { |
|
| 288 | + if ($thousands_sep === ",") { |
|
| 289 | + $formatted = str_replace(",", "", $formatted); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - if ( $decimal_sep === "," ) { |
|
| 293 | - $formatted = str_replace( ",", ".", $formatted ); |
|
| 292 | + if ($decimal_sep === ",") { |
|
| 293 | + $formatted = str_replace(",", ".", $formatted); |
|
| 294 | 294 | } |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | - return apply_filters( 'wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate ); |
|
| 297 | + return apply_filters('wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate); |
|
| 298 | 298 | } |
| 299 | -add_filter( 'wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1 ); |
|
| 299 | +add_filter('wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1); |
|
| 300 | 300 | |
| 301 | -function wpinv_sanitize_key( $key ) { |
|
| 301 | +function wpinv_sanitize_key($key) { |
|
| 302 | 302 | $raw_key = $key; |
| 303 | - $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key ); |
|
| 303 | + $key = preg_replace('/[^a-zA-Z0-9_\-\.\:\/]/', '', $key); |
|
| 304 | 304 | |
| 305 | - return apply_filters( 'wpinv_sanitize_key', $key, $raw_key ); |
|
| 305 | + return apply_filters('wpinv_sanitize_key', $key, $raw_key); |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | -function wpinv_get_file_extension( $str ) { |
|
| 309 | - $parts = explode( '.', $str ); |
|
| 310 | - return end( $parts ); |
|
| 308 | +function wpinv_get_file_extension($str) { |
|
| 309 | + $parts = explode('.', $str); |
|
| 310 | + return end($parts); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | -function wpinv_string_is_image_url( $str ) { |
|
| 314 | - $ext = wpinv_get_file_extension( $str ); |
|
| 313 | +function wpinv_string_is_image_url($str) { |
|
| 314 | + $ext = wpinv_get_file_extension($str); |
|
| 315 | 315 | |
| 316 | - switch ( strtolower( $ext ) ) { |
|
| 316 | + switch (strtolower($ext)) { |
|
| 317 | 317 | case 'jpeg'; |
| 318 | 318 | case 'jpg'; |
| 319 | 319 | $return = true; |
@@ -329,33 +329,33 @@ discard block |
||
| 329 | 329 | break; |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | - return (bool)apply_filters( 'wpinv_string_is_image', $return, $str ); |
|
| 332 | + return (bool) apply_filters('wpinv_string_is_image', $return, $str); |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | -function wpinv_error_log( $log, $title = '', $file = '', $line = '', $exit = false ) { |
|
| 336 | - $should_log = apply_filters( 'wpinv_log_errors', WP_DEBUG ); |
|
| 335 | +function wpinv_error_log($log, $title = '', $file = '', $line = '', $exit = false) { |
|
| 336 | + $should_log = apply_filters('wpinv_log_errors', WP_DEBUG); |
|
| 337 | 337 | |
| 338 | - if ( true === $should_log ) { |
|
| 338 | + if (true === $should_log) { |
|
| 339 | 339 | $label = ''; |
| 340 | - if ( $file && $file !== '' ) { |
|
| 341 | - $label .= basename( $file ) . ( $line ? '(' . $line . ')' : '' ); |
|
| 340 | + if ($file && $file !== '') { |
|
| 341 | + $label .= basename($file) . ($line ? '(' . $line . ')' : ''); |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | - if ( $title && $title !== '' ) { |
|
| 344 | + if ($title && $title !== '') { |
|
| 345 | 345 | $label = $label !== '' ? $label . ' ' : ''; |
| 346 | 346 | $label .= $title . ' '; |
| 347 | 347 | } |
| 348 | 348 | |
| 349 | - $label = $label !== '' ? trim( $label ) . ' : ' : ''; |
|
| 349 | + $label = $label !== '' ? trim($label) . ' : ' : ''; |
|
| 350 | 350 | |
| 351 | - if ( is_array( $log ) || is_object( $log ) ) { |
|
| 352 | - error_log( $label . print_r( $log, true ) ); |
|
| 351 | + if (is_array($log) || is_object($log)) { |
|
| 352 | + error_log($label . print_r($log, true)); |
|
| 353 | 353 | } else { |
| 354 | - error_log( $label . $log ); |
|
| 354 | + error_log($label . $log); |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | - error_log( wp_debug_backtrace_summary() ); |
|
| 358 | - if ( $exit ) { |
|
| 357 | + error_log(wp_debug_backtrace_summary()); |
|
| 358 | + if ($exit) { |
|
| 359 | 359 | exit; |
| 360 | 360 | } |
| 361 | 361 | } |
@@ -363,32 +363,32 @@ discard block |
||
| 363 | 363 | |
| 364 | 364 | function wpinv_is_ajax_disabled() { |
| 365 | 365 | $retval = false; |
| 366 | - return apply_filters( 'wpinv_is_ajax_disabled', $retval ); |
|
| 366 | + return apply_filters('wpinv_is_ajax_disabled', $retval); |
|
| 367 | 367 | } |
| 368 | 368 | |
| 369 | -function wpinv_get_current_page_url( $nocache = false ) { |
|
| 369 | +function wpinv_get_current_page_url($nocache = false) { |
|
| 370 | 370 | global $wp; |
| 371 | 371 | |
| 372 | - if ( get_option( 'permalink_structure' ) ) { |
|
| 373 | - $base = trailingslashit( home_url( $wp->request ) ); |
|
| 372 | + if (get_option('permalink_structure')) { |
|
| 373 | + $base = trailingslashit(home_url($wp->request)); |
|
| 374 | 374 | } else { |
| 375 | - $base = add_query_arg( $wp->query_string, '', trailingslashit( home_url( $wp->request ) ) ); |
|
| 376 | - $base = remove_query_arg( array( 'post_type', 'name' ), $base ); |
|
| 375 | + $base = add_query_arg($wp->query_string, '', trailingslashit(home_url($wp->request))); |
|
| 376 | + $base = remove_query_arg(array('post_type', 'name'), $base); |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | $scheme = is_ssl() ? 'https' : 'http'; |
| 380 | - $uri = set_url_scheme( $base, $scheme ); |
|
| 380 | + $uri = set_url_scheme($base, $scheme); |
|
| 381 | 381 | |
| 382 | - if ( is_front_page() ) { |
|
| 383 | - $uri = home_url( '/' ); |
|
| 384 | - } elseif ( wpinv_is_checkout( array(), false ) ) { |
|
| 382 | + if (is_front_page()) { |
|
| 383 | + $uri = home_url('/'); |
|
| 384 | + } elseif (wpinv_is_checkout(array(), false)) { |
|
| 385 | 385 | $uri = wpinv_get_checkout_uri(); |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | - $uri = apply_filters( 'wpinv_get_current_page_url', $uri ); |
|
| 388 | + $uri = apply_filters('wpinv_get_current_page_url', $uri); |
|
| 389 | 389 | |
| 390 | - if ( $nocache ) { |
|
| 391 | - $uri = wpinv_add_cache_busting( $uri ); |
|
| 390 | + if ($nocache) { |
|
| 391 | + $uri = wpinv_add_cache_busting($uri); |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | return $uri; |
@@ -401,46 +401,46 @@ discard block |
||
| 401 | 401 | * @param string $name Constant name. |
| 402 | 402 | * @param mixed $value Value. |
| 403 | 403 | */ |
| 404 | -function getpaid_maybe_define_constant( $name, $value ) { |
|
| 405 | - if ( ! defined( $name ) ) { |
|
| 406 | - define( $name, $value ); |
|
| 404 | +function getpaid_maybe_define_constant($name, $value) { |
|
| 405 | + if (!defined($name)) { |
|
| 406 | + define($name, $value); |
|
| 407 | 407 | } |
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | function wpinv_get_php_arg_separator_output() { |
| 411 | - return ini_get( 'arg_separator.output' ); |
|
| 411 | + return ini_get('arg_separator.output'); |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | -function wpinv_rgb_from_hex( $color ) { |
|
| 415 | - $color = str_replace( '#', '', $color ); |
|
| 414 | +function wpinv_rgb_from_hex($color) { |
|
| 415 | + $color = str_replace('#', '', $color); |
|
| 416 | 416 | |
| 417 | 417 | // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF" |
| 418 | - $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color ); |
|
| 419 | - if ( empty( $color ) ) { |
|
| 418 | + $color = preg_replace('~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color); |
|
| 419 | + if (empty($color)) { |
|
| 420 | 420 | return NULL; |
| 421 | 421 | } |
| 422 | 422 | |
| 423 | - $color = str_split( $color ); |
|
| 423 | + $color = str_split($color); |
|
| 424 | 424 | |
| 425 | 425 | $rgb = array(); |
| 426 | - $rgb['R'] = hexdec( $color[0] . $color[1] ); |
|
| 427 | - $rgb['G'] = hexdec( $color[2] . $color[3] ); |
|
| 428 | - $rgb['B'] = hexdec( $color[4] . $color[5] ); |
|
| 426 | + $rgb['R'] = hexdec($color[0] . $color[1]); |
|
| 427 | + $rgb['G'] = hexdec($color[2] . $color[3]); |
|
| 428 | + $rgb['B'] = hexdec($color[4] . $color[5]); |
|
| 429 | 429 | |
| 430 | 430 | return $rgb; |
| 431 | 431 | } |
| 432 | 432 | |
| 433 | -function wpinv_hex_darker( $color, $factor = 30 ) { |
|
| 434 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 433 | +function wpinv_hex_darker($color, $factor = 30) { |
|
| 434 | + $base = wpinv_rgb_from_hex($color); |
|
| 435 | 435 | $color = '#'; |
| 436 | 436 | |
| 437 | - foreach ( $base as $k => $v ) { |
|
| 437 | + foreach ($base as $k => $v) { |
|
| 438 | 438 | $amount = $v / 100; |
| 439 | - $amount = round( $amount * $factor ); |
|
| 439 | + $amount = round($amount * $factor); |
|
| 440 | 440 | $new_decimal = $v - $amount; |
| 441 | 441 | |
| 442 | - $new_hex_component = dechex( $new_decimal ); |
|
| 443 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 442 | + $new_hex_component = dechex($new_decimal); |
|
| 443 | + if (strlen($new_hex_component) < 2) { |
|
| 444 | 444 | $new_hex_component = "0" . $new_hex_component; |
| 445 | 445 | } |
| 446 | 446 | $color .= $new_hex_component; |
@@ -449,18 +449,18 @@ discard block |
||
| 449 | 449 | return $color; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | -function wpinv_hex_lighter( $color, $factor = 30 ) { |
|
| 453 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 452 | +function wpinv_hex_lighter($color, $factor = 30) { |
|
| 453 | + $base = wpinv_rgb_from_hex($color); |
|
| 454 | 454 | $color = '#'; |
| 455 | 455 | |
| 456 | - foreach ( $base as $k => $v ) { |
|
| 456 | + foreach ($base as $k => $v) { |
|
| 457 | 457 | $amount = 255 - $v; |
| 458 | 458 | $amount = $amount / 100; |
| 459 | - $amount = round( $amount * $factor ); |
|
| 459 | + $amount = round($amount * $factor); |
|
| 460 | 460 | $new_decimal = $v + $amount; |
| 461 | 461 | |
| 462 | - $new_hex_component = dechex( $new_decimal ); |
|
| 463 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 462 | + $new_hex_component = dechex($new_decimal); |
|
| 463 | + if (strlen($new_hex_component) < 2) { |
|
| 464 | 464 | $new_hex_component = "0" . $new_hex_component; |
| 465 | 465 | } |
| 466 | 466 | $color .= $new_hex_component; |
@@ -469,22 +469,22 @@ discard block |
||
| 469 | 469 | return $color; |
| 470 | 470 | } |
| 471 | 471 | |
| 472 | -function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
|
| 473 | - $hex = str_replace( '#', '', $color ); |
|
| 472 | +function wpinv_light_or_dark($color, $dark = '#000000', $light = '#FFFFFF') { |
|
| 473 | + $hex = str_replace('#', '', $color); |
|
| 474 | 474 | |
| 475 | - $c_r = hexdec( substr( $hex, 0, 2 ) ); |
|
| 476 | - $c_g = hexdec( substr( $hex, 2, 2 ) ); |
|
| 477 | - $c_b = hexdec( substr( $hex, 4, 2 ) ); |
|
| 475 | + $c_r = hexdec(substr($hex, 0, 2)); |
|
| 476 | + $c_g = hexdec(substr($hex, 2, 2)); |
|
| 477 | + $c_b = hexdec(substr($hex, 4, 2)); |
|
| 478 | 478 | |
| 479 | - $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
|
| 479 | + $brightness = (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000; |
|
| 480 | 480 | |
| 481 | 481 | return $brightness > 155 ? $dark : $light; |
| 482 | 482 | } |
| 483 | 483 | |
| 484 | -function wpinv_format_hex( $hex ) { |
|
| 485 | - $hex = trim( str_replace( '#', '', $hex ) ); |
|
| 484 | +function wpinv_format_hex($hex) { |
|
| 485 | + $hex = trim(str_replace('#', '', $hex)); |
|
| 486 | 486 | |
| 487 | - if ( strlen( $hex ) == 3 ) { |
|
| 487 | + if (strlen($hex) == 3) { |
|
| 488 | 488 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
| 489 | 489 | } |
| 490 | 490 | |
@@ -504,12 +504,12 @@ discard block |
||
| 504 | 504 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 505 | 505 | * @return string |
| 506 | 506 | */ |
| 507 | -function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) { |
|
| 508 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 509 | - return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding ); |
|
| 507 | +function wpinv_utf8_strimwidth($str, $start, $width, $trimmaker = '', $encoding = 'UTF-8') { |
|
| 508 | + if (function_exists('mb_strimwidth')) { |
|
| 509 | + return mb_strimwidth($str, $start, $width, $trimmaker, $encoding); |
|
| 510 | 510 | } |
| 511 | 511 | |
| 512 | - return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker; |
|
| 512 | + return wpinv_utf8_substr($str, $start, $width, $encoding) . $trimmaker; |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | /** |
@@ -521,28 +521,28 @@ discard block |
||
| 521 | 521 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 522 | 522 | * @return int Returns the number of characters in string. |
| 523 | 523 | */ |
| 524 | -function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) { |
|
| 525 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 526 | - return mb_strlen( $str, $encoding ); |
|
| 524 | +function wpinv_utf8_strlen($str, $encoding = 'UTF-8') { |
|
| 525 | + if (function_exists('mb_strlen')) { |
|
| 526 | + return mb_strlen($str, $encoding); |
|
| 527 | 527 | } |
| 528 | 528 | |
| 529 | - return strlen( $str ); |
|
| 529 | + return strlen($str); |
|
| 530 | 530 | } |
| 531 | 531 | |
| 532 | -function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) { |
|
| 533 | - if ( function_exists( 'mb_strtolower' ) ) { |
|
| 534 | - return mb_strtolower( $str, $encoding ); |
|
| 532 | +function wpinv_utf8_strtolower($str, $encoding = 'UTF-8') { |
|
| 533 | + if (function_exists('mb_strtolower')) { |
|
| 534 | + return mb_strtolower($str, $encoding); |
|
| 535 | 535 | } |
| 536 | 536 | |
| 537 | - return strtolower( $str ); |
|
| 537 | + return strtolower($str); |
|
| 538 | 538 | } |
| 539 | 539 | |
| 540 | -function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) { |
|
| 541 | - if ( function_exists( 'mb_strtoupper' ) ) { |
|
| 542 | - return mb_strtoupper( $str, $encoding ); |
|
| 540 | +function wpinv_utf8_strtoupper($str, $encoding = 'UTF-8') { |
|
| 541 | + if (function_exists('mb_strtoupper')) { |
|
| 542 | + return mb_strtoupper($str, $encoding); |
|
| 543 | 543 | } |
| 544 | 544 | |
| 545 | - return strtoupper( $str ); |
|
| 545 | + return strtoupper($str); |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | /** |
@@ -556,12 +556,12 @@ discard block |
||
| 556 | 556 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 557 | 557 | * @return int Returns the position of the first occurrence of search in the string. |
| 558 | 558 | */ |
| 559 | -function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 560 | - if ( function_exists( 'mb_strpos' ) ) { |
|
| 561 | - return mb_strpos( $str, $find, $offset, $encoding ); |
|
| 559 | +function wpinv_utf8_strpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 560 | + if (function_exists('mb_strpos')) { |
|
| 561 | + return mb_strpos($str, $find, $offset, $encoding); |
|
| 562 | 562 | } |
| 563 | 563 | |
| 564 | - return strpos( $str, $find, $offset ); |
|
| 564 | + return strpos($str, $find, $offset); |
|
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | /** |
@@ -575,12 +575,12 @@ discard block |
||
| 575 | 575 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 576 | 576 | * @return int Returns the position of the last occurrence of search. |
| 577 | 577 | */ |
| 578 | -function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 579 | - if ( function_exists( 'mb_strrpos' ) ) { |
|
| 580 | - return mb_strrpos( $str, $find, $offset, $encoding ); |
|
| 578 | +function wpinv_utf8_strrpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 579 | + if (function_exists('mb_strrpos')) { |
|
| 580 | + return mb_strrpos($str, $find, $offset, $encoding); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | - return strrpos( $str, $find, $offset ); |
|
| 583 | + return strrpos($str, $find, $offset); |
|
| 584 | 584 | } |
| 585 | 585 | |
| 586 | 586 | /** |
@@ -595,16 +595,16 @@ discard block |
||
| 595 | 595 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 596 | 596 | * @return string |
| 597 | 597 | */ |
| 598 | -function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) { |
|
| 599 | - if ( function_exists( 'mb_substr' ) ) { |
|
| 600 | - if ( $length === null ) { |
|
| 601 | - return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 598 | +function wpinv_utf8_substr($str, $start, $length = null, $encoding = 'UTF-8') { |
|
| 599 | + if (function_exists('mb_substr')) { |
|
| 600 | + if ($length === null) { |
|
| 601 | + return mb_substr($str, $start, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 602 | 602 | } else { |
| 603 | - return mb_substr( $str, $start, $length, $encoding ); |
|
| 603 | + return mb_substr($str, $start, $length, $encoding); |
|
| 604 | 604 | } |
| 605 | 605 | } |
| 606 | 606 | |
| 607 | - return substr( $str, $start, $length ); |
|
| 607 | + return substr($str, $start, $length); |
|
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | /** |
@@ -616,48 +616,48 @@ discard block |
||
| 616 | 616 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 617 | 617 | * @return string The width of string. |
| 618 | 618 | */ |
| 619 | -function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) { |
|
| 620 | - if ( function_exists( 'mb_strwidth' ) ) { |
|
| 621 | - return mb_strwidth( $str, $encoding ); |
|
| 619 | +function wpinv_utf8_strwidth($str, $encoding = 'UTF-8') { |
|
| 620 | + if (function_exists('mb_strwidth')) { |
|
| 621 | + return mb_strwidth($str, $encoding); |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | - return wpinv_utf8_strlen( $str, $encoding ); |
|
| 624 | + return wpinv_utf8_strlen($str, $encoding); |
|
| 625 | 625 | } |
| 626 | 626 | |
| 627 | -function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) { |
|
| 628 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 629 | - $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding ); |
|
| 627 | +function wpinv_utf8_ucfirst($str, $lower_str_end = false, $encoding = 'UTF-8') { |
|
| 628 | + if (function_exists('mb_strlen')) { |
|
| 629 | + $first_letter = wpinv_utf8_strtoupper(wpinv_utf8_substr($str, 0, 1, $encoding), $encoding); |
|
| 630 | 630 | $str_end = ""; |
| 631 | 631 | |
| 632 | - if ( $lower_str_end ) { |
|
| 633 | - $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding ); |
|
| 632 | + if ($lower_str_end) { |
|
| 633 | + $str_end = wpinv_utf8_strtolower(wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding), $encoding); |
|
| 634 | 634 | } else { |
| 635 | - $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 635 | + $str_end = wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 636 | 636 | } |
| 637 | 637 | |
| 638 | 638 | return $first_letter . $str_end; |
| 639 | 639 | } |
| 640 | 640 | |
| 641 | - return ucfirst( $str ); |
|
| 641 | + return ucfirst($str); |
|
| 642 | 642 | } |
| 643 | 643 | |
| 644 | -function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) { |
|
| 645 | - if ( function_exists( 'mb_convert_case' ) ) { |
|
| 646 | - return mb_convert_case( $str, MB_CASE_TITLE, $encoding ); |
|
| 644 | +function wpinv_utf8_ucwords($str, $encoding = 'UTF-8') { |
|
| 645 | + if (function_exists('mb_convert_case')) { |
|
| 646 | + return mb_convert_case($str, MB_CASE_TITLE, $encoding); |
|
| 647 | 647 | } |
| 648 | 648 | |
| 649 | - return ucwords( $str ); |
|
| 649 | + return ucwords($str); |
|
| 650 | 650 | } |
| 651 | 651 | |
| 652 | -function wpinv_period_in_days( $period, $unit ) { |
|
| 653 | - $period = absint( $period ); |
|
| 652 | +function wpinv_period_in_days($period, $unit) { |
|
| 653 | + $period = absint($period); |
|
| 654 | 654 | |
| 655 | - if ( $period > 0 ) { |
|
| 656 | - if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) { |
|
| 655 | + if ($period > 0) { |
|
| 656 | + if (in_array(strtolower($unit), array('w', 'week', 'weeks'))) { |
|
| 657 | 657 | $period = $period * 7; |
| 658 | - } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) { |
|
| 658 | + } else if (in_array(strtolower($unit), array('m', 'month', 'months'))) { |
|
| 659 | 659 | $period = $period * 30; |
| 660 | - } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) { |
|
| 660 | + } else if (in_array(strtolower($unit), array('y', 'year', 'years'))) { |
|
| 661 | 661 | $period = $period * 365; |
| 662 | 662 | } |
| 663 | 663 | } |
@@ -665,14 +665,14 @@ discard block |
||
| 665 | 665 | return $period; |
| 666 | 666 | } |
| 667 | 667 | |
| 668 | -function wpinv_cal_days_in_month( $calendar, $month, $year ) { |
|
| 669 | - if ( function_exists( 'cal_days_in_month' ) ) { |
|
| 670 | - return cal_days_in_month( $calendar, $month, $year ); |
|
| 668 | +function wpinv_cal_days_in_month($calendar, $month, $year) { |
|
| 669 | + if (function_exists('cal_days_in_month')) { |
|
| 670 | + return cal_days_in_month($calendar, $month, $year); |
|
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | // Fallback in case the calendar extension is not loaded in PHP |
| 674 | 674 | // Only supports Gregorian calendar |
| 675 | - return date( 't', mktime( 0, 0, 0, $month, 1, $year ) ); |
|
| 675 | + return date('t', mktime(0, 0, 0, $month, 1, $year)); |
|
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | /** |
@@ -683,11 +683,11 @@ discard block |
||
| 683 | 683 | * |
| 684 | 684 | * @return string |
| 685 | 685 | */ |
| 686 | -function wpi_help_tip( $tip, $allow_html = false ) { |
|
| 687 | - if ( $allow_html ) { |
|
| 688 | - $tip = wpi_sanitize_tooltip( $tip ); |
|
| 686 | +function wpi_help_tip($tip, $allow_html = false) { |
|
| 687 | + if ($allow_html) { |
|
| 688 | + $tip = wpi_sanitize_tooltip($tip); |
|
| 689 | 689 | } else { |
| 690 | - $tip = esc_attr( $tip ); |
|
| 690 | + $tip = esc_attr($tip); |
|
| 691 | 691 | } |
| 692 | 692 | |
| 693 | 693 | return '<span class="wpi-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
@@ -701,8 +701,8 @@ discard block |
||
| 701 | 701 | * @param string $var |
| 702 | 702 | * @return string |
| 703 | 703 | */ |
| 704 | -function wpi_sanitize_tooltip( $var ) { |
|
| 705 | - return htmlspecialchars( wp_kses( html_entity_decode( $var ), array( |
|
| 704 | +function wpi_sanitize_tooltip($var) { |
|
| 705 | + return htmlspecialchars(wp_kses(html_entity_decode($var), array( |
|
| 706 | 706 | 'br' => array(), |
| 707 | 707 | 'em' => array(), |
| 708 | 708 | 'strong' => array(), |
@@ -712,7 +712,7 @@ discard block |
||
| 712 | 712 | 'li' => array(), |
| 713 | 713 | 'ol' => array(), |
| 714 | 714 | 'p' => array(), |
| 715 | - ) ) ); |
|
| 715 | + ))); |
|
| 716 | 716 | } |
| 717 | 717 | |
| 718 | 718 | /** |
@@ -722,7 +722,7 @@ discard block |
||
| 722 | 722 | */ |
| 723 | 723 | function wpinv_get_screen_ids() { |
| 724 | 724 | |
| 725 | - $screen_id = sanitize_title( __( 'Invoicing', 'invoicing' ) ); |
|
| 725 | + $screen_id = sanitize_title(__('Invoicing', 'invoicing')); |
|
| 726 | 726 | |
| 727 | 727 | $screen_ids = array( |
| 728 | 728 | 'toplevel_page_' . $screen_id, |
@@ -740,7 +740,7 @@ discard block |
||
| 740 | 740 | 'invoicing_page_wpi-addons', |
| 741 | 741 | ); |
| 742 | 742 | |
| 743 | - return apply_filters( 'wpinv_screen_ids', $screen_ids ); |
|
| 743 | + return apply_filters('wpinv_screen_ids', $screen_ids); |
|
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | /** |
@@ -751,14 +751,14 @@ discard block |
||
| 751 | 751 | * @param array|string $list List of values. |
| 752 | 752 | * @return array Sanitized array of values. |
| 753 | 753 | */ |
| 754 | -function wpinv_parse_list( $list ) { |
|
| 754 | +function wpinv_parse_list($list) { |
|
| 755 | 755 | |
| 756 | - if ( empty( $list ) ) { |
|
| 756 | + if (empty($list)) { |
|
| 757 | 757 | $list = array(); |
| 758 | 758 | } |
| 759 | 759 | |
| 760 | - if ( ! is_array( $list ) ) { |
|
| 761 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 760 | + if (!is_array($list)) { |
|
| 761 | + return preg_split('/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY); |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | return $list; |
@@ -772,16 +772,16 @@ discard block |
||
| 772 | 772 | * @param string $key Type of data to fetch. |
| 773 | 773 | * @return mixed Fetched data. |
| 774 | 774 | */ |
| 775 | -function wpinv_get_data( $key ) { |
|
| 775 | +function wpinv_get_data($key) { |
|
| 776 | 776 | |
| 777 | 777 | // Try fetching it from the cache. |
| 778 | - $data = wp_cache_get( "wpinv-data-$key", 'wpinv' ); |
|
| 779 | - if( $data ) { |
|
| 778 | + $data = wp_cache_get("wpinv-data-$key", 'wpinv'); |
|
| 779 | + if ($data) { |
|
| 780 | 780 | return $data; |
| 781 | 781 | } |
| 782 | 782 | |
| 783 | - $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
|
| 784 | - wp_cache_set( "wpinv-data-$key", $data, 'wpinv' ); |
|
| 783 | + $data = apply_filters("wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php"); |
|
| 784 | + wp_cache_set("wpinv-data-$key", $data, 'wpinv'); |
|
| 785 | 785 | |
| 786 | 786 | return $data; |
| 787 | 787 | } |
@@ -795,10 +795,10 @@ discard block |
||
| 795 | 795 | * @param bool $first_empty Whether or not the first item in the list should be empty |
| 796 | 796 | * @return mixed Fetched data. |
| 797 | 797 | */ |
| 798 | -function wpinv_maybe_add_empty_option( $options, $first_empty ) { |
|
| 798 | +function wpinv_maybe_add_empty_option($options, $first_empty) { |
|
| 799 | 799 | |
| 800 | - if ( ! empty( $options ) && $first_empty ) { |
|
| 801 | - return array_merge( array( '' => '' ), $options ); |
|
| 800 | + if (!empty($options) && $first_empty) { |
|
| 801 | + return array_merge(array('' => ''), $options); |
|
| 802 | 802 | } |
| 803 | 803 | return $options; |
| 804 | 804 | |
@@ -810,21 +810,21 @@ discard block |
||
| 810 | 810 | * @param mixed $var Data to sanitize. |
| 811 | 811 | * @return string|array |
| 812 | 812 | */ |
| 813 | -function wpinv_clean( $var ) { |
|
| 813 | +function wpinv_clean($var) { |
|
| 814 | 814 | |
| 815 | - if ( is_array( $var ) ) { |
|
| 816 | - return array_map( 'wpinv_clean', $var ); |
|
| 815 | + if (is_array($var)) { |
|
| 816 | + return array_map('wpinv_clean', $var); |
|
| 817 | 817 | } |
| 818 | 818 | |
| 819 | - if ( is_object( $var ) ) { |
|
| 820 | - $object_vars = get_object_vars( $var ); |
|
| 821 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
| 822 | - $var->$property_name = wpinv_clean( $property_value ); |
|
| 819 | + if (is_object($var)) { |
|
| 820 | + $object_vars = get_object_vars($var); |
|
| 821 | + foreach ($object_vars as $property_name => $property_value) { |
|
| 822 | + $var->$property_name = wpinv_clean($property_value); |
|
| 823 | 823 | } |
| 824 | 824 | return $var; |
| 825 | 825 | } |
| 826 | 826 | |
| 827 | - return is_string( $var ) ? sanitize_text_field( $var ) : $var; |
|
| 827 | + return is_string($var) ? sanitize_text_field($var) : $var; |
|
| 828 | 828 | } |
| 829 | 829 | |
| 830 | 830 | /** |
@@ -833,43 +833,43 @@ discard block |
||
| 833 | 833 | * @param string $str Data to convert. |
| 834 | 834 | * @return string|array |
| 835 | 835 | */ |
| 836 | -function getpaid_convert_price_string_to_options( $str ) { |
|
| 836 | +function getpaid_convert_price_string_to_options($str) { |
|
| 837 | 837 | |
| 838 | - $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
| 839 | - $options = array(); |
|
| 838 | + $raw_options = array_map('trim', explode(',', $str)); |
|
| 839 | + $options = array(); |
|
| 840 | 840 | |
| 841 | - foreach ( $raw_options as $option ) { |
|
| 841 | + foreach ($raw_options as $option) { |
|
| 842 | 842 | |
| 843 | - if ( '' == $option ) { |
|
| 843 | + if ('' == $option) { |
|
| 844 | 844 | continue; |
| 845 | 845 | } |
| 846 | 846 | |
| 847 | - $option = array_map( 'trim', explode( '|', $option ) ); |
|
| 847 | + $option = array_map('trim', explode('|', $option)); |
|
| 848 | 848 | |
| 849 | 849 | $price = null; |
| 850 | 850 | $label = null; |
| 851 | 851 | |
| 852 | - if ( isset( $option[0] ) && '' != $option[0] ) { |
|
| 853 | - $label = $option[0]; |
|
| 852 | + if (isset($option[0]) && '' != $option[0]) { |
|
| 853 | + $label = $option[0]; |
|
| 854 | 854 | } |
| 855 | 855 | |
| 856 | - if ( isset( $option[1] ) && '' != $option[1] ) { |
|
| 856 | + if (isset($option[1]) && '' != $option[1]) { |
|
| 857 | 857 | $price = $option[1]; |
| 858 | 858 | } |
| 859 | 859 | |
| 860 | - if ( ! isset( $price ) ) { |
|
| 860 | + if (!isset($price)) { |
|
| 861 | 861 | $price = $label; |
| 862 | 862 | } |
| 863 | 863 | |
| 864 | - if ( ! isset( $price ) || ! is_numeric( $price ) ) { |
|
| 864 | + if (!isset($price) || !is_numeric($price)) { |
|
| 865 | 865 | continue; |
| 866 | 866 | } |
| 867 | 867 | |
| 868 | - if ( ! isset( $label ) ) { |
|
| 868 | + if (!isset($label)) { |
|
| 869 | 869 | $label = $price; |
| 870 | 870 | } |
| 871 | 871 | |
| 872 | - $options[ $price ] = $label; |
|
| 872 | + $options[$price] = $label; |
|
| 873 | 873 | } |
| 874 | 874 | |
| 875 | 875 | return $options; |
@@ -878,23 +878,23 @@ discard block |
||
| 878 | 878 | /** |
| 879 | 879 | * Returns the help tip. |
| 880 | 880 | */ |
| 881 | -function getpaid_get_help_tip( $tip, $additional_classes = '' ) { |
|
| 882 | - $additional_classes = sanitize_html_class( $additional_classes ); |
|
| 883 | - $tip = esc_attr__( $tip ); |
|
| 881 | +function getpaid_get_help_tip($tip, $additional_classes = '') { |
|
| 882 | + $additional_classes = sanitize_html_class($additional_classes); |
|
| 883 | + $tip = esc_attr__($tip); |
|
| 884 | 884 | return "<span class='wpi-help-tip dashicons dashicons-editor-help $additional_classes' title='$tip'></span>"; |
| 885 | 885 | } |
| 886 | 886 | |
| 887 | 887 | /** |
| 888 | 888 | * Formats a date |
| 889 | 889 | */ |
| 890 | -function getpaid_format_date( $date ) { |
|
| 890 | +function getpaid_format_date($date) { |
|
| 891 | 891 | |
| 892 | - if ( empty( $date ) || $date == '0000-00-00 00:00:00' ) { |
|
| 892 | + if (empty($date) || $date == '0000-00-00 00:00:00') { |
|
| 893 | 893 | return ''; |
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | |
| 897 | - return date_i18n( get_option( 'date_format' ), strtotime( $date ) ); |
|
| 897 | + return date_i18n(get_option('date_format'), strtotime($date)); |
|
| 898 | 898 | |
| 899 | 899 | } |
| 900 | 900 | |
@@ -905,16 +905,16 @@ discard block |
||
| 905 | 905 | * @param integer $limit Limit size in characters. |
| 906 | 906 | * @return string |
| 907 | 907 | */ |
| 908 | -function getpaid_limit_length( $string, $limit ) { |
|
| 908 | +function getpaid_limit_length($string, $limit) { |
|
| 909 | 909 | $str_limit = $limit - 3; |
| 910 | 910 | |
| 911 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 912 | - if ( mb_strlen( $string ) > $limit ) { |
|
| 913 | - $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
|
| 911 | + if (function_exists('mb_strimwidth')) { |
|
| 912 | + if (mb_strlen($string) > $limit) { |
|
| 913 | + $string = mb_strimwidth($string, 0, $str_limit) . '...'; |
|
| 914 | 914 | } |
| 915 | 915 | } else { |
| 916 | - if ( strlen( $string ) > $limit ) { |
|
| 917 | - $string = substr( $string, 0, $str_limit ) . '...'; |
|
| 916 | + if (strlen($string) > $limit) { |
|
| 917 | + $string = substr($string, 0, $str_limit) . '...'; |
|
| 918 | 918 | } |
| 919 | 919 | } |
| 920 | 920 | return $string; |
@@ -928,7 +928,7 @@ discard block |
||
| 928 | 928 | * @since 1.0.19 |
| 929 | 929 | */ |
| 930 | 930 | function getpaid_api() { |
| 931 | - return getpaid()->get( 'api' ); |
|
| 931 | + return getpaid()->get('api'); |
|
| 932 | 932 | } |
| 933 | 933 | |
| 934 | 934 | /** |
@@ -938,7 +938,7 @@ discard block |
||
| 938 | 938 | * @since 1.0.19 |
| 939 | 939 | */ |
| 940 | 940 | function getpaid_post_types() { |
| 941 | - return getpaid()->get( 'post_types' ); |
|
| 941 | + return getpaid()->get('post_types'); |
|
| 942 | 942 | } |
| 943 | 943 | |
| 944 | 944 | /** |
@@ -948,7 +948,7 @@ discard block |
||
| 948 | 948 | * @since 1.0.19 |
| 949 | 949 | */ |
| 950 | 950 | function getpaid_session() { |
| 951 | - return getpaid()->get( 'session' ); |
|
| 951 | + return getpaid()->get('session'); |
|
| 952 | 952 | } |
| 953 | 953 | |
| 954 | 954 | /** |
@@ -958,7 +958,7 @@ discard block |
||
| 958 | 958 | * @since 1.0.19 |
| 959 | 959 | */ |
| 960 | 960 | function getpaid_notes() { |
| 961 | - return getpaid()->get( 'notes' ); |
|
| 961 | + return getpaid()->get('notes'); |
|
| 962 | 962 | } |
| 963 | 963 | |
| 964 | 964 | /** |
@@ -970,15 +970,15 @@ discard block |
||
| 970 | 970 | * |
| 971 | 971 | * @return int|array|WPInv_Subscription[]|GetPaid_Subscriptions_Query |
| 972 | 972 | */ |
| 973 | -function getpaid_get_subscriptions( $args = array(), $return = 'results' ) { |
|
| 973 | +function getpaid_get_subscriptions($args = array(), $return = 'results') { |
|
| 974 | 974 | |
| 975 | - $query = new GetPaid_Subscriptions_Query( $args ); |
|
| 975 | + $query = new GetPaid_Subscriptions_Query($args); |
|
| 976 | 976 | |
| 977 | - if ( 'results' == $return ) { |
|
| 977 | + if ('results' == $return) { |
|
| 978 | 978 | return $query->get_results(); |
| 979 | 979 | } |
| 980 | 980 | |
| 981 | - if ( 'count' == $return ) { |
|
| 981 | + if ('count' == $return) { |
|
| 982 | 982 | return $query->get_total(); |
| 983 | 983 | } |
| 984 | 984 | |