\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', 'validate_callback' => function ( $value ) { return in_array( $value, [ 'admin', 'frontend' ], true ); }, ], ], ]); } // Security Check: Ensure only allowed users can query the API function alba_board_rest_permissions_check( $request ) { $card_id = absint( $request['id'] ); return alba_board_can_view_shared_card( $card_id ); } // 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_board_card_modal_cache_key( $context, $card_id ); $cached_html = get_transient( $cache_key ); // Refresh admin modal markup when a cached response predates current form fields. if ( false !== $cached_html && 'admin' === $context && current_user_can( 'edit_card', $card_id ) ) { $has_current_field_layout = false !== strpos( $cached_html, 'alba-card-addons-row' ) && false !== strpos( $cached_html, 'alba-card-title-field' ) && false !== strpos( $cached_html, 'data-alba-members-layout="paired"' ); $has_trash_control = false !== strpos( $cached_html, 'alba-card-trash-btn' ); $can_delete_card = current_user_can( 'delete_card', $card_id ); $has_compact_actions = false !== strpos( $cached_html, 'data-alba-ui="compact-actions-upload"' ); if ( ! $has_current_field_layout || ! $has_compact_actions || $has_trash_control !== $can_delete_card ) { delete_transient( $cache_key ); $cached_html = false; } } // Replace cached frontend modal markup that still contains the removed archive control. if ( false !== $cached_html && 'frontend' === $context && (false !== strpos($cached_html, 'alba-modal-archive') || false === strpos($cached_html, 'data-alba-frontend-ui=') || substr_count($cached_html, 'id="alba-modal-delete"') !== (int) current_user_can('delete_card', $card_id)) ) { delete_transient( $cache_key ); $cached_html = false; } 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' ) { set_transient( 'alba_card_modal_revision_' . absint( $post_id ), wp_generate_uuid4(), 2 * DAY_IN_SECONDS ); delete_transient( 'alba_card_live_admin_' . $post_id ); delete_transient( 'alba_card_live_frontend_' . $post_id ); } } // Add-ons can update card metadata without saving the post itself. function alba_board_clear_card_meta_cache( $meta_id, $post_id, $meta_key, $meta_value ) { alba_board_clear_card_cache( $post_id ); } add_action( 'added_post_meta', 'alba_board_clear_card_meta_cache', 10, 4 ); add_action( 'updated_post_meta', 'alba_board_clear_card_meta_cache', 10, 4 ); add_action( 'deleted_post_meta', 'alba_board_clear_card_meta_cache', 10, 4 ); function alba_board_clear_card_terms_cache( $object_id, $terms, $tt_ids, $taxonomy ) { if ( 'alba_tag' === $taxonomy ) alba_board_clear_card_cache( $object_id ); } add_action( 'set_object_terms', 'alba_board_clear_card_terms_cache', 10, 4 );