BP_Document::bp_document_pdf_previews( $ids,  $check_mime_type = false,  $document_id )

Does the actual PDF preview regenerate.

Description

Source

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

2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
static function bp_document_pdf_previews( $ids, $check_mime_type = false, $document_id ) {
 
    $cnt                    = $num_updates = $num_fails = $time = 0;
    $preview_attachment_id  = bp_document_get_meta( $document_id, 'preview_attachment_id', true );
    if ( $ids && ! $preview_attachment_id ) {
        $time = microtime( true );
        $cnt = count( $ids );
        self::bp_document_set_time_limit( max( $cnt * self::$per_pdf_secs, self::$min_time_limit ) );
 
        foreach ( $ids as $idx => $id ) {
            if ( $check_mime_type && 'application/pdf' !== get_post_mime_type( $id ) ) {
                continue;
            }
            $file = get_attached_file( $id );
            if ( false === $file || '' === $file ) {
                $num_fails++;
            } else {
                // Get current metadata if any.
                $old_value = get_metadata( 'post', $id, '_wp_attachment_metadata' );
                if ( $old_value && ( ! is_array( $old_value ) || 1 !== count( $old_value ) ) ) {
                    $old_value = null;
                }
                // Remove old intermediate thumbnails if any.
                if ( $old_value && ! empty( $old_value[0]['sizes'] ) && is_array( $old_value[0]['sizes'] ) ) {
                    $dirname = dirname( $file ) . '/';
                    foreach ( $old_value[0]['sizes'] as $sizeinfo ) {
                        // Check whether pre WP 4.7.3 lacking PDF marker and if so don't delete so as not to break links to thumbnails in content.
                        if ( false !== strpos( $sizeinfo['file'], '-pdf' ) ) {
                            @ unlink( $dirname . $sizeinfo['file'] );
                        }
                    }
                }
                if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
                    require_once ABSPATH . 'wp-admin' . '/includes/image.php';
                    require_once ABSPATH . 'wp-admin' . '/includes/file.php';
                    require_once ABSPATH . 'wp-admin' . '/includes/media.php';
                }
                // Generate new intermediate thumbnails.
                $meta = wp_generate_attachment_metadata( $id, $file );
                if ( ! $meta ) {
                    $num_fails++;
                } else {
                    // wp_update_attachment_metadata() returns false if nothing to update so check first.
                    if ( ( $old_value && $old_value[0] === $meta ) || false !== wp_update_attachment_metadata( $id, $meta ) ) {
                        $num_updates++;
                    } else {
                        $num_fails++;
                    }
                }
                if ( $meta ) {
                    $upload_dir     = wp_upload_dir();
                    $preview_folder = '';
                    $output_file     = wp_get_attachment_image_url( $id, 'full' );
                    $output_file_src = bp_document_scaled_image_path( $id );
 
                    if ( '' !== $output_file && '' !== basename( $output_file ) && strstr( $output_file, 'bb_documents/' ) ) {
                        add_filter( 'upload_dir', 'bp_document_upload_dir_script' );
                        $upload_dir = $upload_dir['basedir'];
 
                        // Create temp folder.
                        $upload_dir = $upload_dir . '/preview-image-folder-' . time();
 
                        // If folder not exists then create.
                        if ( ! is_dir( $upload_dir ) ) {
 
                            // Create temp folder.
                            wp_mkdir_p( $upload_dir );
                            chmod( $upload_dir, 0777 );
 
                            // Create given main parent folder.
                            $preview_folder = $upload_dir;
                            wp_mkdir_p( $preview_folder );
 
                            $file_name = basename( $output_file );
                            $extension_pos = strrpos($file_name, '.'); // find position of the last dot, so where the extension starts
                            $thumb = substr($file_name, 0, $extension_pos) . '_thumb' . substr($file_name, $extension_pos);
                            copy( $output_file_src, $preview_folder . '/' . $thumb );
 
                        }
 
                        $files = scandir( $preview_folder );
                        $firstFile = $preview_folder . '/' . $files[2];
                        bp_document_chmod_r( $preview_folder );
 
                        $image_data = file_get_contents( $firstFile );
 
                        $filename = basename( $output_file );
                        $upload_dir = wp_upload_dir();
 
                        if ( wp_mkdir_p( $upload_dir['path'] ) ) {
                            $file = $upload_dir['path'] . '/' . $filename;
                        } else {
                            $file = $upload_dir['basedir'] . '/' . $filename;
                        }
 
                        file_put_contents( $file, $image_data );
 
                        $wp_filetype = wp_check_filetype( $filename, null );
 
                        $attachment = array(
                            'post_mime_type' => $wp_filetype['type'],
                            'post_title'     => sanitize_file_name( $filename ),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
 
                        $preview_attachment_id = wp_insert_attachment( $attachment, $file );
                        if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
                            require_once ABSPATH . 'wp-admin' . '/includes/image.php';
                            require_once ABSPATH . 'wp-admin' . '/includes/file.php';
                            require_once ABSPATH . 'wp-admin' . '/includes/media.php';
                        }
                        $attach_data = wp_generate_attachment_metadata( $preview_attachment_id, $file );
                        wp_update_attachment_metadata( $preview_attachment_id, $attach_data );
                        update_post_meta( $id, 'document_preview_generated', 'yes' );
                        update_post_meta( $id, 'document_preview_attachment_id', $preview_attachment_id );
                        bp_document_update_meta( $document_id, 'preview_attachment_id', $preview_attachment_id );
                        BP_Document::bp_document_remove_temp_directory( $preview_folder );
                        remove_filter( 'upload_dir', 'bp_document_upload_dir_script' );
                    }
                }
            }
        }
        $time = round( microtime( true ) - $time, self::$timing_dec_places );
    }
    return array( $cnt, $num_updates, $num_fails, $time );
}

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.