Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect google font loading through postMessage module #1988

Closed
NazarkinRoman opened this issue Sep 22, 2018 · 2 comments
Closed

Incorrect google font loading through postMessage module #1988

NazarkinRoman opened this issue Sep 22, 2018 · 2 comments

Comments

@NazarkinRoman
Copy link

NazarkinRoman commented Sep 22, 2018

Issue description:

When I use typography control with transport set to auto each time I select new font it loads with following code from modules/postmessage/class-kirki-modules-postmessage.php:

/**
 * Processes script generation for typography fields.
 *
 * @access protected
 * @since 3.0.0
 * @param array $args  The arguments for this js_var.
 * @param array $field The field arguments.
 */
protected function script_var_typography( $args, $field ) {

	$args = $this->get_args( $args );

	$script = '';
	$css    = '';

	// Load the font using WenFontloader.
	// This is a bit ugly because wp_add_inline_script doesn't allow adding <script> directly.
	$webfont_loader = 'sc=\'a\';jQuery(\'head\').append(sc.replace(\'a\',\'<\')+\'script>if(!_.isUndefined(WebFont)&&fontFamily){WebFont.load({google:{families:["\'+fontFamily.replace( /\"/g, \'&quot;\' )+\':\'+variant+\'cyrillic,cyrillic-ext,devanagari,greek,greek-ext,khmer,latin,latin-ext,vietnamese,hebrew,arabic,bengali,gujarati,tamil,telugu,thai"]}});}\'+sc.replace(\'a\',\'<\')+\'/script>\');';

	// Add the css.
	$css_build_array  = array(
		'font-family'     => 'fontFamily',
		'font-size'       => 'fontSize',
		'line-height'     => 'lineHeight',
		'letter-spacing'  => 'letterSpacing',
		'word-spacing'    => 'wordSpacing',
		'text-align'      => 'textAlign',
		'text-transform'  => 'textTransform',
		'text-decoration' => 'textDecoration',
		'color'           => 'color',
		'font-weight'     => 'fontWeight',
		'font-style'      => 'fontStyle',
	);
	$choice_condition = ( isset( $args['choice'] ) && '' !== $args['choice'] && isset( $css_build_array[ $args['choice'] ] ) );
	$script          .= ( ! $choice_condition ) ? $webfont_loader : '';
	foreach ( $css_build_array as $property => $var ) {
		if ( $choice_condition && $property !== $args['choice'] ) {
			continue;
		}
		// Fixes https://github.com/aristath/kirki/issues/1436.
		if ( ! isset( $field['default'] ) || (
			( 'font-family' === $property && ! isset( $field['default']['font-family'] ) ) ||
			( 'font-size' === $property && ! isset( $field['default']['font-size'] ) ) ||
			( 'line-height' === $property && ! isset( $field['default']['line-height'] ) ) ||
			( 'letter-spacing' === $property && ! isset( $field['default']['letter-spacing'] ) ) ||
			( 'word-spacing' === $property && ! isset( $field['default']['word-spacing'] ) ) ||
			( 'text-align' === $property && ! isset( $field['default']['text-align'] ) ) ||
			( 'text-transform' === $property && ! isset( $field['default']['text-transform'] ) ) ||
			( 'text-decoration' === $property && ! isset( $field['default']['text-decoration'] ) ) ||
			( 'color' === $property && ! isset( $field['default']['color'] ) ) ||
			( 'font-weight' === $property && ! isset( $field['default']['variant'] ) && ! isset( $field['default']['font-weight'] ) ) ||
			( 'font-style' === $property && ! isset( $field['default']['variant'] ) && ! isset( $field['default']['font-style'] ) )
			) ) {
			continue;
		}
		$script .= ( $choice_condition && 'font-family' === $args['choice'] ) ? $webfont_loader : '';

		if ( 'font-family' === $property || ( isset( $args['choice'] ) && 'font-family' === $args['choice'] ) ) {
			$css .= 'fontFamilyCSS=fontFamily;if(0<fontFamily.indexOf(\' \')&&-1===fontFamily.indexOf(\'"\')){fontFamilyCSS=\'"\'+fontFamily+\'"\';}';
			$var  = 'fontFamilyCSS';
		}
		$var  = ( ( empty( $args['prefix'] ) ) ? '' : '\'' . $args['prefix'] . '\'+' ) . $var . ( ( empty( $args['units'] ) ) ? '' : '+\'' . $args['units'] . '\'' ) . ( ( empty( $args['suffix'] ) ) ? '' : '+\'' . $args['suffix'] . '\'' );
		$css .= 'css+=(\'\'!==' . $var . ')?\'' . $args['element'] . '\'+\'{' . $property . ':\'+' . $var . '+\';}\':\'\';';
	}

	$script .= $css;
	if ( isset( $args['media_query'] ) ) {
		$script .= 'css=\'' . $args['media_query'] . '{\'+css+\'}\';';
	}
	return array(
		'script' => $script,
		'css'    => 'css',
	);
}

But here is a small mistype in webfont loader call: missed semicolon. Just check out screenshot:
devtools - asra-wordpress wp-admin customize php return 2fwp-admin 2fplugins php 2018-09-22 20-54-50

Version used:

3.0.34.1

@niklasp
Copy link

niklasp commented Nov 21, 2018

can be labeled "bug" possibly #2026

@aristath
Copy link
Contributor

Fixed, will be included in 3.0.35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants