Skip to content

Commit b165e5e

Browse files
committed
Add 'Feed Author' field when editing Feed.
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.
1 parent 8f685aa commit b165e5e

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

Core/API/FeedEndpoint.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ public function register_rest_fields() {
183183
],
184184
]
185185
);
186+
187+
register_rest_field(
188+
$this->post_type,
189+
'feed_author',
190+
[
191+
'get_callback' => function ( $post_object ) {
192+
$feed_object = Feed::get_instance_by_id( $post_object['id'] );
193+
return $feed_object->get_feed_author();
194+
},
195+
'update_callback' => function ( $value, $post ) {
196+
$feed_object = Feed::get_instance_by_id( $post->ID );
197+
$feed_object->set_feed_author( $value );
198+
},
199+
'schema' => [
200+
'description' => __( 'The author of the feed', 'pressforward' ),
201+
'type' => 'string',
202+
'context' => [ 'view', 'edit' ],
203+
],
204+
]
205+
);
186206
}
187207

188208
/**

Core/Models/Feed.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,45 @@ public function retrieve() {
349349
return pressforward( 'schema.feed_item' )->assemble_feed_for_pull( $feed_data_object );
350350
}
351351

352+
/**
353+
* Gets the feed author for the feed.
354+
*
355+
* @since 5.8.0
356+
*
357+
* @return string
358+
*/
359+
public function get_feed_author() {
360+
$feed_author_meta = get_post_meta( $this->get( 'id' ), 'feed_author', true );
361+
362+
if ( is_string( $feed_author_meta ) ) {
363+
return $feed_author_meta;
364+
}
365+
366+
// Legacy items have feed_author saved as a SimplePie Author object.
367+
if ( is_object( $feed_author_meta ) ) {
368+
// May be an incomplete class.
369+
$feed_author_meta = (array) $feed_author_meta;
370+
if ( isset( $feed_author_meta['name'] ) ) {
371+
update_post_meta( $this->get( 'id' ), 'feed_author', $feed_author_meta['name'] );
372+
return $feed_author_meta['name'];
373+
}
374+
}
375+
376+
return '';
377+
}
378+
379+
/**
380+
* Sets the feed author for the feed.
381+
*
382+
* @since 5.8.0
383+
*
384+
* @param string $author Author name.
385+
* @return void
386+
*/
387+
public function set_feed_author( $author ) {
388+
update_post_meta( $this->get( 'id' ), 'feed_author', $author );
389+
}
390+
352391
/**
353392
* Gets the module to be used for this feed.
354393
*
@@ -416,7 +455,11 @@ public function health_check( $is_new_feed = false ) {
416455
$this->set( 'title', $the_feed->get_title() );
417456
$this->set( 'description', $the_feed->get_description() );
418457
$this->set( 'htmlUrl', $the_feed->get_link( 0 ) );
419-
$this->set( 'feed_author', $the_feed->get_author() );
458+
459+
$author = $the_feed->get_author();
460+
$author_name = method_exists( $author, 'get_name' ) ? $author->get_name() : '';
461+
$this->set( 'feed_author', $author_name );
462+
420463
$this->set( 'thumbnail', $the_feed->get_image_url() );
421464

422465
$this->save();

assets/src/block-editor-feeds/block-editor-feeds.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ const BlockEditorFeedsInfobox = ( {} ) => {
1616

1717
const {
1818
errorMessage,
19+
feedAuthor,
1920
feedUrl,
2021
postType
2122
} = useSelect( ( select ) => {
2223
const editedPostId = select( 'core/editor' ).getCurrentPostId()
2324
const editedPostMeta = select( 'core/editor' ).getEditedPostAttribute( 'meta' )
2425
const editedErrorMessage = select( 'core/editor' ).getEditedPostAttribute( 'alert_message' )
26+
const editedFeedAuthor = select( 'core/editor' ).getEditedPostAttribute( 'feed_author' )
2527

2628
return {
2729
errorMessage: editedErrorMessage,
30+
feedAuthor: editedFeedAuthor,
2831
feedUrl: editedPostMeta?.feed_url || '',
2932
postId: editedPostId,
3033
postType: select( 'core/editor' ).getEditedPostAttribute( 'type' ),
@@ -52,6 +55,15 @@ const BlockEditorFeedsInfobox = ( {} ) => {
5255
} }
5356
/>
5457

58+
<TextControl
59+
label={ __( 'Feed Author', 'pressforward' ) }
60+
value={ feedAuthor }
61+
onChange={ ( newValue ) => {
62+
editPost( { 'feed_author': newValue } )
63+
} }
64+
help={ __( 'Incoming items without an author name will use this value.', 'pressforward' ) }
65+
/>
66+
5567
{ errorMessage && (
5668
<p className="pf-error-message">
5769
<strong>

modules/rss-import/rss-import.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ public function get_data_object( $a_feed ) {
212212

213213
if ( ! $ag_status ) {
214214
$authors = $this->get_rss_authors( $item );
215+
216+
if ( __( 'No author.', 'pressforward' ) === $authors ) {
217+
// See if the parent feed has an author.
218+
$parent_feed_obj = pressforward( 'schema.feeds' )->get_instance_by_id( $a_feed->ID );
219+
$parent_feed_author = $parent_feed_obj->get_feed_author();
220+
if ( ! empty( $parent_feed_author ) ) {
221+
$authors = $parent_feed_author;
222+
}
223+
}
215224
} else {
216225
$parent_value = pressforward( 'controller.metas' )->get_post_pf_meta( $a_feed->ID, 'pf_feed_default_author', true );
217226
if ( ! empty( $parent_value ) ) {

0 commit comments

Comments
 (0)