PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/users.php

https://github.com/Jonathonbyrd/Wordpress-Plugin-Framework-deprecated
PHP | 1042 lines | 662 code | 135 blank | 245 comment | 126 complexity | 7ae15e47682d4aca90e92483573a33f8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @Author Jonathon byrd
  4. * @link http://www.jonathonbyrd.com
  5. * @Package Wordpress
  6. * @SubPackage Byrd Plugin Framework
  7. * @copyright Proprietary Software, Copyright Byrd Incorporated. All Rights Reserved
  8. * @Since 1.0
  9. *
  10. * users.php
  11. *
  12. */
  13. defined('ABSPATH') or die("Cannot access pages directly.");
  14. if ( !function_exists( 'get_user' ) ):
  15. /**
  16. * Action Hooks
  17. *
  18. */
  19. add_action( 'init', 'do_redirect_from_admin' );
  20. add_action( 'init', 'save_user_profile', 20 );
  21. //actions for user profile page
  22. add_action( 'show_user_profile', 'display_custom_user_fields' );
  23. add_action( 'edit_user_profile', 'display_custom_user_fields' );
  24. //actions for registration page
  25. add_action( 'register_form', 'do_registration_form' );
  26. add_action( 'edit_profile_fields', 'display_profile_fields' );
  27. //actions for updating fields
  28. add_action( 'personal_options_update', 'save_user_meta_data' );
  29. add_action( 'edit_user_profile_update', 'save_user_meta_data' );
  30. /**
  31. * Do registration form
  32. *
  33. * @return boolean
  34. * @since 1.2
  35. */
  36. function do_registration_form()
  37. {
  38. //initializing variables
  39. $user_type = is_user_type();
  40. $pages = get_registration_pages();
  41. $status = true;
  42. //reasons to fail
  43. if (!$user_type) $status = false;
  44. if ($status && !isset($pages[$user_type])) $status = false;
  45. if (!$status) do_redirect( get_bloginfo('url').'/registration/' );
  46. display_custom_user_fields( null, get_registration_fields( $user_type ));
  47. return true;
  48. }
  49. /**
  50. * Redirects to the proper page
  51. */
  52. function do_redirect_from_admin()
  53. {
  54. //initializing variables
  55. $capability = 'activate_plugins';
  56. $user =& get_user();
  57. //reasons to fail
  58. if ( strpos($_SERVER["REQUEST_URI"], '/wp-admin') === false ) return false;
  59. if ( current_user_can($capability) ) return false;
  60. if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) return false;
  61. if ( defined('DOING_AJAX') && DOING_AJAX ) return false;
  62. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return false;
  63. if ( defined('DOING_CRON') && DOING_CRON ) return false;
  64. if ( defined('WP_FIRST_INSTALL') && WP_FIRST_INSTALL ) return false;
  65. if ( defined('WP_IMPORTING') && WP_IMPORTING ) return false;
  66. if ( defined('WP_INSTALLING') && WP_INSTALLING ) return false;
  67. if ( defined('WP_REPAIRING') && WP_REPAIRING ) return false;
  68. if ( defined('WP_UNINSTALL_PLUGIN') && WP_UNINSTALL_PLUGIN ) return false;
  69. if ( is_ajaxrequest() ) return false;
  70. //if this is an ajax post
  71. if (!$user->ID && isset($_POST['logged_in_cookie']))
  72. {
  73. $parts = explode('|', $_POST['logged_in_cookie']);
  74. $user =& get_user( $parts[0] );
  75. }
  76. //if we can get the user ourself
  77. if ( $user->has_cap($capability) ) return false;
  78. if (is_user_logged_in())
  79. {
  80. do_redirect(get_bloginfo('url').'/profile/');
  81. }
  82. else
  83. {
  84. do_redirect(get_bloginfo('url').'/login/');
  85. }
  86. }
  87. /**
  88. * Save the User Profile
  89. *
  90. * This function is responsible for saving the user fields upon post. SO
  91. * LONG AS, the user is already logged in. This does not create a new user.
  92. *
  93. * @return boolean
  94. * @since 1.2
  95. */
  96. function save_user_profile()
  97. {
  98. //initializing variables
  99. $user =& get_user( BRequest::getVar('user_id') );
  100. //reasons to fail
  101. //handling any required actions
  102. if ( !is_user_logged_in() ) return false;
  103. if ( BRequest::getVar('action',false) != 'edit' ) return false;
  104. if ( !wp_verify_nonce(BRequest::getVar("user_meta_box_nonce"), basename(__FILE__)) )
  105. return false;
  106. //initializing variables
  107. $data = BRequest::get('post');
  108. $data['ID'] = $user->ID;
  109. //loading libraries
  110. require_once( ABSPATH.WPINC.DS.'registration.php' );
  111. //doing all the saves
  112. if (!save_useremail()) $data['user_email'] = $user->user_email;
  113. if (wp_insert_user($data) //update the user
  114. && save_userpw( $data['pass1'], $data['pass2'] ) //update the users pw
  115. && save_user_meta_data( $data['ID'] )) //update the users email
  116. {
  117. set_notification('Profile has been updated');
  118. }
  119. return true;
  120. }
  121. /**
  122. * Save the password
  123. *
  124. * @param $pass1
  125. * @param $pass2
  126. * @since 1.0
  127. */
  128. function save_userpw( $pass1 = null, $pass2 = null )
  129. {
  130. //reasons to fail
  131. if ( !is_user_logged_in() ) return false;
  132. if ( is_null($pass1) ) return true;
  133. if ( is_null($pass2) ) return true;
  134. if ( trim($pass1) == "" ) return true;
  135. //checking for harmful injections
  136. $temp = strip_tags($pass1);
  137. if ($temp != $pass1) return false;
  138. if ($pass2 != $pass1) return false;
  139. //initializing variables
  140. $data = array();
  141. $data['user_pass'] = wp_hash_password($pass1);
  142. //loading resources
  143. global $wpdb;
  144. $user =& get_user();
  145. if ($wpdb->update( $wpdb->users, $data, array('ID' => $user->ID) ))
  146. {
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Saves the users email
  153. *
  154. * @since 1.0
  155. */
  156. function save_useremail()
  157. {
  158. //initializing variables
  159. if ( !is_user_logged_in() ) return false;
  160. if ( !BRequest::getVar("user_email", false) )
  161. {
  162. set_warning('An email is required.');
  163. return false;
  164. }
  165. require_once dirname(__file__).DS."includes".DS.'mail.php';
  166. if (!check_email_address(BRequest::getVar("user_email")))
  167. {
  168. set_warning('The given email must be valid.');
  169. return false;
  170. }
  171. //loading resources
  172. require_once(ABSPATH . WPINC . '/pluggable.php');
  173. //initializing variables
  174. global $wpdb;
  175. $user =& get_user();
  176. $data = array();
  177. $data["user_email"] = BRequest::getVar("user_email");
  178. if ($wpdb->update( $wpdb->users, $data, array('ID' => $user->ID) ))
  179. {
  180. return true;
  181. }
  182. return false;
  183. }
  184. /**
  185. * Save user meta data
  186. *
  187. * @param $user_id
  188. */
  189. function save_user_meta_data( $user_id )
  190. {
  191. //initializing variables
  192. $user = new WP_User( $user_id );
  193. $fields = array();
  194. $fields = wp_parse_args($fields, get_custom_user_fields( $user->roles[0] ));
  195. //reasons to fail
  196. if (empty($fields)) return false;
  197. //load library
  198. require_once ABSPATH.WPINC."/pluggable.php";
  199. // verify nonce
  200. if (!wp_verify_nonce(BRequest::getVar('user_meta_box_nonce'), basename(__FILE__))) {
  201. return $user_id;
  202. }
  203. // check autosave
  204. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  205. return $user_id;
  206. }
  207. if (is_array($fields))
  208. {
  209. foreach ($fields as $field)
  210. {
  211. if (!isset($_POST[$field['id']])) continue;
  212. $old = get_user_meta($user_id, $field['id'], true);
  213. $new = BRequest::getVar($field['id'],"");
  214. if ($new && $new != $old)
  215. {
  216. //if ($field['type'] == "address") save_latitude_and_longitude($post_id,$new);
  217. update_user_meta($user_id, $field['id'], $new);
  218. }
  219. elseif ('' == $new && $old)
  220. {
  221. delete_user_meta($user_id, $field['id'], $old);
  222. }
  223. }
  224. return true;
  225. }
  226. }
  227. /**
  228. * Sets a new custom user field
  229. *
  230. * @param array $args
  231. */
  232. function add_custom_user_field( $user_type = 'subscriber', $args = null )
  233. {
  234. static $fields;
  235. if (!isset($fields))
  236. {
  237. $fields = array();
  238. }
  239. if (is_null($args)) return $fields;
  240. $defaults = array(
  241. 'name' => 'example',
  242. 'desc' => '',
  243. 'id' => 'example',
  244. 'type' => 'text',
  245. 'std' => ''
  246. );
  247. $args = wp_parse_args( $args, $defaults );
  248. $fields[$user_type][$args['id']] = $args;
  249. return true;
  250. }
  251. /**
  252. * Register all of the user types
  253. *
  254. * @param array $user_types
  255. */
  256. function register_user_types( $user_types )
  257. {
  258. //reasons to fail
  259. if (!is_array($user_types)) return false;
  260. foreach ($user_types as $user_type)
  261. {
  262. register_user_type( $user_type );
  263. }
  264. return true;
  265. }
  266. /**
  267. * Register the user Type
  268. *
  269. * @param array $user_type
  270. */
  271. function register_user_type( $user_type = null )
  272. {
  273. //initializing variables
  274. static $user_types;
  275. $default = array(
  276. 'role' => get_option('default_role'),
  277. 'name' => ucfirst(get_option('default_role')),
  278. 'registration' => false,
  279. 'user_meta' => false,
  280. );
  281. if (!isset($user_types))
  282. {
  283. $user_types = array();
  284. }
  285. if (is_null($user_type)) return $user_types;
  286. $user_type = wp_parse_args($user_type, $default);
  287. //set the registration page if we have one
  288. if ($user_type['registration'])
  289. {
  290. $user_type['registration']['role'] = $user_type['role'];
  291. $user_type['registration']['name'] = $user_type['name'];
  292. registration_page( $user_type['registration'] );
  293. }
  294. if ($user_type['user_meta'])
  295. {
  296. register_user_metas($user_type['role'], $user_type['user_meta']);
  297. }
  298. $user_types[$user_type['role']] = $user_type;
  299. return true;
  300. }
  301. /**
  302. * Get and return all of the custom user fields
  303. *
  304. * @return array
  305. */
  306. function register_user_metas( $user_type = 'subscriber', $fields = null )
  307. {
  308. if (is_null($fields)) return false;
  309. foreach ($fields as $field)
  310. {
  311. add_custom_user_field( $user_type, $field );
  312. }
  313. return true;
  314. }
  315. /**
  316. * Register a bunch of pages
  317. *
  318. * @param unknown_type $pages
  319. */
  320. function registration_pages( $pages )
  321. {
  322. //reasons to fail
  323. if (!is_array($pages)) return false;
  324. foreach ($pages as $page)
  325. {
  326. registration_page( $page );
  327. }
  328. return true;
  329. }
  330. /**
  331. * Register a single page
  332. *
  333. * @param unknown_type $page
  334. * @return array
  335. * @since 1.2
  336. */
  337. function registration_page( $page = null )
  338. {
  339. //initializing variables
  340. static $pages;
  341. $default = array(
  342. 'role' => get_option('default_role'),
  343. 'name' => ucfirst(get_option('default_role')),
  344. 'redirect_to' => get_bloginfo('url').'/profile/',
  345. 'fields' => array('user_login','user_email'),
  346. 'force_login' => false
  347. );
  348. if (!isset($pages))
  349. {
  350. $pages = array();
  351. $pages[$default['role']] = $default;
  352. }
  353. if (is_null($page)) return $pages;
  354. $page = wp_parse_args($page, $default);
  355. $pages[$page['role']] = $page;
  356. return true;
  357. }
  358. /**
  359. * Get's the display names for the given user
  360. *
  361. * @return array
  362. */
  363. function get_display_names( $user_id = null )
  364. {
  365. $user =& get_user( $user_id );
  366. $displays = array(
  367. BRequest::getVar('user_login', $user->user_login),
  368. trim(BRequest::getVar('first_name', $user->first_name).' '.
  369. BRequest::getVar('last_name', $user->last_name)),
  370. BRequest::getVar('nickname', $user->nickname)
  371. );
  372. return $displays;
  373. }
  374. /**
  375. * Get the Roles Array
  376. *
  377. * This will return an array of user roles
  378. *
  379. * @param $author_id
  380. * @param $post_type
  381. * @return array
  382. */
  383. function get_roles_array()
  384. {
  385. global $wpdb, $wp_roles;
  386. $user =& get_user();
  387. $roles = array();
  388. $continue = true;
  389. $capabilities = $user->{$wpdb->prefix . 'capabilities'};
  390. if (!is_array($capabilities) && !is_object($capabilities)) return false;
  391. if ( !isset( $wp_roles ) )
  392. $wp_roles = new WP_Roles();
  393. foreach ( $wp_roles->role_names as $role => $name ) :
  394. if ( array_key_exists( $role, $capabilities ) )
  395. {
  396. $continue = false;
  397. }
  398. if ($continue) continue;
  399. $roles[$role] = $name;
  400. endforeach;
  401. return $roles;
  402. }
  403. /**
  404. * Count the posts
  405. *
  406. * @param $author_id
  407. * @param $post_type
  408. * @return array
  409. */
  410. function get_user_role( $user_id = null )
  411. {
  412. global $wpdb, $wp_roles;
  413. $user =& get_user();
  414. if (is_null($user_id))
  415. {
  416. $user_id = $user->ID;
  417. }
  418. $user = get_userdata( $user_id );
  419. $capabilities = $user->{$wpdb->prefix . 'capabilities'};
  420. if (!is_array($capabilities) && !is_object($capabilities)) return false;
  421. if ( !isset( $wp_roles ) )
  422. $wp_roles = new WP_Roles();
  423. foreach ( $wp_roles->role_names as $role => $name ) :
  424. if ( array_key_exists( $role, $capabilities ) )
  425. break;
  426. endforeach;
  427. return $role;
  428. }
  429. /**
  430. * Get the current user
  431. *
  432. * Function is responsible for creating and returning the user object
  433. *
  434. * @since 1.0
  435. * @param $userid
  436. * @return global object reference
  437. */
  438. function &get_user( $userid = null )
  439. {
  440. //initializing variables
  441. static $users;
  442. if (is_null($users))
  443. {
  444. $users = array();
  445. }
  446. //loading library
  447. require_once ABSPATH . WPINC . DS . 'pluggable.php';
  448. //if we want the logged in user
  449. if (is_null($userid))
  450. {
  451. if ( !$user = wp_validate_auth_cookie() )
  452. {
  453. if ( is_admin()
  454. || empty($_COOKIE[LOGGED_IN_COOKIE])
  455. || !$user = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in') )
  456. {
  457. $userid = 0;
  458. }
  459. }
  460. $userid = $user;
  461. }
  462. //if we're wanting to standardize the userid
  463. if (is_object($userid) && isset($userid->ID))
  464. {
  465. $userid = $userid->ID;
  466. }
  467. if (!isset($users[$userid]))
  468. {
  469. $user = new WP_User( $userid );
  470. $users[$userid] =& $user;
  471. }
  472. return $users[$userid];
  473. }
  474. /**
  475. * Get the users profile link
  476. *
  477. * @param unknown_type $user_id
  478. * @return string
  479. * @since 1.2
  480. */
  481. function get_profile_url( $user_id = null )
  482. {
  483. //initializing variables
  484. $user =& get_user($user_id);
  485. $link = get_bloginfo('url').'/profile/?user_id='.$user->ID;
  486. return $link;
  487. }
  488. /**
  489. * Count the posts
  490. *
  491. * @param $author_id
  492. * @param $post_type
  493. * @return array
  494. */
  495. function get_users_by_role( $search_term = '', $page = '', $role = "Author" )
  496. {
  497. require_once ABSPATH."/wp-admin/includes/user.php";
  498. $wp_user_search = new WP_User_Search($search_term, $page, $role);
  499. return $wp_user_search->get_results();
  500. }
  501. /**
  502. * Get and return all of the custom user fields
  503. *
  504. * @return array
  505. */
  506. function get_custom_user_fields( $user_type = 'subscriber' )
  507. {
  508. //initializing variables
  509. $fields = add_custom_user_field();
  510. if (!isset($fields[$user_type])) return false;
  511. return $fields[$user_type];
  512. }
  513. /**
  514. * Get the registered pages
  515. *
  516. * @return array
  517. * @since 1.2
  518. */
  519. function get_registration_pages()
  520. {
  521. return registration_page();
  522. }
  523. /**
  524. * Get the registration fields
  525. *
  526. * @return array
  527. */
  528. function get_registration_fields( $user_type )
  529. {
  530. //initializing variables
  531. $pages = get_registration_pages();
  532. $page = $pages[$user_type];
  533. $defaults = get_default_profile_fields();
  534. $field_ids = $page['fields'];
  535. $fields = get_custom_user_fields( $user_type );
  536. $fields = wp_parse_args( $fields, $defaults );
  537. //verifying that we have what we need.
  538. if (!in_array('user_login', $field_ids)) $field_ids[] = 'user_login';
  539. if (!in_array('user_email', $field_ids)) $field_ids[] = 'user_email';
  540. $registration_fields = array();
  541. foreach ($field_ids as $id)
  542. {
  543. $registration_fields[] = $fields[$id];
  544. }
  545. return $registration_fields;
  546. }
  547. /**
  548. * Get the registration details
  549. *
  550. * @return array
  551. * @since 1.2
  552. */
  553. function get_registration_page()
  554. {
  555. $pages = get_registration_pages();
  556. $role = BRequest::getVar('user_type');
  557. $page = $pages[$role];
  558. return $page;
  559. }
  560. /**
  561. * Contains all of the default user fields
  562. *
  563. * @return array
  564. */
  565. function get_default_profile_fields()
  566. {
  567. return array(
  568. 'rich_editing' => array(
  569. 'name' => 'Visual Editor',
  570. 'desc' => 'Disable the visual editor when writing',
  571. 'id' => 'rich_editing',
  572. 'type' => 'checkbox',
  573. 'std' => ''
  574. ),
  575. 'comment_shortcuts' => array(
  576. 'name' => 'Keyboard Shortcuts',
  577. 'desc' => 'Enable keyboard shortcuts for comment moderation. <a href="http://codex.wordpress.org/Keyboard_Shortcuts">More information</a>',
  578. 'id' => 'comment_shortcuts',
  579. 'type' => 'checkbox',
  580. 'std' => ''
  581. ),
  582. 'user_login' => array(
  583. 'name' => 'Username',
  584. 'desc' => 'Usernames cannot be changed.',
  585. 'id' => 'user_login',
  586. 'type' => 'text',
  587. 'std' => ''
  588. ),
  589. 'role' => array(
  590. 'name' => 'Role',
  591. 'desc' => 'Disable the visual editor when writing',
  592. 'id' => 'role',
  593. 'type' => 'select',
  594. 'options' => create_function('', "return get_roles_array();")
  595. ),
  596. 'first_name' => array(
  597. 'name' => 'First Name',
  598. 'desc' => '',
  599. 'id' => 'first_name',
  600. 'type' => 'text',
  601. 'std' => ''
  602. ),
  603. 'last_name' => array(
  604. 'name' => 'Last Name',
  605. 'desc' => '',
  606. 'id' => 'last_name',
  607. 'type' => 'text',
  608. 'std' => ''
  609. ),
  610. 'nickname' => array(
  611. 'name' => 'Nickname',
  612. 'desc' => '',
  613. 'id' => 'nickname',
  614. 'type' => 'text',
  615. 'std' => ''
  616. ),
  617. 'display_name' => array(
  618. 'name' => 'Display name publicly as',
  619. 'desc' => '',
  620. 'id' => 'display_name',
  621. 'type' => 'select',
  622. 'options' => create_function('', "return get_display_names( BRequest::getVar( 'user_id' ));")
  623. ),
  624. 'user_email' => array(
  625. 'name' => 'E-mail',
  626. 'desc' => '',
  627. 'id' => 'user_email',
  628. 'type' => 'email',
  629. 'std' => ''
  630. ),
  631. 'url' => array(
  632. 'name' => 'Website',
  633. 'desc' => '',
  634. 'id' => 'url',
  635. 'type' => 'text',
  636. 'std' => ''
  637. ),
  638. 'aim' => array(
  639. 'name' => 'AIM',
  640. 'desc' => '',
  641. 'id' => 'aim',
  642. 'type' => 'text',
  643. 'std' => ''
  644. ),
  645. 'yim' => array(
  646. 'name' => 'Yahoo IM',
  647. 'desc' => '',
  648. 'id' => 'yim',
  649. 'type' => 'text',
  650. 'std' => ''
  651. ),
  652. 'jabber' => array(
  653. 'name' => 'Jabber / Google Talk',
  654. 'desc' => '',
  655. 'id' => 'jabber',
  656. 'type' => 'text',
  657. 'std' => ''
  658. ),
  659. 'description' => array(
  660. 'name' => 'Biographical Info',
  661. 'desc' => 'Share a little biographical information to fill out your profile. This may be shown publicly.',
  662. 'id' => 'description',
  663. 'type' => 'textarea',
  664. 'std' => ''
  665. ),
  666. 'password' => array(
  667. 'name' => 'New Password',
  668. 'desc' => '',
  669. 'id' => 'password',
  670. 'type' => 'password',
  671. 'std' => ''
  672. ),
  673. );
  674. }
  675. /**
  676. * Checks to see if we have any custom user meta fields
  677. *
  678. * @return boolean
  679. */
  680. function has_custom_user_fields()
  681. {
  682. $fields = get_custom_user_fields();
  683. if (empty($fields)) return false;
  684. return true;
  685. }
  686. /**
  687. * Display the user type links
  688. *
  689. * @return boolean
  690. * @since 1.2
  691. */
  692. function display_user_type_links()
  693. {
  694. if (is_user_type()) return false;
  695. $pages = get_registration_pages();
  696. echo '<ul class="registration_types">';
  697. foreach ($pages as $page)
  698. {
  699. echo "<li><a href='?user_type={$page['role']}'><span>Register as a </span>{$page['name']}</a></li>";
  700. }
  701. echo '</ul>';
  702. return true;
  703. }
  704. /**
  705. * Displays all of the user profile fields.
  706. *
  707. * @param unknown_type $userid
  708. */
  709. function display_profile_fields()
  710. {
  711. //initializing variables
  712. $user =& get_user();
  713. $fields = get_custom_user_fields( $user->roles[0] );
  714. $defaults = get_default_profile_fields();
  715. $fields = wp_parse_args( $fields, $defaults );
  716. display_custom_user_fields($user, $fields);
  717. }
  718. /**
  719. * Display the user edit fields
  720. *
  721. * @param unknown_type $user
  722. */
  723. function display_custom_user_fields($user = null, $fields = null)
  724. {
  725. //initializing variables
  726. if (!is_null($user)) $user = get_userdata($user->ID);
  727. $is_administration = false;
  728. if (is_null($fields)) $is_administration = true;
  729. if ($is_administration)
  730. {
  731. echo "<style>",
  732. ".field_wrapper label {display:block;position:relative;float:left;width:220px;}",
  733. ".typetext input {width: 25em;}",
  734. ".typecheckbox input {margin-right:200px;position:relative;float:left;}",
  735. ".field_wrapper span {display:block;padding-left:220px;}",
  736. ".field_wrapper {padding: 10px;}",
  737. ".typetextarea textarea {width: 500px;}",
  738. ".field_wrapper .profile_description{font-family: 'Lucida Grande', Verdana, Arial, 'Bitstream Vera Sans', sans-serif;font-size: 12px;font-style: italic;color: #666;}",
  739. "</style>",
  740. "<h3>Additional Details</h3>";
  741. //initializing variables
  742. $currentUser = new WP_User( $user->ID );
  743. $fields = array();
  744. foreach ($currentUser->roles as $role)
  745. {
  746. $fields = wp_parse_args($fields, get_custom_user_fields( $role ));
  747. }
  748. }
  749. //reasons to fail
  750. if (empty($fields)) return false;
  751. // Use nonce for verification
  752. echo '<div class="nonce_wrapper"><input type="hidden" name="user_meta_box_nonce" value="',
  753. wp_create_nonce(basename(__FILE__)), '" /></div>',
  754. '<input type="hidden" name="user_type" value="',BRequest::getVar('user_type'),'" />';
  755. foreach ($fields as $field)
  756. {
  757. if (!current_user_can('edit_users') && $field['id'] == 'role')
  758. {
  759. continue;
  760. }
  761. // get current post meta data
  762. $unique = md5(microtime());
  763. if (!is_null($user) && isset($user->{$field['id']}))
  764. {
  765. $meta = $user->{$field['id']};
  766. }
  767. elseif(!is_null($user))
  768. {
  769. $meta = get_user_meta($user->ID, $field['id'], true);
  770. }
  771. else
  772. {
  773. $meta = BRequest::getVar($field['id'], '');
  774. }
  775. echo '<div class="field_wrapper div', $field['id'], ' type',$field['type'],'">';
  776. if ($field['type'] != 'password') echo '<label for="', $field['id'], '">', $field['name'], '</label>';
  777. switch ($field['type'])
  778. {
  779. case 'password':
  780. echo
  781. '<label for="', $field['id'], '">', $field['name'], '</label>',
  782. '<input type="password" name="pass1" id="pass1" size="16" value="" autocomplete="off">',
  783. '<span class="description">If you would like to change the password type a new one. Otherwise leave this blank.</span><br>',
  784. '<input type="password" name="pass2" id="pass2" size="16" value="" autocomplete="off">',
  785. '<span class="description">Type your new password again.</span><br>',
  786. '<div id="pass-strength-result">Strength indicator</div>',
  787. '<p class="description indicator-hint">Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &amp; ).</p>',
  788. '<script type="text/javascript"> /* <![CDATA[ */
  789. var pwsL10n = {
  790. empty: "Strength indicator",
  791. short: "Very weak",
  792. bad: "Weak",
  793. good: "Medium",
  794. strong: "Strong",
  795. mismatch: "Mismatch"
  796. };
  797. try{convertEntities(pwsL10n);}catch(e){};
  798. /* ]]> */
  799. </script>',
  800. '<script type="text/javascript" src="',get_bloginfo('url'),'/wp-admin/load-scripts.php?c=1&load=jquery,hoverIntent,common,jquery-color,user-profile,password-strength-meter"></script>';
  801. break;
  802. case 'address':
  803. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', "\n",
  804. '<span class="profile_description">',$field['desc'],'</span>';
  805. break;
  806. case 'email':
  807. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" class="regular-text" />', "\n";
  808. echo '<input type="text" name="', $field['id'], '1" id="', $field['id'], '" value="', ($default = "Please confirm your email"),
  809. '" class="regular-text" onBlur="if (this.value == \'\') this.value = \'',$default,'\';" onFocus="if (this.value == \'',$default,'\') this.value = \'\';" />', "\n",
  810. '<span class="profile_description">',$field['desc'],'</span>';
  811. break;
  812. case 'text':
  813. $disabled = '';
  814. if (is_user_logged_in() && $field['id'] == 'user_login') $disabled = 'readonly="true"';
  815. echo '<input ',$disabled,' type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" class="regular-text" />', "\n",
  816. '<span class="profile_description">', $field['desc'], '</span>';
  817. break;
  818. case 'textarea':
  819. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="30" rows="5">', $meta ? $meta : $field['std'], '</textarea>', "\n",
  820. '<span class="profile_description">', $field['desc'], '</span>';
  821. break;
  822. case 'select':
  823. echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  824. if (!is_array($field['options']))
  825. {
  826. $field['options'] = $field['options']();
  827. }
  828. foreach ($field['options'] as $key => $option)
  829. {
  830. if (is_int($key)) $key = $option;
  831. echo '<option ', $meta == $option ? ' selected="selected"' : '',
  832. ' value="',$key,'">', $option, '</option>';
  833. }
  834. echo '</select>';
  835. break;
  836. case 'radio':
  837. foreach ($field['options'] as $option)
  838. {
  839. echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  840. }
  841. echo '<br/>',$field['desc'];
  842. break;
  843. case 'checkbox':
  844. echo '<input type="hidden" name="', $field['id'], '" value="" /> ';
  845. echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', ($meta && $meta != 'false') ? ' checked="checked"' : '', ' />',
  846. '<span class="profile_description">', $field['desc'], '</span>';
  847. break;
  848. case 'editor':
  849. echo
  850. '<div style="border:1px solid #DFDFDF;border-collapse: separate;border-top-left-radius: 6px 6px;border-top-right-radius: 6px 6px;">',
  851. '<textarea rows="10" class="theEditor" cols="40" name="', $field['id'], '" id="'.$unique.'"></textarea>',
  852. '</div>',
  853. '<script type="text/javascript">edCanvas = document.getElementById(\''.$unique.'\');</script>', "\n", $field['desc'];
  854. break;
  855. }
  856. echo '</div>';
  857. }
  858. }
  859. /**
  860. * Prints the users profile link
  861. *
  862. * @param unknown_type $user_id
  863. * @return null
  864. * @since 1.2
  865. */
  866. function profile_url( $user_id = null )
  867. {
  868. echo get_profile_url($user_id);
  869. }
  870. /**
  871. * Checks to see if there's a user type
  872. *
  873. */
  874. function is_user_type()
  875. {
  876. if ($type = BRequest::getVar('user_type',false))
  877. return $type;
  878. return false;
  879. }
  880. /**
  881. * Checks to see if the logged in user is the post owner
  882. *
  883. * @return unknown
  884. */
  885. function is_post_owner()
  886. {
  887. //initializing variables
  888. global $authordata;
  889. $user =& get_user();
  890. if (!is_object($user)) return false;
  891. if (!is_object($authordata)) return false;
  892. if ($authordata->ID != $user->ID) return false;
  893. return true;
  894. }
  895. /**
  896. * Count the posts
  897. *
  898. * @param $author_id
  899. * @param $post_type
  900. * @return array
  901. */
  902. function user_is( $role = null )
  903. {
  904. if (strtolower($role) != strtolower(get_user_role(null, true))) return false;
  905. return true;
  906. }
  907. endif;