# convertkit/3.4.2/admin/class-convertkit-admin-tinymce.php

Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages, version 3.4.2. 274 lines.

- Page: https://pluginprobe.com/plugins/convertkit/3.4.2/code/admin/class-convertkit-admin-tinymce.php
- Raw: https://pluginprobe.com/plugins/convertkit/3.4.2/raw/admin/class-convertkit-admin-tinymce.php
- Modified: 2026-02-17T06:19:26+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/convertkit/3.4.2/code/admin/class-convertkit-admin-tinymce.php#L10-L20`.

```php
<?php
/**
 * ConvertKit Admin TinyMCE class.
 *
 * @package ConvertKit
 * @author ConvertKit
 */

/**
 * ConvertKit TinyMCE class. Registers buttons.
 *
 * @since 1.5.0
 * @package ConvertKit
 * @author ConvertKit
 */
class ConvertKit_Admin_TinyMCE {

	/**
	 * Constructor
	 */
	public function __construct() {

		// Outputs the TinyMCE and QuickTag Modal.
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );

		// Add filters to register QuickTag Plugins.
		add_action( 'admin_enqueue_scripts', array( $this, 'register_quicktags' ) ); // WordPress Admin.
		add_action( 'wp_enqueue_scripts', array( $this, 'register_quicktags' ) ); // Frontend Editors.

		// Add filters to register TinyMCE Plugins.
		// Low priority ensures this works with Frontend Page Builders.
		add_filter( 'mce_external_plugins', array( $this, 'register_tinymce_plugins' ), 99999 );
		add_filter( 'mce_buttons', array( $this, 'register_tinymce_buttons' ), 99999 );

	}

	/**
	 * Register REST API routes.
	 *
	 * @since   3.1.8
	 */
	public function register_routes() {

		// Register route to return all blocks registered by the Plugin.
		register_rest_route(
			'kit/v1',
			'/editor/tinymce/modal/(?P<shortcode>[a-zA-Z0-9-_]+)/(?P<editor_type>[a-zA-Z0-9-_]+)',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'args'                => array(
					'shortcode'   => array(
						'required'          => true,
						'validate_callback' => function ( $param ) {
							return is_string( $param ) && in_array( $param, array_keys( convertkit_get_shortcodes() ), true );
						},
						'sanitize_callback' => 'sanitize_text_field',
					),
					'editor_type' => array(
						'required'          => true,
						'validate_callback' => function ( $param ) {
							return is_string( $param ) && in_array( $param, array( 'tinymce', 'quicktags' ), true );
						},
						'sanitize_callback' => 'sanitize_text_field',
					),
				),
				'callback'            => function ( $request ) {
					return rest_ensure_response( $this->output_modal( $request['shortcode'], $request['editor_type'] ) );
				},

				// Only refresh resources for users who can edit posts.
				'permission_callback' => function () {
					return current_user_can( 'edit_posts' );
				},
			)
		);

	}

	/**
	 * Loads the view for a shortcode's modal in the TinyMCE and Text Editors.
	 *
	 * @since   1.9.6
	 *
	 * @param   string $shortcode_name Shortcode Name.
	 * @param   string $editor_type    Editor Type (tinymce|quicktags).
	 * @return  string
	 */
	public function output_modal( $shortcode_name, $editor_type ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed

		// Get shortcodes.
		$shortcodes = convertkit_get_shortcodes();

		// Start output buffering.
		ob_start();

		// If the shortcode is not registered, return a view in the modal to tell the user.
		if ( ! isset( $shortcodes[ $shortcode_name ] ) ) {
			require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-missing.php';
			return ob_get_clean();
		}

		// Define shortcode.
		$shortcode = $shortcodes[ $shortcode_name ];

		// Show a message in the modal if no Access Token is specified.
		if ( array_key_exists( 'has_access_token', $shortcode ) && ! $shortcode['has_access_token'] ) {
			$notice = $shortcode['no_access_token'];
			require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php';
			return ob_get_clean();
		}

		// Show a message in the modal if no resources exist.
		if ( array_key_exists( 'has_resources', $shortcode ) && ! $shortcode['has_resources'] ) {
			$notice = $shortcode['no_resources'];
			require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php';
			return ob_get_clean();
		}

		// If we have less than two panels defined in the shortcode properties, output a basic modal.
		if ( count( $shortcode['panels'] ) < 2 ) {
			require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal.php';
			return ob_get_clean();
		}

		// Output tabbed view.
		require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-tabbed.php';
		return ob_get_clean();

	}

	/**
	 * Registers QuickTags JS for the TinyMCE Text (non-Visual) Editor
	 *
	 * @since   3.0.0
	 */
	public function register_quicktags() {

		// Get shortcodes.
		$shortcodes = convertkit_get_shortcodes();

		// Bail if no shortcode are available.
		if ( ! count( $shortcodes ) ) {
			return;
		}

		// Enqueue Quicktag JS.
		wp_enqueue_script( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/quicktags.js', array( 'quicktags' ), CONVERTKIT_PLUGIN_VERSION, true );

		// Make shortcodes available as convertkit_quicktags JS variable.
		wp_localize_script( 'convertkit-admin-quicktags', 'convertkit_quicktags', $shortcodes );

		// Register JS variable convertkit_admin_tinymce.nonce for AJAX calls.
		wp_localize_script(
			'convertkit-admin-quicktags',
			'convertkit_admin_tinymce',
			array(
				'ajaxurl' => rest_url( 'kit/v1/editor/tinymce/modal' ),
				'nonce'   => wp_create_nonce( 'wp_rest' ),
			)
		);

		// Enqueue Quicktag CSS.
		wp_enqueue_style( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/quicktags.css', array(), CONVERTKIT_PLUGIN_VERSION );

		// Output Backbone View Template.
		add_action( 'wp_print_footer_scripts', array( $this, 'output_quicktags_modal' ) );
		add_action( 'admin_print_footer_scripts', array( $this, 'output_quicktags_modal' ) );

	}

	/**
	 * Register JS plugins for the TinyMCE Editor
	 *
	 * @since   1.5.0
	 *
	 * @param   array $plugins    JS Plugins.
	 * @return  array             JS Plugins
	 */
	public function register_tinymce_plugins( $plugins ) {

		// Get shortcodes.
		$shortcodes = convertkit_get_shortcodes();

		// Bail if no shortcodes are available.
		if ( ! count( $shortcodes ) ) {
			return $plugins;
		}

		// Enqueue TinyMCE CSS and JS.
		wp_enqueue_script( 'convertkit-admin-tabs', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tabs.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
		wp_enqueue_script( 'convertkit-admin-editor', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/editor.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
		wp_enqueue_script( 'convertkit-admin-modal', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/modal.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
		wp_enqueue_style( 'convertkit-admin-tinymce', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/tinymce.css', array(), CONVERTKIT_PLUGIN_VERSION );

		// Register JS variable convertkit_admin_tinymce.nonce for AJAX calls.
		wp_localize_script(
			'convertkit-admin-editor',
			'convertkit_admin_tinymce',
			array(
				'ajaxurl' => rest_url( 'kit/v1/editor/tinymce/modal' ),
				'nonce'   => wp_create_nonce( 'wp_rest' ),
			)
		);

		// Make shortcodes available as convertkit_shortcodes JS variable.
		wp_localize_script( 'convertkit-admin-editor', 'convertkit_shortcodes', $shortcodes );

		// Register TinyMCE Javascript Plugin.
		foreach ( $shortcodes as $shortcode => $properties ) {
			$plugins[ 'convertkit_' . $shortcode ] = CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tinymce-' . $shortcode . '.js';
		}

		return $plugins;

	}

	/**
	 * Registers buttons in the TinyMCE Editor
	 *
	 * @since   1.5.0
	 *
	 * @param   array $buttons    Buttons.
	 * @return  array             Buttons
	 */
	public function register_tinymce_buttons( $buttons ) {

		// Get shortcodes.
		$shortcodes = convertkit_get_shortcodes();

		// Bail if no shortcodes are available.
		if ( ! count( $shortcodes ) ) {
			return $buttons;
		}

		// Register each Shortcode as a TinyMCE Button.
		foreach ( $shortcodes as $shortcode => $properties ) {
			$buttons[] = 'convertkit_' . $shortcode;
		}

		return $buttons;

	}

	/**
	 * Outputs the QuickTags modal view in the footer of the site, which is
	 * used when using the Text editor button to insert a shortcode.
	 *
	 * @since   1.9.7.5
	 */
	public function output_quicktags_modal() {

		?>
		<script type="text/template" id="tmpl-convertkit-quicktags-modal">
			<div id="convertkit-quicktags-modal">
				<div class="media-frame-title"><h1></h1></div>
				<div class="media-frame-content"></div>
				<div class="media-frame-toolbar">
					<div class="media-toolbar">
						<div class="media-toolbar-secondary">
							<button type="button" class="button button-large cancel"><?php esc_html_e( 'Cancel', 'convertkit' ); ?></button>
						</div>
						<div class="media-toolbar-primary">
							<button type="button" class="button button-primary button-large"><?php esc_html_e( 'Insert', 'convertkit' ); ?></button>
						</div>
					</div>
				</div>
			</div>
		</script>
		<?php

	}

}

```
