[ "endpoint" => $appContext . "/wp-json/fiftyonedegrees/v4/json", "host" => isset($_SERVER['HTTP_HOST']) ? sanitize_text_field( $_SERVER['HTTP_HOST'] ) : $url, "protocol" => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? "https" : "http", "minify" => false ] ]); $error = null; try { $cloud = new CloudRequestEngine(array("resourceKey" => $resourceKey)); } catch (Exception $e) { $error = $e->getMessage(); return array("pipeline" => null, "available_engines" => null, "error" => $error); } // Get engines available with th resource key $engines = array_keys($cloud->flowElementProperties); // Add CloudRequestEngine to the pipeline. $builder->add($cloud); // Add all the engines, accessible via provided // resourceKey, to the pipeline foreach ($engines as $engine) { $cloudEngine = new CloudEngine(); $cloudEngine->dataKey = $engine; $builder->add($cloudEngine); } // Build the pipeline $pipeline = $builder->build(); return array("pipeline" => $pipeline, "available_engines" => $engines, "error" => $error); } /** * process function sets the evidence from web request in flowData and * runs the process function on each attached FlowElement * @return array, containing FlowData, properties and errors. */ public static function process() { $cache = null; if ($cache === null) { require_once dirname(__DIR__) . "/lib/vendor/autoload.php"; $cachedPipeline = get_option('fiftyonedegrees_resource_key_pipeline'); if(isset($cachedPipeline["error"]) || !$cachedPipeline){ return; } // Get pipeline and available engines from cache $pipeline = $cachedPipeline["pipeline"]; $engines = $cachedPipeline["available_engines"]; // Create flowData object in the pipeline. $flowData = $pipeline->createFlowData(); // Set evidence from web request. $flowData->evidence->setFromWebRequest(); // Process flowData with evidence supplied $flowData->process(); // Some browsers require that extra HTTP headers are explicitly // requested. So set whatever headers are required by the browser in // order to return the evidence needed by the pipeline. // More info on this can be found at // https://51degrees.com/blog/user-agent-client-hints Utils::setResponseHeader($flowData); // Get properties for each engine from pipeline. $properties = array(); foreach ($engines as $engine) { $properties[$engine] = $pipeline->getElement($engine)->getProperties(); } $result = array("flowData" => $flowData, "properties" => $properties, "errors" => $flowData->errors); $cache = $result; } return $cache; } /** * Retrieves property by engine and property key * @param string FlowElementDataKey * @param string Property Key * @return String Property Value */ public static function get($engine, $key) { $result = Pipeline::process(); if(isset($result["errors"]) && count($result["errors"])) { error_log("Errors processing Flow Data" . $result["errors"]); return; } $flowData = $result["flowData"]; try { if($flowData->{$engine}->{$key}->hasValue) { return $flowData->{$engine}->{$key}->value; } else { error_log($flowData->{$engine}->{$key}->noValueMessage); return null; } } catch (\Exception $e) { error_log($e->getMessage()); return null; } } /** * Retrieves flowData properties JSON object * @return JSON Object */ public static function getJSON() { $result = Pipeline::process(); if(isset($result["errors"]) && count($result["errors"])) { error_log("Errors processing Flow Data" . $result["errors"]); return; } $flowData = $result["flowData"]; try { return $flowData->jsonbundler->json; } catch (\Exception $e) { error_log($e->getMessage()); return; } } /** * Retrieves properties list by category * @param string Category * @return Array Properties List */ public static function getCategory($category) { $result = Pipeline::process(); if(isset($result["errors"]) && count($result["errors"])) { error_log("Errors processing Flow Data" . $result["errors"]); return; } $flowData = $result["flowData"]; $categoryResults = $flowData->getWhere("category", $category); $output = array(); foreach ($categoryResults as $key => $property) { $value = null; if($property->hasValue) { $value = $property->value; } else { //error_log($property->noValueMessage); $value = null; } $output[$key] = $value; } return $output; } /** * Gets client side javascript from FlowData * @param Javascript */ public static function getJavaScript() { $result = Pipeline::process(); if(isset($result["errors"]) && count($result["errors"])) { error_log("Errors processing Flow Data" . $result["errors"]); return; } $flowData = $result["flowData"]; try { return $flowData->javascriptbuilder->javascript; } catch (\Exception $e) { error_log($e->getMessage()); return ""; } } /** * Gets AppContext from the URL * @param URL */ public static function getAppContext($url) { $urlParts = explode("/", str_replace("https://", "", str_replace("http://", "", $url))); if(count($urlParts) > 1) { $appContext = "/" . end($urlParts); } else { $appContext = ""; } return $appContext; } }