PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.1.5
Shortcoder — Create Shortcodes for Anything v4.1.5
trunk 3.0 3.0.1 3.1 3.2 3.3 3.4 3.4.1 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2 4.3 4.4 4.5 4.6 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.1 5.2 5.2.1 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.4 5.5 5.6 5.7 5.8 6.0 6.1 6.2 6.3 6.3.1 6.3.2 6.4 6.5 6.5.1 6.5.2 6.5.3
shortcoder / includes / import.php
shortcoder / includes Last commit date
import.php 8 years ago metadata.php 8 years ago
import.php
113 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_data = json_decode( $imported_json, true );
55
56 if( $imported_data && !empty( $imported_data ) ){
57
58 $shortcodes = $fresh_import ? array() : Shortcoder::list_all();
59 $import_count = 0;
60
61 if( isset( $imported_data[ 'shortcodes' ] ) ){
62
63 foreach( $imported_data[ 'shortcodes' ] as $name => $content ){
64 $shortcodes[ $name ] = wp_parse_args( $content, Shortcoder::defaults() );
65 $import_count++;
66 }
67
68 if( update_option( 'shortcoder_data', $shortcodes ) ){
69 self::print_notice( $import_count . __( ' shortcodes imported successfully !', 'shortcoder' ), 'success' );
70 }else{
71 self::print_notice( __( 'shortcodes are not updated because all the shortcodes remain the same.', 'shortcoder' ) );
72 return false;
73 }
74
75 }
76
77 }else{
78 self::print_notice( __( 'Failed to decode JSON in imported data. Error code: ', 'shortcoder' ) . json_last_error() );
79 }
80
81 }
82
83 public static function do_export(){
84
85 check_admin_referer( 'sc_export_data' );
86
87 $export_file_name = 'shortcoder export ' . date( 'm/d/Y' ) . '.txt';
88 $shortcodes = Shortcoder::list_all();
89
90 $to_export = array(
91 'shortcodes' => $shortcodes
92 );
93
94 $export_json = json_encode( $to_export );
95
96 header('Content-Disposition: attachment; filename="' . $export_file_name . '"');
97 header('Content-Type: text/plain');
98 header('Content-Length: ' . strlen( $export_json ));
99 header('Connection: close');
100
101 echo $export_json;
102 return;
103 }
104
105 public static function print_notice( $msg, $type = 'error' ){
106 echo '<div class="notice notice-' . $type . ' is-dismissible"><p>' . $msg . '</p></div>';
107 }
108
109 }
110
111 Shortcoder_Import::init();
112
113 ?>