From 600d93c546eb8a65eaff7ef190da7b36fa17294c Mon Sep 17 00:00:00 2001 From: thrijith Date: Tue, 2 Feb 2021 20:43:15 +0530 Subject: [PATCH 1/8] Add tweet status column for supported post types --- assets/css/admin-autoshare-for-twitter.css | 19 ++++++ assets/images/twitter_black.svg | 1 + assets/images/twitter_blue.svg | 1 + includes/core.php | 74 ++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 assets/images/twitter_black.svg create mode 100644 assets/images/twitter_blue.svg diff --git a/assets/css/admin-autoshare-for-twitter.css b/assets/css/admin-autoshare-for-twitter.css index 6ca702b9..011e9416 100644 --- a/assets/css/admin-autoshare-for-twitter.css +++ b/assets/css/admin-autoshare-for-twitter.css @@ -102,3 +102,22 @@ span.near-limit { span.over-limit { color: red; } + +/* Styling for post columns */ +.fixed .column-is_tweeted { + width: 30px; +} + +.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; +} + +.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..ea82140d --- /dev/null +++ b/assets/images/twitter_black.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/assets/images/twitter_blue.svg b/assets/images/twitter_blue.svg new file mode 100644 index 00000000..13708476 --- /dev/null +++ b/assets/images/twitter_blue.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/includes/core.php b/includes/core.php index 8edfe0f0..6b890f2b 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'; @@ -33,6 +34,9 @@ function setup() { add_action( 'init', __NAMESPACE__ . '\set_post_type_supports' ); 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' ); + + // Setup hooks to add tweet status columns for supported post types. + add_action( 'init', __NAMESPACE__ . '\add_tweeted_column' ); } /** @@ -80,3 +84,73 @@ function maybe_disable_upload_image( $attachment_id ) { return $attachment_id; } + +/** + * Adds tweeted status columns for supported post types. + */ +function add_tweeted_column() { + // 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 ); + } +} + +/** + * 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 ) { + $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 || empty( $status ) ) { + echo ''; + } else { + if ( '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( + ' + + ', + ); + } + } + } +} From 4f9241424ced8f1f8e74e9efeb97da43ab589291 Mon Sep 17 00:00:00 2001 From: thrijith Date: Fri, 12 Feb 2021 18:21:43 +0530 Subject: [PATCH 2/8] Use early return and merge similar usage to same function Allow hover change on post data row only --- assets/css/admin-autoshare-for-twitter.css | 9 ++- includes/core.php | 71 +++++++++------------- 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/assets/css/admin-autoshare-for-twitter.css b/assets/css/admin-autoshare-for-twitter.css index 011e9416..84b54c09 100644 --- a/assets/css/admin-autoshare-for-twitter.css +++ b/assets/css/admin-autoshare-for-twitter.css @@ -105,7 +105,12 @@ span.over-limit { /* Styling for post columns */ .fixed .column-is_tweeted { - width: 30px; + width: 50px; + padding: 15px 0; +} + +.fixed .column-is_tweeted a { + padding: 0; } .autoshare-for-twitter-status-logo::before { @@ -118,6 +123,6 @@ span.over-limit { width: 25px; } -.autoshare-for-twitter-status-logo:hover::before { +.autoshare-for-twitter-status-logo.allow-hover:hover::before { background-image: url('../images/twitter_blue.svg'); } \ No newline at end of file diff --git a/includes/core.php b/includes/core.php index 6b890f2b..730e7a0a 100644 --- a/includes/core.php +++ b/includes/core.php @@ -31,12 +31,10 @@ 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' ); - - // Setup hooks to add tweet status columns for supported post types. - add_action( 'init', __NAMESPACE__ . '\add_tweeted_column' ); } /** @@ -47,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 ); } } @@ -85,19 +86,6 @@ function maybe_disable_upload_image( $attachment_id ) { return $attachment_id; } -/** - * Adds tweeted status columns for supported post types. - */ -function add_tweeted_column() { - // 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 ); - } -} - /** * Add 'Tweeted' column for supported post types. * @@ -127,30 +115,27 @@ function modify_post_type_add_tweet_status_column( $columns ) { * @param int $post_id Post ID. */ function modify_post_type_add_tweet_status( $column_name, $post_id ) { - if ( 'is_tweeted' === $column_name ) { - $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 || empty( $status ) ) { - echo ''; - } else { - if ( '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( - ' - + 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( + ' + ', - ); - } - } + ); } } From 2f315f2eb3ac132f47d912d78a96b988b3cd9c98 Mon Sep 17 00:00:00 2001 From: thrijith Date: Fri, 12 Feb 2021 18:32:15 +0530 Subject: [PATCH 3/8] Remove box shadow for focused tweet status link --- assets/css/admin-autoshare-for-twitter.css | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/assets/css/admin-autoshare-for-twitter.css b/assets/css/admin-autoshare-for-twitter.css index 84b54c09..cbbfcfc9 100644 --- a/assets/css/admin-autoshare-for-twitter.css +++ b/assets/css/admin-autoshare-for-twitter.css @@ -113,6 +113,10 @@ span.over-limit { 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; From cc34ee530489bd78ddb5326260149b4703d8d271 Mon Sep 17 00:00:00 2001 From: thrijith Date: Wed, 17 Feb 2021 14:58:31 +0530 Subject: [PATCH 4/8] Remove trailing commma to support PHP 7.2 and older --- includes/core.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/core.php b/includes/core.php index 730e7a0a..183137e8 100644 --- a/includes/core.php +++ b/includes/core.php @@ -135,7 +135,7 @@ function modify_post_type_add_tweet_status( $column_name, $post_id ) { printf( ' - ', + ' ); } } From 661c63daea9db62092afe6c73b66871aaa68db62 Mon Sep 17 00:00:00 2001 From: thrijith Date: Wed, 17 Feb 2021 14:58:37 +0530 Subject: [PATCH 5/8] Remove trailing commma to support PHP 7.2 and older --- includes/core.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/core.php b/includes/core.php index 183137e8..bd938cb0 100644 --- a/includes/core.php +++ b/includes/core.php @@ -132,7 +132,7 @@ function modify_post_type_add_tweet_status( $column_name, $post_id ) { $date ); - printf( + printf ' ' From c8ffcefec15791c066dcb6e808267d8a9b83fb60 Mon Sep 17 00:00:00 2001 From: thrijith Date: Wed, 17 Feb 2021 15:01:01 +0530 Subject: [PATCH 6/8] Fix typo in function call --- includes/core.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/core.php b/includes/core.php index bd938cb0..183137e8 100644 --- a/includes/core.php +++ b/includes/core.php @@ -132,7 +132,7 @@ function modify_post_type_add_tweet_status( $column_name, $post_id ) { $date ); - printf + printf( ' ' From 4f16a125bb1e013593d77ed51fee47ee6a546abb Mon Sep 17 00:00:00 2001 From: thrijith Date: Wed, 17 Feb 2021 15:36:21 +0530 Subject: [PATCH 7/8] Fix function name in tests for adding post type supports --- tests/phpunit/integration/TestCore.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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' ) ); From 501f12efdda2a705c713ffb56c5c40808c36d6f2 Mon Sep 17 00:00:00 2001 From: Tung Du Date: Tue, 23 Feb 2021 17:05:16 +0700 Subject: [PATCH 8/8] fix: padding issue, update icon --- assets/css/admin-autoshare-for-twitter.css | 4 ++-- assets/images/twitter_black.svg | 6 +++++- assets/images/twitter_blue.svg | 6 +++++- includes/core.php | 4 ++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/assets/css/admin-autoshare-for-twitter.css b/assets/css/admin-autoshare-for-twitter.css index cbbfcfc9..e086cc45 100644 --- a/assets/css/admin-autoshare-for-twitter.css +++ b/assets/css/admin-autoshare-for-twitter.css @@ -106,7 +106,7 @@ span.over-limit { /* Styling for post columns */ .fixed .column-is_tweeted { width: 50px; - padding: 15px 0; + vertical-align: middle; } .fixed .column-is_tweeted a { @@ -127,6 +127,6 @@ span.over-limit { width: 25px; } -.autoshare-for-twitter-status-logo.allow-hover:hover::before { +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 index ea82140d..f63ea717 100644 --- a/assets/images/twitter_black.svg +++ b/assets/images/twitter_black.svg @@ -1 +1,5 @@ - \ No newline at end of file + + + + + diff --git a/assets/images/twitter_blue.svg b/assets/images/twitter_blue.svg index 13708476..2dab3f5f 100644 --- a/assets/images/twitter_blue.svg +++ b/assets/images/twitter_blue.svg @@ -1 +1,5 @@ - \ No newline at end of file + + + + + diff --git a/includes/core.php b/includes/core.php index 183137e8..edc225c4 100644 --- a/includes/core.php +++ b/includes/core.php @@ -134,8 +134,8 @@ function modify_post_type_add_tweet_status( $column_name, $post_id ) { printf( ' - - ' + + ' ); } }