From d5e67662ed4cd18507d3d7f4684f3c31b1fb2719 Mon Sep 17 00:00:00 2001 From: Dan Phiffer Date: Wed, 20 Nov 2024 08:41:44 -0500 Subject: [PATCH 1/7] add skeleton of feed import plugin --- compose.yaml | 1 + wp-content/plugins/feed-import/feed-import.php | 17 +++++++++++++++++ wp-content/plugins/feed-import/plugin.php | 11 +++++++++++ 3 files changed, 29 insertions(+) create mode 100644 wp-content/plugins/feed-import/feed-import.php create mode 100644 wp-content/plugins/feed-import/plugin.php diff --git a/compose.yaml b/compose.yaml index cc697618..77c2f908 100644 --- a/compose.yaml +++ b/compose.yaml @@ -12,6 +12,7 @@ services: WORDPRESS_CONFIG_EXTRA: | define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false ); + define( 'FEED_IMPORT', ['hello world'] ); WORDPRESS_DEBUG: "true" volumes: - ./wp-content/plugins:/var/www/html/wp-content/plugins diff --git a/wp-content/plugins/feed-import/feed-import.php b/wp-content/plugins/feed-import/feed-import.php new file mode 100644 index 00000000..88e5109d --- /dev/null +++ b/wp-content/plugins/feed-import/feed-import.php @@ -0,0 +1,17 @@ + Date: Fri, 22 Nov 2024 20:35:20 -0500 Subject: [PATCH 2/7] remove the rss parser dependency --- compose.yaml | 2 +- .../plugins/feed-import/feed-import.php | 2 + wp-content/plugins/feed-import/feed.php | 84 +++++++++++++++++++ wp-content/plugins/feed-import/plugin.php | 16 +++- wp-content/plugins/feed-import/post.php | 23 +++++ 5 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 wp-content/plugins/feed-import/feed.php create mode 100644 wp-content/plugins/feed-import/post.php diff --git a/compose.yaml b/compose.yaml index 77c2f908..6138e996 100644 --- a/compose.yaml +++ b/compose.yaml @@ -12,7 +12,7 @@ services: WORDPRESS_CONFIG_EXTRA: | define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false ); - define( 'FEED_IMPORT', ['hello world'] ); + define( 'FEED_IMPORT', ['https://feeds.soundcloud.com/users/soundcloud:users:164222112/sounds.rss'] ); WORDPRESS_DEBUG: "true" volumes: - ./wp-content/plugins:/var/www/html/wp-content/plugins diff --git a/wp-content/plugins/feed-import/feed-import.php b/wp-content/plugins/feed-import/feed-import.php index 88e5109d..ae8561fb 100644 --- a/wp-content/plugins/feed-import/feed-import.php +++ b/wp-content/plugins/feed-import/feed-import.php @@ -8,6 +8,8 @@ */ require_once __DIR__ . '/plugin.php'; +require_once __DIR__ . '/feed.php'; +require_once __DIR__ . '/post.php'; add_action('plugins_loaded', function() { if (!defined('FEED_IMPORT') || !is_array(FEED_IMPORT)) { diff --git a/wp-content/plugins/feed-import/feed.php b/wp-content/plugins/feed-import/feed.php new file mode 100644 index 00000000..9e89f6ec --- /dev/null +++ b/wp-content/plugins/feed-import/feed.php @@ -0,0 +1,84 @@ +url = $url; + } + + function import() { + if ($this->load()) { + $this->parse(); + } + // print_r($this->items); + } + + function load() { + $cache_key = 'feed-import-cache-' . md5($this->url); + $cached = get_option($cache_key); + if ($this->valid_cache($cached)) { + $this->xml = $cached['xml']; + } else { + $response = wp_remote_get($this->url); + if (is_array($response) && !is_wp_error($response)) { + $this->xml = $response['body']; + } else { + return false; + } + update_option($cache_key, [ + 'created' => current_time('U', true), + 'xml' => $this->xml + ], false); + } + return true; + } + + function valid_cache($cache) { + $ttl = 60 * 60; // one hour + $now = current_time('U', true); + if (empty($cache)) { + return false; + } + if (empty($cache['created']) || empty($cache['xml'])) { + return false; + } + echo $now - $cache['created'] . "\n"; + return $now - $cache['created'] < $ttl; + } + + function parse() { + $this->doc = new \DOMDocument; + $this->doc->loadXML($this->xml, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA); + $items = $this->doc->getElementsByTagName('item'); + foreach ($items as $item) { + $this->items[] = [ + 'guid' => $this->get_child($item, 'guid')->nodeValue, + 'title' => $this->get_child($item, 'title')->nodeValue, + 'pubDate' => $this->get_child($item, 'pubDate')->nodeValue, + 'link' => $this->get_child($item, 'link')->nodeValue, + 'description' => $this->get_child($item, 'description')->nodeValue, + 'image' => $this->get_child($item, 'itunes:image')->getAttribute('href'), + 'audio' => $this->get_child($item, 'enclosure')->getAttribute('url'), + 'duration' => $this->get_child($item, 'itunes:duration')->nodeValue, + ]; + } + } + + function get_child($node, $tag) { + $tag = explode(':', $tag); + if (count($tag) == 1) { + return $node->getElementsByTagName($tag[0])->item(0); + } else { + $ns = $this->doc->lookupNamespaceURI($tag[0]); + return $node->getElementsByTagNameNS($ns, $tag[1])->item(0); + } + } + +} \ No newline at end of file diff --git a/wp-content/plugins/feed-import/plugin.php b/wp-content/plugins/feed-import/plugin.php index 85f7f45a..83617fb8 100644 --- a/wp-content/plugins/feed-import/plugin.php +++ b/wp-content/plugins/feed-import/plugin.php @@ -4,8 +4,20 @@ class Plugin { - function __construct() { - + public $feeds; + + function __construct($feeds = []) { + $this->feeds = $feeds; + if (class_exists('WP_CLI')) { + \WP_CLI::add_command('feed-import', [$this, 'import']); + } + } + + function import() { + foreach ($this->feeds as $url) { + $feed = new Feed($url); + $feed->import(); + } } } \ No newline at end of file diff --git a/wp-content/plugins/feed-import/post.php b/wp-content/plugins/feed-import/post.php new file mode 100644 index 00000000..0bdf5f52 --- /dev/null +++ b/wp-content/plugins/feed-import/post.php @@ -0,0 +1,23 @@ +item = $item; + $this->feed = $feed; + } + + function import() { + // $ns = $this->feed->getNamespaces(true); + // print_r($this->feed); + print_r($this->item); + $ns = 'http://www.itunes.com/dtds/podcast-1.0.dtd'; + print_r($this->item->children($ns)); + } + +} From aefe9e5fb4491675ae78de7f4f6bc8d57af01e94 Mon Sep 17 00:00:00 2001 From: Dan Phiffer Date: Sun, 24 Nov 2024 21:07:06 -0500 Subject: [PATCH 3/7] import a single post --- wp-content/plugins/feed-import/feed.php | 15 +- wp-content/plugins/feed-import/plugin.php | 6 +- wp-content/plugins/feed-import/post.php | 192 ++++++++++++++++++++-- 3 files changed, 197 insertions(+), 16 deletions(-) diff --git a/wp-content/plugins/feed-import/feed.php b/wp-content/plugins/feed-import/feed.php index 9e89f6ec..7701a808 100644 --- a/wp-content/plugins/feed-import/feed.php +++ b/wp-content/plugins/feed-import/feed.php @@ -15,9 +15,9 @@ function __construct($url) { function import() { if ($this->load()) { - $this->parse(); + return $this->parse(); } - // print_r($this->items); + return []; } function load() { @@ -41,7 +41,7 @@ function load() { } function valid_cache($cache) { - $ttl = 60 * 60; // one hour + $ttl = 60 * 10; // ten minutes $now = current_time('U', true); if (empty($cache)) { return false; @@ -49,7 +49,6 @@ function valid_cache($cache) { if (empty($cache['created']) || empty($cache['xml'])) { return false; } - echo $now - $cache['created'] . "\n"; return $now - $cache['created'] < $ttl; } @@ -57,6 +56,12 @@ function parse() { $this->doc = new \DOMDocument; $this->doc->loadXML($this->xml, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA); $items = $this->doc->getElementsByTagName('item'); + // foreach ($items as $item) { + // echo "\n\n----\n"; + // $content = $this->get_child($item, 'description')->nodeValue; + + // echo $content; + // } foreach ($items as $item) { $this->items[] = [ 'guid' => $this->get_child($item, 'guid')->nodeValue, @@ -68,7 +73,9 @@ function parse() { 'audio' => $this->get_child($item, 'enclosure')->getAttribute('url'), 'duration' => $this->get_child($item, 'itunes:duration')->nodeValue, ]; + break; } + return $this->items; } function get_child($node, $tag) { diff --git a/wp-content/plugins/feed-import/plugin.php b/wp-content/plugins/feed-import/plugin.php index 83617fb8..73dccab9 100644 --- a/wp-content/plugins/feed-import/plugin.php +++ b/wp-content/plugins/feed-import/plugin.php @@ -16,7 +16,11 @@ function __construct($feeds = []) { function import() { foreach ($this->feeds as $url) { $feed = new Feed($url); - $feed->import(); + $feed_data = $feed->import(); + foreach ($feed_data as $post_data) { + $post = new Post($post_data); + $post->save(); + } } } diff --git a/wp-content/plugins/feed-import/post.php b/wp-content/plugins/feed-import/post.php index 0bdf5f52..211a1e6a 100644 --- a/wp-content/plugins/feed-import/post.php +++ b/wp-content/plugins/feed-import/post.php @@ -4,20 +4,190 @@ class Post { - public $item; - public $feed; + public $id; + public $data; - function __construct($item, $feed) { - $this->item = $item; - $this->feed = $feed; + function __construct($data) { + $this->data = $data; } - function import() { - // $ns = $this->feed->getNamespaces(true); - // print_r($this->feed); - print_r($this->item); - $ns = 'http://www.itunes.com/dtds/podcast-1.0.dtd'; - print_r($this->item->children($ns)); + function save() { + $existing = $this->get_existing(); + if ($existing) { + $this->id = $existing->ID; + wp_update_post([ + 'ID' => $this->id, + 'post_title' => $this->title(), + 'post_content' => $this->content(), + 'post_category' => $this->category(), + ]); + } else { + $this->id = wp_insert_post([ + 'post_status' => $this->status(), + 'post_title' => $this->title(), + 'post_content' => $this->content(), + 'post_date' => $this->date(), + 'post_date_gmt' => $this->date_gmt(), + 'post_category' => $this->category(), + ]); + } + set_post_format($this->id, 'audio'); + update_post_meta($this->id, 'feed_import_guid', $this->data['guid']); + update_post_meta($this->id, 'feed_import_link', $this->data['link']); + update_post_meta($this->id, 'feed_import_audio', $this->data['audio']); + update_post_meta($this->id, 'feed_import_duration', $this->data['duration']); + $this->attach_image(); + } + + function get_existing() { + $existing_query = apply_filters('feed_import_existing_query', [ + 'post_type' => 'post', + 'post_status' => 'any', + 'meta_query' => [ + [ + 'key' => 'feed_import_guid', + 'value' => $this->data['guid'] + ] + ] + ], $this->data); + $posts = get_posts($existing_query); + if (! empty($posts)) { + return $posts[0]; + } else { + return null; + } + } + + function status() { + return 'publish'; + } + + function title() { + return $this->data['title']; + } + + function content() { + $content = $this->data['description']; + $content = $this->autolink_urls($content); + $content = $this->format_paragraphs($content); + return $content; + } + + function autolink_urls($content) { + // Look for URL-shaped text and add hyperlinks. + // The regex is slightly modified from https://www.urlregex.com/ + $regex = '%(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?%iu'; + return preg_replace_callback($regex, function($matches) { + + $url = $matches[0]; + $last_char = substr($url, -1, 1); + $punctuation = ['.', ',', '!', ';']; + $postfix = ''; + + if ($last_char == ')') { + if (strpos($url, '(') === false) { + // do not link ) of "(https://www.mediasanctuary.org/)" + // but do link the ) of "https://en.wikipedia.org/wiki/Douglas_Davis_(artist)" + $url = substr($url, 0, -1); + $postfix = ')'; + } + } else if (in_array($last_char, $punctuation)) { + // do not link . of "https://www.mediasanctuary.org/." + $url = substr($url, 0, -1); + $postfix = $last_char; + } + + $label = $url; + + // Remove the "https://www" part at the front of the label + $label = preg_replace('%^https?://%i', '', $label); + + // Remove the trailing slash part of the label + $label = preg_replace('%^([^/]+)/$%', '$1', $label); + + return "$label$postfix"; + + }, $content); + } + + function format_paragraphs($content) { + // Replace double-newlines (of various kinds) with paragraph elements, + // each

...

wrapped in a WordPress core/paragraph block. + return str_replace( + ["\r\n\r\n", "\n\r\n\r", "\n\n", "\r\r"], + "

\n\n\n\n

", + "\n

" . $content . "

\n" + ); + } + + function date() { + $date = new \DateTime($this->data['pubDate'], wp_timezone()); + return $date->format('Y-m-d H:i:s'); + } + + function date_gmt() { + $date = new \DateTime($this->data['pubDate']); + return $date->format('Y-m-d H:i:s'); + } + + function category() { + return apply_filters('feed_import_post_category', ''); + } + + function attach_image() { + if (empty($this->data['image'])) { + return; + } + + $image_url = $this->data['image']; + $filename = basename($image_url); + + $image_id = get_post_meta($this->id, '_thumbnail_id', true); + if (! empty($image_id)) { + $image = get_post($image_id); + if (! empty($image) && $image->post_title == $filename) { + return; + } + } + + $rsp = wp_remote_get($image_url, [ + 'timeout' => '90', + 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:44.0) Gecko/20100101 Firefox/44.0' + ]); + $status = wp_remote_retrieve_response_code($rsp); + if ($status != 200) { + error_log("Could not load image $image_url"); + return; + } + + $image_data = wp_remote_retrieve_body($rsp); + $content_type = $rsp['headers']['content-type']; + + $upload_dir = wp_upload_dir(); + $dir = $upload_dir['path']; + if (! file_exists($dir)) { + wp_mkdir_p($dir); + } + $path = "$dir/$filename"; + file_put_contents($path, $image_data); + + $filetype = wp_check_filetype($filename, null); + $attachment = [ + 'guid' => "{$upload_dir['url']}/$filename", + 'post_mime_type' => $filetype['type'], + 'post_title' => $filename, + 'post_content' => '', + 'post_status' => 'inherit' + ]; + $attach_id = wp_insert_attachment($attachment, $path); + + if (preg_match('/^image/', $content_type)) { + require_once(ABSPATH . 'wp-admin/includes/image.php'); + $attach_data = wp_generate_attachment_metadata($attach_id, $path); + wp_update_attachment_metadata($attach_id, $attach_data); + } + + update_post_meta($this->id, '_thumbnail_id', $attach_id); } } From 006a1b1fbde2154e1ad8da1a2d305899c467cecf Mon Sep 17 00:00:00 2001 From: Dan Phiffer Date: Mon, 25 Nov 2024 07:17:32 -0500 Subject: [PATCH 4/7] check if a post has updates before updating --- wp-content/plugins/feed-import/feed.php | 7 ------ wp-content/plugins/feed-import/plugin.php | 5 ++++- wp-content/plugins/feed-import/post.php | 27 ++++++++++++++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/wp-content/plugins/feed-import/feed.php b/wp-content/plugins/feed-import/feed.php index 7701a808..93dec535 100644 --- a/wp-content/plugins/feed-import/feed.php +++ b/wp-content/plugins/feed-import/feed.php @@ -56,12 +56,6 @@ function parse() { $this->doc = new \DOMDocument; $this->doc->loadXML($this->xml, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA); $items = $this->doc->getElementsByTagName('item'); - // foreach ($items as $item) { - // echo "\n\n----\n"; - // $content = $this->get_child($item, 'description')->nodeValue; - - // echo $content; - // } foreach ($items as $item) { $this->items[] = [ 'guid' => $this->get_child($item, 'guid')->nodeValue, @@ -73,7 +67,6 @@ function parse() { 'audio' => $this->get_child($item, 'enclosure')->getAttribute('url'), 'duration' => $this->get_child($item, 'itunes:duration')->nodeValue, ]; - break; } return $this->items; } diff --git a/wp-content/plugins/feed-import/plugin.php b/wp-content/plugins/feed-import/plugin.php index 73dccab9..ff5c6c8a 100644 --- a/wp-content/plugins/feed-import/plugin.php +++ b/wp-content/plugins/feed-import/plugin.php @@ -19,7 +19,10 @@ function import() { $feed_data = $feed->import(); foreach ($feed_data as $post_data) { $post = new Post($post_data); - $post->save(); + if ($post->has_updates()) { + echo $post->title() . "\n"; + $post->save(); + } } } } diff --git a/wp-content/plugins/feed-import/post.php b/wp-content/plugins/feed-import/post.php index 211a1e6a..4326f64f 100644 --- a/wp-content/plugins/feed-import/post.php +++ b/wp-content/plugins/feed-import/post.php @@ -31,12 +31,37 @@ function save() { 'post_category' => $this->category(), ]); } + $this->update_metadata(); + $this->attach_image(); + } + + function has_updates() { + $existing = $this->get_existing(); + if (empty($existing)) { + return true; + } + $db_hash = get_post_meta($existing->ID, 'feed_import_hash', true); + return ($this->get_content_hash() != $db_hash); + } + + function update_metadata() { set_post_format($this->id, 'audio'); update_post_meta($this->id, 'feed_import_guid', $this->data['guid']); update_post_meta($this->id, 'feed_import_link', $this->data['link']); update_post_meta($this->id, 'feed_import_audio', $this->data['audio']); update_post_meta($this->id, 'feed_import_duration', $this->data['duration']); - $this->attach_image(); + update_post_meta($this->id, 'feed_import_hash', $this->get_content_hash()); + } + + function get_content_hash() { + $plaintext = $this->data['guid']; + $plaintext .= '|' . $this->title(); + $plaintext .= '|' . $this->content(); + $plaintext .= '|' . $this->data['link']; + $plaintext .= '|' . $this->data['audio']; + $plaintext .= '|' . $this->data['image']; + $plaintext .= '|' . $this->data['duration']; + return md5($plaintext); } function get_existing() { From 4a941ae085606256706061e53b6e7ed7d38d0fca Mon Sep 17 00:00:00 2001 From: Dan Phiffer Date: Mon, 25 Nov 2024 08:09:05 -0500 Subject: [PATCH 5/7] add audio_player function to theme --- .../themes/mediasanctuary/functions.php | 63 +++++++++++++++++++ wp-content/themes/mediasanctuary/single.php | 4 +- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/wp-content/themes/mediasanctuary/functions.php b/wp-content/themes/mediasanctuary/functions.php index 32c13a02..a765b586 100644 --- a/wp-content/themes/mediasanctuary/functions.php +++ b/wp-content/themes/mediasanctuary/functions.php @@ -335,3 +335,66 @@ function is_story_post($post) { } return $query; }); + +function audio_player() { + global $post; + + if (empty($post)) { + return; + } + + $sources = []; + $feed_import_link = ''; + $soundcloud_link = ''; + $internet_archive_link = ''; + + $feed_import_audio = get_post_meta($post->ID, 'feed_import_audio', true); + if (! empty($feed_import_audio)) { + $sources[] = $feed_import_audio; + } + + $feed_import_link = get_post_meta($post->ID, 'feed_import_link', true); + if (! empty($feed_import_link)) { + $feed_import_link = "Listen on SoundCloud"; + } + + $soundcloud_id = get_post_meta($post->ID, 'soundcloud_podcast_id', true); + if (! empty($soundcloud_id)) { + $sources[] = "/wp-json/soundcloud-podcast/v1/stream/$soundcloud_id"; + } + $soundcloud_url = get_post_meta($post->ID, 'soundcloud_podcast_url', true); + if (! empty($soundcloud_url)) { + $soundcloud_link = "Listen on SoundCloud"; + } + + $internet_archive_id = get_post_meta($post->ID, 'internet_archive_id', true); + if (! empty($internet_archive_id) && $internet_archive_id != -1) { + $internet_archive_id = str_replace('https://archive.org/download/', '', $internet_archive_id); + $internet_archive_link = "Listen on Internet Archive"; + if (strpos($internet_archive_id, '/') == false) { + $internet_archive_id = "$internet_archive_id/$internet_archive_id.mp3"; + } + $internet_archive_id = preg_replace('/\.wav$/', '.mp3', $internet_archive_id); + $sources[] = "https://archive.org/download/$internet_archive_id"; + } + + if (empty($sources)) { + return; + } + + $source_elements = ''; + foreach ($sources as $src) { + $source_elements .= "\n"; + } + + echo << + + $feed_import_link + $internet_archive_link + $soundcloud_link + +END; +} \ No newline at end of file diff --git a/wp-content/themes/mediasanctuary/single.php b/wp-content/themes/mediasanctuary/single.php index d89d2182..b495b8a3 100644 --- a/wp-content/themes/mediasanctuary/single.php +++ b/wp-content/themes/mediasanctuary/single.php @@ -89,8 +89,8 @@
ID) == 'audio') { - soundcloud_podcast(); + if (get_post_format($post->ID) == 'audio') { + audio_player(); if (! empty($thumb)) { echo $thumb; } From f8cf4e8cce1ce0ddf62c91369997451b42bbb778 Mon Sep 17 00:00:00 2001 From: Dan Phiffer Date: Mon, 25 Nov 2024 08:18:40 -0500 Subject: [PATCH 6/7] add more post filters --- wp-content/plugins/feed-import/post.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/wp-content/plugins/feed-import/post.php b/wp-content/plugins/feed-import/post.php index 4326f64f..faac03f1 100644 --- a/wp-content/plugins/feed-import/post.php +++ b/wp-content/plugins/feed-import/post.php @@ -84,17 +84,18 @@ function get_existing() { } function status() { - return 'publish'; + return apply_filters('feed_import_post_status', 'publish'); } function title() { - return $this->data['title']; + return apply_filters('feed_import_post_title', $this->data['title']); } function content() { $content = $this->data['description']; $content = $this->autolink_urls($content); $content = $this->format_paragraphs($content); + $content = apply_filters('feed_import_post_content', $content); return $content; } @@ -147,11 +148,13 @@ function format_paragraphs($content) { function date() { $date = new \DateTime($this->data['pubDate'], wp_timezone()); + $date = apply_filters('feed_import_post_date', $date); return $date->format('Y-m-d H:i:s'); } function date_gmt() { $date = new \DateTime($this->data['pubDate']); + $date = apply_filters('feed_import_post_date_gmt', $date); return $date->format('Y-m-d H:i:s'); } From e0d64536964fd59882e47e5fc3c7fdabfac424eb Mon Sep 17 00:00:00 2001 From: Dan Phiffer Date: Mon, 25 Nov 2024 22:23:14 -0500 Subject: [PATCH 7/7] use feed_import filters --- wp-content/plugins/feed-import/plugin.php | 12 ++- wp-content/plugins/feed-import/post.php | 35 +++++++-- .../themes/mediasanctuary/functions.php | 77 ++++++++++++++++++- 3 files changed, 109 insertions(+), 15 deletions(-) diff --git a/wp-content/plugins/feed-import/plugin.php b/wp-content/plugins/feed-import/plugin.php index ff5c6c8a..c195e886 100644 --- a/wp-content/plugins/feed-import/plugin.php +++ b/wp-content/plugins/feed-import/plugin.php @@ -13,15 +13,21 @@ function __construct($feeds = []) { } } - function import() { + function import($args) { foreach ($this->feeds as $url) { $feed = new Feed($url); $feed_data = $feed->import(); foreach ($feed_data as $post_data) { $post = new Post($post_data); - if ($post->has_updates()) { - echo $post->title() . "\n"; + if (!empty($args)) { + if ($post->has_id($args[0])) { + $post->save(); + echo $post->title() . "\n"; + break; + } + } else if ($post->has_updates()) { $post->save(); + echo $post->title() . "\n"; } } } diff --git a/wp-content/plugins/feed-import/post.php b/wp-content/plugins/feed-import/post.php index faac03f1..2e468842 100644 --- a/wp-content/plugins/feed-import/post.php +++ b/wp-content/plugins/feed-import/post.php @@ -19,7 +19,7 @@ function save() { 'ID' => $this->id, 'post_title' => $this->title(), 'post_content' => $this->content(), - 'post_category' => $this->category(), + 'post_category' => $this->post_category(), ]); } else { $this->id = wp_insert_post([ @@ -28,7 +28,7 @@ function save() { 'post_content' => $this->content(), 'post_date' => $this->date(), 'post_date_gmt' => $this->date_gmt(), - 'post_category' => $this->category(), + 'post_category' => $this->post_category(), ]); } $this->update_metadata(); @@ -44,6 +44,14 @@ function has_updates() { return ($this->get_content_hash() != $db_hash); } + function has_id($id) { + $existing = $this->get_existing(); + if (empty($existing)) { + return false; + } + return $existing->ID == $id; + } + function update_metadata() { set_post_format($this->id, 'audio'); update_post_meta($this->id, 'feed_import_guid', $this->data['guid']); @@ -84,18 +92,18 @@ function get_existing() { } function status() { - return apply_filters('feed_import_post_status', 'publish'); + return apply_filters('feed_import_post_status', 'publish', $this); } function title() { - return apply_filters('feed_import_post_title', $this->data['title']); + return apply_filters('feed_import_post_title', $this->data['title'], $this); } function content() { $content = $this->data['description']; $content = $this->autolink_urls($content); $content = $this->format_paragraphs($content); - $content = apply_filters('feed_import_post_content', $content); + $content = apply_filters('feed_import_post_content', $content, $this); return $content; } @@ -148,18 +156,29 @@ function format_paragraphs($content) { function date() { $date = new \DateTime($this->data['pubDate'], wp_timezone()); - $date = apply_filters('feed_import_post_date', $date); + $date = apply_filters('feed_import_post_date', $date, $this); return $date->format('Y-m-d H:i:s'); } function date_gmt() { $date = new \DateTime($this->data['pubDate']); - $date = apply_filters('feed_import_post_date_gmt', $date); + $date = apply_filters('feed_import_post_date_gmt', $date, $this); return $date->format('Y-m-d H:i:s'); } function category() { - return apply_filters('feed_import_post_category', ''); + return apply_filters('feed_import_post_category', '', $this); + } + + function post_category() { + // The wp_insert_post and wp_update_post functions expect an array of + // term IDs, so we convert a more useful string to that format at the + // very last minute. + if (empty($this->category())) { + return []; + } + $term = get_term_by('name', $this->category(), 'category'); + return [$term->term_id]; } function attach_image() { diff --git a/wp-content/themes/mediasanctuary/functions.php b/wp-content/themes/mediasanctuary/functions.php index a765b586..80111849 100644 --- a/wp-content/themes/mediasanctuary/functions.php +++ b/wp-content/themes/mediasanctuary/functions.php @@ -273,9 +273,9 @@ function social_meta_tags() { echo "\n"; - echo ''.$title.'' . "\n"; - echo '' . "\n"; - echo '' . "\n"; + echo ''.$title.'' . "\n"; + echo '' . "\n"; + echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; @@ -397,4 +397,73 @@ function audio_player() { $soundcloud_link
END; -} \ No newline at end of file +} + +add_filter('feed_import_existing_query', function($query, $data) { + // e.g., tag:soundcloud,2010:tracks/1964923891 + $guid_parts = explode('/', $data['guid']); + $soundcloud_id = $guid_parts[1]; + return [ + 'post_type' => 'post', + 'post_status' => 'any', + 'meta_query' => [ + 'relation' => 'OR', + [ + 'key' => 'feed_import_guid', + 'value' => $data['guid'], + ], [ + 'key' => 'soundcloud_podcast_id', + 'value' => $soundcloud_id, + ], [ + 'key' => 'soundcloud_podcast_url', + 'value' => $data['link'], + ], + ] + ]; +}, 10, 2); + +add_filter('feed_import_post_category', function($category, $post) { + if (preg_match('/^HMM/i', $post->data['title'])) { + return 'Hudson Mohawk Magazine Episodes'; + } + return 'Stories'; +}, 10, 2); + +function feed_import_post_date($date, $post) { + $category = $post->category(); + $four_days = 60 * 60 * 24 * 4; + $timezone = $date->getTimezone(); + + if ($category == 'Stories' && + current_time('u') - $date->getTimestamp() < $four_days) { + // If the track's timestamp is within 4 days, we should schedule + // it for the next weekday at 6pm. + $date = null; + } + + if (empty($date)) { + $schedule_at = 'Today 6pm'; + + // If it's after Friday at 7pm, schedule for Monday at 6pm. + if (current_time('w') == 5 && current_time('H') > 19 || + current_time('w') == 6 || + current_time('w') == 0) { + $schedule_at = 'Monday 6pm'; + } + + $date = new \DateTime($schedule_at, $timezone); + } + + return $date; +} +add_filter('feed_import_post_date', 'feed_import_post_date', 10, 2); +add_filter('feed_import_post_date_gmt', 'feed_import_post_date', 10, 2); + +add_filter('feed_import_post_status', function($status, $post) { + $date = $post->date(); + if ($date > current_time('Y-m-d H:i:s')) { + return 'future'; + } else { + return 'publish'; + } +}, 10, 2); \ No newline at end of file