Skip to content

Commit 5fcf515

Browse files
Add "read more" parsing to excerpt fetch
1 parent fef7d73 commit 5fcf515

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/Post.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function getFeaturedUrl($size = null)
7878
);
7979

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

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

9494
// Rebuild the url with the thumbnail image.
9595
return $imageUrl . implode('/',
96-
array_merge($imageParts, [$attachmentMetadata['sizes'][$size]['file']]));
96+
array_merge($imageParts, [$attachmentMetadata['sizes'][ $size ]['file']]));
9797
}
9898

9999
return $this->thumbnail->attachment->guid;
@@ -119,10 +119,14 @@ public function getFeaturedAlt()
119119
*
120120
* @param null $mutators
121121
*
122+
* @param null $toReadMore
123+
*
122124
* @return string
123125
*/
124-
public function getExcerpt($limit = 120, $mutators = null)
126+
public function getExcerpt($limit = 120, $mutators = null, $toReadMore = false)
125127
{
128+
$content = $this->post_content;
129+
126130
if (is_array($mutators)) {
127131
foreach ($mutators as $mutator) {
128132
if (strlen($mutator) < $limit) {
@@ -135,7 +139,11 @@ public function getExcerpt($limit = 120, $mutators = null)
135139
$limit -= strlen($mutators);
136140
}
137141

138-
return str_limit(strip_tags($this->post_content), $limit);
142+
if ($toReadMore) {
143+
$content = $this->getReadMore($content);
144+
}
145+
146+
return str_limit(strip_tags($content), $limit);
139147
}
140148

141149
/**
@@ -255,6 +263,7 @@ public function scopeHasCategories(Builder $builder, $slugs)
255263
public function scopeHasTags(Builder $builder, $slugs)
256264
{
257265
$builder->whereHas('taxonomies', function ($query) use ($slugs) {
266+
258267
$query->where('taxonomy', '=', 'post_tag')->whereHas('term', function ($query) use ($slugs) {
259268
$query->whereIn('slug', $slugs);
260269
});
@@ -318,7 +327,7 @@ public function shares()
318327
[$this->post_name]));
319328
curl_setopt_array($curl, [
320329
CURLOPT_RETURNTRANSFER => 1,
321-
CURLOPT_URL => $url
330+
CURLOPT_URL => $url,
322331
]);
323332

324333
$response = curl_exec($curl);
@@ -391,9 +400,9 @@ public function newFromBuilder($attributes = [], $connection = null)
391400
if (is_object($attributes) && isset($attributes->post_type)
392401
&& array_key_exists($attributes->post_type, static::$postTypes)
393402
) {
394-
$class = static::$postTypes[$attributes->post_type];
403+
$class = static::$postTypes[ $attributes->post_type ];
395404
} elseif (is_array($attributes) && array_key_exists($attributes['post_type'], static::$postTypes)) {
396-
$class = static::$postTypes[$attributes['post_type']];
405+
$class = static::$postTypes[ $attributes['post_type'] ];
397406
} else {
398407
$class = get_called_class();
399408
}
@@ -406,4 +415,14 @@ public function newFromBuilder($attributes = [], $connection = null)
406415

407416
return $model;
408417
}
418+
419+
/**
420+
* Gets the posts content up to the read more tag
421+
*
422+
* @param $content
423+
*/
424+
private function getReadMore($content)
425+
{
426+
return explode('<!--more-->', $content)[0];
427+
}
409428
}

0 commit comments

Comments
 (0)