__( 'Select Files', 'meta-box' ), ) ); } /** * Add actions * * @return void */ static function add_actions() { parent::add_actions(); // Attach images via Ajax add_action( 'wp_ajax_rwmb_attach_file', array( __CLASS__, 'wp_ajax_attach_file' ) ); add_action( 'print_media_templates', array( __CLASS__, 'print_templates' ) ); } static function wp_ajax_attach_file() { $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0; $field_id = isset( $_POST['field_id'] ) ? $_POST['field_id'] : 0; $attachment_ids = isset( $_POST['attachment_ids'] ) ? (array) $_POST['attachment_ids'] : array(); check_ajax_referer( "rwmb-attach-file_{$field_id}" ); foreach ( $attachment_ids as $attachment_id ) { add_post_meta( $post_id, $field_id, $attachment_id, false ); } wp_send_json_success(); } /** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html( $meta, $field ) { $i18n_title = apply_filters( 'rwmb_file_advanced_select_string', _x( 'Select or Upload Files', 'file upload', 'meta-box' ), $field ); $attach_nonce = wp_create_nonce( "rwmb-attach-file_{$field['id']}" ); // Uploaded files $html = self::get_uploaded_files( $meta, $field ); // Show form upload $classes = array( 'button', 'rwmb-file-advanced-upload', 'hide-if-no-js', 'new-files' ); if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] ) $classes[] = 'hidden'; $classes = implode( ' ', $classes ); $html .= "{$i18n_title}"; return $html; } /** * Get field value * It's the combination of new (uploaded) images and saved images * * @param array $new * @param array $old * @param int $post_id * @param array $field * * @return array|mixed */ static function value( $new, $old, $post_id, $field ) { $new = (array) $new; return array_unique( array_merge( $old, $new ) ); } static function print_templates() { $i18n_delete = apply_filters( 'rwmb_file_delete_string', _x( 'Delete', 'file upload', 'meta-box' ) ); $i18n_edit = apply_filters( 'rwmb_file_edit_string', _x( 'Edit', 'file upload', 'meta-box' ) ); ?>