$style ){ $new = ''; foreach( $style as $property => $value ){ if( $value == '' ){ $new .= $property; }else { $new .= " $property: $value;"; } } $output .= "$selector { $new }"; } return $output; } } // Alert Box Block class ABBAlertBoxBlock{ protected static $_instance = null; function __construct(){ add_action( 'enqueue_block_assets', [$this, 'enqueue_block_assets'] ); add_action( 'init', [$this, 'register'] ); } public static function instance(){ if( self::$_instance === null ){ self::$_instance = new self(); } return self::$_instance; } function enqueue_block_assets(){ wp_enqueue_script( 'fontAwesomeKit', ABB_ASSETS_DIR . 'js/font-awesome-kit.js', [], ABB_PLUGIN_VERSION, true ); } function register() { wp_register_script( 'abb_editor_script', plugins_url( 'dist/editor.js', __FILE__ ), [ 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-html-entities', 'wp-i18n', 'wp-rich-text', 'fontAwesomeKit' ], ABB_PLUGIN_VERSION, false ); // Backend Script wp_register_style( 'abb_editor_style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'wp-edit-blocks' ], ABB_PLUGIN_VERSION ); // Backend Style wp_register_script( 'abb_script', plugins_url( 'dist/script.js', __FILE__ ), [ 'jquery', 'fontAwesomeKit' ], ABB_PLUGIN_VERSION, true ); // Frontend Script wp_register_style( 'abb_style', plugins_url( 'dist/style.css', __FILE__ ), [ 'wp-editor' ], ABB_PLUGIN_VERSION ); // Frontend Style register_block_type( 'abb/alert-box', [ 'editor_script' => 'abb_editor_script', 'editor_style' => 'abb_editor_style', 'script' => 'abb_script', 'style' => 'abb_style', 'render_callback' => [$this, 'render'] ] ); // Register Block wp_set_script_translations( 'abb_editor_script', 'alert-box-block', plugin_dir_path( __FILE__ ) . 'languages' ); // Translate } function render( $attributes ){ extract( $attributes ); $align = $align ?? ''; $cId = $cId ?? ''; $type = $type ?? 'general'; $width = $width ?? '100%'; $alignment = $alignment ?? 'center'; $textAlign = $textAlign ?? 'center'; $message = $message ?? 'You have an alert message!'; $icon = $icon ?? [ 'class' => 'fa fa-exclamation-circle' ]; $isDismiss = $isDismiss ?? true; $typography = $typography ?? [ 'fontSize' => 25 ]; $colors = $colors ?? [ 'color' => '#333', 'bg' => '#fff' ]; $padding = $padding ?? [ 'vertical' => '15px', 'horizontal' => '30px' ]; $border = $border ?? [ 'width' => '2px', 'color' => '#333', 'radius' => '8px' ]; $shadow = $shadow ?? []; $alertBoxStyle = new ABBStyleGenerator(); // Generate Styles $alertBoxStyle::addStyle( "#abbAlertBox-$cId .abbAlertBox", [ 'text-align' => $alignment ] ); $alertBoxStyle::addStyle( "#abbAlertBox-$cId .abbAlertBox .alert", [ 'text-align' => $textAlign, 'width' => '0px' === $width || '0%' === $width || '0em' === $width ? 'auto' : $width, $typography['styles'] ?? 'font-size: 25px;' => '', $colors['styles'] ?? 'color: #333; background: #fff;' => '', 'padding' => $padding['styles'] ?? '15px 30px', $border['styles'] ?? 'border: 2px solid #333; border-radius: 8px;' => '', 'box-shadow' => $shadow['styles'] ?? 'none' ] ); $alertBoxStyle::addStyle( "#abbAlertBox-$cId .abbAlertBox .alert .alertIcon", [ 'font-size' => $typography['fontSize'] ?? '25px', 'color' => $colors['color'] ?? '#333' ] ); $alertBoxStyle::addStyle( "#abbAlertBox-$cId .abbAlertBox .alert .alertClose", [ 'color' => $colors['color'] ?? '#333' ] ); ob_start(); ?>