PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.1.1
Post Grid Gutenberg Blocks – PostX v5.1.1
5.1.1 5.1.0 5.0.41 5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 All 240 releases
ultimate-post / assets / js / ultp.js

ultp.js in Post Grid Gutenberg Blocks – PostX 5.1.1, at assets/js/ultp.js

2,159 lines 59.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2 "use strict";
3
4 const isFront = $("body.postx-admin-page").length == 0;
5 // *************************************
6 // Nonce Generation for AJAX
7 // *************************************
8 const ULTP_NONCE_KEY = "ultp_public_nonce";
9
10 // sessionStorage throws outright when the browser blocks site data (private
11 // windows, cookie-blocking extensions). Guard every access so that degrades to
12 // "fetch a nonce per request" instead of breaking the block entirely.
13 function readStoredNonce() {
14 try {
15 return sessionStorage.getItem(ULTP_NONCE_KEY);
16 } catch (e) {
17 return null;
18 }
19 }
20
21 function storeNonce(value) {
22 try {
23 sessionStorage.setItem(ULTP_NONCE_KEY, value);
24 } catch (e) {
25 // Not cacheable in this browser; every call refetches instead.
26 }
27 }
28
29 function clearStoredNonce() {
30 try {
31 sessionStorage.removeItem(ULTP_NONCE_KEY);
32 } catch (e) {
33 // Nothing was stored, so nothing to clear.
34 }
35 }
36
37 async function getUltpNonce(forceRefresh = false) {
38 if (!forceRefresh) {
39 const nonce = readStoredNonce();
40 if (nonce) return nonce;
41 }
42
43 try {
44 const response = await $.ajax({
45 url: ultp_data_frontend.ajax,
46 type: "POST",
47 data: { action: "ultp_get_nonce" },
48 });
49
50 if (response?.success && response?.data?.nonce) {
51 storeNonce(response.data.nonce);
52 return response.data.nonce;
53 }
54
55 console.log("Failed to fetching nonce");
56 return null;
57 } catch (xhr) {
58 console.log(
59 "Error occured.please try again" + xhr.statusText + xhr.responseText,
60 );
61 console.log("Nonce Not Generating Properly");
62 return null;
63 }
64 }
65
66 // A front-end nonce goes stale in normal use: WordPress nonces last 12-24h and
67 // a tab can stay open longer, a full-page cache can serve HTML carrying a nonce
68 // minted for an earlier visitor, and a plugin update can change the action name
69 // under a tab that is already open. All three look identical from here, and
70 // before this existed all three broke the block with no way back short of a
71 // reload. PHP marks exactly this case with ultp_invalid_nonce.
72 function isExpiredNonce(xhr) {
73 return (
74 xhr?.status === 403 &&
75 xhr?.responseJSON?.data?.code === "ultp_invalid_nonce"
76 );
77 }
78
79 /**
80 * $.ajax for the nonce-guarded front-end endpoints.
81 *
82 * Takes the same options as $.ajax plus `nonce`, the value to try first, and
83 * injects it as `wpnonce`. On an expired-nonce 403 it discards the cached
84 * nonce, fetches a fresh one and replays the request once. The retry is
85 * invisible to the caller: `success` runs only for the attempt that worked,
86 * and `complete`/`error` are suppressed on an attempt that is being retried,
87 * so loading states are not torn down half way.
88 */
89 function ultpAjax(options) {
90 const {
91 nonce: initialNonce,
92 data = {},
93 success,
94 error,
95 complete,
96 ...rest
97 } = options;
98
99 let retried = false;
100
101 function fail(xhr, ...args) {
102 if (typeof error === "function") error.call(this, xhr, ...args);
103 if (typeof complete === "function") complete.call(this, xhr, ...args);
104 }
105
106 function send(nonce) {
107 // Set per attempt, not per call: it must not leak into the retry.
108 let retrying = false;
109
110 $.ajax({
111 ...rest,
112 data: { ...data, wpnonce: nonce },
113 success,
114 error(xhr, ...args) {
115 if (!retried && isExpiredNonce(xhr)) {
116 retried = true;
117 retrying = true;
118 clearStoredNonce();
119 getUltpNonce(true).then((fresh) => {
120 if (fresh) {
121 send(fresh);
122 } else {
123 fail.call(this, xhr, ...args);
124 }
125 });
126 return;
127 }
128 if (typeof error === "function") error.call(this, xhr, ...args);
129 },
130 complete(xhr, ...args) {
131 if (retrying) return;
132 if (typeof complete === "function") complete.call(this, xhr, ...args);
133 },
134 });
135 }
136
137 // Prefer a nonce fetched live over whatever the caller passed. A caller's
138 // nonce comes either from page HTML, which a full-page cache can serve
139 // stale, or from a value captured once at init and reused for the life of
140 // the page (UltpInfiniteScroll does this). Reading storage first costs
141 // nothing and stops those callers replaying a known-dead nonce forever.
142 const storedNonce = readStoredNonce();
143 if (storedNonce || initialNonce) {
144 send(storedNonce || initialNonce);
145 return;
146 }
147
148 getUltpNonce().then((nonce) => {
149 if (nonce) {
150 send(nonce);
151 } else {
152 fail({ status: 0, statusText: "no nonce", responseText: "" });
153 }
154 });
155 }
156
157 // *************************************
158 // Social Share window
159 // *************************************
160 $(".ultp-post-share-item a").each(function () {
161 $(this).on("click", function () {
162 // For Share window opening
163 let share_url = $(this).attr("url");
164 let width = 800;
165 let height = 500;
166 let leftPosition, topPosition;
167 //Allow for borders.
168 leftPosition = window.screen.width / 2 - (width / 2 + 10);
169 //Allow for title and status bars.
170 topPosition = window.screen.height / 2 - (height / 2 + 50);
171 let windowFeatures =
172 "height=" +
173 height +
174 ",width=" +
175 width +
176 ",resizable=yes,left=" +
177 leftPosition +
178 ",top=" +
179 topPosition +
180 ",screenX=" +
181 leftPosition +
182 ",screenY=" +
183 topPosition;
184 window.open(share_url, "sharer", windowFeatures);
185 // For Share count add
186 let id = $(this)
187 .parents(".ultp-post-share-item-inner-block")
188 .attr("postId");
189 let count = $(this)
190 .parents(".ultp-post-share-item-inner-block")
191 .attr("count");
192 ultpAjax({
193 url: ultp_data_frontend.ajax,
194 type: "POST",
195 nonce: ultp_data_frontend.security,
196 data: {
197 action: "ultp_share_count",
198 shareCount: count,
199 postId: id,
200 },
201 error: function (xhr) {
202 console.log(
203 "Error occured.please try again" +
204 xhr.statusText +
205 xhr.responseText,
206 );
207 },
208 });
209
210 return false;
211 });
212 });
213 // remove sticky behavior when footer is visible
214 $(window).on("scroll", function () {
215 if (
216 $(window).scrollTop() + window.innerHeight >=
217 $("footer")?.offset()?.top
218 ) {
219 $(
220 ".wp-block-ultimate-post-post_share .ultp-block-wrapper .ultp-disable-sticky-footer",
221 ).addClass("remove-sticky");
222 } else {
223 $(
224 ".wp-block-ultimate-post-post_share .ultp-block-wrapper .ultp-disable-sticky-footer",
225 ).removeClass("remove-sticky");
226 }
227 });
228
229 // *************************************
230 // News Ticker
231 // *************************************
232 $(".ultp-news-ticker").each(function () {
233 $(this).UltpSlider({
234 type: $(this).data("type"),
235 direction: $(this).data("direction"),
236 speed: $(this).data("speed"),
237 pauseOnHover: $(this).data("hover") == 1 ? true : false,
238 controls: {
239 prev: $(this)
240 .closest(".ultp-newsTicker-wrap")
241 .find(".ultp-news-ticker-prev"),
242 next: $(this)
243 .closest(".ultp-newsTicker-wrap")
244 .find(".ultp-news-ticker-next"),
245 toggle: $(this)
246 .closest(".ultp-newsTicker-wrap")
247 .find(".ultp-news-ticker-pause"),
248 },
249 });
250 });
251
252 // *************************************
253 // Table of Contents
254 // *************************************
255 $(".ultp-toc-backtotop").on("click", function (e) {
256 e.preventDefault();
257 $("html, body").animate({ scrollTop: 0 }, "slow");
258 });
259
260 $(window).on("scroll", function () {
261 scrollTopButton();
262 });
263
264 function scrollTopButton() {
265 if ($(document).scrollTop() > 1000) {
266 $(".ultp-toc-backtotop").addClass("tocshow");
267 $(".wp-block-ultimate-post-table-of-content").addClass("ultp-toc-scroll");
268 } else {
269 $(".ultp-toc-backtotop").removeClass("tocshow");
270 $(".wp-block-ultimate-post-table-of-content").removeClass(
271 "ultp-toc-scroll",
272 );
273 }
274 }
275 scrollTopButton();
276
277 $(".ultp-collapsible-open").on("click", function (e) {
278 $(this)
279 .closest(".ultp-collapsible-toggle")
280 .removeClass("ultp-toggle-collapsed");
281 $(this).parents(".ultp-block-toc").find(".ultp-block-toc-body").show();
282 });
283
284 $(".ultp-collapsible-hide").on("click", function (e) {
285 $(this)
286 .closest(".ultp-collapsible-toggle")
287 .addClass("ultp-toggle-collapsed");
288 $(this).parents(".ultp-block-toc").find(".ultp-block-toc-body").hide();
289 });
290
291 $(".ultp-toc-lists li a").on("click", function () {
292 $([document.documentElement, document.body]).animate(
293 {
294 scrollTop: $($(this).attr("href")).offset().top - 50,
295 },
296 500,
297 );
298 });
299
300 // *************************************
301 // Flex Menu
302 // *************************************
303 $(document).ready(function () {
304 if ($(".ultp-flex-menu").length > 0) {
305 const menuText = $("ul.ultp-flex-menu").data("name");
306 $("ul.ultp-flex-menu").flexMenu({
307 linkText: menuText,
308 linkTextAll: menuText,
309 linkTitle: menuText,
310 });
311 }
312 });
313 $(document).on("click", function (e) {
314 if ($(e.target).closest(".flexMenu-viewMore").length === 0) {
315 $(".flexMenu-viewMore").removeClass("active");
316 $(".flexMenu-viewMore")
317 .children("ul.flexMenu-popup")
318 .css("display", "none");
319 }
320 });
321 $(document).on(
322 "click",
323 ".ultp-filter-navigation .flexMenu-popup .filter-item",
324 function (e) {
325 $(".flexMenu-viewMore").removeClass("active");
326 $(".flexMenu-viewMore")
327 .children("ul.flexMenu-popup")
328 .css("display", "none");
329 },
330 );
331
332 // *************************************
333 // Pagination Block
334 // *************************************
335 $(".ultp-post-grid-parent").each(function () {
336 const grid = $(this).find(".ultp-post-grid-block");
337 const pagiWrapper = $(this).find(".ultp-pagination-block");
338 const pagi = grid.find(".pagination-block-html > div");
339
340 if (pagiWrapper.length < 1 || pagi.length < 1) {
341 return;
342 }
343
344 pagiWrapper
345 .attr("class")
346 .split(" ")
347 .forEach((cl) => {
348 pagi.addClass(cl);
349 });
350
351 pagiWrapper.html(pagi);
352 });
353
354 // *************************************
355 // Previous Next
356 // *************************************
357
358 $(document).off(
359 "click",
360 ".ultp-pagination-ajax-action li, .ultp-loadmore-action, .ultp-prev-action, .ultp-next-action",
361 function (e) {},
362 );
363 $(document).on("click", ".ultp-prev-action, .ultp-next-action", function (e) {
364 e.preventDefault();
365 let parents = $(this).closest(".ultp-next-prev-wrap"),
366 wrap = parents
367 .closest(".ultp-block-wrapper")
368 .find(".ultp-block-items-wrap"),
369 paged = parseInt(parents.data("pagenum")),
370 pages = parseInt(parents.data("pages")),
371 blockWrapper = parents.closest(".ultp-block-wrapper");
372
373 const postBlock = parents.parents(".ultp-post-grid-parent");
374
375 // Pagination block integration
376 if (wrap.length < 1) {
377 const pagiFor = parents.data("for");
378 if (pagiFor) {
379 wrap = $("." + pagiFor + " .ultp-block-items-wrap");
380 }
381 }
382
383 if (parents.is(".ultp-disable-editor-click")) {
384 return;
385 }
386 if ($(this).hasClass("ultp-prev-action")) {
387 if ($(this).hasClass("ultp-disable")) {
388 return;
389 } else {
390 paged--;
391 parents.data("pagenum", paged);
392 parents
393 .find(".ultp-prev-action, .ultp-next-action")
394 .removeClass("ultp-disable");
395 if (paged == 1) {
396 $(this).addClass("ultp-disable");
397 }
398 }
399 }
400 if ($(this).hasClass("ultp-next-action")) {
401 if ($(this).hasClass("ultp-disable")) {
402 return;
403 } else {
404 paged++;
405 parents.data("pagenum", paged);
406 parents
407 .find(".ultp-prev-action, .ultp-next-action")
408 .removeClass("ultp-disable");
409 if (paged == pages) {
410 $(this).addClass("ultp-disable");
411 }
412 }
413 }
414
415 let post_ID =
416 parents.parents(".ultp-shortcode").length != 0 &&
417 parents.data("selfpostid") == "no"
418 ? parents.parents(".ultp-shortcode").data("postid")
419 : parents.data("postid");
420
421 if ($(this).closest(".ultp-builder-content").length > 0) {
422 post_ID = $(this).closest(".ultp-builder-content").data("postid");
423 }
424 let widgetBlockId = "";
425 let widgetBlock = $(this).parents(".widget_block:first");
426 if (widgetBlock.length > 0) {
427 let widget_items = widgetBlock.attr("id").split("-");
428 widgetBlockId = widget_items[widget_items.length - 1];
429 }
430 const ultpUniqueIds = sessionStorage.getItem("ultp_uniqueIds");
431 const ultpCurrentUniquePosts = JSON.stringify(
432 wrap.find(".ultp-current-unique-posts").data("current-unique-posts"),
433 );
434
435 // Adv Filter Integration
436 const filterValue = parents.data("filter-value") || "";
437 const advFilterData = {};
438
439 if (Array.isArray(filterValue) && filterValue.length > 0) {
440 advFilterData.filterShow = true;
441 advFilterData.checkFilter = true;
442 advFilterData.isAdv = true;
443 advFilterData.author = parents.data("filter-author") || "";
444 advFilterData.order = parents.data("filter-order") || "";
445 advFilterData.orderby = parents.data("filter-orderby") || "";
446 advFilterData.adv_sort = parents.data("filter-adv-sort") || "";
447 }
448
449 ultpAjax({
450 url: ultp_data_frontend.ajax,
451 type: "POST",
452 nonce: ultp_data_frontend.security,
453 data: {
454 action: "ultp_next_prev",
455 paged: paged,
456 blockId: parents.data("blockid"),
457 postId: post_ID,
458 exclude: parents.data("expost"),
459 blockName: parents.data("blockname"),
460 builder: parents.data("builder"),
461 filterValue: filterValue,
462 filterType: parents.data("filter-type") || "",
463 widgetBlockId: widgetBlockId,
464 ultpUniqueIds: ultpUniqueIds || [],
465 ultpCurrentUniquePosts: ultpCurrentUniquePosts || [],
466
467 ...advFilterData,
468 },
469 beforeSend: function () {
470 if (blockWrapper.length < 1 && postBlock.length > 0) {
471 postBlock.find(".ultp-block-wrapper").addClass("ultp-loading-active");
472 } else {
473 blockWrapper.addClass("ultp-loading-active");
474 }
475 },
476 success: function (data) {
477 if (data) {
478 wrap.html(data);
479 setSession(
480 "ultp_uniqueIds",
481 JSON.stringify(
482 wrap.find(".ultp-current-unique-posts").data("ultp-unique-ids"),
483 ),
484 );
485 if ($(window).scrollTop() > wrap.offset().top) {
486 $([document.documentElement, document.body]).animate(
487 {
488 scrollTop: wrap.offset().top - 80,
489 },
490 100,
491 );
492 }
493 }
494 },
495 complete: function () {
496 if (blockWrapper.length < 1 && postBlock.length > 0) {
497 postBlock
498 .find(".ultp-block-wrapper")
499 .removeClass("ultp-loading-active");
500 } else {
501 blockWrapper.removeClass("ultp-loading-active");
502 }
503 handleDailyMotion();
504 },
505 error: function (xhr) {
506 console.log(
507 "Error occured.please try again" + xhr.statusText + xhr.responseText,
508 );
509 parents
510 .closest(".ultp-block-wrapper")
511 .removeClass("ultp-loading-active");
512 },
513 });
514 });
515
516 // *************************************
517 // Loadmore Append
518 // *************************************
519
520 $(document).on("click", ".ultp-loadmore-action", function (e) {
521 if ($(this).is(".ultp-disable-editor-click")) {
522 return;
523 }
524 e.preventDefault();
525 let that = $(this);
526 let parents = that.closest(".ultp-block-wrapper");
527 let isPagiBlock = false;
528
529 // Pagination block integration
530 if (parents.length < 1) {
531 const pagiFor = that.data("for");
532 if (pagiFor) {
533 parents = $("." + pagiFor + " .ultp-block-wrapper");
534 isPagiBlock = parents.length > 0;
535 }
536 }
537
538 let paged = parseInt(that.data("pagenum"));
539 let pages = parseInt(that.data("pages"));
540
541 if (that.hasClass("ultp-disable")) {
542 return;
543 } else {
544 paged++;
545 that.data("pagenum", paged);
546 if (paged == pages) {
547 $(this).addClass("ultp-disable");
548 } else {
549 $(this).removeClass("ultp-disable");
550 }
551 }
552
553 let post_ID =
554 that.parents(".ultp-shortcode").length != 0 &&
555 that.data("selfpostid") == "no"
556 ? that.parents(".ultp-shortcode").data("postid")
557 : that.data("postid");
558
559 if (that.closest(".ultp-builder-content").length > 0) {
560 post_ID = that.closest(".ultp-builder-content").data("postid");
561 }
562 let widgetBlockId = "";
563 let widgetBlock = $(this).parents(".widget_block:first");
564 if (widgetBlock.length > 0) {
565 let widget_items = widgetBlock.attr("id").split("-");
566 widgetBlockId = widget_items[widget_items.length - 1];
567 }
568
569 const ultpUniqueIds = sessionStorage.getItem("ultp_uniqueIds");
570 const ultpCurrentUniquePosts = JSON.stringify(
571 parents.find(".ultp-current-unique-posts").data("current-unique-posts"),
572 );
573
574 // Adv Filter Integration
575 const filterValue = that.data("filter-value") || "";
576 const advFilterData = {};
577
578 if (Array.isArray(filterValue) && filterValue.length > 0) {
579 advFilterData.filterShow = true;
580 advFilterData.checkFilter = true;
581 advFilterData.isAdv = true;
582 advFilterData.author = that.data("filter-author") || "";
583 advFilterData.order = that.data("filter-order") || "";
584 advFilterData.orderby = that.data("filter-orderby") || "";
585 advFilterData.adv_sort = that.data("filter-adv-sort") || "";
586 }
587
588 ultpAjax({
589 url: ultp_data_frontend.ajax,
590 type: "POST",
591 nonce: ultp_data_frontend.security,
592 data: {
593 action: "ultp_next_prev",
594 paged: paged,
595 blockId: that.data("blockid"),
596 postId: post_ID,
597 blockName: that.data("blockname"),
598 builder: that.data("builder"),
599 exclude: that.data("expost"),
600 filterValue: filterValue,
601 filterType: that.data("filter-type") || "",
602 widgetBlockId: widgetBlockId,
603 ultpUniqueIds: ultpUniqueIds || [],
604 ultpCurrentUniquePosts: ultpCurrentUniquePosts || [],
605
606 ...advFilterData,
607 },
608 beforeSend: function () {
609 parents.addClass("ultp-loading-active");
610
611 if (isPagiBlock) {
612 that.find(".ultp-spin").css("display", "flex");
613 }
614 },
615
616 success: function (data) {
617 if (data) {
618 // Fix for grids that have fixed max-height (Adv Pagination Block integration)
619 parents.find(".ultp-block-row").css("max-height", "unset");
620
621 parents.find(".ultp-current-unique-posts").remove();
622
623 const insertPoint = parents.find(".ultp-loadmore-insert-before");
624
625 if (insertPoint.length) {
626 // Adv Pagination block loadmore fix for post modules
627 if (that.data("blockname").includes("post-module")) {
628 $(
629 '<div style="clear:left;width:100%;padding-block:15px;"></div>',
630 ).insertBefore(insertPoint);
631 }
632
633 $(data).insertBefore(insertPoint);
634 } else {
635 const cardDiv = parents.find(".ultp-block-items-wrap");
636
637 if (that.data("blockname").includes("post-module")) {
638 cardDiv.append(
639 $(
640 '<div style="clear:left;width:100%;padding-block:15px;"></div>',
641 ),
642 );
643 }
644
645 cardDiv.append(data);
646 }
647
648 setSession(
649 "ultp_uniqueIds",
650 JSON.stringify(
651 parents
652 .find(".ultp-current-unique-posts")
653 .data("ultp-unique-ids"),
654 ),
655 );
656 }
657 },
658
659 complete: function () {
660 parents.removeClass("ultp-loading-active");
661
662 if (isPagiBlock) {
663 that.find(".ultp-spin").css("display", "none");
664 }
665 },
666
667 error: function (xhr) {
668 console.log(
669 "Error occured.please try again" + xhr.statusText + xhr.responseText,
670 );
671 parents.removeClass("ultp-loading-active");
672
673 if (isPagiBlock) {
674 that.find(".ultp-spin").css("display", "none");
675 }
676 },
677 });
678 });
679
680 // *************************************
681 // Filter
682 // *************************************
683 $(document).on("click", ".ultp-filter-wrap li a", async function (e) {
684 e.preventDefault();
685 const ultpNonce = await getUltpNonce();
686 if ($(this).closest("li").hasClass("filter-item")) {
687 let that = $(this),
688 parents = that.closest(".ultp-filter-wrap"),
689 wrap = that.closest(".ultp-block-wrapper");
690 const postBlock = that.parents(".ultp-post-grid-parent");
691
692 parents.find("a").removeClass("filter-active");
693 that.addClass("filter-active");
694 if (parents.is(".ultp-disable-editor-click")) {
695 return;
696 }
697 let post_ID =
698 parents.parents(".ultp-shortcode").length != 0 &&
699 parents.data("selfpostid") == "no"
700 ? parents.parents(".ultp-shortcode").data("postid")
701 : parents.data("postid");
702
703 if (that.closest(".ultp-builder-content").length > 0) {
704 post_ID = that.closest(".ultp-builder-content").data("postid");
705 }
706 let widgetBlockId = "";
707 let widgetBlock = $(this).parents(".widget_block:first");
708 if (widgetBlock.length > 0) {
709 let widget_items = widgetBlock.attr("id").split("-");
710 widgetBlockId = widget_items[widget_items.length - 1];
711 }
712
713 const ultpUniqueIds = sessionStorage.getItem("ultp_uniqueIds");
714 const ultpCurrentUniquePosts = JSON.stringify(
715 wrap.find(".ultp-current-unique-posts").data("current-unique-posts"),
716 );
717
718 if (parents.data("blockid")) {
719 ultpAjax({
720 url: ultp_data_frontend.ajax,
721 type: "POST",
722 nonce: ultpNonce,
723 data: {
724 action: "ultp_filter",
725 taxtype: parents.data("taxtype"),
726 taxonomy: that.data("taxonomy"),
727 blockId: parents.data("blockid"),
728 postId: post_ID,
729 blockName: parents.data("blockname"),
730 widgetBlockId: widgetBlockId,
731 ultpUniqueIds: ultpUniqueIds || [],
732 ultpCurrentUniquePosts: ultpCurrentUniquePosts || [],
733 },
734 beforeSend: function () {
735 wrap.addClass("ultp-loading-active");
736 },
737 success: function (response) {
738 wrap
739 .find(".ultp-block-items-wrap")
740 .html(response?.data?.filteredData?.blocks);
741 if (
742 response?.data?.filteredData?.paginationType == "loadMore" &&
743 response?.data?.filteredData?.paginationShow
744 ) {
745 // wrap.find('.ultp-block-items-wrap').append('<span class="ultp-loadmore-insert-before"></span>');
746
747 wrap
748 .find(".ultp-loadmore")
749 .replaceWith(response?.data?.filteredData?.pagination);
750 } else if (
751 response?.data?.filteredData?.paginationType == "navigation"
752 ) {
753 wrap
754 .find(".ultp-next-prev-wrap")
755 .replaceWith(response?.data?.filteredData?.pagination);
756 } else if (
757 response?.data?.filteredData?.paginationType == "pagination"
758 ) {
759 wrap
760 .find(".ultp-pagination-wrap")
761 .replaceWith(response?.data?.filteredData?.pagination);
762 }
763
764 // Adv Pagination Block Integration
765 if (
766 response?.data?.filteredData?.pagination &&
767 postBlock.length > 0
768 ) {
769 postBlock.data("pagi")?.map((pagiClass) => {
770 let pagi = [];
771
772 if (
773 response?.data?.filteredData?.paginationType === "loadMore"
774 ) {
775 pagi = $(".ultp-loadmore." + pagiClass);
776 if (pagi.length > 0) {
777 // wrap.find('.ultp-block-items-wrap').append('<span class="ultp-loadmore-insert-before"></span>');
778 }
779 } else {
780 pagi = $("." + pagiClass + "[data-for]");
781 }
782
783 if (pagi.length > 0) {
784 const newPagi = $(response.data.filteredData.pagination);
785 pagi
786 .attr("class")
787 .split(" ")
788 .forEach((cl) => newPagi.addClass(cl));
789 pagi.replaceWith(newPagi);
790 }
791 });
792 }
793 },
794 complete: function () {
795 wrap.removeClass("ultp-loading-active");
796 setSession(
797 "ultp_uniqueIds",
798 JSON.stringify(
799 wrap.find(".ultp-current-unique-posts").data("ultp-unique-ids"),
800 ),
801 );
802 handleDailyMotion();
803 },
804 error: function (xhr) {
805 console.log(
806 "Error occured.please try again" +
807 xhr.statusText +
808 xhr.responseText,
809 );
810 wrap.removeClass("ultp-loading-active");
811 },
812 });
813 }
814 }
815 });
816
817 // *************************************
818 // Pagination Number
819 // *************************************
820 function showHide(parents, pageNum, pages) {
821 if (pageNum == pages) {
822 parents.find(".ultp-next-page-numbers").hide();
823 } else {
824 parents.find(".ultp-next-page-numbers").show();
825 }
826
827 if (pageNum > 1) {
828 parents.find(".ultp-prev-page-numbers").show();
829 } else {
830 parents.find(".ultp-prev-page-numbers").hide();
831 }
832
833 if (pageNum > 3) {
834 parents.find(".ultp-first-dot").show();
835 } else {
836 parents.find(".ultp-first-dot").hide();
837 }
838
839 if (pageNum > 2) {
840 parents.find(".ultp-first-pages").show();
841 } else {
842 parents.find(".ultp-first-pages").hide();
843 }
844
845 if (pages > pageNum + 2) {
846 parents.find(".ultp-last-dot").show();
847 } else {
848 parents.find(".ultp-last-dot").hide();
849 }
850
851 if (pages > pageNum + 1) {
852 parents.find(".ultp-last-pages").show();
853 } else {
854 parents.find(".ultp-last-pages").hide();
855 }
856 }
857
858 function serial(parents, pageNum, pages) {
859 let datas =
860 pageNum <= 2
861 ? [1, 2, 3]
862 : pages == pageNum
863 ? [pages - 2, pages - 1, pages]
864 : [pageNum - 1, pageNum, pageNum + 1];
865 let i = 0;
866 parents.find(".ultp-center-item").each(function () {
867 if (pageNum == datas[i]) {
868 $(this).addClass("pagination-active");
869 }
870 $(this).find("a").blur();
871 $(this).attr("data-current", datas[i]).find("a").text(datas[i]);
872 i++;
873 });
874 parents.find(".ultp-prev-page-numbers a").blur();
875 parents.find(".ultp-next-page-numbers a").blur();
876 }
877
878 // set session value for unique content on page reload
879 if ($(".ultp-current-unique-posts").length > 0) {
880 $(".ultp-current-unique-posts").each(function () {
881 setSession(
882 "ultp_uniqueIds",
883 JSON.stringify($(this).data("ultp-unique-ids")),
884 );
885 });
886 }
887 // session value set function
888 function setSession(key, value) {
889 if (value != undefined) {
890 sessionStorage.setItem(key, value);
891 }
892 }
893
894 function getCurrPage(blockId) {
895 const params = new URLSearchParams(window.location.search);
896 const page = params.get(blockId + "_page");
897 return page ? +page : 1;
898 }
899
900 function setCurrPage(blockId, currPage, prevPage) {
901 const params = new URLSearchParams(window.location.search);
902 params.set(`${blockId}_page`, currPage);
903 const url = window.location.pathname + "?" + params.toString();
904
905 window.history.replaceState(
906 {
907 page: {
908 [blockId]: prevPage,
909 },
910 },
911 document.title,
912 url,
913 );
914 }
915
916 // *************************************
917 // Common Pagination AJAX Dispatcher
918 // *************************************
919 function paginationAjaxCall({
920 wrap, // .ultp-block-wrapper Selector
921 builder, // Get Builder Data
922 post_ID,
923 exclude,
924 pageNum,
925 blockid,
926 ultpNonce, // Get Nonce
927 blockName,
928 filterType,
929 widgetBlockId,
930 ultpCurrentUniquePosts,
931 filterValue,
932 advFilterData = {},
933 onSuccess,
934 onBeforeSend,
935 }) {
936 if (!pageNum) return;
937
938 const ultpUniqueIds = sessionStorage.getItem("ultp_uniqueIds");
939
940 // console.log({
941 // exclude,
942 // action: "ultp_pagination",
943 // paged: pageNum,
944 // blockId: blockid,
945 // postId: post_ID,
946 // blockName,
947 // builder,
948 // widgetBlockId,
949 // ultpUniqueIds: ultpUniqueIds || [],
950 // ultpCurrentUniquePosts: ultpCurrentUniquePosts || [],
951 // filterType: filterType || "",
952 // wpnonce: ultpNonce,
953 // filterValue,
954 // ...advFilterData,
955 // });
956
957 ultpAjax({
958 url: ultp_data_frontend.ajax,
959 type: "POST",
960 nonce: ultpNonce,
961 data: {
962 exclude,
963 action: "ultp_pagination",
964 paged: pageNum,
965 blockId: blockid,
966 postId: post_ID,
967 blockName,
968 builder,
969 widgetBlockId,
970 ultpUniqueIds: ultpUniqueIds || [],
971 ultpCurrentUniquePosts: ultpCurrentUniquePosts || [],
972 filterType: filterType || "",
973 filterValue,
974 ...advFilterData,
975 },
976 beforeSend() {
977 if (typeof onBeforeSend === "function") {
978 onBeforeSend();
979 } else {
980 wrap.addClass("ultp-loading-active");
981 }
982 },
983 success(data) {
984 if (typeof onSuccess === "function") onSuccess(data);
985 },
986 complete() {
987 wrap.removeClass("ultp-loading-active");
988 handleDailyMotion();
989 },
990 error(xhr) {
991 console.log(
992 "Error occured. please try again " +
993 xhr.statusText +
994 xhr.responseText,
995 );
996 wrap.removeClass("ultp-loading-active");
997 },
998 });
999 }
1000
1001 $(document).on(
1002 "click",
1003 ".ultp-pagination-ajax-action li",
1004 async function (e) {
1005 e.preventDefault();
1006 const ultpNonce = await getUltpNonce();
1007
1008 let that = $(this),
1009 parents = that.closest(".ultp-pagination-ajax-action"),
1010 wrap = that.closest(".ultp-block-wrapper");
1011
1012 if (parents.is(".ultp-disable-editor-click")) {
1013 return;
1014 }
1015
1016 let pageNum = 1;
1017 let pages = parents.attr("data-pages");
1018
1019 const exclude = parents.data("expost");
1020 const builder = parents.data("builder");
1021 const blockName = parents.data("blockname");
1022 const blockid = parents.attr("data-blockid");
1023 const filterType = parents.data("filter-type") || "";
1024 const filterValue = parents.data("filter-value") || "";
1025
1026 // Pagination block integration
1027 if (wrap.length < 1) {
1028 const pagiFor = parents.data("for");
1029 if (pagiFor) {
1030 wrap = $("." + pagiFor + " .ultp-block-wrapper");
1031 }
1032 }
1033
1034 if (that.attr("data-current")) {
1035 pageNum = Number(that.attr("data-current"));
1036 parents
1037 .attr("data-paged", pageNum)
1038 .find("li")
1039 .removeClass("pagination-active");
1040 serial(parents, pageNum, pages);
1041 showHide(parents, pageNum, pages);
1042 } else {
1043 if (that.hasClass("ultp-prev-page-numbers")) {
1044 pageNum = Number(parents.attr("data-paged")) - 1;
1045 parents
1046 .attr("data-paged", pageNum)
1047 .find("li")
1048 .removeClass("pagination-active");
1049 //parents.find('li[data-current="'+pageNum+'"]').addClass('pagination-active')
1050 serial(parents, pageNum, pages);
1051 showHide(parents, pageNum, pages);
1052 } else if (that.hasClass("ultp-next-page-numbers")) {
1053 pageNum = Number(parents.attr("data-paged")) + 1;
1054 parents
1055 .attr("data-paged", pageNum)
1056 .find("li")
1057 .removeClass("pagination-active");
1058 //parents.find('li[data-current="'+pageNum+'"]').addClass('pagination-active')
1059 serial(parents, pageNum, pages);
1060 showHide(parents, pageNum, pages);
1061 }
1062 }
1063
1064 let widgetBlockId = "";
1065 let widgetBlock = that.parents(".widget_block:first");
1066 if (widgetBlock.length > 0) {
1067 let widget_items = widgetBlock.attr("id").split("-");
1068 widgetBlockId = widget_items[widget_items.length - 1];
1069 }
1070
1071 const ultpCurrentUniquePosts = JSON.stringify(
1072 wrap.find(".ultp-current-unique-posts").data("current-unique-posts"),
1073 );
1074
1075 let post_ID =
1076 parents.parents(".ultp-shortcode").length != 0 &&
1077 parents.data("selfpostid") == "no"
1078 ? parents.parents(".ultp-shortcode").data("postid")
1079 : parents.data("postid");
1080
1081 if (that.closest(".ultp-builder-content").length > 0) {
1082 post_ID = that.closest(".ultp-builder-content").data("postid");
1083 }
1084
1085 // Build adv filter data from pagination element
1086 const advFilterData = {};
1087 if (Array.isArray(filterValue) && filterValue.length > 0) {
1088 advFilterData.filterShow = true;
1089 advFilterData.checkFilter = true;
1090 advFilterData.isAdv = true;
1091 advFilterData.author = parents.data("filter-author") || "";
1092 advFilterData.order = parents.data("filter-order") || "";
1093 advFilterData.orderby = parents.data("filter-orderby") || "";
1094 advFilterData.adv_sort = parents.data("filter-adv-sort") || "";
1095 }
1096
1097 if (blockid) {
1098 setCurrPage(blockid, pageNum, getCurrPage(blockid));
1099 }
1100
1101 // console.log(advFilterData, "advFilterData");
1102 paginationAjaxCall({
1103 wrap,
1104 pageNum,
1105 blockid,
1106 ultpNonce,
1107 blockName,
1108 post_ID,
1109 filterType,
1110 ultpCurrentUniquePosts,
1111 widgetBlockId,
1112 exclude,
1113 builder,
1114 filterValue,
1115 advFilterData,
1116 onSuccess(data) {
1117 wrap.find(".ultp-block-items-wrap").html(data);
1118 setSession(
1119 "ultp_uniqueIds",
1120 JSON.stringify(
1121 wrap.find(".ultp-current-unique-posts").data("ultp-unique-ids"),
1122 ),
1123 );
1124 if ($(window).scrollTop() > wrap.offset().top) {
1125 $([document.documentElement, document.body]).animate(
1126 { scrollTop: wrap.offset().top - 80 },
1127 100,
1128 );
1129 }
1130 },
1131 });
1132 },
1133 );
1134
1135 // *************************************
1136 // SlideShow
1137 // *************************************
1138
1139 // Slideshow Display For Elementor via Shortcode
1140 $(window).on("elementor/frontend/init", () => {
1141 setTimeout(() => {
1142 if ($(".elementor-editor-active").length > 0) {
1143 slideshowDisplay();
1144 }
1145 }, 2000);
1146 });
1147
1148 // Bricks Builder Backend Slider Support
1149 // Bricks Builder Backend Slider Support
1150 if (
1151 $(".bricks-builder-iframe").length > 0 &&
1152 $(window.parent.document).find(".bricks-panel-controls").length > 0
1153 ) {
1154 setTimeout(() => {
1155 slideshowDisplay();
1156 }, 2500);
1157 }
1158
1159 function slideshowDisplay() {
1160 $(
1161 ".wp-block-ultimate-post-post-slider-1, .wp-block-ultimate-post-post-slider-2",
1162 ).each(function () {
1163 const sectionId = "#" + $(this).attr("id");
1164 let selector = $(sectionId).find(".ultp-block-items-wrap");
1165 if ($(this).parent(".ultp-shortcode")) {
1166 selector = $(this).find(".ultp-block-items-wrap");
1167 }
1168 let settings = {
1169 arrows: true,
1170 dots: selector.data("dots") ? true : false,
1171 infinite: true,
1172 speed: 500,
1173 slidesToShow: selector.data("slidelg") || 1,
1174 slidesToScroll: 1,
1175 autoplay: selector.data("autoplay") ? true : false,
1176 autoplaySpeed: selector.data("slidespeed") || 3000,
1177 cssEase: "linear",
1178 prevArrow: selector.parent().find(".ultp-slick-prev").html(),
1179 nextArrow: selector.parent().find(".ultp-slick-next").html(),
1180 };
1181
1182 let layTemp =
1183 selector.data("layout") == "slide2" ||
1184 selector.data("layout") == "slide3" ||
1185 selector.data("layout") == "slide5" ||
1186 selector.data("layout") == "slide6" ||
1187 selector.data("layout") == "slide8";
1188
1189 if (!selector.data("layout")) {
1190 // Slider 1
1191 if (selector.data("slidelg") < 2) {
1192 settings.fade = selector.data("fade") ? true : false;
1193 } else {
1194 settings.responsive = [
1195 {
1196 breakpoint: 1024,
1197 settings: {
1198 slidesToShow: selector.data("slidesm") || 1,
1199 slidesToScroll: 1,
1200 },
1201 },
1202 {
1203 breakpoint: 600,
1204 settings: {
1205 slidesToShow: selector.data("slidexs") || 1,
1206 slidesToScroll: 1,
1207 },
1208 },
1209 ];
1210 }
1211 } else {
1212 // Slider 2
1213 if (selector.data("fade") && layTemp) {
1214 settings.fade = selector.data("fade") ? true : false;
1215 } else if (!selector.data("fade") && layTemp) {
1216 ((settings.slidesToShow = selector.data("slidelg") || 1),
1217 (settings.responsive = [
1218 {
1219 breakpoint: 991,
1220 settings: {
1221 slidesToShow: selector.data("slidesm") || 1,
1222 slidesToScroll: 1,
1223 },
1224 },
1225 {
1226 breakpoint: 767,
1227 settings: {
1228 slidesToShow: selector.data("slidexs") || 1,
1229 slidesToScroll: 1,
1230 },
1231 },
1232 ]));
1233 } else {
1234 ((settings.slidesToShow = selector.data("slidelg") || 1),
1235 (settings.centerMode = true));
1236 settings.centerPadding = `${selector.data("paddlg")}px` || 100;
1237 settings.responsive = [
1238 {
1239 breakpoint: 991,
1240 settings: {
1241 slidesToShow: selector.data("slidesm") || 1,
1242 slidesToScroll: 1,
1243 centerPadding: `${selector.data("paddsm")}px` || 50,
1244 },
1245 },
1246 {
1247 breakpoint: 767,
1248 settings: {
1249 slidesToShow: selector.data("slidexs") || 1,
1250 slidesToScroll: 1,
1251 centerPadding: `${selector.data("paddxs")}px` || 50,
1252 },
1253 },
1254 ];
1255 }
1256 }
1257 selector.not(".slick-initialized").slick(settings);
1258 });
1259 }
1260 slideshowDisplay();
1261
1262 // *************************************
1263 // Accessibility for Loadmore Added
1264 // *************************************
1265 $('span[role="button"].ultp-loadmore-action').on("keydown", function (e) {
1266 const keyD = e.key !== undefined ? e.key : e.keyCode;
1267 if (
1268 keyD === "Enter" ||
1269 keyD === 13 ||
1270 ["Spacebar", " "].indexOf(keyD) >= 0 ||
1271 keyD === 32
1272 ) {
1273 e.preventDefault();
1274 this.click();
1275 }
1276 });
1277 // *************************************
1278 // Video Scroll
1279 // *************************************
1280 let isSticky = true;
1281 $(window).on("scroll", function () {
1282 let windowHeight = $(this).scrollTop();
1283 $(".wp-block-ultimate-post-post-image").each(function () {
1284 let contentSelector = $(this).find(
1285 ".ultp-builder-video video , .ultp-builder-video iframe",
1286 );
1287 if ($(this).find(".ultp-video-block").hasClass("ultp-sticky-video")) {
1288 // block height and position
1289 let blockContent = $(this).find(".ultp-image-wrapper");
1290 let blockPosition = blockContent.offset();
1291 // Video Html height and position
1292 let videoContent = contentSelector.height();
1293 let videoPosition = contentSelector.offset();
1294 // Exclude Adminbar height
1295 let windowTotalHeight = windowHeight + ($("#wpadminbar").height() || 0);
1296 let totalHeight = videoPosition.top + videoContent;
1297 // Scrolling Top to bottom
1298 if (windowTotalHeight > videoPosition.top) {
1299 if (windowTotalHeight > totalHeight && isSticky) {
1300 $(this)
1301 .find(".ultp-image-wrapper")
1302 .css("height", blockContent.height());
1303 $(this).find(".ultp-sticky-video").addClass("ultp-sticky-active");
1304 }
1305 }
1306 // Scrolling bottom to top
1307 if (windowTotalHeight < blockContent.height() + blockPosition.top) {
1308 $(this).find(".ultp-sticky-video").removeClass("ultp-sticky-active");
1309 $(this).find(".ultp-image-wrapper").css("height", "auto");
1310 }
1311 // Close Button
1312 $(".ultp-sticky-close").on("click", function () {
1313 $(this).find(".ultp-image-wrapper").css("height", "auto");
1314 $(".ultp-sticky-video").removeClass("ultp-sticky-active");
1315 isSticky = false;
1316 });
1317 }
1318 });
1319 });
1320
1321 // *************************************
1322 // Advanced Filter
1323 // *************************************
1324 function getTax(name) {
1325 switch (name) {
1326 case "categories":
1327 return "category";
1328 case "tag":
1329 case "tags":
1330 return "post_tag";
1331 case "authors":
1332 return "author";
1333 case "order_by":
1334 return "orderby";
1335 default:
1336 return name;
1337 }
1338 }
1339
1340 function getFormattedSelectedFilter(type, name) {
1341 const fType = type
1342 .replace("_", " ")
1343 .replace(/\b\w/g, (l) => l.toUpperCase());
1344 return `${fType}: ${name}`;
1345 }
1346
1347 function applyFilter(parent, blockId, blockName, postId, grids, builder) {
1348 const selectedFilters = [];
1349 const filterData = {};
1350 const pagis = parent.data("pagi");
1351
1352 parent.find(".ultp-filter-select").each(function () {
1353 const type = $(this).attr("data-type");
1354 const selected = $(this).attr("data-selected");
1355 const cTax = $(this)
1356 .find('.ultp-filter-select__dropdown-inner[data-id="' + selected + '"]')
1357 .data("tax");
1358 if (getTax(type) === "author") {
1359 if (selected !== "_all") {
1360 filterData.author = [{ value: selected }];
1361 }
1362 return;
1363 }
1364 if (getTax(type) === "order") {
1365 filterData.order = selected;
1366 return;
1367 }
1368 if (getTax(type) === "orderby") {
1369 filterData.orderby = selected;
1370 return;
1371 }
1372 if (getTax(type) === "adv_sort") {
1373 filterData.adv_sort = selected;
1374 return;
1375 }
1376 if (getTax(type) === "custom_tax") {
1377 if (cTax) {
1378 selectedFilters.push({
1379 value: cTax + "###" + selected,
1380 });
1381 }
1382 return;
1383 }
1384 selectedFilters.push({
1385 value: getTax(type) + "###" + selected,
1386 });
1387 });
1388
1389 parent.find('.ultp-filter-button[data-is-active="true"]').each(function () {
1390 const type = $(this).attr("data-type");
1391 const selected = $(this).attr("data-selected");
1392 const cTax = $(this).attr("data-tax");
1393
1394 if (getTax(type) === "author") {
1395 if (selected !== "_all") {
1396 filterData.author = [{ value: selected }];
1397 }
1398 return;
1399 }
1400
1401 if (getTax(type) === "order") {
1402 filterData.order = selected;
1403 return;
1404 }
1405
1406 if (getTax(type) === "orderby") {
1407 filterData.orderby = selected;
1408 return;
1409 }
1410
1411 if (getTax(type) === "adv_sort") {
1412 filterData.adv_sort = selected;
1413 return;
1414 }
1415
1416 if (getTax(type) === "custom_tax") {
1417 if (cTax) {
1418 selectedFilters.push({
1419 value: cTax + "###" + selected,
1420 });
1421 }
1422 return;
1423 }
1424 selectedFilters.push({
1425 value: getTax(type) + "###" + selected,
1426 });
1427 });
1428
1429 filterData.taxonomy = selectedFilters;
1430
1431 const searchVal = parent.find(".ultp-filter-search input");
1432
1433 if (searchVal.length > 0) {
1434 filterData.search = searchVal.val();
1435 }
1436
1437 const ultpUniqueIds = sessionStorage.getItem("ultp_uniqueIds");
1438 const ultpCurrentUniquePosts = JSON.stringify(
1439 parent.find(".ultp-current-unique-posts").data("current-unique-posts"),
1440 );
1441
1442 let widgetBlockId = "";
1443 let widgetBlock = parent.parents(".widget_block:first");
1444 if (widgetBlock.length > 0) {
1445 let widget_items = widgetBlock.attr("id").split("-");
1446 widgetBlockId = widget_items[widget_items.length - 1];
1447 }
1448
1449 $(grids).each(function () {
1450 const grid = $(this);
1451
1452 ultpAjax({
1453 url: ultp_data_frontend.ajax,
1454 type: "POST",
1455 nonce: ultp_data_frontend.security,
1456 data: {
1457 action: "ultp_adv_filter",
1458
1459 ...filterData,
1460 builder_id: builder,
1461 blockId: blockId,
1462 blockName: blockName,
1463 postId: postId,
1464 ultpUniqueIds: ultpUniqueIds || [],
1465 ultpCurrentUniquePosts: ultpCurrentUniquePosts || [],
1466 widgetBlockId: widgetBlockId,
1467 },
1468 beforeSend: function () {
1469 grid.addClass("ultp-loading-active");
1470 },
1471 success: function (response) {
1472 // console.log(
1473 // JSON.parse(response.data.filteredData.data_attrs),
1474 // "response",
1475 // );
1476 console.log("testing", searchVal);
1477 grid
1478 .closest(".wp-block-ultimate-post-post-grid-parent")
1479 ?.find(".ultp-not-found-message")
1480 ?.remove();
1481 if (
1482 response?.data?.filteredData?.blocks === "" &&
1483 response?.data?.filteredData?.notFound
1484 ) {
1485 grid
1486 .closest(".wp-block-ultimate-post-post-grid-parent")
1487 .append(
1488 '<div class="ultp-not-found-message" role="alert">' +
1489 response?.data?.filteredData?.notFound +
1490 "</div>",
1491 );
1492 }
1493 grid
1494 .find(".ultp-block-items-wrap")
1495 .html(response?.data?.filteredData?.blocks);
1496 if (
1497 response?.data?.filteredData?.paginationType == "loadMore" &&
1498 response?.data?.filteredData?.paginationShow
1499 ) {
1500 // grid.find(".ultp-block-items-wrap")
1501 // .append('<span class="ultp-loadmore-insert-before"></span>');
1502 grid
1503 .find(".ultp-loadmore")
1504 .replaceWith(response?.data?.filteredData?.pagination);
1505 } else if (
1506 response?.data?.filteredData?.paginationType == "navigation"
1507 ) {
1508 grid
1509 .find(".ultp-next-prev-wrap")
1510 .replaceWith(response?.data?.filteredData?.pagination);
1511 } else if (
1512 response?.data?.filteredData?.paginationType == "pagination"
1513 ) {
1514 grid
1515 .find(".ultp-pagination-wrap")
1516 .replaceWith(response?.data?.filteredData?.pagination);
1517 }
1518
1519 if (grid.parent().is(".ultp-iscroll-active")) {
1520 const dataAttrs = JSON.parse(response.data.filteredData.data_attrs);
1521
1522 Object.entries(dataAttrs).forEach(([key, value]) => {
1523 if (value !== null && value !== undefined) {
1524 grid.attr("data-" + key, value);
1525 }
1526 });
1527
1528 const iscrollBlock = grid.parent();
1529 iscrollBlock.UltpInfiniteScroll(
1530 iscrollBlock,
1531 setSession,
1532 getUltpNonce,
1533 paginationAjaxCall,
1534 );
1535 }
1536
1537 if (response?.data?.filteredData?.pagination) {
1538 pagis?.map((pagiClass) => {
1539 let pagi = [];
1540
1541 if (response?.data?.filteredData?.paginationType === "loadMore") {
1542 pagi = $(".ultp-loadmore." + pagiClass);
1543 if (pagi.length > 0) {
1544 // grid.find(".ultp-block-items-wrap")
1545 // .append('<span class="ultp-loadmore-insert-before"></span>');
1546 }
1547 } else {
1548 pagi = $("." + pagiClass + "[data-for]");
1549 }
1550
1551 if (pagi.length > 0) {
1552 const newPagi = $(response.data.filteredData.pagination);
1553 pagi
1554 .attr("class")
1555 .split(" ")
1556 .forEach((cl) => newPagi.addClass(cl));
1557 pagi.replaceWith(newPagi);
1558 }
1559 });
1560 }
1561 },
1562 complete: function () {
1563 grid.removeClass("ultp-loading-active");
1564 setSession(
1565 "ultp_uniqueIds",
1566 JSON.stringify(
1567 parent.find(".ultp-current-unique-posts").data("ultp-unique-ids"),
1568 ),
1569 );
1570 },
1571 error: function (xhr) {
1572 console.log(
1573 "Error occured.please try again" +
1574 xhr.statusText +
1575 xhr.responseText,
1576 );
1577 grid.removeClass("ultp-loading-active");
1578 },
1579 });
1580 });
1581 }
1582
1583 $(".ultp-filter-block").each(function () {
1584 const that = $(this);
1585 const parent = $(this).parents(".ultp-post-grid-parent");
1586 const grids = parent.find(".ultp-block-wrapper");
1587 const gridData = JSON.parse(parent.attr("data-grids"));
1588 const selectedFilterTemp = $(this).find(".ultp-filter-clear-template");
1589 const clearBtn = $(this).find(".ultp-filter-clear-button");
1590 const firstElClass = "ultp-block-" + clearBtn.data("blockid") + "-first";
1591 let postId = "";
1592 let builder = "";
1593
1594 function resetFilters() {
1595 // Resetting select fields
1596 that.find(".ultp-filter-select").each(function () {
1597 const defVal = $(this).find(".ultp-filter-select-options li").first();
1598 $(this).attr("data-selected", defVal.attr("data-id"));
1599 $(this).find(".ultp-filter-select-field-selected").text(defVal.text());
1600 });
1601
1602 // Resetting clear button
1603 const selectedFilter = that.find(".ultp-filter-clear-selected-filter");
1604 if (selectedFilter.hasClass(firstElClass)) {
1605 clearBtn.addClass(firstElClass);
1606 }
1607 selectedFilter.remove();
1608
1609 // Resetting search input
1610 that.find(".ultp-filter-search input").val("");
1611
1612 // Resetting filter buttons
1613 that.find('.ultp-filter-button[data-is-active="true"]').each(function () {
1614 $(this).removeClass("ultp-filter-button-active");
1615 $(this).attr("data-is-active", "false");
1616 });
1617 }
1618
1619 function fetchPostsWithFilter() {
1620 gridData.forEach((data) => {
1621 applyFilter(
1622 parent,
1623 data["blockId"],
1624 data["name"],
1625 postId,
1626 grids,
1627 builder,
1628 );
1629 });
1630 }
1631
1632 that.find(".ultp-filter-select").each(function () {
1633 const dropdown = $(this).find(".ultp-filter-select-options");
1634 const selected = $(this).find(".ultp-filter-select-field-selected");
1635 const icon = $(this).find(".ultp-filter-select-field-icon");
1636 const filter = $(this);
1637 const type = $(this).attr("data-type");
1638 const input = $(this).find(".ultp-filter-select-search");
1639 postId = $(this).attr("data-current-postid");
1640 builder = $(this).attr("data-builder");
1641 function showDropdown(show) {
1642 if (show) {
1643 $(".ultp-filter-select .ultp-filter-select-options").css(
1644 "display",
1645 "none",
1646 );
1647 $(".ultp-filter-select .ultp-filter-select-field-icon").removeClass(
1648 "ultp-dropdown-icon-rotate",
1649 );
1650 $(".ultp-filter-select").attr("aria-expanded", false);
1651
1652 dropdown.css("display", "block");
1653 icon.addClass("ultp-dropdown-icon-rotate");
1654 } else {
1655 dropdown.css("display", "none");
1656 icon.removeClass("ultp-dropdown-icon-rotate");
1657 }
1658
1659 filter.attr("aria-expanded", show);
1660 }
1661
1662 const defValue = dropdown.find("li").first();
1663
1664 function resetFilter() {
1665 selected.text(defValue.text());
1666 filter.attr("data-selected", defValue.attr("data-id"));
1667 }
1668
1669 // Toggle on click
1670 $(this).on("click", function (e) {
1671 e.stopPropagation();
1672 showDropdown(dropdown.css("display") === "none");
1673 });
1674
1675 $(dropdown)
1676 .find("li")
1677 .each(function () {
1678 const value = $(this).attr("data-id");
1679 const filterId = $(this).attr("data-blockId");
1680 const text = $(this).text();
1681 $(this).on("click", function () {
1682 selected.text(text);
1683 filter.attr("data-selected", value);
1684
1685 // Selected filters
1686 if (value === "_all") {
1687 that.find(`.ultp-filter-clear[data-type="${type}"]`).remove();
1688 } else if (clearBtn.length > 0) {
1689 let isNew = false;
1690
1691 let copy = that.find(`.ultp-filter-clear[data-type="${type}"]`);
1692
1693 if (copy.length < 1) {
1694 isNew = true;
1695 copy = selectedFilterTemp.clone();
1696 }
1697
1698 copy.removeClass("ultp-filter-clear-template");
1699 copy.addClass("ultp-filter-clear-selected-filter");
1700 copy
1701 .find(".ultp-selected-filter-text")
1702 .text(
1703 getFormattedSelectedFilter(
1704 filter.attr("data-filter-label"),
1705 text,
1706 ),
1707 );
1708 // Alignment
1709 if (clearBtn.hasClass(firstElClass)) {
1710 clearBtn.removeClass(firstElClass);
1711 copy.addClass(firstElClass);
1712 }
1713 copy.find(".ultp-selected-filter-icon").on("click", function () {
1714 resetFilter();
1715 if (copy.hasClass(firstElClass)) {
1716 if (
1717 copy.next().hasClass("ultp-filter-clear-selected-filter")
1718 ) {
1719 copy.next().addClass(firstElClass);
1720 } else {
1721 clearBtn.addClass(firstElClass);
1722 }
1723 }
1724 copy.remove();
1725 fetchPostsWithFilter();
1726 });
1727 copy.attr("data-id", value);
1728 copy.attr("data-type", type);
1729 copy.attr("data-for", filterId);
1730 copy.css({
1731 display: "block",
1732 });
1733 isNew && copy.insertBefore(clearBtn);
1734 }
1735 fetchPostsWithFilter();
1736 showDropdown(true);
1737 });
1738 });
1739
1740 input.on("click", function (e) {
1741 e.preventDefault();
1742 e.stopPropagation();
1743 });
1744
1745 input.on("input", function (e) {
1746 const value = String(e.target.value).toLowerCase();
1747
1748 if (value.length > 0) {
1749 dropdown.find("li").each(function () {
1750 const currValue = $(this).text();
1751 $(this).css(
1752 "display",
1753 currValue.toLowerCase().includes(value) ? "list-item" : "none",
1754 );
1755 });
1756 } else {
1757 dropdown.find("li").each(function () {
1758 $(this).css("display", "list-item");
1759 });
1760 }
1761 });
1762
1763 $(document).on("click", function (e) {
1764 if (!filter.is(e.target) && !filter.has(e.target).length) {
1765 showDropdown(false);
1766 }
1767 });
1768 });
1769
1770 that.find(".ultp-filter-button").each(function () {
1771 const currFBtn = this;
1772 const fBtnType = $(this).data("type");
1773 postId = $(this).attr("data-current-postid");
1774 $(this).on("click", function () {
1775 const isActive = $(currFBtn).attr("data-is-active") === "true";
1776 const value = $(this).data("selected");
1777
1778 // Clicking all button will disable other
1779 if (value === "_all") {
1780 const otherBtns = that.find(
1781 '.ultp-filter-button[data-selected]:not([data-selected="_all"])',
1782 );
1783 if (otherBtns.length > 0) {
1784 otherBtns.attr("data-is-active", "false");
1785 otherBtns.removeClass("ultp-filter-button-active");
1786 }
1787 }
1788
1789 // This buttons are exclusive
1790 else if (
1791 ["adv_sort", "order", "order_by"].includes(fBtnType) &&
1792 !isActive
1793 ) {
1794 const otherBtns = that.find(
1795 `.ultp-filter-button[data-type="${fBtnType}"]`,
1796 );
1797 if (otherBtns.length > 0) {
1798 otherBtns.attr("data-is-active", "false");
1799 otherBtns.removeClass("ultp-filter-button-active");
1800 }
1801 }
1802
1803 // Click a button should disable all button
1804 else {
1805 const allBtn = that.find('.ultp-filter-button[data-selected="_all"]');
1806 if (allBtn.length > 0) {
1807 allBtn.attr("data-is-active", "false");
1808 allBtn.removeClass("ultp-filter-button-active");
1809 }
1810 }
1811
1812 if (isActive) {
1813 $(currFBtn).attr("data-is-active", "false");
1814 $(currFBtn).removeClass("ultp-filter-button-active");
1815 } else {
1816 $(currFBtn).attr("data-is-active", "true");
1817 $(currFBtn).addClass("ultp-filter-button-active");
1818 if (
1819 ["author"].includes(fBtnType) ||
1820 !($(this).parent().attr("data-multiselect") == "true")
1821 ) {
1822 $(currFBtn)
1823 .siblings()
1824 .attr("data-is-active", "false")
1825 .removeClass("ultp-filter-button-active");
1826 }
1827 }
1828
1829 fetchPostsWithFilter();
1830 });
1831 });
1832
1833 clearBtn.on("click", function () {
1834 resetFilters();
1835 fetchPostsWithFilter();
1836 });
1837
1838 let searchTimeout;
1839
1840 function performSearch(isDebounce) {
1841 clearTimeout(searchTimeout);
1842 searchTimeout = isDebounce
1843 ? setTimeout(fetchPostsWithFilter, 500)
1844 : fetchPostsWithFilter();
1845 }
1846
1847 parent
1848 .find(".ultp-filter-search input")
1849 .off("input")
1850 .on("input", function () {
1851 performSearch(true);
1852 });
1853
1854 parent.find(".ultp-filter-search input").on("keydown", function (e) {
1855 if (e.key === "Enter") {
1856 performSearch(false);
1857 }
1858 });
1859
1860 parent.find(".ultp-filter-search-icon").on("click", function () {
1861 performSearch(false);
1862 });
1863 });
1864
1865 /**************************************
1866 Dark Light Block
1867 **************************************/
1868
1869 // dark light logo vars
1870 const customSrc = ultp_data_frontend?.dark_logo;
1871 const site_logo = $(".ultp-dark-logo.wp-block-site-logo")
1872 .find("img")
1873 .attr("src");
1874 const site_srcset =
1875 $(".ultp-dark-logo.wp-block-site-logo").find("img").attr("srcset") || "";
1876
1877 /************** handle dark light color swap on click **************/
1878 $(document).on(
1879 "click",
1880 ".ultp-dark-light-block-wrapper-content.ultp-frontend .ultp-dl-con",
1881 function (e) {
1882 e.preventDefault();
1883 const parents = $(this).closest(".ultp-dark-light-block-wrapper-content");
1884 const lightMode = $(this).hasClass("ultp-light-con");
1885
1886 const currentParents = $(this).closest(".ultp-dl-after-before-con");
1887 const opponent = parents.find(
1888 `.ultp-${lightMode ? "dark" : "light"}-con`,
1889 );
1890 const opponentParents = opponent.closest(".ultp-dl-after-before-con");
1891
1892 const iconlay = currentParents.data("iconlay");
1893 const iconsize = currentParents.data("iconsize");
1894 const iconrev = currentParents.data("iconrev");
1895
1896 let delay = 0;
1897 if (["layout5", "layout6", "layout7"].includes(iconlay)) {
1898 delay = iconlay == "layout7" ? 500 : 400;
1899 const extraSize =
1900 iconlay == "layout7"
1901 ? $(this).find(".ultp-dl-text").width()
1902 : iconsize / 2;
1903 if (lightMode) {
1904 $(this)
1905 .find(".ultp-dl-svg-con")
1906 .css({
1907 transform: `translateX(calc(${100 * (iconrev ? -1 : 1)}% ${
1908 iconrev ? "-" : "+"
1909 } ${extraSize}px))`,
1910 transition: `transform ${delay / 1000}s ease`,
1911 });
1912 if (iconlay == "layout6") {
1913 $(this)
1914 .find(".ultp-dl-text")
1915 .css({
1916 transform: `translateX(calc(${100 * (iconrev ? 1 : -1)}% ${
1917 iconrev ? "+" : "-"
1918 } ${extraSize}px))`,
1919 transition: `transform ${delay / 1000}s ease`,
1920 });
1921 } else if (iconlay == "layout7") {
1922 $(this)
1923 .find(".ultp-dl-text")
1924 .css({
1925 transform: `translateX(calc(${
1926 (iconrev ? 1 : -1) * iconsize
1927 }px))`,
1928 transition: `transform ${delay / 1000}s ease`,
1929 });
1930 }
1931 } else {
1932 $(this)
1933 .find(".ultp-dl-svg-con")
1934 .css({
1935 transform: `translateX(calc(${100 * (iconrev ? 1 : -1)}% ${
1936 iconrev ? "+" : "-"
1937 } ${extraSize}px))`,
1938 transition: `transform ${delay / 1000}s ease`,
1939 });
1940 if (iconlay == "layout6") {
1941 $(this)
1942 .find(".ultp-dl-text")
1943 .css({
1944 transform: `translateX(calc(${100 * (iconrev ? -1 : 1)}% ${
1945 iconrev ? "-" : "+"
1946 } ${extraSize}px))`,
1947 transition: `transform ${delay / 1000}s ease`,
1948 });
1949 } else if (iconlay == "layout7") {
1950 $(this)
1951 .find(".ultp-dl-text")
1952 .css({
1953 transform: `translateX(calc(${
1954 (iconrev ? -1 : 1) * iconsize
1955 }px))`,
1956 transition: `transform ${delay / 1000}s ease`,
1957 });
1958 }
1959 }
1960 }
1961 setCookie("ultplocalDLMode", lightMode ? "ultpdark" : "ultplight", 60);
1962 setTimeout(() => {
1963 currentParents.addClass("inactive");
1964 opponentParents.removeClass("inactive");
1965
1966 // handle dark light image block
1967 if (lightMode) {
1968 $(".wp-block-ultimate-post-image .ultp-light-image-block").addClass(
1969 "inactive",
1970 );
1971 $(".wp-block-ultimate-post-image .ultp-dark-image-block").removeClass(
1972 "inactive",
1973 );
1974 } else if (!lightMode) {
1975 $(".wp-block-ultimate-post-image .ultp-dark-image-block").addClass(
1976 "inactive",
1977 );
1978 $(
1979 ".wp-block-ultimate-post-image .ultp-light-image-block",
1980 ).removeClass("inactive");
1981 }
1982
1983 // handle dark light logo
1984 $(".ultp-dark-logo.wp-block-site-logo")
1985 .find("img")
1986 .attr("src", lightMode ? customSrc : site_logo)
1987 .attr("srcset", lightMode ? customSrc : site_srcset);
1988 $(".ultp-dark-logo.wp-block-site-logo img").css({ content: "initial" });
1989
1990 // handle more than one block
1991 $(
1992 `.ultp-dark-light-block-wrapper-content .ultp-${
1993 lightMode ? "dark" : "light"
1994 }-con`,
1995 ).each(function () {
1996 $(this).closest(".ultp-dl-after-before-con").removeClass("inactive");
1997 });
1998 $(
1999 `.ultp-dark-light-block-wrapper-content .ultp-${
2000 lightMode ? "light" : "dark"
2001 }-con`,
2002 ).each(function () {
2003 $(this).closest(".ultp-dl-after-before-con").addClass("inactive");
2004 });
2005
2006 // remove transition
2007 $(this).find(".ultp-dl-svg-con").removeAttr("style");
2008 $(this).find(".ultp-dl-text").removeAttr("style");
2009
2010 handleDarkLight();
2011 }, delay);
2012 },
2013 );
2014
2015 /************** set cookie **************/
2016 function setCookie(cookieName, cookieValue, expires) {
2017 const date = new Date();
2018 date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
2019 let expiresAt = "expires=" + date.toUTCString();
2020 document.cookie = cookieName + "=" + cookieValue + ";" + expiresAt + ";";
2021 }
2022
2023 /************** get registered cookie **************/
2024 function getCookie(cookieName) {
2025 let c_name = cookieName + "=";
2026 let c_array = document.cookie.split(";");
2027 for (let i = 0; i < c_array.length; i++) {
2028 let item = c_array[i];
2029 while (item.charAt(0) == " ") {
2030 item = item.substring(1);
2031 }
2032 if (item.indexOf(c_name) == 0) {
2033 return item.substring(c_name.length, item.length);
2034 }
2035 }
2036 return "";
2037 }
2038
2039 /************** handle dark light color swap **************/
2040 function handleDarkLight() {
2041 if (
2042 $("#ultp-preset-colors-style-inline-css") &&
2043 $("#ultp-preset-colors-style-inline-css")[0]
2044 ) {
2045 const root = $("#ultp-preset-colors-style-inline-css")[0].sheet;
2046 const base1 = root.cssRules[0].style.getPropertyValue(
2047 "--postx_preset_Base_1_color",
2048 );
2049 const base2 = root.cssRules[0].style.getPropertyValue(
2050 "--postx_preset_Base_2_color",
2051 );
2052 const base3 = root.cssRules[0].style.getPropertyValue(
2053 "--postx_preset_Base_3_color",
2054 );
2055
2056 const contrast1 = root.cssRules[0].style.getPropertyValue(
2057 "--postx_preset_Contrast_1_color",
2058 );
2059 const contrast2 = root.cssRules[0].style.getPropertyValue(
2060 "--postx_preset_Contrast_2_color",
2061 );
2062 const contrast3 = root.cssRules[0].style.getPropertyValue(
2063 "--postx_preset_Contrast_3_color",
2064 );
2065
2066 root.cssRules[0].style.setProperty(
2067 "--postx_preset_Base_1_color",
2068 contrast1,
2069 );
2070 root.cssRules[0].style.setProperty(
2071 "--postx_preset_Base_2_color",
2072 contrast2,
2073 );
2074 root.cssRules[0].style.setProperty(
2075 "--postx_preset_Base_3_color",
2076 contrast3,
2077 );
2078
2079 root.cssRules[0].style.setProperty(
2080 "--postx_preset_Contrast_1_color",
2081 base1,
2082 );
2083 root.cssRules[0].style.setProperty(
2084 "--postx_preset_Contrast_2_color",
2085 base2,
2086 );
2087 root.cssRules[0].style.setProperty(
2088 "--postx_preset_Contrast_3_color",
2089 base3,
2090 );
2091 }
2092 }
2093
2094 function handleDailyMotion() {
2095 $(
2096 ".ultp-video-modal .ultp-video-modal__content .ultp-video-wrapper > iframe",
2097 ).each(function () {
2098 const that = $(this);
2099 const videoSrc = that.attr("src");
2100 if (
2101 videoSrc &&
2102 videoSrc.includes("dailymotion.com/player") &&
2103 videoSrc[videoSrc.length - 1] == "&"
2104 ) {
2105 that.attr(
2106 "src",
2107 videoSrc.slice(0, videoSrc.length - 1) + "?autoplay=0",
2108 );
2109 }
2110 });
2111 }
2112 if (isFront) {
2113 handleDailyMotion();
2114 }
2115
2116 // /*************************************
2117 // Tab Block
2118 // *************************************/
2119 // $('.ultp-tabs-content[data-show="click"] .ultp-tab-item-label').on(
2120 // "click",
2121 // function (e) {
2122 // handleTabItem($(this));
2123 // }
2124 // );
2125 // $('.ultp-tabs-content[data-show="hover"] .ultp-tab-item-label').hover(
2126 // function (e) {
2127 // handleTabItem($(this));
2128 // }
2129 // );
2130 // function handleTabItem(that) {
2131 // if (that.closest("body.block-editor-page").length) {
2132 // return;
2133 // }
2134 // const parent = that.closest(".ultp-tabs-content");
2135 // parent.find(".ultp-tab-item-label").removeClass("pt-active");
2136 // that.addClass("pt-active");
2137
2138 // const tabId = that.data("tabid");
2139 // parent
2140 // .find("> .ultp-tabs-content-warp > .wp-block-ultimate-post-tab-item")
2141 // .removeClass("pt-active");
2142 // parent
2143 // .find(
2144 // `> .ultp-tabs-content-warp > .wp-block-ultimate-post-tab-item.${tabId}`
2145 // )
2146 // .addClass("pt-active");
2147 // }
2148 if ($(".ultp-iscroll-active").length > 0) {
2149 $(".ultp-iscroll-active").each(function () {
2150 $(this).UltpInfiniteScroll(
2151 $(this),
2152 setSession, // set Unique Post Id
2153 getUltpNonce, // Get Nonce
2154 paginationAjaxCall, // Pagination API Call Function
2155 );
2156 });
2157 }
2158 })(jQuery);
2159