Bez popisu

um-short-functions.php 67KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
  2. //Make public functions without class creation
  3. /**
  4. * Trim string by char length
  5. *
  6. *
  7. * @param $s
  8. * @param int $length
  9. *
  10. * @return string
  11. */
  12. function um_trim_string( $s, $length = 20 ) {
  13. $s = mb_strlen( $s ) > $length ? substr( $s, 0, $length ) . "..." : $s;
  14. return $s;
  15. }
  16. /**
  17. * Get where user should be headed after logging
  18. *
  19. * @param string $redirect_to
  20. *
  21. * @return bool|false|mixed|string|void
  22. */
  23. function um_dynamic_login_page_redirect( $redirect_to = '' ) {
  24. $uri = um_get_core_page( 'login' );
  25. if ( ! $redirect_to ) {
  26. $redirect_to = UM()->permalinks()->get_current_url();
  27. }
  28. $redirect_key = urlencode_deep( $redirect_to );
  29. $uri = add_query_arg( 'redirect_to', $redirect_key, $uri );
  30. return $uri;
  31. }
  32. /**
  33. * Checks if session has been started
  34. *
  35. * @return bool
  36. */
  37. function um_is_session_started() {
  38. if ( php_sapi_name() !== 'cli' ) {
  39. if ( version_compare( phpversion(), '5.4.0', '>=' ) ) {
  40. return session_status() === PHP_SESSION_ACTIVE ? true : false;
  41. } else {
  42. return session_id() === '' ? false : true;
  43. }
  44. }
  45. return false;
  46. }
  47. /**
  48. * User clean basename
  49. *
  50. * @param $value
  51. *
  52. * @return mixed|void
  53. */
  54. function um_clean_user_basename( $value ) {
  55. $raw_value = $value;
  56. $value = str_replace( '.', ' ', $value );
  57. $value = str_replace( '-', ' ', $value );
  58. $value = str_replace( '+', ' ', $value );
  59. /**
  60. * UM hook
  61. *
  62. * @type filter
  63. * @title um_clean_user_basename_filter
  64. * @description Change clean user basename
  65. * @input_vars
  66. * [{"var":"$basename","type":"string","desc":"User basename"},
  67. * {"var":"$raw_basename","type":"string","desc":"RAW user basename"}]
  68. * @change_log
  69. * ["Since: 2.0"]
  70. * @usage add_filter( 'um_clean_user_basename_filter', 'function_name', 10, 2 );
  71. * @example
  72. * <?php
  73. * add_filter( 'um_clean_user_basename_filter', 'my_clean_user_basename', 10, 2 );
  74. * function my_clean_user_basename( $basename, $raw_basename ) {
  75. * // your code here
  76. * return $basename;
  77. * }
  78. * ?>
  79. */
  80. $value = apply_filters( 'um_clean_user_basename_filter', $value, $raw_value );
  81. return $value;
  82. }
  83. /**
  84. * Getting replace placeholders array
  85. *
  86. * @return array
  87. */
  88. function um_replace_placeholders() {
  89. $search = array(
  90. '{display_name}',
  91. '{first_name}',
  92. '{last_name}',
  93. '{gender}',
  94. '{username}',
  95. '{email}',
  96. '{site_name}',
  97. '{user_account_link}',
  98. );
  99. /**
  100. * UM hook
  101. *
  102. * @type filter
  103. * @title um_template_tags_patterns_hook
  104. * @description Extend UM placeholders
  105. * @input_vars
  106. * [{"var":"$placeholders","type":"array","desc":"UM Placeholders"}]
  107. * @change_log
  108. * ["Since: 2.0"]
  109. * @usage add_filter( 'um_template_tags_patterns_hook', 'function_name', 10, 1 );
  110. * @example
  111. * <?php
  112. * add_filter( 'um_template_tags_patterns_hook', 'my_template_tags_patterns', 10, 1 );
  113. * function my_template_tags_patterns( $placeholders ) {
  114. * // your code here
  115. * $placeholders[] = '{my_custom_placeholder}';
  116. * return $placeholders;
  117. * }
  118. * ?>
  119. */
  120. $search = apply_filters( 'um_template_tags_patterns_hook', $search );
  121. $replace = array(
  122. um_user( 'display_name' ),
  123. um_user( 'first_name' ),
  124. um_user( 'last_name' ),
  125. um_user( 'gender' ),
  126. um_user( 'user_login' ),
  127. um_user( 'user_email' ),
  128. UM()->options()->get( 'site_name' ),
  129. um_get_core_page( 'account' ),
  130. );
  131. /**
  132. * UM hook
  133. *
  134. * @type filter
  135. * @title um_template_tags_replaces_hook
  136. * @description Extend UM replace placeholders
  137. * @input_vars
  138. * [{"var":"$replace_placeholders","type":"array","desc":"UM Replace Placeholders"}]
  139. * @change_log
  140. * ["Since: 2.0"]
  141. * @usage add_filter( 'um_template_tags_replaces_hook', 'function_name', 10, 1 );
  142. * @example
  143. * <?php
  144. * add_filter( 'um_template_tags_replaces_hook', 'my_template_tags_replaces', 10, 1 );
  145. * function my_template_tags_replaces( $replace_placeholders ) {
  146. * // your code here
  147. * $replace_placeholders[] = 'my_replace_value';
  148. * return $replace_placeholders;
  149. * }
  150. * ?>
  151. */
  152. $replace = apply_filters( 'um_template_tags_replaces_hook', $replace );
  153. return array_combine( $search, $replace );
  154. }
  155. /**
  156. * Convert template tags
  157. *
  158. * @param $content
  159. * @param array $args
  160. * @param bool $with_kses
  161. *
  162. * @return mixed|string
  163. */
  164. function um_convert_tags( $content, $args = array(), $with_kses = true ) {
  165. $placeholders = um_replace_placeholders();
  166. $content = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $content );
  167. if ( $with_kses ) {
  168. $content = wp_kses_decode_entities( $content );
  169. }
  170. if ( isset( $args['tags'] ) && isset( $args['tags_replace'] ) ) {
  171. $content = str_replace( $args['tags'], $args['tags_replace'], $content );
  172. }
  173. $regex = '~\{(usermeta:[^}]*)\}~';
  174. preg_match_all( $regex, $content, $matches );
  175. // Support for all usermeta keys
  176. if ( ! empty( $matches[1] ) && is_array( $matches[1] ) ) {
  177. foreach ( $matches[1] as $match ) {
  178. $key = str_replace( 'usermeta:', '', $match );
  179. $value = um_user( $key );
  180. if ( is_array( $value ) ) {
  181. $value = implode( ', ', $value );
  182. }
  183. $content = str_replace( '{' . $match . '}', apply_filters( 'um_convert_tags', $value, $key ), $content );
  184. }
  185. }
  186. return $content;
  187. }
  188. /**
  189. * UM Placeholders for activation link in email
  190. *
  191. * @param $placeholders
  192. *
  193. * @return array
  194. */
  195. function account_activation_link_tags_patterns( $placeholders ) {
  196. $placeholders[] = '{account_activation_link}';
  197. return $placeholders;
  198. }
  199. /**
  200. * UM Replace Placeholders for activation link in email
  201. *
  202. * @param $replace_placeholders
  203. *
  204. * @return array
  205. */
  206. function account_activation_link_tags_replaces( $replace_placeholders ) {
  207. $replace_placeholders[] = um_user( 'account_activation_link' );
  208. return $replace_placeholders;
  209. }
  210. /**
  211. * @function um_user_ip()
  212. *
  213. * @description This function returns the IP address of user.
  214. *
  215. * @usage <?php $user_ip = um_user_ip(); ?>
  216. *
  217. * @return string The user's IP address.
  218. *
  219. * @example The example below can retrieve the user's IP address
  220. *
  221. * <?php
  222. *
  223. * $user_ip = um_user_ip();
  224. * echo 'User IP address is: ' . $user_ip; // prints the user IP address e.g. 127.0.0.1
  225. *
  226. * ?>
  227. */
  228. function um_user_ip() {
  229. $ip = '127.0.0.1';
  230. if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
  231. //check ip from share internet
  232. $ip = $_SERVER['HTTP_CLIENT_IP'];
  233. } else if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  234. //to check ip is pass from proxy
  235. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  236. } else if ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
  237. $ip = $_SERVER['REMOTE_ADDR'];
  238. }
  239. /**
  240. * UM hook
  241. *
  242. * @type filter
  243. * @title um_user_ip
  244. * @description Change User IP
  245. * @input_vars
  246. * [{"var":"$ip","type":"string","desc":"User IP"}]
  247. * @change_log
  248. * ["Since: 2.0"]
  249. * @usage add_filter( 'um_user_ip', 'function_name', 10, 1 );
  250. * @example
  251. * <?php
  252. * add_filter( 'um_user_ip', 'my_user_ip', 10, 1 );
  253. * function my_user_ip( $ip ) {
  254. * // your code here
  255. * return $ip;
  256. * }
  257. * ?>
  258. */
  259. return apply_filters( 'um_user_ip', $ip );
  260. }
  261. /**
  262. * If conditions are met return true;
  263. *
  264. * @param $data
  265. *
  266. * @return bool
  267. */
  268. function um_field_conditions_are_met( $data ) {
  269. if ( ! isset( $data['conditions'] ) ) {
  270. return true;
  271. }
  272. $state = ( isset( $data['conditional_action'] ) && $data['conditional_action'] == 'show' ) ? 1 : 0;
  273. $first_group = 0;
  274. $state_array = array();
  275. $count = count( $state_array );
  276. foreach ( $data['conditions'] as $k => $arr ) {
  277. $val = $arr[3];
  278. $op = $arr[2];
  279. if ( strstr( $arr[1], 'role_' ) ) {
  280. $arr[1] = 'role';
  281. }
  282. $field = um_profile( $arr[1] );
  283. if ( ! isset( $arr[5] ) || $arr[5] != $first_group ) {
  284. if ( $arr[0] == 'show' ) {
  285. switch ($op) {
  286. case 'equals to':
  287. $field = maybe_unserialize( $field );
  288. if (is_array( $field ))
  289. $state = in_array( $val, $field ) ? 'show' : 'hide';
  290. else
  291. $state = ( $field == $val ) ? 'show' : 'hide';
  292. break;
  293. case 'not equals':
  294. $field = maybe_unserialize( $field );
  295. if (is_array( $field ))
  296. $state = !in_array( $val, $field ) ? 'show' : 'hide';
  297. else
  298. $state = ( $field != $val ) ? 'show' : 'hide';
  299. break;
  300. case 'empty':
  301. $state = ( !$field ) ? 'show' : 'hide';
  302. break;
  303. case 'not empty':
  304. $state = ( $field ) ? 'show' : 'hide';
  305. break;
  306. case 'greater than':
  307. if ($field > $val) {
  308. $state = 'show';
  309. } else {
  310. $state = 'hide';
  311. }
  312. break;
  313. case 'less than':
  314. if ($field < $val) {
  315. $state = 'show';
  316. } else {
  317. $state = 'hide';
  318. }
  319. break;
  320. case 'contains':
  321. if (strstr( $field, $val )) {
  322. $state = 'show';
  323. } else {
  324. $state = 'hide';
  325. }
  326. break;
  327. }
  328. } elseif ( $arr[0] == 'hide' ) {
  329. switch ( $op ) {
  330. case 'equals to':
  331. $field = maybe_unserialize( $field );
  332. if ( is_array( $field ) ) {
  333. $state = in_array( $val, $field ) ? 'hide' : 'show';
  334. } else {
  335. $state = ( $field == $val ) ? 'hide' : 'show';
  336. }
  337. break;
  338. case 'not equals':
  339. $field = maybe_unserialize( $field );
  340. if ( is_array( $field ) ) {
  341. $state = ! in_array( $val, $field ) ? 'hide' : 'show';
  342. } else {
  343. $state = ( $field != $val ) ? 'hide' : 'show';
  344. }
  345. break;
  346. case 'empty':
  347. $state = ( ! $field ) ? 'hide' : 'show';
  348. break;
  349. case 'not empty':
  350. $state = ( $field ) ? 'hide' : 'show';
  351. break;
  352. case 'greater than':
  353. if ( $field <= $val ) {
  354. $state = 'hide';
  355. } else {
  356. $state = 'show';
  357. }
  358. break;
  359. case 'less than':
  360. if ( $field >= $val ) {
  361. $state = 'hide';
  362. } else {
  363. $state = 'show';
  364. }
  365. break;
  366. case 'contains':
  367. if ( strstr( $field, $val ) ) {
  368. $state = 'hide';
  369. } else {
  370. $state = 'show';
  371. }
  372. break;
  373. }
  374. }
  375. $first_group++;
  376. array_push( $state_array, $state );
  377. } else {
  378. if ( $arr[0] == 'show' ) {
  379. switch ( $op ) {
  380. case 'equals to':
  381. $field = maybe_unserialize( $field );
  382. if ( is_array( $field ) ) {
  383. $state = in_array( $val, $field ) ? 'show' : 'not_show';
  384. } else {
  385. $state = ( $field == $val ) ? 'show' : 'not_show';
  386. }
  387. break;
  388. case 'not equals':
  389. $field = maybe_unserialize( $field );
  390. if ( is_array( $field ) ) {
  391. $state = ! in_array( $val, $field ) ? 'show' : 'not_show';
  392. } else {
  393. $state = ( $field != $val ) ? 'show' : 'not_show';
  394. }
  395. break;
  396. case 'empty':
  397. $state = ( ! $field ) ? 'show' : 'not_show';
  398. break;
  399. case 'not empty':
  400. $state = ( $field ) ? 'show': 'not_show';
  401. break;
  402. case 'greater than':
  403. if ( $field > $val ) {
  404. $state = 'show';
  405. } else {
  406. $state = 'not_show';
  407. }
  408. break;
  409. case 'less than':
  410. if ( $field < $val ) {
  411. $state = 'show';
  412. } else {
  413. $state = 'not_show';
  414. }
  415. break;
  416. case 'contains':
  417. if ( strstr( $field, $val ) ) {
  418. $state = 'show';
  419. } else {
  420. $state = 'not_show';
  421. }
  422. break;
  423. }
  424. } elseif ( $arr[0] == 'hide' ) {
  425. switch ( $op ) {
  426. case 'equals to':
  427. $field = maybe_unserialize( $field );
  428. if ( is_array( $field ) ) {
  429. $state = in_array( $val, $field ) ? 'hide' : 'not_hide';
  430. } else {
  431. $state = ( $field == $val ) ? 'hide' : 'not_hide';
  432. }
  433. break;
  434. case 'not equals':
  435. $field = maybe_unserialize( $field );
  436. if ( is_array( $field ) ) {
  437. $state = ! in_array( $val, $field ) ? 'hide' : 'not_hide';
  438. } else {
  439. $state = ( $field != $val ) ? 'hide' : 'not_hide';
  440. }
  441. break;
  442. case 'empty':
  443. $state = ( ! $field ) ? 'hide' : 'not_hide';
  444. break;
  445. case 'not empty':
  446. $state = ( $field ) ? 'hide' : 'not_hide';
  447. break;
  448. case 'greater than':
  449. if ( $field <= $val ) {
  450. $state = 'hide';
  451. } else {
  452. $state = 'not_hide';
  453. }
  454. break;
  455. case 'less than':
  456. if ( $field >= $val ) {
  457. $state = 'hide';
  458. } else {
  459. $state = 'not_hide';
  460. }
  461. break;
  462. case 'contains':
  463. if ( strstr( $field, $val ) ) {
  464. $state = 'hide';
  465. } else {
  466. $state = 'not_hide';
  467. }
  468. break;
  469. }
  470. }
  471. if ( isset( $state_array[ $count ] ) ) {
  472. if ( $state_array[ $count ] == 'show' || $state_array[ $count ] == 'not_hide' ) {
  473. if ( $state == 'show' || $state == 'not_hide' ) {
  474. $state_array[ $count ] = 'show';
  475. } else {
  476. $state_array[ $count ] = 'hide';
  477. }
  478. } else {
  479. if ( $state == 'hide' || $state == 'not_show' ) {
  480. $state_array[ $count ] = 'hide';
  481. } else {
  482. $state_array[ $count ] = 'hide';
  483. }
  484. }
  485. } else {
  486. if ( $state == 'show' || $state == 'not_hide' ) {
  487. $state_array[ $count ] = 'show';
  488. } else {
  489. $state_array[ $count ] = 'hide';
  490. }
  491. }
  492. }
  493. }
  494. $result = array_unique( $state_array );
  495. if ( ! in_array( 'show', $result ) ) {
  496. return $state = false;
  497. } else {
  498. return $state = true;
  499. }
  500. }
  501. /**
  502. * Exit and redirect to home
  503. *
  504. * @param string $requested_user_id
  505. * @param string $is_my_profile
  506. */
  507. function um_redirect_home( $requested_user_id = '', $is_my_profile = '' ) {
  508. $url = apply_filters( 'um_redirect_home_custom_url', home_url(), $requested_user_id, $is_my_profile );
  509. exit( wp_redirect( $url ) );
  510. }
  511. /**
  512. * @param $url
  513. */
  514. function um_js_redirect( $url ) {
  515. if ( headers_sent() || empty( $url ) ) {
  516. //for blank redirects
  517. if ( '' == $url ) {
  518. $url = set_url_scheme( '//' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] );
  519. }
  520. register_shutdown_function( function( $url ) {
  521. echo '<script data-cfasync="false" type="text/javascript">window.location = "' . esc_js( $url ) . '"</script>';
  522. }, $url );
  523. if ( 1 < ob_get_level() ) {
  524. while ( ob_get_level() > 1 ) {
  525. ob_end_clean();
  526. }
  527. } ?>
  528. <script data-cfasync='false' type="text/javascript">
  529. window.location = '<?php echo esc_js( $url ); ?>';
  530. </script>
  531. <?php exit;
  532. } else {
  533. wp_redirect( $url );
  534. }
  535. exit;
  536. }
  537. /**
  538. * Get limit of words from sentence
  539. *
  540. * @param $str
  541. * @param int $wordCount
  542. *
  543. * @return string
  544. */
  545. function um_get_snippet( $str, $wordCount = 10 ) {
  546. if (str_word_count( $str, 0, "éèàôù" ) > $wordCount) {
  547. $str = implode(
  548. '',
  549. array_slice(
  550. preg_split(
  551. '/([\s,\.;\?\!]+)/',
  552. $str,
  553. $wordCount * 2 + 1,
  554. PREG_SPLIT_DELIM_CAPTURE
  555. ),
  556. 0,
  557. $wordCount * 2 - 1
  558. )
  559. );
  560. }
  561. return $str;
  562. }
  563. /**
  564. * Format submitted data for Info preview & Email template
  565. * @param boolean $style
  566. * @return string
  567. *
  568. * @since 2.1.4
  569. */
  570. function um_user_submitted_registration_formatted( $style = false ) {
  571. $output = null;
  572. $submitted_data = um_user( 'submitted' );
  573. if ( $style ) {
  574. $output .= '<div class="um-admin-infobox">';
  575. }
  576. // Timestamp
  577. $output .= um_user_submited_display( 'timestamp', __( 'Date Submitted', 'ultimate-member' ) );
  578. $output .= um_user_submited_display( 'form_id', __( 'Form', 'ultimate-member' ), $submitted_data );
  579. if ( isset( $submitted_data['use_gdpr_agreement'] ) ) {
  580. $output .= um_user_submited_display( 'use_gdpr_agreement', __( 'GDPR Applied', 'ultimate-member' ), $submitted_data );
  581. }
  582. if ( isset( $submitted_data ) && is_array( $submitted_data ) ) {
  583. if ( isset( $submitted_data['form_id'] ) ) {
  584. $fields = UM()->query()->get_attr( 'custom_fields', $submitted_data['form_id'] );
  585. $fields = maybe_unserialize( $fields );
  586. }
  587. if ( ! empty( $fields ) ) {
  588. $fields['form_id'] = array( 'title' => __( 'Form', 'ultimate-member' ) );
  589. $rows = array();
  590. UM()->fields()->get_fields = $fields;
  591. foreach ( $fields as $key => $array ) {
  592. if ( isset( $array['type'] ) && $array['type'] == 'row' ) {
  593. $rows[ $key ] = $array;
  594. unset( UM()->fields()->get_fields[ $key ] ); // not needed now
  595. }
  596. }
  597. if ( empty( $rows ) ) {
  598. $rows = array(
  599. '_um_row_1' => array(
  600. 'type' => 'row',
  601. 'id' => '_um_row_1',
  602. 'sub_rows' => 1,
  603. 'cols' => 1,
  604. ),
  605. );
  606. }
  607. foreach ( $rows as $row_id => $row_array ) {
  608. $row_fields = UM()->fields()->get_fields_by_row( $row_id );
  609. if ( $row_fields ) {
  610. $output .= UM()->fields()->new_row_output( $row_id, $row_array );
  611. $sub_rows = ( isset( $row_array['sub_rows'] ) ) ? $row_array['sub_rows'] : 1;
  612. for ( $c = 0; $c < $sub_rows; $c++ ) {
  613. // cols
  614. $cols = ( isset( $row_array['cols'] ) ) ? $row_array['cols'] : 1;
  615. if ( strstr( $cols, ':' ) ) {
  616. $col_split = explode( ':', $cols );
  617. } else {
  618. $col_split = array( $cols );
  619. }
  620. $cols_num = $col_split[ $c ];
  621. // sub row fields
  622. $subrow_fields = null;
  623. $subrow_fields = UM()->fields()->get_fields_in_subrow( $row_fields, $c );
  624. if ( is_array( $subrow_fields ) ) {
  625. if ( isset( $subrow_fields['form_id'] ) ) {
  626. unset( $subrow_fields['form_id'] );
  627. }
  628. $subrow_fields = UM()->fields()->array_sort_by_column( $subrow_fields, 'position' );
  629. if ( $cols_num == 1 ) {
  630. $col1_fields = UM()->fields()->get_fields_in_column( $subrow_fields, 1 );
  631. if ( $col1_fields ) {
  632. foreach ( $col1_fields as $key => $data ) {
  633. $output .= um_user_submited_display( $key, $data['title'] );
  634. }
  635. }
  636. } elseif ( $cols_num == 2 ) {
  637. $col1_fields = UM()->fields()->get_fields_in_column( $subrow_fields, 1 );
  638. if ( $col1_fields ) {
  639. foreach ( $col1_fields as $key => $data ) {
  640. $output .= um_user_submited_display( $key, $data['title'] );
  641. }
  642. }
  643. $col2_fields = UM()->fields()->get_fields_in_column( $subrow_fields, 2 );
  644. if ( $col2_fields ) {
  645. foreach ( $col2_fields as $key => $data ) {
  646. $output .= um_user_submited_display( $key, $data['title'] );
  647. }
  648. }
  649. } else {
  650. $col1_fields = UM()->fields()->get_fields_in_column( $subrow_fields, 1 );
  651. if ( $col1_fields ) {
  652. foreach ( $col1_fields as $key => $data ) {
  653. $output .= um_user_submited_display( $key, $data['title'] );
  654. }
  655. }
  656. $col2_fields = UM()->fields()->get_fields_in_column( $subrow_fields, 2 );
  657. if ( $col2_fields ) {
  658. foreach ( $col2_fields as $key => $data ) {
  659. $output .= um_user_submited_display( $key, $data['title'] );
  660. }
  661. }
  662. $col3_fields = UM()->fields()->get_fields_in_column( $subrow_fields, 3 );
  663. if ( $col3_fields ) {
  664. foreach ( $col3_fields as $key => $data ) {
  665. $output .= um_user_submited_display( $key, $data['title'] );
  666. }
  667. }
  668. }
  669. }
  670. }
  671. }
  672. } // endfor
  673. }
  674. }
  675. if ( $style ) {
  676. $output .= '</div>';
  677. }
  678. return $output;
  679. }
  680. /**
  681. * Prepare template
  682. *
  683. * @param string $k
  684. * @param string $title
  685. * @param array $data
  686. * @param boolean $style
  687. * @return string
  688. *
  689. * @since 2.1.4
  690. */
  691. function um_user_submited_display( $k, $title, $data = array(), $style = true ) {
  692. $output = '';
  693. if ( 'form_id' == $k && isset( $data['form_id'] ) && ! empty( $data['form_id'] ) ) {
  694. $v = sprintf( __( '%s - Form ID#: %s', 'ultimate-member' ), get_the_title( $data['form_id'] ), $data['form_id'] );
  695. } else {
  696. $v = um_user( $k );
  697. }
  698. if ( strstr( $k, 'user_pass' ) || in_array( $k, array( 'g-recaptcha-response', 'request', '_wpnonce', '_wp_http_referer' ) ) ) {
  699. return '';
  700. }
  701. $fields_without_metakey = UM()->builtin()->get_fields_without_metakey();
  702. $type = UM()->fields()->get_field_type( $k );
  703. if ( in_array( $type, $fields_without_metakey ) ) {
  704. return '';
  705. }
  706. if ( ! $v ) {
  707. if ( $style ) {
  708. return "<p><label>$title: </label><span>" . __( '(empty)', 'ultimate-member' ) ."</span></p>";
  709. } else {
  710. return '';
  711. }
  712. }
  713. if ( $type == 'image' || $type == 'file' ) {
  714. $file = basename( $v );
  715. $filedata = get_user_meta( um_user( 'ID' ), $k . "_metadata", true );
  716. $baseurl = UM()->uploader()->get_upload_base_url();
  717. if ( ! file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . $file ) ) {
  718. if ( is_multisite() ) {
  719. //multisite fix for old customers
  720. $baseurl = str_replace( '/sites/' . get_current_blog_id() . '/', '/', $baseurl );
  721. }
  722. }
  723. if ( ! empty( $filedata['original_name'] ) ) {
  724. $v = '<a href="' . esc_attr( $baseurl . um_user( 'ID' ) . '/' . $file ) . '">' . esc_html( $filedata['original_name'] ) . '</a>';
  725. } else {
  726. $v = $baseurl . um_user( 'ID' ) . '/' . $file;
  727. }
  728. }
  729. if ( is_array( $v ) ) {
  730. $v = implode( ',', $v );
  731. }
  732. if ( $k == 'timestamp' ) {
  733. $v = date( "d M Y H:i", $v );
  734. } elseif ( $k == 'use_gdpr_agreement' ) {
  735. $v = date( "d M Y H:i", $v );
  736. }
  737. if ( $style ) {
  738. if ( ! $v ) {
  739. $v = __( '(empty)', 'ultimate-member' );
  740. }
  741. $output .= "<p><label>$title: </label><span>$v</span></p>";
  742. } else {
  743. $output .= "$title: $v" . "<br />";
  744. }
  745. return $output;
  746. }
  747. /**
  748. * Show filtered social link
  749. *
  750. * @param string $key
  751. * @param string $match
  752. *
  753. * @return string
  754. */
  755. function um_filtered_social_link( $key, $match ) {
  756. $value = um_profile( $key );
  757. $submatch = str_replace( 'https://', '', $match );
  758. $submatch = str_replace( 'http://', '', $submatch );
  759. if ( strstr( $value, $submatch ) ) {
  760. $value = 'https://' . $value;
  761. } elseif ( strpos( $value, 'http' ) !== 0 ) {
  762. $value = $match . $value;
  763. }
  764. $value = str_replace( 'https://https://', 'https://', $value );
  765. $value = str_replace( 'http://https://', 'https://', $value );
  766. $value = str_replace( 'https://http://', 'https://', $value );
  767. return $value;
  768. }
  769. /**
  770. * Get filtered meta value after applying hooks
  771. *
  772. * @param $key
  773. * @param bool $data
  774. * @return mixed|string|void
  775. */
  776. function um_filtered_value( $key, $data = false ) {
  777. $value = um_user( $key );
  778. if ( is_array( $value ) ) {
  779. $value = add_magic_quotes( $value );
  780. }
  781. if ( ! $data ) {
  782. $data = UM()->builtin()->get_specific_field( $key );
  783. }
  784. $type = ( isset( $data['type'] ) ) ? $data['type'] : '';
  785. /**
  786. * UM hook
  787. *
  788. * @type filter
  789. * @title um_profile_field_filter_hook__
  790. * @description Change or filter field value
  791. * @input_vars
  792. * [{"var":"$value","type":"string","desc":"Field Value"},
  793. * {"var":"$data","type":"array","desc":"Field Data"},
  794. * {"var":"$type","type":"string","desc":"Field Type"}]
  795. * @change_log
  796. * ["Since: 2.0"]
  797. * @usage add_filter( 'um_profile_field_filter_hook__', 'function_name', 10, 3 );
  798. * @example
  799. * <?php
  800. * add_filter( 'um_profile_field_filter_hook__', 'my_profile_field', 10, 3 );
  801. * function my_profile_field( $value, $data, $type ) {
  802. * // your code here
  803. * return $value;
  804. * }
  805. * ?>
  806. */
  807. $value = apply_filters( 'um_profile_field_filter_hook__', $value, $data, $type );
  808. /**
  809. * UM hook
  810. *
  811. * @type filter
  812. * @title um_profile_field_filter_hook__{$key}
  813. * @description Change or filter field value by field key ($key)
  814. * @input_vars
  815. * [{"var":"$value","type":"string","desc":"Field Value"},
  816. * {"var":"$data","type":"array","desc":"Field Data"}]
  817. * @change_log
  818. * ["Since: 2.0"]
  819. * @usage add_filter( 'um_profile_field_filter_hook__{$key}', 'function_name', 10, 2 );
  820. * @example
  821. * <?php
  822. * add_filter( 'um_profile_field_filter_hook__{$key}', 'my_profile_field', 10, 2 );
  823. * function my_profile_field( $value, $data ) {
  824. * // your code here
  825. * return $value;
  826. * }
  827. * ?>
  828. */
  829. $value = apply_filters( "um_profile_field_filter_hook__{$key}", $value, $data );
  830. /**
  831. * UM hook
  832. *
  833. * @type filter
  834. * @title um_profile_field_filter_hook__{$type}
  835. * @description Change or filter field value by field type ($type)
  836. * @input_vars
  837. * [{"var":"$value","type":"string","desc":"Field Value"},
  838. * {"var":"$data","type":"array","desc":"Field Data"}]
  839. * @change_log
  840. * ["Since: 2.0"]
  841. * @usage add_filter( 'um_profile_field_filter_hook__{$type}', 'function_name', 10, 2 );
  842. * @example
  843. * <?php
  844. * add_filter( 'um_profile_field_filter_hook__{$type}', 'my_profile_field', 10, 2 );
  845. * function my_profile_field( $value, $data ) {
  846. * // your code here
  847. * return $value;
  848. * }
  849. * ?>
  850. */
  851. $value = apply_filters( "um_profile_field_filter_hook__{$type}", $value, $data );
  852. $value = UM()->shortcodes()->emotize( $value );
  853. return $value;
  854. }
  855. /**
  856. * Returns requested User ID or current User ID
  857. *
  858. * @return int
  859. */
  860. function um_profile_id() {
  861. $requested_user = um_get_requested_user();
  862. if ( $requested_user ) {
  863. return um_get_requested_user();
  864. } elseif ( is_user_logged_in() && get_current_user_id() ) {
  865. return get_current_user_id();
  866. }
  867. return 0;
  868. }
  869. /**
  870. * Check that temp upload is valid
  871. *
  872. * @param string $url
  873. *
  874. * @return bool|string
  875. */
  876. function um_is_temp_upload( $url ) {
  877. if ( is_string( $url ) ) {
  878. $url = trim( $url );
  879. }
  880. if ( filter_var( $url, FILTER_VALIDATE_URL ) === false ) {
  881. $url = realpath( $url );
  882. }
  883. if ( ! $url ) {
  884. return false;
  885. }
  886. $url = explode( '/ultimatemember/temp/', $url );
  887. if ( isset( $url[1] ) ) {
  888. if ( strstr( $url[1], '../' ) || strstr( $url[1], '%' ) ) {
  889. return false;
  890. }
  891. $src = UM()->files()->upload_temp . $url[1];
  892. if ( ! file_exists( $src ) ) {
  893. return false;
  894. }
  895. return $src;
  896. }
  897. return false;
  898. }
  899. /**
  900. * Check that temp image is valid
  901. *
  902. * @param $url
  903. *
  904. * @return bool|string
  905. */
  906. function um_is_temp_image( $url ) {
  907. $url = explode( '/ultimatemember/temp/', $url );
  908. if (isset( $url[1] )) {
  909. $src = UM()->files()->upload_temp . $url[1];
  910. if (!file_exists( $src ))
  911. return false;
  912. list( $width, $height, $type, $attr ) = @getimagesize( $src );
  913. if (isset( $width ) && isset( $height ))
  914. return $src;
  915. }
  916. return false;
  917. }
  918. /**
  919. * Check user's file ownership
  920. * @param string $url
  921. * @param int|null $user_id
  922. * @param string|bool $image_path
  923. * @return bool
  924. */
  925. function um_is_file_owner( $url, $user_id = null, $image_path = false ) {
  926. if ( strpos( $url, UM()->uploader()->get_upload_base_url() . $user_id . '/' ) !== false && is_user_logged_in() ) {
  927. $user_basedir = UM()->uploader()->get_upload_user_base_dir( $user_id );
  928. } else {
  929. $user_basedir = UM()->uploader()->get_upload_user_base_dir( 'temp' );
  930. }
  931. $filename = wp_basename( parse_url( $url, PHP_URL_PATH ) );
  932. $file = $user_basedir . DIRECTORY_SEPARATOR . $filename;
  933. if ( file_exists( $file ) ) {
  934. if ( $image_path ) {
  935. return $file;
  936. }
  937. return true;
  938. }
  939. return false;
  940. }
  941. /**
  942. * Check if file is temporary
  943. * @param string $filename
  944. * @return bool
  945. */
  946. function um_is_temp_file( $filename ) {
  947. $user_basedir = UM()->uploader()->get_upload_user_base_dir( 'temp' );
  948. $file = $user_basedir . '/' . $filename;
  949. if ( file_exists( $file ) ) {
  950. return true;
  951. }
  952. return false;
  953. }
  954. /**
  955. * Get user's last login timestamp
  956. *
  957. * @param $user_id
  958. *
  959. * @return mixed|string
  960. */
  961. function um_user_last_login_timestamp( $user_id ) {
  962. $value = get_user_meta( $user_id, '_um_last_login', true );
  963. if ( $value ) {
  964. return $value;
  965. }
  966. return '';
  967. }
  968. /**
  969. * Get user's last login (time diff)
  970. *
  971. * @param int $user_id
  972. *
  973. * @return string
  974. */
  975. function um_user_last_login( $user_id ) {
  976. $value = get_user_meta( $user_id, '_um_last_login', true );
  977. return ! empty( $value ) ? UM()->datetime()->time_diff( $value, current_time( 'timestamp' ) ) : '';
  978. }
  979. /**
  980. * Get core page url
  981. *
  982. * @param $slug
  983. * @param bool $updated
  984. *
  985. * @return bool|false|mixed|string|void
  986. */
  987. function um_get_core_page( $slug, $updated = false ) {
  988. $url = '';
  989. if ( isset( UM()->config()->permalinks[ $slug ] ) ) {
  990. $url = get_permalink( UM()->config()->permalinks[ $slug ] );
  991. if ( $updated ) {
  992. $url = add_query_arg( 'updated', esc_attr( $updated ), $url );
  993. }
  994. }
  995. /**
  996. * UM hook
  997. *
  998. * @type filter
  999. * @title um_get_core_page_filter
  1000. * @description Change UM core page URL
  1001. * @input_vars
  1002. * [{"var":"$url","type":"string","desc":"UM Page URL"},
  1003. * {"var":"$slug","type":"string","desc":"UM Page slug"},
  1004. * {"var":"$updated","type":"bool","desc":"Additional parameter"}]
  1005. * @change_log
  1006. * ["Since: 2.0"]
  1007. * @usage add_filter( 'um_get_core_page_filter', 'function_name', 10, 3 );
  1008. * @example
  1009. * <?php
  1010. * add_filter( 'um_get_core_page_filter', 'my_core_page_url', 10, 3 );
  1011. * function my_core_page_url( $url, $slug, $updated ) {
  1012. * // your code here
  1013. * return $url;
  1014. * }
  1015. * ?>
  1016. */
  1017. return apply_filters( 'um_get_core_page_filter', $url, $slug, $updated );
  1018. }
  1019. /**
  1020. * Check if we are on a UM Core Page or not
  1021. *
  1022. * Default um core pages slugs
  1023. * 'user', 'login', 'register', 'members', 'logout', 'account', 'password-reset'
  1024. *
  1025. * @param string $page UM core page slug
  1026. *
  1027. * @return bool
  1028. */
  1029. function um_is_core_page( $page ) {
  1030. global $post;
  1031. if ( empty( $post ) ) {
  1032. return false;
  1033. }
  1034. if ( isset( $post->ID ) && isset( UM()->config()->permalinks[ $page ] ) && $post->ID == UM()->config()->permalinks[ $page ] ) {
  1035. return true;
  1036. }
  1037. if ( isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $page, true ) == 1 ) {
  1038. return true;
  1039. }
  1040. if ( UM()->external_integrations()->is_wpml_active() ) {
  1041. global $sitepress;
  1042. if ( isset( UM()->config()->permalinks[ $page ] ) && UM()->config()->permalinks[ $page ] == wpml_object_id_filter( $post->ID, 'page', true, $sitepress->get_default_language() ) ) {
  1043. return true;
  1044. }
  1045. }
  1046. if ( isset( $post->ID ) ) {
  1047. $_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true );
  1048. if ( isset( UM()->config()->permalinks[ $page ] ) && ( ( $_icl_lang_duplicate_of == UM()->config()->permalinks[ $page ] && !empty( $_icl_lang_duplicate_of ) ) || UM()->config()->permalinks[ $page ] == $post->ID ) ) {
  1049. return true;
  1050. }
  1051. }
  1052. return false;
  1053. }
  1054. /**
  1055. * @param $post
  1056. * @param $core_page
  1057. *
  1058. * @return bool
  1059. */
  1060. function um_is_core_post( $post, $core_page ) {
  1061. if ( isset( $post->ID ) && isset( UM()->config()->permalinks[ $core_page ] ) && $post->ID == UM()->config()->permalinks[ $core_page ] ) {
  1062. return true;
  1063. }
  1064. if ( isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $core_page, true ) == 1 ) {
  1065. return true;
  1066. }
  1067. if ( isset( $post->ID ) ) {
  1068. $_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true );
  1069. if ( isset( UM()->config()->permalinks[ $core_page ] ) && ( ( $_icl_lang_duplicate_of == UM()->config()->permalinks[ $core_page ] && ! empty( $_icl_lang_duplicate_of ) ) || UM()->config()->permalinks[ $core_page ] == $post->ID ) ) {
  1070. return true;
  1071. }
  1072. }
  1073. return false;
  1074. }
  1075. /**
  1076. * Get styling defaults
  1077. *
  1078. * @param $mode
  1079. *
  1080. * @return array
  1081. */
  1082. function um_styling_defaults( $mode ) {
  1083. $new_arr = array();
  1084. $core_form_meta_all = UM()->config()->core_form_meta_all;
  1085. $core_global_meta_all = UM()->config()->core_global_meta_all;
  1086. foreach ( $core_form_meta_all as $k => $v ) {
  1087. $s = str_replace( $mode . '_', '', $k );
  1088. if (strstr( $k, '_um_' . $mode . '_' ) && !in_array( $s, $core_global_meta_all )) {
  1089. $a = str_replace( '_um_' . $mode . '_', '', $k );
  1090. $b = str_replace( '_um_', '', $k );
  1091. $new_arr[$a] = UM()->options()->get( $b );
  1092. } else if (in_array( $k, $core_global_meta_all )) {
  1093. $a = str_replace( '_um_', '', $k );
  1094. $new_arr[$a] = UM()->options()->get( $a );
  1095. }
  1096. }
  1097. return $new_arr;
  1098. }
  1099. /**
  1100. * Get meta option default
  1101. *
  1102. * @param $id
  1103. *
  1104. * @return string
  1105. */
  1106. function um_get_metadefault( $id ) {
  1107. $core_form_meta_all = UM()->config()->core_form_meta_all;
  1108. return isset( $core_form_meta_all[ '_um_' . $id ] ) ? $core_form_meta_all[ '_um_' . $id ] : '';
  1109. }
  1110. /**
  1111. * boolean for account page editing
  1112. *
  1113. * @return bool
  1114. */
  1115. function um_submitting_account_page() {
  1116. if ( isset( $_POST['_um_account'] ) && $_POST['_um_account'] == 1 && is_user_logged_in() ) {
  1117. return true;
  1118. }
  1119. return false;
  1120. }
  1121. /**
  1122. * Get a user's display name
  1123. *
  1124. * @param $user_id
  1125. *
  1126. * @return string
  1127. */
  1128. function um_get_display_name( $user_id ) {
  1129. um_fetch_user( $user_id );
  1130. $name = um_user( 'display_name' );
  1131. um_reset_user();
  1132. return $name;
  1133. }
  1134. /**
  1135. * Clears the user data. You need to fetch a user manually after using this function.
  1136. *
  1137. * @function um_reset_user_clean()
  1138. *
  1139. * @description This function is similar to um_reset_user() with a difference that it will not use the logged-in
  1140. * user data after resetting. It is a hard-reset function for all user data.
  1141. *
  1142. * @usage <?php um_reset_user_clean(); ?>
  1143. *
  1144. * @example You can reset user data by using the following line in your code
  1145. *
  1146. * <?php um_reset_user_clean(); ?>
  1147. */
  1148. function um_reset_user_clean() {
  1149. UM()->user()->reset( true );
  1150. }
  1151. /**
  1152. * Clears the user data. If a user is logged in, the user data will be reset to that user's data
  1153. *
  1154. * @function um_reset_user()
  1155. *
  1156. * @description This function resets the current user. You can use it to reset user data after
  1157. * retrieving the details of a specific user.
  1158. *
  1159. * @usage <?php um_reset_user(); ?>
  1160. *
  1161. * @example You can reset user data by using the following line in your code
  1162. *
  1163. * <?php um_reset_user(); ?>
  1164. */
  1165. function um_reset_user() {
  1166. UM()->user()->reset();
  1167. }
  1168. /**
  1169. * Gets the queried user
  1170. *
  1171. * @return mixed
  1172. */
  1173. function um_queried_user() {
  1174. return get_query_var( 'um_user' );
  1175. }
  1176. /**
  1177. * Sets the requested user
  1178. *
  1179. * @param $user_id
  1180. */
  1181. function um_set_requested_user( $user_id ) {
  1182. UM()->user()->target_id = $user_id;
  1183. }
  1184. /**
  1185. * Gets the requested user
  1186. *
  1187. * @return bool|null
  1188. */
  1189. function um_get_requested_user() {
  1190. if ( ! empty( UM()->user()->target_id ) ) {
  1191. return UM()->user()->target_id;
  1192. }
  1193. return false;
  1194. }
  1195. /**
  1196. * Remove edit profile args from url
  1197. *
  1198. * @param string $url
  1199. *
  1200. * @return mixed|string|void
  1201. */
  1202. function um_edit_my_profile_cancel_uri( $url = '' ) {
  1203. if ( empty( $url ) ) {
  1204. $url = remove_query_arg( 'um_action' );
  1205. $url = remove_query_arg( 'profiletab', $url );
  1206. $url = add_query_arg( 'profiletab', 'main', $url );
  1207. }
  1208. /**
  1209. * UM hook
  1210. *
  1211. * @type filter
  1212. * @title um_edit_profile_cancel_uri
  1213. * @description Change Edit Profile Cancel URL
  1214. * @input_vars
  1215. * [{"var":"$url","type":"string","desc":"Cancel URL"}]
  1216. * @change_log
  1217. * ["Since: 2.0"]
  1218. * @usage add_filter( 'um_edit_profile_cancel_uri', 'function_name', 10, 1 );
  1219. * @example
  1220. * <?php
  1221. * add_filter( 'um_edit_profile_cancel_uri', 'my_edit_profile_cancel_uri', 10, 1 );
  1222. * function my_edit_profile_cancel_uri( $url ) {
  1223. * // your code here
  1224. * return $url;
  1225. * }
  1226. * ?>
  1227. */
  1228. $url = apply_filters( 'um_edit_profile_cancel_uri', $url );
  1229. return $url;
  1230. }
  1231. /**
  1232. * boolean for profile edit page
  1233. *
  1234. * @return bool
  1235. */
  1236. function um_is_on_edit_profile() {
  1237. if ( isset( $_REQUEST['um_action'] ) && sanitize_key( $_REQUEST['um_action'] ) == 'edit' ) {
  1238. return true;
  1239. }
  1240. return false;
  1241. }
  1242. /**
  1243. * Can view field
  1244. *
  1245. * @param $data
  1246. *
  1247. * @return bool
  1248. */
  1249. function um_can_view_field( $data ) {
  1250. $can_view = true;
  1251. if ( ! isset( UM()->fields()->set_mode ) ) {
  1252. UM()->fields()->set_mode = '';
  1253. }
  1254. if ( isset( $data['public'] ) && UM()->fields()->set_mode != 'register' ) {
  1255. $can_edit = false;
  1256. $current_user_roles = [];
  1257. if ( is_user_logged_in() ) {
  1258. $can_edit = UM()->roles()->um_current_user_can( 'edit', um_user( 'ID' ) );
  1259. $previous_user = um_user( 'ID' );
  1260. um_fetch_user( get_current_user_id() );
  1261. $current_user_roles = um_user( 'roles' );
  1262. um_fetch_user( $previous_user );
  1263. }
  1264. switch ( $data['public'] ) {
  1265. case '1': // Everyone
  1266. break;
  1267. case '2': // Members
  1268. if ( ! is_user_logged_in() ) {
  1269. $can_view = false;
  1270. }
  1271. break;
  1272. case '-1': // Only visible to profile owner and users who can edit other member accounts
  1273. if ( ! is_user_logged_in() ) {
  1274. $can_view = false;
  1275. } elseif ( ! um_is_user_himself() && ! $can_edit ) {
  1276. $can_view = false;
  1277. }
  1278. break;
  1279. case '-2': // Only specific member roles
  1280. if ( ! is_user_logged_in() ) {
  1281. $can_view = false;
  1282. } elseif ( ! empty( $data['roles'] ) && count( array_intersect( $current_user_roles, $data['roles'] ) ) <= 0 ) {
  1283. $can_view = false;
  1284. }
  1285. break;
  1286. case '-3': // Only visible to profile owner and specific roles
  1287. if ( ! is_user_logged_in() ) {
  1288. $can_view = false;
  1289. } elseif ( ! um_is_user_himself() && ! empty( $data['roles'] ) && count( array_intersect( $current_user_roles, $data['roles'] ) ) <= 0 ) {
  1290. $can_view = false;
  1291. }
  1292. break;
  1293. default:
  1294. $can_view = apply_filters( 'um_can_view_field_custom', $can_view, $data );
  1295. break;
  1296. }
  1297. }
  1298. return apply_filters( 'um_can_view_field', $can_view, $data );
  1299. }
  1300. /**
  1301. * Checks if user can view profile
  1302. *
  1303. * @param $user_id
  1304. *
  1305. * @return bool
  1306. */
  1307. function um_can_view_profile( $user_id ) {
  1308. if ( ! is_user_logged_in() ) {
  1309. return ! UM()->user()->is_private_profile( $user_id );
  1310. }
  1311. $temp_id = um_user('ID');
  1312. um_fetch_user( get_current_user_id() );
  1313. if ( ! um_user( 'can_view_all' ) && $user_id != get_current_user_id() && is_user_logged_in() ) {
  1314. um_fetch_user( $temp_id );
  1315. return false;
  1316. }
  1317. if ( ! um_user( 'can_access_private_profile' ) && UM()->user()->is_private_profile( $user_id ) ) {
  1318. um_fetch_user( $temp_id );
  1319. return false;
  1320. }
  1321. if ( um_user( 'can_view_roles' ) && $user_id != get_current_user_id() ) {
  1322. $can_view_roles = um_user( 'can_view_roles' );
  1323. if ( ! is_array( $can_view_roles ) ) {
  1324. $can_view_roles = array();
  1325. }
  1326. if ( count( $can_view_roles ) && count( array_intersect( UM()->roles()->get_all_user_roles( $user_id ), $can_view_roles ) ) <= 0 ) {
  1327. um_fetch_user( $temp_id );
  1328. return false;
  1329. }
  1330. }
  1331. um_fetch_user( $temp_id );
  1332. return true;
  1333. }
  1334. /**
  1335. * boolean check for not same user
  1336. *
  1337. * @return bool
  1338. */
  1339. function um_is_user_himself() {
  1340. if (um_get_requested_user() && um_get_requested_user() != get_current_user_id())
  1341. return false;
  1342. return true;
  1343. }
  1344. /**
  1345. * Can edit field
  1346. *
  1347. * @param $data
  1348. *
  1349. * @return bool
  1350. */
  1351. function um_can_edit_field( $data ) {
  1352. $can_edit = true;
  1353. if ( ! empty( UM()->fields()->editing ) && isset( UM()->fields()->set_mode ) && UM()->fields()->set_mode == 'profile' ) {
  1354. if ( ! is_user_logged_in() ) {
  1355. $can_edit = false;
  1356. } else {
  1357. if ( ! UM()->roles()->um_user_can( 'can_edit_everyone' ) ) {
  1358. if ( isset( $data['editable'] ) && $data['editable'] == 0 ) {
  1359. $can_edit = false;
  1360. } else {
  1361. if ( ! um_is_user_himself() ) {
  1362. $can_edit = false;
  1363. }
  1364. }
  1365. }
  1366. }
  1367. }
  1368. return apply_filters( 'um_can_edit_field', $can_edit, $data );
  1369. }
  1370. /**
  1371. * Check if user is in his profile
  1372. *
  1373. * @return bool
  1374. */
  1375. function um_is_myprofile() {
  1376. if (get_current_user_id() && get_current_user_id() == um_get_requested_user()) return true;
  1377. if (!um_get_requested_user() && um_is_core_page( 'user' ) && get_current_user_id()) return true;
  1378. return false;
  1379. }
  1380. /**
  1381. * Returns the edit profile link
  1382. *
  1383. * @param int $user_id
  1384. *
  1385. * @return string
  1386. */
  1387. function um_edit_profile_url( $user_id = null ) {
  1388. if ( um_is_core_page( 'user' ) ) {
  1389. $url = UM()->permalinks()->get_current_url();
  1390. } else {
  1391. $url = isset( $user_id ) ? um_user_profile_url( $user_id ) : um_user_profile_url();
  1392. }
  1393. $url = remove_query_arg( 'profiletab', $url );
  1394. $url = remove_query_arg( 'subnav', $url );
  1395. $url = add_query_arg( 'um_action', 'edit', $url );
  1396. return $url;
  1397. }
  1398. /**
  1399. * Checks if user can edit his profile
  1400. *
  1401. * @return bool
  1402. */
  1403. function um_can_edit_my_profile() {
  1404. if ( ! is_user_logged_in() || ! um_user( 'can_edit_profile' ) ) {
  1405. return false;
  1406. }
  1407. return true;
  1408. }
  1409. /**
  1410. * Short for admin e-mail
  1411. *
  1412. * @return mixed|string|void
  1413. */
  1414. function um_admin_email() {
  1415. return UM()->options()->get( 'admin_email' );
  1416. }
  1417. /**
  1418. * Get admin e-mails
  1419. *
  1420. * @return array
  1421. */
  1422. function um_multi_admin_email() {
  1423. $emails = UM()->options()->get( 'admin_email' );
  1424. $emails_array = explode( ',', $emails );
  1425. if ( ! empty( $emails_array ) ) {
  1426. $emails_array = array_map( 'trim', $emails_array );
  1427. }
  1428. $emails_array = array_unique( $emails_array );
  1429. return $emails_array;
  1430. }
  1431. /**
  1432. * Display a link to profile page
  1433. *
  1434. * @param int|bool $user_id
  1435. *
  1436. * @return bool|string
  1437. */
  1438. function um_user_profile_url( $user_id = false ) {
  1439. if ( ! $user_id ) {
  1440. $user_id = um_user( 'ID' );
  1441. }
  1442. $url = UM()->user()->get_profile_link( $user_id );
  1443. if ( empty( $url ) ) {
  1444. //if empty profile slug - generate it and re-get profile URL
  1445. UM()->user()->generate_profile_slug( $user_id );
  1446. $url = UM()->user()->get_profile_link( $user_id );
  1447. }
  1448. return $url;
  1449. }
  1450. /**
  1451. * Get all UM roles in array
  1452. *
  1453. * @return array
  1454. */
  1455. function um_get_roles() {
  1456. return UM()->roles()->get_roles();
  1457. }
  1458. /**
  1459. * Sets a specific user and prepares profile data and user permissions and makes them accessible.
  1460. *
  1461. * @function um_fetch_user()
  1462. *
  1463. * @description This function sets a user and allow you to retrieve any information for the retrieved user
  1464. *
  1465. * @usage <?php um_fetch_user( $user_id ); ?>
  1466. *
  1467. * @param $user_id (numeric) (required) A user ID is required. This is the user's ID that you wish to set/retrieve
  1468. *
  1469. *
  1470. * @example The example below will set user ID 5 prior to retrieving his profile information.
  1471. *
  1472. * <?php
  1473. *
  1474. * um_fetch_user(5);
  1475. * echo um_user('display_name'); // returns the display name of user ID 5
  1476. *
  1477. * ?>
  1478. *
  1479. * @example In the following example you can fetch the profile of a logged-in user dynamically.
  1480. *
  1481. * <?php
  1482. *
  1483. * um_fetch_user( get_current_user_id() );
  1484. * echo um_user('display_name'); // returns the display name of logged-in user
  1485. *
  1486. * ?>
  1487. *
  1488. */
  1489. function um_fetch_user( $user_id ) {
  1490. UM()->user()->set( $user_id );
  1491. }
  1492. /**
  1493. * Load profile key
  1494. *
  1495. * @param $key
  1496. *
  1497. * @return bool|string
  1498. */
  1499. function um_profile( $key ) {
  1500. if ( ! empty( UM()->user()->profile[ $key ] ) ) {
  1501. /**
  1502. * UM hook
  1503. *
  1504. * @type filter
  1505. * @title um_profile_{$key}__filter
  1506. * @description Change not empty profile field value
  1507. * @input_vars
  1508. * [{"var":"$value","type":"mixed","desc":"Profile Value"}]
  1509. * @change_log
  1510. * ["Since: 2.0"]
  1511. * @usage add_filter( 'um_profile_{$key}__filter', 'function_name', 10, 1 );
  1512. * @example
  1513. * <?php
  1514. * add_filter( 'um_profile_{$key}__filter', 'my_profile_value', 10, 1 );
  1515. * function my_profile_value( $value ) {
  1516. * // your code here
  1517. * return $value;
  1518. * }
  1519. * ?>
  1520. */
  1521. $value = apply_filters( "um_profile_{$key}__filter", UM()->user()->profile[ $key ] );
  1522. } else {
  1523. /**
  1524. * UM hook
  1525. *
  1526. * @type filter
  1527. * @title um_profile_{$key}_empty__filter
  1528. * @description Change Profile field value if it's empty
  1529. * @input_vars
  1530. * [{"var":"$value","type":"mixed","desc":"Profile Value"}]
  1531. * @change_log
  1532. * ["Since: 2.0"]
  1533. * @usage add_filter( 'um_profile_{$key}_empty__filter', 'function_name', 10, 1 );
  1534. * @example
  1535. * <?php
  1536. * add_filter( 'um_profile_{$key}_empty__filter', 'my_profile_value', 10, 1 );
  1537. * function my_profile_value( $value ) {
  1538. * // your code here
  1539. * return $value;
  1540. * }
  1541. * ?>
  1542. */
  1543. $value = apply_filters( "um_profile_{$key}_empty__filter", false );
  1544. }
  1545. return $value;
  1546. }
  1547. /**
  1548. * Get youtube video ID from url
  1549. *
  1550. * @param $url
  1551. *
  1552. * @return bool
  1553. */
  1554. function um_youtube_id_from_url( $url ) {
  1555. $pattern =
  1556. '%^# Match any youtube URL
  1557. (?:https?://)? # Optional scheme. Either http or https
  1558. (?:www\.)? # Optional www subdomain
  1559. (?: # Group host alternatives
  1560. youtu\.be/ # Either youtu.be,
  1561. | youtube\.com # or youtube.com
  1562. (?: # Group path alternatives
  1563. /embed/ # Either /embed/
  1564. | /v/ # or /v/
  1565. | /watch\?v= # or /watch\?v=
  1566. ) # End path alternatives.
  1567. ) # End host alternatives.
  1568. ([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
  1569. $%x';
  1570. $result = preg_match( $pattern, $url, $matches );
  1571. if ( false !== $result && isset( $matches[1] ) ) {
  1572. return $matches[1];
  1573. }
  1574. return false;
  1575. }
  1576. /**
  1577. * Find closest number in an array
  1578. *
  1579. * @param $array
  1580. * @param $number
  1581. *
  1582. * @return mixed
  1583. */
  1584. function um_closest_num( $array, $number ) {
  1585. sort( $array );
  1586. foreach ( $array as $a ) {
  1587. if ( $a >= $number ) return $a;
  1588. }
  1589. return end( $array );
  1590. }
  1591. /**
  1592. * get cover uri
  1593. *
  1594. * @param $image
  1595. * @param $attrs
  1596. *
  1597. * @return bool|string
  1598. */
  1599. function um_get_cover_uri( $image, $attrs ) {
  1600. $uri = false;
  1601. $uri_common = false;
  1602. $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
  1603. $ratio = str_replace(':1','',UM()->options()->get( 'profile_cover_ratio' ) );
  1604. $height = round( $attrs / $ratio );
  1605. if ( is_multisite() ) {
  1606. //multisite fix for old customers
  1607. $multisite_fix_dir = UM()->uploader()->get_upload_base_dir();
  1608. $multisite_fix_url = UM()->uploader()->get_upload_base_url();
  1609. $multisite_fix_dir = str_replace( DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . get_current_blog_id() . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $multisite_fix_dir );
  1610. $multisite_fix_url = str_replace( '/sites/' . get_current_blog_id() . '/', '/', $multisite_fix_url );
  1611. if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo{$ext}" ) ) {
  1612. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/cover_photo{$ext}?" . current_time( 'timestamp' );
  1613. }
  1614. if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo-{$attrs}{$ext}" ) ) {
  1615. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/cover_photo-{$attrs}{$ext}?" . current_time( 'timestamp' );
  1616. }elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo-{$attrs}x{$height}{$ext}" ) ) {
  1617. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/cover_photo-{$attrs}x{$height}{$ext}?". current_time( 'timestamp' );
  1618. }
  1619. }
  1620. if ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo{$ext}" ) ) {
  1621. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/cover_photo{$ext}?" . current_time( 'timestamp' );
  1622. }
  1623. if ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo-{$attrs}{$ext}" ) ) {
  1624. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/cover_photo-{$attrs}{$ext}?" . current_time( 'timestamp' );
  1625. }elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo-{$attrs}x{$height}{$ext}" ) ) {
  1626. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/cover_photo-{$attrs}x{$height}{$ext}?". current_time( 'timestamp' );
  1627. }
  1628. if ( ! empty( $uri_common ) && empty( $uri ) ) {
  1629. $uri = $uri_common;
  1630. }
  1631. return $uri;
  1632. }
  1633. /**
  1634. * get avatar URL instead of image
  1635. *
  1636. * @param $get_avatar
  1637. *
  1638. * @return mixed
  1639. */
  1640. function um_get_avatar_url( $get_avatar ) {
  1641. preg_match( '/src="(.*?)"/i', $get_avatar, $matches );
  1642. return isset( $matches[1] ) ? $matches[1] : '';
  1643. }
  1644. /**
  1645. * get avatar uri
  1646. *
  1647. * @param $image
  1648. * @param string|array $attrs
  1649. *
  1650. * @return bool|string
  1651. */
  1652. function um_get_avatar_uri( $image, $attrs ) {
  1653. $uri = false;
  1654. $uri_common = false;
  1655. $find = false;
  1656. $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
  1657. if ( is_multisite() ) {
  1658. //multisite fix for old customers
  1659. $multisite_fix_dir = UM()->uploader()->get_upload_base_dir();
  1660. $multisite_fix_url = UM()->uploader()->get_upload_base_url();
  1661. $multisite_fix_dir = str_replace( DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . get_current_blog_id() . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $multisite_fix_dir );
  1662. $multisite_fix_url = str_replace( '/sites/' . get_current_blog_id() . '/', '/', $multisite_fix_url );
  1663. if ( $attrs == 'original' && file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
  1664. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}";
  1665. } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
  1666. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}x{$attrs}{$ext}";
  1667. } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}{$ext}" ) ) {
  1668. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}";
  1669. } else {
  1670. $sizes = UM()->options()->get( 'photo_thumb_sizes' );
  1671. if ( is_array( $sizes ) ) {
  1672. $find = um_closest_num( $sizes, $attrs );
  1673. }
  1674. if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}x{$find}{$ext}" ) ) {
  1675. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}x{$find}{$ext}";
  1676. } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}{$ext}" ) ) {
  1677. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}";
  1678. } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
  1679. $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}";
  1680. }
  1681. }
  1682. }
  1683. if ( $attrs == 'original' && file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
  1684. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo{$ext}";
  1685. } elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
  1686. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$attrs}x{$attrs}{$ext}";
  1687. } elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}{$ext}" ) ) {
  1688. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}";
  1689. } else {
  1690. $sizes = UM()->options()->get( 'photo_thumb_sizes' );
  1691. if ( is_array( $sizes ) ) {
  1692. $find = um_closest_num( $sizes, $attrs );
  1693. }
  1694. if ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}x{$find}{$ext}" ) ) {
  1695. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$find}x{$find}{$ext}";
  1696. } elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}{$ext}" ) ) {
  1697. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}";
  1698. } elseif ( file_exists( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) {
  1699. $uri = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/profile_photo{$ext}";
  1700. }
  1701. }
  1702. if ( ! empty( $uri_common ) && empty( $uri ) ) {
  1703. $uri = $uri_common;
  1704. }
  1705. /**
  1706. * UM hook
  1707. *
  1708. * @type filter
  1709. * @title um_filter_avatar_cache_time
  1710. * @description Change Profile field value if it's empty
  1711. * @input_vars
  1712. * [{"var":"$timestamp","type":"timestamp","desc":"Avatar cache time"},
  1713. * {"var":"$user_id","type":"int","desc":"User ID"}]
  1714. * @change_log
  1715. * ["Since: 2.0"]
  1716. * @usage add_filter( 'um_filter_avatar_cache_time', 'function_name', 10, 2 );
  1717. * @example
  1718. * <?php
  1719. * add_filter( 'um_filter_avatar_cache_time', 'my_avatar_cache_time', 10, 2 );
  1720. * function my_avatar_cache_time( $timestamp, $user_id ) {
  1721. * // your code here
  1722. * return $timestamp;
  1723. * }
  1724. * ?>
  1725. */
  1726. $cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) );
  1727. if ( ! empty( $cache_time ) ) {
  1728. $uri .= "?{$cache_time}";
  1729. }
  1730. return $uri;
  1731. }
  1732. /**
  1733. * Default avatar URL
  1734. *
  1735. * @return string
  1736. */
  1737. function um_get_default_avatar_uri() {
  1738. $uri = UM()->options()->get( 'default_avatar' );
  1739. $uri = !empty( $uri['url'] ) ? $uri['url'] : '';
  1740. if ( ! $uri ) {
  1741. $uri = um_url . 'assets/img/default_avatar.jpg';
  1742. }
  1743. return set_url_scheme( $uri );
  1744. }
  1745. /**
  1746. * get user avatar url
  1747. *
  1748. * @param $user_id
  1749. * @param $size
  1750. *
  1751. * @return bool|string
  1752. */
  1753. function um_get_user_avatar_data( $user_id = '', $size = '96' ) {
  1754. if( empty( $user_id ) ) {
  1755. $user_id = um_user( 'ID' );
  1756. } else {
  1757. um_fetch_user( $user_id );
  1758. }
  1759. $data = array(
  1760. 'user_id' => $user_id,
  1761. 'default' => um_get_default_avatar_uri(),
  1762. 'class' => 'gravatar avatar avatar-' . $size . ' um-avatar',
  1763. 'size' => $size
  1764. );
  1765. if ( $profile_photo = um_profile( 'profile_photo' ) ) {
  1766. $data['url'] = um_get_avatar_uri( $profile_photo, $size );
  1767. $data['type'] = 'upload';
  1768. $data['class'] .= ' um-avatar-uploaded';
  1769. } elseif ( $synced_profile_photo = um_user( 'synced_profile_photo' ) ) {
  1770. $data['url'] = $synced_profile_photo;
  1771. $data['type'] = 'sync';
  1772. $data['class'] .= ' um-avatar-default';
  1773. } elseif ( UM()->options()->get( 'use_gravatars' ) ) {
  1774. $avatar_hash_id = md5( um_user( 'user_email' ) );
  1775. $data['url'] = set_url_scheme( '//gravatar.com/avatar/' . $avatar_hash_id );
  1776. $data['url'] = add_query_arg( 's', 400, $data['url'] );
  1777. $rating = get_option( 'avatar_rating' );
  1778. if ( ! empty( $rating ) ) {
  1779. $data['url'] = add_query_arg( 'r', $rating, $data['url'] );
  1780. }
  1781. $gravatar_type = UM()->options()->get( 'use_um_gravatar_default_builtin_image' );
  1782. if ( $gravatar_type == 'default' ) {
  1783. if ( UM()->options()->get( 'use_um_gravatar_default_image' ) ) {
  1784. $data['url'] = add_query_arg( 'd', $data['default'], $data['url'] );
  1785. } else {
  1786. $default = get_option( 'avatar_default', 'mystery' );
  1787. if ( $default == 'gravatar_default' ) {
  1788. $default = '';
  1789. }
  1790. $data['url'] = add_query_arg( 'd', $default, $data['url'] );
  1791. }
  1792. } else {
  1793. $data['url'] = add_query_arg( 'd', $gravatar_type, $data['url'] );
  1794. }
  1795. $data['type'] = 'gravatar';
  1796. $data['class'] .= ' um-avatar-gravatar';
  1797. } else {
  1798. $data['url'] = $data['default'];
  1799. $data['type'] = 'default';
  1800. $data['class'] .= ' um-avatar-default';
  1801. }
  1802. /**
  1803. * UM hook
  1804. *
  1805. * @type filter
  1806. * @title um_user_avatar_url_filter
  1807. * @description Change user avatar URL
  1808. * @input_vars
  1809. * [{"var":"$avatar_uri","type":"string","desc":"Avatar URL"},
  1810. * {"var":"$user_id","type":"int","desc":"User ID"}]
  1811. * @change_log
  1812. * ["Since: 2.0"]
  1813. * @usage add_filter( 'um_user_avatar_url_filter', 'function_name', 10, 2 );
  1814. * @example
  1815. * <?php
  1816. * add_filter( 'um_user_avatar_url_filter', 'my_user_avatar_url', 10, 2 );
  1817. * function my_user_avatar_url( $avatar_uri ) {
  1818. * // your code here
  1819. * return $avatar_uri;
  1820. * }
  1821. * ?>
  1822. */
  1823. $data['url'] = apply_filters( 'um_user_avatar_url_filter', $data['url'], $user_id, $data );
  1824. /**
  1825. * UM hook
  1826. *
  1827. * @type filter
  1828. * @title um_avatar_image_alternate_text
  1829. * @description Change user display name on um_user function profile photo
  1830. * @input_vars
  1831. * [{"var":"$display_name","type":"string","desc":"User Display Name"}]
  1832. * @change_log
  1833. * ["Since: 2.0"]
  1834. * @usage add_filter( 'um_avatar_image_alternate_text', 'function_name', 10, 1 );
  1835. * @example
  1836. * <?php
  1837. * add_filter( 'um_avatar_image_alternate_text', 'my_avatar_image_alternate_text', 10, 1 );
  1838. * function my_avatar_image_alternate_text( $display_name ) {
  1839. * // your code here
  1840. * return $display_name;
  1841. * }
  1842. * ?>
  1843. */
  1844. $data['alt'] = apply_filters( "um_avatar_image_alternate_text", um_user( "display_name" ), $data );
  1845. return $data;
  1846. }
  1847. /**
  1848. * get user avatar url
  1849. *
  1850. * @param $user_id
  1851. * @param $size
  1852. *
  1853. * @return bool|string
  1854. */
  1855. function um_get_user_avatar_url( $user_id = '', $size = '96' ) {
  1856. $data = um_get_user_avatar_data( $user_id, $size );
  1857. return $data['url'];
  1858. }
  1859. /**
  1860. * default cover
  1861. *
  1862. * @return mixed|string|void
  1863. */
  1864. function um_get_default_cover_uri() {
  1865. $uri = UM()->options()->get( 'default_cover' );
  1866. $uri = ! empty( $uri['url'] ) ? $uri['url'] : '';
  1867. if ( $uri ) {
  1868. /**
  1869. * UM hook
  1870. *
  1871. * @type filter
  1872. * @title um_get_default_cover_uri_filter
  1873. * @description Change Default Cover URL
  1874. * @input_vars
  1875. * [{"var":"$uri","type":"string","desc":"Default Cover URL"}]
  1876. * @change_log
  1877. * ["Since: 2.0"]
  1878. * @usage add_filter( 'um_get_default_cover_uri_filter', 'function_name', 10, 1 );
  1879. * @example
  1880. * <?php
  1881. * add_filter( 'um_get_default_cover_uri_filter', 'my_default_cover_uri', 10, 1 );
  1882. * function my_default_cover_uri( $uri ) {
  1883. * // your code here
  1884. * return $uri;
  1885. * }
  1886. * ?>
  1887. */
  1888. return apply_filters( 'um_get_default_cover_uri_filter', $uri );
  1889. }
  1890. return '';
  1891. }
  1892. /**
  1893. * @param $data
  1894. * @param null $attrs
  1895. *
  1896. * @return string|array
  1897. */
  1898. function um_user( $data, $attrs = null ) {
  1899. switch ($data) {
  1900. default:
  1901. $value = um_profile( $data );
  1902. $value = maybe_unserialize( $value );
  1903. if ( in_array( $data, array( 'role', 'gender' ) ) ) {
  1904. if ( is_array( $value ) ) {
  1905. $value = implode( ",", $value );
  1906. }
  1907. return $value;
  1908. }
  1909. return $value;
  1910. break;
  1911. case 'user_email':
  1912. $user_email_in_meta = get_user_meta( um_user( 'ID' ), 'user_email', true );
  1913. if ( $user_email_in_meta ) {
  1914. delete_user_meta( um_user( 'ID' ), 'user_email' );
  1915. }
  1916. $value = um_profile( $data );
  1917. return $value;
  1918. break;
  1919. case 'user_login':
  1920. $user_login_in_meta = get_user_meta( um_user( 'ID' ), 'user_login', true );
  1921. if ( $user_login_in_meta ) {
  1922. delete_user_meta( um_user( 'ID' ), 'user_login' );
  1923. }
  1924. $value = um_profile( $data );
  1925. return $value;
  1926. break;
  1927. case 'first_name':
  1928. case 'last_name':
  1929. $name = um_profile( $data );
  1930. if ( UM()->options()->get( 'force_display_name_capitlized' ) ) {
  1931. $name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) );
  1932. }
  1933. /**
  1934. * UM hook
  1935. *
  1936. * @type filter
  1937. * @title um_user_{$data}_case
  1938. * @description Change user name on um_user function
  1939. * @input_vars
  1940. * [{"var":"$name","type":"string","desc":"User Name"}]
  1941. * @change_log
  1942. * ["Since: 2.0"]
  1943. * @usage add_filter( 'um_user_{$data}_case', 'function_name', 10, 1 );
  1944. * @example
  1945. * <?php
  1946. * add_filter( 'um_user_{$data}_case', 'my_user_case', 10, 1 );
  1947. * function my_user_case( $name ) {
  1948. * // your code here
  1949. * return $name;
  1950. * }
  1951. * ?>
  1952. */
  1953. $name = apply_filters( "um_user_{$data}_case", $name );
  1954. return $name;
  1955. break;
  1956. case 'full_name':
  1957. if ( um_user( 'first_name' ) && um_user( 'last_name' ) ) {
  1958. $full_name = um_user( 'first_name' ) . ' ' . um_user( 'last_name' );
  1959. } else {
  1960. $full_name = um_user( 'display_name' );
  1961. }
  1962. $full_name = UM()->validation()->safe_name_in_url( $full_name );
  1963. // update full_name changed
  1964. if ( um_profile( $data ) !== $full_name ) {
  1965. update_user_meta( um_user( 'ID' ), 'full_name', $full_name );
  1966. }
  1967. return $full_name;
  1968. break;
  1969. case 'first_and_last_name_initial':
  1970. $f_and_l_initial = '';
  1971. if ( um_user( 'first_name' ) && um_user( 'last_name' ) ) {
  1972. $initial = um_user( 'last_name' );
  1973. $f_and_l_initial = um_user( 'first_name' ) . ' ' . $initial[0];
  1974. } else {
  1975. $f_and_l_initial = um_profile( $data );
  1976. }
  1977. $f_and_l_initial = UM()->validation()->safe_name_in_url( $f_and_l_initial );
  1978. if ( UM()->options()->get( 'force_display_name_capitlized' ) ) {
  1979. $name = implode( '-', array_map( 'ucfirst', explode( '-', $f_and_l_initial ) ) );
  1980. } else {
  1981. $name = $f_and_l_initial;
  1982. }
  1983. return $name;
  1984. break;
  1985. case 'display_name':
  1986. $op = UM()->options()->get( 'display_name' );
  1987. $name = '';
  1988. if ( $op == 'default' ) {
  1989. $name = um_profile( 'display_name' );
  1990. }
  1991. if ( $op == 'nickname' ) {
  1992. $name = um_profile( 'nickname' );
  1993. }
  1994. if ( $op == 'full_name' ) {
  1995. if ( um_user( 'first_name' ) && um_user( 'last_name' ) ) {
  1996. $name = um_user( 'first_name' ) . ' ' . um_user( 'last_name' );
  1997. } else {
  1998. $name = um_profile( $data );
  1999. }
  2000. if ( ! $name ) {
  2001. $name = um_user( 'user_login' );
  2002. }
  2003. }
  2004. if ( $op == 'sur_name' ) {
  2005. if ( um_user( 'first_name' ) && um_user( 'last_name' ) ) {
  2006. $name = um_user( 'last_name' ) . ' ' . um_user( 'first_name' );
  2007. } else {
  2008. $name = um_profile( $data );
  2009. }
  2010. }
  2011. if ( $op == 'first_name' ) {
  2012. if ( um_user( 'first_name' ) ) {
  2013. $name = um_user( 'first_name' );
  2014. } else {
  2015. $name = um_profile( $data );
  2016. }
  2017. }
  2018. if ( $op == 'username' ) {
  2019. $name = um_user( 'user_login' );
  2020. }
  2021. if ( $op == 'initial_name' ) {
  2022. if (um_user( 'first_name' ) && um_user( 'last_name' )) {
  2023. $initial = um_user( 'last_name' );
  2024. $name = um_user( 'first_name' ) . ' ' . $initial[0];
  2025. } else {
  2026. $name = um_profile( $data );
  2027. }
  2028. }
  2029. if ( $op == 'initial_name_f' ) {
  2030. if ( um_user( 'first_name' ) && um_user( 'last_name' ) ) {
  2031. $initial = um_user( 'first_name' );
  2032. $name = $initial[0] . ' ' . um_user( 'last_name' );
  2033. } else {
  2034. $name = um_profile( $data );
  2035. }
  2036. }
  2037. if ( $op == 'field' && UM()->options()->get( 'display_name_field' ) != '' ) {
  2038. $fields = array_filter( preg_split( '/[,\s]+/', UM()->options()->get( 'display_name_field' ) ) );
  2039. $name = '';
  2040. foreach ( $fields as $field ) {
  2041. if ( um_profile( $field ) ) {
  2042. $field_value = maybe_unserialize( um_profile( $field ) );
  2043. $field_value = is_array( $field_value ) ? implode( ',', $field_value ) : $field_value;
  2044. $name .= $field_value . ' ';
  2045. } elseif ( um_user( $field ) && $field != 'display_name' && $field != 'full_name' ) {
  2046. $name .= um_user( $field ) . ' ';
  2047. }
  2048. }
  2049. }
  2050. if ( UM()->options()->get( 'force_display_name_capitlized' ) ) {
  2051. $name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) );
  2052. }
  2053. /**
  2054. * UM hook
  2055. *
  2056. * @type filter
  2057. * @title um_user_display_name_filter
  2058. * @description Change user display name on um_user function
  2059. * @input_vars
  2060. * [{"var":"$name","type":"string","desc":"User Name"},
  2061. * {"var":"$user_id","type":"int","desc":"User ID"},
  2062. * {"var":"$html","type":"bool","desc":"Is HTML"}]
  2063. * @change_log
  2064. * ["Since: 2.0"]
  2065. * @usage add_filter( 'um_user_display_name_filter', 'function_name', 10, 3 );
  2066. * @example
  2067. * <?php
  2068. * add_filter( 'um_user_display_name_filter', 'my_user_display_name', 10, 3 );
  2069. * function my_user_display_name( $name, $user_id, $html ) {
  2070. * // your code here
  2071. * return $name;
  2072. * }
  2073. * ?>
  2074. */
  2075. return apply_filters( 'um_user_display_name_filter', $name, um_user( 'ID' ), ( $attrs == 'html' ) ? 1 : 0 );
  2076. break;
  2077. case 'role_select':
  2078. case 'role_radio':
  2079. return UM()->roles()->get_role_name( UM()->roles()->get_editable_priority_user_role( um_user( 'ID' ) ) );
  2080. break;
  2081. case 'submitted':
  2082. $array = um_profile( $data );
  2083. if ( empty( $array ) ) {
  2084. return '';
  2085. }
  2086. $array = maybe_unserialize( $array );
  2087. return $array;
  2088. break;
  2089. case 'password_reset_link':
  2090. return UM()->password()->reset_url();
  2091. break;
  2092. case 'account_activation_link':
  2093. return UM()->permalinks()->activate_url();
  2094. break;
  2095. case 'profile_photo':
  2096. $data = um_get_user_avatar_data( um_user( 'ID' ), $attrs );
  2097. return sprintf( '<img src="%s" class="%s" width="%s" height="%s" alt="%s" data-default="%s" onerror="%s" />',
  2098. esc_attr( $data['url'] ),
  2099. esc_attr( $data['class'] ),
  2100. esc_attr( $data['size'] ),
  2101. esc_attr( $data['size'] ),
  2102. esc_attr( $data['alt'] ),
  2103. esc_attr( $data['default'] ),
  2104. 'if ( ! this.getAttribute(\'data-load-error\') ){ this.setAttribute(\'data-load-error\', \'1\');this.setAttribute(\'src\', this.getAttribute(\'data-default\'));}'
  2105. );
  2106. break;
  2107. case 'cover_photo':
  2108. $is_default = false;
  2109. if ( um_profile( 'cover_photo' ) ) {
  2110. $cover_uri = um_get_cover_uri( um_profile( 'cover_photo' ), $attrs );
  2111. } elseif ( um_profile( 'synced_cover_photo' ) ) {
  2112. $cover_uri = um_profile( 'synced_cover_photo' );
  2113. } else {
  2114. $cover_uri = um_get_default_cover_uri();
  2115. $is_default = true;
  2116. }
  2117. /**
  2118. * UM hook
  2119. *
  2120. * @type filter
  2121. * @title um_user_cover_photo_uri__filter
  2122. * @description Change user avatar URL
  2123. * @input_vars
  2124. * [{"var":"$cover_uri","type":"string","desc":"Cover URL"},
  2125. * {"var":"$is_default","type":"bool","desc":"Default or not"},
  2126. * {"var":"$attrs","type":"array","desc":"Attributes"}]
  2127. * @change_log
  2128. * ["Since: 2.0"]
  2129. * @usage add_filter( 'um_user_cover_photo_uri__filter', 'function_name', 10, 3 );
  2130. * @example
  2131. * <?php
  2132. * add_filter( 'um_user_cover_photo_uri__filter', 'my_user_cover_photo_uri', 10, 3 );
  2133. * function my_user_cover_photo_uri( $cover_uri, $is_default, $attrs ) {
  2134. * // your code here
  2135. * return $cover_uri;
  2136. * }
  2137. * ?>
  2138. */
  2139. $cover_uri = apply_filters( 'um_user_cover_photo_uri__filter', $cover_uri, $is_default, $attrs );
  2140. $alt = um_profile( 'nickname' );
  2141. $cover_html = $cover_uri ? '<img src="' . esc_attr( $cover_uri ) . '" alt="' . esc_attr( $alt ) . '" />' : '';
  2142. $cover_html = apply_filters( 'um_user_cover_photo_html__filter', $cover_html, $cover_uri, $alt, $is_default, $attrs );
  2143. return $cover_html;
  2144. break;
  2145. case 'user_url':
  2146. $value = um_profile( $data );
  2147. return $value;
  2148. break;
  2149. }
  2150. }
  2151. /**
  2152. * Get server protocol
  2153. *
  2154. * @return string
  2155. */
  2156. function um_get_domain_protocol() {
  2157. if (is_ssl()) {
  2158. $protocol = 'https://';
  2159. } else {
  2160. $protocol = 'http://';
  2161. }
  2162. return $protocol;
  2163. }
  2164. /**
  2165. * Set SSL to media URI
  2166. *
  2167. * @param string $url
  2168. *
  2169. * @return string
  2170. */
  2171. function um_secure_media_uri( $url ) {
  2172. if (is_ssl()) {
  2173. $url = str_replace( 'http:', 'https:', $url );
  2174. }
  2175. return $url;
  2176. }
  2177. /**
  2178. * Force strings to UTF-8 encoded
  2179. *
  2180. * @param mixed $value
  2181. *
  2182. * @return mixed
  2183. */
  2184. function um_force_utf8_string( $value ) {
  2185. if ( is_array( $value ) ) {
  2186. $arr_value = array();
  2187. foreach ( $value as $key => $v ) {
  2188. $utf8_decoded_value = utf8_decode( $v );
  2189. if ( mb_check_encoding( $utf8_decoded_value, 'UTF-8' ) ) {
  2190. array_push( $arr_value, $utf8_decoded_value );
  2191. } else {
  2192. array_push( $arr_value, $v );
  2193. }
  2194. }
  2195. return $arr_value;
  2196. } else {
  2197. $utf8_decoded_value = utf8_decode( $value );
  2198. if (mb_check_encoding( $utf8_decoded_value, 'UTF-8' )) {
  2199. return $utf8_decoded_value;
  2200. }
  2201. }
  2202. return $value;
  2203. }
  2204. /**
  2205. * Get user host
  2206. *
  2207. * Returns the webhost this site is using if possible
  2208. *
  2209. * @since 1.3.68
  2210. * @return mixed string $host if detected, false otherwise
  2211. */
  2212. function um_get_host() {
  2213. $host = false;
  2214. if (defined( 'WPE_APIKEY' )) {
  2215. $host = 'WP Engine';
  2216. } else if (defined( 'PAGELYBIN' )) {
  2217. $host = 'Pagely';
  2218. } else if (DB_HOST == 'localhost:/tmp/mysql5.sock') {
  2219. $host = 'ICDSoft';
  2220. } else if (DB_HOST == 'mysqlv5') {
  2221. $host = 'NetworkSolutions';
  2222. } else if (strpos( DB_HOST, 'ipagemysql.com' ) !== false) {
  2223. $host = 'iPage';
  2224. } else if (strpos( DB_HOST, 'ipowermysql.com' ) !== false) {
  2225. $host = 'IPower';
  2226. } else if (strpos( DB_HOST, '.gridserver.com' ) !== false) {
  2227. $host = 'MediaTemple Grid';
  2228. } else if (strpos( DB_HOST, '.pair.com' ) !== false) {
  2229. $host = 'pair Networks';
  2230. } else if (strpos( DB_HOST, '.stabletransit.com' ) !== false) {
  2231. $host = 'Rackspace Cloud';
  2232. } else if (strpos( DB_HOST, '.sysfix.eu' ) !== false) {
  2233. $host = 'SysFix.eu Power Hosting';
  2234. } else if (strpos( $_SERVER['SERVER_NAME'], 'Flywheel' ) !== false) {
  2235. $host = 'Flywheel';
  2236. } else {
  2237. // Adding a general fallback for data gathering
  2238. $host = 'DBH: ' . DB_HOST . ', SRV: ' . $_SERVER['SERVER_NAME'];
  2239. }
  2240. return $host;
  2241. }
  2242. /**
  2243. * Let To Num
  2244. *
  2245. * Does Size Conversions
  2246. *
  2247. * @since 1.3.68
  2248. * @author Chris Christoff
  2249. *
  2250. * @param string $v
  2251. *
  2252. * @return int|string
  2253. */
  2254. function um_let_to_num( $v ) {
  2255. $l = substr( $v, -1 );
  2256. $ret = substr( $v, 0, -1 );
  2257. switch (strtoupper( $l )) {
  2258. case 'P': // fall-through
  2259. case 'T': // fall-through
  2260. case 'G': // fall-through
  2261. case 'M': // fall-through
  2262. case 'K': // fall-through
  2263. $ret *= 1024;
  2264. break;
  2265. default:
  2266. break;
  2267. }
  2268. return $ret;
  2269. }
  2270. /**
  2271. * Check if we are on UM page
  2272. *
  2273. * @return bool
  2274. */
  2275. function is_ultimatemember() {
  2276. global $post;
  2277. if ( isset( $post->ID ) && in_array( $post->ID, UM()->config()->permalinks ) )
  2278. return true;
  2279. return false;
  2280. }
  2281. /**
  2282. * Maybe set empty time limit
  2283. */
  2284. function um_maybe_unset_time_limit() {
  2285. @set_time_limit( 0 );
  2286. }
  2287. /*
  2288. * Check if current user is owner of requested profile
  2289. * @Returns Boolean
  2290. */
  2291. if ( ! function_exists( 'um_is_profile_owner' ) ) {
  2292. /**
  2293. * @param $user_id
  2294. *
  2295. * @return bool
  2296. */
  2297. function um_is_profile_owner( $user_id = false ) {
  2298. if ( ! is_user_logged_in() ) {
  2299. return false;
  2300. }
  2301. if ( empty( $user_id ) ) {
  2302. $user_id = get_current_user_id();
  2303. }
  2304. return ( $user_id == um_profile_id() );
  2305. }
  2306. }
  2307. /**
  2308. * Check whether the current page is in AMP mode or not.
  2309. * We need to check for specific functions, as there is no special AMP header.
  2310. *
  2311. * @since 2.1.11
  2312. *
  2313. * @param bool $check_theme_support Whether theme support should be checked. Defaults to true.
  2314. *
  2315. * @uses is_amp_endpoint() AMP by Automattic
  2316. * @uses is_better_amp() Better AMP
  2317. *
  2318. * @return bool
  2319. */
  2320. function um_is_amp( $check_theme_support = true ) {
  2321. $is_amp = false;
  2322. if ( ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ||
  2323. ( function_exists( 'is_better_amp' ) && is_better_amp() ) ) {
  2324. $is_amp = true;
  2325. }
  2326. if ( $is_amp && $check_theme_support ) {
  2327. $is_amp = current_theme_supports( 'amp' );
  2328. }
  2329. return apply_filters( 'um_is_amp', $is_amp );
  2330. }