default-design
7 months ago
popup
7 months ago
slidein
3 years ago
sshare
7 months ago
class-hustle-meta-base-content.php
3 years ago
class-hustle-meta-base-design.php
3 years ago
class-hustle-meta-base-display.php
6 years ago
class-hustle-meta-base-emails.php
5 months ago
class-hustle-meta-base-integrations.php
3 years ago
class-hustle-meta-base-settings.php
3 years ago
class-hustle-meta-base-visibility.php
7 months ago
class-hustle-meta-base-visibility.php
227 lines
| 1 | <?php |
| 2 | /** |
| 3 | * File for Hustle_Meta_Base_Visibility class. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since 4.2.0 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Hustle_Meta_Base_Visibility is the base class for the "visibility" meta of modules. |
| 11 | * This class should handle what's related to the "visibility" meta. |
| 12 | * |
| 13 | * @since 4.2.0 |
| 14 | */ |
| 15 | class Hustle_Meta_Base_Visibility extends Hustle_Meta { |
| 16 | |
| 17 | /** |
| 18 | * Get the defaults for this meta. |
| 19 | * |
| 20 | * @since 4.2.0 |
| 21 | * @return array |
| 22 | */ |
| 23 | public function get_defaults() { |
| 24 | return array(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Get relevant conditions based on subtype or return FALSE if it shouldn't be shown |
| 29 | * |
| 30 | * @since unkwnon |
| 31 | * @since 4.2.0 Moved from Hustle_Module_Model to this class. Visibility changed from private to public. |
| 32 | * |
| 33 | * @param string $subtype Module's display type floating|inline|shortcode|widget. Only for embeds and ssharing. |
| 34 | * @return array|false |
| 35 | */ |
| 36 | public function get_conditions( $subtype = null ) { |
| 37 | $all_conditions = $this->to_array(); |
| 38 | |
| 39 | // Return all. No need to filter per subtype. |
| 40 | if ( is_null( $subtype ) || empty( $all_conditions['conditions'] ) ) { |
| 41 | return $all_conditions; |
| 42 | } |
| 43 | |
| 44 | // Remove the conditions that are not for this subtype. |
| 45 | $conditions_removed = false; |
| 46 | foreach ( $all_conditions['conditions'] as $group_id => $data ) { |
| 47 | if ( |
| 48 | ( isset( $data[ 'apply_on_' . $subtype ] ) && 'false' === $data[ 'apply_on_' . $subtype ] ) || |
| 49 | ( 'shortcode' === $subtype && ! isset( $data[ 'apply_on_' . $subtype ] ) ) |
| 50 | ) { |
| 51 | $conditions_removed = true; |
| 52 | unset( $all_conditions['conditions'][ $group_id ] ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // No conditions are left after filtering per subtype. |
| 57 | if ( $conditions_removed && empty( $all_conditions['conditions'] ) ) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return $all_conditions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check if it contains only "simple" conditions. |
| 66 | * Simple conditions means conditions which behavior is the same for the same page. |
| 67 | * If so - we can allow to cache it for static cache. |
| 68 | * |
| 69 | * @param string $sub_type Sub type. |
| 70 | * @return boolean |
| 71 | */ |
| 72 | public function is_simple_conditions( $sub_type ) { |
| 73 | $all_conditions = $this->get_conditions( $sub_type ); |
| 74 | |
| 75 | if ( empty( $all_conditions['conditions'] ) ) { |
| 76 | return true; |
| 77 | } |
| 78 | foreach ( $all_conditions['conditions'] as $conditions ) { |
| 79 | $complex_conditions = array_diff( array_keys( $conditions ), self::get_non_condition_properties(), self::simple_conditions() ); |
| 80 | if ( $complex_conditions ) { |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Return simple condition keys |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | private static function simple_conditions() { |
| 94 | return array( 'pages', 'posts', 'tags', 'categories', 'archive_pages', 'page_templates', 'on_url', 'wp_conditions', 'wc_pages', 'wc_categories', 'wc_tags', 'wc_archive_pages', 'wc_static_pages' ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks if this module is allowed to be displayed |
| 99 | * |
| 100 | * @since unknwon |
| 101 | * @since 4.2.0 Moved from Hustle_Module_Model to this class. |
| 102 | * |
| 103 | * @param string $module_type Type of the current module. |
| 104 | * @param string $sub_type Display type for embeddeds and ssharing. |
| 105 | * @return bool |
| 106 | */ |
| 107 | public function is_allowed_to_display( $module_type, $sub_type = null ) { |
| 108 | $module_id = $this->module->id; |
| 109 | |
| 110 | $allow = $this->allowed_to_display( $module_type, $sub_type ); |
| 111 | $allow = apply_filters( 'hustle_module_is_allowed_to_display', $allow, $module_id, $module_type, $sub_type ); |
| 112 | |
| 113 | return $allow; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Checks if this module is allowed to be displayed. It's internal method. Use is_allowed_to_display() instead. |
| 118 | * |
| 119 | * @param string $module_type Type of the current module. |
| 120 | * @param string $sub_type Display type for embeddeds and ssharing. |
| 121 | * @return boolean |
| 122 | */ |
| 123 | private function allowed_to_display( $module_type, $sub_type ) { |
| 124 | $global_behavior = false; |
| 125 | |
| 126 | $all_conditions = $this->get_conditions( $sub_type ); |
| 127 | |
| 128 | if ( false === $all_conditions ) { |
| 129 | if ( 'shortcode' === $sub_type ) { |
| 130 | return true; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | if ( empty( $all_conditions['conditions'] ) ) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | $display = null; |
| 139 | foreach ( $all_conditions['conditions'] as $group_id => $conditions ) { |
| 140 | $any_true = false; |
| 141 | $any_false = false; |
| 142 | $default_behavior = $this->get_default_group_behavior( $conditions ); |
| 143 | if ( $default_behavior ) { |
| 144 | $global_behavior = true; |
| 145 | } |
| 146 | |
| 147 | // Condition type. |
| 148 | $filter_type = isset( $conditions['filter_type'] ) && |
| 149 | 'any' === $conditions['filter_type'] |
| 150 | ? $conditions['filter_type'] : 'all'; |
| 151 | |
| 152 | foreach ( $conditions as $condition_key => $args ) { |
| 153 | |
| 154 | // These are not conditions but group's properties we don't need to check here. |
| 155 | if ( in_array( $condition_key, self::get_non_condition_properties(), true ) ) { |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | // only cpt have 'postType' and 'postTypeLabel' properties. |
| 160 | if ( is_array( $args ) && isset( $args['postType'] ) && isset( $args['postTypeLabel'] ) ) { |
| 161 | $condition_key = 'cpt'; |
| 162 | } |
| 163 | $condition = Hustle_Condition_Factory::build( $condition_key, $args ); |
| 164 | if ( $condition ) { |
| 165 | $some_conditions = true; |
| 166 | $condition->set_type( $module_type ); |
| 167 | $condition->module = $this->module; |
| 168 | $current = (bool) $condition->is_allowed(); |
| 169 | if ( false === $current ) { |
| 170 | $any_false = true; |
| 171 | } else { |
| 172 | $any_true = true; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if ( 'any' === $filter_type ) { |
| 178 | if ( $any_true ) { |
| 179 | $display = $display || ( $any_true && ! $default_behavior ); |
| 180 | } elseif ( $any_false ) { |
| 181 | $display = $display || $default_behavior; |
| 182 | } |
| 183 | } |
| 184 | if ( 'all' === $filter_type ) { |
| 185 | if ( $any_false ) { |
| 186 | $display = $display || $default_behavior; |
| 187 | } elseif ( $any_true ) { |
| 188 | $display = $display || ( $any_true && ! $default_behavior ); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Show module if there are no conditions. |
| 194 | if ( empty( $some_conditions ) ) { |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | if ( is_null( $display ) ) { |
| 199 | return $global_behavior; |
| 200 | } |
| 201 | |
| 202 | return $display; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get property keys which aren't related to conditions |
| 207 | * |
| 208 | * @return array |
| 209 | */ |
| 210 | private static function get_non_condition_properties() { |
| 211 | return array( 'group_id', 'filter_type', 'apply_on_inline', 'apply_on_widget', 'apply_on_shortcode', 'apply_on_floating', 'show_or_hide_conditions' ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Get visibility behavior by default, to display or not. |
| 216 | * |
| 217 | * @since 4.1.0 |
| 218 | * @since 4.2.0 Moved from Hustle_Module_Model to this class. |
| 219 | * |
| 220 | * @param array $conditions Conditions' group. |
| 221 | * @return bool |
| 222 | */ |
| 223 | private function get_default_group_behavior( $conditions ) { |
| 224 | return ! empty( $conditions['show_or_hide_conditions'] ) && 'hide' === $conditions['show_or_hide_conditions']; |
| 225 | } |
| 226 | } |
| 227 |