Skip to content

Commit

Permalink
Merge pull request #53 from godaddy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
bfocht committed Dec 1, 2017
2 parents 994504e + e00d78c commit 34bf820
Show file tree
Hide file tree
Showing 87 changed files with 11,662 additions and 3,904 deletions.
17 changes: 17 additions & 0 deletions assets/js/admin-notice.js
@@ -0,0 +1,17 @@
/* global ajaxurl, rstore_admin_notice, jQuery */

( function( $ ) {
'use strict';

$( document ).ready( function( $ ) {
$('#rstore-update-error').children( ['button.notice-dismiss'] ).click( function ( event ) {
var data = {
'action': 'rstore_dismiss_admin_notice',
'nonce': rstore_admin_notice.nonce
};
event.preventDefault();

$.post( ajaxurl, data );
} );
} );
}( jQuery ));
6 changes: 6 additions & 0 deletions assets/js/domain-search.min.js

Large diffs are not rendered by default.

142 changes: 142 additions & 0 deletions includes/class-admin-notices.php
@@ -0,0 +1,142 @@
<?php
/**
* GoDaddy Reseller Store admin notices.
*
* Display Reseller Store admin notices.
*
* @class Reseller_Store/Admin_Notices
* @package Reseller_Store/Plugin
* @category Class
* @author GoDaddy
* @since 1.3.0
*/

namespace Reseller_Store;

if ( ! defined( 'ABSPATH' ) ) {

// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}

final class Admin_Notices {

/**
* Class constructor.
*
* @since 1.3.0
*/
public function __construct() {

add_action( 'wp_ajax_rstore_dismiss_admin_notice', [ __CLASS__, 'dismiss_admin_notice' ] );

$is_post_type_screen = rstore_is_admin_uri( 'post_type=' . Post_Type::SLUG, false );
if ( ! $is_post_type_screen ) {
return;
}

$errors = rstore_get_option( 'errors', [] );

if ( count( $errors ) > 0 ) {

add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
add_action( 'admin_notices', [ $this, 'error_notices' ] );
}

if ( ! empty( $_REQUEST['bulk_restore_posts'] ) ) {
add_action( 'admin_notices', [ $this, 'restore_notice' ] );
}

}

/**
* Get the key used to generate the nonce.
*
* @return string Generated nonce key.
* @since 1.3.0
*/
private static function nonce_key() {
return rstore_prefix( 'notice-' . get_current_user_id() );
}

/**
* Enqueue admin scripts.
*
* @action admin_enqueue_scripts
* @since 1.3.0
*/
public function admin_enqueue_scripts() {

$suffix = SCRIPT_DEBUG ? '' : '.min';

wp_enqueue_script( 'rstore-admin-notice', Plugin::assets_url( "js/admin-notice{$suffix}.js" ), [ 'jquery' ], rstore()->version, true );

wp_localize_script(
'rstore-admin-notice',
'rstore_admin_notice',
array(
'nonce' => wp_create_nonce( self::nonce_key() ),
)
);

}

/**
* Display restore notices
*
* @since 1.3.0
*/
public function restore_notice() {

$count = intval( $_REQUEST['bulk_restore_posts'] );
printf(
'<div id="rstore-update-success" class="notice notice-success is-dismissible"><p>' .
/* translators: product count */
_n(
'Finished restoring products. %s product updated.',
'Finished restoring products. %s products updated.',
$count,
'reseller-store'
) . '</p></div>', $count
);
}

/**
* Display admin notices
*
* @since 1.3.0
*/
public function error_notices() {

$errors = rstore_get_option( 'errors', [] );

print( '<div id="rstore-update-error" class="notice notice-error is-dismissible">' );
foreach ( $errors as $error ) {
printf( '<p>' . $error->get_error_message() . '</p>', $error->get_error_data() );
}
print ( '</div>' );

}

/**
* Handles Ajax request to persist notices dismissal.
*
* @since 1.3.0
*/
public static function dismiss_admin_notice() {

if ( false === wp_verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ), self::nonce_key() ) ) {

$data = esc_html__( 'Sorry, you are not allowed to do that.', 'reseller-store' );

wp_send_json_error( $data );

}

rstore_delete_option( 'errors' );

wp_send_json_success();
}

}
164 changes: 164 additions & 0 deletions includes/class-bulk-restore.php
@@ -0,0 +1,164 @@
<?php
/**
* GoDaddy Reseller Store bulk edit class.
*
* Bulk restore the product data from the edit-post screen.
*
* @class Reseller_Store/Bulk_Edit
* @package Reseller_Store/Plugin
* @category Class
* @author GoDaddy
* @since 1.3.0
*/

namespace Reseller_Store;

if ( ! defined( 'ABSPATH' ) ) {

// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}

final class Bulk_Restore {

/**
* Class constructor.
*
* @since 1.3.0
*/
public function __construct() {

add_filter(
'bulk_actions-edit-' . Post_Type::SLUG, function ( $bulk_actions ) {
$bulk_actions['restore_product_data'] = esc_html__( 'Restore Product Data', 'reseller-store' );
return $bulk_actions;
}
);

add_filter( 'handle_bulk_actions-edit-' . Post_Type::SLUG, [ $this, 'bulk_action_handler' ], 10, 3 );

}

/**
* Action handler for bulk resetting product posts
*
* @since 1.3.0
*
* @param string $redirect_to The redirect URL.
* @param string $do_action The action being taken.
* @param array $post_ids The items to take the action on.
* @return mixed The url to redirect to after success.
*/
public function bulk_action_handler( $redirect_to, $do_action, $post_ids ) {
if ( 'restore_product_data' !== $do_action ) {
return $redirect_to;
}

$products = rstore_get_products( true );

if ( is_wp_error( $products ) ) {
rstore_error( $products );
return $redirect_to;
}

$success = 0;

foreach ( $post_ids as $post_id ) {

if ( ! current_user_can( 'edit_post', $post_id ) ) {

$error = new \WP_Error(
'invalid_permissions',
esc_html__( 'Current user cannot modify post.', 'reseller-store' )
);

rstore_error( $error );

continue;
}

$product_id = rstore_get_product_meta( $post_id, 'id' );

if ( false === $product_id ) {

$error = new \WP_Error(
'invalid_product_id',
/* translators: product name */
esc_html__( '`%s` does not have a valid product id.', 'reseller-store' ),
get_the_title( $post_id )
);

rstore_error( $error );

continue;
}

$product = $this->find_product( $product_id, $products );

if ( false === $product ) {

$error = new \WP_Error(
'invalid_product',
/* translators: product name */
esc_html__( '`%s` is not a valid product.', 'reseller-store' ),
get_the_title( $post_id )
);

rstore_error( $error );

continue;

}

$import = new Import( $product, $post_id );
$result = $import->import_product();

if ( is_wp_error( $result ) ) {

rstore_error( $result );

continue;

}

$success += 1;

}

if ( $success > 0 ) {
$redirect_to = add_query_arg( 'bulk_restore_posts', $success, $redirect_to );
}

return $redirect_to;
}

/**
* Find the product from the array by id.
*
* @since 1.3.0
*
* @param string $id The product id (i.e. the needle).
* @param array $products Array of products (i.e the haystack).
* @return bool|Product
*/
private function find_product( $id, $products ) {

foreach ( (array) $products as $product ) {

if ( $id === $product->id ) {

$item = new Product( $product );

if ( $item->is_valid() ) {

return $item;

}
}
}

return false;

}
}
5 changes: 3 additions & 2 deletions includes/class-import.php
Expand Up @@ -76,9 +76,10 @@ public function __construct( $product, $post_id = 0 ) {
*/
public function import_product() {

$fallback_id = ( is_a( $this->product, 'stdClass' ) && ! empty( $this->product->fields->id ) ) ? $this->product->fields->id : strtolower( esc_html__( 'unknown', 'reseller-store' ) );

if ( ! $this->product->is_valid() ) {

$fallback_id = ( is_a( $this->product, 'stdClass' ) && ! empty( $this->product->fields->id ) ) ? $this->product->fields->id : strtolower( esc_html__( 'unknown', 'reseller-store' ) );

return new WP_Error(
'invalid_product',
/* translators: product name */
Expand Down
13 changes: 11 additions & 2 deletions includes/class-post-type.php
Expand Up @@ -109,7 +109,6 @@ public function __construct() {
);

add_action( 'save_post', [ $this, 'republish_post' ] );

}

/**
Expand Down Expand Up @@ -531,9 +530,19 @@ public function append_add_to_cart_form( $content ) {

global $post;

if ( ! isset( $post ) ) {
return $content;
}

$is_rest_request = ( defined( 'REST_REQUEST' ) && REST_REQUEST );

if ( self::SLUG === $post->post_type && ! is_feed() && ! $is_rest_request && ! $post->rstore_widget ) {
if ( self::SLUG === $post->post_type && ! is_feed() && ! $is_rest_request ) {

if ( property_exists( $post, 'rstore_widget' ) && true === $post->rstore_widget ) {

return $content;

}

$content .= rstore_price( $post->ID, false );
$content .= rstore_add_to_cart_form( $post->ID, false );
Expand Down

0 comments on commit 34bf820

Please sign in to comment.