From 895b86f35fc0543097a035c8a0c7f9ae534f7665 Mon Sep 17 00:00:00 2001 From: Matthias Kittsteiner Date: Thu, 22 Aug 2019 10:06:48 +0200 Subject: [PATCH] :sparkles: Add visibility options for logged in/out users --- src/class-block-control.php | 45 +++++++++++++++++++++++++++++++++++++ src/control/attributes.js | 4 ++++ src/control/controls.js | 19 ++++++++++++---- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/class-block-control.php b/src/class-block-control.php index eee29f0..a1a09ab 100644 --- a/src/class-block-control.php +++ b/src/class-block-control.php @@ -5,8 +5,10 @@ use function add_filter; use function dirname; use function file_exists; +use function is_user_logged_in; use function load_plugin_textdomain; use function plugin_basename; +use function strpos; use function wp_enqueue_script; /** @@ -86,6 +88,36 @@ private function hide_desktop( $attr, $value ) { return false; } + /** + * Test if the content should be hidden by its attributes. + * + * @param string $attr The attribute name + * @param bool $value The attribute value + * @return bool True if the content should be hidden, false otherwise + */ + private function hide_logged_in( $attr, $value ) { + if ( $attr === 'login_status' && $value === 'logged-out' && is_user_logged_in() ) { + return true; + } + + return false; + } + + /** + * Test if the content should be hidden by its attributes. + * + * @param string $attr The attribute name + * @param bool $value The attribute value + * @return bool True if the content should be hidden, false otherwise + */ + private function hide_logged_out( $attr, $value ) { + if ( $attr === 'login_status' && $value === 'logged-in' && ! is_user_logged_in() ) { + return true; + } + + return false; + } + /** * Test if the content should be hidden by its attributes. * @@ -145,6 +177,11 @@ public function toggle_blocks( $block_content, $block ) { // set default content $content = ''; + // if there are no attributes, the block should be displayed + if ( empty( $block['attrs'] ) ) { + $content = $block_content; + } + // iterate through all block attributes foreach ( $block['attrs'] as $attr => $value ) { if ( $this->hide_desktop( $attr, $value ) ) { @@ -159,6 +196,14 @@ public function toggle_blocks( $block_content, $block ) { break; } + if ( $this->hide_logged_in( $attr, $value ) ) { + break; + } + + if ( $this->hide_logged_out( $attr, $value ) ) { + break; + } + // get the block content to output it $content = $block_content; } diff --git a/src/control/attributes.js b/src/control/attributes.js index 5137a68..f6ace36 100644 --- a/src/control/attributes.js +++ b/src/control/attributes.js @@ -22,6 +22,10 @@ const addControlAttribute = ( settings ) => { default: false, type: 'boolean', }, + login_status: { + default: 'none', + type: 'string', + }, } ); return settings; diff --git a/src/control/controls.js b/src/control/controls.js index 1cebce4..7e67e3e 100644 --- a/src/control/controls.js +++ b/src/control/controls.js @@ -4,7 +4,7 @@ const { createHigherOrderComponent } = wp.compose; const { Fragment } = wp.element; const { InspectorControls } = wp.editor; -const { PanelBody, ToggleControl } = wp.components; +const { PanelBody, RadioControl, ToggleControl } = wp.components; const { addFilter } = wp.hooks; const { __ } = wp.i18n; @@ -18,6 +18,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => { hide_desktop, hide_mobile, hide_tablet, + login_status, }, setAttributes, } = props; @@ -31,23 +32,33 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => { title={ __( 'Visibility', 'block-control' ) } > setAttributes( { hide_mobile: value } ) } /> setAttributes( { hide_tablet: value } ) } /> setAttributes( { hide_desktop: value } ) } /> + setAttributes( { login_status: value } ) } + />