Skip to content

Commit 1c4dc02

Browse files
authored
Merge pull request #1030 from Codeinwp/fix/issue-720
feat: add post author selection to Feed Imports
2 parents f9d822a + 375dc47 commit 1c4dc02

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"}]]';
@@ -537,6 +558,17 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
537558
$data_meta['import_auto_translation'] = isset( $data_meta['import_auto_translation'] ) ? $data_meta['import_auto_translation'] : 'no';
538559
// Check feeds external image URL checkbox checked OR not.
539560
$data_meta['import_use_external_image'] = isset( $data_meta['import_use_external_image'] ) ? $data_meta['import_use_external_image'] : 'no';
561+
562+
// $data_meta['feedzy_post_author'] should be the author username. We convert it to the author ID.
563+
if ( ! empty( $data_meta['import_post_author'] ) ) {
564+
$author = get_user_by( 'login', $data_meta['import_post_author'] );
565+
if ( $author ) {
566+
$data_meta['import_post_author'] = $author->ID;
567+
} else {
568+
$data_meta['import_post_author'] = '';
569+
}
570+
}
571+
540572
foreach ( $data_meta as $key => $value ) {
541573
$value = is_array( $value ) ? implode( ',', $value ) : implode( ',', (array) $value );
542574
if ( 'source' === $key ) {
@@ -1344,6 +1376,7 @@ private function run_job( $job, $max ) {
13441376
$import_auto_translation = get_post_meta( $job->ID, 'import_auto_translation', true );
13451377
$import_auto_translation = $this->feedzy_is_agency() && 'yes' === $import_auto_translation ? true : false;
13461378
$import_translation_lang = get_post_meta( $job->ID, 'import_auto_translation_lang', true );
1379+
$import_post_author = get_post_meta( $job->ID, 'import_post_author', true );
13471380
$max = $import_feed_limit;
13481381

13491382
if ( metadata_exists( 'post', $job->ID, 'import_post_status' ) ) {
@@ -1776,6 +1809,8 @@ function ( $attr, $key ) {
17761809
$item_post_excerpt = apply_filters( 'feedzy_parse_custom_tags', $item_post_excerpt, $item_obj );
17771810
}
17781811

1812+
$post_author = ! empty( $import_post_author ) ? $import_post_author : $job->post_author;
1813+
17791814
$new_post = apply_filters(
17801815
'feedzy_insert_post_args',
17811816
array(
@@ -1785,7 +1820,7 @@ function ( $attr, $key ) {
17851820
'post_date' => $post_date,
17861821
'post_status' => $import_post_status,
17871822
'post_excerpt' => $item_post_excerpt,
1788-
'post_author' => $job->post_author,
1823+
'post_author' => $post_author,
17891824
),
17901825
$item_obj,
17911826
$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)