controls-helper.php
4 years ago
elementor-data-map.php
4 years ago
global-helper.php
3 years ago
helper.php
3 years ago
notice.php
3 years ago
shipping-calculation.php
3 years ago
notice.php
187 lines
| 1 | <?php |
| 2 | namespace ShopEngine\Utils; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) die( 'Forbidden' ); |
| 5 | |
| 6 | /** |
| 7 | * shopengine-builder notice class. |
| 8 | * Handles dynamically notices for lazy developers. |
| 9 | * |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | class Notice { |
| 13 | |
| 14 | /** |
| 15 | * Constructor |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | add_action( 'admin_footer', [ $this, 'enqueue_scripts' ], 9999); |
| 21 | add_action( 'wp_ajax_shopengine-notices', [ $this, 'dismiss' ] ); |
| 22 | } |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Dismiss Notice. |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | * @return void |
| 30 | */ |
| 31 | public function dismiss() { |
| 32 | |
| 33 | if(empty($_POST['shopengine_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['shopengine_nonce'])), 'shopengine_nonce')) { |
| 34 | wp_send_json_error(); |
| 35 | } |
| 36 | |
| 37 | $id = ( isset( $_POST['id'] ) ) ? sanitize_key($_POST['id']) : ''; |
| 38 | $time = ( isset( $_POST['time'] ) ) ? sanitize_text_field(wp_unslash($_POST['time'])) : ''; |
| 39 | $meta = ( isset( $_POST['meta'] ) ) ? sanitize_key($_POST['meta']) : ''; |
| 40 | |
| 41 | // Valid inputs? |
| 42 | if ( ! empty( $id ) ) { |
| 43 | |
| 44 | if ( 'user' === $meta ) { |
| 45 | update_user_meta( get_current_user_id(), $id, true ); |
| 46 | } else { |
| 47 | set_transient( $id, true, $time ); |
| 48 | } |
| 49 | |
| 50 | wp_send_json_success(); |
| 51 | } |
| 52 | |
| 53 | wp_send_json_error(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Enqueue Scripts. |
| 58 | * |
| 59 | * @since 1.0.0 |
| 60 | * @return void |
| 61 | */ |
| 62 | public function enqueue_scripts() { |
| 63 | echo " |
| 64 | <script> |
| 65 | jQuery(document).ready(function ($) { |
| 66 | $( '.shopengine-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() { |
| 67 | _this = $( this ).parents( '.shopengine-active-notice' ); |
| 68 | var id = _this.attr( 'id' ) || ''; |
| 69 | var time = _this.attr( 'dismissible-time' ) || ''; |
| 70 | var meta = _this.attr( 'dismissible-meta' ) || ''; |
| 71 | |
| 72 | $.ajax({ |
| 73 | url: ajaxurl, |
| 74 | type: 'POST', |
| 75 | data: { |
| 76 | action : 'shopengine-notices', |
| 77 | id : id, |
| 78 | meta : meta, |
| 79 | time : time, |
| 80 | shopengine_nonce: ".esc_html(wp_create_nonce( 'shopengine_nonce' ))." |
| 81 | }, |
| 82 | }); |
| 83 | |
| 84 | }); |
| 85 | |
| 86 | }); |
| 87 | </script> |
| 88 | "; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Show Notices |
| 93 | * |
| 94 | * @since 1.0.0 |
| 95 | * @return void |
| 96 | */ |
| 97 | public static function push($notice) { |
| 98 | |
| 99 | $defaults = [ |
| 100 | 'id' => '', |
| 101 | 'type' => 'info', |
| 102 | 'show_if' => true, |
| 103 | 'message' => '', |
| 104 | 'class' => 'shopengine-active-notice', |
| 105 | 'dismissible' => false, |
| 106 | 'btn' => [], |
| 107 | 'dismissible-meta' => 'user', |
| 108 | 'dismissible-time' => WEEK_IN_SECONDS, |
| 109 | 'data' => '', |
| 110 | ]; |
| 111 | |
| 112 | $notice = wp_parse_args( $notice, $defaults ); |
| 113 | |
| 114 | $classes = [ 'shopengine-notice', 'notice' ]; |
| 115 | |
| 116 | $classes[] = $notice['class']; |
| 117 | if ( isset( $notice['type'] ) ) { |
| 118 | $classes[] = 'notice-' . $notice['type']; |
| 119 | } |
| 120 | |
| 121 | // Is notice dismissible? |
| 122 | if ( true === $notice['dismissible'] ) { |
| 123 | $classes[] = 'is-dismissible'; |
| 124 | |
| 125 | // Dismissable time. |
| 126 | $notice['data'] = ' dismissible-time=' . esc_attr( $notice['dismissible-time'] ) . ' '; |
| 127 | } |
| 128 | |
| 129 | // Notice ID. |
| 130 | $notice_id = 'shopengine-sites-notice-id-' . $notice['id']; |
| 131 | $notice['id'] = $notice_id; |
| 132 | if ( ! isset( $notice['id'] ) ) { |
| 133 | $notice_id = 'shopengine-sites-notice-id-' . $notice['id']; |
| 134 | $notice['id'] = $notice_id; |
| 135 | } else { |
| 136 | $notice_id = $notice['id']; |
| 137 | } |
| 138 | |
| 139 | $notice['classes'] = implode( ' ', $classes ); |
| 140 | |
| 141 | // User meta. |
| 142 | $notice['data'] .= ' dismissible-meta=' . esc_attr( $notice['dismissible-meta'] ) . ' '; |
| 143 | if ( 'user' === $notice['dismissible-meta'] ) { |
| 144 | $expired = get_user_meta( get_current_user_id(), $notice_id, true ); |
| 145 | } elseif ( 'transient' === $notice['dismissible-meta'] ) { |
| 146 | $expired = get_transient( $notice_id ); |
| 147 | } |
| 148 | |
| 149 | // Notice visible after transient expire. |
| 150 | if ( isset( $notice['show_if'] ) ) { |
| 151 | if ( true === $notice['show_if'] ) { |
| 152 | |
| 153 | // Is transient expired? |
| 154 | if ( false === $expired || empty( $expired ) ) { |
| 155 | self::markup($notice); |
| 156 | } |
| 157 | } |
| 158 | } else { |
| 159 | self::markup($notice); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Markup Notice. |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | * @param array $notice Notice markup. |
| 168 | * @return void |
| 169 | */ |
| 170 | public static function markup( $notice = [] ) { |
| 171 | ?> |
| 172 | <div id="<?php echo esc_attr( $notice['id'] ); ?>" class="<?php echo esc_attr( $notice['classes'] ); ?>" <?php shopengine_content_render(Helper::render($notice['data'])); ?>> |
| 173 | <p> |
| 174 | <?php echo wp_kses($notice['message'], Helper::get_kses_array()); ?> |
| 175 | </p> |
| 176 | |
| 177 | <?php if(!empty($notice['btn'])):?> |
| 178 | <p> |
| 179 | <a title="<?php esc_html_e('Notification','shopengine')?>" href="<?php echo esc_url($notice['btn']['url']); ?>" class="button-primary"><?php echo esc_html($notice['btn']['label']); ?></a> |
| 180 | </p> |
| 181 | <?php endif; ?> |
| 182 | </div> |
| 183 | <?php |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | new Notice(); |