Skip to content

Commit

Permalink
Improve and document templates
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbills authored Oct 7, 2023
1 parent eacb6fd commit e3c9b13
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 37 deletions.
26 changes: 15 additions & 11 deletions classes/Admin/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ public function __start () {

public function donation_notice () {
if ( ! h::user_is_admin() ) return;

global $pagenow;
$in_plugins = 'plugins.php' === $pagenow;
$in_settings = 'admin.php' === $pagenow && 'wc-settings' === h::get( $_GET['page'] ) && 'wc-shipping-simulator' === h::get( $_GET['section'] );
if ( $in_plugins || $in_settings ) {
$class = 'notice ' . ( $in_settings ? 'woocommerce-message updated' : 'notice-info is-dismissible' );
$cookie = 'wc_shipping_simulator_donation_notice_closed';
echo h::get_template( 'notice-donation', [
'class' => $class,
'in_settings' => $in_settings,
'cookie' => $cookie,
'cookie_max_age' => 3 * MONTH_IN_SECONDS,
'display' => $in_settings || 'yes' !== h::get( $_COOKIE[ $cookie ] ),
] );
}

if ( ! $in_plugins && ! $in_settings ) return;

$cookie = 'wc_shipping_simulator_donation_notice_closed';
if ( ! $in_settings && 'yes' === h::get( $_COOKIE[ $cookie ] ) ) return;

$class = 'notice ' . ( $in_settings ? 'woocommerce-message updated' : 'notice-info is-dismissible' );

echo h::get_template( 'notice-donation', [
'class' => $class,
'is_dismissible' => ! $in_settings,
'cookie' => $cookie,
'cookie_max_age' => 3 * MONTH_IN_SECONDS,
] );
}
}
28 changes: 12 additions & 16 deletions classes/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function __start () {

add_action( 'wc_shipping_simulator_shortcode_included', [ $this, 'handle_form_request' ], 10 );
add_action( 'wc_shipping_simulator_results_wrapper', [ $this, 'maybe_display_form_notice' ] );

add_filter( 'wc_shipping_simulator_no_results_notice', [ $this, 'no_results_notice' ] );
}

public static function get_ajax_action () {
Expand Down Expand Up @@ -97,6 +99,10 @@ public function maybe_display_form_notice ( $html ) {
return $html;
}

public function no_results_notice ( $notice ) {
return $notice ? $notice : Settings::get_option( 'no_results' );
}

protected function calculate_shipping ( $args ) {
h::logger()->info( 'Request raw data: ' . wp_json_encode( $args ) );

Expand All @@ -119,24 +125,14 @@ protected function calculate_shipping ( $args ) {
return $package->calculate_shipping();
}

protected function get_results ( $rates, $notice = null ) {
$args = [
'rates' => [],
'notice' => $notice,
];
if ( ! $notice ) {
$args = [
'rates' => $rates,
'notice' => apply_filters(
'wc_shipping_simulator_no_results_notice',
Settings::get_option( 'no_results' )
),
'data' => $this->data,
];
}
protected function get_results ( $rates ) {
$template_html = h::get_template( 'shipping-simulator-results', [
'rates' => (array) $rates,
'data' => $this->data,
] );
return \apply_filters(
'wc_shipping_simulator_request_results_html',
h::get_template( 'shipping-simulator-results', $args ),
$template_html,
$rates,
$this->data
);
Expand Down
20 changes: 13 additions & 7 deletions templates/notice-donation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/**
* The Template for displaying an admin notice asking for reviews or donations.
*
* @var string $class The wrapper CSS classes
* @var bool $is_dismissible This notice is not dismissible in settings page
* @var string $cookie The cookie nama
* @var int $cookie_max_age The cookie duration
*/

use Shipping_Simulator\Helpers as h;

$five_stars = '&#9733;&#9733;&#9733;&#9733;&#9733;';
Expand All @@ -10,32 +19,29 @@
);
?>

<?php if ( $args['display'] ) : ?>
<div class="<?php echo esc_attr( $args['class'] ) ?>" id="wc_shipping_sim_notice_donation">
<div class="<?php echo esc_attr( $class ) ?>" id="wc_shipping_sim_notice_donation">
<p><?php echo $message; ?></p>
<p>
<a href="<?php echo esc_url( h::config_get( 'DONATION_URL' ) ) ?>" target="_blank" class="button button-primary"><?php esc_html_e( 'Donate', 'wc-shipping-simulator' ); ?></a>
<a href="<?php echo esc_attr( h::config_get( 'PLUGIN_REVIEWS' ) ) ?>" target="_blank" class="button button-secondary"><?php esc_html_e( 'Make a review', 'wc-shipping-simulator' ); ?></a>
</p>
<?php if ( ! $args['in_settings'] ) : ?>
<?php if ( $is_dismissible ) : ?>
<button type="button" class="notice-dismiss" title="<?php esc_attr_e( 'Dismiss this notice.' ); ?>">
<span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.' ); ?></span>
</button>
<?php endif; ?>
</div>

<?php if ( ! $args['in_settings'] ) : ?>
<?php if ( $is_dismissible ) : ?>
<script>
window.addEventListener('DOMContentLoaded', () => {
const notice = document.querySelector('#wc_shipping_sim_notice_donation .notice-dismiss');
notice.addEventListener('click', (evt) => {
const cookie = '<?php echo esc_js( "{$args['cookie']}=yes;max-age={$args['cookie_max_age']};secure;samesite=strict" ) ?>';
const cookie = '<?php echo esc_js( "{$cookie}=yes;max-age={$cookie_max_age};secure;samesite=strict" ) ?>';
document.cookie = cookie;
evt.currentTarget.parentNode.style.display = 'none';
evt.currentTarget.parentNode.style.visibility = 'hidden';
})
})
</script>
<?php endif ?>

<?php endif; ?>
13 changes: 13 additions & 0 deletions templates/shipping-simulator-form.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<?php
/**
* The Template for displaying the shipping simulator form.
*
* @var string $css_class The wrapper CSS classes
* @var string $input_placeholder
* @var string $input_type
* @var string $input_value
* @var string $submit_label
* @var array $params
*/
?>

<section id="wc-shipping-sim" class="<?php echo esc_attr( $css_class ) ?>">
<?php do_action( 'wc_shipping_simulator_wrapper_start' ) ?>

Expand Down
5 changes: 5 additions & 0 deletions templates/shipping-simulator-results-wrapper.php
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<?php
/**
* The Template for displaying the wrapper of the shipping simulator results.
*/
?>
<section id="wc-shipping-sim-results" role="status" aria-busy="false"></section>
20 changes: 17 additions & 3 deletions templates/shipping-simulator-results.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php use Shipping_Simulator\Helpers as h; ?>
<?php
/**
* The Template for displaying the shipping simulator results.
*
* @var \WC_Shipping_Rate[] $rates
* @var array $data
*/

use Shipping_Simulator\Helpers as h;

?>

<?php if ( count( $rates ) > 0 ) : ?>
<?php do_action( 'wc_shipping_simulator_results_before', $data ) ?>
Expand All @@ -23,6 +33,10 @@
</table>

<?php do_action( 'wc_shipping_simulator_results_after', $data ) ?>
<?php elseif ( $notice ) : ?>
<div class="no-results"><?php echo h::safe_html( $notice ) ?></div>
<?php else : ?>
<?php $no_results_notice = apply_filters( 'wc_shipping_simulator_no_results_notice', '' ); ?>

<?php if ( $no_results_notice ) : ?>
<div class="no-results"><?php echo h::safe_html( $no_results_notice ) ?></div>
<?php endif ?>
<?php endif ?>

0 comments on commit e3c9b13

Please sign in to comment.