| 1 |
<?php |
| 2 |
/** |
| 3 |
* Import settings as json file. |
| 4 |
* |
| 5 |
* @package Adminimize |
| 6 |
* @subpackage import |
| 7 |
* @author Frank Bültge |
| 8 |
* @version 2017-11-29 |
| 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 |
// Fallback, if we have no file information on server. |
| 49 |
$extension_types = array( 'octet-stream' ); |
| 50 |
if ( in_array( $extension, $extension_types, false ) ) { |
| 51 |
$finfo = new finfo(FILEINFO_MIME_TYPE); |
| 52 |
$extension = $finfo->file( $_FILES[ 'import_file' ][ 'tmp_name' ] ); |
| 53 |
} |
| 54 |
|
| 55 |
$extension_allow = array( 'json', 'text/plain', 'text/html' ); |
| 56 |
if ( false !== $extension && ! in_array( $extension, $extension_allow, false ) ) { |
| 57 |
wp_die( |
| 58 |
sprintf( |
| 59 |
esc_attr__( 'Please upload a valid .json file, Extension check. Your file have the extension %s.', 'adminimize' ), |
| 60 |
'<code>' . $extension . '</code>' |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
if ( empty( $path ) || ! is_readable( $path ) ) { |
| 66 |
wp_die( |
| 67 |
sprintf( |
| 68 |
esc_attr__( 'It is not possible to find a file in %s', 'adminimize' ), |
| 69 |
$path |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
// Retrieve the settings from the file and convert the json object to an array. |
| 75 |
$settings = json_decode( file_get_contents( $path ), true ); |
| 76 |
unlink( $path ); |
| 77 |
|
| 78 |
_mw_adminimize_update_option( $settings ); |
| 79 |
wp_safe_redirect( esc_url( site_url('/wp-admin/options-general.php?page=adminimize-options') ) ); |
| 80 |
exit(); |
| 81 |
} |
| 82 |
|