From 2a50e8f69bca66d2b2cc299aed2877d03b5f966a Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Thu, 5 Jan 2023 11:49:57 +0530 Subject: [PATCH 1/2] Show twitter status logs for other post statuses. --- includes/admin/post-meta.php | 188 ++++++++++--------- src/js/AutoshareForTwitterPrePublishPanel.js | 17 +- 2 files changed, 116 insertions(+), 89 deletions(-) diff --git a/includes/admin/post-meta.php b/includes/admin/post-meta.php index 1a78aab3..c64e94aa 100644 --- a/includes/admin/post-meta.php +++ b/includes/admin/post-meta.php @@ -247,6 +247,16 @@ function render_tweet_submitbox( $post ) { +
+ +
+
+ ID, TWITTER_STATUS_KEY ); + + if ( empty( $tweet_metas ) || isset( $tweet_metas['twitter_id'] ) ) { + $tweet_metas = array( + array( + 'status' => isset( $tweet_metas['status'] ) ? $tweet_metas['status'] : '', + 'created_at' => isset( $tweet_metas['created_at'] ) ? $tweet_metas['created_at'] : '', + 'twitter_id' => isset( $tweet_metas['twitter_id'] ) ? $tweet_metas['twitter_id'] : '', + ), + ); + } elseif ( isset( $tweet_metas['status'] ) && ( 'error' === $tweet_metas['status'] || 'unknown' === $tweet_metas['status'] || 'other' === $tweet_metas['status'] ) ) { + $tweet_metas = array( + $tweet_metas, + ); + } - $tweet_metas = Utils\get_autoshare_for_twitter_meta( $post->ID, TWITTER_STATUS_KEY ); - - if ( empty( $tweet_metas ) || isset( $tweet_metas['twitter_id'] ) ) { - $tweet_metas = array( - array( - 'status' => isset( $tweet_metas['status'] ) ? $tweet_metas['status'] : '', - 'created_at' => isset( $tweet_metas['created_at'] ) ? $tweet_metas['created_at'] : '', - 'twitter_id' => isset( $tweet_metas['twitter_id'] ) ? $tweet_metas['twitter_id'] : '', - ), - ); - } elseif ( isset( $tweet_metas['status'] ) && ( 'error' === $tweet_metas['status'] || 'unknown' === $tweet_metas['status'] || 'other' === $tweet_metas['status'] ) ) { - $tweet_metas = array( - $tweet_metas, - ); + foreach ( $tweet_metas as $tweet_meta ) { + $status = $tweet_meta['status']; + if ( 'publish' !== $post_status && empty( $status ) ) { + continue; } - foreach ( $tweet_metas as $tweet_meta ) { - $status = $tweet_meta['status']; - - switch ( $status ) { - case 'published': - $date = Utils\date_from_twitter( $tweet_meta['created_at'] ); - $twitter_url = Utils\link_from_twitter( $tweet_meta['twitter_id'] ); - - $response_array[] = [ - // Translators: Placeholder is a date. - 'message' => sprintf( __( 'Tweeted on %s', 'autoshare-for-twitter' ), $date ), - 'url' => $twitter_url, - 'status' => $status, - ]; - - break; - - case 'error': - $response_array[] = [ - 'message' => __( 'Failed to tweet: ', 'autoshare-for-twitter' ) . $tweet_meta['message'], - 'status' => $status, - ]; - - break; - - case 'unknown': - $response_array[] = [ - 'message' => $tweet_meta['message'], - 'status' => $status, - ]; - - break; - - default: - $response_array[] = [ - 'message' => __( 'This post was not tweeted.', 'autoshare-for-twitter' ), - 'status' => $status, - ]; - } + switch ( $status ) { + case 'published': + $date = Utils\date_from_twitter( $tweet_meta['created_at'] ); + $twitter_url = Utils\link_from_twitter( $tweet_meta['twitter_id'] ); + + $response_array[] = [ + // Translators: Placeholder is a date. + 'message' => sprintf( __( 'Tweeted on %s', 'autoshare-for-twitter' ), $date ), + 'url' => $twitter_url, + 'status' => $status, + ]; + + break; + + case 'error': + $response_array[] = [ + 'message' => __( 'Failed to tweet: ', 'autoshare-for-twitter' ) . $tweet_meta['message'], + 'status' => $status, + ]; + + break; + + case 'unknown': + $response_array[] = [ + 'message' => $tweet_meta['message'], + 'status' => $status, + ]; + + break; + + default: + $response_array[] = [ + 'message' => __( 'This post was not tweeted.', 'autoshare-for-twitter' ), + 'status' => $status, + ]; } } @@ -342,48 +352,50 @@ function get_tweet_status_logs( $post ) { $post_status = get_post_status( $post ); $status_logs = ''; - if ( 'publish' === $post_status ) { - $tweet_metas = Utils\get_autoshare_for_twitter_meta( $post->ID, TWITTER_STATUS_KEY ); - - if ( empty( $tweet_metas ) || isset( $tweet_metas['twitter_id'] ) ) { - $tweet_metas = array( - array( - 'status' => isset( $tweet_metas['status'] ) ? $tweet_metas['status'] : '', - 'created_at' => isset( $tweet_metas['created_at'] ) ? $tweet_metas['created_at'] : '', - 'twitter_id' => isset( $tweet_metas['twitter_id'] ) ? $tweet_metas['twitter_id'] : '', - ), - ); - } elseif ( isset( $tweet_metas['status'] ) && ( 'error' === $tweet_metas['status'] || 'unknown' === $tweet_metas['status'] || 'other' === $tweet_metas['status'] ) ) { - $tweet_metas = array( - $tweet_metas, - ); - } - - foreach ( $tweet_metas as $twitter_meta ) { - $status = isset( $twitter_meta['status'] ) ? $twitter_meta['status'] : ''; + $tweet_metas = Utils\get_autoshare_for_twitter_meta( $post->ID, TWITTER_STATUS_KEY ); + + if ( empty( $tweet_metas ) || isset( $tweet_metas['twitter_id'] ) ) { + $tweet_metas = array( + array( + 'status' => isset( $tweet_metas['status'] ) ? $tweet_metas['status'] : '', + 'created_at' => isset( $tweet_metas['created_at'] ) ? $tweet_metas['created_at'] : '', + 'twitter_id' => isset( $tweet_metas['twitter_id'] ) ? $tweet_metas['twitter_id'] : '', + ), + ); + } elseif ( isset( $tweet_metas['status'] ) && ( 'error' === $tweet_metas['status'] || 'unknown' === $tweet_metas['status'] || 'other' === $tweet_metas['status'] ) ) { + $tweet_metas = array( + $tweet_metas, + ); + } - switch ( $status ) { + foreach ( $tweet_metas as $twitter_meta ) { + $status = isset( $twitter_meta['status'] ) ? $twitter_meta['status'] : ''; + if ( 'publish' !== $post_status && empty( $status ) ) { + continue; + } - case 'published': - $output = markup_published( $twitter_meta ); - break; + switch ( $status ) { - case 'error': - $output = markup_error( $twitter_meta ); - break; + case 'published': + $output = markup_published( $twitter_meta ); + break; - case 'unknown': - $output = markup_unknown( $twitter_meta ); - break; + case 'error': + $output = markup_error( $twitter_meta ); + break; - default: - $output = __( 'This post was not tweeted.', 'autoshare-for-twitter' ); - break; - } + case 'unknown': + $output = markup_unknown( $twitter_meta ); + break; - $status_logs .= "
$output
"; + default: + $output = __( 'This post was not tweeted.', 'autoshare-for-twitter' ); + break; } + + $status_logs .= "
$output
"; } + return wp_kses_post( $status_logs ); } diff --git a/src/js/AutoshareForTwitterPrePublishPanel.js b/src/js/AutoshareForTwitterPrePublishPanel.js index 0cc60716..ea047e55 100644 --- a/src/js/AutoshareForTwitterPrePublishPanel.js +++ b/src/js/AutoshareForTwitterPrePublishPanel.js @@ -1,4 +1,5 @@ -import { Button, ToggleControl } from '@wordpress/components'; +import { Button, ToggleControl, ExternalLink, CardDivider } from '@wordpress/components'; +import { select } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { TweetTextField } from './components/TweetTextField'; import { @@ -9,6 +10,7 @@ import { useSaveTwitterData, useHasFeaturedImage, } from './hooks'; +import { getIconByStatus } from './utils'; export default function AutoshareForTwitterPrePublishPanel() { const [ autoshareEnabled, setAutoshareEnabled ] = useTwitterAutoshareEnabled(); @@ -17,10 +19,23 @@ export default function AutoshareForTwitterPrePublishPanel() { const [ errorMessage ] = useTwitterAutoshareErrorMessage(); const hasFeaturedImage = useHasFeaturedImage(); + const messages = select( 'core/editor' ).getCurrentPostAttribute( 'autoshare_for_twitter_status' ); useSaveTwitterData(); return ( <> + { messages && !!messages.message.length && (
+ { messages.message.map( ( statusMessage, index ) => { + const TweetIcon = getIconByStatus( statusMessage.status ); + + return ( +
+ { TweetIcon }{ statusMessage.url ? { statusMessage.message } : statusMessage.message } +
+ ); + } ) } + +
) } Date: Thu, 5 Jan 2023 14:35:15 +0530 Subject: [PATCH 2/2] fix unit tests. --- includes/admin/post-meta.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/admin/post-meta.php b/includes/admin/post-meta.php index c64e94aa..fe769e3d 100644 --- a/includes/admin/post-meta.php +++ b/includes/admin/post-meta.php @@ -275,6 +275,10 @@ function get_tweet_status_message( $post ) { $post_status = get_post_status( $post ); $response_array = array(); + if ( empty( $post ) ) { + return [ 'message' => $response_array ]; + } + $tweet_metas = Utils\get_autoshare_for_twitter_meta( $post->ID, TWITTER_STATUS_KEY ); if ( empty( $tweet_metas ) || isset( $tweet_metas['twitter_id'] ) ) {