Skip to content

Commit

Permalink
Add 'Feed Author' field when editing Feed.
Browse files Browse the repository at this point in the history
Where available, this value is pulled from the RSS feed, using
the heuristics in SimplePie's `get_authors()` method.

Incoming feed items that do not have an associated author
fall back on the feed's author.

See #582.
  • Loading branch information
boonebgorges committed Dec 12, 2024
1 parent 8f685aa commit b165e5e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Core/API/FeedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ public function register_rest_fields() {
],
]
);

register_rest_field(
$this->post_type,
'feed_author',
[
'get_callback' => function ( $post_object ) {
$feed_object = Feed::get_instance_by_id( $post_object['id'] );
return $feed_object->get_feed_author();
},
'update_callback' => function ( $value, $post ) {
$feed_object = Feed::get_instance_by_id( $post->ID );
$feed_object->set_feed_author( $value );
},
'schema' => [
'description' => __( 'The author of the feed', 'pressforward' ),
'type' => 'string',
'context' => [ 'view', 'edit' ],
],
]
);
}

/**
Expand Down
45 changes: 44 additions & 1 deletion Core/Models/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,45 @@ public function retrieve() {
return pressforward( 'schema.feed_item' )->assemble_feed_for_pull( $feed_data_object );
}

/**
* Gets the feed author for the feed.
*
* @since 5.8.0
*
* @return string
*/
public function get_feed_author() {
$feed_author_meta = get_post_meta( $this->get( 'id' ), 'feed_author', true );

if ( is_string( $feed_author_meta ) ) {
return $feed_author_meta;
}

// Legacy items have feed_author saved as a SimplePie Author object.
if ( is_object( $feed_author_meta ) ) {
// May be an incomplete class.
$feed_author_meta = (array) $feed_author_meta;
if ( isset( $feed_author_meta['name'] ) ) {
update_post_meta( $this->get( 'id' ), 'feed_author', $feed_author_meta['name'] );
return $feed_author_meta['name'];
}
}

return '';
}

/**
* Sets the feed author for the feed.
*
* @since 5.8.0
*
* @param string $author Author name.
* @return void
*/
public function set_feed_author( $author ) {
update_post_meta( $this->get( 'id' ), 'feed_author', $author );
}

/**
* Gets the module to be used for this feed.
*
Expand Down Expand Up @@ -416,7 +455,11 @@ public function health_check( $is_new_feed = false ) {
$this->set( 'title', $the_feed->get_title() );
$this->set( 'description', $the_feed->get_description() );
$this->set( 'htmlUrl', $the_feed->get_link( 0 ) );
$this->set( 'feed_author', $the_feed->get_author() );

$author = $the_feed->get_author();
$author_name = method_exists( $author, 'get_name' ) ? $author->get_name() : '';
$this->set( 'feed_author', $author_name );

$this->set( 'thumbnail', $the_feed->get_image_url() );

$this->save();
Expand Down
12 changes: 12 additions & 0 deletions assets/src/block-editor-feeds/block-editor-feeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ const BlockEditorFeedsInfobox = ( {} ) => {

const {
errorMessage,
feedAuthor,
feedUrl,
postType
} = useSelect( ( select ) => {
const editedPostId = select( 'core/editor' ).getCurrentPostId()
const editedPostMeta = select( 'core/editor' ).getEditedPostAttribute( 'meta' )
const editedErrorMessage = select( 'core/editor' ).getEditedPostAttribute( 'alert_message' )
const editedFeedAuthor = select( 'core/editor' ).getEditedPostAttribute( 'feed_author' )

return {
errorMessage: editedErrorMessage,
feedAuthor: editedFeedAuthor,
feedUrl: editedPostMeta?.feed_url || '',
postId: editedPostId,
postType: select( 'core/editor' ).getEditedPostAttribute( 'type' ),
Expand Down Expand Up @@ -52,6 +55,15 @@ const BlockEditorFeedsInfobox = ( {} ) => {
} }
/>

<TextControl
label={ __( 'Feed Author', 'pressforward' ) }
value={ feedAuthor }
onChange={ ( newValue ) => {
editPost( { 'feed_author': newValue } )
} }
help={ __( 'Incoming items without an author name will use this value.', 'pressforward' ) }
/>

{ errorMessage && (
<p className="pf-error-message">
<strong>
Expand Down
9 changes: 9 additions & 0 deletions modules/rss-import/rss-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ public function get_data_object( $a_feed ) {

if ( ! $ag_status ) {
$authors = $this->get_rss_authors( $item );

if ( __( 'No author.', 'pressforward' ) === $authors ) {
// See if the parent feed has an author.
$parent_feed_obj = pressforward( 'schema.feeds' )->get_instance_by_id( $a_feed->ID );
$parent_feed_author = $parent_feed_obj->get_feed_author();
if ( ! empty( $parent_feed_author ) ) {
$authors = $parent_feed_author;
}
}
} else {
$parent_value = pressforward( 'controller.metas' )->get_post_pf_meta( $a_feed->ID, 'pf_feed_default_author', true );
if ( ! empty( $parent_value ) ) {
Expand Down

0 comments on commit b165e5e

Please sign in to comment.