$taxonomyRelation]; if ( 'post' === $postType && count( $selectedCategories ) ) { $termsQuery[] = [ 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $selectedCategories, ]; } if ( 'post' === $postType && count( $selectedTags ) ) { $termsQuery[] = [ 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $selectedTags, ]; } $postsInclude = Functions::filterNaN( $queryAttr['postsInclude'] ?? [] ); $post__in = !empty( $postsInclude ) ? [ 'post__in' => $postsInclude ] : []; $post__not_in = Functions::filterNaN( $queryAttr['postsExclude'] ?? [] ); $postsAuthors = Functions::filterNaN( $queryAttr['postsAuthors'] ?? [] ); $author__in = !empty( $postsAuthors ) ? [ 'author__in' => $postsAuthors ] : []; if ( ! empty( $isExcludeSticky ) ) { $stickyPosts = get_option( 'sticky_posts' ); if ( ! empty( $stickyPosts ) ) { $post__not_in = array_merge( $post__not_in, $stickyPosts ); } } $query = array_merge( [ 'post_type' => $postType, 'posts_per_page' => $isPostsPerPageAll ? -1 : (int) $postsPerPage, 'orderby' => $postsOrderBy, 'order' => $postsOrder, 'tax_query' => count( $termsQuery ) > 1 ? $termsQuery : [], 'offset' => $isPostsPerPageAll ? 0 : (int) $postsOffset, 'post__not_in' => array_unique( $post__not_in ), 'has_password' => false, 'post_status' => 'publish' ], $post__in, $author__in ); // Query Presets are available in the premium version. return $query; } /** * Fetches posts based on attributes and page number. * * @param array $queryAttr Block attributes. * @param int $pageNumber Current page number. * @return array Arranged posts data. */ public static function getPosts( $queryAttr, $pageNumber = 1 ){ $isPostsPerPageAll = $queryAttr['isPostsPerPageAll'] ?? false; $postsPerPage = $queryAttr['postsPerPage'] ?? 12; $postsOffset = $queryAttr['postsOffset'] ?? 0; $postType = $queryAttr['postType'] ?? 'post'; $fImgSize = $queryAttr['fImgSize'] ?? 'full'; $excerptLength = $queryAttr['excerptLength'] ?? 25; $isExcerptFromContent = $queryAttr['isExcerptFromContent'] ?? false; $queryAttr['isPostsPerPageAll'] = true === $isPostsPerPageAll || 'true' === $isPostsPerPageAll; // Ensure numeric values to avoid PHP type errors and handle "all" mode $postsPerPage = (int) $postsPerPage; $pageNumber = (int) $pageNumber; $postsOffset = (int) $postsOffset; // Extract excerptFrom with fallback to legacy isExcerptFromContent $excerptFrom = $queryAttr['excerptFrom'] ?? ( isset( $queryAttr['excerpt']['from'] ) ? $queryAttr['excerpt']['from'] : 'excerpt' ); $excerptFrom = ( 'content' === $excerptFrom || $isExcerptFromContent ) ? 'content' : 'excerpt'; $offset = ( $postsPerPage * max( 0, $pageNumber - 1 ) ) + $postsOffset; $newArgs = wp_parse_args( [ 'offset' => $offset ], self::arrangeQuery( $queryAttr ) ); $posts = Functions::arrangedPosts( get_posts( $newArgs ), $postType, $fImgSize ?? 'full', $excerptFrom, $excerptLength ?? 25 ); return $posts; } /** * Generates a skeleton article HTML for loading state. * * @param string $prefix CSS class prefix. * @return string Skeleton article HTML. */ public static function skeletonArticle( $prefix ){ $articleEl = ""; return wp_kses( $articleEl, [ 'article' => [ 'class' => [], 'aria-hidden' => [] ], 'div' => [ 'class' => [] ], 'span' => [ 'class' => [] ] ] ); } /** * Generates a skeleton accordion item HTML for loading state. * * @param string $prefix CSS class prefix. * @return string Skeleton accordion item HTML. */ public static function skeletonAccordionItem( $prefix ){ $itemEl = ""; return wp_kses( $itemEl, [ 'div' => [ 'class' => [], 'aria-hidden' => [] ], 'span' => [ 'class' => [], 'style' => [] ] ] ); } /** * Generates a loading placeholder HTML based on layout and attributes. * * @param array $attributes Block attributes. * @param string $prefix CSS class prefix. * @return string Loading placeholder HTML. */ public static function loadingPlaceholder( $attributes, $prefix ){ $countQuery = new \WP_Query( Posts::arrangeQuery( $attributes ?? [] ) ); $postCount = $countQuery->found_posts; $layout = $attributes['layout'] ?? ''; $columns = $attributes['columns'] ?? [ 'desktop' => 3, 'tablet' => 2, 'mobile' => 1 ]; $sliderHeight = $attributes['sliderHeight'] ?? '350px'; $sliderIsPage = $attributes['sliderIsPage'] ?? true; $sliderIsPrevNext = $attributes['sliderIsPrevNext'] ?? true; $tickerVisible = $attributes['tickerVisible'] ?? 3; $accordion = $attributes['accordion'] ?? []; $accordionTheme = ! empty( $accordion['theme'] ) ? $accordion['theme'] : 'classic'; $accordionMaxWidth = ! empty( $accordion['maxWidth'] ) ? $accordion['maxWidth']['desktop'] : ''; $colD = $columns['desktop']; $colT = $columns['tablet']; $colM = $columns['mobile']; $gridClass = $prefix ."GridPosts columns-$colD columns-tablet-$colT columns-mobile-$colM"; $placeholderId = wp_unique_id( $prefix .'LoadingPlaceholder-' ); $sliderStyles = "#$placeholderId .$prefix" . "SliderSkeleton article{ min-height: $sliderHeight; }"; $loadingLabel = esc_html__( 'Loading posts…', 'advanced-post-block' ); ob_start(); ?>
">