if (!$current_user_id) {
error_log("[publicaciones] Advertencia: No se encontró ID de usuario");
}
/*
siento que aca hay un problema, cuando envio idea true, parece que no entiende el true porque no usa procesarIdeas cuando es ajax
Parámetros de carga:
{filtro: 'sampleList', tabId: '', identificador: '', idUsuario: '', paginaActual: 2, …}
colec
:
"319708"
filtro
:
"sampleList"
idUsuario
:
""
idea
:
true
identificador
:
""
paginaActual
:
2
tabId
:
""
[[Prototype]]
:
Object
y cuando es ajax salta el log de guardarLog("cargando post de coleccion");
*/
$defaults = [
'filtro' => '',
'tab_id' => '',
'posts' => 12,
'exclude' => [],
'post_type' => 'social_post',
'similar_to' => null,
'colec' => null,
'ideas' => null,
];
$args = array_merge($defaults, $args);
if (filter_var($args['ideas'], FILTER_VALIDATE_BOOLEAN)) {
guardarLog("cargando mas ideas");
$query_args = procesarIdeas($args, $paged);
if (!$query_args) {
error_log("[publicaciones] Error al procesar ideas.");
return false;
}
}
else if (!empty($args['colec']) && is_numeric($args['colec'])) {
guardarLog("cargando post de coleccion");
$samples_meta = get_post_meta($args['colec'], 'samples', true);
if (!is_array($samples_meta)) {
$samples_meta = maybe_unserialize($samples_meta);
}
if (is_array($samples_meta)) {
$query_args = [
'post_type' => $args['post_type'],
'post__in' => array_values($samples_meta),
'orderby' => 'post__in',
'posts_per_page' => 12,
];
} else {
error_log("[publicaciones] El meta 'samples' no es un array válido.");
return false;
}
} else {
$query_args = configuracionQueryArgs($args, $paged, $user_id, $current_user_id);
}
$output = procesarPublicaciones($query_args, $args, $is_ajax);
if ($is_ajax) {
echo $output;
wp_die();
}
return $output;
} catch (Exception $e) {
error_log("[publicaciones] Error crítico: " . $e->getMessage());
return false;
}
}
It looks like you're encountering an issue where the 'idea' parameter isn't being correctly processed in your AJAX request, causing the procesarIdeas function not to be called when expected. Let's dive into your code to identify the problem and provide a solution.
Issue Analysis
In your publicacionAjax() function, you're retrieving the 'idea' parameter from the AJAX request and passing it to the publicaciones() function:
However, in your publicaciones() function, you have the default arguments set up with the key 'ideas' (note the 's' at the end):
php
$defaults = [
// other defaults...
'ideas' => null,
];
Then, you're checking this parameter with:
php
if (filter_var($args['ideas'], FILTER_VALIDATE_BOOLEAN)) {
guardarLog("cargando mas ideas");
$query_args = procesarIdeas($args, $paged);
// ...
}
Here, the key 'ideas' doesn't match the key 'idea' that you're using in your AJAX request and when calling publicaciones(). This inconsistency means that $args['ideas'] remains null, and the condition fails, so procesarIdeas() is not being called.
Solution
To resolve this issue, you need to ensure consistent key usage across your code. Here are the steps to fix the problem:
Update the Default Arguments Key:
Replace 'ideas' with 'idea' in the $defaults array within your publicaciones() function:
php
$defaults = [
// other defaults...
'idea' => null,
];
Adjust the Condition Check:
Update the condition to check 'idea' instead of 'ideas':
php
if (filter_var($args['idea'], FILTER_VALIDATE_BOOLEAN)) {
guardarLog("cargando mas ideas");
$query_args = procesarIdeas($args, $paged);
if (!$query_args) {
error_log("[publicaciones] Error al procesar ideas.");
return false;
}
}
Ensure Consistent Key Usage Throughout:
Verify that all references to this parameter in your code use the same key 'idea'. This includes when you're setting up the arguments array, checking conditions, and any other references.
Updated publicaciones() Function
Here's how your updated publicaciones() function should look:
php
function publicaciones($args = [], $is_ajax = false, $paged = 1)
{
try {
$user_id = obtenerUserId($is_ajax);
$current_user_id = get_current_user_id();
if (!$current_user_id) {
error_log("[publicaciones] Advertencia: No se encontró ID de usuario");
}
$defaults = [
'filtro' => '',
'tab_id' => '',
'posts' => 12,
'exclude' => [],
'post_type' => 'social_post',
'similar_to' => null,
'colec' => null,
'idea' => null, // Changed from 'ideas' to 'idea'
];
$args = array_merge($defaults, $args);
if (filter_var($args['idea'], FILTER_VALIDATE_BOOLEAN)) { // Updated key here
guardarLog("cargando mas ideas");
$query_args = procesarIdeas($args, $paged);
if (!$query_args) {
error_log("[publicaciones] Error al procesar ideas.");
return false;
}
}
else if (!empty($args['colec']) && is_numeric($args['colec'])) {
guardarLog("cargando post de coleccion");
// Rest of your code...
} else {
$query_args = configuracionQueryArgs($args, $paged, $user_id, $current_user_id);
}
$output = procesarPublicaciones($query_args, $args, $is_ajax);
if ($is_ajax) {
echo $output;
wp_die();
}
return $output;
} catch (Exception $e) {
error_log("[publicaciones] Error crítico: " . $e->getMessage());
return false;
}
}
Testing the Solution
After making these changes, test your AJAX request again to see if the procesarIdeas() function is now being called properly. You should also see the log message "cargando mas ideas" when idea is true.
Additional Recommendations
Logging: Utilize logging (as you have done with guardarLog) to confirm the flow of execution and verify that the correct code paths are being taken.
Validate AJAX Data: Ensure that the data being sent via AJAX is correctly formatted and matches the expected data types in your PHP code.
Consistent Naming: Consistently use the same key names ('idea' vs. 'ideas') to prevent similar issues in the future.
Conclusion
The issue stemmed from a mismatch in the key names used for the 'idea' parameter. By ensuring that you're consistently using the same key throughout your code, you should be able to resolve this problem. If you continue to experience issues, please provide any additional error logs or details, and I'll be happy to help further.
Let me know if this works for you or if you need further assistance!