'[image-url]', $url, $str); } // Featured image thumbnail if(stripos($str, '[thumbnail]') !== false) { $markup = $this->getFeaturedImage( $this->post->ID, 'thumbnail' ); $str = str_replace('[thumbnail]', $markup, $str); } // Featured image thumbnail URL if(stripos($str, '[thumbnail-url]') !== false) { $url = $this->getFeaturedImageURL( $this->post->ID, 'thumbnail' ); $str = str_replace('[thumbnail-url]', $url, $str); } // Title if(stripos($str, '[title]') !== false) { $str = str_replace('[title]', $this->getTitle($textlength), $str); } // Content if(stripos($str, '[content]') !== false) { $str = str_replace('[content]', $this->getContent($textlength), $str); } // Excerpt if(stripos($str, '[excerpt]') !== false) { $str = str_replace('[excerpt]', $this->getExcerpt($textlength), $str); } // Author nickname if(stripos($str, '[author]') !== false) { $str = str_replace('[author]', $this->getAuthor(true), $str); } // Author display name if(stripos($str, '[author-name]') !== false) { $str = str_replace('[author-name]', $this->getAuthor(false), $str); } // Author avatar image if(stripos($str, '[author-avatar]') !== false) { $str = str_replace('[author-avatar]', $this->getAuthorImage( $this->post ), $str); } // Author ID if(stripos($str, '[author-id]') !== false) { $str = str_replace('[author-id]', $this->post->post_author, $str); } // Category list if(stripos($str, '[categories]') !== false) { $str = str_replace('[categories]', $this->getCategoryList(), $str); } // Tags list if(stripos($str, '[tags]') !== false) { $str = str_replace('[tags]', $this->getTagList(), $str); } // Number of comments if(stripos($str, '[comments]') !== false) { $str = str_replace('[comments]', $this->post->comment_count, $str); } // Meta if(stripos($str, '[meta:') !== false) { $matches = array(); preg_match_all('/\[meta:\w(?:[-\w]*\w)?]/', $str, $matches); foreach($matches[0] as $match) { $meta = str_replace('[meta:', '', $match); $meta = str_replace(']', '', $meta); $meta = get_post_meta($this->post->ID, $meta, true); $str = str_replace($match, $meta, $str); } } return $str; } /** * Returns the lastly selected post's title * @return string The title of the post */ public function getTitle($length = 0) { if(!is_object($this->post)) { return false; } $title = $this->post->post_title; if( ! empty( $length ) ) { if( function_exists('mb_substr') ) { $title = mb_substr($title, 0, $length); } else { $title = substr($title, 0, $length); } } return $title; } /** * Returns the lastly selected post's excerpt * @return string The excerpt of the post */ public function getExcerpt($textlength = 0) { global $post; $post = $this->post; setup_postdata($post); $excerpt = get_the_excerpt(); wp_reset_postdata(); if( ! empty( $excerpt ) && ! empty( $textlength ) ) { if( function_exists('mb_substr') ) { $excerpt = mb_substr( $excerpt, 0, $textlength ); } else { $excerpt = substr( $excerpt, 0, $textlength ); } } return $excerpt; } public function getAuthor($nick = true) { $key = $nick ? 'user_nicename' : 'display_name'; if(is_object($this->post)) { return get_userdata($this->post->post_author)->$key; } else { return false; } } public function getAuthorImage( $post = null ) { if( ! empty( $post ) ) { $post = $this->post; } if( function_exists( 'get_avatar_url' ) ) { return ''; } return ''; } public function getCategoryList( $post = null ) { if(!empty($post)) { $post = $this->post; } if(has_category(false, $this->post->ID)) { $cats = wp_get_post_categories($this->post->ID); foreach($cats as $val) { $cat = get_category($val); $list[] = ''.$cat->name.''; } return '
'.implode(', ', $list).'
'; } else { return ''; } } public function getTagList( $post = null ) { if(!empty($post)) { $post = $this->post; } if(has_tag(false, $this->post->ID)) { $tags = wp_get_post_tags($this->post->ID); foreach($tags as $val) { $list[] = ''.$val->name.''; } return '
'.implode(', ', $list).'
'; } else { return ''; } } /** * Returns a subset of the post's content, * or the first paragraph if isn't specified * @param integer $length The subset's length * @return string The content */ public function getContent( $length = false ) { if( ! is_object( $this->post ) ) { return false; } $content = $this->post->post_content; if( ! empty( $length ) ) { if( function_exists( 'mb_substr' ) ) { $content = mb_substr( wp_strip_all_tags( $content ), 0, $length); } else { $content = substr( wp_strip_all_tags( $content ), 0, $length); } } return nl2br($content); } /** * Returns the featured image URL for the specified post ID. * Defaults to an empty GIF on error. * * @param integer $postID The ID of the post * @param string $size Attachment image size * @return string Featured image URL */ public function getFeaturedImageURL( $postID = 0, $size = 'full' ) { if( function_exists('get_post_thumbnail_id') ) { $attachmentID = get_post_thumbnail_id( $postID ); $attachment = wp_get_attachment_image_src( $attachmentID, $size ); if( ! empty( $attachment[0] ) ) { return $attachment[0]; } } return LS_ROOT_URL . '/static/admin/img/blank.gif'; } /** * Returns the featured image HTML element markup for the specified post ID. * Defaults to empty string on error. * * @param integer $postID The ID of the post * @param string $size Attachment image size * @return string HTML markup or empty string on error */ public function getFeaturedImage( $postID = 0, $size = 'full' ) { if( function_exists('get_post_thumbnail_id') ) { $attachmentID = get_post_thumbnail_id( $postID ); $attachment = wp_get_attachment_image( $attachmentID, $size ); return $attachment; } return ''; } }