From edf3d3e07e8d63612edb9c92c0e9dcea5540e98a Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Mon, 16 Oct 2023 23:19:44 +0200 Subject: [PATCH] Refactor while fixing CS issues --- inc/feed-pull/auto-setup.php | 103 +++++++++++++++--------- inc/feed-pull/import.php | 69 ++++++++-------- inc/feed-pull/namespace.php | 10 +-- tests/inc/feed-pull/test-auto-setup.php | 25 +++--- 4 files changed, 118 insertions(+), 89 deletions(-) diff --git a/inc/feed-pull/auto-setup.php b/inc/feed-pull/auto-setup.php index fc2d245..e38e1ec 100644 --- a/inc/feed-pull/auto-setup.php +++ b/inc/feed-pull/auto-setup.php @@ -1,5 +1,4 @@ post_type !== LINK_PT ) { return; } - // look for a platform suggestion - // which use user may have given during registration - // see: Figuren_Theater\src\FeaturesAssets\core-my-registration\wp_core.php + /** + * Look for a platform suggestion + * which use user may have given during registration. + * + * @see Figuren_Theater\src\FeaturesAssets\core-my-registration\wp_core.php + */ $suggestion = get_post_meta( $post->ID, '_ft_platform', true ) ?? null; - // get bridged URL + // Get bridged URL. $fp_feed_url = esc_url( Rss_Bridge\get_bridged_url( $post->post_content, $suggestion ), 'https', 'db' ); - // Bail, if not importable + // Bail, if not importable. if ( ! $fp_feed_url ) { return; } - // prepare the insert arguments + // Prepare the insert arguments. $insert_args = wp_slash( [ 'post_author' => $post->post_author, - 'post_type' => FEED_POSTTYPE, + 'post_type' => Feed_Pull\FEED_POSTTYPE, 'post_title' => 'Feed: ' . $post->post_content, 'post_parent' => $post->ID, 'post_status' => 'publish', @@ -103,7 +119,7 @@ function create_feed_post( WP_Post $post ) : void { 'meta_input' => [ 'fp_feed_url' => $fp_feed_url, - ADAPTER_POSTMETA => '', // @todo #16 // array_key of one of the get_bridges() array. + Feed_Pull\ADAPTER_POSTMETA => '', // @todo #16 // array_key of one of the get_bridges() array. ], 'tax_input' => [ UTILITY_TAX => [], @@ -132,16 +148,16 @@ function create_feed_post( WP_Post $post ) : void { * * @param int $object_id Object ID. * @param array $terms An array of object term IDs or slugs. - * @param array $tt_ids An array of term taxonomy IDs. + * @param array $new_terms An array of term taxonomy IDs. * @param string $taxonomy Taxonomy slug. * @param bool $append Whether to append new terms to the old terms. - * @param array $old_tt_ids Old array of term taxonomy IDs. + * @param array $old_terms Old array of term taxonomy IDs. */ function add_or_delete_feed_post( int $object_id, array $terms, array $new_terms, string $taxonomy, bool $append, array $old_terms ) : void { $import_term_id = get_import_term_id(); // Return early if not the utility taxonomy or not the 'import' term being added or removed. - if ( $taxonomy !== UTILITY_TAX || ! in_array( $import_term_id, $new_terms ) && ! in_array( $import_term_id, $old_terms ) ) { + if ( $taxonomy !== UTILITY_TAX || ! in_array( $import_term_id, $new_terms, true ) && ! in_array( $import_term_id, $old_terms, true ) ) { return; } @@ -156,15 +172,15 @@ function add_or_delete_feed_post( int $object_id, array $terms, array $new_terms // depending on 'import' term being // added or removed from Link posts. // - // Term is new and not yet assigned - if ( in_array( $import_term_id, $new_terms ) && ! in_array( $import_term_id, $old_terms ) ) { + // Term is new and not yet assigned. + if ( in_array( $import_term_id, $new_terms, true ) && ! in_array( $import_term_id, $old_terms, true ) ) { create_feed_post( $post ); - // Term is not assigned, but was previously - } elseif ( ! in_array( $import_term_id, $new_terms ) && in_array( $import_term_id, $old_terms ) ) { + // Term is not assigned, but was previously. + } elseif ( ! in_array( $import_term_id, $new_terms, true ) && in_array( $import_term_id, $old_terms, true ) ) { $feed_post_id = get_feed_from_link( $object_id ); if ( $feed_post_id ) { - // Delete without trash bin + // Delete without trash bin. wp_delete_post( $feed_post_id, true ); } } @@ -178,24 +194,37 @@ function add_or_delete_feed_post( int $object_id, array $terms, array $new_terms function delete_feed_post_on_trash( int $post_id ) : void { $post = get_post( $post_id ); - // Bail if post type is not a Link + // Bail if post type is not a Link. if ( $post->post_type !== LINK_PT ) { return; } - // Delete feed post + // Delete & trash the feed post. wp_delete_post( get_feed_from_link( $post_id ), true ); } +/** + * Get the FEED post, by its post_parent ID. + * + * @param int $link_post_id A LINK_PT post_ID. + * + * @return int A Feed_Pull\FEED_POSTTYPE post_ID. + */ function get_feed_from_link( int $link_post_id ) : int { - $feed_post = get_posts( [ - 'post_type' => FEED_POSTTYPE, + + $feed_query = new WP_Query( [ + 'post_type' => Feed_Pull\FEED_POSTTYPE, 'post_parent' => $link_post_id, 'numberposts' => 1, ] ); - return ( empty( $feed_post ) ) ? 0 : $feed_post[0]->ID; + return ( empty( $feed_query->posts ) ) ? 0 : $feed_query->posts[0]->ID; } +/** + * Get Term_ID of 'import' term within the Utility Taxonomy. + * + * @return int + */ function get_import_term_id() : int { $term = get_term_by( 'slug', UTILITY_TERM, UTILITY_TAX ); // Get the "import" term ID. diff --git a/inc/feed-pull/import.php b/inc/feed-pull/import.php index e4c07af..77d97cc 100644 --- a/inc/feed-pull/import.php +++ b/inc/feed-pull/import.php @@ -5,10 +5,12 @@ * @package figuren-theater/ft-data */ -namespace Figuren_Theater\Data\Feed_Pull; +namespace Figuren_Theater\Data\Feed_Pull\Import; -use Figuren_Theater\Data\Rss_Bridge; +use Figuren_Theater\Data\Feed_Pull; +use Figuren_Theater\Data\Feed_Pull\Auto_Setup; +use Figuren_Theater\Data\Rss_Bridge; use Figuren_Theater\Network\Taxonomies; use Figuren_Theater\Network\Users; @@ -23,15 +25,21 @@ use function wp_parse_args; use function wp_slash; -function bootstrap_import() { +/** + * Bootstrap module, when enabled. + * + * @return void + */ +function bootstrap() :void { add_action( 'init', __NAMESPACE__ . '\\init', 5 ); } function init() { - // for debugging only - // delete_option( 'fp_deleted_syndicated' ); + // Debugging only. + // phpcs:ignore + // delete_option( Auto_Setup\FP_DELETED_OPTION_NAME ); // https://github.com/tlovett1/feed-pull/blob/45d667c1275cca0256bd03ed6fa1655cdf26f064/includes/class-fp-pull.php#L274 add_filter( 'fp_pre_post_insert_value', __NAMESPACE__ . '\\fp_pre_post_insert_value', 10, 4 ); @@ -103,14 +111,14 @@ function get_default_static_metas() : array { * @param string $meta_type Type of object metadata is for. Can be 'post', 'comment', 'term', 'user', * or any other object type with an associated meta table. */ -function default_post_metadata( mixed $value, int $object_id, string $meta_key ) : mixed { +function default_post_metadata( mixed $value, int $object_id, string $meta_key, bool $single, string $meta_type ) : mixed { - // Go out for all other post_meta - if ( ! in_array( $meta_key, get_default_static_metas() ) ) { + // Go out for all other post_meta. + if ( ! in_array( $meta_key, get_default_static_metas(), true ) ) { return $value; } - $adapter = get_post_meta( $object_id, ADAPTER_POSTMETA, true ); + $adapter = get_post_meta( $object_id, Feed_Pull\ADAPTER_POSTMETA, true ); $bridges = Rss_Bridge\get_bridges(); if ( ! isset( $bridges[ $adapter ] ) ) { @@ -122,7 +130,7 @@ function default_post_metadata( mixed $value, int $object_id, string $meta_key ) switch ( $meta_key ) { case 'fp_posts_xpath': - return $adapter['fp_posts_xpath'] ?? 'feed/entry'; // Atom + return $adapter['fp_posts_xpath'] ?? 'feed/entry'; // Atom feed. case 'fp_field_map': return $adapter['fp_field_map'] ?? get_fp_field_map(); @@ -156,12 +164,12 @@ function default_post_metadata( mixed $value, int $object_id, string $meta_key ) function get_fp_field_map() : array { return [ [ - 'source_field' => 'title', // Atom + 'source_field' => 'title', // Atom feed. 'destination_field' => 'post_title', 'mapping_type' => 'post_field', ], [ - 'source_field' => 'id', // Atom + 'source_field' => 'id', // Atom feed. 'destination_field' => 'guid', 'mapping_type' => 'post_field', ], @@ -221,7 +229,7 @@ function get_fp_source_feed_id( int $post_id ) : int|false { * If specified, only update existing metadata entries with * this value. Otherwise, update all entries. */ -function dont_update_post_metadata( $check, int $object_id, string $meta_key, mixed $meta_value ) : mixed { +function dont_update_post_metadata( $check, int $object_id, string $meta_key, mixed $meta_value, mixed $prev_value ) : mixed { /* // one special-operation // but instead of writing to post_meta @@ -249,12 +257,12 @@ function dont_update_post_metadata( $check, int $object_id, string $meta_key, mi } } */ - // Send non-null, falsy return to prevent feed-pull post_meta from being written|updated - if ( in_array( $meta_key, get_default_static_metas() ) ) { + // Send non-null, falsy return to prevent feed-pull post_meta from being written|updated. + if ( in_array( $meta_key, get_default_static_metas(), true ) ) { return false; } - // all other post_meta + // All other post_meta. return $check; } @@ -277,28 +285,19 @@ function dont_update_post_metadata( $check, int $object_id, string $meta_key, mi */ function fp_pre_post_insert_value( $pre_filter_post_value, $field, $post, $source_feed_id ): string { - if ( 'post_title' == $field['destination_field'] ) { + if ( 'post_title' === $field['destination_field'] ) { return sanitize_text_field( $pre_filter_post_value ); } - if ( 'post_excerpt' == $field['destination_field'] ) { + if ( 'post_excerpt' === $field['destination_field'] ) { return sanitize_textarea_field( $pre_filter_post_value ); } - if ( 'post_content' == $field['destination_field'] ) { - /* - $tags_to_strip = Array("figure","font" ); - foreach ($tags_to_strip as $tag) - { - $pre_filter_post_value = preg_replace("/<\\/?" . $tag . "(.|\\s)*?>/",'',$pre_filter_post_value); - - } serialize_block( $block );*/ - // return \wpautop( \wp_kses_post( $pre_filter_post_value ), true ); - // return \do_blocks( \wp_kses_post( $pre_filter_post_value ), true ); + if ( 'post_content' === $field['destination_field'] ) { return do_blocks( $pre_filter_post_value ); } - // all other fields + // All other fields. return $pre_filter_post_value; } @@ -319,15 +318,15 @@ function fp_post_args( array $new_post_args, $post, int $source_feed_id ) : arra $import_args = get_import_args_from_source( $source_feed_id ); - // Set some defaults + // Set some defaults. $import_args['comment_status'] = 'closed'; $import_args['ping_status'] = 'closed'; - // set author to machine user, if non set + // set author to machine user, if non set. $new_post_args['post_author'] ?: Users\ft_bot::id(); - // strip (maybe) filled excerpt - // if we can auto-generate it + // Strip (maybe) filled excerpt + // if we can auto-generate it. if ( ! empty( $new_post_args['post_content'] ) && ! empty( $new_post_args['post_excerpt'] ) ) { unset( $new_post_args['post_excerpt'] ); } @@ -343,8 +342,8 @@ function get_import_args_from_source( int $source_feed_id ) : array { $ft_link = get_post_parent( get_post( $source_feed_id ) ); // 2. get sourced 'ft_link_shadow'-term-id - $TAX_Shadow = Taxonomies\TAX_Shadow::init(); - $ft_link_term = $TAX_Shadow->get_associated_term( + $tax_shadow = Taxonomies\TAX_Shadow::init(); + $ft_link_term = $tax_shadow->get_associated_term( $ft_link, $taxonomy ); diff --git a/inc/feed-pull/namespace.php b/inc/feed-pull/namespace.php index c78895d..ab8cd39 100644 --- a/inc/feed-pull/namespace.php +++ b/inc/feed-pull/namespace.php @@ -65,10 +65,10 @@ function load_plugin() :void { // Create new 'fp_feed' posts, when a new 'ft_link' post is created // which has an importable endpoint. - bootstrap_auto_setup(); + Auto_Setup\bootstrap(); // Everything related to importing normal posts from feeds. - bootstrap_import(); + Import\bootstrap(); add_action( 'admin_menu', __NAMESPACE__ . '\\remove_menu', 11 ); @@ -121,9 +121,9 @@ function remove_menu() : void { * * @see https://github.com/tlovett1/feed-pull/blob/45d667c1275cca0256bd03ed6fa1655cdf26f064/includes/class-fp-source-feed-cpt.php#L136 * - * @param array $args [description] + * @param array $args Arguments for registering a post type. See the register_post_type() function for accepted arguments. * - * @return array [description] + * @return array */ function register_post_type_args( array $args ) : array { @@ -167,7 +167,7 @@ function modify_metaboxes() : void { function custom_icons() : void { global $pagenow, $typenow; - if ( ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) || 'fp_feed' !== $typenow ) { + if ( ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) || FEED_POSTTYPE !== $typenow ) { return; } ?> diff --git a/tests/inc/feed-pull/test-auto-setup.php b/tests/inc/feed-pull/test-auto-setup.php index 0352f6c..1f1c075 100644 --- a/tests/inc/feed-pull/test-auto-setup.php +++ b/tests/inc/feed-pull/test-auto-setup.php @@ -1,27 +1,27 @@