PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.7.5
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.7.5
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.7.5, at admin/class-convertkit-admin-tinymce.php

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