';
if (isset($attributes['PaginationOnToggler']) && !empty($attributes['PaginationOnToggler'])) {
$args['paged'] = get_query_var('paged') ? get_query_var('paged') : 1;
}
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
$post_title = get_the_title();
$post_date = get_the_date();
$post_permalink = get_permalink();
$post_author = get_the_author();
$post_categories = get_the_category();
$category_names = array();
if (empty($post_categories)) {
$taxonomies = get_object_taxonomies(get_post(), 'names');
foreach ($taxonomies as $taxonomy) {
$terms = get_the_terms(get_the_ID(), $taxonomy);
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
$category_names[] = $term->name;
}
}
}
} else {
foreach ($post_categories as $category) {
$category_names[] = $category->name;
}
}
$category_list = implode(', ', $category_names);
if (isset($attributes['showImgToggler']) && $attributes['showImgToggler'] == 1) {
if (isset($attributes['imageUrl'])) {
$image_url = get_the_post_thumbnail_url(get_the_ID(), 'medium');
if (empty($attributes['selectedImageTogggler']) && empty($attributes['widthPercentageState'])) {
$custom_width = isset($attributes['ImgWidth']) ? $attributes['ImgWidth'] : '';
$custom_height = isset($attributes['height']) ? $attributes['height'] : '';
} elseif (isset($attributes['widthPercentageState']) && $attributes['widthPercentageState'] == true && empty($attributes['selectedImageTogggler'])) {
$custom_width = $attributes['widthPercentage'];
$custom_height = $attributes['widthPercentage'];
}
}
if (!empty($custom_width) && !empty($custom_height)) {
$list_items_markup .= '
';
if (isset($attributes['PaginationOnToggler']) && !empty($attributes['PaginationOnToggler'])) {
$pagination_args = array(
'total' => $query->max_num_pages,
'current' => $args['paged'],
'mid_size' => 2,
'prev_text' => isset($attributes['prevName']) ? __($attributes['prevName']) : __('Prev'),
'next_text' => isset($attributes['nextName']) ? __($attributes['nextName']) : __('Next'),
'type' => 'array',
);
$pagination_links = paginate_links($pagination_args);
if ($pagination_links) {
$alignment_class = isset($attributes['paginationAline']) ? $attributes['paginationAline'] : 'center';
$list_items_markup .= '';
}
}
return $list_items_markup;
}
function aplb_slider_layout($args, $attributes)
{
$title_Manage_styling = !empty($attributes['title_Manage_styling']) ? 'true' : 'false';
$title_font_size = !empty($attributes['title_font_size']) ? $attributes['title_font_size'] : '1.5rem';
$Meta_Manage_styling = !empty($attributes['Meta_Manage_styling']) ? 'true' : 'false';
$Meta_font_size = !empty($attributes['Meta_font_size']) ? $attributes['Meta_font_size'] : '8px';
$Content_Manage_styling = !empty($attributes['Content_Manage_styling']) ? 'true' : 'false';
$Content_font_size = !empty($attributes['Content_font_size']) ? $attributes['Content_font_size'] : '8px';
$Read_more_font_size = !empty($attributes['Read_more_font_size']) ? $attributes['Read_more_font_size'] : '1rem';
$showReadMoreToggler = !empty($attributes['showReadMoreToggler']) ? 'true' : 'false';
$css = '';
if ($showReadMoreToggler === 'true') {
$button_Top_bottom_Padding = !empty($attributes['button_Top_bottom_Padding']) ? $attributes['button_Top_bottom_Padding'] : '8px';
$button_Right_Left_Padding = !empty($attributes['button_Right_Left_Padding']) ? $attributes['button_Right_Left_Padding'] : '16px';
$ReadMoreTextColor = !empty($attributes['ReadMoreTextColor']) ? $attributes['ReadMoreTextColor'] : '#FFFFFF';
$ReadMoreBgColor = !empty($attributes['ReadMoreBgColor']) ? $attributes['ReadMoreBgColor'] : '#000000';
$css .= '
.slider-container .wep-btn {
padding: ' . $button_Top_bottom_Padding . ' ' . $button_Right_Left_Padding . ' !important;
background-color:' . $ReadMoreBgColor . '!important;
color: ' . $ReadMoreTextColor . '!important;
}
';
}
$customCSS = !empty($attributes['customCSS']) ? $attributes['customCSS'] : '';
if (!empty($customCSS)) {
$css .= $customCSS;
}
$css = apply_filters('apl_slider_style', $css, $attributes);
$custom_css = "";
$list_items_markup = '';
$list_items_markup .= $custom_css;
$list_items_markup .= '
';
$js = "document.addEventListener('DOMContentLoaded', function() {
let currentIndex = 0;
const slides = document.querySelectorAll('.slide');
function showSlide(index) {
slides.forEach((slide, i) => {
slide.style.display = i === index ? 'block' : 'none';
});
}
function nextSlide() {
currentIndex = (currentIndex + 1) % slides.length;
showSlide(currentIndex);
}
function prevSlide() {
currentIndex = (currentIndex - 1 + slides.length) % slides.length;
showSlide(currentIndex);
}
document.querySelector('.next').addEventListener('click', nextSlide);
document.querySelector('.prev').addEventListener('click', prevSlide);
setInterval(nextSlide, 3000);
showSlide(currentIndex);
});";
$js = apply_filters('apl_slider_script', $js, $attributes);
$custom_js = "";
$list_items_markup .= $custom_js;
return $list_items_markup;
}
function aplb_masonry_layout($args, $attributes)
{
$title_Manage_styling = !empty($attributes['title_Manage_styling']) ? 'true' : 'false';
$title_font_size = !empty($attributes['title_font_size']) ? $attributes['title_font_size'] : '1.5rem';
$Meta_Manage_styling = !empty($attributes['Meta_Manage_styling']) ? 'true' : 'false';
$Meta_font_size = !empty($attributes['Meta_font_size']) ? $attributes['Meta_font_size'] : '8px';
$Content_Manage_styling = !empty($attributes['Content_Manage_styling']) ? 'true' : 'false';
$Content_font_size = !empty($attributes['Content_font_size']) ? $attributes['Content_font_size'] : '8px';
$Read_more_font_size = !empty($attributes['Read_more_font_size']) ? $attributes['Read_more_font_size'] : '1rem';
$showReadMoreToggler = !empty($attributes['showReadMoreToggler']) ? 'true' : 'false';
$css = '';
if ($showReadMoreToggler === 'true') {
$button_Top_bottom_Padding = !empty($attributes['button_Top_bottom_Padding']) ? $attributes['button_Top_bottom_Padding'] : '8px';
$button_Right_Left_Padding = !empty($attributes['button_Right_Left_Padding']) ? $attributes['button_Right_Left_Padding'] : '16px';
$ReadMoreTextColor = !empty($attributes['ReadMoreTextColor']) ? $attributes['ReadMoreTextColor'] : '#FFFFFF';
$ReadMoreBgColor = !empty($attributes['ReadMoreBgColor']) ? $attributes['ReadMoreBgColor'] : '#000000';
$css .= '
.masonry-layout .wep-btn {
padding: ' . $button_Top_bottom_Padding . ' ' . $button_Right_Left_Padding . ' !important;
background-color:' . $ReadMoreBgColor . '!important;
color: ' . $ReadMoreTextColor . '!important ;
}
.pagination .page-numbers:hover, .pagination .page-numbers.current {
background-color:' . $ReadMoreBgColor . '!important;
color: ' . $ReadMoreTextColor . '!important;
border-color: ' . $ReadMoreBgColor . '!important;
}
';
}
$customCSS = !empty($attributes['customCSS']) ? $attributes['customCSS'] : '';
$columns = !empty($attributes['totalColoms']) ? $attributes['totalColoms'] : 3;
$column_gap = isset($attributes['columnGap']) && !empty($attributes['columnGap']) ? $attributes['columnGap'] : '20px';
$row_gap = isset($attributes['rowGap']) && !empty($attributes['rowGap']) ? $attributes['rowGap'] : '30px';
$row_gap = str_replace("px", "", $row_gap);
$row_gap .= 'px';
$column_gap = str_replace("px", "", $column_gap);
$column_gap .= 'px';
if (!empty($customCSS)) {
$css .= $customCSS;
}
if (wp_is_mobile()) {
$css .= '.masonry-layout {
column-gap: ' . $column_gap . ' !important;
column-count: 1;
}';
} else {
$css .= '.masonry-layout {
column-gap: ' . $column_gap . ' !important;
column-count: ' . $attributes["totalColoms"] . ';
}';
}
$css .= '
.masonry-layout .wep-card-text {
margin-bottom: ' . $row_gap . ' !important;
}
@media (min-width: 992px) {
.masonry-layout {
column-count: ' . $columns . ' !important;
}
}
.pagination{
margin-top: ' . $row_gap . ' !important;
}
/* Extra large screens (large desktops, 1200px and up) */
@media (min-width: 1200px) {
.masonry-layout {
column-count: ' . $columns . ' !important;
}
}
';
$css = apply_filters('apl_masanory_style', $css, $attributes);
$custom_css = "";
$list_items_markup = '';
$list_items_markup .= $custom_css;
$list_items_markup .= '
';
if (isset($attributes['PaginationOnToggler']) && !empty($attributes['PaginationOnToggler'])) {
$args['paged'] = get_query_var('paged') ? get_query_var('paged') : 1;
}
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
$post_title = get_the_title();
$post_date = get_the_date();
$post_permalink = get_permalink();
$post_author = get_the_author();
$post_categories = get_the_category();
$category_names = array();
if (empty($post_categories)) {
$taxonomies = get_object_taxonomies(get_post(), 'names');
foreach ($taxonomies as $taxonomy) {
$terms = get_the_terms(get_the_ID(), $taxonomy);
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
$category_names[] = $term->name;
}
}
}
} else {
foreach ($post_categories as $category) {
$category_names[] = $category->name;
}
}
$category_list = implode(', ', $category_names);
$list_items_markup .= '
';
$image_url = get_the_post_thumbnail_url(get_the_ID(), 'medium');
if (!empty($image_url)) {
if (isset($attributes['showImgToggler']) && $attributes['showImgToggler'] == 1) {
if (isset($attributes['imageUrl'])) {
$image_url = get_the_post_thumbnail_url(get_the_ID(), 'medium');
if (empty($attributes['selectedImageTogggler']) && empty($attributes['widthPercentageState'])) {
$custom_width = isset($attributes['ImgWidth']) ? $attributes['ImgWidth'] : '';
$custom_height = isset($attributes['height']) ? $attributes['height'] : '';
} elseif (isset($attributes['widthPercentageState']) && $attributes['widthPercentageState'] == true && empty($attributes['selectedImageTogggler'])) {
$custom_width = $attributes['widthPercentage'];
$custom_height = $attributes['widthPercentage'];
}
}
$list_items_markup .= '
';
if (!empty($custom_width) && !empty($custom_height)) {
$list_items_markup .= '
 . ')
';
} else {
$list_items_markup .= '
 . ')
';
}
$list_items_markup .= '
' . $category_list . ' ';
}
}
$list_items_markup .= '
';
if (isset($attributes['showPostTitle']) && $attributes['showPostTitle'] == 1) {
$list_items_markup .= '
' . esc_html($post_title) . '';
$list_items_markup .= '
';
}
if (isset($attributes['showMeta']) && $attributes['showMeta'] == 1) {
$list_items_markup .= '
';
$list_items_markup .= '
';
}
$list_items_markup .= '
';
if (isset($attributes['showContent']) && $attributes['showContent'] == 1) {
if (isset($attributes['contentType'])) {
if ($attributes['contentType'] == 'Excerpt') {
$words_limit = isset($attributes['wordsLimit']) ? intval($attributes['wordsLimit']) : 100;
$excerpt = get_the_excerpt();
$words = explode(' ', $excerpt);
$post_content = implode(' ', array_slice($words, 0, $words_limit));
} elseif ($attributes['contentType'] == 'Full-Post') {
$post_content = get_the_content();
}
}
$list_items_markup .= '
';
$list_items_markup .= '
';
$list_items_markup .= '
';
endwhile;
wp_reset_postdata();
endif;
$list_items_markup .= '
';
if (isset($attributes['PaginationOnToggler']) && !empty($attributes['PaginationOnToggler'])) {
$pagination_args = array(
'total' => $query->max_num_pages,
'current' => $args['paged'],
'mid_size' => 2,
'prev_text' => isset($attributes['prevName']) ? __($attributes['prevName']) : __('Prev'),
'next_text' => isset($attributes['nextName']) ? __($attributes['nextName']) : __('Next'),
'type' => 'array',
);
$pagination_links = paginate_links($pagination_args);
if ($pagination_links) {
$alignment_class = isset($attributes['paginationAline']) ? $attributes['paginationAline'] : 'center';
$list_items_markup .= '';
}
}
return $list_items_markup;
}
function aplb_register_custom_endpoints()
{
register_rest_route(
'wpppro/v1',
'/list-cpt/',
array(
'methods' => 'GET',
'callback' => array($this, 'aplb_get_custom_post_types_and_tax'),
'permission_callback' => '__return_true',
)
);
register_rest_route(
'wpppro/v1',
'/get-post-by-id/',
array(
'methods' => 'GET',
'callback' => array($this, 'aplb_get_posts_type_by_id'),
'permission_callback' => '__return_true',
)
);
}
function aplb_get_posts_type_by_id($request)
{
$id = $request['id'];
$id_array = explode(',', $id);
$args = array(
'post_type' => 'any',
'post__in' => $id_array,
'orderby' => 'post__in',
'posts_per_page' => -1,
);
$query = new WP_Query($args);
$posts_data = array();
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
// Prepare the post data array
$post_data = array(
'id' => $post_id,
'date' => get_the_date('c'),
'date_gmt' => get_gmt_from_date(get_the_date('Y-m-d H:i:s')),
'guid' => array('rendered' => get_the_guid()),
'modified' => get_the_modified_date('c'),
'modified_gmt' => get_gmt_from_date(get_the_modified_date('Y-m-d H:i:s')),
'slug' => get_post_field('post_name', $post_id),
'status' => get_post_status($post_id),
'type' => get_post_type($post_id),
'link' => get_permalink($post_id),
'title' => array('rendered' => get_the_title($post_id)),
'content' => array('rendered' => apply_filters('the_content', get_the_content($post_id)), 'protected' => false),
'featured_media' => get_post_thumbnail_id($post_id),
'template' => get_page_template_slug($post_id),
'meta' => get_post_meta($post_id),
'featured_image_url' => array(
'thumbnail' => get_the_post_thumbnail_url($post_id, 'thumbnail'),
'medium' => get_the_post_thumbnail_url($post_id, 'medium'),
'medium_large' => get_the_post_thumbnail_url($post_id, 'medium_large'),
'large' => get_the_post_thumbnail_url($post_id, 'large'),
'1536x1536' => get_the_post_thumbnail_url($post_id, '1536x1536'),
'2048x2048' => get_the_post_thumbnail_url($post_id, '2048x2048')
),
'post_author' => get_the_author_meta('display_name', get_post_field('post_author', $post_id)),
'assigned_categories' => wp_get_post_categories($post_id, array('fields' => 'names')),
);
$posts_data[] = $post_data;
}
}
wp_reset_postdata();
return new WP_REST_Response($posts_data, 200);
}
function aplb_get_custom_post_types_and_tax()
{
$args = array('public' => true, '_builtin' => false);
$custom_post_types = get_post_types($args, 'objects');
$cpt_minial_info = [];
if ($custom_post_types) {
foreach ($custom_post_types as $post_type) {
$cpt_minial_info[$post_type->name] = $post_type->label;
}
}
$custom_post_types = $cpt_minial_info;
$post_type_taxonomies = array();
$all_cpts = array_merge(array('post' => 'Posts', 'page' => 'Pages'), $custom_post_types);
foreach ($all_cpts as $post_type => $postName) {
$taxonomies = get_object_taxonomies($post_type, 'objects');
$taxonomy_info = array();
foreach ($taxonomies as $taxonomy) {
$taxonomy_info[] = array(
'slug' => $taxonomy->name,
'label' => $taxonomy->label,
);
}
$post_type_taxonomies[$post_type] = $taxonomy_info;
}
$response = array('cpt_list' => $custom_post_types, 'post_type_and_taxonomies' => $post_type_taxonomies);
return rest_ensure_response($response);
}
// Register REST fields for various post types
function aplb_register_custom_rest_fields()
{
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'names', 'and');
$post_types = array_merge($post_types, array('post', 'page'));
foreach ($post_types as $post_type) {
$this->aplb_register_featured_image_rest_field($post_type);
$this->aplb_register_author_rest_field($post_type);
$this->aplb_register_assigned_categories_rest_field($post_type);
}
}
// Register 'featured_image_url' REST field
function aplb_register_featured_image_rest_field($post_type)
{
register_rest_field($post_type, 'featured_image_url', array(
'get_callback' => array($this, 'aplb_get_featured_image_callback'),
'schema' => array(
'description' => esc_html__('Featured Image', 'advanced-posts-listing'),
'type' => 'string'
),
));
}
function aplb_get_featured_image_callback($post)
{
$featured_image_id = get_post_thumbnail_id($post['id']);
$image_sizes = get_intermediate_image_sizes();
$all_sizes = array();
if (!empty($image_sizes)) {
foreach ($image_sizes as $size) {
$image_url = wp_get_attachment_image_src($featured_image_id, $size);
if (isset($image_url[0])) {
$all_sizes[$size] = $image_url[0];
}
}
}
return $all_sizes;
}
function aplb_register_author_rest_field($post_type)
{
register_rest_field($post_type, 'post_author', array(
'get_callback' => array($this, 'aplb_get_author_callback'),
'schema' => array(
'description' => esc_html__('Author Name', 'advanced-posts-listing'),
'type' => 'string'
),
));
}
function aplb_get_author_callback($post)
{
$author_name = '';
if (isset($post['author']) && !empty($post['author'])) {
$author = get_userdata($post['author']);
if ($author && !is_wp_error($author)) {
if ($author->first_name && $author->last_name) {
$author_name = $author->first_name . ' ' . $author->last_name;
} elseif ($author->display_name) {
$author_name = $author->display_name;
} else {
$author_name = $author->user_login;
}
}
}
return $author_name;
}
function aplb_register_assigned_categories_rest_field($post_type)
{
register_rest_field($post_type, 'assigned_categories', array(
'get_callback' => array($this, 'aplb_get_assigned_categories_callback'),
'schema' => array(
'description' => esc_html__('Assigned Categories', 'advanced-posts-listing'),
'type' => 'string'
),
));
}
function aplb_get_assigned_categories_callback($post)
{
$categories = get_the_category($post['id']);
$category_names = array();
if (empty($categories)) {
$post_id = $post['id'];
$post_type = $post['type'];
$taxonomies = get_object_taxonomies($post_type);
foreach ($taxonomies as $taxonomy) {
$terms = wp_get_post_terms($post_id, $taxonomy);
if (!empty($terms)) {
foreach ($terms as $term) {
$category_names[] = $term->name;
}
}
}
$category_names = array_unique($category_names);
} else {
foreach ($categories as $category) {
$category_names[] = $category->name;
}
}
return implode(', ', $category_names);
}
function aplb_get_server_side_pass()
{
wp_localize_script('advanced-posts-listing-advanced-posts-listing-block-editor-script', 'aplb_server_data', array(
'rest_url' => esc_url(get_rest_url(null)),
));
}
}
return new Advanced_Post_Listing_Block();
}