Making WordPress.org

Changeset 6070


Ignore:
Timestamp:
11/01/2017 10:04:32 PM (8 years ago)
Author:
iandunn
Message:

Gutenberg Tweaks: Make the "classic" editor the default for now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/gutenberg-tweaks.php

    r6069 r6070  
    55defined( 'WPINC' ) || die();
    66
     7/*
     8 * Customize Gutenberg for WordCamp.org
     9 *
     10 *   WARNING: This is tied to Gutenberg's hooks, which are likely to evolve quickly. This needs to
     11 *   be tested and updated before pushing Gutenberg updates to production.
     12 *
     13 * Most of this deals with preventing Gutenberg from being the default editor for now.
     14 *
     15 * We want the beta plugin to be available for all organizers to use on an opt-in basis, but don't
     16 * want to aggressively push them to use it if they don't want to.
     17 *
     18 * @see https://make.wordpress.org/community/2017/09/25/gutenberg-on-wordcamp-sites/#comment-24596
     19 *
     20 * @todo: Most of this can be removed once Gutenberg is refined enough to become the default editor
     21 *        for organizers.
     22 */
     23
    724add_filter( 'gutenberg_can_edit_post_type', __NAMESPACE__ . '\disable_gutenberg_on_cpts',           10, 2 );
     25add_filter( 'get_edit_post_link',           __NAMESPACE__ . '\add_classic_param_to_edit_links'            );
     26add_filter( 'page_row_actions',             __NAMESPACE__ . '\add_gutenberg_edit_link',             10, 2 );
     27add_filter( 'post_row_actions',             __NAMESPACE__ . '\add_gutenberg_edit_link',             10, 2 );
     28add_action( 'admin_print_scripts-edit.php', __NAMESPACE__ . '\add_classic_param_to_add_new_button', 11    );
     29add_action( 'admin_print_scripts',          __NAMESPACE__ . '\add_classic_param_to_admin_menus',     1    );
    830
    931
     
    1941    return in_array( $post_type, array( 'post', 'page' ), true );
    2042}
     43
     44// Add the `classic-editor` trigger to all edit post URLs
     45function add_classic_param_to_edit_links( $url ) {
     46    return add_query_arg( 'classic-editor', '', $url );
     47}
     48
     49// Add a "Gutenberg Editor" link to the post hover actions on the "All {Posts|Pages}" screen.
     50function add_gutenberg_edit_link( $actions, $post ) {
     51    if ( ! gutenberg_can_edit_post( $post ) ) {
     52        return $actions;
     53    }
     54
     55    remove_filter( 'get_edit_post_link', __NAMESPACE__ . '\add_classic_param_to_edit_links', 10 );
     56    $edit_url = get_edit_post_link( $post->ID, 'raw' );
     57    add_filter( 'get_edit_post_link', __NAMESPACE__ . '\add_classic_param_to_edit_links', 10 );
     58
     59    $title = _draft_or_post_title( $post->ID );
     60    $edit_action = array(
     61        'gutenberg' => sprintf(
     62            '<a href="%s" aria-label="%s">%s</a>',
     63            esc_url( $edit_url ),
     64            esc_attr( sprintf( 'Edit &#8220;%s&#8221; in the Gutenberg editor', $title ) ),
     65            'Gutenberg Editor'
     66        ),
     67    );
     68
     69    // Insert the Gutenberg Edit action after the Edit action.
     70    $edit_offset = array_search( 'edit', array_keys( $actions ), true );
     71    $actions     = array_merge(
     72        array_slice( $actions, 0, $edit_offset + 1 ),
     73        $edit_action,
     74        array_slice( $actions, $edit_offset + 1 )
     75    );
     76
     77    return $actions;
     78}
     79
     80// Add the `classic-editor` trigger to the modified Add New button on the "All {Posts|Pages" screen.
     81function add_classic_param_to_add_new_button() {
     82    ?>
     83
     84    <script type="text/javascript">
     85        document.addEventListener( 'DOMContentLoaded', function() {
     86            jQuery( '.split-page-title-action' ).children( 'a' ).each( function( index, element ) {
     87                jQuery( element ).attr( 'href', jQuery( element ).attr( 'href' ) + 'classic-editor' );
     88            } );
     89        } );
     90    </script>
     91
     92    <?php
     93}
     94
     95// Add the `classic-editor` trigger to post/page links in the admin menus
     96// This is an ugly hack, but I'm not seeing a better way to do it, and this'll be thrown out soon anyway.
     97function add_classic_param_to_admin_menus() {
     98    ?>
     99
     100    <script>
     101        document.addEventListener( 'DOMContentLoaded', function() {
     102            var adminBarNewContentLink = $( '#wp-admin-bar-new-content' ).children( 'a' ),
     103                adminBarNewPostLink    = $( '#wp-admin-bar-new-post'    ).children( 'a' ),
     104                adminBarNewPageLink    = $( '#wp-admin-bar-new-page'    ).children( 'a' ),
     105                adminMenuNewPostLink   = $( '#menu-posts'    ).children( '.wp-submenu' ).children( 'li' ).children( 'a[href="post-new.php"]' ),
     106                adminMenuNewPageLink   = $( '#menu-pages'    ).children( '.wp-submenu' ).children( 'li' ).children( 'a[href="post-new.php?post_type=page"]' );
     107
     108            // Admin Bar
     109            $( adminBarNewContentLink ).attr( 'href', jQuery( adminBarNewContentLink ).attr( 'href' ) + '?classic-editor' );
     110            $( adminBarNewPostLink ).attr(    'href', jQuery( adminBarNewPostLink    ).attr( 'href' ) + '?classic-editor' );
     111            $( adminBarNewPageLink ).attr(    'href', jQuery( adminBarNewPageLink    ).attr( 'href' ) + '&classic-editor' );
     112
     113            // Admin sidebar menu
     114            $( adminMenuNewPostLink ).attr( 'href', jQuery( adminMenuNewPostLink ).attr( 'href' ) + '?classic-editor' );
     115            $( adminMenuNewPageLink ).attr( 'href', jQuery( adminMenuNewPageLink ).attr( 'href' ) + '&classic-editor' );
     116        } );
     117    </script>
     118
     119    <?php
     120}
Note: See TracChangeset for help on using the changeset viewer.