/** * @return Artmoi_Response */ public function response() { if (!$this->response->error && !$this->response->success) { $this->response->error('Incomplete request. Success or Error must be called.'); } return $this->response; }
/** * sync images to media gallery * @param $post * @return int|object */ public function syncCreation($post) { $url = $post['syncImage']; $page = $post['pageType']; $listId = $post['listId']; // collection or report ID $objectId = $post['syncObjectId']; // image object ID $posIndex = $post['posIndex']; // position index number // Get the image info e $title = $post['syncTitle']; $medium = $post['syncMedium']; $year = $post['syncCreationDateYear']; $month = $post['syncCreationDateMonth']; $creator = $post['syncCreator']; $copyright = $post['syncCopyright']; $price = $post['syncPrice']; $status = $post['syncStatus']; $width = $post['syncWidth']; $height = $post['syncHeight']; $depth = $post['syncDepth']; $unit = $post['syncUnit']; $caption = $post['syncCaption']; $address = $post['syncAddress']; $tag = $post['syncTags']; $desc = "Title: " . $title . "\nMedium: " . $medium . "\nCaption:" . $caption; // Find if image is already in Media Files. $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit'); $the_query = new WP_Query($args); if ($the_query->have_posts()) { while ($the_query->have_posts()) { $the_query->the_post(); $postId = $the_query->post->ID; $getObjectId = get_post_meta($postId, "artmoiObjectId", true); // get saved image IDs $getReportId = get_post_meta($postId, "artmoiReportId", true); // get synced report IDs $getCollectionId = get_post_meta($postId, "artmoiCollectionId", true); // get synced collection IDs if ($objectId == $getObjectId && ($listId == $getReportId || $listId == $getCollectionId)) { return 0; wp_die(); // this is required to terminate immediately and return a proper response } } } error_log("wp sync creation called .. " . $url); // Get the url content $tmp = download_url($url); if (is_wp_error($tmp)) { // download failed, handle error error_log("download failed. " . $tmp->get_error_message()); } $post_id = 0; $file_array = array(); // Set variables for storage // fix file filename for query strings preg_match('/[^\\?]+\\.(jpg|jpe|jpeg|gif|png)/i', $url . '.jpg', $matches); $file_array['name'] = basename($matches[0]); $file_array['tmp_name'] = $tmp; // If error storing temporarily, unlink if (is_wp_error($tmp)) { @unlink($file_array['tmp_name']); $file_array['tmp_name'] = ''; error_log($tmp->get_error_message()); } // do the validation and storage stuff $id = media_handle_sideload($file_array, $post_id, $objectId); // If error storing permanently, unlink if (is_wp_error($id)) { error_log($id->get_error_message()); @unlink($file_array['tmp_name']); return $id; } // Update posts $post = array('ID' => $id, 'post_title' => $title, 'attachment_alt' => 'alt text', 'post_caption' => $caption, 'post_content' => $desc); wp_update_post($post); // Update the meta data for this item. update_post_meta($id, 'artmoi', true); update_post_meta($id, 'artmoiObjectId', $objectId); update_post_meta($id, 'posIndex', $posIndex); update_post_meta($id, 'title', $title); update_post_meta($id, 'caption', $caption); update_post_meta($id, 'medium', $medium); update_post_meta($id, 'address', $address); update_post_meta($id, 'year', $year); update_post_meta($id, 'month', $month); update_post_meta($id, 'creator', $creator); update_post_meta($id, 'tag', $tag); update_post_meta($id, 'width', $width); update_post_meta($id, 'height', $height); update_post_meta($id, 'depth', $depth); update_post_meta($id, 'unit', $unit); update_post_meta($id, 'price', $price); update_post_meta($id, 'copyright', $copyright); update_post_meta($id, 'status', $status); // Update the artmoiReportId or CollectionId meta. It will use for checking synced items if ($page == "report") { update_post_meta($id, 'artmoiReportId', $listId); } elseif ($page == "collection") { update_post_meta($id, 'artmoiCollectionId', $listId); } $src = wp_get_attachment_url($id); error_log("Wordpress media url is now {$src}"); $response = new Artmoi_Response(); $response->results($url); echo json_encode($response); $this->saveSyncedList(); wp_die(); // this is required to terminate immediately and return a proper response }