PHP Class WPAS_File_Upload

Author: Julien Liabeuf ([email protected])
Exibir arquivo Open project: awesome-support/awesome-support Class Usage Examples

Protected Properties

Property Type Description
$error_message Store the potential error messages.
$index
$instance object Instance of this class.
$parent_id
$post_id

Public Methods

Method Description
__construct ( )
add_form_enctype ( $post )
add_wrapper_class_admin ( array $classes, array $field ) : array Add a custom class to the upload field wrapper in the admin
attachment_endpoint ( ) : void Add a new rewrite endpoint.
attachment_query_var ( WP_Query $query ) : void Add the attachment query var to the $query object.
can_attach_files ( ) : boolean Check if the current user can attach a file.
create_upload_dir ( string $dir ) : boolean Create the upload directory for a ticket.
custom_mime_types ( array $mimes ) : array Add the custom file types to the WordPress whitelist
delete_attachments ( integer $post_id ) : void Delete post attachments.
filter_attachments_out ( array $clauses, WP_Query $wp_query ) : array Filter out tickets and ticket replies attachments
get_allowed_filetypes ( )
get_attachments ( integer $post_id ) : array Get post attachments.
get_instance ( ) : object Return an instance of this class.
has_attachments ( integer $post_id ) : boolean Check if post has attachments.
human_filesize ( integer $bytes, integer $decimals = 2 ) : string Human readable filesize.
individualize_files ( ) : boolean Individualize uploaded files.
limit_upload ( array $file ) : array Limit upload filetypes.
new_reply_attachment ( integer $reply_id ) : void Process upload on new reply creation.
new_reply_backend_attachment ( integer $reply_id ) : void Process upload on new reply creation.
new_ticket_attachment ( integer $ticket_id ) : void Process upload on new ticket creation.
process_upload ( ) : boolean Process the upload.
redirect_error ( string $location ) : string Change the redirection URL.
set_upload_dir ( array $upload ) : string Set upload directory.
show_attachments ( integer $post_id ) : void Show ticket attachments.
upload_field ( ) : void Add dropzone markup.
view_attachment ( ) : void Display the attachment.

Protected Methods

Method Description
protect_upload_dir ( string $dir ) : void Protects an upload directory by adding an .htaccess file

Method Details

__construct() public method

public __construct ( )

add_form_enctype() public method

public add_form_enctype ( $post )

add_wrapper_class_admin() public method

Add a custom class to the upload field wrapper in the admin
Since: 3.2.10
public add_wrapper_class_admin ( array $classes, array $field ) : array
$classes array Field wrapper classes
$field array Field parameters
return array

attachment_endpoint() public method

Add a new rewrite endpoint.
Since: 3.2.0
public attachment_endpoint ( ) : void
return void

attachment_query_var() public method

This is used as a fallback for when pretty permalinks are not enabled.
Since: 3.2.0
public attachment_query_var ( WP_Query $query ) : void
$query WP_Query The WordPress main query
return void

can_attach_files() public method

Check if the current user can attach a file.
Since: 3.0.0
public can_attach_files ( ) : boolean
return boolean True if the user has the capability, false otherwise

create_upload_dir() public method

Create the upload directory for a ticket.
Since: 3.1.7
public create_upload_dir ( string $dir ) : boolean
$dir string Upload directory
return boolean Whether or not the directory was created

custom_mime_types() public method

Add the custom file types to the WordPress whitelist
Since: 3.2
public custom_mime_types ( array $mimes ) : array
$mimes array Allowed mime types
return array Our custom mime types list

delete_attachments() public method

Delete all post attachments if a ticket is deleted.
Since: 3.0.0
public delete_attachments ( integer $post_id ) : void
$post_id integer ID of the post to be deleted
return void

filter_attachments_out() public method

Tickets attachments don't have their place in the media library. The library can quickly become a huge mess and it becomes impossible to work with actual post attachments.
Since: 3.3
public filter_attachments_out ( array $clauses, WP_Query $wp_query ) : array
$clauses array SQL query clauses
$wp_query WP_Query WordPress query object
return array

get_allowed_filetypes() public method

get_attachments() public method

Get the attachments for a specific ticket or reply.
Since: 3.0.0
public get_attachments ( integer $post_id ) : array
$post_id integer ID of the post to get attachment for
return array Array of attachments or empty array if no attachments are found

get_instance() public static method

Return an instance of this class.
Since: 1.0.0
public static get_instance ( ) : object
return object A single instance of this class.

has_attachments() public method

Check if a specific ticket or reply has attachments. This method is based on get_attachments() and only returns a boolean value based on the returned array being empty or not.
Since: 3.0.0
public has_attachments ( integer $post_id ) : boolean
$post_id integer ID of the post to get attachment for
return boolean True if the ticket has attachments, false otherwise

human_filesize() public method

Transform the file size into a readable format including the size unit.
public human_filesize ( integer $bytes, integer $decimals = 2 ) : string
$bytes integer Filesize in bytes
$decimals integer Number of decimals to show
return string Human readable filesize

individualize_files() public method

If multiple files were uploaded we need to separate each file in a separate array in the $_FILE array in order to let WordPress process them one by one.
Since: 3.0.0
public individualize_files ( ) : boolean
return boolean Whether or not files were individualized

limit_upload() public method

Gets the list of allowed file extensions from the plugin settings and compare the processed file. If the extension is not in the list we simply return an error message to prevent uploading it.
Since: 3.0.0
public limit_upload ( array $file ) : array
$file array Currently processed file details
return array File details with a possible error message

new_reply_attachment() public method

Process upload on new reply creation.
Since: 3.0.0
public new_reply_attachment ( integer $reply_id ) : void
$reply_id integer New reply ID
return void

new_reply_backend_attachment() public method

Process upload on new reply creation.
Since: 3.0.0
public new_reply_backend_attachment ( integer $reply_id ) : void
$reply_id integer New reply ID
return void

new_ticket_attachment() public method

Process upload on new ticket creation.
Since: 3.0.0
public new_ticket_attachment ( integer $ticket_id ) : void
$ticket_id integer New ticket ID
return void

process_upload() public method

We delegate the upload process to WordPress. Why reinvent the wheel? The only thing we do change is the upload path. For the rest it's all standard.
Since: 3.0.0
public process_upload ( ) : boolean
return boolean Whether or not the upload has been processed

protect_upload_dir() protected method

Protects an upload directory by adding an .htaccess file
Since: 3.1.7
protected protect_upload_dir ( string $dir ) : void
$dir string Upload directory
return void

redirect_error() public method

In case the upload fails we want to notify the user. We change the redirection URL and integrate a custom message encoded in base64 that will be interpreted by the notification class.
Since: 3.0.0
public redirect_error ( string $location ) : string
$location string Original redirection URL
return string New redirection URL

set_upload_dir() public method

Set a custom upload directory in order to properly separate WordPress uploads and tickets attachments.
Since: 3.0.0
public set_upload_dir ( array $upload ) : string
$upload array Initial upload directory
return string Upload directory

show_attachments() public method

Displays a ticket or reply attachments.
Since: 3.0.0
public show_attachments ( integer $post_id ) : void
$post_id integer ID of the post to get attachment for
return void

upload_field() public method

Add dropzone markup.
public upload_field ( ) : void
return void

view_attachment() public method

Uses the new rewrite endpoint to get an attachment ID and display the attachment if the currently logged in user has the authorization to.
Since: 3.2.0
public view_attachment ( ) : void
return void

Property Details

$error_message protected_oe property

Store the potential error messages.
protected $error_message

$index protected_oe property

protected $index

$instance protected_oe static_oe property

Instance of this class.
Since: 1.0.0
protected static object $instance
return object

$parent_id protected_oe property

protected $parent_id

$post_id protected_oe property

protected $post_id