← All changes
|
application/modules/wordpress/controllers/admin/users.php
+178
-141
3.2.4
→
2.2.3
View file →
| @@ -1,47 +1,10 @@ | ||
| 1 | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | 2 | |
| 3 | -class Users_Admin_Wordpress_HC_controller extends _Backend_HC_controller | |
| 3 | +class Wordpress_Users_controller extends Backend_controller | |
| 4 | 4 | { |
| 5 | - var $form = NULL; | |
| 6 | - | |
| 7 | - function __construct() | |
| 8 | - { | |
| 9 | - parent::__construct( USER_HC_MODEL::LEVEL_ADMIN ); | |
| 10 | - | |
| 11 | - $this->form = HC_Lib::form() | |
| 12 | - ; | |
| 13 | - | |
| 14 | - $defaults = array(); | |
| 15 | - $app_conf = HC_App::app_conf(); | |
| 16 | - $wum = HC_App::model('wordpress_user'); | |
| 17 | - $wordpress_roles = $wum->wp_roles(); | |
| 18 | - | |
| 19 | - foreach( $wordpress_roles as $role_value => $role_name ){ | |
| 20 | - $field_name = 'role_' . $role_value; | |
| 21 | - $this->form | |
| 22 | - ->set_input( $field_name, 'dropdown' ) | |
| 23 | - ; | |
| 24 | - $default = $app_conf->get( 'wordpress_' . $field_name ); | |
| 25 | - $defaults[ $field_name ] = $default; | |
| 26 | - } | |
| 27 | - | |
| 28 | - $this->form | |
| 29 | - ->set_input( 'append_role_name', 'checkbox' ) | |
| 30 | - ; | |
| 31 | - $this->form->set_values( $defaults ); | |
| 32 | - } | |
| 33 | - | |
| 34 | 5 | function edit( $id ) |
| 35 | 6 | { |
| 36 | - $args = func_get_args(); | |
| 37 | - $args = hc_parse_args( $args, TRUE ); | |
| 38 | - $id = isset($args['user']) ? $args['user'] : NULL; | |
| 39 | - | |
| 40 | - if( is_object($id) ){ | |
| 41 | - $id = $id->id; | |
| 42 | - } | |
| 43 | - | |
| 44 | 7 | // redirect to WP admin user edit |
| 45 | 8 | $link = get_edit_user_link( $id ); |
| 46 | 9 | $this->redirect( $link ); |
| 47 | 10 | exit; |
| @@ -46,17 +9,8 @@ | ||
| 46 | 9 | $this->redirect( $link ); |
| 47 | 10 | exit; |
| 48 | 11 | } |
| 49 | 12 | |
| 50 | - function edit_menubar( $user ) | |
| 51 | - { | |
| 52 | - return $this->render( | |
| 53 | - 'wordpress/admin/users/edit_menubar', | |
| 54 | - array( | |
| 55 | - ) | |
| 56 | - ); | |
| 57 | - } | |
| 58 | - | |
| 59 | 13 | function add() |
| 60 | 14 | { |
| 61 | 15 | // redirect to WP admin user add |
| 62 | 16 | $link = admin_url( 'user-new.php' ); |
| @@ -63,137 +17,220 @@ | ||
| 63 | 17 | $this->redirect( $link ); |
| 64 | 18 | exit; |
| 65 | 19 | } |
| 66 | 20 | |
| 67 | - function add_menubar() | |
| 68 | - { | |
| 69 | - return $this->render( | |
| 70 | - 'wordpress/admin/users/add_menubar', | |
| 71 | - array( | |
| 72 | - ) | |
| 73 | - ); | |
| 74 | - } | |
| 75 | - | |
| 76 | - function sync_menubar() | |
| 77 | - { | |
| 78 | - return $this->render( | |
| 79 | - 'wordpress/admin/users/sync_menubar', | |
| 80 | - array( | |
| 81 | - ) | |
| 82 | - ); | |
| 83 | - } | |
| 84 | 21 | function sync() |
| 85 | 22 | { |
| 86 | - $app_title = $this->config->item('nts_app_title'); | |
| 23 | + $wum = new Wordpress_User_Model; | |
| 24 | + $wordpress_roles = $wum->wp_roles(); | |
| 25 | + $this->data['wordpress_roles'] = $wordpress_roles; | |
| 26 | + $this->data['wordpress_count_users'] = count_users(); | |
| 87 | 27 | |
| 88 | - $wum = HC_App::model('wordpress_user'); | |
| 89 | - $wordpress_roles = $wum->wp_roles(); | |
| 90 | - $wordpress_count_users = count_users(); | |
| 91 | - $is_setup = 0; | |
| 28 | + foreach( $wordpress_roles as $role_value => $role_name ) | |
| 29 | + { | |
| 30 | + $field_name = 'role_' . $role_value; | |
| 31 | + $default = $this->app_conf->get( 'wordpress_' . $field_name ); | |
| 32 | + $defaults[ $field_name ] = $default; | |
| 33 | + } | |
| 34 | + $this->hc_form->set_defaults( $defaults ); | |
| 92 | 35 | |
| 93 | - $this->layout->set_partial( | |
| 94 | - 'content', | |
| 95 | - $this->render( | |
| 96 | - 'wordpress/admin/users/sync', | |
| 97 | - array( | |
| 98 | - 'post_to' => 'wordpress/admin/users/syncrun', | |
| 99 | - 'app_title' => $app_title, | |
| 100 | - 'form' => $this->form, | |
| 101 | - 'wordpress_roles' => $wordpress_roles, | |
| 102 | - 'wordpress_count_users' => $wordpress_count_users, | |
| 103 | - 'is_setup' => $is_setup, | |
| 104 | - ) | |
| 105 | - ) | |
| 106 | - ); | |
| 107 | - $this->layout(); | |
| 36 | + $this->set_include( 'sync', 'wordpress/admin/users' ); | |
| 37 | + $this->load->view( $this->template, $this->data); | |
| 108 | 38 | } |
| 109 | 39 | |
| 110 | 40 | function syncrun() |
| 111 | 41 | { |
| 112 | - $validator = new HC_Validator; | |
| 113 | - $wum = HC_App::model('wordpress_user'); | |
| 42 | + $fields = array(); | |
| 43 | + $validation = array(); | |
| 44 | + | |
| 45 | + $wum = new Wordpress_User_Model; | |
| 114 | 46 | $wordpress_roles = $wum->wp_roles(); |
| 115 | - $wordpress_count_users = count_users(); | |
| 47 | + foreach( $wordpress_roles as $role_value => $role_name ) | |
| 48 | + { | |
| 49 | + $field_name = 'role_' . $role_value; | |
| 116 | 50 | |
| 117 | - foreach( $wordpress_roles as $role_value => $role_name ){ | |
| 118 | - $field_name = 'role_' . $role_value; | |
| 119 | - $validator->set_rules( $field_name, 'trim|required' ); | |
| 51 | + $fields[] = $field_name; | |
| 52 | + $validation[] = array( | |
| 53 | + 'field' => $field_name, | |
| 54 | + 'label' => $role_name, | |
| 55 | + 'rules' => 'trim|required' | |
| 56 | + ); | |
| 120 | 57 | } |
| 58 | + $this->form_validation->set_rules( $validation ); | |
| 121 | 59 | |
| 122 | - $post = $this->input->post(); | |
| 123 | - $this->form->grab( $post ); | |
| 124 | - $values = $this->form->values(); | |
| 60 | + if( $this->input->post() ) | |
| 61 | + { | |
| 62 | + $post = array(); | |
| 63 | + reset( $fields ); | |
| 64 | + foreach( $fields as $f ) | |
| 65 | + { | |
| 66 | + $post[$f] = $this->input->post($f); | |
| 67 | + } | |
| 68 | + $this->hc_form->set_defaults( $post ); | |
| 125 | 69 | |
| 126 | - if( $post ){ | |
| 127 | - if( $validator->run($values) == FALSE ){ | |
| 128 | - $errors = $validator->error(); | |
| 70 | + if( $this->form_validation->run() == FALSE ) | |
| 71 | + { | |
| 72 | + $errors = array(); | |
| 73 | + reset( $fields ); | |
| 74 | + foreach( $fields as $f ) | |
| 75 | + { | |
| 76 | + $errors[$f] = form_error($f); | |
| 77 | + } | |
| 78 | + $this->hc_form->set_errors( $errors ); | |
| 79 | + } | |
| 80 | + else | |
| 81 | + { | |
| 82 | + $append_role_name = $this->input->post('append_role_name'); | |
| 129 | 83 | |
| 130 | - $this->form->set_values( $values ); | |
| 131 | - $this->form->set_errors( $errors ); | |
| 84 | + /* save settings */ | |
| 85 | + reset( $post ); | |
| 86 | + foreach( $post as $k => $v ) | |
| 87 | + { | |
| 88 | + $this->app_conf->set( 'wordpress_' . $k, $v ); | |
| 89 | + } | |
| 132 | 90 | |
| 133 | - /* render view */ | |
| 134 | - return $this->sync(); | |
| 135 | - } | |
| 136 | - else { | |
| 137 | - $app_conf = HC_App::app_conf(); | |
| 138 | 91 | $setup_ok = TRUE; |
| 139 | 92 | |
| 140 | - $append_role_name = $values['append_role_name']; | |
| 93 | + /* users */ | |
| 94 | + $count_users = 0; | |
| 95 | + $processed_users = array(); | |
| 141 | 96 | |
| 142 | - /* save settings */ | |
| 143 | - reset( $values ); | |
| 144 | - foreach( $values as $k => $v ){ | |
| 145 | - $app_conf->set( 'wordpress_' . $k, $v ); | |
| 97 | + /* this user */ | |
| 98 | + $current_user = wp_get_current_user(); | |
| 99 | + $user = new User_Model; | |
| 100 | + $user->get_by_id( $current_user->ID ); | |
| 101 | + | |
| 102 | + $user->email = $current_user->user_email; | |
| 103 | + if( $current_user->user_firstname ) | |
| 104 | + { | |
| 105 | + $user->first_name = $current_user->user_firstname; | |
| 106 | + $user->last_name = $current_user->user_lastname; | |
| 146 | 107 | } |
| 108 | + else | |
| 109 | + { | |
| 110 | + $user->first_name = $current_user->display_name; | |
| 111 | + } | |
| 147 | 112 | |
| 148 | - $um = HC_App::model('user'); | |
| 113 | + if( $append_role_name ) | |
| 114 | + { | |
| 115 | + $wp_role = ( $current_user->roles && is_array($current_user->roles) && isset($current_user->roles[0]) ) ? $current_user->roles[0] : ''; | |
| 116 | + if( strlen($wp_role) ) | |
| 117 | + $user->first_name = '[' . $wp_role . '] ' . $user->first_name; | |
| 118 | + } | |
| 119 | + $user->level = USER_MODEL::LEVEL_ADMIN; | |
| 149 | 120 | |
| 150 | - /* all users */ | |
| 151 | - $result = $wum->sync_all( $values ); | |
| 152 | - if( $result !== TRUE ){ | |
| 153 | - $msg = array(); | |
| 154 | - foreach( $result as $uid => $user_result ){ | |
| 155 | - foreach( $user_result as $k => $v ){ | |
| 156 | - $msg[] = 'User ID=' . $uid . ': ' . $k . ': ' . $v; | |
| 157 | - } | |
| 158 | - } | |
| 159 | - $msg = join( '<br>', $msg ); | |
| 160 | - $this->session->set_flashdata( 'error', $msg ); | |
| 121 | + if( $user->save() ) | |
| 122 | + { | |
| 123 | + $processed_users[] = $user->id; | |
| 124 | + $count_users++; | |
| 125 | + } | |
| 126 | + else | |
| 127 | + { | |
| 161 | 128 | $setup_ok = FALSE; |
| 129 | + | |
| 130 | + $err_msg = array(); | |
| 131 | + $err_msg[] = $current_user->user_email; | |
| 132 | + $err_msg = array_merge($err_msg, array_values($user->error->all) ); | |
| 133 | + $this->session->set_flashdata( 'error', $err_msg ); | |
| 134 | + | |
| 135 | + ci_redirect( 'admin/users' ); | |
| 136 | + return; | |
| 162 | 137 | } |
| 163 | 138 | |
| 164 | - $success_count = $wum->get_last_count('success'); | |
| 165 | - $archived_count = $wum->get_last_count('archived'); | |
| 139 | + if( $setup_ok ) | |
| 140 | + { | |
| 141 | + /* now by roles */ | |
| 142 | + reset( $wordpress_roles ); | |
| 143 | + foreach( $wordpress_roles as $role_value => $role_name ) | |
| 144 | + { | |
| 145 | + $our_level = $post['role_' . $role_value]; | |
| 146 | + if( ! $our_level ) | |
| 147 | + continue; | |
| 166 | 148 | |
| 167 | - /* this user */ | |
| 168 | - $current_user = wp_get_current_user(); | |
| 169 | - $result = $wum->sync( $current_user->ID, $um->_const('LEVEL_ADMIN') ); | |
| 170 | - if( $result !== TRUE ){ | |
| 171 | - $msg = array(); | |
| 172 | - foreach( $result as $k => $v ){ | |
| 173 | - $msg[] = $k . ': ' . $v; | |
| 149 | + $args = array( | |
| 150 | + 'role' => $role_value, | |
| 151 | + 'exclude' => $current_user->ID | |
| 152 | + ); | |
| 153 | + $wordpress_users = get_users( $args ); | |
| 154 | + foreach( $wordpress_users as $wuser ) | |
| 155 | + { | |
| 156 | + if( (! $role_value) && $wuser->roles ) | |
| 157 | + { | |
| 158 | + continue; | |
| 159 | + } | |
| 160 | + | |
| 161 | + $user = new User_Model; | |
| 162 | + $user->get_by_id( $wuser->ID ); | |
| 163 | + $is_new = $user->exists() ? FALSE : TRUE; | |
| 164 | + | |
| 165 | + $user->email = $wuser->user_email; | |
| 166 | + $user->id = $wuser->ID; | |
| 167 | + | |
| 168 | + if( $wuser->user_firstname ) | |
| 169 | + { | |
| 170 | + $user->first_name = $wuser->user_firstname; | |
| 171 | + $user->last_name = $wuser->user_lastname; | |
| 172 | + } | |
| 173 | + else | |
| 174 | + { | |
| 175 | + $user->first_name = $wuser->display_name; | |
| 176 | + } | |
| 177 | + | |
| 178 | + if( $append_role_name ) | |
| 179 | + { | |
| 180 | + $wp_role = ( $wuser->roles && is_array($wuser->roles) && isset($wuser->roles[0]) ) ? $wuser->roles[0] : ''; | |
| 181 | + if( strlen($wp_role) ) | |
| 182 | + $user->first_name = '[' . $wp_role . '] ' . $user->first_name; | |
| 183 | + } | |
| 184 | + | |
| 185 | + if( $is_new ) | |
| 186 | + $user->password = hc_random(); | |
| 187 | + $user->level = $our_level; | |
| 188 | + | |
| 189 | + if( | |
| 190 | + ( $is_new && $user->save_as_new() ) | |
| 191 | + OR | |
| 192 | + ( (! $is_new) && $user->save() ) | |
| 193 | + ) | |
| 194 | + { | |
| 195 | + $processed_users[] = $user->id; | |
| 196 | + $count_users++; | |
| 197 | + } | |
| 198 | + else | |
| 199 | + { | |
| 200 | + $setup_ok = FALSE; | |
| 201 | + | |
| 202 | + $err_msg = array(); | |
| 203 | + $err_msg[] = $wuser->user_email; | |
| 204 | + $err_msg = array_merge($err_msg, array_values($user->error->all) ); | |
| 205 | + $this->session->set_flashdata( 'error', $err_msg ); | |
| 206 | + ci_redirect( 'admin/users' ); | |
| 207 | + return; | |
| 208 | + } | |
| 209 | + } | |
| 174 | 210 | } |
| 175 | - $msg = join( '<br>', $msg ); | |
| 176 | - $this->session->set_flashdata( 'error', $msg ); | |
| 177 | - $setup_ok = FALSE; | |
| 211 | + | |
| 212 | + /* those that are deleted in WordPress make archived */ | |
| 213 | + $um = new User_Model; | |
| 214 | + $um->where_not_in( 'id', $processed_users ); | |
| 215 | + $um->update( 'active', USER_MODEL::STATUS_ARCHIVE ); | |
| 216 | + $archived_count = $um->db->affected_rows(); | |
| 178 | 217 | } |
| 179 | - // $success_count++; | |
| 180 | 218 | |
| 181 | - if( $setup_ok ){ | |
| 182 | - $msg = 'Synchronized ' . $success_count . ' '; | |
| 183 | - $msg .= ($success_count_users > 1) ? 'users' : 'user'; | |
| 219 | + if( $setup_ok ) | |
| 220 | + { | |
| 221 | + $msg = 'Synchronized ' . $count_users . ' '; | |
| 222 | + $msg .= ($count_users > 1) ? 'users' : 'user'; | |
| 184 | 223 | |
| 185 | - if( $archived_count ){ | |
| 224 | + if( $archived_count ) | |
| 225 | + { | |
| 186 | 226 | $msg .= '<br>Archived ' . $archived_count . ' '; |
| 187 | 227 | $msg .= ($archived_count > 1) ? 'users' : 'user'; |
| 188 | 228 | } |
| 189 | 229 | |
| 190 | 230 | $this->session->set_flashdata( 'message', $msg ); |
| 191 | - $this->redirect('admin/users'); | |
| 231 | + ci_redirect( 'admin/users' ); | |
| 192 | 232 | return; |
| 193 | - } | |
| 194 | - else { | |
| 195 | - $this->redirect('wordpress/admin/users'); | |
| 196 | 233 | } |
| 197 | 234 | } |
| 198 | 235 | } |
| 199 | 236 | return $this->sync(); |