Building on the great work of Zack Pyle over on SnippetNest (a must-subscribe IMO), I have modified the “Edit with Etch” admin bar to move the current content into the dropdown. I did this because I occasionally want to edit on a touch device.

Image 132
<?php

add_action(
    'admin_bar_menu',
    function ( WP_Admin_Bar $admin_bar ) {
        if ( is_admin() || ! current_user_can( 'manage_options' ) ) {
            return;
        }

        if ( ! class_exists( '\Etch\Plugin' ) ) {
            return;
        }

        $post_id = 0;

        if ( is_singular() ) {
            $post_id   = get_queried_object_id();
            $post_type = get_post_type( $post_id );

            if ( ! $post_id || ! $post_type ) {
                return;
            }

            $slug = get_post_field( 'post_name', $post_id );

            $candidates = $post_type === 'page'
                ? [ "page-{$slug}", "page-{$post_id}", 'page', 'index' ]
                : [
                    "single-{$post_type}-{$slug}",
                    "single-{$post_type}",
                    'single',
                    'singular',
                    'index',
                ];

            $original_post_id = $post_id;

        } elseif ( is_post_type_archive() ) {
            $post_type_obj    = get_queried_object();
            $post_type        = $post_type_obj->name;
            $candidates       = [ "archive-{$post_type}", 'archive', 'index' ];
            $original_post_id = 0;

        } elseif ( is_tax() || is_category() || is_tag() ) {
            $term             = get_queried_object();
            $taxonomy         = $term->taxonomy;
            $slug             = $term->slug;
            $original_post_id = $term->term_id;

            $candidates = [
                "taxonomy-{$taxonomy}-{$slug}",
                "taxonomy-{$taxonomy}",
                'taxonomy',
                'archive',
                'index',
            ];

        } elseif ( is_404() ) {
            $candidates       = [ '404', 'index' ];
            $original_post_id = 0;

        } elseif ( is_search() ) {
            $candidates       = [ 'search', 'index' ];
            $original_post_id = 0;

        } else {
            return;
        }

        $matches = get_posts( [
            'post_type'      => 'wp_template',
            'post_name__in'  => $candidates,
            'posts_per_page' => count( $candidates ),
            'post_status'    => 'publish',
        ] );

        if ( empty( $matches ) ) {
            return;
        }

        $slugs_to_ids = array_column( $matches, 'ID', 'post_name' );

        $template_id   = null;
        $template_slug = null;

        foreach ( $candidates as $candidate ) {
            if ( isset( $slugs_to_ids[ $candidate ] ) ) {
                $template_id   = $slugs_to_ids[ $candidate ];
                $template_slug = $candidate;
                break;
            }
        }

        if ( ! $template_id ) {
            return;
        }

        $label = preg_replace(
            '/^(single|page|archive|taxonomy)-/',
            '',
            $template_slug
        );

        $label = ucwords( str_replace( '-', ' ', $label ) );

        if ( str_starts_with( $template_slug, 'archive-' ) ) {
            $label .= ' Archive';
        }

        $parent = $admin_bar->get_node( 'edit-with-etch' );

        $content_edit_url = $parent && ! empty( $parent->href )
            ? $parent->href
            : '';

        $content_label = $post_id
            ? 'Edit ' . get_the_title( $post_id )
            : 'Edit this content';

        $admin_bar->remove_node( 'etch-edit-template' );
        $admin_bar->remove_node( 'etch-edit-content' );
        $admin_bar->remove_node( 'edit-with-etch' );

        $admin_bar->add_node( [
            'id'    => 'edit-with-etch',
            'title' => 'Edit with Etch <span class="dashicons dashicons-arrow-down-alt2" style="font-family:dashicons;font-size:14px;line-height:34px;"></span>',
            'href'  => false,
            'meta'  => [
                'title' => 'Etch editing options',
            ],
        ] );

        if ( $content_edit_url ) {
            $admin_bar->add_node( [
                'id'     => 'etch-edit-content',
                'parent' => 'edit-with-etch',
                'title'  => esc_html( $content_label ),
                'href'   => esc_url( $content_edit_url ),
                'meta'   => [
                    'title' => 'Open this content in the Etch editor',
                ],
            ] );
        }

        $admin_bar->add_node( [
            'id'     => 'etch-edit-template',
            'parent' => 'edit-with-etch',
            'title'  => 'Edit ' . esc_html( $label ) . ' Template',
            'href'   => esc_url(
                add_query_arg(
                    [
                        'etch'             => 'magic',
                        'post_id'          => $template_id,
                        'original_post_id' => $original_post_id,
                    ],
                    home_url( '/' )
                )
            ),
            'meta'   => [
                'title' => 'Open this template in the Etch editor',
            ],
        ] );
    },
    9999
);
add_action( 'wp_head', function() {

    ?>

    <style>

        #wpadminbar #wp-admin-bar-edit-with-etch .ab-submenu .ab-item {
            white-space: normal;
            line-height: 1.3;
            height: auto;
            min-height:1em;
            padding-top: 6px;
            padding-bottom: 6px;
            width:200px;
            max-width:20ch;
        }

    </style>

    <?php

} );