Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi author blog stats #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/API/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,27 @@ function get_total_favorites_count($site_id = null)
function the_total_favorites_count($site_id = null)
{
echo get_total_favorites_count($site_id);
}

/**
* Get the total number of how many times users added author posts to favorites
* @param $user_id int
* @param $site_id int, defaults to current blog/site
* @return int
*/
function get_total_favorites_count_made_by_users($user_id, $site_id = null)
{
$count = new FavoriteCount();
return $count->getCountByUsers($user_id, $site_id);
}

/**
* Print the total number of how many times users added author posts to favorites
* @param $user_id int
* @param $site_id int, defaults to current blog/site
* @return int
*/
function the_total_favorites_count_made_by_users($user_id, $site_id = null)
{
echo get_total_favorites_count_made_by_users($user_id, $site_id);
}
35 changes: 35 additions & 0 deletions app/Entities/Post/FavoriteCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,39 @@ public function getAllCount($site_id = null)
if ( (is_multisite()) && (isset($site_id) && ($site_id !== "")) ) restore_current_blog();
return intval($count);
}

/**
* Get count how many time posts for $user_id has been added to favorites
*
* @param $user_id
* @param null $site_id
* @return int
*/
public function getCountByUsers($user_id, $site_id = null) {
if ( (is_multisite()) && (isset($site_id)) && ($site_id !== "") ) switch_to_blog(intval($site_id));

if (!intval($user_id))
return 0;

$types = get_option('simplefavorites_display');

//Get all posts ids for user
$args = array(
'author' => $user_id,
'post_type' => array_keys($types['posttypes']),
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids'
);

$user_posts = new \WP_Query($args);

$post_ids = implode(",", $user_posts->posts);

global $wpdb;
$query = "SELECT SUM(meta_value) AS favorites_count FROM {$wpdb->prefix}postmeta WHERE meta_key = 'simplefavorites_count' AND post_id = '$post_ids'";
$count = $wpdb->get_var( $query );
if ( (is_multisite()) && (isset($site_id) && ($site_id !== "")) ) restore_current_blog();
return intval($count);
}
}