From d850af202313921fda2e1e93b61422525241c999 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Tue, 9 Jan 2024 16:44:58 -0600 Subject: [PATCH] Reduce duplication of code in fetching eligible post types --- src/wp-to-twitter-manager.php | 143 ++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 69 deletions(-) diff --git a/src/wp-to-twitter-manager.php b/src/wp-to-twitter-manager.php index d561f09..ca3494f 100644 --- a/src/wp-to-twitter-manager.php +++ b/src/wp-to-twitter-manager.php @@ -244,6 +244,36 @@ function wpt_updated_settings() { } } +/** + * Build array of post types eligible for XPoster to send updates for. + * + * @return array + */ +function wpt_possible_post_types() { + $post_types = get_post_types( array(), 'objects' ); + $exclusions = array( 'wp_navigation', 'wp_block', 'attachment', 'nav_menu_item', 'revision' ); + /** + * Exclude post types from the list of available types to post to X.com. + * + * @hook wpt_exclude_post_types + * + * @param {array} $exclusions Array of post type name slugs to exclude. + * + * @return {array} + */ + $excluded = apply_filters( 'wpt_exclude_post_types', $exclusions ); + $return = array(); + foreach ( $post_types as $type ) { + // If post type is both private & has no UI, don't show. + if ( false === $type->public && false === $type->show_ui || in_array( $type->name, $excluded, true ) ) { + continue; + } + $return[] = $type; + } + + return $return; +} + /** * Show XPoster settings form. */ @@ -340,31 +370,13 @@ function wpt_update_settings() { echo apply_filters( 'wpt_tweet_length', '' ); echo apply_filters( 'wpt_auto_tweet', '' ); echo apply_filters( 'wpt_pick_shortener', '' ); - $post_types = get_post_types( array(), 'objects' ); + $post_types = wpt_possible_post_types(); $wpt_settings = get_option( 'wpt_post_types' ); $tabs = "'; echo $tabs; foreach ( $post_types as $type ) { - if ( false === $type->public && false === $type->show_ui ) { - continue; - } $name = $type->labels->name; $slug = $type->name; - if ( 'attachment' === $slug || 'nav_menu_item' === $slug || 'revision' === $slug ) { - continue; - } else { - ?> -
-
- -

- /> - -
- -

-

- /> -

- -

-
- -
- +
+
+ +

+ /> + +
+ +

+

+ /> +

+ +

+
+ +
+