Fix missing thumbnail in Discord story embed

This commit is contained in:
Tetrakern 2025-01-18 20:14:15 +01:00
parent 623d7ffa60
commit d145405955
3 changed files with 28 additions and 15 deletions

View File

@ -304,14 +304,14 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `publish_to_draft` | `fictioneer_chapter_to_draft` (10) | `publish_to_draft` | `fictioneer_chapter_to_draft` (10)
| `rest_api_init` | `fictioneer_register_endpoint_get_story_comments` (10) | `rest_api_init` | `fictioneer_register_endpoint_get_story_comments` (10)
| `restrict_manage_posts` | `fictioneer_add_chapter_story_filter_dropdown` (10) | `restrict_manage_posts` | `fictioneer_add_chapter_story_filter_dropdown` (10)
| `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) | `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)
| `send_headers` | `fictioneer_block_pages_from_indexing` (10) | `send_headers` | `fictioneer_block_pages_from_indexing` (10)
| `set_logged_in_cookie` | `fictioneer_set_logged_in_cookie` (10) | `set_logged_in_cookie` | `fictioneer_set_logged_in_cookie` (10)
| `show_user_profile` | `fictioneer_custom_profile_fields` (20) | `show_user_profile` | `fictioneer_custom_profile_fields` (20)
| `shutdown` | `fictioneer_save_story_card_cache` (10) | `shutdown` | `fictioneer_save_story_card_cache` (10)
| `switch_theme` | `fictioneer_theme_deactivation` (10) | `switch_theme` | `fictioneer_theme_deactivation` (10)
| `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) | `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)
| `transition_post_status` | `fictioneer_log_story_chapter_status_changes` (10), `fictioneer_chapter_future_to_publish` (10), `fictioneer_post_story_to_discord` (99) | `transition_post_status` | `fictioneer_log_story_chapter_status_changes` (10), `fictioneer_chapter_future_to_publish` (10)
| `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) | `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)
| `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) | `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)
| `update_option_*` | `fictioneer_update_option_disable_extended_chapter_list_meta_queries` (10), `fictioneer_update_option_disable_extended_story_list_meta_queries` (10) | `update_option_*` | `fictioneer_update_option_disable_extended_chapter_list_meta_queries` (10), `fictioneer_update_option_disable_extended_story_list_meta_queries` (10)

View File

@ -101,8 +101,8 @@ function fictioneer_generate_test_content() {
// Disable Discord // Disable Discord
remove_action( 'comment_post', 'fictioneer_post_comment_to_discord', 99 ); remove_action( 'comment_post', 'fictioneer_post_comment_to_discord', 99 );
remove_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99 ); remove_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
remove_action( 'transition_post_status', 'fictioneer_post_chapter_to_discord', 99 ); remove_action( 'save_post', 'fictioneer_post_chapter_to_discord', 99 );
// Setup // Setup
$user_count = absint( $_GET['users'] ?? 1 ); $user_count = absint( $_GET['users'] ?? 1 );

View File

@ -191,15 +191,29 @@ if ( get_option( 'fictioneer_discord_channel_comments_webhook' ) ) {
* *
* @since 5.6.0 * @since 5.6.0
* @since 5.21.2 - Refactored. * @since 5.21.2 - Refactored.
* @since 5.27.2 - Switch back to save_post hook to ensure the thumbnail is saved.
* *
* @param string $new_status New post status. * @param int $post_id Post ID.
* @param string $new_status Old post status. * @param WP_Post $post Post object.
* @param WP_Post $post Post object. * @param bool $update Whether this is an existing post being updated. Unreliable.
*/ */
function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) { function fictioneer_post_story_to_discord( $post_id, $post, $update ) {
// Only if story going from non-publish status to publish // Prevent multi-fire
if ( $post->post_type !== 'fcn_story' || $new_status !== 'publish' || $old_status === 'publish' ) { if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Only if published chapter
if ( $post->post_type !== 'fcn_story' || $post->post_status !== 'publish' ) {
return;
}
// Only if published less than 10 minutes ago
$post_timestamp = get_post_time( 'U', true, $post_id );
$current_timestamp = current_time( 'U', true );
if ( $update && ( $current_timestamp - $post_timestamp ) >= 600 ) {
return; return;
} }
@ -210,14 +224,13 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
// Data // Data
$title = html_entity_decode( get_the_title( $post ) ); $title = html_entity_decode( get_the_title( $post ) );
$url = get_permalink( $post->ID );
// Message // Message
$message = array( $message = array(
'content' => sprintf( 'content' => sprintf(
_x( "New story published: [%s](%s)!\n_ _", 'Discord message for new story.', 'fictioneer' ), _x( "New story published: [%s](%s)!\n_ _", 'Discord message for new story.', 'fictioneer' ),
$title, $title,
$url get_permalink( $post_id )
), ),
'embeds' => array( 'embeds' => array(
array( array(
@ -258,11 +271,11 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
update_post_meta( $post->ID, 'fictioneer_discord_post_trigger', true ); update_post_meta( $post->ID, 'fictioneer_discord_post_trigger', true );
// Unhook if done to avoid additional triggers (if any) // Unhook if done to avoid additional triggers (if any)
remove_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99 ); remove_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
} }
if ( get_option( 'fictioneer_discord_channel_stories_webhook' ) ) { if ( get_option( 'fictioneer_discord_channel_stories_webhook' ) ) {
add_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99, 3 ); add_action( 'save_post', 'fictioneer_post_story_to_discord', 99, 3 );
} }
// ============================================================================= // =============================================================================
@ -278,7 +291,7 @@ if ( get_option( 'fictioneer_discord_channel_stories_webhook' ) ) {
* *
* @param int $post_id Post ID. * @param int $post_id Post ID.
* @param WP_Post $post Post object. * @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated. * @param bool $update Whether this is an existing post being updated. Unreliable.
*/ */
function fictioneer_post_chapter_to_discord( $post_id, $post, $update ) { function fictioneer_post_chapter_to_discord( $post_id, $post, $update ) {