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 29, 2021
2 parents f938306 + 9e05aa0 commit d25ad4e
Show file tree
Hide file tree
Showing 51 changed files with 14,015 additions and 443 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.6.0
* Version: 1.7.0
* Requires at least: 5.5
* Requires PHP: 5.6
* Author: Nick Diego
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function enqueue_settings_assets() {
// The full homepage url for use in fetching data from the REST api. The
// full path is needed on websites where WordPress in installed in a
// subdirectory. Without the full path, fetch( '/wp-json/...') fails.
$home_url = 'const blockVisibilityHomeUrl = "' . home_url() . '";';
$home_url = 'const blockVisibilityRestUrl = "' . get_rest_url() . '";';

wp_add_inline_script(
'block-visibility-setting-scripts',
Expand Down
47 changes: 37 additions & 10 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.6.0';
public $version = '1.7.0';

/**
* Return singleton instance of the Block Visibility plugin.
Expand All @@ -42,7 +42,6 @@ public static function instance() {
* Cloning instances of the class is forbidden.
*
* @since 1.0.0
* @return void
*/
public function __clone() {
_doing_it_wrong(
Expand All @@ -56,7 +55,6 @@ public function __clone() {
* Unserializing instances of the class is forbidden.
*
* @since 1.0.0
* @return void
*/
public function __wakeup() {
_doing_it_wrong(
Expand All @@ -79,19 +77,20 @@ private function __construct() {
* Load required actions.
*
* @since 1.0.0
* @return void
*/
public function actions() {
add_action( 'wp_loaded', array( $this, 'add_attributes_to_registered_blocks' ), 999 );
add_action( 'init', array( $this, 'load_textdomain' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'block_localization' ) );

// Specific fixes/work arounds for server-side blocks.
add_action( 'wp_loaded', array( $this, 'add_attributes_to_registered_blocks' ), 999 );
add_filter( 'rest_pre_dispatch', array( $this, 'conditionally_remove_attributes' ), 10, 3 );
}

/**
* Include required files.
*
* @since 1.0.0
* @return void
*/
public function includes() {

Expand Down Expand Up @@ -121,7 +120,7 @@ public function includes() {
}

/**
* Define the contants for the Block Visibility base (BVB) plugin.
* Define the contants for the Block Visibility plugin.
*
* @since 1.4.0
*/
Expand All @@ -138,6 +137,7 @@ private function define_constants() {
* Define constant if not already set.
*
* @since 1.4.0
*
* @param string $name Constant name.
* @param string|bool $value Constant value.
*/
Expand All @@ -158,7 +158,6 @@ private function define( $name, $value ) {
* Reference: https://github.com/WordPress/gutenberg/issues/16850
*
* @since 1.0.0
* @return void
*/
public function add_attributes_to_registered_blocks() {

Expand All @@ -169,11 +168,40 @@ public function add_attributes_to_registered_blocks() {
}
}

/**
* Fix REST API issue with blocks rendered server-side. Without this,
* server-side blocks will not load in the block editor when visibility
* controls have been added.
*
* Reference: https://github.com/phpbits/block-options/blob/f741344033a2c9455828d039881616f77ef109fe/includes/class-editorskit-post-meta.php#L82-L112
*
* @since 1.7.0
*
* @param mixed $result Response to replace the requested version with.
* @param object $server Server instance.
* @param object $request Request used to generate the response.
*
* @return array Returns updated results.
*/
public function conditionally_remove_attributes( $result, $server, $request ) {

if ( strpos( $request->get_route(), '/wp/v2/block-renderer' ) !== false ) {

if ( isset( $request['attributes'] ) && isset( $request['attributes']['blockVisibility'] ) ) {

$attributes = $request['attributes'];
unset( $attributes['blockVisibility'] );
$request['attributes'] = $attributes;
}
}

return $result;
}

/**
* Loads the plugin language files.
*
* @since 1.0.0
* @return void
*/
public function load_textdomain() {
load_plugin_textdomain(
Expand All @@ -187,7 +215,6 @@ public function load_textdomain() {
* Enqueue localization data for our blocks.
*
* @since 1.0.0
* @return void
*/
public function block_localization() {
if ( function_exists( 'wp_set_script_translations' ) ) {
Expand Down
19 changes: 5 additions & 14 deletions includes/frontend/render-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ function has_visibility_settings( $block ) {
*/
function render_with_visibility( $block_content, $block ) {

/**
* Needed for server side rendered blocks since they are rendered via REST
* API endpoint. This endpoint calls the render function in the admin, whereas
* the render function is called directly on the frontend. The function
* is_admin() checks if a backend page was requested. In a REST API Request
* is no backend page, so the function returns false on REST API requests.
*
* Reference: wp-includes/rest-api.php line 302
*/
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $block_content;
}

// Get the plugin core settings.
$settings = get_option( 'block_visibility_settings' );
$attributes = isset( $block['attrs']['blockVisibility'] )
Expand Down Expand Up @@ -109,10 +96,14 @@ function render_with_visibility( $block_content, $block ) {
}
add_filter( 'render_block', __NAMESPACE__ . '\render_with_visibility', 10, 2 );

// Run our tests.
// Run our core tests.
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/hide-block.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_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/query-string.php';

// run our integraiton tests.
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/wp-fusion.php';

// Require utlity functions for tests.
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/utils/is-control-enabled.php';
2 changes: 1 addition & 1 deletion includes/frontend/visibility-tests/date-time.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Adds a filter to the visibility test for "date and time" setting.
* Adds a filter to the visibility test for Date & Time control.
*
* @package block-visibility
* @since 1.1.0
Expand Down
2 changes: 1 addition & 1 deletion includes/frontend/visibility-tests/hide-block.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Adds a filter to the visibility test for the "hide block" setting.
* Adds a filter to the visibility test for the Hide Block control.
*
* @package block-visibility
* @since 1.0.0
Expand Down
140 changes: 140 additions & 0 deletions includes/frontend/visibility-tests/query-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* Adds a filter to the visibility test for the Query String control.
*
* @package block-visibility
* @since 1.7.0
*/

namespace BlockVisibility\Frontend\VisibilityTests;

defined( 'ABSPATH' ) || exit;

/**
* Internal dependencies
*/
use function BlockVisibility\Utils\is_control_enabled as is_control_enabled;

/**
* Turn a query string into an array.
*
* @since 1.7.0
*
* @param string $queries A string of queries.
* @return array $ordered_queries An array of queries.
*/
function prepare_queries( $queries ) {
if ( empty( $queries ) ) {
return null;
}

$query_array = explode( "\n", $queries );

$ordered_queries = array();

foreach ( $query_array as $query ) {
parse_str( $query, $result );
$ordered_queries = array_merge( $ordered_queries, $result );
}
return $ordered_queries;
}

/**
* Run test to see if block visibility should be restricted by query string.
*
* @since 1.7.0
*
* @param boolean $is_visible The current value of the visibility test.
* @param array $settings The core plugin settings.
* @param array $attributes The block visibility attributes.
* @return boolean Return true is the block should be visible, false if not
*/
function query_string_test( $is_visible, $settings, $attributes ) {

// The test is already false, so skip this test, the block should be hidden.
if ( ! $is_visible ) {
return $is_visible;
}

// If this control has been disabled, skip test.
if ( ! is_control_enabled( $settings, 'query_string' ) ) {
return true;
}

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

if ( $has_control_sets ) {
// Just retrieve the first set, need to update in future.
$query_string_atts =
isset( $attributes['controlSets'][0]['controls']['queryString'] )
? $attributes['controlSets'][0]['controls']['queryString']
: null;
} else {
// There are no Query String settings, so skip tests.
return true;
}

$query_string_any = isset( $query_string_atts['queryStringAny'] )
? prepare_queries( $query_string_atts['queryStringAny'] )
: null;
$query_string_all = isset( $query_string_atts['queryStringAll'] )
? prepare_queries( $query_string_atts['queryStringAll'] )
: null;
$query_string_not = isset( $query_string_atts['queryStringNot'] )
? prepare_queries( $query_string_atts['queryStringNot'] )
: null;

// If there is "any" query strings, need to find at lease one match to pass.
if ( ! empty( $query_string_any ) ) {
$any_matches = 0;

foreach ( $query_string_any as $param => $value ) {
if ( isset( $_REQUEST[ $param ] ) ) {
if ( ! $value || '*' === $value ) {
$any_matches++;
} elseif ( $value === $_REQUEST[ $param ] ) {
$any_matches++;
}
}
}

if ( 0 === $any_matches ) {
return false;
}
}

// If there is "all" query strings, need to match all to pass.
if ( ! empty( $query_string_all ) ) {
$all_matches = 0;

foreach ( $query_string_all as $param => $value ) {
if ( isset( $_REQUEST[ $param ] ) ) {
if ( ! $value || '*' === $value ) {
$all_matches++;
} elseif ( $value === $_REQUEST[ $param ] ) {
$all_matches++;
}
}
}

if ( count( $query_string_all ) !== $all_matches ) {
return false;
}
}

// If there is "not" query strings, need to not match any to pass.
if ( ! empty( $query_string_not ) ) {
foreach ( $query_string_not as $param => $value ) {
if ( isset( $_REQUEST[ $param ] ) ) {
if ( ! $value || '*' === $value ) {
return false;
} elseif ( $value === $_REQUEST[ $param ] ) {
return false;
}
}
}
}

return true;
}
add_filter( 'block_visibility_is_block_visible', __NAMESPACE__ . '\query_string_test', 10, 3 );
2 changes: 1 addition & 1 deletion includes/frontend/visibility-tests/user-role.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Adds a filter to the visibility test for "visibility by user role" setting.
* Adds a filter to the visibility test for the User Role control.
*
* @package block-visibility
* @since 1.0.0
Expand Down
Loading

0 comments on commit d25ad4e

Please sign in to comment.