import.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | class Shortcoder_Import{ |
| 4 | |
| 5 | public static function init(){ |
| 6 | |
| 7 | add_action( 'wp_ajax_sc_export', array( __CLASS__, 'do_export' ) ); |
| 8 | |
| 9 | } |
| 10 | |
| 11 | public static function import_form(){ |
| 12 | |
| 13 | echo '<form method="post" enctype="multipart/form-data" id="import_form">'; |
| 14 | echo '<p class="import_desc">' . __( 'Are you sure want to import shortcodes ?', 'shortcoder' ) . '</p>'; |
| 15 | echo '<p class="import_desc2">' . __( 'Do you want to delete existing shortcodes and perform a clean import ? (selecting "cancel" will overwrite existing shortcodes)', 'shortcoder' ) . '</p>'; |
| 16 | echo '<input type="checkbox" name="fresh_import" id="fresh_import" value="1" />'; |
| 17 | echo '<input type="file" name="import" id="import" accept="text/plain"/>'; |
| 18 | wp_nonce_field( 'sc_import_data' ); |
| 19 | echo '<input type="submit" />'; |
| 20 | echo '</form>'; |
| 21 | |
| 22 | } |
| 23 | |
| 24 | public static function check_import(){ |
| 25 | |
| 26 | if( isset( $_POST ) && !empty( $_FILES[ 'import' ][ 'tmp_name' ] ) ){ |
| 27 | |
| 28 | check_admin_referer( 'sc_import_data' ); |
| 29 | |
| 30 | $file = wp_import_handle_upload(); |
| 31 | |
| 32 | if ( isset( $file['error'] ) ){ |
| 33 | self::print_notice( __( 'Failed to import file. Error: ', 'shortcoder' ) . $file['error'] ); |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | $file_id = absint( $file['id'] ); |
| 38 | $file_path = get_attached_file( $file_id ); |
| 39 | $fresh_import = isset( $_POST[ 'fresh_import' ] ) && $_POST[ 'fresh_import' ] == '1' ? true : false; |
| 40 | |
| 41 | self::do_import( $file_path, $fresh_import ); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | } |
| 46 | |
| 47 | public static function do_import( $file_path, $fresh_import = false ){ |
| 48 | |
| 49 | if ( !is_file( $file_path ) ){ |
| 50 | self::print_notice( __( 'Uploaded file does not exist for import. ', 'shortcoder' ) . $file_path ); |
| 51 | } |
| 52 | |
| 53 | $imported_json = utf8_encode( file_get_contents( $file_path ) ); |
| 54 | $imported_json = ( substr($imported_json, -1) == '0' ) ? substr($imported_json, 0 , -1) : $imported_json; |
| 55 | $imported_data = json_decode( $imported_json, true ); |
| 56 | |
| 57 | if( $imported_data && !empty( $imported_data ) ){ |
| 58 | |
| 59 | $shortcodes = $fresh_import ? array() : Shortcoder::list_all(); |
| 60 | $import_count = 0; |
| 61 | |
| 62 | if( isset( $imported_data[ 'shortcodes' ] ) ){ |
| 63 | |
| 64 | foreach( $imported_data[ 'shortcodes' ] as $name => $content ){ |
| 65 | $shortcodes[ $name ] = wp_parse_args( $content, Shortcoder::defaults() ); |
| 66 | $import_count++; |
| 67 | } |
| 68 | |
| 69 | if( update_option( 'shortcoder_data', $shortcodes ) ){ |
| 70 | self::print_notice( $import_count . __( ' shortcodes imported successfully !', 'shortcoder' ), 'success' ); |
| 71 | }else{ |
| 72 | self::print_notice( __( 'Shortcodes are not updated because all the shortcodes remain the same.', 'shortcoder' ) ); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | }else{ |
| 79 | self::print_notice( __( 'Failed to decode JSON in imported data. Error code: ', 'shortcoder' ) . json_last_error() ); |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | |
| 84 | public static function do_export(){ |
| 85 | |
| 86 | check_admin_referer( 'sc_export_data' ); |
| 87 | |
| 88 | $export_file_name = 'shortcoder export ' . date( 'm/d/Y' ) . '.txt'; |
| 89 | $shortcodes = Shortcoder::list_all(); |
| 90 | |
| 91 | $to_export = array( |
| 92 | 'shortcodes' => $shortcodes |
| 93 | ); |
| 94 | |
| 95 | $export_json = json_encode( $to_export ); |
| 96 | |
| 97 | header('Content-Disposition: attachment; filename="' . $export_file_name . '"'); |
| 98 | header('Content-Type: text/plain'); |
| 99 | header('Content-Length: ' . strlen( $export_json )); |
| 100 | header('Connection: close'); |
| 101 | |
| 102 | echo $export_json; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | public static function print_notice( $msg, $type = 'error' ){ |
| 107 | echo '<div class="notice notice-' . $type . ' is-dismissible"><p>' . $msg . '</p></div>'; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | Shortcoder_Import::init(); |
| 113 | |
| 114 | ?> |