static function ajax_save() { if (!isset($_POST[Kanban_Utils::get_nonce()]) || !wp_verify_nonce($_POST[Kanban_Utils::get_nonce()], sprintf('%s-save', Kanban::get_instance()->settings->basename)) || !is_user_logged_in()) { wp_send_json_error(); } do_action(sprintf('%s_before_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); $status_id_old = Kanban_Utils::format_key(self::$slug, 'status_id_old'); $status_id_new = Kanban_Utils::format_key(self::$slug, 'status_id_new'); // build post data $post_data = array('post_type' => Kanban_Post_Types::format_post_type(self::$slug), 'post_title' => sprintf('changed task ID %s from %s to %s', $_POST['task_id'], $_POST['status_id_old'], $_POST['status_id_new']), 'post_parent' => $_POST['task_id'], 'postmeta' => array($status_id_old => $_POST['status_id_old'], $status_id_new => $_POST['status_id_new']), 'terms' => array()); // save our work_hour $post_data = Kanban_Post::save($post_data); if (!$post_data) { wp_send_json_error(); } do_action(sprintf('%s_after_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); wp_send_json_success(array('message' => sprintf('%s saved', self::$slug), self::$slug => $post_data)); }
static function get_all() { if (!isset(self::get_instance()->all_projects)) { $post_type_key = Kanban_Post_Types::format_post_type(self::$slug); $args = array('posts_per_page' => -1, 'post_type' => $post_type_key, 'post_status' => 'publish'); $posts = get_posts($args); self::get_instance()->all_projects = Kanban_Post::apply_postmeta_and_terms_to_posts($posts); } return self::get_instance()->all_projects; }
static function get_all() { if (!isset(self::$instance->all_projects)) { global $wpdb; $post_type_key = Kanban_Post_Types::format_post_type('project'); $sql = "SELECT `{$wpdb->prefix}posts`.*\n\t\t\t\t\tFROM `{$wpdb->prefix}posts`\n\t\t\t\t\tWHERE `{$wpdb->prefix}posts`.`post_type` = '{$post_type_key}'\n\t\t\t\t\tAND `{$wpdb->prefix}posts`.`post_status` IN ('publish')\n\t\t\t;"; $sql = apply_filters(sprintf('%s_sql_%s_get_all', Kanban::$instance->settings->basename, self::$slug), $sql); $posts = $wpdb->get_results($sql); self::$instance->all_projects = Kanban_Post::apply_postmeta_and_terms_to_posts($posts); } return self::$instance->all_projects; }
static function get_post_types_list($allowed_post_types = array()) { $return = array(); foreach (Kanban_Post_Types::$post_types as $post_type_slug => $post_type_data) { if (!empty($allowed_post_types)) { if (!in_array($post_type_slug, $allowed_post_types)) { continue; } } $return[] = Kanban_Post_Types::format_post_type($post_type_slug); } return $return; }
static function ajax_save() { if (!isset($_POST[Kanban_Utils::get_nonce()]) || !wp_verify_nonce($_POST[Kanban_Utils::get_nonce()], sprintf('%s-save', Kanban::get_instance()->settings->basename)) || !is_user_logged_in()) { wp_send_json_error(); } do_action(sprintf('%s_before_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); $current_user_id = get_current_user_id(); $comment_type_field = Kanban_Utils::format_key(self::$slug, 'comment_type'); // build post data $post_data = array('post_type' => Kanban_Post_Types::format_post_type(self::$slug), 'post_title' => sprintf('%s comment for task %s', $_POST['comment_type'], $_POST['id']), 'post_content' => sanitize_text_field(str_replace("\n", '', $_POST['post_content'])), 'post_parent' => $_POST['id'], 'postmeta' => array($comment_type_field => $_POST['comment_type'])); // save our work_hour $post_data = Kanban_Post::save($post_data); if (!$post_data) { wp_send_json_error(); } do_action(sprintf('%s_after_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); wp_send_json_success(array('message' => sprintf('%s saved', self::$slug), self::$slug => $post_data)); }
static function ajax_save() { if (!isset($_POST[Kanban_Utils::get_nonce()]) || !wp_verify_nonce($_POST[Kanban_Utils::get_nonce()], sprintf('%s-save', Kanban::$instance->settings->basename)) || !isset($_POST[Kanban_Task::$slug]) || !is_user_logged_in()) { wp_send_json_error(); } do_action(sprintf('%s_before_%s_ajax_save', Kanban::$instance->settings->basename, self::$slug)); // build post data $post_data = array('post_type' => Kanban_Post_Types::format_post_type('work_hour'), 'post_title' => sanitize_text_field($_POST[Kanban_Task::$slug]['post_title']), 'postmeta' => array(), 'terms' => array()); $hour_operator = Kanban_Utils::format_key('work_hour', 'operator'); $post_data['postmeta'][$hour_operator] = $_POST['operator']; // set assignee as author of work hour $task_user_id_assigned_to = Kanban_Utils::format_key('task', 'user_id_assigned'); if ($_POST[Kanban_Task::$slug]['postmeta'][$task_user_id_assigned_to] > 0) { $post_data['post_author'] = $_POST[Kanban_Task::$slug]['postmeta'][$task_user_id_assigned_to]; } // link task to hour $hour_task_id = Kanban_Utils::format_key('work_hour', 'project_id'); $post_data['postmeta'][$hour_task_id] = $_POST[Kanban_Task::$slug]['ID']; // link current user to hour $hour_user_id_logged = Kanban_Utils::format_key('work_hour', 'user_id_logged'); $post_data['postmeta'][$hour_user_id_logged] = get_current_user_id(); // set task project as work project $task_project_id = Kanban_Utils::format_key('task', 'project_id'); $hour_project_id = Kanban_Utils::format_key('work_hour', 'project_id'); $post_data['postmeta'][$hour_project_id] = $_POST[Kanban_Task::$slug]['postmeta'][$task_project_id]; // set current task status for work hour $task_status = Kanban_Utils::format_key('task', 'status'); $hour_status_id = Kanban_Utils::format_key('work_hour', 'task_status_id'); $post_data['postmeta'][$hour_status_id] = $_POST[Kanban_Task::$slug]['terms'][$task_status][0]; // save our work_hour $post_data = Kanban_Post::save($post_data); if (!$post_data) { wp_send_json_error(); } do_action(sprintf('%s_after_%s_ajax_save', Kanban::$instance->settings->basename, self::$slug)); wp_send_json_success(array('message' => sprintf('%s saved', self::$slug), self::$slug => $post_data)); }
static function add_comments_meta_box() { add_meta_box(sprintf('%s_comments', Kanban_Post_Types::format_post_type(self::$slug)), 'Task action log', array(__CLASS__, 'render_comments_meta_box'), Kanban_Post_Types::format_post_type(self::$slug), 'side'); }
public static function parent_file($parent_file) { global $submenu_file; $current_screen = get_current_screen(); // Set the submenu as active/current while anywhere in your Custom Post Type (nwcm_news) if (Kanban_Post_Types::format_post_type('task') === $current_screen->post_type) { if (Kanban_Utils::format_key('task', 'status') === $current_screen->taxonomy) { $submenu_file = self::get_submenu_file('task', 'status'); } if (Kanban_Utils::format_key('task', 'estimate') === $current_screen->taxonomy) { $submenu_file = self::get_submenu_file('task', 'estimate'); } $parent_file = Kanban::get_instance()->settings->basename; } return $parent_file; }
static function format_key($post_type, $key) { $post_type = Kanban_Post_Types::format_post_type($post_type); return sprintf('%s_%s', $post_type, $key); }
static function admin_menu() { // Base 64 encoded SVG image. $icon_svg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAPCAYAAAAGRPQsAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6QkRFMDQwQTg1NUFFMTFFNUJBRDdBMjA0MjA4NTJFNzEiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6QkRFMDQwQTk1NUFFMTFFNUJBRDdBMjA0MjA4NTJFNzEiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDpCREUwNDBBNjU1QUUxMUU1QkFEN0EyMDQyMDg1MkU3MSIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDpCREUwNDBBNzU1QUUxMUU1QkFEN0EyMDQyMDg1MkU3MSIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PokTEeYAAABuSURBVHjaYvz//z8DCMw8fBXCwAHSbbUZgRReNUwMVASD1zAWGGPLuTvWDIwMf2GBwsiACKJ//xn/AcOMYfq+C5r/GRj//QPJM8JU/AfTf/4x/GccjYBBEpudm48zEogABqWyOXjVjMYm6QAgwADj+y/EHS5dLQAAAABJRU5ErkJggg=='; add_menu_page(Kanban::$instance->settings->pretty_name, Kanban::$instance->settings->pretty_name, 'manage_options', Kanban::$instance->settings->basename, null, $icon_svg); // redeclare same page to change name to settings // @link https://codex.wordpress.org/Function_Reference/add_submenu_page#Inside_menu_created_with_add_menu_page.28.29 add_submenu_page(Kanban::$instance->settings->basename, 'Welcome', 'Welcome', 'manage_options', Kanban::$instance->settings->basename, array(__CLASS__, 'welcome_page')); foreach (Kanban_Post_Types::$post_types as $post_type_slug => $post_type_data) { $post_type_label = ucfirst($post_type_slug); if (defined('KANBAN_DEBUG') && KANBAN_DEBUG === TRUE) { add_submenu_page(Kanban::$instance->settings->basename, sprintf('All %s', str_replace('_', ' ', Kanban_Utils::make_word_plural($post_type_label))), sprintf('All %s', str_replace('_', ' ', Kanban_Utils::make_word_plural($post_type_label))), 'manage_options', sprintf('edit.php?post_type=%s', Kanban_Post_Types::format_post_type($post_type_slug))); } foreach ($post_type_data['taxonomies'] as $taxonomy_slug => $values) { $taxonomy_key = Kanban_Utils::format_key($post_type_slug, $taxonomy_slug); $taxonomy_label = ucwords(sprintf('%s %s', $post_type_slug, Kanban_Utils::make_word_plural($taxonomy_slug))); add_submenu_page(Kanban::$instance->settings->basename, $taxonomy_label, $taxonomy_label, 'manage_options', sprintf('edit-tags.php?taxonomy=%s&post_type=%s', $taxonomy_key, Kanban_Post_Types::format_post_type($post_type_slug))); } } // @link https://codex.wordpress.org/Function_Reference/add_submenu_page#Inside_menu_created_with_add_menu_page.28.29 add_submenu_page(Kanban::$instance->settings->basename, 'Settings', 'Settings', 'manage_options', sprintf('admin.php?page=%s', Kanban::$instance->settings->basename), array(__CLASS__, 'plugin_page')); }
static function on_activation() { // http://wordpress.stackexchange.com/questions/20043/inserting-taxonomy-terms-during-a-plugin-activation Kanban_Post_Types::custom_post_types(); foreach (Kanban_Post_Types::$post_types as $post_type_slug => $post_type_data) { foreach ($post_type_data['taxonomies'] as $taxonomy_slug => $values) { $taxonomy_key = Kanban_Utils::format_key($post_type_slug, $taxonomy_slug); foreach ($values as $slug => $value) { $term_id_arr = wp_insert_term($value, $taxonomy_key, array('slug' => $slug)); } } } // add current user to board $current_user_id = get_current_user_id(); $users_field_name = sprintf('%s_user', Kanban::$instance->settings->basename); $user_settings = Kanban_Settings::get_option($users_field_name, null, array()); if (!isset($user_settings['allowed_users'])) { $user_settings['allowed_users'] = array(); } if (!in_array($current_user_id, $user_settings['allowed_users'])) { $user_settings['allowed_users'][$current_user_id] = $current_user_id; } update_option($users_field_name, $user_settings); // add status order $tax_key = Kanban_Utils::format_key('task', 'status'); $field_name = sprintf('%s_order', $tax_key); $settings = Kanban_Settings::get_option($field_name, null, array()); $slugs_in_order = array_keys(Kanban_Post_Types::$post_types['task']['taxonomies']['status']); $term_ids_in_order = array(); foreach ($slugs_in_order as $order => $slug) { $term = get_term_by('slug', $slug, $tax_key); $term_ids_in_order[$term->term_id] = $order; } update_option($field_name, $term_ids_in_order); // add estimate order $tax_key = Kanban_Utils::format_key('task', 'estimate'); $field_name = sprintf('%s_order', $tax_key); $settings = Kanban_Settings::get_option($field_name, null, array()); $slugs_in_order = array_keys(Kanban_Post_Types::$post_types['task']['taxonomies']['estimate']); $term_ids_in_order = array(); foreach ($slugs_in_order as $order => $slug) { $term = get_term_by('slug', $slug, $tax_key); $term_ids_in_order[$term->term_id] = $order; } update_option($field_name, $term_ids_in_order); // add an example task $post = array('post_status' => 'publish', 'post_type' => Kanban_Post_Types::format_post_type('task'), 'post_author' => $current_user_id); $post_id = wp_insert_post($post); flush_rewrite_rules(); // redirect to welcome page // @link http://premium.wpmudev.org/blog/tabbed-interface/ set_transient(sprintf('_%s_welcome_screen_activation_redirect', Kanban::$instance->settings->basename), true, 30); }
static function on_activation() { // http://wordpress.stackexchange.com/questions/20043/inserting-taxonomy-terms-during-a-plugin-activation Kanban_Post_Types::custom_post_types(); $is_installed_before = get_option(sprintf('_%s_is_installed_before', Kanban::$instance->settings->basename)); if (!$is_installed_before) { foreach (Kanban_Post_Types::$post_types as $post_type_slug => $post_type_data) { foreach ($post_type_data['taxonomies'] as $taxonomy_slug => $values) { $taxonomy_key = Kanban_Utils::format_key($post_type_slug, $taxonomy_slug); foreach ($values as $slug => $value) { $term_id_arr = wp_insert_term($value, $taxonomy_key, array('slug' => $slug)); } } } // add current user to board $users_field_name = sprintf('%s_user', Kanban::$instance->settings->basename); $user_settings = Kanban_Settings::get_option($users_field_name, null, array()); if (empty($user_settings)) { $current_user_id = get_current_user_id(); if (!isset($user_settings['allowed_users'])) { $user_settings['allowed_users'] = array(); } if (!in_array($current_user_id, $user_settings['allowed_users'])) { $user_settings['allowed_users'][$current_user_id] = $current_user_id; } update_option($users_field_name, $user_settings); } // add status order $tax_key = Kanban_Utils::format_key('task', 'status'); $field_name = sprintf('%s_order', $tax_key); $settings = Kanban_Settings::get_option($field_name, null, array()); if (empty($settings)) { $slugs_in_order = array_keys(Kanban_Post_Types::$post_types['task']['taxonomies']['status']); $term_ids_in_order = array(); foreach ($slugs_in_order as $order => $slug) { $term = get_term_by('slug', $slug, $tax_key); $term_ids_in_order[$term->term_id] = $order; } update_option($field_name, $term_ids_in_order); } // add estimate order $tax_key = Kanban_Utils::format_key('task', 'estimate'); $field_name = sprintf('%s_order', $tax_key); $settings = Kanban_Settings::get_option($field_name, null, array()); if (empty($settings)) { $slugs_in_order = array_keys(Kanban_Post_Types::$post_types['task']['taxonomies']['estimate']); $term_ids_in_order = array(); foreach ($slugs_in_order as $order => $slug) { $term = get_term_by('slug', $slug, $tax_key); $term_ids_in_order[$term->term_id] = $order; } update_option($field_name, $term_ids_in_order); } $field_name = sprintf('%s_colors', Kanban_Utils::format_key('task', 'status')); $settings = get_option('kanban_task_status_colors'); if (empty($settings)) { // add status colors $colors = array('backlog' => '#8224e3', 'ready' => '#eeee22', 'in-progress' => '#81d742', 'qa' => '#dd9933', 'done' => '#1e73be', 'archive' => '#545454'); // get status tax $taxonomies = array(Kanban_Utils::format_key('task', 'status')); $args = array('orderby' => 'term_id', 'order' => 'ASC', 'hide_empty' => false, 'childless' => false); $terms = get_terms($taxonomies, $args); // build array of status id => color $option = array(); foreach ($terms as $term) { if (!isset($colors[$term->slug])) { continue; } $option[$term->term_id] = $colors[$term->slug]; } // save it update_option($field_name, $option); } // save colors // check for existing tasks $args = array('post_type' => Kanban_Post_Types::format_post_type('task')); $posts_array = get_posts($args); // if no tasks, add a sample if (empty($posts_array)) { // add an example task $post = array('post_status' => 'publish', 'post_type' => Kanban_Post_Types::format_post_type('task'), 'post_author' => $current_user_id); $post_id = wp_insert_post($post); } // save that the plugin has been installed update_option(sprintf('_%s_is_installed_before', Kanban::$instance->settings->basename), TRUE); } // $is_installed_before flush_rewrite_rules(); // redirect to welcome page // @link http://premium.wpmudev.org/blog/tabbed-interface/ set_transient(sprintf('_%s_welcome_screen_activation_redirect', Kanban::$instance->settings->basename), true, 30); }