1497880000, // timestamp when notice start 'end' => 1497885000, // timestamp when notice end 'name' => 'name', //notice name must be unique for this time period 'html' => '', //text or html code as content of notice 'righthtml' => 'No thanks', //content in the right block, this is default value. This html code must be added to all notices 'rightwidth' => 80, //width of right content is static and will be as this value. berocket_no_thanks block is 60px and 20px is additional 'nothankswidth' => 60, //berocket_no_thanks width. set to 0 if block doesn't uses. Or set to any other value if uses other text inside berocket_no_thanks 'contentwidth' => 400, //width that uses for mediaquery is image_width + contentwidth + rightwidth 'subscribe' => false, //add subscribe form to the righthtml 'priority' => 20, //priority of notice. 1-5 is main priority and displays on settings page always 'height' => 50, //height of notice. image will be scaled 'repeat' => false, //repeat notice after some time. time can use any values that accept function strtotime 'repeatcount' => 1, //repeat count. how many times notice will be displayed after close 'image' => array( 'global' => 'http://berocket.com/images/logo-2.png', //image URL from other site. Image will be copied to uploads folder if it possible //'local' => 'http://wordpress-site.com/wp-content/uploads/logo-2.png', //notice will be used this image directly ), ));*/ //delete_option('berocket_admin_notices'); //remove all notice information //delete_option('berocket_last_close_notices_time'); //remove wait time before next notice //delete_option('berocket_admin_notices_rate_stars'); if( ! class_exists( 'berocket_admin_notices' ) ) { /** * Class berocket_admin_notices */ class berocket_admin_notices { public $find_names, $notice_exist = false; public static $last_time = '-24 hours'; public static $end_soon_time = '+1 hour'; public static $subscribed = false; public static $jquery_script_exist = false; public static $styles_exist = false; public static $notice_index = 0; public static $default_notice_options = array( 'start' => 0, 'end' => 0, 'name' => 'sale', 'html' => '', 'righthtml' => 'No thanks', 'rightwidth' => 80, 'nothankswidth' => 60, 'contentwidth' => 400, 'subscribe' => false, 'closed' => '0', 'priority' => 20, 'height' => 50, 'repeat' => false, 'repeatcount' => 1, 'image' => array( 'global' => 'http://berocket.com/images/logo-2.png' ), ); function __construct($options = array()) { if( ! is_admin() ) return; $options = array_merge(self::$default_notice_options, $options); self::set_notice_by_path($options); } public static function sort_notices($notices) { return self::sort_array ( $notices, array( 1 => 'krsort', 2 => 'ksort', 3 => 'ksort' ), array( '1' => SORT_NUMERIC, '2' => SORT_NUMERIC, '3' => SORT_NUMERIC ) ); } public static function sort_array($array, $sort_functions, $options, $count = 3) { if( $count > 0 ) { if( ! is_array($array) ) { return array(); } $call_function = $sort_functions[$count]; $call_function($array, $options[$count]); if( isset($array[0]) ) { $first_element = $array[0]; unset($array[0]); $array[0] = $first_element; unset($first_element); } foreach($array as $item_id => $item) { if( $count == 2 ) { $time = time(); if( $item_id < $time && $item_id != 0 ) { unset($array[$item_id]); } else { $array[$item_id] = self::sort_array($item, $sort_functions, $options, $count - 1); } } else { $array[$item_id] = self::sort_array($item, $sort_functions, $options, $count - 1); } if( isset($array[$item_id]) && ( ! is_array($array[$item_id]) || count($array[$item_id]) == 0 ) ) { unset($array[$item_id]); } } } return $array; } public static function get_notice_by_path($find_names) { $notices = get_option( 'berocket_admin_notices' ); if ( ! is_array( $notices ) ) { $notices = array(); } $current_notice = &$notices; foreach ( $find_names as $find_name ) { if ( isset( $current_notice[ $find_name ] ) ) { $new_current_notice = &$current_notice[ $find_name ]; unset( $current_notice ); $current_notice = &$new_current_notice; unset( $new_current_notice ); } else { unset( $current_notice ); break; } } if ( ! isset( $current_notice ) ) $current_notice = false; return $current_notice; } public static function berocket_array_udiff_assoc_notice($a1, $a2) { return json_encode($a1) > json_encode($a2); } public static function set_notice_by_path($options, $replace = false, $find_names = false) { self::$subscribed = get_option('berocket_email_subscribed'); if( self::$subscribed && $options['subscribe'] ) { return false; } $notices = get_option('berocket_admin_notices'); if( $options['end'] < time() && $options['end'] != 0 ) { return false; } if( $find_names === false ) { $find_names = array($options['priority'], $options['end'], $options['start'], $options['name']); } if( ! is_array($notices) ) { $notices = array(); } $current_notice = &$notices; foreach($find_names as $find_name) { if( ! isset($current_notice[$find_name]) ) { $current_notice[$find_name] = array(); } $new_current_notice = &$current_notice[$find_name]; unset($current_notice); $current_notice = &$new_current_notice; unset($new_current_notice); } $array_diff = array_udiff_assoc($options, $current_notice, array(__CLASS__, 'berocket_array_udiff_assoc_notice')); if( isset($array_diff['image']) ) { unset($array_diff['image']); } if( count($array_diff) == 0 ) { return true; } if( empty($options['image']) || (empty($options['image']['local']) && empty($options['image']['global'])) ) { $options['image'] = array('width' => 0, 'height' => 0, 'scale' => 0); } else { $file_exist = false; if( isset($options['image']['global']) ) { $wp_upload = wp_upload_dir(); if( ! isset($options['image']['local']) ) { $url_global = $options['image']['global']; $img_local = $wp_upload['basedir'] . '/' . basename($url_global); $url_local = $wp_upload['baseurl'] . '/' . basename($url_global); if( ! file_exists($img_local) && is_writable($wp_upload['path']) ) { file_put_contents($img_local, file_get_contents($url_global)); } if( file_exists($img_local) ) { $options['image']['local'] = $url_local; $options['image']['pathlocal'] = $img_local; } else { $options['image']['local'] = $url_global; $file_exist = true; } } } if( ! $file_exist ) { if( ! empty($options['image']['local']) ) { $img_local = $options['image']['local']; $img_local = str_replace(site_url('/'), '', $img_local); $img_local = ABSPATH . $img_local; $file_exist = ( file_exists($img_local) ); } else { $file_exist = false; } } if( $file_exist ) { $check_size = true; if( isset($current_notice['image']['local']) && $current_notice['image']['local'] == $options['image']['local'] ) { if( isset($current_notice['image']['width']) && isset($current_notice['image']['height']) ) { $options['image']['width'] = $current_notice['image']['width']; $options['image']['height'] = $current_notice['image']['height']; $check_size = false; } } if( $check_size ) { $image_size = @ getimagesize($options['image']['local']); if( ! empty($image_size[0]) && ! empty($image_size[1]) ) { $options['image']['width'] = $image_size[0]; $options['image']['height'] = $image_size[1]; } else { $options['image']['width'] = $options['height']; $options['image']['height'] = $options['height']; } } $options['image']['scale'] = $options['height'] / $options['image']['height']; } else { $options['image'] = array('width' => 0, 'height' => 0, 'scale' => 0); } } if( count($current_notice) == 0 ) { $current_notice = $options; } else { if( ! empty($options['image']['local']) && $options['image']['local'] != $current_notice['image']['local'] ) { if( isset($current_notice['image']['pathlocal']) ) { unlink($current_notice['image']['pathlocal']); } } if( ! $replace ) { $options['closed'] = $current_notice['closed']; } $current_notice = $options; } $notices = self::sort_notices($notices); update_option('berocket_admin_notices', $notices); return true; } public static function get_notice() { $notices = get_option('berocket_admin_notices'); $last_time = get_option('berocket_last_close_notices_time'); self::$subscribed = get_option('berocket_email_subscribed'); if( ! is_array($notices) || count($notices) == 0 ) return false; if( $last_time > strtotime(self::$last_time) ) { $current_notice = self::get_not_closed_notice($notices, true); } else { $current_notice = self::get_not_closed_notice($notices); } update_option('berocket_current_displayed_notice', $current_notice); return $current_notice; } public static function get_notice_for_settings() { $notices = get_option('berocket_admin_notices'); $last_notice = get_option('berocket_admin_notices_last_on_options'); self::$subscribed = get_option('berocket_email_subscribed'); $notices = self::get_notices_with_priority($notices); if( ! is_array($notices) || count($notices) == 0 ) { return false; } if( $last_notice === false ) { $last_notice = 0; } else { $last_notice++; } if( count($notices) <= $last_notice ) { $last_notice = 0; } update_option('berocket_admin_notices_last_on_options', $last_notice); $notice = $notices[$last_notice]; return $notice; } public static function get_not_closed_notice($array, $end_soon = false, $closed = 0, $count = 3) { $notice = false; if( empty($array) || ! is_array($array) ) { $array = array(); } $time = time(); foreach($array as $item_id => $item) { if( $count > 0 ) { if( $count == 2 && $item_id < $time && $item_id != 0 || $count == 1 && $item_id > $time && $item_id != 0 ) { continue; } if( $count == 2 && $item_id < strtotime(self::$end_soon_time) && $item_id != 0 ) { $notice = self::get_not_closed_notice($item, $end_soon, 1, $count - 1); } else { if( $end_soon && $count == 2 ) { break; } $notice = self::get_not_closed_notice($item, $end_soon, $closed, $count - 1); } } else { $display_notice = ( $item['closed'] <= $closed && ( ! self::$subscribed || ! $item['subscribe'] ) && ($item['start'] == 0 || $item['start'] < $time) && ($item['end'] == 0 || $item['end'] > $time) ); $display_notice = apply_filters( 'berocket_admin_notice_is_display_notice', $display_notice, $item, array( 'subscribed' => self::$subscribed, 'end_soon' => $end_soon, 'closed' => $closed, ) ); if( $display_notice ) { return $item; } } if( $notice != false ) break; } return $notice; } public static function get_notices_with_priority($array, $priority = 19, $count = 3) { if( empty($array) || ! is_array($array) ) { $array = array(); } $time = time(); $notices = array(); foreach($array as $item_id => $item) { if( $count > 0 ) { if( $count == 3 && $item_id > $priority || $count == 2 && $item_id < $time && $item_id != 0 || $count == 1 && $item_id > $time && $item_id != 0 ) { continue; } $notice = self::get_notices_with_priority($item, $priority, $count - 1); $notices = array_merge($notices, $notice); } else { $display_notice = ( (!self::$subscribed || ! $item['subscribe']) && ($item['priority'] <= 5 || !$item['closed']) ); $display_notice = apply_filters( 'berocket_admin_notice_is_display_notice_priority', $display_notice, $item, array( 'subscribed' => self::$subscribed, 'priority' => $priority, ) ); if( $display_notice ) { $notices[] = $item; } } } return $notices; } public static function display_admin_notice() { $settings_page = apply_filters('is_berocket_settings_page', false); if( $settings_page ) { $notice = self::get_notice_for_settings(); } else { $notice = self::get_notice(); } if( ! empty($notice['original']) ) { $original_notice = self::get_notice_by_path($notice['original']); unset($original_notice['start'], $original_notice['closed'], $original_notice['repeatcount']); $notice = array_merge($notice, $original_notice); } if( $notice !== false ) { self::echo_notice($notice); } $additional_notice = apply_filters('berocket_display_additional_notices', array()); if( is_array($additional_notice) && count($additional_notice) > 0 ) { foreach($additional_notice as $notice) { if( is_array($notice) ) { self::echo_notice($notice); } } } } public static function echo_notice($notice) { $notice = array_merge(self::$default_notice_options, $notice); $settings_page = apply_filters('is_berocket_settings_page', false); self::$notice_index++; $notice_data = array( 'start' => $notice['start'], 'end' => $notice['end'], 'name' => $notice['name'], 'priority' => $notice['priority'], ); if( $notice['end'] < strtotime(self::$end_soon_time) && $notice['end'] != 0 ) { $time_left = $notice['end'] - time(); $time_left_str = ""; $time = $time_left; if ( $time >= 3600 ) { $hours = floor( $time/3600 ); $time = $time%3600; $time_left_str .= sprintf("%02d", $hours) . ":"; } if ( $time >= 60 || $time_left >= 3600 ) { $minutes = floor( $time/60 ); $time = $time%60; $time_left_str .= sprintf("%02d", $minutes) . ":"; } $time_left_str .= sprintf("%02d", $time); $notice['rightwidth'] += 60; $notice['righthtml'] .= '
Left
' . $time_left_str . '
'; } if( ! empty($notice['subscribe']) ) { $user_email = wp_get_current_user(); if( isset($user_email->user_email) ) { $user_email = $user_email->user_email; } else { $user_email = ''; } $notice['righthtml'] = '
' . $notice['righthtml']; $notice['rightwidth'] += 300; } echo '
', ( empty($notice['image']['local']) ? '' : '' ), ( empty($notice['righthtml']) ? '' : '
' . $notice['righthtml'] . '
' ), '
', $notice['html'], '
'; if( $settings_page && $notice['priority'] <= 5 ) { $notice['rightwidth'] -= $notice['nothankswidth']; } echo ' '; self::echo_styles(); self::echo_jquery_functions(); } public static function echo_styles() { if( ! self::$styles_exist ) { self::$styles_exist = true; echo ''; } } public static function echo_jquery_functions() { if( ! self::$jquery_script_exist ) { self::$jquery_script_exist = true; echo ''; } } public static function close_notice($notice = FALSE) { self::$subscribed = get_option('berocket_email_subscribed'); if( ( $notice == FALSE || ! is_array($notice) ) && ! empty($_POST['notice']) ) { $notice = sanitize_textarea_field($_POST['notice']); } if (empty($notice) || ! is_array($notice) || (empty($notice['start']) && $notice['start'] !== '0') || (empty($notice['end']) && $notice['end'] !== '0') || (empty($notice['priority']) && $notice['priority'] !== '0') || (empty($notice['name'])) ) { $notice = self::get_notice(); } if( empty($notice) || ! is_array($notice) ) { wp_die(); } $find_names = array($notice['priority'], $notice['end'], $notice['start'], $notice['name']); $current_notice = self::get_notice_by_path($find_names); if( isset($current_notice) ) { if( $current_notice['end'] < strtotime(self::$end_soon_time) ) { $current_notice['closed'] = 2; } else { $current_notice['closed'] = 1; } if( $current_notice['closed'] < 2 && ! empty($current_notice['repeat']) && ! empty($current_notice['repeatcount']) && ( ! self::$subscribed || ! $current_notice['subscribe'] ) ) { $new_notice = $current_notice; if( empty($current_notice['original']) ) { $new_notice['original'] = $find_names; } $new_notice['repeatcount'] = $current_notice['repeatcount'] - 1; $new_notice['start'] = strtotime($current_notice['repeat']); $new_notice['closed'] = 0; self::set_notice_by_path($new_notice); } self::set_notice_by_path($current_notice, true); } update_option('berocket_last_close_notices_time', time()); wp_die(); } public static function subscribe() { if( ! empty($_POST['email']) ) { $plugins = array(); if( ! empty($_POST['plugin']) ) { $plugins[] = sanitize_textarea_field($_POST['plugin']); } $plugins = apply_filters('berocket_admin_notices_subscribe_plugins', $plugins); $plugins = array_unique($plugins); $plugins = implode(',', $plugins); $email = sanitize_email($_POST['email']); update_option('berocket_email_subscribed', true); $response = wp_remote_post('https://berocket.com/main/subscribe', array( 'body' => array( 'subs_email' => $email, 'plugins' => $plugins ), 'method' => 'POST', 'timeout' => 15, 'redirection' => 5, 'blocking' => true, 'sslverify' => false )); if( ! is_wp_error($response) ) { $out = wp_remote_retrieve_body($response); echo $out; } } wp_die(); } public static function generate_subscribe_notice() { new berocket_admin_notices(array( 'start' => 0, 'end' => 0, 'name' => 'subscribe', 'html' => 'Subscribe to get latest BeRocket news and updates, plugin recommendations and configuration help, promotional email with discount codes.', 'subscribe' => true, 'image' => array( 'local' => plugin_dir_url( __FILE__ ) . '../assets/images/ad_white_on_orange.png', ), )); } } add_action( 'admin_notices', array('berocket_admin_notices', 'display_admin_notice') ); add_action( 'wp_ajax_berocket_admin_close_notice', array('berocket_admin_notices', 'close_notice') ); add_action( 'wp_ajax_berocket_subscribe_email', array('berocket_admin_notices', 'subscribe') ); } if( ! class_exists( 'berocket_admin_notices_rate_stars' ) ) { class berocket_admin_notices_rate_stars { public $first_time = '+7 days'; public $later_time = '+7 days'; function __construct() { add_action( 'admin_notices', array($this, 'admin_notices') ); add_action( 'wp_ajax_berocket_rate_stars_close', array($this, 'disable_rate_notice') ); add_action( 'wp_ajax_berocket_feature_request_send', array($this, 'feature_request_send') ); add_action( 'berocket_rate_plugin_window', array($this, 'show_rate_window'), 10, 2 ); add_action( 'berocket_feature_request_window', array($this, 'show_feature_request_window'), 10, 2 ); } function admin_notices() { $display_one = false; $disabled = get_option('berocket_admin_notices_rate_stars'); if( ! is_array($disabled) ) { $disabled = array(); } $plugins = apply_filters('berocket_admin_notices_rate_stars_plugins', array()); foreach($plugins as $plugin_id => $plugin) { $display = false; if( empty($disabled[$plugin['id']]) ) { $disabled[$plugin['id']] = array( 'time' => strtotime($this->first_time), 'count' => 0 ); } elseif($disabled[$plugin['id']]['time'] != 0 && $disabled[$plugin['id']]['time'] < time()) { $display = true; } if( $display ) { $display_one = true; ?>

'.$plugin['name'].'', $text); $text_mobile = str_replace('%plugin_name%', ''.$plugin['name'].'', $text_mobile); $text = '' . $text_mobile.'

'.__('Each good feedback is very important for plugin growth', 'BeRocket_domain').'

\' href="https://wordpress.org/support/plugin/'.$plugin['free_slug'].'/reviews/?filter=5#new-post" target="_blank">'.__('Ok, you deserved it', 'BeRocket_domain').' | '.__('Maybe later', 'BeRocket_domain').' '.__('Later', 'BeRocket_domain').' | '.__('I already did', 'BeRocket_domain').' '.__('Already', 'BeRocket_domain').' ' . $text.''; echo $text; ?>

later_time); } } update_option('berocket_admin_notices_rate_stars', $disabled); wp_die(); } function feature_request_send() { $plugin = (empty($_GET['brfeature_plugin']) ? (empty($_POST['brfeature_plugin']) ? '' : $_POST['brfeature_plugin']) : $_GET['brfeature_plugin']); $email = (empty($_GET['brfeature_email']) ? (empty($_POST['brfeature_email']) ? '' : $_POST['brfeature_email']) : $_GET['brfeature_email']); $title = (empty($_GET['brfeature_title']) ? (empty($_POST['brfeature_title']) ? '' : $_POST['brfeature_title']) : $_GET['brfeature_title']); $description = (empty($_GET['brfeature_description']) ? (empty($_POST['brfeature_description']) ? '' : $_POST['brfeature_description']) : $_GET['brfeature_description']); if( ! empty($plugin) && ! empty($title) && ! empty($description) ) { $response = wp_remote_post( 'https://berocket.com/api/data/add_feature_request', array( 'body' => array( 'plugin' => $plugin, 'email' => $email, 'title' => $title, 'description' => $description ), 'method' => 'POST', 'timeout' => 5, 'redirection' => 5, 'blocking' => true, 'sslverify' => false ) ); } wp_die(); } function show_rate_window($html, $plugin_id) { $disabled = get_option('berocket_admin_notices_rate_stars'); if( empty($disabled[$plugin_id]) || $disabled[$plugin_id]['time'] != 0 ) { $plugins = apply_filters('berocket_admin_notices_rate_stars_plugins', array()); foreach($plugins as $plugin) { if( $plugin['id'] == $plugin_id ) { $html = '

'.__('May we ask you to give us a 5-star feedback?', 'BeRocket_domain').'

'.__('Each good feedback is very important for plugin growth', 'BeRocket_domain').'

\' href="https://wordpress.org/support/plugin/'.$plugin['free_slug'].'/reviews/?filter=5#new-post" target="_blank">'.__('Ok, you deserved it', 'BeRocket_domain').'

'.__('Support the plugin by setting good feedback.
We really need this.', 'BeRocket_domain').'

'; add_action('admin_footer', array($this, 'wp_footer_js')); return $html; } } } return $html; } function show_feature_request_window($html, $plugin_id) { $disabled = get_option('berocket_admin_notices_rate_stars'); $plugins = apply_filters('berocket_admin_notices_rate_stars_plugins', array()); foreach($plugins as $plugin) { if( $plugin['id'] == $plugin_id ) { add_action('admin_footer', array($this, 'wp_footer_js')); $html .= '
Feature Request
'; return $html; } } } function wp_footer_js() { ?>