| @@ -652,9 +652,9 @@ | ||
| 652 | 652 | <div id="mainwp-edit-clients-modal" class="ui modal"> |
| 653 | 653 | <i class="close icon"></i> |
| 654 | 654 | <div class="header"><?php esc_html_e( 'Edit client', 'mainwp' ); ?></div> |
| 655 | 655 | <div class="scrolling content"> |
| 656 | - <div class="ui info message"><?php esc_html_e( 'Empty fields will not be passed to child sites.', 'mainwp' ); ?></div> | |
| 656 | + <div class="ui info message"><?php esc_html_e( 'Blank fields will not be changed.', 'mainwp' ); ?></div> | |
| 657 | 657 | <form id="update_client_profile" class="ui form"> |
| 658 | 658 | <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?> |
| 659 | 659 | <h3 class="ui header"><?php esc_html_e( 'Name', 'mainwp' ); ?></h3> |
| 660 | 660 | <div class="ui grid field"> |
| @@ -996,15 +996,22 @@ | ||
| 996 | 996 | |
| 997 | 997 | $has_import_data = ! empty( $_POST['mainwp_client_import_add'] ); |
| 998 | 998 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 999 | 999 | if ( $has_import_data && check_admin_referer( 'mainwp-admin-nonce' ) ) { |
| 1000 | - static::render_import_client_modal(); | |
| 1000 | + $result = static::render_import_client_modal(); | |
| 1001 | 1001 | } |
| 1002 | 1002 | ?> |
| 1003 | 1003 | <div class="ui padded segment" id="mainwp-import-clients"> |
| 1004 | 1004 | <form method="POST" action="" enctype="multipart/form-data" id="mainwp_client_import_form" class="ui form"> |
| 1005 | 1005 | <div> |
| 1006 | - <?php $el_id_mes_zn_1 = 'mainwp-message-zone'; ?> | |
| 1006 | + <?php | |
| 1007 | + if ( ! empty( $result ) && is_wp_error( $result ) ) { | |
| 1008 | + ?> | |
| 1009 | + <div class="ui message yellow"><i class="close icon"></i> <?php echo esc_html( $result->get_error_message() ); ?></div> | |
| 1010 | + <?php | |
| 1011 | + } | |
| 1012 | + $el_id_mes_zn_1 = 'mainwp-message-zone'; | |
| 1013 | + ?> | |
| 1007 | 1014 | <div id="<?php echo esc_attr( $el_id_mes_zn_1 ); ?>" class="ui message" style="display:none"></div> |
| 1008 | 1015 | <h3 class="ui dividing header"> |
| 1009 | 1016 | <?php echo esc_html( $title_page ); ?> |
| 1010 | 1017 | <div class="sub header"><?php esc_html_e( 'Import multiple clients to your MainWP Dashboard.', 'mainwp' ); ?></div> |
| @@ -1038,16 +1045,29 @@ | ||
| 1038 | 1045 | /** |
| 1039 | 1046 | * Method render_import_client_modal() |
| 1040 | 1047 | * |
| 1041 | 1048 | * Render HTML import client modal. |
| 1049 | + * | |
| 1050 | + * @return mixed Result. | |
| 1042 | 1051 | */ |
| 1043 | 1052 | public static function render_import_client_modal() { |
| 1053 | + $import_client_data = false; | |
| 1054 | + $has_file_upload = isset( $_FILES['mainwp_client_import_file_bulkupload'] ) && isset( $_FILES['mainwp_client_import_file_bulkupload']['error'] ) && UPLOAD_ERR_OK === $_FILES['mainwp_client_import_file_bulkupload']['error']; // phpcs:ignore -- NOSONAR | |
| 1055 | + if ( $has_file_upload ) { | |
| 1056 | + $import_client_data = static::handle_client_import_files(); | |
| 1057 | + } | |
| 1058 | + if ( ! is_array( $import_client_data ) || empty( $import_client_data['data'] ) || ! is_array( $import_client_data['data'] ) ) { | |
| 1059 | + return new \WP_Error( | |
| 1060 | + 'client_import_failed', | |
| 1061 | + __( 'Failed to import client data. Please try again.', 'mainwp' ) | |
| 1062 | + ); | |
| 1063 | + } | |
| 1044 | 1064 | ?> |
| 1045 | 1065 | <div class="ui large modal mainwp-qsw-import-client-modal" id="mainwp-import-client-modal" > |
| 1046 | 1066 | <i class="close icon"></i> |
| 1047 | 1067 | <div class="header"><?php esc_html_e( 'Import Clients', 'mainwp' ); ?></div> |
| 1048 | 1068 | <div class="scrolling content"> |
| 1049 | - <?php static::render_import_client_row_modal(); ?> | |
| 1069 | + <?php static::render_import_client_row_modal( $import_client_data ); ?> | |
| 1050 | 1070 | </div> |
| 1051 | 1071 | <div class="actions"> |
| 1052 | 1072 | <div class="ui two column grid"> |
| 1053 | 1073 | <div class="left aligned middle aligned column"> |
| @@ -1067,8 +1087,10 @@ | ||
| 1067 | 1087 | } ).modal( 'show' ); |
| 1068 | 1088 | } ); |
| 1069 | 1089 | </script> |
| 1070 | 1090 | <?php |
| 1091 | + | |
| 1092 | + return true; | |
| 1071 | 1093 | } |
| 1072 | 1094 | |
| 1073 | 1095 | /** |
| 1074 | 1096 | * Method render_import_client_row_modal() |
| @@ -1073,50 +1095,48 @@ | ||
| 1073 | 1095 | /** |
| 1074 | 1096 | * Method render_import_client_row_modal() |
| 1075 | 1097 | * |
| 1076 | 1098 | * Render row HTML import client. |
| 1099 | + * | |
| 1100 | + * @return mixed Result or WP_Error. | |
| 1077 | 1101 | */ |
| 1078 | - public static function render_import_client_row_modal() { | |
| 1079 | - ?> | |
| 1080 | - <div id="mainwp-importing-clients" class="ui active dimmer"> | |
| 1081 | - <div class="ui double text loader"><?php esc_html_e( 'Importing...', 'mainwp' ); ?></div> | |
| 1082 | - </div> | |
| 1083 | - <div class="ui message" id="mainwp-import-clients-status-message"> | |
| 1084 | - <i class="notched circle loading icon"></i> <?php echo esc_html__( 'Importing...', 'mainwp' ); ?> | |
| 1085 | - </div> | |
| 1086 | - <?php | |
| 1087 | - $has_file_upload = isset( $_FILES['mainwp_client_import_file_bulkupload'] ) && isset( $_FILES['mainwp_client_import_file_bulkupload']['error'] ) && UPLOAD_ERR_OK === $_FILES['mainwp_client_import_file_bulkupload']['error']; // phpcs:ignore -- NOSONAR | |
| 1102 | + public static function render_import_client_row_modal( $client_data = array() ) { // phpcs:ignore -- NOSONAR -complex. | |
| 1088 | 1103 | |
| 1089 | - if ( $has_file_upload ) { | |
| 1090 | - $import_client_data = static::handle_client_import_files(); | |
| 1091 | - if ( ! empty( $import_client_data ) && is_array( $import_client_data ) ) { | |
| 1092 | - $row = 0; | |
| 1093 | - $header_line = trim( $import_client_data['header_line'] ); | |
| 1094 | - foreach ( $import_client_data['data'] as $val_client_data ) { | |
| 1095 | - $encoded = wp_json_encode( $val_client_data ); | |
| 1096 | - $original = implode( | |
| 1097 | - ', ', | |
| 1098 | - array_map( | |
| 1099 | - function ( $item ) { | |
| 1100 | - return is_array( $item ) ? implode( ';', $item ) : $item; | |
| 1101 | - }, | |
| 1102 | - $val_client_data | |
| 1103 | - ) | |
| 1104 | - ); | |
| 1105 | - ?> | |
| 1106 | - <input type="hidden" id="mainwp_manageclients_import_csv_line_<?php echo esc_attr( $row + 1 ); ?>" value="" encoded-data="<?php echo esc_attr( $encoded ); ?>" original="<?php echo esc_attr( $original ); ?>"/> | |
| 1107 | - <?php | |
| 1108 | - ++$row; | |
| 1109 | - } | |
| 1104 | + if ( is_array( $client_data ) && ! empty( $client_data['data'] ) && is_array( $client_data['data'] ) ) { | |
| 1105 | + ?> | |
| 1106 | + <div id="mainwp-importing-clients" class="ui active dimmer"> | |
| 1107 | + <div class="ui double text loader"><?php esc_html_e( 'Importing...', 'mainwp' ); ?></div> | |
| 1108 | + </div> | |
| 1109 | + <div class="ui message" id="mainwp-import-clients-status-message"> | |
| 1110 | + <i class="notched circle loading icon"></i> <?php echo esc_html__( 'Importing...', 'mainwp' ); ?> | |
| 1111 | + </div> | |
| 1112 | + <?php | |
| 1113 | + $row = 0; | |
| 1114 | + $header_line = trim( $client_data['header_line'] ); | |
| 1115 | + foreach ( $client_data['data'] as $val_client_data ) { | |
| 1116 | + $encoded = wp_json_encode( $val_client_data ); | |
| 1117 | + $original = implode( | |
| 1118 | + ', ', | |
| 1119 | + array_map( | |
| 1120 | + function ( $item ) { | |
| 1121 | + return is_array( $item ) ? implode( ';', $item ) : $item; | |
| 1122 | + }, | |
| 1123 | + $val_client_data | |
| 1124 | + ) | |
| 1125 | + ); | |
| 1110 | 1126 | ?> |
| 1111 | - <input type="hidden" id="mainwp_manageclients_do_import" value="1"/> | |
| 1112 | - <input type="hidden" id="mainwp_manageclients_total_import" value="<?php echo esc_attr( $row ); ?>"/> | |
| 1113 | - <div class="mainwp_manageclients_import_listing" id="mainwp_manageclients_import_logging"> | |
| 1114 | - <span class="log ui medium text"><?php echo esc_html( $header_line ) . '<br/>'; ?></span> | |
| 1115 | - </div> | |
| 1116 | - <div class="mainwp_manageclients_import_listing" id="mainwp_manageclients_import_fail_logging" style="display: none;"><?php echo esc_html( $header_line ); ?> </div> | |
| 1127 | + <input type="hidden" id="mainwp_manageclients_import_csv_line_<?php echo esc_attr( $row + 1 ); ?>" value="" encoded-data="<?php echo esc_attr( $encoded ); ?>" original="<?php echo esc_attr( $original ); ?>"/> | |
| 1117 | 1128 | <?php |
| 1129 | + ++$row; | |
| 1118 | 1130 | } |
| 1131 | + ?> | |
| 1132 | + <input type="hidden" id="mainwp_manageclients_do_import" value="1"/> | |
| 1133 | + <input type="hidden" id="mainwp_manageclients_total_import" value="<?php echo esc_attr( $row ); ?>"/> | |
| 1134 | + <div class="mainwp_manageclients_import_listing" id="mainwp_manageclients_import_logging"> | |
| 1135 | + <span class="log ui medium text"><?php echo esc_html( $header_line ) . '<br/>'; ?></span> | |
| 1136 | + </div> | |
| 1137 | + <div class="mainwp_manageclients_import_listing" id="mainwp_manageclients_import_fail_logging" style="display: none;"><?php echo esc_html( $header_line ); ?> </div> | |
| 1138 | + <?php | |
| 1119 | 1139 | } |
| 1120 | 1140 | } |
| 1121 | 1141 | |
| 1122 | 1142 | /** |
| @@ -1129,10 +1149,20 @@ | ||
| 1129 | 1149 | * |
| 1130 | 1150 | * @return array Import data. |
| 1131 | 1151 | */ |
| 1132 | 1152 | public static function handle_client_import_files() { // phpcs:ignore -- NOSONAR |
| 1133 | - $tmp_path = isset( $_FILES['mainwp_client_import_file_bulkupload']['tmp_name'] ) ? sanitize_text_field( wp_unslash( $_FILES['mainwp_client_import_file_bulkupload']['tmp_name'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- NOSONAR | |
| 1134 | - MainWP_System_Utility::get_wp_file_system(); | |
| 1153 | + | |
| 1154 | + if ( isset( $_FILES['mainwp_client_import_file_bulkupload']['error'] ) && UPLOAD_ERR_OK !== $_FILES['mainwp_client_import_file_bulkupload']['error'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- NOSONAR | |
| 1155 | + return false; | |
| 1156 | + } | |
| 1157 | + | |
| 1158 | + $tmp_path = isset( $_FILES['mainwp_client_import_file_bulkupload']['tmp_name'] ) ? $_FILES['mainwp_client_import_file_bulkupload']['tmp_name'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- NOSONAR | |
| 1159 | + | |
| 1160 | + if ( ! is_uploaded_file( $tmp_path ) ) { | |
| 1161 | + return false; | |
| 1162 | + } | |
| 1163 | + | |
| 1164 | + $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system(); | |
| 1135 | 1165 | //phpcs:enable |
| 1136 | 1166 | /** |
| 1137 | 1167 | * WordPress files system object. |
| 1138 | 1168 | * |
| @@ -1139,14 +1169,23 @@ | ||
| 1139 | 1169 | * @global object |
| 1140 | 1170 | */ |
| 1141 | 1171 | global $wp_filesystem; |
| 1142 | 1172 | |
| 1143 | - $content = $wp_filesystem->get_contents( $tmp_path ); | |
| 1173 | + $lines = array(); | |
| 1174 | + $header_line = ''; | |
| 1144 | 1175 | |
| 1145 | - // to compatible with EOL on OSs. | |
| 1146 | - $content = str_replace( "\r\n", "\r", $content ); | |
| 1147 | - $content = str_replace( "\n", "\r", $content ); | |
| 1148 | - $lines = explode( "\r", $content ); | |
| 1176 | + if ( $hasWPFileSystem && ! empty( $wp_filesystem ) ) { | |
| 1177 | + $content = $wp_filesystem->get_contents( $tmp_path ); | |
| 1178 | + if ( ! empty( $content ) ) { | |
| 1179 | + // to compatible with EOL on OSs. | |
| 1180 | + $content = str_replace( "\r\n", "\r", $content ); | |
| 1181 | + $content = str_replace( "\n", "\r", $content ); | |
| 1182 | + $lines = explode( "\r", $content ); | |
| 1183 | + } | |
| 1184 | + } else { | |
| 1185 | + return false; | |
| 1186 | + } | |
| 1187 | + | |
| 1149 | 1188 | $import_data = array(); |
| 1150 | 1189 | $default_values = array( |
| 1151 | 1190 | 'client.name' => '', |
| 1152 | 1191 | 'client.email' => '', |
| @@ -1495,10 +1534,10 @@ | ||
| 1495 | 1534 | $client_image = sanitize_text_field( wp_unslash( $_POST['mainwp_add_edit_client_uploaded_icon_hidden'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 1496 | 1535 | } |
| 1497 | 1536 | |
| 1498 | 1537 | // compatible with quick setup. |
| 1499 | - if ( isset( $_FILES['mainwp_client_image_uploader'] ) && isset( $_FILES['mainwp_client_image_uploader']['error']['client_field'] ) && UPLOAD_ERR_OK === $_FILES['mainwp_client_image_uploader']['error']['client_field'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 1500 | - $output = MainWP_System_Utility::handle_upload_image( 'client-images', $_FILES['mainwp_client_image_uploader'], 'client_field' ); // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1538 | + if ( isset( $_FILES['mainwp_client_image_uploader']['error']['client_field'] ) && UPLOAD_ERR_OK === $_FILES['mainwp_client_image_uploader']['error']['client_field'] ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1539 | + $output = MainWP_System_Utility::handle_upload_image( 'client-images', $_FILES['mainwp_client_image_uploader'], 'client_field' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1501 | 1540 | if ( is_array( $output ) && isset( $output['filename'] ) && ! empty( $output['filename'] ) ) { |
| 1502 | 1541 | $client_image = $output['filename']; |
| 1503 | 1542 | } |
| 1504 | 1543 | } |