@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @since $VID:$ |
| 14 | 14 | */ |
| 15 | 15 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 16 | - exit('No direct script access allowed'); |
|
| 16 | + exit('No direct script access allowed'); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | |
@@ -21,37 +21,37 @@ discard block |
||
| 21 | 21 | class Base |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param $required_permission |
|
| 26 | - * @param $attempted_calculation |
|
| 27 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 28 | - */ |
|
| 29 | - protected static function _verify_current_user_can($required_permission, $attempted_calculation) |
|
| 30 | - { |
|
| 31 | - if (! current_user_can($required_permission)) { |
|
| 32 | - throw new Rest_Exception( |
|
| 33 | - 'permission_denied', |
|
| 34 | - sprintf( |
|
| 35 | - __('Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
| 36 | - 'event_espresso'), |
|
| 37 | - $attempted_calculation, |
|
| 38 | - \EEH_Inflector::pluralize_and_lower(self::get_resource_name()), |
|
| 39 | - $required_permission |
|
| 40 | - ) |
|
| 41 | - ); |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Gets the name of the resource of the called class |
|
| 49 | - * |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public static function get_resource_name() |
|
| 53 | - { |
|
| 54 | - $classname = get_called_class(); |
|
| 55 | - return substr($classname, strrpos($classname, '\\') + 1); |
|
| 56 | - } |
|
| 24 | + /** |
|
| 25 | + * @param $required_permission |
|
| 26 | + * @param $attempted_calculation |
|
| 27 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 28 | + */ |
|
| 29 | + protected static function _verify_current_user_can($required_permission, $attempted_calculation) |
|
| 30 | + { |
|
| 31 | + if (! current_user_can($required_permission)) { |
|
| 32 | + throw new Rest_Exception( |
|
| 33 | + 'permission_denied', |
|
| 34 | + sprintf( |
|
| 35 | + __('Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
| 36 | + 'event_espresso'), |
|
| 37 | + $attempted_calculation, |
|
| 38 | + \EEH_Inflector::pluralize_and_lower(self::get_resource_name()), |
|
| 39 | + $required_permission |
|
| 40 | + ) |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Gets the name of the resource of the called class |
|
| 49 | + * |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public static function get_resource_name() |
|
| 53 | + { |
|
| 54 | + $classname = get_called_class(); |
|
| 55 | + return substr($classname, strrpos($classname, '\\') + 1); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | * @author Mike Nelson |
| 13 | 13 | * @since $VID:$ |
| 14 | 14 | */ |
| 15 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 15 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 16 | 16 | exit('No direct script access allowed'); |
| 17 | 17 | } |
| 18 | 18 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | protected static function _verify_current_user_can($required_permission, $attempted_calculation) |
| 30 | 30 | { |
| 31 | - if (! current_user_can($required_permission)) { |
|
| 31 | + if ( ! current_user_can($required_permission)) { |
|
| 32 | 32 | throw new Rest_Exception( |
| 33 | 33 | 'permission_denied', |
| 34 | 34 | sprintf( |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | * @author Mike Nelson |
| 13 | 13 | * @since $VID:$ |
| 14 | 14 | */ |
| 15 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 15 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 16 | 16 | exit('No direct script access allowed'); |
| 17 | 17 | } |
| 18 | 18 | |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | } else { |
| 38 | 38 | $reg = null; |
| 39 | 39 | } |
| 40 | - if (! $reg instanceof \EE_Registration |
|
| 40 | + if ( ! $reg instanceof \EE_Registration |
|
| 41 | 41 | ) { |
| 42 | 42 | throw new \EE_Error( |
| 43 | 43 | sprintf( |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @since $VID:$ |
| 14 | 14 | */ |
| 15 | 15 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 16 | - exit('No direct script access allowed'); |
|
| 16 | + exit('No direct script access allowed'); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | |
@@ -21,58 +21,58 @@ discard block |
||
| 21 | 21 | class Registration extends Calculations_Base |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Calculates the checkin status for each datetime this registration has access to |
|
| 26 | - * |
|
| 27 | - * @param array $wpdb_row |
|
| 28 | - * @param \WP_REST_Request $request |
|
| 29 | - * @param Base $controller |
|
| 30 | - * @return int |
|
| 31 | - * @throws \EE_Error |
|
| 32 | - */ |
|
| 33 | - public static function datetime_checkin_stati($wpdb_row, $request, $controller) |
|
| 34 | - { |
|
| 35 | - if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
| 36 | - $reg = \EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
| 37 | - } else { |
|
| 38 | - $reg = null; |
|
| 39 | - } |
|
| 40 | - if (! $reg instanceof \EE_Registration |
|
| 41 | - ) { |
|
| 42 | - throw new \EE_Error( |
|
| 43 | - sprintf( |
|
| 44 | - __('Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
| 45 | - 'event_espresso'), |
|
| 46 | - $wpdb_row['Registration.REG_ID'], |
|
| 47 | - print_r($wpdb_row, true) |
|
| 48 | - ) |
|
| 49 | - ); |
|
| 50 | - } |
|
| 51 | - $datetime_ids = \EEM_Datetime::instance()->get_col( |
|
| 52 | - array( |
|
| 53 | - array( |
|
| 54 | - 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
| 55 | - ), |
|
| 56 | - 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
| 57 | - ) |
|
| 58 | - ); |
|
| 59 | - $checkin_stati = array(); |
|
| 60 | - foreach ($datetime_ids as $datetime_id) { |
|
| 61 | - $status = $reg->check_in_status_for_datetime($datetime_id); |
|
| 62 | - switch ($status) { |
|
| 63 | - case \EE_Registration::checkin_status_out: |
|
| 64 | - $status_pretty = 'OUT'; |
|
| 65 | - break; |
|
| 66 | - case \EE_Registration::checkin_status_in: |
|
| 67 | - $status_pretty = 'IN'; |
|
| 68 | - break; |
|
| 69 | - case \EE_Registration::checkin_status_never: |
|
| 70 | - default: |
|
| 71 | - $status_pretty = 'NEVER'; |
|
| 72 | - break; |
|
| 73 | - } |
|
| 74 | - $checkin_stati[$datetime_id] = $status_pretty; |
|
| 75 | - } |
|
| 76 | - return $checkin_stati; |
|
| 77 | - } |
|
| 24 | + /** |
|
| 25 | + * Calculates the checkin status for each datetime this registration has access to |
|
| 26 | + * |
|
| 27 | + * @param array $wpdb_row |
|
| 28 | + * @param \WP_REST_Request $request |
|
| 29 | + * @param Base $controller |
|
| 30 | + * @return int |
|
| 31 | + * @throws \EE_Error |
|
| 32 | + */ |
|
| 33 | + public static function datetime_checkin_stati($wpdb_row, $request, $controller) |
|
| 34 | + { |
|
| 35 | + if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
| 36 | + $reg = \EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
| 37 | + } else { |
|
| 38 | + $reg = null; |
|
| 39 | + } |
|
| 40 | + if (! $reg instanceof \EE_Registration |
|
| 41 | + ) { |
|
| 42 | + throw new \EE_Error( |
|
| 43 | + sprintf( |
|
| 44 | + __('Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
| 45 | + 'event_espresso'), |
|
| 46 | + $wpdb_row['Registration.REG_ID'], |
|
| 47 | + print_r($wpdb_row, true) |
|
| 48 | + ) |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | + $datetime_ids = \EEM_Datetime::instance()->get_col( |
|
| 52 | + array( |
|
| 53 | + array( |
|
| 54 | + 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
| 55 | + ), |
|
| 56 | + 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
| 57 | + ) |
|
| 58 | + ); |
|
| 59 | + $checkin_stati = array(); |
|
| 60 | + foreach ($datetime_ids as $datetime_id) { |
|
| 61 | + $status = $reg->check_in_status_for_datetime($datetime_id); |
|
| 62 | + switch ($status) { |
|
| 63 | + case \EE_Registration::checkin_status_out: |
|
| 64 | + $status_pretty = 'OUT'; |
|
| 65 | + break; |
|
| 66 | + case \EE_Registration::checkin_status_in: |
|
| 67 | + $status_pretty = 'IN'; |
|
| 68 | + break; |
|
| 69 | + case \EE_Registration::checkin_status_never: |
|
| 70 | + default: |
|
| 71 | + $status_pretty = 'NEVER'; |
|
| 72 | + break; |
|
| 73 | + } |
|
| 74 | + $checkin_stati[$datetime_id] = $status_pretty; |
|
| 75 | + } |
|
| 76 | + return $checkin_stati; |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | * @since $VID:$ |
| 15 | 15 | */ |
| 16 | 16 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 17 | - exit('No direct script access allowed'); |
|
| 17 | + exit('No direct script access allowed'); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | |
@@ -22,350 +22,350 @@ discard block |
||
| 22 | 22 | class Event extends Calculations_Base |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Calculates the total spaces on the event (not subtracting sales, but taking |
|
| 27 | - * sales into account; so this is the optimum sales that CAN still be achieved) |
|
| 28 | - * See EE_Event::total_available_spaces( true ); |
|
| 29 | - * |
|
| 30 | - * @param array $wpdb_row |
|
| 31 | - * @param \WP_REST_Request $request |
|
| 32 | - * @param Base $controller |
|
| 33 | - * @return int |
|
| 34 | - * @throws \EE_Error |
|
| 35 | - */ |
|
| 36 | - public static function optimum_sales_at_start($wpdb_row, $request, $controller) |
|
| 37 | - { |
|
| 38 | - if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
| 39 | - $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 40 | - } else { |
|
| 41 | - $event_obj = null; |
|
| 42 | - } |
|
| 43 | - if ($event_obj instanceof \EE_Event) { |
|
| 44 | - return $event_obj->total_available_spaces(true); |
|
| 45 | - } else { |
|
| 46 | - throw new \EE_Error( |
|
| 47 | - sprintf( |
|
| 48 | - __('Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
| 49 | - 'event_espresso'), |
|
| 50 | - $wpdb_row['Event_CPT.ID'], |
|
| 51 | - print_r($wpdb_row, true) |
|
| 52 | - ) |
|
| 53 | - ); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
| 61 | - * sales that COULD have been achieved) |
|
| 62 | - * See EE_Event::total_available_spaces( true ); |
|
| 63 | - * |
|
| 64 | - * @param array $wpdb_row |
|
| 65 | - * @param \WP_REST_Request $request |
|
| 66 | - * @param Base $controller |
|
| 67 | - * @return int |
|
| 68 | - * @throws \EE_Error |
|
| 69 | - */ |
|
| 70 | - public static function optimum_sales_now($wpdb_row, $request, $controller) |
|
| 71 | - { |
|
| 72 | - if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
| 73 | - $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 74 | - } else { |
|
| 75 | - $event_obj = null; |
|
| 76 | - } |
|
| 77 | - if ($event_obj instanceof \EE_Event) { |
|
| 78 | - return $event_obj->total_available_spaces(false); |
|
| 79 | - } else { |
|
| 80 | - throw new \EE_Error( |
|
| 81 | - sprintf( |
|
| 82 | - __('Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
| 83 | - 'event_espresso'), |
|
| 84 | - $wpdb_row['Event_CPT.ID'], |
|
| 85 | - print_r($wpdb_row, true) |
|
| 86 | - ) |
|
| 87 | - ); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Like optimum_sales_now, but minus total sales so far. |
|
| 95 | - * See EE_Event::spaces_remaining_for_sale( true ); |
|
| 96 | - * |
|
| 97 | - * @param array $wpdb_row |
|
| 98 | - * @param \WP_REST_Request $request |
|
| 99 | - * @param Base $controller |
|
| 100 | - * @return int |
|
| 101 | - * @throws \EE_Error |
|
| 102 | - */ |
|
| 103 | - public static function spaces_remaining($wpdb_row, $request, $controller) |
|
| 104 | - { |
|
| 105 | - if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
| 106 | - $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 107 | - } else { |
|
| 108 | - $event_obj = null; |
|
| 109 | - } |
|
| 110 | - if ($event_obj instanceof \EE_Event) { |
|
| 111 | - return $event_obj->spaces_remaining_for_sale(); |
|
| 112 | - } else { |
|
| 113 | - throw new \EE_Error( |
|
| 114 | - sprintf( |
|
| 115 | - __('Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
| 116 | - 'event_espresso'), |
|
| 117 | - $wpdb_row['Event_CPT.ID'], |
|
| 118 | - print_r($wpdb_row, true) |
|
| 119 | - ) |
|
| 120 | - ); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Counts the number of approved registrations for this event (regardless |
|
| 128 | - * of how many datetimes each registrations' ticket purchase is for) |
|
| 129 | - * |
|
| 130 | - * @param array $wpdb_row |
|
| 131 | - * @param \WP_REST_Request $request |
|
| 132 | - * @param Base $controller |
|
| 133 | - * @return int |
|
| 134 | - * @throws \EE_Error |
|
| 135 | - */ |
|
| 136 | - public static function spots_taken($wpdb_row, $request, $controller) |
|
| 137 | - { |
|
| 138 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 139 | - throw new \EE_Error( |
|
| 140 | - sprintf( |
|
| 141 | - __('Cannot calculate spots_taken because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 142 | - 'event_espresso'), |
|
| 143 | - print_r($wpdb_row, true) |
|
| 144 | - ) |
|
| 145 | - ); |
|
| 146 | - } |
|
| 147 | - return \EEM_Registration::instance()->count( |
|
| 148 | - array( |
|
| 149 | - array( |
|
| 150 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 151 | - 'STS_ID' => \EEM_Registration::status_id_approved, |
|
| 152 | - ), |
|
| 153 | - ), |
|
| 154 | - 'REG_ID', |
|
| 155 | - true |
|
| 156 | - ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Counts the number of pending-payment registrations for this event (regardless |
|
| 163 | - * of how many datetimes each registrations' ticket purchase is for) |
|
| 164 | - * |
|
| 165 | - * @param array $wpdb_row |
|
| 166 | - * @param \WP_REST_Request $request |
|
| 167 | - * @param Base $controller |
|
| 168 | - * @return int |
|
| 169 | - * @throws \EE_Error |
|
| 170 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 171 | - */ |
|
| 172 | - public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
|
| 173 | - { |
|
| 174 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 175 | - throw new \EE_Error( |
|
| 176 | - sprintf( |
|
| 177 | - __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 178 | - 'event_espresso'), |
|
| 179 | - print_r($wpdb_row, true) |
|
| 180 | - ) |
|
| 181 | - ); |
|
| 182 | - } |
|
| 183 | - self::_verify_current_user_can('ee_read_registrations', 'spots_taken_pending_payment'); |
|
| 184 | - return \EEM_Registration::instance()->count( |
|
| 185 | - array( |
|
| 186 | - array( |
|
| 187 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 188 | - 'STS_ID' => \EEM_Registration::status_id_pending_payment, |
|
| 189 | - ), |
|
| 190 | - ), |
|
| 191 | - 'REG_ID', |
|
| 192 | - true |
|
| 193 | - ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Counts all the registrations who have checked into one of this events' datetimes |
|
| 200 | - * See EE_Event::total_available_spaces( false ); |
|
| 201 | - * |
|
| 202 | - * @param array $wpdb_row |
|
| 203 | - * @param \WP_REST_Request $request |
|
| 204 | - * @param Base $controller |
|
| 205 | - * @return int|null if permission denied |
|
| 206 | - * @throws \EE_Error |
|
| 207 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 208 | - */ |
|
| 209 | - public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
|
| 210 | - { |
|
| 211 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 212 | - throw new \EE_Error( |
|
| 213 | - sprintf( |
|
| 214 | - __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 215 | - 'event_espresso'), |
|
| 216 | - print_r($wpdb_row, true) |
|
| 217 | - ) |
|
| 218 | - ); |
|
| 219 | - } |
|
| 220 | - self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_in_count'); |
|
| 221 | - return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Counts all the registrations who have checked out of one of this events' datetimes |
|
| 228 | - * See EE_Event::total_available_spaces( false ); |
|
| 229 | - * |
|
| 230 | - * @param array $wpdb_row |
|
| 231 | - * @param \WP_REST_Request $request |
|
| 232 | - * @param Base $controller |
|
| 233 | - * @return int |
|
| 234 | - * @throws \EE_Error |
|
| 235 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 236 | - */ |
|
| 237 | - public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
|
| 238 | - { |
|
| 239 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 240 | - throw new \EE_Error( |
|
| 241 | - sprintf( |
|
| 242 | - __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 243 | - 'event_espresso'), |
|
| 244 | - print_r($wpdb_row, true) |
|
| 245 | - ) |
|
| 246 | - ); |
|
| 247 | - } |
|
| 248 | - self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_out_count'); |
|
| 249 | - return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Gets the thumbnail image |
|
| 256 | - * |
|
| 257 | - * @param array $wpdb_row |
|
| 258 | - * @param \WP_REST_Request $request |
|
| 259 | - * @param Base $controller |
|
| 260 | - * @return array |
|
| 261 | - */ |
|
| 262 | - public static function image_thumbnail($wpdb_row, $request, $controller) |
|
| 263 | - { |
|
| 264 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'thumbnail'); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Gets the medium image |
|
| 271 | - * |
|
| 272 | - * @param array $wpdb_row |
|
| 273 | - * @param \WP_REST_Request $request |
|
| 274 | - * @param Base $controller |
|
| 275 | - * @return array |
|
| 276 | - */ |
|
| 277 | - public static function image_medium($wpdb_row, $request, $controller) |
|
| 278 | - { |
|
| 279 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium'); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Gets the medium-large image |
|
| 286 | - * |
|
| 287 | - * @param array $wpdb_row |
|
| 288 | - * @param \WP_REST_Request $request |
|
| 289 | - * @param Base $controller |
|
| 290 | - * @return array |
|
| 291 | - */ |
|
| 292 | - public static function image_medium_large($wpdb_row, $request, $controller) |
|
| 293 | - { |
|
| 294 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium_large'); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Gets the large image |
|
| 301 | - * |
|
| 302 | - * @param array $wpdb_row |
|
| 303 | - * @param \WP_REST_Request $request |
|
| 304 | - * @param Base $controller |
|
| 305 | - * @return array |
|
| 306 | - */ |
|
| 307 | - public static function image_large($wpdb_row, $request, $controller) |
|
| 308 | - { |
|
| 309 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'large'); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Gets the post-thumbnail image |
|
| 316 | - * |
|
| 317 | - * @param array $wpdb_row |
|
| 318 | - * @param \WP_REST_Request $request |
|
| 319 | - * @param Base $controller |
|
| 320 | - * @return array |
|
| 321 | - */ |
|
| 322 | - public static function image_post_thumbnail($wpdb_row, $request, $controller) |
|
| 323 | - { |
|
| 324 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'post-thumbnail'); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Gets the full size image |
|
| 331 | - * |
|
| 332 | - * @param array $wpdb_row |
|
| 333 | - * @param \WP_REST_Request $request |
|
| 334 | - * @param Base $controller |
|
| 335 | - * @return array |
|
| 336 | - */ |
|
| 337 | - public static function image_full($wpdb_row, $request, $controller) |
|
| 338 | - { |
|
| 339 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'full'); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Gets image specs and formats them for the display in the API, |
|
| 346 | - * according to the image size requested |
|
| 347 | - * |
|
| 348 | - * @param int $EVT_ID |
|
| 349 | - * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
| 350 | - * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
| 351 | - */ |
|
| 352 | - protected static function _calculate_image_data($EVT_ID, $image_size) |
|
| 353 | - { |
|
| 354 | - $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
| 355 | - $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
| 356 | - if (! $data) { |
|
| 357 | - return null; |
|
| 358 | - } |
|
| 359 | - if (isset($data[3])) { |
|
| 360 | - $generated = $data[3]; |
|
| 361 | - } else { |
|
| 362 | - $generated = true; |
|
| 363 | - } |
|
| 364 | - return array( |
|
| 365 | - 'url' => $data[0], |
|
| 366 | - 'width' => $data[1], |
|
| 367 | - 'height' => $data[2], |
|
| 368 | - 'generated' => $generated, |
|
| 369 | - ); |
|
| 370 | - } |
|
| 25 | + /** |
|
| 26 | + * Calculates the total spaces on the event (not subtracting sales, but taking |
|
| 27 | + * sales into account; so this is the optimum sales that CAN still be achieved) |
|
| 28 | + * See EE_Event::total_available_spaces( true ); |
|
| 29 | + * |
|
| 30 | + * @param array $wpdb_row |
|
| 31 | + * @param \WP_REST_Request $request |
|
| 32 | + * @param Base $controller |
|
| 33 | + * @return int |
|
| 34 | + * @throws \EE_Error |
|
| 35 | + */ |
|
| 36 | + public static function optimum_sales_at_start($wpdb_row, $request, $controller) |
|
| 37 | + { |
|
| 38 | + if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
| 39 | + $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 40 | + } else { |
|
| 41 | + $event_obj = null; |
|
| 42 | + } |
|
| 43 | + if ($event_obj instanceof \EE_Event) { |
|
| 44 | + return $event_obj->total_available_spaces(true); |
|
| 45 | + } else { |
|
| 46 | + throw new \EE_Error( |
|
| 47 | + sprintf( |
|
| 48 | + __('Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
| 49 | + 'event_espresso'), |
|
| 50 | + $wpdb_row['Event_CPT.ID'], |
|
| 51 | + print_r($wpdb_row, true) |
|
| 52 | + ) |
|
| 53 | + ); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
| 61 | + * sales that COULD have been achieved) |
|
| 62 | + * See EE_Event::total_available_spaces( true ); |
|
| 63 | + * |
|
| 64 | + * @param array $wpdb_row |
|
| 65 | + * @param \WP_REST_Request $request |
|
| 66 | + * @param Base $controller |
|
| 67 | + * @return int |
|
| 68 | + * @throws \EE_Error |
|
| 69 | + */ |
|
| 70 | + public static function optimum_sales_now($wpdb_row, $request, $controller) |
|
| 71 | + { |
|
| 72 | + if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
| 73 | + $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 74 | + } else { |
|
| 75 | + $event_obj = null; |
|
| 76 | + } |
|
| 77 | + if ($event_obj instanceof \EE_Event) { |
|
| 78 | + return $event_obj->total_available_spaces(false); |
|
| 79 | + } else { |
|
| 80 | + throw new \EE_Error( |
|
| 81 | + sprintf( |
|
| 82 | + __('Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
| 83 | + 'event_espresso'), |
|
| 84 | + $wpdb_row['Event_CPT.ID'], |
|
| 85 | + print_r($wpdb_row, true) |
|
| 86 | + ) |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Like optimum_sales_now, but minus total sales so far. |
|
| 95 | + * See EE_Event::spaces_remaining_for_sale( true ); |
|
| 96 | + * |
|
| 97 | + * @param array $wpdb_row |
|
| 98 | + * @param \WP_REST_Request $request |
|
| 99 | + * @param Base $controller |
|
| 100 | + * @return int |
|
| 101 | + * @throws \EE_Error |
|
| 102 | + */ |
|
| 103 | + public static function spaces_remaining($wpdb_row, $request, $controller) |
|
| 104 | + { |
|
| 105 | + if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
| 106 | + $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 107 | + } else { |
|
| 108 | + $event_obj = null; |
|
| 109 | + } |
|
| 110 | + if ($event_obj instanceof \EE_Event) { |
|
| 111 | + return $event_obj->spaces_remaining_for_sale(); |
|
| 112 | + } else { |
|
| 113 | + throw new \EE_Error( |
|
| 114 | + sprintf( |
|
| 115 | + __('Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
| 116 | + 'event_espresso'), |
|
| 117 | + $wpdb_row['Event_CPT.ID'], |
|
| 118 | + print_r($wpdb_row, true) |
|
| 119 | + ) |
|
| 120 | + ); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Counts the number of approved registrations for this event (regardless |
|
| 128 | + * of how many datetimes each registrations' ticket purchase is for) |
|
| 129 | + * |
|
| 130 | + * @param array $wpdb_row |
|
| 131 | + * @param \WP_REST_Request $request |
|
| 132 | + * @param Base $controller |
|
| 133 | + * @return int |
|
| 134 | + * @throws \EE_Error |
|
| 135 | + */ |
|
| 136 | + public static function spots_taken($wpdb_row, $request, $controller) |
|
| 137 | + { |
|
| 138 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 139 | + throw new \EE_Error( |
|
| 140 | + sprintf( |
|
| 141 | + __('Cannot calculate spots_taken because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 142 | + 'event_espresso'), |
|
| 143 | + print_r($wpdb_row, true) |
|
| 144 | + ) |
|
| 145 | + ); |
|
| 146 | + } |
|
| 147 | + return \EEM_Registration::instance()->count( |
|
| 148 | + array( |
|
| 149 | + array( |
|
| 150 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 151 | + 'STS_ID' => \EEM_Registration::status_id_approved, |
|
| 152 | + ), |
|
| 153 | + ), |
|
| 154 | + 'REG_ID', |
|
| 155 | + true |
|
| 156 | + ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Counts the number of pending-payment registrations for this event (regardless |
|
| 163 | + * of how many datetimes each registrations' ticket purchase is for) |
|
| 164 | + * |
|
| 165 | + * @param array $wpdb_row |
|
| 166 | + * @param \WP_REST_Request $request |
|
| 167 | + * @param Base $controller |
|
| 168 | + * @return int |
|
| 169 | + * @throws \EE_Error |
|
| 170 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 171 | + */ |
|
| 172 | + public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
|
| 173 | + { |
|
| 174 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 175 | + throw new \EE_Error( |
|
| 176 | + sprintf( |
|
| 177 | + __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 178 | + 'event_espresso'), |
|
| 179 | + print_r($wpdb_row, true) |
|
| 180 | + ) |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | + self::_verify_current_user_can('ee_read_registrations', 'spots_taken_pending_payment'); |
|
| 184 | + return \EEM_Registration::instance()->count( |
|
| 185 | + array( |
|
| 186 | + array( |
|
| 187 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 188 | + 'STS_ID' => \EEM_Registration::status_id_pending_payment, |
|
| 189 | + ), |
|
| 190 | + ), |
|
| 191 | + 'REG_ID', |
|
| 192 | + true |
|
| 193 | + ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Counts all the registrations who have checked into one of this events' datetimes |
|
| 200 | + * See EE_Event::total_available_spaces( false ); |
|
| 201 | + * |
|
| 202 | + * @param array $wpdb_row |
|
| 203 | + * @param \WP_REST_Request $request |
|
| 204 | + * @param Base $controller |
|
| 205 | + * @return int|null if permission denied |
|
| 206 | + * @throws \EE_Error |
|
| 207 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 208 | + */ |
|
| 209 | + public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
|
| 210 | + { |
|
| 211 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 212 | + throw new \EE_Error( |
|
| 213 | + sprintf( |
|
| 214 | + __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 215 | + 'event_espresso'), |
|
| 216 | + print_r($wpdb_row, true) |
|
| 217 | + ) |
|
| 218 | + ); |
|
| 219 | + } |
|
| 220 | + self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_in_count'); |
|
| 221 | + return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Counts all the registrations who have checked out of one of this events' datetimes |
|
| 228 | + * See EE_Event::total_available_spaces( false ); |
|
| 229 | + * |
|
| 230 | + * @param array $wpdb_row |
|
| 231 | + * @param \WP_REST_Request $request |
|
| 232 | + * @param Base $controller |
|
| 233 | + * @return int |
|
| 234 | + * @throws \EE_Error |
|
| 235 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
| 236 | + */ |
|
| 237 | + public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
|
| 238 | + { |
|
| 239 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 240 | + throw new \EE_Error( |
|
| 241 | + sprintf( |
|
| 242 | + __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 243 | + 'event_espresso'), |
|
| 244 | + print_r($wpdb_row, true) |
|
| 245 | + ) |
|
| 246 | + ); |
|
| 247 | + } |
|
| 248 | + self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_out_count'); |
|
| 249 | + return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Gets the thumbnail image |
|
| 256 | + * |
|
| 257 | + * @param array $wpdb_row |
|
| 258 | + * @param \WP_REST_Request $request |
|
| 259 | + * @param Base $controller |
|
| 260 | + * @return array |
|
| 261 | + */ |
|
| 262 | + public static function image_thumbnail($wpdb_row, $request, $controller) |
|
| 263 | + { |
|
| 264 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'thumbnail'); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Gets the medium image |
|
| 271 | + * |
|
| 272 | + * @param array $wpdb_row |
|
| 273 | + * @param \WP_REST_Request $request |
|
| 274 | + * @param Base $controller |
|
| 275 | + * @return array |
|
| 276 | + */ |
|
| 277 | + public static function image_medium($wpdb_row, $request, $controller) |
|
| 278 | + { |
|
| 279 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium'); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Gets the medium-large image |
|
| 286 | + * |
|
| 287 | + * @param array $wpdb_row |
|
| 288 | + * @param \WP_REST_Request $request |
|
| 289 | + * @param Base $controller |
|
| 290 | + * @return array |
|
| 291 | + */ |
|
| 292 | + public static function image_medium_large($wpdb_row, $request, $controller) |
|
| 293 | + { |
|
| 294 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium_large'); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Gets the large image |
|
| 301 | + * |
|
| 302 | + * @param array $wpdb_row |
|
| 303 | + * @param \WP_REST_Request $request |
|
| 304 | + * @param Base $controller |
|
| 305 | + * @return array |
|
| 306 | + */ |
|
| 307 | + public static function image_large($wpdb_row, $request, $controller) |
|
| 308 | + { |
|
| 309 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'large'); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Gets the post-thumbnail image |
|
| 316 | + * |
|
| 317 | + * @param array $wpdb_row |
|
| 318 | + * @param \WP_REST_Request $request |
|
| 319 | + * @param Base $controller |
|
| 320 | + * @return array |
|
| 321 | + */ |
|
| 322 | + public static function image_post_thumbnail($wpdb_row, $request, $controller) |
|
| 323 | + { |
|
| 324 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'post-thumbnail'); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Gets the full size image |
|
| 331 | + * |
|
| 332 | + * @param array $wpdb_row |
|
| 333 | + * @param \WP_REST_Request $request |
|
| 334 | + * @param Base $controller |
|
| 335 | + * @return array |
|
| 336 | + */ |
|
| 337 | + public static function image_full($wpdb_row, $request, $controller) |
|
| 338 | + { |
|
| 339 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'full'); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Gets image specs and formats them for the display in the API, |
|
| 346 | + * according to the image size requested |
|
| 347 | + * |
|
| 348 | + * @param int $EVT_ID |
|
| 349 | + * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
| 350 | + * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
| 351 | + */ |
|
| 352 | + protected static function _calculate_image_data($EVT_ID, $image_size) |
|
| 353 | + { |
|
| 354 | + $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
| 355 | + $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
| 356 | + if (! $data) { |
|
| 357 | + return null; |
|
| 358 | + } |
|
| 359 | + if (isset($data[3])) { |
|
| 360 | + $generated = $data[3]; |
|
| 361 | + } else { |
|
| 362 | + $generated = true; |
|
| 363 | + } |
|
| 364 | + return array( |
|
| 365 | + 'url' => $data[0], |
|
| 366 | + 'width' => $data[1], |
|
| 367 | + 'height' => $data[2], |
|
| 368 | + 'generated' => $generated, |
|
| 369 | + ); |
|
| 370 | + } |
|
| 371 | 371 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @author Mike Nelson |
| 14 | 14 | * @since $VID:$ |
| 15 | 15 | */ |
| 16 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 16 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 17 | 17 | exit('No direct script access allowed'); |
| 18 | 18 | } |
| 19 | 19 | |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | public static function spots_taken($wpdb_row, $request, $controller) |
| 137 | 137 | { |
| 138 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 138 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 139 | 139 | throw new \EE_Error( |
| 140 | 140 | sprintf( |
| 141 | 141 | __('Cannot calculate spots_taken because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | */ |
| 172 | 172 | public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
| 173 | 173 | { |
| 174 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 174 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 175 | 175 | throw new \EE_Error( |
| 176 | 176 | sprintf( |
| 177 | 177 | __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
| 210 | 210 | { |
| 211 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 211 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 212 | 212 | throw new \EE_Error( |
| 213 | 213 | sprintf( |
| 214 | 214 | __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | */ |
| 237 | 237 | public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
| 238 | 238 | { |
| 239 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 239 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
| 240 | 240 | throw new \EE_Error( |
| 241 | 241 | sprintf( |
| 242 | 242 | __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | { |
| 354 | 354 | $attachment_id = get_post_thumbnail_id($EVT_ID); |
| 355 | 355 | $data = wp_get_attachment_image_src($attachment_id, $image_size); |
| 356 | - if (! $data) { |
|
| 356 | + if ( ! $data) { |
|
| 357 | 357 | return null; |
| 358 | 358 | } |
| 359 | 359 | if (isset($data[3])) { |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @since 4.8.35.rc.001 |
| 16 | 16 | */ |
| 17 | 17 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 18 | - exit('No direct script access allowed'); |
|
| 18 | + exit('No direct script access allowed'); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | |
@@ -23,127 +23,127 @@ discard block |
||
| 23 | 23 | class Calculated_Model_Fields |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - protected $_mapping; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param bool $refresh |
|
| 35 | - * @return array top-level-keys are model names (eg "Event") |
|
| 36 | - * next-level are the calculated field names AND method names on classes |
|
| 37 | - * which perform calculations, values are the fully qualified classnames which do the calculationss |
|
| 38 | - * These callbacks should accept as arguments: |
|
| 39 | - * the wpdb row results, |
|
| 40 | - * the WP_Request object, |
|
| 41 | - * the controller object |
|
| 42 | - */ |
|
| 43 | - public function mapping($refresh = false) |
|
| 44 | - { |
|
| 45 | - if (! $this->_mapping || $refresh) { |
|
| 46 | - $this->_mapping = $this->_generate_new_mapping(); |
|
| 47 | - } |
|
| 48 | - return $this->_mapping; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Generates anew mapping between model calculated fields and their callbacks |
|
| 55 | - * |
|
| 56 | - * @return array |
|
| 57 | - */ |
|
| 58 | - protected function _generate_new_mapping() |
|
| 59 | - { |
|
| 60 | - $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
|
| 61 | - $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
| 62 | - $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
| 63 | - $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
| 64 | - return apply_filters( |
|
| 65 | - 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
| 66 | - array( |
|
| 67 | - 'Event' => array( |
|
| 68 | - 'optimum_sales_at_start' => $event_calculations_class, |
|
| 69 | - 'optimum_sales_now' => $event_calculations_class, |
|
| 70 | - 'spots_taken' => $event_calculations_class, |
|
| 71 | - 'spots_taken_pending_payment' => $event_calculations_class, |
|
| 72 | - 'spaces_remaining' => $event_calculations_class, |
|
| 73 | - 'registrations_checked_in_count' => $event_calculations_class, |
|
| 74 | - 'registrations_checked_out_count' => $event_calculations_class, |
|
| 75 | - 'image_thumbnail' => $event_calculations_class, |
|
| 76 | - 'image_medium' => $event_calculations_class, |
|
| 77 | - 'image_medium_large' => $event_calculations_class, |
|
| 78 | - 'image_large' => $event_calculations_class, |
|
| 79 | - 'image_post_thumbnail' => $event_calculations_class, |
|
| 80 | - 'image_full' => $event_calculations_class, |
|
| 81 | - ), |
|
| 82 | - 'Datetime' => array( |
|
| 83 | - 'spaces_remaining_considering_tickets' => $datetime_calculations_class, |
|
| 84 | - 'registrations_checked_in_count' => $datetime_calculations_class, |
|
| 85 | - 'registrations_checked_out_count' => $datetime_calculations_class, |
|
| 86 | - 'spots_taken_pending_payment' => $datetime_calculations_class, |
|
| 87 | - ), |
|
| 88 | - 'Registration' => array( |
|
| 89 | - 'datetime_checkin_stati' => $registration_class, |
|
| 90 | - ), |
|
| 91 | - ) |
|
| 92 | - ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Gets the known calculated fields for model |
|
| 99 | - * |
|
| 100 | - * @param \EEM_Base $model |
|
| 101 | - * @return array allowable values for this field |
|
| 102 | - */ |
|
| 103 | - public function retrieve_calculated_fields_for_model(\EEM_Base $model) |
|
| 104 | - { |
|
| 105 | - $mapping = $this->mapping(); |
|
| 106 | - if (isset($mapping[$model->get_this_model_name()])) { |
|
| 107 | - return array_keys($mapping[$model->get_this_model_name()]); |
|
| 108 | - } else { |
|
| 109 | - return array(); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Retrieves the value for this calculation |
|
| 117 | - * |
|
| 118 | - * @param \EEM_Base type $model |
|
| 119 | - * @param string $field_name |
|
| 120 | - * @param array $wpdb_row |
|
| 121 | - * @param \WP_REST_Request |
|
| 122 | - * @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
| 123 | - * @return mixed|null |
|
| 124 | - * @throws \EE_Error |
|
| 125 | - */ |
|
| 126 | - public function retrieve_calculated_field_value( |
|
| 127 | - \EEM_Base $model, |
|
| 128 | - $field_name, |
|
| 129 | - $wpdb_row, |
|
| 130 | - $rest_request, |
|
| 131 | - Base $controller |
|
| 132 | - ) { |
|
| 133 | - $mapping = $this->mapping(); |
|
| 134 | - if (isset($mapping[$model->get_this_model_name()]) |
|
| 135 | - && isset($mapping[$model->get_this_model_name()][$field_name]) |
|
| 136 | - ) { |
|
| 137 | - $classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
| 138 | - return call_user_func(array($classname, $field_name), $wpdb_row, $rest_request, $controller); |
|
| 139 | - } |
|
| 140 | - throw new Rest_Exception( |
|
| 141 | - 'calculated_field_does_not_exist', |
|
| 142 | - sprintf( |
|
| 143 | - __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
| 144 | - $field_name, |
|
| 145 | - $model->get_this_model_name() |
|
| 146 | - ) |
|
| 147 | - ); |
|
| 148 | - } |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + protected $_mapping; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param bool $refresh |
|
| 35 | + * @return array top-level-keys are model names (eg "Event") |
|
| 36 | + * next-level are the calculated field names AND method names on classes |
|
| 37 | + * which perform calculations, values are the fully qualified classnames which do the calculationss |
|
| 38 | + * These callbacks should accept as arguments: |
|
| 39 | + * the wpdb row results, |
|
| 40 | + * the WP_Request object, |
|
| 41 | + * the controller object |
|
| 42 | + */ |
|
| 43 | + public function mapping($refresh = false) |
|
| 44 | + { |
|
| 45 | + if (! $this->_mapping || $refresh) { |
|
| 46 | + $this->_mapping = $this->_generate_new_mapping(); |
|
| 47 | + } |
|
| 48 | + return $this->_mapping; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Generates anew mapping between model calculated fields and their callbacks |
|
| 55 | + * |
|
| 56 | + * @return array |
|
| 57 | + */ |
|
| 58 | + protected function _generate_new_mapping() |
|
| 59 | + { |
|
| 60 | + $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
|
| 61 | + $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
| 62 | + $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
| 63 | + $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
| 64 | + return apply_filters( |
|
| 65 | + 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
| 66 | + array( |
|
| 67 | + 'Event' => array( |
|
| 68 | + 'optimum_sales_at_start' => $event_calculations_class, |
|
| 69 | + 'optimum_sales_now' => $event_calculations_class, |
|
| 70 | + 'spots_taken' => $event_calculations_class, |
|
| 71 | + 'spots_taken_pending_payment' => $event_calculations_class, |
|
| 72 | + 'spaces_remaining' => $event_calculations_class, |
|
| 73 | + 'registrations_checked_in_count' => $event_calculations_class, |
|
| 74 | + 'registrations_checked_out_count' => $event_calculations_class, |
|
| 75 | + 'image_thumbnail' => $event_calculations_class, |
|
| 76 | + 'image_medium' => $event_calculations_class, |
|
| 77 | + 'image_medium_large' => $event_calculations_class, |
|
| 78 | + 'image_large' => $event_calculations_class, |
|
| 79 | + 'image_post_thumbnail' => $event_calculations_class, |
|
| 80 | + 'image_full' => $event_calculations_class, |
|
| 81 | + ), |
|
| 82 | + 'Datetime' => array( |
|
| 83 | + 'spaces_remaining_considering_tickets' => $datetime_calculations_class, |
|
| 84 | + 'registrations_checked_in_count' => $datetime_calculations_class, |
|
| 85 | + 'registrations_checked_out_count' => $datetime_calculations_class, |
|
| 86 | + 'spots_taken_pending_payment' => $datetime_calculations_class, |
|
| 87 | + ), |
|
| 88 | + 'Registration' => array( |
|
| 89 | + 'datetime_checkin_stati' => $registration_class, |
|
| 90 | + ), |
|
| 91 | + ) |
|
| 92 | + ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Gets the known calculated fields for model |
|
| 99 | + * |
|
| 100 | + * @param \EEM_Base $model |
|
| 101 | + * @return array allowable values for this field |
|
| 102 | + */ |
|
| 103 | + public function retrieve_calculated_fields_for_model(\EEM_Base $model) |
|
| 104 | + { |
|
| 105 | + $mapping = $this->mapping(); |
|
| 106 | + if (isset($mapping[$model->get_this_model_name()])) { |
|
| 107 | + return array_keys($mapping[$model->get_this_model_name()]); |
|
| 108 | + } else { |
|
| 109 | + return array(); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Retrieves the value for this calculation |
|
| 117 | + * |
|
| 118 | + * @param \EEM_Base type $model |
|
| 119 | + * @param string $field_name |
|
| 120 | + * @param array $wpdb_row |
|
| 121 | + * @param \WP_REST_Request |
|
| 122 | + * @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
| 123 | + * @return mixed|null |
|
| 124 | + * @throws \EE_Error |
|
| 125 | + */ |
|
| 126 | + public function retrieve_calculated_field_value( |
|
| 127 | + \EEM_Base $model, |
|
| 128 | + $field_name, |
|
| 129 | + $wpdb_row, |
|
| 130 | + $rest_request, |
|
| 131 | + Base $controller |
|
| 132 | + ) { |
|
| 133 | + $mapping = $this->mapping(); |
|
| 134 | + if (isset($mapping[$model->get_this_model_name()]) |
|
| 135 | + && isset($mapping[$model->get_this_model_name()][$field_name]) |
|
| 136 | + ) { |
|
| 137 | + $classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
| 138 | + return call_user_func(array($classname, $field_name), $wpdb_row, $rest_request, $controller); |
|
| 139 | + } |
|
| 140 | + throw new Rest_Exception( |
|
| 141 | + 'calculated_field_does_not_exist', |
|
| 142 | + sprintf( |
|
| 143 | + __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
| 144 | + $field_name, |
|
| 145 | + $model->get_this_model_name() |
|
| 146 | + ) |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | * @author Mike Nelson |
| 15 | 15 | * @since 4.8.35.rc.001 |
| 16 | 16 | */ |
| 17 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 17 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 18 | 18 | exit('No direct script access allowed'); |
| 19 | 19 | } |
| 20 | 20 | |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | public function mapping($refresh = false) |
| 44 | 44 | { |
| 45 | - if (! $this->_mapping || $refresh) { |
|
| 45 | + if ( ! $this->_mapping || $refresh) { |
|
| 46 | 46 | $this->_mapping = $this->_generate_new_mapping(); |
| 47 | 47 | } |
| 48 | 48 | return $this->_mapping; |
@@ -58,9 +58,9 @@ discard block |
||
| 58 | 58 | protected function _generate_new_mapping() |
| 59 | 59 | { |
| 60 | 60 | $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
| 61 | - $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
| 62 | - $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
| 63 | - $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
| 61 | + $event_calculations_class = $rest_api_calculations_namespace.'Event'; |
|
| 62 | + $datetime_calculations_class = $rest_api_calculations_namespace.'Datetime'; |
|
| 63 | + $registration_class = $rest_api_calculations_namespace.'Registration'; |
|
| 64 | 64 | return apply_filters( |
| 65 | 65 | 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
| 66 | 66 | array( |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
| 3 | 3 | |
| 4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 5 | - exit('No direct script access allowed'); |
|
| 5 | + exit('No direct script access allowed'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
@@ -17,134 +17,134 @@ discard block |
||
| 17 | 17 | class Capabilities |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The current user can see at least SOME of these entities. |
|
| 22 | - * |
|
| 23 | - * @param \EEM_Base $model |
|
| 24 | - * @param string $model_context one of the return values from EEM_Base::valid_cap_contexts() |
|
| 25 | - * @return boolean |
|
| 26 | - */ |
|
| 27 | - public static function current_user_has_partial_access_to($model, $model_context = \EEM_Base::caps_read) |
|
| 28 | - { |
|
| 29 | - if ( |
|
| 30 | - apply_filters( |
|
| 31 | - 'FHEE__Capabilities__current_user_has_partial_access_to__override_begin', |
|
| 32 | - false, |
|
| 33 | - $model, |
|
| 34 | - $model |
|
| 35 | - ) |
|
| 36 | - ) { |
|
| 37 | - return true; |
|
| 38 | - } |
|
| 39 | - foreach ($model->caps_missing($model_context) as $capability_name => $restriction_obj) { |
|
| 40 | - if ($restriction_obj instanceof \EE_Return_None_Where_Conditions) { |
|
| 41 | - return false; |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - if ( |
|
| 45 | - apply_filters( |
|
| 46 | - 'FHEE__Capabilities__current_user_has_partial_access_to__override_end', |
|
| 47 | - false, |
|
| 48 | - $model, |
|
| 49 | - $model |
|
| 50 | - ) |
|
| 51 | - ) { |
|
| 52 | - return false; |
|
| 53 | - } |
|
| 54 | - return true; |
|
| 55 | - } |
|
| 20 | + /** |
|
| 21 | + * The current user can see at least SOME of these entities. |
|
| 22 | + * |
|
| 23 | + * @param \EEM_Base $model |
|
| 24 | + * @param string $model_context one of the return values from EEM_Base::valid_cap_contexts() |
|
| 25 | + * @return boolean |
|
| 26 | + */ |
|
| 27 | + public static function current_user_has_partial_access_to($model, $model_context = \EEM_Base::caps_read) |
|
| 28 | + { |
|
| 29 | + if ( |
|
| 30 | + apply_filters( |
|
| 31 | + 'FHEE__Capabilities__current_user_has_partial_access_to__override_begin', |
|
| 32 | + false, |
|
| 33 | + $model, |
|
| 34 | + $model |
|
| 35 | + ) |
|
| 36 | + ) { |
|
| 37 | + return true; |
|
| 38 | + } |
|
| 39 | + foreach ($model->caps_missing($model_context) as $capability_name => $restriction_obj) { |
|
| 40 | + if ($restriction_obj instanceof \EE_Return_None_Where_Conditions) { |
|
| 41 | + return false; |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + if ( |
|
| 45 | + apply_filters( |
|
| 46 | + 'FHEE__Capabilities__current_user_has_partial_access_to__override_end', |
|
| 47 | + false, |
|
| 48 | + $model, |
|
| 49 | + $model |
|
| 50 | + ) |
|
| 51 | + ) { |
|
| 52 | + return false; |
|
| 53 | + } |
|
| 54 | + return true; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Gets an array of all the capabilities the current user is missing that affected |
|
| 61 | - * the query |
|
| 62 | - * |
|
| 63 | - * @param \EEM_Base $model |
|
| 64 | - * @param string $request_type one of the constants on WP_JSON_Server |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - public static function get_missing_permissions($model, $request_type = \EEM_Base::caps_read) |
|
| 68 | - { |
|
| 69 | - return $model->caps_missing($request_type); |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * Gets an array of all the capabilities the current user is missing that affected |
|
| 61 | + * the query |
|
| 62 | + * |
|
| 63 | + * @param \EEM_Base $model |
|
| 64 | + * @param string $request_type one of the constants on WP_JSON_Server |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + public static function get_missing_permissions($model, $request_type = \EEM_Base::caps_read) |
|
| 68 | + { |
|
| 69 | + return $model->caps_missing($request_type); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | 72 | |
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Gets a string of all the capabilities the current user is missing that affected |
|
| 76 | - * the query |
|
| 77 | - * |
|
| 78 | - * @param \EEM_Base $model |
|
| 79 | - * @param string $model_context one of the return values from EEM_Base::valid_cap_contexts() |
|
| 80 | - * @return string |
|
| 81 | - */ |
|
| 82 | - public static function get_missing_permissions_string($model, $model_context = \EEM_Base::caps_read) |
|
| 83 | - { |
|
| 84 | - return implode(',', array_keys(self::get_missing_permissions($model, $model_context))); |
|
| 85 | - } |
|
| 74 | + /** |
|
| 75 | + * Gets a string of all the capabilities the current user is missing that affected |
|
| 76 | + * the query |
|
| 77 | + * |
|
| 78 | + * @param \EEM_Base $model |
|
| 79 | + * @param string $model_context one of the return values from EEM_Base::valid_cap_contexts() |
|
| 80 | + * @return string |
|
| 81 | + */ |
|
| 82 | + public static function get_missing_permissions_string($model, $model_context = \EEM_Base::caps_read) |
|
| 83 | + { |
|
| 84 | + return implode(',', array_keys(self::get_missing_permissions($model, $model_context))); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Takes a entity that's ready to be returned and removes fields which the user shouldn't be able to access. |
|
| 91 | - * |
|
| 92 | - * @param array $entity |
|
| 93 | - * @param \EEM_Base $model |
|
| 94 | - * @param string $request_type one of the return values from EEM_Base::valid_cap_contexts() |
|
| 95 | - * @param Model_Version_Info $model_version_info |
|
| 96 | - * @param string $primary_key_string result of EEM_Base::get_index_primary_key_string(), so that we can |
|
| 97 | - * use this with models that have no primary key |
|
| 98 | - * @return array ready for converting into json |
|
| 99 | - */ |
|
| 100 | - public static function filter_out_inaccessible_entity_fields( |
|
| 101 | - $entity, |
|
| 102 | - $model, |
|
| 103 | - $request_type, |
|
| 104 | - $model_version_info, |
|
| 105 | - $primary_key_string = null |
|
| 106 | - ) { |
|
| 107 | - //if they didn't provide the primary key string, we'll just hope we can figure it out |
|
| 108 | - //from the entity (although it's preferred client code does it, because the entity might be missing |
|
| 109 | - //necessary fields for creating the primary key string, or they could be named differently) |
|
| 110 | - if ($primary_key_string === null) { |
|
| 111 | - $primary_key_string = $model->get_index_primary_key_string( |
|
| 112 | - $model->deduce_fields_n_values_from_cols_n_values($entity) |
|
| 113 | - ); |
|
| 114 | - } |
|
| 115 | - //we only care to do this for frontend reads and when the user can't edit the item |
|
| 116 | - if ( |
|
| 117 | - $request_type !== \EEM_Base::caps_read |
|
| 118 | - || $model->exists( |
|
| 119 | - $model->alter_query_params_to_restrict_by_ID( |
|
| 120 | - $primary_key_string, |
|
| 121 | - array( |
|
| 122 | - 'default_where_conditions' => 'none', |
|
| 123 | - 'caps' => \EEM_Base::caps_edit, |
|
| 124 | - ) |
|
| 125 | - ) |
|
| 126 | - ) |
|
| 127 | - ) { |
|
| 128 | - return $entity; |
|
| 129 | - } |
|
| 130 | - foreach ($model->field_settings() as $field_name => $field_obj) { |
|
| 131 | - if ( |
|
| 132 | - $model_version_info->field_has_rendered_format($field_obj) |
|
| 133 | - && isset($entity[$field_name]) |
|
| 134 | - && is_array($entity[$field_name]) |
|
| 135 | - && isset($entity[$field_name]['raw']) |
|
| 136 | - ) { |
|
| 137 | - unset($entity[$field_name]['raw']); |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - //theoretically we may want to filter out specific fields for specific models |
|
| 141 | - return apply_filters( |
|
| 142 | - 'FHEE__Capabilities__filter_out_inaccessible_entity_fields', |
|
| 143 | - $entity, |
|
| 144 | - $model, |
|
| 145 | - $request_type |
|
| 146 | - ); |
|
| 147 | - } |
|
| 89 | + /** |
|
| 90 | + * Takes a entity that's ready to be returned and removes fields which the user shouldn't be able to access. |
|
| 91 | + * |
|
| 92 | + * @param array $entity |
|
| 93 | + * @param \EEM_Base $model |
|
| 94 | + * @param string $request_type one of the return values from EEM_Base::valid_cap_contexts() |
|
| 95 | + * @param Model_Version_Info $model_version_info |
|
| 96 | + * @param string $primary_key_string result of EEM_Base::get_index_primary_key_string(), so that we can |
|
| 97 | + * use this with models that have no primary key |
|
| 98 | + * @return array ready for converting into json |
|
| 99 | + */ |
|
| 100 | + public static function filter_out_inaccessible_entity_fields( |
|
| 101 | + $entity, |
|
| 102 | + $model, |
|
| 103 | + $request_type, |
|
| 104 | + $model_version_info, |
|
| 105 | + $primary_key_string = null |
|
| 106 | + ) { |
|
| 107 | + //if they didn't provide the primary key string, we'll just hope we can figure it out |
|
| 108 | + //from the entity (although it's preferred client code does it, because the entity might be missing |
|
| 109 | + //necessary fields for creating the primary key string, or they could be named differently) |
|
| 110 | + if ($primary_key_string === null) { |
|
| 111 | + $primary_key_string = $model->get_index_primary_key_string( |
|
| 112 | + $model->deduce_fields_n_values_from_cols_n_values($entity) |
|
| 113 | + ); |
|
| 114 | + } |
|
| 115 | + //we only care to do this for frontend reads and when the user can't edit the item |
|
| 116 | + if ( |
|
| 117 | + $request_type !== \EEM_Base::caps_read |
|
| 118 | + || $model->exists( |
|
| 119 | + $model->alter_query_params_to_restrict_by_ID( |
|
| 120 | + $primary_key_string, |
|
| 121 | + array( |
|
| 122 | + 'default_where_conditions' => 'none', |
|
| 123 | + 'caps' => \EEM_Base::caps_edit, |
|
| 124 | + ) |
|
| 125 | + ) |
|
| 126 | + ) |
|
| 127 | + ) { |
|
| 128 | + return $entity; |
|
| 129 | + } |
|
| 130 | + foreach ($model->field_settings() as $field_name => $field_obj) { |
|
| 131 | + if ( |
|
| 132 | + $model_version_info->field_has_rendered_format($field_obj) |
|
| 133 | + && isset($entity[$field_name]) |
|
| 134 | + && is_array($entity[$field_name]) |
|
| 135 | + && isset($entity[$field_name]['raw']) |
|
| 136 | + ) { |
|
| 137 | + unset($entity[$field_name]['raw']); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + //theoretically we may want to filter out specific fields for specific models |
|
| 141 | + return apply_filters( |
|
| 142 | + 'FHEE__Capabilities__filter_out_inaccessible_entity_fields', |
|
| 143 | + $entity, |
|
| 144 | + $model, |
|
| 145 | + $request_type |
|
| 146 | + ); |
|
| 147 | + } |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | // End of file Capabilities.php |
| 151 | 151 | \ No newline at end of file |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
| 3 | 3 | |
| 4 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 5 | 5 | exit('No direct script access allowed'); |
| 6 | 6 | } |
| 7 | 7 | |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | use EventEspresso\core\libraries\rest_api\Model_Data_Translator; |
| 5 | 5 | |
| 6 | 6 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 7 | - exit('No direct script access allowed'); |
|
| 7 | + exit('No direct script access allowed'); |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | |
@@ -21,123 +21,123 @@ discard block |
||
| 21 | 21 | { |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param \WP_REST_Request $request |
|
| 26 | - * @return array|\WP_REST_Response |
|
| 27 | - */ |
|
| 28 | - public static function handle_request_models_meta(\WP_REST_Request $request) |
|
| 29 | - { |
|
| 30 | - $controller = new Meta(); |
|
| 31 | - try { |
|
| 32 | - $matches = $controller->parse_route( |
|
| 33 | - $request->get_route(), |
|
| 34 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . 'resources~', |
|
| 35 | - array('version')); |
|
| 36 | - if ($matches instanceof \WP_REST_Response) { |
|
| 37 | - return $matches; |
|
| 38 | - } |
|
| 39 | - $controller->set_requested_version($matches['version']); |
|
| 40 | - return $controller->send_response($controller->_get_models_metadata_entity()); |
|
| 41 | - } catch (\Exception $e) { |
|
| 42 | - return $controller->send_response($e); |
|
| 43 | - } |
|
| 44 | - } |
|
| 24 | + /** |
|
| 25 | + * @param \WP_REST_Request $request |
|
| 26 | + * @return array|\WP_REST_Response |
|
| 27 | + */ |
|
| 28 | + public static function handle_request_models_meta(\WP_REST_Request $request) |
|
| 29 | + { |
|
| 30 | + $controller = new Meta(); |
|
| 31 | + try { |
|
| 32 | + $matches = $controller->parse_route( |
|
| 33 | + $request->get_route(), |
|
| 34 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . 'resources~', |
|
| 35 | + array('version')); |
|
| 36 | + if ($matches instanceof \WP_REST_Response) { |
|
| 37 | + return $matches; |
|
| 38 | + } |
|
| 39 | + $controller->set_requested_version($matches['version']); |
|
| 40 | + return $controller->send_response($controller->_get_models_metadata_entity()); |
|
| 41 | + } catch (\Exception $e) { |
|
| 42 | + return $controller->send_response($e); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /* |
|
| 48 | + /* |
|
| 49 | 49 | * Gets the model metadata resource entity |
| 50 | 50 | * @return array for JSON response, describing all the models available in teh requested version |
| 51 | 51 | */ |
| 52 | - protected function _get_models_metadata_entity() |
|
| 53 | - { |
|
| 54 | - $response = array(); |
|
| 55 | - foreach ($this->get_model_version_info()->models_for_requested_version() as $model_name => $model_classname) { |
|
| 56 | - $model = $this->get_model_version_info()->load_model($model_name); |
|
| 57 | - $fields_json = array(); |
|
| 58 | - foreach ( |
|
| 59 | - $this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => |
|
| 60 | - $field_obj |
|
| 61 | - ) { |
|
| 62 | - if ($this->get_model_version_info()->field_is_ignored($field_obj)) { |
|
| 63 | - continue; |
|
| 64 | - } |
|
| 65 | - if ($field_obj instanceof \EE_Boolean_Field) { |
|
| 66 | - $datatype = 'Boolean'; |
|
| 67 | - } elseif ($field_obj->get_wpdb_data_type() == '%d') { |
|
| 68 | - $datatype = 'Number'; |
|
| 69 | - } elseif ($field_name instanceof \EE_Serialized_Text_Field) { |
|
| 70 | - $datatype = 'Object'; |
|
| 71 | - } else { |
|
| 72 | - $datatype = 'String'; |
|
| 73 | - } |
|
| 74 | - $default_value = Model_Data_Translator::prepare_field_value_for_json( |
|
| 75 | - $field_obj, |
|
| 76 | - $field_obj->get_default_value(), |
|
| 77 | - $this->get_model_version_info()->requested_version() |
|
| 78 | - ); |
|
| 79 | - $field_json = array( |
|
| 80 | - 'name' => $field_name, |
|
| 81 | - 'nicename' => $field_obj->get_nicename(), |
|
| 82 | - 'has_rendered_format' => $this->get_model_version_info()->field_has_rendered_format($field_obj), |
|
| 83 | - 'has_pretty_format' => $this->get_model_version_info()->field_has_pretty_format($field_obj), |
|
| 84 | - 'type' => str_replace('EE_', '', get_class($field_obj)), |
|
| 85 | - 'datatype' => $datatype, |
|
| 86 | - 'nullable' => $field_obj->is_nullable(), |
|
| 87 | - 'default' => $default_value, |
|
| 88 | - 'table_alias' => $field_obj->get_table_alias(), |
|
| 89 | - 'table_column' => $field_obj->get_table_column(), |
|
| 90 | - ); |
|
| 91 | - $fields_json[$field_json['name']] = $field_json; |
|
| 92 | - } |
|
| 93 | - $fields_json = array_merge($fields_json, |
|
| 94 | - $this->get_model_version_info()->extra_resource_properties_for_model($model)); |
|
| 95 | - $response[$model_name]['fields'] = apply_filters('FHEE__Meta__handle_request_models_meta__fields', |
|
| 96 | - $fields_json, $model); |
|
| 97 | - $relations_json = array(); |
|
| 98 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 99 | - $relation_json = array( |
|
| 100 | - 'name' => $relation_name, |
|
| 101 | - 'type' => str_replace('EE_', '', get_class($relation_obj)), |
|
| 102 | - 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
| 103 | - ); |
|
| 104 | - $relations_json[$relation_name] = $relation_json; |
|
| 105 | - } |
|
| 106 | - $response[$model_name]['relations'] = apply_filters('FHEE__Meta__handle_request_models_meta__relations', |
|
| 107 | - $relations_json, $model); |
|
| 108 | - } |
|
| 109 | - return $response; |
|
| 110 | - } |
|
| 52 | + protected function _get_models_metadata_entity() |
|
| 53 | + { |
|
| 54 | + $response = array(); |
|
| 55 | + foreach ($this->get_model_version_info()->models_for_requested_version() as $model_name => $model_classname) { |
|
| 56 | + $model = $this->get_model_version_info()->load_model($model_name); |
|
| 57 | + $fields_json = array(); |
|
| 58 | + foreach ( |
|
| 59 | + $this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => |
|
| 60 | + $field_obj |
|
| 61 | + ) { |
|
| 62 | + if ($this->get_model_version_info()->field_is_ignored($field_obj)) { |
|
| 63 | + continue; |
|
| 64 | + } |
|
| 65 | + if ($field_obj instanceof \EE_Boolean_Field) { |
|
| 66 | + $datatype = 'Boolean'; |
|
| 67 | + } elseif ($field_obj->get_wpdb_data_type() == '%d') { |
|
| 68 | + $datatype = 'Number'; |
|
| 69 | + } elseif ($field_name instanceof \EE_Serialized_Text_Field) { |
|
| 70 | + $datatype = 'Object'; |
|
| 71 | + } else { |
|
| 72 | + $datatype = 'String'; |
|
| 73 | + } |
|
| 74 | + $default_value = Model_Data_Translator::prepare_field_value_for_json( |
|
| 75 | + $field_obj, |
|
| 76 | + $field_obj->get_default_value(), |
|
| 77 | + $this->get_model_version_info()->requested_version() |
|
| 78 | + ); |
|
| 79 | + $field_json = array( |
|
| 80 | + 'name' => $field_name, |
|
| 81 | + 'nicename' => $field_obj->get_nicename(), |
|
| 82 | + 'has_rendered_format' => $this->get_model_version_info()->field_has_rendered_format($field_obj), |
|
| 83 | + 'has_pretty_format' => $this->get_model_version_info()->field_has_pretty_format($field_obj), |
|
| 84 | + 'type' => str_replace('EE_', '', get_class($field_obj)), |
|
| 85 | + 'datatype' => $datatype, |
|
| 86 | + 'nullable' => $field_obj->is_nullable(), |
|
| 87 | + 'default' => $default_value, |
|
| 88 | + 'table_alias' => $field_obj->get_table_alias(), |
|
| 89 | + 'table_column' => $field_obj->get_table_column(), |
|
| 90 | + ); |
|
| 91 | + $fields_json[$field_json['name']] = $field_json; |
|
| 92 | + } |
|
| 93 | + $fields_json = array_merge($fields_json, |
|
| 94 | + $this->get_model_version_info()->extra_resource_properties_for_model($model)); |
|
| 95 | + $response[$model_name]['fields'] = apply_filters('FHEE__Meta__handle_request_models_meta__fields', |
|
| 96 | + $fields_json, $model); |
|
| 97 | + $relations_json = array(); |
|
| 98 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 99 | + $relation_json = array( |
|
| 100 | + 'name' => $relation_name, |
|
| 101 | + 'type' => str_replace('EE_', '', get_class($relation_obj)), |
|
| 102 | + 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
| 103 | + ); |
|
| 104 | + $relations_json[$relation_name] = $relation_json; |
|
| 105 | + } |
|
| 106 | + $response[$model_name]['relations'] = apply_filters('FHEE__Meta__handle_request_models_meta__relations', |
|
| 107 | + $relations_json, $model); |
|
| 108 | + } |
|
| 109 | + return $response; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | |
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Adds EE metadata to the index |
|
| 116 | - * |
|
| 117 | - * @param \WP_REST_Response $rest_response_obj |
|
| 118 | - * @return \WP_REST_Response |
|
| 119 | - */ |
|
| 120 | - public static function filter_ee_metadata_into_index(\WP_REST_Response $rest_response_obj) |
|
| 121 | - { |
|
| 122 | - $response_data = $rest_response_obj->get_data(); |
|
| 123 | - $addons = array(); |
|
| 124 | - foreach (\EE_Registry::instance()->addons as $addon) { |
|
| 125 | - $addon_json = array( |
|
| 126 | - 'name' => $addon->name(), |
|
| 127 | - 'version' => $addon->version(), |
|
| 128 | - ); |
|
| 129 | - $addons[$addon_json['name']] = $addon_json; |
|
| 130 | - } |
|
| 131 | - $response_data['ee'] = array( |
|
| 132 | - 'version' => \EEM_System_Status::instance()->get_ee_version(), |
|
| 133 | - 'documentation_url' => 'https://github.com/eventespresso/event-espresso-core/tree/master/docs/C--REST-API', |
|
| 134 | - 'addons' => $addons, |
|
| 135 | - 'maintenance_mode' => \EE_Maintenance_Mode::instance()->real_level(), |
|
| 136 | - 'served_core_versions' => array_keys(\EED_Core_Rest_Api::versions_served()), |
|
| 137 | - ); |
|
| 138 | - $rest_response_obj->set_data($response_data); |
|
| 139 | - return $rest_response_obj; |
|
| 140 | - } |
|
| 114 | + /** |
|
| 115 | + * Adds EE metadata to the index |
|
| 116 | + * |
|
| 117 | + * @param \WP_REST_Response $rest_response_obj |
|
| 118 | + * @return \WP_REST_Response |
|
| 119 | + */ |
|
| 120 | + public static function filter_ee_metadata_into_index(\WP_REST_Response $rest_response_obj) |
|
| 121 | + { |
|
| 122 | + $response_data = $rest_response_obj->get_data(); |
|
| 123 | + $addons = array(); |
|
| 124 | + foreach (\EE_Registry::instance()->addons as $addon) { |
|
| 125 | + $addon_json = array( |
|
| 126 | + 'name' => $addon->name(), |
|
| 127 | + 'version' => $addon->version(), |
|
| 128 | + ); |
|
| 129 | + $addons[$addon_json['name']] = $addon_json; |
|
| 130 | + } |
|
| 131 | + $response_data['ee'] = array( |
|
| 132 | + 'version' => \EEM_System_Status::instance()->get_ee_version(), |
|
| 133 | + 'documentation_url' => 'https://github.com/eventespresso/event-espresso-core/tree/master/docs/C--REST-API', |
|
| 134 | + 'addons' => $addons, |
|
| 135 | + 'maintenance_mode' => \EE_Maintenance_Mode::instance()->real_level(), |
|
| 136 | + 'served_core_versions' => array_keys(\EED_Core_Rest_Api::versions_served()), |
|
| 137 | + ); |
|
| 138 | + $rest_response_obj->set_data($response_data); |
|
| 139 | + return $rest_response_obj; |
|
| 140 | + } |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | use EventEspresso\core\libraries\rest_api\Model_Data_Translator; |
| 5 | 5 | |
| 6 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 6 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 7 | 7 | exit('No direct script access allowed'); |
| 8 | 8 | } |
| 9 | 9 | |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | try { |
| 32 | 32 | $matches = $controller->parse_route( |
| 33 | 33 | $request->get_route(), |
| 34 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . 'resources~', |
|
| 34 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'resources~', |
|
| 35 | 35 | array('version')); |
| 36 | 36 | if ($matches instanceof \WP_REST_Response) { |
| 37 | 37 | return $matches; |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | use EventEspresso\core\libraries\rest_api\Model_Version_Info; |
| 6 | 6 | |
| 7 | 7 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 8 | - exit('No direct script access allowed'); |
|
| 8 | + exit('No direct script access allowed'); |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | |
@@ -21,70 +21,70 @@ discard block |
||
| 21 | 21 | class Base extends Controller_Base |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Holds reference to the model version info, which knows the requested version |
|
| 26 | - * |
|
| 27 | - * @var Model_Version_Info |
|
| 28 | - */ |
|
| 29 | - protected $_model_version_info; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Sets the version the user requested |
|
| 35 | - * |
|
| 36 | - * @param string $version eg '4.8' |
|
| 37 | - */ |
|
| 38 | - public function set_requested_version($version) |
|
| 39 | - { |
|
| 40 | - parent::set_requested_version($version); |
|
| 41 | - $this->_model_version_info = new Model_Version_Info($version); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Gets the object that should be used for getting any info from the models, |
|
| 48 | - * because it's takes the requested and current core version into account |
|
| 49 | - * |
|
| 50 | - * @return \EventEspresso\core\libraries\rest_api\Model_Version_Info |
|
| 51 | - * @throws \EE_Error |
|
| 52 | - */ |
|
| 53 | - public function get_model_version_info() |
|
| 54 | - { |
|
| 55 | - if (! $this->_model_version_info) { |
|
| 56 | - throw new \EE_Error( |
|
| 57 | - sprintf( |
|
| 58 | - __( |
|
| 59 | - 'Cannot use model version info before setting the requested version in the controller', |
|
| 60 | - 'event_espresso' |
|
| 61 | - ) |
|
| 62 | - ) |
|
| 63 | - ); |
|
| 64 | - } |
|
| 65 | - return $this->_model_version_info; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
| 72 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
| 73 | - * in $classnames |
|
| 74 | - * |
|
| 75 | - * @param object $object |
|
| 76 | - * @param array $classnames |
|
| 77 | - * @return boolean |
|
| 78 | - */ |
|
| 79 | - public function is_subclass_of_one($object, $classnames) |
|
| 80 | - { |
|
| 81 | - foreach ($classnames as $classname) { |
|
| 82 | - if (is_a($object, $classname)) { |
|
| 83 | - return true; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - return false; |
|
| 87 | - } |
|
| 24 | + /** |
|
| 25 | + * Holds reference to the model version info, which knows the requested version |
|
| 26 | + * |
|
| 27 | + * @var Model_Version_Info |
|
| 28 | + */ |
|
| 29 | + protected $_model_version_info; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Sets the version the user requested |
|
| 35 | + * |
|
| 36 | + * @param string $version eg '4.8' |
|
| 37 | + */ |
|
| 38 | + public function set_requested_version($version) |
|
| 39 | + { |
|
| 40 | + parent::set_requested_version($version); |
|
| 41 | + $this->_model_version_info = new Model_Version_Info($version); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Gets the object that should be used for getting any info from the models, |
|
| 48 | + * because it's takes the requested and current core version into account |
|
| 49 | + * |
|
| 50 | + * @return \EventEspresso\core\libraries\rest_api\Model_Version_Info |
|
| 51 | + * @throws \EE_Error |
|
| 52 | + */ |
|
| 53 | + public function get_model_version_info() |
|
| 54 | + { |
|
| 55 | + if (! $this->_model_version_info) { |
|
| 56 | + throw new \EE_Error( |
|
| 57 | + sprintf( |
|
| 58 | + __( |
|
| 59 | + 'Cannot use model version info before setting the requested version in the controller', |
|
| 60 | + 'event_espresso' |
|
| 61 | + ) |
|
| 62 | + ) |
|
| 63 | + ); |
|
| 64 | + } |
|
| 65 | + return $this->_model_version_info; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
| 72 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
| 73 | + * in $classnames |
|
| 74 | + * |
|
| 75 | + * @param object $object |
|
| 76 | + * @param array $classnames |
|
| 77 | + * @return boolean |
|
| 78 | + */ |
|
| 79 | + public function is_subclass_of_one($object, $classnames) |
|
| 80 | + { |
|
| 81 | + foreach ($classnames as $classname) { |
|
| 82 | + if (is_a($object, $classname)) { |
|
| 83 | + return true; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + return false; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | 89 | } |
| 90 | 90 | |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | use EventEspresso\core\libraries\rest_api\controllers\Base as Controller_Base; |
| 5 | 5 | use EventEspresso\core\libraries\rest_api\Model_Version_Info; |
| 6 | 6 | |
| 7 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 7 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 8 | 8 | exit('No direct script access allowed'); |
| 9 | 9 | } |
| 10 | 10 | |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function get_model_version_info() |
| 54 | 54 | { |
| 55 | - if (! $this->_model_version_info) { |
|
| 55 | + if ( ! $this->_model_version_info) { |
|
| 56 | 56 | throw new \EE_Error( |
| 57 | 57 | sprintf( |
| 58 | 58 | __( |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | use EE_Datetime_Field; |
| 10 | 10 | |
| 11 | 11 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 12 | - exit('No direct script access allowed'); |
|
| 12 | + exit('No direct script access allowed'); |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | |
@@ -27,1245 +27,1245 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var Calculated_Model_Fields |
|
| 32 | - */ |
|
| 33 | - protected $_fields_calculator; |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Read constructor. |
|
| 39 | - */ |
|
| 40 | - public function __construct() |
|
| 41 | - { |
|
| 42 | - parent::__construct(); |
|
| 43 | - $this->_fields_calculator = new Calculated_Model_Fields(); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
| 50 | - * |
|
| 51 | - * @param \WP_REST_Request $request |
|
| 52 | - * @return \WP_REST_Response|\WP_Error |
|
| 53 | - */ |
|
| 54 | - public static function handle_request_get_all(\WP_REST_Request $request) |
|
| 55 | - { |
|
| 56 | - $controller = new Read(); |
|
| 57 | - try { |
|
| 58 | - $matches = $controller->parse_route( |
|
| 59 | - $request->get_route(), |
|
| 60 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
| 61 | - array('version', 'model') |
|
| 62 | - ); |
|
| 63 | - $controller->set_requested_version($matches['version']); |
|
| 64 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
| 65 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 66 | - return $controller->send_response( |
|
| 67 | - new \WP_Error( |
|
| 68 | - 'endpoint_parsing_error', |
|
| 69 | - sprintf( |
|
| 70 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 71 | - 'event_espresso'), |
|
| 72 | - $model_name_singular |
|
| 73 | - ) |
|
| 74 | - ) |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - return $controller->send_response( |
|
| 78 | - $controller->get_entities_from_model( |
|
| 79 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
| 80 | - $request |
|
| 81 | - ) |
|
| 82 | - ); |
|
| 83 | - } catch (\Exception $e) { |
|
| 84 | - return $controller->send_response($e); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Prepares and returns schema for any OPTIONS request. |
|
| 92 | - * |
|
| 93 | - * @param string $model_name Something like `Event` or `Registration` |
|
| 94 | - * @param string $version The API endpoint version being used. |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - public static function handle_schema_request($model_name, $version) |
|
| 98 | - { |
|
| 99 | - $controller = new Read(); |
|
| 100 | - try { |
|
| 101 | - $controller->set_requested_version($version); |
|
| 102 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
| 103 | - return array(); |
|
| 104 | - } |
|
| 105 | - //get the model for this version |
|
| 106 | - $model = $controller->get_model_version_info()->load_model($model_name); |
|
| 107 | - $model_schema = new JsonModelSchema($model); |
|
| 108 | - return $model_schema->getModelSchemaForRelations( |
|
| 109 | - $controller->get_model_version_info()->relation_settings($model), |
|
| 110 | - $controller->_customize_schema_for_rest_response( |
|
| 111 | - $model, |
|
| 112 | - $model_schema->getModelSchemaForFields( |
|
| 113 | - $controller->get_model_version_info()->fields_on_model_in_this_version($model), |
|
| 114 | - $model_schema->getInitialSchemaStructure() |
|
| 115 | - ) |
|
| 116 | - ) |
|
| 117 | - ); |
|
| 118 | - } catch (\Exception $e) { |
|
| 119 | - return array(); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * This loops through each field in the given schema for the model and does the following: |
|
| 127 | - * - add any extra fields that are REST API specific and related to existing fields. |
|
| 128 | - * - transform default values into the correct format for a REST API response. |
|
| 129 | - * |
|
| 130 | - * @param \EEM_Base $model |
|
| 131 | - * @param array $schema |
|
| 132 | - * @return array The final schema. |
|
| 133 | - */ |
|
| 134 | - protected function _customize_schema_for_rest_response(\EEM_Base $model, array $schema) |
|
| 135 | - { |
|
| 136 | - foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field) { |
|
| 137 | - $schema = $this->_translate_defaults_for_rest_response( |
|
| 138 | - $field_name, |
|
| 139 | - $field, |
|
| 140 | - $this->_maybe_add_extra_fields_to_schema($field_name, $field, $schema) |
|
| 141 | - ); |
|
| 142 | - } |
|
| 143 | - return $schema; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
| 150 | - * response. |
|
| 151 | - * |
|
| 152 | - * @param $field_name |
|
| 153 | - * @param \EE_Model_Field_Base $field |
|
| 154 | - * @param array $schema |
|
| 155 | - * @return array |
|
| 156 | - */ |
|
| 157 | - protected function _translate_defaults_for_rest_response($field_name, \EE_Model_Field_Base $field, array $schema) |
|
| 158 | - { |
|
| 159 | - if (isset($schema['properties'][$field_name]['default'])) { |
|
| 160 | - if (is_array($schema['properties'][$field_name]['default'])) { |
|
| 161 | - foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
| 162 | - if ($default_key === 'raw') { |
|
| 163 | - $schema['properties'][$field_name]['default'][$default_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 164 | - $field, |
|
| 165 | - $default_value, |
|
| 166 | - $this->get_model_version_info()->requested_version() |
|
| 167 | - ); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - } else { |
|
| 171 | - $schema['properties'][$field_name]['default'] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 172 | - $field, |
|
| 173 | - $schema['properties'][$field_name]['default'], |
|
| 174 | - $this->get_model_version_info()->requested_version() |
|
| 175 | - ); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - return $schema; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Adds additional fields to the schema |
|
| 185 | - * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
| 186 | - * needs to be added to the schema. |
|
| 187 | - * |
|
| 188 | - * @param $field_name |
|
| 189 | - * @param \EE_Model_Field_Base $field |
|
| 190 | - * @param array $schema |
|
| 191 | - * @return array |
|
| 192 | - */ |
|
| 193 | - protected function _maybe_add_extra_fields_to_schema($field_name, \EE_Model_Field_Base $field, array $schema) |
|
| 194 | - { |
|
| 195 | - if ($field instanceof EE_Datetime_Field) { |
|
| 196 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
| 197 | - //modify the description |
|
| 198 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
| 199 | - esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
| 200 | - $field->get_nicename() |
|
| 201 | - ); |
|
| 202 | - } |
|
| 203 | - return $schema; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
| 210 | - * |
|
| 211 | - * @return string |
|
| 212 | - */ |
|
| 213 | - protected function get_route_from_request() |
|
| 214 | - { |
|
| 215 | - if (isset($GLOBALS['wp']) |
|
| 216 | - && $GLOBALS['wp'] instanceof \WP |
|
| 217 | - && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
| 218 | - ) { |
|
| 219 | - return $GLOBALS['wp']->query_vars['rest_route']; |
|
| 220 | - } else { |
|
| 221 | - return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Gets a single entity related to the model indicated in the path and its id |
|
| 229 | - * |
|
| 230 | - * @param \WP_REST_Request $request |
|
| 231 | - * @return \WP_REST_Response|\WP_Error |
|
| 232 | - */ |
|
| 233 | - public static function handle_request_get_one(\WP_REST_Request $request) |
|
| 234 | - { |
|
| 235 | - $controller = new Read(); |
|
| 236 | - try { |
|
| 237 | - $matches = $controller->parse_route( |
|
| 238 | - $request->get_route(), |
|
| 239 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
| 240 | - array('version', 'model', 'id')); |
|
| 241 | - $controller->set_requested_version($matches['version']); |
|
| 242 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
| 243 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 244 | - return $controller->send_response( |
|
| 245 | - new \WP_Error( |
|
| 246 | - 'endpoint_parsing_error', |
|
| 247 | - sprintf( |
|
| 248 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 249 | - 'event_espresso'), |
|
| 250 | - $model_name_singular |
|
| 251 | - ) |
|
| 252 | - ) |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - return $controller->send_response( |
|
| 256 | - $controller->get_entity_from_model( |
|
| 257 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
| 258 | - $request |
|
| 259 | - ) |
|
| 260 | - ); |
|
| 261 | - } catch (\Exception $e) { |
|
| 262 | - return $controller->send_response($e); |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
| 270 | - * to the item with the given id |
|
| 271 | - * |
|
| 272 | - * @param \WP_REST_Request $request |
|
| 273 | - * @return \WP_REST_Response|\WP_Error |
|
| 274 | - */ |
|
| 275 | - public static function handle_request_get_related(\WP_REST_Request $request) |
|
| 276 | - { |
|
| 277 | - $controller = new Read(); |
|
| 278 | - try { |
|
| 279 | - $matches = $controller->parse_route( |
|
| 280 | - $request->get_route(), |
|
| 281 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
| 282 | - array('version', 'model', 'id', 'related_model') |
|
| 283 | - ); |
|
| 284 | - $controller->set_requested_version($matches['version']); |
|
| 285 | - $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
| 286 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
| 287 | - return $controller->send_response( |
|
| 288 | - new \WP_Error( |
|
| 289 | - 'endpoint_parsing_error', |
|
| 290 | - sprintf( |
|
| 291 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 292 | - 'event_espresso'), |
|
| 293 | - $main_model_name_singular |
|
| 294 | - ) |
|
| 295 | - ) |
|
| 296 | - ); |
|
| 297 | - } |
|
| 298 | - $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
| 299 | - //assume the related model name is plural and try to find the model's name |
|
| 300 | - $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
| 301 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 302 | - //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
| 303 | - $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
| 304 | - } |
|
| 305 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 306 | - return $controller->send_response( |
|
| 307 | - new \WP_Error( |
|
| 308 | - 'endpoint_parsing_error', |
|
| 309 | - sprintf( |
|
| 310 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 311 | - 'event_espresso'), |
|
| 312 | - $related_model_name_singular |
|
| 313 | - ) |
|
| 314 | - ) |
|
| 315 | - ); |
|
| 316 | - } |
|
| 317 | - return $controller->send_response( |
|
| 318 | - $controller->get_entities_from_relation( |
|
| 319 | - $request->get_param('id'), |
|
| 320 | - $main_model->related_settings_for($related_model_name_singular), |
|
| 321 | - $request |
|
| 322 | - ) |
|
| 323 | - ); |
|
| 324 | - } catch (\Exception $e) { |
|
| 325 | - return $controller->send_response($e); |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Gets a collection for the given model and filters |
|
| 333 | - * |
|
| 334 | - * @param \EEM_Base $model |
|
| 335 | - * @param \WP_REST_Request $request |
|
| 336 | - * @return array|\WP_Error |
|
| 337 | - */ |
|
| 338 | - public function get_entities_from_model($model, $request) |
|
| 339 | - { |
|
| 340 | - $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
| 341 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
| 342 | - $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 343 | - return new \WP_Error( |
|
| 344 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
| 345 | - sprintf( |
|
| 346 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
| 347 | - $model_name_plural, |
|
| 348 | - Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
| 349 | - ), |
|
| 350 | - array('status' => 403) |
|
| 351 | - ); |
|
| 352 | - } |
|
| 353 | - if (! $request->get_header('no_rest_headers')) { |
|
| 354 | - $this->_set_headers_from_query_params($model, $query_params); |
|
| 355 | - } |
|
| 356 | - /** @type array $results */ |
|
| 357 | - $results = $model->get_all_wpdb_results($query_params); |
|
| 358 | - $nice_results = array(); |
|
| 359 | - foreach ($results as $result) { |
|
| 360 | - $nice_results[] = $this->create_entity_from_wpdb_result( |
|
| 361 | - $model, |
|
| 362 | - $result, |
|
| 363 | - $request |
|
| 364 | - ); |
|
| 365 | - } |
|
| 366 | - return $nice_results; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * @param array $primary_model_query_params query params for finding the item from which |
|
| 373 | - * relations will be based |
|
| 374 | - * @param \EE_Model_Relation_Base $relation |
|
| 375 | - * @param \WP_REST_Request $request |
|
| 376 | - * @return \WP_Error|array |
|
| 377 | - */ |
|
| 378 | - protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
| 379 | - { |
|
| 380 | - $context = $this->validate_context($request->get_param('caps')); |
|
| 381 | - $model = $relation->get_this_model(); |
|
| 382 | - $related_model = $relation->get_other_model(); |
|
| 383 | - if (! isset($primary_model_query_params[0])) { |
|
| 384 | - $primary_model_query_params[0] = array(); |
|
| 385 | - } |
|
| 386 | - //check if they can access the 1st model object |
|
| 387 | - $primary_model_query_params = array( |
|
| 388 | - 0 => $primary_model_query_params[0], |
|
| 389 | - 'limit' => 1, |
|
| 390 | - ); |
|
| 391 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
| 392 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
| 393 | - } |
|
| 394 | - $restricted_query_params = $primary_model_query_params; |
|
| 395 | - $restricted_query_params['caps'] = $context; |
|
| 396 | - $this->_set_debug_info('main model query params', $restricted_query_params); |
|
| 397 | - $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
| 398 | - if ( |
|
| 399 | - ! ( |
|
| 400 | - Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
| 401 | - && $model->exists($restricted_query_params) |
|
| 402 | - ) |
|
| 403 | - ) { |
|
| 404 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
| 405 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
| 406 | - } else { |
|
| 407 | - $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
| 408 | - } |
|
| 409 | - return new \WP_Error( |
|
| 410 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
| 411 | - sprintf( |
|
| 412 | - __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
| 413 | - 'event_espresso'), |
|
| 414 | - $related_model_name_maybe_plural, |
|
| 415 | - $relation->get_this_model()->get_this_model_name(), |
|
| 416 | - implode( |
|
| 417 | - ',', |
|
| 418 | - array_keys( |
|
| 419 | - Capabilities::get_missing_permissions($related_model, $context) |
|
| 420 | - ) |
|
| 421 | - ) |
|
| 422 | - ), |
|
| 423 | - array('status' => 403) |
|
| 424 | - ); |
|
| 425 | - } |
|
| 426 | - $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
| 427 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
| 428 | - $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
| 429 | - . '.' |
|
| 430 | - . $where_condition_key] = $where_condition_value; |
|
| 431 | - } |
|
| 432 | - $query_params['default_where_conditions'] = 'none'; |
|
| 433 | - $query_params['caps'] = $context; |
|
| 434 | - if (! $request->get_header('no_rest_headers')) { |
|
| 435 | - $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
| 436 | - } |
|
| 437 | - /** @type array $results */ |
|
| 438 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
| 439 | - $nice_results = array(); |
|
| 440 | - foreach ($results as $result) { |
|
| 441 | - $nice_result = $this->create_entity_from_wpdb_result( |
|
| 442 | - $relation->get_other_model(), |
|
| 443 | - $result, |
|
| 444 | - $request |
|
| 445 | - ); |
|
| 446 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
| 447 | - //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
| 448 | - //if there are conflicts we prefer the properties from the main model |
|
| 449 | - $join_model_result = $this->create_entity_from_wpdb_result( |
|
| 450 | - $relation->get_join_model(), |
|
| 451 | - $result, |
|
| 452 | - $request |
|
| 453 | - ); |
|
| 454 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
| 455 | - //but keep the meta stuff from the main model |
|
| 456 | - if (isset($nice_result['meta'])) { |
|
| 457 | - $joined_result['meta'] = $nice_result['meta']; |
|
| 458 | - } |
|
| 459 | - $nice_result = $joined_result; |
|
| 460 | - } |
|
| 461 | - $nice_results[] = $nice_result; |
|
| 462 | - } |
|
| 463 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
| 464 | - return array_shift($nice_results); |
|
| 465 | - } else { |
|
| 466 | - return $nice_results; |
|
| 467 | - } |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Gets the collection for given relation object |
|
| 474 | - * The same as Read::get_entities_from_model(), except if the relation |
|
| 475 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
| 476 | - * the join-model-object into the results |
|
| 477 | - * |
|
| 478 | - * @param string $id the ID of the thing we are fetching related stuff from |
|
| 479 | - * @param \EE_Model_Relation_Base $relation |
|
| 480 | - * @param \WP_REST_Request $request |
|
| 481 | - * @return array|\WP_Error |
|
| 482 | - * @throws \EE_Error |
|
| 483 | - */ |
|
| 484 | - public function get_entities_from_relation($id, $relation, $request) |
|
| 485 | - { |
|
| 486 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
| 487 | - throw new \EE_Error( |
|
| 488 | - sprintf( |
|
| 489 | - __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
| 490 | - 'event_espresso'), |
|
| 491 | - $relation->get_this_model()->get_this_model_name() |
|
| 492 | - ) |
|
| 493 | - ); |
|
| 494 | - } |
|
| 495 | - return $this->_get_entities_from_relation( |
|
| 496 | - array( |
|
| 497 | - array( |
|
| 498 | - $relation->get_this_model()->primary_key_name() => $id, |
|
| 499 | - ), |
|
| 500 | - ), |
|
| 501 | - $relation, |
|
| 502 | - $request |
|
| 503 | - ); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * Sets the headers that are based on the model and query params, |
|
| 510 | - * like the total records. This should only be called on the original request |
|
| 511 | - * from the client, not on subsequent internal |
|
| 512 | - * |
|
| 513 | - * @param \EEM_Base $model |
|
| 514 | - * @param array $query_params |
|
| 515 | - * @return void |
|
| 516 | - */ |
|
| 517 | - protected function _set_headers_from_query_params($model, $query_params) |
|
| 518 | - { |
|
| 519 | - $this->_set_debug_info('model query params', $query_params); |
|
| 520 | - $this->_set_debug_info('missing caps', |
|
| 521 | - Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
| 522 | - //normally the limit to a 2-part array, where the 2nd item is the limit |
|
| 523 | - if (! isset($query_params['limit'])) { |
|
| 524 | - $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
| 525 | - } |
|
| 526 | - if (is_array($query_params['limit'])) { |
|
| 527 | - $limit_parts = $query_params['limit']; |
|
| 528 | - } else { |
|
| 529 | - $limit_parts = explode(',', $query_params['limit']); |
|
| 530 | - if (count($limit_parts) == 1) { |
|
| 531 | - $limit_parts = array(0, $limit_parts[0]); |
|
| 532 | - } |
|
| 533 | - } |
|
| 534 | - //remove the group by and having parts of the query, as those will |
|
| 535 | - //make the sql query return an array of values, instead of just a single value |
|
| 536 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
| 537 | - $count = $model->count($query_params, null, true); |
|
| 538 | - $pages = $count / $limit_parts[1]; |
|
| 539 | - $this->_set_response_header('Total', $count, false); |
|
| 540 | - $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
| 541 | - $this->_set_response_header('TotalPages', ceil($pages), false); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Changes database results into REST API entities |
|
| 548 | - * |
|
| 549 | - * @param \EEM_Base $model |
|
| 550 | - * @param array $db_row like results from $wpdb->get_results() |
|
| 551 | - * @param \WP_REST_Request $rest_request |
|
| 552 | - * @param string $deprecated no longer used |
|
| 553 | - * @return array ready for being converted into json for sending to client |
|
| 554 | - */ |
|
| 555 | - public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
| 556 | - { |
|
| 557 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
| 558 | - //ok so this was called in the old style, where the 3rd arg was |
|
| 559 | - //$include, and the 4th arg was $context |
|
| 560 | - //now setup the request just to avoid fatal errors, although we won't be able |
|
| 561 | - //to truly make use of it because it's kinda devoid of info |
|
| 562 | - $rest_request = new \WP_REST_Request(); |
|
| 563 | - $rest_request->set_param('include', $rest_request); |
|
| 564 | - $rest_request->set_param('caps', $deprecated); |
|
| 565 | - } |
|
| 566 | - if ($rest_request->get_param('caps') == null) { |
|
| 567 | - $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
| 568 | - } |
|
| 569 | - $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
| 570 | - $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
| 571 | - $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
| 572 | - $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
| 573 | - $entity_array = apply_filters('FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
| 574 | - $entity_array, $model, $rest_request->get_param('caps'), $rest_request, $this); |
|
| 575 | - $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
| 576 | - $entity_array = apply_filters( |
|
| 577 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
| 578 | - $entity_array, |
|
| 579 | - $model, |
|
| 580 | - $rest_request->get_param('caps'), |
|
| 581 | - $rest_request, |
|
| 582 | - $this |
|
| 583 | - ); |
|
| 584 | - $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
| 585 | - $entity_array, |
|
| 586 | - $model, |
|
| 587 | - $rest_request->get_param('caps'), |
|
| 588 | - $this->get_model_version_info(), |
|
| 589 | - $model->get_index_primary_key_string( |
|
| 590 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
| 591 | - ) |
|
| 592 | - ); |
|
| 593 | - $this->_set_debug_info( |
|
| 594 | - 'inaccessible fields', |
|
| 595 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
| 596 | - ); |
|
| 597 | - return apply_filters( |
|
| 598 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
| 599 | - $result_without_inaccessible_fields, |
|
| 600 | - $model, |
|
| 601 | - $rest_request->get_param('caps') |
|
| 602 | - ); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
| 609 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
| 610 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
| 611 | - * |
|
| 612 | - * @param \EEM_Base $model |
|
| 613 | - * @param array $db_row |
|
| 614 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
| 615 | - */ |
|
| 616 | - protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
| 617 | - { |
|
| 618 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
| 619 | - $result = array_intersect_key($result, |
|
| 620 | - $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
| 621 | - foreach ($result as $field_name => $raw_field_value) { |
|
| 622 | - $field_obj = $model->field_settings_for($field_name); |
|
| 623 | - $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
| 624 | - if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
| 625 | - unset($result[$field_name]); |
|
| 626 | - } elseif ( |
|
| 627 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
| 628 | - ) { |
|
| 629 | - $result[$field_name] = array( |
|
| 630 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
| 631 | - 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
| 632 | - ); |
|
| 633 | - } elseif ( |
|
| 634 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
| 635 | - ) { |
|
| 636 | - $result[$field_name] = array( |
|
| 637 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
| 638 | - 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
| 639 | - ); |
|
| 640 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
| 641 | - if ($field_value instanceof \DateTime) { |
|
| 642 | - $timezone = $field_value->getTimezone(); |
|
| 643 | - $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
| 644 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 645 | - $field_obj, |
|
| 646 | - $field_value, |
|
| 647 | - $this->get_model_version_info()->requested_version() |
|
| 648 | - ); |
|
| 649 | - $field_value->setTimezone($timezone); |
|
| 650 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 651 | - $field_obj, |
|
| 652 | - $field_value, |
|
| 653 | - $this->get_model_version_info()->requested_version() |
|
| 654 | - ); |
|
| 655 | - } |
|
| 656 | - } else { |
|
| 657 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 658 | - $field_obj, |
|
| 659 | - $field_obj->prepare_for_get($field_value), |
|
| 660 | - $this->get_model_version_info()->requested_version() |
|
| 661 | - ); |
|
| 662 | - } |
|
| 663 | - } |
|
| 664 | - return $result; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Adds a few extra fields to the entity response |
|
| 671 | - * |
|
| 672 | - * @param \EEM_Base $model |
|
| 673 | - * @param array $db_row |
|
| 674 | - * @param array $entity_array |
|
| 675 | - * @return array modified entity |
|
| 676 | - */ |
|
| 677 | - protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
| 678 | - { |
|
| 679 | - if ($model instanceof \EEM_CPT_Base) { |
|
| 680 | - $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
| 681 | - } |
|
| 682 | - return $entity_array; |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Gets links we want to add to the response |
|
| 689 | - * |
|
| 690 | - * @global \WP_REST_Server $wp_rest_server |
|
| 691 | - * @param \EEM_Base $model |
|
| 692 | - * @param array $db_row |
|
| 693 | - * @param array $entity_array |
|
| 694 | - * @return array the _links item in the entity |
|
| 695 | - */ |
|
| 696 | - protected function _get_entity_links($model, $db_row, $entity_array) |
|
| 697 | - { |
|
| 698 | - //add basic links |
|
| 699 | - $links = array(); |
|
| 700 | - if ($model->has_primary_key_field()) { |
|
| 701 | - $links['self'] = array( |
|
| 702 | - array( |
|
| 703 | - 'href' => $this->get_versioned_link_to( |
|
| 704 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 705 | - . '/' |
|
| 706 | - . $entity_array[$model->primary_key_name()] |
|
| 707 | - ), |
|
| 708 | - ), |
|
| 709 | - ); |
|
| 710 | - } |
|
| 711 | - $links['collection'] = array( |
|
| 712 | - array( |
|
| 713 | - 'href' => $this->get_versioned_link_to( |
|
| 714 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 715 | - ), |
|
| 716 | - ), |
|
| 717 | - ); |
|
| 718 | - //add links to related models |
|
| 719 | - if ($model->has_primary_key_field()) { |
|
| 720 | - foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
| 721 | - $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
| 722 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
| 723 | - array( |
|
| 724 | - 'href' => $this->get_versioned_link_to( |
|
| 725 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 726 | - . '/' |
|
| 727 | - . $entity_array[$model->primary_key_name()] |
|
| 728 | - . '/' |
|
| 729 | - . $related_model_part |
|
| 730 | - ), |
|
| 731 | - 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
| 732 | - ), |
|
| 733 | - ); |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - return $links; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - |
|
| 741 | - /** |
|
| 742 | - * Adds the included models indicated in the request to the entity provided |
|
| 743 | - * |
|
| 744 | - * @param \EEM_Base $model |
|
| 745 | - * @param \WP_REST_Request $rest_request |
|
| 746 | - * @param array $entity_array |
|
| 747 | - * @param array $db_row |
|
| 748 | - * @return array the modified entity |
|
| 749 | - */ |
|
| 750 | - protected function _include_requested_models( |
|
| 751 | - \EEM_Base $model, |
|
| 752 | - \WP_REST_Request $rest_request, |
|
| 753 | - $entity_array, |
|
| 754 | - $db_row = array() |
|
| 755 | - ) { |
|
| 756 | - //if $db_row not included, hope the entity array has what we need |
|
| 757 | - if (! $db_row) { |
|
| 758 | - $db_row = $entity_array; |
|
| 759 | - } |
|
| 760 | - $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
| 761 | - $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
| 762 | - //if they passed in * or didn't specify any includes, return everything |
|
| 763 | - if (! in_array('*', $includes_for_this_model) |
|
| 764 | - && ! empty($includes_for_this_model) |
|
| 765 | - ) { |
|
| 766 | - if ($model->has_primary_key_field()) { |
|
| 767 | - //always include the primary key. ya just gotta know that at least |
|
| 768 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
| 769 | - } |
|
| 770 | - if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
| 771 | - $includes_for_this_model[] = '_calculated_fields'; |
|
| 772 | - } |
|
| 773 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
| 774 | - } |
|
| 775 | - $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
| 776 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
| 777 | - $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
| 778 | - $rest_request->get_param('include'), |
|
| 779 | - $relation_name |
|
| 780 | - ); |
|
| 781 | - $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
| 782 | - $rest_request->get_param('calculate'), |
|
| 783 | - $relation_name |
|
| 784 | - ); |
|
| 785 | - //did they specify they wanted to include a related model, or |
|
| 786 | - //specific fields from a related model? |
|
| 787 | - //or did they specify to calculate a field from a related model? |
|
| 788 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
| 789 | - //if so, we should include at least some part of the related model |
|
| 790 | - $pretend_related_request = new \WP_REST_Request(); |
|
| 791 | - $pretend_related_request->set_query_params( |
|
| 792 | - array( |
|
| 793 | - 'caps' => $rest_request->get_param('caps'), |
|
| 794 | - 'include' => $related_fields_to_include, |
|
| 795 | - 'calculate' => $related_fields_to_calculate, |
|
| 796 | - ) |
|
| 797 | - ); |
|
| 798 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
| 799 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
| 800 | - $model->get_index_primary_key_string( |
|
| 801 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
| 802 | - ) |
|
| 803 | - ); |
|
| 804 | - $related_results = $this->_get_entities_from_relation( |
|
| 805 | - $primary_model_query_params, |
|
| 806 | - $relation_obj, |
|
| 807 | - $pretend_related_request |
|
| 808 | - ); |
|
| 809 | - $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
| 810 | - instanceof |
|
| 811 | - \WP_Error |
|
| 812 | - ? null |
|
| 813 | - : $related_results; |
|
| 814 | - } |
|
| 815 | - } |
|
| 816 | - return $entity_array; |
|
| 817 | - } |
|
| 818 | - |
|
| 819 | - |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * Returns a new array with all the names of models removed. Eg |
|
| 823 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
| 824 | - * |
|
| 825 | - * @param array $arr |
|
| 826 | - * @return array |
|
| 827 | - */ |
|
| 828 | - private function _remove_model_names_from_array($arr) |
|
| 829 | - { |
|
| 830 | - return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
| 831 | - } |
|
| 832 | - |
|
| 833 | - |
|
| 834 | - |
|
| 835 | - /** |
|
| 836 | - * Gets the calculated fields for the response |
|
| 837 | - * |
|
| 838 | - * @param \EEM_Base $model |
|
| 839 | - * @param array $wpdb_row |
|
| 840 | - * @param \WP_REST_Request $rest_request |
|
| 841 | - * @return \stdClass the _calculations item in the entity |
|
| 842 | - */ |
|
| 843 | - protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
| 844 | - { |
|
| 845 | - $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
| 846 | - $rest_request->get_param('calculate'), |
|
| 847 | - '' |
|
| 848 | - ); |
|
| 849 | - //note: setting calculate=* doesn't do anything |
|
| 850 | - $calculated_fields_to_return = new \stdClass(); |
|
| 851 | - foreach ($calculated_fields as $field_to_calculate) { |
|
| 852 | - try { |
|
| 853 | - $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
| 854 | - null, |
|
| 855 | - $this->_fields_calculator->retrieve_calculated_field_value( |
|
| 856 | - $model, |
|
| 857 | - $field_to_calculate, |
|
| 858 | - $wpdb_row, |
|
| 859 | - $rest_request, |
|
| 860 | - $this |
|
| 861 | - ), |
|
| 862 | - $this->get_model_version_info()->requested_version() |
|
| 863 | - ); |
|
| 864 | - } catch (Rest_Exception $e) { |
|
| 865 | - //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
| 866 | - $this->_set_response_header( |
|
| 867 | - 'Notices-Field-Calculation-Errors[' |
|
| 868 | - . $e->get_string_code() |
|
| 869 | - . '][' |
|
| 870 | - . $model->get_this_model_name() |
|
| 871 | - . '][' |
|
| 872 | - . $field_to_calculate |
|
| 873 | - . ']', |
|
| 874 | - $e->getMessage(), |
|
| 875 | - true |
|
| 876 | - ); |
|
| 877 | - } |
|
| 878 | - } |
|
| 879 | - return $calculated_fields_to_return; |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - |
|
| 883 | - |
|
| 884 | - /** |
|
| 885 | - * Gets the full URL to the resource, taking the requested version into account |
|
| 886 | - * |
|
| 887 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
| 888 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
| 889 | - */ |
|
| 890 | - public function get_versioned_link_to($link_part_after_version_and_slash) |
|
| 891 | - { |
|
| 892 | - return rest_url( |
|
| 893 | - \EED_Core_Rest_Api::ee_api_namespace |
|
| 894 | - . $this->get_model_version_info()->requested_version() |
|
| 895 | - . '/' |
|
| 896 | - . $link_part_after_version_and_slash |
|
| 897 | - ); |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - |
|
| 901 | - |
|
| 902 | - /** |
|
| 903 | - * Gets the correct lowercase name for the relation in the API according |
|
| 904 | - * to the relation's type |
|
| 905 | - * |
|
| 906 | - * @param string $relation_name |
|
| 907 | - * @param \EE_Model_Relation_Base $relation_obj |
|
| 908 | - * @return string |
|
| 909 | - */ |
|
| 910 | - public static function get_related_entity_name($relation_name, $relation_obj) |
|
| 911 | - { |
|
| 912 | - if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
| 913 | - return strtolower($relation_name); |
|
| 914 | - } else { |
|
| 915 | - return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
| 916 | - } |
|
| 917 | - } |
|
| 918 | - |
|
| 919 | - |
|
| 920 | - |
|
| 921 | - /** |
|
| 922 | - * Gets the one model object with the specified id for the specified model |
|
| 923 | - * |
|
| 924 | - * @param \EEM_Base $model |
|
| 925 | - * @param \WP_REST_Request $request |
|
| 926 | - * @return array|\WP_Error |
|
| 927 | - */ |
|
| 928 | - public function get_entity_from_model($model, $request) |
|
| 929 | - { |
|
| 930 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
| 931 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
| 932 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
| 933 | - } |
|
| 934 | - $restricted_query_params = $query_params; |
|
| 935 | - $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
| 936 | - $this->_set_debug_info('model query params', $restricted_query_params); |
|
| 937 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
| 938 | - if (! empty ($model_rows)) { |
|
| 939 | - return $this->create_entity_from_wpdb_result( |
|
| 940 | - $model, |
|
| 941 | - array_shift($model_rows), |
|
| 942 | - $request); |
|
| 943 | - } else { |
|
| 944 | - //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
| 945 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 946 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
| 947 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
| 948 | - //you got shafted- it existed but we didn't want to tell you! |
|
| 949 | - return new \WP_Error( |
|
| 950 | - 'rest_user_cannot_read', |
|
| 951 | - sprintf( |
|
| 952 | - __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
| 953 | - strtolower($model->get_this_model_name()), |
|
| 954 | - Capabilities::get_missing_permissions_string( |
|
| 955 | - $model, |
|
| 956 | - $this->validate_context($request->get_param('caps'))) |
|
| 957 | - ), |
|
| 958 | - array('status' => 403) |
|
| 959 | - ); |
|
| 960 | - } else { |
|
| 961 | - //it's not you. It just doesn't exist |
|
| 962 | - return new \WP_Error( |
|
| 963 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 964 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 965 | - array('status' => 404) |
|
| 966 | - ); |
|
| 967 | - } |
|
| 968 | - } |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - |
|
| 972 | - |
|
| 973 | - /** |
|
| 974 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
| 975 | - * version so just treat it as a default read |
|
| 976 | - * |
|
| 977 | - * @param string $context |
|
| 978 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
| 979 | - */ |
|
| 980 | - public function validate_context($context) |
|
| 981 | - { |
|
| 982 | - if (! $context) { |
|
| 983 | - $context = \EEM_Base::caps_read; |
|
| 984 | - } |
|
| 985 | - $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
| 986 | - if (in_array($context, $valid_contexts)) { |
|
| 987 | - return $context; |
|
| 988 | - } else { |
|
| 989 | - return \EEM_Base::caps_read; |
|
| 990 | - } |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - |
|
| 994 | - |
|
| 995 | - /** |
|
| 996 | - * Verifies the passed in value is an allowable default where conditions value. |
|
| 997 | - * |
|
| 998 | - * @param $default_query_params |
|
| 999 | - * @return string |
|
| 1000 | - */ |
|
| 1001 | - public function validate_default_query_params($default_query_params) |
|
| 1002 | - { |
|
| 1003 | - $valid_default_where_conditions_for_api_calls = array( |
|
| 1004 | - \EEM_Base::default_where_conditions_all, |
|
| 1005 | - \EEM_Base::default_where_conditions_minimum_all, |
|
| 1006 | - \EEM_Base::default_where_conditions_minimum_others, |
|
| 1007 | - ); |
|
| 1008 | - if (! $default_query_params) { |
|
| 1009 | - $default_query_params = \EEM_Base::default_where_conditions_all; |
|
| 1010 | - } |
|
| 1011 | - if ( |
|
| 1012 | - in_array( |
|
| 1013 | - $default_query_params, |
|
| 1014 | - $valid_default_where_conditions_for_api_calls, |
|
| 1015 | - true |
|
| 1016 | - ) |
|
| 1017 | - ) { |
|
| 1018 | - return $default_query_params; |
|
| 1019 | - } else { |
|
| 1020 | - return \EEM_Base::default_where_conditions_all; |
|
| 1021 | - } |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - |
|
| 1025 | - |
|
| 1026 | - /** |
|
| 1027 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
| 1028 | - * Note: right now the query parameter keys for fields (and related fields) |
|
| 1029 | - * can be left as-is, but it's quite possible this will change someday. |
|
| 1030 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
| 1031 | - * |
|
| 1032 | - * @param \EEM_Base $model |
|
| 1033 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
| 1034 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
| 1035 | - * that absolutely no results should be returned |
|
| 1036 | - * @throws \EE_Error |
|
| 1037 | - */ |
|
| 1038 | - public function create_model_query_params($model, $query_parameters) |
|
| 1039 | - { |
|
| 1040 | - $model_query_params = array(); |
|
| 1041 | - if (isset($query_parameters['where'])) { |
|
| 1042 | - $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
| 1043 | - $query_parameters['where'], |
|
| 1044 | - $model, |
|
| 1045 | - $this->get_model_version_info()->requested_version() |
|
| 1046 | - ); |
|
| 1047 | - } |
|
| 1048 | - if (isset($query_parameters['order_by'])) { |
|
| 1049 | - $order_by = $query_parameters['order_by']; |
|
| 1050 | - } elseif (isset($query_parameters['orderby'])) { |
|
| 1051 | - $order_by = $query_parameters['orderby']; |
|
| 1052 | - } else { |
|
| 1053 | - $order_by = null; |
|
| 1054 | - } |
|
| 1055 | - if ($order_by !== null) { |
|
| 1056 | - if (is_array($order_by)) { |
|
| 1057 | - $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
| 1058 | - } else { |
|
| 1059 | - //it's a single item |
|
| 1060 | - $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
| 1061 | - } |
|
| 1062 | - $model_query_params['order_by'] = $order_by; |
|
| 1063 | - } |
|
| 1064 | - if (isset($query_parameters['group_by'])) { |
|
| 1065 | - $group_by = $query_parameters['group_by']; |
|
| 1066 | - } elseif (isset($query_parameters['groupby'])) { |
|
| 1067 | - $group_by = $query_parameters['groupby']; |
|
| 1068 | - } else { |
|
| 1069 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
| 1070 | - } |
|
| 1071 | - //make sure they're all real names |
|
| 1072 | - if (is_array($group_by)) { |
|
| 1073 | - $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
| 1074 | - } |
|
| 1075 | - if ($group_by !== null) { |
|
| 1076 | - $model_query_params['group_by'] = $group_by; |
|
| 1077 | - } |
|
| 1078 | - if (isset($query_parameters['having'])) { |
|
| 1079 | - $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
| 1080 | - $query_parameters['having'], |
|
| 1081 | - $model, |
|
| 1082 | - $this->get_model_version_info()->requested_version() |
|
| 1083 | - ); |
|
| 1084 | - } |
|
| 1085 | - if (isset($query_parameters['order'])) { |
|
| 1086 | - $model_query_params['order'] = $query_parameters['order']; |
|
| 1087 | - } |
|
| 1088 | - if (isset($query_parameters['mine'])) { |
|
| 1089 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
| 1090 | - } |
|
| 1091 | - if (isset($query_parameters['limit'])) { |
|
| 1092 | - //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
| 1093 | - if (! is_array($query_parameters['limit'])) { |
|
| 1094 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
| 1095 | - } else { |
|
| 1096 | - $limit_array = $query_parameters['limit']; |
|
| 1097 | - } |
|
| 1098 | - $sanitized_limit = array(); |
|
| 1099 | - foreach ($limit_array as $key => $limit_part) { |
|
| 1100 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
| 1101 | - throw new \EE_Error( |
|
| 1102 | - sprintf( |
|
| 1103 | - __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
| 1104 | - 'event_espresso'), |
|
| 1105 | - wp_json_encode($query_parameters['limit']) |
|
| 1106 | - ) |
|
| 1107 | - ); |
|
| 1108 | - } |
|
| 1109 | - $sanitized_limit[] = (int)$limit_part; |
|
| 1110 | - } |
|
| 1111 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
| 1112 | - } else { |
|
| 1113 | - $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
| 1114 | - } |
|
| 1115 | - if (isset($query_parameters['caps'])) { |
|
| 1116 | - $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
| 1117 | - } else { |
|
| 1118 | - $model_query_params['caps'] = \EEM_Base::caps_read; |
|
| 1119 | - } |
|
| 1120 | - if (isset($query_parameters['default_where_conditions'])) { |
|
| 1121 | - $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
| 1122 | - } |
|
| 1123 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - |
|
| 1127 | - |
|
| 1128 | - /** |
|
| 1129 | - * Changes the REST-style query params for use in the models |
|
| 1130 | - * |
|
| 1131 | - * @deprecated |
|
| 1132 | - * @param \EEM_Base $model |
|
| 1133 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
| 1134 | - * @return array |
|
| 1135 | - */ |
|
| 1136 | - public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
| 1137 | - { |
|
| 1138 | - $model_ready_query_params = array(); |
|
| 1139 | - foreach ($query_params as $key => $value) { |
|
| 1140 | - if (is_array($value)) { |
|
| 1141 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
| 1142 | - } else { |
|
| 1143 | - $model_ready_query_params[$key] = $value; |
|
| 1144 | - } |
|
| 1145 | - } |
|
| 1146 | - return $model_ready_query_params; |
|
| 1147 | - } |
|
| 1148 | - |
|
| 1149 | - |
|
| 1150 | - |
|
| 1151 | - /** |
|
| 1152 | - * @deprecated |
|
| 1153 | - * @param $model |
|
| 1154 | - * @param $query_params |
|
| 1155 | - * @return array |
|
| 1156 | - */ |
|
| 1157 | - public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
| 1158 | - { |
|
| 1159 | - $model_ready_query_params = array(); |
|
| 1160 | - foreach ($query_params as $key => $value) { |
|
| 1161 | - if (is_array($value)) { |
|
| 1162 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
| 1163 | - } else { |
|
| 1164 | - $model_ready_query_params[$key] = $value; |
|
| 1165 | - } |
|
| 1166 | - } |
|
| 1167 | - return $model_ready_query_params; |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - |
|
| 1171 | - |
|
| 1172 | - /** |
|
| 1173 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
| 1174 | - * If no prefix is specified, returns items with no period. |
|
| 1175 | - * |
|
| 1176 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
| 1177 | - * @param string $prefix "Event" or "foobar" |
|
| 1178 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
| 1179 | - * we only return strings starting with that and a period; if no prefix was |
|
| 1180 | - * specified we return all items containing NO periods |
|
| 1181 | - */ |
|
| 1182 | - public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
| 1183 | - { |
|
| 1184 | - if (is_string($string_to_explode)) { |
|
| 1185 | - $exploded_contents = explode(',', $string_to_explode); |
|
| 1186 | - } else if (is_array($string_to_explode)) { |
|
| 1187 | - $exploded_contents = $string_to_explode; |
|
| 1188 | - } else { |
|
| 1189 | - $exploded_contents = array(); |
|
| 1190 | - } |
|
| 1191 | - //if the string was empty, we want an empty array |
|
| 1192 | - $exploded_contents = array_filter($exploded_contents); |
|
| 1193 | - $contents_with_prefix = array(); |
|
| 1194 | - foreach ($exploded_contents as $item) { |
|
| 1195 | - $item = trim($item); |
|
| 1196 | - //if no prefix was provided, so we look for items with no "." in them |
|
| 1197 | - if (! $prefix) { |
|
| 1198 | - //does this item have a period? |
|
| 1199 | - if (strpos($item, '.') === false) { |
|
| 1200 | - //if not, then its what we're looking for |
|
| 1201 | - $contents_with_prefix[] = $item; |
|
| 1202 | - } |
|
| 1203 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
| 1204 | - //this item has the prefix and a period, grab it |
|
| 1205 | - $contents_with_prefix[] = substr( |
|
| 1206 | - $item, |
|
| 1207 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
| 1208 | - ); |
|
| 1209 | - } else if ($item === $prefix) { |
|
| 1210 | - //this item is JUST the prefix |
|
| 1211 | - //so let's grab everything after, which is a blank string |
|
| 1212 | - $contents_with_prefix[] = ''; |
|
| 1213 | - } |
|
| 1214 | - } |
|
| 1215 | - return $contents_with_prefix; |
|
| 1216 | - } |
|
| 1217 | - |
|
| 1218 | - |
|
| 1219 | - |
|
| 1220 | - /** |
|
| 1221 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
| 1222 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
| 1223 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
| 1224 | - * array('*') (when you provided a model and a model of that kind was found). |
|
| 1225 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
| 1226 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
| 1227 | - * name and then a period). |
|
| 1228 | - * @param string $include_string @see Read:handle_request_get_all |
|
| 1229 | - * @param string $model_name |
|
| 1230 | - * @return array of fields for this model. If $model_name is provided, then |
|
| 1231 | - * the fields for that model, with the model's name removed from each. |
|
| 1232 | - * If $include_string was blank or '*' returns an empty array |
|
| 1233 | - */ |
|
| 1234 | - public function extract_includes_for_this_model($include_string, $model_name = null) |
|
| 1235 | - { |
|
| 1236 | - if (is_array($include_string)) { |
|
| 1237 | - $include_string = implode(',', $include_string); |
|
| 1238 | - } |
|
| 1239 | - if ($include_string === '*' || $include_string === '') { |
|
| 1240 | - return array(); |
|
| 1241 | - } |
|
| 1242 | - $includes = explode(',', $include_string); |
|
| 1243 | - $extracted_fields_to_include = array(); |
|
| 1244 | - if ($model_name) { |
|
| 1245 | - foreach ($includes as $field_to_include) { |
|
| 1246 | - $field_to_include = trim($field_to_include); |
|
| 1247 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
| 1248 | - //found the model name at the exact start |
|
| 1249 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
| 1250 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
| 1251 | - } elseif ($field_to_include == $model_name) { |
|
| 1252 | - $extracted_fields_to_include[] = '*'; |
|
| 1253 | - } |
|
| 1254 | - } |
|
| 1255 | - } else { |
|
| 1256 | - //look for ones with no period |
|
| 1257 | - foreach ($includes as $field_to_include) { |
|
| 1258 | - $field_to_include = trim($field_to_include); |
|
| 1259 | - if ( |
|
| 1260 | - strpos($field_to_include, '.') === false |
|
| 1261 | - && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
| 1262 | - ) { |
|
| 1263 | - $extracted_fields_to_include[] = $field_to_include; |
|
| 1264 | - } |
|
| 1265 | - } |
|
| 1266 | - } |
|
| 1267 | - return $extracted_fields_to_include; |
|
| 1268 | - } |
|
| 30 | + /** |
|
| 31 | + * @var Calculated_Model_Fields |
|
| 32 | + */ |
|
| 33 | + protected $_fields_calculator; |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Read constructor. |
|
| 39 | + */ |
|
| 40 | + public function __construct() |
|
| 41 | + { |
|
| 42 | + parent::__construct(); |
|
| 43 | + $this->_fields_calculator = new Calculated_Model_Fields(); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
| 50 | + * |
|
| 51 | + * @param \WP_REST_Request $request |
|
| 52 | + * @return \WP_REST_Response|\WP_Error |
|
| 53 | + */ |
|
| 54 | + public static function handle_request_get_all(\WP_REST_Request $request) |
|
| 55 | + { |
|
| 56 | + $controller = new Read(); |
|
| 57 | + try { |
|
| 58 | + $matches = $controller->parse_route( |
|
| 59 | + $request->get_route(), |
|
| 60 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
| 61 | + array('version', 'model') |
|
| 62 | + ); |
|
| 63 | + $controller->set_requested_version($matches['version']); |
|
| 64 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
| 65 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 66 | + return $controller->send_response( |
|
| 67 | + new \WP_Error( |
|
| 68 | + 'endpoint_parsing_error', |
|
| 69 | + sprintf( |
|
| 70 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 71 | + 'event_espresso'), |
|
| 72 | + $model_name_singular |
|
| 73 | + ) |
|
| 74 | + ) |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + return $controller->send_response( |
|
| 78 | + $controller->get_entities_from_model( |
|
| 79 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
| 80 | + $request |
|
| 81 | + ) |
|
| 82 | + ); |
|
| 83 | + } catch (\Exception $e) { |
|
| 84 | + return $controller->send_response($e); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Prepares and returns schema for any OPTIONS request. |
|
| 92 | + * |
|
| 93 | + * @param string $model_name Something like `Event` or `Registration` |
|
| 94 | + * @param string $version The API endpoint version being used. |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + public static function handle_schema_request($model_name, $version) |
|
| 98 | + { |
|
| 99 | + $controller = new Read(); |
|
| 100 | + try { |
|
| 101 | + $controller->set_requested_version($version); |
|
| 102 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
| 103 | + return array(); |
|
| 104 | + } |
|
| 105 | + //get the model for this version |
|
| 106 | + $model = $controller->get_model_version_info()->load_model($model_name); |
|
| 107 | + $model_schema = new JsonModelSchema($model); |
|
| 108 | + return $model_schema->getModelSchemaForRelations( |
|
| 109 | + $controller->get_model_version_info()->relation_settings($model), |
|
| 110 | + $controller->_customize_schema_for_rest_response( |
|
| 111 | + $model, |
|
| 112 | + $model_schema->getModelSchemaForFields( |
|
| 113 | + $controller->get_model_version_info()->fields_on_model_in_this_version($model), |
|
| 114 | + $model_schema->getInitialSchemaStructure() |
|
| 115 | + ) |
|
| 116 | + ) |
|
| 117 | + ); |
|
| 118 | + } catch (\Exception $e) { |
|
| 119 | + return array(); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * This loops through each field in the given schema for the model and does the following: |
|
| 127 | + * - add any extra fields that are REST API specific and related to existing fields. |
|
| 128 | + * - transform default values into the correct format for a REST API response. |
|
| 129 | + * |
|
| 130 | + * @param \EEM_Base $model |
|
| 131 | + * @param array $schema |
|
| 132 | + * @return array The final schema. |
|
| 133 | + */ |
|
| 134 | + protected function _customize_schema_for_rest_response(\EEM_Base $model, array $schema) |
|
| 135 | + { |
|
| 136 | + foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field) { |
|
| 137 | + $schema = $this->_translate_defaults_for_rest_response( |
|
| 138 | + $field_name, |
|
| 139 | + $field, |
|
| 140 | + $this->_maybe_add_extra_fields_to_schema($field_name, $field, $schema) |
|
| 141 | + ); |
|
| 142 | + } |
|
| 143 | + return $schema; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
| 150 | + * response. |
|
| 151 | + * |
|
| 152 | + * @param $field_name |
|
| 153 | + * @param \EE_Model_Field_Base $field |
|
| 154 | + * @param array $schema |
|
| 155 | + * @return array |
|
| 156 | + */ |
|
| 157 | + protected function _translate_defaults_for_rest_response($field_name, \EE_Model_Field_Base $field, array $schema) |
|
| 158 | + { |
|
| 159 | + if (isset($schema['properties'][$field_name]['default'])) { |
|
| 160 | + if (is_array($schema['properties'][$field_name]['default'])) { |
|
| 161 | + foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
| 162 | + if ($default_key === 'raw') { |
|
| 163 | + $schema['properties'][$field_name]['default'][$default_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 164 | + $field, |
|
| 165 | + $default_value, |
|
| 166 | + $this->get_model_version_info()->requested_version() |
|
| 167 | + ); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + } else { |
|
| 171 | + $schema['properties'][$field_name]['default'] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 172 | + $field, |
|
| 173 | + $schema['properties'][$field_name]['default'], |
|
| 174 | + $this->get_model_version_info()->requested_version() |
|
| 175 | + ); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + return $schema; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Adds additional fields to the schema |
|
| 185 | + * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
| 186 | + * needs to be added to the schema. |
|
| 187 | + * |
|
| 188 | + * @param $field_name |
|
| 189 | + * @param \EE_Model_Field_Base $field |
|
| 190 | + * @param array $schema |
|
| 191 | + * @return array |
|
| 192 | + */ |
|
| 193 | + protected function _maybe_add_extra_fields_to_schema($field_name, \EE_Model_Field_Base $field, array $schema) |
|
| 194 | + { |
|
| 195 | + if ($field instanceof EE_Datetime_Field) { |
|
| 196 | + $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
| 197 | + //modify the description |
|
| 198 | + $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
| 199 | + esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
| 200 | + $field->get_nicename() |
|
| 201 | + ); |
|
| 202 | + } |
|
| 203 | + return $schema; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
| 210 | + * |
|
| 211 | + * @return string |
|
| 212 | + */ |
|
| 213 | + protected function get_route_from_request() |
|
| 214 | + { |
|
| 215 | + if (isset($GLOBALS['wp']) |
|
| 216 | + && $GLOBALS['wp'] instanceof \WP |
|
| 217 | + && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
| 218 | + ) { |
|
| 219 | + return $GLOBALS['wp']->query_vars['rest_route']; |
|
| 220 | + } else { |
|
| 221 | + return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Gets a single entity related to the model indicated in the path and its id |
|
| 229 | + * |
|
| 230 | + * @param \WP_REST_Request $request |
|
| 231 | + * @return \WP_REST_Response|\WP_Error |
|
| 232 | + */ |
|
| 233 | + public static function handle_request_get_one(\WP_REST_Request $request) |
|
| 234 | + { |
|
| 235 | + $controller = new Read(); |
|
| 236 | + try { |
|
| 237 | + $matches = $controller->parse_route( |
|
| 238 | + $request->get_route(), |
|
| 239 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
| 240 | + array('version', 'model', 'id')); |
|
| 241 | + $controller->set_requested_version($matches['version']); |
|
| 242 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
| 243 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 244 | + return $controller->send_response( |
|
| 245 | + new \WP_Error( |
|
| 246 | + 'endpoint_parsing_error', |
|
| 247 | + sprintf( |
|
| 248 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 249 | + 'event_espresso'), |
|
| 250 | + $model_name_singular |
|
| 251 | + ) |
|
| 252 | + ) |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + return $controller->send_response( |
|
| 256 | + $controller->get_entity_from_model( |
|
| 257 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
| 258 | + $request |
|
| 259 | + ) |
|
| 260 | + ); |
|
| 261 | + } catch (\Exception $e) { |
|
| 262 | + return $controller->send_response($e); |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
| 270 | + * to the item with the given id |
|
| 271 | + * |
|
| 272 | + * @param \WP_REST_Request $request |
|
| 273 | + * @return \WP_REST_Response|\WP_Error |
|
| 274 | + */ |
|
| 275 | + public static function handle_request_get_related(\WP_REST_Request $request) |
|
| 276 | + { |
|
| 277 | + $controller = new Read(); |
|
| 278 | + try { |
|
| 279 | + $matches = $controller->parse_route( |
|
| 280 | + $request->get_route(), |
|
| 281 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
| 282 | + array('version', 'model', 'id', 'related_model') |
|
| 283 | + ); |
|
| 284 | + $controller->set_requested_version($matches['version']); |
|
| 285 | + $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
| 286 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
| 287 | + return $controller->send_response( |
|
| 288 | + new \WP_Error( |
|
| 289 | + 'endpoint_parsing_error', |
|
| 290 | + sprintf( |
|
| 291 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 292 | + 'event_espresso'), |
|
| 293 | + $main_model_name_singular |
|
| 294 | + ) |
|
| 295 | + ) |
|
| 296 | + ); |
|
| 297 | + } |
|
| 298 | + $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
| 299 | + //assume the related model name is plural and try to find the model's name |
|
| 300 | + $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
| 301 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 302 | + //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
| 303 | + $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
| 304 | + } |
|
| 305 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 306 | + return $controller->send_response( |
|
| 307 | + new \WP_Error( |
|
| 308 | + 'endpoint_parsing_error', |
|
| 309 | + sprintf( |
|
| 310 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
| 311 | + 'event_espresso'), |
|
| 312 | + $related_model_name_singular |
|
| 313 | + ) |
|
| 314 | + ) |
|
| 315 | + ); |
|
| 316 | + } |
|
| 317 | + return $controller->send_response( |
|
| 318 | + $controller->get_entities_from_relation( |
|
| 319 | + $request->get_param('id'), |
|
| 320 | + $main_model->related_settings_for($related_model_name_singular), |
|
| 321 | + $request |
|
| 322 | + ) |
|
| 323 | + ); |
|
| 324 | + } catch (\Exception $e) { |
|
| 325 | + return $controller->send_response($e); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Gets a collection for the given model and filters |
|
| 333 | + * |
|
| 334 | + * @param \EEM_Base $model |
|
| 335 | + * @param \WP_REST_Request $request |
|
| 336 | + * @return array|\WP_Error |
|
| 337 | + */ |
|
| 338 | + public function get_entities_from_model($model, $request) |
|
| 339 | + { |
|
| 340 | + $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
| 341 | + if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
| 342 | + $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 343 | + return new \WP_Error( |
|
| 344 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
| 345 | + sprintf( |
|
| 346 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
| 347 | + $model_name_plural, |
|
| 348 | + Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
| 349 | + ), |
|
| 350 | + array('status' => 403) |
|
| 351 | + ); |
|
| 352 | + } |
|
| 353 | + if (! $request->get_header('no_rest_headers')) { |
|
| 354 | + $this->_set_headers_from_query_params($model, $query_params); |
|
| 355 | + } |
|
| 356 | + /** @type array $results */ |
|
| 357 | + $results = $model->get_all_wpdb_results($query_params); |
|
| 358 | + $nice_results = array(); |
|
| 359 | + foreach ($results as $result) { |
|
| 360 | + $nice_results[] = $this->create_entity_from_wpdb_result( |
|
| 361 | + $model, |
|
| 362 | + $result, |
|
| 363 | + $request |
|
| 364 | + ); |
|
| 365 | + } |
|
| 366 | + return $nice_results; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * @param array $primary_model_query_params query params for finding the item from which |
|
| 373 | + * relations will be based |
|
| 374 | + * @param \EE_Model_Relation_Base $relation |
|
| 375 | + * @param \WP_REST_Request $request |
|
| 376 | + * @return \WP_Error|array |
|
| 377 | + */ |
|
| 378 | + protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
| 379 | + { |
|
| 380 | + $context = $this->validate_context($request->get_param('caps')); |
|
| 381 | + $model = $relation->get_this_model(); |
|
| 382 | + $related_model = $relation->get_other_model(); |
|
| 383 | + if (! isset($primary_model_query_params[0])) { |
|
| 384 | + $primary_model_query_params[0] = array(); |
|
| 385 | + } |
|
| 386 | + //check if they can access the 1st model object |
|
| 387 | + $primary_model_query_params = array( |
|
| 388 | + 0 => $primary_model_query_params[0], |
|
| 389 | + 'limit' => 1, |
|
| 390 | + ); |
|
| 391 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
| 392 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
| 393 | + } |
|
| 394 | + $restricted_query_params = $primary_model_query_params; |
|
| 395 | + $restricted_query_params['caps'] = $context; |
|
| 396 | + $this->_set_debug_info('main model query params', $restricted_query_params); |
|
| 397 | + $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
| 398 | + if ( |
|
| 399 | + ! ( |
|
| 400 | + Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
| 401 | + && $model->exists($restricted_query_params) |
|
| 402 | + ) |
|
| 403 | + ) { |
|
| 404 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
| 405 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
| 406 | + } else { |
|
| 407 | + $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
| 408 | + } |
|
| 409 | + return new \WP_Error( |
|
| 410 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
| 411 | + sprintf( |
|
| 412 | + __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
| 413 | + 'event_espresso'), |
|
| 414 | + $related_model_name_maybe_plural, |
|
| 415 | + $relation->get_this_model()->get_this_model_name(), |
|
| 416 | + implode( |
|
| 417 | + ',', |
|
| 418 | + array_keys( |
|
| 419 | + Capabilities::get_missing_permissions($related_model, $context) |
|
| 420 | + ) |
|
| 421 | + ) |
|
| 422 | + ), |
|
| 423 | + array('status' => 403) |
|
| 424 | + ); |
|
| 425 | + } |
|
| 426 | + $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
| 427 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
| 428 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
| 429 | + . '.' |
|
| 430 | + . $where_condition_key] = $where_condition_value; |
|
| 431 | + } |
|
| 432 | + $query_params['default_where_conditions'] = 'none'; |
|
| 433 | + $query_params['caps'] = $context; |
|
| 434 | + if (! $request->get_header('no_rest_headers')) { |
|
| 435 | + $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
| 436 | + } |
|
| 437 | + /** @type array $results */ |
|
| 438 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
| 439 | + $nice_results = array(); |
|
| 440 | + foreach ($results as $result) { |
|
| 441 | + $nice_result = $this->create_entity_from_wpdb_result( |
|
| 442 | + $relation->get_other_model(), |
|
| 443 | + $result, |
|
| 444 | + $request |
|
| 445 | + ); |
|
| 446 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
| 447 | + //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
| 448 | + //if there are conflicts we prefer the properties from the main model |
|
| 449 | + $join_model_result = $this->create_entity_from_wpdb_result( |
|
| 450 | + $relation->get_join_model(), |
|
| 451 | + $result, |
|
| 452 | + $request |
|
| 453 | + ); |
|
| 454 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
| 455 | + //but keep the meta stuff from the main model |
|
| 456 | + if (isset($nice_result['meta'])) { |
|
| 457 | + $joined_result['meta'] = $nice_result['meta']; |
|
| 458 | + } |
|
| 459 | + $nice_result = $joined_result; |
|
| 460 | + } |
|
| 461 | + $nice_results[] = $nice_result; |
|
| 462 | + } |
|
| 463 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
| 464 | + return array_shift($nice_results); |
|
| 465 | + } else { |
|
| 466 | + return $nice_results; |
|
| 467 | + } |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Gets the collection for given relation object |
|
| 474 | + * The same as Read::get_entities_from_model(), except if the relation |
|
| 475 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
| 476 | + * the join-model-object into the results |
|
| 477 | + * |
|
| 478 | + * @param string $id the ID of the thing we are fetching related stuff from |
|
| 479 | + * @param \EE_Model_Relation_Base $relation |
|
| 480 | + * @param \WP_REST_Request $request |
|
| 481 | + * @return array|\WP_Error |
|
| 482 | + * @throws \EE_Error |
|
| 483 | + */ |
|
| 484 | + public function get_entities_from_relation($id, $relation, $request) |
|
| 485 | + { |
|
| 486 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
| 487 | + throw new \EE_Error( |
|
| 488 | + sprintf( |
|
| 489 | + __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
| 490 | + 'event_espresso'), |
|
| 491 | + $relation->get_this_model()->get_this_model_name() |
|
| 492 | + ) |
|
| 493 | + ); |
|
| 494 | + } |
|
| 495 | + return $this->_get_entities_from_relation( |
|
| 496 | + array( |
|
| 497 | + array( |
|
| 498 | + $relation->get_this_model()->primary_key_name() => $id, |
|
| 499 | + ), |
|
| 500 | + ), |
|
| 501 | + $relation, |
|
| 502 | + $request |
|
| 503 | + ); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * Sets the headers that are based on the model and query params, |
|
| 510 | + * like the total records. This should only be called on the original request |
|
| 511 | + * from the client, not on subsequent internal |
|
| 512 | + * |
|
| 513 | + * @param \EEM_Base $model |
|
| 514 | + * @param array $query_params |
|
| 515 | + * @return void |
|
| 516 | + */ |
|
| 517 | + protected function _set_headers_from_query_params($model, $query_params) |
|
| 518 | + { |
|
| 519 | + $this->_set_debug_info('model query params', $query_params); |
|
| 520 | + $this->_set_debug_info('missing caps', |
|
| 521 | + Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
| 522 | + //normally the limit to a 2-part array, where the 2nd item is the limit |
|
| 523 | + if (! isset($query_params['limit'])) { |
|
| 524 | + $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
| 525 | + } |
|
| 526 | + if (is_array($query_params['limit'])) { |
|
| 527 | + $limit_parts = $query_params['limit']; |
|
| 528 | + } else { |
|
| 529 | + $limit_parts = explode(',', $query_params['limit']); |
|
| 530 | + if (count($limit_parts) == 1) { |
|
| 531 | + $limit_parts = array(0, $limit_parts[0]); |
|
| 532 | + } |
|
| 533 | + } |
|
| 534 | + //remove the group by and having parts of the query, as those will |
|
| 535 | + //make the sql query return an array of values, instead of just a single value |
|
| 536 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
| 537 | + $count = $model->count($query_params, null, true); |
|
| 538 | + $pages = $count / $limit_parts[1]; |
|
| 539 | + $this->_set_response_header('Total', $count, false); |
|
| 540 | + $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
| 541 | + $this->_set_response_header('TotalPages', ceil($pages), false); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Changes database results into REST API entities |
|
| 548 | + * |
|
| 549 | + * @param \EEM_Base $model |
|
| 550 | + * @param array $db_row like results from $wpdb->get_results() |
|
| 551 | + * @param \WP_REST_Request $rest_request |
|
| 552 | + * @param string $deprecated no longer used |
|
| 553 | + * @return array ready for being converted into json for sending to client |
|
| 554 | + */ |
|
| 555 | + public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
| 556 | + { |
|
| 557 | + if (! $rest_request instanceof \WP_REST_Request) { |
|
| 558 | + //ok so this was called in the old style, where the 3rd arg was |
|
| 559 | + //$include, and the 4th arg was $context |
|
| 560 | + //now setup the request just to avoid fatal errors, although we won't be able |
|
| 561 | + //to truly make use of it because it's kinda devoid of info |
|
| 562 | + $rest_request = new \WP_REST_Request(); |
|
| 563 | + $rest_request->set_param('include', $rest_request); |
|
| 564 | + $rest_request->set_param('caps', $deprecated); |
|
| 565 | + } |
|
| 566 | + if ($rest_request->get_param('caps') == null) { |
|
| 567 | + $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
| 568 | + } |
|
| 569 | + $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
| 570 | + $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
| 571 | + $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
| 572 | + $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
| 573 | + $entity_array = apply_filters('FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
| 574 | + $entity_array, $model, $rest_request->get_param('caps'), $rest_request, $this); |
|
| 575 | + $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
| 576 | + $entity_array = apply_filters( |
|
| 577 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
| 578 | + $entity_array, |
|
| 579 | + $model, |
|
| 580 | + $rest_request->get_param('caps'), |
|
| 581 | + $rest_request, |
|
| 582 | + $this |
|
| 583 | + ); |
|
| 584 | + $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
| 585 | + $entity_array, |
|
| 586 | + $model, |
|
| 587 | + $rest_request->get_param('caps'), |
|
| 588 | + $this->get_model_version_info(), |
|
| 589 | + $model->get_index_primary_key_string( |
|
| 590 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
| 591 | + ) |
|
| 592 | + ); |
|
| 593 | + $this->_set_debug_info( |
|
| 594 | + 'inaccessible fields', |
|
| 595 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
| 596 | + ); |
|
| 597 | + return apply_filters( |
|
| 598 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
| 599 | + $result_without_inaccessible_fields, |
|
| 600 | + $model, |
|
| 601 | + $rest_request->get_param('caps') |
|
| 602 | + ); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
| 609 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
| 610 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
| 611 | + * |
|
| 612 | + * @param \EEM_Base $model |
|
| 613 | + * @param array $db_row |
|
| 614 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
| 615 | + */ |
|
| 616 | + protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
| 617 | + { |
|
| 618 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
| 619 | + $result = array_intersect_key($result, |
|
| 620 | + $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
| 621 | + foreach ($result as $field_name => $raw_field_value) { |
|
| 622 | + $field_obj = $model->field_settings_for($field_name); |
|
| 623 | + $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
| 624 | + if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
| 625 | + unset($result[$field_name]); |
|
| 626 | + } elseif ( |
|
| 627 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
| 628 | + ) { |
|
| 629 | + $result[$field_name] = array( |
|
| 630 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
| 631 | + 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
| 632 | + ); |
|
| 633 | + } elseif ( |
|
| 634 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
| 635 | + ) { |
|
| 636 | + $result[$field_name] = array( |
|
| 637 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
| 638 | + 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
| 639 | + ); |
|
| 640 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
| 641 | + if ($field_value instanceof \DateTime) { |
|
| 642 | + $timezone = $field_value->getTimezone(); |
|
| 643 | + $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
| 644 | + $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 645 | + $field_obj, |
|
| 646 | + $field_value, |
|
| 647 | + $this->get_model_version_info()->requested_version() |
|
| 648 | + ); |
|
| 649 | + $field_value->setTimezone($timezone); |
|
| 650 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 651 | + $field_obj, |
|
| 652 | + $field_value, |
|
| 653 | + $this->get_model_version_info()->requested_version() |
|
| 654 | + ); |
|
| 655 | + } |
|
| 656 | + } else { |
|
| 657 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 658 | + $field_obj, |
|
| 659 | + $field_obj->prepare_for_get($field_value), |
|
| 660 | + $this->get_model_version_info()->requested_version() |
|
| 661 | + ); |
|
| 662 | + } |
|
| 663 | + } |
|
| 664 | + return $result; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Adds a few extra fields to the entity response |
|
| 671 | + * |
|
| 672 | + * @param \EEM_Base $model |
|
| 673 | + * @param array $db_row |
|
| 674 | + * @param array $entity_array |
|
| 675 | + * @return array modified entity |
|
| 676 | + */ |
|
| 677 | + protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
| 678 | + { |
|
| 679 | + if ($model instanceof \EEM_CPT_Base) { |
|
| 680 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
| 681 | + } |
|
| 682 | + return $entity_array; |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Gets links we want to add to the response |
|
| 689 | + * |
|
| 690 | + * @global \WP_REST_Server $wp_rest_server |
|
| 691 | + * @param \EEM_Base $model |
|
| 692 | + * @param array $db_row |
|
| 693 | + * @param array $entity_array |
|
| 694 | + * @return array the _links item in the entity |
|
| 695 | + */ |
|
| 696 | + protected function _get_entity_links($model, $db_row, $entity_array) |
|
| 697 | + { |
|
| 698 | + //add basic links |
|
| 699 | + $links = array(); |
|
| 700 | + if ($model->has_primary_key_field()) { |
|
| 701 | + $links['self'] = array( |
|
| 702 | + array( |
|
| 703 | + 'href' => $this->get_versioned_link_to( |
|
| 704 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 705 | + . '/' |
|
| 706 | + . $entity_array[$model->primary_key_name()] |
|
| 707 | + ), |
|
| 708 | + ), |
|
| 709 | + ); |
|
| 710 | + } |
|
| 711 | + $links['collection'] = array( |
|
| 712 | + array( |
|
| 713 | + 'href' => $this->get_versioned_link_to( |
|
| 714 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 715 | + ), |
|
| 716 | + ), |
|
| 717 | + ); |
|
| 718 | + //add links to related models |
|
| 719 | + if ($model->has_primary_key_field()) { |
|
| 720 | + foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
| 721 | + $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
| 722 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
| 723 | + array( |
|
| 724 | + 'href' => $this->get_versioned_link_to( |
|
| 725 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 726 | + . '/' |
|
| 727 | + . $entity_array[$model->primary_key_name()] |
|
| 728 | + . '/' |
|
| 729 | + . $related_model_part |
|
| 730 | + ), |
|
| 731 | + 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
| 732 | + ), |
|
| 733 | + ); |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + return $links; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + |
|
| 741 | + /** |
|
| 742 | + * Adds the included models indicated in the request to the entity provided |
|
| 743 | + * |
|
| 744 | + * @param \EEM_Base $model |
|
| 745 | + * @param \WP_REST_Request $rest_request |
|
| 746 | + * @param array $entity_array |
|
| 747 | + * @param array $db_row |
|
| 748 | + * @return array the modified entity |
|
| 749 | + */ |
|
| 750 | + protected function _include_requested_models( |
|
| 751 | + \EEM_Base $model, |
|
| 752 | + \WP_REST_Request $rest_request, |
|
| 753 | + $entity_array, |
|
| 754 | + $db_row = array() |
|
| 755 | + ) { |
|
| 756 | + //if $db_row not included, hope the entity array has what we need |
|
| 757 | + if (! $db_row) { |
|
| 758 | + $db_row = $entity_array; |
|
| 759 | + } |
|
| 760 | + $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
| 761 | + $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
| 762 | + //if they passed in * or didn't specify any includes, return everything |
|
| 763 | + if (! in_array('*', $includes_for_this_model) |
|
| 764 | + && ! empty($includes_for_this_model) |
|
| 765 | + ) { |
|
| 766 | + if ($model->has_primary_key_field()) { |
|
| 767 | + //always include the primary key. ya just gotta know that at least |
|
| 768 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
| 769 | + } |
|
| 770 | + if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
| 771 | + $includes_for_this_model[] = '_calculated_fields'; |
|
| 772 | + } |
|
| 773 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
| 774 | + } |
|
| 775 | + $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
| 776 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
| 777 | + $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
| 778 | + $rest_request->get_param('include'), |
|
| 779 | + $relation_name |
|
| 780 | + ); |
|
| 781 | + $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
| 782 | + $rest_request->get_param('calculate'), |
|
| 783 | + $relation_name |
|
| 784 | + ); |
|
| 785 | + //did they specify they wanted to include a related model, or |
|
| 786 | + //specific fields from a related model? |
|
| 787 | + //or did they specify to calculate a field from a related model? |
|
| 788 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
| 789 | + //if so, we should include at least some part of the related model |
|
| 790 | + $pretend_related_request = new \WP_REST_Request(); |
|
| 791 | + $pretend_related_request->set_query_params( |
|
| 792 | + array( |
|
| 793 | + 'caps' => $rest_request->get_param('caps'), |
|
| 794 | + 'include' => $related_fields_to_include, |
|
| 795 | + 'calculate' => $related_fields_to_calculate, |
|
| 796 | + ) |
|
| 797 | + ); |
|
| 798 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
| 799 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
| 800 | + $model->get_index_primary_key_string( |
|
| 801 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
| 802 | + ) |
|
| 803 | + ); |
|
| 804 | + $related_results = $this->_get_entities_from_relation( |
|
| 805 | + $primary_model_query_params, |
|
| 806 | + $relation_obj, |
|
| 807 | + $pretend_related_request |
|
| 808 | + ); |
|
| 809 | + $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
| 810 | + instanceof |
|
| 811 | + \WP_Error |
|
| 812 | + ? null |
|
| 813 | + : $related_results; |
|
| 814 | + } |
|
| 815 | + } |
|
| 816 | + return $entity_array; |
|
| 817 | + } |
|
| 818 | + |
|
| 819 | + |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * Returns a new array with all the names of models removed. Eg |
|
| 823 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
| 824 | + * |
|
| 825 | + * @param array $arr |
|
| 826 | + * @return array |
|
| 827 | + */ |
|
| 828 | + private function _remove_model_names_from_array($arr) |
|
| 829 | + { |
|
| 830 | + return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + |
|
| 834 | + |
|
| 835 | + /** |
|
| 836 | + * Gets the calculated fields for the response |
|
| 837 | + * |
|
| 838 | + * @param \EEM_Base $model |
|
| 839 | + * @param array $wpdb_row |
|
| 840 | + * @param \WP_REST_Request $rest_request |
|
| 841 | + * @return \stdClass the _calculations item in the entity |
|
| 842 | + */ |
|
| 843 | + protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
| 844 | + { |
|
| 845 | + $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
| 846 | + $rest_request->get_param('calculate'), |
|
| 847 | + '' |
|
| 848 | + ); |
|
| 849 | + //note: setting calculate=* doesn't do anything |
|
| 850 | + $calculated_fields_to_return = new \stdClass(); |
|
| 851 | + foreach ($calculated_fields as $field_to_calculate) { |
|
| 852 | + try { |
|
| 853 | + $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
| 854 | + null, |
|
| 855 | + $this->_fields_calculator->retrieve_calculated_field_value( |
|
| 856 | + $model, |
|
| 857 | + $field_to_calculate, |
|
| 858 | + $wpdb_row, |
|
| 859 | + $rest_request, |
|
| 860 | + $this |
|
| 861 | + ), |
|
| 862 | + $this->get_model_version_info()->requested_version() |
|
| 863 | + ); |
|
| 864 | + } catch (Rest_Exception $e) { |
|
| 865 | + //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
| 866 | + $this->_set_response_header( |
|
| 867 | + 'Notices-Field-Calculation-Errors[' |
|
| 868 | + . $e->get_string_code() |
|
| 869 | + . '][' |
|
| 870 | + . $model->get_this_model_name() |
|
| 871 | + . '][' |
|
| 872 | + . $field_to_calculate |
|
| 873 | + . ']', |
|
| 874 | + $e->getMessage(), |
|
| 875 | + true |
|
| 876 | + ); |
|
| 877 | + } |
|
| 878 | + } |
|
| 879 | + return $calculated_fields_to_return; |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + |
|
| 883 | + |
|
| 884 | + /** |
|
| 885 | + * Gets the full URL to the resource, taking the requested version into account |
|
| 886 | + * |
|
| 887 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
| 888 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
| 889 | + */ |
|
| 890 | + public function get_versioned_link_to($link_part_after_version_and_slash) |
|
| 891 | + { |
|
| 892 | + return rest_url( |
|
| 893 | + \EED_Core_Rest_Api::ee_api_namespace |
|
| 894 | + . $this->get_model_version_info()->requested_version() |
|
| 895 | + . '/' |
|
| 896 | + . $link_part_after_version_and_slash |
|
| 897 | + ); |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + |
|
| 901 | + |
|
| 902 | + /** |
|
| 903 | + * Gets the correct lowercase name for the relation in the API according |
|
| 904 | + * to the relation's type |
|
| 905 | + * |
|
| 906 | + * @param string $relation_name |
|
| 907 | + * @param \EE_Model_Relation_Base $relation_obj |
|
| 908 | + * @return string |
|
| 909 | + */ |
|
| 910 | + public static function get_related_entity_name($relation_name, $relation_obj) |
|
| 911 | + { |
|
| 912 | + if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
| 913 | + return strtolower($relation_name); |
|
| 914 | + } else { |
|
| 915 | + return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
| 916 | + } |
|
| 917 | + } |
|
| 918 | + |
|
| 919 | + |
|
| 920 | + |
|
| 921 | + /** |
|
| 922 | + * Gets the one model object with the specified id for the specified model |
|
| 923 | + * |
|
| 924 | + * @param \EEM_Base $model |
|
| 925 | + * @param \WP_REST_Request $request |
|
| 926 | + * @return array|\WP_Error |
|
| 927 | + */ |
|
| 928 | + public function get_entity_from_model($model, $request) |
|
| 929 | + { |
|
| 930 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
| 931 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
| 932 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
| 933 | + } |
|
| 934 | + $restricted_query_params = $query_params; |
|
| 935 | + $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
| 936 | + $this->_set_debug_info('model query params', $restricted_query_params); |
|
| 937 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
| 938 | + if (! empty ($model_rows)) { |
|
| 939 | + return $this->create_entity_from_wpdb_result( |
|
| 940 | + $model, |
|
| 941 | + array_shift($model_rows), |
|
| 942 | + $request); |
|
| 943 | + } else { |
|
| 944 | + //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
| 945 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 946 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
| 947 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
| 948 | + //you got shafted- it existed but we didn't want to tell you! |
|
| 949 | + return new \WP_Error( |
|
| 950 | + 'rest_user_cannot_read', |
|
| 951 | + sprintf( |
|
| 952 | + __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
| 953 | + strtolower($model->get_this_model_name()), |
|
| 954 | + Capabilities::get_missing_permissions_string( |
|
| 955 | + $model, |
|
| 956 | + $this->validate_context($request->get_param('caps'))) |
|
| 957 | + ), |
|
| 958 | + array('status' => 403) |
|
| 959 | + ); |
|
| 960 | + } else { |
|
| 961 | + //it's not you. It just doesn't exist |
|
| 962 | + return new \WP_Error( |
|
| 963 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 964 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 965 | + array('status' => 404) |
|
| 966 | + ); |
|
| 967 | + } |
|
| 968 | + } |
|
| 969 | + } |
|
| 970 | + |
|
| 971 | + |
|
| 972 | + |
|
| 973 | + /** |
|
| 974 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
| 975 | + * version so just treat it as a default read |
|
| 976 | + * |
|
| 977 | + * @param string $context |
|
| 978 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
| 979 | + */ |
|
| 980 | + public function validate_context($context) |
|
| 981 | + { |
|
| 982 | + if (! $context) { |
|
| 983 | + $context = \EEM_Base::caps_read; |
|
| 984 | + } |
|
| 985 | + $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
| 986 | + if (in_array($context, $valid_contexts)) { |
|
| 987 | + return $context; |
|
| 988 | + } else { |
|
| 989 | + return \EEM_Base::caps_read; |
|
| 990 | + } |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + |
|
| 994 | + |
|
| 995 | + /** |
|
| 996 | + * Verifies the passed in value is an allowable default where conditions value. |
|
| 997 | + * |
|
| 998 | + * @param $default_query_params |
|
| 999 | + * @return string |
|
| 1000 | + */ |
|
| 1001 | + public function validate_default_query_params($default_query_params) |
|
| 1002 | + { |
|
| 1003 | + $valid_default_where_conditions_for_api_calls = array( |
|
| 1004 | + \EEM_Base::default_where_conditions_all, |
|
| 1005 | + \EEM_Base::default_where_conditions_minimum_all, |
|
| 1006 | + \EEM_Base::default_where_conditions_minimum_others, |
|
| 1007 | + ); |
|
| 1008 | + if (! $default_query_params) { |
|
| 1009 | + $default_query_params = \EEM_Base::default_where_conditions_all; |
|
| 1010 | + } |
|
| 1011 | + if ( |
|
| 1012 | + in_array( |
|
| 1013 | + $default_query_params, |
|
| 1014 | + $valid_default_where_conditions_for_api_calls, |
|
| 1015 | + true |
|
| 1016 | + ) |
|
| 1017 | + ) { |
|
| 1018 | + return $default_query_params; |
|
| 1019 | + } else { |
|
| 1020 | + return \EEM_Base::default_where_conditions_all; |
|
| 1021 | + } |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + |
|
| 1025 | + |
|
| 1026 | + /** |
|
| 1027 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
| 1028 | + * Note: right now the query parameter keys for fields (and related fields) |
|
| 1029 | + * can be left as-is, but it's quite possible this will change someday. |
|
| 1030 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
| 1031 | + * |
|
| 1032 | + * @param \EEM_Base $model |
|
| 1033 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
| 1034 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
| 1035 | + * that absolutely no results should be returned |
|
| 1036 | + * @throws \EE_Error |
|
| 1037 | + */ |
|
| 1038 | + public function create_model_query_params($model, $query_parameters) |
|
| 1039 | + { |
|
| 1040 | + $model_query_params = array(); |
|
| 1041 | + if (isset($query_parameters['where'])) { |
|
| 1042 | + $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
| 1043 | + $query_parameters['where'], |
|
| 1044 | + $model, |
|
| 1045 | + $this->get_model_version_info()->requested_version() |
|
| 1046 | + ); |
|
| 1047 | + } |
|
| 1048 | + if (isset($query_parameters['order_by'])) { |
|
| 1049 | + $order_by = $query_parameters['order_by']; |
|
| 1050 | + } elseif (isset($query_parameters['orderby'])) { |
|
| 1051 | + $order_by = $query_parameters['orderby']; |
|
| 1052 | + } else { |
|
| 1053 | + $order_by = null; |
|
| 1054 | + } |
|
| 1055 | + if ($order_by !== null) { |
|
| 1056 | + if (is_array($order_by)) { |
|
| 1057 | + $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
| 1058 | + } else { |
|
| 1059 | + //it's a single item |
|
| 1060 | + $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
| 1061 | + } |
|
| 1062 | + $model_query_params['order_by'] = $order_by; |
|
| 1063 | + } |
|
| 1064 | + if (isset($query_parameters['group_by'])) { |
|
| 1065 | + $group_by = $query_parameters['group_by']; |
|
| 1066 | + } elseif (isset($query_parameters['groupby'])) { |
|
| 1067 | + $group_by = $query_parameters['groupby']; |
|
| 1068 | + } else { |
|
| 1069 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
| 1070 | + } |
|
| 1071 | + //make sure they're all real names |
|
| 1072 | + if (is_array($group_by)) { |
|
| 1073 | + $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
| 1074 | + } |
|
| 1075 | + if ($group_by !== null) { |
|
| 1076 | + $model_query_params['group_by'] = $group_by; |
|
| 1077 | + } |
|
| 1078 | + if (isset($query_parameters['having'])) { |
|
| 1079 | + $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
| 1080 | + $query_parameters['having'], |
|
| 1081 | + $model, |
|
| 1082 | + $this->get_model_version_info()->requested_version() |
|
| 1083 | + ); |
|
| 1084 | + } |
|
| 1085 | + if (isset($query_parameters['order'])) { |
|
| 1086 | + $model_query_params['order'] = $query_parameters['order']; |
|
| 1087 | + } |
|
| 1088 | + if (isset($query_parameters['mine'])) { |
|
| 1089 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
| 1090 | + } |
|
| 1091 | + if (isset($query_parameters['limit'])) { |
|
| 1092 | + //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
| 1093 | + if (! is_array($query_parameters['limit'])) { |
|
| 1094 | + $limit_array = explode(',', (string)$query_parameters['limit']); |
|
| 1095 | + } else { |
|
| 1096 | + $limit_array = $query_parameters['limit']; |
|
| 1097 | + } |
|
| 1098 | + $sanitized_limit = array(); |
|
| 1099 | + foreach ($limit_array as $key => $limit_part) { |
|
| 1100 | + if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
| 1101 | + throw new \EE_Error( |
|
| 1102 | + sprintf( |
|
| 1103 | + __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
| 1104 | + 'event_espresso'), |
|
| 1105 | + wp_json_encode($query_parameters['limit']) |
|
| 1106 | + ) |
|
| 1107 | + ); |
|
| 1108 | + } |
|
| 1109 | + $sanitized_limit[] = (int)$limit_part; |
|
| 1110 | + } |
|
| 1111 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
| 1112 | + } else { |
|
| 1113 | + $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
| 1114 | + } |
|
| 1115 | + if (isset($query_parameters['caps'])) { |
|
| 1116 | + $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
| 1117 | + } else { |
|
| 1118 | + $model_query_params['caps'] = \EEM_Base::caps_read; |
|
| 1119 | + } |
|
| 1120 | + if (isset($query_parameters['default_where_conditions'])) { |
|
| 1121 | + $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
| 1122 | + } |
|
| 1123 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + |
|
| 1127 | + |
|
| 1128 | + /** |
|
| 1129 | + * Changes the REST-style query params for use in the models |
|
| 1130 | + * |
|
| 1131 | + * @deprecated |
|
| 1132 | + * @param \EEM_Base $model |
|
| 1133 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
| 1134 | + * @return array |
|
| 1135 | + */ |
|
| 1136 | + public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
| 1137 | + { |
|
| 1138 | + $model_ready_query_params = array(); |
|
| 1139 | + foreach ($query_params as $key => $value) { |
|
| 1140 | + if (is_array($value)) { |
|
| 1141 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
| 1142 | + } else { |
|
| 1143 | + $model_ready_query_params[$key] = $value; |
|
| 1144 | + } |
|
| 1145 | + } |
|
| 1146 | + return $model_ready_query_params; |
|
| 1147 | + } |
|
| 1148 | + |
|
| 1149 | + |
|
| 1150 | + |
|
| 1151 | + /** |
|
| 1152 | + * @deprecated |
|
| 1153 | + * @param $model |
|
| 1154 | + * @param $query_params |
|
| 1155 | + * @return array |
|
| 1156 | + */ |
|
| 1157 | + public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
| 1158 | + { |
|
| 1159 | + $model_ready_query_params = array(); |
|
| 1160 | + foreach ($query_params as $key => $value) { |
|
| 1161 | + if (is_array($value)) { |
|
| 1162 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
| 1163 | + } else { |
|
| 1164 | + $model_ready_query_params[$key] = $value; |
|
| 1165 | + } |
|
| 1166 | + } |
|
| 1167 | + return $model_ready_query_params; |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + |
|
| 1171 | + |
|
| 1172 | + /** |
|
| 1173 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
| 1174 | + * If no prefix is specified, returns items with no period. |
|
| 1175 | + * |
|
| 1176 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
| 1177 | + * @param string $prefix "Event" or "foobar" |
|
| 1178 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
| 1179 | + * we only return strings starting with that and a period; if no prefix was |
|
| 1180 | + * specified we return all items containing NO periods |
|
| 1181 | + */ |
|
| 1182 | + public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
| 1183 | + { |
|
| 1184 | + if (is_string($string_to_explode)) { |
|
| 1185 | + $exploded_contents = explode(',', $string_to_explode); |
|
| 1186 | + } else if (is_array($string_to_explode)) { |
|
| 1187 | + $exploded_contents = $string_to_explode; |
|
| 1188 | + } else { |
|
| 1189 | + $exploded_contents = array(); |
|
| 1190 | + } |
|
| 1191 | + //if the string was empty, we want an empty array |
|
| 1192 | + $exploded_contents = array_filter($exploded_contents); |
|
| 1193 | + $contents_with_prefix = array(); |
|
| 1194 | + foreach ($exploded_contents as $item) { |
|
| 1195 | + $item = trim($item); |
|
| 1196 | + //if no prefix was provided, so we look for items with no "." in them |
|
| 1197 | + if (! $prefix) { |
|
| 1198 | + //does this item have a period? |
|
| 1199 | + if (strpos($item, '.') === false) { |
|
| 1200 | + //if not, then its what we're looking for |
|
| 1201 | + $contents_with_prefix[] = $item; |
|
| 1202 | + } |
|
| 1203 | + } else if (strpos($item, $prefix . '.') === 0) { |
|
| 1204 | + //this item has the prefix and a period, grab it |
|
| 1205 | + $contents_with_prefix[] = substr( |
|
| 1206 | + $item, |
|
| 1207 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
| 1208 | + ); |
|
| 1209 | + } else if ($item === $prefix) { |
|
| 1210 | + //this item is JUST the prefix |
|
| 1211 | + //so let's grab everything after, which is a blank string |
|
| 1212 | + $contents_with_prefix[] = ''; |
|
| 1213 | + } |
|
| 1214 | + } |
|
| 1215 | + return $contents_with_prefix; |
|
| 1216 | + } |
|
| 1217 | + |
|
| 1218 | + |
|
| 1219 | + |
|
| 1220 | + /** |
|
| 1221 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
| 1222 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
| 1223 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
| 1224 | + * array('*') (when you provided a model and a model of that kind was found). |
|
| 1225 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
| 1226 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
| 1227 | + * name and then a period). |
|
| 1228 | + * @param string $include_string @see Read:handle_request_get_all |
|
| 1229 | + * @param string $model_name |
|
| 1230 | + * @return array of fields for this model. If $model_name is provided, then |
|
| 1231 | + * the fields for that model, with the model's name removed from each. |
|
| 1232 | + * If $include_string was blank or '*' returns an empty array |
|
| 1233 | + */ |
|
| 1234 | + public function extract_includes_for_this_model($include_string, $model_name = null) |
|
| 1235 | + { |
|
| 1236 | + if (is_array($include_string)) { |
|
| 1237 | + $include_string = implode(',', $include_string); |
|
| 1238 | + } |
|
| 1239 | + if ($include_string === '*' || $include_string === '') { |
|
| 1240 | + return array(); |
|
| 1241 | + } |
|
| 1242 | + $includes = explode(',', $include_string); |
|
| 1243 | + $extracted_fields_to_include = array(); |
|
| 1244 | + if ($model_name) { |
|
| 1245 | + foreach ($includes as $field_to_include) { |
|
| 1246 | + $field_to_include = trim($field_to_include); |
|
| 1247 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
| 1248 | + //found the model name at the exact start |
|
| 1249 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
| 1250 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
| 1251 | + } elseif ($field_to_include == $model_name) { |
|
| 1252 | + $extracted_fields_to_include[] = '*'; |
|
| 1253 | + } |
|
| 1254 | + } |
|
| 1255 | + } else { |
|
| 1256 | + //look for ones with no period |
|
| 1257 | + foreach ($includes as $field_to_include) { |
|
| 1258 | + $field_to_include = trim($field_to_include); |
|
| 1259 | + if ( |
|
| 1260 | + strpos($field_to_include, '.') === false |
|
| 1261 | + && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
| 1262 | + ) { |
|
| 1263 | + $extracted_fields_to_include[] = $field_to_include; |
|
| 1264 | + } |
|
| 1265 | + } |
|
| 1266 | + } |
|
| 1267 | + return $extracted_fields_to_include; |
|
| 1268 | + } |
|
| 1269 | 1269 | } |
| 1270 | 1270 | |
| 1271 | 1271 | |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | use EventEspresso\core\entities\models\JsonModelSchema; |
| 9 | 9 | use EE_Datetime_Field; |
| 10 | 10 | |
| 11 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 11 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 12 | 12 | exit('No direct script access allowed'); |
| 13 | 13 | } |
| 14 | 14 | |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | try { |
| 58 | 58 | $matches = $controller->parse_route( |
| 59 | 59 | $request->get_route(), |
| 60 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
| 60 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)~', |
|
| 61 | 61 | array('version', 'model') |
| 62 | 62 | ); |
| 63 | 63 | $controller->set_requested_version($matches['version']); |
| 64 | 64 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
| 65 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 65 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 66 | 66 | return $controller->send_response( |
| 67 | 67 | new \WP_Error( |
| 68 | 68 | 'endpoint_parsing_error', |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | $controller = new Read(); |
| 100 | 100 | try { |
| 101 | 101 | $controller->set_requested_version($version); |
| 102 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
| 102 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
| 103 | 103 | return array(); |
| 104 | 104 | } |
| 105 | 105 | //get the model for this version |
@@ -193,9 +193,9 @@ discard block |
||
| 193 | 193 | protected function _maybe_add_extra_fields_to_schema($field_name, \EE_Model_Field_Base $field, array $schema) |
| 194 | 194 | { |
| 195 | 195 | if ($field instanceof EE_Datetime_Field) { |
| 196 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
| 196 | + $schema['properties'][$field_name.'_gmt'] = $field->getSchema(); |
|
| 197 | 197 | //modify the description |
| 198 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
| 198 | + $schema['properties'][$field_name.'_gmt']['description'] = sprintf( |
|
| 199 | 199 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
| 200 | 200 | $field->get_nicename() |
| 201 | 201 | ); |
@@ -236,11 +236,11 @@ discard block |
||
| 236 | 236 | try { |
| 237 | 237 | $matches = $controller->parse_route( |
| 238 | 238 | $request->get_route(), |
| 239 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
| 239 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)~', |
|
| 240 | 240 | array('version', 'model', 'id')); |
| 241 | 241 | $controller->set_requested_version($matches['version']); |
| 242 | 242 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
| 243 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 243 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
| 244 | 244 | return $controller->send_response( |
| 245 | 245 | new \WP_Error( |
| 246 | 246 | 'endpoint_parsing_error', |
@@ -278,12 +278,12 @@ discard block |
||
| 278 | 278 | try { |
| 279 | 279 | $matches = $controller->parse_route( |
| 280 | 280 | $request->get_route(), |
| 281 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
| 281 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)/(.*)~', |
|
| 282 | 282 | array('version', 'model', 'id', 'related_model') |
| 283 | 283 | ); |
| 284 | 284 | $controller->set_requested_version($matches['version']); |
| 285 | 285 | $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
| 286 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
| 286 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
| 287 | 287 | return $controller->send_response( |
| 288 | 288 | new \WP_Error( |
| 289 | 289 | 'endpoint_parsing_error', |
@@ -298,11 +298,11 @@ discard block |
||
| 298 | 298 | $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
| 299 | 299 | //assume the related model name is plural and try to find the model's name |
| 300 | 300 | $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
| 301 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 301 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 302 | 302 | //so the word didn't singularize well. Maybe that's just because it's a singular word? |
| 303 | 303 | $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
| 304 | 304 | } |
| 305 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 305 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
| 306 | 306 | return $controller->send_response( |
| 307 | 307 | new \WP_Error( |
| 308 | 308 | 'endpoint_parsing_error', |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | public function get_entities_from_model($model, $request) |
| 339 | 339 | { |
| 340 | 340 | $query_params = $this->create_model_query_params($model, $request->get_params()); |
| 341 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
| 341 | + if ( ! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
| 342 | 342 | $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
| 343 | 343 | return new \WP_Error( |
| 344 | 344 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | array('status' => 403) |
| 351 | 351 | ); |
| 352 | 352 | } |
| 353 | - if (! $request->get_header('no_rest_headers')) { |
|
| 353 | + if ( ! $request->get_header('no_rest_headers')) { |
|
| 354 | 354 | $this->_set_headers_from_query_params($model, $query_params); |
| 355 | 355 | } |
| 356 | 356 | /** @type array $results */ |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | $context = $this->validate_context($request->get_param('caps')); |
| 381 | 381 | $model = $relation->get_this_model(); |
| 382 | 382 | $related_model = $relation->get_other_model(); |
| 383 | - if (! isset($primary_model_query_params[0])) { |
|
| 383 | + if ( ! isset($primary_model_query_params[0])) { |
|
| 384 | 384 | $primary_model_query_params[0] = array(); |
| 385 | 385 | } |
| 386 | 386 | //check if they can access the 1st model object |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | } |
| 432 | 432 | $query_params['default_where_conditions'] = 'none'; |
| 433 | 433 | $query_params['caps'] = $context; |
| 434 | - if (! $request->get_header('no_rest_headers')) { |
|
| 434 | + if ( ! $request->get_header('no_rest_headers')) { |
|
| 435 | 435 | $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
| 436 | 436 | } |
| 437 | 437 | /** @type array $results */ |
@@ -483,7 +483,7 @@ discard block |
||
| 483 | 483 | */ |
| 484 | 484 | public function get_entities_from_relation($id, $relation, $request) |
| 485 | 485 | { |
| 486 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
| 486 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
| 487 | 487 | throw new \EE_Error( |
| 488 | 488 | sprintf( |
| 489 | 489 | __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
@@ -520,7 +520,7 @@ discard block |
||
| 520 | 520 | $this->_set_debug_info('missing caps', |
| 521 | 521 | Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
| 522 | 522 | //normally the limit to a 2-part array, where the 2nd item is the limit |
| 523 | - if (! isset($query_params['limit'])) { |
|
| 523 | + if ( ! isset($query_params['limit'])) { |
|
| 524 | 524 | $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
| 525 | 525 | } |
| 526 | 526 | if (is_array($query_params['limit'])) { |
@@ -554,7 +554,7 @@ discard block |
||
| 554 | 554 | */ |
| 555 | 555 | public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
| 556 | 556 | { |
| 557 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
| 557 | + if ( ! $rest_request instanceof \WP_REST_Request) { |
|
| 558 | 558 | //ok so this was called in the old style, where the 3rd arg was |
| 559 | 559 | //$include, and the 4th arg was $context |
| 560 | 560 | //now setup the request just to avoid fatal errors, although we won't be able |
@@ -641,7 +641,7 @@ discard block |
||
| 641 | 641 | if ($field_value instanceof \DateTime) { |
| 642 | 642 | $timezone = $field_value->getTimezone(); |
| 643 | 643 | $field_value->setTimezone(new \DateTimeZone('UTC')); |
| 644 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 644 | + $result[$field_name.'_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
| 645 | 645 | $field_obj, |
| 646 | 646 | $field_value, |
| 647 | 647 | $this->get_model_version_info()->requested_version() |
@@ -719,7 +719,7 @@ discard block |
||
| 719 | 719 | if ($model->has_primary_key_field()) { |
| 720 | 720 | foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
| 721 | 721 | $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
| 722 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
| 722 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
| 723 | 723 | array( |
| 724 | 724 | 'href' => $this->get_versioned_link_to( |
| 725 | 725 | \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
@@ -754,13 +754,13 @@ discard block |
||
| 754 | 754 | $db_row = array() |
| 755 | 755 | ) { |
| 756 | 756 | //if $db_row not included, hope the entity array has what we need |
| 757 | - if (! $db_row) { |
|
| 757 | + if ( ! $db_row) { |
|
| 758 | 758 | $db_row = $entity_array; |
| 759 | 759 | } |
| 760 | 760 | $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
| 761 | 761 | $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
| 762 | 762 | //if they passed in * or didn't specify any includes, return everything |
| 763 | - if (! in_array('*', $includes_for_this_model) |
|
| 763 | + if ( ! in_array('*', $includes_for_this_model) |
|
| 764 | 764 | && ! empty($includes_for_this_model) |
| 765 | 765 | ) { |
| 766 | 766 | if ($model->has_primary_key_field()) { |
@@ -935,7 +935,7 @@ discard block |
||
| 935 | 935 | $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
| 936 | 936 | $this->_set_debug_info('model query params', $restricted_query_params); |
| 937 | 937 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
| 938 | - if (! empty ($model_rows)) { |
|
| 938 | + if ( ! empty ($model_rows)) { |
|
| 939 | 939 | return $this->create_entity_from_wpdb_result( |
| 940 | 940 | $model, |
| 941 | 941 | array_shift($model_rows), |
@@ -944,7 +944,7 @@ discard block |
||
| 944 | 944 | //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
| 945 | 945 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
| 946 | 946 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
| 947 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
| 947 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
| 948 | 948 | //you got shafted- it existed but we didn't want to tell you! |
| 949 | 949 | return new \WP_Error( |
| 950 | 950 | 'rest_user_cannot_read', |
@@ -979,7 +979,7 @@ discard block |
||
| 979 | 979 | */ |
| 980 | 980 | public function validate_context($context) |
| 981 | 981 | { |
| 982 | - if (! $context) { |
|
| 982 | + if ( ! $context) { |
|
| 983 | 983 | $context = \EEM_Base::caps_read; |
| 984 | 984 | } |
| 985 | 985 | $valid_contexts = \EEM_Base::valid_cap_contexts(); |
@@ -1005,7 +1005,7 @@ discard block |
||
| 1005 | 1005 | \EEM_Base::default_where_conditions_minimum_all, |
| 1006 | 1006 | \EEM_Base::default_where_conditions_minimum_others, |
| 1007 | 1007 | ); |
| 1008 | - if (! $default_query_params) { |
|
| 1008 | + if ( ! $default_query_params) { |
|
| 1009 | 1009 | $default_query_params = \EEM_Base::default_where_conditions_all; |
| 1010 | 1010 | } |
| 1011 | 1011 | if ( |
@@ -1090,14 +1090,14 @@ discard block |
||
| 1090 | 1090 | } |
| 1091 | 1091 | if (isset($query_parameters['limit'])) { |
| 1092 | 1092 | //limit should be either a string like '23' or '23,43', or an array with two items in it |
| 1093 | - if (! is_array($query_parameters['limit'])) { |
|
| 1094 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
| 1093 | + if ( ! is_array($query_parameters['limit'])) { |
|
| 1094 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
| 1095 | 1095 | } else { |
| 1096 | 1096 | $limit_array = $query_parameters['limit']; |
| 1097 | 1097 | } |
| 1098 | 1098 | $sanitized_limit = array(); |
| 1099 | 1099 | foreach ($limit_array as $key => $limit_part) { |
| 1100 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
| 1100 | + if ($this->_debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
| 1101 | 1101 | throw new \EE_Error( |
| 1102 | 1102 | sprintf( |
| 1103 | 1103 | __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
@@ -1106,7 +1106,7 @@ discard block |
||
| 1106 | 1106 | ) |
| 1107 | 1107 | ); |
| 1108 | 1108 | } |
| 1109 | - $sanitized_limit[] = (int)$limit_part; |
|
| 1109 | + $sanitized_limit[] = (int) $limit_part; |
|
| 1110 | 1110 | } |
| 1111 | 1111 | $model_query_params['limit'] = implode(',', $sanitized_limit); |
| 1112 | 1112 | } else { |
@@ -1194,17 +1194,17 @@ discard block |
||
| 1194 | 1194 | foreach ($exploded_contents as $item) { |
| 1195 | 1195 | $item = trim($item); |
| 1196 | 1196 | //if no prefix was provided, so we look for items with no "." in them |
| 1197 | - if (! $prefix) { |
|
| 1197 | + if ( ! $prefix) { |
|
| 1198 | 1198 | //does this item have a period? |
| 1199 | 1199 | if (strpos($item, '.') === false) { |
| 1200 | 1200 | //if not, then its what we're looking for |
| 1201 | 1201 | $contents_with_prefix[] = $item; |
| 1202 | 1202 | } |
| 1203 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
| 1203 | + } else if (strpos($item, $prefix.'.') === 0) { |
|
| 1204 | 1204 | //this item has the prefix and a period, grab it |
| 1205 | 1205 | $contents_with_prefix[] = substr( |
| 1206 | 1206 | $item, |
| 1207 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
| 1207 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
| 1208 | 1208 | ); |
| 1209 | 1209 | } else if ($item === $prefix) { |
| 1210 | 1210 | //this item is JUST the prefix |
@@ -1244,9 +1244,9 @@ discard block |
||
| 1244 | 1244 | if ($model_name) { |
| 1245 | 1245 | foreach ($includes as $field_to_include) { |
| 1246 | 1246 | $field_to_include = trim($field_to_include); |
| 1247 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
| 1247 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
| 1248 | 1248 | //found the model name at the exact start |
| 1249 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
| 1249 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
| 1250 | 1250 | $extracted_fields_to_include[] = $field_sans_model_name; |
| 1251 | 1251 | } elseif ($field_to_include == $model_name) { |
| 1252 | 1252 | $extracted_fields_to_include[] = '*'; |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace EventEspresso\core\libraries\rest_api\controllers\config; |
| 3 | 3 | |
| 4 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 5 | 5 | exit('No direct script access allowed'); |
| 6 | 6 | } |
| 7 | 7 | |
@@ -80,8 +80,8 @@ discard block |
||
| 80 | 80 | ), |
| 81 | 81 | 'default_currency' => \EE_Config::instance()->currency, |
| 82 | 82 | 'authentication' => array( |
| 83 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
| 84 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
| 83 | + 'received_basic_auth_data' => (bool) $wp_json_basic_auth_received_data, |
|
| 84 | + 'insecure_usage_of_basic_auth' => (bool) $insecure_usage_of_basic_auth, |
|
| 85 | 85 | 'warning' => $warning |
| 86 | 86 | ) |
| 87 | 87 | ); |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | namespace EventEspresso\core\libraries\rest_api\controllers\config; |
| 3 | 3 | |
| 4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 5 | - exit('No direct script access allowed'); |
|
| 5 | + exit('No direct script access allowed'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
@@ -18,74 +18,74 @@ discard block |
||
| 18 | 18 | class Read |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @param \WP_REST_Request $request |
|
| 23 | - * @return \EE_Config|\WP_Error |
|
| 24 | - */ |
|
| 25 | - public static function handle_request(\WP_REST_Request $request) |
|
| 26 | - { |
|
| 27 | - $cap = \EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 28 | - if (\EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
| 29 | - return \EE_Config::instance(); |
|
| 30 | - } else { |
|
| 31 | - return new \WP_Error( |
|
| 32 | - 'cannot_read_config', |
|
| 33 | - sprintf( |
|
| 34 | - __( |
|
| 35 | - 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
| 36 | - 'event_espresso' |
|
| 37 | - ), |
|
| 38 | - $cap |
|
| 39 | - ), |
|
| 40 | - array('status' => 403) |
|
| 41 | - ); |
|
| 42 | - } |
|
| 43 | - } |
|
| 21 | + /** |
|
| 22 | + * @param \WP_REST_Request $request |
|
| 23 | + * @return \EE_Config|\WP_Error |
|
| 24 | + */ |
|
| 25 | + public static function handle_request(\WP_REST_Request $request) |
|
| 26 | + { |
|
| 27 | + $cap = \EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 28 | + if (\EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
| 29 | + return \EE_Config::instance(); |
|
| 30 | + } else { |
|
| 31 | + return new \WP_Error( |
|
| 32 | + 'cannot_read_config', |
|
| 33 | + sprintf( |
|
| 34 | + __( |
|
| 35 | + 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
| 36 | + 'event_espresso' |
|
| 37 | + ), |
|
| 38 | + $cap |
|
| 39 | + ), |
|
| 40 | + array('status' => 403) |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Handles the request for public site info |
|
| 49 | - * |
|
| 50 | - * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
| 51 | - * current user could be authenticated using basic auth data |
|
| 52 | - * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
| 53 | - * basic auth data was somehow received |
|
| 54 | - * @param \WP_REST_Request $request |
|
| 55 | - * @return \EE_Config|\WP_Error |
|
| 56 | - */ |
|
| 57 | - public static function handle_request_site_info(\WP_REST_Request $request) |
|
| 58 | - { |
|
| 59 | - global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
| 60 | - $insecure_usage_of_basic_auth = apply_filters( |
|
| 61 | - 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
| 62 | - $wp_json_basic_auth_success && ! is_ssl(), |
|
| 63 | - $request |
|
| 64 | - ); |
|
| 65 | - if ($insecure_usage_of_basic_auth) { |
|
| 66 | - $warning = sprintf( |
|
| 67 | - esc_html__('Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
| 68 | - 'event_espresso'), |
|
| 69 | - '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
| 70 | - '</a>' |
|
| 71 | - ); |
|
| 72 | - } else { |
|
| 73 | - $warning = ''; |
|
| 74 | - } |
|
| 75 | - return array( |
|
| 76 | - 'default_timezone' => array( |
|
| 77 | - 'pretty' => \EEH_DTT_Helper::get_timezone_string_for_display(), |
|
| 78 | - 'string' => get_option('timezone_string'), |
|
| 79 | - 'offset' => \EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
| 80 | - ), |
|
| 81 | - 'default_currency' => \EE_Config::instance()->currency, |
|
| 82 | - 'authentication' => array( |
|
| 83 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
| 84 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
| 85 | - 'warning' => $warning |
|
| 86 | - ) |
|
| 87 | - ); |
|
| 88 | - } |
|
| 47 | + /** |
|
| 48 | + * Handles the request for public site info |
|
| 49 | + * |
|
| 50 | + * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
| 51 | + * current user could be authenticated using basic auth data |
|
| 52 | + * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
| 53 | + * basic auth data was somehow received |
|
| 54 | + * @param \WP_REST_Request $request |
|
| 55 | + * @return \EE_Config|\WP_Error |
|
| 56 | + */ |
|
| 57 | + public static function handle_request_site_info(\WP_REST_Request $request) |
|
| 58 | + { |
|
| 59 | + global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
| 60 | + $insecure_usage_of_basic_auth = apply_filters( |
|
| 61 | + 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
| 62 | + $wp_json_basic_auth_success && ! is_ssl(), |
|
| 63 | + $request |
|
| 64 | + ); |
|
| 65 | + if ($insecure_usage_of_basic_auth) { |
|
| 66 | + $warning = sprintf( |
|
| 67 | + esc_html__('Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
| 68 | + 'event_espresso'), |
|
| 69 | + '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
| 70 | + '</a>' |
|
| 71 | + ); |
|
| 72 | + } else { |
|
| 73 | + $warning = ''; |
|
| 74 | + } |
|
| 75 | + return array( |
|
| 76 | + 'default_timezone' => array( |
|
| 77 | + 'pretty' => \EEH_DTT_Helper::get_timezone_string_for_display(), |
|
| 78 | + 'string' => get_option('timezone_string'), |
|
| 79 | + 'offset' => \EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
| 80 | + ), |
|
| 81 | + 'default_currency' => \EE_Config::instance()->currency, |
|
| 82 | + 'authentication' => array( |
|
| 83 | + 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
| 84 | + 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
| 85 | + 'warning' => $warning |
|
| 86 | + ) |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | // End of file Read.php |
| 92 | 92 | \ No newline at end of file |