PluginProbe
Customify / 2.10.0
Customify v2.10.0
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / includes / class-customify-classic-editor.php

class-customify-classic-editor.php in Customify 2.10.0, at includes/class-customify-classic-editor.php

253 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This is the class that handles the overall logic for integration with the classic editor (TinyMCE).
4 *
5 * @see https://pixelgrade.com
6 * @author Pixelgrade
7 * @since 2.7.0
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 if ( ! class_exists( 'Customify_Classic_Editor' ) ) {
15
16 class Customify_Classic_Editor {
17
18 /**
19 * Holds the only instance of this class.
20 * @var null|Customify_Classic_Editor
21 * @access protected
22 * @since 2.7.0
23 */
24 protected static $_instance = null;
25
26 /**
27 * Constructor.
28 *
29 * @since 2.7.0
30 */
31 protected function __construct() {
32 // We will initialize the logic after the plugin has finished with it's configuration (at priority 15).
33 add_action( 'init', array( $this, 'init' ), 15 );
34 }
35
36 /**
37 * Initialize this module.
38 *
39 * @since 2.7.0
40 */
41 public function init() {
42
43 // Hook up.
44 $this->add_hooks();
45 }
46
47 /**
48 * Initiate our hooks
49 *
50 * @since 2.7.0
51 */
52 public function add_hooks() {
53
54 add_action( 'admin_enqueue_scripts', array( $this, 'script_to_add_customizer_settings_into_wp_editor' ), 10, 1 );
55 }
56
57 /**
58 * Add our customizer styling edits into the wp_editor
59 */
60 function script_to_add_customizer_settings_into_wp_editor() {
61 $current_screen = get_current_screen();
62 // Bail if setting unchecked, if using the block editor,
63 // or we are not on an admin page that might have editors (something related to posts, at the moment).
64 if ( ! PixCustomifyPlugin()->settings->get_plugin_setting( 'enable_editor_style', true )
65 || ! in_array( $current_screen->base, ['post'] )
66 || ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() )
67 ) {
68 return;
69 }
70
71 $script = $this->get_fonts_editor_dynamic_script();
72 if ( ! empty( $script ) ) {
73 // Make sure the the script is enqueued in the footer. We want all the DOM to be loaded and need jQuery.
74 wp_deregister_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader' );
75 wp_register_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader',
76 plugins_url( 'js/vendor/webfontloader-1-6-28.min.js', PixCustomifyPlugin()->get_file() ), array('jquery'), null, true );
77 wp_enqueue_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader' );
78 wp_add_inline_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader', $script );
79 }
80
81 ob_start();
82
83 PixCustomify_Customizer::instance()->output_dynamic_style();
84 Customify_Fonts_Global::instance()->outputFontsDynamicStyle();
85
86 $custom_output = ob_get_clean();
87
88 ob_start(); ?>
89 (function ($) {
90 $(window).on('load',function () {
91 /**
92 * @param iframe_id the id of the frame you want to append the style
93 * @param style_element the style element you want to append - boooom
94 */
95 const append_script_to_iframe = function (ifrm_id, scriptEl) {
96 var myIframe = document.getElementById(ifrm_id);
97
98 var script = myIframe.contentWindow.document.createElement("script");
99 script.type = "text/javascript";
100 if (scriptEl.getAttribute("src")) { script.src = scriptEl.getAttribute("src"); }
101 script.innerHTML = scriptEl.innerHTML;
102
103 myIframe.contentWindow.document.head.appendChild(script);
104 };
105
106 const append_style_to_iframe = function (ifrm_id, styleElement) {
107 var ifrm = window.frames[ifrm_id];
108 if ( typeof ifrm === "undefined" ) {
109 return;
110 }
111 ifrm = ( ifrm.contentDocument || ifrm.contentDocument || ifrm.document );
112 var head = ifrm.getElementsByTagName('head')[0];
113
114 if (typeof styleElement !== "undefined") {
115 head.appendChild(styleElement);
116 }
117 };
118
119 const xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_output ) ); ?>,
120 parser = new DOMParser();
121
122 $('.mce-edit-area iframe').each(function(idx, iframe) {
123 if (typeof iframe.id !== 'undefined' ) {
124 const doc = parser.parseFromString(xmlString, "text/html");
125 $.each(doc.head.childNodes, function (key, el) {
126 if (typeof el !== "undefined" && typeof el.tagName !== "undefined") {
127
128 switch (el.tagName) {
129 case 'STYLE' :
130 append_style_to_iframe(iframe.id, el);
131 break;
132 case 'SCRIPT' :
133 append_script_to_iframe(iframe.id, el);
134 break;
135 default:
136 break;
137 }
138 }
139 });
140 }
141 })
142 });
143 })(jQuery);
144 <?php
145 $script = ob_get_clean();
146 wp_add_inline_script( 'editor', $script );
147
148 }
149
150 protected function get_fonts_editor_dynamic_script() {
151 // If typography has been deactivated from the settings, bail.
152 if ( ! PixCustomifyPlugin()->settings->get_plugin_setting( 'typography', '1' ) ) {
153 return '';
154 }
155
156 $args = Customify_Fonts_Global::instance()->getFontFamiliesDetailsForWebfontloader();
157
158 if ( empty ( $args['custom_families'] ) && empty ( $args['google_families'] ) ) {
159 return '';
160 }
161
162 ob_start(); ?>
163 (function ($) { $(window).on('load',function () {
164 const customifyIframeFontLoader = function(context) {
165 const webfontargs = {
166 classes: true,
167 events: true,
168 loading: function() {
169 $( window ).trigger( 'wf-loading' );
170 },
171 active: function() {
172 $( window ).trigger( 'wf-active' );
173 },
174 inactive: function() {
175 $( window ).trigger( 'wf-inactive' );
176 },
177 context: context
178 };
179 <?php if ( ! empty( $args['google_families'] ) ) { ?>
180 webfontargs.google = {
181 families: [<?php echo join( ',', $args['google_families'] ); ?>]
182 };
183 <?php }
184 $custom_families = array();
185 $custom_urls = array();
186
187 if ( ! empty( $args['custom_families'] ) && ! empty( $args['custom_srcs'] ) ) {
188 $custom_families += $args['custom_families'];
189 $custom_urls += $args['custom_srcs'];
190 }
191
192 if ( ! empty( $custom_families ) && ! empty( $custom_urls ) ) { ?>
193 webfontargs.custom = {
194 families: [<?php echo join( ',', $custom_families ); ?>],
195 urls: [<?php echo join( ',', $custom_urls ) ?>]
196 };
197 <?php } ?>
198 WebFont.load(webfontargs);
199 };
200 if (typeof WebFont !== 'undefined') {
201 $('.mce-edit-area iframe').each(function(idx, el) {
202 if (typeof el.id !== 'undefined' ) {
203 customifyIframeFontLoader(frames[el.id].contentWindow)
204 }
205 })
206 }
207 }); })(jQuery);<?php
208 $output = ob_get_clean();
209
210 return apply_filters( 'customify_fonts_editor_webfont_script', $output );
211 }
212
213 /**
214 * Main Customify_Classic_Editor Instance
215 *
216 * Ensures only one instance of Customify_Classic_Editor is loaded or can be loaded.
217 *
218 * @return Customify_Classic_Editor Main Customify_Classic_Editor instance
219 * @since 2.7.0
220 * @static
221 *
222 */
223 public static function instance() {
224
225 if ( is_null( self::$_instance ) ) {
226 self::$_instance = new self();
227 }
228
229 return self::$_instance;
230 }
231
232 /**
233 * Cloning is forbidden.
234 *
235 * @since 2.7.0
236 */
237 public function __clone() {
238
239 _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null );
240 }
241
242 /**
243 * Unserializing instances of this class is forbidden.
244 *
245 * @since 2.7.0
246 */
247 public function __wakeup() {
248
249 _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null );
250 }
251 }
252 }
253