| 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( $_GET[self::HIDE_REVIEW_NOTICE] ) && wp_verify_nonce( $_GET['groups_notice'], 'hide' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 82 |
add_user_meta( $user_id, self::HIDE_REVIEW_NOTICE, true ); |
| 83 |
} |
| 84 |
if ( !empty( $_GET[self::REMIND_LATER_NOTICE] ) && wp_verify_nonce( $_GET['groups_notice'], 'later' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 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 = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 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 |
* Provide the Bitcoin box markup. |
| 178 |
* |
| 179 |
* @return string |
| 180 |
*/ |
| 181 |
public static function get_groups_bitcoin_box( $params = null ) { |
| 182 |
$type = ''; |
| 183 |
$where = ''; |
| 184 |
if ( is_array( $params ) ) { |
| 185 |
if ( isset( $params['type'] ) ) { |
| 186 |
switch ( $params['type'] ) { |
| 187 |
case 'vertical': |
| 188 |
case 'horizontal': |
| 189 |
$type = $params['type']; |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
if ( isset( $params['where'] ) ) { |
| 194 |
switch ( $params['where'] ) { |
| 195 |
case 'options': |
| 196 |
case 'add-ons': |
| 197 |
$where = $params['where']; |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
$bitcoin_address = 'bc1q7klf9ge8gvtl4h4qlh6c73tgk0cdehfv5vcq0g'; |
| 203 |
$bitcoin_box = sprintf( '<div id="groups-bitcoin-box" class="groups-bitcoin-box %s %s">', esc_attr( $type ), esc_attr( $where ) ); |
| 204 |
$bitcoin_box .= '<div class="groups-bitcoin-box-inside">'; |
| 205 |
$bitcoin_box .= '<div class="groups-bitcoin-cta">'; |
| 206 |
$bitcoin_box .= esc_html__( 'Contribute Bitcoin to Groups', 'groups' ); |
| 207 |
$bitcoin_box .= '</div>'; // .groups-bitcoin-cta |
| 208 |
$bitcoin_box .= '<div class="groups-bitcoin-image">'; |
| 209 |
$bitcoin_box .= sprintf( '<img class="groups-bitcoin-address-image" src="%s"/>', esc_url( GROUPS_PLUGIN_URL .'images/groups-bitcoin.png' ) ); |
| 210 |
$bitcoin_box .= '</div>'; // .groups-bitcoin-image |
| 211 |
$bitcoin_box .= '<div class="groups-bitcoin-description">'; |
| 212 |
$bitcoin_box .= sprintf( |
| 213 |
/* translators: HTML */ |
| 214 |
esc_html__( 'Support the developers and contribute Bitcoin to %s', 'groups' ), |
| 215 |
'<span class="groups-bitcoin-address">' . esc_html( $bitcoin_address ) . '</span>' . |
| 216 |
' ' . |
| 217 |
'<span class="dashicons dashicons-admin-page pseudo-copy-icon" style="display:none"></span>' |
| 218 |
); |
| 219 |
$bitcoin_box .= '</div>'; // .groups-bitcoin-description |
| 220 |
$bitcoin_box .= '</div>'; // .groups-bitcoin-box-inside |
| 221 |
$bitcoin_box .= '</div>'; // .groups-bitcoin-box |
| 222 |
$bitcoin_box .= '<script type="text/javascript">'; |
| 223 |
$bitcoin_box .= 'if ( typeof groups_bitcoin_box_address === "undefined" ) {'; |
| 224 |
$bitcoin_box .= 'function groups_bitcoin_box_address( event ) {'; |
| 225 |
$bitcoin_box .= 'if ( navigator.clipboard && window.isSecureContext ) {'; |
| 226 |
$bitcoin_box .= 'try {'; |
| 227 |
$bitcoin_box .= 'navigator.clipboard.writeText("' . esc_html( $bitcoin_address ) .'");'; |
| 228 |
$bitcoin_box .= 'jQuery( event.target ).closest( ".groups-bitcoin-box-inside" ).fadeOut( 100 ).fadeIn( 100 );'; |
| 229 |
$bitcoin_box .= '} catch (error) {'; |
| 230 |
$bitcoin_box .= '}'; |
| 231 |
$bitcoin_box .= '} else if ( document.queryCommandEnabled && document.queryCommandEnabled( "copy" ) ) {'; |
| 232 |
$bitcoin_box .= 'let tmp = document.createElement("textarea");'; |
| 233 |
$bitcoin_box .= sprintf( 'tmp.value="%s";', $bitcoin_address ); |
| 234 |
$bitcoin_box .= 'tmp.setAttribute("style","display:none");'; |
| 235 |
$bitcoin_box .= 'tmp.select();'; |
| 236 |
$bitcoin_box .= 'try {'; |
| 237 |
$bitcoin_box .= 'document.execCommand( "copy" );'; |
| 238 |
$bitcoin_box .= 'jQuery( event.target ).closest( ".groups-bitcoin-box-inside" ).fadeOut( 100 ).fadeIn( 100 );'; |
| 239 |
$bitcoin_box .= '} catch ( error ) {'; |
| 240 |
$bitcoin_box .= '} finally {'; |
| 241 |
$bitcoin_box .= 'tmp.remove();'; |
| 242 |
$bitcoin_box .= '}'; |
| 243 |
$bitcoin_box .= '}'; |
| 244 |
$bitcoin_box .= '}'; |
| 245 |
$bitcoin_box .= 'document.addEventListener( "DOMContentLoaded", function() {'; |
| 246 |
$bitcoin_box .= 'if ( typeof jQuery !== "undefined" ) {'; |
| 247 |
$bitcoin_box .= 'if ( navigator.clipboard && window.isSecureContext || document.queryCommandEnabled && document.queryCommandEnabled( "copy" ) ) {'; |
| 248 |
$bitcoin_box .= 'jQuery( ".groups-bitcoin-box" ).click( groups_bitcoin_box_address );'; |
| 249 |
$bitcoin_box .= 'jQuery( ".groups-bitcoin-box" ).css( "cursor", "pointer" );'; |
| 250 |
$bitcoin_box .= 'jQuery( ".groups-bitcoin-box .pseudo-copy-icon" ).css( "display", "inline" );'; |
| 251 |
$bitcoin_box .= '} else {'; |
| 252 |
$bitcoin_box .= 'jQuery( ".groups-bitcoin-box .dashicons.dashicons-admin-page" ).remove();'; |
| 253 |
$bitcoin_box .= '}'; |
| 254 |
$bitcoin_box .= '}'; |
| 255 |
$bitcoin_box .= '} );'; // DOMContentLoaded |
| 256 |
$bitcoin_box .= '}'; // typeof |
| 257 |
$bitcoin_box .= '</script>'; |
| 258 |
return $bitcoin_box; |
| 259 |
} |
| 260 |
} |
| 261 |
Groups_Admin_Notice::init(); |
| 262 |
|