WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/29/2020 12:43:23 AM (15 months ago)
Author:
SergeyBiryukov
Message:

Docs: Improve inline comments per the documentation standards.

Includes minor code layout fixes for better readability.

See #48303.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/plugin-editor.php

    r45932 r47122  
    8080$edit_error     = null;
    8181$posted_content = null;
     82
    8283if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
    8384    $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
     
    102103}
    103104
    104     // List of allowable extensions
    105     $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
     105// List of allowable extensions.
     106$editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
    106107
    107108if ( ! is_file( $real_file ) ) {
    108109    wp_die( sprintf( '<p>%s</p>', __( 'File does not exist! Please double check the name and try again.' ) ) );
    109110} else {
    110     // Get the extension of the file
     111    // Get the extension of the file.
    111112    if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
    112113        $ext = strtolower( $matches[1] );
    113         // If extension is not in the acceptable list, skip it
     114        // If extension is not in the acceptable list, skip it.
    114115        if ( ! in_array( $ext, $editable_extensions ) ) {
    115116            wp_die( sprintf( '<p>%s</p>', __( 'Files of this type are not editable.' ) ) );
     
    118119}
    119120
    120     get_current_screen()->add_help_tab(
    121         array(
    122             'id'      => 'overview',
    123             'title'   => __( 'Overview' ),
    124             'content' =>
    125                     '<p>' . __( 'You can use the plugin editor to make changes to any of your plugins&#8217; individual PHP files. Be aware that if you make changes, plugins updates will overwrite your customizations.' ) . '</p>' .
    126                     '<p>' . __( 'Choose a plugin to edit from the dropdown menu and click the Select button. Click once on any file name to load it in the editor, and make your changes. Don&#8217;t forget to save your changes (Update File) when you&#8217;re finished.' ) . '</p>' .
    127                     '<p>' . __( 'The Documentation menu below the editor lists the PHP functions recognized in the plugin file. Clicking Look Up takes you to a web page about that particular function.' ) . '</p>' .
    128                     '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
    129                     '<ul>' .
    130                     '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
    131                     '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
    132                     '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
    133                     '</ul>' .
    134                     '<p>' . __( 'If you want to make changes but don&#8217;t want them to be overwritten when the plugin is updated, you may be ready to think about writing your own plugin. For information on how to edit plugins, write your own from scratch, or just better understand their anatomy, check out the links below.' ) . '</p>' .
    135                     ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
    136         )
    137     );
    138 
    139     get_current_screen()->set_help_sidebar(
    140         '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
    141         '<p>' . __( '<a href="https://wordpress.org/support/article/plugins-editor-screen/">Documentation on Editing Plugins</a>' ) . '</p>' .
    142         '<p>' . __( '<a href="https://developer.wordpress.org/plugins/">Documentation on Writing Plugins</a>' ) . '</p>' .
    143         '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
    144     );
    145 
    146     $settings = array(
    147         'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ),
    148     );
    149     wp_enqueue_script( 'wp-theme-plugin-editor' );
    150     wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
    151     wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) );
    152 
    153     require_once( ABSPATH . 'wp-admin/admin-header.php' );
    154 
    155     update_recently_edited( WP_PLUGIN_DIR . '/' . $file );
    156 
    157     if ( ! empty( $posted_content ) ) {
    158         $content = $posted_content;
    159     } else {
    160         $content = file_get_contents( $real_file );
    161     }
    162 
    163     if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
    164         $functions = wp_doc_link_parse( $content );
    165 
    166         if ( ! empty( $functions ) ) {
    167             $docs_select  = '<select name="docs-list" id="docs-list">';
    168             $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
    169             foreach ( $functions as $function ) {
    170                 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
    171             }
    172             $docs_select .= '</select>';
    173         }
    174     }
    175 
    176     $content = esc_textarea( $content );
    177     ?>
     121get_current_screen()->add_help_tab(
     122    array(
     123        'id'      => 'overview',
     124        'title'   => __( 'Overview' ),
     125        'content' =>
     126                '<p>' . __( 'You can use the plugin editor to make changes to any of your plugins&#8217; individual PHP files. Be aware that if you make changes, plugins updates will overwrite your customizations.' ) . '</p>' .
     127                '<p>' . __( 'Choose a plugin to edit from the dropdown menu and click the Select button. Click once on any file name to load it in the editor, and make your changes. Don&#8217;t forget to save your changes (Update File) when you&#8217;re finished.' ) . '</p>' .
     128                '<p>' . __( 'The Documentation menu below the editor lists the PHP functions recognized in the plugin file. Clicking Look Up takes you to a web page about that particular function.' ) . '</p>' .
     129                '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
     130                '<ul>' .
     131                '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
     132                '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
     133                '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
     134                '</ul>' .
     135                '<p>' . __( 'If you want to make changes but don&#8217;t want them to be overwritten when the plugin is updated, you may be ready to think about writing your own plugin. For information on how to edit plugins, write your own from scratch, or just better understand their anatomy, check out the links below.' ) . '</p>' .
     136                ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
     137    )
     138);
     139
     140get_current_screen()->set_help_sidebar(
     141    '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
     142    '<p>' . __( '<a href="https://wordpress.org/support/article/plugins-editor-screen/">Documentation on Editing Plugins</a>' ) . '</p>' .
     143    '<p>' . __( '<a href="https://developer.wordpress.org/plugins/">Documentation on Writing Plugins</a>' ) . '</p>' .
     144    '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
     145);
     146
     147$settings = array(
     148    'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ),
     149);
     150wp_enqueue_script( 'wp-theme-plugin-editor' );
     151wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
     152wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) );
     153
     154require_once( ABSPATH . 'wp-admin/admin-header.php' );
     155
     156update_recently_edited( WP_PLUGIN_DIR . '/' . $file );
     157
     158if ( ! empty( $posted_content ) ) {
     159    $content = $posted_content;
     160} else {
     161    $content = file_get_contents( $real_file );
     162}
     163
     164if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
     165    $functions = wp_doc_link_parse( $content );
     166
     167    if ( ! empty( $functions ) ) {
     168        $docs_select  = '<select name="docs-list" id="docs-list">';
     169        $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
     170        foreach ( $functions as $function ) {
     171            $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
     172        }
     173        $docs_select .= '</select>';
     174    }
     175}
     176
     177$content = esc_textarea( $content );
     178?>
    178179<div class="wrap">
    179180<h1><?php echo esc_html( $title ); ?></h1>
     
    218219        <strong><label for="plugin"><?php _e( 'Select plugin to edit:' ); ?> </label></strong>
    219220        <select name="plugin" id="plugin">
    220 <?php
    221 foreach ( $plugins as $plugin_key => $a_plugin ) {
    222     $plugin_name = $a_plugin['Name'];
    223     if ( $plugin_key == $plugin ) {
    224         $selected = " selected='selected'";
    225     } else {
    226         $selected = '';
    227     }
    228     $plugin_name = esc_attr( $plugin_name );
    229     $plugin_key  = esc_attr( $plugin_key );
    230     echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
    231 }
    232 ?>
     221        <?php
     222        foreach ( $plugins as $plugin_key => $a_plugin ) {
     223            $plugin_name = $a_plugin['Name'];
     224            if ( $plugin_key == $plugin ) {
     225                $selected = " selected='selected'";
     226            } else {
     227                $selected = '';
     228            }
     229            $plugin_name = esc_attr( $plugin_name );
     230            $plugin_key  = esc_attr( $plugin_key );
     231            echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
     232        }
     233        ?>
    233234        </select>
    234235        <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
     
    256257    </ul>
    257258</div>
     259
    258260<form name="template" id="template" action="plugin-editor.php" method="post">
    259261    <?php wp_nonce_field( 'edit-plugin_' . $file, 'nonce' ); ?>
    260         <div>
    261             <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
    262             <textarea cols="70" rows="25" name="newcontent" id="newcontent" aria-describedby="editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4"><?php echo $content; ?></textarea>
    263             <input type="hidden" name="action" value="update" />
    264             <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" />
    265             <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" />
     262    <div>
     263        <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
     264        <textarea cols="70" rows="25" name="newcontent" id="newcontent" aria-describedby="editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4"><?php echo $content; ?></textarea>
     265        <input type="hidden" name="action" value="update" />
     266        <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" />
     267        <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" />
     268    </div>
     269
     270    <?php if ( ! empty( $docs_select ) ) : ?>
     271        <div id="documentation" class="hide-if-no-js">
     272            <label for="docs-list"><?php _e( 'Documentation:' ); ?></label>
     273            <?php echo $docs_select; ?>
     274            <input disabled id="docs-lookup" type="button" class="button" value="<?php esc_attr_e( 'Look Up' ); ?>" onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ); ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ); ?>&amp;redirect=true'); }" />
    266275        </div>
    267         <?php if ( ! empty( $docs_select ) ) : ?>
    268         <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e( 'Documentation:' ); ?></label> <?php echo $docs_select; ?> <input disabled id="docs-lookup" type="button" class="button" value="<?php esc_attr_e( 'Look Up' ); ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ); ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ); ?>&amp;redirect=true'); }" /></div>
    269         <?php endif; ?>
    270 <?php if ( is_writeable( $real_file ) ) : ?>
    271     <div class="editor-notices">
     276    <?php endif; ?>
     277
     278    <?php if ( is_writeable( $real_file ) ) : ?>
     279        <div class="editor-notices">
    272280        <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) ) { ?>
    273281            <div class="notice notice-warning inline active-plugin-edit-warning">
    274             <p><?php _e( '<strong>Warning:</strong> Making changes to active plugins is not recommended.' ); ?></p>
     282                <p><?php _e( '<strong>Warning:</strong> Making changes to active plugins is not recommended.' ); ?></p>
     283            </div>
     284        <?php } ?>
    275285        </div>
    276         <?php } ?>
    277     </div>
    278     <p class="submit">
    279         <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
    280         <span class="spinner"></span>
    281     </p>
    282 <?php else : ?>
    283     <p><em>
    284         <?php
    285         printf(
    286             /* translators: %s: Documentation URL. */
    287             __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
    288             __( 'https://wordpress.org/support/article/changing-file-permissions/' )
    289         );
    290         ?>
    291     </em></p>
    292 <?php endif; ?>
    293 <?php wp_print_file_editor_templates(); ?>
     286        <p class="submit">
     287            <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
     288            <span class="spinner"></span>
     289        </p>
     290    <?php else : ?>
     291        <p><em>
     292            <?php
     293            printf(
     294                /* translators: %s: Documentation URL. */
     295                __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
     296                __( 'https://wordpress.org/support/article/changing-file-permissions/' )
     297            );
     298            ?>
     299        </em></p>
     300    <?php endif; ?>
     301
     302    <?php wp_print_file_editor_templates(); ?>
    294303</form>
    295304<br class="clear" />
     
    298307$dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
    299308if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) :
    300     // Get a back URL
    301     $referer                    = wp_get_referer();
     309    // Get a back URL.
     310    $referer = wp_get_referer();
     311
    302312    $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' );
    303313
     
    308318    }
    309319    ?>
    310 <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
    311     <div class="notification-dialog-background"></div>
    312     <div class="notification-dialog">
    313         <div class="file-editor-warning-content">
    314             <div class="file-editor-warning-message">
    315                 <h1><?php _e( 'Heads up!' ); ?></h1>
    316                 <p><?php _e( 'You appear to be making direct edits to your plugin in the WordPress dashboard. We recommend that you don&#8217;t! Editing plugins directly may introduce incompatibilities that break your site and your changes may be lost in future updates.' ); ?></p>
    317                 <p><?php _e( 'If you absolutely have to make direct edits to this plugin, use a file manager to create a copy with a new name and hang on to the original. That way, you can re-enable a functional version if something goes wrong.' ); ?></p>
     320    <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
     321        <div class="notification-dialog-background"></div>
     322        <div class="notification-dialog">
     323            <div class="file-editor-warning-content">
     324                <div class="file-editor-warning-message">
     325                    <h1><?php _e( 'Heads up!' ); ?></h1>
     326                    <p><?php _e( 'You appear to be making direct edits to your plugin in the WordPress dashboard. We recommend that you don&#8217;t! Editing plugins directly may introduce incompatibilities that break your site and your changes may be lost in future updates.' ); ?></p>
     327                    <p><?php _e( 'If you absolutely have to make direct edits to this plugin, use a file manager to create a copy with a new name and hang on to the original. That way, you can re-enable a functional version if something goes wrong.' ); ?></p>
     328                </div>
     329                <p>
     330                    <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
     331                    <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
     332                </p>
    318333            </div>
    319             <p>
    320                 <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
    321                 <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
    322             </p>
    323334        </div>
    324335    </div>
    325 </div>
    326336    <?php
    327337endif; // Editor warning notice.
Note: See TracChangeset for help on using the changeset viewer.