Skip to content

Commit

Permalink
Merge pull request #3 from victorandeloci/dev
Browse files Browse the repository at this point in the history
multi feed support
  • Loading branch information
victorandeloci authored Sep 29, 2023
2 parents 28302fc + 36c6de3 commit 7e236c0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 36 deletions.
22 changes: 20 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ if (form) {
form.addEventListener('submit', function(e) {
e.preventDefault();

let feed_url = document.getElementsByName('one_wp_feed_rss_monitor_feed_url')[0].value;
let feed_url_list = [];
document.getElementsByName('one_wp_feed_rss_monitor_feed_url').forEach(input => {
feed_url_list.push(input.value);
});
let default_category_id = document.querySelector('input[name="one_wp_feed_rss_monitor_default_cat"]:checked').value;

// settings save fetch
let formData = new FormData();
formData.append('action', 'one_wp_feed_rss_monitor_save');
formData.append('feed_url', feed_url);
formData.append('feed_url_list', JSON.stringify(feed_url_list));
formData.append('default_category_id', default_category_id);
formData.append('ids_to_terms', JSON.stringify(idsToTerms));

Expand Down Expand Up @@ -70,4 +73,19 @@ if (jobBtn) {
document.getElementById('one_wp_feed_rss_monitor_job_response').innerHTML = text;
});
});
}

var addFeedBtn = document.getElementById('one_wp_feed_rss_monitor_add_btn');
if (addFeedBtn) {
addFeedBtn.addEventListener('click', function(e) {
e.preventDefault();

let input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('name', 'one_wp_feed_rss_monitor_feed_url');
input.classList.add('regular-text');

let container = document.getElementById('one_wp_feed_rss_monitor_feeds_container');
container.appendChild(input);
});
}
65 changes: 39 additions & 26 deletions one_wp_feed_rss_monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* Plugin Name: One WP Feed RSS Monitor
* Description: Monitor and auto-publish podcast episodes as wordpress posts
* Version: 1.0.2
* Version: 1.1.0
* Author: Victor Andeloci
* Author URI: https://github.com/victorandeloci
*/
Expand Down Expand Up @@ -33,12 +33,12 @@ function one_wp_feed_rss_monitor_page() {
'one_wp_feed_rss_monitor_main_js',
plugin_dir_url(__FILE__) . 'js/main.js',
[],
'1.0.2',
'1.1.0',
true
);

$options = [
'feed_url' => get_option('one_wp_feed_rss_monitor_feed_url', ''),
'feed_url_list' => get_option('one_wp_feed_rss_monitor_feed_url_list', ''),
'default_category_id' => get_option('one_wp_feed_rss_monitor_default_cat', ''),
'ids_to_terms' => get_option('one_wp_feed_rss_monitor_ids_to_terms', ''),
];
Expand All @@ -56,7 +56,7 @@ function one_wp_feed_rss_monitor_page() {

function one_wp_feed_rss_monitor_save() {
try {
update_option('one_wp_feed_rss_monitor_feed_url', sanitize_text_field($_POST['feed_url']));
update_option('one_wp_feed_rss_monitor_feed_url_list', stripslashes($_POST['feed_url_list']));
update_option('one_wp_feed_rss_monitor_default_cat', sanitize_text_field($_POST['default_category_id']));
update_option('one_wp_feed_rss_monitor_ids_to_terms', stripslashes($_POST['ids_to_terms']));

Expand Down Expand Up @@ -93,17 +93,25 @@ function one_wp_feed_rss_monitor_get_podcast_episodes($feed_url) {
foreach ($rss->channel->item as $item) {
$episode = [];
$episode['default_title'] = (string) $item->title;
$episode['title'] = (string) str_replace('', '"',
str_replace('', '"',
str_replace('', "'",
str_replace('', '...',
str_replace('', "'",
str_replace('', '-',
trim($item->title)))))));

// title tricky chars removal
$unwanted = [
'' => '"',
'' => '"',
'' => "'",
'' => '...',
'' => "'",
'' => '-'
];
$episode['title'] = strtr( $item->title, $unwanted );

$episode['description'] = (string) $item->description;
$episode['link'] = (string) $item->link;
$episode['mp3_url'] = (string) one_wp_feed_rss_monitor_xml_attribute($item->enclosure, 'url');
$episode['duration'] = (string) $item->children('itunes', true)->duration;
$episode['season'] = (string) $item->children('itunes', true)->season;
$episode['number'] = (string) $item->children('itunes', true)->episode;
$episode['type'] = (string) $item->children('itunes', true)->episodeType;
$episode['image_url'] = (string) $item->children('itunes', true)->image->attributes()->href;
$episode['pub_date'] = (string) $item->pubDate;
$episode['tags'] = [];
Expand All @@ -125,7 +133,7 @@ function one_wp_feed_rss_monitor_get_podcast_episodes($feed_url) {
$tags_str = substr($description, $tags_start, $tags_end - $tags_start);
$tags = explode(' ', $tags_str);
foreach ($tags as $tag) {
$episode['tags'][] = str_replace('ç', 'c', str_replace('ã', 'a', str_replace('#', '', $tag)));
$episode['tags'][] = iconv('UTF-8', 'ASCII//TRANSLIT', str_replace('#', '', $tag));
}
}

Expand All @@ -143,12 +151,15 @@ function one_wp_feed_rss_monitor_create_podcast_post($episode) {
'post_content' => $episode['description'],
'post_status' => 'publish',
'post_type' => 'post',
'post_date' => date('Y-m-d', strtotime($episode['pub_date'])),
'post_date' => date('Y-m-d H:i:s', strtotime($episode['pub_date'])),
'post_author' => (get_current_user_id() ?? 1),
'meta_input' => array(
'episode_link' => $episode['link'],
'episode_mp3_url' => $episode['mp3_url'],
'episode_duration' => $episode['duration'],
'episode_season' => $episode['season'],
'episode_number' => $episode['number'],
'episode_type' => $episode['type'],
'episode_cover' => $episode['image_url']
)
);
Expand Down Expand Up @@ -201,20 +212,22 @@ function one_wp_feed_rss_monitor_create_podcast_post($episode) {
}

function one_wp_feed_rss_monitor_update_posts_episodes() {
$feed_url = get_option('one_wp_feed_rss_monitor_feed_url', '');
if (!empty($feed_url)) {
// get feed RSS eps
$episodes = one_wp_feed_rss_monitor_get_podcast_episodes($feed_url);
if (!empty($episodes)) {
// create posts foreach ep
$podcastPostCount = 0;
foreach ($episodes as $episode) {
if (one_wp_feed_rss_monitor_create_podcast_post($episode))
$podcastPostCount++;
$feed_url_list = get_option('one_wp_feed_rss_monitor_feed_url_list', '');
if (!empty($feed_url_list)) {
foreach (json_decode($feed_url_list) as $i => $feed_url) {
// get feed RSS eps
$episodes = one_wp_feed_rss_monitor_get_podcast_episodes($feed_url);
if (!empty($episodes)) {
// create posts foreach ep
$podcastPostCount = 0;
foreach ($episodes as $episode) {
if (one_wp_feed_rss_monitor_create_podcast_post($episode))
$podcastPostCount++;
}
echo $podcastPostCount . ' post(s) created!';
} else {
echo 'Could not find new episodes on feed ' . ($i + 1) . '...<br>';
}
echo $podcastPostCount . ' post(s) created!';
} else {
echo 'Could not find new episodes...';
}
} else {
echo 'Feed RSS URL not defined!';
Expand Down
25 changes: 17 additions & 8 deletions templates/settings_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
<form method="post" action="" id="one_wp_feed_rss_monitor_form">
<table class="form-table">
<tr>
<th scope="row"><label for="one_wp_feed_rss_monitor_feed_url">Feed RSS URL:</label></th>
<th scope="row"><label for="one_wp_feed_rss_monitor_feed_url">Feed RSS URLs:</label></th>
<td id="one_wp_feed_rss_monitor_feeds_container">
<?php if (!empty(esc_attr($options['feed_url_list']))) : ?>
<?php
$feedUrlList = json_decode($options['feed_url_list']);
foreach ($feedUrlList as $feedUrl) :
?>
<input
type="text"
name="one_wp_feed_rss_monitor_feed_url"
value="<?= $feedUrl ?>"
class="regular-text"
/>
<?php endforeach; ?>
<?php endif; ?>
</td>
<td>
<input
type="text"
id="one_wp_feed_rss_monitor_feed_url"
name="one_wp_feed_rss_monitor_feed_url"
value="<?= esc_attr($options['feed_url']) ?>"
class="regular-text"
/>
<button class="button-secondary" id="one_wp_feed_rss_monitor_add_btn">Add feed</button>
</td>
</tr>
<tr>
Expand Down

0 comments on commit 7e236c0

Please sign in to comment.