mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-07-17 13:43:38 +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',
|
||||
|
||||
Reference in New Issue
Block a user