Skip to content

Commit

Permalink
Merge pull request #1108 from Automattic/master
Browse files Browse the repository at this point in the history
Alpha release May 03
  • Loading branch information
dkoo authored May 3, 2023
2 parents f0ef39e + 58736a5 commit 622d11a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
24 changes: 18 additions & 6 deletions includes/class-newspack-popups-presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ public static function get_ras_presets() {
return $lists;
}

// Get override values if previewing a preset.
$override_values = filter_input( INPUT_GET, 'values', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY );

// Populate prompt configs with saved inputs.
$data['prompts'] = array_map(
function( $prompt ) use ( $lists, $saved_inputs ) {
function( $prompt ) use ( $lists, $saved_inputs, $override_values ) {
// Check for saved inputs.
if ( ! empty( $saved_inputs[ $prompt['slug'] ] ) ) {
$fields = array_map(
Expand All @@ -145,11 +148,16 @@ function ( $field ) use ( $saved_inputs, $prompt ) {

// Populate placeholder content with saved inputs or default values.
foreach ( $prompt['user_input_fields'] as $field ) {
$override = ! empty( $override_values[ $field['name'] ] ) ? $override_values[ $field['name'] ] : null;
if ( 'array' === $field['type'] || 'string' === $field['type'] ) {
$prompt['content'] = self::process_user_inputs( $prompt['content'], $field );
$prompt['content'] = self::process_user_inputs( $prompt['content'], $field, $override );
}
if ( 'int' === $field['type'] && 'featured_image_id' === $field['name'] && isset( $field['value'] ) ) {
$prompt['featured_image_id'] = $field['value'];
if ( 'int' === $field['type'] && 'featured_image_id' === $field['name'] ) {
if ( ! empty( $override_values['featured_image_id'] ) ) {
$prompt['featured_image_id'] = $override_values['featured_image_id'];
} elseif ( isset( $field['value'] ) ) {
$prompt['featured_image_id'] = $field['value'];
}
}
}

Expand Down Expand Up @@ -202,16 +210,20 @@ function( $list ) {
* $field['default'] string Field default value. Required.
* $field['value'] string Field user input value.
* $field['max_length'] int Max length of string-type user input value.
* @param string $value Optional. Override value to use instead of $field['value'].
*
* @return string Prompt content with placeholders replaced.
*/
public static function process_user_inputs( $prompt_content, $field ) {
public static function process_user_inputs( $prompt_content, $field, $value = null ) {
if ( ! isset( $field['name'] ) || ! isset( $field['type'] ) || ! isset( $field['default'] ) ) {
return $prompt_content;
}

$field_name = $field['name'];
$value = isset( $field['value'] ) ? $field['value'] : $field['default'];

if ( ! $value ) {
$value = isset( $field['value'] ) ? $field['value'] : $field['default'];
}

// If a string, crop to max_length if set.
if ( 'string' === $field['type'] && isset( $field['max_length'] ) ) {
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"eslint": "^7.32.0",
"lint-staged": "^13.2.1",
"lint-staged": "^13.2.2",
"newspack-scripts": "^5.1.0",
"postcss-scss": "^4.0.6",
"prettier": "npm:wp-prettier@^2.2.1-beta-1",
Expand Down

0 comments on commit 622d11a

Please sign in to comment.