repository = $repository; $this->builtins = $builtins; } public function register() { add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); add_action( 'add_meta_boxes', array( $this, 'meta_boxes' ), 10, 2 ); } public function assets( $hook ) { if ( '1' !== get_option( TitleShortcodes::OPTION, '0' ) || ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { return; } $screen = get_current_screen(); if ( ! $screen || ! $screen->post_type || ! current_user_can( 'edit_posts' ) ) { return; } $block_editor = $this->uses_block_editor( $screen->post_type, get_post() ); $dependencies = $block_editor ? array( 'wp-components', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-plugins' ) : array(); wp_enqueue_script( 'glovario-title-inserter', GLOVARIO_URL . 'assets/title-inserter.js', $dependencies, GLOVARIO_VERSION, true ); wp_localize_script( 'glovario-title-inserter', 'glovarioTitleInserter', array( 'panelTitle' => __( 'Site Variable in title', 'glovario' ), 'select' => __( 'Variable', 'glovario' ), 'choose' => __( 'Select a variable', 'glovario' ), 'insert' => __( 'Insert into title', 'glovario' ), 'help' => __( 'The editor keeps the shortcode visible. The current variable value appears on the frontend.', 'glovario' ), 'variables' => $this->variables(), // "Make variable" opens the shared dialog, which only administrators can use. 'canCreate' => current_user_can( 'manage_options' ), 'make' => __( 'Make variable from title', 'glovario' ), 'makeHelp' => __( 'Uses the part of the title you selected, or the whole title when nothing is selected, and replaces it with the new variable.', 'glovario' ), ) ); } /** * Whether this post opens in the block editor. The Classic Editor plugin can * decide per post (when users may switch editors), so the post wins over its type. * * @param string $post_type Post type. * @param \WP_Post|null $post Post being edited, if known. * @return bool */ private function uses_block_editor( $post_type, $post = null ) { return $post instanceof \WP_Post ? use_block_editor_for_post( $post ) : use_block_editor_for_post_type( $post_type ); } public function meta_boxes( $post_type, $post = null ) { if ( '1' !== get_option( TitleShortcodes::OPTION, '0' ) || $this->uses_block_editor( $post_type, $post ) ) { return; } add_meta_box( 'glovario-title-inserter', __( 'Site Variable in title', 'glovario' ), array( $this, 'meta_box' ), $post_type, 'side', 'default' ); } public function meta_box() { $variables = $this->variables(); ?>
repository->all( array( 'number' => 500 ) ) as $variable ) { $variables[] = array( 'key' => $variable['variable_key'], 'label' => $variable['label'], ); } // Built-ins (site name, current year, …) after the site's own, as in the block search. foreach ( $this->builtins ? $this->builtins->all() : array() as $variable ) { $variables[] = array( 'key' => $variable['variable_key'], 'label' => $variable['category_name'] . ' — ' . $variable['label'], ); } return $variables; } }