| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Get a list of all registered hand book post types. |
| 5 | * Wrapper function for WPorg_Handbook_Init::get_post_types() |
| 6 | * |
| 7 | * @return array Array with full handbook post type names {post-type}-handbook. |
| 8 | */ |
| 9 | function wporg_get_handbook_post_types() { |
| 10 | |
| 11 | if ( !class_exists( 'WPorg_Handbook_Init' ) ) { |
| 12 | return array(); |
| 13 | } |
| 14 | |
| 15 | $post_types = WPorg_Handbook_Init::get_post_types(); |
| 16 | |
| 17 | foreach ( $post_types as $key => $post_type ) { |
| 18 | if ( 'handbook' !== $post_type ) { |
| 19 | $post_types[ $key ] = $post_type . '-handbook'; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | return $post_types; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Is the query for an existing handbook page? |
| 28 | * |
| 29 | * @param string $handbook Handbook post type. |
| 30 | * @return bool Whether the query is for an existing handbook page. Returns true on handbook pages. |
| 31 | */ |
| 32 | function wporg_is_handbook( $handbook = '' ) { |
| 33 | |
| 34 | $post_types = wporg_get_handbook_post_types(); |
| 35 | |
| 36 | if ( is_admin() || empty( $post_types ) ) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | foreach ( $post_types as $post_type ) { |
| 41 | |
| 42 | $is_handbook = !$handbook || ( $handbook === $post_type ); |
| 43 | $handbook_query = is_singular( $post_type ) || is_post_type_archive( $post_type ); |
| 44 | |
| 45 | if ( $is_handbook && $handbook_query ) { |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Is the current (or specified) post_type a handbook post type? |
| 55 | * |
| 56 | * @param string $post_type Optional. The post_type to check for being a handbook post type. Default '' (the current post type). |
| 57 | * @return bool |
| 58 | */ |
| 59 | function wporg_is_handbook_post_type( $post_type = '' ) { |
| 60 | if ( ! $post_type ) { |
| 61 | $post_type = get_post_type(); |
| 62 | } |
| 63 | |
| 64 | return in_array( $post_type, wporg_get_handbook_post_types() ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return the current handbook post type. |
| 69 | * |
| 70 | * @return string|false Post type on success, false on failure. |
| 71 | */ |
| 72 | function wporg_get_current_handbook() { |
| 73 | $handbooks = wporg_get_handbook_post_types(); |
| 74 | |
| 75 | foreach ( $handbooks as $handbook ) { |
| 76 | if ( wporg_is_handbook( $handbook ) ) { |
| 77 | return $handbook; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return false; |
| 82 | } |