| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin TinyMCE class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit TinyMCE class. Registers buttons. |
| 11 |
* |
| 12 |
* @since 1.5.0 |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
class ConvertKit_Admin_TinyMCE { |
| 17 |
|
| 18 |
/** |
| 19 |
* Constructor |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
|
| 23 |
// Outputs the TinyMCE and QuickTag Modal. |
| 24 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 25 |
|
| 26 |
// Add filters to register QuickTag Plugins. |
| 27 |
add_action( 'admin_enqueue_scripts', array( $this, 'register_quicktags' ) ); // WordPress Admin. |
| 28 |
add_action( 'wp_enqueue_scripts', array( $this, 'register_quicktags' ) ); // Frontend Editors. |
| 29 |
|
| 30 |
// Add filters to register TinyMCE Plugins. |
| 31 |
// Low priority ensures this works with Frontend Page Builders. |
| 32 |
add_filter( 'mce_external_plugins', array( $this, 'register_tinymce_plugins' ), 99999 ); |
| 33 |
add_filter( 'mce_buttons', array( $this, 'register_tinymce_buttons' ), 99999 ); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 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 |
/** |
| 80 |
* Loads the view for a shortcode's modal in the TinyMCE and Text Editors. |
| 81 |
* |
| 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 |
| 87 |
*/ |
| 88 |
public function output_modal( $shortcode_name, $editor_type ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 89 |
|
| 90 |
// Get shortcodes. |
| 91 |
$shortcodes = convertkit_get_shortcodes(); |
| 92 |
|
| 93 |
// Start output buffering. |
| 94 |
ob_start(); |
| 95 |
|
| 96 |
// If the shortcode is not registered, return a view in the modal to tell the user. |
| 97 |
if ( ! isset( $shortcodes[ $shortcode_name ] ) ) { |
| 98 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-missing.php'; |
| 99 |
return ob_get_clean(); |
| 100 |
} |
| 101 |
|
| 102 |
// Define shortcode. |
| 103 |
$shortcode = $shortcodes[ $shortcode_name ]; |
| 104 |
|
| 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']; |
| 108 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php'; |
| 109 |
return ob_get_clean(); |
| 110 |
} |
| 111 |
|
| 112 |
// Show a message in the modal if no resources exist. |
| 113 |
if ( array_key_exists( 'has_resources', $shortcode ) && ! $shortcode['has_resources'] ) { |
| 114 |
$notice = $shortcode['no_resources']; |
| 115 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php'; |
| 116 |
return ob_get_clean(); |
| 117 |
} |
| 118 |
|
| 119 |
// If we have less than two panels defined in the shortcode properties, output a basic modal. |
| 120 |
if ( count( $shortcode['panels'] ) < 2 ) { |
| 121 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal.php'; |
| 122 |
return ob_get_clean(); |
| 123 |
} |
| 124 |
|
| 125 |
// Output tabbed view. |
| 126 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-tabbed.php'; |
| 127 |
return ob_get_clean(); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Registers QuickTags JS for the TinyMCE Text (non-Visual) Editor |
| 133 |
* |
| 134 |
* @since 3.0.0 |
| 135 |
*/ |
| 136 |
public function register_quicktags() { |
| 137 |
|
| 138 |
// Get shortcodes. |
| 139 |
$shortcodes = convertkit_get_shortcodes(); |
| 140 |
|
| 141 |
// Bail if no shortcode are available. |
| 142 |
if ( ! count( $shortcodes ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
// Enqueue Quicktag JS. |
| 147 |
wp_enqueue_script( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/quicktags.js', array( 'quicktags' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 148 |
|
| 149 |
// Make shortcodes available as convertkit_quicktags JS variable. |
| 150 |
wp_localize_script( 'convertkit-admin-quicktags', 'convertkit_quicktags', $shortcodes ); |
| 151 |
|
| 152 |
// Register JS variable convertkit_admin_tinymce.nonce for AJAX calls. |
| 153 |
wp_localize_script( |
| 154 |
'convertkit-admin-quicktags', |
| 155 |
'convertkit_admin_tinymce', |
| 156 |
array( |
| 157 |
'ajaxurl' => rest_url( 'kit/v1/editor/tinymce/modal' ), |
| 158 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 159 |
) |
| 160 |
); |
| 161 |
|
| 162 |
// Enqueue Quicktag CSS. |
| 163 |
wp_enqueue_style( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/quicktags.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 164 |
|
| 165 |
// Output Backbone View Template. |
| 166 |
add_action( 'wp_print_footer_scripts', array( $this, 'output_quicktags_modal' ) ); |
| 167 |
add_action( 'admin_print_footer_scripts', array( $this, 'output_quicktags_modal' ) ); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Register JS plugins for the TinyMCE Editor |
| 173 |
* |
| 174 |
* @since 1.5.0 |
| 175 |
* |
| 176 |
* @param array $plugins JS Plugins. |
| 177 |
* @return array JS Plugins |
| 178 |
*/ |
| 179 |
public function register_tinymce_plugins( $plugins ) { |
| 180 |
|
| 181 |
// Get shortcodes. |
| 182 |
$shortcodes = convertkit_get_shortcodes(); |
| 183 |
|
| 184 |
// Bail if no shortcodes are available. |
| 185 |
if ( ! count( $shortcodes ) ) { |
| 186 |
return $plugins; |
| 187 |
} |
| 188 |
|
| 189 |
// Enqueue TinyMCE CSS and JS. |
| 190 |
wp_enqueue_script( 'convertkit-admin-tabs', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tabs.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 ); |
| 193 |
wp_enqueue_style( 'convertkit-admin-tinymce', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/tinymce.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 194 |
|
| 195 |
// Register JS variable convertkit_admin_tinymce.nonce for AJAX calls. |
| 196 |
wp_localize_script( |
| 197 |
'convertkit-admin-editor', |
| 198 |
'convertkit_admin_tinymce', |
| 199 |
array( |
| 200 |
'ajaxurl' => rest_url( 'kit/v1/editor/tinymce/modal' ), |
| 201 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 202 |
) |
| 203 |
); |
| 204 |
|
| 205 |
// Make shortcodes available as convertkit_shortcodes JS variable. |
| 206 |
wp_localize_script( 'convertkit-admin-editor', 'convertkit_shortcodes', $shortcodes ); |
| 207 |
|
| 208 |
// Register TinyMCE Javascript Plugin. |
| 209 |
foreach ( $shortcodes as $shortcode => $properties ) { |
| 210 |
$plugins[ 'convertkit_' . $shortcode ] = CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tinymce-' . $shortcode . '.js'; |
| 211 |
} |
| 212 |
|
| 213 |
return $plugins; |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Registers buttons in the TinyMCE Editor |
| 219 |
* |
| 220 |
* @since 1.5.0 |
| 221 |
* |
| 222 |
* @param array $buttons Buttons. |
| 223 |
* @return array Buttons |
| 224 |
*/ |
| 225 |
public function register_tinymce_buttons( $buttons ) { |
| 226 |
|
| 227 |
// Get shortcodes. |
| 228 |
$shortcodes = convertkit_get_shortcodes(); |
| 229 |
|
| 230 |
// Bail if no shortcodes are available. |
| 231 |
if ( ! count( $shortcodes ) ) { |
| 232 |
return $buttons; |
| 233 |
} |
| 234 |
|
| 235 |
// Register each Shortcode as a TinyMCE Button. |
| 236 |
foreach ( $shortcodes as $shortcode => $properties ) { |
| 237 |
$buttons[] = 'convertkit_' . $shortcode; |
| 238 |
} |
| 239 |
|
| 240 |
return $buttons; |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Outputs the QuickTags modal view in the footer of the site, which is |
| 246 |
* used when using the Text editor button to insert a shortcode. |
| 247 |
* |
| 248 |
* @since 1.9.7.5 |
| 249 |
*/ |
| 250 |
public function output_quicktags_modal() { |
| 251 |
|
| 252 |
?> |
| 253 |
<script type="text/template" id="tmpl-convertkit-quicktags-modal"> |
| 254 |
<div id="convertkit-quicktags-modal"> |
| 255 |
<div class="media-frame-title"><h1></h1></div> |
| 256 |
<div class="media-frame-content"></div> |
| 257 |
<div class="media-frame-toolbar"> |
| 258 |
<div class="media-toolbar"> |
| 259 |
<div class="media-toolbar-secondary"> |
| 260 |
<button type="button" class="button button-large cancel"><?php esc_html_e( 'Cancel', 'convertkit' ); ?></button> |
| 261 |
</div> |
| 262 |
<div class="media-toolbar-primary"> |
| 263 |
<button type="button" class="button button-primary button-large"><?php esc_html_e( 'Insert', 'convertkit' ); ?></button> |
| 264 |
</div> |
| 265 |
</div> |
| 266 |
</div> |
| 267 |
</div> |
| 268 |
</script> |
| 269 |
<?php |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|