예제 #1
0
 private function getRichSnippet($product_id)
 {
     $result = '';
     if (Configuration::get('yotpo_app_key') != '' && Configuration::get('yotpo_oauth_token') != '' && is_int($product_id)) {
         try {
             $result = YotpoSnippetCache::getRichSnippet($product_id);
             $should_update_row = is_array($result) && !YotpoSnippetCache::isValidCache($result);
             if ($result == false || $should_update_row) {
                 $result = '';
                 $expiration_time = null;
                 $request_result = $this->httpClient()->makeRichSnippetRequest(Configuration::get('yotpo_app_key'), Configuration::get('yotpo_oauth_token'), $product_id);
                 if ($request_result['status_code'] == 200) {
                     if ($request_result['json'] == true) {
                         $result .= $request_result['response']['rich_snippet']['html_code'];
                         $expiration_time = $request_result['response']['rich_snippet']['ttl'];
                     } else {
                         preg_match("/html_code[\"']:[\"'](.*)[\"'],[\"']ttl/", $request_result['response'], $matches);
                         $result = $matches[1];
                         unset($matches);
                         $result = str_replace('\\"', '"', $result);
                         $result = str_replace('\\n', '', $result);
                         preg_match("/ttl[\"']:(.*)}/", $request_result['response'], $matches);
                         $expiration_time = $matches[1];
                         unset($matches);
                     }
                     if (strlen($result) > 0 && strlen($expiration_time) > 0 && is_numeric($expiration_time)) {
                         if ($should_update_row) {
                             YotpoSnippetCache::updateCahce($product_id, $result, $expiration_time);
                         } else {
                             YotpoSnippetCache::addRichSnippetToCahce($product_id, $result, $expiration_time);
                         }
                     }
                 }
             } elseif (is_array($result) && !$should_update_row) {
                 $result = $result['rich_snippet_code'];
             }
         } catch (Exception $e) {
             error_log($e->getMessage());
         }
     }
     return $result;
 }