return '';
}
}
// Posts: title, comments_count, date, permalink, post_id, counter
$results = $wpdb->get_results(
$wpdb->prepare( "
SELECT p.*, COUNT(tr.object_id) AS counter {$select_excerpt} {$select_gp_concat}
FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->term_relationships} AS tr ON (p.ID = tr.object_id)
INNER JOIN {$wpdb->term_taxonomy} AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
WHERE 1 = 1
AND (tt.taxonomy = '{$taxonomy}' AND tt.term_id IN ({$term_list}))
{$exclude_posts_sql}
AND p.post_status = 'publish'
AND p.post_date_gmt < '" . current_time( 'mysql' ) . "'
{$limit_days_sql}
{$restrict_sql}
GROUP BY tr.object_id
ORDER BY {$order_by}
LIMIT 0, %d",
$limit_number
) );
if (!$cache) {
$cache = [];
}
$cache[ $key ] = $results;
wp_cache_set( 'related_posts' . $taxonomy, $cache, 'simple-tags' );
}
if ( $format == 'object' || $format == 'array' ) {
return $results;
} elseif ( $results === false || empty( $results ) ) {
if((int)$hide_output === 0){
return SimpleTags_Client::output_content( 'st-related-posts', $format, $title, $nopoststext, $copyright, '', $wrap_class, $link_class );
} else {
return '';
}
}
if ( empty( $dateformat ) ) {
$dateformat = get_option( 'date_format' );
}
$output = array();
//update xformat with class link class
if(!empty(trim($link_class))){
$link_class = taxopress_format_class($link_class);
$xformat = taxopress_add_class_to_format($xformat, $link_class);
}
// Replace placeholders
foreach ( (array) $results as $result ) {
if ( ( $min_shared > 1 && ( count( explode( ',', $result->terms_id ) ) < $min_shared ) ) || ! is_object( $result ) ) {
continue;
}
$element_loop = $xformat;
$post_title = apply_filters( 'the_title', $result->post_title, $result->ID );
$element_loop = str_replace( '%post_date%', mysql2date( $dateformat, $result->post_date ), $element_loop );
$element_loop = str_replace( '%post_permalink%', get_permalink( $result ), $element_loop );
$element_loop = str_replace( '%post_title%', $post_title, $element_loop );
$element_loop = str_replace( '%post_title_attribute%', esc_html( strip_tags( $post_title ) ), $element_loop );
$element_loop = str_replace( '%post_comment%', (int) $result->comment_count, $element_loop );
$element_loop = str_replace( '%post_tagcount%', (int) $result->counter, $element_loop );
$element_loop = str_replace( '%post_id%', $result->ID, $element_loop );
if ( isset( $result->terms_id ) ) {
$element_loop = str_replace( '%post_relatedtags%', self::get_tags_from_id( $result->terms_id, $taxonomy ), $element_loop, $link_class );
}
if ( isset( $result->post_excerpt ) || isset( $result->post_content ) ) {
$element_loop = str_replace( '%post_excerpt%', self::get_excerpt_post( $result->post_excerpt, $result->post_content, $result->post_password, $excerpt_wrap ), $element_loop );
}
$output[] = $element_loop;
}
return SimpleTags_Client::output_content( 'st-related-posts', $format, $title, $output, $copyright, $separator, $wrap_class, $link_class, $before, $after );
}
/**
* Build excerpt from post data with specific lenght
*
* @param string $excerpt
* @param string $content
* @param string $password
* @param integer $excerpt_length
*
* @return string
* @author WebFactory Ltd
*/
public static function get_excerpt_post( $excerpt = '', $content = '', $password = '', $excerpt_length = 55 ) {
if ( ! empty( $password ) ) { // if there's a password
// phpcs:ignore WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___COOKIE
if ( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] != $password ) { // and it doesn't match the cookie
return __( 'There is no excerpt because this is a protected post.', 'simple-tags' );
}
}
if ( ! empty( $excerpt ) ) {
return apply_filters( 'get_the_excerpt', $excerpt );
} else { // Fake excerpt
$content = str_replace( ']]>', ']]>', $content );
$content = strip_tags( $content );
$excerpt_length = (int) $excerpt_length;
if ( 0 === $excerpt_length ) {
$excerpt_length = 55;
}
$words = explode( ' ', $content, $excerpt_length + 1 );
if ( count( $words ) > $excerpt_length ) {
array_pop( $words );
array_push( $words, '[...]' );
$content = implode( ' ', $words );
}
return $content;
}
}
/**
* Get and format tags from list ID (SQL Group Concat)
*
* @param string $terms
* @param string $taxonomy
*
* @return string
* @author WebFactory Ltd
*/
public static function get_tags_from_id( $terms = '', $taxonomy = 'post_tag' ) {
if ( empty( $terms ) ) {
return '';
}
// Get tags since Term ID.
$terms = (array) get_terms( $taxonomy, 'include=' . $terms );
if ( empty( $terms ) ) {
return '';
}
// HTML Rel (tag)
$rel = SimpleTags_Client::get_rel_attribut();
$output = array();
foreach ( (array) $terms as $term ) {
$link = get_term_link( $term->term_id, $term->taxonomy );
if ( empty( $link ) || is_wp_error( $link ) ) {
continue;
}
$output[] = '' . esc_html( $term->name ) . '';
}
return implode( ', ', $output );
}
}