mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-07-13 19:56:27 +09:00
- added ability to set restriction options for each Gutenberg block;
This commit is contained in:
@@ -199,6 +199,8 @@ a.um-delete{ color: #a00; }
|
||||
margin-bottom: 2px !important;
|
||||
}
|
||||
|
||||
.um_hidden_notice {
|
||||
.um_hidden_notice,
|
||||
.um_block_settings_hide {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Add Control element
|
||||
*/
|
||||
var um_el = wp.element.createElement;
|
||||
|
||||
var um_components = wp.components,
|
||||
umToggleControl = um_components.ToggleControl,
|
||||
umSelectControl = um_components.SelectControl,
|
||||
umTextareaControl = um_components.TextareaControl,
|
||||
umPanelBody = um_components.PanelBody;
|
||||
|
||||
var __ = wp.i18n.__;
|
||||
|
||||
var um_block_restriction = wp.compose.createHigherOrderComponent( function( BlockEdit ) {
|
||||
var um_condition_fields = {
|
||||
um_who_access:'um_block_settings_hide',
|
||||
um_roles_access:'um_block_settings_hide',
|
||||
um_message_type:'um_block_settings_hide',
|
||||
um_message_content:'um_block_settings_hide'
|
||||
};
|
||||
|
||||
return function( props ) {
|
||||
|
||||
if ( props.attributes.um_is_restrict !== true ) {
|
||||
um_condition_fields['um_who_access'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_roles_access'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_message_type'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_message_content'] = 'um_block_settings_hide';
|
||||
} else {
|
||||
um_condition_fields['um_who_access'] = '';
|
||||
|
||||
if ( parseInt( props.attributes.um_who_access ) === 0 || typeof props.attributes.um_who_access === 'undefined' ) {
|
||||
um_condition_fields['um_roles_access'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_message_type'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_message_content'] = 'um_block_settings_hide';
|
||||
} else if ( parseInt( props.attributes.um_who_access ) === 1 ) {
|
||||
um_condition_fields['um_roles_access'] = '';
|
||||
um_condition_fields['um_message_type'] = '';
|
||||
|
||||
if ( parseInt( props.attributes.um_message_type ) === 0 || typeof props.attributes.um_message_type === 'undefined' ) {
|
||||
um_condition_fields['um_message_content'] = 'um_block_settings_hide';
|
||||
} else {
|
||||
um_condition_fields['um_message_content'] = '';
|
||||
}
|
||||
} else {
|
||||
um_condition_fields['um_message_type'] = '';
|
||||
|
||||
if ( parseInt( props.attributes.um_message_type ) === 0 || typeof props.attributes.um_message_type === 'undefined' ) {
|
||||
um_condition_fields['um_message_content'] = 'um_block_settings_hide';
|
||||
} else {
|
||||
um_condition_fields['um_message_content'] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return um_el(
|
||||
wp.element.Fragment,
|
||||
{},
|
||||
um_el( BlockEdit, props ),
|
||||
um_el(
|
||||
wp.editor.InspectorControls,
|
||||
{},
|
||||
um_el(
|
||||
umPanelBody,
|
||||
{
|
||||
title: __( 'UM access Controls', 'ultimate-member' )
|
||||
},
|
||||
um_el(
|
||||
umToggleControl,
|
||||
{
|
||||
label: __( 'Restrict access?', 'ultimate-member' ),
|
||||
checked: props.attributes.um_is_restrict,
|
||||
onChange: function onChange( value ) {
|
||||
props.setAttributes({ um_is_restrict: value });
|
||||
if ( value === false ) {
|
||||
um_condition_fields['um_who_access'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_roles_access'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_message_type'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_message_content'] = 'um_block_settings_hide';
|
||||
} else {
|
||||
um_condition_fields['um_who_access'] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
),
|
||||
um_el(
|
||||
umSelectControl,
|
||||
{
|
||||
type: 'number',
|
||||
className: um_condition_fields['um_who_access'],
|
||||
label: __( 'Who can access this content?', 'ultimate-member' ),
|
||||
value: props.attributes.um_who_access,
|
||||
options: [
|
||||
{
|
||||
label: __( 'Everyone', 'ultimate-member' ),
|
||||
value: 0
|
||||
},
|
||||
{
|
||||
label: __( 'Logged in users', 'ultimate-member' ),
|
||||
value: 1
|
||||
},
|
||||
{
|
||||
label: __( 'Logged out users', 'ultimate-member' ),
|
||||
value: 2
|
||||
}
|
||||
],
|
||||
onChange: function onChange( value ) {
|
||||
props.setAttributes({ um_who_access: value });
|
||||
if ( parseInt( value ) === 0 ) {
|
||||
um_condition_fields['um_message_type'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_message_content'] = 'um_block_settings_hide';
|
||||
um_condition_fields['um_roles_access'] = 'um_block_settings_hide';
|
||||
} else if ( parseInt( value ) === 1 ) {
|
||||
um_condition_fields['um_message_type'] = '';
|
||||
um_condition_fields['um_roles_access'] = '';
|
||||
} else {
|
||||
um_condition_fields['um_message_type'] = '';
|
||||
um_condition_fields['um_roles_access'] = 'um_block_settings_hide';
|
||||
}
|
||||
}
|
||||
}
|
||||
),
|
||||
um_el(
|
||||
umSelectControl,
|
||||
{
|
||||
multiple: true,
|
||||
className: um_condition_fields['um_roles_access'],
|
||||
label: __( 'What roles can access this content?', 'ultimate-member' ),
|
||||
value: props.attributes.um_roles_access,
|
||||
options: um_restrict_roles,
|
||||
onChange: function onChange( value ) {
|
||||
props.setAttributes({ um_roles_access: value });
|
||||
}
|
||||
}
|
||||
),
|
||||
um_el(
|
||||
umSelectControl,
|
||||
{
|
||||
type: 'number',
|
||||
className: um_condition_fields['um_message_type'],
|
||||
label: __( 'Restriction Message', 'ultimate-member' ),
|
||||
value: props.attributes.um_message_type,
|
||||
options: [
|
||||
{
|
||||
label: __( 'Global default message', 'ultimate-member' ),
|
||||
value: 0
|
||||
},
|
||||
{
|
||||
label: __( 'Custom Message', 'ultimate-member' ),
|
||||
value: 1
|
||||
}
|
||||
],
|
||||
onChange: function onChange( value ) {
|
||||
props.setAttributes({ um_message_type: value });
|
||||
if ( parseInt( value ) === 0 ) {
|
||||
um_condition_fields['um_message_content'] = 'um_block_settings_hide';
|
||||
} else {
|
||||
um_condition_fields['um_message_content'] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
),
|
||||
um_el(
|
||||
umTextareaControl,
|
||||
{
|
||||
type: 'number',
|
||||
className: um_condition_fields['um_message_content'],
|
||||
label: __( 'Message Content', 'ultimate-member' ),
|
||||
value: props.attributes.um_message_content,
|
||||
onChange: function onChange( value ) {
|
||||
props.setAttributes({ um_message_content: value });
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
};
|
||||
}, 'um_block_restriction' );
|
||||
|
||||
wp.hooks.addFilter( 'editor.BlockEdit', 'um-block/um_block_restriction', um_block_restriction );
|
||||
|
||||
|
||||
/**
|
||||
* Save Attributes
|
||||
*
|
||||
* @type {{um_is_restrict: {type: string}, um_who_access: {type: string}, um_message_type: {type: string}, um_message_content: {type: string}}}
|
||||
*/
|
||||
var um_block_restrict_settings = {
|
||||
um_is_restrict: {
|
||||
type: "boolean"
|
||||
},
|
||||
um_who_access: {
|
||||
type: "select"
|
||||
},
|
||||
um_roles_access: {
|
||||
type: "select"
|
||||
},
|
||||
um_message_type: {
|
||||
type: "select"
|
||||
},
|
||||
um_message_content: {
|
||||
type: "string"
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param settings
|
||||
* @returns {*}
|
||||
*/
|
||||
function um_add_block_attributes( settings ) {
|
||||
var _lodash = lodash,
|
||||
assign = _lodash.assign;
|
||||
|
||||
settings.attributes = assign( settings.attributes, um_block_restrict_settings );
|
||||
return settings;
|
||||
}
|
||||
|
||||
wp.hooks.addFilter( 'blocks.registerBlockType', 'um-block/um_add_block_attributes', um_add_block_attributes );
|
||||
@@ -51,6 +51,13 @@ if ( ! class_exists( 'um\admin\core\Admin_Enqueue' ) ) {
|
||||
*/
|
||||
var $um_cpt_form_screen;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
var $post_page;
|
||||
|
||||
|
||||
/**
|
||||
* Admin_Enqueue constructor.
|
||||
*/
|
||||
@@ -91,6 +98,8 @@ if ( ! class_exists( 'um\admin\core\Admin_Enqueue' ) ) {
|
||||
if ( ( isset( $_GET['post_type'] ) && 'um_form' == $_GET['post_type'] ) || ( isset( $_GET['post'] ) && 'um_form' == get_post_type( $_GET['post'] ) ) ) {
|
||||
$this->um_cpt_form_screen = true;
|
||||
}
|
||||
|
||||
$this->post_page = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -380,6 +389,29 @@ if ( ! class_exists( 'um\admin\core\Admin_Enqueue' ) ) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load Gutenberg scripts
|
||||
*/
|
||||
function load_gutenberg_js() {
|
||||
wp_register_script( 'um_block_js', $this->js_url . 'um-admin-blocks.js', array( 'wp-i18n', 'wp-blocks', 'wp-editor', 'wp-components' ), ultimatemember_version, true );
|
||||
wp_set_script_translations( 'um_block_js', 'ultimate-member' );
|
||||
|
||||
$restrict_options = array();
|
||||
$roles = UM()->roles()->get_roles( false, array( 'administrator' ) );
|
||||
if ( ! empty( $roles ) ) {
|
||||
foreach ( $roles as $role_key => $title ) {
|
||||
$restrict_options[] = array(
|
||||
'label' => $title,
|
||||
'value' => $role_key
|
||||
);
|
||||
}
|
||||
}
|
||||
wp_localize_script( 'um_block_js', 'um_restrict_roles', $restrict_options );
|
||||
|
||||
wp_enqueue_script( 'um_block_js' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load localize scripts
|
||||
*/
|
||||
@@ -472,6 +504,11 @@ if ( ! class_exists( 'um\admin\core\Admin_Enqueue' ) ) {
|
||||
|
||||
}
|
||||
|
||||
global $wp_version;
|
||||
if ( version_compare( $wp_version, '5.0', '>=' ) && ! empty( $this->post_page ) ) {
|
||||
$this->load_gutenberg_js();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
<?php
|
||||
namespace um\admin\core;
|
||||
|
||||
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
|
||||
if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
|
||||
|
||||
|
||||
@@ -524,11 +526,17 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
|
||||
'conditional' => array( 'accessible', '=', 2 ),
|
||||
),
|
||||
array(
|
||||
'id' => 'restricted_access_message',
|
||||
'type' => 'wp_editor',
|
||||
'label' => __( 'Restricted Access Message','ultimate-member' ),
|
||||
'id' => 'restricted_access_message',
|
||||
'type' => 'wp_editor',
|
||||
'label' => __( 'Restricted Access Message','ultimate-member' ),
|
||||
'tooltip' => __( 'This is the message shown to users that do not have permission to view the content','ultimate-member' ),
|
||||
),
|
||||
array(
|
||||
'id' => 'restricted_block_message',
|
||||
'type' => 'textarea',
|
||||
'label' => __( 'Restricted Block Message', 'ultimate-member' ),
|
||||
'tooltip' => __( 'This is the message shown to users that do not have permission to view the block\'s content', 'ultimate-member' ),
|
||||
),
|
||||
array(
|
||||
'id' => 'restricted_access_post_metabox',
|
||||
'type' => 'hidden',
|
||||
|
||||
@@ -507,6 +507,8 @@ if ( ! class_exists( 'um\Config' ) ) {
|
||||
'access_exclude_uris' => array(),
|
||||
'home_page_accessible' => 1,
|
||||
'category_page_accessible' => 1,
|
||||
'restricted_access_message' => '',
|
||||
'restricted_block_message' => '',
|
||||
'enable_reset_password_limit' => 1,
|
||||
'reset_password_limit_number' => 3,
|
||||
'blocked_emails' => '',
|
||||
|
||||
@@ -69,6 +69,8 @@ if ( ! class_exists( 'um\core\Access' ) ) {
|
||||
/* Disable comments if user has not permission to access current post */
|
||||
add_filter( 'comments_open', array( $this, 'disable_comments_open' ), 99, 2 );
|
||||
add_filter( 'get_comments_number', array( $this, 'disable_comments_open_number' ), 99, 2 );
|
||||
|
||||
add_filter( 'render_block', array( $this, 'restrict_blocks' ), 10, 2 );
|
||||
}
|
||||
|
||||
|
||||
@@ -1153,5 +1155,83 @@ if ( ! class_exists( 'um\core\Access' ) ) {
|
||||
|
||||
return $filtered_items;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $block_content
|
||||
* @param $block
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function restrict_blocks( $block_content, $block ) {
|
||||
if ( is_admin() ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
if ( $block['attrs']['um_is_restrict'] !== true ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
if ( empty( $block['attrs']['um_who_access'] ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$default_message = UM()->options()->get( 'restricted_block_message' );
|
||||
switch ( $block['attrs']['um_who_access'] ) {
|
||||
case '1': {
|
||||
if ( ! is_user_logged_in() ) {
|
||||
$block_content = '';
|
||||
if ( isset( $block['attrs']['um_message_type'] ) ) {
|
||||
if ( $block['attrs']['um_message_type'] == '0' ) {
|
||||
$block_content = $default_message;
|
||||
} elseif ( $block['attrs']['um_message_type'] == '1' ) {
|
||||
$block_content = $block['attrs']['um_message_content'];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( ! empty( $block['attrs']['um_roles_access'] ) ) {
|
||||
$display = false;
|
||||
foreach ( $block['attrs']['um_roles_access'] as $role ) {
|
||||
if ( current_user_can( $role ) ) {
|
||||
$display = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $display ) {
|
||||
$block_content = '';
|
||||
if ( isset( $block['attrs']['um_message_type'] ) ) {
|
||||
if ( $block['attrs']['um_message_type'] == '0' ) {
|
||||
$block_content = $default_message;
|
||||
} elseif ( $block['attrs']['um_message_type'] == '1' ) {
|
||||
$block_content = $block['attrs']['um_message_content'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '2': {
|
||||
if ( is_user_logged_in() ) {
|
||||
$block_content = '';
|
||||
if ( isset( $block['attrs']['um_message_type'] ) ) {
|
||||
if ( $block['attrs']['um_message_type'] == '0' ) {
|
||||
$block_content = $default_message;
|
||||
} elseif ( $block['attrs']['um_message_type'] == '1' ) {
|
||||
$block_content = $block['attrs']['um_message_content'];
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -133,7 +133,10 @@ The plugin works with popular caching plugins by automatically excluding Ultimat
|
||||
|
||||
= Important: UM2.0+ is a significant update to the code base from 1.3.88. Please make sure you take a full-site backup with restore point before updating the plugin =
|
||||
|
||||
= 2.0.36: January 7, 2019 =
|
||||
= 2.0.36: January 8, 2019 =
|
||||
|
||||
* Enhancements:
|
||||
- Added Block's restriction options
|
||||
|
||||
* Bugfixes:
|
||||
- Fixed Live Preview form at wp-admin
|
||||
|
||||
Reference in New Issue
Block a user