Skip to content

Commit

Permalink
Things: Pass previous state to after_save() and *_saved actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean90 committed Feb 19, 2017
1 parent 0f0b32d commit 92c2881
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 15 deletions.
36 changes: 30 additions & 6 deletions gp-includes/thing.php
Expand Up @@ -340,14 +340,37 @@ public function get( $thing_or_id ) {
return $this->find_one( array( 'id' => $id ) );
}

/**
* Saves an existing thing.
*
* @since 1.0.0
*
* @param mixed $args Values to update.
* @return bool|null Null and false on failure, true on success.
*/
public function save( $args = null ) {
if ( is_null( $args ) ) $args = get_object_vars( $this );
if ( !is_array( $args ) ) $args = (array)$args;
$thing_before = clone $this;

if ( is_null( $args ) ) {
$args = get_object_vars( $this );
}

if ( ! is_array( $args ) ) {
$args = (array) $args;
}

$args = $this->prepare_fields_for_save( $args );
$update_res = $this->update( $args );

$update_res = $this->update( $args );

$this->set_fields( $args );
if ( !$update_res ) return null;
$update_res = $this->after_save();

if ( ! $update_res ) {
return null;
}

$update_res = $this->after_save( $thing_before );

return $update_res;
}

Expand Down Expand Up @@ -482,9 +505,10 @@ public function after_create() {
*
* This is a placeholder function which should be implemented in the child classes.
*
* @param GP_Thing $thing_before Object before the update.
* @return bool
*/
public function after_save() {
public function after_save( $thing_before ) {
return true;
}

Expand Down
10 changes: 7 additions & 3 deletions gp-includes/things/original.php
Expand Up @@ -479,18 +479,22 @@ public function after_create() {
* Executes after saving an original.
*
* @since 2.0.0
* @since 2.4.0 Added the `$original_before` parameter.
*
* @param GP_Original $original_before Original before the update.
* @return bool
*/
public function after_save() {
public function after_save( $original_before ) {
/**
* Fires after an original is saved.
*
* @since 2.0.0
* @since 2.4.0 Added the `$original_before` parameter.
*
* @param GP_original $original The original that was saved.
* @param GP_Original $original Original following the update.
* @param GP_Original $original_before Original before the update.
*/
do_action( 'gp_original_saved', $this );
do_action( 'gp_original_saved', $this, $original_before );

return true;
}
Expand Down
10 changes: 7 additions & 3 deletions gp-includes/things/project.php
Expand Up @@ -127,18 +127,22 @@ public function after_create() {
* Executes after saving a project.
*
* @since 1.0.0
* @since 2.4.0 Added the `$project_before` parameter.
*
* @param GP_Project $project_before Project before the update.
* @return bool
*/
public function after_save() {
public function after_save( $project_before ) {
/**
* Fires after saving a project.
*
* @since 1.0.0
* @since 2.4.0 Added the `$project_before` parameter.
*
* @param GP_Project $project The project that was saved.
* @param GP_Project $project Project following the update.
* @param GP_Project $project_before Project before the update.
*/
do_action( 'gp_project_saved', $this );
do_action( 'gp_project_saved', $this, $project_before );

// TODO: pass the update args to after/pre_save?
// TODO: only call it if the slug or parent project were changed
Expand Down
10 changes: 7 additions & 3 deletions gp-includes/things/translation.php
Expand Up @@ -687,18 +687,22 @@ public function after_create() {
* Executes after saving a translation.
*
* @since 1.0.0
* @since 2.4.0 Added the `$original_before` parameter.
*
* @param GP_Translation $translation_before Translation before the update.
* @return bool
*/
public function after_save() {
public function after_save( $translation_before ) {
/**
* Fires after a translation was saved.
*
* @since 1.0.0
* @since 2.4.0 Added the `$original_before` parameter.
*
* @param GP_Translation $translation Translation that was saved.
* @param GP_Translation $translation Translation following the update.
* @param GP_Translation $translation_before Translation before the update.
*/
do_action( 'gp_translation_saved', $this );
do_action( 'gp_translation_saved', $this, $translation_before );

return true;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/testcases/tests_things/test_thing_original.php
Expand Up @@ -287,4 +287,25 @@ function test_return_types_of_count_by_project_id() {
$non_existent = GP::$original->count_by_project_id( $project->id, 'non_existent' );
$this->assertSame( 0, $non_existent );
}

public function test_previous_state_is_passed_to_saved_action() {
$project = $this->factory->project->create();
$original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'Before' ) );
$initial_original = clone $original;

$previous_original = null;
$closure = function( $original_after, $original_before ) use ( &$previous_original ) {
$previous_original = $original_before;
};

add_action( 'gp_original_saved', $closure, 10, 2 );

$original->save( array( 'singular' => 'After' ) );

remove_action( 'gp_original_saved', $closure );

$this->assertEquals( $initial_original, $previous_original );
$this->assertEquals( $previous_original->singular, 'Before' );
$this->assertEquals( $original->singular, 'After' );
}
}
19 changes: 19 additions & 0 deletions tests/phpunit/testcases/tests_things/test_thing_project.php
Expand Up @@ -288,4 +288,23 @@ function test_delete() {
$this->assertNotEquals( $pre_delete, $post_delete );
}

public function test_previous_state_is_passed_to_saved_action() {
$project = $this->factory->project->create( array( 'name' => 'Before' ) );
$initial_project = clone $project;

$previous_project = null;
$closure = function( $project_after, $project_before ) use ( &$previous_project ) {
$previous_project = $project_before;
};

add_action( 'gp_project_saved', $closure, 10, 2 );

$project->save( array( 'name' => 'After' ) );

remove_action( 'gp_project_saved', $closure );

$this->assertEquals( $initial_project, $previous_project );
$this->assertEquals( $previous_project->name, 'Before' );
$this->assertEquals( $project->name, 'After' );
}
}
20 changes: 20 additions & 0 deletions tests/phpunit/testcases/tests_things/test_thing_translation.php
Expand Up @@ -290,4 +290,24 @@ function test_cannot_approve_translation_without_approve_permission() {
$this->assertNotEquals( $user, $translation->user_id_last_modified );
}

public function test_previous_state_is_passed_to_saved_action() {
$set = $this->factory->translation_set->create_with_project_and_locale();
$translation = $this->factory->translation->create_with_original_for_translation_set( $set, array( 'translation_0' => 'Before' ) );
$initial_translation = clone $translation;

$previous_translation = null;
$closure = function( $translation_after, $translation_before ) use ( &$previous_translation ) {
$previous_translation = $translation_before;
};

add_action( 'gp_translation_saved', $closure, 10, 2 );

$translation->save( array( 'translation_0' => 'After' ) );

remove_action( 'gp_translation_saved', $closure );

$this->assertEquals( $initial_translation, $previous_translation );
$this->assertEquals( $previous_translation->translation_0, 'Before' );
$this->assertEquals( $translation->translation_0, 'After' );
}
}

0 comments on commit 92c2881

Please sign in to comment.