/
theme.php
1161 lines (1007 loc) · 32.9 KB
/
theme.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* AnsPress theme and template handling.
*
* @author Rahul Aryan <rah12@live.com>
* @license GPL-3.0+
* @link https://anspress.net
* @copyright 2014 Rahul Aryan
* @package AnsPress
* @subpackage Theme Functions
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Return current page title.
*
* @return string current title
* @since unknown
* @since 4.1.0 Removed `question_name` query var check.
*/
function ap_page_title() {
$new_title = '';
$new_title = apply_filters( 'ap_page_title', $new_title );
return $new_title;
}
/**
* Check if current page is search page
*
* @return boolean
*/
function is_ap_search() {
if ( is_anspress() && get_query_var( 'ap_s' ) ) {
return true;
}
return false;
}
/**
* Get post status.
*
* @param boolean|integer $post_id question or answer ID.
* @return string
* @since 2.0.0
*/
function ap_post_status( $post_id = false ) {
if ( false === $post_id ) {
$post_id = get_the_ID();
}
return get_post_status( $post_id );
}
/**
* Check if current post is private.
*
* @param boolean|integer $post_id question or answer ID.
* @return boolean
*/
function is_private_post( $post_id = false ) {
if ( ap_post_status( $post_id ) === 'private_post' ) {
return true;
}
return false;
}
/**
* Check if post is waiting moderation.
*
* @param boolean|integer $post_id question or answer ID.
* @return bool
*/
function is_post_waiting_moderation( $post_id = false ) {
if ( get_post_status( $post_id ) === 'moderate' ) {
return true;
}
return false;
}
/**
* Check if question is closed.
*
* @param boolean|integer $post_id question or answer ID.
* @return boolean
* @since 2.0.0
*/
function is_post_closed( $post_id = null ) {
if ( '1' === ap_get_post_field( 'closed', $post_id ) ) {
return true;
}
return false;
}
/**
* Check if question have a parent post.
*
* @param boolean|integer $post_id question or answer ID.
* @return boolean
* @since 2.0.0
*/
function ap_have_parent_post( $post_id = false ) {
if ( ! $post_id ) {
$post_id = get_the_ID();
}
// Get post.
$post_o = ap_get_post( $post_id );
if ( $post_o->post_parent > 0 && 'question' === $post_o->post_type ) {
return true;
}
return false;
}
/**
* Anspress pagination
* Uses paginate_links.
*
* @param float $current Current paged, if not set then get_query_var('paged') is used.
* @param int $total Total number of pages, if not set then global $questions is used.
* @param string $format pagination format.
* @param string $page_num_link Base link.
* @return string
*/
function ap_pagination( $current = false, $total = false, $format = '?paged=%#%', $page_num_link = false ) {
global $ap_max_num_pages, $ap_current;
if ( is_front_page() ) {
$format = '';
}
$big = 999999999; // Need an unlikely integer.
if ( false === $current ) {
$paged = ap_sanitize_unslash( 'ap_paged', 'r', 1 );
$current = is_front_page() ? max( 1, $paged ) : max( 1, get_query_var( 'paged' ) );
} elseif ( ! empty( $ap_current ) ) {
$current = $ap_current;
}
if ( ! empty( $ap_max_num_pages ) ) {
$total = $ap_max_num_pages;
} elseif ( false === $total && isset( anspress()->questions->max_num_pages ) ) {
$total = anspress()->questions->max_num_pages;
}
if ( false === $page_num_link ) {
$page_num_link = str_replace( array( '&', '&' ), '&', get_pagenum_link( $big ) );
}
$base = str_replace( $big, '%#%', $page_num_link );
if ( 1 === $total ) {
return;
}
echo '<div class="ap-pagination clearfix">';
$links = paginate_links(
array(
'base' => $base,
'format' => $format,
'current' => $current,
'total' => $total,
'end_size' => 2,
'mid_size' => 2,
)
);
if ( $links ) {
$links = str_replace( '<a class="next page-numbers"', '<a class="next page-numbers" rel="next"', $links );
$links = str_replace( '<a class="prev page-numbers"', '<a class="prev page-numbers" rel="prev"', $links );
echo wp_kses_post( $links );
}
echo '</div>';
}
/**
* Register anspress pages.
*
* @param string $page_slug slug for links.
* @param string $page_title Page title.
* @param callable $func Hook to run when shortcode is found.
* @param bool $show_in_menu User can add this pages to their WordPress menu from appearance->menu->AnsPress.
* @param bool $is_private Only show to currently logged in user.
*
* @since 2.0.1
*/
function ap_register_page( $page_slug, $page_title, $func, $show_in_menu = true, $is_private = false ) {
anspress()->pages[ $page_slug ] = array(
'title' => $page_title,
'func' => $func,
'show_in_menu' => $show_in_menu,
'private' => $is_private,
);
}
/**
* Output current AnsPress page.
*
* @param string $current_page Pass current page to override.
*
* @since 2.0.0
* @since 4.1.9 Fixed: page attribute not working.
*/
function ap_page( $current_page = '' ) {
$pages = anspress()->pages;
if ( empty( $current_page ) ) {
$current_page = ap_current_page();
$current_page = '' === $current_page ? 'base' : $current_page;
}
if ( isset( $pages[ $current_page ]['func'] ) ) {
call_user_func( $pages[ $current_page ]['func'] );
} else {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
include ap_get_theme_location( 'not-found.php' );
}
}
/**
* Return post actions array.
*
* @param mixed $_post Post.
* @return array
* @since 3.0.0
*/
function ap_post_actions( $_post = null ) {
$_post = ap_get_post( $_post );
$actions = array();
if ( ! in_array( $_post->post_type, array( 'question', 'answer' ), true ) ) {
return $actions;
}
// Featured link.
if ( 'question' === $_post->post_type ) {
$actions[] = ap_featured_post_args( $_post->ID );
}
// Question close action.
if ( ap_user_can_close_question() && 'question' === $_post->post_type ) {
$nonce = wp_create_nonce( 'close_' . $_post->ID );
$close_label = $_post->closed ? __( 'Open', 'anspress-question-answer' ) : __( 'Close', 'anspress-question-answer' );
$close_title = $_post->closed ? __( 'Open this question for new answers', 'anspress-question-answer' ) : __( 'Close this question for new answer.', 'anspress-question-answer' );
$actions[] = array(
'cb' => 'close',
'icon' => 'apicon-check',
'query' => array(
'nonce' => $nonce,
'post_id' => $_post->ID,
),
'label' => $close_label,
'title' => $close_title,
);
}
// Edit link.
if ( ap_user_can_edit_post( $_post->ID ) ) {
$actions[] = array(
'cb' => 'edit',
'label' => __( 'Edit', 'anspress-question-answer' ),
'href' => ap_post_edit_link( $_post ),
);
}
// Flag link.
$actions[] = ap_flag_btn_args( $_post );
$status_args = ap_post_status_btn_args( $_post );
if ( ! empty( $status_args ) ) {
$actions[] = array(
'label' => __( 'Status', 'anspress-question-answer' ),
'header' => true,
);
$actions = array_merge( $actions, $status_args );
$actions[] = array( 'header' => true );
}
$answers = ap_count_published_answers( $_post->ID );
if ( ap_user_can_delete_post( $_post->ID ) && ( ! ap_opt( 'trashing_question_with_answer' ) || empty( $answers ) ) ) {
if ( 'trash' === $_post->post_status ) {
$label = __( 'Undelete', 'anspress-question-answer' );
$title = sprintf(
/* Translators: %s Question or Answer post type label for restoring a question or answer. */
__( 'Restore this %s', 'anspress-question-answer' ),
( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
);
} else {
$label = __( 'Delete', 'anspress-question-answer' );
$title = sprintf(
/* Translators: %s Question or Answer post type label for deleting a question or answer. */
__( 'Delete this %s (can be restored again)', 'anspress-question-answer' ),
( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
);
}
$actions[] = array(
'cb' => 'toggle_delete_post',
'query' => array(
'post_id' => $_post->ID,
'__nonce' => wp_create_nonce( 'trash_post_' . $_post->ID ),
),
'label' => $label,
'title' => $title,
);
}
// Permanent delete link.
if ( ap_user_can_permanent_delete( $_post->ID ) && ( ! ap_opt( 'deleting_question_with_answer' ) || empty( $answers ) ) ) {
$actions[] = array(
'cb' => 'delete_permanently',
'query' => array(
'post_id' => $_post->ID,
'__nonce' => wp_create_nonce( 'delete_post_' . $_post->ID ),
),
'label' => __( 'Delete Permanently', 'anspress-question-answer' ),
'title' => sprintf(
/* Translators: %s Question or Answer post type label for permanently deleting a question or answer. */
__( 'Delete %s permanently (cannot be restored again)', 'anspress-question-answer' ),
( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
),
);
}
// Convert question to a post.
if ( ( is_super_admin() || current_user_can( 'manage_options' ) ) && 'question' === $_post->post_type ) {
$actions[] = array(
'cb' => 'convert_to_post',
'query' => array(
'post_id' => $_post->ID,
'__nonce' => wp_create_nonce( 'convert-post-' . $_post->ID ),
),
'label' => __( 'Convert to post', 'anspress-question-answer' ),
'title' => __( 'Convert this question to blog post', 'anspress-question-answer' ),
);
}
/**
* For filtering post actions buttons
*
* @var array
* @since 2.0
*/
$actions = apply_filters( 'ap_post_actions', array_filter( $actions ) );
return array_values( $actions );
}
/**
* Post actions buttons.
*
* @since 2.0
*/
function ap_post_actions_buttons() {
if ( ! is_user_logged_in() ) {
return;
}
$args = wp_json_encode(
array(
'post_id' => get_the_ID(),
'nonce' => wp_create_nonce( 'post-actions-' . get_the_ID() ),
)
);
echo '<postActions class="ap-dropdown"><button class="ap-btn apicon-gear ap-actions-handle ap-dropdown-toggle" ap="actiontoggle" apquery="' . esc_js( $args ) . '"></button><ul class="ap-actions ap-dropdown-menu"></ul></postActions>';
}
/**
* Return all order by options for questions list.
*
* @param string $current_url Current page URL.
* @return array
* @since 3.0.0 Moved from `ap_question_sorting()`.
*/
function ap_get_questions_orderby( $current_url = '' ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
$param = array();
$search_q = get_query_var( 'ap_s' );
if ( ! empty( $search_q ) ) {
$param['ap_s'] = $search_q;
}
$navs = array(
array(
'key' => 'order_by',
'value' => 'active',
'label' => __( 'Active', 'anspress-question-answer' ),
),
array(
'key' => 'order_by',
'value' => 'newest',
'label' => __( 'New', 'anspress-question-answer' ),
),
);
if ( ! ap_opt( 'disable_voting_on_question' ) ) {
$navs[] = array(
'key' => 'order_by',
'value' => 'voted',
'label' => __( 'Votes', 'anspress-question-answer' ),
);
}
$navs[] = array(
'key' => 'order_by',
'value' => 'answers',
'label' => __( 'Answers', 'anspress-question-answer' ),
);
$navs[] = array(
'key' => 'order_by',
'value' => 'views',
'label' => __( 'Views', 'anspress-question-answer' ),
);
$navs[] = array(
'key' => 'order_by',
'value' => 'solved',
'label' => __( 'Solved', 'anspress-question-answer' ),
);
$navs[] = array(
'key' => 'order_by',
'value' => 'unanswered',
'label' => __( 'Unanswered', 'anspress-question-answer' ),
);
$navs[] = array(
'key' => 'order_by',
'value' => 'unsolved',
'label' => __( 'Unsolved', 'anspress-question-answer' ),
);
foreach ( (array) $navs as $k => $args ) {
$active = ap_get_current_list_filters( 'order_by' );
if ( $active === $args['value'] ) {
$navs[ $k ]['active'] = true;
}
}
/**
* Filter question sorting.
*
* @param array $navs Questions orderby list.
* @since 2.3
*/
return apply_filters( 'ap_questions_order_by', $navs );
}
/**
* Output answers tab.
*
* @param string|boolean $base Current page url.
* @since 2.0.1
*/
function ap_answers_tab( $base = false ) {
$sort = ap_sanitize_unslash( 'order_by', 'r', ap_opt( 'answers_sort' ) );
if ( ! $base ) {
$base = get_permalink();
}
$navs = array(
'active' => array(
'link' => add_query_arg( array( 'order_by' => 'active' ), $base ),
'title' => __( 'Active', 'anspress-question-answer' ),
),
);
if ( ! ap_opt( 'disable_voting_on_answer' ) ) {
$navs['voted'] = array(
'link' => add_query_arg( array( 'order_by' => 'voted' ), $base ),
'title' => __( 'Voted', 'anspress-question-answer' ),
);
}
$navs['newest'] = array(
'link' => add_query_arg( array( 'order_by' => 'newest' ), $base ),
'title' => __( 'Newest', 'anspress-question-answer' ),
);
$navs['oldest'] = array(
'link' => add_query_arg( array( 'order_by' => 'oldest' ), $base ),
'title' => __( 'Oldest', 'anspress-question-answer' ),
);
echo '<ul id="answers-order" class="ap-answers-tab ap-ul-inline clearfix">';
foreach ( (array) $navs as $k => $nav ) {
echo '<li' . ( $sort === $k ? ' class="active"' : '' ) . '><a href="' . esc_url( $nav['link'] . '#answers-order' ) . '">' . esc_attr( $nav['title'] ) . '</a></li>';
}
echo '</ul>';
}
/**
* Answer meta to display.
*
* @param false|integer $answer_id Answer id.
* @return string
* @since 2.0.1
* @deprecated 4.2.0
*/
function ap_display_answer_metas( $answer_id = false ) {
_deprecated_function( __FUNCTION__, '4.2.0' );
return;
// @codingStandardsIgnoreStart
if ( false === $answer_id ) {
$answer_id = get_the_ID();
}
$metas = array();
if ( ap_is_selected( $answer_id ) ) {
$metas['best_answer'] = '<span class="ap-best-answer-label">' . __( 'Best answer', 'anspress-question-answer' ) . '</span>';
}
$metas['history'] = ap_last_active_time( $answer_id );
/**
* Used to filter answer display meta.
*
* @since 2.0.1
* @deprecated 4.2.0
*/
$metas = apply_filters( 'ap_display_answer_metas', $metas, $answer_id );
$output = '';
if ( ! empty( $metas ) && is_array( $metas ) ) {
foreach ( $metas as $meta => $display ) {
$output .= "<span class='ap-display-meta-item {$meta}'>{$display}</span>";
}
}
return $output;
// @codingStandardsIgnoreEnd
}
/**
* Echo ask button.
*
* @since 2.1
*/
function ap_ask_btn() {
echo ap_get_ask_btn(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Return the ask button.
*
* @return string Ask button HTML
* @since 2.1
*/
function ap_get_ask_btn() {
$link = ap_get_link_to( 'ask' );
/**
* Filter ask button link.
*
* @param string $link
*/
$link = apply_filters( 'ap_ask_btn_link', $link );
return '<a class="ap-btn-ask" href="' . esc_url( $link ) . '">' . esc_attr__( 'Ask question', 'anspress-question-answer' ) . '</a>';
}
/**
* Include template php files.
*
* @param string $file File name without extension.
* @param array|false $args Arguments to be passed.
* @since 2.1
*/
function ap_get_template_part( $file, $args = false ) {
if ( false !== $args ) {
extract( $args ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
}
include ap_get_theme_location( $file . '.php' );
}
/**
* Return current AnsPress page
*
* @param false|string $looking_for Looking for page.
*
* @return string|bool
*
* @since unknown
* @since 4.1.0 Check if ask question page.
* @since 4.1.1 Do not return `base` by default.
* @since 4.1.2 If 404 do not return anything.
* @since 4.1.9 Changed cache key which was causing conflict with core.
* @since 4.1.15 Added parameter `$looking_for`.
*/
function ap_current_page( $looking_for = false ) {
$query_var = get_query_var( 'ap_page', '' );
$main_pages = array_keys( ap_main_pages() );
$page_ids = array();
foreach ( $main_pages as $page_slug ) {
$page_ids[ ap_opt( $page_slug ) ] = $page_slug;
}
if ( is_question() || is_singular( 'question' ) ) {
$query_var = 'question';
} elseif ( 'edit' === $query_var ) {
$query_var = 'edit';
} elseif ( in_array( $query_var . '_page', $main_pages, true ) ) {
$query_var = $query_var;
} elseif ( in_array( get_the_ID(), array_keys( $page_ids ) ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
if ( ! empty( $page_ids[ get_the_ID() ] ) ) {
$query_var = str_replace( '_page', '', $page_ids[ get_the_ID() ] );
}
} elseif ( 'base' === $query_var ) {
$query_var = 'base';
} elseif ( is_404() ) {
$query_var = '';
}
/**
* Filter AnsPress current page.
*
* @param string $query_var Current page slug.
*/
$ret = apply_filters( 'ap_current_page', esc_attr( $query_var ) );
if ( false !== $looking_for ) {
return $looking_for === $ret;
}
return $ret;
}
/**
* AnsPress CSS and JS.
*
* @return array
*/
function ap_assets() {
wp_register_script( 'selectize', ANSPRESS_URL . 'assets/js/lib/selectize.min.js', array( 'jquery' ), AP_VERSION, true );
wp_register_script( 'anspress-common', ANSPRESS_URL . 'assets/js/common.js', array( 'jquery', 'jquery-form', 'underscore', 'backbone', 'selectize' ), AP_VERSION, true );
wp_register_script( 'anspress-question', ANSPRESS_URL . 'assets/js/question.js', array( 'anspress-common' ), AP_VERSION, true );
wp_register_script( 'anspress-ask', ANSPRESS_URL . 'assets/js/ask.js', array( 'anspress-common' ), AP_VERSION, true );
wp_register_script( 'anspress-list', ANSPRESS_URL . 'assets/js/list.js', array( 'anspress-common' ), AP_VERSION, true );
wp_register_script( 'anspress-notifications', ANSPRESS_URL . 'assets/js/notifications.js', array( 'anspress-common' ), AP_VERSION, true );
wp_register_script( 'anspress-theme', ap_get_theme_url( 'js/theme.js', false, false ), array( 'anspress-common', 'anspress-question', 'anspress-ask', 'anspress-list', 'anspress-notifications' ), AP_VERSION, true );
wp_register_style( 'anspress-fonts', ap_get_theme_url( 'css/fonts.css', false, false ), array(), AP_VERSION );
wp_register_style( 'anspress-main', ap_get_theme_url( 'css/main.css', false, false ), array( 'anspress-fonts' ), AP_VERSION );
wp_register_style( 'anspress-rtl', ap_get_theme_url( 'css/rtl.css', false, false ), array( 'anspress-main' ), AP_VERSION );
return array();
}
/**
* Enqueue AnsPress assets.
*
* @since 2.4.6
*/
function ap_enqueue_scripts() {
if ( ap_current_page() !== '' ) {
wp_enqueue_script( 'anspress-theme' );
}
wp_enqueue_style( 'anspress-main' );
if ( is_rtl() ) {
wp_enqueue_style( 'anspress-rtl' );
}
}
/**
* Localize scripts for enqueue.
*
* @since 4.4.0
*/
function ap_localize_script() {
$aplang = array(
'loading' => __( 'Loading..', 'anspress-question-answer' ),
'sending' => __( 'Sending request', 'anspress-question-answer' ),
// translators: %s is file size in MB.
'file_size_error' => esc_attr( sprintf( __( 'File size is bigger than %s MB', 'anspress-question-answer' ), round( ap_opt( 'max_upload_size' ) / ( 1024 * 1024 ), 2 ) ) ),
'attached_max' => __( 'You have already attached maximum numbers of allowed attachments', 'anspress-question-answer' ),
'commented' => __( 'commented', 'anspress-question-answer' ),
'comment' => __( 'Comment', 'anspress-question-answer' ),
'cancel' => __( 'Cancel', 'anspress-question-answer' ),
'update' => __( 'Update', 'anspress-question-answer' ),
'your_comment' => __( 'Write your comment...', 'anspress-question-answer' ),
'notifications' => __( 'Notifications', 'anspress-question-answer' ),
'mark_all_seen' => __( 'Mark all as seen', 'anspress-question-answer' ),
'search' => __( 'Search', 'anspress-question-answer' ),
'no_permission_comments' => __( 'Sorry, you don\'t have permission to read comments.', 'anspress-question-answer' ),
/* translators: %s is post type. */
'ajax_events' => __( 'Are you sure you want to %s?', 'anspress-question-answer' ),
'ajax_error' => array(
'snackbar' => array(
'success' => false,
'message' => esc_html__( 'Something went wrong. Please try again.', 'anspress-question-answer' ),
),
'modal' => array(
'imageUpload',
),
),
);
echo '<script type="text/javascript">';
echo 'var ajaxurl = "' . esc_url( admin_url( 'admin-ajax.php' ) ) . '",';
echo 'ap_nonce = "' . esc_attr( wp_create_nonce( 'ap_ajax_nonce' ) ) . '",';
echo 'apTemplateUrl = "' . esc_url( ap_get_theme_url( 'js-template', false, false ) ) . '";';
echo 'apQuestionID = "' . (int) get_question_id() . '";';
echo 'aplang = ' . wp_json_encode( $aplang ) . ';';
echo 'disable_q_suggestion = "' . (bool) ap_opt( 'disable_q_suggestion' ) . '";';
echo '</script>';
}
/**
* Get all list filters.
*/
function ap_get_list_filters() {
$param = array();
$search_q = get_query_var( 'ap_s' );
if ( ! empty( $search_q ) ) {
$param['ap_s'] = $search_q;
}
$filters = array(
'order_by' => array(
'title' => __( 'Order By', 'anspress-question-answer' ),
'items' => array(),
'multiple' => false,
),
);
/*
* Filter question sorting.
* @param array Question sortings.
* @since 2.3
*/
return apply_filters( 'ap_list_filters', $filters );
}
/**
* Output list filters form.
*
* @param string $current_url Current Url.
*/
function ap_list_filters( $current_url = '' ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
$filters = ap_get_list_filters();
echo '<form id="ap-filters" class="ap-filters clearfix" method="GET">';
foreach ( (array) $filters as $key => $filter ) {
$active = '';
$current_order_by = ap_get_current_list_filters( 'order_by' );
if ( ! empty( $current_order_by ) ) {
$active_arr = ap_search_array( ap_get_questions_orderby(), 'value', $current_order_by );
if ( ! empty( $active_arr ) ) {
$active = ': <span class="ap-filter-active">' . $active_arr[0]['label'] . '</span>';
}
}
$active = apply_filters( 'ap_list_filter_active_' . $key, $active, $filter );
$args = wp_json_encode(
array(
'__nonce' => wp_create_nonce( 'filter_' . $key ),
'filter' => $key,
)
);
echo '<div class="ap-dropdown ap-filter filter-' . esc_attr( $key ) . '">';
echo '<a class="ap-dropdown-toggle ap-filter-toggle" href="#" ap-filter apquery="' . esc_js( $args ) . '">' . esc_attr( $filter['title'] ) . wp_kses_post( $active ) . '</a>';
echo '</div>';
}
echo '<button id="ap-filter-reset" type="submit" name="reset-filter" title="' . esc_attr__( 'Reset sorting and filter', 'anspress-question-answer' ) . '"><i class="apicon-x"></i><span>' . esc_attr__( 'Clear Filter', 'anspress-question-answer' ) . '</span></button>';
foreach ( (array) ap_get_current_list_filters() as $key => $value ) {
if ( ! is_array( $value ) ) {
echo '<input type="hidden" value="' . esc_attr( $value ) . '" name="' . esc_attr( $key ) . '" />';
} else {
foreach ( (array) $value as $v ) {
echo '<input type="hidden" value="' . esc_attr( $v ) . '" name="' . esc_attr( $key ) . '[]" />';
}
}
}
echo '</form>';
}
/**
* Print select anser HTML button.
*
* @param mixed $_post Post.
* @return string
*/
function ap_select_answer_btn_html( $_post = null ) {
if ( ! ap_user_can_select_answer( $_post ) ) {
return;
}
$_post = ap_get_post( $_post );
$nonce = wp_create_nonce( 'select-answer-' . $_post->ID );
$q = wp_json_encode(
array(
'answer_id' => $_post->ID,
'__nonce' => $nonce,
)
);
$active = false;
$title = __( 'Select this answer as best', 'anspress-question-answer' );
$label = __( 'Select', 'anspress-question-answer' );
$have_best = ap_have_answer_selected( $_post->post_parent );
$selected = ap_is_selected( $_post );
$hide = false;
if ( $have_best && $selected ) {
$title = __( 'Unselect this answer', 'anspress-question-answer' );
$label = __( 'Unselect', 'anspress-question-answer' );
$active = true;
}
if ( $have_best && ! $selected ) {
$hide = true;
}
return '<a href="#" class="ap-btn-select ap-btn ' . ( $active ? ' active' : '' ) . ( $hide ? ' hide' : '' ) . '" ap="select_answer" apquery="' . esc_js( $q ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $label ) . '</a>';
}
/**
* Output chnage post status button.
*
* @param mixed $_post Post.
* @return null|string
* @since 4.0.0
*/
function ap_post_status_btn_args( $_post = null ) {
$_post = ap_get_post( $_post );
$args = array();
if ( 'trash' === $_post->post_status ) {
return $args;
}
if ( ap_user_can_change_status( $_post->ID ) ) {
global $wp_post_statuses;
$allowed_status = array( 'publish', 'private_post', 'moderate' );
$status_labels = array();
foreach ( (array) $allowed_status as $s ) {
if ( isset( $wp_post_statuses[ $s ] ) ) {
$status_labels[ $s ] = esc_attr( $wp_post_statuses[ $s ]->label );
}
}
foreach ( (array) $status_labels as $slug => $label ) {
$can = true;
if ( 'moderate' === $slug && ! ap_user_can_change_status_to_moderate() ) {
$can = false;
}
if ( $can ) {
$args[] = array(
'cb' => 'status',
'active' => ( $slug === $_post->post_status ),
'query' => array(
'status' => $slug,
'__nonce' => wp_create_nonce( 'change-status-' . $slug . '-' . $_post->ID ),
'post_id' => $_post->ID,
),
'label' => esc_attr( $label ),
);
}
}
return $args;
}
}
/**
* Return set featured question action args.
*
* @param boolean|integer $post_id Post ID.
* @return array
*/
function ap_featured_post_args( $post_id = false ) {
if ( ! is_user_logged_in() || ! ap_user_can_toggle_featured() ) {
return array();
}
if ( false === $post_id ) {
$post_id = get_question_id();
}
$is_featured = ap_is_featured_question( $post_id );
if ( $is_featured ) {
$title = __( 'Unmark this question as featured', 'anspress-question-answer' );
$label = __( 'Unfeature', 'anspress-question-answer' );
} else {
$title = __( 'Mark this question as featured', 'anspress-question-answer' );
$label = __( 'Feature', 'anspress-question-answer' );
}
return array(
'cb' => 'toggle_featured',
'active' => $is_featured,
'query' => array(
'__nonce' => wp_create_nonce( 'set_featured_' . $post_id ),
'post_id' => $post_id,
),
'title' => esc_attr( $title ),
'label' => esc_attr( $label ),
);
}
/**
* Output question subscribe button.
*
* @param object|integer|false $_post Post object or ID.
* @param boolean $output Echo or return.
* @return string|null
* @since 4.0.0
*/
function ap_subscribe_btn( $_post = false, $output = true ) {
$_post = ap_get_post( $_post );
$args = wp_json_encode(
array(
'__nonce' => wp_create_nonce( 'subscribe_' . $_post->ID ),
'id' => $_post->ID,
)
);
$subscribers = (int) ap_get_post_field( 'subscribers', $_post );
$subscribed = ap_is_user_subscriber( 'question', $_post->ID );
$label = $subscribed ? __( 'Unsubscribe', 'anspress-question-answer' ) : __( 'Subscribe', 'anspress-question-answer' );
$html = '<a href="#" class="ap-btn ap-btn-subscribe ap-btn-small ' . ( $subscribed ? 'active' : '' ) . '" apsubscribe apquery="' . esc_js( $args ) . '"><span class="apsubscribers-title">' . esc_html( $label ) . '</span><span class="apsubscribers-count">' . esc_html( $subscribers ) . '</span></a>';
if ( ! $output ) {
return $html;
}
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Create array of object containing AnsPress pages. To be used in admin menu metabox.
*
* @return array
* @since unknown
* @since 4.1.0 Improved ask page object.
*/
function ap_menu_obejct() {
$menu_items = array();
foreach ( (array) anspress()->pages as $k => $args ) {
if ( $args['show_in_menu'] ) {
$object_id = 1;
$object = $k;
$title = $args['title'];
$url = home_url( '/' );
$type = 'anspress-links';
$main_pages = array_keys( ap_main_pages() );
if ( in_array( $k . '_page', $main_pages, true ) ) {
$post = get_post( ap_opt( $k . '_page' ) );
// Proceed only if post is available.
if ( $post ) {
$object_id = ap_opt( $k . '_page' );
$object = 'page';
$url = get_permalink( $post );
$title = $post->post_title;
$type = 'post_type';
}
}
$menu_items[] = (object) array(