|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Enqueue frontend styles. |
| 4 | + * |
| 5 | + * @package block-visibility |
| 6 | + * @since 1.5.0 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace BlockVisibility\Frontend; |
| 10 | + |
| 11 | +defined( 'ABSPATH' ) || exit; |
| 12 | + |
| 13 | +/** |
| 14 | + * Internal dependencies |
| 15 | + */ |
| 16 | +use function BlockVisibility\Utils\get_asset_file as get_asset_file; |
| 17 | +use function BlockVisibility\Utils\is_control_enabled as is_control_enabled; |
| 18 | +use function BlockVisibility\Utils\get_setting as get_setting; |
| 19 | + |
| 20 | +/** |
| 21 | + * Enqueue plugin specific frontend styles |
| 22 | + * |
| 23 | + * @since 1.5.0 |
| 24 | + */ |
| 25 | +function enqueue_frontend_styles() { |
| 26 | + |
| 27 | + // Get the plugin core settings. |
| 28 | + $settings = get_option( 'block_visibility_settings' ); |
| 29 | + |
| 30 | + // Bail early if screen size controls are disabled, or the user has chosen |
| 31 | + // not to enable frontend CSS. |
| 32 | + if ( |
| 33 | + ! is_control_enabled( $settings, 'screen_size' ) || |
| 34 | + ! is_control_enabled( $settings, 'screen_size', 'enable_frontend_css' ) |
| 35 | + ) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Styles. |
| 40 | + $asset_file = get_asset_file( 'dist/block-visibility-frontend-styles' ); |
| 41 | + |
| 42 | + // Currently this is a "dummy" file, but is needed for wp_add_inline_style. |
| 43 | + wp_enqueue_style( |
| 44 | + 'block-visibility-frontend-styles', |
| 45 | + BLOCK_VISIBILITY_PLUGIN_URL . 'dist/block-visibility-frontend-styles.css', |
| 46 | + array(), |
| 47 | + $asset_file['version'] |
| 48 | + ); |
| 49 | + |
| 50 | + $styles = get_screen_size_styles( $settings ); |
| 51 | + |
| 52 | + if ( $styles ) { |
| 53 | + wp_add_inline_style( 'block-visibility-frontend-styles', $styles ); |
| 54 | + } |
| 55 | +} |
| 56 | +add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_frontend_styles' ); |
| 57 | + |
| 58 | +/** |
| 59 | + * Get the screen size styles. |
| 60 | + * |
| 61 | + * @since 1.5.0 |
| 62 | + * |
| 63 | + * @param array $settings The plugin settings. |
| 64 | + * @return string The screen size styles. |
| 65 | + */ |
| 66 | +function get_screen_size_styles( $settings ) { |
| 67 | + |
| 68 | + $has_advanced_controls = get_setting( |
| 69 | + $settings, |
| 70 | + 'visibility_controls', |
| 71 | + 'screen_size', |
| 72 | + 'enable_advanced_controls', |
| 73 | + null, |
| 74 | + false |
| 75 | + ); |
| 76 | + |
| 77 | + $styles = $has_advanced_controls ? |
| 78 | + get_advanced_styles( $settings ) : |
| 79 | + get_default_styles( $settings ); |
| 80 | + |
| 81 | + return $styles; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Generate the default styles |
| 86 | + * |
| 87 | + * @since 1.5.0 |
| 88 | + * |
| 89 | + * @param array $settings The plugin settings. |
| 90 | + * @return string The default screen size styles. |
| 91 | + */ |
| 92 | +function get_default_styles( $settings ) { |
| 93 | + |
| 94 | + // Breakpoints. |
| 95 | + $large = get_setting( $settings, 'visibility_controls', 'screen_size', 'breakpoints', 'large', '992px' ); |
| 96 | + $medium = get_setting( $settings, 'visibility_controls', 'screen_size', 'breakpoints', 'medium', '768px' ); |
| 97 | + |
| 98 | + // Screen size controls. |
| 99 | + $large_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'large', true ); |
| 100 | + $medium_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'medium', true ); |
| 101 | + $small_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'small', true ); |
| 102 | + |
| 103 | + // Render styles. |
| 104 | + $spacer = ' |
| 105 | +
|
| 106 | +'; |
| 107 | + $styles = ''; |
| 108 | + |
| 109 | + if ( $large_enabled ) { |
| 110 | + $prev_styles = $styles ? $styles . $spacer : $styles; |
| 111 | + $styles = $prev_styles . "/* Large screens (desktops, {$large} and up) */ |
| 112 | +@media ( min-width: {$large} ) { |
| 113 | + .block-visibility-hide-large-screen { |
| 114 | + display: none !important; |
| 115 | + } |
| 116 | +}"; |
| 117 | + } |
| 118 | + |
| 119 | + if ( $medium_enabled ) { |
| 120 | + $prev_styles = $styles ? $styles . $spacer : $styles; |
| 121 | + $styles = $prev_styles . "/* Medium screens (tablets, between {$medium} and {$large}) */ |
| 122 | +@media ( min-width: {$medium} ) and ( max-width: " . set_max_width( $large ) . ' ) { |
| 123 | + .block-visibility-hide-medium-screen { |
| 124 | + display: none !important; |
| 125 | + } |
| 126 | +}'; |
| 127 | + } |
| 128 | + |
| 129 | + if ( $small_enabled ) { |
| 130 | + $prev_styles = $styles ? $styles . $spacer : $styles; |
| 131 | + $styles = $prev_styles . "/* Small screens (mobile devices, less than {$medium}) */ |
| 132 | +@media ( max-width: " . set_max_width( $medium ) . ' ) { |
| 133 | + .block-visibility-hide-small-screen { |
| 134 | + display: none !important; |
| 135 | + } |
| 136 | +}'; |
| 137 | + } |
| 138 | + |
| 139 | + if ( ! $styles ) { |
| 140 | + $styles = '/* All screen size controls have been disabled. */'; |
| 141 | + } |
| 142 | + |
| 143 | + return $styles; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Generate the advanced styles |
| 148 | + * |
| 149 | + * @since 1.5.0 |
| 150 | + * |
| 151 | + * @param array $settings The plugin settings. |
| 152 | + * @return string The advanced screen size styles. |
| 153 | + */ |
| 154 | +function get_advanced_styles( $settings ) { |
| 155 | + |
| 156 | + // Breakpoints. |
| 157 | + $extra_large = get_setting( $settings, 'visibility_controls', 'screen_size', 'breakpoints', 'extra_large', '1200px' ); |
| 158 | + $large = get_setting( $settings, 'visibility_controls', 'screen_size', 'breakpoints', 'large', '992px' ); |
| 159 | + $medium = get_setting( $settings, 'visibility_controls', 'screen_size', 'breakpoints', 'medium', '768px' ); |
| 160 | + $small = get_setting( $settings, 'visibility_controls', 'screen_size', 'breakpoints', 'small', '576px' ); |
| 161 | + |
| 162 | + // Screen size controls. |
| 163 | + $extra_large_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'extra_large', true ); |
| 164 | + $large_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'large', true ); |
| 165 | + $medium_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'medium', true ); |
| 166 | + $small_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'small', true ); |
| 167 | + $extra_small_enabled = get_setting( $settings, 'visibility_controls', 'screen_size', 'controls', 'extra_small', true ); |
| 168 | + |
| 169 | + // Render styles. |
| 170 | + $spacer = ' |
| 171 | +
|
| 172 | +'; |
| 173 | + $styles = ''; |
| 174 | + |
| 175 | + if ( $extra_large_enabled ) { |
| 176 | + $styles = "/* Extra large screens (large desktops, {$extra_large} and up) */ |
| 177 | +@media ( min-width: {$extra_large} ) { |
| 178 | + .block-visibility-hide-extra-large-screen { |
| 179 | + display: none !important; |
| 180 | + } |
| 181 | +}"; |
| 182 | + } |
| 183 | + |
| 184 | + if ( $large_enabled ) { |
| 185 | + $prev_styles = $styles ? $styles . $spacer : $styles; |
| 186 | + $styles = $prev_styles . "/* Large screens (desktops, between {$large} and {$extra_large}) */ |
| 187 | +@media ( min-width: {$large} ) and (max-width: " . set_max_width( $extra_large ) . ' ) { |
| 188 | + .block-visibility-hide-large-screen { |
| 189 | + display: none !important; |
| 190 | + } |
| 191 | +}'; |
| 192 | + } |
| 193 | + |
| 194 | + if ( $medium_enabled ) { |
| 195 | + $prev_styles = $styles ? $styles . $spacer : $styles; |
| 196 | + $styles = $prev_styles . "/* Medium screens (tablets, between {$medium} and {$large}) */ |
| 197 | +@media ( min-width: {$medium} ) and ( max-width: " . set_max_width( $large ) . ' ) { |
| 198 | +.block-visibility-hide-medium-screen { |
| 199 | + display: none !important; |
| 200 | +} |
| 201 | +}'; |
| 202 | + } |
| 203 | + |
| 204 | + if ( $small_enabled ) { |
| 205 | + $prev_styles = $styles ? $styles . $spacer : $styles; |
| 206 | + $styles = $prev_styles . "/* Small screens (landscape mobile devices, between {$small} and {$medium}) */ |
| 207 | +@media ( min-width: {$small} ) and ( max-width: " . set_max_width( $medium ) . ' ) { |
| 208 | + .block-visibility-hide-small-screen { |
| 209 | + display: none !important; |
| 210 | + } |
| 211 | +}'; |
| 212 | + } |
| 213 | + |
| 214 | + if ( $extra_small_enabled ) { |
| 215 | + $prev_styles = $styles ? $styles . $spacer : $styles; |
| 216 | + $styles = $prev_styles . "/* Extra small screens (portrait mobile devices, less than {$small}) */ |
| 217 | +@media ( max-width: " . set_max_width( $small ) . ' ) { |
| 218 | + .block-visibility-hide-extra-small-screen { |
| 219 | + display: none !important; |
| 220 | + } |
| 221 | +}'; |
| 222 | + } |
| 223 | + |
| 224 | + if ( ! $styles ) { |
| 225 | + $styles = '/* All screen size controls have been disabled. */'; |
| 226 | + } |
| 227 | + |
| 228 | + return $styles; |
| 229 | +} |
| 230 | + |
| 231 | +/** |
| 232 | + * Takes a given width string and subracts 0.02. The width string includes 'px' |
| 233 | + * so need to remove that first to do calculation, then add it back. |
| 234 | + * |
| 235 | + * @since 1.5.0 |
| 236 | + * |
| 237 | + * @param string $width A given width string. |
| 238 | + * @return string The width string minus 0.02. |
| 239 | + */ |
| 240 | +function set_max_width( $width ) { |
| 241 | + $max_width = trim( $width, 'px' ) - 0.02; |
| 242 | + return (string) $max_width . 'px'; |
| 243 | +} |
| 244 | + |
| 245 | +// Require utlity functions for tests. |
| 246 | +require_once BLOCK_VISIBILITY_ABSPATH . 'includes/utils/is-control-enabled.php'; |
| 247 | +require_once BLOCK_VISIBILITY_ABSPATH . 'includes/utils/get-setting.php'; |
0 commit comments