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