Changes to blocks

This commit is contained in:
Jeremy Rangel
2024-12-22 15:20:12 -08:00
parent cfbb860bf9
commit f3fbe0fa32
25 changed files with 612 additions and 1035 deletions

View File

@ -87,14 +87,51 @@ if (!function_exists('lcp_random_string')) {
}
}
// Return a valid array of Media IDs
// Used to return custom meta fields/table fields data as an array
function validate_media_ids($input) {
// Check if the input is a serialized array (e.g., 'a:2:{i:0;s:2:"63";i:1;s:2:"64";}')
if (is_string($input) && (@unserialize($input) !== false || $input === 'b:0;')) {
// If serialized, unserialize it
$media_ids = unserialize($input);
// Ensure the unserialized data is an array
if (is_array($media_ids)) {
// Return the array of IDs (ensure they're integers)
return array_map('intval', array_filter($media_ids));
}
}
// Check if the input is a comma-separated string of IDs (e.g., "112,512,581")
if (is_string($input)) {
// Split the string into an array using commas as separators
$media_ids = array_map('intval', explode(',', $input));
// Filter out any invalid IDs (non-numeric or empty)
return array_filter(array_unique($media_ids));
}
// Check if the input is already an array (e.g., [63, 64, 112])
if (is_array($input)) {
// Ensure the array only contains integers
return array_filter(array_map('intval', $input));
}
// Return an empty array if the input format is not recognized
return [];
}
// Return an array of Media IDs
function get_media_ids($attributes) {
// Ensure 'galleryItems' exists in the attributes array
if (isset($attributes['galleryItems']) && $attributes['source'] == "manual") {
// Manual Source
if ($attributes['source'] == "manual" && isset($attributes['galleryItems'])) {
// Extract 'id' from each item in the 'galleryItems' array
$media_ids = array_map(function($item) {
return $item['id']; // Return the 'id' value from each array item
}, $attributes['galleryItems']); // Use $attributes['galleryItems'] here
// Check if we need to include the post thumbnail
if ($attributes['includePostThumbnail']) {
// Get the featured image (post thumbnail) ID
@ -104,14 +141,30 @@ function get_media_ids($attributes) {
array_unshift($media_ids, $featured_media_id);
}
}
// Return the array of media IDs
// Post Meta Source
return $media_ids;
}
// Meta Field Source
elseif (isset($attributes['source']) && $attributes['source'] == "metaField") {
// Ensure 'sourceMetaField' exists in the attributes array
if (isset($attributes['sourceMetaField'])) {
// Retrieve the raw data directly from the custom field (it will be automatically unserialized by WordPress if it's a serialized string)
$raw_media_ids = get_post_meta(get_the_ID(), $attributes['sourceMetaField'], true);
// Pass the raw data to the helper function to decide how to process it
$media_ids = validate_media_ids($raw_media_ids);
// Return the processed media IDs
return $media_ids;
}
}
// Return an empty array if 'galleryItems' doesn't exist or 'source' is not 'manual'
// Return an empty array if no gallery items or source is not handled
return [];
}
//Get the gallery items from the media IDs
function get_gallery_items_data($media_ids) {
$gallery_items = array();
@ -390,42 +443,42 @@ function render_lcp_gallery_block($attributes) {
// Generate styles for the gallery based on the unique class
$classes = 'lcp-gallery '; // Start with lcp-gallery class
$classes .= $unique_class;
// Generate styles for the gallery based on the unique class
$classes = 'lcp-gallery '; // Start with lcp-gallery class
$classes .= $unique_class;
// Check if 'initialLayout' is set to 'grid' and add grid column classes if applicable
if (isset($attributes['initialLayout']) && $attributes['initialLayout'] === 'grid') {
// Image aspect ratios
$classes .= ' aspect-' . esc_attr($attributes['itemsAspectRatio']);
// Grid classes
$classes .= ' grid';
// Grid columns
if (isset($attributes['gridColumnsLarge'])) {
$classes .= ' large-' . esc_attr($attributes['gridColumnsLarge']) . '-columns';
// Check if 'initialLayout' is set to 'grid' and add grid column classes if applicable
if (isset($attributes['initialLayout']) && $attributes['initialLayout'] === 'grid') {
// Image aspect ratios
$classes .= ' aspect-' . esc_attr($attributes['itemsAspectRatio']);
// Grid classes
$classes .= ' grid';
// Grid columns
if (isset($attributes['gridColumnsLarge'])) {
$classes .= ' large-' . esc_attr($attributes['gridColumnsLarge']) . '-columns';
}
if (isset($attributes['gridColumnsMedium'])) {
$classes .= ' medium-' . esc_attr($attributes['gridColumnsMedium']) . '-columns';
}
if (isset($attributes['gridColumnsSmall'])) {
$classes .= ' small-' . esc_attr($attributes['gridColumnsSmall']) . '-columns';
}
}
if (isset($attributes['gridColumnsMedium'])) {
$classes .= ' medium-' . esc_attr($attributes['gridColumnsMedium']) . '-columns';
// Add 'initialLayout' class if it exists and isn't 'grid'
if (isset($attributes['initialLayout']) && !empty($attributes['initialLayout']) && $attributes['initialLayout'] !== 'grid') {
$classes .= ' ' . "lcp-inline-gallery";
}
if (isset($attributes['gridColumnsSmall'])) {
$classes .= ' small-' . esc_attr($attributes['gridColumnsSmall']) . '-columns';
}
}
// Add 'initialLayout' class if it exists and isn't 'grid'
if (isset($attributes['initialLayout']) && !empty($attributes['initialLayout']) && $attributes['initialLayout'] !== 'grid') {
$classes .= ' ' . "lcp-inline-gallery";
}
// Build the styles using the unique class (if necessary)
$styles = build_gallery_styles($attributes, $unique_class);
// Build the styles using the unique class (if necessary)
$styles = build_gallery_styles($attributes, $unique_class);
// Return the complete gallery HTML with the unique class and the settings as a JSON string
return "
<div class= '{$classes}' data-lgSettings='" . esc_attr($gallery_settings_json) . "'>
{$gallery_items_html}
</div>";
// Return the complete gallery HTML with the unique class and the settings as a JSON string
return "
<div class= '{$classes}' data-lgSettings='" . esc_attr($gallery_settings_json) . "'>
{$gallery_items_html}
</div>";
}
/* Initialize Gallery Block */
function lcp_gallery_block_init() {