Skip to content

Commit

Permalink
use feed_import filters
Browse files Browse the repository at this point in the history
  • Loading branch information
dphiffer committed Nov 26, 2024
1 parent f8cf4e8 commit e0d6453
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 15 deletions.
12 changes: 9 additions & 3 deletions wp-content/plugins/feed-import/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}
Expand Down
35 changes: 27 additions & 8 deletions wp-content/plugins/feed-import/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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();
Expand All @@ -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']);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand Down
77 changes: 73 additions & 4 deletions wp-content/themes/mediasanctuary/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ function social_meta_tags() {


echo "\n";
echo '<title>'.$title.'</title>' . "\n";
echo '<meta name="description" content="'.$description.'"/>' . "\n";
echo '<meta property="og:title" content="'.$title.'">' . "\n";
echo '<title>'.$title.'</title>' . "\n";
echo '<meta name="description" content="'.$description.'"/>' . "\n";
echo '<meta property="og:title" content="'.$title.'">' . "\n";
echo '<meta property="og:description" content="'.$description.'">' . "\n";
echo '<meta property="og:image" content="'.$thumb_url.'">' . "\n";
echo '<meta property="og:url" content="'.$url.'">' . "\n";
Expand Down Expand Up @@ -397,4 +397,73 @@ function audio_player() {
$soundcloud_link
</div>
END;
}
}

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);

0 comments on commit e0d6453

Please sign in to comment.