Skip to content

Commit 2d7fff4

Browse files
fixes & finish update job
1 parent fcccf70 commit 2d7fff4

File tree

4 files changed

+179
-5
lines changed

4 files changed

+179
-5
lines changed

js/main.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if (idsToTermsInput != null && idsToTermsInput.value != null && idsToTermsInput.
55
for (const [id, term] of Object.entries(idsToTerms)) {
66
let inputElement = document.querySelector('input[data-id="' + id + '"]');
77
if (inputElement)
8-
inputElement.value = term;
8+
inputElement.value = term;
99
}
1010
}
1111

@@ -29,7 +29,7 @@ if (form) {
2929
let feed_url = document.getElementsByName('one_wp_feed_rss_monitor_feed_url')[0].value;
3030
let default_category_id = document.querySelector('input[name="one_wp_feed_rss_monitor_default_cat"]:checked').value;
3131

32-
// fetch
32+
// settings save fetch
3333
let formData = new FormData();
3434
formData.append('action', 'one_wp_feed_rss_monitor_save');
3535
formData.append('feed_url', feed_url);
@@ -45,7 +45,29 @@ if (form) {
4545
})
4646
.then((text) => {
4747
if (text != null && text != '')
48-
document.getElementById('one_wp_feed_rss_monitor_response_label').innerHTML = text;
48+
alert(text);
49+
});
50+
});
51+
}
52+
53+
var jobBtn = document.getElementById('one_wp_feed_rss_monitor_job_btn');
54+
if (jobBtn) {
55+
jobBtn.addEventListener('click', function() {
56+
document.getElementById('one_wp_feed_rss_monitor_job_response').innerHTML = 'Loading...';
57+
// job run fetch
58+
let formData = new FormData();
59+
formData.append('action', 'one_wp_feed_rss_monitor_update_posts_episodes');
60+
61+
fetch(ajaxurl, {
62+
method: 'POST',
63+
body: formData
64+
})
65+
.then(function(response) {
66+
return response.text();
67+
})
68+
.then((text) => {
69+
if (text != null && text != '')
70+
document.getElementById('one_wp_feed_rss_monitor_job_response').innerHTML = text;
4971
});
5072
});
5173
}

one_wp_feed_rss_monitor.php

+147
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
* Author URI: https://github.com/victorandeloci
99
*/
1010

11+
function one_wp_feed_rss_monitor_xml_attribute($object, $attribute) {
12+
if (isset($object[$attribute]))
13+
return (string) $object[$attribute];
14+
}
15+
1116
if ( !function_exists('one_wp_feed_rss_monitor_page') ) {
1217
function one_wp_feed_rss_monitor_page() {
1318
// user permissions
@@ -40,6 +45,8 @@ function one_wp_feed_rss_monitor_page() {
4045

4146
$categories = get_categories();
4247

48+
// cron job exec
49+
include_once('templates/cron_exec.php');
4350
// form render
4451
include_once('templates/settings_form.php');
4552
}
@@ -75,3 +82,143 @@ function one_wp_feed_rss_monitor_menu() {
7582
}
7683
}
7784
add_action( 'admin_menu', 'one_wp_feed_rss_monitor_menu' );
85+
86+
// get feed rss eps
87+
function one_wp_feed_rss_monitor_get_podcast_episodes($feed_url) {
88+
$rss = simplexml_load_file($feed_url);
89+
$episodes = [];
90+
91+
foreach ($rss->channel->item as $item) {
92+
$episode = [];
93+
$episode['default_title'] = (string) $item->title;
94+
$episode['title'] = (string) str_replace('', '"',
95+
str_replace('', '"',
96+
str_replace('', "'",
97+
str_replace('', '...',
98+
str_replace('', "'",
99+
str_replace('', '-',
100+
trim($item->title)))))));
101+
$episode['description'] = (string) $item->description;
102+
$episode['link'] = (string) $item->link;
103+
$episode['mp3_url'] = (string) one_wp_feed_rss_monitor_xml_attribute($item->enclosure, 'url');
104+
$episode['duration'] = (string) $item->children('itunes', true)->duration;
105+
$episode['image_url'] = (string) $item->children('itunes', true)->image->attributes()->href;
106+
$episode['pub_date'] = (string) $item->pubDate;
107+
$episode['tags'] = [];
108+
109+
// verify if episode post exists
110+
$existing_post = get_page_by_title($episode['default_title'], OBJECT, 'post');
111+
if (!$existing_post)
112+
$existing_post = get_page_by_title($episode['title'], OBJECT, 'post');
113+
114+
if (!$existing_post) {
115+
// get episode description tags (#tag1, #tag2)
116+
$description = $episode['description'];
117+
$tags_start = strpos($description, '#');
118+
if ($tags_start !== false) {
119+
$tags_end = strpos($description, "\n", $tags_start);
120+
if ($tags_end === false) {
121+
$tags_end = strlen($description);
122+
}
123+
$tags_str = substr($description, $tags_start, $tags_end - $tags_start);
124+
$tags = explode(' ', $tags_str);
125+
foreach ($tags as $tag) {
126+
$episode['tags'][] = str_replace('#', '', $tag);
127+
}
128+
}
129+
130+
$episodes[] = $episode;
131+
}
132+
}
133+
134+
return $episodes;
135+
}
136+
137+
function one_wp_feed_rss_monitor_create_podcast_post($episode) {
138+
try {
139+
$post_data = array(
140+
'post_title' => $episode['title'],
141+
'post_content' => $episode['description'],
142+
'post_status' => 'publish',
143+
'post_type' => 'post',
144+
'post_date' => date('Y-m-d', strtotime($episode['pub_date'])),
145+
'post_author' => 1,
146+
'meta_input' => array(
147+
'episode_link' => $episode['link'],
148+
'episode_mp3_url' => $episode['mp3_url'],
149+
'episode_duration' => $episode['duration'],
150+
'episode_cover' => $episode['image_url']
151+
)
152+
);
153+
154+
$post_id = wp_insert_post($post_data, true);
155+
156+
if ($post_id && !is_wp_error($post_id)) {
157+
// feat. image using "itunes:image"
158+
$image_url = $episode['image_url'];
159+
if ($image_url) {
160+
$image_id = media_sideload_image($image_url, $post_id, null, 'id');
161+
if (!is_wp_error($image_id)) {
162+
set_post_thumbnail($post_id, $image_id);
163+
}
164+
}
165+
166+
// tags
167+
$tags = $episode['tags'];
168+
if (!empty($tags)) {
169+
wp_set_post_tags($post_id, $tags);
170+
}
171+
172+
// episode / post category based on title search by term (defined in settings)
173+
$idsToTermsData = get_option('one_wp_feed_rss_monitor_ids_to_terms', '');
174+
if ($idsToTermsData != null && $idsToTermsData != '') {
175+
$idsToTerms = json_decode($idsToTermsData);
176+
// default category defined in settings
177+
$defaultCategory = get_option('one_wp_feed_rss_monitor_default_cat', '');
178+
$postCategories = [];
179+
if ($defaultCategory != null && $defaultCategory != '')
180+
$postCategories[] = $defaultCategory;
181+
182+
foreach ($idsToTerms as $id => $term) {
183+
if (strpos($episode['title'], $term) !== false) {
184+
$postCategories[] = $id;
185+
}
186+
wp_set_post_categories($post_id, $postCategories);
187+
}
188+
} else {
189+
echo 'Terms not defined!<br>';
190+
}
191+
192+
return true;
193+
} else {
194+
echo 'Could not create post <strong>' . $episode['title'] . '</strong> - ' . $post_id->get_error_message() . '<br>';
195+
}
196+
} catch (\Throwable $th) {
197+
echo 'Fatal error during post creation: ' . $th . '<br>';
198+
}
199+
}
200+
201+
function one_wp_feed_rss_monitor_update_posts_episodes() {
202+
$feed_url = get_option('one_wp_feed_rss_monitor_feed_url', '');
203+
if (!empty($feed_url)) {
204+
// get feed RSS eps
205+
$episodes = one_wp_feed_rss_monitor_get_podcast_episodes($feed_url);
206+
if (!empty($episodes)) {
207+
// create posts foreach ep
208+
$podcastPostCount = 0;
209+
foreach ($episodes as $episode) {
210+
if (one_wp_feed_rss_monitor_create_podcast_post($episode))
211+
$podcastPostCount++;
212+
}
213+
echo $podcastPostCount . ' post(s) created!';
214+
} else {
215+
echo 'Could not find new episodes...';
216+
}
217+
} else {
218+
echo 'Feed RSS URL not defined!';
219+
}
220+
221+
die();
222+
}
223+
add_action('wp_ajax_one_wp_feed_rss_monitor_update_posts_episodes', 'one_wp_feed_rss_monitor_update_posts_episodes');
224+
add_action('wp_ajax_nopriv_one_wp_feed_rss_monitor_update_posts_episodes', 'one_wp_feed_rss_monitor_update_posts_episodes');

templates/cron_exec.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="wrap">
2+
<h2>Feed RSS Monitor</h2>
3+
<p>Use the button bellow to run the update task based on saved settings</p>
4+
<p id="one_wp_feed_rss_monitor_job_response"></p>
5+
<button class="button-primary" id="one_wp_feed_rss_monitor_job_btn">Update posts/episodes</button>
6+
</div>

templates/settings_form.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="wrap">
2-
<h1>Feed RSS Monitor</h1>
2+
<h2>Settings</h2>
33
<form method="post" action="" id="one_wp_feed_rss_monitor_form">
44
<table class="form-table">
55
<tr>
@@ -62,7 +62,6 @@ class="term"
6262
id="one_wp_feed_rss_monitor_ids_to_terms"
6363
value='<?= $options['ids_to_terms'] ?>'
6464
/>
65-
<p id="one_wp_feed_rss_monitor_response_label"></p>
6665
<p class="submit">
6766
<input type="submit" name="one_wp_feed_rss_monitor_submit" class="button-primary" value="Save Settings" />
6867
</p>

0 commit comments

Comments
 (0)