From 289c8ffa221862aa43699946e899d5daead2c7bd Mon Sep 17 00:00:00 2001 From: Slamik Date: Fri, 12 Jan 2018 11:38:31 +0200 Subject: [PATCH 1/2] Added public function to count how many times author posts has been added to favorites --- app/Entities/Post/FavoriteCount.php | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/Entities/Post/FavoriteCount.php b/app/Entities/Post/FavoriteCount.php index 4079c22..34cef59 100644 --- a/app/Entities/Post/FavoriteCount.php +++ b/app/Entities/Post/FavoriteCount.php @@ -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); + } } \ No newline at end of file From d118312866668eb7686a8070faa525dbf30536c9 Mon Sep 17 00:00:00 2001 From: Slamik Date: Fri, 12 Jan 2018 11:44:18 +0200 Subject: [PATCH 2/2] Helper functions to count how many times author posts has been added to favorites --- app/API/functions.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/app/API/functions.php b/app/API/functions.php index 4416bc3..fbda70e 100644 --- a/app/API/functions.php +++ b/app/API/functions.php @@ -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); } \ No newline at end of file