-
Notifications
You must be signed in to change notification settings - Fork 815
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subscriptions Block: Stop saving localized attributes defaults in the…
… block content (#16361) When a block is created in English and the site switched to a second language before the block is migrated, the block will be deemed invalid. This change will attempt to fall back to English defaults for the placeholder text in the saved shortcode that causes the invalidation. This will not change migrated attributes only the generated content from the deprecated block save function. It also will only cover blocks first created in English and using default text. If the block was created in a second language, there is no means to determine the language and therefore the default text for that translation to fall back to. Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
- Loading branch information
1 parent
1172af4
commit 01eeddd
Showing
6 changed files
with
283 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
extensions/blocks/subscriptions/deprecated/v3/attributes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export default { | ||
subscribePlaceholder: { | ||
type: 'string', | ||
default: __( 'Enter your email address', 'jetpack' ), | ||
}, | ||
showSubscribersTotal: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
buttonOnNewLine: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
submitButtonText: { | ||
type: 'string', | ||
default: __( 'Sign Up', 'jetpack' ), | ||
}, | ||
emailFieldBackgroundColor: { | ||
type: 'string', | ||
}, | ||
customEmailFieldBackgroundColor: { | ||
type: 'string', | ||
}, | ||
emailFieldGradient: { | ||
type: 'string', | ||
}, | ||
customEmailFieldGradient: { | ||
type: 'string', | ||
}, | ||
buttonBackgroundColor: { | ||
type: 'string', | ||
}, | ||
customButtonBackgroundColor: { | ||
type: 'string', | ||
}, | ||
buttonGradient: { | ||
type: 'string', | ||
}, | ||
customButtonGradient: { | ||
type: 'string', | ||
}, | ||
textColor: { | ||
type: 'string', | ||
}, | ||
customTextColor: { | ||
type: 'string', | ||
}, | ||
fontSize: { | ||
type: 'number', | ||
}, | ||
customFontSize: { | ||
type: 'number', | ||
}, | ||
borderRadius: { | ||
type: 'number', | ||
}, | ||
borderWeight: { | ||
type: 'number', | ||
}, | ||
borderColor: { | ||
type: 'string', | ||
}, | ||
customBorderColor: { | ||
type: 'string', | ||
}, | ||
padding: { | ||
type: 'number', | ||
}, | ||
spacing: { | ||
type: 'number', | ||
}, | ||
}; |
143 changes: 143 additions & 0 deletions
143
extensions/blocks/subscriptions/deprecated/v3/get-subscriptions-shortcode.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { RawHTML } from '@wordpress/element'; | ||
import { | ||
getColorClassName, | ||
__experimentalGetGradientClass as getGradientClass, | ||
getFontSizeClass, | ||
} from '@wordpress/block-editor'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import definedAttributes from './attributes'; | ||
import { | ||
DEFAULT_BORDER_RADIUS_VALUE, | ||
DEFAULT_BORDER_WEIGHT_VALUE, | ||
DEFAULT_PADDING_VALUE, | ||
DEFAULT_SPACING_VALUE, | ||
DEFAULT_FONTSIZE_VALUE, | ||
} from '../../constants'; | ||
|
||
export default function getSubscriptionsShortcode( className, attributes, checkTextDefaults = null ) { | ||
const { | ||
subscribePlaceholder, | ||
showSubscribersTotal, | ||
buttonOnNewLine, | ||
submitButtonText, | ||
emailFieldBackgroundColor, | ||
customEmailFieldBackgroundColor, | ||
emailFieldGradient, | ||
customEmailFieldGradient, | ||
buttonBackgroundColor, | ||
customButtonBackgroundColor, | ||
buttonGradient, | ||
customButtonGradient, | ||
textColor, | ||
customTextColor, | ||
fontSize, | ||
customFontSize, | ||
borderRadius, | ||
borderWeight, | ||
borderColor, | ||
customBorderColor, | ||
padding, | ||
spacing, | ||
} = attributes; | ||
|
||
const isGradientAvailable = !! getGradientClass; | ||
|
||
const textColorClass = getColorClassName( 'color', textColor ); | ||
const fontSizeClass = getFontSizeClass( fontSize ); | ||
const borderClass = getColorClassName( 'border-color', borderColor ); | ||
const buttonBackgroundClass = getColorClassName( 'background-color', buttonBackgroundColor ); | ||
const buttonGradientClass = isGradientAvailable ? getGradientClass( buttonGradient ) : undefined; | ||
|
||
const emailFieldBackgroundClass = getColorClassName( | ||
'background-color', | ||
emailFieldBackgroundColor | ||
); | ||
const emailFieldGradientClass = isGradientAvailable | ||
? getGradientClass( emailFieldGradient ) | ||
: undefined; | ||
|
||
const sharedClasses = classnames( | ||
borderRadius === 0 ? 'no-border-radius' : undefined, | ||
fontSizeClass, | ||
borderClass | ||
); | ||
|
||
const submitButtonClasses = classnames( | ||
sharedClasses, | ||
textColor ? 'has-text-color' : undefined, | ||
textColorClass, | ||
buttonBackgroundColor || buttonGradient ? 'has-background' : undefined, | ||
buttonBackgroundClass, | ||
buttonGradientClass | ||
); | ||
|
||
const emailFieldClasses = classnames( | ||
sharedClasses, | ||
emailFieldBackgroundClass, | ||
emailFieldGradientClass | ||
); | ||
|
||
const emailFieldBackgroundStyle = | ||
! emailFieldBackgroundClass && customEmailFieldGradient | ||
? customEmailFieldGradient | ||
: customEmailFieldBackgroundColor; | ||
|
||
const buttonBackgroundStyle = | ||
! buttonBackgroundClass && customButtonGradient | ||
? customButtonGradient | ||
: customButtonBackgroundColor; | ||
|
||
const getBlockClassName = () => { | ||
return classnames( | ||
className, | ||
'wp-block-jetpack-subscriptions__supports-newline', | ||
buttonOnNewLine ? 'wp-block-jetpack-subscriptions__use-newline' : undefined, | ||
showSubscribersTotal ? 'wp-block-jetpack-subscriptions__show-subs' : undefined | ||
); | ||
}; | ||
|
||
let placeholderText = subscribePlaceholder; | ||
let buttonText = submitButtonText; | ||
|
||
if ( 'check-text-defaults' === checkTextDefaults ) { | ||
placeholderText = subscribePlaceholder === definedAttributes.subscribePlaceholder.default | ||
? 'Enter your email address' | ||
: subscribePlaceholder; | ||
buttonText = submitButtonText === definedAttributes.submitButtonText.default | ||
? 'Sign Up' | ||
: submitButtonText; | ||
} | ||
|
||
return ( | ||
<div className={ getBlockClassName() }> | ||
<RawHTML> | ||
{ ` | ||
[jetpack_subscription_form | ||
subscribe_placeholder="${ placeholderText }" | ||
show_subscribers_total="${ showSubscribersTotal }" | ||
button_on_newline="${ buttonOnNewLine }" | ||
submit_button_text="${ buttonText }" | ||
custom_background_emailfield_color="${ emailFieldBackgroundStyle }" | ||
custom_background_button_color="${ buttonBackgroundStyle }" | ||
custom_text_button_color="${ customTextColor }" | ||
custom_font_size="${ customFontSize || DEFAULT_FONTSIZE_VALUE }" | ||
custom_border_radius="${ borderRadius || DEFAULT_BORDER_RADIUS_VALUE }" | ||
custom_border_weight="${ borderWeight || DEFAULT_BORDER_WEIGHT_VALUE }" | ||
custom_border_color="${ customBorderColor }" | ||
custom_padding="${ padding || DEFAULT_PADDING_VALUE }" | ||
custom_spacing="${ spacing || DEFAULT_SPACING_VALUE }" | ||
submit_button_classes="${ submitButtonClasses }" | ||
email_field_classes="${ emailFieldClasses }" | ||
show_only_email_and_button="true" | ||
]` } | ||
</RawHTML> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import definedAttributes from './attributes'; | ||
import getSubscriptionsShortcode from './get-subscriptions-shortcode'; | ||
|
||
export default { | ||
attributes: definedAttributes, | ||
save: ( { className, attributes } ) => getSubscriptionsShortcode( className, attributes ), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import definedAttributes from '../v3/attributes'; | ||
import getSubscriptionsShortcode from '../v3/get-subscriptions-shortcode'; | ||
|
||
export default { | ||
attributes: definedAttributes, | ||
save: ( { className, attributes } ) => | ||
getSubscriptionsShortcode( className, attributes, 'check-text-defaults' ), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters