column
12 years ago
storage_model
12 years ago
api.php
12 years ago
column.php
12 years ago
deprecated.php
12 years ago
export_import.php
12 years ago
settings.php
12 years ago
storage_model.php
12 years ago
third_party.php
12 years ago
upgrade.php
12 years ago
utility.php
12 years ago
export_import.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * CPAC_Export_Import Class |
| 5 | * |
| 6 | * @since 1.4.6.5 |
| 7 | * |
| 8 | */ |
| 9 | class CPAC_Export_Import { |
| 10 | |
| 11 | private $cpac; |
| 12 | |
| 13 | /** |
| 14 | * Constructor |
| 15 | * |
| 16 | * @since 1.4.6.5 |
| 17 | */ |
| 18 | function __construct( $cpac ) { |
| 19 | |
| 20 | $this->cpac = $cpac; |
| 21 | |
| 22 | add_action( 'admin_init', array( $this, 'download_export' ) ); |
| 23 | add_action( 'admin_init', array( $this, 'handle_file_import' ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get export string |
| 28 | * |
| 29 | * @since 2.0.0 |
| 30 | */ |
| 31 | function get_export_string( $types = array() ) { |
| 32 | |
| 33 | if ( empty( $types ) ) |
| 34 | return false; |
| 35 | |
| 36 | $columns = array(); |
| 37 | |
| 38 | // get stored columns |
| 39 | foreach ( $this->cpac->storage_models as $storage_model ) { |
| 40 | |
| 41 | if ( ! in_array( $storage_model->key, $types ) ) |
| 42 | continue; |
| 43 | |
| 44 | $columns[ $storage_model->key ] = $storage_model->get_stored_columns(); |
| 45 | } |
| 46 | |
| 47 | if ( empty( $columns ) ) |
| 48 | return false; |
| 49 | |
| 50 | return "<!-- START: Admin Columns export -->\n" . base64_encode( serialize( array_filter( $columns ) ) ) . "\n<!-- END: Admin Columns export -->"; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Download Export |
| 55 | * |
| 56 | * @since 2.0.0 |
| 57 | */ |
| 58 | function download_export() { |
| 59 | if ( ! isset( $_REQUEST['_cpac_nonce'] ) || ! wp_verify_nonce( $_REQUEST['_cpac_nonce'], 'download-export' ) ) |
| 60 | return false; |
| 61 | |
| 62 | if ( empty( $_REQUEST['export_types'] ) ) { |
| 63 | |
| 64 | cpac_admin_message( __( 'Export field is empty. Please select your types from the left column.', 'cpac' ), 'error' ); |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $single_type = ''; |
| 70 | if ( 1 == count( $_REQUEST['export_types'] ) ) { |
| 71 | $single_type = '_' . $_REQUEST['export_types'][0]; |
| 72 | } |
| 73 | |
| 74 | $filename = 'admin-columns-export_' . date('Y-m-d', time() ) . $single_type; |
| 75 | |
| 76 | // generate text file |
| 77 | header( "Content-disposition: attachment; filename={$filename}.txt" ); |
| 78 | header( 'Content-type: text/plain' ); |
| 79 | echo $this->get_export_string( $_REQUEST['export_types'] ); |
| 80 | exit; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Handle file import |
| 85 | * |
| 86 | * @uses wp_import_handle_upload() |
| 87 | * @since 2.0.0 |
| 88 | */ |
| 89 | function handle_file_import() { |
| 90 | if ( ! isset( $_REQUEST['_cpac_nonce'] ) || ! wp_verify_nonce( $_REQUEST['_cpac_nonce'], 'file-import' ) || empty( $_FILES['import'] ) ) |
| 91 | return false; |
| 92 | |
| 93 | // handles upload |
| 94 | $file = wp_import_handle_upload(); |
| 95 | |
| 96 | // any errors? |
| 97 | $error = false; |
| 98 | if ( isset( $file['error'] ) ) { |
| 99 | $error = __( 'Sorry, there has been an error.', 'cpac' ) . '<br />' . esc_html( $file['error'] ); |
| 100 | } else if ( ! file_exists( $file['file'] ) ) { |
| 101 | $error = __( 'Sorry, there has been an error.', 'cpac' ) . '<br />' . sprintf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'cpac' ), esc_html( $file['file'] ) ); |
| 102 | } |
| 103 | |
| 104 | if ( $error ) { |
| 105 | cpac_admin_message( $error, 'error' ); |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | // read file contents and start the import |
| 110 | $content = file_get_contents( $file['file'] ); |
| 111 | |
| 112 | // cleanup |
| 113 | wp_delete_attachment( $file['id'] ); |
| 114 | |
| 115 | // decode file contents |
| 116 | $columns = $this->get_decoded_settings( $content ); |
| 117 | |
| 118 | if ( ! $columns ) { |
| 119 | cpac_admin_message( __( 'Import failed. File does not contain Admin Column settings.', 'cpac' ), 'error' ); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // store settings |
| 124 | foreach( $columns as $type => $cols ) { |
| 125 | |
| 126 | $storage_model = $this->cpac->get_storage_model( $type ); |
| 127 | $storage_model->store( $cols ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get decoded settings |
| 133 | * |
| 134 | * @since 2.0.0 |
| 135 | * |
| 136 | * @param string $encoded_string |
| 137 | * @return array Columns |
| 138 | */ |
| 139 | function get_decoded_settings( $encoded_string = '' ) { |
| 140 | if( ! $encoded_string || ! is_string( $encoded_string ) || strpos( $encoded_string, '<!-- START: Admin Columns export -->' ) === false ) |
| 141 | return false; |
| 142 | |
| 143 | // decode |
| 144 | $encoded_string = str_replace( "<!-- START: Admin Columns export -->\n", "", $encoded_string ); |
| 145 | $encoded_string = str_replace( "\n<!-- END: Admin Columns export -->", "", $encoded_string); |
| 146 | $decoded = maybe_unserialize( base64_decode( trim( $encoded_string ) ) ); |
| 147 | |
| 148 | if ( empty( $decoded ) || ! is_array( $decoded ) ) |
| 149 | return false; |
| 150 | |
| 151 | return $decoded; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Update settings |
| 156 | * |
| 157 | * @since 2.0.0 |
| 158 | * |
| 159 | * @param array $columns Columns |
| 160 | * @return bool |
| 161 | */ |
| 162 | function update_settings( $columns ) { |
| 163 | $options = get_option( 'cpac_options' ); |
| 164 | |
| 165 | // merge saved setting if they exist.. |
| 166 | if ( ! empty( $options['columns'] ) ) { |
| 167 | $options['columns'] = array_merge( $options['columns'], $columns ); |
| 168 | } |
| 169 | |
| 170 | // .. if there are no setting yet use the import |
| 171 | else { |
| 172 | $options = array( |
| 173 | 'columns' => $columns |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | return update_option( 'cpac_options', array_filter( $options ) ); |
| 178 | } |
| 179 | } |
| 180 |