Skip to content

Commit

Permalink
WIP Fix CS & phpstan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Oct 17, 2023
1 parent 5a966f3 commit e6ce49c
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 86 deletions.
64 changes: 33 additions & 31 deletions inc/distributor/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 );
Expand All @@ -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<string, string> $active_sitewide_plugins WordPress' default 'active_sitewide_plugins' site-option.
*
* @return array
* @return array<string, string>
*/
function filter_site_option( array $active_sitewide_plugins ) : array {

Expand Down Expand Up @@ -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' );
}
Expand All @@ -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<string, mixed> $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<string, mixed>
*/
function register_post_type_args( array $args, string $post_type ) : array {
if ( ! in_array( $post_type, [ 'dt_ext_connection', 'dt_subscription' ], true ) ) {
Expand Down Expand Up @@ -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<int, array<string, mixed>> $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<string, array<int|string, array<int, int>>> $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;
Expand All @@ -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<string, array<int|string, array<int, int>>> $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<string, array<int|string, array<int, int>>> 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 );
Expand All @@ -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<string, array<int|string, array<int, int>>> $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<string, array<int|string, array<int, int>>> 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 );
Expand All @@ -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<string, array<int|string, array<int, int>>> $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<string, array<int|string, array<int, int>>> 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;
Expand Down
72 changes: 43 additions & 29 deletions inc/feed-pull/auto-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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,
Expand All @@ -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()
)
);
}

/**
Expand All @@ -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();
Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
Loading

0 comments on commit e6ce49c

Please sign in to comment.