BP_REST_Document_Endpoint::document_get_prepare_response( BP_Document $document, array $request )

Prepare object response for the document/folder.

Description

Parameters

$document

(Required) Document object.

$request

(Required) Request paramaters.

Return

(array)

Source

File: bp-document/classes/class-bp-rest-document-endpoint.php

	public function document_get_prepare_response( $document, $request ) {
		$data = array(
			'id'                    => $document->id,
			'blog_id'               => $document->blog_id,
			'attachment_id'         => ( isset( $document->attachment_id ) ? $document->attachment_id : 0 ),
			'user_id'               => $document->user_id,
			'title'                 => $document->title,
			'type'                  => ( empty( $document->attachment_id ) ? 'folder' : 'document' ),
			'folder_id'             => $document->parent,
			'group_id'              => $document->group_id,
			'activity_id'           => ( isset( $document->activity_id ) ? $document->activity_id : 0 ),
			'privacy'               => $document->privacy,
			'menu_order'            => ( isset( $document->menu_order ) ? $document->menu_order : 0 ),
			'date_created'          => $document->date_created,
			'date_modified'         => $document->date_modified,
			'group_name'            => $document->group_name,
			'group_status'          => ( bp_is_active( 'groups' ) && ! empty( $document->group_id ) ? bp_get_group_status( groups_get_group( $document->group_id ) ) : '' ),
			'visibility'            => $document->visibility,
			'download_url'          => '',
			'extension'             => '',
			'extension_description' => '',
			'svg_icon'              => '',
			'filename'              => '',
			'size'                  => '',
			'msg_preview'           => '',
			'attachment_data'       => ( isset( $document->attachment_data ) ? $document->attachment_data : array() ),
			'user_nicename'         => $document->user_nicename,
			'user_login'            => $document->user_login,
			'display_name'          => $document->display_name,
			'user_permissions'      => $this->get_document_current_user_permissions( $document, $request ),
		);

		if ( ! empty( $document->attachment_id ) ) {
			$data['download_url'] = bp_document_download_link( $document->attachment_id, $document->id );
			$data['extension']    = bp_document_extension( $document->attachment_id );
			$data['svg_icon']     = bp_document_svg_icon( $data['extension'], $document->attachment_id, 'svg' );
			$data['filename']     = basename( get_attached_file( $document->attachment_id ) );
			$data['size']         = bp_document_size_format( filesize( get_attached_file( $document->attachment_id ) ) );

			$extension_lists = bp_document_extensions_list();
			if ( ! empty( $extension_lists ) && ! empty( $data['extension'] ) ) {
				$extension_lists = array_column( $extension_lists, 'description', 'extension' );
				$extension_name  = '.' . $data['extension'];
				if ( ! empty( $extension_lists ) && ! empty( $data['extension'] ) && array_key_exists( $extension_name, $extension_lists ) ) {
					$data['extension_description'] = esc_html( $extension_lists[ $extension_name ] );
				}
			}

			$output = '';
			ob_start();

			if ( in_array( $data['extension'], bp_get_document_preview_music_extensions(), true ) ) {
				$audio_url = bp_document_get_preview_audio_url( $document->id, $data['extension'], $document->attachment_id );

				echo '<div class="document-audio-wrap">' .
					'<audio controls controlsList="nodownload">' .
						'<source src="' . esc_url_raw( $audio_url ) . '" type="audio/mpeg">' .
						esc_html__( 'Your browser does not support the audio element.', 'buddyboss' ) .
					'</audio>' .
				'</div>';

			}
			$attachment_url = bp_document_get_preview_image_url( $document->id, $data['extension'], $document->attachment_id );
			if ( $attachment_url ) {
				echo '<div class="document-preview-wrap">' .
					'<img src="' . esc_url_raw( $attachment_url ) . '" alt="" />' .
				'</div>';
			}
			$sizes = is_file( get_attached_file( $document->attachment_id ) ) ? get_attached_file( $document->attachment_id ) : 0;
			if ( $sizes && filesize( $sizes ) / 1e+6 < 2 ) {
				if ( in_array( $data['extension'], bp_get_document_preview_code_extensions(), true ) ) {
					$data_temp = bp_document_get_preview_text_from_attachment( $document->attachment_id );
					$file_data = $data_temp['text'];
					$more_text = $data_temp['more_text'];

					echo '<div class="document-text-wrap">' .
						'<div class="document-text" data-extension="' . esc_attr( $data['extension'] ) . '">' .
							'<textarea class="document-text-file-data-hidden" style="display: none;">' . wp_kses_post( $file_data ) . '</textarea>' .
						'</div>' .
						'<div class="document-expand">' .
							'<a href="#" class="document-expand-anchor"><i class="bb-icon-plus document-icon-plus"></i> ' . esc_html__( 'Click to expand', 'buddyboss' ) . '</a>' .
						'</div>' .
					'</div>';

					if ( true === $more_text ) {
						printf(
						/* translators: %s: download string */
							'<div class="more_text_view">%s</div>',
							sprintf(
							/* translators: %s: download url */
								wp_kses_post( 'This file was truncated for preview. Please <a href="%s">download</a> to view the full file.', 'buddyboss' ),
								esc_url( $data['download_url'] )
							)
						);
					}
				}
			}

			$output .= ob_get_clean();

			$data['msg_preview'] = $output;
		} else {
			$data['svg_icon']     = bp_document_svg_icon( 'folder', '', 'svg' );
			$data['download_url'] = bp_document_folder_download_link( $document->id );
		}

		return $data;
	}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.