PageRenderTime 40ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/events-manager/multilingual/em-ml-io.php

https://gitlab.com/pankajmohale/chef2go
PHP | 266 lines | 169 code | 16 blank | 81 comment | 35 complexity | 79b5bb5068731ded9be433583cca463c MD5 | raw file
  1. <?php
  2. /**
  3. * Model-related functions. Each ML plugin will do its own thing so must be accounted for accordingly. Below is a description of things that should happen so everything 'works'.
  4. *
  5. * When an event or location is saved, we need to perform certain options depending whether saved on the front-end editor,
  6. * or if saved/translated in the backend, since events share information across translations.
  7. *
  8. * Event translations should assign one event to be the 'original' event, meaning bookings and event times will be managed by the 'orignal' event.
  9. * Since ML Plugins can change default languages and you can potentially create events in non-default languages first, the first language will be the 'orignal' event.
  10. * If an event is deleted and is the original event, but there are still other translations, the original event is reassigned to the default language translation, or whichever other event is found first
  11. */
  12. class EM_ML_IO {
  13. public static function init(){
  14. //Saving/Editing
  15. add_filter('em_event_save_meta','EM_ML_IO::event_save_meta',1000000000000,2); //let other add-ons hook in first
  16. add_filter('em_event_get_post_meta','EM_ML_IO::event_get_post_meta',10,2);
  17. add_filter('em_location_save_meta','EM_ML_IO::location_save_meta',1000000000000,2); //let other add-ons hook in first
  18. add_filter('em_location_get_post_meta','EM_ML_IO::location_get_post_meta',10,2);
  19. //Deletion
  20. add_action('em_event_delete_meta_event_pre', 'EM_ML_IO::event_delete', 10, 1);
  21. add_action('em_location_delete_meta_pre', 'EM_ML_IO::location_delete', 10, 1);
  22. //Loading
  23. add_filter('em_event_get_location','EM_ML_IO::event_get_location',10,2);
  24. //Duplication link
  25. add_filter('em_event_duplicate_url','EM_ML_IO::event_duplicate_url',10, 2);
  26. }
  27. /**
  28. * Changes necessary event location to same language as event if different
  29. * @param EM_Event $EM_Event
  30. */
  31. public static function event_get_location( $EM_Location, $EM_Event ){
  32. if( $EM_Location->location_id ){
  33. $event_lang = EM_ML::get_the_language($EM_Event);
  34. $location_lang = EM_ML::get_the_language($EM_Location);
  35. if( $event_lang != $location_lang ){
  36. $EM_Location = EM_ML::get_translation($EM_Location, $event_lang);
  37. }
  38. }
  39. return $EM_Location;
  40. }
  41. public static function event_save_meta($result, $EM_Event){
  42. if( $result && EM_ML::is_original($EM_Event) ){
  43. //save post meta for all others as well
  44. foreach( EM_ML::get_langs() as $lang_code => $language ){
  45. $event = EM_ML::get_translation($EM_Event, $lang_code); /* @var $EM_Event EM_Event */
  46. if( $event->event_id != $EM_Event->event_id ){
  47. self::event_merge_original_meta($event, $EM_Event);
  48. //if we execute a meta save here, we will screw up the current em_event_save_meta $wp_filter pointer executed in do_action()
  49. //therefore, we save the current pointer position (priority) and set it back after saving the location further down
  50. global $wp_filter, $wp_current_filter;
  51. $wp_filter_priority = key($wp_filter['em_event_save_meta']);
  52. $tag = end($wp_current_filter);
  53. //save the event meta
  54. $event->save_meta();
  55. //reset save_post pointer in $wp_filter to its original position
  56. reset( $wp_filter[$tag] );
  57. do{
  58. if( key($wp_filter[$tag]) == $wp_filter_priority ) break;
  59. }while ( next($wp_filter[$tag]) !== false );
  60. }
  61. }
  62. }
  63. return $result;
  64. }
  65. public static function event_merge_original_meta( $EM_Event, $event ){
  66. $EM_Event->original_event_id = $event->event_id;
  67. //set values over from original event
  68. $EM_Event->event_start_date = $event->event_start_date ;
  69. $EM_Event->event_end_date = $event->event_end_date ;
  70. $EM_Event->recurrence = $event->recurrence ;
  71. $EM_Event->post_type = $event->post_type ;
  72. $EM_Event->location_id = $event->location_id ;
  73. $EM_Event->location = false;
  74. $EM_Event->event_all_day = $event->event_all_day ;
  75. $EM_Event->event_start_time = $event->event_start_time ;
  76. $EM_Event->event_end_time = $event->event_end_time ;
  77. $EM_Event->start = $event->start ;
  78. $EM_Event->end = $event->end ;
  79. $EM_Event->event_rsvp_date = $event->event_rsvp_date ;
  80. $EM_Event->event_rsvp = $event->event_rsvp ;
  81. $EM_Event->event_rsvp_time = $event->event_rsvp_time ;
  82. $EM_Event->blog_id = $event->blog_id ;
  83. $EM_Event->group_id = $event->group_id ;
  84. $EM_Event->recurrence = $event->recurrence ;
  85. $EM_Event->recurrence_freq = $event->recurrence_freq ;
  86. $EM_Event->recurrence_byday = $event->recurrence_byday ;
  87. $EM_Event->recurrence_interval = $event->recurrence_interval ;
  88. $EM_Event->recurrence_byweekno = $event->recurrence_byweekno ;
  89. $EM_Event->recurrence_days = $event->recurrence_days ;
  90. self::event_merge_original_attributes($EM_Event, $event);
  91. }
  92. public static function event_merge_original_attributes($EM_Event, $event){
  93. //merge attributes
  94. $event->event_attributes = maybe_unserialize($event->event_attributes);
  95. $EM_Event->event_attributes = maybe_unserialize($EM_Event->event_attributes);
  96. foreach($event->event_attributes as $event_attribute_key => $event_attribute){
  97. if( !empty($event_attribute) && empty($EM_Event->event_attributes[$event_attribute_key]) ){
  98. $EM_Event->event_attributes[$event_attribute_key] = $event_attribute;
  99. }
  100. }
  101. }
  102. /**
  103. * Hooks into em_event_get_post and writes the original event translation data into the current event, to avoid validation errors and correct data saving.
  104. * @param boolean $result
  105. * @param EM_Event $EM_Event
  106. * @return boolean
  107. */
  108. public static function event_get_post_meta($result, $EM_Event){
  109. //check if this is a master event, if not then we need to get the relevant master event info and populate this object with it so it passes validation and saves correctly.
  110. if( !EM_ML::is_original($EM_Event) ){
  111. //get original event object
  112. $event = EM_ML::get_original_event($EM_Event);
  113. EM_ML_IO::event_merge_original_meta($EM_Event, $event);
  114. if( $EM_Event->location_id == 0 ) $_POST['no_location'] = 1;
  115. // We need to save ticket translations here as well to the ticket objects
  116. foreach( $EM_Event->get_tickets()->tickets as $EM_Ticket ){ /* @var $EM_Ticket EM_Ticket */
  117. $ticket_translation = array();
  118. if( !empty($_REQUEST['ticket_translations'][$EM_Ticket->ticket_id]['ticket_name'] ) ) $ticket_translation['ticket_name'] = wp_kses_data(wp_unslash($_REQUEST['ticket_translations'][$EM_Ticket->ticket_id]['ticket_name']));
  119. if( !empty($_REQUEST['ticket_translations'][$EM_Ticket->ticket_id]['ticket_description'] ) ) $ticket_translation['ticket_description'] = wp_kses_post(wp_unslash($_REQUEST['ticket_translations'][$EM_Ticket->ticket_id]['ticket_description']));
  120. if( !empty($ticket_translation) ) $EM_Ticket->ticket_meta['langs'][EM_ML::$current_language] = $ticket_translation;
  121. }
  122. }elseif( !empty($EM_Event->location_id) ){
  123. //we need to make sure the location is the original location
  124. $EM_Location = $EM_Event->get_location();
  125. if( !EM_ML::is_original($EM_Location) ){
  126. $EM_Event->location_id = EM_ML::get_original_location($EM_Location)->location_id;
  127. }
  128. }
  129. return $result;
  130. }
  131. /**
  132. * When a master event is deleted, translations are not necessarily deleted so things like bookings must be transferred to a translation and that must now be the master event.
  133. * @param EM_Event $EM_Location
  134. */
  135. public static function event_delete($EM_Event){
  136. global $wpdb, $sitepress;
  137. if( EM_ML::is_original($EM_Event) ){
  138. //check to see if there's any translations of this event
  139. $event = EM_ML::get_translation($EM_Event);
  140. //if so check if the default language still exists
  141. if( $EM_Event->event_id != $event->event_id && !empty($event->event_id) ){
  142. //make that translation the master event by changing event ids of bookings, tickets etc. to the new master event
  143. $wpdb->update(EM_TICKETS_TABLE, array('event_id'=>$event->event_id), array('event_id'=>$EM_Event->event_id));
  144. $wpdb->update(EM_BOOKINGS_TABLE, array('event_id'=>$event->event_id), array('event_id'=>$EM_Event->event_id));
  145. do_action('em_ml_transfer_original_event', $event, $EM_Event); //other add-ons with tables with event_id foreign keys should hook here and change
  146. }
  147. }
  148. }
  149. /**
  150. * Changes the event id of the link for duplication so that it duplicates the original event instead of a translation.
  151. * Translation plugins should hook into em_event_duplicate, checking to make sure it is the original translation and then duplicating the translations of the original event.
  152. * @param string $url
  153. * @param EM_Event $EM_Event
  154. * @return string
  155. */
  156. public static function event_duplicate_url($url, $EM_Event){
  157. if( !EM_ML::is_original($EM_Event) ){
  158. $EM_Event = EM_ML::get_original($EM_Event);
  159. $url = add_query_arg(array('action'=>'event_duplicate', 'event_id'=>$EM_Event->event_id, '_wpnonce'=> wp_create_nonce('event_duplicate_'.$EM_Event->event_id)));
  160. //this gets escaped later
  161. }
  162. return $url;
  163. }
  164. /**
  165. * Hooks into em_location_get_post_meta and assigns location info from original translation so other translations don't manage location-specific info.
  166. * @param boolean $result
  167. * @param EM_Location $EM_Location
  168. * @param string $validate
  169. * @return boolean
  170. */
  171. public static function location_get_post_meta($result, $EM_Location, $validate = true){
  172. //check if this is a master location, if not then we need to get the relevant master location info and populate this object with it so it passes validation and saves correctly.
  173. if( !EM_ML::is_original($EM_Location) ){
  174. //get original location object
  175. $location = EM_ML::get_original_location($EM_Location);
  176. self::location_merge_original_meta($EM_Location, $location);
  177. if ($validate) $result = $EM_Location->validate();
  178. }
  179. return $result;
  180. }
  181. /**
  182. * Merge shared translation meta from original $location into $EM_Location.
  183. * @param EM_Location $EM_Location
  184. * @param EM_Location $location
  185. */
  186. public static function location_merge_original_meta( $EM_Location, $location ){
  187. $EM_Location->original_location_id = $location->location_id;
  188. //set values over from original location
  189. $EM_Location->location_address = $location->location_address;
  190. $EM_Location->location_town = $location->location_town;
  191. $EM_Location->location_state = $location->location_state;
  192. $EM_Location->location_postcode = $location->location_postcode;
  193. $EM_Location->location_region = $location->location_region;
  194. $EM_Location->location_country = $location->location_country;
  195. $EM_Location->location_latitude = $location->location_latitude;
  196. $EM_Location->location_longitude = $location->location_longitude;
  197. self::location_merge_original_attributes($EM_Location, $location);
  198. }
  199. public static function location_merge_original_attributes($EM_Location, $location){
  200. //merge attributes
  201. $location->location_attributes = maybe_unserialize($location->location_attributes);
  202. $EM_Location->location_attributes = maybe_unserialize($EM_Location->location_attributes);
  203. foreach($location->location_attributes as $attribute_key => $attribute){
  204. if( !empty($attribute) && empty($EM_Location->location_attributes[$attribute_key]) ){
  205. $EM_Location->location_attributes[$attribute_key] = $attribute;
  206. }
  207. }
  208. }
  209. /**
  210. * When a master location is deleted, translations are not necessarily deleted so things like event-location linkage must be transferred to a translation and that must now be the master event.
  211. * @param EM_Location $EM_Location
  212. */
  213. public static function location_delete($EM_Location){
  214. global $wpdb, $sitepress;
  215. if( EM_ML::is_original($EM_Location) ){
  216. //check to see if there's any translations of this event
  217. $location = EM_ML::get_translation($EM_Location);
  218. //if so check if the default language still exists
  219. if( $EM_Location->location_id != $location->location_id && !empty($location->location_id) ){
  220. //make that translation the master event by changing event ids of bookings, tickets etc. to the new master event
  221. $wpdb->update(EM_EVENTS_TABLE, array('location_id'=>$location->location_id), array('location_id'=>$EM_Location->location_id));
  222. //also change wp_postmeta
  223. $EM_Location->ms_global_switch();
  224. $wpdb->update($wpdb->postmeta, array('meta_value'=>$location->location_id), array('meta_key'=>'_location_id', 'meta_value'=>$EM_Location->location_id));
  225. $EM_Location->ms_global_switch_back();
  226. do_action('em_ml_transfer_original_location', $location, $EM_Location); //other add-ons with tables with location_id foreign keys should hook here and change
  227. }
  228. }
  229. }
  230. /**
  231. * When saving an original location, save shared meta to translations as well.
  232. * @param boolean $result
  233. * @param EM_Location $EM_Location
  234. * @return boolean
  235. */
  236. public static function location_save_meta($result, $EM_Location){
  237. if( $result && EM_ML::is_original($EM_Location) ){
  238. //save post meta for all others as well
  239. foreach( EM_ML::get_langs() as $lang_code => $language ){
  240. $location = EM_ML::get_translation($EM_Location, $lang_code); /* @var $EM_Location EM_Location */
  241. if( $location->location_id != $EM_Location->location_id ){
  242. self::location_merge_original_meta($location, $EM_Location);
  243. $location->save_meta();
  244. }
  245. }
  246. }
  247. return $result;
  248. }
  249. }
  250. EM_ML_IO::init();