PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.1
Shortcoder — Create Shortcodes for Anything v4.1
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
110 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 '<input type="file" name="import" id="import" accept="text/plain"/>';
16 wp_nonce_field( 'sc_import_data' );
17 echo '<input type="submit" />';
18 echo '</form>';
19
20 }
21
22 public static function check_import(){
23
24 if( isset( $_POST ) && !empty( $_FILES[ 'import' ][ 'tmp_name' ] ) ){
25
26 check_admin_referer( 'sc_import_data' );
27
28 $file = wp_import_handle_upload();
29
30 if ( isset( $file['error'] ) ){
31 self::print_notice( __( 'Failed to import file. Error: ', 'shortcoder' ) . $file['error'] );
32 return false;
33 }
34
35 $file_id = absint( $file['id'] );
36 $file_path = get_attached_file( $file_id );
37
38 self::do_import( $file_path );
39
40 }
41
42 }
43
44 public static function do_import( $file_path ){
45
46 if ( !is_file( $file_path ) ){
47 self::print_notice( __( 'Uploaded file does not exist for import. ', 'shortcoder' ) . $file_path );
48 }
49
50 $imported_json = file_get_contents( $file_path );
51 $imported_data = json_decode( $imported_json, true );
52
53 if( $imported_data && !empty( $imported_data ) ){
54
55 $shortcodes = Shortcoder::list_all();
56 $import_count = 0;
57
58 if( isset( $imported_data[ 'shortcodes' ] ) ){
59
60 foreach( $imported_data[ 'shortcodes' ] as $name => $content ){
61 $shortcodes[ $name ] = wp_parse_args( $content, Shortcoder::defaults() );
62 $import_count++;
63 }
64
65 if( update_option( 'shortcoder_data', $shortcodes ) ){
66 self::print_notice( $import_count . __( ' shortcodes imported successfully !', 'shortcoder' ), 'success' );
67 }else{
68 self::print_notice( __( 'shortcodes are not updated because all the shortcodes remain the same.', 'shortcoder' ) );
69 return false;
70 }
71
72 }
73
74 }else{
75 self:print_notice( __( 'Failed to decode JSON in imported data.', 'shortcoder' ) );
76 }
77
78 }
79
80 public static function do_export(){
81
82 check_admin_referer( 'sc_export_data' );
83
84 $export_file_name = 'shortcoder export ' . date( 'm/d/Y' ) . '.txt';
85 $shortcodes = Shortcoder::list_all();
86
87 $to_export = array(
88 'shortcodes' => $shortcodes
89 );
90
91 $export_json = json_encode( $to_export );
92
93 header('Content-Disposition: attachment; filename="' . $export_file_name . '"');
94 header('Content-Type: text/plain');
95 header('Content-Length: ' . strlen( $export_json ));
96 header('Connection: close');
97
98 echo $export_json;
99
100 }
101
102 public static function print_notice( $msg, $type = 'error' ){
103 echo '<div class="notice notice-' . $type . ' is-dismissible"><p>' . $msg . '</p></div>';
104 }
105
106 }
107
108 Shortcoder_Import::init();
109
110 ?>