set_post( $_post );
$this->set_config();
}
function get_config_default() {
$args = array(
'excerpt_type' => 'custom',
'excerpt_length' => StarterBlog()->get_setting( 'blog_post_excerpt_length' ),
'excerpt_more' => null,
'thumbnail_size' => StarterBlog()->get_setting( 'blog_post_thumb_size' ),
'meta_config' => StarterBlog()->get_setting( 'blog_post_meta' ),
'meta_sep' => _x( '-', 'post meta separator', 'starter-blog' ),
'more_text' => null,
'more_display' => 1,
'term_sep' => _x( ',', 'post term separator', 'starter-blog' ),
'term_count' => 1,
'tax' => 'category',
'title_tag' => 'h2',
'title_link' => 1,
'author_avatar' => StarterBlog()->get_setting( 'blog_post_author_avatar' ),
'avatar_size' => 32,
);
$size = StarterBlog()->get_setting( 'blog_post_avatar_size' );
if ( is_array( $size ) && isset( $size['value'] ) ) {
$args['avatar_size'] = absint( $size['value'] );
}
return $args;
}
/**
* Set config
*
* @param null $config
*/
function set_config( $config = null ) {
if ( ! is_array( $config ) ) {
$config = array();
}
$config = wp_parse_args( $config, $this->get_config_default() );
$this->config = $config;
}
/**
* Reset config
*/
function reset_config() {
$this->config = $this->get_config_default();
}
/**
* Set post data
*
* @param null $_post
*/
function set_post( $_post = null ) {
if ( ! $_post ) {
global $post;
$_post = get_post();
}
if ( is_array( $_post ) ) {
$_post = (object) $_post;
}
$this->post = $_post;
}
/**
* Main instance
*
* @return StarterBlog_Post_Entry
*/
static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Trim the excerpt with custom length
*
* @see wp_trim_excerpt
*
* @param string $text
* @param int|bool $excerpt_length
* @return string
*/
function trim_excerpt( $text, $excerpt_length = null ) {
$text = strip_shortcodes( $text );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]>', $text );
if ( ! $excerpt_length ) {
/**
* Filters the number of words in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
}
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
* @param string $more_string The string shown within the more link.
*/
if ( ! $this->config['excerpt_more'] ) {
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '…' );
} else {
$excerpt_more = $this->config['excerpt_more'];
}
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
return $text;
}
/**
* Get meta date markup
*
* @return string
*/
function meta_date() {
$icon = ' ';
$time_string = '%2$s ';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '%2$s ';
}
$time_string = sprintf(
$time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = '' . $icon . $time_string . ' ';
return '' . $posted_on . ' ';
}
/**
* Get terms array
*
* @param string $id Post ID.
* @param string $taxonomy Name of taxonomy.
* @param bool $icon_first
* @return array|bool|WP_Error
*/
function get_terms_list( $id, $taxonomy, $icon_first = false ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) ) {
return $terms;
}
if ( empty( $terms ) ) {
return false;
}
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
$prm_term_id = $this->get_primary_term_id( $id, $taxonomy );
$prm_term = get_term( $prm_term_id, $taxonomy );
$prm_term_arr[] = $prm_term;
// Make the primary term be the first term in the terms array.
foreach ( $terms as $index => $term ) {
if ( $prm_term_id == $term->term_id ) {
unset( $terms[ $index ] );
break;
}
}
$terms = array_merge( $prm_term_arr, $terms );
}
$links = array();
$icon = ' ';
foreach ( $terms as $index => $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) ) {
return $link;
}
if ( $icon_first && 0 == $index ) { // phpcs:ignore
} else {
$icon = '';
}
$links[] = '' . $icon . esc_html( $term->name ) . ' ';
}
return $links;
}
/**
* Get primary term ID.
*
* The primary term is either the first term or the primary term set via YOAST SEO Plugin.
*
* @param int $post_id Post ID.
* @param string $taxonomy Name of taxonomy.
* @return int|false Primary or first term ID. False if no term is set.
*/
function get_primary_term_id( $post_id, $taxonomy ) {
$prm_term = '';
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
$wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post_id );
$prm_term = $wpseo_primary_term->get_primary_term();
}
if ( ! is_object( $wpseo_primary_term ) || empty( $prm_term ) ) {
$term = wp_get_post_terms( $post_id, $taxonomy );
if ( isset( $term ) && ! empty( $term ) ) {
return $term[0]->term_id;
} else {
return '';
}
}
return $wpseo_primary_term->get_primary_term();
}
/**
* Get first category markup
*
* @return string
*/
function meta_categories() {
$html = '';
if ( get_post_type() === $this->post_type ) {
/* translators: used between list items, there is a space after the comma */
$categories_list = $this->get_terms_list( $this->get_post_id(), $this->config['tax'], true );
if ( is_array( $categories_list ) && $this->config['term_count'] > 0 ) {
$categories_list = array_slice( $categories_list, 0, $this->config['term_count'] );
}
$html .= sprintf( '%1$s ', join( $this->config['term_sep'], $categories_list ) ); // WPCS: XSS OK.
}
return $html;
}
/**
* Get Tags list markup
*
* @return string
*/
function meta_tags() {
$html = '';
if ( 'post' === get_post_type() ) {
/* translators: used between list items, there is a space after the comma */
$tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator', 'starter-blog' ) );
if ( $tags_list ) {
$html .= sprintf( '%1$s ', $tags_list ); // WPCS: XSS OK.
}
}
return $html;
}
/**
* Get tags list markup
*/
function post_tags() {
$html = '';
if ( 'post' === get_post_type() ) {
/* translators: used between list items, there is a space after the comma */
$tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator', 'starter-blog' ) );
if ( $tags_list ) {
$html .= sprintf( '
' . esc_html__( 'Tagged ', 'starter-blog' ) . '%1$s
', $tags_list ); // WPCS: XSS OK.
}
}
echo $html;
}
/**
* Get categories list markup
*/
function post_categories() {
$html = '';
if ( 'post' === get_post_type() ) {
/* translators: used between list items, there is a space after the comma */
$list = get_the_category_list( esc_html_x( ', ', 'list item separator', 'starter-blog' ) );
if ( $list ) {
$html .= sprintf( '' . esc_html__( 'Posted in ', 'starter-blog' ) . '%1$s
', $list ); // WPCS: XSS OK.
}
}
echo $html;
}
/**
* Get comment number markup
*
* @return string
*/
function meta_comment() {
$html = '';
if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
$icon = ' ';
$comment_count = get_comments_number();
$html .= '';
}
return $html;
}
/**
* Get author markup
*
* @return string
*/
function meta_author() {
if ( $this->config['author_avatar'] ) {
$avatar = get_avatar( get_the_author_meta( 'ID' ), $this->config['avatar_size'] );
} else {
$avatar = ' ';
}
$byline = '' . $avatar . esc_html( get_the_author() ) . ' ';
return ' ' . $byline . ' '; // WPCS: XSS OK.
}
/**
* Check if show post meta for this post
*
* @param object|integer $post
*
* @return boolean
*/
private function show_post_meta( $post ) {
return apply_filters( 'starterblog/show/post_meta', get_post_type( $post ) == 'post' && ! is_search() );
}
/**
* Get post meta markup
*
* @param object|integer $post
* @param array $meta_fields
* @param array $args
*/
function post_meta( $post = null, $meta_fields = array(), $args = array() ) {
if ( ! $this->show_post_meta( $post ) ) {
return;
}
if ( empty( $meta_fields ) ) {
$meta_fields = $this->config['meta_config'];
}
$metas = array();
foreach ( (array) $meta_fields as $item ) {
$item = wp_parse_args(
$item,
array(
'_key' => '',
'_visibility' => '',
)
);
if ( 'hidden' !== $item['_visibility'] ) {
if ( method_exists( $this, 'meta_' . $item['_key'] ) ) {
$s = call_user_func_array( array( $this, 'meta_' . $item['_key'] ), array( $this->post, $args ) );
if ( $s ) {
$metas[ $item['_key'] ] = $s;
}
}
}
}
if ( ! empty( $metas ) ) {
?>
' . $this->config['meta_sep'] . '', $metas );
?>
', '' );
}
} else {
if ( $this->config['title_link'] ) {
the_title( '<' . $this->config['title_tag'] . ' class="entry-title entry--item">', ' ' . $this->config['title_tag'] . '>' );
} else {
the_title( '<' . $this->config['title_tag'] . ' class="entry-title entry--item">', '' . $this->config['title_tag'] . '>' );
}
}
}
function get_post_id( $post = null ) {
if ( is_object( $post ) ) {
return $post->ID;
} elseif ( is_array( $post ) ) {
return $post['ID'];
} elseif ( is_numeric( $post ) ) {
return $post;
} else {
return get_the_ID();
}
}
/**
* Get first category markup
*
* @param null|WP_Post|int $post
*/
function post_category( $post = null ) {
$html = '';
if ( get_post_type() === $this->post_type ) {
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_term_list( $this->get_post_id( $post ), $this->config['tax'], '', '__cate_sep__' );
if ( $categories_list && ! is_wp_error( $categories_list ) ) {
$categories_list = explode( '__cate_sep__', $categories_list );
if ( $this->config['term_count'] > 0 ) {
$categories_list = array_slice( $categories_list, 0, $this->config['term_count'] );
}
$html .= sprintf( '%1$s ', join( $this->config['term_sep'], $categories_list ) ); // WPCS: XSS OK.
}
}
echo $html;
}
/**
* Post thumbnail markup
*
* @param null|WP_Post|int $post
*/
function post_thumbnail( $post = null ) {
if ( is_single() && ! is_front_page() && ! is_home() ) {
if ( has_post_thumbnail() ) {
?>
config['thumbnail_size'] ); ?>
config['thumbnail_size'] ); ?>
config['excerpt_type'];
}
if ( ! $length ) {
$length = $this->config['excerpt_length'];
}
echo '';
if ( 'excerpt' == $type ) {
the_excerpt();
} elseif ( 'more_tag' == $type ) {
the_content( '', true );
} elseif ( 'content' == $type ) {
the_content( '', false );
} else {
$text = '';
if ( $this->post ) {
if ( '' != get_the_excerpt() ) {
$text = get_the_excerpt();
} elseif ( $this->post->post_excerpt ) {
$text = $this->post->post_excerpt;
} else {
$text = $this->post->post_content;
}
}
$excerpt = $this->trim_excerpt( $text, $length );
if ( $excerpt ) {
// WPCS: XSS OK.
echo apply_filters( 'the_excerpt', $excerpt );
} else {
the_excerpt();
}
}
echo '
';
}
/**
* Post content markup
*/
function post_content() {
?>
post_pagination();
?>
config['more_display'] ) {
return;
}
$more = $this->config['more_text'];
if ( ! $more ) {
if ( ! is_rtl() ) {
$more = __( 'Read more →', 'starter-blog' );
} else {
$more = __( 'Read more ←', 'starter-blog' );
}
}
?>
';
comments_template();
echo '';
endif;
}
}
function post_author_bio() {
if ( ! is_singular( 'post' ) ) {
return;
}
if ( is_single() ) {
global $post;
// Detect if it is a single post with a post author.
if ( is_single() && isset( $post->post_author ) ) {
$user = get_user_by( 'ID', $post->post_author );
if ( ! $user ) {
return;
}
$display_name = $user->display_name ? $user->display_name : $user->user_login;
// Get author's biographical information or description.
$user_description = get_the_author_meta( 'user_description', $user->ID );
// Get author's website URL.
$user_website = get_the_author_meta( 'url', $user->ID );
// Get link to the author archive page.
$user_posts = get_author_posts_url( get_the_author_meta( 'ID', $user->ID ) );
if ( ! empty( $display_name ) ) {
$author_details = '' . sprintf( __( 'About the Author: %s ', 'starter-blog' ), $display_name ) . ' ';
}
$user_description = wptexturize( $user_description );
$user_description = wpautop( $user_description );
$user_description = convert_smilies( $user_description );
$author_links = '' . sprintf( 'View all post by %s', $display_name ) . ' ';
// Check if author has a website in their profile.
if ( ! empty( $user_website ) ) {
// Display author website link.
$author_links .= ' | Website
';
} else {
// if there is no author website then just close the paragraph.
$author_links .= '';
}
$author_details .= '' . get_avatar( get_the_author_meta( 'user_email' ), 80 ) . '
' . $user_description . '
' . $author_links . '
';
// Pass all this info to post content.
$content = '' . $author_details . '
';
}
echo $content;
}
}
function post_navigation() {
if ( ! is_single() ) {
return '';
}
if ( get_post_type() != 'post' ) {
return '';
}
echo '';
the_post_navigation(
array(
'next_text' => ''
. __( 'Next', 'starter-blog' ) . ' '
. '' . __( 'Next post:', 'starter-blog' ) . ' '
. '%title ',
'prev_text' => ''
. __( 'Previous', 'starter-blog' ) . ' '
. '' . __( 'Previous post:', 'starter-blog' ) . ' '
. '%title ',
)
);
echo '
';
}
/**
* Post pagination markup
*/
function post_pagination() {
if ( ! is_single() ) {
return '';
}
wp_link_pages(
array(
'before' => '' . esc_html__( 'Pages:', 'starter-blog' ),
'after' => '
',
)
);
}
/**
* Display related post
*/
function post_related() {
if ( ! is_single() ) {
return '';
}
if ( get_post_type() != 'post' ) {
return '';
}
StarterBlog_Related_Posts::get_instance()->display();
}
/**
* Build item markup width field config
*
* @param string $field Field settings.
* @param WP_Post|null|int $post
* @param array $fields
* @param array $args
*/
function build( $field, $post = null, $fields = null, $args = array() ) {
// Allowed 3rd party hook to this.
$cb = apply_filters( 'starterblog/single/build_field_callback', false, $field );
if ( ! is_callable( $cb ) ) {
if ( method_exists( $this, 'post_' . $field ) ) {
$cb = array( $this, 'post_' . $field );
}
}
$type = is_single() ? 'single' : 'loop';
/**
* Hook before post item part
*
* @since 0.2.2
*/
do_action( "starterblog/{$type}/field_{$field}/before", $post, $fields, $args, $this );
if ( is_callable( $cb ) ) {
call_user_func_array( $cb, array( $post, $fields, $args ) );
}
/**
* Hook after post item part
*
* @since 0.2.2
*/
do_action( "starterblog/{$type}/field_{$field}/after", $post, $fields, $args, $this );
}
/**
* Build item markup width fields config
*
* @param array $fields
* @param WP_Post|null|int $post
* @param array $args
*/
function build_fields( $fields, $post = null, $args = array() ) {
foreach ( (array) $fields as $item ) {
$item = wp_parse_args(
$item,
array(
'_key' => '',
'_visibility' => '',
'fields' => null,
)
);
if ( 'hidden' !== $item['_visibility'] ) {
$this->build( $item['_key'], $post, $item['fields'], $args );
}
}
}
}