diff --git a/assets/css/admin-autoshare-for-twitter.css b/assets/css/admin-autoshare-for-twitter.css index 6ca702b9..e086cc45 100644 --- a/assets/css/admin-autoshare-for-twitter.css +++ b/assets/css/admin-autoshare-for-twitter.css @@ -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'); +} \ No newline at end of file diff --git a/assets/images/twitter_black.svg b/assets/images/twitter_black.svg new file mode 100644 index 00000000..f63ea717 --- /dev/null +++ b/assets/images/twitter_black.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/assets/images/twitter_blue.svg b/assets/images/twitter_blue.svg new file mode 100644 index 00000000..2dab3f5f --- /dev/null +++ b/assets/images/twitter_blue.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/includes/core.php b/includes/core.php index 8edfe0f0..edc225c4 100644 --- a/includes/core.php +++ b/includes/core.php @@ -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'; @@ -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' ); } @@ -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 ); } } @@ -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( + '', + esc_attr__( 'Tweeted status', 'autoshare-for-twitter' ), + esc_html__( 'Tweeted status', 'autoshare-for-twitter' ) + ); + + // 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( + ' + + ' + ); + } +} diff --git a/tests/phpunit/integration/TestCore.php b/tests/phpunit/integration/TestCore.php index 065020b0..e4ebab18 100644 --- a/tests/phpunit/integration/TestCore.php +++ b/tests/phpunit/integration/TestCore.php @@ -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. @@ -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' ) ); @@ -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' ) );