/**
  * Loops through to-do items
  * Passes a completed value, limit value and a category id
  * @param int $status
  * @param int $cat_id
  * @param $limit
  */
 public function loop_through_todos($status = 0, $cat_id = 0, $limit = 100)
 {
     global $userdata, $current_user;
     get_currentuserinfo();
     $limit = isset(CTDL_Loader::$dashboard_settings['dashboard_number']) ? CTDL_Loader::$dashboard_settings['dashboard_number'] : $limit;
     $limit = $limit == '-1' ? 1000 : $limit;
     $user = CTDL_Lib::check_permission('todo', 'view') ? $current_user->ID : $userdata->ID;
     if (CTDL_Loader::$settings['categories'] == 1 && CTDL_Loader::$settings['sort_order'] == 'cat_id' && $cat_id == 0) {
         $categories = CTDL_Categories::get_categories();
         $items = 0;
         $posts_to_exclude = array();
         foreach ($categories as $category) {
             $todo_items = CTDL_Lib::get_todos($user, $limit, $status, $category->term_id);
             if ($todo_items->have_posts()) {
                 array_splice($posts_to_exclude, count($posts_to_exclude), 0, $this->show_todo_list_items($todo_items, $status, $category->term_id));
                 $items = 1;
             }
         }
         $todo_items = CTDL_Lib::get_todos($user, 0, $status, 0, $posts_to_exclude);
         if ($todo_items->have_posts()) {
             $this->show_todo_list_items($todo_items, $status);
             $items = 1;
         }
         if ($items == 0 && $status == 0) {
             echo '<p>' . apply_filters('ctdl_no_items', esc_html__('No items to do.', 'cleverness-to-do-list')) . '</p>';
         }
     } else {
         $todo_items = CTDL_Lib::get_todos($user, $limit, $status, $cat_id);
         if ($todo_items->have_posts()) {
             $this->show_todo_list_items($todo_items, $status);
         } elseif ($status == 0) {
             echo '<p>' . apply_filters('ctdl_no_items', esc_html__('No items to do.', 'cleverness-to-do-list')) . '</p>';
         }
     }
 }
 /**
  * Creates the widget
  * @param $args
  * @param $instance
  *
  */
 function widget($args, $instance)
 {
     global $ClevernessToDoList, $CTDL_widget_settings;
     $CTDL_widget_settings = $instance;
     get_currentuserinfo();
     $title = apply_filters('widget_title', $instance['title']);
     $limit = $instance['number'] == '-1' ? 1000 : $instance['number'];
     $assigned_to = $instance['assigned_to'];
     $deadline = $instance['deadline'];
     $progress = $instance['progress'];
     $category = CTDL_Loader::$settings['categories'] == 1 ? $instance['category'] : 0;
     $individual = isset($instance['individual']) ? $instance['individual'] : 0;
     if ($individual == 1 && is_user_logged_in()) {
         global $current_user, $userdata;
         if (CTDL_Loader::$settings['list_view'] == '2') {
             $user = $current_user->ID;
         } else {
             $user = $userdata->ID;
         }
     } else {
         $user = 0;
     }
     echo $args['before_widget'];
     if ($title) {
         echo $args['before_title'];
         echo $title;
         echo $args['after_title'];
     }
     $ClevernessToDoList->list = '';
     $visible = 0;
     if (CTDL_Loader::$settings['categories'] == 1 && CTDL_Loader::$settings['sort_order'] == 'cat_id' && $category == 0) {
         $categories = CTDL_Categories::get_categories();
         $items = 0;
         $posts_to_exclude = array();
         $visibility = get_option('CTDL_categories');
         foreach ($categories as $category) {
             echo '<ol>';
             $category_id = $category->term_id;
             $visible = $visibility["category_{$category->term_id}"];
             $todo_items = CTDL_Lib::get_todos($user, $limit, 0, $category_id);
             if ($todo_items->have_posts()) {
                 array_splice($posts_to_exclude, count($posts_to_exclude), 0, $this->show_todo_list_items($todo_items, $progress, $deadline, $assigned_to, 0, $visible));
                 $items = 1;
             }
             echo '</ol>';
         }
         $todo_items = CTDL_Lib::get_todos($user, $limit, 0, 0, $posts_to_exclude);
         if ($todo_items->have_posts()) {
             echo '<ol>';
             $this->show_todo_list_items($todo_items, $progress, $deadline, $assigned_to, 0, $visible);
             echo '</ol>';
             $items = 1;
         }
         if ($items == 0) {
             echo '<p>' . apply_filters('ctdl_no_items', esc_html__('No items to do.', 'cleverness-to-do-list')) . '</p>';
         }
     } else {
         echo '<ol>';
         $todo_items = CTDL_Lib::get_todos($user, $limit, 0, $category);
         if ($todo_items->have_posts()) {
             $this->show_todo_list_items($todo_items, $progress, $deadline, $assigned_to, $category, $visible);
         } else {
             echo '<p>' . apply_filters('ctdl_no_items', esc_html__('No items to do.', 'cleverness-to-do-list')) . '</p>';
         }
     }
     echo '</ol>';
     echo $args['after_widget'];
 }
 /**
  * Show category heading only if it's the first item from that category
  * @param $categories
  * @param $list_type
  * @param int $completed
  */
 protected function show_category_headings($categories, $list_type, $completed = 0)
 {
     $class = $completed == 0 ? 'todolist' : 'todolist todolist-completed';
     static $i = 0;
     if (CTDL_Loader::$settings['categories'] == 1 && $categories != false) {
         foreach ($categories as $category) {
             $cat = CTDL_Categories::get_category_name($category->term_id);
             if ($this->cat_id != $category->term_id && $cat != '') {
                 if ($this->cat_id != '') {
                     $this->list .= '</' . $list_type . '>';
                 }
                 $this->list .= '<h4 class="todo-category-heading">' . esc_html($cat) . '</h4><' . $list_type . ' class="' . $class . '">';
                 $this->cat_id = $category->term_id;
             }
         }
     } elseif ($categories == false && $i == 0) {
         if ($this->cat_id != '') {
             $this->list .= '</' . $list_type . '>';
         }
         $this->list .= '<' . $list_type . ' class="' . $class . '">';
         $i++;
     } elseif ($categories == false && $completed == 1 && $i == 1) {
         if ($this->cat_id != '') {
             $this->list .= '</' . $list_type . '>';
         }
         $this->list .= '<' . $list_type . ' class="' . $class . '">';
         $i++;
     }
 }
 /**
  * Send an email to assigned user
  * @static
  * @param $assign
  * @param $deadline
  * @param int $category
  * @param int $planner
  */
 protected static function email_user($assign, $deadline, $category = 0, $planner = 0)
 {
     global $current_user;
     get_currentuserinfo();
     add_filter('wp_mail_content_type', array('CTDL_Lib', 'set_html_email'));
     $priority = $_POST['cleverness_todo_priority'];
     $todo_text = $_POST['cleverness_todo_description'];
     $priority_array = array(0 => CTDL_Loader::$settings['priority_0'], 1 => CTDL_Loader::$settings['priority_1'], 2 => CTDL_Loader::$settings['priority_2']);
     if ($category != 0 && $category != -1) {
         $category_name = CTDL_Categories::get_category_name($category);
     }
     if (is_array($assign)) {
         foreach ($assign as $assign_value) {
             if (current_user_can(CTDL_Loader::$settings['assign_capability']) && $assign_value != '' && $assign_value != '-1' && $assign_value != '0') {
                 $headers = 'From: ' . CTDL_Loader::$settings['email_from'] . ' <' . CTDL_Loader::$settings['email_from_email'] . '>' . "\r\n\\";
                 $subject = CTDL_Loader::$settings['email_subject'];
                 if (CTDL_Loader::$settings['email_category'] == 1 && $category != 0 && $category != -1) {
                     $subject .= ' - ' . $category_name;
                 }
                 $assign_user = get_userdata($assign_value);
                 $email = $assign_user->user_email;
                 $email_message = CTDL_Loader::$settings['email_text'] . "<br>";
                 $email_message .= "<br>" . __('Priority', 'cleverness-to-do-list') . ': ' . $priority_array[$priority] . "<br>";
                 if (CTDL_Loader::$settings['email_show_assigned_by'] == 1) {
                     $email_message .= "<br>" . __('From', 'cleverness-to-do-list') . ': ' . $current_user->display_name . ' (' . $current_user->user_email . ')' . "<br>";
                 }
                 if ($category != 0 && $category != -1) {
                     $email_message .= __('Category', 'cleverness-to-do-list') . ': ' . $category_name . "<br>";
                 }
                 if ($deadline != '' && $deadline != 0) {
                     $email_message .= __('Deadline:', 'cleverness-to-do-list') . ' ' . date(CTDL_Loader::$settings['date_format'], strtotime($deadline)) . "<br>";
                 }
                 if (CTDL_Loader::$settings['post_planner'] == 1 && $planner != 0) {
                     $url = admin_url('post.php?post=' . absint($planner) . '&action=edit');
                     $email_message .= esc_html__('Post Planner', 'post-planner') . ': <a href="' . $url . '">' . esc_html__('View', 'cleverness-to-do-list') . "</a><br>";
                 }
                 $email_message .= __('To-Do:', 'cleverness-to-do-list') . ' ' . $todo_text . "<br>";
                 wp_mail($email, $subject, $email_message, $headers);
             }
         }
     } else {
         if (current_user_can(CTDL_Loader::$settings['assign_capability']) && $assign != '' && $assign != '-1' && $assign != '0') {
             $headers = 'From: ' . CTDL_Loader::$settings['email_from'] . ' <' . get_bloginfo('admin_email') . '>' . "\r\n\\";
             $subject = CTDL_Loader::$settings['email_subject'];
             if (CTDL_Loader::$settings['email_category'] == 1 && $category != 0 && $category != -1) {
                 $subject .= ' - ' . $category_name;
             }
             $assign_user = get_userdata($assign);
             $email = $assign_user->user_email;
             $email_message = CTDL_Loader::$settings['email_text'] . "<br>";
             $email_message .= "<br>" . __('Priority', 'cleverness-to-do-list') . ': ' . $priority_array[$priority] . "<br>";
             if (CTDL_Loader::$settings['email_show_assigned_by'] == 1) {
                 $email_message .= "<br>" . __('From', 'cleverness-to-do-list') . ': ' . $current_user->display_name . ' (' . $current_user->user_email . ')' . "<br>";
             }
             if ($category != 0 && $category != -1) {
                 $email_message .= __('Category', 'cleverness-to-do-list') . ': ' . $category_name . "<br>";
             }
             if ($deadline != '') {
                 $email_message .= __('Deadline:', 'cleverness-to-do-list') . ' ' . date(CTDL_Loader::$settings['date_format'], strtotime($deadline)) . "<br>";
             }
             if (CTDL_Loader::$settings['post_planner'] == 1 && $planner != 0) {
                 $url = admin_url('post.php?post=' . absint($planner) . '&action=edit');
                 $email_message .= esc_html__('Post Planner URL', 'post-planner') . ': ' . $url . "<br>";
             }
             $email_message .= __('To-Do:', 'cleverness-to-do-list') . ' ' . $todo_text . "<br>";
             wp_mail($email, $subject, $email_message, $headers);
         }
     }
     remove_filter('wp_mail_content_type', array('CTDL_Lib', 'set_html_email'));
 }
 /**
  * Loop through to-do items
  * @param int $completed
  * @param int $cat_id
  * @param int $limit
  */
 protected function loop_through_todos($completed = 0, $cat_id = 0, $limit = 5000)
 {
     global $current_user, $userdata;
     $user = CTDL_Lib::get_user_id($current_user, $userdata);
     // if categories are enabled and sort order is set to cat id and we're not getting todos for a specific category
     if (CTDL_Loader::$settings['categories'] == 1 && CTDL_Loader::$settings['sort_order'] == 'cat_id' && $cat_id == 0) {
         $categories = CTDL_Categories::get_categories();
         $items = 0;
         $visible = 0;
         $posts_to_exclude = array();
         if (!is_admin() || defined('DOING_AJAX') && DOING_AJAX) {
             $visibility = get_option('CTDL_categories');
         }
         foreach ($categories as $category) {
             if (!is_admin() || defined('DOING_AJAX') && DOING_AJAX) {
                 $visible = $visibility["category_{$category->term_id}"];
             }
             $todo_items = CTDL_Lib::get_todos($user, 5000, $completed, $category->term_id);
             if ($todo_items->have_posts()) {
                 array_splice($posts_to_exclude, count($posts_to_exclude), 0, $this->show_todo_list_items($todo_items, $completed, $visible));
                 $items = 1;
             }
         }
         $todo_items = CTDL_Lib::get_todos($user, 5000, $completed, 0, $posts_to_exclude);
         if ($todo_items->have_posts()) {
             $this->show_todo_list_items($todo_items, $completed);
             $items = 1;
         }
         if ($items == 0) {
             if ($completed == 0) {
                 $this->list .= '<tr><td colspan="100%">' . apply_filters('ctdl_no_items', esc_html__('No items to do.', 'cleverness-to-do-list')) . '</td></tr>';
             } else {
                 $this->list .= '<tr><td colspan="100%">' . apply_filters('ctdl_no_completed_items', esc_html__('No completed items.', 'cleverness-to-do-list')) . '</td></tr>';
             }
         }
     } else {
         $todo_items = CTDL_Lib::get_todos($user, 5000, $completed, $cat_id);
         if ($todo_items->have_posts()) {
             $this->show_todo_list_items($todo_items, $completed);
         } else {
             if ($completed == 0) {
                 $this->list .= '<tr><td colspan="100%">' . apply_filters('ctdl_no_items', esc_html__('No items to do.', 'cleverness-to-do-list')) . '</td></tr>';
             } else {
                 $this->list .= '<tr><td colspan="100%">' . apply_filters('ctdl_no_completed_items', esc_html__('No completed items.', 'cleverness-to-do-list')) . '</td></tr>';
             }
         }
     }
 }
 /**
  * Add Javascript to the category page
  * @static
  */
 public static function add_category_js()
 {
     wp_enqueue_script('cleverness_todo_category_js');
     wp_enqueue_script('jquery-color');
     wp_localize_script('cleverness_todo_category_js', 'ctdlcat', CTDL_Categories::get_js_vars());
 }