| Server IP : 38.190.208.107 / Your IP : 216.73.217.46 Web Server : nginx/1.28.1 System : Linux ht2026040333114 6.1.0-10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.37-1 (2023-07-03) x86_64 User : root ( 0) PHP Version : 8.2.28 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv,eval,assert,passthru,system,exec,shell_exec,popen,proc_open MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/wp.3/wp-content/plugins/better-block-editor/Modules/Visibility/ |
Upload File : |
<?php
/**
* Adds responsive visibility settings to all blocks.
* Standard approach with ResponsiveBlockModuleBase can not be used here
* as visibility='hidden' must work even without any responsive settings
*
* @package BetterBlockEditor
*/
namespace BetterBlockEditor\Modules\Visibility;
use BetterBlockEditor\Base\ManagableModuleInterface;
use BetterBlockEditor\Base\ModuleBase;
use BetterBlockEditor\Core\BlockUtils;
use BetterBlockEditor\Core\CssMediaBreakpoints;
use BetterBlockEditor\Plugin;
defined( 'ABSPATH' ) || exit;
class Module extends ModuleBase implements ManagableModuleInterface {
const MODULE_IDENTIFIER = 'block-responsive-visibility';
const ASSETS_BUILD_PATH = 'editor/blocks/__all__/visibility/';
const PLUGIN_ASSETS_BUILD_PATH = 'editor/plugins/visibility/';
const SETTINGS_ORDER = 990;
const ATTRIBUTES = 'wpbbeVisibility';
public function process_assets(): void {
parent::process_assets();
// in asset bundle mode plugin assets are already registered
if ( Plugin::instance()->is_asset_bundle_mode() ) {
return;
}
$asset_file = require WPBBE_DIST . $this::PLUGIN_ASSETS_BUILD_PATH . 'editor.asset.php';
wp_register_script(
$this->build_script_handle( 'editor-plugin' ),
WPBBE_URL_DIST . $this::PLUGIN_ASSETS_BUILD_PATH . $this::EDITOR_ASSET_KEY . '.js',
$asset_file['dependencies'],
$asset_file['version'],
array(
'strategy' => 'defer',
'in_footer' => true,
)
);
if ( file_exists( WPBBE_DIST . $this::PLUGIN_ASSETS_BUILD_PATH . $this::EDITOR_ASSET_KEY . '.css' ) ) {
wp_register_style(
$this->build_style_handle( 'editor-plugin' ),
WPBBE_URL_DIST . $this::PLUGIN_ASSETS_BUILD_PATH . $this::EDITOR_ASSET_KEY . '.css',
array(),
$asset_file['version']
);
}
add_action(
'enqueue_block_editor_assets',
function () {
$this->enqueue_assets( 'editor-plugin' );
}
);
}
public function setup_hooks(): void {
add_filter( 'render_block', array( $this, 'render' ), 20, 2 );
}
public function render( $block_content, $block ) {
$attributes = isset( $block['attrs'] ) ? $block['attrs'] : null;
if ( ! isset( $attributes[ self::ATTRIBUTES ] ) || $block_content === '' ) {
return $block_content;
}
list( $visibility, $breakpoint ) = $this->get_visibility_settings( $attributes );
// always visible (normal state, visibility is not applied)
if ( $visibility === 'visible' && $breakpoint === CssMediaBreakpoints::BREAKPOINT_NAME_OFF ) {
return $block_content;
}
$class_id = BlockUtils::get_unique_class_id( $block_content );
$block_content = BlockUtils::append_classes( $block_content, $class_id );
$this->add_styles( $attributes, $class_id );
return $block_content;
}
/**
* Helper to get visibility settings from block attributes
*
* @return array [ visibility, breakpoint, breakpointCustomValue ]
*/
private function get_visibility_settings( $attributes ): array {
return
array(
$attributes[ self::ATTRIBUTES ]['visibility'] ?? 'visible', // default to visible if not set
$attributes[ self::ATTRIBUTES ]['breakpoint'] ?? null,
$attributes[ self::ATTRIBUTES ]['breakpointCustomValue'] ?? null,
);
}
private function add_styles( $attributes, $class_id ): void {
list( $visibility, $breakpoint, $breakpoint_value ) = $this->get_visibility_settings( $attributes );
$switch_width = CssMediaBreakpoints::getSwitchWidth( $breakpoint, $breakpoint_value );
if ( null === $switch_width ) {
if ( $visibility === 'hidden' ) {
// if block is always hidden
BlockUtils::add_styles_from_css_rules(
array(
array(
'selector' => ".{$class_id}.{$class_id}",
'declarations' => array( 'display' => 'none !important' ),
),
)
);
}
return;
}
BlockUtils::add_style_for_media_query(
"@media screen and (width ".($visibility === 'visible' ? '<=' : '>=')." {$switch_width})",
".{$class_id}.{$class_id}",
array( 'display' => 'none !important' )
);
}
public static function get_title() {
return __( 'Blocks Visibility', 'better-block-editor' );
}
public static function get_label() {
return __( 'Add Responsive Visibility Settings to all blocks.', 'better-block-editor' );
}
}