Skip to content

Commit

Permalink
Add "read more" parsing to excerpt fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
fullstackfool authored May 11, 2017
1 parent fef7d73 commit 5fcf515
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getFeaturedUrl($size = null)
);

//get the resized filename, otherwise serve the full size url
if ( ! isset($attachmentMetadata['sizes'][$size]['file'])) {
if ( ! isset($attachmentMetadata['sizes'][ $size ]['file'])) {
return $this->thumbnail->attachment->guid;
}

Expand All @@ -93,7 +93,7 @@ public function getFeaturedUrl($size = null)

// Rebuild the url with the thumbnail image.
return $imageUrl . implode('/',
array_merge($imageParts, [$attachmentMetadata['sizes'][$size]['file']]));
array_merge($imageParts, [$attachmentMetadata['sizes'][ $size ]['file']]));
}

return $this->thumbnail->attachment->guid;
Expand All @@ -119,10 +119,14 @@ public function getFeaturedAlt()
*
* @param null $mutators
*
* @param null $toReadMore
*
* @return string
*/
public function getExcerpt($limit = 120, $mutators = null)
public function getExcerpt($limit = 120, $mutators = null, $toReadMore = false)
{
$content = $this->post_content;

if (is_array($mutators)) {
foreach ($mutators as $mutator) {
if (strlen($mutator) < $limit) {
Expand All @@ -135,7 +139,11 @@ public function getExcerpt($limit = 120, $mutators = null)
$limit -= strlen($mutators);
}

return str_limit(strip_tags($this->post_content), $limit);
if ($toReadMore) {
$content = $this->getReadMore($content);
}

return str_limit(strip_tags($content), $limit);
}

/**
Expand Down Expand Up @@ -255,6 +263,7 @@ public function scopeHasCategories(Builder $builder, $slugs)
public function scopeHasTags(Builder $builder, $slugs)
{
$builder->whereHas('taxonomies', function ($query) use ($slugs) {

$query->where('taxonomy', '=', 'post_tag')->whereHas('term', function ($query) use ($slugs) {
$query->whereIn('slug', $slugs);
});
Expand Down Expand Up @@ -318,7 +327,7 @@ public function shares()
[$this->post_name]));
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
CURLOPT_URL => $url,
]);

$response = curl_exec($curl);
Expand Down Expand Up @@ -391,9 +400,9 @@ public function newFromBuilder($attributes = [], $connection = null)
if (is_object($attributes) && isset($attributes->post_type)
&& array_key_exists($attributes->post_type, static::$postTypes)
) {
$class = static::$postTypes[$attributes->post_type];
$class = static::$postTypes[ $attributes->post_type ];
} elseif (is_array($attributes) && array_key_exists($attributes['post_type'], static::$postTypes)) {
$class = static::$postTypes[$attributes['post_type']];
$class = static::$postTypes[ $attributes['post_type'] ];
} else {
$class = get_called_class();
}
Expand All @@ -406,4 +415,14 @@ public function newFromBuilder($attributes = [], $connection = null)

return $model;
}

/**
* Gets the posts content up to the read more tag
*
* @param $content
*/
private function getReadMore($content)
{
return explode('<!--more-->', $content)[0];
}
}

0 comments on commit 5fcf515

Please sign in to comment.