|
1
|
|
|
<?php namespace EventEspresso\core\libraries\rest_api\changes; |
|
2
|
|
|
|
|
3
|
|
|
use EE_Error; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Class for tracking what changes are made to the API and when. It's almost like |
|
7
|
|
|
* on-the-fly-migrations. |
|
8
|
|
|
* This class' name indicates what requested versions it applies to. |
|
9
|
|
|
* For example, if its name is "In_4_8_33" then it relates to changes made in 4.8.33. |
|
10
|
|
|
* So if a request comes in for 4.8.29, this class adds hooks and filters will take |
|
11
|
|
|
* care of altering the 4.8.33 endpoints/contents/functionality |
|
12
|
|
|
* to behave like it did in 4.8.29. |
|
13
|
|
|
* For example, if 4.8.33 adds a new endpoint called "/registrations/{id}/datetimes/{id}" |
|
14
|
|
|
* then this class will enqueue a filter to remove that endpoint (and remember this c |
|
15
|
|
|
* class is only loaded for requests to 4.8.29 and lower). |
|
16
|
|
|
* If an endpoint's response had a new field added in 4.8.33, then this would |
|
17
|
|
|
* also enqueue a filter to change that endpoint's response to remove it from |
|
18
|
|
|
* requests to the 4.9.29 version of that endpoint. Etc. |
|
19
|
|
|
* Note that this is different than the Model_Version_Info class. That class just |
|
20
|
|
|
* tracks changes made in EE core's models. This tracks changes just made in the REST API. |
|
21
|
|
|
* They have the same purpose though: provide backwards compatibility so that |
|
22
|
|
|
* the EE4 REST API behaves the same regardless of what version of EE is running |
|
23
|
|
|
* behind the scenes. |
|
24
|
|
|
* Note: this class is loaded on every rest api request, and its filters get |
|
25
|
|
|
* added too, so its up to its filters and action callbacks to determine if they should |
|
26
|
|
|
* apply or not. They should probably use the Changes_In_Base::applies_to_version() |
|
27
|
|
|
* helper method to determine whether to perform their action or not. |
|
28
|
|
|
* Also note that children of this class should be in the same folder and be |
|
29
|
|
|
* named like "Changes_In_{MajorVersion}_{MinorVersion}_{MicroVersion}", |
|
30
|
|
|
* eg "Changes_In_4_8_33", and they should be the namespace "EventEspresso\core\libraries\rest_api\changes". |
|
31
|
|
|
* If so, they will be automatically loaded on all rest api requests, and their |
|
32
|
|
|
* "set_hooks" method will be called automatically during "rest_api_init" |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract class ChangesInBase |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The version that these changes happened |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $version = null; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Called when an EE4 REST API request is made to an earlier version than |
|
48
|
|
|
* what is indicated in this class' name. |
|
49
|
|
|
* Uses WordPress' add_filter and add_action to modify the EE4 REST API's response |
|
50
|
|
|
* so that regardless of what version of EE4 core is running, API clients |
|
51
|
|
|
* will have a consistent response |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract public function setHooks(); |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns whether or not this class' name indicates its hooks should |
|
61
|
|
|
* apply when a request comes in for $requested_version. A class can use |
|
62
|
|
|
* other conditions when determining whether to perform their callbacks or not, |
|
63
|
|
|
* but this will typically be enough |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $requested_version eg "4.8.33" |
|
66
|
|
|
* @return boolean true: this class' name indicates its filters and actions |
|
67
|
|
|
* should take effect. False: this class' name indicates it shouldn't do anything |
|
68
|
|
|
*/ |
|
69
|
|
|
public function appliesToVersion($requested_version) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->version() > $requested_version) { |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Gets the EE core version when this changes were made to the rest api. |
|
81
|
|
|
* Any requests to earlier versions should have modifications made to them |
|
82
|
|
|
* by the callbacks of this class. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string eg "4.8.33" |
|
85
|
|
|
* @throws EE_Error |
|
86
|
|
|
*/ |
|
87
|
|
|
public function version() |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->version === null) { |
|
90
|
|
|
$matches = array(); |
|
91
|
|
|
$regex = '~ChangesIn(\d)(\d\d)(\d\d)$~'; |
|
92
|
|
|
$success = preg_match( |
|
93
|
|
|
$regex, |
|
94
|
|
|
get_class($this), |
|
95
|
|
|
$matches |
|
96
|
|
|
); |
|
97
|
|
|
if (! $success) { |
|
98
|
|
|
throw new EE_Error( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
__('The class %1$s was misnamed. It name should match the regex "%2$s"', 'event_espresso'), |
|
101
|
|
|
get_class($this), |
|
102
|
|
|
$regex |
|
103
|
|
|
) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
$this->version = (int)$matches[1] . '.' . (int)$matches[2] . '.' . (int)$matches[3]; |
|
107
|
|
|
} |
|
108
|
|
|
return $this->version; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|