Request.php 3.49 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
5
6
<?php

	namespace WP_Piwik;

	abstract class Request {
		
lechuck's avatar
lechuck committed
7
		protected static $wpPiwik, $settings, $debug, $lastError = '', $requests = array(), $results = array(), $isCacheable = array(), $piwikVersion;
ale's avatar
ale committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
		
		public function __construct($wpPiwik, $settings) {
			self::$wpPiwik = $wpPiwik;
			self::$settings = $settings;
			self::register('API.getPiwikVersion', array());
		}
		
		public function reset() {
			self::$debug = null;
			self::$requests = array();
			self::$results = array();
			self::$isCacheable = array();
			self::$piwikVersion = null;
		}
		
		public static function register($method, $parameter) {
			if ($method == 'API.getPiwikVersion')
				$id = 'global.getPiwikVersion';
			else
				$id = 'method='.$method.self::parameterToString($parameter);
			if ( 
				in_array( $method, array( 'API.getPiwikVersion', 'SitesManager.getJavascriptTag', 'SitesManager.getSitesWithAtLeastViewAccess', 'SitesManager.getSitesIdFromSiteUrl', 'SitesManager.addSite', 'SitesManager.updateSite', 'SitesManager.getSitesWithAtLeastViewAccess' ) ) ||
				!isset( $parameter['date'] ) ||
				!isset( $parameter['period'] ) ||
				substr($parameter['date'], 0, 4) == 'last' ||
				$parameter['date'] == 'today' ||
				( $parameter['period'] == 'day' && $parameter['date'] == date('Ymd') ) ||
				( $parameter['period'] == 'month' && $parameter['date'] == date('Ym') ) ||
				( $parameter['period'] == 'week' && $parameter['date'] == date( 'Ymd', strtotime( "last Monday" ) ) ) 
			) self::$isCacheable[$id] = false;
lucha's avatar
lucha committed
38
			else self::$isCacheable[$id] = $method.'-'.serialize($parameter);
ale's avatar
ale committed
39
40
41
42
			if (!isset(self::$requests[$id]))
				self::$requests[$id] = array('method' => $method, 'parameter' => $parameter);
			return $id;
		}
lucha's avatar
lucha committed
43

ale's avatar
ale committed
44
45
46
47
48
49
50
		private static function parameterToString($parameter) {
			$return = '';
			if (is_array($parameter))
				foreach ($parameter as $key => $value)
					$return .= '&'.$key.'='.$value;
			return $return;
		}
lucha's avatar
lucha committed
51

ale's avatar
ale committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
		public function perform($id) {
			if ( self::$settings->getGlobalOption('cache') && false !== ( $cached = get_transient( 'wp-piwik_c_'.md5(self::$isCacheable[$id] ) ) ) ) {
				if (!empty ( $cached ) && !(! empty ( $cached['result'] ) &&  $cached['result'] == 'error') ) { 
					self::$wpPiwik->log("Deliver cached data: ".$id);
					return $cached;
				}
			}
			self::$wpPiwik->log("Perform request: ".$id);
			if (!isset(self::$requests[$id]))
				return array('result' => 'error', 'message' => 'Request '.$id.' was not registered.');
			elseif (!isset(self::$results[$id])) {
				$this->request($id);
			}
			if ( isset ( self::$results[$id] ) ) {
				if ( self::$settings->getGlobalOption('cache') && self::$isCacheable[$id] ) {
					set_transient( 'wp-piwik_c_'.md5(self::$isCacheable[$id]) , self::$results[$id], WEEK_IN_SECONDS );
				}
				return self::$results[$id];
			} else return false;
		}
		
		public function getDebug($id) {
			return isset( self::$debug[$id] )? self::$debug[$id] : false;
		}
		
		protected function buildURL($config, $urlDecode = false) {
			$url = 'method='.($config['method']).'&idSite='.self::$settings->getOption('site_id');
			foreach ($config['parameter'] as $key => $value)
				$url .= '&'.$key.'='.($urlDecode?urldecode($value):$value);
			return $url;
		}
		
		protected function unserialize($str) {
			self::$wpPiwik->log("Result string: ".$str);
		    return ($str == json_decode(false, true) || @json_decode($str, true) !== false)?json_decode($str, true):array();
		}
lechuck's avatar
lechuck committed
88
89
90
91

		public static function getLastError() {
			return self::$lastError;
		}			
ale's avatar
ale committed
92
93
94
95
		
		abstract protected function request($id);
			
	}