Skip to content

Commit

Permalink
Merge pull request #1035 from Codeinwp/feat/category-mapping
Browse files Browse the repository at this point in the history
feat: Assign imported posts to categories based on the keywords
  • Loading branch information
vytisbulkevicius authored Dec 28, 2024
2 parents 804dd56 + c7d78fa commit 4a6071c
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 119 deletions.
27 changes: 27 additions & 0 deletions css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ fieldset[disabled] .form-control {
.fz-form-wrap .form-block .only-pro-content .only-pro-container .upgrade-alert{
width: 100%;
}
#fz-features .fz-form-wrap .form-block .only-pro-content .only-pro-container{
width: 100%;
}

.form-block-pro-text{
padding-top: 8px;
Expand Down Expand Up @@ -1216,6 +1219,30 @@ input.fz-switch-toggle[type=checkbox]:checked:before{
padding-left: 12px;
padding-right: 12px;
}
.fz-auto-cat {
width: 100%;
}
.fz-auto-cat .fz-select-control {
min-width: auto;
}
.fz-auto-cat tr {
display: flex;
gap: 12px;
padding: 6px 0;
}
.fz-auto-cat .fz-auto-cat-col-8{
width: 66.66%;
}
.fz-auto-cat .fz-auto-cat-col-4{
width: 33.34%;
display: flex;
gap: 6px;
}
.fz-auto-cat-actions {
display: flex;
padding-top: 12px;
justify-content: flex-end;
}

.support-box-list{
padding: 30px 0 24px;
Expand Down
14 changes: 14 additions & 0 deletions includes/admin/feedzy-rss-feeds-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,12 +995,26 @@ private function save_settings() {
$settings = apply_filters( 'feedzy_get_settings', array() );
switch ( $post_tab ) {
case 'general':
$auto_categories = isset( $_POST['auto-categories'] ) ? filter_input( INPUT_POST, 'auto-categories', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY ) : array();

$auto_categories = array_filter( $auto_categories, function( $item ) {
return ! empty( $item['keywords'] ) && is_numeric( $item['category'] );
});

$auto_categories = array_map(function( $item ) {
$item['keywords'] = sanitize_text_field( $item['keywords'] );
return $item;
}, $auto_categories );

$auto_categories = array_values( $auto_categories );

$settings['general']['disable-default-style'] = isset( $_POST['disable-default-style'] ) ? (int) filter_input( INPUT_POST, 'disable-default-style', FILTER_SANITIZE_NUMBER_INT ) : '';
$settings['general']['feedzy-delete-days'] = isset( $_POST['feedzy-delete-days'] ) ? (int) filter_input( INPUT_POST, 'feedzy-delete-days', FILTER_SANITIZE_NUMBER_INT ) : '';
$settings['general']['default-thumbnail-id'] = isset( $_POST['default-thumbnail-id'] ) ? (int) filter_input( INPUT_POST, 'default-thumbnail-id', FILTER_SANITIZE_NUMBER_INT ) : 0;
$settings['general']['fz_cron_execution'] = isset( $_POST['fz_cron_execution'] ) ? sanitize_text_field( wp_unslash( $_POST['fz_cron_execution'] ) ) : '';
$settings['general']['fz_cron_schedule'] = isset( $_POST['fz_cron_schedule'] ) ? filter_input( INPUT_POST, 'fz_cron_schedule', FILTER_UNSAFE_RAW ) : 'hourly';
$settings['general']['fz_execution_offset'] = isset( $_POST['fz_execution_offset'] ) ? filter_input( INPUT_POST, 'fz_execution_offset', FILTER_UNSAFE_RAW ) : '';
$settings['general']['auto-categories'] = $auto_categories;
$settings['general']['feedzy-telemetry'] = isset( $_POST['feedzy-telemetry'] ) ? (int) filter_input( INPUT_POST, 'feedzy-telemetry', FILTER_SANITIZE_NUMBER_INT ) : '';
break;
case 'headers':
Expand Down
13 changes: 10 additions & 3 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ public function feedzy_import_feed_options() {
$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"}]]';
}

if ( feedzy_is_pro() && empty( $import_post_term ) ) {
$import_post_term = '[#auto_categories]';
}

$import_link_author_admin = get_post_meta( $post->ID, 'import_link_author_admin', true );
$import_link_author_public = get_post_meta( $post->ID, 'import_link_author_public', true );

Expand Down Expand Up @@ -1940,6 +1944,7 @@ function ( $attr, $key ) {

if ( $import_post_term !== 'none' && strpos( $import_post_term, '_' ) > 0 ) {
$terms = explode( ',', $import_post_term );
$terms = apply_filters( 'feedzy_import_terms', $terms, $item );
$terms = array_filter(
$terms,
function( $term ) {
Expand All @@ -1949,6 +1954,9 @@ function( $term ) {
if ( false !== strpos( $term, '[#item_' ) ) {
return;
}
if ( false !== strpos( $term, '[#auto_categories]' ) ) {
return;
}
return $term;
}
);
Expand Down Expand Up @@ -3321,10 +3329,9 @@ private function wizard_import_feed() {

if ( ! is_wp_error( $job_id ) ) {
update_post_meta( $job_id, 'source', $wizard_data['feed'] );
update_post_meta( $job_id, 'import_post_title', '[#item_title]' );
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"}]]' );
update_post_meta( $job_id, 'import_post_date', '[#item_date]' );
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
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"}]]' );
update_post_meta( $job_id, 'import_post_type', $post_type );
update_post_meta( $job_id, 'import_post_status', 'publish' );
update_post_meta( $job_id, 'import_post_featured_img', '[#item_image]' );
Expand Down
52 changes: 52 additions & 0 deletions includes/layouts/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ class="<?php echo $_tab === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php e

$feedzy_delete_days = isset( $settings['general']['feedzy-delete-days'] ) ? $settings['general']['feedzy-delete-days'] : 0;
$default_thumbnail_id = isset( $settings['general']['default-thumbnail-id'] ) ? $settings['general']['default-thumbnail-id'] : 0;
$mapped_categories = isset( $settings['general']['auto-categories'] ) && ! empty( $settings['general']['auto-categories'] ) ? $settings['general']['auto-categories'] : array(
array(
'keywords' => '',
'category' => '',
),
);
$categories = get_categories(
array(
'hide_empty' => false,
)
);
$telemetry_enabled = get_option( 'feedzy_rss_feeds_logger_flag', 0 );

switch ( $active_tab ) {
Expand Down Expand Up @@ -155,6 +166,47 @@ class="<?php echo $_tab === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php e
<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>
</div>
</div>
<div class="form-block <?php echo esc_attr( apply_filters( 'feedzy_upsell_class', '' ) ); ?>">
<?php echo wp_kses_post( apply_filters( 'feedzy_upsell_content', '', 'auto-categories', 'settings' ) ); ?>
<div class="fz-form-group">
<label class="form-label"><?php esc_html_e( 'Auto Categories Mapping', 'feedzy-rss-feeds' ); ?></label>
<table class="fz-auto-cat">
<tbody>
<?php foreach ( $mapped_categories as $index => $category_mapping ) : ?>
<tr>
<td class="fz-auto-cat-col-8">
<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'] ); ?>"/>
</td>
<td class="fz-auto-cat-col-4">
<select name="auto-categories[<?php echo esc_attr( $index ); ?>][category]" class="form-control fz-select-control">
<option value=""><?php esc_html_e( 'Select a category', 'feedzy-rss-feeds' ); ?></option>
<?php
foreach ( $categories as $category ) {
$selected = $category->term_id == $category_mapping['category'] ? 'selected' : '';
echo '<option value="' . esc_attr( $category->term_id ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $category->name ) . '</option>';
}
?>
</select>
<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>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="fz-auto-cat-actions">
<button type="button"class="btn btn-outline-primary"><?php esc_html_e( 'Add New', 'feedzy-rss-feeds' ); ?></button>
</div>
<div class="help-text pt-8">
<?php
printf(
// translators: %s is a placeholder for the auto categories tag, like [#auto_categories].
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' ),
'<strong>[#auto_categories]</strong>'
);
?>
</div>
</div>
</div>
<?php if ( feedzy_is_pro() ) : ?>
<div class="form-block">
<div class="fz-form-group">
Expand Down
14 changes: 14 additions & 0 deletions includes/views/import-metabox-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ class="dashicons dashicons-arrow-down-alt2"></span>
);
?>
</div>
<div class="help-text pt-8">
<?php
// phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment
echo wp_kses_post(
sprintf(
// translators: %1$s: magic tag, %2$s: opening anchor tag, %3$s: closing anchor tag
__( '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' ),
'<strong>[#auto_categories]</strong>',
'<a href="' . esc_url( get_admin_url( null, 'admin.php?page=feedzy-settings' ) ) . '" target="_blank">',
'</a>'
)
);
?>
</div>
</div>
</div>

Expand Down
Loading

0 comments on commit 4a6071c

Please sign in to comment.