| 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 |
'business_type' => $posted['business_type'] |
| 134 |
]; |
| 135 |
|
| 136 |
$company = new Company(); |
| 137 |
$company->update( $args ); |
| 138 |
|
| 139 |
$redirect_to = admin_url( 'admin.php?page=erp-company&action=edit&msg=updated' ); |
| 140 |
wp_redirect( $redirect_to ); |
| 141 |
exit; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Handle audit log bulk action |
| 146 |
* |
| 147 |
* @since 0.1 |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public function audit_log_bulk_action() { |
| 152 |
|
| 153 |
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! isset( $_GET['page'] ) ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
if ( $_GET['page'] != 'erp-audit-log' ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-audit_logs' ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$redirect = remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'filter_audit_log' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 166 |
wp_redirect( $redirect ); |
| 167 |
exit(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Handle all the forms in the tools page |
| 172 |
* |
| 173 |
* @return void |
| 174 |
*/ |
| 175 |
public function tools_general() { |
| 176 |
|
| 177 |
// admin menu form |
| 178 |
if ( isset( $_POST['erp_admin_menu'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'erp-remove-menu-nonce' ) ) { |
| 179 |
|
| 180 |
$menu = isset( $_POST['menu'] ) ? array_map( 'strip_tags', $_POST['menu'] ) : []; |
| 181 |
$bar_menu = isset( $_POST['admin_menu'] ) ? array_map( 'strip_tags', $_POST['admin_menu'] ) : []; |
| 182 |
|
| 183 |
update_option( '_erp_admin_menu', $menu ); |
| 184 |
update_option( '_erp_adminbar_menu', $bar_menu ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Send test email |
| 190 |
* |
| 191 |
* @since 1.1.2 |
| 192 |
* |
| 193 |
* @return void |
| 194 |
*/ |
| 195 |
public function tools_test_mail() { |
| 196 |
if ( isset( $_POST['erp_send_test_email'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'erp-test-email-nonce' ) ) { |
| 197 |
|
| 198 |
$to = isset( $_POST['to'] ) ? sanitize_text_field( $_POST['to'] ) : ''; |
| 199 |
$subject = sprintf( __( 'Test email from %s', 'erp' ), get_bloginfo( 'name' ) ); |
| 200 |
$body = isset( $_POST['body'] ) ? $_POST['body'] : ''; |
| 201 |
|
| 202 |
if ( empty( $body ) ) { |
| 203 |
$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' ) ); |
| 204 |
} |
| 205 |
|
| 206 |
erp_mail( $to, $subject, $body ); |
| 207 |
|
| 208 |
$redirect_to = admin_url( 'admin.php?page=erp-tools&tab=misc&sent=true' ); |
| 209 |
wp_redirect( $redirect_to ); |
| 210 |
exit; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
new Form_Handler(); |
| 217 |
|