Skip to content

Commit

Permalink
✨ Add visibility options for logged in/out users
Browse files Browse the repository at this point in the history
  • Loading branch information
MatzeKitt committed Aug 22, 2019
1 parent bdaeeb4 commit 895b86f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
45 changes: 45 additions & 0 deletions src/class-block-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/control/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const addControlAttribute = ( settings ) => {
default: false,
type: 'boolean',
},
login_status: {
default: 'none',
type: 'string',
},
} );

return settings;
Expand Down
19 changes: 15 additions & 4 deletions src/control/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,6 +18,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
hide_desktop,
hide_mobile,
hide_tablet,
login_status,
},
setAttributes,
} = props;
Expand All @@ -31,23 +32,33 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
title={ __( 'Visibility', 'block-control' ) }
>
<ToggleControl
label={ __( 'Hide on Mobile', 'block-control' ) }
label={ __( 'Hide on mobile devices', 'block-control' ) }
value={ hide_mobile }
checked={ !! hide_mobile }
onChange={ ( value ) => setAttributes( { hide_mobile: value } ) }
/>
<ToggleControl
label={ __( 'Hide on Tablets', 'block-control' ) }
label={ __( 'Hide on tablets', 'block-control' ) }
value={ hide_tablet }
checked={ !! hide_tablet }
onChange={ ( value ) => setAttributes( { hide_tablet: value } ) }
/>
<ToggleControl
label={ __( 'Hide on Desktop', 'block-control' ) }
label={ __( 'Hide on desktops', 'block-control' ) }
value={ hide_desktop }
checked={ !! hide_desktop }
onChange={ ( value ) => setAttributes( { hide_desktop: value } ) }
/>
<RadioControl
label={ __( 'Login status', 'block-control' ) }
selected={ login_status }
options={ [
{ label: __( 'Show for all users', 'block-control' ), value: 'none' },
{ label: __( 'Show for logged in users', 'block-control' ), value: 'logged-in' },
{ label: __( 'Show for logged out users', 'block-control' ), value: 'logged-out' },
] }
onChange={ ( value ) => setAttributes( { login_status: value } ) }
/>
</PanelBody>
</InspectorControls>
</Fragment>
Expand Down

0 comments on commit 895b86f

Please sign in to comment.