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

Fixed save callback to call before save OR delete (rather than only on the save branch) #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bobbysmith007
Copy link

Also made the argument order match $custom_metadata_manager->
save_metadata_field for ease of calling the default
method. (Essentially I am trying to allow custom save permissions).

This seems like it could break existing code, but it also seems like a better use / definition of save_callback than previously.

This is my callback, does that seem like a reasonable use case for the save callback?
Is it safe to clear the save_callback then recall the default? It seems like this should work, but I dont yet have a grasp on how everything is fitting together.

function eit_readonly_save_cb($field_slug, $field, $object_type, $object_id, $value){
  global $custom_metadata_manager;
  if(current_user_can('edit_users')){
    $field->save_callback = null;
    $custom_metadata_manager->save_metadata_field( $field_slug, $field, $object_type, $object_id);
  }
}

Hope this helps,
Russ

the save branch)

Also made the argument order match $custom_metadata_manager->
save_metadata_field for ease of calling the default
method. (Essentially I am trying to allow custom save permissions).
@jkudish
Copy link
Contributor

jkudish commented Feb 2, 2012

We already have a required_cap paramater when registering a field, wouldn't that be enough for what you're trying to do?

@bobbysmith007
Copy link
Author

The problem I was having with required_cap, was that it completely removes the field. I started with a display_callback that removed the input tags in favor of a span when needed. This caused the data to always be removed when the user saved their own profile.

function eit_readonly_display_cb($slug, $field, $otype, $oid, $value){
  global $custom_metadata_manager;
  if(current_user_can('edit_users')){
    $field->display_callback = null;
    $custom_metadata_manager->_display_metadata_field($slug, $field, $otype, $oid);
  }
  else{
    $label = $field->label;
    if(is_array($value)) $value = implode(', ',$value);
    echo "<span class=\"readonly custom-metafield $slug\">$label: $value</span>";
  }
}

Its certainly possible I am missing an obviously better way to accomplish these tasks. Aside from that though, it seemed to make more sense to call the "save_callback" before saving/deleting ANY changes. Previously it would only call on the save branch (not when deleting values). It also seemed to make more sense to allow the callback to call the original function in certain situations, which made the public save function a more obvious place to call from.

@jkudish
Copy link
Contributor

jkudish commented Feb 2, 2012

Fair enough. I'll review your proposed changes in more details and act accordingly either later today or over the weekend.

Thanks for the input!

@ghost ghost assigned jkudish Feb 16, 2012
@jkudish jkudish removed their assignment Jan 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants