-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSeoABTest.php
299 lines (284 loc) · 7.2 KB
/
SeoABTest.php
1
2
3
4
5
6
7
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
App::uses('SeoAppModel', 'Seo.Model');
/**
* SeoABTest Model
*
* @property SeoUri $SeoUri
*/
class SeoABTest extends SeoAppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'slug';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'seo_uri_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Must be associated to an seo_uri',
),
),
'priority' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Priority must be a number',
),
),
'redmine' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Priority must be a number',
'allowEmpty' => true,
),
),
'roll' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => 'Must have a roll must not be empty.',
),
'numberOrCallback' => array(
'rule' => array('numberOrCallback'),
'message' => 'The roll must either be a number between 1 and 100, or a callback function (Model::function syntax)'
),
),
'testable' => array(
'callback' => array(
'rule' => array('testableValidation'),
'message' => 'testable must either be blank (always true), or a callback function (Model::function syntax)',
'allowEmpty' => true,
),
),
'slug' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => 'Must have a slug (custom Variable for GA)',
),
'noquotes' => array(
'rule' => array('noquotes'),
'message' => 'The slug cannot contain \' marks.'
),
'isUnique' => array(
'rule' => array('isUnique'),
'message' => 'This slug already exists.'
),
),
'description' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => 'Must have a description of the test.',
),
),
'start_date' => array(
'date' => array(
'rule' => array('date'),
'message' => 'Enter a start valid date.',
'allowEmpty' => true
)
),
'end_date' => array(
'date' => array(
'rule' => array('date'),
'message' => 'Enter a end valid date.',
'allowEmpty' => true
)
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'SeoUri' => array(
'className' => 'Seo.SeoUri',
'foreignKey' => 'seo_uri_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $searchFields = array(
'SeoUri.uri', 'SeoABTest.description', 'SeoABTest.slug', 'SeoABTest.id'
);
public $slots = array(
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
);
/**
* Validate there are no ' marks in the slug
* @return boolean success
*/
function noquotes(){
if(isset($this->data[$this->alias]['slug']) && strpos($this->data[$this->alias]['slug'],"'") !== false){
return false;
}
return true;
}
function testableValidation(){
if(isset($this->data[$this->alias]['testable'])){
$testable = $this->data[$this->alias]['testable'];
if (strpos($testable,'::')) {
return true;
}
return false;
}
return true;
}
/**
* Validate the roll is a number between 1 and 100, or is a callback to a Model::function syntax
* @return boolean success
*/
function numberOrCallback(){
if(isset($this->data[$this->alias]['roll'])){
$roll = $this->data[$this->alias]['roll'];
if(is_int($roll) || preg_match('/^\d+$/', $roll)) {
if($roll > 0 && $roll <= 100){
return true;
}
} elseif (strpos($roll,'::')) {
return true;
}
return false;
}
return true;
}
/**
* Check if SEO already exists, if so, unset it and set the ID then save.
*/
public function beforeSave($options = array()){
$this->createOrSetUri();
return true;
}
/**
* Rolls the test roll.
* @param mixed string roll, int roll, or array test
* @return boolean success
*/
public function roll($roll = null){
if(isset($roll[$this->alias]['roll'])){
$roll = $roll[$this->alias]['roll'];
}
if($roll){
if(strpos($roll, '::')){
list($model,$method) = explode('::',$roll);
return ClassRegistry::init($model)->$method(env('REQUEST_URI'));
} else {
return ($roll >= rand(1,100));
}
}
return false;
}
/**
* Take in a test and decide if it's testable.
* @param mixed string testable or array of test
* @return boolean isTestable
*/
public function isTestable($testable = null){
if(isset($testable[$this->alias]['testable'])){
$testable = $testable[$this->alias]['testable'];
}
if(is_string($testable) && $testable){
if(strpos($testable, '::')){
list($model,$method) = explode('::',$testable);
return ClassRegistry::init($model)->$method(env('REQUEST_URI'));
}
}
return true;
}
/**
* Find a test and roll to use it.
* @param string request (default to env('REQUEST_URI') if left null)
* @param boolean debug, will return tests even if they're not active if true
* @return mixed test if we find a test and rolled successful, or false
*/
public function findTestableWithRoll($request = null, $debug = false){
$test = $this->findTestByUri($request, $debug);
if($test && $this->isTestable($test) && $this->roll($test)){
return true;
}
return false;
}
/**
* Decide if we have a test for this request
* @param string request (optional will default to REQUEST_URI
* @param boolean debug (default false), if true, will also return non active tests based on the request
* @return mixed boolean false if no test, otherwise the ABTest will be returned
*/
public function findTestByUri($request = null, $debug = false){
if($request === null){
$request = env('REQUEST_URI');
}
$fields = array(
"{$this->SeoUri->alias}.uri",
"{$this->alias}.slug",
"{$this->alias}.id",
"{$this->alias}.roll",
"{$this->alias}.priority",
"{$this->alias}.testable"
);
$conditions = array(
"AND" => array(
array(
'OR' => array(
"{$this->alias}.start_date IS NULL",
"{$this->alias}.start_date <=" => date('Y-m-d')
)
),
array(
'OR' => array(
"{$this->alias}.end_date IS NULL",
"{$this->alias}.end_date >=" => date('Y-m-d')
)
)
)
);
if(!$debug){
$conditions["{$this->alias}.is_active"] = true;
}
//Test one of one.
if($test = $this->find('first', array(
'conditions' => array_merge(array(
"{$this->SeoUri->alias}.uri" => $request,
"{$this->SeoUri->alias}.is_approved" => true,
),$conditions),
'contain' => array("{$this->SeoUri->alias}.uri"),
'fields' => $fields,
'order' => "{$this->alias}.priority ASC"
))){
return $test;
}
//Check Many to Many and Many to One
$cacheEngine = SeoUtil::getConfig('cacheEngine');
if (!empty($cacheEngine)) {
$cacheKey = 'seo_ab_tests';
$tests = Cache::read($cacheKey, $cacheEngine);
}
if(!isset($tests) || empty($tests)){
$tests = $this->find('all', array(
'conditions' => $conditions,
'contain' => array("{$this->SeoUri->alias}.uri"),
'fields' => $fields,
'order' => "{$this->alias}.priority ASC"
));
if (!empty($tests) && !empty($cacheEngine)) {
Cache::write($cacheKey, $tests, $cacheEngine);
}
}
foreach($tests as $test){
if(SeoUtil::requestMatch($request, $test[$this->SeoUri->alias]['uri'])){
return $test;
}
}
return false;
}
}