Making WordPress.org


Ignore:
Timestamp:
05/15/2024 01:45:40 AM (2 years ago)
Author:
adamwood
Message:

Learn: Sync with git WordPress/learn@af83214

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/taxonomy.php

    r12268 r13705  
    3333        register_included_content();
    3434        register_topic();
     35        register_learning_pathway();
    3536}
    3637
    … …  
    6263        );
    6364
    64         $args   = array(
     65        $args = array(
    6566                'labels'            => $labels,
    6667                'hierarchical'      => true,
    … …  
    7778        );
    7879
    79         register_taxonomy( 'audience', array( 'lesson-plan' ), $args );
     80        register_taxonomy( 'audience', array( 'lesson-plan', 'lesson', 'course' ), $args );
    8081}
    8182
    … …  
    241242        );
    242243
    243         $args   = array(
     244        $args = array(
    244245                'labels'            => $labels,
    245246                'hierarchical'      => true,
    … …  
    256257        );
    257258
    258         register_taxonomy( 'level', array( 'lesson-plan' ), $args );
     259        register_taxonomy( 'level', array( 'lesson-plan', 'lesson', 'course' ), $args );
    259260}
    260261
    … …  
    527528
    528529        register_taxonomy( 'wporg_included_content', $post_types, $args );
     530}
     531
     532/**
     533 * Register the Learning Pathway taxonomy.
     534 */
     535function register_learning_pathway() {
     536        $labels = array(
     537                'name'                       => _x( 'Learning Pathways', 'Taxonomy General Name', 'wporg-learn' ),
     538                'singular_name'              => _x( 'Learning Pathway', 'Taxonomy Singular Name', 'wporg-learn' ),
     539                'menu_name'                  => __( 'Learning pathway', 'wporg-learn' ),
     540                'all_items'                  => __( 'All learning pathways', 'wporg-learn' ),
     541                'parent_item'                => __( 'Parent learning pathway', 'wporg-learn' ),
     542                'parent_item_colon'          => __( 'Parent learning pathway:', 'wporg-learn' ),
     543                'new_item_name'              => __( 'New learning pathway Name', 'wporg-learn' ),
     544                'add_new_item'               => __( 'Add New learning pathway', 'wporg-learn' ),
     545                'edit_item'                  => __( 'Edit learning pathway', 'wporg-learn' ),
     546                'update_item'                => __( 'Update learning pathway', 'wporg-learn' ),
     547                'view_item'                  => __( 'View learning pathway', 'wporg-learn' ),
     548                'separate_items_with_commas' => __( 'Separate learning pathways with commas', 'wporg-learn' ),
     549                'add_or_remove_items'        => __( 'Add or remove learning pathways', 'wporg-learn' ),
     550                'choose_from_most_used'      => __( 'Choose from the most used', 'wporg-learn' ),
     551                'popular_items'              => __( 'Popular learning pathways', 'wporg-learn' ),
     552                'search_items'               => __( 'Search learning pathways', 'wporg-learn' ),
     553                'not_found'                  => __( 'No learning pathway found', 'wporg-learn' ),
     554                'no_terms'                   => __( 'No learning pathways', 'wporg-learn' ),
     555                'items_list'                 => __( 'Learning pathways list', 'wporg-learn' ),
     556                'items_list_navigation'      => __( 'Learning pathways list navigation', 'wporg-learn' ),
     557        );
     558
     559        $args = array(
     560                'labels'            => $labels,
     561                'hierarchical'      => false,
     562                'public'            => true,
     563                'query_var'         => 'wporg_learning_pathway', // Prevent collisions with query params in the archive
     564                'show_ui'           => true,
     565                'show_admin_column' => true,
     566                'show_in_nav_menus' => true,
     567                'show_tagcloud'     => false,
     568                'show_in_rest'      => true,
     569                'capabilities'      => array(
     570                        'assign_terms' => 'edit_others_posts',
     571                ),
     572        );
     573
     574        register_taxonomy( 'learning-pathway', array( 'course' ), $args );
    529575}
    530576
    … …  
    604650        );
    605651}
     652
     653/**
     654 * Get available taxonomy terms for a post type.
     655 *
     656 * @param string $taxonomy The taxonomy.
     657 * @param string $post_type The post type.
     658 * @param string $post_status The post status.
     659 * @return array The available taxonomy terms.
     660 */
     661function get_available_taxonomy_terms( $taxonomy, $post_type, $post_status = null ) {
     662        $posts = get_posts( array(
     663                'post_status'    => $post_status ?? 'any',
     664                'post_type'      => $post_type,
     665                'posts_per_page' => -1,
     666        ) );
     667
     668        if ( empty( $posts ) ) {
     669                return array();
     670        }
     671
     672        $term_ids = array();
     673        foreach ( $posts as $post ) {
     674                $post_terms = wp_get_post_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     675
     676                if ( ! is_wp_error( $post_terms ) ) {
     677                        $term_ids = array_merge( $term_ids, $post_terms );
     678                }
     679        }
     680
     681        if ( empty( $term_ids ) ) {
     682                return array();
     683        }
     684
     685        $term_ids = array_unique( $term_ids );
     686
     687        $term_objects = get_terms( array(
     688                'taxonomy'   => $taxonomy,
     689                'include'    => $term_ids,
     690                'hide_empty' => false,
     691        ) );
     692
     693        return array_reduce( $term_objects, function( $terms, $term_object ) {
     694                $terms[ $term_object->slug ] = $term_object->name;
     695                return $terms;
     696        }, array());
     697}
Note: See TracChangeset for help on using the changeset viewer.