暂无描述

Popups.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Repository_Popups
  10. */
  11. class PUM_Repository_Popups extends PUM_Abstract_Repository_Posts {
  12. /**
  13. * @var string
  14. */
  15. protected $model = 'PUM_Model_Popup';
  16. /**
  17. * @return string
  18. */
  19. protected function get_post_type() {
  20. return 'popup';
  21. }
  22. /**
  23. * Build the args for WP Query.
  24. *
  25. * @param array $args
  26. *
  27. * @return array
  28. */
  29. protected function build_wp_query_args( $args = array() ) {
  30. // Ordering
  31. $orderby = array();
  32. // Meta Query
  33. if ( ! isset( $args['meta_query'] ) ) {
  34. $args['meta_query'] = array(
  35. 'relation' => 'AND',
  36. );
  37. }
  38. if ( isset( $args['popups'] ) ) {
  39. /**
  40. * If Looking for specific popups. No need for filtering.
  41. */
  42. $args['post__in'] = wp_parse_id_list( $args['popups'] );
  43. unset( $args['popups'] );
  44. }
  45. /**
  46. * Apply easy ordering options or allow setting it manually.
  47. */
  48. if ( ! isset( $args['orderby'] ) ) {
  49. $orderby['post_modified'] = isset( $args['order'] ) ? $args['order'] : 'DESC';
  50. } elseif ( ! empty( $args['post__in'] ) && in_array( $args['orderby'], array( 'post__in', 'user_order' ) ) ) {
  51. // This one can't be part of an $orderby array so needs to override.
  52. $orderby = 'post__in';
  53. } else {
  54. switch ( $args['orderby'] ) {
  55. case 'name' :
  56. $orderby['post_title'] = isset( $args['order'] ) ? $args['order'] : 'ASC';
  57. break;
  58. case 'date' :
  59. $orderby['post_date'] = isset( $args['order'] ) ? $args['order'] : 'DESC';
  60. break;
  61. case 'activity':
  62. $orderby['post_modified'] = isset( $args['order'] ) ? $args['order'] : 'DESC';
  63. break;
  64. default:
  65. $orderby[ $args['orderby'] ] = isset( $args['order'] ) ? $args['order'] : 'DESC';
  66. break;
  67. }
  68. }
  69. // Replace the orderby property with the new $orderby array.
  70. $args['orderby'] = $orderby;
  71. // Clear unneeded values.
  72. unset( $args['order'] );
  73. return parent::build_wp_query_args( $args );
  74. }
  75. /**
  76. * @param int $id
  77. *
  78. * @return PUM_Model_Popup|WP_Post
  79. * @throws \InvalidArgumentException
  80. */
  81. public function get_item( $id ) {
  82. return parent::get_item( $id );
  83. }
  84. /**
  85. * @param array $args
  86. *
  87. * @return PUM_Model_Popup[]|WP_Post[]
  88. */
  89. public function get_items( $args = array() ) {
  90. return parent::get_items( $args );
  91. }
  92. /**
  93. * @param array $data
  94. *
  95. * @return PUM_Model_Popup|WP_Post
  96. * @throws InvalidArgumentException
  97. */
  98. public function create_item( $data ) {
  99. return parent::create_item( $data );
  100. }
  101. /**
  102. * @param int $id
  103. * @param array $data
  104. *
  105. * @return PUM_Model_Popup|WP_Post
  106. * @throws Exception
  107. */
  108. public function update_item( $id, $data ) {
  109. return parent::update_item( $id, $data );
  110. }
  111. /**
  112. * Assert that data is valid.
  113. *
  114. * @param array $data
  115. *
  116. * @throws InvalidArgumentException
  117. */
  118. protected function assert_data( $data ) {
  119. // REQUIRED: Implement assert_data() method.
  120. }
  121. }