| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
/** |
| 6 |
* Components class. |
| 7 |
* |
| 8 |
* @since 1.1.0 |
| 9 |
*/ |
| 10 |
class Sellkit_Notices { |
| 11 |
|
| 12 |
/** |
| 13 |
* Sellkit_Notices constructor. |
| 14 |
* |
| 15 |
* @since 1.1.0 |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
$this->load_notices(); |
| 19 |
|
| 20 |
add_action( 'wp_ajax_sellkit_admin_notice_dismiss', [ $this, 'dismiss_notice' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Load notices. |
| 25 |
* |
| 26 |
* @since 1.1.0 |
| 27 |
*/ |
| 28 |
public function load_notices() { |
| 29 |
$path = trailingslashit( sellkit()->plugin_dir() . 'includes/admin/notices' ); |
| 30 |
$file_paths = glob( $path . '*.php' ); |
| 31 |
|
| 32 |
foreach ( $file_paths as $file_path ) { |
| 33 |
if ( ! file_exists( $file_path ) ) { |
| 34 |
continue; |
| 35 |
} |
| 36 |
|
| 37 |
require_once $file_path; |
| 38 |
|
| 39 |
$file_name = str_replace( '.php', '', basename( $file_path ) ); |
| 40 |
$operator_class = str_replace( '-', ' ', $file_name ); |
| 41 |
$operator_class = str_replace( ' ', '_', ucwords( $operator_class ) ); |
| 42 |
$operator_class = "Sellkit\Admin\Notices\\{$operator_class}"; |
| 43 |
|
| 44 |
if ( ! class_exists( $operator_class ) || 'notice-base' === $file_name ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$notice = new $operator_class(); |
| 49 |
|
| 50 |
if ( $notice->is_valid() ) { |
| 51 |
add_action( 'admin_notices', [ $notice, 'notice_content_wrapper' ], $notice->priority() ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Dismiss notice handler. |
| 58 |
* |
| 59 |
* @since 1.1.0 |
| 60 |
*/ |
| 61 |
public function dismiss_notice() { |
| 62 |
check_ajax_referer( 'sellkit_admin', 'nonce' ); |
| 63 |
|
| 64 |
$new_notice_key = ! empty( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; |
| 65 |
$dismissed_notices = sellkit_get_option( 'dismissed_notices' ); |
| 66 |
$dismissed_notices = empty( $dismissed_notices ) ? [] : $dismissed_notices; |
| 67 |
$new_dismissed_notices = array_merge( $dismissed_notices, [ $new_notice_key ] ); |
| 68 |
|
| 69 |
sellkit_update_option( 'dismissed_notices', array_filter( array_unique( $new_dismissed_notices ) ) ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
new Sellkit_Notices(); |
| 74 |
|