Skip to content

Commit

Permalink
Customizer: Simple Payments Widget breaks when starting without produ…
Browse files Browse the repository at this point in the history
…cts (#9809)
  • Loading branch information
rodrigoi authored Jun 23, 2018
1 parent f9a214c commit ad2ad4c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
45 changes: 24 additions & 21 deletions modules/widgets/simple-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ function __construct() {
*/
private function defaults() {
$current_user = wp_get_current_user();
$default_product_id = $this->get_first_product_id();

return array(
'title' => '',
'product_post_id' => 0,
'product_post_id' => $default_product_id,
'form_action' => '',
'form_product_id' => 0,
'form_product_title' => '',
Expand Down Expand Up @@ -232,6 +233,18 @@ public function validate_ajax_params( $params ) {

return $errors;
}

function get_first_product_id() {
$product_posts = get_posts( array(
'numberposts' => 1,
'orderby' => 'date',
'post_type' => Jetpack_Simple_Payments::$post_type_product,
'post_status' => 'publish',
) );

return ! empty( $product_posts ) ? $product_posts[0]->ID : null;
}

/**
* Front-end display of widget.
*
Expand All @@ -256,26 +269,14 @@ function widget( $args, $instance ) {
if ( ! empty( $instance['form_action'] ) && in_array( $instance['form_action'], array( 'add', 'edit' ) ) && is_customize_preview() ) {
require( dirname( __FILE__ ) . '/simple-payments/widget.php' );
} else {
if ( ! empty( $instance['product_post_id'] ) ) {
$attrs = array( 'id' => $instance['product_post_id'] );
} else {
$product_posts = get_posts( array(
'numberposts' => 1,
'orderby' => 'date',
'post_type' => Jetpack_Simple_Payments::$post_type_product,
'post_status' => 'publish',
) );

$attrs = array( 'id' => $product_posts[0]->ID );
}

$jsp = Jetpack_Simple_Payments::getInstance();
$simple_payments_button = $jsp->parse_shortcode( $attrs );
if ( is_null( $simple_payments_button ) && ! is_customize_preview() ) {
return;
}
$simple_payments_button = $jsp->parse_shortcode( array(
'id' => $instance['product_post_id'],
) );

echo $simple_payments_button;
if ( ! is_null( $simple_payments_button ) || is_customize_preview() ) {
echo $simple_payments_button;
}
}

echo '</div><!--simple-payments-->';
Expand Down Expand Up @@ -339,8 +340,10 @@ private function get_product_from_post( $product_post_id ) {
* @return array Updated safe values to be saved.
*/
function update( $new_instance, $old_instance ) {
$new_instance = wp_parse_args( $new_instance, $this->defaults() );
$old_instance = wp_parse_args( $old_instance, $this->defaults() );
$defaults = $this->defaults();
//do not overrite `product_post_id` for `$new_instance` with the defaults
$new_instance = wp_parse_args( $new_instance, array_diff_key( $defaults, array( 'product_post_id' => 0 ) ) );
$old_instance = wp_parse_args( $old_instance, $defaults );

$required_widget_props = array(
'title' => $this->get_latest_field_value( $new_instance, $old_instance, 'title' ),
Expand Down
10 changes: 10 additions & 0 deletions modules/widgets/simple-payments/customizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@
);
select.val( data.product_post_id ).change();
}

widgetForm.find( '.jetpack-simple-payments-products-fieldset' ).show();
widgetForm.find( '.jetpack-simple-payments-products-warning' ).hide();

changeFormAction( widgetForm, 'clear' );
hideForm( widgetForm );
} );
Expand Down Expand Up @@ -286,6 +290,12 @@
var productList = widgetForm.find( 'select.jetpack-simple-payments-products' )[ 0 ];
productList.remove( productList.selectedIndex );
productList.dispatchEvent( new Event( 'change' ) );

if ( widgetForm.find( 'select.jetpack-simple-payments-products' ).has( 'option' ).length === 0 ) {
widgetForm.find( '.jetpack-simple-payments-products-fieldset' ).hide();
widgetForm.find( '.jetpack-simple-payments-products-warning' ).show();
}

changeFormAction( widgetForm, 'clear' );
hideForm( widgetForm );
} );
Expand Down
9 changes: 5 additions & 4 deletions modules/widgets/simple-payments/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ class="widefat jetpack-simple-payments-widget-title"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
value="<?php echo esc_attr( $instance['title'] ); ?>" />
</p>
<?php if ( ! empty( $product_posts ) ) { ?>
<p>
<p class="jetpack-simple-payments-products-warning" <?php if ( ! empty( $product_posts ) ) { echo 'style="display:none;"'; } ?>>
<?php echo __( 'Looks like you don\'t have any products. You can create one using the Add New button below.' ) ?>
</p>
<p class="jetpack-simple-payments-products-fieldset" <?php if ( empty( $product_posts ) ) { echo 'style="display:none;"'; } ?>>
<label for="<?php echo $this->get_field_id('product_post_id'); ?>"><?php _e( 'Select a Simple Payment Button:', 'jetpack' ); ?></label>
<select
class="widefat jetpack-simple-payments-products"
id="<?php echo $this->get_field_id('product_post_id'); ?>"
name="<?php echo $this->get_field_name('product_post_id'); ?>">
<?php foreach ( $product_posts as $product_post ) { ?>
<option value="<?php echo esc_attr( $product_post->ID ) ?>"<?php selected( (int) $instance['product_post_id'], $product_post->ID ); ?>>
<option value="<?php echo esc_attr( $product_post->ID ) ?>" <?php selected( (int) $instance['product_post_id'], $product_post->ID ); ?>>
<?php echo esc_attr( get_the_title( $product_post ) ) ?>
</option>
<?php } ?>
</select>
</p>
<?php } ?>
<p>
<div class="alignleft">
<button class="button jetpack-simple-payments-edit-product"><?php esc_html_e( 'Edit Selected' ); ?></button>
Expand Down

0 comments on commit ad2ad4c

Please sign in to comment.