| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- <?php
- /*******************************************************************************
- * Copyright (c) 2019, Code Atlantic LLC
- ******************************************************************************/
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- /**
- * Class Conditions
- */
- class PUM_Conditions {
- /**
- * @var
- */
- public static $instance;
- /**
- * @var bool
- */
- public $preload_posts = false;
- /**
- * @var array
- */
- public $conditions;
- /**
- * @var array
- */
- public $condition_sort_order = array();
- /**
- *
- */
- public static function init() {
- self::instance();
- }
- /**
- * @return self
- */
- public static function instance() {
- if ( ! isset( self::$instance ) ) {
- self::$instance = new self;
- self::$instance->preload_posts = popmake_is_admin_popup_page();
- }
- return self::$instance;
- }
- /**
- * @param array $conditions
- */
- public function add_conditions( $conditions = array() ) {
- foreach ( $conditions as $key => $condition ) {
- if ( empty( $condition['id'] ) && ! is_numeric( $key ) ) {
- $condition['id'] = $key;
- }
- $this->add_condition( $condition );
- }
- }
- /**
- * @param array $condition
- */
- public function add_condition( $condition = array() ) {
- if ( ! empty( $condition['id'] ) && ! isset ( $this->conditions[ $condition['id'] ] ) ) {
- $condition = wp_parse_args( $condition, array(
- 'id' => '',
- 'callback' => null,
- 'group' => '',
- 'name' => '',
- 'priority' => 10,
- 'fields' => array(),
- 'advanced' => false,
- ) );
- $this->conditions[ $condition['id'] ] = $condition;
- }
- return;
- }
- /**
- * @return array
- */
- public function get_conditions() {
- if ( ! isset( $this->conditions ) ) {
- $this->register_conditions();
- }
- return $this->conditions;
- }
- /**
- * @return array|mixed
- */
- public function condition_sort_order() {
- if ( ! $this->condition_sort_order ) {
- $order = array(
- __( 'General', 'popup-maker' ) => 1,
- __( 'Pages', 'popup-maker' ) => 5,
- __( 'Posts', 'popup-maker' ) => 5,
- __( 'Categories', 'popup-maker' ) => 14,
- __( 'Tags', 'popup-maker' ) => 14,
- __( 'Format', 'popup-maker' ) => 16,
- );
- $post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
- foreach ( $post_types as $name => $post_type ) {
- $order[ $post_type->labels->name ] = 10;
- }
- $taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'objects' );
- foreach ( $taxonomies as $tax_name => $taxonomy ) {
- $order[ $taxonomy->labels->name ] = 15;
- }
- $this->condition_sort_order = apply_filters( 'pum_condition_sort_order', $order );
- }
- return $this->condition_sort_order;
- }
- /**
- * @param $a
- * @param $b
- *
- * @return int
- */
- public function sort_condition_groups( $a, $b ) {
- $order = $this->condition_sort_order();
- $ai = isset( $order[ $a ] ) ? intval( $order[ $a ] ) : 10;
- $bi = isset( $order[ $b ] ) ? intval( $order[ $b ] ) : 10;
- if ( $ai == $bi ) {
- return 0;
- }
- // Compare their positions in line.
- return $ai > $bi ? 1 : - 1;
- }
- /**
- * @return array
- */
- public function get_conditions_by_group() {
- static $groups;
- if ( ! isset( $groups ) ) {
- $groups = array();
- foreach ( $this->get_conditions() as $condition ) {
- $groups[ $condition['group'] ][ $condition['id'] ] = $condition;
- }
- uksort( $groups, array( $this, 'sort_condition_groups' ) );
- }
- return $groups;
- }
- /**
- * @return array
- */
- public function dropdown_list() {
- $groups = array();
- $conditions_by_group = $this->get_conditions_by_group();
- foreach ( $conditions_by_group as $group => $_conditions ) {
- $conditions = array();
- foreach ( $_conditions as $id => $condition ) {
- $conditions[ $id ] = $condition['name'];
- }
- $groups[ $group ] = $conditions;
- }
- return $groups;
- }
- /**
- * @param null $condition
- *
- * @return mixed|null
- */
- public function get_condition( $condition = null ) {
- $conditions = $this->get_conditions();
- return isset( $conditions[ $condition ] ) ? $conditions[ $condition ] : null;
- }
- /**
- * @return array
- */
- public function generate_post_type_conditions() {
- $conditions = array();
- $post_types = get_post_types( array( 'public' => true ), 'objects' );
- foreach ( $post_types as $name => $post_type ) {
- if ( $name == 'popup' || $name == 'popup_theme' ) {
- continue;
- }
- if ( $post_type->has_archive ) {
- $conditions[ $name . '_index' ] = array(
- 'group' => $post_type->labels->name,
- 'name' => sprintf( _x( '%s Archive', 'condition: post type plural label ie. Posts: All', 'popup-maker' ), $post_type->labels->name ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
- 'priority' => 5,
- );
- }
- $conditions[ $name . '_all' ] = array(
- 'group' => $post_type->labels->name,
- 'name' => sprintf( _x( 'All %s', 'condition: post type plural label ie. Posts: All', 'popup-maker' ), $post_type->labels->name ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
- );
- $conditions[ $name . '_selected' ] = array(
- 'group' => $post_type->labels->name,
- 'name' => sprintf( _x( '%s: Selected', 'condition: post type plural label ie. Posts: Selected', 'popup-maker' ), $post_type->labels->name ),
- 'fields' => array(
- 'selected' => array(
- 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'popup-maker' ), strtolower( $post_type->labels->name ) ),
- 'type' => 'postselect',
- 'post_type' => $name,
- 'multiple' => true,
- 'as_array' => true,
- 'std' => array(),
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
- );
- $conditions[ $name . '_ID' ] = array(
- 'group' => $post_type->labels->name,
- 'name' => sprintf( _x( '%s: ID', 'condition: post type plural label ie. Posts: ID', 'popup-maker' ), $post_type->labels->name ),
- 'fields' => array(
- 'selected' => array(
- 'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: post type singular label ie. Posts IDs', 'popup-maker' ), strtolower( $post_type->labels->singular_name ) ),
- 'type' => 'text',
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
- );
- if ( is_post_type_hierarchical( $name ) ) {
- $conditions[ $name . '_children' ] = array(
- 'group' => $post_type->labels->name,
- 'name' => sprintf( _x( '%s: Child Of', 'condition: post type plural label ie. Posts: ID', 'popup-maker' ), $post_type->labels->name ),
- 'fields' => array(
- 'selected' => array(
- 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'popup-maker' ), strtolower( $post_type->labels->name ) ),
- 'type' => 'postselect',
- 'post_type' => $name,
- 'multiple' => true,
- 'as_array' => true,
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
- );
- $conditions[ $name . '_ancestors' ] = array(
- 'group' => $post_type->labels->name,
- 'name' => sprintf( _x( '%s: Ancestor Of', 'condition: post type plural label ie. Posts: ID', 'popup-maker' ), $post_type->labels->name ),
- 'fields' => array(
- 'selected' => array(
- 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'popup-maker' ), strtolower( $post_type->labels->name ) ),
- 'type' => 'postselect',
- 'post_type' => $name,
- 'multiple' => true,
- 'as_array' => true,
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
- );
- }
- $templates = wp_get_theme()->get_page_templates();
- if ( $name == 'page' && ! empty( $templates ) ) {
- $conditions[ $name . '_template' ] = array(
- 'group' => $post_type->labels->name,
- 'name' => sprintf( _x( '%s: With Template', 'condition: post type plural label ie. Pages: With Template', 'popup-maker' ), $post_type->labels->name ),
- 'fields' => array(
- 'selected' => array(
- 'type' => 'select',
- 'select2' => true,
- 'multiple' => true,
- 'as_array' => true,
- 'options' => array_merge( array( 'default' => __( 'Default', 'popup-maker' ) ), $templates ),
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type' ),
- );
- }
- $conditions = array_merge( $conditions, $this->generate_post_type_tax_conditions( $name ) );
- }
- return $conditions;
- }
- /**
- * @param $name
- *
- * @return array
- */
- public function generate_post_type_tax_conditions( $name ) {
- $post_type = get_post_type_object( $name );
- $taxonomies = get_object_taxonomies( $name, 'object' );
- $conditions = array();
- foreach ( $taxonomies as $tax_name => $taxonomy ) {
- $conditions[ $name . '_w_' . $tax_name ] = array(
- 'group' => $post_type->labels->name,
- '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 ),
- 'fields' => array(
- 'selected' => array(
- 'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select categories', 'popup-maker' ), strtolower( $taxonomy->labels->name ) ),
- 'type' => 'taxonomyselect',
- 'taxonomy' => $tax_name,
- 'multiple' => true,
- 'as_array' => true,
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'post_type_tax' ),
- );
- }
- return $conditions;
- }
- /**
- * Generates conditions for all public taxonomies.
- *
- * @return array
- */
- public function generate_taxonomy_conditions() {
- $conditions = array();
- $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' );
- foreach ( $taxonomies as $tax_name => $taxonomy ) {
- $conditions[ 'tax_' . $tax_name . '_all' ] = array(
- 'group' => $taxonomy->labels->name,
- 'name' => sprintf( _x( '%s: All', 'condition: taxonomy plural label ie. Categories: All', 'popup-maker' ), $taxonomy->labels->name ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'taxonomy' ),
- );
- $conditions[ 'tax_' . $tax_name . '_selected' ] = array(
- 'group' => $taxonomy->labels->name,
- 'name' => sprintf( _x( '%s: Selected', 'condition: taxonomy plural label ie. Categories: Selected', 'popup-maker' ), $taxonomy->labels->name ),
- 'fields' => array(
- 'selected' => array(
- 'placeholder' => sprintf( _x( 'Select %s.', 'condition: taxonomy plural label ie. Select Categories', 'popup-maker' ), strtolower( $taxonomy->labels->name ) ),
- 'type' => 'taxonomyselect',
- 'taxonomy' => $tax_name,
- 'multiple' => true,
- 'as_array' => true,
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'taxonomy' ),
- );
- $conditions[ 'tax_' . $tax_name . '_ID' ] = array(
- 'group' => $taxonomy->labels->name,
- 'name' => sprintf( _x( '%s: IDs', 'condition: taxonomy plural label ie. Categories: Selected', 'popup-maker' ), $taxonomy->labels->name ),
- 'fields' => array(
- 'selected' => array(
- 'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: taxonomy plural label ie. Category IDs', 'popup-maker' ), strtolower( $taxonomy->labels->singular_name ) ),
- 'type' => 'text',
- ),
- ),
- 'callback' => array( 'PUM_ConditionCallbacks', 'taxonomy' ),
- );
- }
- return $conditions;
- }
- /**
- * Registers all known conditions when called.
- */
- public function register_conditions() {
- $conditions = array_merge( $this->generate_post_type_conditions(), $this->generate_taxonomy_conditions() );
- $conditions['is_front_page'] = array(
- 'group' => __( 'General', 'popup-maker' ),
- 'name' => __( 'Home Page', 'popup-maker' ),
- 'callback' => 'is_front_page',
- 'priority' => 2,
- );
- $conditions['is_home'] = array(
- 'group' => __( 'Posts', 'popup-maker' ),
- 'name' => __( 'Blog Index', 'popup-maker' ),
- 'callback' => 'is_home',
- 'priority' => 1,
- );
- $conditions['is_search'] = array(
- 'group' => __( 'General', 'popup-maker' ),
- 'name' => __( 'Search Result Page', 'popup-maker' ),
- 'callback' => 'is_search',
- );
- $conditions['is_404'] = array(
- 'group' => __( 'General', 'popup-maker' ),
- 'name' => __( '404 Error Page', 'popup-maker' ),
- 'callback' => 'is_404',
- );
- $conditions = apply_filters( 'pum_registered_conditions', $conditions );
- // @deprecated filter.
- $old_conditions = apply_filters( 'pum_get_conditions', array() );
- foreach ( $old_conditions as $id => $condition ) {
- if ( ! empty( $condition['labels'] ) && ! empty( $condition['labels']['name'] ) ) {
- $condition['name'] = $condition['labels']['name'];
- unset( $condition['labels'] );
- }
- if ( ! isset( $conditions[ $id ] ) ) {
- $conditions[ $id ] = $condition;
- }
- }
- $this->add_conditions( $conditions );
- }
- /**
- * Gets a filterable array of the allowed user roles.
- *
- * @return array
- */
- public static function allowed_user_roles() {
- global $wp_roles;
- static $roles;
- if ( ! isset( $roles ) && is_object( $wp_roles ) ) {
- $roles = apply_filters( 'pum_user_roles', $wp_roles->role_names );
- if ( ! is_array( $roles ) || empty( $roles ) ) {
- $roles = array();
- }
- } else {
- return array();
- }
- return $roles;
- }
- }
|