To update the paraDescarga metadata when the permitirDescarga action is triggered, you need to modify the cambioDeEstado function to handle this specific action. Here's how you can adjust your code:
-
Add a New Action for Permitting Downloads: Separate the logic for allowing downloads from other post status changes.
-
Update the Metadata: Use WordPress functions to update the post metadata within the permitirDescarga action.
Here's the updated code incorporating these changes:
// Function to change the download permission status
function permitirDescarga($post_id)
{
update_post_meta($post_id, 'paraDescarga', 1);
return json_encode(['success' => true, 'message' => 'Download permission granted']);
}
// Generic function to handle AJAX requests
function cambioDeEstado()
{
// Validate that post_id was received
if (!isset($_POST['post_id'])) {
echo json_encode(['success' => false, 'message' => 'Post ID is missing']);
wp_die();
}
$post_id = $_POST['post_id'];
$action = $_POST['action'];
$estados = [
'toggle_post_status' => ($_POST['current_status'] == 'pending') ? 'publish' : 'pending',
'reject_post' => 'rejected',
'request_post_deletion' => 'pending_deletion',
'eliminarPostRs' => 'pending_deletion',
'rechazarcolab' => 'pending_deletion',
'aceptarcolab' => 'publish',
];
if ($action === 'permitirDescarga') {
echo permitirDescarga($post_id);
} elseif (isset($estados[$action])) {
$new_status = $estados[$action];
echo cambiarEstado($post_id, $new_status);
} else {
echo json_encode(['success' => false, 'message' => 'Invalid action']);
}
wp_die();
}
// Register AJAX actions
add_action('wp_ajax_permitirDescarga', 'cambioDeEstado');
add_action('wp_ajax_aceptarcolab', 'cambioDeEstado');
add_action('wp_ajax_rechazarcolab', 'cambioDeEstado');
add_action('wp_ajax_toggle_post_status', 'cambioDeEstado');
add_action('wp_ajax_reject_post', 'cambioDeEstado');
add_action('wp_ajax_request_post_deletion', 'cambioDeEstado');
add_action('wp_ajax_eliminarPostRs', 'cambioDeEstado');
Key Changes:
permitirDescarga Function: This function updates the paraDescarga metadata to 1, allowing downloads.
- Conditional Logic: In
cambioDeEstado, a check is added specifically for the permitirDescarga action to call the permitirDescarga function.
- AJAX Handling: The action
permitirDescarga is routed through the generic AJAX function cambioDeEstado, ensuring it processes the metadata update correctly.
This setup ensures that when the permitirDescarga action is triggered via AJAX, the download permission status is updated as required.