'alba_board', 'post_title' => __('Project Alpha (Demo)', 'alba-board'), 'post_status' => 'publish', 'post_author' => get_current_user_id() ]); if ($board_id && !is_wp_error($board_id)) { $lists_names = [__('To Do', 'alba-board'), __('In Progress', 'alba-board'), __('Done', 'alba-board')]; $list_ids = []; $menu_order = 0; foreach ($lists_names as $list_title) { $list_ids[] = wp_insert_post([ 'post_type' => 'alba_list', 'post_title' => $list_title, 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'menu_order' => $menu_order++, 'meta_input' => ['alba_board_parent' => $board_id] ]); } if (count($list_ids) >= 3) { $cards_data = [ ['title' => __('👋 Welcome!', 'alba-board'), 'content' => '', 'list_id' => $list_ids, 'order' => 0], ['title' => __('🖱️ Drag me', 'alba-board'), 'content' => '', 'list_id' => $list_ids, 'order' => 1] ]; foreach ($cards_data as $c) { wp_insert_post([ 'post_type' => 'alba_card', 'post_title' => $c['title'], 'post_content'=> $c['content'], 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'menu_order' => $c['order'], 'meta_input' => ['alba_list_parent' => $c['list_id']] ]); } } wp_safe_redirect(admin_url('admin.php?page=alba-board-visual&board_id=' . $board_id)); exit; } } // New Board Creation logic if ( isset($_POST['alba_create_board']) && !empty($_POST['alba_new_board_title']) && check_admin_referer('alba_create_board_action') ) { $board_id = wp_insert_post([ 'post_type' => 'alba_board', 'post_title' => sanitize_text_field(wp_unslash($_POST['alba_new_board_title'])), 'post_status' => 'publish', 'post_author' => get_current_user_id() ]); if ( $board_id && ! is_wp_error($board_id) ) { wp_safe_redirect(admin_url('admin.php?page=alba-board-visual&board_id=' . $board_id)); exit; } } // New List Creation logic if ( isset($_POST['alba_create_list']) && !empty($_POST['alba_new_list_title']) && !empty($_POST['current_board_id']) && check_admin_referer('alba_create_list_action') ) { $board_id = absint($_POST['current_board_id']); global $wpdb; $max_list_order = $wpdb->get_var( $wpdb->prepare("SELECT MAX(menu_order) FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' AND ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'alba_board_parent' AND meta_value = %d)", 'alba_list', $board_id) ); $new_list_order = (($max_list_order !== null) ? intval($max_list_order) + 1 : 0); $list_id = wp_insert_post([ 'post_type' => 'alba_list', 'post_title' => sanitize_text_field(wp_unslash($_POST['alba_new_list_title'])), 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'menu_order' => $new_list_order, 'meta_input' => ['alba_board_parent' => $board_id] ]); if ( $list_id && ! is_wp_error($list_id) ) { wp_safe_redirect(admin_url('admin.php?page=alba-board-visual&board_id=' . $board_id)); exit; } } // New Card Creation logic if ( isset($_POST['alba_create_card'], $_POST['alba_new_card_title'], $_POST['current_list_id']) && !empty($_POST['alba_new_card_title']) && !empty($_POST['current_list_id']) && check_admin_referer('alba_create_card_action') ) { $list_id = absint($_POST['current_list_id']); $board_id = isset($_POST['current_board_id']) ? absint($_POST['current_board_id']) : 0; global $wpdb; $max_menu_order = $wpdb->get_var( $wpdb->prepare("SELECT MAX(menu_order) FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' AND ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'alba_list_parent' AND meta_value = %d)", 'alba_card', $list_id)); $card_id = wp_insert_post([ 'post_type' => 'alba_card', 'post_title' => sanitize_text_field(wp_unslash($_POST['alba_new_card_title'])), 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'menu_order' => (($max_menu_order !== null) ? intval($max_menu_order) + 1 : 0), 'meta_input' => ['alba_list_parent' => $list_id] ]); if ( $card_id && ! is_wp_error($card_id) ) { wp_safe_redirect(admin_url('admin.php?page=alba-board-visual' . ($board_id ? '&board_id=' . $board_id : ''))); exit; } } } /** * 2. UI LAYER (Rendering Only) */ function alba_render_admin_board_page() { // Fetch available boards $boards = get_posts(['post_type' => 'alba_board', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC']); // ANTIFRAGILE CHECK: Ensure we handle the element as an object even if the environment returns arrays $selected_board_id = 0; if ( isset($_GET['board_id']) ) { $selected_board_id = absint($_GET['board_id']); } elseif ( !empty($boards) ) { $first_board = (object) reset($boards); $selected_board_id = $first_board->ID; } // UI configuration options $display_opts = get_option('alba_board_display', ['show_avatars' => 1, 'theme' => 'default']); $show_avatars = !empty($display_opts['show_avatars']); $theme_class = isset($display_opts['theme']) ? 'alba-theme-' . $display_opts['theme'] : 'alba-theme-default'; echo '
'; echo '

' . esc_html__('Boards', 'alba-board') . '

'; if (!empty($boards)) { // Top Action & Filters Bar echo '
'; echo '
'; echo ''; echo '
'; echo '
'; echo ''; echo ''; if (taxonomy_exists('alba_tag')) { $tags = get_terms(['taxonomy' => 'alba_tag', 'hide_empty' => false]); if (!is_wp_error($tags) && !empty($tags)) { echo ''; } } echo '
'; // Board management buttons echo '
'; echo ''; if ( $selected_board_id && current_user_can('administrator') ) { $csv_url = wp_nonce_url(admin_url('admin-post.php?action=alba_export_board&board_id=' . $selected_board_id . '&format=csv'), 'alba_export_board_' . $selected_board_id); $json_url = wp_nonce_url(admin_url('admin-post.php?action=alba_export_board&board_id=' . $selected_board_id . '&format=json'), 'alba_export_board_' . $selected_board_id); echo '' . esc_html__('Export CSV', 'alba-board') . ''; echo '' . esc_html__('Export JSON', 'alba-board') . ''; } echo '
'; echo '
'; // Inline form for board creation echo '
'; wp_nonce_field('alba_create_board_action'); echo ''; echo ''; echo ''; echo ''; echo '
'; } else { // Empty state for new installations echo '
'; echo '

' . esc_html__('Welcome to Alba Board! 🎉', 'alba-board') . '

'; echo '
'; wp_nonce_field('alba_create_demo_board_action'); echo '
'; } // Render active board content if ($selected_board_id) { $board = (object) get_post($selected_board_id); // Cast to object if ($board && isset($board->post_type) && $board->post_type === 'alba_board') { echo '

' . esc_html($board->post_title) . '

'; $lists = get_posts(['post_type' => 'alba_list', 'numberposts' => -1, 'meta_key' => 'alba_board_parent', 'meta_value' => $board->ID, 'orderby' => 'menu_order', 'order' => 'ASC']); echo '
'; if ($lists) { foreach ($lists as $list) { $list = (object) $list; // Cast to object echo '
'; echo '
'; echo '

' . esc_html($list->post_title) . '

'; echo '
'; // List toggle collapse and delete buttons echo ''; echo ''; echo '
'; echo '
'; $cards = get_posts(['post_type' => 'alba_card', 'numberposts' => -1, 'meta_key' => 'alba_list_parent', 'meta_value' => $list->ID, 'orderby' => 'menu_order', 'order' => 'ASC']); if ($cards) { foreach ($cards as $card) { $card = (object) $card; // Cast to object echo '
'; echo '' . esc_html($card->post_title) . ''; // Display due date if exists $due_date = get_post_meta($card->ID, 'alba_due_date', true); if (!empty($due_date)) { $formatted_date = date_i18n('M j', strtotime($due_date)); echo '
'; echo ' '; echo esc_html($formatted_date); echo '
'; } echo '
'; } } $no_cards_class = empty($cards) ? 'alba-no-cards-msg' : 'alba-no-cards-msg alba-is-hidden'; echo '

' . esc_html__('No cards.', 'alba-board') . '

'; echo '
'; // Card creation section in list footer echo ''; echo '
'; } } // Ghost column for adding new lists echo '
'; $add_list_text = empty($lists) ? esc_html__('Add List', 'alba-board') : esc_html__('Add another list', 'alba-board'); echo ''; echo '
'; wp_nonce_field('alba_create_list_action'); echo ''; echo ''; echo ''; echo '
'; echo ''; echo ''; echo '
'; } } // Modal structure for card details in backend ?>
'; echo '
'; } /** * 3. AJAX LIST HANDLERS */ add_action('wp_ajax_alba_delete_list_action', function() { check_ajax_referer('alba_delete_list_nonce', 'nonce'); if (!current_user_can('delete_posts')) wp_send_json_error(); $list_id = isset($_POST['list_id']) ? absint($_POST['list_id']) : 0; if ($list_id && get_post_type($list_id) === 'alba_list') { $cards = get_posts(['post_type' => 'alba_card', 'numberposts' => -1, 'meta_key' => 'alba_list_parent', 'meta_value' => $list_id]); foreach ($cards as $card) wp_trash_post($card->ID); wp_trash_post($list_id); wp_send_json_success(); } wp_send_json_error(); }); add_action('wp_ajax_alba_move_list_action', function() { check_ajax_referer('alba_move_list_nonce', 'nonce'); if (!current_user_can('edit_posts')) wp_send_json_error(); $list_orders = isset($_POST['order']) ? $_POST['order'] : []; if (!empty($list_orders) && is_array($list_orders)) { global $wpdb; foreach ($list_orders as $index => $list_id) { $wpdb->update($wpdb->posts, ['menu_order' => intval($index)], ['ID' => intval($list_id)]); } wp_send_json_success(); } wp_send_json_error(); });