| 1 |
<?php |
| 2 |
/** |
| 3 |
* Import settings as json file. |
| 4 |
* |
| 5 |
* @package Adminimize |
| 6 |
* @subpackage import |
| 7 |
* @author Frank Bültge |
| 8 |
* @version 2017-04-13 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! function_exists( 'add_action' ) ) { |
| 12 |
echo "Hi there! I'm just a part of plugin, not much I can do when called directly."; |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
add_action( 'admin_init', '_mw_adminimize_import_json' ); |
| 17 |
/** |
| 18 |
* Process a settings import from a json file. |
| 19 |
*/ |
| 20 |
function _mw_adminimize_import_json() { |
| 21 |
|
| 22 |
if ( ! is_admin() ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// If is AJAX Call. |
| 31 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
if ( empty( $_POST[ '_mw_adminimize_action' ] ) || '_mw_adminimize_import' !== $_POST[ '_mw_adminimize_action' ] ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
if ( ! wp_verify_nonce( $_POST[ 'mw_adminimize_import_nonce' ], 'mw_adminimize_import_nonce' ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$path = esc_attr( $_FILES[ 'import_file' ][ 'tmp_name' ] ); |
| 44 |
$type = (string) esc_attr( $_FILES[ 'import_file' ][ 'type' ] ); |
| 45 |
$tmp = explode( '/', $type ); |
| 46 |
$extension = end( $tmp ); |
| 47 |
|
| 48 |
if ( 'json' !== $extension ) { |
| 49 |
wp_die( |
| 50 |
esc_attr__( 'Please upload a valid .json file', 'adminimize' ) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
if ( empty( $path ) ) { |
| 55 |
wp_die( |
| 56 |
esc_attr__( 'Please upload a file to import', 'adminimize' ) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
// Retrieve the settings from the file and convert the json object to an array. |
| 61 |
$settings = (array) json_decode( |
| 62 |
file_get_contents( $path ) |
| 63 |
); |
| 64 |
unlink( $path ); |
| 65 |
|
| 66 |
_mw_adminimize_update_option( $settings ); |
| 67 |
} |
| 68 |
|