Skip to content

Commit

Permalink
Merge branch 'release/4.1.6'
Browse files Browse the repository at this point in the history
* release/4.1.6:
  Fixed string helper deprecations and added more context to the view methods
  Fixed the padding on embedded image captions
  • Loading branch information
austintoddj committed Mar 2, 2019
2 parents 4e71ecc + 8f45952 commit 473c5d3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/CanvasServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Canvas\Console\InstallCommand;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Container\BindingResolutionException;

class CanvasServiceProvider extends ServiceProvider
{
Expand All @@ -16,6 +17,7 @@ class CanvasServiceProvider extends ServiceProvider
* Bootstrap any package services.
*
* @return void
* @throws BindingResolutionException
*/
public function boot()
{
Expand Down Expand Up @@ -44,6 +46,7 @@ public function register()
* Register the events and listeners.
*
* @return void
* @throws BindingResolutionException
*/
private function registerEvents()
{
Expand Down
49 changes: 40 additions & 9 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Str;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -143,10 +144,13 @@ public function getPublishedAttribute($value): bool
*/
public function getReadTimeAttribute($value): string
{
// Only count words in our estimation
$words = str_word_count(strip_tags($this->body));
$minutes = ceil($words / 200);

return sprintf('%s %s %s', $minutes, str_plural(' min', $minutes), ' read');
// Divide by the average number of words per minute
$minutes = ceil($words / 250);

return sprintf('%s %s %s', $minutes, Str::plural(' min', $minutes), ' read');
}

/**
Expand All @@ -157,29 +161,42 @@ public function getReadTimeAttribute($value): string
*/
public function getPopularReadingTimesAttribute($value): array
{
// Get the views associated with the post
$data = View::where('post_id', $this->id)->get();

// Filter the view data to only include hours:minutes
$collection = collect();
$data->each(function ($item, $key) use ($collection) {
$collection->push($item->created_at->minute(0)->format('H:i'));
});

// Count the unique values and assign to their respective keys
$filtered = array_count_values($collection->toArray());
$popular_reading_times = collect();

$popular_reading_times = collect();
foreach ($filtered as $key => $value) {

// Use each given time to create a 60 min range
$start_time = Carbon::createFromTimeString($key);
$end_time = $start_time->copy()->addMinutes(60);

// Find the percentage based on the value
$percentage = round($value / $data->count() * 100);

// Get a human-readable hour range and floating percentage
$popular_reading_times->put(sprintf('%s - %s', $start_time->format('g:i A'), $end_time->format('g:i A')), $percentage);
}

// Cast the collection to an array
$array = $popular_reading_times->toArray();
$sorted = array_slice($array, 0, 5, true);
arsort($sorted);

return $sorted;
// Only return the top 5 reading times and percentages
$sliced = array_slice($array, 0, 5, true);

// Sort the array in a descending fashion
arsort($sliced);

return $sliced;
}

/**
Expand All @@ -190,18 +207,25 @@ public function getPopularReadingTimesAttribute($value): array
*/
public function getTopReferersAttribute($value): array
{
// Get the views associated with the post
$data = $this->views;

// Filter the view data to only include referrers
$collection = collect();
$data->each(function ($item, $key) use ($collection) {
is_null($item->referer) ? $collection->push('Other') : $collection->push(parse_url($item->referer)['host']);
});

// Count the unique values and assign to their respective keys
$array = array_count_values($collection->toArray());
$sorted = array_slice($array, 0, 10, true);
arsort($sorted);

return $sorted;
// Only return the top 10 referrers with their view count
$sliced = array_slice($array, 0, 10, true);

// Sort the array in a descending fashion
arsort($sliced);

return $sliced;
}

/**
Expand All @@ -212,26 +236,33 @@ public function getTopReferersAttribute($value): array
*/
public function getViewTrendAttribute($value): array
{
// Get the views associated with the post
$data = $this->views;

// Filter views to only include the last 30 days
$filtered = $data->filter(function ($value, $key) {
return $value->created_at >= now()->subDays(30);
});

// Filter the view data to only include created_at time strings
$collection = collect();
$filtered->sortBy('created_at')->each(function ($item, $key) use ($collection) {
$collection->push($item->created_at->toDateString());
});

// Count the unique values and assign to their respective keys
$views = array_count_values($collection->toArray());

// Create a 30 day range to hold the default date values
$period = CarbonPeriod::create(now()->subDays(30)->toDateString(), 30)->excludeStartDate();

// Prep the array to perform a comparison with the actual view data
$range = collect();
foreach ($period as $key => $date) {
$range->push($date->toDateString());
}

// Compare the view data and date range arrays, assigning view counts where applicable
$total = collect();
foreach ($range as $date) {
if (array_key_exists($date, $views)) {
Expand Down
12 changes: 6 additions & 6 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ public function post(): BelongsTo
*/
public static function viewTrend(Collection $views): array
{
// Get all the views for the last [X] days
// Filter views to only include the last [X] days
$filtered = $views->filter(function ($value, $key) {
return $value->created_at >= now()->subDays(self::DAYS_PRIOR);
});

// Sort the collection by created dates
// Filter the view data to only include created_at time strings
$collection = collect();
$filtered->sortBy('created_at')->each(function ($item, $key) use ($collection) {
$collection->push($item->created_at->toDateString());
});

// Count the views and assign the key/value pairs
// Count the unique values and assign to their respective keys
$views = array_count_values($collection->toArray());

// Create a [X] day period
// Create a [X] day range to hold the default date values
$period = CarbonPeriod::create(now()->subDays(self::DAYS_PRIOR)->toDateString(), self::DAYS_PRIOR)->excludeStartDate();

// Build a collection of dates for each day in the period
// Prep the array to perform a comparison with the actual view data
$range = collect();
foreach ($period as $key => $date) {
$range->push($date->toDateString());
}

// Compare the view data and date range arrays, assigning view counts where applicable
$total = collect();
// Compare the collections and assign matching dates with their views
foreach ($range as $date) {
if (array_key_exists($date, $views)) {
$total->put($date, $views[$date]);
Expand Down
1 change: 0 additions & 1 deletion stubs/views/blog/partials/styles.stub
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@
div.embedded_image p {
text-align: center !important;
color: #6c757d !important;
padding-top: 1rem !important;
font-size: 0.9rem !important;
line-height: 1.6 !important;
font-weight: 400 !important;
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
use Authorizable, Authenticatable;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';

/**
* The attributes that are mass assignable.
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
protected $guarded = [];

/**
* @var bool
Expand Down

0 comments on commit 473c5d3

Please sign in to comment.