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

Save autoshare meta from post form before publishing tweet #82

Merged
merged 4 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 32 additions & 10 deletions includes/admin/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,46 @@
function setup() {
add_action( 'post_submitbox_misc_actions', __NAMESPACE__ . '\tweet_submitbox_callback', 15 );
add_action( 'autoshare_for_twitter_metabox', __NAMESPACE__ . '\render_tweet_submitbox', 10, 1 );
add_action( 'save_post', __NAMESPACE__ . '\save_tweet_meta', 10, 1 );
add_action( 'save_post', __NAMESPACE__ . '\save_tweet_meta', 10, 3 );
}

/**
* Handles the saving of post_meta to catch the times the ajax save might not run.
* Like when clicking 'Save Draft' or 'Publish' straight from the tweet body field.
*
* @param int $post_id The post id.
* @param int $post_id The post id.
* @param WP_Post $post Post object.
* @param boolean $update Whether the post already exists.
*
* @return void
*/
function save_tweet_meta( $post_id ) {
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! current_user_can( 'edit_post', $post_id ) ) {
function save_tweet_meta( $post_id, $post = null, $update = true ) {
if ( ! $update || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! current_user_can( 'edit_post', $post_id ) ) {
return;
}

$form_data = sanitize_autoshare_for_twitter_meta_data(
// Using FILTER_DEFAULT here as data is being passed to sanitize function.
filter_input( INPUT_POST, META_PREFIX, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broke this out into a function (see below) for easier testing.

);
$form_data = sanitize_autoshare_for_twitter_meta_data( get_autoshare_post_form_data() );

save_autoshare_for_twitter_meta_data( $post_id, $form_data );
}

/**
* Provides data passed from the post editor form.
*
* @return array
*/
function get_autoshare_post_form_data() {
// Using FILTER_DEFAULT here as data is being passed to sanitize function.
$data = filter_input( INPUT_POST, META_PREFIX, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );

/**
* Filters data received from the post form.
*
* @param array $data
*/
return apply_filters( 'autoshare_post_form_data', $data );
}

/**
* Sanitizes autoshare-related fields passed while saving a post.
*
Expand Down Expand Up @@ -105,8 +121,14 @@ function sanitize_autoshare_for_twitter_meta_data( $data ) {
* @param array $data Associative array of data to save.
*/
function save_autoshare_for_twitter_meta_data( $post_id, $data ) {
if ( empty( $data ) || ! is_array( $data ) ) {
return;

if ( ! is_array( $data ) ) {
$data = [];
}

// If the enable key is not set, it should be turned off.
if ( ! isset( $data[ ENABLE_AUTOSHARE_FOR_TWITTER_KEY ] ) ) {
$data[ ENABLE_AUTOSHARE_FOR_TWITTER_KEY ] = 0;
}

foreach ( $data as $key => $value ) {
Expand Down
4 changes: 4 additions & 0 deletions includes/admin/post-transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use TenUp\AutoshareForTwitter\Utils as Utils;
use function TenUp\AutoshareForTwitter\Utils\delete_autoshare_for_twitter_meta;
use function TenUp\AutoshareForTwitter\Utils\update_autoshare_for_twitter_meta;
use function TenUp\AutoshareForTwitter\Core\Post_Meta\save_tweet_meta;

/**
* Setup function.
Expand Down Expand Up @@ -46,6 +47,9 @@ function maybe_publish_tweet( $new_status, $old_status, $post ) {
return;
}

// Ensure Autoshare-related form data is saved before reaching the publish_tweet step.
save_tweet_meta( $post->ID );

/**
* Don't bother enqueuing assets if the post type hasn't opted into autosharing
*/
Expand Down
61 changes: 60 additions & 1 deletion tests/phpunit/integration/TestPostMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
use WP_UnitTestCase;

use const TenUp\AutoshareForTwitter\Core\Admin\AT_SETTINGS;
use const TenUp\AutoshareForTwitter\Core\Post_Meta\ENABLE_AUTOSHARE_FOR_TWITTER_KEY;
use const TenUp\AutoshareForTwitter\Core\Post_Meta\TWEET_BODY_KEY;
use const TenUp\AutoshareForTwitter\Core\Post_Meta\TWITTER_STATUS_KEY;

use function TenUp\AutoshareForTwitter\Core\Post_Meta\get_tweet_status_message;
use function TenUp\AutoshareForTwitter\Core\Post_Meta\save_autoshare_for_twitter_meta_data;
use function TenUp\AutoshareForTwitter\Utils\get_autoshare_for_twitter_meta;

/**
* TestUtils class.
*
* @sincd 1.0.0
*
* @group post_meta
*/
class TestPostMeta extends WP_UnitTestCase {
/**
Expand All @@ -32,7 +38,7 @@ public function test_get_tweet_status_message() {
get_tweet_status_message( -1 )
);

$post = $this->factory->post->create( [ 'status' => 'publish' ] );
$post = $this->factory->post->create( [ 'post_status' => 'publish' ] );

$published_filter = function( $data, $id, $key ) use ( $post ) {
if ( intval( $post ) === intval( $id ) && TWITTER_STATUS_KEY === $key ) {
Expand Down Expand Up @@ -112,4 +118,57 @@ public function test_get_tweet_status_message() {
remove_filter( 'autoshare_for_twitter_meta', $other_filter );
}

/**
* Provides test data.
*
* @return array
*/
public function save_autoshare_for_twitter_meta_data_provider() {
return [
[
// Test autoshare is disabled when no data is passed.
[ 'post_status' => 'publish' ],
[],
false,
],
[
// Test autoshare is disabled when false is passed.
[ 'post_status' => 'publish' ],
[ ENABLE_AUTOSHARE_FOR_TWITTER_KEY => '0' ],
false,
],
[
// Test autoshare is disabled when only a tweet body is passed.
[ 'post_status' => 'publish' ],
[ TWEET_BODY_KEY => 'my cool tweet' ],
false,
],
[
// Test autoshare is enabled when true is passed.
[ 'post_status' => 'publish' ],
[ ENABLE_AUTOSHARE_FOR_TWITTER_KEY => '1' ],
true,
],
];
}

/**
* Tests the save_autoshare_for_twitter_meta_data function.
*
* @dataProvider save_autoshare_for_twitter_meta_data_provider
*
* @param array $args Create post args.
* @param array $data Meta data to save.
* @param boolean $expected Expecte result.
*/
public function test_save_autoshare_for_twitter_meta_data( $args, $data, $expected ) {
$id = $this->factory->post->create( $args );
save_autoshare_for_twitter_meta_data( $id, $data );

if ( $expected ) {
$this->assertTrue( (bool) get_autoshare_for_twitter_meta( $id, ENABLE_AUTOSHARE_FOR_TWITTER_KEY ) );
} else {
$this->assertFalse( (bool) get_autoshare_for_twitter_meta( $id, ENABLE_AUTOSHARE_FOR_TWITTER_KEY ) );
}
}
}
129 changes: 129 additions & 0 deletions tests/phpunit/integration/TestPostTransition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* Tests functions admin/post-meta.php.
*
* @since 1.0.0
* @package TenUp\AutoshareForTwitter
*/

namespace TenUp\AutoshareForTwitter\Tests;

use WP_UnitTestCase;

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

use function TenUp\AutoshareForTwitter\Core\Post_Meta\get_tweet_status_message;
use function TenUp\AutoshareForTwitter\Core\Post_Transition\maybe_publish_tweet;

/**
* TestPostTransition
*
* @group post_transition
*
* @sincd 1.0.0
*/
class TestPostTransition extends WP_UnitTestCase {
/**
* Setup.
*/
public function setUp() {
wp_set_current_user( 1 );

parent::setUp();
}

/**
* Provides test data.
*
* @return array
*/
public function maybe_publish_tweet_provider() {
return [
[
// Post transitioning from publish to draft should not tweet.
[ 'post_status' => 'publish' ],
'1',
'draft',
'publish',
false,
],
[
// Already-published post should not tweet.
[ 'post_status' => 'publish' ],
'1',
'publish',
'publish',
false,
],
[
// Post transitioning from draft to publish should tweet if autotweet_enabled is true.
[
'post_status' => 'draft',
'post_title' => 'TEST',
'post_author' => 1,
],
'1',
'publish',
'draft',
true,
],
[
// Post transitioning from draft to publish should not tweet if autotweet is not true.
[ 'post_status' => 'draft' ],
'0',
'publish',
'draft',
false,
],
];
}

/**
* Tests the maybe_publish_tweet function.
*
* @dataProvider maybe_publish_tweet_provider
*
* @param array $post_args Args to pass to the create post function.
* @param boolean $autoshare_enabled_form_data Updated autoshare enabled meta value.
* @param string $new_status The new post status.
* @param string $old_status The old post status.
* @param boolean $expected_should_tweet Whether the post should be tweeted.
*/
public function test_maybe_publish_tweet(
$post_args,
$autoshare_enabled_form_data,
$new_status,
$old_status,
$expected_should_tweet
) {
$the_post = $this->factory->post->create_and_get( $post_args );
$post_was_tweeted = false;
$pre_status_update_callback = function() use ( &$post_was_tweeted ) {
$post_was_tweeted = true;

// Minimum valid response.
return (object) [
'id' => 1,
'created_at' => time(),
];
};
add_filter( 'autoshare_for_twitter_pre_status_update', $pre_status_update_callback );

$post_form_data_callback = function() use ( $autoshare_enabled_form_data ) {
return [ ENABLE_AUTOSHARE_FOR_TWITTER_KEY => $autoshare_enabled_form_data ];
};
add_filter( 'autoshare_post_form_data', $post_form_data_callback );

maybe_publish_tweet( $new_status, $old_status, $the_post );

if ( $expected_should_tweet ) {
$this->assertTrue( $post_was_tweeted );
} else {
$this->assertFalse( $post_was_tweeted );
}

remove_filter( 'autoshare_for_twitter_pre_status_update', $pre_status_update_callback );
remove_filter( 'autoshare_post_form_data', $post_form_data_callback );
}
}