Skip to content

Commit 800a13f

Browse files
author
Ian Jenkins
committed
Add hooks on form element for integrators to hook into.
By adding these hooks it allows users to do such things as switch to blog/restore blog, which is particularly useful when using something like https://github.com/humanmade/network-media-library where all media will be stored on a single site in the network.
1 parent 5942c28 commit 800a13f

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

bin/install-wp-tests.sh

100644100755
File mode changed.

php/class-fieldmanager-media.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public function presave( $value, $current_value = array() ) {
152152
* @return string HTML string.
153153
*/
154154
public function form_element( $value = array() ) {
155+
do_action( 'fieldmanager_media_pre_form_element');
155156
if ( is_numeric( $value ) && $value > 0 ) {
156157
$attachment = get_post( $value );
157158
if ( strpos( $attachment->post_mime_type, 'image/' ) === 0 ) {
@@ -173,6 +174,8 @@ public function form_element( $value = array() ) {
173174
} else {
174175
$preview = '';
175176
}
177+
178+
do_action( 'fieldmanager_media_post_form_element');
176179
return sprintf(
177180
'<input type="button" class="fm-media-button button-secondary fm-incrementable" id="%1$s" value="%3$s" data-choose="%7$s" data-update="%8$s" data-preview-size="%6$s" data-mime-type="%9$s" %10$s />
178181
<input type="hidden" name="%2$s" value="%4$s" class="fm-element fm-media-id" />

tests/php/test-fieldmanager-media-field.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,46 @@ public function test_attributes() {
130130
$html = ob_get_clean();
131131
$this->assertRegExp( '/<input[^>]+type=[\'"]button[\'"][^>]+data-test=[\'"]' . $args['attributes']['data-test'] . '[\'"]/', $html );
132132
}
133+
134+
public function test_form_element_hooks() {
135+
136+
// Use a mock so we can verify the action function is called.
137+
$pre_mock = $this->build_callback_mock( 'pre_form_element_function' );
138+
139+
// Add the hook.
140+
add_action( 'fieldmanager_media_pre_form_element', array($pre_mock, 'pre_form_element_function'));
141+
142+
// Do the same for the post hook as above for the pre hook.
143+
$post_mock = $this->build_callback_mock( 'post_form_element_function' );
144+
add_action( 'fieldmanager_media_post_form_element', array($post_mock, 'post_form_element_function'));
145+
146+
$args = array(
147+
'name' => 'test_media',
148+
);
149+
150+
$attachment = self::factory()->attachment->create_object(
151+
'image.jpg',
152+
0,
153+
array(
154+
'post_mime_type' => 'image/jpeg',
155+
'post_type' => 'attachment',
156+
'post_status' => 'inherit',
157+
)
158+
);
159+
160+
$fm = new Fieldmanager_Media( $args );
161+
$fm->form_element( $attachment );
162+
}
163+
164+
private function build_callback_mock( $callback_name = 'callback_function', $expected_return = true ) {
165+
$mock = $this->getMockBuilder('stdClass')
166+
->setMethods(array( $callback_name ))
167+
->getMock();
168+
169+
$mock->expects( $this->once() )
170+
->method( $callback_name )
171+
->willReturn( $expected_return );
172+
173+
return $mock;
174+
}
133175
}

0 commit comments

Comments
 (0)