Changes to repeater ui, and completed MVP for custom image sizes

This commit is contained in:
Jeremy Rangel
2025-01-04 14:58:58 -08:00
parent 63e181109c
commit ceb37fc5af
8 changed files with 360 additions and 7 deletions

View File

@ -575,3 +575,120 @@ function lcp_delete_demo_posts_ajax_handler() {
// Send a success response back to the browser
wp_send_json_success('Demo posts deleted successfully.');
}
// Set a visited post cookies
// Hooked into send_headers via wp-hooks.php
function lcp_set_visited_posts_cookie() {
// Only track posts on single post pages
if (is_single()) {
// Get the current post's ID and post type
$current_post_id = get_the_ID();
$post_type = get_post_type();
$visit_time = time(); // Get the current time as a Unix timestamp
// Check if the cookie exists
if (isset($_COOKIE['lcp_visited_posts'])) {
// Decode the cookie's JSON value
$visited_posts = json_decode(stripslashes($_COOKIE['lcp_visited_posts']), true);
// Check if decoding was successful and if the format is correct
if (json_last_error() !== JSON_ERROR_NONE || !is_array($visited_posts)) {
// If the cookie is not valid, delete the cookie and start over
setcookie('lcp_visited_posts', '', time() - 3600, '/'); // Expire the cookie
$visited_posts = []; // Initialize an empty array
}
} else {
// Initialize an empty array if the cookie doesn't exist
$visited_posts = [];
}
// If the post type is not already in the cookie, initialize it as an empty array
if (!isset($visited_posts[$post_type])) {
$visited_posts[$post_type] = [];
}
// Add the current post ID and the visit timestamp as an array entry
// Ensure that the post ID isn't already added for this post type
$found = false;
foreach ($visited_posts[$post_type] as &$post) {
if (array_key_exists($current_post_id, $post)) {
$post[$current_post_id] = $visit_time; // Update the timestamp if post already exists
$found = true;
break;
}
}
// If post wasn't found, add it as a new entry
if (!$found) {
$visited_posts[$post_type][] = [$current_post_id => $visit_time];
}
// Encode the updated visited posts array to JSON
$cookie_value = json_encode($visited_posts);
// Set the cookie with a 30-day expiration time (this must be done before any output)
setcookie('lcp_visited_posts', $cookie_value, time() + (30 * 24 * 60 * 60), '/'); // '/' makes the cookie available across the whole site
}
}
/* CUSTOM IMAGE SIZES */
function lcp_update_image_sizes() {
// Verify nonce for security
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'custom_image_sizes_nonce')) {
wp_send_json_error(array('message' => 'Nonce validation failed.'));
return;
}
// Check if image_sizes data is provided
if (isset($_POST['image_sizes'])) {
$image_sizes = json_decode(stripslashes($_POST['image_sizes']), true); // Decode JSON data
// Make sure that the image_sizes data is valid
if (!is_array($image_sizes)) {
wp_send_json_error(array('message' => 'Invalid image sizes data.'));
return;
}
// Process the data (e.g., update options)
update_option('lcp_image_sizes', $image_sizes);
// Return success message
wp_send_json_success(array('message' => 'Image sizes updated successfully.'));
} else {
wp_send_json_error(array('message' => 'No image sizes data received.'));
}
}
add_action('wp_ajax_update_lcp_theme_settings', 'lcp_update_image_sizes');
add_action('wp_ajax_nopriv_update_lcp_theme_settings', 'lcp_update_image_sizes');
function lcp_register_custom_image_sizes() {
// Retrieve the 'lcp_image_sizes' option, which is already unserialized by WordPress
$custom_image_sizes = get_option('lcp_image_sizes');
// Check if the option exists and is an array (because WordPress auto-unserializes the data)
if ($custom_image_sizes && is_array($custom_image_sizes)) {
// Loop through the array and register each image size
foreach ($custom_image_sizes as $size) {
// Ensure the size has the correct keys and valid values
if (isset($size['name'], $size['width'], $size['height'], $size['crop']) && !empty($size['name'])) {
// Sanitize the name, but ensure it's not just a number like '1'
$name = sanitize_key($size['name']);
$width = (int) $size['width']; // Cast to integer
$height = (int) $size['height']; // Cast to integer
$crop = (bool) $size['crop']; // Convert to boolean
// Register the custom image size
add_image_size($name, $width, $height, $crop);
}
}
}
}
// Hook into WordPress to register custom sizes
add_action('after_setup_theme', 'lcp_register_custom_image_sizes');