_x( 'Affiliates Dashboard Registration', 'block title', 'affiliates' ), 'description' => _x( 'Displays the Registration form from the Affiliates Dashboard', 'block description', 'affiliates' ), 'keyword_affiliates' => __( 'Affiliates', 'affiliates' ), 'keyword_dashboard' => __( 'Dashboard', 'affiliates' ), 'keyword_registration' => __( 'Registration', 'affiliates' ), 'dashboard_registration_notice' => _x( 'Affiliates Dashboard Registration', 'Notice shown when editing the Affiliates Dashboard Registration block as a non-affiliate.', 'affiliates' ) ) ); // Our editor stylesheet - not required yet. // wp_register_style( // 'affiliates-dashboard-registration-block-editor', // plugins_url( 'css/dashboard-blocks-editor.css', AFFILIATES_FILE ), // array( 'wp-edit-blocks' ), // AFFILIATES_CORE_VERSION // ); } public static function enqueue_block_assets() { // Our front end stylesheet - not required yet. // wp_register_style( // 'affiliates-dashboard-registration-block', // plugins_url( 'css/dashboard-blocks.css', AFFILIATES_FILE ), // array(), // AFFILIATES_CORE_VERSION // ); } /** * Initialization - register the block. */ public static function wp_init() { if ( function_exists( 'register_block_type' ) ) { register_block_type( 'affiliates/dashboard-registration', array( 'editor_script' => 'affiliates-dashboard-registration-block', 'render_callback' => array( __CLASS__, 'block' ) ) ); } } /** * Callback for the section block. * * @param array $atts attributes * @param string $content not used * * @return string */ public static function block( $atts, $content = '' ) { $output = ''; if ( !affiliates_user_is_affiliate( get_current_user_id() ) ) { // Render the registration: /** * @var Affiliates_Dashboard_Registration $section */ $section = Affiliates_Dashboard_Section_Factory::get_section_instance( Affiliates_Dashboard_Registration::get_key() ); ob_start(); $section->render(); $output = ob_get_clean(); } // The following fixes a Gutenberg UX/UI bug : if the callback returns an empty string, you would see a spinner that never goes away. // So we render something other than the empty string, to avoid the spinner being shown eternally. // The form obviously won't be rendered when previewing in the editor because you're logged in. // The REST_REQUEST ... part is trying to recognize it's a request to render the block on the back end. if ( ( strlen( $output ) === 0 ) && defined( 'REST_REQUEST' ) && REST_REQUEST && isset( $_REQUEST['context'] ) && $_REQUEST['context'] === 'edit' ) { $output .= '
'; } return $output; } } Affiliates_Dashboard_Registration_Block::init();