| 1 |
<?php |
| 2 |
/** |
| 3 |
* Function that creates the "Register your version" submenu page |
| 4 |
* |
| 5 |
* @since v.2.0 |
| 6 |
* |
| 7 |
* @return void |
| 8 |
*/ |
| 9 |
function wppb_register_your_version_submenu_page() { |
| 10 |
if ( PROFILE_BUILDER != 'Profile Builder Free' ) |
| 11 |
add_submenu_page( 'profile-builder', __( 'Register Your Version', 'profilebuilder' ), __( 'Register Version', 'profilebuilder' ), 'manage_options', 'profile-builder-register', 'wppb_register_your_version_content' ); |
| 12 |
} |
| 13 |
add_action( 'admin_menu', 'wppb_register_your_version_submenu_page', 20 ); |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Function that adds content to the "Register your Version" submenu page |
| 18 |
* |
| 19 |
* @since v.2.0 |
| 20 |
* |
| 21 |
* @return string |
| 22 |
*/ |
| 23 |
function wppb_register_your_version_content() { |
| 24 |
|
| 25 |
?> |
| 26 |
<div class="wrap wppb-wrap"> |
| 27 |
<?php |
| 28 |
if ( PROFILE_BUILDER == 'Profile Builder Pro' ){ |
| 29 |
wppb_serial_form('pro', 'Profile Builder Pro'); |
| 30 |
}elseif ( PROFILE_BUILDER == 'Profile Builder Hobbyist' ){ |
| 31 |
wppb_serial_form('hobbyist', 'Profile Builder Hobbyist'); |
| 32 |
} |
| 33 |
?> |
| 34 |
|
| 35 |
</div> |
| 36 |
<?php |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Function that creates the "Register your version" form depending on Pro or Hobbyist version |
| 41 |
* |
| 42 |
* @since v.2.0 |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
function wppb_serial_form($version, $fullname){ |
| 47 |
?> |
| 48 |
|
| 49 |
<h2><?php _e( "Register your version of $fullname", 'profilebuilder' ); ?></h2> |
| 50 |
|
| 51 |
<form method="post" action="options.php"> |
| 52 |
|
| 53 |
<?php $wppb_profile_builder_serial = get_option( 'wppb_profile_builder_'.$version.'_serial' ); ?> |
| 54 |
<?php $wppb_profile_builder_serial_status = get_option( 'wppb_profile_builder_'.$version.'_serial_status' ); ?> |
| 55 |
<?php settings_fields( 'wppb_profile_builder_'.$version.'_serial' ); ?> |
| 56 |
|
| 57 |
<p><?php _e( "Now that you acquired a copy of $fullname, you should take the time and register it with the serial number you received", 'profilebuilder' );?></p> |
| 58 |
<p><?php _e( "If you register this version of Profile Builder, you'll receive information regarding upgrades, patches, and technical support.", 'profilebuilder' );?></p> |
| 59 |
<p class="wppb-serial-wrap"> |
| 60 |
<label for="wppb_profile_builder_<?php echo $version; ?>_serial"><?php _e(' Serial Number:', 'profilebuilder' );?></label> |
| 61 |
<input type="text" size="50" name="wppb_profile_builder_<?php echo $version; ?>_serial" id="wppb_profile_builder_<?php echo $version; ?>_serial" class="regular-text" <?php if ( $wppb_profile_builder_serial != ''){ echo ' value="'.$wppb_profile_builder_serial.'"';} ?>/> |
| 62 |
|
| 63 |
<?php |
| 64 |
if( $wppb_profile_builder_serial_status == 'found' ) |
| 65 |
echo '<span class="validateStatus"><img src="'.WPPB_PLUGIN_URL.'/assets/images/accept.png" title="'.__( 'The serial number was successfully validated!', 'profilebuilder' ).'"/></span>'; |
| 66 |
elseif ( $wppb_profile_builder_serial_status == 'notFound' ) |
| 67 |
echo '<span class="validateStatus"><img src="'.WPPB_PLUGIN_URL.'/assets/images/icon_error.png" title="'.__( 'The serial number entered couldn\'t be validated!','profilebuilder' ).'"/></span>'; |
| 68 |
elseif ( $wppb_profile_builder_serial_status == 'expired' ) |
| 69 |
echo '<span class="validateStatus"><img src="'.WPPB_PLUGIN_URL.'/assets/images/icon_error.png" title="'.__( 'The serial number couldn\'t be validated because it expired!','profilebuilder' ).'"/></span>'; |
| 70 |
elseif ( $wppb_profile_builder_serial_status == 'serverDown' ) |
| 71 |
echo '<span class="validateStatus"><img src="'.WPPB_PLUGIN_URL.'/assets/images/icon_error.png" title="'.__( 'The serial number couldn\'t be validated because process timed out. This is possible due to the server being down. Please try again later!','profilebuilder' ).'"/></span>'; |
| 72 |
?> |
| 73 |
<span class="wppb-serialnumber-descr"><?php _e( '(e.g. RMPB-15-SN-253a55baa4fbe7bf595b2aabb8d72985)', 'profilebuilder' );?></span> |
| 74 |
</p> |
| 75 |
|
| 76 |
|
| 77 |
<div id="wppb_submit_button_div"> |
| 78 |
<input type="hidden" name="action" value="update" /> |
| 79 |
<p class="submit"> |
| 80 |
<?php wp_nonce_field( 'wppb_register_version_nonce', 'wppb_register_version_nonce' ); ?> |
| 81 |
<input type="submit" name="wppb_serial_number_activate" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
| 82 |
</p> |
| 83 |
</div> |
| 84 |
|
| 85 |
</form> |
| 86 |
<?php |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
//the function to check the validity of the serial number and save a variable in the DB; purely visual |
| 91 |
function wppb_check_serial_number($oldVal, $newVal){ |
| 92 |
|
| 93 |
$serial_number_set = $newVal; |
| 94 |
|
| 95 |
$response = wp_remote_get( 'http://updatemetadata.cozmoslabs.com/checkserial/?serialNumberSent='.$serial_number_set ); |
| 96 |
|
| 97 |
if ( PROFILE_BUILDER == 'Profile Builder Pro' ){ |
| 98 |
wppb_update_serial_status($response, 'pro'); |
| 99 |
wp_clear_scheduled_hook( "check_plugin_updates-profile-builder-pro-update" ); |
| 100 |
} else { |
| 101 |
wppb_update_serial_status($response, 'hobbyist'); |
| 102 |
wp_clear_scheduled_hook( "check_plugin_updates-profile-builder-hobbyist-update" ); |
| 103 |
} |
| 104 |
$user_ID = get_current_user_id(); |
| 105 |
delete_user_meta( $user_ID, 'wppb_dismiss_notification' ); |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
add_action( 'update_option_wppb_profile_builder_pro_serial', 'wppb_check_serial_number', 10, 2 ); |
| 110 |
add_action( 'update_option_wppb_profile_builder_hobbyist_serial', 'wppb_check_serial_number', 10, 2 ); |
| 111 |
|
| 112 |
add_action( 'add_option_wppb_profile_builder_pro_serial', 'wppb_check_serial_number', 10, 2 ); |
| 113 |
add_action( 'add_option_wppb_profile_builder_hobbyist_serial', 'wppb_check_serial_number', 10, 2 ); |
| 114 |
|
| 115 |
/** |
| 116 |
* @param $response |
| 117 |
*/ |
| 118 |
function wppb_update_serial_status($response, $version) |
| 119 |
{ |
| 120 |
if (is_wp_error($response)) { |
| 121 |
update_option('wppb_profile_builder_'.$version.'_serial_status', 'serverDown'); //server down |
| 122 |
} elseif ((trim($response['body']) != 'notFound') && (trim($response['body']) != 'found') && (trim($response['body']) != 'expired')) { |
| 123 |
update_option('wppb_profile_builder_'.$version.'_serial_status', 'serverDown'); //unknown response parameter |
| 124 |
update_option('wppb_profile_builder_'.$version.'_serial', ''); //reset the entered password, since the user will need to try again later |
| 125 |
|
| 126 |
} else { |
| 127 |
update_option('wppb_profile_builder_'.$version.'_serial_status', trim($response['body'])); //either found, notFound or expired |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
//the update didn't work when the old value = new value, so we need to apply a filter on get_option (that is run before update_option), that resets the old value |
| 132 |
function wppb_check_serial_number_fix($newvalue, $oldvalue){ |
| 133 |
|
| 134 |
if ( $newvalue == $oldvalue ) |
| 135 |
wppb_check_serial_number( $oldvalue, $newvalue ); |
| 136 |
|
| 137 |
return $newvalue; |
| 138 |
} |
| 139 |
add_filter( 'pre_update_option_wppb_profile_builder_pro_serial', 'wppb_check_serial_number_fix', 10, 2 ); |
| 140 |
add_filter( 'pre_update_option_wppb_profile_builder_hobbyist_serial', 'wppb_check_serial_number_fix', 10, 2 ); |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Class that adds a notice when either the serial number wasn't found, or it has expired |
| 145 |
* |
| 146 |
* @since v.2.0 |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
class wppb_add_notices{ |
| 151 |
public $pluginPrefix = ''; |
| 152 |
public $pluginName = ''; |
| 153 |
public $notificaitonMessage = ''; |
| 154 |
public $pluginSerialStatus = ''; |
| 155 |
|
| 156 |
function __construct( $pluginPrefix, $pluginName, $notificaitonMessage, $pluginSerialStatus ){ |
| 157 |
$this->pluginPrefix = $pluginPrefix; |
| 158 |
$this->pluginName = $pluginName; |
| 159 |
$this->notificaitonMessage = $notificaitonMessage; |
| 160 |
$this->pluginSerialStatus = $pluginSerialStatus; |
| 161 |
|
| 162 |
add_action( 'admin_notices', array( $this, 'add_admin_notice' ) ); |
| 163 |
add_action( 'admin_init', array( $this, 'dismiss_notification' ) ); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
// Display a notice that can be dismissed in case the serial number is inactive |
| 168 |
function add_admin_notice() { |
| 169 |
global $current_user ; |
| 170 |
global $pagenow; |
| 171 |
|
| 172 |
$user_id = $current_user->ID; |
| 173 |
|
| 174 |
do_action( $this->pluginPrefix.'_before_notification_displayed', $current_user, $pagenow ); |
| 175 |
|
| 176 |
if ( current_user_can( 'manage_options' ) ){ |
| 177 |
|
| 178 |
$plugin_serial_status = get_option( $this->pluginSerialStatus ); |
| 179 |
if ( $plugin_serial_status != 'found' ){ |
| 180 |
// Check that the user hasn't already clicked to ignore the message |
| 181 |
if ( ! get_user_meta($user_id, $this->pluginPrefix.'_dismiss_notification' ) ) { |
| 182 |
echo $finalMessage = apply_filters($this->pluginPrefix.'_notification_message','<div class="error wppb-serial-notification" >'.$this->notificaitonMessage.'</div>', $this->notificaitonMessage); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
do_action( $this->pluginPrefix.'_notification_displayed', $current_user, $pagenow, $plugin_serial_status ); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
do_action( $this->pluginPrefix.'_after_notification_displayed', $current_user, $pagenow ); |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
function dismiss_notification() { |
| 195 |
global $current_user; |
| 196 |
|
| 197 |
$user_id = $current_user->ID; |
| 198 |
|
| 199 |
do_action( $this->pluginPrefix.'_before_notification_dismissed', $current_user ); |
| 200 |
|
| 201 |
// If user clicks to ignore the notice, add that to their user meta |
| 202 |
if ( isset( $_GET[$this->pluginPrefix.'_dismiss_notification']) && '0' == $_GET[$this->pluginPrefix.'_dismiss_notification'] ) |
| 203 |
add_user_meta( $user_id, $this->pluginPrefix.'_dismiss_notification', 'true', true ); |
| 204 |
|
| 205 |
do_action( $this->pluginPrefix.'_after_notification_dismissed', $current_user ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if ( PROFILE_BUILDER == 'Profile Builder Pro' ){ |
| 210 |
$wppb_profile_builder_pro_hobbyist_serial_status = get_option( 'wppb_profile_builder_pro_serial_status', 'empty' ); |
| 211 |
} elseif( PROFILE_BUILDER == 'Profile Builder Hobbyist' ) { |
| 212 |
$wppb_profile_builder_pro_hobbyist_serial_status = get_option( 'wppb_profile_builder_hobbyist_serial_status', 'empty' ); |
| 213 |
} |
| 214 |
|
| 215 |
if ( $wppb_profile_builder_pro_hobbyist_serial_status == 'notFound' || $wppb_profile_builder_pro_hobbyist_serial_status == 'empty' ){ |
| 216 |
new wppb_add_notices( 'wppb', 'profile_builder_pro', sprintf( __( '<p>Your <strong>Profile Builder</strong> serial number is invalid or missing. <br/>Please %1$sregister your copy%2$s of Profile Builder to receive access to automatic updates and support. Need a license key? %3$sPurchase one now%4$s</p>', 'profilebuilder'), "<a href='admin.php?page=profile-builder-register'>", "</a>", "<a href='http://www.cozmoslabs.com/wordpress-profile-builder/?utm_source=PB&utm_medium=dashboard&utm_campaign=PB-Purchase' target='_blank' class='button-primary'>", "</a>" ), 'wppb_profile_builder_pro_serial_status' ); |
| 217 |
} |
| 218 |
elseif ( $wppb_profile_builder_pro_hobbyist_serial_status == 'expired' ){ |
| 219 |
$wppb_notice = new wppb_add_notices( 'wppb', 'profile_builder_pro', sprintf( __( '<p>Your <strong>Profile Builder</strong> licence has expired. <br/>Please %1$sRenew Your Licence%2$s to receive access to automatic updates and support. %3$sPurchase one now%4$s %5$sDismiss%6$s</p>', 'profilebuilder'), "<a href='http://www.cozmoslabs.com/downloads/profile-builder-pro-1-year/?utm_source=PB&utm_medium=dashboard&utm_campaign=PB-Renewal' target='_blank'>", "</a>", "<a href='http://www.cozmoslabs.com/downloads/profile-builder-pro-1-year/?utm_source=PB&utm_medium=dashboard&utm_campaign=PB-Renewal' target='_blank' class='button-primary'>", "</a>", "<a href='". add_query_arg( 'wppb_dismiss_notification', '0' ) ."' class='wppb-dismiss-notification'>", "</a>" ), 'wppb_profile_builder_pro_serial_status' ); |
| 220 |
} |
| 221 |
elseif( strpos( $wppb_profile_builder_pro_hobbyist_serial_status, 'aboutToExpire' ) === 0 ){ |
| 222 |
$serial_status_parts = explode( '#', $wppb_profile_builder_pro_hobbyist_serial_status ); |
| 223 |
$date = $serial_status_parts[1]; |
| 224 |
new wppb_add_notices( 'wppb', 'profile_builder_pro', sprintf( __( '<p>Your <strong>Profile Builder</strong> serial number is about to expire on %5$s. <br/>Please %1$sRenew Your Licence%2$s to receive access to automatic updates and support. %3$sPurchase one now%4$s</p>', 'profilebuilder'), "<a href='admin.php?page=profile-builder-register'>", "</a>", "<a href='http://www.cozmoslabs.com/wordpress-profile-builder/?utm_source=PB&utm_medium=dashboard&utm_campaign=PB-Purchase' target='_blank' class='button-primary'>", "</a>", $date ), 'wppb_profile_builder_pro_serial_status' ); |
| 225 |
} |