PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/mailchimp-for-wp/includes/functions.php

https://gitlab.com/ebrjose/comcebu
PHP | 499 lines | 226 code | 75 blank | 198 comment | 41 complexity | cb25cd9b90dda84f5a8d0ea02432d447 MD5 | raw file
  1. <?php
  2. /**
  3. * Get a service by its name
  4. *
  5. * _Example:_
  6. *
  7. * $forms = mc4wp('forms');
  8. * $api = mc4wp('api');
  9. *
  10. * When no service parameter is given, the entire container will be returned.
  11. *
  12. * @ignore
  13. * @access private
  14. *
  15. * @param string $service (optional)
  16. * @return mixed
  17. *
  18. * @throws Exception when service is not found
  19. */
  20. function mc4wp( $service = null ) {
  21. static $mc4wp;
  22. if ( ! $mc4wp ) {
  23. $mc4wp = new MC4WP_Container();
  24. }
  25. if ( $service ) {
  26. return $mc4wp->get( $service );
  27. }
  28. return $mc4wp;
  29. }
  30. /**
  31. * Gets the Mailchimp for WP options from the database
  32. * Uses default values to prevent undefined index notices.
  33. *
  34. * @since 1.0
  35. * @access public
  36. * @static array $options
  37. * @return array
  38. */
  39. function mc4wp_get_options() {
  40. $defaults = require MC4WP_PLUGIN_DIR . '/config/default-settings.php';
  41. $options = (array) get_option( 'mc4wp', array() );
  42. $options = array_merge( $defaults, $options );
  43. /**
  44. * Filters the Mailchimp for WordPress settings (general).
  45. *
  46. * @param array $options
  47. */
  48. return apply_filters( 'mc4wp_settings', $options );
  49. }
  50. /**
  51. * @return array
  52. */
  53. function mc4wp_get_settings() {
  54. return mc4wp_get_options();
  55. }
  56. /**
  57. * @since 4.2.6
  58. * @return string
  59. */
  60. function mc4wp_get_api_key() {
  61. // try to get from constant
  62. if ( defined( 'MC4WP_API_KEY' ) && constant( 'MC4WP_API_KEY' ) !== '' ) {
  63. return MC4WP_API_KEY;
  64. }
  65. // get from options
  66. $opts = mc4wp_get_options();
  67. return $opts['api_key'];
  68. }
  69. /**
  70. * Gets the Mailchimp for WP API class (v3) and injects it with the API key
  71. *
  72. * @since 4.0
  73. * @access public
  74. *
  75. * @return MC4WP_API_V3
  76. */
  77. function mc4wp_get_api_v3() {
  78. $api_key = mc4wp_get_api_key();
  79. $instance = new MC4WP_API_V3( $api_key );
  80. return $instance;
  81. }
  82. /**
  83. * Gets the Mailchimp for WP API class and injects it with the API key
  84. *
  85. * @deprecated 4.0
  86. * @use mc4wp_get_api_v3
  87. *
  88. * @since 1.0
  89. * @access public
  90. *
  91. * @return MC4WP_API
  92. */
  93. function mc4wp_get_api() {
  94. _deprecated_function( __FUNCTION__, '4.0', 'mc4wp_get_api_v3' );
  95. $api_key = mc4wp_get_api_key();
  96. $instance = new MC4WP_API( $api_key );
  97. return $instance;
  98. }
  99. /**
  100. * Creates a new instance of the Debug Log
  101. *
  102. * @return MC4WP_Debug_Log
  103. */
  104. function mc4wp_get_debug_log() {
  105. $opts = mc4wp_get_options();
  106. // get default log file location
  107. $upload_dir = wp_upload_dir( null, false );
  108. $file = $upload_dir['basedir'] . '/mailchimp-for-wp/debug-log.php';
  109. $default_file = $file;
  110. /**
  111. * Filters the log file to write to.
  112. *
  113. * @param string $file The log file location. Default: /wp-content/uploads/mailchimp-for-wp/mc4wp-debug.log
  114. */
  115. $file = apply_filters( 'mc4wp_debug_log_file', $file );
  116. if ( $file === $default_file ) {
  117. $dir = dirname( $file );
  118. if ( ! is_dir( $dir ) ) {
  119. mkdir( $dir, 0755, true );
  120. }
  121. if ( ! is_file( $dir . '/.htaccess' ) ) {
  122. $lines = array(
  123. '<IfModule !authz_core_module>',
  124. 'Order deny,allow',
  125. 'Deny from all',
  126. '</IfModule>',
  127. '<IfModule authz_core_module>',
  128. 'Require all denied',
  129. '</IfModule>',
  130. );
  131. file_put_contents( $dir . '/.htaccess', join( PHP_EOL, $lines ) );
  132. }
  133. if ( ! is_file( $dir . '/index.html' ) ) {
  134. file_put_contents( $dir . '/index.html', '' );
  135. }
  136. }
  137. /**
  138. * Filters the minimum level to log messages.
  139. *
  140. * @see MC4WP_Debug_Log
  141. *
  142. * @param string|int $level The minimum level of messages which should be logged.
  143. */
  144. $level = apply_filters( 'mc4wp_debug_log_level', $opts['debug_log_level'] );
  145. return new MC4WP_Debug_Log( $file, $level );
  146. }
  147. /**
  148. * Get URL to a file inside the plugin directory
  149. *
  150. * @since 4.8.3
  151. * @param string $path
  152. * @return string
  153. */
  154. function mc4wp_plugin_url( $path ) {
  155. static $base = null;
  156. if ( $base === null ) {
  157. $base = plugins_url( '/', MC4WP_PLUGIN_FILE );
  158. }
  159. return $base . $path;
  160. }
  161. /**
  162. * Get current URL (full)
  163. *
  164. * @return string
  165. */
  166. function mc4wp_get_request_url() {
  167. global $wp;
  168. // get requested url from global $wp object
  169. $site_request_uri = $wp->request;
  170. // fix for IIS servers using index.php in the URL
  171. if ( false !== stripos( $_SERVER['REQUEST_URI'], '/index.php/' . $site_request_uri ) ) {
  172. $site_request_uri = 'index.php/' . $site_request_uri;
  173. }
  174. // concatenate request url to home url
  175. $url = home_url( $site_request_uri );
  176. $url = trailingslashit( $url );
  177. return esc_url( $url );
  178. }
  179. /**
  180. * Get current URL path.
  181. *
  182. * @return string
  183. */
  184. function mc4wp_get_request_path() {
  185. return $_SERVER['REQUEST_URI'];
  186. }
  187. /**
  188. * Get IP address for client making current request
  189. *
  190. * @return string|null
  191. */
  192. function mc4wp_get_request_ip_address() {
  193. if ( isset( $_SERVER['X-Forwarded-For'] ) ) {
  194. return $_SERVER['X-Forwarded-For'];
  195. }
  196. if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  197. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  198. }
  199. if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
  200. return $_SERVER['REMOTE_ADDR'];
  201. }
  202. return null;
  203. }
  204. /**
  205. * Strips all HTML tags from all values in a mixed variable, then trims the result.
  206. *
  207. * @access public
  208. * @param mixed $value
  209. *
  210. * @return mixed
  211. */
  212. function mc4wp_sanitize_deep( $value ) {
  213. if ( is_scalar( $value ) ) {
  214. // strip all HTML tags & whitespace
  215. $value = trim( strip_tags( $value ) );
  216. // convert &amp; back to &
  217. $value = html_entity_decode( $value, ENT_NOQUOTES );
  218. } elseif ( is_array( $value ) ) {
  219. $value = array_map( 'mc4wp_sanitize_deep', $value );
  220. } elseif ( is_object( $value ) ) {
  221. $vars = get_object_vars( $value );
  222. foreach ( $vars as $key => $data ) {
  223. $value->{$key} = mc4wp_sanitize_deep( $data );
  224. }
  225. }
  226. return $value;
  227. }
  228. /**
  229. *
  230. * @since 4.0
  231. * @ignore
  232. *
  233. * @param array $data
  234. * @return array
  235. */
  236. function _mc4wp_update_groupings_data( $data = array() ) {
  237. // data still has old "GROUPINGS" key?
  238. if ( empty( $data['GROUPINGS'] ) ) {
  239. return $data;
  240. }
  241. // prepare new key
  242. if ( ! isset( $data['INTERESTS'] ) ) {
  243. $data['INTERESTS'] = array();
  244. }
  245. $map = get_option( 'mc4wp_groupings_map', array() );
  246. foreach ( $data['GROUPINGS'] as $grouping_id => $groups ) {
  247. // for compatibility with expanded grouping arrays
  248. $grouping_key = $grouping_id;
  249. if ( is_array( $groups ) && isset( $groups['id'] ) && isset( $groups['groups'] ) ) {
  250. $grouping_id = $groups['id'];
  251. $groups = $groups['groups'];
  252. }
  253. // do we have transfer data for this grouping id?
  254. if ( ! isset( $map[ $grouping_id ] ) ) {
  255. continue;
  256. }
  257. // if we get a string, explode on delimiter(s)
  258. if ( is_string( $groups ) ) {
  259. // for BC with 3.x: explode on comma's
  260. $groups = join( '|', explode( ',', $groups ) );
  261. // explode on current delimiter
  262. $groups = explode( '|', $groups );
  263. }
  264. // loop through groups and find interest ID
  265. $migrated = 0;
  266. foreach ( $groups as $key => $group_name_or_id ) {
  267. // do we know the new interest ID?
  268. if ( empty( $map[ $grouping_id ]['groups'][ $group_name_or_id ] ) ) {
  269. continue;
  270. }
  271. $interest_id = $map[ $grouping_id ]['groups'][ $group_name_or_id ];
  272. // add to interests data
  273. if ( ! in_array( $interest_id, $data['INTERESTS'], false ) ) {
  274. $migrated++;
  275. $data['INTERESTS'][] = $interest_id;
  276. }
  277. }
  278. // remove old grouping ID if we migrated all groups.
  279. if ( $migrated === count( $groups ) ) {
  280. unset( $data['GROUPINGS'][ $grouping_key ] );
  281. }
  282. }
  283. // if everything went well, this is now empty & moved to new INTERESTS key.
  284. if ( empty( $data['GROUPINGS'] ) ) {
  285. unset( $data['GROUPINGS'] );
  286. }
  287. // is this empty? just unset it then.
  288. if ( empty( $data['INTERESTS'] ) ) {
  289. unset( $data['INTERESTS'] );
  290. }
  291. return $data;
  292. }
  293. /**
  294. * Guesses merge vars based on given data & current request.
  295. *
  296. * @since 3.0
  297. * @access public
  298. *
  299. * @param array $data
  300. *
  301. * @return array
  302. */
  303. function mc4wp_add_name_data( $data = array() ) {
  304. // Guess first and last name
  305. if ( ! empty( $data['NAME'] ) && empty( $data['FNAME'] ) && empty( $data['LNAME'] ) ) {
  306. $data['NAME'] = trim( $data['NAME'] );
  307. $strpos = strpos( $data['NAME'], ' ' );
  308. if ( $strpos !== false ) {
  309. $data['FNAME'] = trim( substr( $data['NAME'], 0, $strpos ) );
  310. $data['LNAME'] = trim( substr( $data['NAME'], $strpos ) );
  311. } else {
  312. $data['FNAME'] = $data['NAME'];
  313. }
  314. }
  315. // Set name value
  316. if ( empty( $data['NAME'] ) && ! empty( $data['FNAME'] ) && ! empty( $data['LNAME'] ) ) {
  317. $data['NAME'] = sprintf( '%s %s', $data['FNAME'], $data['LNAME'] );
  318. }
  319. return $data;
  320. }
  321. /**
  322. * Gets the "email type" for new subscribers.
  323. *
  324. * Possible return values are either "html" or "text"
  325. *
  326. * @access public
  327. * @since 3.0
  328. *
  329. * @return string
  330. */
  331. function mc4wp_get_email_type() {
  332. $email_type = 'html';
  333. /**
  334. * Filters the email type preference for this new subscriber.
  335. *
  336. * @param string $email_type
  337. */
  338. $email_type = (string) apply_filters( 'mc4wp_email_type', $email_type );
  339. return $email_type;
  340. }
  341. /**
  342. *
  343. * @ignore
  344. * @return bool
  345. */
  346. function _mc4wp_use_sslverify() {
  347. // Disable for all transports other than CURL
  348. if ( ! function_exists( 'curl_version' ) ) {
  349. return false;
  350. }
  351. $curl = curl_version();
  352. // Disable if OpenSSL is not installed
  353. if ( empty( $curl['ssl_version'] ) ) {
  354. return false;
  355. }
  356. // Disable if on WP 4.4, see https://core.trac.wordpress.org/ticket/34935
  357. if ( $GLOBALS['wp_version'] === '4.4' ) {
  358. return false;
  359. }
  360. return true;
  361. }
  362. /**
  363. * This will replace the first half of a string with "*" characters.
  364. *
  365. * @param string $string
  366. * @return string
  367. */
  368. function mc4wp_obfuscate_string( $string ) {
  369. $length = strlen( $string );
  370. $obfuscated_length = ceil( $length / 2 );
  371. $string = str_repeat( '*', $obfuscated_length ) . substr( $string, $obfuscated_length );
  372. return $string;
  373. }
  374. /**
  375. * @internal
  376. * @ignore
  377. */
  378. function _mc4wp_obfuscate_email_addresses_callback( $m ) {
  379. $one = $m[1] . str_repeat( '*', strlen( $m[2] ) );
  380. $two = $m[3] . str_repeat( '*', strlen( $m[4] ) );
  381. $three = $m[5];
  382. return sprintf( '%s@%s.%s', $one, $two, $three );
  383. }
  384. /**
  385. * Obfuscates email addresses in a string.
  386. *
  387. * @param $string String possibly containing email address
  388. * @return string
  389. */
  390. function mc4wp_obfuscate_email_addresses( $string ) {
  391. return preg_replace_callback( '/([\w\.]{1,4})([\w\.]*)\@(\w{1,2})(\w*)\.(\w+)/', '_mc4wp_obfuscate_email_addresses_callback', $string );
  392. }
  393. /**
  394. * Refreshes Mailchimp lists. This can take a while if the connected Mailchimp account has many lists.
  395. *
  396. * @return void
  397. */
  398. function mc4wp_refresh_mailchimp_lists() {
  399. $mailchimp = new MC4WP_MailChimp();
  400. $mailchimp->refresh_lists();
  401. }
  402. /**
  403. * Get element from array, allows for dot notation eg: "foo.bar"
  404. *
  405. * @param array $array
  406. * @param string $key
  407. * @param mixed $default
  408. * @return mixed
  409. */
  410. function mc4wp_array_get( $array, $key, $default = null ) {
  411. if ( is_null( $key ) ) {
  412. return $array;
  413. }
  414. if ( isset( $array[ $key ] ) ) {
  415. return $array[ $key ];
  416. }
  417. foreach ( explode( '.', $key ) as $segment ) {
  418. if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
  419. return $default;
  420. }
  421. $array = $array[ $segment ];
  422. }
  423. return $array;
  424. }