actions
3 years ago
addons
3 years ago
admin
3 years ago
blocks
3 years ago
classes
3 years ago
compatibility
3 years ago
db-queries
3 years ago
dev-mode
3 years ago
exceptions
3 years ago
form-actions
3 years ago
form-messages
3 years ago
form-patterns
3 years ago
form-response
3 years ago
gateways
3 years ago
generators
3 years ago
integrations
3 years ago
migrations
3 years ago
presets
3 years ago
request
3 years ago
rest-api
3 years ago
shortcodes
3 years ago
widgets
3 years ago
wp-cli
3 years ago
autoloader.php
3 years ago
file-upload.php
3 years ago
form-break.php
3 years ago
form-handler.php
3 years ago
form-manager.php
3 years ago
live-form.php
3 years ago
plugin.php
3 years ago
post-type.php
3 years ago
file-upload.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder; |
| 4 | |
| 5 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 6 | use Jet_Form_Builder\Classes\Tools; |
| 7 | |
| 8 | // If this file is called directly, abort. |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * @method static File_Upload instance() |
| 15 | * |
| 16 | * Class description |
| 17 | * |
| 18 | * @package package_name |
| 19 | * @author Cherry Team |
| 20 | * @license GPL-2.0+ |
| 21 | */ |
| 22 | class File_Upload { |
| 23 | |
| 24 | use Instance_Trait; |
| 25 | |
| 26 | private $rendered_scripts = false; |
| 27 | |
| 28 | public function __construct() { |
| 29 | } |
| 30 | |
| 31 | |
| 32 | public function get_loader() { |
| 33 | return '<div class="jet-form-builder-file-upload__loader">' . apply_filters( |
| 34 | 'jet-form-builder/file-upload/loader', |
| 35 | '<svg xmlns="http://www.w3.org/2000/svg" width="38" height="38" viewBox="0 0 38 38" stroke="#fff"><g fill="none" fill-rule="evenodd"><g transform="translate(1 1)" stroke-width="2"><circle stroke-opacity=".5" cx="18" cy="18" r="18"/><path d="M36 18c0-9.94-8.06-18-18-18" transform="rotate(137.826 18 18)"><animateTransform attributeName="transform" type="rotate" from="0 18 18" to="360 18 18" dur="1s" repeatCount="indefinite"/></path></g></g></svg>' |
| 36 | ) . '</div>'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Resturns max upload size based on field arguments |
| 41 | * |
| 42 | * @param array $args [description] |
| 43 | * |
| 44 | * @return [type] [description] |
| 45 | */ |
| 46 | public function get_max_size_for_field( $args = array() ) { |
| 47 | |
| 48 | $max_size = wp_max_upload_size(); |
| 49 | $field_max_size = $max_size; |
| 50 | |
| 51 | if ( ! empty( $args['max_size'] ) ) { |
| 52 | |
| 53 | $field_max_size = intval( floatval( $args['max_size'] ) * MB_IN_BYTES ); |
| 54 | |
| 55 | if ( $field_max_size > $max_size ) { |
| 56 | $field_max_size = $max_size; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return $field_max_size; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Register form-specific assets |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function enqueue_scripts() { |
| 70 | wp_enqueue_script( 'jet-form-builder-sortable' ); |
| 71 | wp_enqueue_script( 'jet-form-builder-file-upload' ); |
| 72 | |
| 73 | $messages = wp_json_encode( jet_form_builder()->msg_router->get_manager()->get_messages() ); |
| 74 | $form_id = (int) Live_Form::instance()->form_id; |
| 75 | |
| 76 | wp_add_inline_script( |
| 77 | 'jet-form-builder-file-upload', |
| 78 | " |
| 79 | window.JetFormBuilderFileUploadConfig = window.JetFormBuilderFileUploadConfig || {}; |
| 80 | window.JetFormBuilderFileUploadConfig.errors = window.JetFormBuilderFileUploadConfig.errors || {}; |
| 81 | |
| 82 | window.JetFormBuilderFileUploadConfig.errors[ $form_id ] = $messages; |
| 83 | " |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | public function ensure_media_js( $content = '' ) { |
| 88 | if ( $this->rendered_scripts ) { |
| 89 | return $content; |
| 90 | } |
| 91 | $this->rendered_scripts = true; |
| 92 | |
| 93 | ob_start(); |
| 94 | jet_form_builder()->blocks->register_form_scripts(); |
| 95 | |
| 96 | $this->enqueue_scripts(); |
| 97 | wp_scripts()->print_scripts( 'jet-form-builder-file-upload' ); |
| 98 | |
| 99 | return $content . ob_get_clean(); |
| 100 | } |
| 101 | |
| 102 | } |
| 103 |