class.swpm-utils-misc.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. <?php
  2. class SwpmMiscUtils {
  3. public static $stripe_sca_frontend_scripts_printed = false;
  4. public static function create_mandatory_wp_pages() {
  5. $settings = SwpmSettings::get_instance();
  6. //Create join us page
  7. $swpm_join_page_content = '<p style="color:red;font-weight:bold;">This page and the content has been automatically generated for you to give you a basic idea of how a "Join Us" page should look like. You can customize this page however you like it by editing this page from your WordPress page editor.</p>';
  8. $swpm_join_page_content .= '<p style="font-weight:bold;">If you end up changing the URL of this page then make sure to update the URL value in the settings menu of the plugin.</p>';
  9. $swpm_join_page_content .= '<p style="border-top:1px solid #ccc;padding-top:10px;margin-top:10px;"></p>
  10. <strong>Free Membership</strong>
  11. <br />
  12. You get unlimited access to free membership content
  13. <br />
  14. <em><strong>Price: Free!</strong></em>
  15. <br /><br />Link the following image to go to the Registration Page if you want your visitors to be able to create a free membership account<br /><br />
  16. <img title="Join Now" src="' . SIMPLE_WP_MEMBERSHIP_URL . '/images/join-now-button-image.gif" alt="Join Now Button" width="277" height="82" />
  17. <p style="border-bottom:1px solid #ccc;padding-bottom:10px;margin-bottom:10px;"></p>';
  18. $swpm_join_page_content .= '<p><strong>You can register for a Free Membership or pay for one of the following membership options</strong></p>';
  19. $swpm_join_page_content .= '<p style="border-top:1px solid #ccc;padding-top:10px;margin-top:10px;"></p>
  20. [ ==> Insert Payment Button For Your Paid Membership Levels Here <== ]
  21. <p style="border-bottom:1px solid #ccc;padding-bottom:10px;margin-bottom:10px;"></p>';
  22. $swpm_join_page = array(
  23. 'post_title' => 'Join Us',
  24. 'post_name' => 'membership-join',
  25. 'post_content' => $swpm_join_page_content,
  26. 'post_parent' => 0,
  27. 'post_status' => 'publish',
  28. 'post_type' => 'page',
  29. 'comment_status' => 'closed',
  30. 'ping_status' => 'closed',
  31. );
  32. $join_page_obj = get_page_by_path( 'membership-join' );
  33. if ( ! $join_page_obj ) {
  34. $join_page_id = wp_insert_post( $swpm_join_page );
  35. } else {
  36. $join_page_id = $join_page_obj->ID;
  37. if ( $join_page_obj->post_status == 'trash' ) { //For cases where page may be in trash, bring it out of trash
  38. wp_update_post(
  39. array(
  40. 'ID' => $join_page_obj->ID,
  41. 'post_status' => 'publish',
  42. )
  43. );
  44. }
  45. }
  46. $swpm_join_page_permalink = get_permalink( $join_page_id );
  47. $settings->set_value( 'join-us-page-url', $swpm_join_page_permalink );
  48. //Create registration page
  49. $swpm_rego_page = array(
  50. 'post_title' => SwpmUtils::_( 'Registration' ),
  51. 'post_name' => 'membership-registration',
  52. 'post_content' => '[swpm_registration_form]',
  53. 'post_parent' => $join_page_id,
  54. 'post_status' => 'publish',
  55. 'post_type' => 'page',
  56. 'comment_status' => 'closed',
  57. 'ping_status' => 'closed',
  58. );
  59. $rego_page_obj = get_page_by_path( 'membership-registration' );
  60. if ( ! $rego_page_obj ) {
  61. $rego_page_id = wp_insert_post( $swpm_rego_page );
  62. } else {
  63. $rego_page_id = $rego_page_obj->ID;
  64. if ( $rego_page_obj->post_status == 'trash' ) { //For cases where page may be in trash, bring it out of trash
  65. wp_update_post(
  66. array(
  67. 'ID' => $rego_page_obj->ID,
  68. 'post_status' => 'publish',
  69. )
  70. );
  71. }
  72. }
  73. $swpm_rego_page_permalink = get_permalink( $rego_page_id );
  74. $settings->set_value( 'registration-page-url', $swpm_rego_page_permalink );
  75. //Create login page
  76. $swpm_login_page = array(
  77. 'post_title' => SwpmUtils::_( 'Member Login' ),
  78. 'post_name' => 'membership-login',
  79. 'post_content' => '[swpm_login_form]',
  80. 'post_parent' => 0,
  81. 'post_status' => 'publish',
  82. 'post_type' => 'page',
  83. 'comment_status' => 'closed',
  84. 'ping_status' => 'closed',
  85. );
  86. $login_page_obj = get_page_by_path( 'membership-login' );
  87. if ( ! $login_page_obj ) {
  88. $login_page_id = wp_insert_post( $swpm_login_page );
  89. } else {
  90. $login_page_id = $login_page_obj->ID;
  91. if ( $login_page_obj->post_status == 'trash' ) { //For cases where page may be in trash, bring it out of trash
  92. wp_update_post(
  93. array(
  94. 'ID' => $login_page_obj->ID,
  95. 'post_status' => 'publish',
  96. )
  97. );
  98. }
  99. }
  100. $swpm_login_page_permalink = get_permalink( $login_page_id );
  101. $settings->set_value( 'login-page-url', $swpm_login_page_permalink );
  102. //Create profile page
  103. $swpm_profile_page = array(
  104. 'post_title' => SwpmUtils::_( 'Profile' ),
  105. 'post_name' => 'membership-profile',
  106. 'post_content' => '[swpm_profile_form]',
  107. 'post_parent' => $login_page_id,
  108. 'post_status' => 'publish',
  109. 'post_type' => 'page',
  110. 'comment_status' => 'closed',
  111. 'ping_status' => 'closed',
  112. );
  113. $profile_page_obj = get_page_by_path( 'membership-profile' );
  114. if ( ! $profile_page_obj ) {
  115. $profile_page_id = wp_insert_post( $swpm_profile_page );
  116. } else {
  117. $profile_page_id = $profile_page_obj->ID;
  118. if ( $profile_page_obj->post_status == 'trash' ) { //For cases where page may be in trash, bring it out of trash
  119. wp_update_post(
  120. array(
  121. 'ID' => $profile_page_obj->ID,
  122. 'post_status' => 'publish',
  123. )
  124. );
  125. }
  126. }
  127. $swpm_profile_page_permalink = get_permalink( $profile_page_id );
  128. $settings->set_value( 'profile-page-url', $swpm_profile_page_permalink );
  129. //Create reset page
  130. $swpm_reset_page = array(
  131. 'post_title' => SwpmUtils::_( 'Password Reset' ),
  132. 'post_name' => 'password-reset',
  133. 'post_content' => '[swpm_reset_form]',
  134. 'post_parent' => $login_page_id,
  135. 'post_status' => 'publish',
  136. 'post_type' => 'page',
  137. 'comment_status' => 'closed',
  138. 'ping_status' => 'closed',
  139. );
  140. $reset_page_obj = get_page_by_path( 'password-reset' );
  141. if ( ! $profile_page_obj ) {
  142. $reset_page_id = wp_insert_post( $swpm_reset_page );
  143. } else {
  144. $reset_page_id = $reset_page_obj->ID;
  145. if ( $reset_page_obj->post_status == 'trash' ) { //For cases where page may be in trash, bring it out of trash
  146. wp_update_post(
  147. array(
  148. 'ID' => $reset_page_obj->ID,
  149. 'post_status' => 'publish',
  150. )
  151. );
  152. }
  153. }
  154. $swpm_reset_page_permalink = get_permalink( $reset_page_id );
  155. $settings->set_value( 'reset-page-url', $swpm_reset_page_permalink );
  156. $settings->save(); //Save all settings object changes
  157. }
  158. public static function redirect_to_url( $url ) {
  159. if ( empty( $url ) ) {
  160. return;
  161. }
  162. $url = apply_filters( 'swpm_redirect_to_url', $url );
  163. if ( ! preg_match( '/http/', $url ) ) {//URL value is incorrect
  164. echo '<p>Error! The URL value you entered in the plugin configuration is incorrect.</p>';
  165. echo '<p>A URL must always have the "http" keyword in it.</p>';
  166. echo '<p style="font-weight: bold;">The URL value you currently configured is: <br />' . $url . '</p>';
  167. echo '<p>Here are some examples of correctly formatted URL values for your reference: <br />http://www.example.com<br/>http://example.com<br />https://www.example.com</p>';
  168. echo '<p>Find the field where you entered this incorrect URL value and correct the mistake then try again.</p>';
  169. exit;
  170. }
  171. if ( ! headers_sent() ) {
  172. header( 'Location: ' . $url );
  173. } else {
  174. echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
  175. }
  176. exit;
  177. }
  178. public static function show_temporary_message_then_redirect( $msg, $redirect_url, $timeout = 5 ) {
  179. $timeout = absint( $timeout );
  180. $redirect_html = sprintf( '<meta http-equiv="refresh" content="%d; url=\'%s\'" />', $timeout, $redirect_url );
  181. $redir_msg = SwpmUtils::_( 'You will be automatically redirected in a few seconds. If not, please %s.' );
  182. $redir_msg = sprintf( $redir_msg, '<a href="' . $redirect_url . '">' . SwpmUtils::_( 'click here' ) . '</a>' );
  183. $msg = $msg . '<br/><br/>' . $redir_msg . $redirect_html;
  184. $title = SwpmUtils::_( 'Action Status' );
  185. wp_die( $msg, $title );
  186. }
  187. public static function get_current_page_url() {
  188. $pageURL = 'http';
  189. if ( isset( $_SERVER['SCRIPT_URI'] ) && ! empty( $_SERVER['SCRIPT_URI'] ) ) {
  190. $pageURL = $_SERVER['SCRIPT_URI'];
  191. $pageURL = str_replace(':443', '', $pageURL);//remove any port number from the URL value (some hosts include the port number with this).
  192. $pageURL = apply_filters( 'swpm_get_current_page_url_filter', $pageURL );
  193. return $pageURL;
  194. }
  195. if ( isset( $_SERVER['HTTPS'] ) && ( $_SERVER['HTTPS'] == 'on' ) ) {
  196. $pageURL .= 's';
  197. }
  198. $pageURL .= '://';
  199. if ( isset( $_SERVER['SERVER_PORT'] ) && ( $_SERVER['SERVER_PORT'] != '80' ) && ( $_SERVER['SERVER_PORT'] != '443' ) ) {
  200. $pageURL .= ltrim( $_SERVER['SERVER_NAME'], '.*' ) . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  201. } else {
  202. $pageURL .= ltrim( $_SERVER['SERVER_NAME'], '.*' ) . $_SERVER['REQUEST_URI'];
  203. }
  204. $pageURL = apply_filters( 'swpm_get_current_page_url_filter', $pageURL );
  205. return $pageURL;
  206. }
  207. /*
  208. * This is an alternative to the get_current_page_url() function. It needs to be tested on many different server conditions before it can be utilized
  209. */
  210. public static function get_current_page_url_alt() {
  211. $url_parts = array();
  212. $url_parts['proto'] = 'http';
  213. if ( isset( $_SERVER['SCRIPT_URI'] ) && ! empty( $_SERVER['SCRIPT_URI'] ) ) {
  214. return $_SERVER['SCRIPT_URI'];
  215. }
  216. if ( isset( $_SERVER['HTTPS'] ) && ( $_SERVER['HTTPS'] == 'on' ) ) {
  217. $url_parts['proto'] = 'https';
  218. }
  219. $url_parts['port'] = '';
  220. if ( isset( $_SERVER['SERVER_PORT'] ) && ( $_SERVER['SERVER_PORT'] != '80' ) && ( $_SERVER['SERVER_PORT'] != '443' ) ) {
  221. $url_parts['port'] = $_SERVER['SERVER_PORT'];
  222. }
  223. $url_parts['domain'] = ltrim( $_SERVER['SERVER_NAME'], '.*' );
  224. $url_parts['uri'] = $_SERVER['REQUEST_URI'];
  225. $url_parts = apply_filters( 'swpm_get_current_page_url_alt_filter', $url_parts );
  226. $pageURL = sprintf( '%s://%s%s%s', $url_parts['proto'], $url_parts['domain'], ! empty( $url_parts['port'] ) ? ':' . $url_parts['port'] : '', $url_parts['uri'] );
  227. return $pageURL;
  228. }
  229. /*
  230. * Returns just the domain name. Something like example.com
  231. */
  232. public static function get_home_url_without_http_and_www() {
  233. $site_url = get_site_url();
  234. $parse = parse_url( $site_url );
  235. $site_url = $parse['host'];
  236. $site_url = str_replace( 'https://', '', $site_url );
  237. $site_url = str_replace( 'http://', '', $site_url );
  238. if ( preg_match( '/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $site_url, $regs ) ) {
  239. $site_url = $regs['domain'];
  240. }
  241. return $site_url;
  242. }
  243. public static function replace_dynamic_tags( $msg_body, $member_id, $additional_args = '' ) {
  244. $settings = SwpmSettings::get_instance();
  245. $user_record = SwpmMemberUtils::get_user_by_id( $member_id );
  246. $password = '';
  247. $reg_link = '';
  248. if ( ! empty( $additional_args ) ) {
  249. $password = isset( $additional_args['password'] ) ? $additional_args['password'] : $password;
  250. $reg_link = isset( $additional_args['reg_link'] ) ? $additional_args['reg_link'] : $reg_link;
  251. }
  252. $login_link = $settings->get_value( 'login-page-url' );
  253. //Construct the primary address value
  254. $primary_address = '';
  255. if ( ! empty( $user_record->address_street ) && ! empty( $user_record->address_city ) ) {
  256. //An address value is present.
  257. $primary_address .= $user_record->address_street;
  258. $primary_address .= "\n" . $user_record->address_city;
  259. if ( ! empty( $user_record->address_state ) ) {
  260. $primary_address .= ' ' . $user_record->address_state;
  261. }
  262. if ( ! empty( $user_record->address_zipcode ) ) {
  263. $primary_address .= ' ' . $user_record->address_zipcode;
  264. }
  265. if ( ! empty( $user_record->country ) ) {
  266. $primary_address .= "\n" . $user_record->country;
  267. }
  268. }
  269. $membership_level_name = SwpmMembershipLevelUtils::get_membership_level_name_of_a_member( $member_id );
  270. //Format some field values
  271. $member_since_formatted = SwpmUtils::get_formatted_date_according_to_wp_settings( $user_record->member_since );
  272. $subsc_starts_formatted = SwpmUtils::get_formatted_date_according_to_wp_settings( $user_record->subscription_starts );
  273. //Define the replacable tags
  274. $tags = array(
  275. '{member_id}',
  276. '{user_name}',
  277. '{first_name}',
  278. '{last_name}',
  279. '{membership_level}',
  280. '{membership_level_name}',
  281. '{account_state}',
  282. '{email}',
  283. '{phone}',
  284. '{member_since}',
  285. '{subscription_starts}',
  286. '{company_name}',
  287. '{password}',
  288. '{login_link}',
  289. '{reg_link}',
  290. '{primary_address}',
  291. '{birth_place}',
  292. '{birth_date}',
  293. );
  294. //Define the values
  295. $vals = array(
  296. $member_id,
  297. $user_record->user_name,
  298. $user_record->first_name,
  299. $user_record->last_name,
  300. $user_record->membership_level,
  301. $membership_level_name,
  302. $user_record->account_state,
  303. $user_record->email,
  304. $user_record->phone,
  305. $member_since_formatted,
  306. $subsc_starts_formatted,
  307. $user_record->company_name,
  308. $password,
  309. $login_link,
  310. $reg_link,
  311. $primary_address,
  312. $user_record->birth_place,
  313. $user_record->birth_date,
  314. );
  315. $msg_body = str_replace( $tags, $vals, $msg_body );
  316. return $msg_body;
  317. }
  318. public static function get_login_link() {
  319. $login_url = SwpmSettings::get_instance()->get_value( 'login-page-url' );
  320. $joinus_url = SwpmSettings::get_instance()->get_value( 'join-us-page-url' );
  321. if ( empty( $login_url ) || empty( $joinus_url ) ) {
  322. return '<span style="color:red;">Simple Membership is not configured correctly. The login page or the join us page URL is missing in the settings configuration. '
  323. . 'Please contact <a href="mailto:' . get_option( 'admin_email' ) . '">Admin</a>';
  324. }
  325. //Create the login/protection message
  326. $filtered_login_url = apply_filters( 'swpm_get_login_link_url', $login_url ); //Addons can override the login URL value using this filter.
  327. $login_msg = '';
  328. $login_msg .= SwpmUtils::_( 'Please' ) . ' <a class="swpm-login-link" href="' . $filtered_login_url . '">' . SwpmUtils::_( 'Login' ) . '</a>. ';
  329. $login_msg .= SwpmUtils::_( 'Not a Member?' ) . ' <a href="' . $joinus_url . '">' . SwpmUtils::_( 'Join Us' ) . '</a>';
  330. return $login_msg;
  331. }
  332. public static function get_renewal_link() {
  333. $renewal = SwpmSettings::get_instance()->get_value( 'renewal-page-url' );
  334. if ( empty( $renewal ) ) {
  335. //No renewal page is configured so don't show any renewal page link. It is okay to have no renewal page configured.
  336. return '';
  337. }
  338. return SwpmUtils::_( 'Please' ) . ' <a class="swpm-renewal-link" href="' . $renewal . '">' . SwpmUtils::_( 'renew' ) . '</a> ' . SwpmUtils::_( ' your account to gain access to this content.' );
  339. }
  340. public static function compare_url( $url1, $url2 ) {
  341. $url1 = trailingslashit( strtolower( $url1 ) );
  342. $url2 = trailingslashit( strtolower( $url2 ) );
  343. if ( $url1 == $url2 ) {
  344. return true;
  345. }
  346. $url1 = parse_url( $url1 );
  347. $url2 = parse_url( $url2 );
  348. $components = array( 'scheme', 'host', 'port', 'path' );
  349. foreach ( $components as $key => $value ) {
  350. if ( ! isset( $url1[ $value ] ) && ! isset( $url2[ $value ] ) ) {
  351. continue;
  352. }
  353. if ( ! isset( $url2[ $value ] ) ) {
  354. return false;
  355. }
  356. if ( ! isset( $url1[ $value ] ) ) {
  357. return false;
  358. }
  359. if ( $url1[ $value ] != $url2[ $value ] ) {
  360. return false;
  361. }
  362. }
  363. if ( ! isset( $url1['query'] ) && ! isset( $url2['query'] ) ) {
  364. return true;
  365. }
  366. if ( ! isset( $url2['query'] ) ) {
  367. return false;
  368. }
  369. if ( ! isset( $url1['query'] ) ) {
  370. return false;
  371. }
  372. return strpos( $url1['query'], $url2['query'] ) || strpos( $url2['query'], $url1['query'] );
  373. }
  374. public static function is_swpm_admin_page() {
  375. if ( isset( $_GET['page'] ) && ( stripos( $_GET['page'], 'simple_wp_membership' ) !== false ) ) {
  376. //This is an admin page of the SWPM plugin
  377. return true;
  378. }
  379. return false;
  380. }
  381. public static function check_user_permission_and_is_admin( $action_name ) {
  382. //Check we are on the admin end
  383. if ( ! is_admin() ) {
  384. //Error! This is not on the admin end. This can only be done from the admin side
  385. wp_die( SwpmUtils::_( 'Error! This action (' . $action_name . ') can only be done from admin end.' ) );
  386. }
  387. //Check user has management permission
  388. if ( ! current_user_can( SWPM_MANAGEMENT_PERMISSION ) ) {
  389. //Error! Only management users can do this
  390. wp_die( SwpmUtils::_( 'Error! This action (' . $action_name . ') can only be done by an user with management permission.' ) );
  391. }
  392. }
  393. public static function format_raw_content_for_front_end_display( $raw_content ) {
  394. $formatted_content = wptexturize( $raw_content );
  395. $formatted_content = convert_smilies( $formatted_content );
  396. $formatted_content = convert_chars( $formatted_content );
  397. $formatted_content = wpautop( $formatted_content );
  398. $formatted_content = shortcode_unautop( $formatted_content );
  399. $formatted_content = prepend_attachment( $formatted_content );
  400. $formatted_content = capital_P_dangit( $formatted_content );
  401. $formatted_content = do_shortcode( $formatted_content );
  402. return $formatted_content;
  403. }
  404. public static function get_countries_dropdown( $country = '' ) {
  405. $countries = array(
  406. 'Afghanistan',
  407. 'Albania',
  408. 'Algeria',
  409. 'Andorra',
  410. 'Angola',
  411. 'Antigua and Barbuda',
  412. 'Argentina',
  413. 'Armenia',
  414. 'Aruba',
  415. 'Australia',
  416. 'Austria',
  417. 'Azerbaijan',
  418. 'Bahamas',
  419. 'Bahrain',
  420. 'Bangladesh',
  421. 'Barbados',
  422. 'Belarus',
  423. 'Belgium',
  424. 'Belize',
  425. 'Benin',
  426. 'Bhutan',
  427. 'Bolivia',
  428. 'Bonaire',
  429. 'Bosnia and Herzegovina',
  430. 'Botswana',
  431. 'Brazil',
  432. 'Brunei',
  433. 'Bulgaria',
  434. 'Burkina Faso',
  435. 'Burundi',
  436. 'Cambodia',
  437. 'Cameroon',
  438. 'Canada',
  439. 'Cape Verde',
  440. 'Central African Republic',
  441. 'Chad',
  442. 'Chile',
  443. 'China',
  444. 'Colombia',
  445. 'Comoros',
  446. 'Congo (Brazzaville)',
  447. 'Congo',
  448. 'Costa Rica',
  449. "Cote d\'Ivoire",
  450. 'Croatia',
  451. 'Cuba',
  452. 'Curacao',
  453. 'Cyprus',
  454. 'Czech Republic',
  455. 'Denmark',
  456. 'Djibouti',
  457. 'Dominica',
  458. 'Dominican Republic',
  459. 'East Timor (Timor Timur)',
  460. 'Ecuador',
  461. 'Egypt',
  462. 'El Salvador',
  463. 'Equatorial Guinea',
  464. 'Eritrea',
  465. 'Estonia',
  466. 'Eswatini',
  467. 'Ethiopia',
  468. 'Fiji',
  469. 'Finland',
  470. 'France',
  471. 'Gabon',
  472. 'Gambia, The',
  473. 'Georgia',
  474. 'Germany',
  475. 'Ghana',
  476. 'Greece',
  477. 'Grenada',
  478. 'Guatemala',
  479. 'Guinea',
  480. 'Guinea-Bissau',
  481. 'Guyana',
  482. 'Haiti',
  483. 'Honduras',
  484. 'Hong Kong',
  485. 'Hungary',
  486. 'Iceland',
  487. 'India',
  488. 'Indonesia',
  489. 'Iran',
  490. 'Iraq',
  491. 'Ireland',
  492. 'Israel',
  493. 'Italy',
  494. 'Jamaica',
  495. 'Japan',
  496. 'Jordan',
  497. 'Kazakhstan',
  498. 'Kenya',
  499. 'Kiribati',
  500. 'Korea, North',
  501. 'Korea, South',
  502. 'Kuwait',
  503. 'Kyrgyzstan',
  504. 'Laos',
  505. 'Latvia',
  506. 'Lebanon',
  507. 'Lesotho',
  508. 'Liberia',
  509. 'Libya',
  510. 'Liechtenstein',
  511. 'Lithuania',
  512. 'Luxembourg',
  513. 'Macedonia',
  514. 'Madagascar',
  515. 'Malawi',
  516. 'Malaysia',
  517. 'Maldives',
  518. 'Mali',
  519. 'Malta',
  520. 'Marshall Islands',
  521. 'Mauritania',
  522. 'Mauritius',
  523. 'Mexico',
  524. 'Micronesia',
  525. 'Moldova',
  526. 'Monaco',
  527. 'Mongolia',
  528. 'Montenegro',
  529. 'Morocco',
  530. 'Mozambique',
  531. 'Myanmar',
  532. 'Namibia',
  533. 'Nauru',
  534. 'Nepa',
  535. 'Netherlands',
  536. 'New Zealand',
  537. 'Nicaragua',
  538. 'Niger',
  539. 'Nigeria',
  540. 'Norway',
  541. 'Oman',
  542. 'Pakistan',
  543. 'Palau',
  544. 'Palestine',
  545. 'Panama',
  546. 'Papua New Guinea',
  547. 'Paraguay',
  548. 'Peru',
  549. 'Philippines',
  550. 'Poland',
  551. 'Portugal',
  552. 'Qatar',
  553. 'Romania',
  554. 'Russia',
  555. 'Rwanda',
  556. 'Saint Kitts and Nevis',
  557. 'Saint Lucia',
  558. 'Saint Vincent',
  559. 'Samoa',
  560. 'San Marino',
  561. 'Sao Tome and Principe',
  562. 'Saudi Arabia',
  563. 'Senegal',
  564. 'Serbia',
  565. 'Seychelles',
  566. 'Sierra Leone',
  567. 'Singapore',
  568. 'Slovakia',
  569. 'Slovenia',
  570. 'Solomon Islands',
  571. 'Somalia',
  572. 'South Africa',
  573. 'Spain',
  574. 'Sri Lanka',
  575. 'Sudan',
  576. 'Suriname',
  577. 'Swaziland',
  578. 'Sweden',
  579. 'Switzerland',
  580. 'Syria',
  581. 'Taiwan',
  582. 'Tajikistan',
  583. 'Tanzania',
  584. 'Thailand',
  585. 'Togo',
  586. 'Tonga',
  587. 'Trinidad and Tobago',
  588. 'Tunisia',
  589. 'Turkey',
  590. 'Turkmenistan',
  591. 'Tuvalu',
  592. 'Uganda',
  593. 'Ukraine',
  594. 'United Arab Emirates',
  595. 'United Kingdom',
  596. 'United States of America',
  597. 'Uruguay',
  598. 'Uzbekistan',
  599. 'Vanuatu',
  600. 'Vatican City',
  601. 'Venezuela',
  602. 'Vietnam',
  603. 'Yemen',
  604. 'Zambia',
  605. 'Zimbabwe',
  606. );
  607. //let's try to "guess" country name
  608. $curr_lev = -1;
  609. $guess_country = '';
  610. foreach ( $countries as $country_name ) {
  611. similar_text( strtolower( $country ), strtolower( $country_name ), $lev );
  612. if ( $lev >= $curr_lev ) {
  613. //this is closest match so far
  614. $curr_lev = $lev;
  615. $guess_country = $country_name;
  616. }
  617. if ( $curr_lev == 100 ) {
  618. //exact match
  619. break;
  620. }
  621. }
  622. if ( $curr_lev <= 80 ) {
  623. // probably bad guess
  624. $guess_country = '';
  625. }
  626. $countries_dropdown = '';
  627. //let's add "(Please select)" option
  628. $countries_dropdown .= "\r\n" . '<option value=""' . ( $country == '' ? ' selected' : '' ) . '>' . SwpmUtils::_( '(Please Select)' ) . '</option>';
  629. if ( $guess_country == '' && $country != '' ) {
  630. //since we haven't guessed the country name, let's add current value to the options
  631. $countries_dropdown .= "\r\n" . '<option value="' . $country . '" selected>' . $country . '</option>';
  632. }
  633. if ( $guess_country != '' ) {
  634. $country = $guess_country;
  635. }
  636. foreach ( $countries as $country_name ) {
  637. $countries_dropdown .= "\r\n" . '<option value="' . $country_name . '"' . ( strtolower( $country_name ) == strtolower( $country ) ? ' selected' : '' ) . '>' . $country_name . '</option>';
  638. }
  639. return $countries_dropdown;
  640. }
  641. public static function get_button_type_name( $button_type ) {
  642. $btnTypesNames = array(
  643. 'pp_buy_now' => SwpmUtils::_( 'PayPal Buy Now' ),
  644. 'pp_subscription' => SwpmUtils::_( 'PayPal Subscription' ),
  645. 'pp_smart_checkout' => SwpmUtils::_( 'PayPal Smart Checkout' ),
  646. 'stripe_buy_now' => SwpmUtils::_( 'Stripe Buy Now' ),
  647. 'stripe_subscription' => SwpmUtils::_( 'Stripe Subscription' ),
  648. 'stripe_sca_buy_now' => SwpmUtils::_( 'Stripe SCA Buy Now' ),
  649. 'stripe_sca_subscription' => SwpmUtils::_( 'Stripe SCA Subscription' ),
  650. 'braintree_buy_now' => SwpmUtils::_( 'Braintree Buy Now' ),
  651. );
  652. $button_type_name = $button_type;
  653. if ( array_key_exists( $button_type, $btnTypesNames ) ) {
  654. $button_type_name = $btnTypesNames[ $button_type ];
  655. }
  656. return $button_type_name;
  657. }
  658. public static function format_money( $amount, $currency = false ) {
  659. $formatted = number_format( $amount, 2 );
  660. if ( $currency ) {
  661. $formatted .= ' ' . $currency;
  662. }
  663. return $formatted;
  664. }
  665. public static function load_stripe_lib() {
  666. //this function loads Stripe PHP SDK and ensures only once instance is loaded
  667. if ( ! class_exists( '\Stripe\Stripe' ) ) {
  668. require_once SIMPLE_WP_MEMBERSHIP_PATH . 'lib/stripe-gateway/init.php';
  669. \Stripe\Stripe::setAppInfo( 'Simple Membership', SIMPLE_WP_MEMBERSHIP_VER, 'https://simple-membership-plugin.com/', 'pp_partner_Fvas9OJ0jQ2oNQ' );
  670. }
  671. }
  672. public static function get_stripe_api_keys_from_payment_button( $button_id, $live = false ) {
  673. $keys = array(
  674. 'public' => '',
  675. 'secret' => '',
  676. );
  677. $button = get_post( $button_id );
  678. if ( $button ) {
  679. $opts = get_option( 'swpm-settings' );
  680. $use_global_keys = get_post_meta( $button_id, 'stripe_use_global_keys', true );
  681. if ( $use_global_keys ) {
  682. if ( $live ) {
  683. $keys['public'] = isset( $opts['stripe-live-public-key'] ) ? $opts['stripe-live-public-key'] : '';
  684. $keys['secret'] = isset( $opts['stripe-live-secret-key'] ) ? $opts['stripe-live-secret-key'] : '';
  685. } else {
  686. $keys['public'] = isset( $opts['stripe-test-public-key'] ) ? $opts['stripe-test-public-key'] : '';
  687. $keys['secret'] = isset( $opts['stripe-test-secret-key'] ) ? $opts['stripe-test-secret-key'] : '';
  688. }
  689. } else {
  690. if ( $live ) {
  691. $stripe_live_secret_key = get_post_meta( $button_id, 'stripe_live_secret_key', true );
  692. $stripe_live_publishable_key = get_post_meta( $button_id, 'stripe_live_publishable_key', true );
  693. $keys['public'] = $stripe_live_publishable_key;
  694. $keys['secret'] = $stripe_live_secret_key;
  695. } else {
  696. $stripe_test_secret_key = get_post_meta( $button_id, 'stripe_test_secret_key', true );
  697. $stripe_test_publishable_key = get_post_meta( $button_id, 'stripe_test_publishable_key', true );
  698. $keys['public'] = $stripe_test_publishable_key;
  699. $keys['secret'] = $stripe_test_secret_key;
  700. }
  701. }
  702. }
  703. return $keys;
  704. }
  705. public static function mail( $email, $subject, $email_body, $headers ) {
  706. $settings = SwpmSettings::get_instance();
  707. $html_enabled = $settings->get_value( 'email-enable-html' );
  708. if ( ! empty( $html_enabled ) ) {
  709. $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
  710. $email_body = nl2br( $email_body );
  711. }
  712. wp_mail( $email, $subject, $email_body, $headers );
  713. }
  714. /**
  715. * Outputs Stripe SCA frontend scripts and styles once
  716. */
  717. public static function output_stripe_sca_frontend_scripts_once() {
  718. $out = '';
  719. if ( ! self::$stripe_sca_frontend_scripts_printed ) {
  720. $out .= '<script src="https://js.stripe.com/v3/"></script>';
  721. $out .= "<link rel='stylesheet' href='https://checkout.stripe.com/v3/checkout/button.css' type='text/css' media='all' />";
  722. self::$stripe_sca_frontend_scripts_printed = true;
  723. }
  724. return $out;
  725. }
  726. }