/
profile.php
540 lines (464 loc) · 15.1 KB
/
profile.php
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
/**
* An AnsPress add-on to for displaying user profile.
*
* @author Rahul Aryan <rah12@live.com>
* @copyright 2014 anspress.net & Rahul Aryan
* @license GPL-3.0+ https://www.gnu.org/licenses/gpl-3.0.txt
* @link https://anspress.net
* @package AnsPress
* @subpackage User Profile Addon
*
* @anspress-addon
* Addon Name: User Profile
* Addon URI: https://anspress.net
* Description: Display user profile.
* Author: Rahul Aryan
* Author URI: https://anspress.net
*/
namespace AnsPress\Addons;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
// @codeCoverageIgnoreStart
die;
// @codeCoverageIgnoreEnd
}
/**
* User profile hooks.
*/
class Profile extends \AnsPress\Singleton {
/**
* Instance of this class.
*
* @var object
* @since 4.1.8
*/
protected static $instance = null;
/**
* Initialize the plugin by setting localization and loading public scripts
* and styles.
*
* @since 4.0.0
*/
protected function __construct() {
ap_add_default_options(
array(
'user_page_slug_questions' => 'questions',
'user_page_slug_answers' => 'answers',
'user_page_title_questions' => __( 'Questions', 'anspress-question-answer' ),
'user_page_title_answers' => __( 'Answers', 'anspress-question-answer' ),
)
);
anspress()->add_filter( 'ap_settings_menu_features_groups', $this, 'add_to_settings_page' );
anspress()->add_action( 'ap_form_options_features_profile', $this, 'options' );
ap_register_page( 'user', __( 'User profile', 'anspress-question-answer' ), array( $this, 'user_page' ), true, true );
anspress()->add_action( 'ap_rewrites', $this, 'rewrite_rules', 10, 3 );
anspress()->add_action( 'ap_ajax_user_more_answers', $this, 'load_more_answers', 10, 2 );
anspress()->add_filter( 'wp_title', $this, 'page_title' );
anspress()->add_action( 'the_post', $this, 'filter_page_title' );
anspress()->add_filter( 'ap_breadcrumbs', $this, 'ap_breadcrumbs' );
anspress()->add_filter( 'ap_current_page', $this, 'ap_current_page' );
anspress()->add_filter( 'posts_pre_query', $this, 'modify_query_archive', 999, 2 );
}
/**
* Add tags settings to features settings page.
*
* @param array $groups Features settings group.
* @return array
* @since 4.2.0
*/
public function add_to_settings_page( $groups ) {
$groups['profile'] = array(
'label' => __( 'Profile', 'anspress-question-answer' ),
);
return $groups;
}
/**
* Register profile options
*/
public function options() {
$opt = ap_opt();
$form = array(
'fields' => array(
'user_page_title_questions' => array(
'label' => __( 'Questions page title', 'anspress-question-answer' ),
'desc' => __( 'Custom title for user profile questions page', 'anspress-question-answer' ),
'value' => $opt['user_page_title_questions'],
),
'user_page_slug_questions' => array(
'label' => __( 'Questions page slug', 'anspress-question-answer' ),
'desc' => __( 'Custom slug for user profile questions page', 'anspress-question-answer' ),
'value' => $opt['user_page_slug_questions'],
),
'user_page_title_answers' => array(
'label' => __( 'Answers page title', 'anspress-question-answer' ),
'desc' => __( 'Custom title for user profile answers page', 'anspress-question-answer' ),
'value' => $opt['user_page_title_answers'],
),
'user_page_slug_answers' => array(
'label' => __( 'Answers page slug', 'anspress-question-answer' ),
'desc' => __( 'Custom slug for user profile answers page', 'anspress-question-answer' ),
'value' => $opt['user_page_slug_answers'],
),
),
);
return $form;
}
/**
* Layout of base page
*/
public function user_page() {
$this->user_pages();
dynamic_sidebar( 'ap-top' );
echo '<div id="ap-user" class="ap-row">';
include ap_get_theme_location( 'addons/user/index.php' );
echo '</div>';
}
/**
* Add category pages rewrite rule.
*
* @param array $rules AnsPress rules.
* @param string $slug Slug.
* @param integer $base_page_id Base page ID.
* @return array
*/
public function rewrite_rules( $rules, $slug, $base_page_id ) {
$base_slug = get_page_uri( ap_opt( 'user_page' ) );
update_option( 'ap_user_path', $base_slug, true );
$new_rules = array();
$new_rules = array(
$base_slug . '/([^/]+)/([^/]+)/page/?([0-9]{1,})/?' => 'index.php?author_name=$matches[#]&ap_page=user&user_page=$matches[#]&ap_paged=$matches[#]',
$base_slug . '/([^/]+)/([^/]+)/?' => 'index.php?author_name=$matches[#]&ap_page=user&user_page=$matches[#]',
$base_slug . '/([^/]+)/?' => 'index.php?author_name=$matches[#]&ap_page=user',
$base_slug . '/?' => 'index.php?ap_page=user',
);
return $new_rules + $rules;
}
/**
* Register user profile pages.
*/
public function user_pages() {
if ( ! empty( anspress()->user_pages ) ) {
return;
}
anspress()->user_pages = array(
array(
'slug' => 'questions',
'label' => __( 'Questions', 'anspress-question-answer' ),
'icon' => 'apicon-question',
'cb' => array( $this, 'question_page' ),
'order' => 2,
),
array(
'slug' => 'answers',
'label' => __( 'Answers', 'anspress-question-answer' ),
'icon' => 'apicon-answer',
'cb' => array( $this, 'answer_page' ),
'order' => 2,
),
);
do_action( 'ap_user_pages' );
foreach ( (array) anspress()->user_pages as $key => $args ) {
$rewrite = ap_opt( 'user_page_slug_' . $args['slug'] );
$title = ap_opt( 'user_page_title_' . $args['slug'] );
// If BuddyPress addon is active then, do not modify the slug since
// template file loaded has the exact name with slug.
if ( ap_is_addon_active( 'buddypress.php' ) ) {
$rewrite = $args['slug'];
}
// Override user page slug.
if ( empty( $args['rewrite'] ) ) {
anspress()->user_pages[ $key ]['rewrite'] = ! empty( $rewrite ) ? sanitize_title( $rewrite ) : $args['slug'];
}
// Override user page title.
if ( ! empty( $title ) ) {
anspress()->user_pages[ $key ]['label'] = $title;
}
// Add default order.
if ( ! isset( $args['order'] ) ) {
anspress()->user_pages[ $key ]['order'] = 10;
}
}
anspress()->user_pages = ap_sort_array_by_order( anspress()->user_pages );
}
/**
* Output user profile menu.
*
* @param int|false $user_id Id of user, default is current user.
* @param string $class_name CSS class.
*/
public function user_menu( $user_id = false, $class_name = '' ) {
$user_id = false !== $user_id ? $user_id : ap_current_user_id();
$user = get_user_by( 'id', $user_id );
$current_tab = get_query_var( 'user_page', ap_opt( 'user_page_slug_questions' ) );
$ap_menu = apply_filters( 'ap_user_menu_items', anspress()->user_pages, $user_id );
// If BuddyPress addon is active, set the profile menu active links as required
// with the help of the 'pagename' query var.
if ( ap_is_addon_active( 'buddypress.php' ) ) {
$current_tab = 'qa' === get_query_var( 'pagename' ) ? 'questions' : get_query_var( 'pagename' );
}
echo '<ul class="ap-tab-nav clearfix ' . esc_attr( $class_name ) . '">';
foreach ( (array) $ap_menu as $args ) {
if ( empty( $args['private'] ) || ( true === $args['private'] && get_current_user_id() === $user_id ) ) {
echo '<li class="ap-menu-' . esc_attr( $args['slug'] ) . ( $args['rewrite'] === $current_tab ? ' active' : '' ) . '">';
$url = isset( $args['url'] ) ? $args['url'] : ap_user_link( $user_id, $args['rewrite'] );
if (
( ap_is_addon_active( 'buddypress.php' ) && function_exists( 'bp_core_get_userlink' ) )
&& ( in_array( 'about', $args, true ) || in_array( 'edit-profile', $args, true ) )
) {
$slug = get_option( 'ap_user_path' );
$link = home_url( $slug ) . '/' . $user->user_nicename . '/';
$url = isset( $args['url'] ) ? $args['url'] : $link . $args['rewrite'];
}
echo '<a href="' . esc_url( $url ) . '">';
// Show icon.
if ( ! empty( $args['icon'] ) ) {
echo '<i class="' . esc_attr( $args['icon'] ) . '"></i>';
}
echo esc_attr( $args['label'] );
// Show count.
if ( ! empty( $args['count'] ) ) {
echo '<span>' . esc_attr( number_format_i18n( $args['count'] ) ) . '</span>';
}
echo '</a>';
echo '</li>';
}
}
echo '</ul>';
}
/**
* Profile page title.
*
* @return string
*/
public function user_page_title() {
$this->user_pages();
$title = ap_user_display_name( ap_current_user_id() );
$current_tab = sanitize_title( get_query_var( 'user_page', ap_opt( 'user_page_slug_questions' ) ) );
$page = ap_search_array( anspress()->user_pages, 'rewrite', $current_tab );
if ( ! empty( $page ) ) {
return $title . ' | ' . $page[0]['label'];
}
}
/**
* Add user page title.
*
* @param string $title AnsPress page title.
* @return string
*/
public function page_title( $title ) {
if ( 'user' === ap_current_page() ) {
return $this->user_page_title() . ' | ';
}
return $title;
}
/**
* Filter user page title.
*
* @param object $_post WP post object.
* @return void
*/
public function filter_page_title( $_post ) {
if ( 'user' === ap_current_page() && ap_opt( 'user_page' ) == $_post->ID && ! is_admin() ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
$_post->post_title = $this->user_page_title();
}
}
/**
* Hook into AnsPress breadcrums to show user profile page.
*
* @param array $navs Breadcrumbs navs.
* @return array
*/
public function ap_breadcrumbs( $navs ) {
if ( 'user' === ap_current_page() ) {
$navs['page'] = array(
'title' => ap_user_display_name( ap_current_user_id() ),
'link' => ap_user_link( ap_current_user_id() ),
'order' => 8,
);
$current = get_query_var( 'user_page', ap_opt( 'user_page_slug_questions' ) );
$current_page = ap_search_array( anspress()->user_pages, 'rewrite', $current );
if ( ! empty( $current_page ) ) {
$navs['user'] = array(
'title' => $current_page[0]['label'],
'link' => ap_user_link( ap_current_user_id() ) . $current . '/',
'order' => 8,
);
}
}
return $navs;
}
/**
* Render sub page template.
*/
public function sub_page_template() {
$current = get_query_var( 'user_page', ap_opt( 'user_page_slug_questions' ) );
$current_page = ap_search_array( anspress()->user_pages, 'rewrite', $current );
if ( ! empty( $current_page ) ) {
$current_page = $current_page[0];
// Callback.
if ( isset( $current_page['cb'] ) && is_array( $current_page['cb'] ) && method_exists( $current_page['cb'][0], $current_page['cb'][1] ) ) {
call_user_func( $current_page['cb'] );
} elseif ( function_exists( $current_page['cb'] ) ) {
call_user_func( $current_page['cb'] );
} else {
esc_attr_e( 'Callback function not found for rendering this page', 'anspress-question-answer' );
}
}
}
/**
* Display user questions page.
*/
public function question_page() {
$user_id = ap_current_user_id();
$args['ap_current_user_ignore'] = true;
$args['author'] = $user_id;
/**
* Filter authors question list args
*
* @var array
*/
$args = apply_filters( 'ap_authors_questions_args', $args );
anspress()->questions = new \Question_Query( $args );
include ap_get_theme_location( 'addons/user/questions.php' );
}
/**
* Display user questions page.
*/
public function answer_page() {
global $answers;
$user_id = ap_current_user_id();
$args['ap_current_user_ignore'] = true;
$args['ignore_selected_answer'] = true;
$args['showposts'] = 10;
$args['author'] = $user_id;
/**
* Filter authors question list args
*
* @var array
*/
$args = apply_filters( 'ap_user_answers_args', $args );
$answers = new \Answers_Query( $args );
anspress()->answers = $answers;
ap_get_template_part( 'addons/user/answers' );
}
/**
* Ajax callback for loading more answers.
*
* @return void
*/
public function load_more_answers() {
global $answers;
$user_id = ap_sanitize_unslash( 'user_id', 'r' );
$paged = ap_sanitize_unslash( 'current', 'r', 1 ) + 1;
$args['ap_current_user_ignore'] = true;
$args['ignore_selected_answer'] = true;
$args['showposts'] = 10;
$args['author'] = (int) $user_id;
if ( false !== $paged ) {
$args['paged'] = $paged;
}
/**
* Filter authors question list args
*
* @param array $args WP_Query arguments.
*/
$args = apply_filters( 'ap_user_answers_args', $args );
$answers = new \Answers_Query( $args );
anspress()->answers = $answers;
ob_start();
if ( ap_have_answers() ) {
/* Start the Loop */
while ( ap_have_answers() ) :
ap_the_answer();
ap_get_template_part( 'addons/user/answer-item' );
endwhile;
}
$html = ob_get_clean();
// Pagination fix on Ajax load more event.
$paged = $answers->max_num_pages > $paged ? $paged : 0;
ap_ajax_json(
array(
'success' => true,
'element' => '#ap-bp-answers',
'args' => array(
'ap_ajax_action' => 'user_more_answers',
'__nonce' => wp_create_nonce( 'loadmore-answers' ),
'type' => 'answers',
'current' => $paged,
'user_id' => $user_id,
),
'html' => $html,
)
);
}
/**
* Override current page of AnsPress.
*
* @param string $query_var Current page name.
* @return string
* @since 4.1.0
*/
public function ap_current_page( $query_var ) {
if ( is_author() && 'user' === get_query_var( 'ap_page' ) ) {
$query_var = 'user';
}
return $query_var;
}
/**
* Modify main query.
*
* @param array $posts Array of post object.
* @param object $query Wp_Query object.
* @return void|array
* @since 4.1.0
* @since 4.1.1 Redirect to current user profile if no author set.
* @since 4.1.2 Check for 404 error.
*/
public function modify_query_archive( $posts, $query ) {
if ( $query->is_main_query() && ! $query->is_404 && 'user' === get_query_var( 'ap_page' ) ) {
$query_object = get_queried_object();
if ( ! $query_object && ! get_query_var( 'author_name' ) && is_user_logged_in() ) {
wp_safe_redirect( ap_user_link( get_current_user_id() ) );
exit;
} elseif ( $query_object && $query_object instanceof \WP_User ) {
return array( get_post( ap_opt( 'user_page' ) ) );
} else {
$query->set_404();
status_header( 404 );
}
}
return $posts;
}
/**
* Override user page template.
*
* @param string $template Template file.
* @return string
* @since 4.1.0
*/
public function page_template( $template ) {
if ( is_author() && 'user' === get_query_var( 'ap_page' ) ) {
$user_slug = ap_opt( 'user_page_id' );
return locate_template( array( 'page-' . $user_slug . '.php', 'page.php' ) );
}
return $template;
}
/**
* Get current user id for AnsPress profile.
*
* @return integer
* @since 4.1.0
*/
public function current_user_id() {
$query_object = get_queried_object();
$user_id = get_queried_object_id();
// Current user id if queried object is not set.
if ( ! $query_object instanceof \WP_User || empty( $user_id ) ) {
$user_id = get_current_user_id();
}
return (int) $user_id;
}
}
// Init addon.
Profile::init();