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

Add tweet status column for supported post types #121

Merged
merged 10 commits into from
Mar 3, 2021
28 changes: 28 additions & 0 deletions assets/css/admin-autoshare-for-twitter.css
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,31 @@ span.near-limit {
span.over-limit {
color: red;
}

/* Styling for post columns */
.fixed .column-is_tweeted {
width: 50px;
vertical-align: middle;
}

.fixed .column-is_tweeted a {
padding: 0;
}

.fixed .column-is_tweeted a:focus {
box-shadow: unset;
}

.autoshare-for-twitter-status-logo::before {
background-image: url('../images/twitter_black.svg');
background-repeat: no-repeat;
background-size: 25px 25px;
content: ' ';
display: block;
height: 25px;
width: 25px;
}

tbody .autoshare-for-twitter-status-logo:hover::before {
background-image: url('../images/twitter_blue.svg');
}
5 changes: 5 additions & 0 deletions assets/images/twitter_black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions assets/images/twitter_blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 62 additions & 3 deletions includes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace TenUp\AutoshareForTwitter\Core;

use TenUp\AutoshareForTwitter\Utils;
use const TenUp\AutoshareForTwitter\Core\Post_Meta\TWITTER_STATUS_KEY;

const POST_TYPE_SUPPORT_FEATURE = 'autoshare-for-twitter';

Expand All @@ -30,7 +31,8 @@ function setup() {
*/
do_action( 'autoshare_for_twitter_setup' );

add_action( 'init', __NAMESPACE__ . '\set_post_type_supports' );
// Setup hooks to add post type support and tweet status columns for supported / enabled post types.
add_action( 'init', __NAMESPACE__ . '\set_post_type_supports_with_custom_columns' );
add_filter( 'autoshare_for_twitter_enabled_default', __NAMESPACE__ . '\maybe_enable_autoshare_by_default' );
add_filter( 'autoshare_for_twitter_attached_image', __NAMESPACE__ . '\maybe_disable_upload_image' );
}
Expand All @@ -43,14 +45,17 @@ function setup() {
add_action( 'autoshare_for_twitter_loaded', __NAMESPACE__ . '\setup' );

/**
* Adds autoshare support for default post types.
* Adds autoshare support for enabled post types, and add tweeted status column.
*
* @since 1.0.0
*/
function set_post_type_supports() {
function set_post_type_supports_with_custom_columns() {
// Loop through all the supported post types and add tweet status column.
$post_types = Utils\get_enabled_post_types();
foreach ( (array) $post_types as $post_type ) {
add_post_type_support( $post_type, POST_TYPE_SUPPORT_FEATURE );
add_filter( "manage_{$post_type}_posts_columns", __NAMESPACE__ . '\modify_post_type_add_tweet_status_column' );
add_action( 'manage_' . $post_type . '_posts_custom_column', __NAMESPACE__ . '\modify_post_type_add_tweet_status', 10, 2 );
}
}

Expand Down Expand Up @@ -80,3 +85,57 @@ function maybe_disable_upload_image( $attachment_id ) {

return $attachment_id;
}

/**
* Add 'Tweeted' column for supported post types.
*
* @param array $columns Supported columns for a post type.
*/
function modify_post_type_add_tweet_status_column( $columns ) {
// Do this so our custom column doesn't end up being the last one, messing up UI.
unset( $columns['date'] );

// Add tweet status column header.
$columns['is_tweeted'] = sprintf(
'<span class="autoshare-for-twitter-status-logo" title="%s"><span class="screen-reader-text">%s</span></span>',
esc_attr__( 'Tweeted status', 'autoshare-for-twitter' ),
esc_html__( 'Tweeted status', 'autoshare-for-twitter' )
dinhtungdu marked this conversation as resolved.
Show resolved Hide resolved
);

// Add the date column back.
$columns['date'] = esc_html__( 'Date', 'autoshare-for-twitter' );

return $columns;
}

/**
* Add tweet status data to each row.
*
* @param string $column_name Column name.
* @param int $post_id Post ID.
*/
function modify_post_type_add_tweet_status( $column_name, $post_id ) {
if ( 'is_tweeted' !== $column_name ) {
return;
}

$post_status = get_post_status( $post_id );
$tweet_status = Utils\get_autoshare_for_twitter_meta( $post_id, TWITTER_STATUS_KEY );
$status = isset( $tweet_status['status'] ) ? $tweet_status['status'] : '';

if ( 'publish' === $post_status && 'published' === $status ) {
$date = Utils\date_from_twitter( $tweet_status['created_at'] );
$twitter_url = Utils\link_from_twitter( $tweet_status['twitter_id'] );
$tweet_title = sprintf(
'%s %s',
__( 'Tweeted on', 'autoshare-for-twitter' ),
$date
);

printf(
'<a href="' . esc_url( $twitter_url ) . '" target="_blank" title="' . esc_attr( $tweet_title ) . '">
<span class="autoshare-for-twitter-status-logo"></span>
</a>'
);
}
}
6 changes: 3 additions & 3 deletions tests/phpunit/integration/TestCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace TenUp\AutoshareForTwitter\Tests;

use \WP_UnitTestCase;
use function TenUp\AutoshareForTwitter\Core\set_post_type_supports;
use function TenUp\AutoshareForTwitter\Core\set_post_type_supports_with_custom_columns;

/**
* TestCore class.
Expand All @@ -35,7 +35,7 @@ public function test_set_post_type_supports() {

// Test that posts and pages support the feature by default, but not other post types.
reset_post_type_support();
set_post_type_supports();
set_post_type_supports_with_custom_columns();
$this->assertTrue( post_type_supports( 'post', 'autoshare-for-twitter' ) );
$this->assertTrue( post_type_supports( 'page', 'autoshare-for-twitter' ) );

Expand All @@ -48,7 +48,7 @@ public function test_set_post_type_supports() {
};
add_filter( 'autoshare_for_twitter_default_post_types', $filter_post_type_supports );

set_post_type_supports();
set_post_type_supports_with_custom_columns();
$this->assertFalse( post_type_supports( 'post', 'autoshare-for-twitter' ) );
$this->assertFalse( post_type_supports( 'page', 'autoshare-for-twitter' ) );
$this->assertTrue( post_type_supports( $non_default_post_type, 'autoshare-for-twitter' ) );
Expand Down