A_Attachment_DataMapper

Modifies a custom post datamapper to use the WordPress built-in 'attachment' custom post type, as used by the Media Library.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/nextgen_data/package.module.nextgen_data.php  
  1. class A_Attachment_DataMapper extends Mixin 
  2. function initialize() 
  3. $this->object->_object_name = 'attachment'; 
  4. /** 
  5. * Saves the entity using the wp_insert_attachment function 
  6. * instead of the wp_insert_post 
  7. * @param stdObject $entity 
  8. */ 
  9. function _save_entity($entity) 
  10. $post = $this->object->_convert_entity_to_post($entity); 
  11. $filename = property_exists($entity, 'filename') ? $entity->filename : FALSE; 
  12. $primary_key = $this->object->get_primary_key_column(); 
  13. if ($post_id = $attachment_id = wp_insert_attachment($post, $filename)) { 
  14. $new_entity = $this->object->find($post_id); 
  15. foreach ($new_entity as $key => $value) { 
  16. $entity->{$key} = $value; 
  17. // Merge meta data with WordPress Attachment Meta Data 
  18. if (property_exists($entity, 'meta_data')) { 
  19. $meta_data = wp_get_attachment_metadata($attachment_id); 
  20. if (isset($meta_data['image_meta'])) { 
  21. $entity->meta_data = array_merge_recursive($meta_data['image_meta'], $entity->meta_data); 
  22. wp_update_attachment_metadata($attachment_id, $entity->meta_data); 
  23. // Save properties are post meta as well 
  24. $this->object->_flush_and_update_postmeta($attachment_id, $entity instanceof stdClass ? $entity : $entity->get_entity(), array('_wp_attached_file', '_wp_attachment_metadata', '_mapper')); 
  25. $entity->id_field = $primary_key; 
  26. return $attachment_id; 
  27. function select($fields = '*') 
  28. $ret = $this->call_parent('select', $fields); 
  29. $this->object->_query_args['datamapper_attachment'] = true; 
  30. return $ret;