\d+)', [ 'methods' => WP_REST_Server::READABLE, 'callback' => 'alba_board_rest_get_card_details', 'permission_callback' => 'alba_board_rest_permissions_check', 'args' => [ 'context' => [ 'default' => 'admin', 'sanitize_callback' => 'sanitize_text_field', ], ], ]); } // Security Check: Ensure only allowed users can query the API function alba_board_rest_permissions_check( $request ) { $card_id = (int) $request['id']; if ( 'admin' === $request->get_param( 'context' ) ) { return current_user_can( 'edit_cards' ); } // IDOR PATCH: Contextual Authorization // If the card belongs to a published board, it is public on the frontend $list_id = get_post_meta( $card_id, 'alba_list_parent', true ); $board_id = get_post_meta( $list_id, 'alba_board_parent', true ); if ( $board_id ) { $board = get_post( $board_id ); if ( $board && $board->post_status === 'publish' ) { return true; // Board is explicitly public, anyone can view its cards } } // STRICT FALLBACK: 'read_card' maps to 'read' for published posts, which is insecure. // We strictly require 'edit_cards' (Admin/Editor) to view isolated or private cards. return current_user_can( 'edit_cards' ); } // The Core Function: Fetch HTML and Cache via Transients (Redis) function alba_board_rest_get_card_details( $request ) { $card_id = (int) $request['id']; $context = $request->get_param( 'context' ); if ( ! function_exists('alba_output_card_details_admin_modal') ) { require_once dirname(__FILE__) . '/ajax-card-details-admin.php'; } if ( ! function_exists('alba_output_card_details_frontend_modal') ) { require_once dirname(__FILE__) . '/ajax-card-details.php'; } $cache_key = 'alba_card_live_' . $context . '_' . $card_id; $cached_html = get_transient( $cache_key ); if ( false !== $cached_html ) { return new WP_REST_Response( [ 'html' => $cached_html, 'cached' => true ], 200 ); } ob_start(); if ( 'admin' === $context ) { alba_output_card_details_admin_modal( $card_id ); } else { alba_output_card_details_frontend_modal( $card_id ); } $html = ob_get_clean(); if ( empty( trim( $html ) ) ) { return new WP_REST_Response( [ 'message' => 'Error: Blank response generated by the server.' ], 500 ); } set_transient( $cache_key, $html, 12 * HOUR_IN_SECONDS ); return new WP_REST_Response( [ 'html' => $html, 'cached' => false ], 200 ); } // Clear Redis cache when a card is updated or deleted add_action( 'save_post_alba_card', 'alba_board_clear_card_cache', 10, 3 ); add_action( 'before_delete_post', 'alba_board_clear_card_cache', 10, 3 ); function alba_board_clear_card_cache( $post_id, $post = null, $update = false ) { if ( get_post_type( $post_id ) === 'alba_card' ) { delete_transient( 'alba_card_live_admin_' . $post_id ); delete_transient( 'alba_card_live_frontend_' . $post_id ); } }