[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-attachment-cover-image.php (source)

   1  <?php
   2  /**
   3   * Core Cover Image attachment class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 2.4.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * BP Attachment Cover Image class.
  15   *
  16   * Extends BP Attachment to manage the cover images uploads.
  17   *
  18   * @since 2.4.0
  19   */
  20  class BP_Attachment_Cover_Image extends BP_Attachment {
  21      /**
  22       * The constuctor.
  23       *
  24       * @since 2.4.0
  25       */
  26  	public function __construct() {
  27          // Allowed cover image types & upload size.
  28          $allowed_types        = bp_attachments_get_allowed_types( 'cover_image' );
  29          $max_upload_file_size = bp_attachments_get_max_upload_file_size( 'cover_image' );
  30  
  31          parent::__construct( array(
  32              'action'                => 'bp_cover_image_upload',
  33              'file_input'            => 'file',
  34              'original_max_filesize' => $max_upload_file_size,
  35              'base_dir'              => bp_attachments_uploads_dir_get( 'dir' ),
  36              'required_wp_files'     => array( 'file', 'image' ),
  37  
  38              // Specific errors for cover images.
  39              'upload_error_strings'  => array(
  40                  11  => sprintf( __( 'That image is too big. Please upload one smaller than %s', 'buddypress' ), size_format( $max_upload_file_size ) ),
  41                  12  => sprintf( _n( 'Please upload only this file type: %s.', 'Please upload only these file types: %s.', count( $allowed_types ), 'buddypress' ), self::get_cover_image_types( $allowed_types ) ),
  42              ),
  43          ) );
  44      }
  45  
  46      /**
  47       * Gets the available cover image types.
  48       *
  49       * @since 2.4.0
  50       *
  51       * @param array $allowed_types Array of allowed cover image types.
  52       * @return string $value Comma-separated list of allowed cover image types.
  53       */
  54  	public static function get_cover_image_types( $allowed_types = array() ) {
  55          $types = array_map( 'strtoupper', $allowed_types );
  56          $comma = _x( ',', 'cover image types separator', 'buddypress' );
  57          return join( $comma . ' ', $types );
  58      }
  59  
  60      /**
  61       * Cover image specific rules.
  62       *
  63       * Adds an error if the cover image size or type don't match BuddyPress needs.
  64       * The error code is the index of $upload_error_strings.
  65       *
  66       * @since 2.4.0
  67       *
  68       * @param array $file The temporary file attributes (before it has been moved).
  69       * @return array $file The file with extra errors if needed.
  70       */
  71  	public function validate_upload( $file = array() ) {
  72          // Bail if already an error.
  73          if ( ! empty( $file['error'] ) ) {
  74              return $file;
  75          }
  76  
  77          // File size is too big.
  78          if ( $file['size'] > $this->original_max_filesize ) {
  79              $file['error'] = 11;
  80  
  81          // File is of invalid type.
  82          } elseif ( ! bp_attachments_check_filetype( $file['tmp_name'], $file['name'], bp_attachments_get_allowed_mimes( 'cover_image' ) ) ) {
  83              $file['error'] = 12;
  84          }
  85  
  86          // Return with error code attached.
  87          return $file;
  88      }
  89  
  90      /**
  91       * Set the directory when uploading a file.
  92       *
  93       * @since 2.4.0
  94       *
  95       * @param array $upload_dir The original Uploads dir.
  96       * @return array $value Upload data (path, url, basedir...).
  97       */
  98  	public function upload_dir_filter( $upload_dir = array() ) {
  99          return bp_attachments_cover_image_upload_dir();
 100      }
 101  
 102      /**
 103       * Adjust the cover image to fit with advised width & height.
 104       *
 105       * @since 2.4.0
 106       *
 107       * @param string $file       The absolute path to the file.
 108       * @param array  $dimensions Array of dimensions for the cover image.
 109       * @return mixed
 110       */
 111  	public function fit( $file = '', $dimensions = array() ) {
 112          if ( empty( $dimensions['width'] ) || empty( $dimensions['height'] ) ) {
 113              return false;
 114          }
 115  
 116          // Get image size.
 117          $cover_data = parent::get_image_data( $file );
 118  
 119          // Init the edit args.
 120          $edit_args = array();
 121  
 122          // Do we need to resize the image?
 123          if ( ( isset( $cover_data['width'] ) && $cover_data['width'] > $dimensions['width'] ) ||
 124          ( isset( $cover_data['height'] ) && $cover_data['height'] > $dimensions['height'] ) ) {
 125              $edit_args = array(
 126                  'max_w' => $dimensions['width'],
 127                  'max_h' => $dimensions['height'],
 128                  'crop'  => true,
 129              );
 130          }
 131  
 132          // Do we need to rotate the image?
 133          $angles = array(
 134              3 => 180,
 135              6 => -90,
 136              8 =>  90,
 137          );
 138  
 139          if ( isset( $cover_data['meta']['orientation'] ) && isset( $angles[ $cover_data['meta']['orientation'] ] ) ) {
 140              $edit_args['rotate'] = $angles[ $cover_data['meta']['orientation'] ];
 141          }
 142  
 143          // No need to edit the avatar, original file will be used.
 144          if ( empty( $edit_args ) ) {
 145              return false;
 146  
 147          // Add the file to the edit arguments.
 148          } else {
 149              $edit_args = array_merge( $edit_args, array( 'file' => $file, 'save' => false ) );
 150          }
 151  
 152          // Get the editor so that we can use a specific save method.
 153          $editor = parent::edit_image( 'cover_image', $edit_args );
 154  
 155          if ( is_wp_error( $editor ) )  {
 156              return $editor;
 157          } elseif ( ! is_a( $editor, 'WP_Image_Editor' ) ) {
 158              return false;
 159          }
 160  
 161          // Save the new image file.
 162          return $editor->save( $this->generate_filename( $file ) );
 163      }
 164  
 165      /**
 166       * Generate a filename for the cover image.
 167       *
 168       * @since 2.4.0
 169       *
 170       * @param string $file The absolute path to the file.
 171       * @return false|string $value The absolute path to the new file name.
 172       */
 173  	public function generate_filename( $file = '' ) {
 174          if ( empty( $file ) || ! file_exists( $file ) ) {
 175              return false;
 176          }
 177  
 178          $info = pathinfo( $file );
 179          $ext  = strtolower( $info['extension'] );
 180          $name = wp_unique_filename( $info['dirname'], uniqid() . "-bp-cover-image.$ext" );
 181  
 182          return trailingslashit( $info['dirname'] ) . $name;
 183      }
 184  
 185      /**
 186       * Build script datas for the Uploader UI.
 187       *
 188       * @since 2.4.0
 189       *
 190       * @return array The javascript localization data
 191       */
 192  	public function script_data() {
 193          // Get default script data.
 194          $script_data = parent::script_data();
 195  
 196          if ( bp_is_user() ) {
 197              $item_id = bp_displayed_user_id();
 198  
 199              $script_data['bp_params'] = array(
 200                  'object'          => 'user',
 201                  'item_id'         => $item_id,
 202                  'has_cover_image' => bp_attachments_get_user_has_cover_image( $item_id ),
 203                  'nonces'  => array(
 204                      'remove' => wp_create_nonce( 'bp_delete_cover_image' ),
 205                  ),
 206              );
 207  
 208              // Set feedback messages.
 209              $script_data['feedback_messages'] = array(
 210                  1 => __( 'Your new cover image was uploaded successfully.', 'buddypress' ),
 211                  2 => __( 'There was a problem deleting your cover image. Please try again.', 'buddypress' ),
 212                  3 => __( 'Your cover image was deleted successfully!', 'buddypress' ),
 213              );
 214          } elseif ( bp_is_group() ) {
 215              $item_id = bp_get_current_group_id();
 216  
 217              $script_data['bp_params'] = array(
 218                  'object'          => 'group',
 219                  'item_id'         => bp_get_current_group_id(),
 220                  'has_cover_image' => bp_attachments_get_group_has_cover_image( $item_id ),
 221                  'nonces'  => array(
 222                      'remove' => wp_create_nonce( 'bp_delete_cover_image' ),
 223                  ),
 224              );
 225  
 226              // Set feedback messages.
 227              $script_data['feedback_messages'] = array(
 228                  1 => __( 'The group cover image was uploaded successfully.', 'buddypress' ),
 229                  2 => __( 'There was a problem deleting the group cover image. Please try again.', 'buddypress' ),
 230                  3 => __( 'The group cover image was deleted successfully!', 'buddypress' ),
 231              );
 232          } else {
 233  
 234              /**
 235               * Filters the cover image params to include specific BuddyPress params for your object.
 236               * e.g. Cover image for blogs single item.
 237               *
 238               * @since 2.4.0
 239               *
 240               * @param array $value The cover image specific BuddyPress parameters.
 241               */
 242              $script_data['bp_params'] = apply_filters( 'bp_attachment_cover_image_params', array() );
 243          }
 244  
 245          // Include our specific js & css.
 246          $script_data['extra_js']  = array( 'bp-cover-image' );
 247          $script_data['extra_css'] = array( 'bp-avatar' );
 248  
 249          /**
 250           * Filters the cover image script data.
 251           *
 252           * @since 2.4.0
 253           *
 254           * @param array $script_data Array of data for the cover image.
 255           */
 256          return apply_filters( 'bp_attachments_cover_image_script_data', $script_data );
 257      }
 258  }


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1