abstract-wc-object-query.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Query abstraction layer functionality.
  4. *
  5. * @package WooCommerce/Abstracts
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * Abstract WC Object Query Class
  12. *
  13. * Extended by classes to provide a query abstraction layer for safe object searching.
  14. *
  15. * @version 3.1.0
  16. * @package WooCommerce/Abstracts
  17. */
  18. abstract class WC_Object_Query {
  19. /**
  20. * Stores query data.
  21. *
  22. * @var array
  23. */
  24. protected $query_vars = array();
  25. /**
  26. * Create a new query.
  27. *
  28. * @param array $args Criteria to query on in a format similar to WP_Query.
  29. */
  30. public function __construct( $args = array() ) {
  31. $this->query_vars = wp_parse_args( $args, $this->get_default_query_vars() );
  32. }
  33. /**
  34. * Get the current query vars.
  35. *
  36. * @return array
  37. */
  38. public function get_query_vars() {
  39. return $this->query_vars;
  40. }
  41. /**
  42. * Get the value of a query variable.
  43. *
  44. * @param string $query_var Query variable to get value for.
  45. * @param mixed $default Default value if query variable is not set.
  46. * @return mixed Query variable value if set, otherwise default.
  47. */
  48. public function get( $query_var, $default = '' ) {
  49. if ( isset( $this->query_vars[ $query_var ] ) ) {
  50. return $this->query_vars[ $query_var ];
  51. }
  52. return $default;
  53. }
  54. /**
  55. * Set a query variable.
  56. *
  57. * @param string $query_var Query variable to set.
  58. * @param mixed $value Value to set for query variable.
  59. */
  60. public function set( $query_var, $value ) {
  61. $this->query_vars[ $query_var ] = $value;
  62. }
  63. /**
  64. * Get the default allowed query vars.
  65. *
  66. * @return array
  67. */
  68. protected function get_default_query_vars() {
  69. return array(
  70. 'name' => '',
  71. 'parent' => '',
  72. 'parent_exclude' => '',
  73. 'exclude' => '',
  74. 'limit' => get_option( 'posts_per_page' ),
  75. 'page' => 1,
  76. 'offset' => '',
  77. 'paginate' => false,
  78. 'order' => 'DESC',
  79. 'orderby' => 'date',
  80. 'return' => 'objects',
  81. );
  82. }
  83. }