AnsPress_Tag()
Description #
Tags addon for AnsPress
Source #
File: addons/free/tag.php
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 | class AnsPress_Tag { /** * Initialize the class */ public static function init() { ap_register_page( 'tag' , __( 'Tag' , 'anspress-question-answer' ), [ __CLASS__ , 'tag_page' ], false ); ap_register_page( 'tags' , __( 'Tags' , 'anspress-question-answer' ), [ __CLASS__ , 'tags_page' ] ); anspress()->add_action( 'ap_form_addon-free_tag' , __CLASS__ , 'option_fields' ); anspress()->add_action( 'widgets_init' , __CLASS__ , 'widget_positions' ); anspress()->add_action( 'init' , __CLASS__ , 'register_question_tag' , 1 ); anspress()->add_action( 'ap_admin_menu' , __CLASS__ , 'admin_tags_menu' ); anspress()->add_action( 'ap_display_question_metas' , __CLASS__ , 'ap_display_question_metas' , 10, 2 ); anspress()->add_action( 'ap_question_info' , __CLASS__ , 'ap_question_info' ); anspress()->add_action( 'ap_assets_js' , __CLASS__ , 'ap_assets_js' ); anspress()->add_action( 'ap_enqueue' , __CLASS__ , 'ap_localize_scripts' ); anspress()->add_filter( 'term_link' , __CLASS__ , 'term_link_filter' , 10, 3 ); anspress()->add_action( 'ap_question_form_fields' , __CLASS__ , 'ap_question_form_fields' ); anspress()->add_action( 'ap_processed_new_question' , __CLASS__ , 'after_new_question' , 0, 2 ); anspress()->add_action( 'ap_processed_update_question' , __CLASS__ , 'after_new_question' , 0, 2 ); anspress()->add_filter( 'ap_page_title' , __CLASS__ , 'page_title' ); anspress()->add_filter( 'ap_breadcrumbs' , __CLASS__ , 'ap_breadcrumbs' ); anspress()->add_action( 'wp_ajax_ap_tags_suggestion' , __CLASS__ , 'ap_tags_suggestion' ); anspress()->add_action( 'wp_ajax_nopriv_ap_tags_suggestion' , __CLASS__ , 'ap_tags_suggestion' ); anspress()->add_action( 'ap_rewrites' , __CLASS__ , 'rewrite_rules' , 10, 3 ); anspress()->add_filter( 'ap_main_questions_args' , __CLASS__ , 'ap_main_questions_args' ); anspress()->add_filter( 'ap_current_page' , __CLASS__ , 'ap_current_page' ); anspress()->add_action( 'posts_pre_query' , __CLASS__ , 'modify_query_archive' , 9999, 2 ); // List filtering. anspress()->add_filter( 'ap_list_filters' , __CLASS__ , 'ap_list_filters' ); anspress()->add_action( 'ap_ajax_load_filter_qtag' , __CLASS__ , 'load_filter_tag' ); anspress()->add_action( 'ap_ajax_load_filter_tags_order' , __CLASS__ , 'load_filter_tags_order' ); anspress()->add_filter( 'ap_list_filter_active_qtag' , __CLASS__ , 'filter_active_tag' , 10, 2 ); anspress()->add_filter( 'ap_list_filter_active_tags_order' , __CLASS__ , 'filter_active_tags_order' , 10, 2 ); } /** * Tag page layout. * * @since 4.1.0 Use `get_queried_object()` to get current term. */ public static function tag_page() { global $question_tag ; $question_tag = get_queried_object(); $question_args = array ( 'paged' => max( 1, get_query_var( 'ap_paged' ) ), 'tax_query' => array ( array ( 'taxonomy' => 'question_tag' , 'field' => 'id' , 'terms' => array ( $question_tag ->term_id ), ), ), ); $question_args = apply_filters( 'ap_tag_question_query_args' , $question_args ); if ( $question_tag ) { anspress()->questions = ap_get_questions( $question_args ); include ( ap_get_theme_location( 'addons/tag/tag.php' ) ); } } /** * Tags page layout */ public static function tags_page() { global $question_tags , $ap_max_num_pages , $ap_per_page , $tags_rows_found ; $paged = max( 1, get_query_var( 'paged' ) ); $per_page = (int) ap_opt( 'tags_per_page' ); $per_page = 0 === $per_page ? 1 : $per_page ; $offset = $per_page * ( $paged - 1); $tag_args = array ( 'taxonomy' => 'question_tag' , 'ap_tags_query' => true, 'parent' => 0, 'number' => $per_page , 'offset' => $offset , 'hide_empty' => false, 'order' => 'DESC' , ); $ap_sort = ap_isset_post_value( 'tags_order' , 'count' ); if ( 'new' === $ap_sort ) { $tag_args [ 'orderby' ] = 'id' ; $tag_args [ 'order' ] = 'DESC' ; } elseif ( 'name' === $ap_sort ) { $tag_args [ 'orderby' ] = 'name' ; $tag_args [ 'order' ] = 'ASC' ; } else { $tag_args [ 'orderby' ] = 'count' ; } if ( ap_isset_post_value( 'ap_s' ) ) { $tag_args [ 'search' ] = ap_sanitize_unslash( 'ap_s' , 'r' ); } /** * Filter applied before getting tags. * * @var array */ $tag_args = apply_filters( 'ap_tags_shortcode_args' , $tag_args ); $query = new WP_Term_Query( $tag_args ); // Count terms. $tag_args [ 'fields' ] = 'count' ; $found_query = new WP_Term_Query( $tag_args ); $tags_rows_found = $found_query ->get_terms(); $ap_max_num_pages = ceil ( $tags_rows_found / $per_page ); $question_tags = $query ->get_terms(); include ap_get_theme_location( 'addons/tag/tags.php' ); } /** * Register widget position. */ public static function widget_positions() { register_sidebar( array ( 'name' => __( '(AnsPress) Tags' , 'anspress-question-answer' ), 'id' => 'ap-tags' , 'before_widget' => '<div id="%1$s" class="ap-widget-pos %2$s">' , 'after_widget' => '</div>' , 'description' => __( 'Widgets in this area will be shown in anspress tags page.' , 'anspress-question-answer' ), 'before_title' => '<h3 class="ap-widget-title">' , 'after_title' => '</h3>' , ) ); } /** * Register tag taxonomy for question cpt. * * @return void * @since 2.0 */ public static function register_question_tag() { ap_add_default_options([ 'max_tags' => 5, 'min_tags' => 1, 'tags_per_page' => 20, 'tag_page_slug' => 'tag' , ]); $tag_labels = array ( 'name' => __( 'Question Tags' , 'anspress-question-answer' ), 'singular_name' => _x( 'Tag' , 'anspress-question-answer' ), 'all_items' => __( 'All Tags' , 'anspress-question-answer' ), 'add_new_item' => _x( 'Add New Tag' , 'anspress-question-answer' ), 'edit_item' => __( 'Edit Tag' , 'anspress-question-answer' ), 'new_item' => __( 'New Tag' , 'anspress-question-answer' ), 'view_item' => __( 'View Tag' , 'anspress-question-answer' ), 'search_items' => __( 'Search Tag' , 'anspress-question-answer' ), 'not_found' => __( 'Nothing Found' , 'anspress-question-answer' ), 'not_found_in_trash' => __( 'Nothing found in Trash' , 'anspress-question-answer' ), 'parent_item_colon' => '' , ); /** * FILTER: ap_question_tag_labels * Filter ic called before registering question_tag taxonomy */ $tag_labels = apply_filters( 'ap_question_tag_labels' , $tag_labels ); $tag_args = array ( 'hierarchical' => true, 'labels' => $tag_labels , 'rewrite' => false, ); /** * FILTER: ap_question_tag_args * Filter ic called before registering question_tag taxonomy */ $tag_args = apply_filters( 'ap_question_tag_args' , $tag_args ); /** * Now let WordPress know about our taxonomy */ register_taxonomy( 'question_tag' , array ( 'question' ), $tag_args ); } /** * Add tags menu in wp-admin. */ public static function admin_tags_menu() { add_submenu_page( 'anspress' , __( 'Question Tags' , 'anspress-question-answer' ), __( 'Tags' , 'anspress-question-answer' ), 'manage_options' , 'edit-tags.php?taxonomy=question_tag' ); } /** * Register option fields. */ public static function option_fields() { $opt = ap_opt(); $form = array ( 'fields' => array ( 'tags_per_page' => array ( 'label' => __( 'Tags to show' , 'anspress-question-answer' ), 'description' => __( 'Numbers of tags to show in tags page.' , 'anspress-question-answer' ), 'subtype' => 'number' , 'value' => $opt [ 'tags_per_page' ], ), 'max_tags' => array ( 'label' => __( 'Maximum tags' , 'anspress-question-answer' ), 'description' => __( 'Maximum numbers of tags that user can add when asking.' , 'anspress-question-answer' ), 'subtype' => 'number' , 'value' => $opt [ 'max_tags' ], ), 'min_tags' => array ( 'label' => __( 'Minimum tags' , 'anspress-question-answer' ), 'description' => __( 'minimum numbers of tags that user must add when asking.' , 'anspress-question-answer' ), 'subtype' => 'number' , 'value' => $opt [ 'min_tags' ], ), 'tag_page_slug' => array ( 'label' => __( 'Tag page slug' , 'anspress-question-answer' ), 'desc' => __( 'Slug for tag page' , 'anspress-question-answer' ), 'value' => $opt [ 'tag_page_slug' ], ), ), ); return $form ; } /** * Append meta display. * * @param array $metas Display metas. * @param array $question_id Post ID. * @return array * @since 2.0 */ public static function ap_display_question_metas( $metas , $question_id ) { if ( ap_post_have_terms( $question_id , 'question_tag' ) ) { $metas [ 'tags' ] = ap_question_tags_html( array ( 'label' => '<i class="apicon-tag"></i>' , 'show' => 1 ) ); } return $metas ; } /** * Hook tags after post. * * @param object $post Post object. * @return string * @since 1.0 */ public static function ap_question_info( $post ) { if ( ap_question_have_tags() ) { echo '<div class="widget"><span class="ap-widget-title">' . esc_attr__( 'Tags' , 'anspress-question-answer' ) . '</span>' ; echo '<div class="ap-post-tags clearfix">' . ap_question_tags_html( [ 'list' => true, 'label' => '' ] ) . '</div></div>' ; // WPCS: xss okay. } } /** * Enqueue scripts. * * @param array $js Javacript array. * @return array */ public static function ap_assets_js( $js ) { $js [ 'tags' ] = [ 'dep' => [ 'anspress-main' ], 'footer' => true ]; return $js ; } /** * Add translated strings to the javascript files */ public static function ap_localize_scripts() { $l10n_data = array ( 'deleteTag' => __( 'Delete Tag' , 'anspress-question-answer' ), 'addTag' => __( 'Add Tag' , 'anspress-question-answer' ), 'tagAdded' => __( 'added to the tags list.' , 'anspress-question-answer' ), 'tagRemoved' => __( 'removed from the tags list.' , 'anspress-question-answer' ), 'suggestionsAvailable' => __( 'Suggestions are available. Use the up and down arrow keys to read it.' , 'anspress-question-answer' ), ); wp_localize_script( 'anspress-tags' , 'apTagsTranslation' , $l10n_data ); } /** * Filter tag term link. * * @param string $url Default URL of taxonomy. * @param array $term Term array. * @param string $taxonomy Taxonomy type. * @return string New URL for term. */ public static function term_link_filter( $url , $term , $taxonomy ) { if ( 'question_tag' === $taxonomy ) { if ( get_option( 'permalink_structure' ) != '' ) { $opt = get_option( 'ap_tags_path' , 'tags' ); return home_url( $opt ) . '/' . $term ->slug . '/' ; } else { return add_query_arg( [ 'ap_page' => 'tag' , 'question_tag' => $term ->slug ], home_url() ); } } return $url ; } /** * Add tag field in question form. * * @param array $form AnsPress form arguments. * @since 4.1.0 */ public static function ap_question_form_fields( $form ) { $editing_id = ap_sanitize_unslash( 'id' , 'r' ); $form [ 'fields' ][ 'tags' ] = array ( 'label' => __( 'Tags' , 'anspress-question-answer' ), 'desc' => sprintf( // Translators: %1$d contain minimum tags required and %2$d contain maximum tags allowed. __( 'Tagging will helps others to easily find your question. Minimum %1$d and maximum %2$d tags.' , 'anspress-question-answer' ), ap_opt( 'min_tags' ), ap_opt( 'max_tags' ) ), 'type' => 'tags' , 'array_max' => ap_opt( 'max_tags' ), 'array_min' => ap_opt( 'min_tags' ), 'js_options' => array ( 'create' => true, ), ); // Add value when editing post. if ( ! empty ( $editing_id ) ) { $tags = get_the_terms( $editing_id , 'question_tag' ); if ( $tags ) { $tags = wp_list_pluck( $tags , 'term_id' ); $form [ 'fields' ][ 'tags' ][ 'value' ] = $tags ; } } return $form ; } /** * Things to do after creating a question. * * @param integer $post_id Post ID. * @param object $post Post object. * @since 1.0 */ public static function after_new_question( $post_id , $post ) { $values = anspress()->get_form( 'question' )->get_values(); if ( isset( $values [ 'tags' ] ) ) { wp_set_object_terms( $post_id , $values [ 'tags' ][ 'value' ], 'question_tag' ); } } /** * Tags page title. * * @param string $title Title. * @return string */ public static function page_title( $title ) { if ( is_question_tags() ) { $title = ap_opt( 'tags_page_title' ); } elseif ( is_question_tag() ) { $tag_id = sanitize_title( get_query_var( 'q_tag' ) ); $tag = get_term_by( 'slug' , $tag_id , 'question_tag' ); // @codingStandardsIgnoreLine. $title = $tag ->name; } return $title ; } /** * Hook into AnsPress breadcrums to show tags page. * * @param array $navs Breadcrumbs navs. * @return array */ public static function ap_breadcrumbs( $navs ) { if ( is_question_tag() ) { $tag_id = sanitize_title( get_query_var( 'q_tag' ) ); $tag = get_term_by( 'slug' , $tag_id , 'question_tag' ); // @codingStandardsIgnoreLine. $navs [ 'page' ] = array ( 'title' => __( 'Tags' , 'anspress-question-answer' ), 'link' => ap_get_link_to( 'tags' ), 'order' => 8 ); if ( $tag ) { $navs [ 'tag' ] = array ( 'title' => $tag ->name, 'link' => get_term_link( $tag , 'question_tag' ), // @codingStandardsIgnoreLine. 'order' => 8, ); } } elseif ( is_question_tags() ) { $navs [ 'page' ] = array ( 'title' => __( 'Tags' , 'anspress-question-answer' ), 'link' => ap_get_link_to( 'tags' ), 'order' => 8 ); } return $navs ; } /** * Handle tags suggestion on question form */ public static function ap_tags_suggestion() { $keyword = ap_sanitize_unslash( 'q' , 'r' ); $tags = get_terms( 'question_tag' , array ( 'orderby' => 'count' , 'order' => 'DESC' , 'hide_empty' => false, 'search' => $keyword , 'number' => 8, )); if ( $tags ) { $items = array (); foreach ( $tags as $k => $t ) { $items [ $k ] = $t ->slug; } $result = array ( 'status' => true, 'items' => $items ); wp_send_json( $result ); } wp_send_json( array ( 'status' => false ) ); } /** * Add category pages rewrite rule. * * @param array $rules AnsPress rules. * @return array */ public static function rewrite_rules( $rules , $slug , $base_page_id ) { $base_slug = get_page_uri( ap_opt( 'tags_page' ) ); update_option( 'ap_tags_path' , $base_slug , true ); $cat_rules = array ( $base_slug . '/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?question_tag=$matches[#]&ap_paged=$matches[#]&ap_page=tag' , $base_slug . '/([^/]+)/?$' => 'index.php?question_tag=$matches[#]&ap_page=tag' , ); return $cat_rules + $rules ; } /** * Filter main questions query args. Modify and add tags args. * * @param array $args Questions args. * @return array */ public static function ap_main_questions_args( $args ) { global $wp ; $query = $wp ->query_vars; $current_filter = ap_get_current_list_filters( 'qtag' ); $tags_operator = ! empty ( $wp ->query_vars[ 'ap_tags_operator' ] ) ? $wp ->query_vars[ 'ap_tags_operator' ] : 'IN' ; if ( isset( $query [ 'ap_tags' ] ) && is_array ( $query [ 'ap_tags' ] ) ) { $args [ 'tax_query' ][] = array ( 'taxonomy' => 'question_tag' , 'field' => 'slug' , 'terms' => $query [ 'ap_tags' ], 'operator' => $tags_operator , ); } elseif ( ! empty ( $current_filter ) ) { $args [ 'tax_query' ][] = array ( 'taxonomy' => 'question_tag' , 'field' => 'term_id' , 'terms' => $current_filter , ); } return $args ; } /** * Add tags sorting in list filters * * @return array */ public static function ap_list_filters( $filters ) { global $wp ; if ( ! isset( $wp ->query_vars[ 'ap_tags' ] ) ) { $filters [ 'qtag' ] = array ( 'title' => __( 'Tag' , 'anspress-question-answer' ), 'search' => true, 'multiple' => true, ); } if ( 'tags' === ap_current_page() ) { return array ( 'tags_order' => array ( 'title' => __( 'Order' , 'anspress-question-answer' ), ) ); } return $filters ; } /** * Ajax callback for loading order by filter. * * @since 4.0.0 */ public static function load_filter_tag() { $filter = ap_sanitize_unslash( 'filter' , 'r' ); check_ajax_referer( 'filter_' . $filter , '__nonce' ); $search = ap_sanitize_unslash( 'search' , 'r' , false ); ap_ajax_json( array ( 'success' => true, 'items' => ap_get_tag_filter( $search ), 'multiple' => true, 'nonce' => wp_create_nonce( 'filter_' . $filter ), )); } /** * Ajax callback for loading order by filter for tags. * * @since 4.0.0 */ public static function load_filter_tags_order() { $filter = ap_sanitize_unslash( 'filter' , 'r' ); check_ajax_referer( 'filter_' . $filter , '__nonce' ); ap_ajax_json( array ( 'success' => true, 'items' => array ( [ 'key' => 'tags_order' , 'value' => 'popular' , 'label' => __( 'Popular' , 'anspress-question-answer' ) ], [ 'key' => 'tags_order' , 'value' => 'new' , 'label' => __( 'New' , 'anspress-question-answer' ) ], [ 'key' => 'tags_order' , 'value' => 'name' , 'label' => __( 'Name' , 'anspress-question-answer' ) ], ), 'nonce' => wp_create_nonce( 'filter_' . $filter ), )); } /** * Output active tag in filter * * @since 4.0.0 */ public static function filter_active_tag( $active , $filter ) { $current_filters = ap_get_current_list_filters( 'qtag' ); if ( ! empty ( $current_filters ) ) { $args = array ( 'hierarchical' => true, 'hide_if_empty' => true, 'number' => 2, 'include' => $current_filters , ); $terms = get_terms( 'question_tag' , $args ); if ( $terms ) { $active_terms = []; foreach ( ( array ) $terms as $t ) { $active_terms [] = $t ->name; } $count = count ( $current_filters ); $more_label = sprintf( __( ', %d+' , 'anspress-question-answer' ), $count - 2 ); return ': <span class="ap-filter-active">' . implode( ', ' , $active_terms ) . ( $count > 2 ? $more_label : '' ) . '</span>' ; } } } /** * Output active tags_order in filter * * @since 4.1.0 */ public static function filter_active_tags_order( $active , $filter ) { $tags_order = ap_get_current_list_filters( 'tags_order' ); $tags_order = ! empty ( $tags_order ) ? $tags_order : 'popular' ; $orders = array ( 'popular' => __( 'Popular' , 'anspress-question-answer' ), 'new' => __( 'New' , 'anspress-question-answer' ), 'name' => __( 'Name' , 'anspress-question-answer' ), ); $active = isset( $orders [ $tags_order ] ) ? $orders [ $tags_order ] : '' ; return ': <span class="ap-filter-active">' . $active . '</span>' ; } /** * Modify current page to show tag archive. * * @param string $query_var Current page. * @return string * @since 4.1.0 */ public static function ap_current_page( $query_var ) { if ( 'tags' === $query_var && 'tag' === get_query_var( 'ap_page' ) ) { return 'tag' ; } return $query_var ; } /** * Modify main query to show tag archive. * * @param array|null $posts Array of objects. * @param object $query Wp_Query object. * * @return array|null * @since 4.1.0 */ public static function modify_query_archive( $posts , $query ) { if ( $query ->is_main_query() && $query ->is_tax( 'question_tag' ) && 'tag' === get_query_var( 'ap_page' ) ) { $query ->found_posts = 1; $query ->max_num_pages = 1; $page = get_page( ap_opt( 'tags_page' ) ); $page ->post_title = get_queried_object()->name; $posts = [ $page ]; } return $posts ; } } |
Expand full source code Collapse full source code View on GitHub: addons/free/tag.php:28
Add your comment