/** * Method to parse the XML Feed coming from the PHP Input stream in the TlsHubSubscriberFE Callback URL page parsing * * @param $feed * * @return mixed */ public function parseFeed($feed) { // Start Time counter to use for calculation of the time it takes to parse the feed $start_time = microtime(true); // Replace & with the HTML entity of & which was causing an error to load the string $feed = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $feed); // Turn on Simple XML Internal Errors to catch any errors that appear libxml_use_internal_errors(true); $xml = simplexml_load_string($feed, null, LIBXML_NOCDATA); // If there are errors then add them to the error logs if ($xml === false) { $error_msg = "Failed loading Article XML\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 Article XML"; wp_mail($this->admin_email, $email_subject, $error_msg); exit; } // Parse the article entries [Separate method so this method doesn't become extremely long] $articlesResult = $this->parseArticles($xml); // Stop Time Counter $end_time = microtime(true); // Calculate time it took to run through the import $execution_time = $end_time - $start_time; // Add Log of the time it took and how many articles it imported $articles = $articlesResult['articleCount'] == 1 ? 'article' : 'articles'; echo 'It took ' . number_format($execution_time, 2) . ' seconds to ' . $articlesResult['import_type'] . ' Article ID: ' . $articlesResult['original_article_id'] . ' & Title: ' . $articlesResult['original_article_title']; HubLogger::log('It took ' . number_format($execution_time, 2) . ' seconds to ' . $articlesResult['import_type'] . ' Article ID: ' . $articlesResult['original_article_id'] . ' & Title: ' . $articlesResult['original_article_title']); }
/** * 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); } }