Nenhuma Descrição

Conditions.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class Conditions
  10. */
  11. class PUM_Conditions {
  12. /**
  13. * @var
  14. */
  15. public static $instance;
  16. /**
  17. * @var bool
  18. */
  19. public $preload_posts = false;
  20. /**
  21. * @var array
  22. */
  23. public $conditions;
  24. /**
  25. * @var array
  26. */
  27. public $condition_sort_order = array();
  28. /**
  29. *
  30. */
  31. public static function init() {
  32. self::instance();
  33. }
  34. /**
  35. * @return self
  36. */
  37. public static function instance() {
  38. if ( ! isset( self::$instance ) ) {
  39. self::$instance = new self;
  40. self::$instance->preload_posts = popmake_is_admin_popup_page();
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * @param array $conditions
  46. */
  47. public function add_conditions( $conditions = array() ) {
  48. foreach ( $conditions as $key => $condition ) {
  49. if ( empty( $condition['id'] ) && ! is_numeric( $key ) ) {
  50. $condition['id'] = $key;
  51. }
  52. $this->add_condition( $condition );
  53. }
  54. }
  55. /**
  56. * @param array $condition
  57. */
  58. public function add_condition( $condition = array() ) {
  59. if ( ! empty( $condition['id'] ) && ! isset ( $this->conditions[ $condition['id'] ] ) ) {
  60. $condition = wp_parse_args( $condition, array(
  61. 'id' => '',
  62. 'callback' => null,
  63. 'group' => '',
  64. 'name' => '',
  65. 'priority' => 10,
  66. 'fields' => array(),
  67. 'advanced' => false,
  68. ) );
  69. $this->conditions[ $condition['id'] ] = $condition;
  70. }
  71. return;
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function get_conditions() {
  77. if ( ! isset( $this->conditions ) ) {
  78. $this->register_conditions();
  79. }
  80. return $this->conditions;
  81. }
  82. /**
  83. * @return array|mixed
  84. */
  85. public function condition_sort_order() {
  86. if ( ! $this->condition_sort_order ) {
  87. $order = array(
  88. __( 'General', 'popup-maker' ) => 1,
  89. __( 'Pages', 'popup-maker' ) => 5,
  90. __( 'Posts', 'popup-maker' ) => 5,
  91. __( 'Categories', 'popup-maker' ) => 14,
  92. __( 'Tags', 'popup-maker' ) => 14,
  93. __( 'Format', 'popup-maker' ) => 16,
  94. );
  95. $post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
  96. foreach ( $post_types as $name => $post_type ) {
  97. $order[ $post_type->labels->name ] = 10;
  98. }
  99. $taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'objects' );
  100. foreach ( $taxonomies as $tax_name => $taxonomy ) {
  101. $order[ $taxonomy->labels->name ] = 15;
  102. }
  103. $this->condition_sort_order = apply_filters( 'pum_condition_sort_order', $order );
  104. }
  105. return $this->condition_sort_order;
  106. }
  107. /**
  108. * @param $a
  109. * @param $b
  110. *
  111. * @return int
  112. */
  113. public function sort_condition_groups( $a, $b ) {
  114. $order = $this->condition_sort_order();
  115. $ai = isset( $order[ $a ] ) ? intval( $order[ $a ] ) : 10;
  116. $bi = isset( $order[ $b ] ) ? intval( $order[ $b ] ) : 10;
  117. if ( $ai == $bi ) {
  118. return 0;
  119. }
  120. // Compare their positions in line.
  121. return $ai > $bi ? 1 : - 1;
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function get_conditions_by_group() {
  127. static $groups;
  128. if ( ! isset( $groups ) ) {
  129. $groups = array();
  130. foreach ( $this->get_conditions() as $condition ) {
  131. $groups[ $condition['group'] ][ $condition['id'] ] = $condition;
  132. }
  133. uksort( $groups, array( $this, 'sort_condition_groups' ) );
  134. }
  135. return $groups;
  136. }
  137. /**
  138. * @return array
  139. */
  140. public function dropdown_list() {
  141. $groups = array();
  142. $conditions_by_group = $this->get_conditions_by_group();
  143. foreach ( $conditions_by_group as $group => $_conditions ) {
  144. $conditions = array();
  145. foreach ( $_conditions as $id => $condition ) {
  146. $conditions[ $id ] = $condition['name'];
  147. }
  148. $groups[ $group ] = $conditions;
  149. }
  150. return $groups;
  151. }
  152. /**
  153. * @param null $condition
  154. *
  155. * @return mixed|null
  156. */
  157. public function get_condition( $condition = null ) {
  158. $conditions = $this->get_conditions();
  159. return isset( $conditions[ $condition ] ) ? $conditions[ $condition ] : null;
  160. }
  161. /**
  162. * @return array
  163. */
  164. public function generate_post_type_conditions() {
  165. $conditions = array();
  166. $post_types = get_post_types( array( 'public' => true ), 'objects' );
  167. foreach ( $post_types as $name => $post_type ) {
  168. if ( $name == 'popup' || $name == 'popup_theme' ) {
  169. continue;
  170. }
  171. if ( $post_type->has_archive ) {
  172. $conditions[ $name . '_index' ] = array(
  173. 'group' => $post_type->labels->name,
  174. 'name' => sprintf( _x( '%s Archive', 'condition: post type plural label ie. Posts: All', 'popup-maker' ), $post_type->labels->name ),
  175. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
  176. 'priority' => 5,
  177. );
  178. }
  179. $conditions[ $name . '_all' ] = array(
  180. 'group' => $post_type->labels->name,
  181. 'name' => sprintf( _x( 'All %s', 'condition: post type plural label ie. Posts: All', 'popup-maker' ), $post_type->labels->name ),
  182. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
  183. );
  184. $conditions[ $name . '_selected' ] = array(
  185. 'group' => $post_type->labels->name,
  186. 'name' => sprintf( _x( '%s: Selected', 'condition: post type plural label ie. Posts: Selected', 'popup-maker' ), $post_type->labels->name ),
  187. 'fields' => array(
  188. 'selected' => array(
  189. 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'popup-maker' ), strtolower( $post_type->labels->name ) ),
  190. 'type' => 'postselect',
  191. 'post_type' => $name,
  192. 'multiple' => true,
  193. 'as_array' => true,
  194. 'std' => array(),
  195. ),
  196. ),
  197. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
  198. );
  199. $conditions[ $name . '_ID' ] = array(
  200. 'group' => $post_type->labels->name,
  201. 'name' => sprintf( _x( '%s: ID', 'condition: post type plural label ie. Posts: ID', 'popup-maker' ), $post_type->labels->name ),
  202. 'fields' => array(
  203. 'selected' => array(
  204. 'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: post type singular label ie. Posts IDs', 'popup-maker' ), strtolower( $post_type->labels->singular_name ) ),
  205. 'type' => 'text',
  206. ),
  207. ),
  208. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
  209. );
  210. if ( is_post_type_hierarchical( $name ) ) {
  211. $conditions[ $name . '_children' ] = array(
  212. 'group' => $post_type->labels->name,
  213. 'name' => sprintf( _x( '%s: Child Of', 'condition: post type plural label ie. Posts: ID', 'popup-maker' ), $post_type->labels->name ),
  214. 'fields' => array(
  215. 'selected' => array(
  216. 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'popup-maker' ), strtolower( $post_type->labels->name ) ),
  217. 'type' => 'postselect',
  218. 'post_type' => $name,
  219. 'multiple' => true,
  220. 'as_array' => true,
  221. ),
  222. ),
  223. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
  224. );
  225. $conditions[ $name . '_ancestors' ] = array(
  226. 'group' => $post_type->labels->name,
  227. 'name' => sprintf( _x( '%s: Ancestor Of', 'condition: post type plural label ie. Posts: ID', 'popup-maker' ), $post_type->labels->name ),
  228. 'fields' => array(
  229. 'selected' => array(
  230. 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'popup-maker' ), strtolower( $post_type->labels->name ) ),
  231. 'type' => 'postselect',
  232. 'post_type' => $name,
  233. 'multiple' => true,
  234. 'as_array' => true,
  235. ),
  236. ),
  237. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
  238. );
  239. }
  240. $templates = wp_get_theme()->get_page_templates();
  241. if ( $name == 'page' && ! empty( $templates ) ) {
  242. $conditions[ $name . '_template' ] = array(
  243. 'group' => $post_type->labels->name,
  244. 'name' => sprintf( _x( '%s: With Template', 'condition: post type plural label ie. Pages: With Template', 'popup-maker' ), $post_type->labels->name ),
  245. 'fields' => array(
  246. 'selected' => array(
  247. 'type' => 'select',
  248. 'select2' => true,
  249. 'multiple' => true,
  250. 'as_array' => true,
  251. 'options' => array_merge( array( 'default' => __( 'Default', 'popup-maker' ) ), $templates ),
  252. ),
  253. ),
  254. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
  255. );
  256. }
  257. $conditions = array_merge( $conditions, $this->generate_post_type_tax_conditions( $name ) );
  258. }
  259. return $conditions;
  260. }
  261. /**
  262. * @param $name
  263. *
  264. * @return array
  265. */
  266. public function generate_post_type_tax_conditions( $name ) {
  267. $post_type = get_post_type_object( $name );
  268. $taxonomies = get_object_taxonomies( $name, 'object' );
  269. $conditions = array();
  270. foreach ( $taxonomies as $tax_name => $taxonomy ) {
  271. $conditions[ $name . '_w_' . $tax_name ] = array(
  272. 'group' => $post_type->labels->name,
  273. 'name' => sprintf( _x( '%1$s: With %2$s', 'condition: post type plural and taxonomy singular label ie. Posts: With Category', 'popup-maker' ), $post_type->labels->name, $taxonomy->labels->singular_name ),
  274. 'fields' => array(
  275. 'selected' => array(
  276. 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select categories', 'popup-maker' ), strtolower( $taxonomy->labels->name ) ),
  277. 'type' => 'taxonomyselect',
  278. 'taxonomy' => $tax_name,
  279. 'multiple' => true,
  280. 'as_array' => true,
  281. ),
  282. ),
  283. 'callback' => array( 'PUM_ConditionCallbacks', 'post_type_tax' ),
  284. );
  285. }
  286. return $conditions;
  287. }
  288. /**
  289. * Generates conditions for all public taxonomies.
  290. *
  291. * @return array
  292. */
  293. public function generate_taxonomy_conditions() {
  294. $conditions = array();
  295. $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' );
  296. foreach ( $taxonomies as $tax_name => $taxonomy ) {
  297. $conditions[ 'tax_' . $tax_name . '_all' ] = array(
  298. 'group' => $taxonomy->labels->name,
  299. 'name' => sprintf( _x( '%s: All', 'condition: taxonomy plural label ie. Categories: All', 'popup-maker' ), $taxonomy->labels->name ),
  300. 'callback' => array( 'PUM_ConditionCallbacks', 'taxonomy' ),
  301. );
  302. $conditions[ 'tax_' . $tax_name . '_selected' ] = array(
  303. 'group' => $taxonomy->labels->name,
  304. 'name' => sprintf( _x( '%s: Selected', 'condition: taxonomy plural label ie. Categories: Selected', 'popup-maker' ), $taxonomy->labels->name ),
  305. 'fields' => array(
  306. 'selected' => array(
  307. 'placeholder' => sprintf( _x( 'Select %s.', 'condition: taxonomy plural label ie. Select Categories', 'popup-maker' ), strtolower( $taxonomy->labels->name ) ),
  308. 'type' => 'taxonomyselect',
  309. 'taxonomy' => $tax_name,
  310. 'multiple' => true,
  311. 'as_array' => true,
  312. ),
  313. ),
  314. 'callback' => array( 'PUM_ConditionCallbacks', 'taxonomy' ),
  315. );
  316. $conditions[ 'tax_' . $tax_name . '_ID' ] = array(
  317. 'group' => $taxonomy->labels->name,
  318. 'name' => sprintf( _x( '%s: IDs', 'condition: taxonomy plural label ie. Categories: Selected', 'popup-maker' ), $taxonomy->labels->name ),
  319. 'fields' => array(
  320. 'selected' => array(
  321. 'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: taxonomy plural label ie. Category IDs', 'popup-maker' ), strtolower( $taxonomy->labels->singular_name ) ),
  322. 'type' => 'text',
  323. ),
  324. ),
  325. 'callback' => array( 'PUM_ConditionCallbacks', 'taxonomy' ),
  326. );
  327. }
  328. return $conditions;
  329. }
  330. /**
  331. * Registers all known conditions when called.
  332. */
  333. public function register_conditions() {
  334. $conditions = array_merge( $this->generate_post_type_conditions(), $this->generate_taxonomy_conditions() );
  335. $conditions['is_front_page'] = array(
  336. 'group' => __( 'General', 'popup-maker' ),
  337. 'name' => __( 'Home Page', 'popup-maker' ),
  338. 'callback' => 'is_front_page',
  339. 'priority' => 2,
  340. );
  341. $conditions['is_home'] = array(
  342. 'group' => __( 'Posts', 'popup-maker' ),
  343. 'name' => __( 'Blog Index', 'popup-maker' ),
  344. 'callback' => 'is_home',
  345. 'priority' => 1,
  346. );
  347. $conditions['is_search'] = array(
  348. 'group' => __( 'General', 'popup-maker' ),
  349. 'name' => __( 'Search Result Page', 'popup-maker' ),
  350. 'callback' => 'is_search',
  351. );
  352. $conditions['is_404'] = array(
  353. 'group' => __( 'General', 'popup-maker' ),
  354. 'name' => __( '404 Error Page', 'popup-maker' ),
  355. 'callback' => 'is_404',
  356. );
  357. $conditions = apply_filters( 'pum_registered_conditions', $conditions );
  358. // @deprecated filter.
  359. $old_conditions = apply_filters( 'pum_get_conditions', array() );
  360. foreach ( $old_conditions as $id => $condition ) {
  361. if ( ! empty( $condition['labels'] ) && ! empty( $condition['labels']['name'] ) ) {
  362. $condition['name'] = $condition['labels']['name'];
  363. unset( $condition['labels'] );
  364. }
  365. if ( ! isset( $conditions[ $id ] ) ) {
  366. $conditions[ $id ] = $condition;
  367. }
  368. }
  369. $this->add_conditions( $conditions );
  370. }
  371. /**
  372. * Gets a filterable array of the allowed user roles.
  373. *
  374. * @return array
  375. */
  376. public static function allowed_user_roles() {
  377. global $wp_roles;
  378. static $roles;
  379. if ( ! isset( $roles ) && is_object( $wp_roles ) ) {
  380. $roles = apply_filters( 'pum_user_roles', $wp_roles->role_names );
  381. if ( ! is_array( $roles ) || empty( $roles ) ) {
  382. $roles = array();
  383. }
  384. } else {
  385. return array();
  386. }
  387. return $roles;
  388. }
  389. }