bp_attachments_create_item_type

Use the absolute path to an image to set an attachment type for a given item.

Description

bp_attachments_create_item_type( (string) $type = 'avatar', (array) $args = array() ); 

Parameters (2)

0. $type — Optional. (string) => 'avatar'
The attachment type to create (avatar or cover_image). Default: avatar.
1. $args — Optional. (array) => array()
The args.

Options

  • item_id (int) => 0

    The ID of the object (Required). Default: 0.

  • object (string) => ''

    The object type (eg: group, user, blog) (Required). Default: user..

  • component (string) => ''

    The component for the object (eg: groups, xprofile, blogs). Default: ''.

  • image (string) => ''

    The absolute path to the image (Required). Default: ''.

  • crop_w (int) => 0

    Crop width. Default: 0.

  • crop_h (int) => 0

    Crop height. Default: 0.

  • crop_x (int) => 0

    The horizontal starting point of the crop. Default: 0.

array(

    /**
     * The ID of the object (Required). Default: 0.
     *
     * @type int
     */
    'item_id' => 0,

    /**
     * The object type (eg: group, user, blog) (Required). Default: 'user'.
     *
     * @type string
     * @default ''
     */
    'object' => '',

    /**
     * The component for the object (eg: groups, xprofile, blogs). Default: ''.
     *
     * @type string
     * @default ''
     */
    'component' => '',

    /**
     * The absolute path to the image (Required). Default: ''.
     *
     * @type string
     * @default ''
     */
    'image' => '',

    /**
     * Crop width. Default: 0.
     *
     * @type int
     */
    'crop_w' => 0,

    /**
     * Crop height. Default: 0.
     *
     * @type int
     */
    'crop_h' => 0,

    /**
     * The horizontal starting point of the crop. Default: 0.
     *
     * @type int
     */
    'crop_x' => 0
);        


Usage

  1. if ( !function_exists( 'bp_attachments_create_item_type' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-attachments.php'; 
  3.  
  4. // The attachment type to create (avatar or cover_image). Default: avatar. 
  5. $type = 'avatar'; 
  6.  
  7. // The args. 
  8. $args = array( 
  9. 'item_id' => 0, 
  10. 'object' => '', 
  11. 'component' => '', 
  12. 'image' => '', 
  13. 'crop_w' => 0, 
  14. 'crop_h' => 0, 
  15. 'crop_x' => 0 
  16. ); 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = bp_attachments_create_item_type($type, $args); 
  20.  

Defined (1)

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

/bp-core/bp-core-attachments.php  
  1. function bp_attachments_create_item_type( $type = 'avatar', $args = array() ) { 
  2. if ( empty( $type ) || ( $type !== 'avatar' && $type !== 'cover_image' ) ) { 
  3. return false; 
  4.  
  5. $r = bp_parse_args( $args, array( 
  6. 'item_id' => 0,  
  7. 'object' => 'user',  
  8. 'component' => '',  
  9. 'image' => '',  
  10. 'crop_w' => 0,  
  11. 'crop_h' => 0,  
  12. 'crop_x' => 0,  
  13. 'crop_y' => 0 
  14. ), 'create_item_' . $type ); 
  15.  
  16. if ( empty( $r['item_id'] ) || empty( $r['object'] ) || ! file_exists( $r['image'] ) || ! @getimagesize( $r['image'] ) ) { 
  17. return false; 
  18.  
  19. // Make sure the file path is safe. 
  20. if ( 0 !== validate_file( $r['image'] ) ) { 
  21. return false; 
  22.  
  23. // Set the component if not already done. 
  24. if ( empty( $r['component'] ) ) { 
  25. if ( 'user' === $r['object'] ) { 
  26. $r['component'] = 'xprofile'; 
  27. } else { 
  28. $r['component'] = $r['object'] . 's'; 
  29.  
  30. // Get allowed mimes for the Attachment type and check the image one is. 
  31. $allowed_mimes = bp_attachments_get_allowed_mimes( $type ); 
  32. $is_allowed = wp_check_filetype( $r['image'], $allowed_mimes ); 
  33.  
  34. // It's not an image. 
  35. if ( ! $is_allowed['ext'] ) { 
  36. return false; 
  37.  
  38. // Init the Attachment data. 
  39. $attachment_data = array(); 
  40.  
  41. if ( 'avatar' === $type ) { 
  42. // Set crop width for the avatar if not given. 
  43. if ( empty( $r['crop_w'] ) ) { 
  44. $r['crop_w'] = bp_core_avatar_full_width(); 
  45.  
  46. // Set crop height for the avatar if not given. 
  47. if ( empty( $r['crop_h'] ) ) { 
  48. $r['crop_h'] = bp_core_avatar_full_height(); 
  49.  
  50. if ( is_callable( $r['component'] . '_avatar_upload_dir' ) ) { 
  51. $dir_args = array( $r['item_id'] ); 
  52.  
  53. // In case of xprofile, we need an extra argument. 
  54. if ( 'xprofile' === $r['component'] ) { 
  55. $dir_args = array( false, $r['item_id'] ); 
  56.  
  57. $attachment_data = call_user_func_array( $r['component'] . '_avatar_upload_dir', $dir_args ); 
  58. } elseif ( 'cover_image' === $type ) { 
  59. $attachment_data = bp_attachments_uploads_dir_get(); 
  60.  
  61. // The BP Attachments Uploads Dir is not set, stop. 
  62. if ( ! $attachment_data ) { 
  63. return false; 
  64.  
  65. // Default to members for xProfile. 
  66. $object_subdir = 'members'; 
  67.  
  68. if ( 'xprofile' !== $r['component'] ) { 
  69. $object_subdir = sanitize_key( $r['component'] ); 
  70.  
  71. // Set Subdir. 
  72. $attachment_data['subdir'] = $object_subdir . '/' . $r['item_id'] . '/cover-image'; 
  73.  
  74. // Set Path. 
  75. $attachment_data['path'] = trailingslashit( $attachment_data['basedir'] ) . $attachment_data['subdir']; 
  76.  
  77. if ( ! isset( $attachment_data['path'] ) || ! isset( $attachment_data['subdir'] ) ) { 
  78. return false; 
  79.  
  80. // It's not a regular upload, we may need to create some folders. 
  81. if ( ! is_dir( $attachment_data['path'] ) ) { 
  82. if ( ! wp_mkdir_p( $attachment_data['path'] ) ) { 
  83. return false; 
  84.  
  85. // Set the image name and path. 
  86. $image_file_name = wp_unique_filename( $attachment_data['path'], basename( $r['image'] ) ); 
  87. $image_file_path = $attachment_data['path'] . '/' . $image_file_name; 
  88.  
  89. // Copy the image file into the avatar dir. 
  90. if ( ! copy( $r['image'], $image_file_path ) ) { 
  91. return false; 
  92.  
  93. // Init the response. 
  94. $created = false; 
  95.  
  96. // It's an avatar, we need to crop it. 
  97. if ( 'avatar' === $type ) { 
  98. $created = bp_core_avatar_handle_crop( array( 
  99. 'object' => $r['object'],  
  100. 'avatar_dir' => trim( dirname( $attachment_data['subdir'] ), '/' ),  
  101. 'item_id' => (int) $r['item_id'],  
  102. 'original_file' => trailingslashit( $attachment_data['subdir'] ) . $image_file_name,  
  103. 'crop_w' => $r['crop_w'],  
  104. 'crop_h' => $r['crop_h'],  
  105. 'crop_x' => $r['crop_x'],  
  106. 'crop_y' => $r['crop_y'] 
  107. ) ); 
  108.  
  109. // It's a cover image we need to fit it to feature's dimensions. 
  110. } elseif ( 'cover_image' === $type ) { 
  111. 'file' => $image_file_path,  
  112. 'component' => $r['component'],  
  113. 'cover_image_dir' => $attachment_data['path'] 
  114. ) ); 
  115.  
  116. $created = ! empty( $cover_image['cover_file'] ); 
  117.  
  118. // Remove copied file if it fails. 
  119. if ( ! $created ) { 
  120. @unlink( $image_file_path ); 
  121.  
  122. // Return the response. 
  123. return $created;