Skip to content

Commit a4a39a9

Browse files
author
Nick Diego
committed
Merge branch 'develop' into main
2 parents 505563d + 95f2559 commit a4a39a9

File tree

40 files changed

+2276
-376
lines changed

40 files changed

+2276
-376
lines changed

block-visibility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Block Visibility
44
* Plugin URI: https://www.blockvisibilitywp.com/
55
* Description: Provides visibility controls and scheduling functionality to all WordPress blocks.
6-
* Version: 1.4.3
6+
* Version: 1.5.0
77
* Requires at least: 5.5
88
* Requires PHP: 5.6
99
* Author: Nick Diego

includes/admin/editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function is_full_control_mode() {
103103

104104
if ( isset( $settings['plugin_settings']['enable_full_control_mode'] ) ) {
105105
if ( $settings['plugin_settings']['enable_full_control_mode'] ) {
106-
$enabled = true;
106+
$enabled = true;
107107
}
108108
}
109109

includes/admin/settings.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ function enqueue_settings_assets() {
7676
$asset_file['version']
7777
);
7878

79-
// @TODO convert to wp_add_inline_script.
80-
wp_localize_script(
81-
'wp-api',
82-
'wpApiSettings',
83-
array(
84-
'root' => esc_url_raw( rest_url() ),
85-
'nonce' => wp_create_nonce( 'wp_rest' ),
86-
)
87-
);
88-
8979
// Get all the registed block categories.
9080
$block_categories = array();
9181

includes/class-block-visibility.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class Block_Visibility {
2121
* @since 1.4.0
2222
* @var string
2323
*/
24-
public $version = '1.4.3';
24+
public $version = '1.5.0';
2525

2626
/**
2727
* Return singleton instance of the Block Visibility plugin.
@@ -103,19 +103,20 @@ public function includes() {
103103
// needs to be included at all times.
104104
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/utils/user-functions.php';
105105

106+
// General utility functions.
107+
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/utils/get-asset-file.php';
108+
106109
// Only include in the admin.
107110
if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
108111
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/admin/editor.php';
109112
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/admin/settings.php';
110113
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/admin/plugin-action-links.php';
111-
112-
// Utility functions.
113-
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/utils/get-asset-file.php';
114114
}
115115

116116
// Only include on the frontend.
117117
if ( ! is_admin() ) {
118118
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/render-block.php';
119+
include_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/styles.php';
119120
}
120121
}
121122

@@ -195,6 +196,26 @@ public function add_attributes_to_registered_blocks() {
195196
),
196197
),
197198
),
199+
'hideOnScreenSize' => array(
200+
'type' => 'object',
201+
'properties' => array(
202+
'extraLarge' => array(
203+
'type' => 'boolean',
204+
),
205+
'large' => array(
206+
'type' => 'boolean',
207+
),
208+
'medium' => array(
209+
'type' => 'boolean',
210+
),
211+
'small' => array(
212+
'type' => 'boolean',
213+
),
214+
'extraSmall' => array(
215+
'type' => 'boolean',
216+
),
217+
),
218+
),
198219
// Depracated attributes.
199220
'startDateTime' => array(
200221
'type' => 'string',

includes/frontend/styles.php

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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';

includes/frontend/visibility-tests/scheduling.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ function scheduling_test( $is_visible, $settings, $attributes ) {
143143
}
144144

145145
// If this functionality has been disabled, skip test.
146-
if ( ! is_control_enabled( $settings, 'date_time' ) ) {
146+
if (
147+
! is_control_enabled( $settings, 'date_time' ) ||
148+
! is_control_enabled( $settings, 'date_time', 'enable_scheduling' )
149+
) {
147150
return true;
148151
}
149152

0 commit comments

Comments
 (0)