| @@ -4,28 +4,23 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmXMLController { |
| 7 | 7 | |
| 8 | - /** | |
| 9 | - * @return void | |
| 10 | - */ | |
| 11 | 8 | public static function menu() { |
| 12 | - add_submenu_page( 'formidable', 'Formidable | ' . __( 'Import/Export', 'formidable' ), __( 'Import/Export', 'formidable' ), 'frm_edit_forms', 'formidable-import', 'FrmXMLController::route' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 9 | + add_submenu_page( 'formidable', 'Formidable | ' . __( 'Import/Export', 'formidable' ), __( 'Import/Export', 'formidable' ), 'frm_edit_forms', 'formidable-import', 'FrmXMLController::route' ); | |
| 13 | 10 | } |
| 14 | 11 | |
| 15 | - /** | |
| 16 | - * @return void | |
| 17 | - */ | |
| 18 | 12 | public static function add_default_templates() { |
| 19 | 13 | if ( FrmXMLHelper::check_if_libxml_disable_entity_loader_exists() ) { |
| 20 | - // XML import is not enabled on your server. | |
| 14 | + // XML import is not enabled on your server | |
| 21 | 15 | return; |
| 22 | 16 | } |
| 23 | 17 | |
| 24 | 18 | $set_err = libxml_use_internal_errors( true ); |
| 25 | 19 | $loader = FrmXMLHelper::maybe_libxml_disable_entity_loader( true ); |
| 26 | - $files = apply_filters( 'frm_default_templates_files', array() ); | |
| 27 | 20 | |
| 21 | + $files = apply_filters( 'frm_default_templates_files', array() ); | |
| 22 | + | |
| 28 | 23 | foreach ( (array) $files as $file ) { |
| 29 | 24 | FrmXMLHelper::import_xml( $file ); |
| 30 | 25 | unset( $file ); |
| 31 | 26 | } |
| @@ -39,35 +34,18 @@ | ||
| 39 | 34 | /** |
| 40 | 35 | * Use the template link to install the XML template |
| 41 | 36 | * |
| 42 | 37 | * @since 3.06 |
| 43 | - * | |
| 44 | - * @return void | |
| 45 | 38 | */ |
| 46 | 39 | public static function install_template() { |
| 47 | 40 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 48 | 41 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 49 | 42 | |
| 50 | - if ( ! function_exists( 'simplexml_load_string' ) ) { | |
| 51 | - $response = array( | |
| 52 | - 'message' => __( 'Your server is missing the Simple XML extension. This is required to install a template.', 'formidable' ), | |
| 53 | - ); | |
| 54 | - echo wp_json_encode( $response ); | |
| 55 | - wp_die(); | |
| 56 | - } | |
| 43 | + $url = FrmAppHelper::get_param( 'xml', '', 'post', 'esc_url_raw' ); | |
| 57 | 44 | |
| 58 | 45 | $form = self::get_posted_form(); |
| 59 | - $url = FrmAppHelper::get_param( 'xml', '', 'post', 'esc_url_raw' ); | |
| 60 | 46 | self::override_url( $form, $url ); |
| 61 | 47 | |
| 62 | - if ( ! self::validate_xml_url( $url ) ) { | |
| 63 | - $response = array( | |
| 64 | - 'message' => __( 'The template you are trying to install could not be validated.', 'formidable' ), | |
| 65 | - ); | |
| 66 | - echo wp_json_encode( $response ); | |
| 67 | - wp_die(); | |
| 68 | - } | |
| 69 | - | |
| 70 | 48 | $response = wp_remote_get( $url ); |
| 71 | 49 | $body = wp_remote_retrieve_body( $response ); |
| 72 | 50 | $xml = simplexml_load_string( $body ); |
| 73 | 51 | |
| @@ -72,9 +50,9 @@ | ||
| 72 | 50 | $xml = simplexml_load_string( $body ); |
| 73 | 51 | |
| 74 | 52 | if ( ! $xml ) { |
| 75 | 53 | $response = array( |
| 76 | - 'message' => __( 'There was an error reading the form template.', 'formidable' ), | |
| 54 | + 'message' => __( 'There was an error reading the form template', 'formidable' ), | |
| 77 | 55 | ); |
| 78 | 56 | echo wp_json_encode( $response ); |
| 79 | 57 | wp_die(); |
| 80 | 58 | } |
| @@ -80,71 +58,60 @@ | ||
| 80 | 58 | } |
| 81 | 59 | |
| 82 | 60 | self::set_new_form_name( $xml ); |
| 83 | 61 | |
| 84 | - $imported = FrmXMLHelper::import_xml_now( $xml, true ); | |
| 85 | - | |
| 86 | - if ( ! empty( $imported['form_status'] ) ) { | |
| 62 | + $imported = FrmXMLHelper::import_xml_now( $xml ); | |
| 63 | + if ( isset( $imported['form_status'] ) && ! empty( $imported['form_status'] ) ) { | |
| 87 | 64 | // Get the last form id in case there are child forms. |
| 88 | - $form_id = array_key_last( $imported['form_status'] ); | |
| 65 | + end( $imported['form_status'] ); | |
| 66 | + $form_id = key( $imported['form_status'] ); | |
| 89 | 67 | $response = array( |
| 90 | 68 | 'id' => $form_id, |
| 91 | - 'redirect' => FrmForm::get_edit_link( $form_id ) . '&new_template=true', | |
| 69 | + 'redirect' => FrmForm::get_edit_link( $form_id ), | |
| 92 | 70 | 'success' => 1, |
| 93 | 71 | ); |
| 94 | - | |
| 95 | 72 | if ( ! empty( $imported['imported']['posts'] ) ) { |
| 96 | 73 | // Return the link to the last page created. |
| 97 | 74 | $pages = $imported['posts']; |
| 98 | 75 | } |
| 99 | 76 | |
| 100 | - if ( $form ) { | |
| 77 | + if ( ! empty( $form ) ) { | |
| 101 | 78 | // Create selected pages with the correct shortcodes. |
| 102 | 79 | $pages = self::create_pages_for_import( $form ); |
| 103 | 80 | } |
| 104 | 81 | |
| 105 | - if ( ! empty( $pages ) ) { | |
| 106 | - $post_id = end( $pages ); | |
| 82 | + if ( isset( $pages ) && ! empty( $pages ) ) { | |
| 83 | + $post_id = end( $pages ); | |
| 107 | 84 | $response['redirect'] = get_permalink( $post_id ); |
| 108 | 85 | } |
| 109 | 86 | } else { |
| 110 | - $message = $imported['error'] ?? __( 'There was an error importing form', 'formidable' ); | |
| 87 | + if ( isset( $imported['error'] ) ) { | |
| 88 | + $message = $imported['error']; | |
| 89 | + } else { | |
| 90 | + $message = __( 'There was an error importing form', 'formidable' ); | |
| 91 | + } | |
| 111 | 92 | $response = array( |
| 112 | 93 | 'message' => $message, |
| 113 | 94 | ); |
| 114 | - }//end if | |
| 115 | 95 | |
| 116 | - /** | |
| 117 | - * @since 6.18 Added `url` to the $args. | |
| 118 | - */ | |
| 119 | - $response = apply_filters( 'frm_xml_response', $response, compact( 'form', 'imported', 'url' ) ); | |
| 96 | + } | |
| 120 | 97 | |
| 98 | + $response = apply_filters( 'frm_xml_response', $response, compact( 'form', 'imported' ) ); | |
| 99 | + | |
| 121 | 100 | echo wp_json_encode( $response ); |
| 122 | 101 | wp_die(); |
| 123 | 102 | } |
| 124 | 103 | |
| 125 | 104 | /** |
| 126 | - * Make sure that the XML file we're trying to load is in fact an XML file, and that it's coming from our S3 bucket. | |
| 127 | - * This is to make sure that the URL can't be exploited for a SSRF attack. | |
| 128 | - * | |
| 129 | - * @since 5.5.5 | |
| 130 | - * | |
| 131 | - * @param string $url | |
| 132 | - * | |
| 133 | - * @return bool True on success, False on error. | |
| 134 | - */ | |
| 135 | - private static function validate_xml_url( $url ) { | |
| 136 | - return FrmAppHelper::validate_url_is_in_s3_bucket( $url, 'xml' ); | |
| 137 | - } | |
| 138 | - | |
| 139 | - /** | |
| 140 | 105 | * @since 4.06.02 |
| 141 | - * | |
| 142 | - * @return mixed | |
| 143 | 106 | */ |
| 144 | 107 | private static function get_posted_form() { |
| 145 | 108 | $form = FrmAppHelper::get_param( 'form', '', 'post', 'wp_unslash' ); |
| 146 | - return $form ? json_decode( $form, true ) : $form; | |
| 109 | + if ( empty( $form ) ) { | |
| 110 | + return $form; | |
| 111 | + } | |
| 112 | + $form = json_decode( $form, true ); | |
| 113 | + return $form; | |
| 147 | 114 | } |
| 148 | 115 | |
| 149 | 116 | /** |
| 150 | 117 | * Get a different URL depending on the selection in the form. |
| @@ -149,24 +116,17 @@ | ||
| 149 | 116 | /** |
| 150 | 117 | * Get a different URL depending on the selection in the form. |
| 151 | 118 | * |
| 152 | 119 | * @since 4.06.02 |
| 153 | - * | |
| 154 | - * @param array $form The posted form values. | |
| 155 | - * @param string $url The URL to override. | |
| 156 | - * | |
| 157 | - * @return void | |
| 158 | 120 | */ |
| 159 | 121 | private static function override_url( $form, &$url ) { |
| 160 | 122 | $selected_form = self::get_selected_in_form( $form, 'form' ); |
| 161 | - | |
| 162 | - if ( ! $selected_form ) { | |
| 123 | + if ( empty( $selected_form ) ) { | |
| 163 | 124 | return; |
| 164 | 125 | } |
| 165 | 126 | |
| 166 | - $selected_xml = isset( $form['xml'] ) && isset( $form['xml'][ $selected_form ] ) ? $form['xml'][ $selected_form ] : ''; | |
| 167 | - | |
| 168 | - if ( ! $selected_xml || ! str_starts_with( $selected_xml, 'http' ) ) { | |
| 127 | + $selected_xml = isset( $form['xml'] ) && isset( $form['xml'][ $selected_form ] ) ? $form['xml'][ $selected_form ] : ''; | |
| 128 | + if ( empty( $selected_xml ) || strpos( $selected_xml, 'http' ) !== 0 ) { | |
| 169 | 129 | return; |
| 170 | 130 | } |
| 171 | 131 | |
| 172 | 132 | $url = $selected_xml; |
| @@ -173,16 +133,11 @@ | ||
| 173 | 133 | } |
| 174 | 134 | |
| 175 | 135 | /** |
| 176 | 136 | * @since 4.06.02 |
| 177 | - * | |
| 178 | - * @param array $form | |
| 179 | - * @param string $value | |
| 180 | - * | |
| 181 | - * @return string | |
| 182 | 137 | */ |
| 183 | 138 | private static function get_selected_in_form( $form, $value = 'form' ) { |
| 184 | - if ( $form && ! empty( $form[ $value ] ) ) { | |
| 139 | + if ( ! empty( $form ) && isset( $form[ $value ] ) && ! empty( $form[ $value ] ) ) { | |
| 185 | 140 | return $form[ $value ]; |
| 186 | 141 | } |
| 187 | 142 | |
| 188 | 143 | return ''; |
| @@ -192,21 +147,21 @@ | ||
| 192 | 147 | * @since 4.06.02 |
| 193 | 148 | * |
| 194 | 149 | * @param array $form The posted form values. |
| 195 | 150 | * |
| 196 | - * @return array|null The array of created pages. | |
| 151 | + * @return array The array of created pages. | |
| 197 | 152 | */ |
| 198 | 153 | private static function create_pages_for_import( $form ) { |
| 199 | - if ( empty( $form['pages'] ) ) { | |
| 200 | - return null; | |
| 154 | + if ( ! isset( $form['pages'] ) || empty( $form['pages'] ) ) { | |
| 155 | + return; | |
| 201 | 156 | } |
| 202 | 157 | |
| 203 | - $form_key = self::get_selected_in_form( $form, 'form' ); | |
| 158 | + $form_key = self::get_selected_in_form( $form, 'form' ); | |
| 204 | 159 | $view_keys = self::get_selected_in_form( $form, 'view' ); |
| 205 | - $page_ids = array(); | |
| 206 | 160 | |
| 161 | + $page_ids = array(); | |
| 207 | 162 | foreach ( (array) $form['pages'] as $for => $name ) { |
| 208 | - if ( ! $name ) { | |
| 163 | + if ( empty( $name ) ) { | |
| 209 | 164 | // Don't create a page if no title is given. |
| 210 | 165 | continue; |
| 211 | 166 | } |
| 212 | 167 | |
| @@ -213,9 +168,9 @@ | ||
| 213 | 168 | if ( $for === 'view' ) { |
| 214 | 169 | $item_key = is_array( $view_keys ) ? $view_keys[ $form_key ] : $view_keys; |
| 215 | 170 | $shortcode = '[display-frm-data id=%1$s filter=limited]'; |
| 216 | 171 | } elseif ( $for === 'form' ) { |
| 217 | - $item_key = $form_key; | |
| 172 | + $item_key = $form_key; | |
| 218 | 173 | $shortcode = '[formidable id=%1$s]'; |
| 219 | 174 | } else { |
| 220 | 175 | $item_key = self::get_selected_in_form( $form, 'form' ); |
| 221 | 176 | $shortcode = '[' . esc_html( $for ) . ' id=%1$s]'; |
| @@ -232,9 +187,9 @@ | ||
| 232 | 187 | 'post_type' => 'page', |
| 233 | 188 | 'post_content' => sprintf( $shortcode, $item_key ), |
| 234 | 189 | ) |
| 235 | 190 | ); |
| 236 | - }//end foreach | |
| 191 | + } | |
| 237 | 192 | |
| 238 | 193 | return $page_ids; |
| 239 | 194 | } |
| 240 | 195 | |
| @@ -245,10 +200,8 @@ | ||
| 245 | 200 | * |
| 246 | 201 | * @since 3.06 |
| 247 | 202 | * |
| 248 | 203 | * @param object $xml The values included in the XML. |
| 249 | - * | |
| 250 | - * @return void | |
| 251 | 204 | */ |
| 252 | 205 | private static function set_new_form_name( &$xml ) { |
| 253 | 206 | if ( ! isset( $xml->form ) ) { |
| 254 | 207 | return; |
| @@ -255,38 +208,33 @@ | ||
| 255 | 208 | } |
| 256 | 209 | |
| 257 | 210 | $name = FrmAppHelper::get_param( 'name', '', 'post', 'sanitize_text_field' ); |
| 258 | 211 | $description = FrmAppHelper::get_param( 'desc', '', 'post', 'sanitize_textarea_field' ); |
| 259 | - | |
| 260 | - if ( ! $name && ! $description ) { | |
| 212 | + if ( empty( $name ) && empty( $description ) ) { | |
| 261 | 213 | return; |
| 262 | 214 | } |
| 263 | 215 | |
| 264 | 216 | // Get the main form ID. |
| 265 | 217 | $set_name = 0; |
| 266 | - | |
| 267 | 218 | foreach ( $xml->form as $form ) { |
| 268 | - if ( empty( $form->parent_form_id ) ) { | |
| 269 | - $set_name = (int) $form->id; | |
| 219 | + if ( ! isset( $form->parent_form_id ) || empty( $form->parent_form_id ) ) { | |
| 220 | + $set_name = $form->id; | |
| 270 | 221 | } |
| 271 | 222 | } |
| 272 | 223 | |
| 273 | 224 | foreach ( $xml->form as $form ) { |
| 274 | 225 | // Maybe set the form name if this isn't a child form. |
| 275 | - if ( $set_name === (int) $form->id ) { | |
| 226 | + if ( $set_name == $form->id ) { | |
| 276 | 227 | $form->name = $name; |
| 277 | 228 | $form->description = $description; |
| 278 | 229 | } |
| 279 | 230 | |
| 280 | 231 | // Use a unique key to prevent editing existing form. |
| 281 | - $sanitized_form_name = sanitize_title( $form->name ); | |
| 282 | - $form->form_key = FrmAppHelper::get_unique_key( $sanitized_form_name, 'frm_forms', 'form_key' ); | |
| 232 | + $name = sanitize_title( $form->name ); | |
| 233 | + $form->form_key = FrmAppHelper::get_unique_key( $name, 'frm_forms', 'form_key' ); | |
| 283 | 234 | } |
| 284 | 235 | } |
| 285 | 236 | |
| 286 | - /** | |
| 287 | - * @return void | |
| 288 | - */ | |
| 289 | 237 | public static function route() { |
| 290 | 238 | $action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; |
| 291 | 239 | $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); |
| 292 | 240 | FrmAppHelper::include_svg(); |
| @@ -291,22 +239,16 @@ | ||
| 291 | 239 | $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); |
| 292 | 240 | FrmAppHelper::include_svg(); |
| 293 | 241 | |
| 294 | 242 | if ( 'import_xml' === $action ) { |
| 295 | - self::import_xml(); | |
| 243 | + return self::import_xml(); | |
| 296 | 244 | } elseif ( 'export_xml' === $action ) { |
| 297 | - self::export_xml(); | |
| 245 | + return self::export_xml(); | |
| 298 | 246 | } elseif ( apply_filters( 'frm_xml_route', true, $action ) ) { |
| 299 | - self::form(); | |
| 247 | + return self::form(); | |
| 300 | 248 | } |
| 301 | 249 | } |
| 302 | 250 | |
| 303 | - /** | |
| 304 | - * @param string[] $errors | |
| 305 | - * @param string $message | |
| 306 | - * | |
| 307 | - * @return void | |
| 308 | - */ | |
| 309 | 251 | public static function form( $errors = array(), $message = '' ) { |
| 310 | 252 | $where = array( |
| 311 | 253 | 'status' => array( null, '', 'published' ), |
| 312 | 254 | ); |
| @@ -331,19 +273,16 @@ | ||
| 331 | 273 | ), |
| 332 | 274 | ); |
| 333 | 275 | $export_format = apply_filters( 'frm_export_formats', $export_format ); |
| 334 | 276 | |
| 335 | - include FrmAppHelper::plugin_path() . '/classes/views/xml/import_form.php'; | |
| 277 | + include( FrmAppHelper::plugin_path() . '/classes/views/xml/import_form.php' ); | |
| 336 | 278 | } |
| 337 | 279 | |
| 338 | - /** | |
| 339 | - * @return void | |
| 340 | - */ | |
| 341 | 280 | public static function import_xml() { |
| 342 | - $errors = array(); | |
| 343 | - $message = ''; | |
| 281 | + $errors = array(); | |
| 282 | + $message = ''; | |
| 283 | + | |
| 344 | 284 | $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'import-xml', 'import-xml-nonce' ); |
| 345 | - | |
| 346 | 285 | if ( false !== $permission_error ) { |
| 347 | 286 | $errors[] = $permission_error; |
| 348 | 287 | self::form( $errors ); |
| 349 | 288 | |
| @@ -349,11 +288,9 @@ | ||
| 349 | 288 | |
| 350 | 289 | return; |
| 351 | 290 | } |
| 352 | 291 | |
| 353 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 354 | - $has_file = ! empty( $_FILES['frm_import_file'] ) && ! empty( $_FILES['frm_import_file']['name'] ) && ! empty( $_FILES['frm_import_file']['size'] ) && (int) $_FILES['frm_import_file']['size'] > 0; | |
| 355 | - | |
| 292 | + $has_file = isset( $_FILES ) && isset( $_FILES['frm_import_file'] ) && ! empty( $_FILES['frm_import_file']['name'] ) && ! empty( $_FILES['frm_import_file']['size'] ) && (int) $_FILES['frm_import_file']['size'] > 0; | |
| 356 | 293 | if ( ! $has_file ) { |
| 357 | 294 | $errors[] = __( 'Oops, you didn\'t select a file.', 'formidable' ); |
| 358 | 295 | self::form( $errors ); |
| 359 | 296 | |
| @@ -359,9 +296,9 @@ | ||
| 359 | 296 | |
| 360 | 297 | return; |
| 361 | 298 | } |
| 362 | 299 | |
| 363 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Missing | |
| 300 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 364 | 301 | $file = isset( $_FILES['frm_import_file']['tmp_name'] ) ? sanitize_option( 'upload_path', $_FILES['frm_import_file']['tmp_name'] ) : ''; |
| 365 | 302 | |
| 366 | 303 | if ( ! is_uploaded_file( $file ) ) { |
| 367 | 304 | unset( $file ); |
| @@ -370,8 +307,10 @@ | ||
| 370 | 307 | |
| 371 | 308 | return; |
| 372 | 309 | } |
| 373 | 310 | |
| 311 | + //add_filter('upload_mimes', 'FrmXMLController::allow_mime'); | |
| 312 | + | |
| 374 | 313 | $export_format = array( |
| 375 | 314 | 'xml' => array( |
| 376 | 315 | 'name' => 'XML', |
| 377 | 316 | 'support' => 'forms', |
| @@ -379,14 +318,13 @@ | ||
| 379 | 318 | ), |
| 380 | 319 | ); |
| 381 | 320 | $export_format = apply_filters( 'frm_export_formats', $export_format ); |
| 382 | 321 | |
| 383 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Missing | |
| 322 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 384 | 323 | $file_type = sanitize_option( 'upload_path', $_FILES['frm_import_file']['name'] ); |
| 385 | 324 | $file_type = strtolower( pathinfo( $file_type, PATHINFO_EXTENSION ) ); |
| 386 | - | |
| 387 | 325 | if ( 'xml' !== $file_type && isset( $export_format[ $file_type ] ) ) { |
| 388 | - // Allow other file types to be imported | |
| 326 | + // allow other file types to be imported | |
| 389 | 327 | do_action( 'frm_before_import_' . $file_type ); |
| 390 | 328 | |
| 391 | 329 | return; |
| 392 | 330 | } |
| @@ -400,9 +338,10 @@ | ||
| 400 | 338 | } |
| 401 | 339 | |
| 402 | 340 | $set_err = libxml_use_internal_errors( true ); |
| 403 | 341 | $loader = FrmXMLHelper::maybe_libxml_disable_entity_loader( true ); |
| 404 | - $result = FrmXMLHelper::import_xml( $file ); | |
| 342 | + | |
| 343 | + $result = FrmXMLHelper::import_xml( $file ); | |
| 405 | 344 | FrmXMLHelper::parse_message( $result, $message, $errors ); |
| 406 | 345 | |
| 407 | 346 | unset( $file ); |
| 408 | 347 | |
| @@ -411,15 +350,11 @@ | ||
| 411 | 350 | |
| 412 | 351 | self::form( $errors, $message ); |
| 413 | 352 | } |
| 414 | 353 | |
| 415 | - /** | |
| 416 | - * @return void | |
| 417 | - */ | |
| 418 | 354 | public static function export_xml() { |
| 419 | 355 | $error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'export-xml', 'export-xml-nonce' ); |
| 420 | - | |
| 421 | - if ( $error ) { | |
| 356 | + if ( ! empty( $error ) ) { | |
| 422 | 357 | wp_die( esc_html( $error ) ); |
| 423 | 358 | } |
| 424 | 359 | |
| 425 | 360 | $ids = FrmAppHelper::get_post_param( 'frm_export_forms', array(), 'sanitize_text_field' ); |
| @@ -426,9 +361,9 @@ | ||
| 426 | 361 | $type = FrmAppHelper::get_post_param( 'type', array(), 'sanitize_text_field' ); |
| 427 | 362 | $format = FrmAppHelper::get_post_param( 'format', 'xml', 'sanitize_title' ); |
| 428 | 363 | |
| 429 | 364 | if ( ! headers_sent() && ! $type ) { |
| 430 | - wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=formidable-import' ) ) ); | |
| 365 | + wp_redirect( esc_url_raw( admin_url( 'admin.php?page=formidable-import' ) ) ); | |
| 431 | 366 | die(); |
| 432 | 367 | } |
| 433 | 368 | |
| 434 | 369 | if ( 'xml' === $format ) { |
| @@ -441,28 +376,13 @@ | ||
| 441 | 376 | |
| 442 | 377 | wp_die(); |
| 443 | 378 | } |
| 444 | 379 | |
| 445 | - /** | |
| 446 | - * @param array<string>|string $type | |
| 447 | - * @param array $args | |
| 448 | - * | |
| 449 | - * @psalm-param array{ids?: mixed} $args | |
| 450 | - * | |
| 451 | - * @return void | |
| 452 | - */ | |
| 453 | - public static function generate_xml( $type, $args = array() ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 380 | + public static function generate_xml( $type, $args = array() ) { | |
| 454 | 381 | global $wpdb; |
| 455 | 382 | |
| 456 | 383 | self::prepare_types_array( $type ); |
| 457 | 384 | |
| 458 | - if ( ! is_array( $type ) ) { | |
| 459 | - // This shouldn't be possible. | |
| 460 | - // It is cast to array in prepare_types_array. | |
| 461 | - // This is just for static analysis. | |
| 462 | - return; | |
| 463 | - } | |
| 464 | - | |
| 465 | 385 | $tables = array( |
| 466 | 386 | 'items' => $wpdb->prefix . 'frm_items', |
| 467 | 387 | 'forms' => $wpdb->prefix . 'frm_forms', |
| 468 | 388 | 'posts' => $wpdb->posts, |
| @@ -475,9 +395,9 @@ | ||
| 475 | 395 | ); |
| 476 | 396 | $args = wp_parse_args( $args, $defaults ); |
| 477 | 397 | |
| 478 | 398 | // Make sure ids are numeric. |
| 479 | - if ( is_array( $args['ids'] ) && $args['ids'] ) { | |
| 399 | + if ( is_array( $args['ids'] ) && ! empty( $args['ids'] ) ) { | |
| 480 | 400 | $args['ids'] = array_filter( $args['ids'], 'is_numeric' ); |
| 481 | 401 | } |
| 482 | 402 | |
| 483 | 403 | $records = array(); |
| @@ -482,17 +402,18 @@ | ||
| 482 | 402 | |
| 483 | 403 | $records = array(); |
| 484 | 404 | |
| 485 | 405 | foreach ( $type as $tb_type ) { |
| 486 | - $where = array(); | |
| 487 | - $join = ''; | |
| 488 | - $table = $tables[ $tb_type ]; | |
| 406 | + $where = array(); | |
| 407 | + $join = ''; | |
| 408 | + $table = $tables[ $tb_type ]; | |
| 409 | + | |
| 489 | 410 | $select = $table . '.id'; |
| 490 | 411 | $query_vars = array(); |
| 491 | 412 | |
| 492 | 413 | switch ( $tb_type ) { |
| 493 | 414 | case 'forms': |
| 494 | - // Add forms. | |
| 415 | + //add forms | |
| 495 | 416 | if ( $args['ids'] ) { |
| 496 | 417 | $where[] = array( |
| 497 | 418 | 'or' => 1, |
| 498 | 419 | $table . '.id' => $args['ids'], |
| @@ -504,9 +425,8 @@ | ||
| 504 | 425 | break; |
| 505 | 426 | case 'actions': |
| 506 | 427 | $select = $table . '.ID'; |
| 507 | 428 | $where['post_type'] = FrmFormActionsController::$action_post_type; |
| 508 | - | |
| 509 | 429 | if ( ! empty( $args['ids'] ) ) { |
| 510 | 430 | $where['menu_order'] = $args['ids']; |
| 511 | 431 | } |
| 512 | 432 | break; |
| @@ -521,12 +441,10 @@ | ||
| 521 | 441 | $frm_style = new FrmStyle(); |
| 522 | 442 | $default_style = $frm_style->get_default_style(); |
| 523 | 443 | $form_ids = $args['ids']; |
| 524 | 444 | $style_ids = array(); |
| 525 | - | |
| 526 | 445 | foreach ( $form_ids as $form_id ) { |
| 527 | 446 | $form_data = FrmForm::getOne( $form_id ); |
| 528 | - | |
| 529 | 447 | // For forms that have not been updated while running 2.0, check if custom_style is set. |
| 530 | 448 | if ( isset( $form_data->options['custom_style'] ) ) { |
| 531 | 449 | if ( 1 === absint( $form_data->options['custom_style'] ) ) { |
| 532 | 450 | $style_ids[] = $default_style->ID; |
| @@ -535,14 +453,13 @@ | ||
| 535 | 453 | } |
| 536 | 454 | } |
| 537 | 455 | unset( $form_id, $form_data ); |
| 538 | 456 | } |
| 539 | - | |
| 540 | 457 | $select = $table . '.ID'; |
| 541 | 458 | $where['post_type'] = 'frm_styles'; |
| 542 | 459 | |
| 543 | 460 | // Only export selected styles. |
| 544 | - if ( $style_ids ) { | |
| 461 | + if ( ! empty( $style_ids ) ) { | |
| 545 | 462 | $where['ID'] = $style_ids; |
| 546 | 463 | } |
| 547 | 464 | break; |
| 548 | 465 | default: |
| @@ -554,15 +471,15 @@ | ||
| 554 | 471 | $where['pm.meta_value >'] = 1; |
| 555 | 472 | } else { |
| 556 | 473 | $where['pm.meta_value'] = $args['ids']; |
| 557 | 474 | } |
| 558 | - }//end switch | |
| 475 | + } | |
| 559 | 476 | |
| 560 | 477 | $records[ $tb_type ] = FrmDb::get_col( $table . $join, $where, $select ); |
| 561 | 478 | unset( $tb_type ); |
| 562 | - }//end foreach | |
| 479 | + } | |
| 563 | 480 | |
| 564 | - $filename = self::get_file_name( $args, $records ); | |
| 481 | + $filename = self::get_file_name( $args, $type, $records ); | |
| 565 | 482 | |
| 566 | 483 | header( 'Content-Description: File Transfer' ); |
| 567 | 484 | header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 568 | 485 | header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); |
| @@ -567,50 +484,20 @@ | ||
| 567 | 484 | header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 568 | 485 | header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); |
| 569 | 486 | |
| 570 | 487 | echo '<?xml version="1.0" encoding="' . esc_attr( get_bloginfo( 'charset' ) ) . "\" ?>\n"; |
| 571 | - include FrmAppHelper::plugin_path() . '/classes/views/xml/xml.php'; | |
| 488 | + include( FrmAppHelper::plugin_path() . '/classes/views/xml/xml.php' ); | |
| 572 | 489 | } |
| 573 | 490 | |
| 574 | - /** | |
| 575 | - * Returns an array that has parent term slugs for the terms provided. | |
| 576 | - * | |
| 577 | - * @since 6.8.3 | |
| 578 | - * | |
| 579 | - * @param array $terms | |
| 580 | - * | |
| 581 | - * @return array | |
| 582 | - */ | |
| 583 | - public static function get_parent_terms_slugs( $terms ) { | |
| 584 | - $parent_term_ids = array_filter( array_unique( wp_list_pluck( $terms, 'parent' ) ) ); | |
| 585 | - $parent_slugs = array(); | |
| 586 | - | |
| 587 | - if ( ! $parent_term_ids ) { | |
| 588 | - return $parent_slugs; | |
| 589 | - } | |
| 590 | - | |
| 591 | - $results = FrmDb::get_results( 'terms', array( 'term_id' => $parent_term_ids ), 'term_id, slug' ); | |
| 592 | - | |
| 593 | - return wp_list_pluck( $results, 'slug', 'term_id' ); | |
| 594 | - } | |
| 595 | - | |
| 596 | - /** | |
| 597 | - * Prepare the types array. | |
| 598 | - * | |
| 599 | - * @param array<string>|string $type | |
| 600 | - * | |
| 601 | - * @return void | |
| 602 | - */ | |
| 603 | 491 | private static function prepare_types_array( &$type ) { |
| 604 | 492 | $type = (array) $type; |
| 605 | - | |
| 606 | - if ( ! in_array( 'forms', $type, true ) && ( in_array( 'items', $type, true ) || in_array( 'posts', $type, true ) ) ) { | |
| 607 | - // Make sure the form is included if there are entries | |
| 493 | + if ( ! in_array( 'forms', $type ) && ( in_array( 'items', $type ) || in_array( 'posts', $type ) ) ) { | |
| 494 | + // make sure the form is included if there are entries | |
| 608 | 495 | $type[] = 'forms'; |
| 609 | 496 | } |
| 610 | 497 | |
| 611 | - if ( in_array( 'forms', $type, true ) ) { | |
| 612 | - // Include actions with forms | |
| 498 | + if ( in_array( 'forms', $type ) ) { | |
| 499 | + // include actions with forms | |
| 613 | 500 | $type[] = 'actions'; |
| 614 | 501 | } |
| 615 | 502 | } |
| 616 | 503 | |
| @@ -619,28 +506,22 @@ | ||
| 619 | 506 | * Use the nme of the form if only one form is exported. |
| 620 | 507 | * |
| 621 | 508 | * @since 3.06 |
| 622 | 509 | * |
| 623 | - * @param array $args | |
| 624 | - * @param array $records | |
| 625 | - * | |
| 626 | 510 | * @return string |
| 627 | 511 | */ |
| 628 | - private static function get_file_name( $args, $records ) { | |
| 629 | - $has_one_form = ! empty( $records['forms'] ) && count( $args['ids'] ) === 1; | |
| 630 | - | |
| 512 | + private static function get_file_name( $args, $type, $records ) { | |
| 513 | + $has_one_form = isset( $records['forms'] ) && ! empty( $records['forms'] ) && count( $args['ids'] ) === 1; | |
| 631 | 514 | if ( $has_one_form ) { |
| 632 | - // One form is being exported | |
| 515 | + // one form is being exported | |
| 633 | 516 | $selected_form_id = reset( $args['ids'] ); |
| 634 | 517 | $filename = 'form-' . $selected_form_id . '.xml'; |
| 635 | 518 | |
| 636 | 519 | foreach ( $records['forms'] as $form_id ) { |
| 637 | 520 | $filename = 'form-' . $form_id . '.xml'; |
| 638 | - | |
| 639 | 521 | if ( $selected_form_id === $form_id ) { |
| 640 | 522 | $form = FrmForm::getOne( $form_id ); |
| 641 | - $filename = $form->name !== '' ? $form->name : $form->form_key; | |
| 642 | - $filename = sanitize_title( $filename ) . '-form.xml'; | |
| 523 | + $filename = sanitize_title( $form->name ) . '-form.xml'; | |
| 643 | 524 | break; |
| 644 | 525 | } |
| 645 | 526 | } |
| 646 | 527 | } else { |
| @@ -645,32 +526,20 @@ | ||
| 645 | 526 | } |
| 646 | 527 | } else { |
| 647 | 528 | $sitename = sanitize_key( get_bloginfo( 'name' ) ); |
| 648 | 529 | |
| 649 | - if ( $sitename ) { | |
| 530 | + if ( ! empty( $sitename ) ) { | |
| 650 | 531 | $sitename .= '.'; |
| 651 | 532 | } |
| 652 | - | |
| 653 | 533 | $filename = $sitename . 'formidable.' . gmdate( 'Y-m-d' ) . '.xml'; |
| 654 | - }//end if | |
| 534 | + } | |
| 655 | 535 | |
| 656 | - /** | |
| 657 | - * @since 5.3 | |
| 658 | - * | |
| 659 | - * @param string $filename | |
| 660 | - */ | |
| 661 | - return apply_filters( 'frm_xml_filename', $filename ); | |
| 536 | + return $filename; | |
| 662 | 537 | } |
| 663 | 538 | |
| 664 | - /** | |
| 665 | - * @param array $atts | |
| 666 | - * | |
| 667 | - * @return void | |
| 668 | - */ | |
| 669 | 539 | public static function generate_csv( $atts ) { |
| 670 | 540 | $form_ids = $atts['ids']; |
| 671 | - | |
| 672 | - if ( ! $form_ids ) { | |
| 541 | + if ( empty( $form_ids ) ) { | |
| 673 | 542 | wp_die( esc_html__( 'Please select a form', 'formidable' ) ); |
| 674 | 543 | } |
| 675 | 544 | self::csv( reset( $form_ids ) ); |
| 676 | 545 | } |
| @@ -678,14 +547,8 @@ | ||
| 678 | 547 | /** |
| 679 | 548 | * Export to CSV |
| 680 | 549 | * |
| 681 | 550 | * @since 2.0.19 |
| 682 | - * | |
| 683 | - * @param false|int|string $form_id | |
| 684 | - * @param string $search | |
| 685 | - * @param string $fid | |
| 686 | - * | |
| 687 | - * @return void | |
| 688 | 551 | */ |
| 689 | 552 | public static function csv( $form_id = false, $search = '', $fid = '' ) { |
| 690 | 553 | FrmAppHelper::permission_check( 'frm_view_entries' ); |
| 691 | 554 | |
| @@ -690,19 +553,14 @@ | ||
| 690 | 553 | FrmAppHelper::permission_check( 'frm_view_entries' ); |
| 691 | 554 | |
| 692 | 555 | if ( ! $form_id ) { |
| 693 | 556 | $form_id = FrmAppHelper::get_param( 'form', '', 'get', 'sanitize_text_field' ); |
| 694 | - $search = FrmAppHelper::get_param( isset( $_REQUEST['s'] ) ? 's' : 'search', '', 'get', 'sanitize_text_field' ); | |
| 557 | + $search = FrmAppHelper::get_param( ( isset( $_REQUEST['s'] ) ? 's' : 'search' ), '', 'get', 'sanitize_text_field' ); | |
| 695 | 558 | $fid = FrmAppHelper::get_param( 'fid', '', 'get', 'sanitize_text_field' ); |
| 696 | 559 | } |
| 697 | 560 | |
| 698 | - // Remove time limit to execute this function. | |
| 699 | - if ( function_exists( 'set_time_limit' ) ) { | |
| 700 | - set_time_limit( 0 ); | |
| 701 | - } | |
| 702 | - | |
| 561 | + set_time_limit( 0 ); //Remove time limit to execute this function | |
| 703 | 562 | $mem_limit = str_replace( 'M', '', ini_get( 'memory_limit' ) ); |
| 704 | - | |
| 705 | 563 | if ( (int) $mem_limit < 256 ) { |
| 706 | 564 | wp_raise_memory_limit(); |
| 707 | 565 | } |
| 708 | 566 | |
| @@ -707,20 +565,15 @@ | ||
| 707 | 565 | } |
| 708 | 566 | |
| 709 | 567 | global $wpdb; |
| 710 | 568 | |
| 711 | - $form = FrmForm::getOne( $form_id ); | |
| 569 | + $form = FrmForm::getOne( $form_id ); | |
| 570 | + $form_id = $form->id; | |
| 712 | 571 | |
| 713 | - if ( ! $form ) { | |
| 714 | - esc_html_e( 'Form not found.', 'formidable' ); | |
| 715 | - wp_die(); | |
| 716 | - } | |
| 717 | - | |
| 718 | - $form_id = $form->id; | |
| 719 | 572 | $form_cols = self::get_fields_for_csv_export( $form_id, $form ); |
| 720 | - $item_id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'sanitize_text_field' ); | |
| 721 | 573 | |
| 722 | - if ( $item_id ) { | |
| 574 | + $item_id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'sanitize_text_field' ); | |
| 575 | + if ( ! empty( $item_id ) ) { | |
| 723 | 576 | $item_id = explode( ',', $item_id ); |
| 724 | 577 | } |
| 725 | 578 | |
| 726 | 579 | $query = array( |
| @@ -741,12 +594,12 @@ | ||
| 741 | 594 | |
| 742 | 595 | $entry_ids = FrmDb::get_col( $wpdb->prefix . 'frm_items it', $query ); |
| 743 | 596 | unset( $query ); |
| 744 | 597 | |
| 745 | - if ( $entry_ids ) { | |
| 598 | + if ( empty( $entry_ids ) ) { | |
| 599 | + esc_html_e( 'There are no entries for that form.', 'formidable' ); | |
| 600 | + } else { | |
| 746 | 601 | FrmCSVExportHelper::generate_csv( compact( 'form', 'entry_ids', 'form_cols' ) ); |
| 747 | - } else { | |
| 748 | - esc_html_e( 'There are no entries for that form.', 'formidable' ); | |
| 749 | 602 | } |
| 750 | 603 | |
| 751 | 604 | wp_die(); |
| 752 | 605 | } |
| @@ -754,19 +607,17 @@ | ||
| 754 | 607 | /** |
| 755 | 608 | * Get the fields that should be included in the CSV export |
| 756 | 609 | * |
| 757 | 610 | * @since 2.0.19 |
| 758 | - * @since 5.0.16 function went from private to public. | |
| 759 | 611 | * |
| 760 | - * @param int $form_id | |
| 612 | + * @param int $form_id | |
| 761 | 613 | * @param object $form |
| 762 | 614 | * |
| 763 | - * @return array CSV fields. | |
| 615 | + * @return array $csv_fields | |
| 764 | 616 | */ |
| 765 | - public static function get_fields_for_csv_export( $form_id, $form ) { | |
| 617 | + private static function get_fields_for_csv_export( $form_id, $form ) { | |
| 766 | 618 | $csv_fields = FrmField::get_all_for_form( $form_id, '', 'include', 'include' ); |
| 767 | 619 | $no_export_fields = FrmField::no_save_fields(); |
| 768 | - | |
| 769 | 620 | foreach ( $csv_fields as $k => $f ) { |
| 770 | 621 | if ( in_array( $f->type, $no_export_fields, true ) ) { |
| 771 | 622 | unset( $csv_fields[ $k ] ); |
| 772 | 623 | } |
| @@ -771,24 +622,19 @@ | ||
| 771 | 622 | unset( $csv_fields[ $k ] ); |
| 772 | 623 | } |
| 773 | 624 | } |
| 774 | 625 | |
| 775 | - return apply_filters( 'frm_fields_for_csv_export', $csv_fields, compact( 'form' ) ); | |
| 626 | + return $csv_fields; | |
| 776 | 627 | } |
| 777 | 628 | |
| 778 | - /** | |
| 779 | - * @param array $mimes | |
| 780 | - * | |
| 781 | - * @return array | |
| 782 | - */ | |
| 783 | 629 | public static function allow_mime( $mimes ) { |
| 784 | 630 | if ( ! isset( $mimes['csv'] ) ) { |
| 785 | - // Allow csv files | |
| 631 | + // allow csv files | |
| 786 | 632 | $mimes['csv'] = 'text/csv'; |
| 787 | 633 | } |
| 788 | 634 | |
| 789 | 635 | if ( ! isset( $mimes['xml'] ) ) { |
| 790 | - // Allow xml | |
| 636 | + // allow xml | |
| 791 | 637 | $mimes['xml'] = 'text/xml'; |
| 792 | 638 | } |
| 793 | 639 | |
| 794 | 640 | return $mimes; |