| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
class WPBottomMenu_Condition { |
| 6 |
|
| 7 |
function wpbm_condition_render( $rule ){ |
| 8 |
|
| 9 |
$id = false; |
| 10 |
$reverse = get_option( 'wpbottommenu_condition_reverse', false ); |
| 11 |
|
| 12 |
$condition = $rule['condition']; |
| 13 |
$archives_condition = is_array($rule['archives_condition']) ? 'all' : $rule['archives_condition']; |
| 14 |
$singular_condition = is_array($rule['singular_condition']) ? 'all' : $rule['singular_condition']; |
| 15 |
$woocommerce_condition = is_array($rule['woocommerce_condition']) ? 'all' : $rule['woocommerce_condition']; |
| 16 |
$singular_page_condition = $rule['singular_page_condition']; |
| 17 |
$singular_post_condition = $rule['singular_post_condition']; |
| 18 |
$singular_product_condition = $rule['singular_product_condition']; |
| 19 |
|
| 20 |
$cpts = apply_filters( 'wpbm_cpt_condition', array( 'post' ) ); |
| 21 |
|
| 22 |
switch ($condition) { |
| 23 |
// entire |
| 24 |
case 'entire': |
| 25 |
$id = true; |
| 26 |
break; |
| 27 |
// archives |
| 28 |
case 'archives': |
| 29 |
if ( is_archive() && $archives_condition === 'all' ) { |
| 30 |
$id = true; |
| 31 |
} elseif ( is_author() && $archives_condition === 'author' ){ |
| 32 |
$id = true; |
| 33 |
} elseif ( is_category() && $archives_condition === 'cats' ){ |
| 34 |
$id = true; |
| 35 |
} elseif ( is_tag() && $archives_condition === 'tags' ){ |
| 36 |
$id = true; |
| 37 |
} else { |
| 38 |
foreach ( $cpts as $cpt ) { |
| 39 |
if ( is_archive() && $archives_condition === $cpt && get_post_type() === $cpt ) { |
| 40 |
$id = true; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
break; |
| 45 |
// singular |
| 46 |
case 'singular': |
| 47 |
if ( is_singular() ) { |
| 48 |
if ( is_page() && $singular_condition === 'pages' ){ |
| 49 |
//$id = true; |
| 50 |
if ( ! empty( $singular_page_condition ) && is_page($singular_page_condition )){ |
| 51 |
$id = true; |
| 52 |
} |
| 53 |
} elseif ( is_single() && in_array( $singular_condition, $cpts ) ){ |
| 54 |
foreach ( $cpts as $cpt ) { |
| 55 |
if ( $singular_condition === $cpt ){ |
| 56 |
if ( ! empty( $rule['singular_' . $cpt . '_condition']) && in_array( get_the_ID(), $rule['singular_' . $cpt . '_condition'] ) ){ |
| 57 |
$id = true; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} elseif ( $singular_condition === 'all' ){ |
| 62 |
$id = true; |
| 63 |
} elseif ( is_front_page() && $singular_condition === 'front-page' ){ |
| 64 |
$id = true; |
| 65 |
} |
| 66 |
} elseif ( is_search() && $singular_condition === 'search' ) { |
| 67 |
$id = true; |
| 68 |
} elseif ( is_404() && $singular_condition === 'page-404') { |
| 69 |
$id = true; |
| 70 |
} |
| 71 |
break; |
| 72 |
// woocommerce |
| 73 |
case 'woocommerce': |
| 74 |
if ( class_exists( 'WooCommerce' ) ){ |
| 75 |
if ( $woocommerce_condition === 'all' && ( is_woocommerce() || is_shop() || is_product_category() || is_product_tag() || is_product() || is_cart() || is_checkout() || is_account_page() ) ) { |
| 76 |
$id = true; |
| 77 |
} elseif ( $woocommerce_condition === 'archive' && ( is_product_category() || is_product_tag() ) ){ |
| 78 |
$id = true; |
| 79 |
} elseif ( $woocommerce_condition === 'shop' && is_shop() ){ |
| 80 |
$id = true; |
| 81 |
} elseif ( $woocommerce_condition === 'cats' && is_product_category() ){ |
| 82 |
$id = true; |
| 83 |
} elseif ( $woocommerce_condition === 'tags' && is_product_tag() ){ |
| 84 |
$id = true; |
| 85 |
} elseif ( $woocommerce_condition === 'products' && is_product() ){ |
| 86 |
$id = true; |
| 87 |
} elseif ( $woocommerce_condition === 'product' && is_product() && ( ( is_array( $singular_product_condition ) && array_search( get_the_ID() , $singular_product_condition ) !== false ) || $singular_product_condition === 'all' ) ){ |
| 88 |
$id = true; |
| 89 |
} |
| 90 |
} |
| 91 |
break; |
| 92 |
} |
| 93 |
|
| 94 |
if ( $reverse ){ |
| 95 |
$id = !$id; |
| 96 |
} |
| 97 |
|
| 98 |
return $id; |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
function wpbm_user_role_condition( $allowed_roles ){ |
| 103 |
|
| 104 |
if ( !is_array( $allowed_roles ) || empty( $allowed_roles ) ){ |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
if ( ($key = array_search('all', $allowed_roles)) !== false ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
$user = wp_get_current_user(); |
| 113 |
if ( array_intersect( $allowed_roles, $user->roles ) ) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
return false; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
function get_condition(){ |
| 122 |
|
| 123 |
// always show for in customize preview mode |
| 124 |
if ( is_customize_preview() ){ |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
$rule = [ |
| 129 |
'condition' => get_option( 'wpbottommenu_condition', 'entire' ), |
| 130 |
'archives_condition' => get_option( 'wpbottommenu_archives_condition', 'all' ), |
| 131 |
'singular_condition' => get_option( 'wpbottommenu_singular_condition', 'all' ), |
| 132 |
'woocommerce_condition' => get_option( 'wpbottommenu_woocommerce_condition', 'all' ), |
| 133 |
'singular_page_condition' => get_option( 'wpbottommenu_singular_page_condition', 'all' ), |
| 134 |
'singular_post_condition' => get_option( 'wpbottommenu_singular_post_condition', 'all' ), |
| 135 |
'singular_product_condition' => get_option( 'wpbottommenu_singular_product_condition', 'all' ), |
| 136 |
]; |
| 137 |
|
| 138 |
$roles = get_option( 'wpbottommenu_user_role_condition', array( 'all' ) ); |
| 139 |
|
| 140 |
if ( $this->wpbm_condition_render( $rule ) ){ |
| 141 |
if ( $this->wpbm_user_role_condition( $roles ) ){ |
| 142 |
return $this->wpbm_user_role_condition( $roles ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
} |