Plugin Directory

source: salon-booking-system/trunk/src/SLN/Admin/Customers.php @ 2779160

Last change on this file since 2779160 was 2779160, checked in by wordpresschef, 3 years ago

Update trunk - version 7.9.4

  • Property svn:executable set to *
File size: 5.4 KB
Line 
1<?php
2
3class SLN_Admin_Customers extends SLN_Admin_AbstractPage {
4
5        const PAGE = 'salon-customers';
6    const PRIORITY = 10;
7
8    public function __construct(SLN_Plugin $plugin)
9    {
10        parent::__construct($plugin);
11        add_action('in_admin_header', array($this, 'in_admin_header'));
12    }
13
14    public function admin_menu()
15    {
16        $this->classicAdminMenu(__('Salon Customers', 'salon-booking-system'), __('Customers', 'salon-booking-system'));
17        if ( ! isset($_REQUEST['id'])) {
18            add_filter(
19                'manage_'.get_plugin_page_hookname('salon-customers', 'salon').'_columns',
20                array($this, 'users_columns')
21            );
22        }
23    }
24
25        public function show() {
26                if (isset($_REQUEST['id'])) {
27                        $this->show_customer_page(intval($_REQUEST['id']));
28                }
29                else {
30                        $this->show_customers();
31                }
32        }
33
34        public function show_customer_page($user_id) {
35
36            $customer = new SLN_Wrapper_Customer(new WP_User($user_id));
37
38            if (!empty($user_id) && $customer->isEmpty()) {
39                wp_redirect(get_edit_user_link($user_id));
40                exit;
41            }
42
43            if (isset($_POST['save'])) {
44                $error = $this->save_customer($user_id);
45            }
46
47            $customer = new SLN_Wrapper_Customer(new WP_User($user_id));
48
49            echo $this->plugin->loadView(
50                'admin/_customer',
51                array(
52                    'customer' => $customer,
53                    'new_link' => self::get_edit_customer_link(0),
54                    'error'    => isset($error) ? $error : null,
55                )
56            );
57        }
58
59    private function check_email($email){
60        if (email_exists($email)) {
61                $error = new WP_Error();
62                $error->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.', 'salon-booking-system'));
63                return $error;
64        }
65        return false;
66    }
67
68        private function save_customer($user_id) {
69                $customer = [];
70        $email = isset($_POST['sln_customer_meta']['_sln_email']) ? sanitize_email( wp_unslash($_POST['sln_customer_meta']['_sln_email']) ) : false;
71        if(!$email){
72            $error = new WP_Error();
73            $error->add('missing_email', __('<strong>ERROR</strong>: This email is empty.', 'salon-booking-system'));
74            return $error;
75        }
76        if (empty($_POST['id'])) {
77            if(($error = $this->check_email($email))) return $error;
78            $user_id = wp_create_user($email, wp_generate_password(), $email);
79            $customer['user_email'] = $email;
80        }
81        else {
82            $user_id = intval($_POST['id']);
83            $user_info = get_userdata($user_id);
84            $old_mail = $user_info->user_email;
85            if($email !== $old_mail && !array_intersect(['administrator'],$user_info->roles) ){
86                if(($error = $this->check_email($email))) return $error;
87                $customer['user_email'] = $email;
88            }
89        }
90
91
92        $customer['ID'] = $user_id;
93        $customer['role'] = SLN_Plugin::USER_ROLE_CUSTOMER;
94        if(isset($_POST['sln_customer_meta']['_sln_firstname'])) $customer['first_name'] = sanitize_text_field(wp_unslash( $_POST['sln_customer_meta']['_sln_firstname'] ));
95        if(isset($_POST['sln_customer_meta']['_sln_lastname'])) $customer['last_name'] = sanitize_text_field(wp_unslash( $_POST['sln_customer_meta']['_sln_lastname'] ));
96        wp_update_user($customer);
97
98        foreach (array_map('sanitize_textarea_field',$_POST['sln_customer_meta']) as $k => $value) {
99            update_user_meta($user_id, $k, $value);
100        }
101
102        wp_redirect(self::get_edit_customer_link($user_id));
103        exit;
104    }
105
106        public function show_customers() {
107                if (empty($_REQUEST)) {
108                        $referer = '<input type="hidden" name="wp_http_referer" value="'. esc_attr(esc_url(wp_unslash($_SERVER['REQUEST_URI']))) . '" />';
109                } elseif (isset($_REQUEST['wp_http_referer'])) {
110                        $redirect = remove_query_arg(array('wp_http_referer', 'updated', 'delete_count'), esc_url(wp_unslash($_REQUEST['wp_http_referer'])));
111                        $referer = '<input type="hidden" name="wp_http_referer" value="' . esc_attr($redirect) . '" />';
112                } else {
113                        $redirect = 'admin.php';
114                        $referer = '';
115                }
116
117                $table = new SLN_Admin_Customers_List();
118
119                switch ($table->current_action()) {
120                    case 'delete':
121                        if (!is_multisite() && !empty($_REQUEST['users']) && is_array($_REQUEST['users'])) {
122                            $users =  array_map('intval',$_REQUEST['users']);
123                            foreach($users as $userId) {
124                                wp_delete_user( $userId );
125                            }
126
127                            wp_redirect(admin_url('admin.php?page='.self::PAGE));
128                            exit;
129
130                        }
131                        break;
132                }
133
134                $table->prepare_items();
135
136                echo $this->plugin->loadView(
137                    'admin/customers',
138                    array(
139                        'new_link' => self::get_edit_customer_link(0),
140                        'table'    => $table,
141                    )
142                );
143        }
144
145        public function users_columns($users_columns) {
146                $table = new SLN_Admin_Customers_List();
147
148                return $table->get_columns();
149        }
150
151
152
153        public static function get_edit_customer_link($user_id) {
154
155                return get_admin_url() . "admin.php?page=salon-customers&id=$user_id";
156        }
157}
Note: See TracBrowser for help on using the repository browser.