| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for showing admin notices. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/admin |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Responsible for showing admin notices. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/admin |
| 18 |
* @author Brecht Vandersmissen <brecht@bootstrapped.ventures> |
| 19 |
*/ |
| 20 |
class WPUPG_Notices { |
| 21 |
|
| 22 |
/** |
| 23 |
* Register actions and filters. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
add_filter( 'wpupg_admin_notices', array( __CLASS__, 'new_user_notice' ) ); |
| 29 |
add_filter( 'wpupg_admin_notices', array( __CLASS__, 'version_3_notice' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get all notices to show. |
| 34 |
* |
| 35 |
* @since 3.0.0 |
| 36 |
*/ |
| 37 |
public static function get_notices() { |
| 38 |
$notices_to_display = array(); |
| 39 |
$current_user_id = get_current_user_id(); |
| 40 |
|
| 41 |
if ( $current_user_id ) { |
| 42 |
$notices = apply_filters( 'wpupg_admin_notices', array() ); |
| 43 |
$dismissed_notices = get_user_meta( $current_user_id, 'wpupg_dismissed_notices', false ); |
| 44 |
|
| 45 |
foreach ( $notices as $notice ) { |
| 46 |
// Set defaults. |
| 47 |
$notice = wp_parse_args( $notice, array( |
| 48 |
'dismissable' => true, |
| 49 |
'location' => false, |
| 50 |
'capability' => false, |
| 51 |
)); |
| 52 |
|
| 53 |
// Check capability. |
| 54 |
if ( false !== $notice['capability'] && ! current_user_can( $notice['capability'] ) ) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
// Only dismissable when ID is set. |
| 59 |
if ( ! isset( $notice['id'] ) ) { |
| 60 |
$notice['dismissable'] = false; |
| 61 |
} |
| 62 |
|
| 63 |
// Check if user has already dismissed notice. |
| 64 |
if ( false !== $notice['dismissable'] && in_array( $notice['id'], $dismissed_notices ) ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$notices_to_display[] = $notice; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $notices_to_display; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Check if a specific notice has been dismissed. |
| 77 |
* |
| 78 |
* @since 3.0.0 |
| 79 |
*/ |
| 80 |
public static function is_dismissed( $id ) { |
| 81 |
$user_id = get_current_user_id(); |
| 82 |
$dismissed_notices = get_user_meta( $user_id, 'wpupg_dismissed_notices', false ); |
| 83 |
return in_array( $id, $dismissed_notices ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Show a notice to new users. |
| 88 |
* |
| 89 |
* @since 3.0.0 |
| 90 |
*/ |
| 91 |
public static function new_user_notice( $notices ) { |
| 92 |
$count = wp_count_posts( WPUPG_POST_TYPE )->publish; |
| 93 |
|
| 94 |
if ( 1 > intval( $count ) ) { |
| 95 |
$notices[] = array( |
| 96 |
'id' => 'new_user', |
| 97 |
'title' => __( 'Welcome to WP Ultimate Post Grid', 'wp-ultimate-post-grid' ), |
| 98 |
'text' => __( 'Not sure how to get started?', 'wp-ultimate-post-grid' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wpupg_faq' ) ). '">' . __( 'Check out our documentation!', 'wp-ultimate-post-grid' ) . '</a>', |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
return $notices; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Show a notice about version 3.0.0. |
| 107 |
* |
| 108 |
* @since 3.0.0 |
| 109 |
*/ |
| 110 |
public static function version_3_notice( $notices ) { |
| 111 |
$notice_id = 'version_3'; |
| 112 |
|
| 113 |
if ( ! self::is_dismissed( $notice_id ) ) { |
| 114 |
$has_old_version_grids = false; |
| 115 |
|
| 116 |
// Check all grids for version. |
| 117 |
$args = array( |
| 118 |
'post_type' => WPUPG_POST_TYPE, |
| 119 |
'post_status' => 'any', |
| 120 |
'posts_per_page' => -1, |
| 121 |
); |
| 122 |
$query = new WP_Query( $args ); |
| 123 |
|
| 124 |
$posts = $query->posts; |
| 125 |
foreach ( $posts as $post ) { |
| 126 |
$grid = WPUPG_Grid_Manager::get_grid( $post ); |
| 127 |
|
| 128 |
if ( version_compare( $grid->version(), '3.0.0', '<' ) ) { |
| 129 |
$has_old_version_grids = true; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if ( $has_old_version_grids ) { |
| 135 |
$notices[] = array( |
| 136 |
'id' => $notice_id, |
| 137 |
'title' => __( 'WARNING: WP Ultimate Post Grid 3.0.0+', 'wp-ultimate-post-grid' ), |
| 138 |
'text' => 'This is a major upgrade and we highly recommend you to test your grids. <a href="https://help.bootstrapped.ventures/article/218-wp-ultimate-post-grid-3-0-0" target="_blank">Learn more in our documentation!</a>', |
| 139 |
); |
| 140 |
} else { |
| 141 |
$user_id = get_current_user_id(); |
| 142 |
add_user_meta( $user_id, 'wpupg_dismissed_notices', $notice_id ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return $notices; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
WPUPG_Notices::init(); |
| 151 |
|