Changeset 2264717
- Timestamp:
- 03/20/2020 07:31:51 PM (6 years ago)
- Location:
- experience-manager/trunk
- Files:
-
- 6 edited
-
experience-manager.php (modified) (1 diff)
-
includes/backend/class.tma_metabox.php (modified) (1 diff)
-
includes/class.request.php (modified) (7 diffs)
-
includes/tma_functions.php (modified) (1 diff)
-
includes/widgets/class.widget_targeting.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
experience-manager/trunk/experience-manager.php
r2264706 r2264717 14 14 } 15 15 16 define("TMA_EXPERIENCE_MANAGER_VERSION", "4. 0.1");16 define("TMA_EXPERIENCE_MANAGER_VERSION", "4.1.0"); 17 17 define("TMA_EXPERIENCE_MANAGER_SEGMENT_MATCHING_ALL", "all"); 18 18 define("TMA_EXPERIENCE_MANAGER_SEGMENT_MATCHING_ANY", "any"); -
experience-manager/trunk/includes/backend/class.tma_metabox.php
r2096995 r2264717 75 75 } 76 76 77 // das ist komplett defekt und muss überarbeitet werden 77 78 $request = new TMA_Request(); 78 79 $response = $request->getAllSegments(); -
experience-manager/trunk/includes/class.request.php
r2264429 r2264717 49 49 * setup the request object and return 50 50 */ 51 public function get($url, $parameters = NULL, $headers = ['Content-Type' => 'text/plain', 'Accept' => 'application/json']) {51 public function get($url, $parameters = [], $headers = ['Content-Type' => 'text/plain', 'Accept' => 'application/json']) { 52 52 53 53 if (!isset($this->options["webtools_apikey"]) || !isset($this->options['webtools_url'])) { … … 69 69 70 70 tma_exm_log(json_encode($parameters)); 71 72 return $this->garded(function () use ($webtools_url, $parameters) { 73 $response = wp_remote_get($webtools_url, $parameters); 74 if (is_array($response) && !is_wp_error($response)) { 75 return $response; // use the content 76 } 77 return FALSE; 78 }); 71 72 $cache_key = $webtools_url . "_" . json_encode($parameters); 73 $result = wp_cache_get($cache_key); 74 if (false === $result) { 75 $result = $this->garded(function () use ($webtools_url, $parameters) { 76 $response = wp_remote_get($webtools_url, $parameters); 77 if (is_array($response) && !is_wp_error($response)) { 78 return $response; // use the content 79 } 80 return FALSE; 81 }); 82 83 wp_cache_set($cache_key, $result); 84 } 85 86 return $result; 79 87 } 80 88 … … 230 238 return FALSE; 231 239 } 232 240 233 241 private function get_webtools_url() { 234 242 $url = $this->options['webtools_url']; … … 249 257 $vid = \TMA\ExperienceManager\TMA_COOKIE_HELPER::getInstance()->getCookie(TMA_COOKIE_HELPER::$COOKIE_VISIT, UUID::v4(), TMA_COOKIE_HELPER::$COOKIE_VISIT_EXPIRE); 250 258 $apikey = $this->options["webtools_apikey"]; 251 $url = $this->get_webtools_url();252 259 $siteid = tma_exm_get_site(); 253 260 … … 294 301 $site = tma_exm_get_site(); 295 302 if (false === $result) { 296 $url = $this->options['webtools_url'] .'rest/userinformation/user?apikey=' . $apikey . '&user=' . $userid297 . '&site=' . $site;303 $url = 'rest/userinformation/user?apikey=' . $apikey . '&user=' . $userid 304 . '&site=' . $site; 298 305 tma_exm_log("url: " . $url); 299 306 $result = $this->loadContent($url, '{"user" : {"segments" : []}, "status" : "ok", "default": true}'); … … 307 314 } 308 315 309 /**310 * REST call to get defined segments311 *312 * @return object the segments313 */314 public function getAllSegments() {315 if (!isset($this->options["webtools_apikey"]) || !isset($this->options['webtools_url'])) {316 $result = '{"status" : "default", segments" : []}';317 return json_decode($result);318 }319 $result = wp_cache_get("tma-all-segments");320 $apikey = $this->options["webtools_apikey"];321 if (false === $result) {322 $url = $this->options['webtools_url'] . 'rest/segments/all?apikey=' . $apikey;323 $result = $this->loadContent($url, '{"status" : "default", segments" : []}');324 wp_cache_set("tma-all-segments", $result, "", 60);325 }326 327 $result = apply_filters("experience-manager/request/all_segments", $result);328 return $result;329 }330 331 /**332 * calls a rest extension333 *334 * e.g. the recommendation module:335 * <url>/rest/extension?extension=recommendation-module&recommendation=<recommendation_id>&id=<user id or item id>336 *337 * @param type $cachekey338 * @param type $extension339 * @param type $attributes340 * @return type341 */342 public function extension_get($cachekey, $extension, $attributes) {343 $result = NULL;344 if (!isset($this->options["webtools_apikey"]) || !isset($this->options['webtools_url'])) {345 $result = '{"error" : true}';346 return json_decode($result);347 }348 349 if (!is_null($cachekey)) {350 $result = wp_cache_get($cachekey);351 }352 $apikey = $this->options["webtools_apikey"];353 354 $url = $this->options['webtools_url'] . "rest/extension?extension={$extension}&apikey=" . $apikey;355 356 // add the custom parameters357 if (isset($attributes)) {358 foreach ($attributes as $key => $value) {359 if (is_array($value)) {360 foreach ($value as $vk) {361 $url .= '&' . urldecode($key) . '=' . urlencode($vk);362 }363 } else {364 $url .= '&' . urldecode($key) . '=' . urlencode($value);365 }366 }367 }368 if ($result === false) {369 $result = $this->loadContent($url, '{"error" : true}');370 371 if ($cachekey !== NULL) {372 wp_cache_set($cachekey, $result, "", 60);373 }374 }375 376 return $result;377 }378 379 /**380 * calls a rest extension381 *382 * e.g. the recommendation module:383 * <url>/rest/extension?extension=recommendation-module&recommendation=<recommendation_id>&id=<user id or item id>384 *385 * @param type $cachekey386 * @param type $extension387 * @param type $attributes388 * @param type $body389 * @return type390 */391 public function extension_post($cachekey, $extension, $attributes, $body) {392 $result = NULL;393 if (!isset($this->options["webtools_apikey"]) || !isset($this->options['webtools_url'])) {394 $result = '{"error" : true}';395 return json_decode($result);396 }397 398 if (!is_null($cachekey)) {399 $result = wp_cache_get($cachekey);400 }401 $apikey = $this->options["webtools_apikey"];402 403 $url = $this->options['webtools_url'] . "rest/extension?extension={$extension}&apikey=" . $apikey;404 405 // add the custom parameters406 if (isset($attributes)) {407 foreach ($attributes as $key => $value) {408 if (is_array($value)) {409 foreach ($value as $vk) {410 $url .= '&' . urldecode($key) . '=' . urlencode($vk);411 }412 } else {413 $url .= '&' . urldecode($key) . '=' . urlencode($value);414 }415 }416 }417 418 if (is_null($result)) {419 $result = $this->postContent($url, $body, '{"error" : true}');420 if ($cachekey !== NULL) {421 wp_cache_set($cachekey, $result, "", 60);422 }423 }424 425 return $result;426 }427 316 428 317 private function loadContent($url, $defaultContent) { … … 435 324 $parameters['headers']['Content-Type'] = "text/plain"; 436 325 437 $response = wp_remote_get($url, $parameters); 326 // $response = wp_remote_get($url, $parameters); 327 $response = $this->get($url, [], ['Content-Type' => "text/plain"]); 438 328 tma_exm_log(json_encode($response)); 439 if ( (is_object($response) || is_array($response)) && !is_wp_error($response)) {329 if ($response !== FALSE && (is_object($response) || is_array($response)) && !is_wp_error($response)) { 440 330 $result = $response['body']; // use the content 441 331 } -
experience-manager/trunk/includes/tma_functions.php
r2264706 r2264717 54 54 } 55 55 return $defaultValue; 56 }57 58 function tma_exm_get_segments() {59 $request = new \TMA\ExperienceManager\TMA_Request();60 $response = $request->getAllSegments();61 if ($response !== NULL && $response->status === "ok") {62 return $response->segments;63 }64 65 return FALSE;66 }67 68 function tma_exm_get_segments_as_array2() {69 $segments = [];70 71 $response_segments = tma_exm_get_segments();72 if ($response_segments !== FALSE) {73 foreach ($response_segments as $segment) {74 $segments[] = (object) [75 'id' => $segment->id,76 'name' => $segment->name77 ];78 }79 }80 81 return $segments;82 56 } 83 57 -
experience-manager/trunk/includes/widgets/class.widget_targeting.php
r2200549 r2264717 61 61 $group_default = isset($instance['tma_default']) ? $instance['tma_default'] : ''; 62 62 63 // $request = new TMA_Request();64 // $response = $request->getAllSegments();65 63 $segments = tma_exm_get_segments_as_array(); 66 64 ?> -
experience-manager/trunk/readme.txt
r2264706 r2264717 4 4 Requires at least: 4.4.1 5 5 Tested up to: 5.3.2 6 Stable tag: 4. 0.17 Version: 4. 0.16 Stable tag: 4.1.0 7 Version: 4.1.0 8 8 License: GPLv2 or later 9 9 … … 67 67 == Changelog == 68 68 69 4. 0.169 4.1.0 70 70 * Fixed a type in a variable name 71 * Add cache to reduce calls to the Experience Platform 71 72 72 73 4.0.0
Note: See TracChangeset
for help on using the changeset viewer.