Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show twitter status logs for the draft post if the post has been switched back to Draft from Published, and has already been Tweeted. #215

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 104 additions & 88 deletions includes/admin/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ function render_tweet_submitbox( $post ) {
<?php
// Default output.
} else {
$status_logs = get_tweet_status_logs( $post );
if ( ! empty( $status_logs ) ) {
// Display tweet status logs.
?>
<div class="autoshare-for-twitter-status-logs-wrapper">
<?php echo wp_kses_post( $status_logs ); ?>
</div>
<hr/>
<?php
}
echo _safe_markup_default(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

Expand All @@ -265,63 +275,67 @@ function get_tweet_status_message( $post ) {
$post_status = get_post_status( $post );
$response_array = array();

if ( 'publish' === $post_status ) {
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'] ) ) {
$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,
];
}
}

Expand All @@ -342,48 +356,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 .= "<div class='autoshare-for-twitter-status-wrap'><span class='autoshare-for-twitter-status-icon autoshare-for-twitter-status-icon--$status'></span>$output</div>";
default:
$output = __( 'This post was not tweeted.', 'autoshare-for-twitter' );
break;
}

$status_logs .= "<div class='autoshare-for-twitter-status-wrap'><span class='autoshare-for-twitter-status-icon autoshare-for-twitter-status-icon--$status'></span>$output</div>";
}

return wp_kses_post( $status_logs );
}

Expand Down
17 changes: 16 additions & 1 deletion src/js/AutoshareForTwitterPrePublishPanel.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,6 +10,7 @@ import {
useSaveTwitterData,
useHasFeaturedImage,
} from './hooks';
import { getIconByStatus } from './utils';

export default function AutoshareForTwitterPrePublishPanel() {
const [ autoshareEnabled, setAutoshareEnabled ] = useTwitterAutoshareEnabled();
Expand All @@ -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 && ( <div className="autoshare-for-twitter-post-status">
{ messages.message.map( ( statusMessage, index ) => {
const TweetIcon = getIconByStatus( statusMessage.status );

return (
<div className="autoshare-for-twitter-log" key={ index }>
{ TweetIcon }{ statusMessage.url ? <ExternalLink href={ statusMessage.url }>{ statusMessage.message }</ExternalLink> : statusMessage.message }
</div>
);
} ) }
<CardDivider />
</div> ) }
<ToggleControl
label={ autoshareEnabled ? __( 'Tweet when published', 'autoshare-for-twitter' ) : __( 'Don\'t Tweet', 'autoshare-for-twitter' )
}
Expand Down