editor = $editor; $this->shortcode = $shortcode; } /** * Registers hooks. * * @return void */ public function register(): void { add_action( 'admin_menu', array( $this, 'add_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); } /** * The page's address. * * @return string */ public static function url(): string { return admin_url( 'admin.php?page=' . self::SLUG ); } /** * Adds the page. Registered before Settings, so it's listed above it. * * @return void */ public function add_menu(): void { $parent = current_user_can( DashboardPage::CAPABILITY ) ? DashboardPage::SLUG : ''; $this->hook_suffix = (string) add_submenu_page( $parent, __( 'Shortcode builder', 'yangra-conditional' ), __( 'Shortcode builder', 'yangra-conditional' ), self::CAPABILITY, self::SLUG, array( $this, 'render' ) ); if ( '' === $parent && '' !== $this->hook_suffix ) { add_action( 'load-' . $this->hook_suffix, array( $this, 'set_title' ) ); } } /** * Gives the page its title when it has no menu item. WordPress finds a * page's title through its menu item, so it would otherwise be null, which * admin-header.php passes to strip_tags() (deprecated in PHP 8.1+). * * @return void */ public function set_title(): void { global $title; $title = __( 'Shortcode builder', 'yangra-conditional' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- The admin page title WordPress reads. } /** * Renders the React mount point. * * @return void */ public function render(): void { echo '

'; } /** * Data for the app: the conditions that can be used as attributes, the * attribute for each condition type, the Pro condition names and the tags. * * @return array{conditions: array>, attributes: array, proTypes: array, tags: array} */ public function data(): array { $attributes = array(); foreach ( $this->shortcode->attributes() as $attribute => $type ) { if ( ! isset( $attributes[ $type ] ) ) { $attributes[ $type ] = $attribute; } } $editor = $this->editor->data(); $conditions = array_values( array_filter( $editor['conditions'], static function ( array $condition ) use ( $attributes ): bool { return isset( $attributes[ $condition['type'] ] ); } ) ); return array( 'conditions' => $conditions, 'attributes' => $attributes, 'proTypes' => $editor['proTypes'], 'tags' => ConditionalShortcode::TAGS, ); } /** * Enqueues the app on this page only. * * @param string $hook_suffix Current admin page hook suffix. * @return void */ public function enqueue( string $hook_suffix ): void { if ( '' === $this->hook_suffix || $hook_suffix !== $this->hook_suffix ) { return; } AdminApp::enqueue( 'shortcodes' ); wp_add_inline_script( self::HANDLE, 'window.yangraConditionalShortcodes = ' . wp_json_encode( $this->data() ) . ';', 'before' ); } }