Completed
Branch BUG-10878-event-spaces-remaini... (def5f8)
by
unknown
65:16 queued 53:59
created

ChangesIn40836   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 179
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setHooks() 0 41 1
A removeCalculateQueryParam() 0 7 2
A removeCalculatedFieldsFromResponse() 0 12 2
A removeHeadersNewInThisVersion() 0 19 2
A addOldFeaturedImagePartOfCptEntities() 0 18 4
A useNegativeOneForInfinityBeforeThisVersion() 0 14 3
1
<?php namespace EventEspresso\core\libraries\rest_api\changes;
2
3
use WP_REST_Request;
4
use EventEspresso\core\libraries\rest_api\controllers\model\Read;
5
use EventEspresso\core\libraries\rest_api\controllers\model\Base;
6
use EventEspresso\core\libraries\rest_api\controllers\Base as Controller_Base;
7
use EEM_Base;
8
9
/*
10
 * The checkin and checkout endpoints were added in 4.8.36,
11
 * where we just added a response headers
12
 */
13
14
15
16
class ChangesIn40836 extends ChangesInBase
17
{
18
19
    /**
20
     * Adds hooks so requests to 4.8.29 don't have the checkin endpoints
21
     */
22
    public function setHooks()
23
    {
24
        //set a hook to remove the "calculate" query param
25
        add_filter(
26
            'FHEE__EED_Core_Rest_Api___get_response_selection_query_params',
27
            array($this, 'removeCalculateQueryParam'),
28
            10,
29
            3
30
        );
31
        //don't add the _calculated_fields either
32
        add_filter(
33
            'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal',
34
            array($this, 'removeCalculatedFieldsFromResponse'),
35
            10,
36
            5
37
        );
38
        //and also don't add the count headers
39
        add_filter(
40
            'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers',
41
            array($this, 'removeHeadersNewInThisVersion'),
42
            10,
43
            3
44
        );
45
        //remove the old featured_image part of the response...
46
        add_filter(
47
            'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models',
48
            array($this, 'addOldFeaturedImagePartOfCptEntities'),
49
            10,
50
            5
51
        );
52
        //assuming ticket 9425's change gets pushed with 9406, we don't need to
53
        //remove it from the calculated fields on older requests (because this will
54
        //be the first version with calculated fields)
55
        //before this, infinity was -1, now it's null
56
        add_filter(
57
            'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api',
58
            array($this, 'useNegativeOneForInfinityBeforeThisVersion'),
59
            10,
60
            4
61
        );
62
    }
63
64
65
66
    /**
67
     * Don't show "calculate" as an query param option in the index
68
     *
69
     * @param array     $query_params
70
     * @param EEM_Base $model
71
     * @param string    $version
72
     * @return array
73
     */
74
    public function removeCalculateQueryParam($query_params, EEM_Base $model, $version)
75
    {
76
        if ($this->appliesToVersion($version)) {
77
            unset($query_params['calculate']);
78
        }
79
        return $query_params;
80
    }
81
82
83
84
    /**
85
     * Removes the "_calculate_fields" part of entity responses before 4.8.36
86
     *
87
     * @param array            $entity_response_array
88
     * @param EEM_Base        $model
89
     * @param string           $request_context
90
     * @param WP_REST_Request $request
91
     * @param Read             $controller
92
     * @return array
93
     */
94
    public function removeCalculatedFieldsFromResponse(
95
        $entity_response_array,
96
        EEM_Base $model,
97
        $request_context,
98
        WP_REST_Request $request,
99
        Read $controller
100
    ) {
101
        if ($this->appliesToVersion($controller->getModelVersionInfo()->requestedVersion())) {
102
            unset($entity_response_array['_calculated_fields']);
103
        }
104
        return $entity_response_array;
105
    }
106
107
108
109
    /**
110
     * Removes the new headers for requests before 4.8.36
111
     *
112
     * @param array           $headers
113
     * @param Controller_Base $controller
114
     * @param string          $version
115
     * @return array
116
     */
117
    public function removeHeadersNewInThisVersion(
118
        $headers,
119
        Controller_Base $controller,
120
        $version
121
    ) {
122
        if ($this->appliesToVersion($version)) {
123
            $headers = array_diff_key(
124
                $headers,
125
                array_flip(
126
                    array(
127
                        Base::HEADER_PREFIX_FOR_WP . 'Total',
128
                        Base::HEADER_PREFIX_FOR_WP . 'TotalPages',
129
                        Base::HEADER_PREFIX_FOR_WP . 'PageSize',
130
                    )
131
                )
132
            );
133
        }
134
        return $headers;
135
    }
136
137
138
139
    /**
140
     * Puts the 'featured_image_url' back in for responses before 4.8.36.
141
     *
142
     * @param array            $entity_response_array
143
     * @param EEM_Base        $model
144
     * @param string           $request_context
145
     * @param WP_REST_Request $request
146
     * @param Read             $controller
147
     * @return array
148
     */
149
    public function addOldFeaturedImagePartOfCptEntities(
150
        $entity_response_array,
151
        EEM_Base $model,
152
        $request_context,
153
        WP_REST_Request $request,
154
        Read $controller
155
    ) {
156
        if ($this->appliesToVersion($controller->getModelVersionInfo()->requestedVersion())
157
            && $model instanceof \EEM_CPT_Base
158
        ) {
159
            $attachment = wp_get_attachment_image_src(
160
                get_post_thumbnail_id($entity_response_array[$model->primary_key_name()]),
161
                'full'
162
            );
163
            $entity_response_array['featured_image_url'] = ! empty($attachment) ? $attachment[0] : null;
164
        }
165
        return $entity_response_array;
166
    }
167
168
169
170
    /**
171
     * If the value was infinity, we now use null in our JSON responses,
172
     * but before this version we used -1.
173
     *
174
     * @param mixed                $new_value
175
     * @param \EE_Model_Field_Base $field_obj
176
     * @param mixed                $original_value
177
     * @param string               $requested_value
178
     * @return mixed
179
     */
180
    public function useNegativeOneForInfinityBeforeThisVersion(
181
        $new_value,
182
        $field_obj,
183
        $original_value,
184
        $requested_value
185
    ) {
186
        if ($this->appliesToVersion($requested_value)
187
            && $original_value === EE_INF
188
        ) {
189
            //return the old representation of infinity in the JSON
190
            return -1;
191
        }
192
        return $new_value;
193
    }
194
}
195