PluginProbe
Customify / 2.7.0
Customify v2.7.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.7.0, at includes/class-customify-classic-editor.php

251 lines 6.8 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 // Bail if setting unchecked, if using the block editor,
62 // or we are not on an admin page that might have editors (something related to posts, at the moment).
63 if ( ! PixCustomifyPlugin()->settings->get_plugin_setting( 'enable_editor_style', true )
64 || get_current_screen()->is_block_editor()
65 || ! in_array( get_current_screen()->base, ['post'] ) ) {
66 return;
67 }
68
69 $script = $this->get_fonts_editor_dynamic_script();
70 if ( ! empty( $script ) ) {
71 // Make sure the the script is enqueued in the footer. We want all the DOM to be loaded and need jQuery.
72 wp_deregister_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader' );
73 wp_register_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader',
74 plugins_url( 'js/vendor/webfontloader-1-6-28.js', PixCustomifyPlugin()->get_file() ), array('jquery'), null, true );
75 wp_enqueue_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader' );
76 wp_add_inline_script( PixCustomifyPlugin()->get_slug() . '-web-font-loader', $script );
77 }
78
79 ob_start();
80
81 PixCustomify_Customizer::instance()->output_dynamic_style();
82 Customify_Fonts_Global::instance()->output_fonts_dynamic_style();
83
84 $custom_output = ob_get_clean();
85
86 ob_start(); ?>
87 (function ($) {
88 $(window).on('load',function () {
89 /**
90 * @param iframe_id the id of the frame you want to append the style
91 * @param style_element the style element you want to append - boooom
92 */
93 const append_script_to_iframe = function (ifrm_id, scriptEl) {
94 var myIframe = document.getElementById(ifrm_id);
95
96 var script = myIframe.contentWindow.document.createElement("script");
97 script.type = "text/javascript";
98 if (scriptEl.getAttribute("src")) { script.src = scriptEl.getAttribute("src"); }
99 script.innerHTML = scriptEl.innerHTML;
100
101 myIframe.contentWindow.document.head.appendChild(script);
102 };
103
104 const append_style_to_iframe = function (ifrm_id, styleElement) {
105 var ifrm = window.frames[ifrm_id];
106 if ( typeof ifrm === "undefined" ) {
107 return;
108 }
109 ifrm = ( ifrm.contentDocument || ifrm.contentDocument || ifrm.document );
110 var head = ifrm.getElementsByTagName('head')[0];
111
112 if (typeof styleElement !== "undefined") {
113 head.appendChild(styleElement);
114 }
115 };
116
117 const xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_output ) ); ?>,
118 parser = new DOMParser();
119
120 $('.mce-edit-area iframe').each(function(idx, iframe) {
121 if (typeof iframe.id !== 'undefined' ) {
122 const doc = parser.parseFromString(xmlString, "text/html");
123 $.each(doc.head.childNodes, function (key, el) {
124 if (typeof el !== "undefined" && typeof el.tagName !== "undefined") {
125
126 switch (el.tagName) {
127 case 'STYLE' :
128 append_style_to_iframe(iframe.id, el);
129 break;
130 case 'SCRIPT' :
131 append_script_to_iframe(iframe.id, el);
132 break;
133 default:
134 break;
135 }
136 }
137 });
138 }
139 })
140 });
141 })(jQuery);
142 <?php
143 $script = ob_get_clean();
144 wp_add_inline_script( 'editor', $script );
145
146 }
147
148 protected function get_fonts_editor_dynamic_script() {
149 // If typography has been deactivated from the settings, bail.
150 if ( ! PixCustomifyPlugin()->settings->get_plugin_setting( 'typography', '1' ) ) {
151 return '';
152 }
153
154 $args = Customify_Fonts_Global::instance()->get_font_families_details_for_webfontloader();
155
156 if ( empty ( $args['custom_families'] ) && empty ( $args['google_families'] ) ) {
157 return '';
158 }
159
160 ob_start(); ?>
161 (function ($) { $(window).on('load',function () {
162 const customifyIframeFontLoader = function(context) {
163 const webfontargs = {
164 classes: true,
165 events: true,
166 loading: function() {
167 $( window ).trigger( 'wf-loading' );
168 },
169 active: function() {
170 $( window ).trigger( 'wf-active' );
171 },
172 inactive: function() {
173 $( window ).trigger( 'wf-inactive' );
174 },
175 context: context
176 };
177 <?php if ( ! empty( $args['google_families'] ) ) { ?>
178 webfontargs.google = {
179 families: [<?php echo join( ',', $args['google_families'] ); ?>]
180 };
181 <?php }
182 $custom_families = array();
183 $custom_urls = array();
184
185 if ( ! empty( $args['custom_families'] ) && ! empty( $args['custom_srcs'] ) ) {
186 $custom_families += $args['custom_families'];
187 $custom_urls += $args['custom_srcs'];
188 }
189
190 if ( ! empty( $custom_families ) && ! empty( $custom_urls ) ) { ?>
191 webfontargs.custom = {
192 families: [<?php echo join( ',', $custom_families ); ?>],
193 urls: [<?php echo join( ',', $custom_urls ) ?>]
194 };
195 <?php } ?>
196 WebFont.load(webfontargs);
197 };
198 if (typeof WebFont !== 'undefined') {
199 $('.mce-edit-area iframe').each(function(idx, el) {
200 if (typeof el.id !== 'undefined' ) {
201 customifyIframeFontLoader(frames[el.id].contentWindow)
202 }
203 })
204 }
205 }); })(jQuery);<?php
206 $output = ob_get_clean();
207
208 return apply_filters( 'customify_fonts_editor_webfont_script', $output );
209 }
210
211 /**
212 * Main Customify_Classic_Editor Instance
213 *
214 * Ensures only one instance of Customify_Classic_Editor is loaded or can be loaded.
215 *
216 * @return Customify_Classic_Editor Main Customify_Classic_Editor instance
217 * @since 2.7.0
218 * @static
219 *
220 */
221 public static function instance() {
222
223 if ( is_null( self::$_instance ) ) {
224 self::$_instance = new self();
225 }
226
227 return self::$_instance;
228 }
229
230 /**
231 * Cloning is forbidden.
232 *
233 * @since 2.7.0
234 */
235 public function __clone() {
236
237 _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null );
238 }
239
240 /**
241 * Unserializing instances of this class is forbidden.
242 *
243 * @since 2.7.0
244 */
245 public function __wakeup() {
246
247 _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null );
248 }
249 }
250 }
251