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

230 lines 7.1 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 Access Token is specified.
64 if ( array_key_exists( 'has_access_token', $shortcode ) && ! $shortcode['has_access_token'] ) {
65 $notice = $shortcode['no_access_token'];
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( '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 // Output Backbone View Template.
123 add_action( 'wp_print_footer_scripts', array( $this, 'output_quicktags_modal' ) );
124 add_action( 'admin_print_footer_scripts', array( $this, 'output_quicktags_modal' ) );
125
126 }
127
128 /**
129 * Register JS plugins for the TinyMCE Editor
130 *
131 * @since 1.5.0
132 *
133 * @param array $plugins JS Plugins.
134 * @return array JS Plugins
135 */
136 public function register_tinymce_plugins( $plugins ) {
137
138 // Get shortcodes.
139 $shortcodes = convertkit_get_shortcodes();
140
141 // Bail if no shortcodes are available.
142 if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) {
143 return $plugins;
144 }
145
146 // Enqueue TinyMCE CSS and JS.
147 wp_enqueue_script( 'convertkit-admin-tabs', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tabs.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
148 wp_enqueue_script( 'convertkit-admin-tinymce', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tinymce.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
149 wp_enqueue_script( 'convertkit-admin-modal', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/modal.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
150 wp_enqueue_style( 'convertkit-admin-tinymce', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/tinymce.css', array(), CONVERTKIT_PLUGIN_VERSION );
151
152 // Register JS variable convertkit_admin_tinymce.nonce for AJAX calls.
153 wp_localize_script(
154 'convertkit-admin-tinymce',
155 'convertkit_admin_tinymce',
156 array(
157 'nonce' => wp_create_nonce( 'convertkit_admin_tinymce' ),
158 )
159 );
160
161 // Make shortcodes available as convertkit_shortcodes JS variable.
162 wp_localize_script( 'convertkit-admin-tinymce', 'convertkit_shortcodes', $shortcodes );
163
164 // Register TinyMCE Javascript Plugin.
165 foreach ( $shortcodes as $shortcode => $properties ) {
166 $plugins[ 'convertkit_' . $shortcode ] = CONVERTKIT_PLUGIN_URL . 'resources/backend/js/tinymce-' . $shortcode . '.js';
167 }
168
169 return $plugins;
170
171 }
172
173 /**
174 * Registers buttons in the TinyMCE Editor
175 *
176 * @since 1.5.0
177 *
178 * @param array $buttons Buttons.
179 * @return array Buttons
180 */
181 public function register_tinymce_buttons( $buttons ) {
182
183 // Get shortcodes.
184 $shortcodes = convertkit_get_shortcodes();
185
186 // Bail if no shortcodes are available.
187 if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) {
188 return $buttons;
189 }
190
191 // Register each Shortcode as a TinyMCE Button.
192 foreach ( $shortcodes as $shortcode => $properties ) {
193 $buttons[] = 'convertkit_' . $shortcode;
194 }
195
196 return $buttons;
197
198 }
199
200 /**
201 * Outputs the QuickTags modal view in the footer of the site, which is
202 * used when using the Text editor button to insert a shortcode.
203 *
204 * @since 1.9.7.5
205 */
206 public function output_quicktags_modal() {
207
208 ?>
209 <script type="text/template" id="tmpl-convertkit-quicktags-modal">
210 <div id="convertkit-quicktags-modal">
211 <div class="media-frame-title"><h1></h1></div>
212 <div class="media-frame-content"></div>
213 <div class="media-frame-toolbar">
214 <div class="media-toolbar">
215 <div class="media-toolbar-secondary">
216 <button type="button" class="button button-large cancel"><?php esc_html_e( 'Cancel', 'convertkit' ); ?></button>
217 </div>
218 <div class="media-toolbar-primary">
219 <button type="button" class="button button-primary button-large"><?php esc_html_e( 'Insert', 'convertkit' ); ?></button>
220 </div>
221 </div>
222 </div>
223 </div>
224 </script>
225 <?php
226
227 }
228
229 }
230