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

Use return_format on ACFs within the media site. #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
40 changes: 26 additions & 14 deletions network-media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,24 @@ public function __construct() {
}

/**
* Fiters the return value when using field retrieval functions in Advanced Custom Fields.
* Transforms an Advanced Custom Field into the format specified return format.
*
* @param string $return_format The expected format to be returned - specified in the ACF field.
* @param mixed $value The field value.
* @return mixed The ACF field's value in the new format.
*/
private function transform_acf_to_return_format( $return_format, $value ) {
switch ( $return_format ) {
case 'url':
return wp_get_attachment_url( $value );
case 'array':
return acf_get_attachment( $value );
}
return $value;
}

/**
* Filters the return value when using field retrieval functions in Advanced Custom Fields.
*
* @param mixed $value The field value.
* @param int|string $post_id The post ID for this value.
Expand All @@ -442,19 +459,14 @@ public function __construct() {
public function filter_acf_attachment_load_value( $value, $post_id, array $field ) {
$image = $value;

if ( ! is_media_site() && ! is_admin() ) {
switch_to_media_site();

switch ( $field['return_format'] ) {
case 'url':
$image = wp_get_attachment_url( $value );
break;
case 'array':
$image = acf_get_attachment( $value );
break;
if ( ! is_admin() ) {
if ( ! is_media_site() ) {
switch_to_media_site();
$image = $this->transform_acf_to_return_format( $field['return_format'], $value );
restore_current_blog();
} else {
$image = $this->transform_acf_to_return_format( $field['return_format'], $value );
}

restore_current_blog();
}

$this->value = $image;
Expand All @@ -463,7 +475,7 @@ public function filter_acf_attachment_load_value( $value, $post_id, array $field
}

/**
* Fiters the optionally formatted value when using field retrieval functions in Advanced Custom Fields.
* Filters the optionally formatted value when using field retrieval functions in Advanced Custom Fields.
*
* @param mixed $value The field value.
* @param int|string $post_id The post ID for this value.
Expand Down