profile-builder
/
assets
/
misc
/
gutenberg
/
block-content-restriction
/
block-content-restriction.php
block-content-restriction.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.5, at assets/misc/gutenberg/block-content-restriction/block-content-restriction.php
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | function wppb_render_blocks( $block_content, $block ) { |
| 6 | $block_attrs = isset( $block['attrs']['wppbContentRestriction'] ) ? $block['attrs']['wppbContentRestriction'] : null; |
| 7 | |
| 8 | // Abort if: |
| 9 | // the block does not have the content restriction settings attribute or |
| 10 | // the block is to be displayed to all users or |
| 11 | // the current block is the Content Restriction Start block |
| 12 | if ( !isset( $block_attrs ) || $block_attrs['display_to'] === 'all' || $block['blockName'] === 'wppb/content-restriction-start' ) { |
| 13 | return $block_content; |
| 14 | } |
| 15 | |
| 16 | // Map the block content restriction settings to the wppb-restrict shortcode parameters |
| 17 | $atts = array( |
| 18 | 'user_roles' => is_array( $block_attrs ) && array_key_exists( 'user_roles', $block_attrs ) ? implode( ',', $block_attrs['user_roles'] ) : '', |
| 19 | 'display_to' => $block_attrs['display_to'], |
| 20 | 'message' => $block_attrs['display_to'] === 'not_logged_in' |
| 21 | ? ( $block_attrs['enable_message_logged_out'] ? $block_attrs['message_logged_out'] : '' ) |
| 22 | : ( $block_attrs['enable_message_logged_in'] ? $block_attrs['message_logged_in'] : '' ), |
| 23 | 'users_id' => $block_attrs['users_ids'], |
| 24 | ); |
| 25 | |
| 26 | return '<div>'.wppb_content_restriction_shortcode( $atts, $block_content ).'</div>'; |
| 27 | } |
| 28 | add_filter( 'render_block', 'wppb_render_blocks', 10, 2 ); |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Adds the `wppbContentRestriction` attribute to all blocks |
| 33 | */ |
| 34 | add_action( 'wp_loaded', 'wppb_add_custom_attributes_to_blocks', 199 ); |
| 35 | function wppb_add_custom_attributes_to_blocks() { |
| 36 | |
| 37 | $registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered(); |
| 38 | |
| 39 | foreach( $registered_blocks as $name => $block ) { |
| 40 | |
| 41 | $block->attributes['wppbContentRestriction'] = array( |
| 42 | 'type' => 'object', |
| 43 | 'properties' => array( |
| 44 | 'user_roles' => array( |
| 45 | 'type' => 'array', |
| 46 | ), |
| 47 | 'users_ids' => array( |
| 48 | 'type' => 'string', |
| 49 | ), |
| 50 | 'display_to' => array( |
| 51 | 'type' => 'string', |
| 52 | ), |
| 53 | 'enable_message_logged_in' => array( |
| 54 | 'type' => 'boolean', |
| 55 | ), |
| 56 | 'enable_message_logged_out' => array( |
| 57 | 'type' => 'boolean', |
| 58 | ), |
| 59 | 'message_logged_in' => array( |
| 60 | 'type' => 'string', |
| 61 | ), |
| 62 | 'message_logged_out' => array( |
| 63 | 'type' => 'string', |
| 64 | ), |
| 65 | |
| 66 | 'panel_open' => array( |
| 67 | 'type' => 'boolean', |
| 68 | ), |
| 69 | ), |
| 70 | 'default' => array( |
| 71 | 'user_roles' => array(), |
| 72 | 'users_ids' => '', |
| 73 | 'display_to' => 'all', |
| 74 | 'enable_message_logged_in' => false, |
| 75 | 'enable_message_logged_out' => false, |
| 76 | 'message_logged_in' => '', |
| 77 | 'message_logged_out' => '', |
| 78 | 'panel_open' => false, |
| 79 | ), |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 |