PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.3.0
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.3.0
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / admin / class-convertkit-admin-tinymce.php

class-convertkit-admin-tinymce.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.3.0, at admin/class-convertkit-admin-tinymce.php

234 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( 'wp_ajax_convertkit_admin_tinymce_output_modal', array( $this, 'output_modal' ) );
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 * Loads the view for a shortcode's modal in the TinyMCE and Text Editors.
39 *
40 * @since 1.9.6
41 */
42 public function output_modal() {
43
44 // Check nonce.
45 check_ajax_referer( 'convertkit_admin_tinymce', 'nonce' );
46
47 // Get shortcodes.
48 $shortcodes = convertkit_get_shortcodes();
49
50 // Get requested shortcode name.
51 $shortcode_name = sanitize_text_field( $_REQUEST['shortcode'] );
52 $editor_type = sanitize_text_field( $_REQUEST['editor_type'] );
53
54 // If the shortcode is not registered, return a view in the modal to tell the user.
55 if ( ! isset( $shortcodes[ $shortcode_name ] ) ) {
56 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-missing.php';
57 die();
58 }
59
60 // Define shortcode.
61 $shortcode = $shortcodes[ $shortcode_name ];
62
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'];
66 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php';
67 die();
68 }
69
70 // Show a message in the modal if no resources exist.
71 if ( array_key_exists( 'has_resources', $shortcode ) && ! $shortcode['has_resources'] ) {
72 $notice = $shortcode['no_resources'];
73 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-notice.php';
74 die();
75 }
76
77 // If we have less than two panels defined in the shortcode properties, output a basic modal.
78 if ( count( $shortcode['panels'] ) < 2 ) {
79 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal.php';
80 die();
81 }
82
83 // Output tabbed view.
84 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/tinymce/modal-tabbed.php';
85 die();
86
87 }
88
89 /**
90 * Registers QuickTags JS for the TinyMCE Text (non-Visual) Editor
91 *
92 * @since 3.0.0
93 */
94 public function register_quicktags() {
95
96 // Get shortcodes.
97 $shortcodes = convertkit_get_shortcodes();
98
99 // Bail if no shortcode are available.
100 if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) {
101 return;
102 }
103
104 // 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 );
106
107 // Make shortcodes available as convertkit_quicktags JS variable.
108 wp_localize_script( 'convertkit-admin-quicktags', 'convertkit_quicktags', $shortcodes );
109
110 // Register JS variable convertkit_admin_tinymce.nonce for AJAX calls.
111 wp_localize_script(
112 'convertkit-admin-quicktags',
113 'convertkit_admin_tinymce',
114 array(
115 'nonce' => wp_create_nonce( 'convertkit_admin_tinymce' ),
116 )
117 );
118
119 // Enqueue Quicktag CSS.
120 wp_enqueue_style( 'convertkit-admin-quicktags', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/quicktags.css', array(), CONVERTKIT_PLUGIN_VERSION );
121
122 // Enqueue WordPress JS and CSS.
123 wp_enqueue_script( 'wp-color-picker' );
124 wp_enqueue_style( 'wp-color-picker' );
125
126 // Output Backbone View Template.
127 add_action( 'wp_print_footer_scripts', array( $this, 'output_quicktags_modal' ) );
128 add_action( 'admin_print_footer_scripts', array( $this, 'output_quicktags_modal' ) );
129
130 }
131
132 /**
133 * Register JS plugins for the TinyMCE Editor
134 *
135 * @since 1.5.0
136 *
137 * @param array $plugins JS Plugins.
138 * @return array JS Plugins
139 */
140 public function register_tinymce_plugins( $plugins ) {
141
142 // Get shortcodes.
143 $shortcodes = convertkit_get_shortcodes();
144
145 // Bail if no shortcodes are available.
146 if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) {
147 return $plugins;
148 }
149
150 // Enqueue TinyMCE CSS and JS.
151 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 );
154 wp_enqueue_style( 'convertkit-admin-tinymce', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/tinymce.css', array(), CONVERTKIT_PLUGIN_VERSION );
155
156 // Register JS variable convertkit_admin_tinymce.nonce for AJAX calls.
157 wp_localize_script(
158 'convertkit-admin-tinymce',
159 'convertkit_admin_tinymce',
160 array(
161 'nonce' => wp_create_nonce( 'convertkit_admin_tinymce' ),
162 )
163 );
164
165 // Make shortcodes available as convertkit_shortcodes JS variable.
166 wp_localize_script( 'convertkit-admin-tinymce', 'convertkit_shortcodes', $shortcodes );
167
168 // Register TinyMCE Javascript Plugin.
169 foreach ( $shortcodes as $shortcode => $properties ) {
170 $plugins[ 'convertkit_' . $shortcode ] = CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tinymce-' . $shortcode . '.js';
171 }
172
173 return $plugins;
174
175 }
176
177 /**
178 * Registers buttons in the TinyMCE Editor
179 *
180 * @since 1.5.0
181 *
182 * @param array $buttons Buttons.
183 * @return array Buttons
184 */
185 public function register_tinymce_buttons( $buttons ) {
186
187 // Get shortcodes.
188 $shortcodes = convertkit_get_shortcodes();
189
190 // Bail if no shortcodes are available.
191 if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) {
192 return $buttons;
193 }
194
195 // Register each Shortcode as a TinyMCE Button.
196 foreach ( $shortcodes as $shortcode => $properties ) {
197 $buttons[] = 'convertkit_' . $shortcode;
198 }
199
200 return $buttons;
201
202 }
203
204 /**
205 * Outputs the QuickTags modal view in the footer of the site, which is
206 * used when using the Text editor button to insert a shortcode.
207 *
208 * @since 1.9.7.5
209 */
210 public function output_quicktags_modal() {
211
212 ?>
213 <script type="text/template" id="tmpl-convertkit-quicktags-modal">
214 <div id="convertkit-quicktags-modal">
215 <div class="media-frame-title"><h1></h1></div>
216 <div class="media-frame-content"></div>
217 <div class="media-frame-toolbar">
218 <div class="media-toolbar">
219 <div class="media-toolbar-secondary">
220 <button type="button" class="button button-large cancel"><?php esc_html_e( 'Cancel', 'convertkit' ); ?></button>
221 </div>
222 <div class="media-toolbar-primary">
223 <button type="button" class="button button-primary button-large"><?php esc_html_e( 'Insert', 'convertkit' ); ?></button>
224 </div>
225 </div>
226 </div>
227 </div>
228 </script>
229 <?php
230
231 }
232
233 }
234