Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions assets/js/admin-pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { document } = window;
const chooseConnection = document.getElementById( 'pull_connections' );
const choosePostType = document.getElementById( 'pull_post_type' );
const choosePostTypeBtn = document.getElementById( 'pull_post_type_submit' );
const choosePostCategory = document.getElementById( 'pull_post_category' );
const searchField = document.getElementById( 'post-search-input' );
const searchBtn = document.getElementById( 'search-submit' );
const form = document.getElementById( 'posts-filter' );
Expand All @@ -24,7 +25,7 @@ jQuery( chooseConnection ).on( 'change', ( event ) => {
document.body.className += ' ' + 'dt-loading';
} );

if ( chooseConnection && choosePostType && form ) {
if ( chooseConnection && ( choosePostType || choosePostCategory ) && form ) {
if ( choosePostTypeBtn ) {
jQuery( choosePostTypeBtn ).on( 'click', ( event ) => {
event.preventDefault();
Expand Down Expand Up @@ -84,6 +85,8 @@ if ( chooseConnection && choosePostType && form ) {
const getURL = () => {
const postType =
choosePostType.options[ choosePostType.selectedIndex ].value;
const postCategory =
choosePostCategory.options[ choosePostCategory.selectedIndex ].value;
const baseURL =
chooseConnection.options[ chooseConnection.selectedIndex ].getAttribute(
'data-pull-url'
Expand All @@ -96,5 +99,5 @@ const getURL = () => {
status = 'pulled';
}

return `${ baseURL }&pull_post_type=${ postType }&status=${ status }`;
return `${ baseURL }&pull_post_type=${ postType }&pull_post_category=${ postCategory }&status=${ status }`;
};
8 changes: 8 additions & 0 deletions includes/classes/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ abstract public function get_sync_log( $id );
*/
abstract public function get_post_types();

/**
* Get available post types from a connection
*
* @since 1.3
* @return array|\WP_Error
*/
abstract public function get_post_categories();

/**
* This method is called on every page load. It's helpful for canonicalization
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ class WordPressExternalConnection extends ExternalConnection {
*/
public $pull_post_types;

/**
* Default posts category to pull.
*
* @var string
*/
public $category;

/**
* Default posts categories to show in filter.
*
* @var string
*/
public $categories;

/**
* This is a utility function for parsing annoying API link headers returned by the types endpoint
*
Expand Down Expand Up @@ -167,6 +181,10 @@ public function remote_get( $args = array() ) {
}
}

if ( isset( $args['tax_query'] ) ) {
$query_args['tax_query'] = $args['tax_query'];
}

// When running a query for the Pull screen, make a POST request instead
if ( empty( $id ) ) {
$query_args['post_type'] = isset( $post_type ) ? $post_type : 'post';
Expand Down Expand Up @@ -665,6 +683,41 @@ public function get_post_types() {
return $types_body_array;
}

/**
* Get the available post categories.
*
* @since 1.3
* @return array|\WP_Error
*/
public function get_post_categories() {
$path = self::$namespace;

$categories_path = untrailingslashit( $this->base_url ) . '/' . $path . '/categories';

$categories_response = Utils\remote_http_request(
$categories_path,
$this->auth_handler->format_get_args( array( 'timeout' => self::$timeout ) )
);

if ( is_wp_error( $categories_response ) ) {
return $categories_response;
}

if ( 404 === wp_remote_retrieve_response_code( $categories_response ) ) {
return new \WP_Error( 'bad-endpoint', esc_html__( 'Could not connect to API endpoint.', 'distributor' ) );
}

$categories_body = wp_remote_retrieve_body( $categories_response );

if ( empty( $categories_body ) ) {
return new \WP_Error( 'no-response-body', esc_html__( 'Response body is empty.', 'distributor' ) );
}

$categories_body_array = json_decode( $categories_body, true );

return $categories_body_array;
}

/**
* Check what we can do with a given external connection (push or pull)
*
Expand Down
32 changes: 32 additions & 0 deletions includes/classes/InternalConnections/NetworkSiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ class NetworkSiteConnection extends Connection {
*/
public $pull_post_types;

/**
* Default posts category to pull.
*
* @var string
*/
public $pull_post_category;

/**
* Default posts categories to show in filter.
*
* @var string
*/
public $pull_post_categories;

/**
* Set up network site connection
*
Expand Down Expand Up @@ -494,6 +508,20 @@ public function get_post_types() {
return $post_types;
}

/**
* Get the available post categories.
*
* @since 1.3
* @return array
*/
public function get_post_categories() {
switch_to_blog( $this->site->blog_id );
$post_categories = Utils\distributable_categories();
restore_current_blog();

return $post_categories;
}

/**
* Remotely get posts so we can list them for pulling
*
Expand Down Expand Up @@ -541,6 +569,10 @@ public function remote_get( $args = array(), $new_post_args = array() ) {
$query_args['post__not_in'] = $args['post__not_in'];
}

if ( isset( $args['tax_query'] ) ) {
$query_args['tax_query'] = $args['tax_query'];
}

$query_args['post_type'] = ( empty( $args['post_type'] ) ) ? 'post' : $args['post_type'];
$query_args['post_status'] = ( empty( $args['post_status'] ) ) ? [ 'publish', 'draft', 'private', 'pending', 'future' ] : $args['post_status'];
$query_args['posts_per_page'] = ( empty( $args['posts_per_page'] ) ) ? get_option( 'posts_per_page' ) : $args['posts_per_page'];
Expand Down
34 changes: 31 additions & 3 deletions includes/classes/PullListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,15 @@ public function prepare_items() {
} else {
$post_type = $connection_now->pull_post_type;
}

if ( empty( $connection_now->pull_post_category ) || 'all' === $connection_now->pull_post_category ) {
$post_category = wp_list_pluck( $connection_now->pull_post_post_categories, 'slug' );
} else {
$post_category = $connection_now->pull_post_category;
}
} else {
$post_type = $connection_now->pull_post_type ? $connection_now->pull_post_type : 'post';
$post_category = $connection_now->pull_post_category ? $connection_now->pull_post_category : '';
}

$remote_get_args = [
Expand All @@ -482,6 +489,16 @@ public function prepare_items() {
$remote_get_args['s'] = rawurlencode( $_GET['s'] ); // @codingStandardsIgnoreLine Nonce isn't required.
}

if ( ! empty( $post_category ) && 'all' !== $post_category ) {
$remote_get_args['tax_query'] = [
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $post_category,
],
];
}

if ( is_a( $connection_now, '\Distributor\ExternalConnection' ) ) {
$this->sync_log = get_post_meta( $connection_now->id, 'dt_sync_log', true );
} else {
Expand Down Expand Up @@ -631,15 +648,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 && $connection_now->pull_post_type && $connection_now->pull_post_categories ) :
?>

<div class="alignleft actions dt-pull-post-type">
<label for="pull_post_type" class="screen-reader-text">Content to Pull</label>
<label for="pull_post_type" class="screen-reader-text">Post Type 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' ); ?>
<?php esc_html_e( 'All post types', 'distributor' ); ?>
</option>
<?php endif; ?>
<?php foreach ( $connection_now->pull_post_types as $post_type ) : ?>
Expand All @@ -648,6 +665,17 @@ public function extra_tablenav( $which ) {
</option>
<?php endforeach; ?>
</select>
<label for="pull_post_category" class="screen-reader-text">Post Categories to Pull</label>
<select id="pull_post_category" name="pull_post_category">
<option <?php selected( $connection_now->pull_post_category, 'all' ); ?> value="all">
<?php esc_html_e( 'All categories', 'distributor' ); ?>
</option>
<?php foreach ( $connection_now->pull_post_categories as $post_category ) : ?>
<option <?php selected( $connection_now->pull_post_category, $post_category['slug'] ); ?> value="<?php echo esc_attr( $post_category['slug'] ); ?>">
<?php echo esc_html( $post_category['name'] ); ?>
</option>
<?php endforeach; ?>
</select>
<input type="submit" name="filter_action" id="pull_post_type_submit" class="button" value="<?php esc_attr_e( 'Filter', 'distributor' ); ?>">

<?php
Expand Down
28 changes: 23 additions & 5 deletions includes/pull-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,22 @@ function process_actions() {
);
}

if ( empty( $_GET['pull_post_type'] ) || empty( $_GET['connection_type'] ) || empty( $_GET['connection_id'] ) || empty( $_GET['post'] ) ) {
if ( empty( $_GET['pull_post_type'] ) || empty( $_GET['pull_post_category'] ) || empty( $_GET['connection_type'] ) || empty( $_GET['connection_id'] ) || empty( $_GET['post'] ) ) {
break;
}

$posts = array_map( 'intval', (array) wp_unslash( $_GET['post'] ) );
$post_type = sanitize_text_field( $_GET['pull_post_type'] );
$post_status = ! empty( $_GET['dt_as_draft'] ) && 'draft' === $_GET['dt_as_draft'] ? 'draft' : '';
$posts = array_map( 'intval', (array) wp_unslash( $_GET['post'] ) );
$post_type = sanitize_text_field( $_GET['pull_post_type'] );
$post_category = sanitize_text_field( $_GET['pull_post_category'] );
$post_status = ! empty( $_GET['dt_as_draft'] ) && 'draft' === $_GET['dt_as_draft'] ? 'draft' : '';

$posts = array_map(
function( $remote_post_id ) use ( $post_type, $post_status ) {
function( $remote_post_id ) use ( $post_type, $post_status, $post_category ) {
return [
'remote_post_id' => $remote_post_id,
'post_type' => $post_type,
'post_status' => $post_status,
'post_category' => 'all' === $post_category ? '' : $post_category,
];
},
$posts
Expand Down Expand Up @@ -473,12 +475,17 @@ function dashboard() {

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

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

// 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
Expand All @@ -495,6 +502,17 @@ function dashboard() {
}
}
}

foreach ( $connection_now->pull_post_categories as $post_category ) {
if ( isset( $_GET['pull_post_category'] ) ) { // @codingStandardsIgnoreLine No nonce needed here.
if ( $_GET['pull_post_category'] === $post_category['slug'] ) { // @codingStandardsIgnoreLine Comparing values, no nonce needed.
$connection_now->pull_post_category = $post_category['slug'];
break;
}
} else {
$connection_now->pull_post_category = $post_category['slug'];
}
}
?>

<?php endif; ?>
Expand Down
4 changes: 4 additions & 0 deletions includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ function get_pull_content_list( $request ) {
$args['orderby'] = 'relevance';
}

if ( isset( $request['tax_query'] ) ) {
$args['tax_query'] = $request['tax_query'];
}

if ( ! empty( $request['exclude'] ) && ! empty( $request['include'] ) ) {
/*
* Use only `post__in` if both `include` and `exclude` are populated.
Expand Down
Loading