2023-01-21 01:31:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
// SAVE BOOKMARKS FOR USERS - AJAX
|
|
|
|
// =============================================================================
|
|
|
|
|
2023-08-08 22:45:55 +02:00
|
|
|
/**
|
|
|
|
* Save bookmarks JSON for user via AJAX
|
|
|
|
*
|
2023-10-07 13:35:21 +02:00
|
|
|
* Note: Bookmarks are not evaluated server-side, only stored as JSON string.
|
|
|
|
* Everything else happens client-side.
|
|
|
|
*
|
2024-01-26 17:45:59 +01:00
|
|
|
* @since 4.0.0
|
2023-08-08 22:45:55 +02:00
|
|
|
* @link https://developer.wordpress.org/reference/functions/wp_send_json_success/
|
|
|
|
* @link https://developer.wordpress.org/reference/functions/wp_send_json_error/
|
|
|
|
* @see fictioneer_get_validated_ajax_user()
|
|
|
|
*/
|
|
|
|
|
|
|
|
function fictioneer_ajax_save_bookmarks() {
|
2023-08-21 15:45:54 +02:00
|
|
|
// Enabled?
|
|
|
|
if ( ! get_option( 'fictioneer_enable_bookmarks' ) ) {
|
2024-10-12 23:42:54 +02:00
|
|
|
wp_send_json_error( null, 403 );
|
2023-08-21 15:45:54 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 22:45:55 +02:00
|
|
|
// Setup and validations
|
|
|
|
$user = fictioneer_get_validated_ajax_user();
|
2023-08-21 15:31:07 +02:00
|
|
|
|
|
|
|
if ( ! $user ) {
|
2024-10-12 23:42:54 +02:00
|
|
|
wp_send_json_error( array( 'error' => 'Request did not pass validation.' ) );
|
2023-08-21 15:31:07 +02:00
|
|
|
}
|
2023-08-08 22:45:55 +02:00
|
|
|
|
|
|
|
if ( empty( $_POST['bookmarks'] ) ) {
|
2024-10-12 23:42:54 +02:00
|
|
|
wp_send_json_error( array( 'error' => 'Missing arguments.' ) );
|
2023-08-08 22:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid?
|
|
|
|
$bookmarks = sanitize_text_field( $_POST['bookmarks'] );
|
2023-01-21 01:31:34 +01:00
|
|
|
|
2023-10-07 04:23:10 +02:00
|
|
|
if ( $bookmarks && fictioneer_is_valid_json( wp_unslash( $bookmarks ) ) ) {
|
2023-08-08 22:45:55 +02:00
|
|
|
// Inspect
|
2023-10-07 04:23:10 +02:00
|
|
|
$decoded = json_decode( wp_unslash( $bookmarks ), true );
|
2023-08-08 22:45:55 +02:00
|
|
|
|
2023-10-07 01:35:50 +02:00
|
|
|
if ( ! $decoded || ! isset( $decoded['data'] ) ) {
|
2024-10-12 23:42:54 +02:00
|
|
|
wp_send_json_error( array( 'error' => 'Invalid JSON.' ) );
|
2023-01-21 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
2023-10-07 04:23:10 +02:00
|
|
|
// Update and response (uses wp_slash/wp_unslash internally)
|
2023-10-07 01:35:50 +02:00
|
|
|
$old_bookmarks = get_user_meta( $user->ID, 'fictioneer_bookmarks', true );
|
|
|
|
|
2023-10-07 13:35:21 +02:00
|
|
|
if ( wp_unslash( $bookmarks ) === $old_bookmarks ) {
|
2023-10-07 01:35:50 +02:00
|
|
|
wp_send_json_success(); // Nothing to update
|
|
|
|
}
|
|
|
|
|
2023-08-08 22:45:55 +02:00
|
|
|
if ( update_user_meta( $user->ID, 'fictioneer_bookmarks', $bookmarks ) ) {
|
2023-10-07 04:23:10 +02:00
|
|
|
wp_send_json_success();
|
2023-08-08 22:45:55 +02:00
|
|
|
} else {
|
2024-10-12 23:42:54 +02:00
|
|
|
wp_send_json_error( array( 'error' => 'Bookmarks could not be updated.' ) );
|
2023-08-08 22:45:55 +02:00
|
|
|
}
|
2023-01-21 01:31:34 +01:00
|
|
|
}
|
2023-08-08 22:45:55 +02:00
|
|
|
|
|
|
|
// Something went wrong if we end up here...
|
2024-10-12 23:42:54 +02:00
|
|
|
wp_send_json_error( array( 'error' => 'An unknown error occurred.' ) );
|
2023-01-21 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( get_option( 'fictioneer_enable_bookmarks' ) ) {
|
|
|
|
add_action( 'wp_ajax_fictioneer_ajax_save_bookmarks', 'fictioneer_ajax_save_bookmarks' );
|
|
|
|
}
|