', esc_html( $tag ) ); if ( ! empty( $url ) ) { printf( '%2$s', esc_url( $url ), esc_html( $title ) ); } else { echo esc_html( $title ); } printf( '', esc_html( $tag ) ); } /** * Build sanitized settings from AJAX request. * * @return array */ function gridelwi_get_ajax_settings() { // Nonce validation happens in each AJAX callback before this helper is called. if ( ! filter_has_var( INPUT_POST, 'settings' ) ) { return array(); } $raw_settings = filter_input( INPUT_POST, 'settings', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); if ( ! is_array( $raw_settings ) ) { return array(); } return gridelwi_sanitize_request_value( $raw_settings ); } /** * Display post thumbnail markup. * * @param string $thumbsize Thumbnail size. * @return string */ function gridelwi_display_post_thumb( $thumbsize ) { $output = ''; if ( has_post_thumbnail() ) { $output = gridelwi_post_thumbnail( $thumbsize ); } return $output; } /** * Get post thumbnail HTML. * * @param string $thumbsize Thumbnail size. * @param string $link Optional link. * @return string|null */ function gridelwi_post_thumbnail( $thumbsize = '', $link = '' ) { if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) { return null; } global $post; $link = empty( $link ) ? get_permalink() : $link; $html = ''; if ( is_singular( 'post' ) && is_single( $post ) ) { $html .= '
'; $html .= gridelwi_get_attachment_thumbnail( get_post_thumbnail_id(), 'full' ); $html .= '
'; } else { $html .= '
'; $html .= ''; $html .= '
'; } return $html; } /** * Get attachment thumbnail markup. * * @param int $attachment_id Attachment ID. * @param string|array $size Image size. * @param bool $icon Icon fallback. * @param array|string $attr Extra attributes. * @param bool $wrapper Wrap image. * @return string */ function gridelwi_get_attachment_thumbnail( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '', $wrapper = true ) { $html = ''; $attachment_size = array(); $image_sizes = get_intermediate_image_sizes(); $image_sizes[] = 'full'; if ( ! in_array( $size, $image_sizes, true ) ) { $attachment_size = array( 0 => null, 1 => null, 'bfi_thumb' => true, 'crop' => true, ); $sizes = explode( 'x', (string) $size ); if ( 2 === count( $sizes ) ) { $attachment_size[0] = absint( $sizes[0] ); $attachment_size[1] = absint( $sizes[1] ); } } if ( ! empty( $attachment_size ) ) { $size = $attachment_size; } $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); if ( ! $image ) { return $html; } list( $src, $width, $height ) = $image; $hwstring = image_hwstring( $width, $height ); $size_class = is_array( $size ) ? implode( 'x', $size ) : $size; $attachment = get_post( $attachment_id ); $default_attr = array( 'src' => $src, 'class' => "attachment-$size_class size-$size_class", 'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ), ); $attr = wp_parse_args( $attr, $default_attr ); /** This filter is documented in wp-includes/media.php */ $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound $attr = array_map( 'esc_attr', $attr ); if ( $wrapper ) { $html .= '
'; } $html .= rtrim( " $value ) { $html .= sprintf( ' %1$s="%2$s"', $name, $value ); } $html .= ' />'; if ( $wrapper ) { $html .= '
'; } return $html; } /** * Output first category. * * @param WP_Post $post Post object. * @return void */ function gridelwi_post_categories_first( $post ) { $cat_ids = wp_get_post_categories( $post->ID ); echo '
'; foreach ( $cat_ids as $cat_id ) { $category = get_category( $cat_id ); if ( ! $category || is_wp_error( $category ) ) { continue; } printf( '%2$s', esc_url( get_category_link( $category->term_id ) ), esc_html( $category->name ) ); break; } echo '
'; } /** * Build post term links. * * @param int $post_id Post ID. * @param string $separator Separator. * @return string */ function gridelwi_get_post_terms_list( $post_id = 0, $separator = ', ' ) { $post_id = $post_id ? absint( $post_id ) : get_the_ID(); if ( ! $post_id ) { return ''; } $post_type = get_post_type( $post_id ); $taxonomies = get_object_taxonomies( $post_type, 'objects' ); if ( empty( $taxonomies ) ) { return ''; } foreach ( $taxonomies as $taxonomy ) { if ( empty( $taxonomy->public ) ) { continue; } $terms = get_the_terms( $post_id, $taxonomy->name ); if ( empty( $terms ) || is_wp_error( $terms ) ) { continue; } $links = array(); foreach ( $terms as $term ) { $term_link = get_term_link( $term ); if ( is_wp_error( $term_link ) ) { continue; } $links[] = sprintf( '', esc_url( $term_link ), esc_html( $term->name ) ); } if ( ! empty( $links ) ) { return implode( esc_html( $separator ), $links ); } } return ''; } /** * Limit text to word count. * * @param string $string Original string. * @param int $limit Word limit. * @param string $afterlimit Append string. * @return string */ function gridelwi_substring( $string, $limit, $afterlimit = '[...]' ) { if ( empty( $string ) ) { return $string; } $parts = explode( ' ', wp_strip_all_tags( $string ), absint( $limit ) ); if ( count( $parts ) >= absint( $limit ) ) { array_pop( $parts ); $string = implode( ' ', $parts ) . ' ' . $afterlimit; } else { $string = implode( ' ', $parts ); } $string = preg_replace( '`[[^]]*]`', '', $string ); return strip_shortcodes( $string ); } /** * Get avatar HTML. * * @param int $author_id Author ID. * @param int $size Avatar size. * @return string */ function gridelwi_get_avatar( $author_id, $size = 80 ) { $logo = ''; if ( get_option( 'show_avatars' ) ) { $logo = get_avatar( $author_id, $size ); } else { $avatar_id = get_the_author_meta( '_user_avatar', $author_id ); if ( ! empty( $avatar_id ) ) { $avatar_url = wp_get_attachment_image_src( $avatar_id, 'thumbnail' ); if ( ! empty( $avatar_url[0] ) ) { $logo = sprintf( '', esc_url( $avatar_url[0] ), absint( $size ) ); } } } return $logo; } /** * Sanitize valid term IDs for a post type. * * @param array $term_ids Term IDs. * @param string $post_type_name Post type. * @return array */ function gridelwi_get_valid_term_ids( $term_ids, $post_type_name ) { $term_ids = array_map( 'absint', (array) $term_ids ); $taxonomies = get_object_taxonomies( $post_type_name ); $valid_ids = array(); foreach ( $taxonomies as $taxonomy ) { $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'include' => $term_ids, ) ); if ( empty( $terms ) || is_wp_error( $terms ) ) { continue; } foreach ( $terms as $term ) { $valid_ids[] = (int) $term->term_id; } } return array_values( array_unique( $valid_ids ) ); } /** * AJAX handler for layout 1. * * @return void */ function gridelwi_more_post_ajax_layout1() { check_ajax_referer( 'elementor_ajax_nonce', 'nonce' ); $settings = gridelwi_get_ajax_settings(); $posts_per_page = isset( $_POST['ppp'] ) ? absint( wp_unslash( $_POST['ppp'] ) ) : 3; $page = isset( $_POST['pageNumber'] ) ? absint( wp_unslash( $_POST['pageNumber'] ) ) : 1; $post_type_name = ! empty( $settings['source'] ) ? sanitize_key( $settings['source'] ) : 'post'; $thumbnail_size = ! empty( $settings['post_thumbnail_size'] ) ? sanitize_key( $settings['post_thumbnail_size'] ) : 'full'; $show_title = ! empty( $settings['show_title'] ) ? $settings['show_title'] : ''; $show_image = ! empty( $settings['show_image'] ) ? $settings['show_image'] : ''; $meta_data = ! empty( $settings['meta_data'] ) && is_array( $settings['meta_data'] ) ? array_map( 'sanitize_key', $settings['meta_data'] ) : array(); $show_excerpt = ! empty( $settings['show_excerpt'] ) ? $settings['show_excerpt'] : ''; $show_read_more = ! empty( $settings['show_read_more'] ) ? $settings['show_read_more'] : ''; $read_more_text = ! empty( $settings['read_more_text'] ) ? $settings['read_more_text'] : ''; $excerpt_length = ! empty( $settings['excerpt_length'] ) ? absint( $settings['excerpt_length'] ) : 25; $title_tag = ! empty( $settings['title_tag'] ) ? gridelwi_get_safe_html_tag( $settings['title_tag'] ) : 'h3'; $order = isset( $settings['order'] ) ? sanitize_key( $settings['order'] ) : 'ASC'; $orderby = isset( $settings['orderby'] ) ? sanitize_key( $settings['orderby'] ) : 'date'; $query_args = array( 'post_type' => $post_type_name, 'posts_per_page' => $posts_per_page, 'paged' => $page, 'suppress_filters' => true, 'ignore_sticky_posts' => true, 'post_status' => 'publish', 'orderby' => $orderby, 'order' => $order, ); if ( ! empty( $settings['posts_include_term_ids'] ) && is_array( $settings['posts_include_term_ids'] ) ) { $valid_term_ids = gridelwi_get_valid_term_ids( $settings['posts_include_term_ids'], $post_type_name ); $taxonomies = get_object_taxonomies( $post_type_name ); if ( ! empty( $valid_term_ids ) && ! empty( $taxonomies[0] ) ) { $query_args['tax_query'] = array( array( 'taxonomy' => $taxonomies[0], 'field' => 'term_id', 'terms' => $valid_term_ids, ), ); } } $loop = new WP_Query( $query_args ); $last_page = $loop->max_num_pages; ob_start(); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) { $loop->the_post(); ?>
>
%s', wp_kses_post( $categories_list ) ); } } ?>
ob_get_clean(), 'lastpage' => $last_page, ) ); } add_action( 'wp_ajax_nopriv_gridelwi_more_post_ajax_layout1', 'gridelwi_more_post_ajax_layout1' ); add_action( 'wp_ajax_gridelwi_more_post_ajax_layout1', 'gridelwi_more_post_ajax_layout1' ); /** * AJAX handler for other layouts. * * @return void */ function gridelwi_more_post_ajax_layout() { check_ajax_referer( 'elementor_ajax_nonce', 'nonce' ); $widget_settings = gridelwi_get_ajax_settings(); $grid_style = isset( $_POST['grid_style'] ) ? absint( wp_unslash( $_POST['grid_style'] ) ) : 1; $posts_per_page = isset( $_POST['ppp'] ) ? absint( wp_unslash( $_POST['ppp'] ) ) : 3; $page = isset( $_POST['pageNumber'] ) ? absint( wp_unslash( $_POST['pageNumber'] ) ) : 1; $post_type_name = ! empty( $widget_settings['source'] ) ? sanitize_key( $widget_settings['source'] ) : 'post'; $query_args = array( 'post_type' => $post_type_name, 'posts_per_page' => $posts_per_page, 'paged' => $page, 'suppress_filters' => true, 'ignore_sticky_posts' => true, 'post_status' => 'publish', ); if ( ! empty( $widget_settings['orderby'] ) ) { $query_args['orderby'] = sanitize_key( $widget_settings['orderby'] ); } if ( ! empty( $widget_settings['order'] ) ) { $query_args['order'] = sanitize_key( $widget_settings['order'] ); } if ( ! empty( $widget_settings['posts_include_term_ids'] ) && is_array( $widget_settings['posts_include_term_ids'] ) ) { $valid_term_ids = gridelwi_get_valid_term_ids( $widget_settings['posts_include_term_ids'], $post_type_name ); $taxonomies = get_object_taxonomies( $post_type_name ); if ( ! empty( $valid_term_ids ) && ! empty( $taxonomies[0] ) ) { $query_args['tax_query'] = array( array( 'taxonomy' => $taxonomies[0], 'field' => 'term_id', 'terms' => $valid_term_ids, ), ); } } $loop = new WP_Query( $query_args ); $last_page = $loop->max_num_pages; ob_start(); if ( $loop->have_posts() ) { $settings = $widget_settings; switch ( $grid_style ) { case 5: include GRIDELWI_PATH . 'templates/ajax_layout/layout-5.php'; break; case 4: include GRIDELWI_PATH . 'templates/ajax_layout/layout-4.php'; break; case 3: include GRIDELWI_PATH . 'templates/ajax_layout/layout-3.php'; break; case 2: include GRIDELWI_PATH . 'templates/ajax_layout/layout-2.php'; break; default: include GRIDELWI_PATH . 'templates/ajax_layout/layout-1.php'; break; } } wp_reset_postdata(); wp_send_json_success( array( 'html' => ob_get_clean(), 'lastpage' => $last_page, ) ); } add_action( 'wp_ajax_nopriv_gridelwi_more_post_ajax_layout', 'gridelwi_more_post_ajax_layout' ); add_action( 'wp_ajax_gridelwi_more_post_ajax_layout', 'gridelwi_more_post_ajax_layout' ); /** * Render thumbnail in AJAX templates. * * @param array $settings Widget settings. * @return void */ function gridelwi_render_thumbnail_ajax( $settings = array() ) { if ( empty( $settings['show_image'] ) || 'yes' !== $settings['show_image'] ) { return; } $post_thumbnail_size = ! empty( $settings['post_thumbnail_size'] ) ? sanitize_key( $settings['post_thumbnail_size'] ) : 'full'; if ( has_post_thumbnail() ) { ?>
%s', wp_kses_post( $categories_list ) ); } } ?>

admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'gridelwi_fetch_terms_by_post_type' ), ) ); } add_action( 'elementor/editor/after_enqueue_scripts', 'gridelwi_custom_elementor_editor_scripts' ); /** * Fetch terms by post type for editor control. * * @return void */ function gridelwi_handle_fetch_terms_by_post_type() { check_ajax_referer( 'gridelwi_fetch_terms_by_post_type', 'nonce' ); $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : 'post'; $selected_terms = isset( $_POST['selected_terms'] ) && is_array( $_POST['selected_terms'] ) ? array_map( 'absint', wp_unslash( $_POST['selected_terms'] ) ) : array(); $taxonomy_objects = get_object_taxonomies( $post_type, 'objects' ); $taxonomies = array(); if ( ! empty( $taxonomy_objects ) ) { foreach ( $taxonomy_objects as $taxonomy_object ) { if ( empty( $taxonomy_object->show_ui ) ) { continue; } $taxonomies[] = $taxonomy_object->name; } } if ( empty( $taxonomies ) ) { echo ''; wp_die(); } $terms = get_terms( array( 'taxonomy' => $taxonomies, 'hide_empty' => false, ) ); if ( empty( $terms ) || is_wp_error( $terms ) ) { echo ''; wp_die(); } foreach ( $terms as $term ) { printf( '', esc_attr( $term->term_id ), selected( in_array( (int) $term->term_id, $selected_terms, true ), true, false ), esc_html( $term->name ) ); } wp_die(); } add_action( 'wp_ajax_gridelwi_fetch_terms_by_post_type', 'gridelwi_handle_fetch_terms_by_post_type' );