Skip to content

Commit d145405

Browse files
committed
Fix missing thumbnail in Discord story embed
1 parent 623d7ff commit d145405

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

DEVELOPMENT.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,14 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
304304
| `publish_to_draft` | `fictioneer_chapter_to_draft` (10)
305305
| `rest_api_init` | `fictioneer_register_endpoint_get_story_comments` (10)
306306
| `restrict_manage_posts` | `fictioneer_add_chapter_story_filter_dropdown` (10)
307-
| `save_post` | `fictioneer_refresh_chapters_schema` (20), `fictioneer_refresh_chapter_schema` (20), `fictioneer_refresh_collections_schema` (20), `fictioneer_refresh_post_caches` (20), `fictioneer_refresh_post_schema` (20), `fictioneer_refresh_recommendations_schema` (20), `fictioneer_refresh_recommendation_schema` (20), `fictioneer_refresh_stories_schema` (20), `fictioneer_refresh_story_schema` (20), `fictioneer_save_seo_metabox` (10), `fictioneer_save_word_count` (10), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_update_shortcode_relationships` (10), `fictioneer_purge_transients_after_update` (10), `fictioneer_save_story_metaboxes` (10), `fictioneer_save_chapter_metaboxes` (10), `fictioneer_save_extra_metabox` (10), `fictioneer_save_support_links_metabox` (10), `fictioneer_save_collection_metaboxes` (10), `fictioneer_save_recommendation_metaboxes` (10), `fictioneer_save_post_metaboxes` (10), `fictioneer_delete_cached_story_card_after_update` (10), `fictioneer_rebuild_story_data_collection` (999), `fictioneer_post_chapter_to_discord` (99)
307+
| `save_post` | `fictioneer_refresh_chapters_schema` (20), `fictioneer_refresh_chapter_schema` (20), `fictioneer_refresh_collections_schema` (20), `fictioneer_refresh_post_caches` (20), `fictioneer_refresh_post_schema` (20), `fictioneer_refresh_recommendations_schema` (20), `fictioneer_refresh_recommendation_schema` (20), `fictioneer_refresh_stories_schema` (20), `fictioneer_refresh_story_schema` (20), `fictioneer_save_seo_metabox` (10), `fictioneer_save_word_count` (10), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_update_shortcode_relationships` (10), `fictioneer_purge_transients_after_update` (10), `fictioneer_save_story_metaboxes` (10), `fictioneer_save_chapter_metaboxes` (10), `fictioneer_save_extra_metabox` (10), `fictioneer_save_support_links_metabox` (10), `fictioneer_save_collection_metaboxes` (10), `fictioneer_save_recommendation_metaboxes` (10), `fictioneer_save_post_metaboxes` (10), `fictioneer_delete_cached_story_card_after_update` (10), `fictioneer_rebuild_story_data_collection` (999), `fictioneer_post_chapter_to_discord` (99), `fictioneer_post_story_to_discord` (99)
308308
| `send_headers` | `fictioneer_block_pages_from_indexing` (10)
309309
| `set_logged_in_cookie` | `fictioneer_set_logged_in_cookie` (10)
310310
| `show_user_profile` | `fictioneer_custom_profile_fields` (20)
311311
| `shutdown` | `fictioneer_save_story_card_cache` (10)
312312
| `switch_theme` | `fictioneer_theme_deactivation` (10)
313313
| `template_redirect` | `fictioneer_generate_epub` (10), `fictioneer_oauth2_process` (10), `fictioneer_logout` (10), `fictioneer_disable_attachment_pages` (10), `fictioneer_gate_unpublished_content` (10), `fictioneer_serve_sitemap` (10), `fictioneer_redirect_story` (10), `fictioneer_redirect_scheduled_chapter_404` (10)
314-
| `transition_post_status` | `fictioneer_log_story_chapter_status_changes` (10), `fictioneer_chapter_future_to_publish` (10), `fictioneer_post_story_to_discord` (99)
314+
| `transition_post_status` | `fictioneer_log_story_chapter_status_changes` (10), `fictioneer_chapter_future_to_publish` (10)
315315
| `trashed_post` | `fictioneer_refresh_post_caches` (20), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_purge_transients_after_update` (10), `fictioneer_remove_chapter_from_story` (10)
316316
| `untrash_post` | `fictioneer_refresh_post_caches` (20), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_purge_transients_after_update` (10)
317317
| `update_option_*` | `fictioneer_update_option_disable_extended_chapter_list_meta_queries` (10), `fictioneer_update_option_disable_extended_story_list_meta_queries` (10)

includes/functions/_development.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ function fictioneer_generate_test_content() {
101101

102102
// Disable Discord
103103
remove_action( 'comment_post', 'fictioneer_post_comment_to_discord', 99 );
104-
remove_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99 );
105-
remove_action( 'transition_post_status', 'fictioneer_post_chapter_to_discord', 99 );
104+
remove_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
105+
remove_action( 'save_post', 'fictioneer_post_chapter_to_discord', 99 );
106106

107107
// Setup
108108
$user_count = absint( $_GET['users'] ?? 1 );

includes/functions/_module-discord.php

+24-11
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,29 @@ function fictioneer_post_comment_to_discord( $comment_id, $comment_approved ) {
191191
*
192192
* @since 5.6.0
193193
* @since 5.21.2 - Refactored.
194+
* @since 5.27.2 - Switch back to save_post hook to ensure the thumbnail is saved.
194195
*
195-
* @param string $new_status New post status.
196-
* @param string $new_status Old post status.
197-
* @param WP_Post $post Post object.
196+
* @param int $post_id Post ID.
197+
* @param WP_Post $post Post object.
198+
* @param bool $update Whether this is an existing post being updated. Unreliable.
198199
*/
199200

200-
function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
201-
// Only if story going from non-publish status to publish
202-
if ( $post->post_type !== 'fcn_story' || $new_status !== 'publish' || $old_status === 'publish' ) {
201+
function fictioneer_post_story_to_discord( $post_id, $post, $update ) {
202+
// Prevent multi-fire
203+
if ( fictioneer_multi_save_guard( $post_id ) ) {
204+
return;
205+
}
206+
207+
// Only if published chapter
208+
if ( $post->post_type !== 'fcn_story' || $post->post_status !== 'publish' ) {
209+
return;
210+
}
211+
212+
// Only if published less than 10 minutes ago
213+
$post_timestamp = get_post_time( 'U', true, $post_id );
214+
$current_timestamp = current_time( 'U', true );
215+
216+
if ( $update && ( $current_timestamp - $post_timestamp ) >= 600 ) {
203217
return;
204218
}
205219

@@ -210,14 +224,13 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
210224

211225
// Data
212226
$title = html_entity_decode( get_the_title( $post ) );
213-
$url = get_permalink( $post->ID );
214227

215228
// Message
216229
$message = array(
217230
'content' => sprintf(
218231
_x( "New story published: [%s](%s)!\n_ _", 'Discord message for new story.', 'fictioneer' ),
219232
$title,
220-
$url
233+
get_permalink( $post_id )
221234
),
222235
'embeds' => array(
223236
array(
@@ -258,11 +271,11 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
258271
update_post_meta( $post->ID, 'fictioneer_discord_post_trigger', true );
259272

260273
// Unhook if done to avoid additional triggers (if any)
261-
remove_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99 );
274+
remove_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
262275
}
263276

264277
if ( get_option( 'fictioneer_discord_channel_stories_webhook' ) ) {
265-
add_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99, 3 );
278+
add_action( 'save_post', 'fictioneer_post_story_to_discord', 99, 3 );
266279
}
267280

268281
// =============================================================================
@@ -278,7 +291,7 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
278291
*
279292
* @param int $post_id Post ID.
280293
* @param WP_Post $post Post object.
281-
* @param bool $update Whether this is an existing post being updated.
294+
* @param bool $update Whether this is an existing post being updated. Unreliable.
282295
*/
283296

284297
function fictioneer_post_chapter_to_discord( $post_id, $post, $update ) {

0 commit comments

Comments
 (0)