diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 90a9dd1e65..3342cad375 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -1497,7 +1497,10 @@ function wp_ajax_wp_link_ajax() { $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1; - require(ABSPATH . WPINC . '/class-wp-editor.php'); + if ( ! class_exists( '_WP_Editors', false ) ) { + require( ABSPATH . WPINC . '/class-wp-editor.php' ); + } + $results = _WP_Editors::wp_link_query( $args ); if ( ! isset( $results ) ) diff --git a/wp-admin/js/editor.js b/wp-admin/js/editor.js index a63a51cc53..917721a426 100644 --- a/wp-admin/js/editor.js +++ b/wp-admin/js/editor.js @@ -1,5 +1,8 @@ +window.wp = window.wp || {}; + +( function( $, wp ) { + wp.editor = wp.editor || {}; -( function( $ ) { /** * @summary Utility functions for the editor. * @@ -484,10 +487,8 @@ } ); } - window.wp = window.wp || {}; - window.wp.editor = window.wp.editor || {}; - window.wp.editor.autop = wpautop; - window.wp.editor.removep = pre_wpautop; + wp.editor.autop = wpautop; + wp.editor.removep = pre_wpautop; exports = { go: switchEditor, @@ -505,4 +506,143 @@ * Expose the switch editors to be used globally. */ window.switchEditors = new SwitchEditors(); -}( window.jQuery )); + + /** + * Initialize TinyMCE and/or Quicktags. For use with wp_enqueue_editor() (PHP). + * + * Intended for use with an existing textarea that will become the Text editor tab. + * The editor width will be the width of the textarea container, height will be adjustable. + * + * Settings for both TinyMCE and Quicktags can be passed on initialization, and are "filtered" + * with custom jQuery events on the document element, wp-before-tinymce-init and wp-before-quicktags-init. + * + * @since 4.8 + * + * @param {string} id The HTML id of the textarea that is used for the editor. + * Has to be jQuery compliant. No brackets, special chars, etc. + * @param {object} settings Example: + * settings = { + * // See https://www.tinymce.com/docs/configure/integration-and-setup/. + * // Alternatively set to `true` to use the defaults. + * tinymce: { + * setup: function( editor ) { + * console.log( 'Editor initialized', editor ); + * } + * } + * + * // Alternatively set to `true` to use the defaults. + * quicktags: { + * buttons: 'strong,em,link' + * } + * } + */ + wp.editor.initialize = function( id, settings ) { + var init; + var defaults; + + if ( ! $ || ! id || ! wp.editor.getDefaultSettings ) { + return; + } + + defaults = wp.editor.getDefaultSettings(); + + // Initialize TinyMCE by default + if ( ! settings ) { + settings = { + tinymce: true + }; + } + + // Add wrap and the Visual|Text tabs. + if ( settings.tinymce && settings.quicktags ) { + var $textarea = $( '#' + id ); + var $wrap = $( '
' ).attr( { + 'class': 'wp-core-ui wp-editor-wrap tmce-active', + id: 'wp-' + id + '-wrap' + } ); + var $editorContainer = $( '
' ); + var $button = $( '