예제 #1
0
 public function checkProductImages()
 {
     // get all listings
     $listings = WPLE_ListingQueryHelper::getAll();
     $found_images = array();
     $found_products = array();
     // allow WP to upscale images
     if (isset($_REQUEST['resize_images'])) {
         add_filter('image_resize_dimensions', array($this, 'filter_image_resize_dimensions'), 10, 6);
     }
     // process published listings
     foreach ($listings as $item) {
         // get featured image id
         $post_id = $item['post_id'];
         $attachment_id = get_post_thumbnail_id($post_id);
         if (!$attachment_id) {
             continue;
         }
         // $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' );
         // $img_width  = $image_attributes[1];
         // $img_height = $image_attributes[2];
         // get attachment meta data
         $meta = wp_get_attachment_metadata($attachment_id);
         if (empty($meta)) {
             continue;
         }
         // echo "<pre>";print_r($meta);echo"</pre>";#die();
         // check if at least one side is 500px or longer
         if ($meta['width'] >= 500 || $meta['height'] >= 500) {
             continue;
         }
         // echo "<pre>";print_r($attachment_id);echo"</pre>";#die();
         // resize image
         if (isset($_REQUEST['resize_images'])) {
             $size = $this->upscaleImage($meta['file']);
             if ($size) {
                 // update attachment meta sizes
                 // echo "<pre>new size: ";print_r($size);echo"</pre>";#die();
                 $meta['width'] = $size['width'];
                 $meta['height'] = $size['height'];
                 // echo wp_update_attachment_metadata( $post_id, $meta );
                 update_post_meta($attachment_id, '_wp_attachment_metadata', $meta);
                 // clear EPS cache for listing item
                 ListingsModel::updateListing($item['id'], array('eps' => NULL));
                 $this->showMessage(sprintf('Resized image <code>%s</code> to %s x %s.', $meta['file'], $meta['width'], $meta['height']));
                 continue;
             }
         }
         // get image url
         $image_attributes = wp_get_attachment_image_src($attachment_id, 'full');
         $meta['url'] = $image_attributes[0];
         $meta['post_id'] = $post_id;
         $meta['ebay_id'] = $item['ebay_id'];
         $meta['ViewItemURL'] = $item['ViewItemURL'];
         // add to list of found images
         $found_images[$attachment_id] = $meta;
     }
     // echo "<pre>";print_r($found_images);echo"</pre>";
     // return if empty
     if (empty($found_images)) {
         $this->showMessage('All images are okay.');
         return;
     }
     $msg = '<p>';
     $msg .= 'Warning: Some product images do not meet the requirements.';
     $msg .= '</p>';
     // table header
     $msg .= '<table style="width:100%">';
     $msg .= "<tr>";
     $msg .= "<th style='text-align:left'>Width</th>";
     $msg .= "<th style='text-align:left'>Height</th>";
     $msg .= "<th style='text-align:left'>File</th>";
     $msg .= "<th style='text-align:left'>Product</th>";
     $msg .= "<th style='text-align:left'>eBay ID</th>";
     $msg .= "<th style='text-align:left'>ID</th>";
     $msg .= "</tr>";
     // table rows
     foreach ($found_images as $attachment_id => $item) {
         // get column data
         $post_id = $item['post_id'];
         $ebay_id = $item['ebay_id'];
         $width = $item['width'];
         $height = $item['height'];
         $file = $item['file'];
         $url = $item['url'];
         $title = get_the_title($item['post_id']);
         // build links
         $ebay_url = $item['ViewItemURL'] ? $item['ViewItemURL'] : ($ebay_url = 'http://www.ebay.com/itm/' . $ebay_id);
         $ebay_link = '<a href="' . $ebay_url . '" target="_blank">' . $ebay_id . '</a>';
         $edit_link = '<a href="post.php?action=edit&post=' . $post_id . '" target="_blank">' . $title . '</a>';
         $file_link = '<a href="' . $url . '" target="_blank">' . $file . '</a>';
         // build table row
         $msg .= "<tr>";
         $msg .= "<td>{$width}</td>";
         $msg .= "<td>{$height}</td>";
         $msg .= "<td>{$file_link}</td>";
         $msg .= "<td>{$edit_link} (ID {$post_id})</td>";
         $msg .= "<td>{$ebay_link}</td>";
         $msg .= "<td>{$attachment_id}</td>";
         $msg .= "</tr>";
     }
     $msg .= '</table>';
     $msg .= '<p>';
     $url = 'admin.php?page=wplister-tools&action=check_ebay_image_requirements&resize_images=yes&_wpnonce=' . wp_create_nonce('e2e_tools_page');
     $msg .= '<a href="' . $url . '" class="button">' . __('Resize all', 'wplister') . '</a> &nbsp; ';
     $msg .= 'Click this button to upscale all found images to 500px.';
     $msg .= '</p>';
     $this->showMessage($msg, 1);
 }