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 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
85d9457
Add categories to pull screen
kirtangajjar Jun 23, 2024
7fee3dd
Merge branch 'develop' into feature/show-category-pull
faisal-alvi Jul 2, 2024
8ef5b57
Add trailing slash to cat urls
kirtangajjar Jul 4, 2024
32c11d0
Add a full stop.
kirtangajjar Jul 4, 2024
d3d6648
Address PR feedback
kirtangajjar Jul 4, 2024
4ad5180
Address PR feedback
kirtangajjar Jul 4, 2024
8d71ce7
Update wp-env
kirtangajjar Jul 4, 2024
0a82a88
Update connection_now object
kirtangajjar Jul 4, 2024
98d6346
Update wp-env version
kirtangajjar Jul 4, 2024
93090a0
Remove initialization file
kirtangajjar Jul 4, 2024
4c249a8
Fix initialize file
kirtangajjar Jul 4, 2024
b1ad11d
Convert all npm run to npx
kirtangajjar Jul 4, 2024
26480c1
Convert all npm run to npx
kirtangajjar Jul 4, 2024
74319fe
Merge branch 'develop' into feature/show-category-pull
kirtangajjar Jul 8, 2024
e31a171
Add plugin version
kirtangajjar Jul 8, 2024
4e8ed85
Update filter doccomment
kirtangajjar Jul 8, 2024
8df0fa9
Merge branch 'develop' into feature/show-category-pull
kirtangajjar Jul 15, 2024
bd2d8eb
Merge remote-tracking branch 'origin/develop' into feature/show-categ…
kirtangajjar Aug 5, 2024
ab0bfc3
Merge branch 'develop' into feature/show-category-pull
Sidsector9 Aug 19, 2024
0850705
Merge branch 'develop' into feature/show-category-pull
jeffpaul Sep 4, 2024
b6431d2
Merge branch 'develop' into feature/show-category-pull
faisal-alvi Sep 6, 2024
90acb95
Merge branch 'develop' into feature/show-category-pull
kirtangajjar Mar 13, 2025
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 ) );
}

/**
* 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 @@ -491,6 +491,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.
*/
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 );

/**
* 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>';
}
}

/**
* 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.
*/
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?
*
* @since 2.0.5
*
* @return boolean
*/
function is_external_connection() {
global $connection_now;

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

/**
* 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;
}

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