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

Added ability to view all post types when Pulling from an External Connection #1002

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions includes/classes/PullListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,11 @@ public function prepare_items() {

$current_page = $this->get_pagenum();

// Support 'View all' filtering for internal connections.
if ( is_a( $connection_now, '\Distributor\InternalConnections\NetworkSiteConnection' ) ) {
if ( empty( $connection_now->pull_post_type ) || 'all' === $connection_now->pull_post_type ) {
$post_type = wp_list_pluck( $connection_now->pull_post_types, 'slug' );
} else {
$post_type = $connection_now->pull_post_type;
}
// Support 'View all' filtering for all type of connections.
if ( empty( $connection_now->pull_post_type ) || 'all' === $connection_now->pull_post_type ) {
$post_type = wp_list_pluck( $connection_now->pull_post_types, 'slug' );
} else {
$post_type = $connection_now->pull_post_type ? $connection_now->pull_post_type : 'post';
$post_type = $connection_now->pull_post_type;
}

$remote_get_args = [
Expand Down Expand Up @@ -527,17 +523,24 @@ public function prepare_items() {
$remote_get_args['paged'] = 1;
}

$remote_get = $connection_now->remote_get( $remote_get_args );
if ( ! is_array( $remote_get_args['post_type'] ) ) {
$remote_get_args['post_type'] = [ $remote_get_args['post_type'] ];
}

if ( is_wp_error( $remote_get ) ) {
$this->pull_error = $remote_get->get_error_messages();
$total_items = '';
$response_data = [];

return;
}
foreach ( $remote_get_args['post_type'] as $type ) {
$remote_get_args['post_type'] = $type;
mehul0810 marked this conversation as resolved.
Show resolved Hide resolved
$remote_get = $connection_now->remote_get( $remote_get_args );

if ( is_wp_error( $remote_get ) ) {
$this->pull_error = $remote_get->get_error_messages();
mehul0810 marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

// Get total items retrieved from the remote request if not already set.
if ( false === $total_items ) {
$total_items = $remote_get['total_items'];
$total_items += $remote_get['total_items'];
$response_data = array_merge( $response_data, array_values( $remote_get['items'] ) );
}

$this->set_pagination_args(
Expand All @@ -547,7 +550,7 @@ public function prepare_items() {
]
);

foreach ( $remote_get['items'] as $item ) {
foreach ( $response_data as $item ) {
$this->items[] = $item;
}
}
Expand Down Expand Up @@ -612,17 +615,15 @@ public function extra_tablenav( $which ) {
$connection_type = 'external';
}

if ( $connection_now && $connection_now->pull_post_types && $connection_now->pull_post_type ) :
if ( $connection_now && $connection_now->pull_post_types ) :
?>

<div class="alignleft actions dt-pull-post-type">
<label for="pull_post_type" class="screen-reader-text">Content to Pull</label>
<select id="pull_post_type" name="pull_post_type">
<?php if ( 'internal' === $connection_type ) : ?>
<option <?php selected( $connection_now->pull_post_type, 'all' ); ?> value="all">
<?php esc_html_e( 'View all', 'distributor' ); ?>
</option>
<?php endif; ?>
<option <?php selected( $connection_now->pull_post_type, 'all' ); ?> value="all">
<?php esc_html_e( 'View all', 'distributor' ); ?>
</option>
<?php foreach ( $connection_now->pull_post_types as $post_type ) : ?>
<option <?php selected( $connection_now->pull_post_type, $post_type['slug'] ); ?> value="<?php echo esc_attr( $post_type['slug'] ); ?>">
<?php echo esc_html( $post_type['name'] ); ?>
Expand Down
20 changes: 8 additions & 12 deletions includes/pull-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,27 +472,23 @@ function dashboard() {
</select>

<?php
$connection_now->pull_post_type = '';
$connection_now->pull_post_types = \Distributor\Utils\available_pull_post_types( $connection_now, $connection_type );

// Ensure we have at least one post type to pull.
$connection_now->pull_post_type = '';
if ( ! empty( $connection_now->pull_post_types ) ) {
$connection_now->pull_post_type = ( 'internal' === $connection_type ) ? 'all' : $connection_now->pull_post_types[0]['slug'];
}

// Set the post type we want to pull (if any)
// This is either from a query param, "post" post type, or the first in the list
foreach ( $connection_now->pull_post_types as $post_type ) {
if ( isset( $_GET['pull_post_type'] ) ) { // @codingStandardsIgnoreLine No nonce needed here.
if ( $_GET['pull_post_type'] === $post_type['slug'] ) { // @codingStandardsIgnoreLine Comparing values, no nonce needed.
$connection_now->pull_post_type = $post_type['slug'];
if ( ! empty( $_GET['pull_post_type'] ) ) { // @codingStandardsIgnoreLine No nonce needed here.
if ( 'all' === $_GET['pull_post_type'] ) {
$connection_now->pull_post_type = wp_list_pluck( $connection_now->pull_post_types, 'slug' );
break;
}
} else {
if ( 'post' === $post_type['slug'] && 'external' === $connection_type ) {
} elseif ( $_GET['pull_post_type'] === $post_type['slug'] ) { // @codingStandardsIgnoreLine Comparing values, no nonce needed.
$connection_now->pull_post_type = $post_type['slug'];
break;
}
} else {
$connection_now->pull_post_type = ! empty( $post_type['slug'] ) ? $post_type['slug'] : 'post';
break;
}
}
?>
Expand Down
5 changes: 4 additions & 1 deletion includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,13 @@ function check_post_types_permissions() {
* @return \WP_REST_Response|\WP_Error
*/
function get_pull_content( $request ) {
$post_type = isset( $request['post_type'] ) ? $request['post_type'] : 'all';
$post_type = 'all' === $post_type ? array( 'post', 'page' ) : $post_type;
mehul0810 marked this conversation as resolved.
Show resolved Hide resolved

$args = [
'posts_per_page' => isset( $request['posts_per_page'] ) ? $request['posts_per_page'] : 20,
'paged' => isset( $request['page'] ) ? $request['page'] : 1,
'post_type' => isset( $request['post_type'] ) ? $request['post_type'] : 'post',
'post_type' => array( 'post', 'page' ),
mehul0810 marked this conversation as resolved.
Show resolved Hide resolved
'post_status' => isset( $request['post_status'] ) ? $request['post_status'] : array( 'any' ),
];

Expand Down