diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh old mode 100644 new mode 100755 diff --git a/php/class-fieldmanager-media.php b/php/class-fieldmanager-media.php index 77e7744afe..038d736942 100644 --- a/php/class-fieldmanager-media.php +++ b/php/class-fieldmanager-media.php @@ -152,6 +152,7 @@ public function presave( $value, $current_value = array() ) { * @return string HTML string. */ public function form_element( $value = array() ) { + do_action( 'fieldmanager_media_pre_form_element' ); if ( is_numeric( $value ) && $value > 0 ) { $attachment = get_post( $value ); if ( strpos( $attachment->post_mime_type, 'image/' ) === 0 ) { @@ -173,6 +174,8 @@ public function form_element( $value = array() ) { } else { $preview = ''; } + + do_action( 'fieldmanager_media_post_form_element' ); return sprintf( ' diff --git a/tests/php/test-fieldmanager-media-field.php b/tests/php/test-fieldmanager-media-field.php index 6f2c1a12f6..fe6bf3e898 100644 --- a/tests/php/test-fieldmanager-media-field.php +++ b/tests/php/test-fieldmanager-media-field.php @@ -130,4 +130,46 @@ public function test_attributes() { $html = ob_get_clean(); $this->assertRegExp( '/]+type=[\'"]button[\'"][^>]+data-test=[\'"]' . $args['attributes']['data-test'] . '[\'"]/', $html ); } + + public function test_form_element_hooks() { + + // Use a mock so we can verify the action function is called. + $pre_mock = $this->build_callback_mock( 'pre_form_element_function' ); + + // Add the hook. + add_action( 'fieldmanager_media_pre_form_element', array($pre_mock, 'pre_form_element_function')); + + // Do the same for the post hook as above for the pre hook. + $post_mock = $this->build_callback_mock( 'post_form_element_function' ); + add_action( 'fieldmanager_media_post_form_element', array($post_mock, 'post_form_element_function')); + + $args = array( + 'name' => 'test_media', + ); + + $attachment = self::factory()->attachment->create_object( + 'image.jpg', + 0, + array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment', + 'post_status' => 'inherit', + ) + ); + + $fm = new Fieldmanager_Media( $args ); + $fm->form_element( $attachment ); + } + + private function build_callback_mock( $callback_name = 'callback_function', $expected_return = true ) { + $mock = $this->getMockBuilder('stdClass') + ->setMethods(array( $callback_name )) + ->getMock(); + + $mock->expects( $this->once() ) + ->method( $callback_name ) + ->willReturn( $expected_return ); + + return $mock; + } }