| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
class WPPB_Review_Request { |
| 6 |
|
| 7 |
// Number of days to wait until review request is displayed |
| 8 |
public $delay = 7; |
| 9 |
public $wppb_review_cron_hook = 'wppb_review_check'; |
| 10 |
public $notificationId = 'wppb_review_request'; |
| 11 |
public $query_arg = 'wppb_dismiss_admin_notification'; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
$wppb_review_request_status = get_option( 'wppb_review_request_status', 'not_found' ); |
| 15 |
|
| 16 |
// Initialize the option that keeps track of the number of days elapsed |
| 17 |
if ( $wppb_review_request_status === 'not_found' || !is_numeric( $wppb_review_request_status ) ) { |
| 18 |
update_option( 'wppb_review_request_status', 0 ); |
| 19 |
} |
| 20 |
|
| 21 |
// Handle the cron |
| 22 |
if ( $wppb_review_request_status <= $this->delay ) { |
| 23 |
if ( !wp_next_scheduled( $this->wppb_review_cron_hook ) ) { |
| 24 |
wp_schedule_event( time(), 'daily', $this->wppb_review_cron_hook ); |
| 25 |
} |
| 26 |
|
| 27 |
if ( !has_action( $this->wppb_review_cron_hook ) ) { |
| 28 |
add_action( $this->wppb_review_cron_hook, array( $this, 'check_for_registration_shortcode' ) ); |
| 29 |
} |
| 30 |
} else if ( wp_next_scheduled( $this->wppb_review_cron_hook ) ){ |
| 31 |
wp_clear_scheduled_hook( $this->wppb_review_cron_hook ); |
| 32 |
} |
| 33 |
|
| 34 |
// Admin notice requesting review |
| 35 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 36 |
add_action( 'admin_init', array( $this, 'dismiss_notification' ) ); |
| 37 |
// Footer text requesting review |
| 38 |
add_filter('admin_footer_text', array( $this, 'admin_footer_rate_us' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
// Function that looks for the PB registration form shortcode and counts the number of days elapsed |
| 42 |
public function check_for_registration_shortcode() { |
| 43 |
global $wpdb; |
| 44 |
|
| 45 |
$query = "SELECT ID, post_title, guid FROM ".$wpdb->posts." WHERE post_content LIKE '%[wppb-register%' AND post_status = 'publish'"; |
| 46 |
|
| 47 |
if ( !empty( $wpdb->get_results( $query ) ) ) { |
| 48 |
$wppb_review_request_status = get_option( 'wppb_review_request_status', 'not_found' ); |
| 49 |
|
| 50 |
if ( $wppb_review_request_status !== 'not_found' && is_numeric( $wppb_review_request_status ) ) { |
| 51 |
update_option( 'wppb_review_request_status', $wppb_review_request_status + 1 ); |
| 52 |
} else { |
| 53 |
update_option( 'wppb_review_request_status', 1 ); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Function that displays the notice |
| 59 |
public function admin_notices() { |
| 60 |
$wppb_review_request_status = get_option( 'wppb_review_request_status' ); |
| 61 |
|
| 62 |
if ( is_numeric( $wppb_review_request_status ) && $wppb_review_request_status > $this->delay ) { |
| 63 |
global $current_user; |
| 64 |
global $pagenow; |
| 65 |
|
| 66 |
$user_id = $current_user->ID; |
| 67 |
|
| 68 |
if ( current_user_can( 'manage_options' ) && apply_filters( 'wppb_enable_review_request_notice', true ) ) { |
| 69 |
// Check that the user hasn't already dismissed the message |
| 70 |
if ( !get_user_meta( $user_id, $this->notificationId . '_dismiss_notification' ) ) { |
| 71 |
do_action( $this->notificationId . '_before_notification_displayed', $current_user, $pagenow ); |
| 72 |
$ul_icon_url = ( file_exists( WPPB_PLUGIN_DIR . 'assets/images/pb-logo.svg' )) ? WPPB_PLUGIN_URL . 'assets/images/pb-logo.svg' : ''; |
| 73 |
?> |
| 74 |
<div class="wppb-review-notice wppb-notice notice is-dismissible"> |
| 75 |
<div style="display: flex;"> |
| 76 |
<div> |
| 77 |
<img style="width: 80px; height: 80px; margin-right: 20px; margin-top: 20px;" src="<?php echo esc_url($ul_icon_url); ?>" > |
| 78 |
</div> |
| 79 |
<div> |
| 80 |
<p style="margin-top: 16px; font-size: 15px;"> |
| 81 |
<?php esc_html_e("Hello! Seems like you've been using Profile Builder to create front-end user forms. That's awesome!", 'profile-builder'); ?> |
| 82 |
<br/> |
| 83 |
<?php esc_html_e("If you can spare a few moments to rate it on WordPress.org, it would help us a lot (and boost my motivation).", 'profile-builder'); ?> |
| 84 |
</p> |
| 85 |
<p> |
| 86 |
<?php esc_html_e("~ Paul, developer of Profile Builder", 'profile-builder'); ?> |
| 87 |
</p> |
| 88 |
<p></p> |
| 89 |
</div> |
| 90 |
</div> |
| 91 |
|
| 92 |
<div style="display: flex; margin-bottom: 24px; padding-left: 95px;"> |
| 93 |
<div> |
| 94 |
<a href="https://wordpress.org/support/plugin/profile-builder/reviews/#new-post" |
| 95 |
target="_blank" rel="noopener" class="button-primary" style="margin-right: 20px"> |
| 96 |
<?php esc_html_e('Ok, I will gladly help!', 'profile-builder'); ?> |
| 97 |
</a> |
| 98 |
</div> |
| 99 |
|
| 100 |
<div> |
| 101 |
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( $this->query_arg => $this->notificationId ) ), 'wppb_review_notice_dismiss' ) ) ?>" |
| 102 |
class="button-secondary"> |
| 103 |
<?php esc_html_e('No, thanks.', 'profile-builder'); ?> |
| 104 |
</a> |
| 105 |
</div> |
| 106 |
</div> |
| 107 |
|
| 108 |
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( $this->query_arg => $this->notificationId ) ), 'wppb_review_notice_dismiss' ) ) ?>" |
| 109 |
type="button" class="notice-dismiss" style="text-decoration: none;"> |
| 110 |
<span class="screen-reader-text"> |
| 111 |
<?php esc_html_e('Dismiss this notice.', 'profile-builder'); ?> |
| 112 |
</span> |
| 113 |
</a> |
| 114 |
</div> |
| 115 |
<?php |
| 116 |
do_action( $this->notificationId . '_after_notification_displayed', $current_user, $pagenow ); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Function that saves the notification dismissal to the user meta |
| 123 |
public function dismiss_notification() { |
| 124 |
|
| 125 |
if( empty( $_GET['_wpnonce'] ) || !wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), 'wppb_review_notice_dismiss' ) ) |
| 126 |
return; |
| 127 |
|
| 128 |
global $current_user; |
| 129 |
|
| 130 |
$user_id = $current_user->ID; |
| 131 |
|
| 132 |
// If user clicks to ignore the notice, add that to their user meta |
| 133 |
if ( isset( $_GET[$this->query_arg] ) && $this->notificationId === $_GET[$this->query_arg] ) { |
| 134 |
do_action( $this->notificationId.'_before_notification_dismissed', $current_user ); |
| 135 |
add_user_meta($user_id, $this->notificationId . '_dismiss_notification', 'true', true); |
| 136 |
do_action( $this->notificationId.'_after_notification_dismissed', $current_user ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Function that adds admin footer text for encouraging users to leave a review of the plugin on wordpress.org |
| 141 |
function admin_footer_rate_us( $footer_text ) { |
| 142 |
global $current_screen; |
| 143 |
|
| 144 |
if ( $current_screen->parent_base == 'profile-builder' ){ |
| 145 |
$rate_text = sprintf( __( 'If you enjoy using <strong> %1$s </strong> please <a href="%2$s" target="_blank">rate us on WordPress.org</a>. More happy users means more features, less bugs and better support for everyone. ', 'profile-builder' ), |
| 146 |
PROFILE_BUILDER, |
| 147 |
'https://wordpress.org/support/view/plugin-reviews/profile-builder#postform' |
| 148 |
); |
| 149 |
return '<span id="footer-thankyou">' .$rate_text . '</span>'; |
| 150 |
} else { |
| 151 |
return $footer_text; |
| 152 |
} |
| 153 |
} |
| 154 |
} |