| 1 |
<?php |
| 2 |
/** |
| 3 |
* Methods related to Ask for review notice. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Ask for review only after a specific number of orders have been received on the store. |
| 12 |
*/ |
| 13 |
class Orderable_Ask_Review { |
| 14 |
/** |
| 15 |
* Order count required to display the notice. |
| 16 |
* |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
private static $order_count_required = 20; |
| 20 |
|
| 21 |
/** |
| 22 |
* Run. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public static function run() { |
| 27 |
add_action( 'woocommerce_checkout_order_processed', array( __CLASS__, 'register_order' ), 10, 1 ); |
| 28 |
add_action( 'admin_notices', array( __CLASS__, 'show_notice' ) ); |
| 29 |
add_action( 'admin_init', array( __CLASS__, 'dismiss_notice' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Dismiss notice. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function dismiss_notice() { |
| 38 |
$orderable_dismiss_review_notice = filter_input( INPUT_GET, 'orderable_dismiss_review_notice' ); |
| 39 |
|
| 40 |
if ( ! $orderable_dismiss_review_notice ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
check_admin_referer( 'orderable', '_nonce' ); |
| 45 |
|
| 46 |
$data = self::get_review_data(); |
| 47 |
$data['dismiss'] = true; |
| 48 |
|
| 49 |
update_option( 'orderable_ask_review', $data ); |
| 50 |
|
| 51 |
$url = remove_query_arg( array( 'orderable_dismiss_review_notice', '_nonce' ) ); |
| 52 |
|
| 53 |
wp_safe_redirect( $url ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get review data. |
| 58 |
* |
| 59 |
* @return array. |
| 60 |
*/ |
| 61 |
public static function get_review_data() { |
| 62 |
$review_data = get_option( 'orderable_ask_review' ); |
| 63 |
|
| 64 |
if ( empty( $review_data ) ) { |
| 65 |
$review_data = array( |
| 66 |
'dismiss' => false, |
| 67 |
'orders_processes' => 0, |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
return $review_data; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Register order. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public static function register_order() { |
| 80 |
$review_data = self::get_review_data(); |
| 81 |
|
| 82 |
if ( ! is_array( $review_data ) || ! is_numeric( $review_data['orders_processes'] ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$review_data['orders_processes'] = absint( $review_data['orders_processes'] ) + 1; |
| 87 |
|
| 88 |
update_option( 'orderable_ask_review', $review_data, 1 ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Should display notice? |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public static function should_display_notice() { |
| 97 |
$review_data = self::get_review_data(); |
| 98 |
|
| 99 |
if ( ! $review_data['dismiss'] && $review_data['orders_processes'] >= self::$order_count_required ) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Show notice. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public static function show_notice() { |
| 110 |
$dismiss_url = admin_url(); |
| 111 |
$review_url = 'https://wordpress.org/support/plugin/orderable/reviews/#new-post'; |
| 112 |
|
| 113 |
if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { |
| 114 |
// phpcs:ignore |
| 115 |
$current_url = wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ); |
| 116 |
$current_url .= ( strpos( $current_url, '?' ) ? '&' : '?' ) . http_build_query( |
| 117 |
array( |
| 118 |
'orderable_dismiss_review_notice' => '1', |
| 119 |
'_nonce' => wp_create_nonce( 'orderable' ), |
| 120 |
) |
| 121 |
); |
| 122 |
$dismiss_url = $current_url; |
| 123 |
} |
| 124 |
|
| 125 |
if ( self::should_display_notice() ) { |
| 126 |
?> |
| 127 |
<div class="notice notice-warning is-dismissible notice-orderable-ask-review" style="border-left-color: #4233B6;"> |
| 128 |
<h4 style='margin-bottom: 10px;'> |
| 129 |
<?php |
| 130 |
/* translators: %1$s - number of orders. */ |
| 131 |
echo esc_html( sprintf( __( 'You have processed %1$s+ orders with Orderable 🥳', 'orderable' ), self::$order_count_required ) ); |
| 132 |
?> |
| 133 |
</h4> |
| 134 |
<p> |
| 135 |
<?php esc_html_e( 'You have been using Orderable for a while. If you are enjoying Orderable, please help us by leaving a review on WordPress.org', 'orderable' ); ?> |
| 136 |
</p> |
| 137 |
<p> |
| 138 |
<a href='<?php echo esc_url( $review_url ); ?>' target='_blank' class="button button-primary"><?php esc_html_e( 'Rate Now', 'orderable' ); ?></a> |
| 139 |
<a href='<?php echo esc_url( $dismiss_url ); ?>' class="button button-default"><?php esc_html_e( 'Dismiss Forever', 'orderable' ); ?></a> |
| 140 |
</p> |
| 141 |
</div> |
| 142 |
<?php |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|