Skip to content

Commit 4a6071c

Browse files
Merge pull request #1035 from Codeinwp/feat/category-mapping
feat: Assign imported posts to categories based on the keywords
2 parents 804dd56 + c7d78fa commit 4a6071c

File tree

6 files changed

+329
-119
lines changed

6 files changed

+329
-119
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,12 +995,26 @@ 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+
return ! empty( $item['keywords'] ) && is_numeric( $item['category'] );
1002+
});
1003+
1004+
$auto_categories = array_map(function( $item ) {
1005+
$item['keywords'] = sanitize_text_field( $item['keywords'] );
1006+
return $item;
1007+
}, $auto_categories );
1008+
1009+
$auto_categories = array_values( $auto_categories );
1010+
9981011
$settings['general']['disable-default-style'] = isset( $_POST['disable-default-style'] ) ? (int) filter_input( INPUT_POST, 'disable-default-style', FILTER_SANITIZE_NUMBER_INT ) : '';
9991012
$settings['general']['feedzy-delete-days'] = isset( $_POST['feedzy-delete-days'] ) ? (int) filter_input( INPUT_POST, 'feedzy-delete-days', FILTER_SANITIZE_NUMBER_INT ) : '';
10001013
$settings['general']['default-thumbnail-id'] = isset( $_POST['default-thumbnail-id'] ) ? (int) filter_input( INPUT_POST, 'default-thumbnail-id', FILTER_SANITIZE_NUMBER_INT ) : 0;
10011014
$settings['general']['fz_cron_execution'] = isset( $_POST['fz_cron_execution'] ) ? sanitize_text_field( wp_unslash( $_POST['fz_cron_execution'] ) ) : '';
10021015
$settings['general']['fz_cron_schedule'] = isset( $_POST['fz_cron_schedule'] ) ? filter_input( INPUT_POST, 'fz_cron_schedule', FILTER_UNSAFE_RAW ) : 'hourly';
10031016
$settings['general']['fz_execution_offset'] = isset( $_POST['fz_execution_offset'] ) ? filter_input( INPUT_POST, 'fz_execution_offset', FILTER_UNSAFE_RAW ) : '';
1017+
$settings['general']['auto-categories'] = $auto_categories;
10041018
$settings['general']['feedzy-telemetry'] = isset( $_POST['feedzy-telemetry'] ) ? (int) filter_input( INPUT_POST, 'feedzy-telemetry', FILTER_SANITIZE_NUMBER_INT ) : '';
10051019
break;
10061020
case 'headers':

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ public function feedzy_import_feed_options() {
409409
$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"}]]';
410410
}
411411

412+
if ( feedzy_is_pro() && empty( $import_post_term ) ) {
413+
$import_post_term = '[#auto_categories]';
414+
}
415+
412416
$import_link_author_admin = get_post_meta( $post->ID, 'import_link_author_admin', true );
413417
$import_link_author_public = get_post_meta( $post->ID, 'import_link_author_public', true );
414418

@@ -1940,6 +1944,7 @@ function ( $attr, $key ) {
19401944

19411945
if ( $import_post_term !== 'none' && strpos( $import_post_term, '_' ) > 0 ) {
19421946
$terms = explode( ',', $import_post_term );
1947+
$terms = apply_filters( 'feedzy_import_terms', $terms, $item );
19431948
$terms = array_filter(
19441949
$terms,
19451950
function( $term ) {
@@ -1949,6 +1954,9 @@ function( $term ) {
19491954
if ( false !== strpos( $term, '[#item_' ) ) {
19501955
return;
19511956
}
1957+
if ( false !== strpos( $term, '[#auto_categories]' ) ) {
1958+
return;
1959+
}
19521960
return $term;
19531961
}
19541962
);
@@ -3321,10 +3329,9 @@ private function wizard_import_feed() {
33213329

33223330
if ( ! is_wp_error( $job_id ) ) {
33233331
update_post_meta( $job_id, 'source', $wizard_data['feed'] );
3324-
update_post_meta( $job_id, 'import_post_title', '[#item_title]' );
3332+
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"}]]' );
33253333
update_post_meta( $job_id, 'import_post_date', '[#item_date]' );
3326-
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
3327-
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
3334+
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"}]]' );
33283335
update_post_meta( $job_id, 'import_post_type', $post_type );
33293336
update_post_meta( $job_id, 'import_post_status', 'publish' );
33303337
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'] ) && ! empty( $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', '', 'auto-categories', 'settings' ) ); ?>
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, like [#auto_categories].
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">

includes/views/import-metabox-edit.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,20 @@ class="dashicons dashicons-arrow-down-alt2"></span>
215215
);
216216
?>
217217
</div>
218+
<div class="help-text pt-8">
219+
<?php
220+
// phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment
221+
echo wp_kses_post(
222+
sprintf(
223+
// translators: %1$s: magic tag, %2$s: opening anchor tag, %3$s: closing anchor tag
224+
__( 'You can automatically assign categories with a magic tag %1$s by the keywords used in title. Configure it %2$s here%3$s .', 'feedzy-rss-feeds' ),
225+
'<strong>[#auto_categories]</strong>',
226+
'<a href="' . esc_url( get_admin_url( null, 'admin.php?page=feedzy-settings' ) ) . '" target="_blank">',
227+
'</a>'
228+
)
229+
);
230+
?>
231+
</div>
218232
</div>
219233
</div>
220234

0 commit comments

Comments
 (0)