From 4fc64e85a6435f211fcdc7c991dada1c9554d377 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 27 Jun 2018 17:47:30 +0100 Subject: [PATCH] Create placeholder image on install --- includes/class-wc-install.php | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index e07100c8262..98bfbb7167e 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -1087,6 +1087,54 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( } } } + + // Create attachment for placeholders. + self::create_placeholder_image(); + } + + /** + * Create a placeholder image in the media library. + * + * @since 3.5.0 + */ + private static function create_placeholder_image() { + $placeholder_image_id = get_option( 'woocommerce_placeholder_image_id', 0 ); + + if ( $placeholder_image_id && wp_attachment_is_image( $placeholder_image_id ) ) { + return; + } + + $upload_dir = wp_upload_dir(); + $source = WC()->plugin_path() . '/assets/images/placeholder.png'; + $filename = $upload_dir['basedir'] . '/woocommerce-placeholder.png'; + + if ( ! file_exists( $filename ) ) { + copy( $source, $filename ); // @codingStandardsIgnoreLine. + } + + if ( ! file_exists( $filename ) ) { + update_option( 'woocommerce_placeholder_image_id', 0 ); + return; + } + + $filetype = wp_check_filetype( basename( $filename ), null ); + $attachment = array( + 'guid' => $upload_dir['url'] . '/' . basename( $filename ), + 'post_mime_type' => $filetype['type'], + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), + 'post_content' => '', + 'post_status' => 'inherit', + ); + $attach_id = wp_insert_attachment( $attachment, $filename ); + + update_option( 'woocommerce_placeholder_image_id', $attach_id ); + + // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. + require_once ABSPATH . 'wp-admin/includes/image.php'; + + // Generate the metadata for the attachment, and update the database record. + $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); + wp_update_attachment_metadata( $attach_id, $attach_data ); } /**