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

"; if( $sby_check_license && !$sby_license_expired ){ $sby_expired_box_classes = "sby-license-expired sby-license-valid"; $sby_expired_box_msg = "Thanks ".$sby_license_data["customer_name"].", your YouTube Feeds Pro license key is valid."; } _e("
"); if( $sby_notice_dismissible ){ _e("Dismiss"); } _e("

".$sby_expired_box_msg."

"); } } /** * Admin header notices * * @since 2.0.2 */ public function sby_admin_header_license_notice () { if ( !sby_is_pro() ) { return; } 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() ) ) { echo $this->get_post_grace_period_header_notice( $inactive = 'sby-license-inactive-state' ); return; } // If license not expired then return; $license_expired = Util::is_license_expired(); if ( !$license_expired ) { return; } // Grace period ended? if ( Util::is_license_grace_period_ended( true ) ) { if ( get_option( 'sby_check_license_api_post_grace_period' ) !== 'false' ) { $license_expired = Util::sby_check_license( Util::get_license_key(), true, true ); } if ( $license_expired ) { echo $this->get_post_grace_period_header_notice(); } } return; } /** * Get post grace period header notice content * * @since 2.0 */ public function get_post_grace_period_header_notice( $license_status = 'expired' ) { $notice_text = 'Your YouTube Feeds Pro License has expired. Renew to keep using PRO features.'; if ( $license_status == 'sby-license-inactive-state' ) { $notice_text = 'Your license key is inactive. Please add license key to enable PRO features.'; } return '
'. $notice_text .' Learn More
'; } private function get_license_key() { return Util::get_license_key(); } private function recheck_license_status() { if ( empty( $this->get_license_key() ) ) { return []; } $license_last_check = get_option( 'sby_license_last_check_timestamp' ); $date = time() - ( DAY_IN_SECONDS * 90 ); if ( $date < $license_last_check ) { return $this->get_license_data(); } $sby_license = $this->get_license_key(); $this->sby_activate_license($sby_license); update_option( 'sby_license_last_check_timestamp', time() ); return $this->get_license_data(); } public function get_license_status() { return get_option('sby_license_status', 'inactive'); } public function get_license_data() { return $this->get_license_error_message( get_option( 'sby_license_data' ) ); } public function get_license_error_message( $license_data ) { global $sby_download_id; $license_key = $this->get_license_key(); $upgrade_url = sprintf( 'https://smashballoon.com/youtube-feed/pricing/?edd_license_key=%s&upgrade=true&utm_campaign=youtube-pro&utm_source=settings&utm_medium=upgrade-license', $license_key ); $renew_url = sprintf( 'https://smashballoon.com/checkout/?edd_license_key=%s&download_id=%s&utm_campaign=youtube-pro&utm_source=settings&utm_medium=upgrade-license&utm_content=renew-license', $license_key, $sby_download_id ); $learn_more_url = 'https://smashballoon.com/doc/my-license-key-wont-activate/?utm_campaign=youtube-pro&utm_source=settings&utm_medium=license&utm_content=learn-more'; // Check if the license key reached max site installations if ( !empty($license_data->error) && 'no_activations_left' === $license_data->error ) { $license_data->errorMsg = sprintf( '%s (%s/%s). %s %s %s %s', __( 'You have reached the maximum number of sites available in your plan', 'feeds-for-youtube' ), $license_data->site_count, $license_data->max_sites, __( 'Learn more about it', 'feeds-for-youtube' ), $learn_more_url, 'here', __( 'or upgrade your plan.', 'feeds-for-youtube' ), $upgrade_url, __( 'Upgrade', 'feeds-for-youtube' ) ); } // Check if the license key has expired if ( ( !empty( $license_data->license ) && 'expired' === $license_data->license ) || ( !empty( $license_data->error ) && 'expired' === $license_data->error ) ) { $license_data->error = true; $expired_date = new \DateTime( $license_data->expires ); $expired_date = $expired_date->format( 'F d, Y' ); $license_data->errorMsg = sprintf( '%s %s. %s %s', __( 'The license expired on ', 'feeds-for-youtube' ), $expired_date, __( 'Please renew it and try again.', 'feeds-for-youtube' ), $renew_url, __( 'Renew', 'feeds-for-youtube' ) ); } return $license_data; } /** * Get content for expired license notice * * @since 2.0 * * @return string $output */ public function get_expired_license_notice_content() { global $current_user; $current_screen = get_current_screen(); $output = '

Your license key has expired

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.

Renew License Why Renew? Re-check License Key
'; if ( ! empty( $current_screen->base ) && $current_screen->base == 'dashboard' ) { $output .= ''; } return $output; } /** * Get content for successfully renewed license notice * * @since 2.0 * * @return string $output */ public function get_renewed_license_notice_content() { $output = '

Thanks! Your license key is valid.

You can safely dismiss this modal.

Dismiss
'; return $output; } /** * Get modal content that will trigger by "Why Renew" button * * @since 2.0 * * @return string $output */ public function get_modal_content() { $output = '

Why Renew?

See below for why it\'s so important to keep an active plugin license.

Protected Against All Upcoming YouTube Platform Updates and API Changes

Don\'t worry about your YouTubes feeds breaking due to constant changes in the YouTube platform. Stay protected with access to continual plugin updates, giving you peace of mind that the software will always be up to date.

Expert Technical Support

Without a valid license key you will no longer be able to receive updates or support for the YouTube Feeds plugin. A renewed license key grants you access to our top-notch, quick and effective support for another full year.

WordPress Compatibility Updates

With WordPress updates being released continually, we make sure the plugin is always compatible with the latest version so you can update WordPress without needing to worry.

All Pro YouTube Feeds Features

Video Statistics, Live Streams, Search Feeds, PlayLists, Call to Action Settings, Carousel Layouts, Video filtering and more!

'; return $output; } /** * SBY Check License * * @since 2.0 */ public function check_license() { $sby_license = trim( get_option( 'sby_license_key' ) ); // Check the API $sby_api_params = array( 'edd_action'=> 'check_license', 'license' => $sby_license, 'item_name' => urlencode( SBY_PLUGIN_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 ) ); // Update the updated license data update_option( 'sby_license_data', $sby_license_data ); $sby_todays_date = date('Y-m-d'); // Check whether it's active if( $sby_license_data['license'] !== 'expired' && ( strtotime( $sby_license_data['expires'] ) > strtotime($sby_todays_date) ) ) { // if the license is active then lets remove the ignore check for dashboard so next time it will show the expired notice in dashboard screen update_user_meta( get_current_user_id(), 'sby_ignore_dashboard_license_notice', false ); wp_send_json_success( array( 'msg' => 'License Active', 'content' => $this->get_renewed_license_notice_content() ) ); } else { $content = $this->get_expired_license_notice_content(); $content = str_replace( 'Your YouTube Feeds Pro license key has expired', 'We rechecked but your license key is still expired', $content ); wp_send_json_success( array( 'msg' => 'License Not Renewed', 'content' => $content ) ); } } /** * SBY Dismiss Notice * * @since 2.0 */ public function dismiss_license_notice() { global $current_user; $user_id = $current_user->ID; update_user_meta( $user_id, 'sby_ignore_dashboard_license_notice', true ); } }