Skip to content

Commit 375dc47

Browse files
committed
feat: add post author selection to Feed Imports
1 parent 72921d8 commit 375dc47

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

css/settings.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,15 @@ fieldset[disabled] .form-control {
614614
background: #D0D4D7;
615615
}
616616

617+
.fz-form-wrap .chosen-container .chosen-results li.helper-text {
618+
display: block;
619+
background: white;
620+
cursor: help;
621+
font-size: 12px;
622+
text-align: center;
623+
color: #757575;
624+
}
625+
617626
.date-range-group{
618627
display: flex;
619628
align-items: flex-end;

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public function enqueue_styles() {
161161
'media_iframe_button' => __( 'Set default image', 'feedzy-rss-feeds' ),
162162
'action_btn_text_1' => __( 'Choose image', 'feedzy-rss-feeds' ),
163163
'action_btn_text_2' => __( 'Replace image', 'feedzy-rss-feeds' ),
164+
'author_helper' => __( 'If the author you are looking for isn\'t found, you can input the author username.', 'feedzy-rss-feeds' ),
164165
),
165166
)
166167
);
@@ -326,6 +327,11 @@ public function feedzy_import_feed_options() {
326327
$post_types = get_post_types( '', 'names' );
327328
$post_types = array_diff( $post_types, array( 'feedzy_imports', 'feedzy_categories' ) );
328329
$published_status = array( 'publish', 'draft' );
330+
$authors = get_users( array( 'number' => 100 ) );
331+
$authors_array = array();
332+
foreach ( $authors as $author ) {
333+
$authors_array[] = $author->user_login;
334+
}
329335
$keyword_filter_fields = array( __( 'Title', 'feedzy-rss-feeds' ) );
330336
if ( feedzy_is_pro() ) {
331337
$keyword_filter_fields = array_merge(
@@ -364,6 +370,21 @@ public function feedzy_import_feed_options() {
364370
$import_auto_translation = get_post_meta( $post->ID, 'import_auto_translation', true );
365371
$import_auto_translation = 'yes' === $import_auto_translation ? 'checked' : '';
366372
$import_translation_lang = get_post_meta( $post->ID, 'import_auto_translation_lang', true );
373+
$import_post_author = get_post_meta( $post->ID, 'import_post_author', true );
374+
375+
/**
376+
* This code snippet retrieves the post author for backward compatibility for existing imports as well as for any new imports.
377+
* It checks if the $import_post_author variable is not empty, otherwise it defaults to the current post's author.
378+
*/
379+
$import_post_author = ! empty( $import_post_author ) ? $import_post_author : $post->post_author;
380+
$author = get_user_by( 'ID', $import_post_author );
381+
if ( $author ) {
382+
$import_post_author = $author->user_login;
383+
if ( ! in_array( $import_post_author, $authors_array, true ) ) {
384+
$authors_array[] = $import_post_author;
385+
}
386+
}
387+
367388
// default values so that post is not created empty.
368389
if ( empty( $import_title ) ) {
369390
$import_title = '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_title%22%2C%22data%22%3A%7B%7D%7D%5D"}]]';
@@ -508,6 +529,17 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
508529
$data_meta['import_auto_translation'] = isset( $data_meta['import_auto_translation'] ) ? $data_meta['import_auto_translation'] : 'no';
509530
// Check feeds external image URL checkbox checked OR not.
510531
$data_meta['import_use_external_image'] = isset( $data_meta['import_use_external_image'] ) ? $data_meta['import_use_external_image'] : 'no';
532+
533+
// $data_meta['feedzy_post_author'] should be the author username. We convert it to the author ID.
534+
if ( ! empty( $data_meta['import_post_author'] ) ) {
535+
$author = get_user_by( 'login', $data_meta['import_post_author'] );
536+
if ( $author ) {
537+
$data_meta['import_post_author'] = $author->ID;
538+
} else {
539+
$data_meta['import_post_author'] = '';
540+
}
541+
}
542+
511543
foreach ( $data_meta as $key => $value ) {
512544
$value = is_array( $value ) ? implode( ',', $value ) : implode( ',', (array) $value );
513545
if ( 'source' === $key ) {
@@ -1284,6 +1316,7 @@ private function run_job( $job, $max ) {
12841316
$import_auto_translation = get_post_meta( $job->ID, 'import_auto_translation', true );
12851317
$import_auto_translation = $this->feedzy_is_agency() && 'yes' === $import_auto_translation ? true : false;
12861318
$import_translation_lang = get_post_meta( $job->ID, 'import_auto_translation_lang', true );
1319+
$import_post_author = get_post_meta( $job->ID, 'import_post_author', true );
12871320
$max = $import_feed_limit;
12881321

12891322
if ( metadata_exists( 'post', $job->ID, 'import_post_status' ) ) {
@@ -1716,6 +1749,8 @@ function ( $attr, $key ) {
17161749
$item_post_excerpt = apply_filters( 'feedzy_parse_custom_tags', $item_post_excerpt, $item_obj );
17171750
}
17181751

1752+
$post_author = ! empty( $import_post_author ) ? $import_post_author : $job->post_author;
1753+
17191754
$new_post = apply_filters(
17201755
'feedzy_insert_post_args',
17211756
array(
@@ -1725,7 +1760,7 @@ function ( $attr, $key ) {
17251760
'post_date' => $post_date,
17261761
'post_status' => $import_post_status,
17271762
'post_excerpt' => $item_post_excerpt,
1728-
'post_author' => $job->post_author,
1763+
'post_author' => $post_author,
17291764
),
17301765
$item_obj,
17311766
$post_title,

includes/views/import-metabox-edit.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,26 @@ class="fz-switch-toggle" type="checkbox" value="yes"
602602
<h4 class="h4"><?php esc_html_e( 'Post Author', 'feedzy-rss-feeds' ); ?> <?php echo ! feedzy_is_pro() ? ' <span class="pro-label">PRO</span>' : ''; ?></h4>
603603
</div>
604604
<div class="fz-right">
605+
<div class="fz-form-group">
606+
<label class="form-label"><?php esc_html_e( 'The post author for the imported posts.', 'feedzy-rss-feeds' ); ?></label>
607+
<div class="mx-320">
608+
<select id="feedzy_post_author" class="form-control feedzy-chosen fz-chosen-custom-tag" name="feedzy_meta_data[import_post_author]">
609+
<?php
610+
foreach ( $authors_array as $_author ) {
611+
?>
612+
<option value="<?php echo esc_attr( $_author ); ?>" <?php selected( $import_post_author, $_author ); ?>>
613+
<?php echo esc_html( $_author ); ?></option>
614+
<?php
615+
}
616+
?>
617+
</select>
618+
</div>
619+
<div class="help-text pt-8 pb-8">
620+
<?php
621+
esc_html_e( 'Select the author to assign to the imported posts. By default, this will be set to your current account. Note that this choice is independent of the options below, which control how the source author details are displayed.', 'feedzy-rss-feeds' );
622+
?>
623+
</div>
624+
</div>
605625
<div class="fz-form-group">
606626
<div class="fz-form-switch">
607627
<input id="feedzy-toggle_author_admin" name="feedzy_meta_data[import_link_author_admin]"

includes/views/js/import-metabox-edit.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@
327327
}
328328
})
329329

330+
// Add magic tag support for post taxonomy field.
331+
$("#feedzy_post_author.fz-chosen-custom-tag").on("chosen:no_results", function() {
332+
var select = $(this);
333+
var search = select.siblings(".chosen-container").find(".chosen-search-input");
334+
var text = htmlEntities(search.val().replace(/\s+/g, ''));
335+
// dont add if it already exists
336+
if (! select.find('option[value=' + escapeSelector(search.val().replace(/\s+/g, '')) + ']').length) {
337+
var btn = $('<li class="active-result highlighted">' + text + '</li>');
338+
btn.on("mousedown mouseup click", function(e) {
339+
var arr = select.val() || [];
340+
select.append("<option value='" + text + "' selected>" + text + "</option>");
341+
select.trigger("chosen:updated").trigger('chosen:close');
342+
e.stopImmediatePropagation();
343+
return false;
344+
});
345+
search.on("keydown", function(e) {
346+
if (e.which == 13) {
347+
btn.click();
348+
return false;
349+
}
350+
});
351+
select.siblings(".chosen-container").find(".no-results").replaceWith(btn);
352+
select.siblings(".chosen-container").find(".chosen-results").append('<li class="helper-text">' + feedzy.i10n.author_helper + '</li>');
353+
} else {
354+
select.siblings(".chosen-container").find(".no-results").replaceWith('');
355+
}
356+
})
357+
330358
/*
331359
Form
332360
*/

0 commit comments

Comments
 (0)