Skip to content

Commit 78b6e42

Browse files
committed
feat: Assign imported posts to categories based on the keywords
1 parent 7d45ada commit 78b6e42

File tree

5 files changed

+157
-3
lines changed

5 files changed

+157
-3
lines changed

css/settings.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ fieldset[disabled] .form-control {
427427
.fz-form-wrap .form-block .only-pro-content .only-pro-container .upgrade-alert{
428428
width: 100%;
429429
}
430+
#fz-features .fz-form-wrap .form-block .only-pro-content .only-pro-container{
431+
width: 100%;
432+
}
430433

431434
.form-block-pro-text{
432435
padding-top: 8px;
@@ -1216,6 +1219,30 @@ input.fz-switch-toggle[type=checkbox]:checked:before{
12161219
padding-left: 12px;
12171220
padding-right: 12px;
12181221
}
1222+
.fz-auto-cat {
1223+
width: 100%;
1224+
}
1225+
.fz-auto-cat .fz-select-control {
1226+
min-width: auto;
1227+
}
1228+
.fz-auto-cat tr {
1229+
display: flex;
1230+
gap: 12px;
1231+
padding: 6px 0;
1232+
}
1233+
.fz-auto-cat .fz-auto-cat-col-8{
1234+
width: 66.66%;
1235+
}
1236+
.fz-auto-cat .fz-auto-cat-col-4{
1237+
width: 33.34%;
1238+
display: flex;
1239+
gap: 6px;
1240+
}
1241+
.fz-auto-cat-actions {
1242+
display: flex;
1243+
padding-top: 12px;
1244+
justify-content: flex-end;
1245+
}
12191246

12201247
.support-box-list{
12211248
padding: 30px 0 24px;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,12 +995,25 @@ private function save_settings() {
995995
$settings = apply_filters( 'feedzy_get_settings', array() );
996996
switch ( $post_tab ) {
997997
case 'general':
998+
$auto_categories = isset( $_POST['auto-categories'] ) ? filter_input( INPUT_POST, 'auto-categories', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY ) : array();
999+
1000+
$auto_categories = array_filter( $auto_categories, function( $item ) {
1001+
if ( ! empty( $item['keywords'] ) && is_numeric( $item['category'] ) ) {
1002+
$item['keywords'] = sanitize_text_field( $item['keywords'] );
1003+
return true;
1004+
}
1005+
return false;
1006+
} );
1007+
1008+
$auto_categories = array_values( $auto_categories );
1009+
9981010
$settings['general']['disable-default-style'] = isset( $_POST['disable-default-style'] ) ? (int) filter_input( INPUT_POST, 'disable-default-style', FILTER_SANITIZE_NUMBER_INT ) : '';
9991011
$settings['general']['feedzy-delete-days'] = isset( $_POST['feedzy-delete-days'] ) ? (int) filter_input( INPUT_POST, 'feedzy-delete-days', FILTER_SANITIZE_NUMBER_INT ) : '';
10001012
$settings['general']['default-thumbnail-id'] = isset( $_POST['default-thumbnail-id'] ) ? (int) filter_input( INPUT_POST, 'default-thumbnail-id', FILTER_SANITIZE_NUMBER_INT ) : 0;
10011013
$settings['general']['fz_cron_execution'] = isset( $_POST['fz_cron_execution'] ) ? sanitize_text_field( wp_unslash( $_POST['fz_cron_execution'] ) ) : '';
10021014
$settings['general']['fz_cron_schedule'] = isset( $_POST['fz_cron_schedule'] ) ? filter_input( INPUT_POST, 'fz_cron_schedule', FILTER_UNSAFE_RAW ) : 'hourly';
10031015
$settings['general']['fz_execution_offset'] = isset( $_POST['fz_execution_offset'] ) ? filter_input( INPUT_POST, 'fz_execution_offset', FILTER_UNSAFE_RAW ) : '';
1016+
$settings['general']['auto-categories'] = $auto_categories;
10041017
$settings['general']['feedzy-telemetry'] = isset( $_POST['feedzy-telemetry'] ) ? (int) filter_input( INPUT_POST, 'feedzy-telemetry', FILTER_SANITIZE_NUMBER_INT ) : '';
10051018
break;
10061019
case 'headers':

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ public function feedzy_import_feed_options() {
403403
$import_content = '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_content%22%2C%22data%22%3A%7B%7D%7D%5D"}]]';
404404
}
405405

406+
if ( feedzy_is_pro() && empty( $import_post_term ) ) {
407+
$import_post_term = '[#auto_categories]';
408+
}
409+
406410
$import_link_author_admin = get_post_meta( $post->ID, 'import_link_author_admin', true );
407411
$import_link_author_public = get_post_meta( $post->ID, 'import_link_author_public', true );
408412

@@ -1934,6 +1938,7 @@ function ( $attr, $key ) {
19341938

19351939
if ( $import_post_term !== 'none' && strpos( $import_post_term, '_' ) > 0 ) {
19361940
$terms = explode( ',', $import_post_term );
1941+
$terms = apply_filters( 'feedzy_import_terms', $terms, $item );
19371942
$terms = array_filter(
19381943
$terms,
19391944
function( $term ) {
@@ -1943,6 +1948,9 @@ function( $term ) {
19431948
if ( false !== strpos( $term, '[#item_' ) ) {
19441949
return;
19451950
}
1951+
if ( false !== strpos( $term, '[#auto_categories]' ) ) {
1952+
return;
1953+
}
19461954
return $term;
19471955
}
19481956
);
@@ -3291,10 +3299,9 @@ private function wizard_import_feed() {
32913299

32923300
if ( ! is_wp_error( $job_id ) ) {
32933301
update_post_meta( $job_id, 'source', $wizard_data['feed'] );
3294-
update_post_meta( $job_id, 'import_post_title', '[#item_title]' );
3302+
update_post_meta( $job_id, 'import_post_title', '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_title%22%2C%22data%22%3A%7B%7D%7D%5D"}]]' );
32953303
update_post_meta( $job_id, 'import_post_date', '[#item_date]' );
3296-
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
3297-
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
3304+
update_post_meta( $job_id, 'import_post_content', '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_content%22%2C%22data%22%3A%7B%7D%7D%5D"}]]' );
32983305
update_post_meta( $job_id, 'import_post_type', $post_type );
32993306
update_post_meta( $job_id, 'import_post_status', 'publish' );
33003307
update_post_meta( $job_id, 'import_post_featured_img', '[#item_image]' );

includes/layouts/settings.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ class="<?php echo $_tab === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php e
108108

109109
$feedzy_delete_days = isset( $settings['general']['feedzy-delete-days'] ) ? $settings['general']['feedzy-delete-days'] : 0;
110110
$default_thumbnail_id = isset( $settings['general']['default-thumbnail-id'] ) ? $settings['general']['default-thumbnail-id'] : 0;
111+
$mapped_categories = isset( $settings['general']['auto-categories'] ) ? $settings['general']['auto-categories'] : array(
112+
array(
113+
'keywords' => '',
114+
'category' => '',
115+
),
116+
);
117+
$categories = get_categories(
118+
array(
119+
'hide_empty' => false,
120+
)
121+
);
111122
$telemetry_enabled = get_option( 'feedzy_rss_feeds_logger_flag', 0 );
112123

113124
switch ( $active_tab ) {
@@ -155,6 +166,47 @@ class="<?php echo $_tab === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php e
155166
<div class="help-text pt-8"><?php esc_html_e( 'This setting will be used to inherit the current theme style instead of the default style. If disabled, it will be considered the individual widget/block/shortcode setting.', 'feedzy-rss-feeds' ); ?></div>
156167
</div>
157168
</div>
169+
<div class="form-block <?php echo esc_attr( apply_filters( 'feedzy_upsell_class', '' ) ); ?>">
170+
<?php echo wp_kses_post( apply_filters( 'feedzy_upsell_content', '', 'filter-keyword', 'import' ) ); ?>
171+
<div class="fz-form-group">
172+
<label class="form-label"><?php esc_html_e( 'Auto Categories Mapping', 'feedzy-rss-feeds' ); ?></label>
173+
<table class="fz-auto-cat">
174+
<tbody>
175+
<?php foreach ( $mapped_categories as $index => $category_mapping ) : ?>
176+
<tr>
177+
<td class="fz-auto-cat-col-8">
178+
<input type="text" name="auto-categories[<?php echo esc_attr( $index ); ?>][keywords]" class="form-control" placeholder="<?php esc_attr_e( 'Values separated by commas', 'feedzy-rss-feeds' ); ?>" value="<?php echo esc_attr( $category_mapping['keywords'] ); ?>"/>
179+
</td>
180+
<td class="fz-auto-cat-col-4">
181+
<select name="auto-categories[<?php echo esc_attr( $index ); ?>][category]" class="form-control fz-select-control">
182+
<option value=""><?php esc_html_e( 'Select a category', 'feedzy-rss-feeds' ); ?></option>
183+
<?php
184+
foreach ( $categories as $category ) {
185+
$selected = $category->term_id == $category_mapping['category'] ? 'selected' : '';
186+
echo '<option value="' . esc_attr( $category->term_id ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $category->name ) . '</option>';
187+
}
188+
?>
189+
</select>
190+
<button type="button" class="btn btn-outline-primary<?php echo $index === 0 ? ' disabled' : ''; ?>" <?php echo $index === 0 ? 'disabled' : ''; ?>><?php esc_html_e( 'Delete', 'feedzy-rss-feeds' ); ?></button>
191+
</td>
192+
</tr>
193+
<?php endforeach; ?>
194+
</tbody>
195+
</table>
196+
<div class="fz-auto-cat-actions">
197+
<button type="button"class="btn btn-outline-primary"><?php esc_html_e( 'Add New', 'feedzy-rss-feeds' ); ?></button>
198+
</div>
199+
<div class="help-text pt-8">
200+
<?php
201+
printf(
202+
// translators: %s is a placeholder for the auto categories tag.
203+
esc_html__( 'Automatically assign categories to your posts based on their titles. You need to add %s tag to the category field of your import to support this feature.', 'feedzy-rss-feeds' ),
204+
'<strong>[#auto_categories]</strong>'
205+
);
206+
?>
207+
</div>
208+
</div>
209+
</div>
158210
<?php if ( feedzy_is_pro() ) : ?>
159211
<div class="form-block">
160212
<div class="fz-form-group">

js/feedzy-setting.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,59 @@ jQuery( function( $ ) {
122122
);
123123
});
124124
snackbarNotice();
125+
126+
const initializeAutoCatActions = () => {
127+
const elements = {
128+
table: document.querySelector( '.fz-auto-cat' ),
129+
tbody: document.querySelector( '.fz-auto-cat tbody' ),
130+
addBtn: document.querySelector( '.fz-auto-cat-actions button' )
131+
};
132+
133+
if ( ! Object.values( elements ).every( Boolean ) ) {
134+
return;
135+
}
136+
137+
const rows = elements.tbody.querySelectorAll( 'tr' );
138+
let rowIndex = rows.length - 1;
139+
140+
const getNewRow = ( index ) => {
141+
const row = rows[0].cloneNode( true );
142+
const input = row.querySelector( 'input' );
143+
const select = row.querySelector( 'select' );
144+
const deleteBtn = row.querySelector( 'button' );
145+
146+
if ( input ) {
147+
input.value = '';
148+
input.name = `auto-categories[${index}][keywords]`;
149+
}
150+
151+
if ( select ) {
152+
select.name = `auto-categories[${index}][category]`;
153+
}
154+
155+
if ( deleteBtn ) {
156+
deleteBtn.classList.remove( 'disabled' );
157+
deleteBtn.removeAttribute( 'disabled' );
158+
}
159+
160+
return row;
161+
};
162+
163+
elements.tbody.addEventListener( 'click', ( e ) => {
164+
if ( e.target.matches( 'button:not(.disabled)' ) ) {
165+
e.target.closest( 'tr' )?.remove();
166+
}
167+
} );
168+
169+
elements.addBtn.addEventListener( 'click', ( e ) => {
170+
e.preventDefault();
171+
if ( rows.length > 0 ) {
172+
const newRow = getNewRow( ++rowIndex );
173+
elements.tbody.appendChild( newRow );
174+
}
175+
} );
176+
};
177+
178+
179+
initializeAutoCatActions();
125180
});

0 commit comments

Comments
 (0)