Sin descripción

ConditionCallbacks.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. class PUM_ConditionCallbacks {
  6. /**
  7. * Checks if this is one of the selected post_type items.
  8. *
  9. * @param array $condition
  10. *
  11. * @return bool
  12. */
  13. public static function post_type( $condition = array() ) {
  14. global $post;
  15. $target = explode( '_', $condition['target'] );
  16. // Modifier should be the last key.
  17. $modifier = array_pop( $target );
  18. // Post type is the remaining keys combined.
  19. $post_type = implode( '_', $target );
  20. $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
  21. switch ( $modifier ) {
  22. case 'index':
  23. if ( is_post_type_archive( $post_type ) ) {
  24. return true;
  25. }
  26. break;
  27. case 'all':
  28. // Checks for valid post type, if $post_type is page, then include the front page as most users simply expect this.
  29. if ( self::is_post_type( $post_type ) || ( $post_type == 'page' && is_front_page() ) ) {
  30. return true;
  31. }
  32. break;
  33. case 'ID':
  34. case 'selected':
  35. if ( self::is_post_type( $post_type ) && is_singular( $post_type ) && in_array( $post->ID, wp_parse_id_list( $selected ) ) ) {
  36. return true;
  37. }
  38. break;
  39. case 'children':
  40. if ( ! is_post_type_hierarchical( $post_type ) || ! is_singular( $post_type ) ) {
  41. return false;
  42. }
  43. // Chosen parents.
  44. $selected = wp_parse_id_list( $selected );
  45. foreach ( $selected as $id ) {
  46. if ( $post->post_parent == $id ) {
  47. return true;
  48. }
  49. }
  50. break;
  51. case 'ancestors':
  52. if ( ! is_post_type_hierarchical( $post_type ) || ! is_singular( $post_type ) ) {
  53. return false;
  54. }
  55. // Ancestors of the current page.
  56. $ancestors = get_post_ancestors( $post->ID );
  57. // Chosen parent/grandparents.
  58. $selected = wp_parse_id_list( $selected );
  59. foreach ( $selected as $id ) {
  60. if ( in_array( $id, $ancestors ) ) {
  61. return true;
  62. }
  63. }
  64. break;
  65. case 'template':
  66. if ( is_page() && is_page_template( $selected ) ) {
  67. return true;
  68. }
  69. break;
  70. }
  71. return false;
  72. }
  73. /**
  74. * Checks if this is one of the selected taxonomy term.
  75. *
  76. * @param array $condition
  77. *
  78. * @return bool
  79. */
  80. public static function taxonomy( $condition = array() ) {
  81. $target = explode( '_', $condition['target'] );
  82. // Remove the tax_ prefix.
  83. array_shift( $target );
  84. // Assign the last key as the modifier _all, _selected
  85. $modifier = array_pop( $target );
  86. // Whatever is left is the taxonomy.
  87. $taxonomy = implode( '_', $target );
  88. if ( $taxonomy == 'category' ) {
  89. return self::category( $condition );
  90. } elseif ( $taxonomy == 'post_tag' ) {
  91. return self::post_tag( $condition );
  92. }
  93. switch ( $modifier ) {
  94. case 'all':
  95. if ( is_tax( $taxonomy ) ) {
  96. return true;
  97. }
  98. break;
  99. case 'ID':
  100. case 'selected':
  101. $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
  102. if ( is_tax( $taxonomy, wp_parse_id_list( $selected ) ) ) {
  103. return true;
  104. }
  105. break;
  106. }
  107. return false;
  108. }
  109. /**
  110. * Checks if this is one of the selected categories.
  111. *
  112. * @param array $condition
  113. *
  114. * @return bool
  115. */
  116. public static function category( $condition = array() ) {
  117. $target = explode( '_', $condition['target'] );
  118. // Assign the last key as the modifier _all, _selected
  119. $modifier = array_pop( $target );
  120. switch ( $modifier ) {
  121. case 'all':
  122. if ( is_category() ) {
  123. return true;
  124. }
  125. break;
  126. case 'selected':
  127. $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
  128. if ( is_category( wp_parse_id_list( $selected ) ) ) {
  129. return true;
  130. }
  131. break;
  132. }
  133. return false;
  134. }
  135. /**
  136. * Checks if this is one of the selected tags.
  137. *
  138. * @param array $condition
  139. *
  140. * @return bool
  141. */
  142. public static function post_tag( $condition = array() ) {
  143. $target = explode( '_', $condition['target'] );
  144. // Assign the last key as the modifier _all, _selected
  145. $modifier = array_pop( $target );
  146. switch ( $modifier ) {
  147. case 'all':
  148. if ( is_tag() ) {
  149. return true;
  150. }
  151. break;
  152. case 'selected':
  153. $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
  154. if ( is_tag( wp_parse_id_list( $selected ) ) ) {
  155. return true;
  156. }
  157. break;
  158. }
  159. return false;
  160. }
  161. /**
  162. * Checks if the post_type has the selected categories.
  163. *
  164. * @param array $condition
  165. *
  166. * @return bool
  167. */
  168. public static function post_type_tax( $condition = array() ) {
  169. $target = explode( '_w_', $condition['target'] );
  170. // First key is the post type.
  171. $post_type = array_shift( $target );
  172. // Last Key is the taxonomy
  173. $taxonomy = array_pop( $target );
  174. if ( $taxonomy == 'category' ) {
  175. return self::post_type_category( $condition );
  176. } elseif ( $taxonomy == 'post_tag' ) {
  177. return self::post_type_tag( $condition );
  178. }
  179. $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
  180. if ( self::is_post_type( $post_type ) && has_term( wp_parse_id_list( $selected ), $taxonomy ) ) {
  181. return true;
  182. }
  183. return false;
  184. }
  185. /**
  186. * Checks if the post_type has the selected categories.
  187. *
  188. * @param array $condition
  189. *
  190. * @return bool
  191. */
  192. public static function post_type_category( $condition = array() ) {
  193. $target = explode( '_w_', $condition['target'] );
  194. // First key is the post type.
  195. $post_type = array_shift( $target );
  196. $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
  197. if ( self::is_post_type( $post_type ) && has_category( wp_parse_id_list( $selected ) ) ) {
  198. return true;
  199. }
  200. return false;
  201. }
  202. /**
  203. * Checks is a post_type has the selected tags.
  204. *
  205. * @param array $condition
  206. *
  207. * @return bool
  208. */
  209. public static function post_type_tag( $condition = array() ) {
  210. $target = explode( '_w_', $condition['target'] );
  211. // First key is the post type.
  212. $post_type = array_shift( $target );
  213. $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
  214. if ( self::is_post_type( $post_type ) && has_tag( wp_parse_id_list( $selected ) ) ) {
  215. return true;
  216. }
  217. return false;
  218. }
  219. public static function is_post_type( $post_type ) {
  220. global $post;
  221. return is_object( $post ) && ( is_singular( $post_type ) || $post->post_type == $post_type );
  222. }
  223. }