| 1 |
const { Component } = wp.element; |
| 2 |
const { applyFilters } = wp.hooks; |
| 3 |
|
| 4 |
import getCurrentUserData from './../data-providers/currentUserData'; |
| 5 |
|
| 6 |
/** |
| 7 |
* A wrapper that contains user data for making decisions when rendering block setting controls. |
| 8 |
*/ |
| 9 |
export default class RenderSettingControl extends Component { |
| 10 |
render() { |
| 11 |
if ('undefined' === typeof this.props.children) { |
| 12 |
return null; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Let the temporary hacks begin. |
| 17 |
* Get the block name this setting is associated with. |
| 18 |
*/ |
| 19 |
let fallback = false; |
| 20 |
|
| 21 |
if ( |
| 22 |
'undefined' === typeof this.props.children.props || |
| 23 |
'undefined' === typeof this.props.children.props.name |
| 24 |
) { |
| 25 |
fallback = true; |
| 26 |
} |
| 27 |
|
| 28 |
if ( |
| 29 |
fallback && |
| 30 |
('undefined' === typeof this.props.children._owner || |
| 31 |
'undefined' === |
| 32 |
typeof this.props.children._owner.memoizedProps || |
| 33 |
'undefined' === |
| 34 |
typeof this.props.children._owner.memoizedProps.name) |
| 35 |
) { |
| 36 |
return this.props.children; |
| 37 |
} |
| 38 |
|
| 39 |
const blockName = fallback |
| 40 |
? this.props.children._owner.memoizedProps.name |
| 41 |
: this.props.children.props.name; |
| 42 |
|
| 43 |
/** |
| 44 |
* A filter for determining whether or not a setting should be rendered. |
| 45 |
* |
| 46 |
* @param {boolean} Whether or not the setting control should be rendered. Default true. |
| 47 |
* @param {string} The block name. |
| 48 |
* @param {string} The setting control's ID. |
| 49 |
* @param {Object} The current user's data. |
| 50 |
*/ |
| 51 |
if ( |
| 52 |
applyFilters( |
| 53 |
'gb_should_render_block_setting', |
| 54 |
true, |
| 55 |
blockName, |
| 56 |
this.props.id, |
| 57 |
getCurrentUserData() |
| 58 |
) |
| 59 |
) { |
| 60 |
return this.props.children; |
| 61 |
} |
| 62 |
|
| 63 |
return null; |
| 64 |
} |
| 65 |
} |
| 66 |
|