/wp-content/plugins/the-events-calendar/src/Tribe/Importer/File_Importer_Venues.php

https://github.com/livinglab/openlab · PHP · 251 lines · 123 code · 28 blank · 100 comment · 14 complexity · 83d1d9c090f35ce7a26f37286ecb406c MD5 · raw file

  1. <?php
  2. /**
  3. * Class Tribe__Events__Importer__File_Importer_Venues
  4. */
  5. class Tribe__Events__Importer__File_Importer_Venues extends Tribe__Events__Importer__File_Importer {
  6. protected $required_fields = [ 'venue_name' ];
  7. protected function match_existing_post( array $record ) {
  8. $name = $this->get_value_by_key( $record, 'venue_name' );
  9. $id = $this->find_matching_post_id( $name, Tribe__Events__Main::VENUE_POST_TYPE );
  10. return $id;
  11. }
  12. protected function update_post( $post_id, array $record ) {
  13. $venue = $this->build_venue_array( $post_id, $record );
  14. Tribe__Events__API::updateVenue( $post_id, $venue );
  15. if ( $this->is_aggregator && ! empty( $this->aggregator_record ) ) {
  16. $this->aggregator_record->meta['activity']->add( 'venue', 'updated', $post_id );
  17. }
  18. }
  19. protected function create_post( array $record ) {
  20. $post_status_setting = tribe( 'events-aggregator.settings' )->default_post_status( 'csv' );
  21. $venue = $this->build_venue_array( false, $record );
  22. $id = Tribe__Events__API::createVenue( $venue, $post_status_setting );
  23. if ( $this->is_aggregator && ! empty( $this->aggregator_record ) ) {
  24. $this->aggregator_record->meta['activity']->add( 'venue', 'created', $id );
  25. }
  26. return $id;
  27. }
  28. /**
  29. * Build a venue array for creation/update of the current imported venue.
  30. *
  31. * @since 3.2
  32. * @since 5.1.6 Adjust to prevent overwriting values that aren't mapped.
  33. *
  34. * @param int $venue_id The ID of the venue we're currently importing.
  35. * @param array <string,string> $record The event record from the import file. Only contains mapped values.
  36. * Useful if value and key above don't appear to match what's expected.
  37. * In the format [ mapped_key => value ].
  38. *
  39. * @return array $venue The array of venue data for creation/update.
  40. */
  41. private function build_venue_array( $venue_id, array $record ) {
  42. $venue = [];
  43. $columns = [
  44. 'Venue' => 'venue_name',
  45. 'Address' => 'venue_address',
  46. 'City' => 'venue_city',
  47. 'Country' => 'venue_country',
  48. 'Description' => 'venue_description',
  49. 'Phone' => 'venue_phone',
  50. 'Province' => 'venue_state',
  51. 'State' => 'venue_state',
  52. 'URL' => 'venue_url',
  53. 'Zip' => 'venue_zip',
  54. ];
  55. foreach ( $columns as $name => $key ) {
  56. // Don't set/overwrite unmapped columns.
  57. if ( ! $this->has_value_by_key( $record, $key ) ) {
  58. continue;
  59. }
  60. // Reset.
  61. $value = '';
  62. // Address is a concatenation of two possible values.
  63. if ( 'venue_address' === $key ) {
  64. $address_1 = trim( $this->get_value_by_key( $record, 'venue_address' ) );
  65. $address_2 = trim( $this->get_value_by_key( $record, 'venue_address2' ) );
  66. $value = ( empty( $address_2 ) ) ? $address_1 : $address_1 . ' ' . $address_2;
  67. } else {
  68. $value = $this->get_value_by_key( $record, $key );
  69. }
  70. /**
  71. * Allows filtering of individual main values before setting.
  72. * Return boolean false to prevent importing that value.
  73. * @since 5.1.6
  74. *
  75. * @param string $key The mapped key for the value we'll be importing.
  76. * From the $columns array above, this would be 'venue_name', for example.
  77. * @param string $value The mapped value we'll be importing.
  78. * @param array $venue The entire array of venue data we're modifying.
  79. * @param array <string,string> $record The event record from the import file. Only contains mapped values.
  80. * Useful if value and key above don't appear to match what's expected.
  81. * In the format [ mapped_key => value ].
  82. * @param object $this The current instance of Tribe__Events__Importer__File_Importer_Venues.
  83. */
  84. $value = apply_filters(
  85. "tribe_events_importer_venue_{$key}_value",
  86. $value,
  87. $key,
  88. $venue,
  89. $record,
  90. $this
  91. );
  92. if ( false === $value ) {
  93. continue;
  94. }
  95. $venue[ $name ] = $value;
  96. }
  97. // Handle the manual stuff.
  98. $venue['FeaturedImage'] = $this->get_featured_image( $venue_id, $record );
  99. $show_map_setting = tribe( 'events-aggregator.settings' )->default_map( 'csv' );
  100. $venue['ShowMap'] = $venue_id ? get_post_meta( $venue_id, '_VenueShowMap', true ) : $show_map_setting;
  101. $venue['ShowMapLink'] = $venue_id ? get_post_meta( $venue_id, '_VenueShowMapLink', true ) : $show_map_setting;
  102. /**
  103. * Allows triggering using the default values set in the admin for imported venues.
  104. *
  105. * @since 5.1.6
  106. * @param int $venue_id The ID of the venue we're currently importing.
  107. * @param array $venue The array of venue data we're modifying.
  108. * @param array <string,string> $record The event record from the import file. Only contains mapped values.
  109. * Useful if value and key above don't appear to match what's expected.
  110. * In the format [ mapped_key => value ].
  111. */
  112. $set_defaults = apply_filters(
  113. 'tribe_events_importer_set_default_venue_import_values',
  114. false,
  115. $venue_id,
  116. $venue,
  117. $record
  118. );
  119. if ( $set_defaults ) {
  120. $venue = $this->set_defaults( $venue, $record );
  121. }
  122. /**
  123. * Allows filtering of values before import.
  124. *
  125. * @since 4.2
  126. *
  127. * @param array $venue The array of venue data we're modifying.
  128. * @param array <string,string> $record The event record from the import file. Only contains mapped values.
  129. * Useful if value and key above don't appear to match what's expected.
  130. * In the format [ mapped_key => value ].
  131. * @param int $venue_id The ID of the venue we're currently importing.
  132. * @param object $this The current instance of Tribe__Events__Importer__File_Importer_Venues.
  133. *
  134. $record,
  135. */
  136. $venue = apply_filters(
  137. 'tribe_events_importer_venue_array',
  138. $venue,
  139. $record,
  140. $venue_id,
  141. $this
  142. );
  143. return $venue;
  144. }
  145. /**
  146. * Set default venue values.
  147. * Note this will only set a value if it has been mapped, and it is empty.
  148. * If you are using the importer to erase values, you should NOT be triggering this!
  149. *
  150. * @since 5.1.6
  151. *
  152. * @param array $venue The array of venue data we're modifying.
  153. * @param array <string,string> $record The event record from the import file. Only contains mapped values.
  154. * Useful if value and key above don't appear to match what's expected.
  155. * In the format [ mapped_key => value ].
  156. *
  157. * @return array The modified venue data.
  158. */
  159. public function set_defaults( $venue, $record ) {
  160. $columns = [
  161. 'Address' => 'address',
  162. 'City' => 'city',
  163. 'Country' => 'country',
  164. 'Phone' => 'phone',
  165. 'Province' => 'state',
  166. 'State' => 'state',
  167. 'URL' => 'url',
  168. 'Zip' => 'zip',
  169. ];
  170. foreach ( $columns as $name => $key ) {
  171. // Only fill in empty columns that we're importing.
  172. if ( ! isset( $venue[ $name ] ) || ! empty( $venue[ $name ] ) ) {
  173. continue;
  174. }
  175. /**
  176. * Allows filtering of default value before setting.
  177. * Also allows setting a value (specifically for imports) by filter
  178. * that is not set in the admin for manually-created venues.
  179. *
  180. * @since 5.1.6
  181. *
  182. * @param string $value The default value as set in the admin "defaults" settings.
  183. * @param string $key The mapped key for the value we'll be importing.
  184. * From the $columns array above,
  185. * this would be 'state' (the array "value"), for example.
  186. * @param string $name The name for the value.
  187. * From the $columns array above,
  188. * this would be 'Province' (the array "key"), for example.
  189. * @param array $venue The entire array of venue data we're modifying.
  190. * In the format [ $key => $value ].
  191. * @param array <string,string> $record The event record from the import file. Only contains mapped values.
  192. * Useful if value and key above don't appear to match what's expected.
  193. * In the format [ mapped_key => value ].
  194. */
  195. $default_value = apply_filters(
  196. "tribe_events_importer_venue_default_{$key}_value",
  197. tribe_get_default_value( $key ),
  198. $key,
  199. $name,
  200. $venue,
  201. $record
  202. );
  203. /*
  204. * Country comes through as an array: [ 'US', 'United States' ]
  205. * We could handle this with a filter elsewhere, but let's deal with it here
  206. * so we don't break Geolocation functions.
  207. */
  208. if (
  209. 'country' === $key
  210. && is_array( $default_value )
  211. ) {
  212. $default_value = array_pop( $default_value );
  213. }
  214. // Let's not set values that haven't been set in the admin!
  215. if ( empty( $default_value ) ) {
  216. continue;
  217. }
  218. $venue[ $name ] = $default_value;
  219. }
  220. return $venue;
  221. }
  222. }