Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Diego committed Mar 21, 2021
2 parents 85d77b8 + 0e60e36 commit f938306
Show file tree
Hide file tree
Showing 27 changed files with 1,602 additions and 1,015 deletions.
2 changes: 1 addition & 1 deletion block-visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Block Visibility
* Plugin URI: https://www.blockvisibilitywp.com/
* Description: Provides visibility controls and scheduling functionality to all WordPress blocks.
* Version: 1.5.3
* Version: 1.6.0
* Requires at least: 5.5
* Requires PHP: 5.6
* Author: Nick Diego
Expand Down
71 changes: 2 additions & 69 deletions includes/class-block-visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Block_Visibility {
* @since 1.4.0
* @var string
*/
public $version = '1.5.3';
public $version = '1.6.0';

/**
* Return singleton instance of the Block Visibility plugin.
Expand Down Expand Up @@ -164,75 +164,8 @@ public function add_attributes_to_registered_blocks() {

$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();

$attributes = array(
'type' => 'object',
'properties' => array(
'hideBlock' => array(
'type' => 'boolean',
),
'visibilityByRole' => array(
'type' => 'string',
),
'hideOnRestrictedRoles' => array(
'type' => 'boolean',
),
'restrictedRoles' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
'scheduling' => array(
'type' => 'object',
'properties' => array(
'enable' => array(
'type' => 'boolean',
),
'start' => array(
'type' => 'string',
),
'end' => array(
'type' => 'string',
),
),
),
'hideOnScreenSize' => array(
'type' => 'object',
'properties' => array(
'extraLarge' => array(
'type' => 'boolean',
),
'large' => array(
'type' => 'boolean',
),
'medium' => array(
'type' => 'boolean',
),
'small' => array(
'type' => 'boolean',
),
'extraSmall' => array(
'type' => 'boolean',
),
),
),
// Depracated attributes.
'startDateTime' => array(
'type' => 'string',
),
'endDateTime' => array(
'type' => 'string',
),
),
);

$attributes = apply_filters(
'block_visibility_attributes',
$attributes
);

foreach ( $registered_blocks as $name => $block ) {
$block->attributes['blockVisibility'] = $attributes;
$block->attributes['blockVisibility'] = array( 'type' => 'object' );
}
}

Expand Down
4 changes: 2 additions & 2 deletions includes/frontend/render-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ function render_with_visibility( $block_content, $block ) {

// Run our tests.
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/hide-block.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/visibility-by-role.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/scheduling.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/user-role.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/date-time.php';

// Require utlity functions for tests.
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/utils/is-control-enabled.php';
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @param boolean $localize Should we localize the time string, or just append the timezone.
* @return Object Return the DateTime object for the timestamp and timezone.
*/
function create_datetime( $timestamp = null, $localize = true ) {
function create_date_time( $timestamp = null, $localize = true ) {

// The timezone settings from the WordPress general settings.
$tz_string = get_option( 'timezone_string' );
Expand Down Expand Up @@ -74,55 +74,32 @@ function create_datetime( $timestamp = null, $localize = true ) {
return $datetime;
}

/**
* Helper function for getting the start date. Function checks if the depracated
* startDateTime attribute is set and handles accordingly.
*
* @since 1.4.1
*
* @param array $attributes The block visibility attributes.
* @param boolean $enable Is scheduling enabled.
* @return string Return the correct start date.
*/
function get_start( $attributes, $enable ) {
$depracated_start = isset( $attributes['startDateTime'] )
? $attributes['startDateTime']
: null;
$new_start = isset( $attributes['scheduling']['start'] )
? $attributes['scheduling']['start']
: null;

if ( isset( $enable ) ) {
return $new_start;
}

return $depracated_start;
}

/**
* Helper function for getting the end date. Function checks if the depracated
* endDateTime attribute is set and handles accordingly.
*
* @since 1.4.1
* @since 1.6.0
*
* @param array $attributes The block visibility attributes.
* @param boolean $enable Is scheduling enabled.
* @return string Return the correct end date.
* @param array $attributes The block visibility attributes.
* @param array $schedule_atts The schedule specific attributes.
* @param string $old The old date time attribute.
* @param string $new The new date time attribute.
* @return string Return the correct end date.
*/
function get_end( $attributes, $enable ) {
$depracated_end = isset( $attributes['endDateTime'] )
? $attributes['endDateTime']
function get_date_time( $attributes, $schedule_atts, $old, $new ) {
$depracated_date_time = isset( $attributes[ $old ] )
? $attributes[ $old ]
: null;
$new_end = isset( $attributes['scheduling']['end'] )
? $attributes['scheduling']['end']
$new_date_time = isset( $schedule_atts[ $new ] )
? $schedule_atts[ $new ]
: null;

// If the enable setting exists then use the new start attribute.
if ( isset( $enable ) ) {
return $new_end;
if ( isset( $schedule_atts['enable'] ) ) {
return $new_date_time;
}

return $depracated_end;
return $depracated_date_time;
}

/**
Expand All @@ -135,7 +112,7 @@ function get_end( $attributes, $enable ) {
* @param array $attributes The block visibility attributes.
* @return boolean Return true is the block should be visible, false if not.
*/
function scheduling_test( $is_visible, $settings, $attributes ) {
function date_time_test( $is_visible, $settings, $attributes ) {

// The test is already false, so skip this test, the block should be hidden.
if ( ! $is_visible ) {
Expand All @@ -150,11 +127,44 @@ function scheduling_test( $is_visible, $settings, $attributes ) {
return true;
}

$enable = isset( $attributes['scheduling']['enable'] )
? $attributes['scheduling']['enable']
: null;
$start = get_start( $attributes, $enable );
$end = get_end( $attributes, $enable );
$has_control_sets = isset( $attributes['controlSets'] );

if ( $has_control_sets ) {
// Just retrieve the first set and schedule, need to update in future.
$schedule_atts =
isset( $attributes['controlSets'][0]['controls']['dateTime']['schedules'][0] )
? $attributes['controlSets'][0]['controls']['dateTime']['schedules'][0]
: null;
} else {
$schedule_atts = isset( $attributes['scheduling'] )
? $attributes['scheduling']
: null;
}

// There are no date time settings, skip tests.
if (
! $schedule_atts &&
! isset( $attributes['startDateTime'] ) &&
! isset( $attributes['endDateTime'] )
) {
return true;
}

$enable = isset( $schedule_atts['enable'] )
? $schedule_atts['enable']
: true;
$start = get_date_time(
$attributes,
$schedule_atts,
'startDateTime',
'start'
);
$end = get_date_time(
$attributes,
$schedule_atts,
'endDateTime',
'end'
);

// The new enable setting does not exist and there are no depracated
// scheduling settings, so skip the est.
Expand All @@ -173,8 +183,8 @@ function scheduling_test( $is_visible, $settings, $attributes ) {
return true;
}

$start = $start ? create_datetime( $start, false ) : null;
$end = $end ? create_datetime( $end, false ) : null;
$start = $start ? create_date_time( $start, false ) : null;
$end = $end ? create_date_time( $end, false ) : null;

// If the start date is before the end date, skip test.
if ( ( $start && $end ) && $start > $end ) {
Expand All @@ -191,4 +201,4 @@ function scheduling_test( $is_visible, $settings, $attributes ) {
// Block has passed the date & time test.
return true;
}
add_filter( 'block_visibility_is_block_visible', __NAMESPACE__ . '\scheduling_test', 10, 3 );
add_filter( 'block_visibility_is_block_visible', __NAMESPACE__ . '\date_time_test', 10, 3 );
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @param array $attributes The block visibility attributes.
* @return boolean Return true is the block should be visible, false if not.
*/
function visibility_by_role_test( $is_visible, $settings, $attributes ) {
function user_role_test( $is_visible, $settings, $attributes ) {

// The test is already false, so skip this test, the block should be hidden.
if ( ! $is_visible ) {
Expand All @@ -37,8 +37,25 @@ function visibility_by_role_test( $is_visible, $settings, $attributes ) {
return true;
}

$visibility_by_role = isset( $attributes['visibilityByRole'] )
? $attributes['visibilityByRole']
$has_control_sets = isset( $attributes['controlSets'] );

if ( $has_control_sets ) {
// Just retrieve the first set and schedule, need to update in future.
$user_role_atts =
isset( $attributes['controlSets'][0]['controls']['userRole'] )
? $attributes['controlSets'][0]['controls']['userRole']
: null;
} else {
$user_role_atts = $attributes;
}

// There are no user role settings, skip tests.
if ( ! $user_role_atts ) {
return true;
}

$visibility_by_role = isset( $user_role_atts['visibilityByRole'] )
? $user_role_atts['visibilityByRole']
: null;

// Run through our visibility by role tests.
Expand All @@ -59,29 +76,36 @@ function visibility_by_role_test( $is_visible, $settings, $attributes ) {
return true;
}

$restricted_roles = isset( $attributes['restrictedRoles'] )
? $attributes['restrictedRoles']
$restricted_roles = isset( $user_role_atts['restrictedRoles'] )
? $user_role_atts['restrictedRoles']
: array();

$hide_on_resticted_roles = isset( $attributes['hideOnRestrictedRoles'] )
? $attributes['hideOnRestrictedRoles']
: false;
$hide_on_resticted_roles =
isset( $user_role_atts['hideOnRestrictedRoles'] )
? $user_role_atts['hideOnRestrictedRoles']
: false;

// Make sure there are restricted roles set, if not the block should be
// hidden, unless "hide on restricted" has been set.
if ( ! empty( $restricted_roles ) ) {

if (
(
in_array( 'logged-out', $restricted_roles, true )
|| in_array( 'public', $restricted_roles, true ) // Depractated role option, but check regardless.
)
&& ! is_user_logged_in()
&& ! $hide_on_resticted_roles
) {
return true;
// If user is logged out.
if ( ! is_user_logged_in() ) {
$in_restricted_roles =
in_array( 'logged-out', $restricted_roles, true ) ||
in_array( 'public', $restricted_roles, true ); // Depractated role option, but check regardless.

if ( ! $hide_on_resticted_roles && $in_restricted_roles ) {
return true;
} elseif (
$hide_on_resticted_roles &&
! $in_restricted_roles
) {
return true;
}
}

// If user is logged in.
if ( is_user_logged_in() ) {

// Get the roles of the current user since they are logged-in.
Expand Down Expand Up @@ -111,4 +135,4 @@ function visibility_by_role_test( $is_visible, $settings, $attributes ) {
// If we don't pass any of the above tests, hide the block.
return false;
}
add_filter( 'block_visibility_is_block_visible', __NAMESPACE__ . '\visibility_by_role_test', 10, 3 );
add_filter( 'block_visibility_is_block_visible', __NAMESPACE__ . '\user_role_test', 10, 3 );
Loading

0 comments on commit f938306

Please sign in to comment.