CMB2 / cmb2-attached-posts

Custom field for CMB2 for attaching posts to a page.
GNU General Public License v2.0
135 stars 62 forks source link

Quality of Life / Modernization #72

Open Ipstenu opened 1 year ago

Ipstenu commented 1 year ago

This pull is basically bringing a LOT of the existing pulls up to modern speed, slapping it around with PHPCS and formatting, and adding in things I needed if I didn't want to hardcode in everything.

It runs live on lezwatchtv.com and is what powers our connections of characters to actors/shows.

Overview

  1. Bring (most of) the code to modern standards per PHPCS
  2. Improve inline documentation (spelling etc)
  3. Added new filters
  4. cmb2_attached_posts_objects filter (from https://github.com/CMB2/cmb2-attached-posts/pull/58 - props @njeffers )
  5. Change lightbox 'title' based on post-types (i.e. 'Search Custom Post Named Bob' vs 'Find Posts and Pages')
  6. Display errors instead of failing silently if no posts are found
  7. support for limiting the amount of posts that can be selected (based on https://github.com/CMB2/cmb2-attached-posts/pull/52 - props @d79 )
  8. Fix for deleted posts borking meta (from https://github.com/CMB2/cmb2-attached-posts/pull/49/ - props @jrmd )
  9. Allow multiple metaboxes on one page (from https://github.com/CMB2/cmb2-attached-posts/pull/41 - props @cggit )
  10. Change enqueue URLs to be flexible for weirdos who put the library inside a massive plugin/theme instead of as a separate plugin

New Filters

cmb2_attached_posts_title_filter - allows the display title of the post to be altered

example:

add_filter( 'cmb2_attached_posts_title_filter', 'YOURPREFIX_cmb_filter_title_attached_posts', 10, 2 );

function YOURPREFIX_cmb_filter_title_attached_posts( $post_title, $post_id ) {
    $additional = array();
    $status     = get_post_status( $post_id );
    $has_meta   = get_post_meta( $post_id, 'EXAMPLE_META', true );
    if ( ! empty( $has_meta ) && $has_meta ) {
        $additional[] = 'This is extra stuff I want to show at the end of the title';
    }
    if ( ! empty( $additional ) ) {
        $post_title .= ' (' . implode( ', ', $additional ) . ')';
    }

    return $post_title;
}

cmb2_attached_posts_status_filter - changes the post types allowed.

example:

add_filter( 'cmb2_attached_posts_status_filter', 'YOURPREFIX_cmb_filter_post_status_attached_posts', 10, 2 );

function YOURPREFIX_cmb_filter_post_status_attached_posts( $post_status ) {
    $post_status = array( 'publish', 'pending', 'draft', 'future', 'private', 'inherit' );
    return $post_status;
}

cmb2_attached_posts_per_page_filter - changes how many pages to search (important to me as we have a couple thousand ;)

example:

add_filter( 'cmb2_attached_posts_per_page_filter', 'YOURPREFIX_cmb_filter_post_statue_attached_posts', 10, 2 );

function YOURPREFIX_cmb_filter_post_statue_attached_posts( $post_status ) {
    $post_status = array( 'publish', 'pending', 'draft', 'future', 'private', 'inherit' );
    return $post_status;
}
tw2113 commented 1 year ago

Will do what I can to get some feedback and review your way ASAP. Appreciate all the help here and hopefully we can get things merged in pretty soon.

Thanks Mika.

njeffers commented 1 year ago

@Ipstenu First off, thank you for the mention, as well as all you do for the WP community.

I haven't gotten a chance to check anything here, so please excuse me if I'm wrong, but I think the args order on the cmb2_attached_posts_title_filter should have $post_id and $post_title reversed. As is, I think it'll show the $post_id if a callback function via add_filter() isn't in play.

jtsternberg commented 1 year ago

I'm curious, have you had a chance to check out this WIP for bringing this lib into CMB2 Core? https://github.com/CMB2/CMB2/pull/1056

Ipstenu commented 1 year ago

@jtsternberg Yes, but seeing as it was untouched for 5+ years, I assumed it was an abandoned idea.

@njeffers Since I use it for all posts (to flag the status) I didn't notice :D Flipped it. Weirdly that way makes my check run multiple times, but I fixed my own code.

tw2113 commented 1 year ago

Discussing internally about merging this one in for the time being. I've looked over everything and all looks good to me. Not managing to find any compatibility issues, but I am probably not using as extensively as you are, to create logged items.

Ipstenu commented 1 year ago

TBH, I was recently wrestling with some issues on the search and that led to what is a WAY more significant divergence.

  1. Removed all the stuff about users (it doesn't work, I never use it)
  2. Changed the query to order by relevance unless the title is under 4 characters, then use exact by title

I may be the only use case where post titles can be 2-characters though!