| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) |
| 3 |
exit; // Exit if accessed directly |
| 4 |
|
| 5 |
// this file print gallery - gallery callback |
| 6 |
if (!function_exists('ufg_gallery')) { |
| 7 |
function ufg_gallery($ufg_gallery_id, $ufg_gallery, $ufg_images_per_page, $atts = array()) |
| 8 |
{ |
| 9 |
if (!is_array($ufg_gallery)) $ufg_gallery = array(); |
| 10 |
if (!isset($ufg_gallery['ufg-attachment-id']) || !is_array($ufg_gallery['ufg-attachment-id'])) $ufg_gallery['ufg-attachment-id'] = array(); |
| 11 |
if (!isset($ufg_gallery['ufg-title']) || !is_array($ufg_gallery['ufg-title'])) $ufg_gallery['ufg-title'] = array(); |
| 12 |
|
| 13 |
$load_class = ''; |
| 14 |
$new_array = array(); |
| 15 |
$new_array_final = array(); |
| 16 |
|
| 17 |
$ufg_setting = get_option("ufg_settings_" . $ufg_gallery_id); |
| 18 |
include('setting.php'); |
| 19 |
|
| 20 |
echo "<div id='ufg-gallery-" . esc_attr($ufg_gallery_id) . "' class='ufg-gallery'>"; |
| 21 |
echo "<div class='ufg-grid-sizer'></div>"; |
| 22 |
$j = 0; |
| 23 |
$new_array = array(); |
| 24 |
$new_array_final = array(); |
| 25 |
if (is_array($ufg_gallery) && array_key_exists('ufg-image-filters', $ufg_gallery)) { |
| 26 |
foreach ($ufg_gallery['ufg-image-filters'] as $key => $array) { |
| 27 |
if (is_array($array)) { |
| 28 |
foreach ($array as $key2 => $val) { |
| 29 |
if (strpos($val, ',') !== false) { |
| 30 |
$parts = explode(',', $val); |
| 31 |
foreach ($parts as $p) { |
| 32 |
$p = trim($p); |
| 33 |
if (!empty($p)) { |
| 34 |
$new_array[$p][$j] = $key; |
| 35 |
} |
| 36 |
} |
| 37 |
} else { |
| 38 |
//array_push($new_array[], $val); |
| 39 |
$new_array[$val][$j] = $key; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
$j++; |
| 44 |
} |
| 45 |
foreach ($new_array as $new_key => $new_val) { |
| 46 |
$new_re_in = array_values($new_val); |
| 47 |
$new_array_final[$new_key] = $new_re_in; |
| 48 |
} |
| 49 |
$filter_image = $new_array_final; |
| 50 |
|
| 51 |
if (!function_exists('ufg_expand_filter_images_hierarchy')) { |
| 52 |
function ufg_expand_filter_images_hierarchy($filters, &$filter_images) { |
| 53 |
$all_images = array(); |
| 54 |
if (is_array($filters)) { |
| 55 |
foreach ($filters as $f) { |
| 56 |
if (!isset($f->filterkey)) continue; |
| 57 |
$key = str_replace(" ", "-", strtolower(trim($f->filterkey))); |
| 58 |
|
| 59 |
$my_images = isset($filter_images[$key]) ? $filter_images[$key] : array(); |
| 60 |
|
| 61 |
if (isset($f->children) && is_array($f->children)) { |
| 62 |
ufg_expand_filter_images_hierarchy($f->children, $filter_images); |
| 63 |
} |
| 64 |
|
| 65 |
$my_images = array_values(array_unique($my_images)); |
| 66 |
if (!empty($my_images)) { |
| 67 |
$filter_images[$key] = $my_images; |
| 68 |
} |
| 69 |
$all_images = array_merge($all_images, $my_images); |
| 70 |
} |
| 71 |
} |
| 72 |
return array_unique($all_images); |
| 73 |
} |
| 74 |
} |
| 75 |
if (!isset($ufg_filters) || empty($ufg_filters)) { |
| 76 |
$ufg_filters = get_option("ufg_filters_" . $ufg_gallery_id); |
| 77 |
} |
| 78 |
if (!empty($ufg_filters)) { |
| 79 |
if (function_exists('ufg_normalize_filters_recursive')) { |
| 80 |
ufg_normalize_filters_recursive($ufg_filters); |
| 81 |
} |
| 82 |
if (function_exists('ufg_remove_blank_filters_recursive')) { |
| 83 |
ufg_remove_blank_filters_recursive($ufg_filters); |
| 84 |
} |
| 85 |
ufg_expand_filter_images_hierarchy($ufg_filters, $filter_image); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
//image sorting |
| 90 |
if (is_array($ufg_gallery) && array_key_exists('ufg-title', $ufg_gallery)) { |
| 91 |
if ($ufg_image_sorting == 1) |
| 92 |
ksort($ufg_gallery['ufg-title']); //ascending image id |
| 93 |
if ($ufg_image_sorting == 2) |
| 94 |
krsort($ufg_gallery['ufg-title']); //descending image id |
| 95 |
} |
| 96 |
|
| 97 |
$ufg_total_images = is_array($ufg_gallery['ufg-attachment-id']) ? count($ufg_gallery['ufg-attachment-id']) : 0; |
| 98 |
|
| 99 |
// load more array |
| 100 |
$load_id_array = array(); |
| 101 |
if ($ufg_image_sorting == 5) { //if sorting is OFF |
| 102 |
if (is_array($ufg_gallery['ufg-attachment-id'])) { |
| 103 |
foreach ($ufg_gallery['ufg-attachment-id'] as $value) { |
| 104 |
$load_id_array[] = $value; |
| 105 |
} |
| 106 |
} |
| 107 |
} else { //if sorting is ON |
| 108 |
if (is_array($ufg_gallery['ufg-title'])) { |
| 109 |
foreach ($ufg_gallery['ufg-title'] as $key => $value) { |
| 110 |
$load_id_array[] = $key; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
//echo $ufg_total_images; |
| 116 |
|
| 117 |
//keys: ufg-attachment-id / ufg-title / ufg-alt / ufg-description / ufg-url / ufg-image-filters |
| 118 |
//defaults |
| 119 |
$ufg_title = $ufg_alt = $ufg_description = $ufg_url = ""; |
| 120 |
|
| 121 |
// Load more var |
| 122 |
$load_more = 'off'; |
| 123 |
$count = 0; |
| 124 |
$no = 1; |
| 125 |
//******************** Load Image With Limit [Shortcode] *******************// |
| 126 |
if ($load_more != 'on') { |
| 127 |
|
| 128 |
if ($ufg_image_sorting == 5) { //if sorting is OFF |
| 129 |
$reversed_attachment_ids = is_array($ufg_gallery['ufg-attachment-id']) ? $ufg_gallery['ufg-attachment-id'] : array(); |
| 130 |
//$reversed_attachment_ids = array_reverse($ufg_gallery['ufg-attachment-id'], true); |
| 131 |
foreach ($reversed_attachment_ids as $value) { |
| 132 |
$attachment_id = $value; |
| 133 |
// Load Gallery Content |
| 134 |
include('gallery-content.php'); |
| 135 |
} |
| 136 |
} else { //if sorting is ON |
| 137 |
$reversed_attachment_ids = is_array($ufg_gallery['ufg-title']) ? $ufg_gallery['ufg-title'] : array(); |
| 138 |
foreach ($reversed_attachment_ids as $key => $value) { |
| 139 |
$attachment_id = $key; |
| 140 |
// Load Gallery Content |
| 141 |
include('gallery-content.php'); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
//******************** Load More Is ON *******************// |
| 148 |
if ($load_more == 'on') { |
| 149 |
// run loop according to remaining images |
| 150 |
//$load_limit = 5; |
| 151 |
$load_limit_int = (int) $load_limit; |
| 152 |
if ($load_limit_int <= 0) { |
| 153 |
$load_limit_int = 4; // Fallback default if empty or 0 |
| 154 |
} |
| 155 |
|
| 156 |
$remain_images = $ufg_total_images - $load_limit_int; |
| 157 |
if ($remain_images < 0) { |
| 158 |
$load_limit_int = $load_limit_int + $remain_images; |
| 159 |
} |
| 160 |
|
| 161 |
for ($i = 0; $i < $load_limit_int; $i++) { |
| 162 |
$attachment_id = $load_id_array[$i]; |
| 163 |
// Load Gallery Data & Content |
| 164 |
include('gallery-content.php'); |
| 165 |
|
| 166 |
$no++; |
| 167 |
$count++; |
| 168 |
} |
| 169 |
|
| 170 |
if (isset($_POST['ufg_security'])) { |
| 171 |
$ufg_security = sanitize_text_field(wp_unslash($_POST['ufg_security'])); |
| 172 |
if (wp_verify_nonce($ufg_security, 'ufg_load_more_nonce')) { |
| 173 |
$ufg_limit_start = isset($_POST['ufg_limit_start']) ? intval(wp_unslash($_POST['ufg_limit_start'])) : 0; |
| 174 |
$ufg_limit_end = isset($_POST['ufg_limit_end']) ? intval(wp_unslash($_POST['ufg_limit_end'])) : 0; |
| 175 |
$targetFilter = isset($_POST['targetFilter']) ? sanitize_text_field(wp_unslash($_POST['targetFilter'])) : ''; |
| 176 |
$CalTotalLoadedItem = isset($_POST['CalTotalLoadedItem']) ? intval(wp_unslash($_POST['CalTotalLoadedItem'])) : 0; |
| 177 |
|
| 178 |
// get already loaded images id |
| 179 |
$get_all_items = isset($_POST['get_all_items']) ? sanitize_text_field(wp_unslash($_POST['get_all_items'])) : ''; |
| 180 |
$get_all_items_val = explode(",", $get_all_items); |
| 181 |
$img_ids_diff = array_diff($load_id_array, $get_all_items_val); |
| 182 |
$img_ids_diff2 = array_values($img_ids_diff); |
| 183 |
$no = 0; |
| 184 |
|
| 185 |
if ($targetFilter != '*' && isset($filter_image[$targetFilter]) && is_array($filter_image[$targetFilter])) { |
| 186 |
$targetFilterD = explode(",", $targetFilter); |
| 187 |
foreach ($targetFilterD as $key => $targetFilter_r) { |
| 188 |
if (isset($filter_image[$targetFilter_r]) && is_array($filter_image[$targetFilter_r])) { |
| 189 |
foreach ($filter_image[$targetFilter_r] as $key => $filter_image_r) { |
| 190 |
$attached[] = $filter_image_r; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
// unset already load images |
| 195 |
$img_ids_diff_filter = array_diff($attached, $get_all_items_val); |
| 196 |
$img_ids_diff_val = array_values($img_ids_diff_filter); |
| 197 |
|
| 198 |
$img_ids_diff_val_count = count($img_ids_diff_val); |
| 199 |
} |
| 200 |
|
| 201 |
/* echo "<pre>"; |
| 202 |
print_r($ufg_limit_start); |
| 203 |
echo "</pre>"; |
| 204 |
echo "<br>"; |
| 205 |
echo "<pre>"; |
| 206 |
print_r($ufg_limit_start); |
| 207 |
echo "</pre>"; |
| 208 |
echo "<br>"; |
| 209 |
echo "<pre>"; |
| 210 |
print_r($ufg_limit_end); |
| 211 |
echo "</pre>"; */ |
| 212 |
|
| 213 |
for ($i = $ufg_limit_start; $i < $ufg_limit_end; $i++) { |
| 214 |
|
| 215 |
if ($targetFilter != '*') { |
| 216 |
if (!isset($img_ids_diff_val[$no])) { |
| 217 |
break; |
| 218 |
} |
| 219 |
$attachment_id = $img_ids_diff_val[$no]; |
| 220 |
} else { |
| 221 |
if (!isset($img_ids_diff2[$no])) { |
| 222 |
break; |
| 223 |
} |
| 224 |
$attachment_id = $img_ids_diff2[$no]; |
| 225 |
} |
| 226 |
|
| 227 |
if ($load_more == 'on') { |
| 228 |
$load_class = 'ufg_result'; |
| 229 |
} else { |
| 230 |
$load_class = ''; |
| 231 |
} |
| 232 |
|
| 233 |
// Load Gallery Data & Content |
| 234 |
include('gallery-content.php'); |
| 235 |
|
| 236 |
$CalTotalLoadedItem++; |
| 237 |
$count++; |
| 238 |
$no++; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
echo "</div>"; |
| 244 |
} |
| 245 |
} |
| 246 |
?> |