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

Add categories to pull screen - 1 #1237

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
29 changes: 24 additions & 5 deletions includes/classes/PullListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Distributor;

use function Distributor\Utils\generate_taxonomy_links;

/**
* List table class for pull screen
*/
Expand Down Expand Up @@ -54,10 +56,11 @@ public function __construct() {
*/
public function get_columns() {
$columns = [
'cb' => '<input type="checkbox" />',
'name' => esc_html__( 'Name', 'distributor' ),
'post_type' => esc_html__( 'Post Type', 'distributor' ),
'date' => esc_html__( 'Date', 'distributor' ),
'cb' => '<input type="checkbox" />',
'name' => esc_html__( 'Name', 'distributor' ),
'post_type' => esc_html__( 'Post Type', 'distributor' ),
'categories' => esc_html__( 'Categories', 'distributor' ),
'date' => esc_html__( 'Date', 'distributor' ),
];

/**
Expand Down Expand Up @@ -247,14 +250,30 @@ public function column_date( $post ) {
}
}

/**
* Output categories column.
*
* @param \WP_Post $post Post object.
* @since 2.0.5
*/
public function column_categories( $post ) {
$categories = $post->terms['category'] ?? [];

if ( empty( $categories ) ) {
return;
}

echo wp_kses_post( generate_taxonomy_links( 'category', $post, $categories ) );
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Output standard table columns.
*
* @param array|\WP_Post $item Item to output.
* @param string $column_name Column name.
*
* @return string.
* @since 0.8
* @since 2.0.5
Comment on lines -257 to +276
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the version bumped for this existing function?

*/
public function column_default( $item, $column_name ) {
if ( 'post_type' === $column_name ) {
Expand Down
199 changes: 199 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,205 @@ function prepare_meta( $post_id ) {
return $prepared_meta;
}

/**
* Generates taxonomy term links for a given post.
*
* The code is taken from WP_Posts_List_Table::column_default and modified
* lightly to work in our context.
*
* @param string $taxonomy The taxonomy name.
* @param object $post The post object.
* @param array $terms Optional. Array of terms.
*
* @return string The generated HTML for the taxonomy links.
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
*/
function generate_taxonomy_links( $taxonomy, $post, $terms = [] ) {
$taxonomy_object = get_taxonomy( $taxonomy );

if ( ! $taxonomy_object ) {
return '';
}

if ( ! $terms ) {
$terms = get_the_terms( $post, $taxonomy );
}

/**
* Filter the taxonomy terms that should be synced.
*
* @since 2.0.5
* @hook dt_syncable_taxonomy_terms
*
* @param {array} $terms Array of terms.
* @param {string} $taxonomy Taxonomy name.
* @param {object} $post Post Object.
*
* @return {array} Array of terms.
*/
$terms = apply_filters( "dt_syncable_{$taxonomy}_terms", $terms, $taxonomy, $post );
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved

/**
* Filter the terms that should be synced.
*
* @since 2.0.5
* @hook dt_syncable_terms
*
* @param {array} $terms Array of categories.
* @param {string} $taxonomy Taxonomy name.
* @param {object} $post Post Object.
*
* @return {array} Array of categories.
*/
$terms = apply_filters( 'dt_syncable_terms', $terms, $taxonomy, $post );

if ( is_array( $terms ) ) {
$term_links = array();

foreach ( $terms as $t ) {
if ( is_array( $t ) ) {
$t = (object) $t;
}
$posts_in_term_qv = array();

if ( 'post' !== $post->post_type ) {
$posts_in_term_qv['post_type'] = $post->post_type;
}

if ( $taxonomy_object->query_var ) {
$posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug;
} else {
$posts_in_term_qv['taxonomy'] = $taxonomy;
$posts_in_term_qv['term'] = $t->slug;
}

$label = esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) );

$term_links[] = get_edit_link( $posts_in_term_qv, $label );
}

/**
* Filters the links in `$taxonomy` column of edit.php.
*
* @since 2.0.5
* @hook dt_taxonomy_links
*
* @param {string[]} $term_links Array of term editing links.
* @param {string} $taxonomy Taxonomy name.
* @param {WP_Term[]} $terms Array of term objects appearing in the post row.
*
* @return {string[]} Array of term editing links.
*/
$term_links = apply_filters( 'dt_taxonomy_links', $term_links, $taxonomy, $terms );

return implode( wp_get_list_item_separator(), $term_links );
} else {
return '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . $taxonomy_object->labels->no_terms . '</span>';
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Creates a link to edit.php with params.
*
* The edit link is created in such a way that it will link to source site.
*
* @since 2.0.5
*
* @param string[] $args Associative array of URL parameters for the link.
* @param string $link_text Link text.
* @param string $css_class Optional. Class attribute. Default empty string.
*
* @return string The formatted link string.
kirtangajjar marked this conversation as resolved.
Show resolved Hide resolved
*/
function get_edit_link( $args, $link_text, $css_class = '' ) {
$url = '';
if ( is_internal_connection() ) {
$url = add_query_arg( $args, get_admin_url( null, 'edit.php' ) );
} else {
$url = add_query_arg( $args, trailingslashit( get_root_url() ) . 'wp-admin/edit.php' );
}

$class_html = '';
$aria_current = '';

if ( ! empty( $css_class ) ) {
$class_html = sprintf(
' class="%s"',
esc_attr( $css_class )
);

if ( 'current' === $css_class ) {
$aria_current = ' aria-current="page"';
}
}

return sprintf(
'<a href="%s"%s%s>%s</a>',
esc_url( $url ),
$class_html,
$aria_current,
$link_text
);
}

/**
* Is current connection an external connection?
*
kirtangajjar marked this conversation as resolved.
Show resolved Hide resolved
* @since 2.0.5
*
* @return boolean
*/
function is_external_connection() {
global $connection_now;

return is_a( $connection_now, '\Distributor\ExternalConnection' );
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Is current connection an internal connection?
*
* @since 2.0.5
*
* @return boolean
*/
function is_internal_connection() {
global $connection_now;

return is_a( $connection_now, '\Distributor\InternalConnections\NetworkSiteConnection' );
}

/**
* Get the root URL of the current connection
*
* @since 2.0.5
*
* @return string
*/
function get_root_url() {
$base_url = get_conn_base_url();

return str_replace( '/wp-json', '', $base_url );
}

/**
* Get the base URL of the current connection
*
* @since 2.0.5
*
* @return string
*/
function get_conn_base_url() {
if ( ! is_external_connection() ) {
return get_site_url();
}
global $connection_now;

if ( ! $connection_now || ! property_exists( $connection_now, 'base_url' ) ) {
return '';
}

return $connection_now->base_url;
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Format media items for consumption
*
Expand Down
Loading