Skip to content

Commit

Permalink
Try to make it work in the Customizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Copons committed Jul 16, 2018
1 parent 72176ee commit 255c5e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 66 deletions.
69 changes: 5 additions & 64 deletions modules/widgets/simple-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ function __construct() {
);

global $pagenow;
if ( is_customize_preview() ) {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles_and_scripts' ) );
if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
/* add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles_and_scripts' ) );
add_filter( 'customize_refresh_nonces', array( $this, 'filter_nonces' ) );
} else if ( 'widgets.php' === $pagenow ) {
} else if ( 'widgets.php' === $pagenow ) {*/
add_action( 'admin_enqueue_scripts', array( $this, 'widgets_page_enqueue_styles_and_scripts' ) );
}
add_action( 'wp_ajax_customize-jetpack-simple-payments-buttons-get', array( $this, 'ajax_get_payment_buttons' ) );
//add_action( 'wp_ajax_customize-jetpack-simple-payments-buttons-get', array( $this, 'ajax_get_payment_buttons' ) );
add_action( 'wp_ajax_customize-jetpack-simple-payments-button-get', array( $this, 'ajax_get_payment_button' ) );
add_action( 'wp_ajax_customize-jetpack-simple-payments-button-save', array( $this, 'ajax_save_payment_button' ) );
add_action( 'wp_ajax_customize-jetpack-simple-payments-button-delete', array( $this, 'ajax_delete_payment_button' ) );
Expand Down Expand Up @@ -100,31 +100,10 @@ private function defaults() {
);
}

/**
* Adds a nonce for customizing menus.
*
* @param array $nonces Array of nonces.
* @return array $nonces Modified array of nonces.
*/
function filter_nonces( $nonces ) {
$nonces['customize-jetpack-simple-payments'] = wp_create_nonce( 'customize-jetpack-simple-payments' );
return $nonces;
}

function enqueue_style() {
wp_enqueue_style( 'jetpack-simple-payments-widget-style', plugins_url( 'simple-payments/style.css', __FILE__ ), array(), '20180518' );
}

function admin_enqueue_styles_and_scripts(){
wp_enqueue_style( 'jetpack-simple-payments-widget-customizer', plugins_url( 'simple-payments/customizer.css', __FILE__ ) );

wp_enqueue_media();
wp_enqueue_script( 'jetpack-simple-payments-widget-customizer', plugins_url( '/simple-payments/customizer.js', __FILE__ ), array( 'jquery' ), false, true );
wp_localize_script( 'jetpack-simple-payments-widget-customizer', 'jpSimplePaymentsStrings', array(
'deleteConfirmation' => __( 'Are you sure you want to delete this item? It will be disabled and removed from all locations where it currently appears.', 'jetpack' )
) );
}

function widgets_page_enqueue_styles_and_scripts() {
wp_enqueue_style(
'jetpack-simple-payments-widget-customizer',
Expand All @@ -150,39 +129,6 @@ function widgets_page_enqueue_styles_and_scripts() {
);
}

public function ajax_get_payment_buttons() {
if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) {
wp_send_json_error( 'bad_nonce', 400 );
}

if ( ! current_user_can( 'customize' ) ) {
wp_send_json_error( 'customize_not_allowed', 403 );
}

$post_type_object = get_post_type_object( Jetpack_Simple_Payments::$post_type_product );
if ( ! current_user_can( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->publish_posts ) ) {
wp_send_json_error( 'insufficient_post_permissions', 403 );
}

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

$formatted_products = array_map( array( $this, 'format_product_post_for_ajax_reponse' ), $product_posts );

wp_send_json_success( $formatted_products );
}

public function format_product_post_for_ajax_reponse( $product_post ) {
return array(
'ID' => $product_post->ID,
'post_title' => $product_post->post_title,
);
}

public function ajax_get_payment_button() {
if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) {
wp_send_json_error( 'bad_nonce', 400 );
Expand Down Expand Up @@ -537,12 +483,7 @@ function form( $instance ) {
'post_type' => Jetpack_Simple_Payments::$post_type_product,
'post_status' => 'publish',
) );

if ( is_customize_preview() ) {
require( dirname( __FILE__ ) . '/simple-payments/form.php' );
} else {
require( dirname( __FILE__ ) . '/simple-payments/form-widgets-page.php' );
}
require( dirname( __FILE__ ) . '/simple-payments/form-widgets-page.php' );
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/widgets/simple-payments/form-widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class="jetpack-simple-payments-select-image"

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'form_product_description' ) ); ?>">
<?php esc_html_e( 'Description:', jetpack ); ?>
<?php esc_html_e( 'Description:', 'jetpack' ); ?>
</label>
<textarea
class="widefat jetpack-simple-payments-form-product-description"
Expand Down Expand Up @@ -177,7 +177,7 @@ class="field-email widefat jetpack-simple-payments-form-product-email"
/>
<small>
<?php printf( esc_html__(
"This is where PayPal will send your money. To claim a payment, you'll need a %1$sPayPal account%2$s connected to a bank account." ),
'This is where PayPal will send your money. To claim a payment, you\'ll need a %1$sPayPal account%2$s connected to a bank account.' ),
'<a href="https://paypal.com" target="_blank">',
'</a>'
) ?>
Expand Down
1 change: 1 addition & 0 deletions modules/widgets/simple-payments/widgets-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
$( 'option[value="' + data.product_post_id + '"]', $selector ).remove();
break;
}
$selector.change();
}

// Clear the image of a product form.
Expand Down

0 comments on commit 255c5e2

Please sign in to comment.