diff --git a/includes/admin/assets/js/um-admin-blocks.js b/includes/admin/assets/js/um-admin-blocks.js index d7b16826..2f3ec381 100644 --- a/includes/admin/assets/js/um-admin-blocks.js +++ b/includes/admin/assets/js/um-admin-blocks.js @@ -1,7 +1,7 @@ 'use strict'; -var um_components = wp.components, - umSelectControl = um_components.SelectControl, +var um_components = wp.components, + umSelectControl = um_components.SelectControl, umTextareaControl = um_components.TextareaControl; @@ -9,228 +9,189 @@ function um_admin_blocks_custom_fields( um_condition_fields, props ) { return wp.hooks.applyFilters( 'um_admin_blocks_custom_fields', [], um_condition_fields, props ); } -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' - }; +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' + }; - um_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields_default', um_condition_fields ); + um_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields_default', um_condition_fields ); - return function( props ) { - let initialIsRestrict = props.attributes.um_is_restrict !== undefined ? props.attributes.um_is_restrict : false; + return function( props ) { + let initialIsRestrict = props.attributes.um_is_restrict !== undefined ? props.attributes.um_is_restrict : false; - 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'; + 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 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 ) === 2 ) { - um_condition_fields['um_message_content'] = ''; - } else { - um_condition_fields['um_message_content'] = 'um_block_settings_hide'; - } } else { - um_condition_fields['um_message_type'] = ''; + um_condition_fields['um_who_access'] = ''; - if ( parseInt( props.attributes.um_message_type ) === 2 ) { - um_condition_fields['um_message_content'] = ''; - } else { + 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 ) === 2 ) { + um_condition_fields['um_message_content'] = ''; + } else { + um_condition_fields['um_message_content'] = 'um_block_settings_hide'; + } + } else { + um_condition_fields['um_message_type'] = ''; + + if ( parseInt( props.attributes.um_message_type ) === 2 ) { + um_condition_fields['um_message_content'] = ''; + } else { + um_condition_fields['um_message_content'] = 'um_block_settings_hide'; + } } } - } - um_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields', um_condition_fields, props ); + um_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields', um_condition_fields, props ); - return wp.element.createElement( - wp.element.Fragment, - {}, - wp.element.createElement( BlockEdit, props ), - wp.element.createElement( - wp.blockEditor.InspectorControls, + return wp.element.createElement( + wp.element.Fragment, {}, + wp.element.createElement( BlockEdit, props ), wp.element.createElement( - wp.components.PanelBody, - { - title: wp.i18n.__( 'Ultimate Member: Content Restriction', 'ultimate-member' ), - className: 'um_block_settings' - }, + wp.blockEditor.InspectorControls, + {}, wp.element.createElement( - wp.components.ToggleControl, + wp.components.PanelBody, { - label: wp.i18n.__( 'Restrict access?', 'ultimate-member' ), - checked: initialIsRestrict, - 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'] = ''; - } + title: wp.i18n.__( 'Ultimate Member: Content Restriction', 'ultimate-member' ), + className: 'um_block_settings' + }, + wp.element.createElement( + wp.components.ToggleControl, + { + label: wp.i18n.__( 'Restrict access?', 'ultimate-member' ), + checked: initialIsRestrict, + 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_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields_on_change', um_condition_fields, 'um_is_restrict', value ); + um_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields_on_change', um_condition_fields, 'um_is_restrict', value ); + } } - } - ), - wp.element.createElement( - umSelectControl, - { - type: 'number', - className: um_condition_fields['um_who_access'], - label: wp.i18n.__( 'Who can access this block?', 'ultimate-member' ), - value: props.attributes.um_who_access, - options: [ - { - label: wp.i18n.__( 'Everyone', 'ultimate-member' ), - value: 0 - }, - { - label: wp.i18n.__( 'Logged in users', 'ultimate-member' ), - value: 1 - }, - { - label: wp.i18n.__( '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'; - } + ), + wp.element.createElement( + umSelectControl, + { + type: 'number', + className: um_condition_fields['um_who_access'], + label: wp.i18n.__( 'Who can access this block?', 'ultimate-member' ), + value: props.attributes.um_who_access, + options: [ + { + label: wp.i18n.__( 'Everyone', 'ultimate-member' ), + value: 0 + }, + { + label: wp.i18n.__( 'Logged in users', 'ultimate-member' ), + value: 1 + }, + { + label: wp.i18n.__( '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_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields_on_change', um_condition_fields, 'um_who_access', value ); - } - } - ), - wp.element.createElement( - umSelectControl, - { - multiple: true, - className: um_condition_fields['um_roles_access'], - label: wp.i18n.__( 'What roles can access this block?', 'ultimate-member' ), - value: props.attributes.um_roles_access, - options: um_restrict_roles, - onChange: function onChange( value ) { - props.setAttributes({ um_roles_access: value }); - } - } - ), - wp.element.createElement( - umSelectControl, - { - type: 'number', - className: um_condition_fields['um_message_type'], - label: wp.i18n.__( 'Restriction action', 'ultimate-member' ), - value: props.attributes.um_message_type, - options: [ - { - label: wp.i18n.__( 'Hide block', 'ultimate-member' ), - value: 0 - }, - { - label: wp.i18n.__( 'Show global default message', 'ultimate-member' ), - value: 1 - }, - { - label: wp.i18n.__( 'Show custom message', 'ultimate-member' ), - value: 2 - } - ], - onChange: function onChange( value ) { - props.setAttributes({ um_message_type: value }); - if ( parseInt( value ) === 2 ) { - um_condition_fields['um_message_content'] = ''; - } else { - um_condition_fields['um_message_content'] = 'um_block_settings_hide'; + um_condition_fields = wp.hooks.applyFilters( 'um_admin_blocks_condition_fields_on_change', um_condition_fields, 'um_who_access', value ); } } - } - ), - wp.element.createElement( - umTextareaControl, - { - type: 'number', - className: um_condition_fields['um_message_content'], - label: wp.i18n.__( 'Custom restricted access message', 'ultimate-member' ), - value: props.attributes.um_message_content, - onChange: function onChange( value ) { - props.setAttributes({ um_message_content: value }); + ), + wp.element.createElement( + umSelectControl, + { + multiple: true, + className: um_condition_fields['um_roles_access'], + label: wp.i18n.__( 'What roles can access this block?', 'ultimate-member' ), + value: props.attributes.um_roles_access, + options: um_restrict_roles, + onChange: function onChange( value ) { + props.setAttributes( { um_roles_access: value } ); + } } - } - ), - um_admin_blocks_custom_fields( um_condition_fields, props ) + ), + wp.element.createElement( + umSelectControl, + { + type: 'number', + className: um_condition_fields['um_message_type'], + label: wp.i18n.__( 'Restriction action', 'ultimate-member' ), + value: props.attributes.um_message_type, + options: [ + { + label: wp.i18n.__( 'Hide block', 'ultimate-member' ), + value: 0 + }, + { + label: wp.i18n.__( 'Show global default message', 'ultimate-member' ), + value: 1 + }, + { + label: wp.i18n.__( 'Show custom message', 'ultimate-member' ), + value: 2 + } + ], + onChange: function onChange( value ) { + props.setAttributes( { um_message_type: value } ); + if ( parseInt( value ) === 2 ) { + um_condition_fields['um_message_content'] = ''; + } else { + um_condition_fields['um_message_content'] = 'um_block_settings_hide'; + } + } + } + ), + wp.element.createElement( + umTextareaControl, + { + type: 'number', + className: um_condition_fields['um_message_content'], + label: wp.i18n.__( 'Custom restricted access message', 'ultimate-member' ), + value: props.attributes.um_message_content, + onChange: function onChange( value ) { + props.setAttributes( { um_message_content: value } ); + } + } + ), + um_admin_blocks_custom_fields( um_condition_fields, props ) + ) ) - ) - ); - }; -}, 'um_block_restriction' ); + ); + }; + }, + '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" -// } -// }; -// -// um_block_restrict_settings = wp.hooks.applyFilters( 'um_admin_blocks_restrict_settings', um_block_restrict_settings ); - - -/** - * - * @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 ); diff --git a/includes/admin/core/class-admin-enqueue.php b/includes/admin/core/class-admin-enqueue.php index 65e7badc..75ed7b5d 100644 --- a/includes/admin/core/class-admin-enqueue.php +++ b/includes/admin/core/class-admin-enqueue.php @@ -572,7 +572,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Enqueue' ) ) { wp_set_script_translations( 'um_block_js', 'ultimate-member' ); $restrict_options = array(); - $roles = UM()->roles()->get_roles( false ); + $roles = UM()->roles()->get_roles( false ); if ( ! empty( $roles ) ) { foreach ( $roles as $role_key => $title ) { $restrict_options[] = array( diff --git a/includes/core/class-blocks.php b/includes/core/class-blocks.php index 1522fbcd..201b2bb8 100644 --- a/includes/core/class-blocks.php +++ b/includes/core/class-blocks.php @@ -21,38 +21,38 @@ if ( ! class_exists( 'um\core\Blocks' ) ) { */ public function __construct() { add_action( 'init', array( &$this, 'block_editor_render' ), 10 ); - add_filter( 'block_type_metadata', array( &$this, 'block_type_metadata' ), 11, 1 ); + add_filter( 'block_type_metadata_settings', array( &$this, 'block_type_metadata_settings' ), 10, 2 ); } - public function block_type_metadata( $metadata ) { - if ( empty( $metadata['attributes']['um_is_restrict'] ) ) { - $metadata['attributes']['um_is_restrict'] = array( + public function block_type_metadata_settings( $settings, $args ) { + if ( empty( $settings['attributes']['um_is_restrict'] ) ) { + $settings['attributes']['um_is_restrict'] = array( 'type' => 'boolean', ); } - if ( empty( $metadata['attributes']['um_who_access'] ) ) { - $metadata['attributes']['um_who_access'] = array( + if ( empty( $settings['attributes']['um_who_access'] ) ) { + $settings['attributes']['um_who_access'] = array( 'type' => 'string', ); } - if ( empty( $metadata['attributes']['um_roles_access'] ) ) { - $metadata['attributes']['um_roles_access'] = array( + if ( empty( $settings['attributes']['um_roles_access'] ) ) { + $settings['attributes']['um_roles_access'] = array( 'type' => 'array', ); } - if ( empty( $metadata['attributes']['um_message_type'] ) ) { - $metadata['attributes']['um_message_type'] = array( + if ( empty( $settings['attributes']['um_message_type'] ) ) { + $settings['attributes']['um_message_type'] = array( 'type' => 'string', ); } - if ( empty( $metadata['attributes']['um_message_content'] ) ) { - $metadata['attributes']['um_message_content'] = array( + if ( empty( $settings['attributes']['um_message_content'] ) ) { + $settings['attributes']['um_message_content'] = array( 'type' => 'string', ); } - return $metadata; + return $settings; }