function birthdays_widget_check_for_birthdays($all = false)
{
    global $wpdb;
    $table_name = $wpdb->prefix . "birthdays";
    if ($all) {
        $query = "SELECT * FROM {$table_name} ORDER BY DATE_FORMAT(date, '%m-%d');";
        $results = $wpdb->get_results($query);
    } else {
        $query = "SELECT * FROM {$table_name} WHERE date LIKE '%%%s' ;";
        $results = $wpdb->get_results($wpdb->prepare($query, date_i18n('-m-d')));
    }
    $birthdays_settings = get_option('birthdays_settings');
    $birthdays_settings = maybe_unserialize($birthdays_settings);
    //If birthdays for WordPress Users are drawn from a meta key of their profile
    if ($birthdays_settings['date_from_profile']) {
        $birthday_date_meta_field = $birthdays_settings['date_meta_field'];
        $meta_key = $birthdays_settings['meta_field'];
        $users = get_users();
        foreach ($users as $user) {
            //If this meta key exists for this user, and it's his/her birthday
            if (isset($user->{$birthday_date_meta_field}) && !empty($user->{$birthday_date_meta_field})) {
                $date = date("-m-d", strtotime(str_replace('/', '-', $user->{$birthday_date_meta_field})));
                if (!$all && $date == date_i18n('-m-d') || $all) {
                    $tmp_user = new stdClass();
                    $tmp_user->name = $user->{$meta_key};
                    $tmp_user->email = $user->user_email;
                    $tmp_user->date = date("Y-m-d", strtotime(str_replace('/', '-', $user->{$birthday_date_meta_field})));
                    //If user's image is drawn from Gravatar
                    if ($birthdays_settings['wp_user_gravatar']) {
                        $tmp_user->image = Birthdays_Widget_Settings::get_avatar_url($tmp_user->email, 256);
                    }
                    array_push($results, $tmp_user);
                }
            }
        }
    }
    return $results;
}
 public static function birthdays_code($instance, $birthdays = NULL)
 {
     wp_enqueue_style('birthdays-css');
     $html = "";
     $birthdays_settings = get_option('birthdays_settings');
     $birthdays_settings = maybe_unserialize($birthdays_settings);
     if (isset($instance['img_width'])) {
         $birthdays_settings['image_width'] = $instance['img_width'];
     }
     if (!isset($instance['class'])) {
         $instance['class'] = '';
     }
     if (!isset($instance['template'])) {
         $instance['template'] = 0;
     }
     $html .= "<div class=\"birthdays-widget {$instance['class']}\">";
     if ($birthdays_settings['image_enabled']) {
         $tmp_size = $birthdays_settings['image_width'];
         if (is_numeric($birthdays_settings['image_url'])) {
             $default_image_src = wp_get_attachment_image_src($birthdays_settings['image_url'], 'medium');
             $default_image_src = $default_image_src[0];
         } else {
             $default_image_src = $birthdays_settings['image_url'];
         }
         $html .= "<img style=\"width: {$birthdays_settings['image_width']}\" \r\n                    src=\"{$default_image_src}\" alt=\"birthday_cake\" class=\"aligncenter\" />";
     }
     if ($birthdays_settings['user_image_enabled']) {
         if (is_numeric($birthdays_settings['user_image_url'])) {
             $default_user_image_src = wp_get_attachment_image_src($birthdays_settings['user_image_url'], 'medium');
             $default_user_image_src = $default_user_image_src[0];
         } else {
             $default_user_image_src = $birthdays_settings['user_image_url'];
         }
     }
     $html .= "<div class=\"birthday-wish\">{$birthdays_settings['wish']}</div>";
     /*
      * For each user that has birthday today, if his name is
      * in the cs_birth_widg_# format (which means he is a WP User),
      * show his name if and only if the option to 
      * save Users' birthdays in our table is enabled.
      */
     $meta_key = $birthdays_settings['meta_field'];
     $prefix = "cs_birth_widg_";
     $filtered = array();
     $year = true;
     foreach ($birthdays as $row) {
         //Check if this is record represents a WordPress user
         $wp_usr = strpos($row->name, $prefix);
         //var_dump( $row );
         if (is_numeric($row->image) || $row->image == NULL) {
             if ($instance['template'] == 2) {
                 $row->image = wp_get_attachment_image_src($row->image, array(150, 150));
             } else {
                 $row->image = wp_get_attachment_image_src($row->image, 'medium');
             }
             $row->image = $row->image[0];
         }
         if ($wp_usr !== false) {
             //If birthdays are disabled for WP Users, or birthday date is drown from WP Profile, skip the record
             if ($birthdays_settings['profile_page'] == 0 && $birthdays_settings['date_from_profile'] == 0 || $birthdays_settings['date_from_profile']) {
                 continue;
             }
             //Get the ID from the record, which is of the format $prefixID and get the user's data
             $birth_user = get_userdata(substr($row->name, strlen($prefix)));
             //If user's image is drawn from Gravatar
             if ($birthdays_settings['wp_user_gravatar']) {
                 if ($instance['template'] == 2) {
                     $row->image = Birthdays_Widget_Settings::get_avatar_url($birth_user->user_email, 96);
                 } else {
                     $row->image = Birthdays_Widget_Settings::get_avatar_url($birth_user->user_email, 256);
                 }
             }
             //If birthdays are enabled for WP Users, draw user's name from the corresponding meta key
             if ($birthdays_settings['profile_page']) {
                 $row->name = $birth_user->{$meta_key};
             }
         }
         //If user has no image, set the default
         if ((!isset($row->image) || empty($row->image)) && $birthdays_settings['user_image_enabled']) {
             $row->image = $default_user_image_src;
         }
         array_push($filtered, $row);
     }
     switch ($instance['template']) {
         case 0:
             wp_enqueue_script('jquery-ui-tooltip');
             wp_enqueue_script('birthdays-script');
             wp_enqueue_style('jquery-style');
             $flag = false;
             foreach ($filtered as $row) {
                 $html .= '<div class="birthday_element birthday_name">';
                 if ($flag && $birthdays_settings['comma']) {
                     $html .= ', ';
                 } else {
                     $flag = true;
                 }
                 $html .= $row->name;
                 $age = date("Y") - date("Y", strtotime($row->date));
                 $html .= '<a href="' . $row->image . '" target="_blank" ';
                 if ($birthdays_settings['user_age']) {
                     $html .= 'data-age="' . $age . ' ' . __('years old', 'birthdays-widget') . '" ';
                 }
                 $html .= '></a></div>';
             }
             break;
         case 1:
             $html .= '<ul class="birthday_list">';
             foreach ($filtered as $row) {
                 $html .= "<li class=\"birthday_name\"><img style=\"width:{$birthdays_settings['list_image_width']}\" \r\n                                    src=\"{$row->image}\" class=\"birthday_list_image\" />{$row->name}";
                 if ($birthdays_settings['user_age']) {
                     $age = date("Y") - date("Y", strtotime($row->date));
                     $html .= '<span class="birthday_age"> ' . $age . ' ' . __('years old', 'birthdays-widget') . '</span>';
                 }
                 $html .= "</li>";
             }
             $html .= '</ul>';
             break;
         case 2:
             if (defined('CALENDAR')) {
                 $html .= "<span class=\"description\">" . __('Only one calendar template is available per page. Please check your widget and shortcode options.', 'birthdays-widget') . "</span>";
                 break;
             }
             define('CALENDAR', true);
             $days_organized = self::organize_days($filtered);
             wp_enqueue_style('birthdays-bootstrap-css');
             wp_enqueue_style('birthdays-calendar-css');
             wp_enqueue_script('birthdays-bootstrap-js');
             wp_enqueue_script('birthdays-calendar-js');
             global $wp_locale;
             $months = array();
             for ($i = 1; $i <= 12; $i++) {
                 $months[] = $wp_locale->get_month($i);
             }
             $week_days = array();
             for ($i = 0; $i <= 6; $i++) {
                 $week_days[] = $wp_locale->get_weekday_abbrev($wp_locale->get_weekday($i));
             }
             $week_days[] = array_shift($week_days);
             if (get_locale() == 'el') {
                 for ($i = 0; $i <= 11; $i++) {
                     $months[$i] = mb_strcut($months[$i], 0, strlen($months[$i]) - 1);
                     $months[$i] .= "ς";
                 }
             }
             $months = implode('", "', $months);
             $months = '[ "' . $months . '" ]';
             $week_days = implode('", "', $week_days);
             $week_days = '[ "' . $week_days . '" ]';
             $html .= '<script>
                     jQuery( document ).ready( function() {
                         var monthNames = ' . $months . ';
                         var dayNames = ' . $week_days . ';
                         var events = [ ';
             $flag = false;
             foreach ($days_organized as $day) {
                 $html .= '{ date: "' . date('j/n', strtotime($day[0]->date)) . '/' . date('Y') . '",';
                 $html .= 'title: \'' . $birthdays_settings['wish'] . '\',';
                 if (date('m-d', strtotime($day[0]->date)) == date('m-d')) {
                     $color = $birthdays_settings['color_current_day'];
                 } else {
                     if ($flag && $birthdays_settings['second_color']) {
                         $color = $birthdays_settings['color_two'];
                         $flag = false;
                     } else {
                         $color = $birthdays_settings['color_one'];
                         $flag = true;
                     }
                 }
                 $html .= ' color: "' . $color . '",';
                 $html .= ' content: \'';
                 $comma = false;
                 foreach ($day as $user) {
                     $html .= '<img src="' . $user->image . '" width="150" /><div class="birthday_center birthday_name">' . $user->name;
                     if ($birthdays_settings['user_age']) {
                         $age = date("Y") - date("Y", strtotime($user->date));
                         $html .= '<span class="birthday_age"> ' . $age . ' ' . __('years old', 'birthdays-widget') . '</span>';
                     }
                     $html .= '</div>';
                 }
                 $html .= '\' }, ';
             }
             $html .= ' ];';
             $html .= "\r\n                                jQuery( '#birthday_calendar' ).bic_calendar( {\r\n                                    events: events,\r\n                                    dayNames: dayNames,\r\n                                    monthNames: monthNames,\r\n                                    showDays: true,\r\n                                    displayMonthController: true,\r\n                                    displayYearController: false\r\n                                } );\r\n                            ";
             $html .= "jQuery( '#bic_calendar_'+'";
             $html .= date('d_m_Y');
             $html .= "' ).addClass( 'selection' ); ";
             $html .= '} );';
             $html .= '</script>';
             $html .= '<div id="birthday_calendar"></div>';
             break;
         case 3:
             wp_enqueue_script('jquery-ui-tooltip');
             wp_enqueue_script('birthdays-script');
             wp_enqueue_style('jquery-style');
             $days_organized = self::organize_days($filtered);
             //TODO get current day in format MM-DD
             $today_key = date('m-d');
             //var_dump( $today_key );
             $upcoming_days = $birthdays_settings['upcoming_days_birthdays'];
             $consecutive_days = $birthdays_settings['upcoming_consecutive_days'];
             $upcoming_mode = $birthdays_settings['upcoming_mode'];
             /* If today is not in the array, add the key and sort the array again */
             if (!array_key_exists($today_key, $days_organized)) {
                 $days_organized[$today_key] = array();
                 ksort($days_organized);
             }
             /* Find the current day in the array, then iterate to it */
             $offset = array_search($today_key, array_keys($days_organized));
             for ($i = 0; $i < $offset; $i++) {
                 next($days_organized);
             }
             /* Now show the number of days user desires */
             $final_days = array();
             if ($upcoming_mode) {
                 $today = DateTime::createFromFormat('m-d', $today_key);
                 for ($i = 0; $i < $consecutive_days; $i++) {
                     $today->add(new DateInterval('P1D'));
                     $tmp_day = $today->format('m-d');
                     if (!array_key_exists($tmp_day, $days_organized)) {
                         $days_organized[$tmp_day] = array();
                     }
                 }
                 ksort($days_organized);
                 $offset = array_search($today_key, array_keys($days_organized));
                 for ($i = 0; $i < $offset; $i++) {
                     next($days_organized);
                 }
                 $upcoming_days = $consecutive_days;
             }
             for ($i = 0; $i < $upcoming_days; $i++) {
                 $final_days[] = current($days_organized);
                 next($days_organized);
             }
             foreach ($final_days as $day) {
                 if (!$day) {
                     continue;
                 }
                 //var_dump( $day[ 0 ]->date );
                 $timestamp_date = strtotime($day[0]->date);
                 $html_date = date_i18n('j F', $timestamp_date);
                 $html .= '<div class="birthday_date" >' . $html_date . '</div>';
                 $flag = false;
                 foreach ($day as $row) {
                     //var_dump( $row );
                     $html .= '<div class="birthday_element birthday_name">';
                     if ($flag && $birthdays_settings['comma']) {
                         $html .= ', ';
                     } else {
                         $flag = true;
                     }
                     $html .= $row->name;
                     $age = date("Y") - date("Y", strtotime($row->date));
                     $html .= '<a href="' . $row->image . '" target="_blank" ';
                     if ($birthdays_settings['user_age']) {
                         $html .= 'data-age="' . $age . ' ' . __('years old', 'birthdays-widget') . '" ';
                     }
                     $html .= '></a></div>';
                 }
             }
             break;
     }
     $html .= '</div>';
     return $html;
 }