Skip to content

Commit

Permalink
WIP Fix CS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Oct 17, 2023
1 parent e55497d commit 5a966f3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 78 deletions.
64 changes: 39 additions & 25 deletions inc/feed-pull/auto-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ function admin_init() {
* when a link post with the "import" term
* is saved or updated.
*
* @param WP_Post $post The post object being saved or updated.
* @param WP_Post $link_post The post object being saved or updated.
*/
function create_feed_post( WP_Post $post ) : void {
function create_feed_post( WP_Post $link_post ) : void {

// Bail if post type is not a Link.
if ( $post->post_type !== LINK_PT ) {
if ( $link_post->post_type !== LINK_PT ) {
return;
}

Expand All @@ -91,11 +91,11 @@ function create_feed_post( WP_Post $post ) : void {
*
* @see Figuren_Theater\src\FeaturesAssets\core-my-registration\wp_core.php
*/
$suggestion = get_post_meta( $post->ID, '_ft_platform', true ) ?? null;
$suggestion = get_post_meta( $link_post->ID, '_ft_platform', true ) ?? null;

// Get bridged URL.
$fp_feed_url = esc_url(
Rss_Bridge\get_bridged_url( $post->post_content, $suggestion ),
Rss_Bridge\get_bridged_url( $link_post->post_content, $suggestion ),
'https',
'db'
);
Expand All @@ -107,10 +107,10 @@ function create_feed_post( WP_Post $post ) : void {

// Prepare the insert arguments.
$insert_args = wp_slash( [
'post_author' => $post->post_author,
'post_author' => $link_post->post_author,
'post_type' => Feed_Pull\FEED_POSTTYPE,
'post_title' => 'Feed: ' . $post->post_content,
'post_parent' => $post->ID,
'post_title' => 'Feed: ' . $link_post->post_content,
'post_parent' => $link_post->ID,
'post_status' => 'publish',

'menu_order' => 0,
Expand All @@ -134,7 +134,7 @@ function create_feed_post( WP_Post $post ) : void {
error_log(
sprintf(
'Error creating feed post for link post with ID %d: %s',
$post->ID,
$link_post->ID,
$feed_post_id->get_error_message()
)
);
Expand All @@ -146,25 +146,38 @@ function create_feed_post( WP_Post $post ) : void {
*
* Fires after an object's terms have been set.
*
* @param int $object_id Object ID.
* @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 $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.
*/
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();
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();

// 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, true ) && ! in_array( $import_term_id, $old_terms, true ) ) {
$is_in_new = in_array( $import_term_id, $new_terms, true );
$is_in_old = in_array( $import_term_id, $old_terms, true );

// Return early if not the utility taxonomy.
if ( $taxonomy !== UTILITY_TAX ) {
return;
}

// Return early if not the 'import' term being added or removed.
if ( ! $is_in_new && ! $is_in_old ) {
return;
}

$post = get_post( $object_id );
// Return early if the 'import' term already existed and is not removed.
if ( $is_in_new && $is_in_old ) {
return;
}

$link_post = get_post( $link_post_id );

// Return early if not a link post.
if ( ! is_a( $post, 'WP_Post' ) || $post->post_type !== LINK_PT ) {
if ( ! is_a( $link_post, 'WP_Post' ) || $link_post->post_type !== LINK_PT ) {
return;
}

Expand All @@ -173,12 +186,13 @@ function add_or_delete_feed_post( int $object_id, array $terms, array $new_terms
// added or removed from Link posts.
//
// 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 );
if ( $is_in_new && ! $is_in_old ) {
create_feed_post( $link_post );

// 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 );
} elseif ( ! $is_in_new && $is_in_old ) {

$feed_post_id = get_feed_from_link( $link_post_id );
if ( $feed_post_id ) {
// Delete without trash bin.
wp_delete_post( $feed_post_id, true );
Expand All @@ -189,17 +203,17 @@ function add_or_delete_feed_post( int $object_id, array $terms, array $new_terms
/**
* Delete feed post when a link post is trashed.
*
* @param int $post_id The ID of the post being trashed.
* @param int $link_post_id The ID of the post being trashed.
*/
function delete_feed_post_on_trash( int $post_id ) : void {
$post = get_post( $post_id );
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 ( $post->post_type !== LINK_PT ) {
if ( $link_post->post_type !== LINK_PT ) {
return;
}
// Delete & trash the feed post.
wp_delete_post( get_feed_from_link( $post_id ), true );
wp_delete_post( get_feed_from_link( $link_post_id ), true );
}

/**
Expand Down
96 changes: 45 additions & 51 deletions inc/feed-pull/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Figuren_Theater\Data\Feed_Pull\Import;

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;
Expand All @@ -31,17 +30,25 @@
* @return void
*/
function bootstrap() :void {

add_action( 'init', __NAMESPACE__ . '\\init', 5 );
}

/**
* Define hooks for automated 'CRUD' of imported/sourced posts.
*
* @return void
*/
function init() {

// 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
/**
* Modify the imported content before save.
*
* @see 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 );

add_filter( 'fp_post_args', __NAMESPACE__ . '\\fp_post_args', 10, 3 );
Expand All @@ -66,25 +73,26 @@ function get_default_static_metas() : array {
// post_meta of a normal 'fp_feed' post
// ////////////////////////////////////////////////////

// 'fp_feed_url', // that should not treated by our filters
// 'fp_feed_url', // Should not treated by our filters.

Check warning on line 76 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

This comment is 50% valid code; is this commented out code?

'fp_posts_xpath', // this should come from a defined BridgeAdapter
'fp_field_map', // this should come from a defined BridgeAdapter
// 'fp_post_status', // this should NOT come from a defined BridgeAdapter
// 'fp_post_type', // this should NOT come from a defined BridgeAdapter
'fp_allow_updates', // this should come from a defined BridgeAdapter
'fp_new_post_categories', // this should come from a defined BridgeAdapter
// 'fp_custom_namespaces', // this should come from a defined BridgeAdapter
// 'fp_namespace_prefix', // this should come from a defined BridgeAdapter
// 'fp_namespace_url', // this should come from a defined BridgeAdapter
'fp_posts_xpath', // Should ... come from a defined BridgeAdapter.
'fp_field_map', // Should ... come from a defined BridgeAdapter.
// 'fp_post_status', // Should NOT come from a defined BridgeAdapter.
// 'fp_post_type', // Should NOT come from a defined BridgeAdapter.
'fp_allow_updates', // Should ... come from a defined BridgeAdapter.
'fp_new_post_categories', // Should ... come from a defined BridgeAdapter.
// 'fp_custom_namespaces', // Should ... come from a defined BridgeAdapter.
// 'fp_namespace_prefix', // Should ... come from a defined BridgeAdapter.
// 'fp_namespace_url', // Should ... come from a defined BridgeAdapter.

// ////////////////////////////////////////////////////
// post_meta of an imported DESTINATION_POSTTYPE post
// ////////////////////////////////////////////////////

// 'fp_source_feed_id', // source post_ID
'fp_syndicated_post', // 1 // should not be saved, as it's never used by the plugin itself
// 'fp_guid', // that should not treated by our filters
// 'fp_source_feed_id', // source post_ID

Check warning on line 92 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

This comment is 50% valid code; is this commented out code?
'fp_syndicated_post', // 1 // should not be saved, as it's never used by the plugin itself.

// 'fp_guid', // Should not treated by our filters.

Check warning on line 95 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

This comment is 50% valid code; is this commented out code?
];
}

Expand Down Expand Up @@ -136,7 +144,7 @@ function default_post_metadata( mixed $value, int $object_id, string $meta_key,
return $adapter['fp_field_map'] ?? get_fp_field_map();

// case 'fp_post_status':

Check warning on line 146 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

This comment is 50% valid code; is this commented out code?
// return 'pending';
// return 'pending'; //

Check failure on line 147 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

Inline comments must end in full-stops, exclamation marks, or question marks

// case 'fp_post_type':

Check warning on line 149 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

This comment is 53% valid code; is this commented out code?
// return $adapter['fp_post_type'] ?? DESTINATION_POSTTYPE;

Check failure on line 150 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

Inline comments must end in full-stops, exclamation marks, or question marks
Expand Down Expand Up @@ -229,7 +237,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 $prev_value ) : mixed {
function dont_update_post_metadata( null|bool $check, int $object_id, string $meta_key, mixed $meta_value, mixed $prev_value ) : null|bool {
/*

Check warning on line 241 in inc/feed-pull/import.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

This comment is 48% valid code; is this commented out code?
// one special-operation
// but instead of writing to post_meta
Expand Down Expand Up @@ -267,23 +275,25 @@ function dont_update_post_metadata( $check, int $object_id, string $meta_key, mi
}

/**
* [fp_pre_post_insert_value description]
* Modify the imported content before save.
*
* @param [mixed] $pre_filter_post_value [description]
* @param [array] $field [description]
* @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]
*
* array (
* 'source_field' => 'guid',
* 'destination_field' => 'guid',
* 'mapping_type' => 'post_field',
* ),
*
* @param [WP_Post] $post [description]
* @param [int] $source_feed_id [description]
* @param WP_Post $post [description]
* @param int $source_feed_id [description]
*
* @return [type] [description]
* @return string [description]
*/
function fp_pre_post_insert_value( $pre_filter_post_value, $field, $post, $source_feed_id ): string {
function fp_pre_post_insert_value( $pre_filter_post_value, $field, $post, $source_feed_id ) : string {

if ( 'post_title' === $field['destination_field'] ) {
return sanitize_text_field( $pre_filter_post_value );
Expand Down Expand Up @@ -336,6 +346,15 @@ function fp_post_args( array $new_post_args, $post, int $source_feed_id ) : arra
return wp_slash( $new_post_args );
}

/**
* 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.
*
* @return array
*/
function get_import_args_from_source( int $source_feed_id ) : array {
// 1. get sourced 'ft_link' post,
// which is the parent of the 'fp_feed' that is sourcing this post
Expand All @@ -345,7 +364,7 @@ function get_import_args_from_source( int $source_feed_id ) : array {
$tax_shadow = Taxonomies\TAX_Shadow::init();
$ft_link_term = $tax_shadow->get_associated_term(
$ft_link,
$taxonomy
Taxonomies\Taxonomy__ft_link_shadow::NAME
);

// 3. translate 'utility-tax' terms at the source
Expand All @@ -360,28 +379,3 @@ function get_import_args_from_source( int $source_feed_id ) : array {
return $args;
}

/*
function get_post_fields_from_utility_terms( int $source_feed_id ) : array {
$utility_terms = get_the_terms(
get_post( $source_feed_id ),
Features\UtilityFeaturesManager::TAX
);
if (empty($utility_terms)) {
return [];
}
$utility_terms = wp_list_pluck( $utility_terms, 'slug' );
return array_map(
__NAMESPACE__ . '\\__get_post_field_from_term_slug',
$utility_terms
);
}
function __get_post_field_from_term_slug( string $term_slug ) : array {
$parts = explode('-', $term_slug);
$field = str_replace('post', 'post_', $parts[1]);
return [ $field => $parts[2] ];
}
*/
6 changes: 4 additions & 2 deletions inc/feed-pull/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ function remove_menu() : void {
/**
* Modify 'fp_feed' post_type
*
* @todo https://github.com/figuren-theater/ft-data/issues/21 Remove hard dependency on 'deprecated__Figuren_Theater__v2' using Taxonomies\...
*
* @see https://github.com/tlovett1/feed-pull/blob/45d667c1275cca0256bd03ed6fa1655cdf26f064/includes/class-fp-source-feed-cpt.php#L136
*
* @param array $args Arguments for registering a post type. See the register_post_type() function for accepted arguments.
* @param array<string, mixed> $args Arguments for registering a post type. See the register_post_type() function for accepted arguments.
*
* @return array
* @return array<string, mixed>
*/
function register_post_type_args( array $args ) : array {

Expand Down

0 comments on commit 5a966f3

Please sign in to comment.