Skip to content

Commit

Permalink
Merge pull request #109 from Emilia-Capital/refactor/content-widget
Browse files Browse the repository at this point in the history
Refactor the content widget
  • Loading branch information
aristath authored Nov 12, 2024
2 parents fa24a25 + ea1ad23 commit d3a4fb1
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 356 deletions.
5 changes: 0 additions & 5 deletions assets/css/page-widgets/published-content-density.css

This file was deleted.

5 changes: 0 additions & 5 deletions assets/css/page-widgets/published-words.css

This file was deleted.

12 changes: 0 additions & 12 deletions assets/js/grid-masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ const prplResizeGridItem = ( item ) => {
( elHeight + paddingTop + paddingBottom ) / rowHeight
);
item.style.gridRowEnd = 'span ' + ( rowSpan + 1 );

// If this is the last item and taller than the 2 previous items, move it to the 1st column.
if (
item.nextElementSibling === null &&
item.getBoundingClientRect().height >
item.previousElementSibling.getBoundingClientRect().height &&
item.getBoundingClientRect().height >
item.previousElementSibling.previousElementSibling.getBoundingClientRect()
.height
) {
item.style.gridColumn = '1';
}
};

/**
Expand Down
4 changes: 1 addition & 3 deletions classes/admin/class-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ public function get_widgets() {
\progress_planner()->get_widgets__activity_scores(),
\progress_planner()->get_widgets__suggested_tasks(),
\progress_planner()->get_widgets__todo(),
\progress_planner()->get_widgets__badge_streak(),
\progress_planner()->get_widgets__latest_badge(),
\progress_planner()->get_widgets__published_content_density(),
\progress_planner()->get_widgets__published_words(),
\progress_planner()->get_widgets__badge_streak(),
\progress_planner()->get_widgets__published_content(),
\progress_planner()->get_widgets__whats_new(),
];
Expand Down
145 changes: 0 additions & 145 deletions classes/widgets/class-published-content-density.php

This file was deleted.

138 changes: 137 additions & 1 deletion classes/widgets/class-published-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ final class Published_Content extends \Progress_Planner\Widget {
* @return array The stats.
*/
public function get_stats() {
static $stats;
if ( null !== $stats ) {
return $stats;
}
$post_types = \progress_planner()->get_activities__content_helpers()->get_post_types_names();
$weekly = [];
$all = [];
Expand All @@ -48,10 +52,41 @@ public function get_stats() {
$all[ $post_type ] = \wp_count_posts( $post_type )->publish;
}

return [
$stats = [
'weekly' => $weekly,
'all' => $all,
];
return $stats;
}

/**
* Get the chart args.
*
* @return array The chart args.
*/
public function get_chart_args_content_density() {
return array_merge(
$this->get_chart_args(),
[
'count_callback' => [ $this, 'count_density' ],
]
);
}

/**
* Get the chart args.
*
* @return array The chart args.
*/
public function get_chart_args_content_count() {
return array_merge(
$this->get_chart_args(),
[
'count_callback' => function ( $activities, $date = null ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
return count( $activities );
},
]
);
}

/**
Expand Down Expand Up @@ -96,4 +131,105 @@ function ( $activity ) {
}
);
}

/**
* Callback to count the words in the activities.
*
* @param \Progress_Planner\Activities\Content[] $activities The activities array.
*
* @return int
*/
public function count_words( $activities ) {
$words = 0;
foreach ( $activities as $activity ) {
if ( null === $activity->get_post() ) {
continue;
}
$words += \progress_planner()->get_activities__content_helpers()->get_word_count(
$activity->get_post()->post_content,
(int) $activity->data_id
);
}
return $words;
}

/**
* Callback to count the density of the activities.
*
* Returns the average number of words per activity.
*
* @param \Progress_Planner\Activities\Content[] $activities The activities array.
*
* @return int
*/
public function count_density( $activities ) {
$words = $this->count_words( $activities );
$count = count( $activities );
return (int) round( $words / max( 1, $count ) );
}

/**
* Get the density of all activities.
*
* @return int
*/
public function get_all_activities_density() {
// Get the all-time average.
static $density;
if ( null === $density ) {
$activities = $this->filter_activities(
\progress_planner()->get_query()->query_activities(
[
'category' => 'content',
'type' => 'publish',
]
)
);
$density = $this->count_density( $activities );
}
return $density;
}

/**
* Get the weekly activities density.
*
* @return int
*/
public function get_weekly_activities_density() {
static $density;
if ( null === $density ) {
// Get the weekly average.
$density = $this->count_density(
\progress_planner()->get_query()->query_activities(
[
'category' => 'content',
'type' => 'publish',
'start_date' => new \DateTime( '-7 days' ),
]
)
);
}
return $density;
}

/**
* Get the weekly words count.
*
* @return int The weekly words count.
*/
public function get_weekly_words() {
static $weekly_words;
if ( null === $weekly_words ) {
$weekly_words = $this->count_words(
\progress_planner()->get_query()->query_activities(
[
'category' => 'content',
'type' => 'publish',
'start_date' => new \DateTime( '-7 days' ),
]
)
);
}
return $weekly_words;
}
}
Loading

0 comments on commit d3a4fb1

Please sign in to comment.