Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,11 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
$data_meta['import_use_external_image'] = isset( $data_meta['import_use_external_image'] ) ? $data_meta['import_use_external_image'] : 'no';
// Check feeds remove html checkbox checked OR not.
$data_meta['import_remove_html'] = isset( $data_meta['import_remove_html'] ) ? $data_meta['import_remove_html'] : 'no';
$data_meta['import_post_term'] = isset( $data_meta['import_post_term'] ) ? $data_meta['import_post_term'] : '';
// An unselected multiselect submits nothing, so a missing value means "cleared" only once the
// taxonomy options have loaded; otherwise the selector never loaded and the saved value is kept.
if ( ! isset( $data_meta['import_post_term'] ) && isset( $_POST['feedzy_post_terms_loaded'] ) ) {
$data_meta['import_post_term'] = '';
}

// If it is filter_conditions we want to escape it.
if ( isset( $data_meta['filter_conditions'] ) ) {
Expand Down Expand Up @@ -1423,6 +1427,7 @@ private function get_taxonomies() {
check_ajax_referer( FEEDZY_BASEFILE, 'security' );

$post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( $_POST['post_type'] ) : '';
$selected = isset( $_POST['selected'] ) ? (array) map_deep( wp_unslash( $_POST['selected'] ), 'sanitize_text_field' ) : array();
$taxonomies = get_object_taxonomies(
array(
'post_type' => $post_type,
Expand All @@ -1431,21 +1436,53 @@ private function get_taxonomies() {
$results = array();
if ( ! empty( $taxonomies ) ) {
foreach ( $taxonomies as $taxonomy ) {
$terms = get_terms(
$terms = get_terms(
array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'fields' => 'id=>name',
'number' => apply_filters( 'feedzy_post_taxonomy_limit', 999, $taxonomy ),
)
);
// Saved terms past the limit must stay selectable, or the next save drops them.
$missing = is_array( $terms ) ? array_diff( $this->get_saved_term_ids( $selected, $taxonomy ), array_keys( $terms ) ) : array();
if ( ! empty( $missing ) ) {
$saved_terms = get_terms(
array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'fields' => 'id=>name',
'include' => $missing,
)
);
$terms += is_array( $saved_terms ) ? $saved_terms : array();
}
$results[ $taxonomy ] = $terms;
}
}
echo wp_json_encode( $results );
wp_die();
}

/**
* Term IDs of a taxonomy among saved Post Taxonomy values.
*
* @param array<mixed> $selected Saved values, `<taxonomy>_<term id>` for terms.
* @param string $taxonomy Taxonomy name.
*
* @return list<int>
*/
private function get_saved_term_ids( array $selected, string $taxonomy ): array {
$ids = array();
$pattern = '/^' . preg_quote( $taxonomy, '/' ) . '_(\d+)$/';
foreach ( $selected as $value ) {
if ( is_string( $value ) && preg_match( $pattern, $value, $match ) ) {
$ids[] = (int) $match[1];
}
}
return $ids;
}

/**
* Run a specific job.
*
Expand Down
1 change: 1 addition & 0 deletions includes/views/import-metabox-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class="dashicons dashicons-arrow-down-alt2"></span>
<select id="feedzy_post_terms" data-placeholder="<?php esc_attr_e( 'Select Some Options', 'feedzy-rss-feeds' ); ?>" multiple class="form-control feedzy-chosen<?php echo feedzy_is_pro() ? ' fz-chosen-custom-tag' : ''; ?>"
name="feedzy_meta_data[import_post_term][]">
</select>
<input type="hidden" id="feedzy_post_terms_loaded" name="feedzy_post_terms_loaded" value="1" disabled>
</div>
<div class="help-text pt-8">
<?php esc_html_e( 'If you don\'t select at least one taxonomy, posts will be assigned to Uncategorized by default.', 'feedzy-rss-feeds' ); ?>
Expand Down
10 changes: 10 additions & 0 deletions includes/views/js/import-metabox-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@
return false;
}

let taxonomyRequest = 0;

function update_taxonomy() {
const request = ++taxonomyRequest;
const selected = $(this).val();
let tax_selected = $(this).data('tax');
const custom_tag = $(this).data('custom-tag');
Expand All @@ -365,16 +368,22 @@
action: 'feedzy',
_action: 'get_taxonomies',
post_type: selected,
selected: tax_selected,
};

$('#feedzy_post_terms')
.html($('#loading_select_tpl').html())
.trigger('chosen:updated');
$('#feedzy_post_terms_loaded').prop('disabled', true);

$.post(
ajaxurl,
data,
function (response) {
// Superseded by a later post type change.
if (request !== taxonomyRequest) {
return;
}
let show_terms = true;

let options = '';
Expand Down Expand Up @@ -475,6 +484,7 @@
}

$('#feedzy_post_terms').html(options).trigger('chosen:updated');
$('#feedzy_post_terms_loaded').prop('disabled', false);
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
},
'json'
);
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/mu-plugins/feedzy-e2e-taxonomy-limit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Plugin Name: Feedzy E2E Taxonomy Limit
* Description: Lists one term per taxonomy in Post Taxonomy on requests that post feedzy_e2e_taxonomy_limit, so e2e tests can cover saved terms past the limit.
*
* @package feedzy-rss-feeds
*/

add_filter(
'feedzy_post_taxonomy_limit',
function ( $limit ) {
return isset( $_POST['feedzy_e2e_taxonomy_limit'] ) ? 1 : $limit;
}
);
Loading
Loading