PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.2.6
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.2.6
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / admin / class-form-handler.php

class-form-handler.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.2.6, at includes/admin/class-form-handler.php

216 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WeDevs\ERP\Admin;
3 use WeDevs\ERP\Company;
4 use WeDevs\ERP\Framework\Traits\Hooker;
5
6 /**
7 * Admin form handler
8 *
9 * Handles all the form submission
10 */
11 class Form_Handler {
12
13 use Hooker;
14
15 /**
16 * [__construct description]
17 */
18 public function __construct() {
19 $this->action( 'erp_action_create_new_company', 'create_new_company' );
20
21 $this->action( 'admin_init', 'save_settings' );
22 $this->action( 'admin_init', 'tools_general' );
23 $this->action( 'admin_init', 'tools_test_mail' );
24
25 $erp_settings = sanitize_title( __( 'ERP Settings', 'erp' ) );
26 add_action( "load-{$erp_settings}_page_erp-audit-log", array( $this, 'audit_log_bulk_action' ) );
27 }
28
29 /**
30 * Save all settings
31 *
32 * @since 0.1
33 *
34 * @return void
35 */
36 public function save_settings() {
37 if ( ! isset( $_POST['erp_module_status'] ) ) {
38 return;
39 }
40
41 if ( ! wp_verify_nonce( $_POST['erp_settings'], 'erp_nonce' ) ) {
42 return;
43 }
44
45 $inactive = ( isset( $_GET['tab'] ) && $_GET['tab'] == 'inactive' ) ? true : false;
46 $modules = isset( $_POST['modules'] ) ? $_POST['modules'] : array();
47 $all_modules = wperp()->modules->get_modules();
48
49 foreach ( $all_modules as $key => $module ) {
50 if ( ! in_array( $key, $modules ) ) {
51 unset( $all_modules[$key] );
52 }
53 }
54
55 if ( $inactive ) {
56 $active_modules = wperp()->modules->get_active_modules();
57 $all_modules = array_merge( $all_modules, $active_modules );
58 }
59 update_option( 'erp_modules', $all_modules );
60 wp_redirect( $_POST['_wp_http_referer'] );
61 exit();
62 }
63
64 /**
65 * Check is valid input or not
66 *
67 * @since 0.1
68 *
69 * @param array $array
70 * @param string $key
71 *
72 * @return boolean
73 */
74 public function is_valid_input( $array, $key ) {
75 if ( ! isset( $array[$key]) || empty( $array[$key] ) || $array[$key] == '-1' ) {
76 return false;
77 }
78
79 return true;
80 }
81
82 /**
83 * Create a new company
84 *
85 * @return void
86 */
87 public function create_new_company() {
88 if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp-new-company' ) ) {
89 wp_die( __( 'Cheating?', 'erp' ) );
90 }
91
92 $posted = array_map( 'strip_tags_deep', $_POST );
93 $posted = array_map( 'trim_deep', $posted );
94
95 $errors = [];
96 $required = [
97 'name' => __( 'Company name', 'erp' ),
98 'address' => [
99 'country' => __( 'Country', 'erp' )
100 ]
101 ];
102
103 if ( ! $this->is_valid_input( $posted, 'name' ) ) {
104 $errors[] = 'error-company=1';
105 }
106
107 if ( ! $this->is_valid_input( $posted['address'], 'country' ) ) {
108 $errors[] = 'error-country=1';
109 }
110
111 if ( $errors ) {
112 $args = implode( '&' , $errors );
113 $redirect_to = admin_url( 'admin.php?page=erp-company&action=edit&msg=error&' . $args );
114 wp_redirect( $redirect_to );
115 exit;
116 }
117
118 $args = [
119 'logo' => isset( $posted['company_logo_id'] ) ? absint( $posted['company_logo_id'] ) : 0,
120 'name' => $posted['name'],
121 'address' => [
122 'address_1' => $posted['address']['address_1'],
123 'address_2' => $posted['address']['address_2'],
124 'city' => $posted['address']['city'],
125 'state' => $posted['address']['state'],
126 'zip' => $posted['address']['zip'],
127 'country' => $posted['address']['country'],
128 ],
129 'phone' => $posted['phone'],
130 'fax' => $posted['fax'],
131 'mobile' => $posted['mobile'],
132 'website' => $posted['website'],
133 ];
134
135 $company = new Company();
136 $company->update( $args );
137
138 $redirect_to = admin_url( 'admin.php?page=erp-company&action=edit&msg=updated' );
139 wp_redirect( $redirect_to );
140 exit;
141 }
142
143 /**
144 * Handle audit log bulk action
145 *
146 * @since 0.1
147 *
148 * @return void
149 */
150 public function audit_log_bulk_action() {
151
152 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! isset( $_GET['page'] ) ) {
153 return;
154 }
155
156 if ( $_GET['page'] != 'erp-audit-log' ) {
157 return;
158 }
159
160 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-audit_logs' ) ) {
161 return;
162 }
163
164 $redirect = remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'filter_audit_log' ), wp_unslash( $_SERVER['REQUEST_URI'] ) );
165 wp_redirect( $redirect );
166 exit();
167 }
168
169 /**
170 * Handle all the forms in the tools page
171 *
172 * @return void
173 */
174 public function tools_general() {
175
176 // admin menu form
177 if ( isset( $_POST['erp_admin_menu'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'erp-remove-menu-nonce' ) ) {
178
179 $menu = isset( $_POST['menu'] ) ? array_map( 'strip_tags', $_POST['menu'] ) : [];
180 $bar_menu = isset( $_POST['admin_menu'] ) ? array_map( 'strip_tags', $_POST['admin_menu'] ) : [];
181
182 update_option( '_erp_admin_menu', $menu );
183 update_option( '_erp_adminbar_menu', $bar_menu );
184 }
185 }
186
187 /**
188 * Send test email
189 *
190 * @since 1.1.2
191 *
192 * @return void
193 */
194 public function tools_test_mail() {
195 if ( isset( $_POST['erp_send_test_email'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'erp-test-email-nonce' ) ) {
196
197 $to = isset( $_POST['to'] ) ? sanitize_text_field( $_POST['to'] ) : '';
198 $subject = sprintf( __( 'Test email from %s', 'erp' ), get_bloginfo( 'name' ) );
199 $body = isset( $_POST['body'] ) ? $_POST['body'] : '';
200
201 if ( empty( $body ) ) {
202 $body = sprintf( __( 'This test email proves that your WordPress installation at %1$s can send emails.\n\nSent: %2$s', 'erp' ), get_bloginfo( 'url' ), date( 'r' ) );
203 }
204
205 erp_mail( $to, $subject, $body );
206
207 $redirect_to = admin_url( 'admin.php?page=erp-tools&tab=misc&sent=true' );
208 wp_redirect( $redirect_to );
209 exit;
210 }
211 }
212
213 }
214
215 new Form_Handler();
216