Changeset 13431 for trunk/src/bp-core/bp-core-functions.php
- Timestamp:
- 02/24/2023 09:37:44 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-functions.php
r13377 r13431 698 698 699 699 /** 700 * Get the directory pages post type. 701 * 702 * @since 12.0.0 703 * 704 * @return string The post type to use for directory pages. 705 */ 706 function bp_core_get_directory_post_type() { 707 $post_type = 'buddypress'; 708 709 /** 710 * Filter here to edit the post type to use for directory pages. 711 * 712 * @since 12.0.0 713 * 714 * @param string $post_type The post type to use for directory pages. 715 */ 716 return apply_filters( 'bp_core_get_directory_post_type', $post_type ); 717 } 718 719 /** 700 720 * Get names and slugs for BuddyPress component directory pages. 701 721 * … … 834 854 // Create the pages. 835 855 foreach ( $pages_to_create as $component_name => $page_name ) { 836 $exist s = get_page_by_path( $component_name );856 $existing_id = bp_core_get_directory_page_id( $component_name ); 837 857 838 858 // If page already exists, use it. 839 if ( ! empty( $exist s) ) {840 $pages[ $component_name ] = $exists->ID;859 if ( ! empty( $existing_id ) ) { 860 $pages[ $component_name ] = (int) $existing_id; 841 861 } else { 842 862 $pages[ $component_name ] = wp_insert_post( array( … … 845 865 'post_status' => 'publish', 846 866 'post_title' => $page_name, 847 'post_type' => 'page',867 'post_type' => bp_core_get_directory_post_type(), 848 868 ) ); 849 869 } … … 887 907 888 908 /** 909 * Make sure Components directory page `post_name` are unique. 910 * 911 * Goal is to avoid a slug conflict between a Page and a Component's directory page `post_name`. 912 * 913 * @since 12.0.0 914 * 915 * @param string $slug The post slug. 916 * @param int $post_ID Post ID. 917 * @param string $post_status The post status. 918 * @param string $post_type Post type. 919 * @param int $post_parent Post parent ID. 920 * @param string $original_slug The original post slug. 921 */ 922 function bp_core_set_unique_directory_page_slug( $slug = '', $post_ID = 0, $post_status = '', $post_type = '', $post_parent = 0, $original_slug = '' ) { 923 if ( ( 'buddypress' === $post_type || 'page' === $post_type ) && $slug === $original_slug ) { 924 $pages = get_posts( 925 array( 926 'post__not_in' => array( $post_ID ), 927 'post_status' => array( 'publish', 'bp_restricted' ), 928 'post_type' => array( 'buddypress', 'page' ), 929 ) 930 ); 931 932 $illegal_names = wp_list_pluck( $pages, 'post_name' ); 933 if ( is_multisite() && ! is_subdomain_install() ) { 934 $current_site = get_current_site(); 935 $site = get_site_by_path( $current_site->domain, trailingslashit( $current_site->path ) . $slug ); 936 937 if ( isset( $site->blog_id ) && 1 !== $site->blog_id ) { 938 $illegal_names[] = $slug; 939 } 940 } 941 942 if ( in_array( $slug, $illegal_names, true ) ) { 943 $suffix = 2; 944 do { 945 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 946 $post_name_check = in_array( $alt_post_name, $illegal_names, true ); 947 $suffix++; 948 } while ( $post_name_check ); 949 $slug = $alt_post_name; 950 } 951 } 952 953 return $slug; 954 } 955 add_filter( 'wp_unique_post_slug', 'bp_core_set_unique_directory_page_slug', 10, 6 ); 956 957 /** 889 958 * Remove the entry from bp_pages when the corresponding WP page is deleted. 890 959 * … … 914 983 } 915 984 add_action( 'delete_post', 'bp_core_on_directory_page_delete' ); 916 917 /**918 * Create a default component slug from a WP page root_slug.919 *920 * Since 1.5, BP components get their root_slug (the slug used immediately921 * following the root domain) from the slug of a corresponding WP page.922 *923 * E.g. if your BP installation at example.com has its members page at924 * example.com/community/people, $bp->members->root_slug will be925 * 'community/people'.926 *927 * By default, this function creates a shorter version of the root_slug for928 * use elsewhere in the URL, by returning the content after the final '/'929 * in the root_slug ('people' in the example above).930 *931 * Filter on 'bp_core_component_slug_from_root_slug' to override this method932 * in general, or define a specific component slug constant (e.g.933 * BP_MEMBERS_SLUG) to override specific component slugs.934 *935 * @since 1.5.0936 *937 * @param string $root_slug The root slug, which comes from $bp->pages->[component]->slug.938 * @return string The short slug for use in the middle of URLs.939 */940 function bp_core_component_slug_from_root_slug( $root_slug ) {941 $slug_chunks = explode( '/', $root_slug );942 $slug = array_pop( $slug_chunks );943 944 /**945 * Filters the default component slug from a WP page root_slug.946 *947 * @since 1.5.0948 *949 * @param string $slug Short slug for use in the middle of URLs.950 * @param string $root_slug The root slug which comes from $bp->pages-[component]->slug.951 */952 return apply_filters( 'bp_core_component_slug_from_root_slug', $slug, $root_slug );953 }954 955 /**956 * Add support for a top-level ("root") component.957 *958 * This function originally (pre-1.5) let plugins add support for pages in the959 * root of the install. These root level pages are now handled by actual960 * WordPress pages and this function is now a convenience for compatibility961 * with the new method.962 *963 * @since 1.0.0964 *965 * @param string $slug The slug of the component being added to the root list.966 */967 function bp_core_add_root_component( $slug ) {968 $bp = buddypress();969 970 if ( empty( $bp->pages ) ) {971 $bp->pages = bp_core_get_directory_pages();972 }973 974 $match = false;975 976 // Check if the slug is registered in the $bp->pages global.977 foreach ( (array) $bp->pages as $key => $page ) {978 if ( $key == $slug || $page->slug == $slug ) {979 $match = true;980 }981 }982 983 // Maybe create the add_root array.984 if ( empty( $bp->add_root ) ) {985 $bp->add_root = array();986 }987 988 // If there was no match, add a page for this root component.989 if ( empty( $match ) ) {990 $add_root_items = $bp->add_root;991 $add_root_items[] = $slug;992 $bp->add_root = $add_root_items;993 }994 995 // Make sure that this component is registered as requiring a top-level directory.996 if ( isset( $bp->{$slug} ) ) {997 $bp->loaded_components[$bp->{$slug}->slug] = $bp->{$slug}->id;998 $bp->{$slug}->has_directory = true;999 }1000 }1001 1002 /**1003 * Create WordPress pages to be used as BP component directories.1004 *1005 * @since 1.5.01006 */1007 function bp_core_create_root_component_page() {1008 1009 // Get BuddyPress.1010 $bp = buddypress();1011 1012 $new_page_ids = array();1013 1014 foreach ( (array) $bp->add_root as $slug ) {1015 $new_page_ids[ $slug ] = wp_insert_post( array(1016 'comment_status' => 'closed',1017 'ping_status' => 'closed',1018 'post_title' => ucwords( $slug ),1019 'post_status' => 'publish',1020 'post_type' => 'page'1021 ) );1022 }1023 1024 $page_ids = array_merge( $new_page_ids, bp_core_get_directory_page_ids( 'all' ) );1025 bp_core_update_directory_page_ids( $page_ids );1026 }1027 985 1028 986 /** … … 4820 4778 '10.0', 4821 4779 '11.0', 4780 '12.0', 4822 4781 ); 4823 4782 … … 4892 4851 return $latest_deprecated_functions_versions; 4893 4852 } 4853 4854 /** 4855 * Get the BuddyPress Post Type site ID. 4856 * 4857 * @since 12.0.0 4858 * 4859 * @return int The site ID the BuddyPress Post Type should be registered on. 4860 */ 4861 function bp_get_post_type_site_id() { 4862 $site_id = bp_get_root_blog_id(); 4863 4864 /** 4865 * Filter here to edit the site ID. 4866 * 4867 * @todo This will need to be improved to take in account 4868 * specific configurations like multiblog. 4869 * 4870 * @since 12.0.0 4871 * 4872 * @param integer $site_id The site ID to register the post type on. 4873 */ 4874 return (int) apply_filters( 'bp_get_post_type_site_id', $site_id ); 4875 }
Note: See TracChangeset
for help on using the changeset viewer.