PluginProbe ʕ •ᴥ•ʔ
Code Manager / 1.0.16
Code Manager v1.0.16
1.0.47 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9
code-manager / Code_Manager / Code_Manager_Export.php
code-manager / Code_Manager Last commit date
Code_Manager.php 4 years ago Code_Manager_Dashboard.php 4 years ago Code_Manager_Export.php 4 years ago Code_Manager_Form.php 4 years ago Code_Manager_Import.php 4 years ago Code_Manager_Import_File.php 4 years ago Code_Manager_List.php 4 years ago Code_Manager_List_View.php 4 years ago Code_Manager_Model.php 4 years ago Code_Manager_Preview.php 4 years ago Code_Manager_Settings.php 4 years ago Code_Manager_Tabs.php 4 years ago Message_Box.php 4 years ago WP_List_Table.php 4 years ago
Code_Manager_Export.php
122 lines
1 <?php
2 /**
3 * Code Manager export code
4 *
5 * @package Code_Manager
6 */
7
8 namespace Code_Manager {
9
10 /**
11 * Class Code_Manager_Export
12 *
13 * Add export feature to plugin (reached from list table).
14 *
15 * @author Peter Schulz
16 * @since 1.0.0
17 */
18 class Code_Manager_Export {
19
20 /**
21 * Start code export
22 *
23 * @return void
24 */
25 public static function export() {
26 $wp_nonce = isset( $_REQUEST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpnonce'] ) ) : ''; // input var okay.
27 if ( ! wp_verify_nonce( $wp_nonce, 'code-manager-export' . Code_manager::get_current_user_login() ) ) {
28 wp_die( __( 'ERROR: Not authorized', 'code-manager' ) );
29 }
30 if ( isset( $_REQUEST['cid'] ) && is_array( $_REQUEST['cid'] ) ) {
31 self::export_rows( wp_unslash( $_REQUEST['cid'] ) );
32 }
33 }
34
35 /**
36 * Start export from ajax request
37 *
38 * @return void
39 */
40 public static function export_ajax() {
41 self::export();
42 wp_die();
43 }
44
45 /**
46 * Export table row = code block
47 *
48 * @param array $code_ids Array of code IDs.
49 * @return void
50 */
51 protected static function export_rows( $code_ids ) {
52 if ( defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
53 $wp_memory_limit = WP_MAX_MEMORY_LIMIT;
54 $current_memory_limit = @ini_set( 'memory_limit' );
55 if ( false === $current_memory_limit ||
56 self::convert_memory_to_decimal( $current_memory_limit ) <
57 self::convert_memory_to_decimal( $wp_memory_limit )
58 ) {
59 @ini_set( 'memory_limit', $wp_memory_limit );
60 }
61 }
62
63 header( 'Content-type: text/plain; charset=utf-8' );
64 header( "Content-Disposition: attachment; filename=code_manager_export.sql" );
65 header( 'Pragma: no-cache' );
66 header( 'Expires: 0' );
67
68 echo "--\n";
69 echo "-- Code Manager table export\n";
70 echo "-- Code IDs are not exported. New IDs are generated on import.\n";
71 echo "--\n";
72
73 $valid_ids = array(); // Hold sanitized array values.
74 $in_operator = ''; // Placeholders used for IN operator.
75 foreach ( $code_ids as $code_id ) {
76 $valid_ids[] = sanitize_text_field( wp_unslash( $code_id ) );
77 if ( '' !== $in_operator ) {
78 $in_operator .= ',';
79 }
80 $in_operator .= '%d';
81 }
82
83 global $wpdb;
84 $query = 'select * from ' . Code_Manager_Model::get_base_table_name() . " where code_id in ({$in_operator})";
85 $rows = $wpdb->get_results( $wpdb->prepare( $query, $valid_ids ), 'ARRAY_A' );
86
87 foreach ( $rows as $row ) {
88 $table_name = Code_Manager_Model::BASE_TABLE_NAME;
89 $code = str_replace( "\t", "\\t", $wpdb->remove_placeholder_escape( esc_sql( $row['code'] ) ) );
90 $code_description = str_replace( "\t", "\\t", $wpdb->remove_placeholder_escape( esc_sql( $row['code_description'] ) ) );
91 $insert =
92 "insert into {wp_prefix}{$table_name} " .
93 '(code_name, code_type, code, code_author, code_description) ' .
94 'values ' .
95 "('{$row['code_name']}','{$row['code_type']}','{$code}','{$row['code_author']}','{$code_description}');\n";
96
97 echo $insert;
98 }
99 }
100
101 /**
102 * Convert bytes to decimal
103 *
104 * @param string $memory_value Memory in G|M|K bytes.
105 * @return float|int|void
106 */
107 public static function convert_memory_to_decimal( $memory_value ) {
108 if ( preg_match( '/^(\d+)(.)$/', $memory_value, $matches ) ) {
109 if ( 'G' === $matches[2] ) {
110 return $matches[1] * 1024 * 1024 * 1024;
111 } elseif ( 'M' === $matches[2] ) {
112 return $matches[1] * 1024 * 1024;
113 } elseif ( 'K' === $matches[2] ) {
114 return $matches[1] * 1024;
115 }
116 }
117 }
118
119 }
120
121 }
122