예제 #1
0
 /**
  * Method to handle the Subscription Verification
  */
 public function verifySubscription()
 {
     // Grab the JSON Payload being sent from the Hub and Decode it
     $jsonPayload = file_get_contents('php://input');
     $json = json_decode($jsonPayload);
     // Perform a GET Request using Guzzle on the SubscribeURL that JSON Payload sent
     $verificationResponse = $this->guzzleClient->get($json->SubscribeURL);
     // Check if Response is 200, 202 or 204 and add a log message otherwise log error message
     if (in_array($verificationResponse->getStatusCode(), array(200, 202, 204))) {
         $this->current_options['subscription_status'] = 'Subscribed';
         update_option($this->option_name, $this->current_options);
         HubLogger::log('Positive answer from Subscription Verification. You are now Subscribed', $verificationResponse->getStatusCode());
         header('HTTP/1.1 200 "Found"', null, 200);
     } else {
         HubLogger::error('Error from Subscription Verification', $verificationResponse->getStatusCode());
         header('HTTP/1.1 404 "Not Found"', null, 404);
     }
 }
예제 #2
0
 /**
  * Download Images from URL
  *
  * @param string     $href
  * @param bool       $inline_image
  * @param string|int $image_counter
  *
  * @return array
  */
 private function handleImageUpload($href, $inline_image = false, $image_counter = '')
 {
     if ($inline_image === false) {
         switch (true) {
             case strpos($href, '440x220'):
                 $image_option = 'thumbnail_image';
                 break;
             case strpos($href, '1280x490'):
                 $image_option = 'hero_image';
                 break;
             case strpos($href, '760x320'):
                 $image_option = 'full_image';
                 break;
             case strpos($href, '.main_image'):
                 $image_option = 'main_image';
                 break;
         }
     }
     // Image XML
     // Originally done directly with simplexml_load_file, but due to server problems, it's now with curl
     // $image_xml = simplexml_load_file((string) $href, null, LIBXML_NOCDATA);
     $path = $href;
     // URL which contains an XML with the image info
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $path);
     curl_setopt($ch, CURLOPT_FAILONERROR, 1);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 35);
     curl_setopt($ch, CURLOPT_TIMEOUT, 35);
     $returned = curl_exec($ch);
     $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     if ($returned === false) {
         $curl_error = curl_error($ch) . "\nHTTP_CODE: " . $httpcode . "\n\n";
     }
     curl_close($ch);
     // check $image_xml === False on failure
     $image_xml = simplexml_load_string($returned, 'SimpleXMLElement', LIBXML_NOCDATA);
     if ($image_xml === false) {
         $error_msg = "Failed loading Image XML\n";
         $error_msg .= "\tDATA: " . $returned . "\n";
         foreach (libxml_get_errors() as $error) {
             $error_msg .= "\t" . $error->message;
         }
         HubLogger::error($error_msg);
         // Send Email to admin
         $email_subject = "TLS Article Importer Error - Failed loading Image XML";
         wp_mail($this->admin_email, $email_subject, $error_msg);
         return false;
     }
     $imageCpiNamespace = $image_xml->children('cpi', true);
     $image_url = (string) $image_xml->link->attributes()->href;
     $image_type = explode('/', $image_xml->link->attributes()->type);
     $image_extension = array_pop($image_type);
     $image_title = !empty($image_xml->title) ? (string) $image_xml->title : (string) $imageCpiNamespace->description;
     if (empty($image_title)) {
         $length = 10;
         $image_title = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
     }
     //USE CURL
     $file_array = $this->tls_get_remote_img($image_url, $image_title . '.' . $image_extension);
     $image_upload_id = media_handle_sideload($file_array, 0, $image_title);
     // Check for handle sideload errors.
     if (empty($image_upload_id) || is_wp_error($image_upload_id)) {
         //@unlink( $file_array['tmp_name'] );
         $errors = $image_upload_id->get_error_messages();
         $error_msg = "Failed downloading image: " . $image_url . "\n";
         foreach ($errors as $error) {
             $error_msg .= "\t" . $error;
         }
         HubLogger::error($error_msg);
         // Send Email to admin
         $email_subject = "TLS Article Importer Error - Failed downloading image: " . $image_url;
         wp_mail($this->admin_email, $email_subject, $error_msg);
     }
     if ($inline_image === false && empty($image_option)) {
         return null;
     }
     // Save Image Alt Text
     $image_alttext = !empty($imageCpiNamespace->alttext) ? (string) $imageCpiNamespace->alttext : $image_title;
     update_post_meta($image_upload_id, '_wp_attachment_image_alt', $image_alttext);
     // Save Custom Attachment Metadata
     $attachment_custom_metadata = array('field_5523b88f78941' => (string) $imageCpiNamespace->caption, 'field_5523b8017893e' => (string) $imageCpiNamespace->copyright, 'field_5523b84b7893f' => (string) $imageCpiNamespace->credit, 'field_5523b85578940' => (string) $imageCpiNamespace->photographer);
     $this->saveArticleCustomFields($attachment_custom_metadata, $image_upload_id);
     // If it is an inline image and the counter is not null then return Attachment ID with image counter
     if ($inline_image === true && $image_counter !== '') {
         return array($image_counter . '_attachment_id' => $image_upload_id);
     }
     return array($image_option . '_attachment_id' => $image_upload_id);
 }