Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/906 Distributable post types made consistent #907

Merged
merged 8 commits into from
Jul 21, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public function get_sync_log( $blog_id = 0 ) {
*/
public function get_post_types() {
switch_to_blog( $this->site->blog_id );
$post_types = get_post_types( [ 'public' => true ], 'objects' );
$post_types = Utils\distributable_post_types( 'objects' );
restore_current_blog();

return $post_types;
Expand Down
28 changes: 8 additions & 20 deletions includes/external-connection-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,7 @@ function meta_box_external_connection_details( $post ) {

$external_connection_status = get_post_meta( $post->ID, 'dt_external_connections', true );

$post_types = get_post_types(
array(
'show_in_rest' => true,
),
'objects'
);
$post_types = \Distributor\Utils\distributable_post_types( 'objects' );

$registered_external_connection_types = \Distributor\Connections::factory()->get_registered();

Expand Down Expand Up @@ -544,20 +539,13 @@ function meta_box_external_connection_details( $post ) {
<th><?php esc_html_e( 'Can push?', 'distributor' ); ?></th>
</thead>
<tbody>
<?php
$hide_from_list = \Distributor\Utils\get_excluded_post_types_from_permission_list();

foreach ( $post_types as $post_type ) :
if ( in_array( $post_type->name, $hide_from_list, true ) ) {
continue;
}
?>
<tr>
<td><?php echo esc_html( $post_type->label ); ?></td>
<td><?php echo in_array( $post_type->name, $external_connection_status['can_get'] ) ? esc_html__( 'Yes', 'distributor' ) : esc_html__( 'No', 'distributor' ); ?></td>
<td><?php echo in_array( $post_type->name, $external_connection_status['can_post'] ) ? esc_html__( 'Yes', 'distributor' ) : esc_html__( 'No', 'distributor' ); ?></td>
</tr>
<?php endforeach; ?>
<?php foreach ( $post_types as $post_type ) : ?>
<tr>
<td><?php echo esc_html( $post_type->label ); ?></td>
<td><?php echo in_array( $post_type->name, $external_connection_status['can_get'] ) ? esc_html__( 'Yes', 'distributor' ) : esc_html__( 'No', 'distributor' ); ?></td>
<td><?php echo in_array( $post_type->name, $external_connection_status['can_post'] ) ? esc_html__( 'Yes', 'distributor' ) : esc_html__( 'No', 'distributor' ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
Expand Down
61 changes: 22 additions & 39 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,24 @@ function available_pull_post_types( $connection, $type ) {
/**
* Return post types that are allowed to be distributed
*
* @param string $output Optional. The type of output to return.
* Accepts post type 'names' or 'objects'. Default 'names'.
*
* @since 1.0
* @since x.x.x $output parameter introduced.
* @return array
*/
function distributable_post_types() {
$post_types = get_post_types( [ 'public' => true ] );
function distributable_post_types( $output = 'names' ) {
$post_types = array_filter( get_post_types(), 'is_post_type_viewable' );

$exclude_post_types = [
'attachment',
'dt_ext_connection',
'dt_subscription',
];

if ( ! empty( $post_types['attachment'] ) ) {
unset( $post_types['attachment'] );
foreach ( $exclude_post_types as $exclude_post_type ) {
unset( $post_types[ $exclude_post_type ] );
}

/**
Expand All @@ -320,44 +330,17 @@ function distributable_post_types() {
*
* @return {array} Post types that are distributable.
*/
return apply_filters( 'distributable_post_types', array_diff( $post_types, [ 'dt_ext_connection', 'dt_subscription' ] ) );
}

/**
* Return post types that should be excluded from the permission list.
*
* @since 1.7.0
* @return array
*/
function get_excluded_post_types_from_permission_list() {
// Hide the built-in post types except 'post' and 'page'.
$hide_from_list = get_post_types(
array(
'_builtin' => true,
'show_in_rest' => true,
)
);
unset( $hide_from_list['post'], $hide_from_list['page'] );
$post_types = apply_filters( 'distributable_post_types', $post_types );

// Default is keyed by the post type 'post' => 'post', etc; hence using `array_values`.
$hide_from_list = array_values( $hide_from_list );
// Remove unregistered post types added via the filter.
$post_types = array_filter( $post_types, 'post_type_exists' );

/**
* Filter to update the list of post types that should be hidden from the "Post types permissions" list.
*
* @since 1.7.0
* @hook dt_excluded_post_types_from_permission_list
*
* @param {array} The list of hidden post types.
*
* @return {bool} The updated array with the list of post types that should be hidden.
*/
$hide_from_list = apply_filters( 'dt_excluded_post_types_from_permission_list', $hide_from_list );

// Strict Hide 'dt_subscription' post type.
$hide_from_list[] = 'dt_subscription';
if ( 'objects' === $output ) {
// Convert to objects.
$post_types = array_map( 'get_post_type_object', $post_types );
}

return $hide_from_list;
return $post_types;
}

/**
Expand Down