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'] .= '
'.$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;
?>
'.__('Support the plugin by setting good feedback.
We really need this.', 'BeRocket_domain').'