| @@ -20,9 +20,9 @@ | ||
| 20 | 20 | */ |
| 21 | 21 | public function __construct() { |
| 22 | 22 | |
| 23 | 23 | // Outputs the TinyMCE and QuickTag Modal. |
| 24 | - add_action( 'wp_ajax_convertkit_admin_tinymce_output_modal', array( $this, 'output_modal' ) ); | |
| 24 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 25 | 25 | |
| 26 | 26 | // Add filters to register QuickTag Plugins. |
| 27 | 27 | add_action( 'admin_enqueue_scripts', array( $this, 'register_quicktags' ) ); // WordPress Admin. |
| 28 | 28 | add_action( 'wp_enqueue_scripts', array( $this, 'register_quicktags' ) ); // Frontend Editors. |
| @@ -34,38 +34,80 @@ | ||
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | + * Register REST API routes. | |
| 39 | + * | |
| 40 | + * @since 3.1.8 | |
| 41 | + */ | |
| 42 | + public function register_routes() { | |
| 43 | + | |
| 44 | + // Register route to return all blocks registered by the Plugin. | |
| 45 | + register_rest_route( | |
| 46 | + 'kit/v1', | |
| 47 | + '/editor/tinymce/modal/(?P<shortcode>[a-zA-Z0-9-_]+)/(?P<editor_type>[a-zA-Z0-9-_]+)', | |
| 48 | + array( | |
| 49 | + 'methods' => WP_REST_Server::READABLE, | |
| 50 | + 'args' => array( | |
| 51 | + 'shortcode' => array( | |
| 52 | + 'required' => true, | |
| 53 | + 'validate_callback' => function ( $param ) { | |
| 54 | + return is_string( $param ) && in_array( $param, array_keys( convertkit_get_shortcodes() ), true ); | |
| 55 | + }, | |
| 56 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 57 | + ), | |
| 58 | + 'editor_type' => array( | |
| 59 | + 'required' => true, | |
| 60 | + 'validate_callback' => function ( $param ) { | |
| 61 | + return is_string( $param ) && in_array( $param, array( 'tinymce', 'quicktags' ), true ); | |
| 62 | + }, | |
| 63 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 64 | + ), | |
| 65 | + ), | |
| 66 | + 'callback' => function ( $request ) { | |
| 67 | + return rest_ensure_response( $this->output_modal( $request['shortcode'], $request['editor_type'] ) ); | |
| 68 | + }, | |
| 69 | + | |
| 70 | + // Only refresh resources for users who can edit posts. | |
| 71 | + 'permission_callback' => function () { | |
| 72 | + return current_user_can( 'edit_posts' ); | |
| 73 | + }, | |
| 74 | + ) | |
| 75 | + ); | |
| 76 | + | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 38 | 80 | * Loads the view for a shortcode's modal in the TinyMCE and Text Editors. |
| 39 | 81 | * |
| 40 | 82 | * @since 1.9.6 |
| 83 | + * | |
| 84 | + * @param string $shortcode_name Shortcode Name. | |
| 85 | + * @param string $editor_type Editor Type (tinymce|quicktags). | |
| 86 | + * @return string | |
| 41 | 87 | */ |
| 42 | - public function output_modal() { | |
| 88 | + public function output_modal( $shortcode_name, $editor_type ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed | |
| 43 | 89 | |
| 44 | - // Check nonce. | |
| 45 | - check_ajax_referer( 'convertkit_admin_tinymce', 'nonce' ); | |
| 46 | - | |
| 47 | 90 | // Get shortcodes. |
| 48 | 91 | $shortcodes = convertkit_get_shortcodes(); |
| 49 | 92 | |
| 50 | - // Get requested shortcode name. | |
| 51 | - $shortcode_name = sanitize_text_field( $_REQUEST['shortcode'] ); | |
| 52 | - $editor_type = sanitize_text_field( $_REQUEST['editor_type'] ); | |
| 93 | + // Start output buffering. | |
| 94 | + ob_start(); | |
| 53 | 95 | |
| 54 | 96 | // If the shortcode is not registered, return a view in the modal to tell the user. |
| 55 | 97 | if ( ! isset( $shortcodes[ $shortcode_name ] ) ) { |
| 56 | 98 | require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-missing.php'; |
| 57 | - die(); | |
| 99 | + return ob_get_clean(); | |
| 58 | 100 | } |
| 59 | 101 | |
| 60 | 102 | // Define shortcode. |
| 61 | 103 | $shortcode = $shortcodes[ $shortcode_name ]; |
| 62 | 104 | |
| 63 | - // Show a message in the modal if no API Key is specified. | |
| 64 | - if ( array_key_exists( 'has_api_key', $shortcode ) && ! $shortcode['has_api_key'] ) { | |
| 65 | - $notice = $shortcode['no_api_key']; | |
| 105 | + // Show a message in the modal if no Access Token is specified. | |
| 106 | + if ( array_key_exists( 'has_access_token', $shortcode ) && ! $shortcode['has_access_token'] ) { | |
| 107 | + $notice = $shortcode['no_access_token']; | |
| 66 | 108 | require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php'; |
| 67 | - die(); | |
| 109 | + return ob_get_clean(); | |
| 68 | 110 | } |
| 69 | 111 | |
| 70 | 112 | // Show a message in the modal if no resources exist. |
| 71 | 113 | if ( array_key_exists( 'has_resources', $shortcode ) && ! $shortcode['has_resources'] ) { |
| @@ -70,20 +112,20 @@ | ||
| 70 | 112 | // Show a message in the modal if no resources exist. |
| 71 | 113 | if ( array_key_exists( 'has_resources', $shortcode ) && ! $shortcode['has_resources'] ) { |
| 72 | 114 | $notice = $shortcode['no_resources']; |
| 73 | 115 | require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php'; |
| 74 | - die(); | |
| 116 | + return ob_get_clean(); | |
| 75 | 117 | } |
| 76 | 118 | |
| 77 | 119 | // If we have less than two panels defined in the shortcode properties, output a basic modal. |
| 78 | 120 | if ( count( $shortcode['panels'] ) < 2 ) { |
| 79 | 121 | require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal.php'; |
| 80 | - die(); | |
| 122 | + return ob_get_clean(); | |
| 81 | 123 | } |
| 82 | 124 | |
| 83 | 125 | // Output tabbed view. |
| 84 | 126 | require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-tabbed.php'; |
| 85 | - die(); | |
| 127 | + return ob_get_clean(); | |
| 86 | 128 | |
| 87 | 129 | } |
| 88 | 130 | |
| 89 | 131 | /** |
| @@ -96,14 +138,14 @@ | ||
| 96 | 138 | // Get shortcodes. |
| 97 | 139 | $shortcodes = convertkit_get_shortcodes(); |
| 98 | 140 | |
| 99 | 141 | // Bail if no shortcode are available. |
| 100 | - if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) { | |
| 142 | + if ( ! count( $shortcodes ) ) { | |
| 101 | 143 | return; |
| 102 | 144 | } |
| 103 | 145 | |
| 104 | 146 | // Enqueue Quicktag JS. |
| 105 | - wp_enqueue_script( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/quicktags.js', array( 'jquery', 'quicktags' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 147 | + wp_enqueue_script( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/quicktags.js', array( 'quicktags' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 106 | 148 | |
| 107 | 149 | // Make shortcodes available as convertkit_quicktags JS variable. |
| 108 | 150 | wp_localize_script( 'convertkit-admin-quicktags', 'convertkit_quicktags', $shortcodes ); |
| 109 | 151 | |
| @@ -111,9 +153,10 @@ | ||
| 111 | 153 | wp_localize_script( |
| 112 | 154 | 'convertkit-admin-quicktags', |
| 113 | 155 | 'convertkit_admin_tinymce', |
| 114 | 156 | array( |
| 115 | - 'nonce' => wp_create_nonce( 'convertkit_admin_tinymce' ), | |
| 157 | + 'ajaxurl' => rest_url( 'kit/v1/editor/tinymce/modal' ), | |
| 158 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 116 | 159 | ) |
| 117 | 160 | ); |
| 118 | 161 | |
| 119 | 162 | // Enqueue Quicktag CSS. |
| @@ -118,12 +161,8 @@ | ||
| 118 | 161 | |
| 119 | 162 | // Enqueue Quicktag CSS. |
| 120 | 163 | wp_enqueue_style( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/quicktags.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 121 | 164 | |
| 122 | - // Enqueue WordPress JS and CSS. | |
| 123 | - wp_enqueue_script( 'wp-color-picker' ); | |
| 124 | - wp_enqueue_style( 'wp-color-picker' ); | |
| 125 | - | |
| 126 | 165 | // Output Backbone View Template. |
| 127 | 166 | add_action( 'wp_print_footer_scripts', array( $this, 'output_quicktags_modal' ) ); |
| 128 | 167 | add_action( 'admin_print_footer_scripts', array( $this, 'output_quicktags_modal' ) ); |
| 129 | 168 | |
| @@ -142,29 +181,30 @@ | ||
| 142 | 181 | // Get shortcodes. |
| 143 | 182 | $shortcodes = convertkit_get_shortcodes(); |
| 144 | 183 | |
| 145 | 184 | // Bail if no shortcodes are available. |
| 146 | - if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) { | |
| 185 | + if ( ! count( $shortcodes ) ) { | |
| 147 | 186 | return $plugins; |
| 148 | 187 | } |
| 149 | 188 | |
| 150 | 189 | // Enqueue TinyMCE CSS and JS. |
| 151 | 190 | wp_enqueue_script( 'convertkit-admin-tabs', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tabs.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 152 | - wp_enqueue_script( 'convertkit-admin-tinymce', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tinymce.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 153 | - wp_enqueue_script( 'convertkit-admin-modal', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/modal.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 191 | + wp_enqueue_script( 'convertkit-admin-editor', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/editor.js', array(), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 192 | + wp_enqueue_script( 'convertkit-admin-modal', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/modal.js', array(), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 154 | 193 | wp_enqueue_style( 'convertkit-admin-tinymce', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/tinymce.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 155 | 194 | |
| 156 | 195 | // Register JS variable convertkit_admin_tinymce.nonce for AJAX calls. |
| 157 | 196 | wp_localize_script( |
| 158 | - 'convertkit-admin-tinymce', | |
| 197 | + 'convertkit-admin-editor', | |
| 159 | 198 | 'convertkit_admin_tinymce', |
| 160 | 199 | array( |
| 161 | - 'nonce' => wp_create_nonce( 'convertkit_admin_tinymce' ), | |
| 200 | + 'ajaxurl' => rest_url( 'kit/v1/editor/tinymce/modal' ), | |
| 201 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 162 | 202 | ) |
| 163 | 203 | ); |
| 164 | 204 | |
| 165 | 205 | // Make shortcodes available as convertkit_shortcodes JS variable. |
| 166 | - wp_localize_script( 'convertkit-admin-tinymce', 'convertkit_shortcodes', $shortcodes ); | |
| 206 | + wp_localize_script( 'convertkit-admin-editor', 'convertkit_shortcodes', $shortcodes ); | |
| 167 | 207 | |
| 168 | 208 | // Register TinyMCE Javascript Plugin. |
| 169 | 209 | foreach ( $shortcodes as $shortcode => $properties ) { |
| 170 | 210 | $plugins[ 'convertkit_' . $shortcode ] = CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tinymce-' . $shortcode . '.js'; |
| @@ -187,9 +227,9 @@ | ||
| 187 | 227 | // Get shortcodes. |
| 188 | 228 | $shortcodes = convertkit_get_shortcodes(); |
| 189 | 229 | |
| 190 | 230 | // Bail if no shortcodes are available. |
| 191 | - if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) { | |
| 231 | + if ( ! count( $shortcodes ) ) { | |
| 192 | 232 | return $buttons; |
| 193 | 233 | } |
| 194 | 234 | |
| 195 | 235 | // Register each Shortcode as a TinyMCE Button. |