Skip to content

Commit

Permalink
fix: testing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Dec 23, 2024
1 parent 8bc5d44 commit 8bc3f8a
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 129 deletions.
2 changes: 1 addition & 1 deletion includes/layouts/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ 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'] ) ? $settings['general']['auto-categories'] : array(
$mapped_categories = isset( $settings['general']['auto-categories'] ) && ! empty( $settings['general']['auto-categories'] ) ? $settings['general']['auto-categories'] : array(
array(
'keywords' => '',
'category' => '',
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 @@ -386,6 +386,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
296 changes: 168 additions & 128 deletions js/feedzy-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,135 +3,175 @@
* Plugin URI: https://themeisle.com/plugins/feedzy-rss-feeds/
* Author: Themeisle
*
* @package feedzy-rss-feeds
* @package
*/
/* global feedzy_setting */
/* jshint unused:false */
jQuery( function( $ ) {

// Snackbar notice.
var snackbarNotice = function() {
$( '.fz-snackbar-notice' ).toggleClass( 'open', 1000 );
if ( $( '.fz-snackbar-notice' ).hasClass( 'open' ) ) {
setTimeout( function() {
snackbarNotice();
}, 3000 );
}
};

// on upload button click
$( 'body' ).on( 'click', '.feedzy-open-media', function( e ) {
e.preventDefault();
var button = $( this ),
wp_media_uploader = wp.media( {
title: feedzy_setting.l10n.media_iframe_title,
library : {
type : 'image'
},
button: {
text: feedzy_setting.l10n.media_iframe_button
},
multiple: false
} ).on( 'select', function() { // it also has "open" and "close" events
var attachment = wp_media_uploader.state().get( 'selection' ).first().toJSON();
var attachmentUrl = attachment.url;
if ( attachment.sizes.thumbnail ) {
attachmentUrl = attachment.sizes.thumbnail.url;
}
if ( $( '.feedzy-media-preview' ).length ) {
$( '.feedzy-media-preview' ).find( 'img' ).attr( 'src', attachmentUrl );
} else {
$( '<div class="fz-form-group feedzy-media-preview"><img src="' + attachmentUrl + '"></div>' ).insertBefore( button.parent() );
}
button.parent().find( '.feedzy-remove-media' ).addClass( 'is-show' );
button.parent().find( 'input:hidden' ).val( attachment.id ).trigger( 'change' );
$( '.feedzy-open-media' ).html( feedzy_setting.l10n.action_btn_text_2 );
} ).open();
});

// on remove button click
$( 'body' ).on( 'click', '.feedzy-remove-media', function( e ) {
e.preventDefault();
var button = $( this );
button.parent().prev( '.feedzy-media-preview' ).remove();
button.removeClass( 'is-show' );
button.parent().find( 'input:hidden' ).val( '' ).trigger( 'change' );
$( '.feedzy-open-media' ).html( feedzy_setting.l10n.action_btn_text_1 );
});

// Unsaved form exit confirmation.
var unsaved = false;
$( ':input' ).change(function () {
unsaved = true;
});
$( '#feedzy-settings-submit, #check_wordai_api, #check_spinnerchief_api, #check_aws_api, #check_openai_api' ).on( 'click', function() {
unsaved = false;
} );
window.addEventListener( 'beforeunload', function( e ) {
if ( unsaved ) {
e.preventDefault();
e.returnValue = '';
}
});

// Select cron execution time.
$( document ).on( 'change', '#fz-event-execution', function() {
$( '#fz-execution-offset' ).val( new Date().getTimezoneOffset() / 60 );
} );
snackbarNotice();

const initializeAutoCatActions = () => {
const elements = {
table: document.querySelector( '.fz-auto-cat' ),
tbody: document.querySelector( '.fz-auto-cat tbody' ),
addBtn: document.querySelector( '.fz-auto-cat-actions button' )
};

if ( ! Object.values( elements ).every( Boolean ) ) {
return;
}

const rows = elements.tbody.querySelectorAll( 'tr' );
let rowIndex = rows.length - 1;

const getNewRow = ( index ) => {
const row = rows[0].cloneNode( true );
const input = row.querySelector( 'input' );
const select = row.querySelector( 'select' );
const deleteBtn = row.querySelector( 'button' );

if ( input ) {
input.value = '';
input.name = `auto-categories[${index}][keywords]`;
}

if ( select ) {
select.name = `auto-categories[${index}][category]`;
}

if ( deleteBtn ) {
deleteBtn.classList.remove( 'disabled' );
deleteBtn.removeAttribute( 'disabled' );
}

return row;
};

elements.tbody.addEventListener( 'click', ( e ) => {
if ( e.target.matches( 'button:not(.disabled)' ) ) {
e.target.closest( 'tr' )?.remove();
}
} );

elements.addBtn.addEventListener( 'click', ( e ) => {
e.preventDefault();
if ( rows.length > 0 ) {
const newRow = getNewRow( ++rowIndex );
elements.tbody.appendChild( newRow );
}
} );
};


initializeAutoCatActions();
jQuery(function ($) {
// Snackbar notice.
const snackbarNotice = function () {
$('.fz-snackbar-notice').toggleClass('open', 1000);
if ($('.fz-snackbar-notice').hasClass('open')) {
setTimeout(function () {
snackbarNotice();
}, 3000);
}
};

// on upload button click
$('body').on('click', '.feedzy-open-media', function (e) {
e.preventDefault();
var button = $(this),
wp_media_uploader = wp
.media({
title: feedzy_setting.l10n.media_iframe_title,
library: {
type: 'image',
},
button: {
text: feedzy_setting.l10n.media_iframe_button,
},
multiple: false,
})
.on('select', function () {
// it also has "open" and "close" events
const attachment = wp_media_uploader
.state()
.get('selection')
.first()
.toJSON();
let attachmentUrl = attachment.url;
if (attachment.sizes.thumbnail) {
attachmentUrl = attachment.sizes.thumbnail.url;
}
if ($('.feedzy-media-preview').length) {
$('.feedzy-media-preview')
.find('img')
.attr('src', attachmentUrl);
} else {
$(
'<div class="fz-form-group feedzy-media-preview"><img src="' +
attachmentUrl +
'"></div>'
).insertBefore(button.parent());
}
button
.parent()
.find('.feedzy-remove-media')
.addClass('is-show');
button
.parent()
.find('input:hidden')
.val(attachment.id)
.trigger('change');
$('.feedzy-open-media').html(
feedzy_setting.l10n.action_btn_text_2
);
})
.open();
});

// on remove button click
$('body').on('click', '.feedzy-remove-media', function (e) {
e.preventDefault();
const button = $(this);
button.parent().prev('.feedzy-media-preview').remove();
button.removeClass('is-show');
button.parent().find('input:hidden').val('').trigger('change');
$('.feedzy-open-media').html(feedzy_setting.l10n.action_btn_text_1);
});

// Unsaved form exit confirmation.
let unsaved = false;
$(':input').change(function () {
unsaved = true;
});
$(
'#feedzy-settings-submit, #check_wordai_api, #check_spinnerchief_api, #check_aws_api, #check_openai_api'
).on('click', function () {
unsaved = false;
});
window.addEventListener('beforeunload', function (e) {
if (unsaved) {
e.preventDefault();
e.returnValue = '';
}
});

// Select cron execution time.
$(document).on('change', '#fz-event-execution', function () {
$('#fz-execution-offset').val(new Date().getTimezoneOffset() / 60);
});
snackbarNotice();

const initializeAutoCatActions = () => {
const elements = {
table: document.querySelector('.fz-auto-cat'),
tbody: document.querySelector('.fz-auto-cat tbody'),
addBtn: document.querySelector('.fz-auto-cat-actions button'),
};

if (!Object.values(elements).every(Boolean)) {
return;
}

const rows = elements.tbody.querySelectorAll('tr');
let rowIndex = rows.length - 1;

const getNewRow = (index) => {
const row = rows[0].cloneNode(true);
const input = row.querySelector('input');
const select = row.querySelector('select');
const deleteBtn = row.querySelector('button');

if (input) {
input.value = '';
input.name = `auto-categories[${index}][keywords]`;
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
}
});
}

if (select) {
select.name = `auto-categories[${index}][category]`;
}

if (deleteBtn) {
deleteBtn.classList.remove('disabled');
deleteBtn.removeAttribute('disabled');
}

return row;
};

elements.tbody.addEventListener('click', (e) => {
if (e.target.matches('button:not(.disabled)')) {
e.target.closest('tr')?.remove();
}
});

elements.addBtn.addEventListener('click', (e) => {
e.preventDefault();
if (rows.length > 0) {
const newRow = getNewRow(++rowIndex);
elements.tbody.appendChild(newRow);
}
});

// Add event listener to existing inputs
rows.forEach((row) => {
const input = row.querySelector('input');
if (input) {
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
}
});
}
});
};

initializeAutoCatActions();
});

0 comments on commit 8bc3f8a

Please sign in to comment.