visibility = $visibility; $this->registry = $registry; $this->settings = $settings; } /** * Registers hooks. * * @return void */ public function register(): void { add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor' ) ); add_action( 'enqueue_block_assets', array( $this, 'enqueue_canvas' ) ); } /** * Loads the editor script and panel styles, with the panel's data. * * @return void */ public function enqueue_editor(): void { $asset_file = YANGRA_CONDITIONAL_PATH . 'build/editor/index.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $asset = require $asset_file; wp_enqueue_script( self::HANDLE, YANGRA_CONDITIONAL_URL . 'build/editor/index.js', $asset['dependencies'], $asset['version'], true ); wp_add_inline_script( self::HANDLE, 'window.yangraConditionalEditor = ' . wp_json_encode( $this->data() ) . ';', 'before' ); wp_set_script_translations( self::HANDLE, 'yangra-conditional', YANGRA_CONDITIONAL_PATH . 'languages' ); wp_enqueue_style( self::HANDLE, YANGRA_CONDITIONAL_URL . 'build/editor/index.css', array( 'wp-components' ), $asset['version'] ); } /** * Loads the badge styles into the editor canvas (an iframe), when badges are on. * `enqueue_block_assets` also runs on the front end, hence the is_admin() check. * * @return void */ public function enqueue_canvas(): void { if ( ! is_admin() || empty( $this->settings->all()['show_visibility_badges'] ) ) { return; } $asset_file = YANGRA_CONDITIONAL_PATH . 'build/editor/canvas.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $asset = require $asset_file; wp_enqueue_style( self::HANDLE . '-canvas', YANGRA_CONDITIONAL_URL . 'build/editor/canvas.css', array(), $asset['version'] ); } /** * Data for the editor script: the conditions this build can check, the Pro * condition names (for "Requires Pro, skipped"), the blocks that can't have a * rule, whether to show badges, and the site's timezone for the Schedule * section. * * @return array{excludedBlockTypes: array, conditions: array>, proTypes: array, showBadges: bool, timezone: string} */ public function data(): array { $conditions = array(); foreach ( $this->registry->all() as $type => $condition ) { $options = array(); foreach ( $condition->options() as $value => $label ) { $options[] = array( 'value' => (string) $value, 'label' => $label, ); } $definition = array( 'type' => (string) $type, 'label' => $condition->label(), 'multiple' => $condition->is_multiple(), 'searchable' => $condition instanceof SearchableCondition, 'options' => $options, 'notices' => ConditionNotices::for_type( (string) $type ), ); if ( $condition instanceof SearchableCondition ) { $picker = $condition->picker(); $definition['picker'] = array( 'label' => $picker['label'], 'placeholder' => $picker['placeholder'], 'savedById' => $picker['saved_by_id'], ); } $conditions[] = $definition; } $pro_types = array(); foreach ( ConditionRegistry::PRO_TYPES as $type ) { $pro_types[ $type ] = $this->registry->pro_type_label( $type ); } return array( 'excludedBlockTypes' => $this->visibility->excluded_block_types(), 'conditions' => $conditions, 'proTypes' => $pro_types, 'showBadges' => ! empty( $this->settings->all()['show_visibility_badges'] ), 'timezone' => ScheduledElements::timezone_label(), ); } }