From e6ce49c2e74cdda10216a950b794cbe4c0cf40ed Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Tue, 17 Oct 2023 16:59:53 +0200 Subject: [PATCH] WIP Fix CS & phpstan issues --- inc/distributor/namespace.php | 64 ++++++++++++++++--------------- inc/feed-pull/auto-setup.php | 72 +++++++++++++++++++++-------------- inc/feed-pull/import.php | 58 +++++++++++++++++----------- inc/feed-pull/namespace.php | 12 ++++-- 4 files changed, 120 insertions(+), 86 deletions(-) diff --git a/inc/distributor/namespace.php b/inc/distributor/namespace.php index 65d99ab..6cedaaf 100644 --- a/inc/distributor/namespace.php +++ b/inc/distributor/namespace.php @@ -23,11 +23,10 @@ use function add_action; use function add_filter; use function current_user_can; - use function remove_action; use function remove_menu_page; +use function wp_get_environment_type; use WP_DEBUG; -use WP_ENVIRONMENT_TYPE; use WP_Post; const BASENAME = 'distributor/distributor.php'; @@ -54,7 +53,7 @@ function load_plugin() :void { // Because this makes things visible // to normal 'administrator' users. - if ( ! defined( 'DISTRIBUTOR_DEBUG' ) && 'local' === WP_ENVIRONMENT_TYPE ) { + if ( ! defined( 'DISTRIBUTOR_DEBUG' ) && 'local' === wp_get_environment_type() ) { define( 'DISTRIBUTOR_DEBUG', WP_DEBUG ); } @@ -84,7 +83,7 @@ function admin_init() : void { add_action( 'admin_init', __NAMESPACE__ . '\\remove_columns_from_lists', 9 ); // Allow bypassing of all media processing. - add_filter( 'dt_push_post_media', __NAMESPACE__ . '\\dt_push_post_media' ); + add_filter( 'dt_push_post_media', __NAMESPACE__ . '\\dt_push_post_media', 10, 6 ); add_filter( 'dt_push_post_args', __NAMESPACE__ . '\\dt_push_post_args', 9, 4 ); add_filter( 'dt_pull_post_args', __NAMESPACE__ . '\\dt_pull_post_args', 9, 4 ); @@ -99,9 +98,9 @@ function admin_init() : void { * Prevents the default admin-notice for missing plugin files, * which gets triggered by the FT_VENDOR_DIR path construct. * - * @param array $active_sitewide_plugins WordPress' default 'active_sitewide_plugins' site-option. + * @param array $active_sitewide_plugins WordPress' default 'active_sitewide_plugins' site-option. * - * @return array + * @return array */ function filter_site_option( array $active_sitewide_plugins ) : array { @@ -166,12 +165,13 @@ function remove_menu() : void { /** * Unclutter the UI for "normal" users. * - * @todo #20 Refactor hard dependency on 'deprecated_figuren_theater_v2' + * @todo https://github.com/figuren-theater/ft-data/issues/21 Refactor hard dependency on 'deprecated_figuren_theater_v2' * * @return void */ function remove_columns_from_lists() : void { + // @phpstan-ignore-next-line if ( ! Figuren_Theater\FT::site()->has_feature( [ FeaturesRepo\Feature__core__contenthub::SLUG ] ) ) { remove_action( 'admin_init', 'Distributor\\SyndicatedPostUI\\setup_columns' ); } @@ -184,9 +184,11 @@ function remove_columns_from_lists() : void { * * @since WP 4.4.0 * - * @param array $args Array of arguments for registering a post type. - * See the register_post_type() function for accepted arguments. - * @param string $post_type Post type key. + * @param array $args Array of arguments for registering a post type. + * See the register_post_type() function for accepted arguments. + * @param string $post_type Post type key. + * + * @return array */ function register_post_type_args( array $args, string $post_type ) : array { if ( ! in_array( $post_type, [ 'dt_ext_connection', 'dt_subscription' ], true ) ) { @@ -225,14 +227,14 @@ function dt_syndicatable_capabilities( string $capabilities ) : string { * * @hook dt_push_post_media * - * @param bool $value If Distributor should push the post media. - * @param int $new_post_id The newly created post ID. - * @param array $media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}. - * @param int $post_id The original post ID. - * @param array $args The arguments passed into wp_insert_post. - * @param Connection $connection The distributor connection being pushed to. + * @param bool $value If Distributor should push the post media. + * @param int $new_post_id The newly created post ID. + * @param array> $media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}. + * @param int $post_id The original post ID. + * @param array>> $args The post data to be inserted. List of 'wp_insert_post()' combatible data. + * @param Connection $connection The distributor connection being pushed to. * - * @return {bool} If Distributor should push the post media. + * @return bool If Distributor should push the post media. */ function dt_push_post_media( bool $value, int $new_post_id, array $media, int $post_id, array $args, Connection $connection ) : bool { return false; @@ -243,12 +245,12 @@ function dt_push_post_media( bool $value, int $new_post_id, array $media, int $p * * @see https://10up.github.io/distributor/dt_push_post_args.html * - * @param array $new_post_args The request body to send. - * @param WP_Post $post The WP_Post that is being pushed. - * @param mixed $connection_args Connection args to push. - * @param Connection $connection The distributor connection being pushed to. + * @param array>> $new_post_args Weirdly, it says: 'The request body to send.', but usually this is: 'The post data to be inserted. List of 'wp_insert_post()' combatible data.' + * @param WP_Post $post The WP_Post that is being pushed. + * @param mixed $connection_args Connection args to push. + * @param Connection $connection The distributor connection being pushed to. * - * @return array + * @return array>> The post data to be inserted. List of 'wp_insert_post()' combatible data. */ function dt_push_post_args( array $new_post_args, WP_Post $post, mixed $connection_args, Connection $connection ) : array { return push_pull_default_args( $new_post_args, $post ); @@ -259,12 +261,12 @@ function dt_push_post_args( array $new_post_args, WP_Post $post, mixed $connecti * * @see https://10up.github.io/distributor/dt_pull_post_args.html * - * @param array $new_post_args The post data to be inserted. - * @param int $remote_post_id The remote post ID. - * @param WP_Post $remote_post The request that got the post. - * @param Connection $connection The Distributor connection pulling the post. + * @param array>> $new_post_args The post data to be inserted. List of 'wp_insert_post()' combatible data. + * @param int $remote_post_id The remote post ID. + * @param WP_Post $remote_post The request that got the post. + * @param Connection $connection The Distributor connection pulling the post. * - * @return array The post data to be inserted. + * @return array>> The post data to be inserted. List of 'wp_insert_post()' combatible data. */ function dt_pull_post_args( array $new_post_args, int $remote_post_id, WP_Post $remote_post, Connection $connection ) : array { return push_pull_default_args( $new_post_args, $remote_post ); @@ -275,14 +277,14 @@ function dt_pull_post_args( array $new_post_args, int $remote_post_id, WP_Post $ * * @todo #20 Refactor hard dependency on 'deprecated_figuren_theater_v2' * - * @param array $new_post_args The post data to be inserted. - * @param WP_Post $original_post The original WP_post. + * @param array>> $new_post_args The post data to be inserted. List of 'wp_insert_post()' combatible data. + * @param WP_Post $original_post The original WP_post. * - * @return array + * @return array>> The post data to be inserted. List of 'wp_insert_post()' combatible data. */ function push_pull_default_args( array $new_post_args, WP_Post $original_post ) : array { // Set author to machine user. - $new_post_args['post_author'] = Users\ft_bot::id(); + $new_post_args['post_author'] = Users\ft_bot::id(); // @phpstan-ignore-line // By default 'Distributor' sets the current date as new published_date. $new_post_args['post_date'] = $original_post->post_date; diff --git a/inc/feed-pull/auto-setup.php b/inc/feed-pull/auto-setup.php index cc575ca..87807cb 100644 --- a/inc/feed-pull/auto-setup.php +++ b/inc/feed-pull/auto-setup.php @@ -18,20 +18,21 @@ use Figuren_Theater\Data\Feed_Pull; use Figuren_Theater\Data\Rss_Bridge; +use function add_action; // use FP_DELETED_OPTION_NAME; // 'fp_deleted_syndicated' // Debugging only. -use function add_action; use function get_post; use function get_post_meta; use function get_term_by; -use function is_wp_error; use function wp_delete_post; use function wp_insert_post; use function wp_slash; +use WP_Error; use WP_Post; use WP_Query; +use WP_Term; // Normally defined in Post_Types\Post_Type__ft_link::NAME . const LINK_PT = 'ft_link'; @@ -91,14 +92,15 @@ function create_feed_post( WP_Post $link_post ) : void { * * @see Figuren_Theater\src\FeaturesAssets\core-my-registration\wp_core.php */ - $suggestion = get_post_meta( $link_post->ID, '_ft_platform', true ) ?? null; + $suggestion = get_post_meta( $link_post->ID, '_ft_platform', true ); + $suggestion = ( \is_string( $suggestion ) ) ? $suggestion : null; + $bridged_url = Rss_Bridge\get_bridged_url( $link_post->post_content, $suggestion ); + if ( empty( $bridged_url ) ) { + return; + } // Get bridged URL. - $fp_feed_url = esc_url( - Rss_Bridge\get_bridged_url( $link_post->post_content, $suggestion ), - 'https', - 'db' - ); + $fp_feed_url = esc_url( $bridged_url, [ 'https' ], 'db' ); // Bail, if not importable. if ( ! $fp_feed_url ) { @@ -107,10 +109,10 @@ function create_feed_post( WP_Post $link_post ) : void { // Prepare the insert arguments. $insert_args = wp_slash( [ - 'post_author' => $link_post->post_author, + 'post_author' => (int) $link_post->post_author, 'post_type' => Feed_Pull\FEED_POSTTYPE, 'post_title' => 'Feed: ' . $link_post->post_content, - 'post_parent' => $link_post->ID, + 'post_parent' => (int) $link_post->ID, 'post_status' => 'publish', 'menu_order' => 0, @@ -127,18 +129,21 @@ function create_feed_post( WP_Post $link_post ) : void { ]); // Create the feed post with the link post as parent. - $feed_post_id = wp_insert_post( $insert_args ); - - if ( is_wp_error( $feed_post_id ) ) { - // Log an error if the feed post could not be created. - error_log( - sprintf( - 'Error creating feed post for link post with ID %d: %s', - $link_post->ID, - $feed_post_id->get_error_message() - ) - ); + $feed_post_id = wp_insert_post( $insert_args, true ); + + if ( ! $feed_post_id instanceof WP_Error ) { + return; } + + // Something went wrong. + // Log an error if the feed post could not be created. + error_log( + sprintf( + 'Error creating feed post for link post with ID %d: %s', + $link_post->ID, + $feed_post_id->get_error_message() + ) + ); } /** @@ -147,11 +152,11 @@ function create_feed_post( WP_Post $link_post ) : void { * Fires after an object's terms have been set. * * @param int $link_post_id Object ID. - * @param array $terms An array of object term IDs or slugs. - * @param array $new_terms An array of term taxonomy IDs. + * @param string[]|int[] $terms An array of object term IDs or slugs. + * @param int[] $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_terms Old array of term taxonomy IDs. + * @param int[] $old_terms Old array of term taxonomy IDs. */ function add_or_delete_feed_post( int $link_post_id, array $terms, array $new_terms, string $taxonomy, bool $append, array $old_terms ) : void { $import_term_id = get_import_term_id(); @@ -177,7 +182,7 @@ function add_or_delete_feed_post( int $link_post_id, array $terms, array $new_te $link_post = get_post( $link_post_id ); // Return early if not a link post. - if ( ! is_a( $link_post, 'WP_Post' ) || $link_post->post_type !== LINK_PT ) { + if ( ( ! $link_post instanceof WP_Post ) || $link_post->post_type !== LINK_PT ) { return; } @@ -209,7 +214,7 @@ function delete_feed_post_on_trash( int $link_post_id ) : void { $link_post = get_post( $link_post_id ); // Bail if post type is not a Link. - if ( $link_post->post_type !== LINK_PT ) { + if ( \is_null( $link_post ) || $link_post->post_type !== LINK_PT ) { return; } // Delete & trash the feed post. @@ -231,7 +236,11 @@ function get_feed_from_link( int $link_post_id ) : int { 'numberposts' => 1, ] ); - return ( empty( $feed_query->posts ) ) ? 0 : $feed_query->posts[0]->ID; + if ( empty( $feed_query->posts ) || ! $feed_query->posts[0] instanceof WP_Post ) { + return 0; + } + + return $feed_query->posts[0]->ID; } /** @@ -240,7 +249,12 @@ function get_feed_from_link( int $link_post_id ) : int { * @return int */ function get_import_term_id() : int { - $term = get_term_by( 'slug', UTILITY_TERM, UTILITY_TAX ); // Get the "import" term ID. - return ( is_wp_error( $term ) ) ? 0 : $term->term_id; + $term = get_term_by( 'slug', UTILITY_TERM, UTILITY_TAX ); + + if ( ! $term instanceof WP_Term ) { + return 0; + } + + return $term->term_id; } diff --git a/inc/feed-pull/import.php b/inc/feed-pull/import.php index e6893c8..ea8df80 100644 --- a/inc/feed-pull/import.php +++ b/inc/feed-pull/import.php @@ -24,6 +24,9 @@ use function wp_parse_args; use function wp_slash; +use SimpleXMLElement; +use WP_Post; + /** * Bootstrap module, when enabled. * @@ -53,8 +56,8 @@ function init() { add_filter( 'fp_post_args', __NAMESPACE__ . '\\fp_post_args', 10, 3 ); - add_filter( 'default_post_metadata', __NAMESPACE__ . '\\default_post_metadata', 10, 3 ); - add_filter( 'update_post_metadata', __NAMESPACE__ . '\\dont_update_post_metadata', 1000, 3 ); + add_filter( 'default_post_metadata', __NAMESPACE__ . '\\default_post_metadata', 10, 5 ); + add_filter( 'update_post_metadata', __NAMESPACE__ . '\\dont_update_post_metadata', 1000, 5 ); } /** @@ -64,7 +67,7 @@ function init() { * * @see https://github.com/tlovett1/feed-pull/blob/45d667c1275cca0256bd03ed6fa1655cdf26f064/includes/class-fp-pull.php#L174-L181 * - * @return array post_meta keys we want to act on or with. + * @return string[] post_meta keys we want to act on or with. */ function get_default_static_metas() : array { return [ @@ -167,7 +170,7 @@ function default_post_metadata( mixed $value, int $object_id, string $meta_key, * Default static post_meta that doesn't need to be saved into DB * because its the same (per bridge) * - * @return array List of feed-fields and their mappings within WordPress, following the 'feed-pull'-plugin conventions. + * @return array> List of feed-fields and their mappings within WordPress, following the 'feed-pull'-plugin conventions. */ function get_fp_field_map() : array { return [ @@ -279,8 +282,8 @@ function dont_update_post_metadata( null|bool $check, int $object_id, string $me * * @see https://github.com/tlovett1/feed-pull/blob/45d667c1275cca0256bd03ed6fa1655cdf26f064/includes/class-fp-pull.php#L274 * - * @param mixed $pre_filter_post_value [description] - * @param array $field [description] + * @param string[]|string $pre_filter_post_value Content of the current feed-field iterated over. + * @param array $field One of the field types, see get_fp_field_map() for reference. * * array ( * 'source_field' => 'guid', @@ -288,12 +291,16 @@ function dont_update_post_metadata( null|bool $check, int $object_id, string $me * 'mapping_type' => 'post_field', * ), * - * @param WP_Post $post [description] - * @param int $source_feed_id [description] + * @param WP_Post $post The new feed-item to import. + * @param int $source_feed_id The feed-post that we subscribed to. * - * @return string [description] + * @return string */ -function fp_pre_post_insert_value( $pre_filter_post_value, $field, $post, $source_feed_id ) : string { +function fp_pre_post_insert_value( array|string $pre_filter_post_value, array $field, WP_Post $post, int $source_feed_id ) : string { + + if ( \is_array( $pre_filter_post_value ) ) { + $pre_filter_post_value = $pre_filter_post_value[0]; + } if ( 'post_title' === $field['destination_field'] ) { return sanitize_text_field( $pre_filter_post_value ); @@ -318,13 +325,15 @@ function fp_pre_post_insert_value( $pre_filter_post_value, $field, $post, $sourc * Last chance to change anything * and: anything available ;) * - * @param array $new_post_args [description] - * @param ?????? $post This is not a WP_Post. - * @param int $source_feed_id This is the fp_feed Post, at least its ID, which is sourcing the new post + * @todo https://github.com/figuren-theater/ft-data/issues/21 Remove hard dependency on 'deprecated__Figuren_Theater__v2' using Taxonomies\... + * + * @param array>> $new_post_args List of 'wp_insert_post()' combatible data. + * @param SimpleXMLElement $post This is the sourced feed element. + * @param int $source_feed_id This is the fp_feed Post, at least its ID, which is sourcing the new post. * - * @return array List of 'wp_insert_post()' combatible data. + * @return array>> List of 'wp_insert_post()' combatible data. */ -function fp_post_args( array $new_post_args, $post, int $source_feed_id ) : array { +function fp_post_args( array $new_post_args, SimpleXMLElement $post, int $source_feed_id ) : array { $import_args = get_import_args_from_source( $source_feed_id ); @@ -333,7 +342,7 @@ function fp_post_args( array $new_post_args, $post, int $source_feed_id ) : arra $import_args['ping_status'] = 'closed'; // set author to machine user, if non set. - $new_post_args['post_author'] ?: Users\ft_bot::id(); + $new_post_args['post_author'] ?: Users\ft_bot::id(); // @phpstan-ignore-line // Strip (maybe) filled excerpt // if we can auto-generate it. @@ -347,13 +356,15 @@ function fp_post_args( array $new_post_args, $post, int $source_feed_id ) : arra } /** - * Transform a given feed post_id into an array of wp_insert_post() compatible data for the new, to import, post. + * Transform a given feed post_id into an array + * of wp_insert_post() compatible data + * for the new, to import, post. * * @todo https://github.com/figuren-theater/ft-data/issues/21 Remove hard dependency on 'deprecated__Figuren_Theater__v2' using Taxonomies\... * - * @param int $source_feed_id The post_ID of the sourcing feed. + * @param int $source_feed_id The post_ID of the sourcing feed. * - * @return array + * @return array>> List of 'wp_insert_post()' combatible data. */ function get_import_args_from_source( int $source_feed_id ) : array { // 1. get sourced 'ft_link' post, @@ -361,10 +372,11 @@ 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_link = Taxonomies\Taxonomy__ft_link_shadow::NAME; // @phpstan-ignore-line + $tax_shadow = Taxonomies\TAX_Shadow::init(); // @phpstan-ignore-line + $ft_link_term = $tax_shadow->get_associated_term( $ft_link, - Taxonomies\Taxonomy__ft_link_shadow::NAME + $tax_shadow_link ); // 3. translate 'utility-tax' terms at the source @@ -372,7 +384,7 @@ function get_import_args_from_source( int $source_feed_id ) : array { // $args = get_post_fields_from_utility_terms( $source_feed_id ) + [ $args = [ 'tax_input' => [ - Taxonomies\Taxonomy__ft_link_shadow::NAME => [ $ft_link_term->term_id ], + $tax_shadow_link => [ (int) $ft_link_term->term_id ], ], ]; diff --git a/inc/feed-pull/namespace.php b/inc/feed-pull/namespace.php index 2c76b8c..9fd3234 100644 --- a/inc/feed-pull/namespace.php +++ b/inc/feed-pull/namespace.php @@ -13,6 +13,7 @@ use function add_action; use function add_filter; use function current_user_can; +use function get_current_screen; use function is_admin; use function is_network_admin; use function is_user_admin; @@ -141,7 +142,7 @@ function register_post_type_args( array $args ) : array { $args['menu_position'] = 100; $args['taxonomies'] = $args['taxonomies'] ?? []; - $args['taxonomies'][] = Features\UtilityFeaturesManager::TAX; + $args['taxonomies'][] = Features\UtilityFeaturesManager::TAX; // @phpstan-ignore-line return $args; } @@ -154,10 +155,15 @@ function register_post_type_args( array $args ) : array { */ function modify_metaboxes() : void { - remove_meta_box( 'slugdiv', null, 'normal' ); + $screen = get_current_screen(); + if ( \is_null( $screen ) ) { + return; + } + + remove_meta_box( 'slugdiv', $screen, 'normal' ); if ( ! current_user_can( 'manage_sites' ) ) { - remove_meta_box( 'postcustom', null, 'normal' ); + remove_meta_box( 'postcustom', $screen, 'normal' ); } }