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