PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/theme-my-login/modules/themed-profiles/themed-profiles.php

https://gitlab.com/Gashler/sg
PHP | 331 lines | 159 code | 52 blank | 120 comment | 46 complexity | 18951053fceb64c41d33de495f97374c MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin Name: Themed Profiles
  4. * Description: Enabling this module will initialize and enable themed profiles. You will then have to configure the settings via the "Themed Profiles" tab.
  5. *
  6. * Holds Theme My Login Themed Profiles class
  7. *
  8. * @package Theme_My_Login
  9. * @subpackage Theme_My_Login_Themed_Profiles
  10. * @since 6.0
  11. */
  12. if ( ! class_exists( 'Theme_My_Login_Themed_Profiles' ) ) :
  13. /**
  14. * Theme My Login Themed Profiles class
  15. *
  16. * Allows users to edit profile on the front-end.
  17. *
  18. * @since 6.0
  19. */
  20. class Theme_My_Login_Themed_Profiles extends Theme_My_Login_Abstract {
  21. /**
  22. * Holds options key
  23. *
  24. * @since 6.3
  25. * @access protected
  26. * @var string
  27. */
  28. protected $options_key = 'theme_my_login_themed_profiles';
  29. /**
  30. * Returns singleton instance
  31. *
  32. * @since 6.3
  33. * @access public
  34. * @return object
  35. */
  36. public static function get_object( $class = null ) {
  37. return parent::get_object( __CLASS__ );
  38. }
  39. /**
  40. * Returns default options
  41. *
  42. * @since 6.3
  43. * @access public
  44. *
  45. * @return array Default options
  46. */
  47. public static function default_options() {
  48. global $wp_roles;
  49. if ( empty( $wp_roles ) )
  50. $wp_roles = new WP_Roles;
  51. $options = array();
  52. foreach ( $wp_roles->get_names() as $role => $label ) {
  53. if ( 'pending' != $role ) {
  54. $options[$role] = array(
  55. 'theme_profile' => true,
  56. 'restrict_admin' => false
  57. );
  58. }
  59. }
  60. return $options;
  61. }
  62. /**
  63. * Loads the module
  64. *
  65. * @since 6.0
  66. * @access protected
  67. */
  68. protected function load() {
  69. add_action( 'tml_modules_loaded', array( &$this, 'modules_loaded' ) );
  70. add_action( 'init', array( &$this, 'init' ) );
  71. add_action( 'template_redirect', array( &$this, 'template_redirect' ) );
  72. add_filter( 'show_admin_bar', array( &$this, 'show_admin_bar' ) );
  73. add_action( 'tml_request_profile', array( &$this, 'tml_request_profile' ) );
  74. add_action( 'tml_display_profile', array( &$this, 'tml_display_profile' ) );
  75. }
  76. /**
  77. * Adds filters to site_url() and admin_url()
  78. *
  79. * Callback for "tml_modules_loaded" in file "theme-my-login.php"
  80. *
  81. * @since 6.0
  82. * @access public
  83. */
  84. public function modules_loaded() {
  85. add_filter( 'site_url', array( &$this, 'site_url' ), 10, 3 );
  86. add_filter( 'admin_url', array( &$this, 'site_url' ), 10, 2 );
  87. }
  88. /**
  89. * Redirects "profile.php" to themed profile page
  90. *
  91. * Callback for "init" hook
  92. *
  93. * @since 6.0
  94. * @access public
  95. */
  96. public function init() {
  97. global $current_user, $pagenow;
  98. if ( is_user_logged_in() && is_admin() ) {
  99. $redirect_to = Theme_My_Login::get_page_link( 'profile' );
  100. $user_role = reset( $current_user->roles );
  101. if ( is_multisite() && empty( $user_role ) )
  102. $user_role = 'subscriber';
  103. if ( 'profile.php' == $pagenow && ! isset( $_REQUEST['page'] ) ) {
  104. if ( $this->get_option( array( $user_role, 'theme_profile' ) ) ) {
  105. if ( ! empty( $_GET ) )
  106. $redirect_to = add_query_arg( (array) $_GET, $redirect_to );
  107. wp_redirect( $redirect_to );
  108. exit;
  109. }
  110. } else {
  111. if ( $this->get_option( array( $user_role, 'restrict_admin' ) ) ) {
  112. if ( ! defined( 'DOING_AJAX' ) ) {
  113. wp_redirect( $redirect_to );
  114. exit;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Redirects login page to profile if user is logged in
  122. *
  123. * Callback for "template_redirect" hook
  124. *
  125. * @since 6.0
  126. * @access public
  127. */
  128. public function template_redirect() {
  129. $theme_my_login = Theme_My_Login::get_object();
  130. if ( Theme_My_Login::is_tml_page() ) {
  131. switch ( $theme_my_login->request_action ) {
  132. case 'profile' :
  133. // Redirect to login page if not logged in
  134. if ( ! is_user_logged_in() ) {
  135. $redirect_to = Theme_My_Login::get_page_link( 'login', 'reauth=1' );
  136. wp_redirect( $redirect_to );
  137. exit;
  138. }
  139. break;
  140. case 'logout' :
  141. // Allow logout action
  142. break;
  143. case 'register' :
  144. // Allow register action if multisite
  145. if ( is_multisite() )
  146. break;
  147. default :
  148. // Redirect to profile for any other action if logged in
  149. if ( is_user_logged_in() ) {
  150. $redirect_to = Theme_My_Login::get_page_link( 'profile' );
  151. wp_redirect( $redirect_to );
  152. exit;
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * Hides admin bar is admin is restricted
  159. *
  160. * Callback for "show_admin_bar" hook
  161. *
  162. * @since 6.2
  163. * @access public
  164. */
  165. public function show_admin_bar( $show_admin_bar ) {
  166. global $current_user;
  167. $user_role = reset( $current_user->roles );
  168. if ( is_multisite() && empty( $user_role ) )
  169. $user_role = 'subscriber';
  170. if ( $this->get_option( array( $user_role, 'restrict_admin' ) ) )
  171. return false;
  172. return $show_admin_bar;
  173. }
  174. /**
  175. * Handles profile action
  176. *
  177. * Callback for "tml_request_profile" in method Theme_My_Login::the_request()
  178. *
  179. * @see Theme_My_Login::the_request()
  180. * @since 6.0
  181. * @access public
  182. */
  183. public function tml_request_profile() {
  184. require_once( ABSPATH . 'wp-admin/includes/user.php' );
  185. require_once( ABSPATH . 'wp-admin/includes/misc.php' );
  186. define( 'IS_PROFILE_PAGE', true );
  187. load_textdomain( 'default', WP_LANG_DIR . '/admin-' . get_locale() . '.mo' );
  188. register_admin_color_schemes();
  189. wp_enqueue_style( 'password-strength', plugins_url( 'theme-my-login/modules/themed-profiles/themed-profiles.css' ) );
  190. wp_enqueue_script( 'user-profile' );
  191. $current_user = wp_get_current_user();
  192. if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
  193. check_admin_referer( 'update-user_' . $current_user->ID );
  194. if ( ! current_user_can( 'edit_user', $current_user->ID ) )
  195. wp_die( __( 'You do not have permission to edit this user.', 'theme-my-login' ) );
  196. do_action( 'personal_options_update', $current_user->ID );
  197. $errors = edit_user( $current_user->ID );
  198. if ( ! is_wp_error( $errors ) ) {
  199. $args = array( 'updated' => 'true' );
  200. if ( ! empty( $_REQUEST['instance'] ) )
  201. $args['instance'] = $_REQUEST['instance'];
  202. $redirect = add_query_arg( $args );
  203. wp_redirect( $redirect );
  204. exit;
  205. } else {
  206. Theme_My_Login::get_object()->errors = $errors;
  207. }
  208. }
  209. }
  210. /**
  211. * Outputs profile form HTML
  212. *
  213. * Callback for "tml_display_profile" hook in method Theme_My_login_Template::display()
  214. *
  215. * @see Theme_My_Login_Template::display()
  216. * @since 6.0
  217. * @access public
  218. *
  219. * @param object $template Reference to $theme_my_login_template object
  220. */
  221. public function tml_display_profile( &$template ) {
  222. global $current_user, $profileuser, $_wp_admin_css_colors, $wp_version;
  223. require_once( ABSPATH . 'wp-admin/includes/user.php' );
  224. require_once( ABSPATH . 'wp-admin/includes/misc.php' );
  225. if ( isset( $_GET['updated'] ) && 'true' == $_GET['updated'] )
  226. Theme_My_Login::get_object()->errors->add( 'profile_updated', __( 'Profile updated.', 'theme-my-login' ), 'message' );
  227. $current_user = wp_get_current_user();
  228. $profileuser = get_user_to_edit( $current_user->ID );
  229. $user_role = reset( $profileuser->roles );
  230. if ( is_multisite() && empty( $user_role ) )
  231. $user_role = 'subscriber';
  232. $_template = array();
  233. // Allow template override via shortcode or template tag args
  234. if ( ! empty( $template->options['profile_template'] ) )
  235. $_template[] = $template->options['profile_template'];
  236. // Allow role template overrid via shortcode or template tag args
  237. if ( ! empty( $template->options["profile_template_{$user_role}"] ) )
  238. $_template[] = $template->options["profile_template_{$user_role}"];
  239. // Role template
  240. $_template[] = "profile-form-{$user_role}.php";
  241. // Default template
  242. $_template[] = 'profile-form.php';
  243. // Load template
  244. $template->get_template( $_template, true, compact( 'current_user', 'profileuser', 'user_role', '_wp_admin_css_colors', 'wp_version' ) );
  245. }
  246. /**
  247. * Changes links from "profile.php" to themed profile page
  248. *
  249. * Callback for "site_url" hook
  250. *
  251. * @see site_url()
  252. * @since 6.0
  253. * @access public
  254. *
  255. * @param string $url The generated link
  256. * @param string $path The specified path
  257. * @param string $orig_scheme The original connection scheme
  258. * @return string The filtered link
  259. */
  260. public function site_url( $url, $path, $orig_scheme = '' ) {
  261. global $current_user, $pagenow;
  262. if ( 'profile.php' != $pagenow && strpos( $url, 'profile.php' ) !== false ) {
  263. $user_role = reset( $current_user->roles );
  264. if ( is_multisite() && empty( $user_role ) )
  265. $user_role = 'subscriber';
  266. if ( $user_role && ! $this->get_option( array( $user_role, 'theme_profile' ) ) )
  267. return $url;
  268. $parsed_url = parse_url( $url );
  269. $url = Theme_My_Login::get_page_link( 'profile' );
  270. if ( isset( $parsed_url['query'] ) )
  271. $url = add_query_arg( array_map( 'rawurlencode', wp_parse_args( $parsed_url['query'] ) ), $url );
  272. }
  273. return $url;
  274. }
  275. }
  276. Theme_My_Login_Themed_Profiles::get_object();
  277. endif;
  278. if ( is_admin() )
  279. include_once( dirname( __FILE__ ) . '/admin/themed-profiles-admin.php' );