Guest User

Untitled

a guest
Jul 14th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.38 KB | None | 0 0
  1. <?php
  2. /**
  3. * The WP_Members User Profile Class.
  4. *
  5. * @package WP-Members
  6. * @subpackage WP_Members Admin User Profile Object Class
  7. * @since 3.1.8
  8. */
  9.  
  10. // Exit if accessed directly.
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit();
  13. }
  14.  
  15. class WP_Members_User_Profile {
  16.  
  17. /**
  18. * Static function to display WP-Members fields on the admin/dashboard user profile.
  19. *
  20. * Function was created in 3.1.9 as a merge of wpmem_admin_fields()
  21. * and wpmem_user_profile().
  22. *
  23. * @since 3.1.9
  24. *
  25. * @global object $current_screen
  26. * @global string $user_ID
  27. * @global object $wpmem
  28. * @param object $user_obj
  29. */
  30. static function profile( $user_obj ) {
  31.  
  32. global $current_screen, $user_ID, $wpmem;
  33. $user_id = ( 'profile' == $current_screen->id ) ? $user_ID : filter_var( $_REQUEST['user_id'], FILTER_SANITIZE_NUMBER_INT );
  34. $display = ( 'profile' == $current_screen->base ) ? 'user' : 'admin'; ?>
  35.  
  36. <h3><?php
  37. $heading = ( 'admin' == $display ) ? __( 'WP-Members Additional Fields', 'wp-members' ) : __( 'Additional Information', 'wp-members' );
  38. /**
  39. * Filter the heading for additional profile fields.
  40. *
  41. * @since 2.8.2 Admin Profile
  42. * @since 2.9.1 Dashboard Profile
  43. * @since 3.1.9 Merged admin/dashboard profile
  44. *
  45. * @param string The default additional fields heading.
  46. */
  47. echo apply_filters( 'wpmem_' . $display . '_profile_heading', $heading ); ?></h3>
  48. <table class="form-table">
  49. <?php
  50. // Get fields.
  51. $wpmem_fields = ( 'admin' == $display ) ? wpmem_fields( 'admin_profile' ) : wpmem_fields( 'dashboard_profile' );
  52. // Get excluded meta.
  53. $exclude = wpmem_get_excluded_meta( $display . '-profile' );
  54.  
  55. // If tos is an active field, this is the dashboard profile, and user has current field value.
  56. if ( isset( $wpmem_fields['tos'] ) && get_user_meta( $user_ID, 'tos', true ) == $wpmem_fields['tos']['checked_value'] ) {
  57. unset( $wpmem_fields['tos'] );
  58. }
  59.  
  60. /**
  61. * Fires at the beginning of generating the WP-Members fields in the user profile.
  62. *
  63. * @since 2.9.3 Created for admin profile.
  64. * @since 3.1.9 Added to dashboard profile.
  65. *
  66. * @param int $user_id The user's ID.
  67. * @param array $wpmem_fields The WP-Members fields.
  68. */
  69. do_action( 'wpmem_' . $display . '_before_profile', $user_id, $wpmem_fields );
  70.  
  71. // Assemble form rows array.
  72. $rows = array();
  73. foreach ( $wpmem_fields as $meta => $field ) {
  74.  
  75. $valtochk = ''; $values = '';
  76.  
  77. // Determine which fields to show in the additional fields area.
  78. $show = ( ! $field['native'] && ! in_array( $meta, $exclude ) ) ? true : false;
  79. $show = ( 'tos' == $meta && $field['register'] ) ? null : $show;
  80.  
  81. if ( $show ) {
  82.  
  83. $val = get_user_meta( $user_id, $meta, true );
  84. $val = ( $field['type'] == 'multiselect' || $field['type'] == 'multicheckbox' ) ? $val : htmlspecialchars( $val );
  85. if ( $field['type'] == 'checkbox' ) {
  86. $valtochk = $val;
  87. $val = $field['checked_value'];
  88. }
  89.  
  90. if ( 'multicheckbox' == $field['type'] || 'select' == $field['type'] || 'multiselect' == $field['type'] || 'radio' == $field['type'] ) {
  91. $values = $field['values'];
  92. $valtochk = $val;
  93. }
  94.  
  95. // Is this an image or a file?
  96. if ( 'file' == $field['type'] || 'image' == $field['type'] ) {
  97. $empty_file = '<span class="description">' . __( 'None' ) . '</span>';
  98. if ( 'file' == $field['type'] ) {
  99. $attachment_url = wp_get_attachment_url( $val );
  100. $input = ( $attachment_url ) ? '<a href="' . $attachment_url . '">' . $attachment_url . '</a>' : $empty_file;
  101. } else {
  102. $attachment_url = wp_get_attachment_image( $val, 'medium' );
  103. if ( 'admin' == $display ) {
  104. $edit_url = admin_url( 'upload.php?item=' . $val );
  105. $input = ( $attachment_url ) ? '<a href="' . $edit_url . '">' . $attachment_url . '</a>' : $empty_file;
  106. } else {
  107. $input = ( $attachment_url ) ? $attachment_url : $empty_file;
  108. }
  109. }
  110. $input.= '<br />' . $wpmem->get_text( 'profile_upload' ) . '<br />';
  111. $input.= wpmem_form_field( array(
  112. 'name' => $meta,
  113. 'type' => $field['type'],
  114. 'value' => $val,
  115. 'compare' => $valtochk,
  116. ) );
  117. } else {
  118. if ( 'select' == $field['type'] || 'radio' == $field['type'] ) {
  119. $input = wpmem_create_formfield( $meta, $field['type'], $values, $valtochk );
  120. } elseif( 'multicheckbox' == $field['type'] || 'multiselect' == $field['type'] ) {
  121. $input = $wpmem->forms->create_form_field( array( 'name'=>$meta, 'type'=>$field['type'], 'value'=>$values, 'compare'=>$valtochk, 'delimiter'=>$field['delimiter'] ) );
  122. } else {
  123. $field['type'] = ( 'hidden' == $field['type'] ) ? 'text' : $field['type'];
  124. $input = wpmem_create_formfield( $meta, $field['type'], $val, $valtochk );
  125. }
  126. }
  127.  
  128. // Is the field required?
  129. $req = ( $field['required'] ) ? ' <span class="description">' . __( '(required)' ) . '</span>' : '';
  130. $label = '<label>' . __( $field['label'], 'wp-members' ) . $req . '</label>';
  131.  
  132. // Build the form rows for filtering.
  133. $rows[ $meta ] = array(
  134. 'meta' => $meta,
  135. 'type' => $field['type'],
  136. 'value' => $val,
  137. 'values' => $values,
  138. 'label_text' => __( $field['label'], 'wp-members' ),
  139. 'row_before' => '<tr>',
  140. 'label' => '<th>' . $label . '</th>',
  141. 'field_before' => '<td>',
  142. 'field' => $input,
  143. 'field_after' => '</td>',
  144. 'row_after' => '</tr>',
  145. );
  146. }
  147. }
  148.  
  149. /**
  150. * Filter for rows
  151. *
  152. * @since 3.1.0
  153. * @since 3.1.6 Deprecated $order.
  154. *
  155. * @param array $rows {
  156. * An array of the profile rows.
  157. *
  158. * @type string $meta The meta key.
  159. * @type string $type The field type.
  160. * @type string $value Value if set.
  161. * @type string $values Possible values (select, multiselect, multicheckbox, radio).
  162. * @type string $label_text Raw label text (no HTML).
  163. * @type string $row_before HTML before the row.
  164. * @type string $label HTML label.
  165. * @type string $field_before HTML before the field input tag.
  166. * @type string $field HTML for field input.
  167. * @type string $field_after HTML after the field.
  168. * @type string $row_after HTML after the row.
  169. * }
  170. * @param string $tag adminprofile|userprofile
  171. */
  172. $rows = apply_filters( 'wpmem_register_form_rows_' . $display, $rows, $display . 'profile' );
  173.  
  174. // Handle form rows display from array.
  175. foreach ( $rows as $row ) {
  176. $show_field =
  177. $row['row_before'] .
  178. $row['label'] .
  179. $row['field_before'] . $row['field'] . $row['field_after'] .
  180. $row['field_after'];
  181.  
  182. /**
  183. * Filter the profile field.
  184. *
  185. * @since 2.8.2
  186. * @since 3.1.1 Added $user_id and $row
  187. *
  188. * @param string $show_field The HTML string for the additional profile field.
  189. * @param string $user_id
  190. * @param array $row
  191. */
  192. echo apply_filters( 'wpmem_' . $display . '_profile_field', $show_field, $user_id, $row );
  193. }
  194.  
  195. /**
  196. * Fires after generating the WP-Members fields in the user profile.
  197. *
  198. * @since 2.9.3
  199. *
  200. * @param int $user_id The user's ID.
  201. * @param array $wpmem_fields The WP-Members fields.
  202. */
  203. do_action( 'wpmem_' . $display . '_after_profile', $user_id, $wpmem_fields ); ?>
  204.  
  205. </table><?php
  206.  
  207. }
  208.  
  209. /**
  210. * Static function to update admin/dashboard user profile.
  211. *
  212. * Function was created in 3.1.9 as a merge of wpmem_admin_update()
  213. * and wpmem_profile_update().
  214. *
  215. * @since 3.1.9
  216. *
  217. * @global object $current_screen
  218. * @global string $user_id
  219. * @global object $wpmem
  220. * @param string $user_id
  221. * @return
  222. */
  223. static function update( $user_id ) {
  224.  
  225. global $current_screen, $user_id, $wpmem;
  226. $display = ( 'profile' == $current_screen->base ) ? 'user' : 'admin';
  227.  
  228. if ( ! $user_id ) {
  229. $user_id = filter_var( wpmem_get( 'user_id', -1, 'request' ), FILTER_SANITIZE_NUMBER_INT );
  230. if ( 1 > $user_id ) {
  231. // Still no user id? User cannot be updated.
  232. return;
  233. }
  234. }
  235.  
  236. $wpmem_fields = ( 'admin' == $display ) ? wpmem_fields( 'admin_profile_update' ) : wpmem_fields( 'dashboard_profile_update' );
  237.  
  238. $exclude = wpmem_get_excluded_meta( $display . '-profile' );
  239.  
  240. foreach ( $exclude as $excluded ) {
  241. unset( $wpmem_fields[ $excluded ] );
  242. }
  243.  
  244. // If tos is an active field, this is the dashboard profile, and user has current field value.
  245. if ( isset( $wpmem_fields['tos'] ) && get_user_meta( $user_id, 'tos', true ) == $wpmem_fields['tos']['checked_value'] ) {
  246. unset( $wpmem_fields['tos'] );
  247. }
  248.  
  249. /**
  250. * Fires before the user profile is updated.
  251. *
  252. * @since 2.9.2 Added for admin profile update.
  253. * @since 3.1.9 Added for user profile update.
  254. *
  255. * @param int $user_id The user ID.
  256. * @param array $wpmem_fields Array of the custom fields.
  257. */
  258. do_action( 'wpmem_' . $display . '_pre_user_update', $user_id, $wpmem_fields );
  259.  
  260. $fields = array();
  261. $chk_pass = false;
  262. foreach ( $wpmem_fields as $meta => $field ) {
  263. if ( ! $field['native']
  264. && $field['type'] != 'password'
  265. && $field['type'] != 'checkbox'
  266. && $field['type'] != 'multiselect'
  267. && $field['type'] != 'multicheckbox'
  268. && $field['type'] != 'file'
  269. && $field['type'] != 'image'
  270. && $field['type'] != 'textarea' ) {
  271. ( isset( $_POST[ $meta ] ) && 'password' != $field['type'] ) ? $fields[ $meta ] = sanitize_text_field( $_POST[ $meta ] ) : false;
  272.  
  273. // For user profile (not admin).
  274. $chk = false;
  275. if ( 'admin' != $display ) {
  276. // Check for required fields.
  277. if ( ! $field['required'] ) {
  278. $chk = 'ok';
  279. }
  280. if ( $field['required'] && $_POST[ $meta ] != '' ) {
  281. $chk = 'ok';
  282. }
  283. }
  284. } elseif ( $meta == 'password' && $field['register'] ) {
  285. $chk_pass = true;
  286. } elseif ( $field['type'] == 'checkbox' ) {
  287. $fields[ $meta ] = ( isset( $_POST[ $meta ] ) ) ? sanitize_text_field( $_POST[ $meta ] ) : '';
  288. } elseif ( $field['type'] == 'multiselect' || $field['type'] == 'multicheckbox' ) {
  289. $fields[ $meta ] = ( isset( $_POST[ $meta ] ) ) ? implode( $field['delimiter'], wp_unslash( $_POST[ $meta ] ) ) : '';
  290. } elseif ( $field['type'] == 'textarea' ) {
  291. $fields[ $meta ] = ( isset( $_POST[ $meta ] ) ) ? sanitize_textarea_field( $_POST[ $meta ] ) : '';
  292. }
  293. }
  294.  
  295. /**
  296. * Filter the submitted field values for backend profile update.
  297. *
  298. * @since 2.8.2 Added for Admin profile update.
  299. * @since 3.1.9 Added for User profile update.
  300. *
  301. * @param array $fields An array of the posted form values.
  302. * @param int $user_id The ID of the user being updated.
  303. */
  304. $fields = apply_filters( 'wpmem_' . $display . '_profile_update', $fields, $user_id );
  305.  
  306. // Handle meta update, skip excluded fields.
  307. foreach ( $fields as $key => $val ) {
  308. if ( ! in_array( $key, $exclude ) ) {
  309. if ( ( 'admin' != $display && 'ok' == $chk ) || 'admin' == $display ) {
  310. update_user_meta( $user_id, $key, $val );
  311. }
  312. }
  313. }
  314.  
  315. if ( ! empty( $_FILES ) ) {
  316. $wpmem->user->upload_user_files( $user_id, $wpmem->fields );
  317. }
  318.  
  319. if ( 'admin' == $display || current_user_can( 'edit_users' ) ) {
  320. if ( $wpmem->mod_reg == 1 ) {
  321.  
  322. $wpmem_activate_user = ( isset( $_POST['activate_user'] ) == '' ) ? -1 : filter_var( $_POST['activate_user'], FILTER_SANITIZE_NUMBER_INT );
  323.  
  324. if ( $wpmem_activate_user == 1 ) {
  325. wpmem_a_activate_user( $user_id, $chk_pass );
  326. } elseif ( $wpmem_activate_user == 0 ) {
  327. wpmem_a_deactivate_user( $user_id );
  328. }
  329. }
  330.  
  331. if ( defined( 'WPMEM_EXP_MODULE' ) && $wpmem->use_exp == 1 ) {
  332. if ( function_exists( 'wpmem_a_extenduser' ) ) {
  333. wpmem_a_extend_user( $user_id );
  334. }
  335. }
  336.  
  337. if ( 1 == $wpmem->enable_products ) {
  338. // Update products.
  339. if ( isset( $_POST['_wpmem_membership_product'] ) ) {
  340. foreach ( $_POST['_wpmem_membership_product'] as $product_key => $product_value ) {
  341. // Enable or Disable?
  342. if ( 'enable' == $product_value ) {
  343. // Does product require a role?
  344. if ( false !== $wpmem->membership->product_detail[ $product_key ]['role'] ) {
  345. wpmem_update_user_role( $user_id, $wpmem->membership->product_detail[ $product_key ]['role'], 'add' );
  346. }
  347. $wpmem->user->set_user_product( $product_key, $user_id );
  348. }
  349. if ( 'disable' == $product_value ) {
  350. $wpmem->user->remove_user_product( $product_key, $user_id );
  351. }
  352. }
  353. }
  354. }
  355. }
  356.  
  357. /**
  358. * Fires after the user profile is updated.
  359. *
  360. * @since 2.9.2
  361. *
  362. * @param int $user_id The user ID.
  363. */
  364. do_action( 'wpmem_' . $display . '_after_user_update', $user_id );
  365.  
  366. return;
  367. }
  368.  
  369. /**
  370. * Sets user profile update to multipart form data.
  371. *
  372. * If the fields array has a file or image field, this will echo the
  373. * necessary "multipart/form-data" enctype for the form tag.
  374. *
  375. * @since 3.1.8 (as wpmem_profile_multipart()).
  376. * @since 3.1.9 Moved to User Profile object.
  377. */
  378. public static function add_multipart() {
  379. $has_file = false;
  380. foreach ( wpmem_fields() as $field ) {
  381. if ( $field['type'] == 'file' || $field['type'] == 'image' ) {
  382. $has_file = true;
  383. break;
  384. }
  385. }
  386. echo ( $has_file ) ? " enctype=\"multipart/form-data\"" : '';
  387. }
  388.  
  389. /**
  390. * Adds user activation to the user profile.
  391. *
  392. * @since 3.1.1
  393. * @since 3.2.0 Moved to WP_Members_User_Profile object
  394. *
  395. * @global object $wpmem
  396. * @param int $user_id
  397. */
  398. public static function _show_activate( $user_id ) {
  399. global $wpmem;
  400. // See if reg is moderated, and if the user has been activated.
  401. if ( $wpmem->mod_reg == 1 ) {
  402. $user_active_flag = get_user_meta( $user_id, 'active', true );
  403. switch( $user_active_flag ) {
  404.  
  405. case '':
  406. $label = __( 'Activate this user?', 'wp-members' );
  407. $action = 1;
  408. break;
  409.  
  410. case 0:
  411. $label = __( 'Reactivate this user?', 'wp-members' );
  412. $action = 1;
  413. break;
  414.  
  415. case 1:
  416. $label = __( 'Deactivate this user?', 'wp-members' );
  417. $action = 0;
  418. break;
  419.  
  420. } ?>
  421. <tr>
  422. <th><label><?php echo $label; ?></label></th>
  423. <td><input id="activate_user" type="checkbox" class="input" name="activate_user" value="<?php echo $action; ?>" /></td>
  424. </tr>
  425. <?php }
  426. }
  427.  
  428. /**
  429. * Adds user expiration to the user profile.
  430. *
  431. * @since 3.1.1
  432. * @since 3.2.0 Moved to WP_Members_User_Profile object
  433. *
  434. * @global object $wpmem
  435. * @param int $user_id
  436. */
  437. public static function _show_expiration( $user_id ) {
  438.  
  439. global $wpmem;
  440. /*
  441. * If using subscription model, show expiration.
  442. * If registration is moderated, this doesn't show
  443. * if user is not active yet.
  444. */
  445. if ( defined( 'WPMEM_EXP_MODULE' ) && $wpmem->use_exp == 1 ) {
  446. if ( ( $wpmem->mod_reg == 1 && get_user_meta( $user_id, 'active', true ) == 1 ) || ( $wpmem->mod_reg != 1 ) ) {
  447. if ( function_exists( 'wpmem_a_extenduser' ) ) {
  448. wpmem_a_extenduser( $user_id );
  449. }
  450. }
  451. }
  452. }
  453.  
  454.  
  455. /**
  456. * Adds user registration IP to the user profile.
  457. *
  458. * @since 3.1.1
  459. * @since 3.2.0 Moved to WP_Members_User_Profile object
  460. *
  461. * @param int $user_id
  462. */
  463. public static function _show_ip( $user_id ) { ?>
  464. <tr>
  465. <th><label><?php _e( 'IP @ registration', 'wp-members' ); ?></label></th>
  466. <td><?php echo get_user_meta( $user_id, 'wpmem_reg_ip', true ); ?></td>
  467. </tr>
  468. <?php
  469. }
  470.  
  471. /**
  472. * Add user product access to user profile.
  473. *
  474. * @since 3.2.0
  475. *
  476. * @global object $wpmem
  477. * @param int $user_id
  478. */
  479. public static function _show_product( $user_id ) {
  480. // If product enabled
  481. global $wpmem;
  482. $user_products = $wpmem->user->get_user_products( $user_id ); ?>
  483. <tr>
  484. <th><label><?php _e( 'Product Access', 'wp-members' ); ?></label></th>
  485. <td><table><?php
  486. foreach ( $wpmem->membership->products as $key => $label ) {
  487. $checked = ( $user_products && array_key_exists( $key, $user_products ) ) ? "checked" : "";
  488. echo "<tr>";
  489. echo '<td style="padding:0px 0px;">
  490. <select name="_wpmem_membership_product[' . $key . ']">
  491. <option value="">----</option>
  492. <option value="enable">' . __( 'Enable', 'wp-members' ) . '</option>
  493. <option value="disable">' . __( 'Disable', 'wp-members' ) . '</option>
  494. </select></td><td style="padding:0px 0px;">' . $label . '</td>
  495. <td style="padding:0px 0px;">';
  496. if ( isset( $user_products[ $key ] ) ) {
  497. if ( $user_products[ $key ] !== true ) {
  498. echo __( 'Expires:', 'wp-members' ) . ' ' . $user_products[ $key ];
  499. } else {
  500. _e( 'Enabled', 'wp-members' );
  501. }
  502. } else {
  503. echo "&nbsp;";
  504. }
  505. echo '</td></tr>';
  506. }
  507. ?></table></td>
  508. </tr>
  509. <?php
  510. }
  511. }
Add Comment
Please, Sign In to add comment