class-user-posts.php
6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace um\core;
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'um\core\User_posts' ) ) {
/**
* Class User_posts
* @package um\core
*/
class User_posts {
/**
* User_posts constructor.
*/
function __construct() {
add_action( 'um_profile_content_posts', array( &$this, 'add_posts' ) );
add_action( 'um_profile_content_comments', array( &$this, 'add_comments' ) );
}
/**
* Add posts
*/
function add_posts() {
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'offset' => 0,
'author' => um_get_requested_user(),
'post_status' => array( 'publish' ),
'um_main_query' => true,
'suppress_filters' => false,
);
/**
* UM hook
*
* @type filter
* @title um_profile_query_make_posts
* @description Some changes of WP_Query Posts Tab
* @input_vars
* [{"var":"$query_posts","type":"WP_Query","desc":"UM Posts Tab query"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_profile_query_make_posts', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_profile_query_make_posts', 'my_profile_query_make_posts', 10, 1 );
* function my_profile_query_make_posts( $query_posts ) {
* // your code here
* return $query_posts;
* }
* ?>
*/
$args = apply_filters( 'um_profile_query_make_posts', $args );
$posts = get_posts( $args );
$args['posts_per_page'] = -1;
$args['fields'] = 'ids';
unset( $args['offset'] );
$count_posts = get_posts( $args );
if ( ! empty( $count_posts ) && ! is_wp_error( $count_posts ) ) {
$count_posts = count( $count_posts );
}
UM()->get_template( 'profile/posts.php', '', array( 'posts' => $posts, 'count_posts' => $count_posts ), true );
}
/**
* Add comments
*/
function add_comments() {
$comments = get_comments( array(
'number' => 10,
'offset' => 0,
'user_id' => um_user( 'ID' ),
'post_status' => array( 'publish' ),
'type__not_in' => apply_filters( 'um_excluded_comment_types', array('') ),
) );
$comments_count = get_comments( array(
'user_id' => um_user( 'ID' ),
'post_status' => array( 'publish' ),
'type__not_in' => apply_filters( 'um_excluded_comment_types', array('') ),
'count' => 1,
) );
UM()->get_template( 'profile/comments.php', '', array( 'comments' => $comments, 'count_comments' => $comments_count ), true );
}
/**
* Dynamic load of posts
*
*/
function load_posts() {
UM()->check_ajax_nonce();
$author = ! empty( $_POST['author'] ) ? absint( $_POST['author'] ) : get_current_user_id();
$page = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'offset' => ( $page - 1 ) * 10,
'author' => $author,
'post_status' => array( 'publish' ),
'um_main_query' => true,
'suppress_filters' => false,
);
/**
* UM hook
*
* @type filter
* @title um_profile_query_make_posts
* @description Some changes of WP_Query Posts Tab
* @input_vars
* [{"var":"$query_posts","type":"WP_Query","desc":"UM Posts Tab query"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_profile_query_make_posts', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_profile_query_make_posts', 'my_profile_query_make_posts', 10, 1 );
* function my_profile_query_make_posts( $query_posts ) {
* // your code here
* return $query_posts;
* }
* ?>
*/
$args = apply_filters( 'um_profile_query_make_posts', $args );
$posts = get_posts( $args );
UM()->get_template( 'profile/posts.php', '', array( 'posts' => $posts ), true );
wp_die();
}
/**
* Dynamic load of comments
*/
function load_comments() {
UM()->check_ajax_nonce();
$user_id = ! empty( $_POST['user_id'] ) ? absint( $_POST['user_id'] ) : get_current_user_id();
$page = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
$comments = get_comments( array(
'number' => 10,
'offset' => ( $page - 1 ) * 10,
'user_id' => $user_id,
'post_status' => array('publish'),
'type__not_in' => apply_filters( 'um_excluded_comment_types', array('') ),
) );
UM()->get_template( 'profile/comments.php', '', array( 'comments' => $comments ), true );
wp_die();
}
/**
* Count posts by type
*
* @param string $user_id
* @param string $post_type
*
* @return int|string
*/
function count_user_posts_by_type( $user_id = '', $post_type = 'post' ) {
global $wpdb;
if ( ! $user_id ) {
$user_id = um_user( 'ID' );
}
if ( ! $user_id ) {
return 0;
}
$where = get_posts_by_author_sql( $post_type, true, $user_id );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return $this->pretty_number_formatting( $count );
}
/**
* Count comments
*
* @param int|null $user_id
*
* @return int|string
*/
function count_user_comments( $user_id = null ) {
global $wpdb;
if ( ! $user_id ) {
$user_id = um_user( 'ID' );
}
if ( ! $user_id ) {
return 0;
}
$count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM " . $wpdb->comments. " WHERE user_id = " . $user_id . " AND comment_approved = '1'");
return $this->pretty_number_formatting( $count );
}
/**
* @param int $count
*
* @return string
*/
function pretty_number_formatting( $count ) {
/**
* UM hook
*
* @type filter
* @title um_pretty_number_formatting
* @description Change User Posts count value
* @input_vars
* [{"var":"$count","type":"int","desc":"Posts Count"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_pretty_number_formatting', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_pretty_number_formatting', 'my_pretty_number_formatting', 10, 1 );
* function my_pretty_number_formatting( $count ) {
* // your code here
* return $count;
* }
* ?>
*/
return apply_filters( 'um_pretty_number_formatting', $count );
}
}
}