| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-admin-notice.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author itthinx |
| 18 |
* @package groups |
| 19 |
* @since 2.2.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Notices |
| 28 |
*/ |
| 29 |
class Groups_Admin_Notice { |
| 30 |
|
| 31 |
/** |
| 32 |
* Time mark. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
const INIT_TIME = 'groups-init-time'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Used to store user meta and hide the notice asking to review. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
const HIDE_REVIEW_NOTICE = 'groups-hide-review-notice'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Used to check next time. |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
const REMIND_LATER_NOTICE = 'groups-remind-later-notice'; |
| 51 |
|
| 52 |
/** |
| 53 |
* The number of seconds in seven days, since init date to show the notice. |
| 54 |
* |
| 55 |
* @var int |
| 56 |
*/ |
| 57 |
const SHOW_LAPSE = 604800; |
| 58 |
|
| 59 |
/** |
| 60 |
* The number of seconds in one day, used to show notice later again. |
| 61 |
* |
| 62 |
* @var int |
| 63 |
*/ |
| 64 |
const REMIND_LAPSE = 86400; |
| 65 |
|
| 66 |
/** |
| 67 |
* Adds actions. |
| 68 |
*/ |
| 69 |
public static function init() { |
| 70 |
add_action( 'admin_init', array( __CLASS__,'admin_init' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Hooked on the admin_init action. |
| 75 |
*/ |
| 76 |
public static function admin_init() { |
| 77 |
// @since 3.1.0 make sure the class and method exists, in case script load order and action triggers conflict |
| 78 |
if ( class_exists( 'Groups_User' ) && method_exists( 'Groups_User', 'current_user_can' ) ) { |
| 79 |
if ( Groups_User::current_user_can( 'activate_plugins' ) ) { |
| 80 |
$user_id = get_current_user_id(); |
| 81 |
if ( !empty( groups_sanitize_get( self::HIDE_REVIEW_NOTICE ) ) && groups_verify_get_nonce( 'groups_notice', 'hide' ) ) { |
| 82 |
add_user_meta( $user_id, self::HIDE_REVIEW_NOTICE, true ); |
| 83 |
} |
| 84 |
if ( !empty( groups_sanitize_get( self::REMIND_LATER_NOTICE ) ) && groups_verify_get_nonce( 'groups_notice', 'later' ) ) { |
| 85 |
update_user_meta( $user_id, self::REMIND_LATER_NOTICE, time() + self::REMIND_LAPSE ); |
| 86 |
} |
| 87 |
$hide_review_notice = get_user_meta( $user_id, self::HIDE_REVIEW_NOTICE, true ); |
| 88 |
if ( empty( $hide_review_notice ) ) { |
| 89 |
$d = time() - self::get_init_time(); |
| 90 |
if ( $d >= self::SHOW_LAPSE ) { |
| 91 |
$remind_later_notice = get_user_meta( $user_id, self::REMIND_LATER_NOTICE, true ); |
| 92 |
if ( empty( $remind_later_notice ) || ( time() > $remind_later_notice ) ) { |
| 93 |
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Initializes if necessary and returns the init time. |
| 103 |
* |
| 104 |
* @return int |
| 105 |
*/ |
| 106 |
public static function get_init_time() { |
| 107 |
$init_time = get_site_option( self::INIT_TIME, null ); |
| 108 |
if ( $init_time === null ) { |
| 109 |
$init_time = time(); |
| 110 |
add_site_option( self::INIT_TIME, $init_time ); |
| 111 |
} |
| 112 |
return $init_time; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Adds the admin notice. |
| 117 |
*/ |
| 118 |
public static function admin_notices() { |
| 119 |
|
| 120 |
$current_url = groups_get_current_url(); |
| 121 |
$hide_url = wp_nonce_url( add_query_arg( self::HIDE_REVIEW_NOTICE, true, $current_url ), 'hide', 'groups_notice' ); |
| 122 |
$remind_url = wp_nonce_url( add_query_arg( self::REMIND_LATER_NOTICE, true, $current_url ), 'later', 'groups_notice' ); |
| 123 |
|
| 124 |
$output = ''; |
| 125 |
|
| 126 |
$output .= '<style type="text/css">'; |
| 127 |
$output .= 'div.groups-rating {'; |
| 128 |
$output .= sprintf( 'background: url(%s) #fff no-repeat 8px 8px;', GROUPS_PLUGIN_URL . '/images/groups-256x256.png' ); |
| 129 |
$output .= 'padding-left: 76px ! important;'; |
| 130 |
$output .= 'background-size: 64px 64px;'; |
| 131 |
$output .= '}'; |
| 132 |
$output .= '</style>'; |
| 133 |
|
| 134 |
$output .= '<div class="updated groups-rating">'; |
| 135 |
$output .= '<p>'; |
| 136 |
$output .= wp_kses_post( __( 'Many thanks for using <strong>Groups</strong>!', 'groups' ) ); |
| 137 |
$output .= ' '; |
| 138 |
$output .= esc_html__( 'Could you please spare a minute and give it a review over at WordPress.org?', 'groups' ); |
| 139 |
$output .= ' '; |
| 140 |
$output .= sprintf( |
| 141 |
'<a title="%s" style="color:inherit;white-space:nowrap;cursor:help;opacity:0.5;" href="%s">%s</a>', |
| 142 |
esc_attr__( 'I have already done that or do not want to submit a review.', 'groups' ), |
| 143 |
esc_url( $hide_url ), |
| 144 |
esc_html__( 'Dismiss', 'groups' ) |
| 145 |
); |
| 146 |
$output .= '</p>'; |
| 147 |
$output .= '<p>'; |
| 148 |
$output .= sprintf( |
| 149 |
'<a title="%s" class="button button-primary" href="%s" target="_blank">%s</a>', |
| 150 |
esc_attr__( 'I want to submit a review right now!', 'groups' ), |
| 151 |
esc_url( 'https://wordpress.org/support/view/plugin-reviews/groups?filter=5#postform' ), |
| 152 |
esc_html__( 'Yes, here we go!', 'groups' ) |
| 153 |
); |
| 154 |
$output .= ' '; |
| 155 |
$output .= sprintf( |
| 156 |
'<a title="%s" class="button" href="%s">%s</a>', |
| 157 |
esc_attr__( 'I want to submit a review later, remind me!', 'groups' ), |
| 158 |
esc_url( $remind_url ), |
| 159 |
esc_html__( 'Remind me later', 'groups' ) |
| 160 |
); |
| 161 |
|
| 162 |
$output .= '</p>'; |
| 163 |
$output .= '<p>'; |
| 164 |
$output .= sprintf( |
| 165 |
/* translators: 1: link, 2: link */ |
| 166 |
__( 'Follow %1$s and visit %2$s to stay tuned for free and premium tools.', 'groups' ), |
| 167 |
'<a href="https://x.com/itthinx" target="_blank">@itthinx</a>', |
| 168 |
'<a href="https://www.itthinx.com" target="_blank">itthinx.com</a>' |
| 169 |
); |
| 170 |
$output .= '</p>'; |
| 171 |
$output .= '</div>'; |
| 172 |
|
| 173 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
Groups_Admin_Notice::init(); |
| 178 |
|