license_service = new LicenseNotification();
}
public function register() {
/* Display a license expired notice */
add_action('sby_admin_notices', [$this, 'sby_renew_license_notice']);
add_action('sby_admin_header_notices', [$this, 'sby_admin_header_license_notice']);
// Add extra localize script items
add_filter('sby_localized_settings', [$this, 'localized_license_settings']);
add_action('wp_ajax_sby_check_connection', [$this, 'test_connection']);
add_action('wp_ajax_sby_license_activation', [$this, 'ajax_activate_license']);
add_action('wp_ajax_sby_license_deactivation', [$this, 'ajax_deactivate_license']);
add_action( 'wp_ajax_sby_check_license', [ $this, 'check_license' ] );
add_action( 'wp_ajax_sby_dismiss_license_notice', [ $this, 'dismiss_license_notice' ] );
}
public function localized_license_settings($settings) {
$license_key = $this->get_license_key();
$settings['licenseStatus'] = $this->get_license_status();
$settings['licenseData'] = $this->get_license_data();
$settings['licenseKey'] = $license_key;
$settings['upgradeUrl'] = sprintf( 'https://smashballoon.com/youtube-feed/pricing/?edd_license_key=%s&upgrade=true&utm_campaign=youtube-pro&utm_source=settings&utm_medium=upgrade-license', $this->get_license_key() );
return $settings;
}
public function test_connection() {
check_ajax_referer( 'sby-admin', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error(); // This auto-dies.
}
$api_params = array(
'edd_action' => 'check_license',
'license' => $this->get_license_key(),
'item_name' => urlencode( SBY_PLUGIN_EDD_NAME ), // the name of our product in EDD
);
$url = add_query_arg( $api_params, SBY_STORE_URL );
$args = array(
'timeout' => 60,
);
// Make the remote API request
$request = HTTP_Request::request( 'GET', $url, $args );
if ( HTTP_Request::is_error( $request ) ) {
$response = new Response(
false,
array(
'hasError' => true,
)
);
$response->send();
}
$response = new Response(
true,
array(
'hasError' => false,
)
);
$response->send();
}
public function ajax_deactivate_license() {
check_ajax_referer( 'sby-admin', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error(); // This auto-dies.
}
$response = $this->sby_deactivate_license();
if ( $response === true ) {
wp_send_json_success( [
'licenseStatus' => $this->get_license_status(),
'licenseData' => $this->get_license_data()
] );
}
wp_send_json_error();
}
public function ajax_activate_license() {
check_ajax_referer( 'sby-admin', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error(); // This auto-dies.
}
$license_key = sanitize_text_field($_POST['license_key']);
$response = $this->sby_activate_license($license_key);
if ( $response === true ) {
wp_send_json_success( [
'licenseStatus' => $this->get_license_status(),
'licenseData' => $this->get_license_data()
] );
}
wp_send_json_error();
}
public function sby_activate_license($license_key) {
// retrieve the license from the database
$sby_license = trim( $license_key );
// data to send in our API request
$api_params = array(
'edd_action' => 'activate_license',
'license' => $sby_license,
'item_name' => urlencode( SBY_PLUGIN_EDD_NAME ), // the name of our product in EDD
'url' => home_url()
);
// Call the custom API.
$response = wp_remote_get( add_query_arg( $api_params, SBY_STORE_URL ),
array( 'timeout' => 15, 'sslverify' => false ) );
// make sure the response came back okay
if ( is_wp_error( $response ) ) {
return false;
}
// decode the license data
$sby_license_data = json_decode( wp_remote_retrieve_body( $response ) );
if (
isset( $sby_license_data->success ) && ( $sby_license_data->success == false ) ||
isset( $sby_license_data->error ) && ( $sby_license_data->error == 'missing' ) ||
isset( $sby_license_data->license ) && (
$sby_license_data->license == 'invalid_item_id' ||
$sby_license_data->license == 'invalid' ||
$sby_license_data->license == 'expired'
)
) {
return false;
}
// only store the license key
update_option( 'sby_license_key', $license_key );
//store the license data in an option
update_option( 'sby_license_data', $sby_license_data );
// $license_data->license will be either "valid" or "invalid"
update_option( 'sby_license_status', $sby_license_data->license );
// make license check_api true so next time it expires it checks again
update_option( 'sby_check_license_api_when_expires', 'true' );
update_option( 'sby_check_license_api_post_grace_period', 'true' );
return true;
}
public function sby_deactivate_license() {
// retrieve the license from the database
$sby_license= trim( get_option( 'sby_license_key' ) );
// data to send in our API request
$api_params = array(
'edd_action'=> 'deactivate_license',
'license' => $sby_license,
'item_name' => urlencode( SBY_PLUGIN_EDD_NAME ), // the name of our product in EDD
'url' => home_url()
);
// Call the custom API.
$response = wp_remote_get( add_query_arg( $api_params, SBY_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );
// make sure the response came back okay
if ( is_wp_error( $response ) ) {
return false;
}
// decode the license data
$sby_license_data = json_decode( wp_remote_retrieve_body( $response ) );
// $license_data->license will be either "deactivated" or "failed"
if( $sby_license_data->license == 'deactivated' || $sby_license_data->license == 'failed' ) {
delete_option( 'sby_license_data' );
delete_option( 'sby_license_status' );
}
return true;
}
/**
* Check license key
*
* @since 2.0.2
*/
public function sby_check_license( $sby_license, $check_license_status = false ) {
// data to send in our API request
$sby_api_params = array(
'edd_action'=> 'check_license',
'license' => $sby_license,
'item_name' => urlencode( SBY_PLUGIN_NAME ) // the name of our product in EDD
);
$api_url = add_query_arg( $sby_api_params, SBY_STORE_URL );
$args = array(
'timeout' => 60,
'sslverify' => false
);
// Call the custom API.
$request = wp_remote_get( $api_url, $args );
if ( is_wp_error( $request ) ) {
return;
}
// decode the license data
$sby_license_data = json_decode( wp_remote_retrieve_body( $request ) );
if ( $check_license_status ) {
//Check whether it's active
if( $sby_license_data['license'] !== 'expired' && ( strtotime( $sby_license_data['expires'] ) > strtotime( $sby_todays_date ) ) ){
$sby_license_status = false;
} else {
$sby_license_status = true;
//Set a flag so it doesn't check the API again until the next time it expires
update_option( 'sby_check_license_api_when_expires', 'false' );
}
return $sby_license_status;
}
//Store license data in db
update_option( 'sby_license_data', $sby_license_data );
return $sby_license_data;
}
public function sby_renew_license_notice() {
if ( !current_user_can( Util::sby_capability_check() ) ) {
return;
}
// We will display the license notice only on specified allowed screens
if ( ! Util::isCurrentScreenAllowed() ) {
return;
}
// Check that the license exists and the user hasn't already clicked to ignore the message
if ( empty( Util::get_license_key() ) ) {
return;
}
// If license not expired then return;
if ( !Util::is_license_expired() ) {
return;
}
// Grace period ended?
if ( Util::is_license_grace_period_ended() ) {
return;
}
// So, license has expired and grace period active
// Lets display the error notice
echo $this->get_expired_license_notice_content();
}
public function old_sby_renew_license_notice() {
//Show this notice on every page apart from the YouTube Feed settings pages
isset($_GET['page'])? $sby_check_page = $_GET['page'] : $sby_check_page = '';
( $sby_check_page == 'youtube-feed' || $sby_check_page == 'sby_single_settings' || $sby_check_page == 'youtube-feed_license' ) ? $sby_notice_dismissible = false : $sby_notice_dismissible = true;
//If the user is re-checking the license key then use the API below to recheck it
( isset( $_GET['sbychecklicense'] ) ) ? $sby_check_license = true : $sby_check_license = false;
$sby_license = trim( get_option( 'sby_license_key' ) );
global $current_user;
$user_id = $current_user->ID;
// Use this to show notice again
// delete_user_meta($user_id, 'sby_ignore_notice');
/* Check that the license exists and the user hasn't already clicked to ignore the message */
if( empty($sby_license) || !isset($sby_license) || ( ( get_user_meta( $user_id,
'sby_ignore_notice' ) && $sby_notice_dismissible ) && ! $sby_check_license ) ) {
return;
}
//Is there already license data in the db?
if( get_option( 'sby_license_data' ) && !$sby_check_license ){
//Yes
//Get license data from the db and convert the object to an array
$sby_license_data = (array) get_option( 'sby_license_data' );
} else {
//No
// data to send in our API request
$sby_api_params = array(
'edd_action'=> 'check_license',
'license' => $sby_license,
'item_name' => urlencode( SBY_PLUGIN_EDD_NAME ) // the name of our product in EDD
);
// Call the custom API.
$sby_response = wp_remote_get( add_query_arg( $sby_api_params, SBY_STORE_URL ), array( 'timeout' => 60, 'sslverify' => false ) );
// decode the license data
$sby_license_data = (array) json_decode( wp_remote_retrieve_body( $sby_response ) );
//Store license data in db
update_option( 'sby_license_data', $sby_license_data );
}
//Number of days until license expires
$sby_license_expires_date = isset( $sby_license_data['expires'] ) ? $sby_license_data['expires'] : $sby_license_expires_date = '2036-12-31 23:59:59'; //If expires param isn't set yet then set it to be a date to avoid PHP notice
if( $sby_license_expires_date == 'lifetime' ) $sby_license_expires_date = '2036-12-31 23:59:59';
$sby_todays_date = date('Y-m-d');
$sby_interval = round(abs(strtotime($sby_todays_date . ' -1 day')-strtotime($sby_license_expires_date))/86400); //-1 day to make sure auto-renewal has run before showing expired
//Is license expired?
if( $sby_interval == 0 || strtotime($sby_license_expires_date) < strtotime($sby_todays_date) ){
//If we haven't checked the API again one last time before displaying the expired notice then check it to make sure the license hasn't been renewed
if( get_option( 'sby_check_license_api_when_expires' ) == FALSE || get_option( 'sby_check_license_api_when_expires' ) == 'true' ){
// Check the API
$sby_api_params = array(
'edd_action'=> 'check_license',
'license' => $sby_license,
'item_name' => urlencode( SBY_PLUGIN_EDD_NAME ) // the name of our product in EDD
);
$sby_response = wp_remote_get( add_query_arg( $sby_api_params, SBY_STORE_URL ), array( 'timeout' => 60, 'sslverify' => false ) );
$sby_license_data = (array) json_decode( wp_remote_retrieve_body( $sby_response ) );
//Check whether it's active
if( $sby_license_data['license'] !== 'expired' && ( strtotime( $sby_license_data['expires'] ) > strtotime($sby_todays_date) ) ){
$sby_license_expired = false;
} else {
$sby_license_expired = true;
//Set a flag so it doesn't check the API again until the next time it expires
update_option( 'sby_check_license_api_when_expires', 'false' );
}
//Store license data in db
update_option( 'sby_license_data', $sby_license_data );
} else {
//Display the expired notice
$sby_license_expired = true;
}
} else {
$sby_license_expired = false;
//License is not expired so change the check_api setting to be true so the next time it expires it checks again
update_option( 'sby_check_license_api_when_expires', 'true' );
}
//If expired date is returned as 1970 (or any other 20th century year) then it means that the correct expired date was not returned and so don't show the renewal notice
if( $sby_license_expires_date[0] == '1' ) $sby_license_expired = false;
//If there's no expired date then don't show the expired notification
if( empty($sby_license_expires_date) || !isset($sby_license_expires_date) ) $sby_license_expired = false;
//Is license missing - ie. on very first check
if( isset($sby_license_data['error']) ){
if( $sby_license_data['error'] == 'missing' ) $sby_license_expired = false;
}
//Is the license expired?
if( $sby_license_expired || $sby_check_license ) {
global $sby_download_id;
$sby_expired_box_classes = "sby-license-expired";
$sby_expired_box_msg = '';
$sby_expired_box_msg .= "Important: Your YouTube Feeds Pro license key has expired.
You are no longer receiving updates that protect you against upcoming YouTube platform changes.";
//Create the re-check link using the existing query string in the URL
$sby_url = '?' . $_SERVER["QUERY_STRING"];
//Determine the separator
( !empty($sby_url) && $sby_url != '' ) ? $separator = '&' : $separator = '';
//Add the param to check license if it doesn't already exist in URL
if( strpos($sby_url, 'sbychecklicense') === false ) $sby_url .= $separator . "sbychecklicense=true";
//Create the notice message
$sby_expired_box_msg .= " Renew LicenseWhy renew? Re-check License
".$sby_expired_box_msg."
You are no longer receiving updates that protect you against upcoming YouTube changes. There’s a 14 day grace period before access to some Pro features in the plugin will be limited.