| @@ -90,9 +90,9 @@ | ||
| 90 | 90 | |
| 91 | 91 | wp_enqueue_script( |
| 92 | 92 | self::SCRIPT_HANDLE, |
| 93 | 93 | plugins_url( 'dist/admin-bar.js', PLUGIN_FILE ), |
| 94 | - [], | |
| 94 | + [ 'wp-i18n', 'wp-url' ], | |
| 95 | 95 | PLUGIN_VERSION, |
| 96 | 96 | [ 'in_footer' => true ] |
| 97 | 97 | ); |
| 98 | 98 | |
| @@ -126,9 +126,9 @@ | ||
| 126 | 126 | return; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // Always show safe mode indicator regardless of setting. |
| 130 | - $this->add_safe_mode_nodes( $wp_admin_bar, code_snippets()->evaluate_functions->is_safe_mode_active() ); | |
| 130 | + $this->add_safe_mode_nodes( $wp_admin_bar ); | |
| 131 | 131 | |
| 132 | 132 | // Check if admin bar menu is enabled via settings. |
| 133 | 133 | $is_enabled = get_setting( 'general', 'enable_admin_bar' ); |
| 134 | 134 | |
| @@ -154,17 +154,56 @@ | ||
| 154 | 154 | $this->add_safe_mode_link( $wp_admin_bar ); |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
| 158 | + * Render a small color badge for a snippet's execution kind, matching the | |
| 159 | + * type-badge treatment on the snippets list. | |
| 160 | + * | |
| 161 | + * @param string $kind Execution kind: 'php', 'content', 'css', 'js'. | |
| 162 | + * | |
| 163 | + * @return string Safe HTML. | |
| 164 | + */ | |
| 165 | + private function format_kind_badge( string $kind ): string { | |
| 166 | + $kind = 'content' === $kind ? 'html' : $kind; | |
| 167 | + $labels = [ | |
| 168 | + 'php' => 'PHP', | |
| 169 | + 'html' => 'HTML', | |
| 170 | + 'css' => 'CSS', | |
| 171 | + 'js' => 'JS', | |
| 172 | + 'cond' => 'COND', | |
| 173 | + ]; | |
| 174 | + $colours = [ | |
| 175 | + 'php' => '#2271b1', | |
| 176 | + 'html' => '#cd4510', | |
| 177 | + 'css' => '#9b59b6', | |
| 178 | + 'js' => '#f7d67a', | |
| 179 | + 'cond' => '#22826f', | |
| 180 | + ]; | |
| 181 | + $text_colours = [ | |
| 182 | + 'js' => '#1c1f20', | |
| 183 | + ]; | |
| 184 | + | |
| 185 | + $label = $labels[ $kind ] ?? strtoupper( $kind ); | |
| 186 | + $colour = $colours[ $kind ] ?? '#5b7290'; | |
| 187 | + $text_colour = $text_colours[ $kind ] ?? '#fff'; | |
| 188 | + | |
| 189 | + return sprintf( | |
| 190 | + '<span class="code-snippets-kind-badge" style="background:%1$s;color:%2$s">%3$s</span> ', | |
| 191 | + esc_attr( $colour ), | |
| 192 | + esc_attr( $text_colour ), | |
| 193 | + esc_html( $label ) | |
| 194 | + ); | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 158 | 198 | * Add menu item for safe mode status. |
| 159 | 199 | * |
| 160 | - * @param WP_Admin_Bar $wp_admin_bar Admin bar instance. | |
| 161 | - * @param bool $is_safe_mode_active Whether safe mode is active. | |
| 200 | + * @param WP_Admin_Bar $wp_admin_bar Admin bar instance. | |
| 162 | 201 | * |
| 163 | 202 | * @return void |
| 164 | 203 | */ |
| 165 | - private function add_safe_mode_nodes( WP_Admin_Bar $wp_admin_bar, bool $is_safe_mode_active ): void { | |
| 166 | - if ( ! $is_safe_mode_active ) { | |
| 204 | + private function add_safe_mode_nodes( WP_Admin_Bar $wp_admin_bar ): void { | |
| 205 | + if ( ! Evaluate_Functions::is_safe_mode_active() ) { | |
| 167 | 206 | return; |
| 168 | 207 | } |
| 169 | 208 | |
| 170 | 209 | $wp_admin_bar->add_node( |
| @@ -287,8 +326,17 @@ | ||
| 287 | 326 | } |
| 288 | 327 | |
| 289 | 328 | $wp_admin_bar->add_node( |
| 290 | 329 | [ |
| 330 | + 'id' => self::ROOT_NODE_ID . '-insights', | |
| 331 | + 'title' => esc_html_x( 'Insights', 'snippets', 'code-snippets' ), | |
| 332 | + 'href' => $plugin->get_menu_url( 'insights' ), | |
| 333 | + 'parent' => self::ROOT_NODE_ID, | |
| 334 | + ] | |
| 335 | + ); | |
| 336 | + | |
| 337 | + $wp_admin_bar->add_node( | |
| 338 | + [ | |
| 291 | 339 | 'id' => self::ROOT_NODE_ID . '-import', |
| 292 | 340 | 'title' => esc_html_x( 'Import', 'snippets', 'code-snippets' ), |
| 293 | 341 | 'href' => $plugin->get_menu_url( 'import' ), |
| 294 | 342 | 'parent' => self::ROOT_NODE_ID, |
| @@ -401,9 +449,9 @@ | ||
| 401 | 449 | foreach ( $active_page_snippets as $snippet ) { |
| 402 | 450 | $wp_admin_bar->add_node( |
| 403 | 451 | [ |
| 404 | 452 | 'id' => self::ROOT_NODE_ID . '-snippet-' . $snippet->id, |
| 405 | - 'title' => esc_html( $this->format_snippet_title( $snippet ) ), | |
| 453 | + 'title' => $this->format_snippet_title( $snippet ), | |
| 406 | 454 | 'href' => esc_url( add_query_arg( 'id', $snippet->id, $plugin->get_menu_url( 'edit' ) ) ), |
| 407 | 455 | 'parent' => self::ROOT_NODE_ID . '-active-snippets', |
| 408 | 456 | 'meta' => [ 'class' => 'code-snippets-snippet-item' ], |
| 409 | 457 | ] |
| @@ -434,9 +482,9 @@ | ||
| 434 | 482 | foreach ( $inactive_page_snippets as $snippet ) { |
| 435 | 483 | $wp_admin_bar->add_node( |
| 436 | 484 | [ |
| 437 | 485 | 'id' => self::ROOT_NODE_ID . '-snippet-' . $snippet->id, |
| 438 | - 'title' => esc_html( $this->format_snippet_title( $snippet ) ), | |
| 486 | + 'title' => $this->format_snippet_title( $snippet ), | |
| 439 | 487 | 'href' => esc_url( add_query_arg( 'id', $snippet->id, $plugin->get_menu_url( 'edit' ) ) ), |
| 440 | 488 | 'parent' => self::ROOT_NODE_ID . '-inactive-snippets', |
| 441 | 489 | 'meta' => [ 'class' => 'code-snippets-snippet-item' ], |
| 442 | 490 | ] |
| @@ -448,12 +496,12 @@ | ||
| 448 | 496 | * Build an admin bar snippet title including type prefix. |
| 449 | 497 | * |
| 450 | 498 | * @param Snippet $snippet Snippet object. |
| 451 | 499 | * |
| 452 | - * @return string | |
| 500 | + * @return string Safe HTML. | |
| 453 | 501 | */ |
| 454 | 502 | private function format_snippet_title( Snippet $snippet ): string { |
| 455 | - return sprintf( '(%s) %s', strtoupper( $snippet->type ), $snippet->display_name ); | |
| 503 | + return $this->format_kind_badge( $snippet->type ) . esc_html( $snippet->display_name ); | |
| 456 | 504 | } |
| 457 | 505 | |
| 458 | 506 | /** |
| 459 | 507 | * Retrieve the number of snippets to show per page in the admin bar. |