Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from godaddy/develop
Develop
- Loading branch information
Showing
87 changed files
with
11,662 additions
and
3,904 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 )); |
Large diffs are not rendered by default.
Oops, something went wrong.
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,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(); | ||
} | ||
|
||
} |
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,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; | ||
|
||
} | ||
} |
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
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
Oops, something went wrong.