Skip to content

Commit

Permalink
Refactor while fixing CS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Oct 16, 2023
1 parent 6e48905 commit edf3d3e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 89 deletions.
103 changes: 66 additions & 37 deletions inc/feed-pull/auto-setup.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types=1);
/**
* Figuren_Theater Data Feed_Pull.
*
Expand All @@ -14,47 +13,61 @@
* @package figuren-theater/ft-data
*/

namespace Figuren_Theater\Data\Feed_Pull;
namespace Figuren_Theater\Data\Feed_Pull\Auto_Setup;

use Figuren_Theater\Data\Feed_Pull;

use Figuren_Theater\Data\Rss_Bridge;

// use FP_DELETED_OPTION_NAME; // 'fp_deleted_syndicated'
// use FP_DELETED_OPTION_NAME; // 'fp_deleted_syndicated' // Debugging only.

use function add_action;
use function get_post;
use function get_posts;
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;

// const LINK_PT = Post_Types\Post_Type__ft_link::NAME;
use WP_Post;
use WP_Query;

// Normally defined in Post_Types\Post_Type__ft_link::NAME .
const LINK_PT = 'ft_link';
// const UTILITY_TAX = Features\UtilityFeaturesManager::TAX;
// Normally defined in Features\UtilityFeaturesManager::TAX .
const UTILITY_TAX = 'hm-utility';
// const UTILITY_TERM = UtilityFeaturesRepo\UtilityFeature__ft_link__feedpull_import::SLUG; //'feedpull-import',;
// Normally defined in UtilityFeaturesRepo\UtilityFeature__ft_link__feedpull_import::SLUG .
const UTILITY_TERM = 'feedpull-import';

function bootstrap_auto_setup() {

/**
* Bootstrap module, when enabled.
*
* @return void
*/
function bootstrap() :void {
add_action( 'admin_init', __NAMESPACE__ . '\\admin_init', 5 );
}

/**
* Define hooks for automated 'CRUD' of FEED_POSTTYPE posts.
*
* @return void
*/
function admin_init() {

// for debugging only
// Debugging only.
// phpcs:ignore
// delete_option( FP_DELETED_OPTION_NAME );

// Hook into save_post to create/update feed post
// add_action( 'save_post_'.LINK_PT, __NAMESPACE__ . '\\create_feed_post', 10, 2 );
// Hook into save_post to create/update feed post.
// add_action( 'save_post_'.LINK_PT, __NAMESPACE__ . '\\create_feed_post', 10, 2 ); // DEBUG !

// Hook into set_object_terms to add or delete a feed post
// Hook into set_object_terms to add or delete a feed post.
add_action( 'set_object_terms', __NAMESPACE__ . '\\add_or_delete_feed_post', 10, 6 );

// Hook into wp_trash_post to delete feed post
// add_action( 'wp_trash_post', __NAMESPACE__ . '\\delete_feed_post_on_trash' );
// Hook into wp_trash_post to delete feed post.
// add_action( 'wp_trash_post', __NAMESPACE__ . '\\delete_feed_post_on_trash' ); // MAYBE REMOVE, if really not needed anymore !
add_action( 'before_delete_post', __NAMESPACE__ . '\\delete_feed_post_on_trash' );
}

Expand All @@ -67,32 +80,35 @@ function admin_init() {
*/
function create_feed_post( WP_Post $post ) : void {

// Bail if post type is not a Link
// Bail if post type is not a Link.
if ( $post->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',
Expand All @@ -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 => [],
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 );
}
}
Expand All @@ -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.
Expand Down
69 changes: 34 additions & 35 deletions inc/feed-pull/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

View workflow job for this annotation

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

Unused use statement

use Figuren_Theater\Data\Rss_Bridge;
use Figuren_Theater\Network\Taxonomies;
use Figuren_Theater\Network\Users;

Expand All @@ -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() {

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

View workflow job for this annotation

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

Missing doc comment for 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

Check failure on line 44 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
add_filter( 'fp_pre_post_insert_value', __NAMESPACE__ . '\\fp_pre_post_insert_value', 10, 4 );
Expand Down Expand Up @@ -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 ] ) ) {
Expand All @@ -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();
Expand Down Expand Up @@ -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',
],
Expand Down Expand Up @@ -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 {
/*

Check warning on line 233 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 @@ -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;
}

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

Expand All @@ -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'] );
}
Expand All @@ -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

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

View workflow job for this annotation

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

Variable $taxonomy is undefined.
);
Expand Down
Loading

0 comments on commit edf3d3e

Please sign in to comment.