Skip to content

Commit

Permalink
Merge branch 'main' into chore/wp-6.7
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffDusome authored Nov 14, 2024
2 parents 9e6d924 + 0fd5a92 commit b18dde9
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
pull_request:
types:
- opened
- synchronize
push:
branches:
- main
Expand Down
1 change: 1 addition & 0 deletions wp-content/plugins/core/src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}
15 changes: 15 additions & 0 deletions wp-content/plugins/core/src/Integrations/RankMath.php
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;
}

}
15 changes: 15 additions & 0 deletions wp-content/plugins/core/src/Integrations/YoastSEO.php
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 wp-content/plugins/core/src/Post_Types/Training/Config.php
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 wp-content/plugins/core/src/Post_Types/Training/Training.php
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';

}
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 );
}

}

0 comments on commit b18dde9

Please sign in to comment.