|
8 | 8 | * Author URI: https://github.com/victorandeloci
|
9 | 9 | */
|
10 | 10 |
|
| 11 | +function one_wp_feed_rss_monitor_xml_attribute($object, $attribute) { |
| 12 | + if (isset($object[$attribute])) |
| 13 | + return (string) $object[$attribute]; |
| 14 | +} |
| 15 | + |
11 | 16 | if ( !function_exists('one_wp_feed_rss_monitor_page') ) {
|
12 | 17 | function one_wp_feed_rss_monitor_page() {
|
13 | 18 | // user permissions
|
@@ -40,6 +45,8 @@ function one_wp_feed_rss_monitor_page() {
|
40 | 45 |
|
41 | 46 | $categories = get_categories();
|
42 | 47 |
|
| 48 | + // cron job exec |
| 49 | + include_once('templates/cron_exec.php'); |
43 | 50 | // form render
|
44 | 51 | include_once('templates/settings_form.php');
|
45 | 52 | }
|
@@ -75,3 +82,143 @@ function one_wp_feed_rss_monitor_menu() {
|
75 | 82 | }
|
76 | 83 | }
|
77 | 84 | 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'); |
0 commit comments