diff --git a/wp-content/plugins/core/src/Core.php b/wp-content/plugins/core/src/Core.php index 833ca084..14d7623e 100644 --- a/wp-content/plugins/core/src/Core.php +++ b/wp-content/plugins/core/src/Core.php @@ -34,6 +34,7 @@ class Core { // Post Types Post_Types\Page\Page_Subscriber::class, Post_Types\Post\Post_Subscriber::class, + Post_Types\Training\Training_Subscriber::class, ]; private static self $instance; diff --git a/wp-content/plugins/core/src/Integrations/Integrations_Subscriber.php b/wp-content/plugins/core/src/Integrations/Integrations_Subscriber.php index d3657426..9ee0a7ea 100644 --- a/wp-content/plugins/core/src/Integrations/Integrations_Subscriber.php +++ b/wp-content/plugins/core/src/Integrations/Integrations_Subscriber.php @@ -12,6 +12,22 @@ public function register(): void { add_filter( 'acf/settings/show_admin', function ( $show ): bool { return $this->container->get( ACF::class )->show_acf_menu_item( (bool) $show ); }, 10, 1 ); + + add_filter( 'wpseo_accessible_post_types', function ( $post_types ) { + if ( ! is_array( $post_types ) ) { + return []; + } + + return $this->container->get( YoastSEO::class )->exclude_post_types( $post_types ); + }, 100 ); + + add_filter( 'rank_math/excluded_post_types', function ( $post_types ) { + if ( ! is_array( $post_types ) ) { + return []; + } + + return $this->container->get( RankMath::class )->exclude_post_types( $post_types ); + }, 100 ); } } diff --git a/wp-content/plugins/core/src/Integrations/RankMath.php b/wp-content/plugins/core/src/Integrations/RankMath.php new file mode 100644 index 00000000..261acbbb --- /dev/null +++ b/wp-content/plugins/core/src/Integrations/RankMath.php @@ -0,0 +1,15 @@ + $this->post_type, + 'delete_with_user' => false, + 'exclude_from_search' => true, + 'has_archive' => false, + 'hierarchical' => false, + 'map_meta_cap' => false, + 'menu_icon' => 'dashicons-welcome-learn-more', + 'menu_position' => 19, + 'public' => true, + 'publicly_queryable' => true, + 'rewrite' => [ 'slug' => $this->post_type, 'with_front' => false ], + 'show_in_nav_menus' => false, + 'show_in_rest' => current_user_can( 'read_' . $this->post_type ), + 'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' ], + ]; + } + + public function get_labels(): array { + return [ + 'singular' => __( 'Training Doc', 'tribe' ), + 'plural' => __( 'Training Docs', 'tribe' ), + 'slug' => $this->post_type, + ]; + } + + /** + * Define block templates for initial editor state + */ + public function register_block_template(): void { + $post_type_object = get_post_type_object( $this->post_type ); + $post_type_object->template = [ + [ + 'core/pattern', + [ + 'slug' => 'patterns/page', // Use patterns for Pages. + ], + ], + ]; + } + + /** + * Add capabilities to user roles. + */ + public function add_user_caps(): void { + global $wp_roles; + + if ( ! $wp_roles || ! is_object( $wp_roles ) || ! property_exists( $wp_roles, 'roles' ) || ! is_array( $wp_roles->roles ) ) { + return; + } + + $post_type_object = get_post_type_object( $this->post_type ); + $adjust_roles = [ + 'administrator', + 'editor', + 'author', + 'contributor', + ]; + + foreach ( $adjust_roles as $role ) { + $role = get_role( $role ); + + foreach ( (array) $post_type_object->cap as $cap ) { + $role->add_cap( $cap ); + } + } + } + + /** + * Check the user is authorized to read the post; + * if not, send and display 404. + */ + public function send_404_unauthorized(): void { + if ( is_post_type_viewable( $this->post_type ) ) { + return; + } + + global $wp_query; + + $wp_query->set_404(); + status_header( 404 ); + nocache_headers(); + + require_once get_query_template( '404' ); + exit; + } + + /** + * Indicate if the post type is viewable. + */ + public function current_user_can_read( bool $bool, \WP_Post_Type $post_type_object ): bool { + if ( ! $bool ) { + return false; + } + + if ( $post_type_object->name !== $this->post_type ) { + return true; + } + + return current_user_can( $post_type_object->cap->read_post ); + } + + /** + * Remove filters from list table. + */ + public function list_table_filters( string $post_type ): void { + if ( $post_type !== $this->post_type ) { + return; + } + + if ( ! has_action( current_action() ) ) { + return; + } + + remove_all_actions( current_action() ); + } + + /** + * Limit list table columns to checkbox and title. + */ + public function list_table_columns(): array { + return [ + 'cb' => '', + 'title' => 'Title', + ]; + } + +} diff --git a/wp-content/plugins/core/src/Post_Types/Training/Training.php b/wp-content/plugins/core/src/Post_Types/Training/Training.php new file mode 100644 index 00000000..e7f0c564 --- /dev/null +++ b/wp-content/plugins/core/src/Post_Types/Training/Training.php @@ -0,0 +1,11 @@ +block_templates(); + $this->user_permissions(); + $this->list_table(); + } + + public function block_templates(): void { + add_action( 'init', function (): void { + $this->container->get( Config::class )->register_block_template(); + } ); + } + + public function user_permissions(): void { + add_action( 'init', function (): void { + $this->container->get( Config::class )->add_user_caps(); + } ); + + // Useful for excluding from sitemaps and controlling user access. + add_filter( 'is_post_type_viewable', function ( $bool, $post_type_object ): bool { + return $this->container->get( Config::class )->current_user_can_read( $bool, $post_type_object ); + }, 10, 2 ); + + // Send 404 for unauthorized users. + add_action( 'template_redirect', function (): void { + $this->container->get( Config::class )->send_404_unauthorized(); + } ); + } + + public function list_table(): void { + // Prevent filtering added by plugins. + add_action( 'restrict_manage_posts', function ( $post_type ): void { + $this->container->get( Config::class )->list_table_filters( $post_type ); + }, 0 ); + + // Remove columns added by plugins. + add_filter( 'manage_' . Training::NAME . '_posts_columns', function ( $columns ): array { + return $this->container->get( Config::class )->list_table_columns(); + }, 100 ); + } + +}