-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #160
- Loading branch information
Showing
7 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tribe\Plugin\Integrations; | ||
|
||
use Tribe\Plugin\Post_Types\Training\Training; | ||
|
||
class RankMath { | ||
|
||
public function exclude_post_types( array $post_types ): array { | ||
$post_types = array_diff( $post_types, [ Training::NAME ] ); | ||
|
||
return $post_types; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tribe\Plugin\Integrations; | ||
|
||
use Tribe\Plugin\Post_Types\Training\Training; | ||
|
||
class YoastSEO { | ||
|
||
public function exclude_post_types( array $post_types ): array { | ||
$post_types = array_diff( $post_types, [ Training::NAME ] ); | ||
|
||
return $post_types; | ||
} | ||
|
||
} |
140 changes: 140 additions & 0 deletions
140
wp-content/plugins/core/src/Post_Types/Training/Config.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tribe\Plugin\Post_Types\Training; | ||
|
||
use Tribe\Libs\Post_Type\Post_Type_Config; | ||
|
||
class Config extends Post_Type_Config { | ||
|
||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint | ||
protected $post_type = Training::NAME; | ||
|
||
public function get_args(): array { | ||
return [ | ||
'capability_type' => $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' => '<input type="checkbox" />', | ||
'title' => 'Title', | ||
]; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
wp-content/plugins/core/src/Post_Types/Training/Training.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tribe\Plugin\Post_Types\Training; | ||
|
||
use Tribe\Libs\Post_Type\Post_Object; | ||
|
||
class Training extends Post_Object { | ||
|
||
public const NAME = 'training'; | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
wp-content/plugins/core/src/Post_Types/Training/Training_Subscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tribe\Plugin\Post_Types\Training; | ||
|
||
use Tribe\Libs\Post_Type\Post_Type_Subscriber; | ||
|
||
class Training_Subscriber extends Post_Type_Subscriber { | ||
|
||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint | ||
protected $config_class = Config::class; | ||
|
||
public function register(): void { | ||
parent::register(); | ||
|
||
$this->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 ); | ||
} | ||
|
||
} |