/wp-content/plugins/mailchimp-for-wp/includes/class-field-map.php

https://gitlab.com/bhargavi_dcw/dflocal · PHP · 291 lines · 119 code · 53 blank · 119 comment · 11 complexity · e82f8735b12cf1d3dfbd9a175e17be91 MD5 · raw file

  1. <?php
  2. /**
  3. * Class MC4WP_Field_Map
  4. *
  5. * @access private
  6. * @since 2.0
  7. * @ignore
  8. */
  9. class MC4WP_Field_Map {
  10. /**
  11. * Raw array of data
  12. *
  13. * @var array
  14. */
  15. protected $raw_data = array();
  16. /**
  17. * Global fields (default list fields)
  18. *
  19. * @var array
  20. */
  21. protected $global_fields = array();
  22. /**
  23. * Array of list instances
  24. *
  25. * @var MC4WP_MailChimp_List[]
  26. */
  27. protected $lists = array();
  28. /**
  29. * Formatted array of data
  30. *
  31. * @var array
  32. */
  33. public $formatted_data = array(
  34. '_MC4WP_LISTS' => array(),
  35. 'GROUPINGS' => array(),
  36. );
  37. /**
  38. * @var array
  39. */
  40. public $pretty_data = array();
  41. /**
  42. * Map of list id's with fields belonging to that list
  43. *
  44. * @var array
  45. */
  46. public $list_fields = array();
  47. /**
  48. * Array of fields not belonging to any of the given lists
  49. *
  50. * @var array
  51. */
  52. public $custom_fields = array();
  53. /**
  54. * @var MC4WP_Field_Formatter
  55. */
  56. private $formatter;
  57. /**
  58. * @param array $raw_data
  59. * @param array $list_ids
  60. */
  61. public function __construct( array $raw_data, array $list_ids ) {
  62. $this->formatter = new MC4WP_Field_Formatter();
  63. $this->raw_data = $raw_data;
  64. $this->lists = $this->fetch_lists( $list_ids );
  65. // 1. Assume all given data is custom
  66. $this->custom_fields = $raw_data;
  67. // 2. Extract global fields (default list fields)
  68. $this->extract_global_fields();
  69. // 3. Extract list-specific fields
  70. $this->extract_list_fields();
  71. // 4. Add all leftover fields to data but make sure not to overwrite known fields
  72. $this->formatted_data = $this->merge( $this->custom_fields, $this->formatted_data );
  73. $this->pretty_data = $this->merge( $this->custom_fields, $this->pretty_data );
  74. }
  75. /**
  76. * @param array $list_ids
  77. * @return MC4WP_MailChimp_List[]
  78. */
  79. protected function fetch_lists( array $list_ids ) {
  80. $mailchimp = new MC4WP_MailChimp();
  81. $lists = array();
  82. foreach( $list_ids as $id ) {
  83. $list = $mailchimp->get_list( $id, true );
  84. if( $list instanceof MC4WP_MailChimp_List ) {
  85. $lists[ $id ] = $list;
  86. }
  87. }
  88. return $lists;
  89. }
  90. /**
  91. * @return array
  92. */
  93. protected function extract_list_fields() {
  94. array_walk( $this->lists, array( $this, 'extract_fields_for_list' ) );
  95. $this->formatted_data[ '_MC4WP_LISTS' ] = wp_list_pluck( $this->lists, 'name' );
  96. $this->pretty_data[ 'Lists' ] = $this->formatted_data[ '_MC4WP_LISTS' ];
  97. }
  98. /**
  99. * @param MC4WP_MailChimp_List $list
  100. *
  101. * @return array
  102. */
  103. protected function extract_fields_for_list( MC4WP_MailChimp_List $list ) {
  104. $this->list_fields[ $list->id ] = array(
  105. 'GROUPINGS' => array(),
  106. );
  107. // extract values for merge_vars & groupings
  108. array_walk( $list->merge_vars, array( $this, 'extract_merge_var' ), $list );
  109. array_walk( $list->groupings, array( $this, 'extract_grouping' ), $list );
  110. // filter out empty values
  111. $this->list_fields[ $list->id ]['GROUPINGS'] = array_filter( $this->list_fields[ $list->id ]['GROUPINGS'] );
  112. $this->list_fields[ $list->id ] = array_filter( $this->list_fields[ $list->id ] );
  113. // add global fields (fields belong to ALL lists automatically)
  114. $this->list_fields[ $list->id ] = $this->merge( $this->list_fields[ $list->id ], $this->global_fields );
  115. }
  116. /**
  117. * @param MC4WP_MailChimp_Merge_Var $merge_var
  118. *
  119. * @return mixed
  120. */
  121. protected function extract_merge_var( MC4WP_MailChimp_Merge_Var $merge_var, $index, MC4WP_MailChimp_List $list ) {
  122. // if field is not set, continue.
  123. // don't use empty here as empty fields are perfectly valid (for non-required fields)
  124. if( ! isset( $this->raw_data[ $merge_var->tag ] ) ) {
  125. return;
  126. }
  127. // grab field value from data
  128. $value = $this->raw_data[ $merge_var->tag ];
  129. unset( $this->custom_fields[ $merge_var->tag ] );
  130. // format field value according to its type
  131. $value = $this->format_merge_var_value( $value, $merge_var->field_type );
  132. // store
  133. $this->list_fields[ $list->id ][ $merge_var->tag ] = $value;
  134. $this->formatted_data[ $merge_var->tag ] = $value;
  135. $this->pretty_data[ $merge_var->name ] = $value;
  136. }
  137. /**
  138. * @param MC4WP_MailChimp_Grouping $grouping
  139. * @param string $index
  140. * @param MC4WP_MailChimp_List $list
  141. *
  142. * @return array|null
  143. */
  144. protected function extract_grouping( MC4WP_MailChimp_Grouping $grouping, $index, MC4WP_MailChimp_List $list ) {
  145. // check if data for this group was sent
  146. if( ! empty( $this->raw_data['GROUPINGS'][$grouping->id] ) ) {
  147. $groups = $this->raw_data['GROUPINGS'][$grouping->id];
  148. } elseif( ! empty( $this->raw_data['GROUPINGS'][$grouping->name] ) ) {
  149. $groups = $this->raw_data['GROUPINGS'][$grouping->name];
  150. } else {
  151. return;
  152. }
  153. // reset entire groupings array here
  154. unset( $this->custom_fields['GROUPINGS'] );
  155. // make sure groups is an array
  156. if( ! is_array( $groups ) ) {
  157. $groups = array_map( 'trim', explode( ',', $groups ) );
  158. }
  159. // if groups is an array of id's, get the group name instead
  160. foreach( $groups as $key => $group_name_or_id ) {
  161. if( is_numeric( $group_name_or_id ) && isset( $grouping->groups[ $group_name_or_id ] ) ) {
  162. $groups[ $key ] = $grouping->groups[ $group_name_or_id ];
  163. }
  164. }
  165. // format grouping data for MailChimp
  166. $formatted_grouping = array(
  167. 'id' => $grouping->id,
  168. 'groups' => $groups,
  169. );
  170. // add to list data
  171. $this->list_fields[ $list->id ]['GROUPINGS'][] = $formatted_grouping;
  172. $this->formatted_data['GROUPINGS'][ $grouping->id ] = $groups;
  173. //
  174. $this->pretty_data[ $grouping->name ] = $groups;
  175. }
  176. /**
  177. * @return array
  178. */
  179. protected function extract_global_fields() {
  180. // map global fields
  181. $global_field_names = array(
  182. 'MC_LOCATION',
  183. 'MC_NOTES',
  184. 'MC_LANGUAGE',
  185. 'OPTIN_IP',
  186. );
  187. foreach( $global_field_names as $field_name ) {
  188. if( isset( $this->raw_data[ $field_name ] ) ) {
  189. $value = $this->raw_data[ $field_name ];
  190. // MC_LANGUAGE expects a 2 char code.
  191. if( $field_name === 'MC_LANGUAGE' ) {
  192. $value = substr( $value, 0, 2 );
  193. }
  194. $this->global_fields[ $field_name ] = $value;
  195. $this->formatted_data[ $field_name ] = $value;
  196. unset( $this->custom_fields[ $field_name ] );
  197. }
  198. }
  199. }
  200. /**
  201. * Format field value according to its type
  202. *
  203. * @param $field_type
  204. * @param $field_value
  205. *
  206. * @return array|string
  207. */
  208. protected function format_merge_var_value( $field_value, $field_type ) {
  209. $field_type = strtolower( $field_type );
  210. if( method_exists( $this->formatter, $field_type ) ) {
  211. $field_value = call_user_func( array( $this->formatter, $field_type ), $field_value );
  212. }
  213. /**
  214. * Filters the value of a field after it is formatted.
  215. *
  216. * Use this to format a field value according to the field type (in MailChimp).
  217. *
  218. * @since 3.0
  219. * @param string $field_value The value
  220. * @param string $field_type The type of the field (in MailChimp)
  221. */
  222. $field_value = apply_filters( 'mc4wp_format_field_value', $field_value, $field_type );
  223. return $field_value;
  224. }
  225. /**
  226. * @param array $one
  227. * @param array $two
  228. *
  229. * @return array
  230. */
  231. protected function merge( array $one, array $two ) {
  232. // fallback for PHP 5.2
  233. if( ! function_exists( 'array_replace_recursive' ) ) {
  234. return array_merge( $one, $two );
  235. }
  236. return array_replace_recursive( $one, $two );
  237. }
  238. }