| @@ -1,0 +1,130 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Import/Export Settings Tool | |
| 4 | + * | |
| 5 | + * @package GamiPress\Admin\Tools\Import_Export_Settings | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.1.7 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register Import/Export Settings Tool meta boxes | |
| 14 | + * | |
| 15 | + * @since 1.1.7 | |
| 16 | + * | |
| 17 | + * @param array $meta_boxes | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function gamipress_import_export_settings_tool_meta_boxes( $meta_boxes ) { | |
| 22 | + | |
| 23 | + $meta_boxes['import-export-settings'] = array( | |
| 24 | + 'title' => gamipress_dashicon( 'admin-settings' ) . __( 'Settings', 'gamipress' ), | |
| 25 | + 'fields' => apply_filters( 'gamipress_import_export_settings_tool_fields', array( | |
| 26 | + 'export_settings' => array( | |
| 27 | + 'label' => __( 'Export Settings', 'gamipress' ), | |
| 28 | + 'desc' => __( 'Export settings from this site as a file to easily import this configuration to another site.', 'gamipress' ), | |
| 29 | + 'type' => 'button', | |
| 30 | + 'button' => 'primary', | |
| 31 | + 'icon' => 'dashicons-download', | |
| 32 | + 'action' => 'export_settings' | |
| 33 | + ), | |
| 34 | + 'import_settings_file' => array( | |
| 35 | + 'type' => 'text', | |
| 36 | + 'attributes' => array( 'type' => 'file' ) | |
| 37 | + ), | |
| 38 | + 'import_settings' => array( | |
| 39 | + 'label' => __( 'Import Settings', 'gamipress' ), | |
| 40 | + 'type' => 'button', | |
| 41 | + 'button' => 'primary', | |
| 42 | + 'icon' => 'dashicons-upload', | |
| 43 | + ), | |
| 44 | + ) ), | |
| 45 | + 'vertical_tabs' => true, | |
| 46 | + 'tabs' => apply_filters( 'gamipress_import_export_settings_tool_tabs', array( | |
| 47 | + 'export_settings' => array( | |
| 48 | + 'icon' => 'dashicons-download', | |
| 49 | + 'title' => __( 'Export', 'gamipress' ), | |
| 50 | + 'fields' => array( | |
| 51 | + 'export_settings', | |
| 52 | + ), | |
| 53 | + ), | |
| 54 | + 'import_settings' => array( | |
| 55 | + 'icon' => 'dashicons-upload', | |
| 56 | + 'title' => __( 'Import', 'gamipress' ), | |
| 57 | + 'fields' => array( | |
| 58 | + 'import_settings_file', | |
| 59 | + 'import_settings', | |
| 60 | + ), | |
| 61 | + ), | |
| 62 | + ) ), | |
| 63 | + ); | |
| 64 | + | |
| 65 | + return $meta_boxes; | |
| 66 | + | |
| 67 | +} | |
| 68 | +add_filter( 'gamipress_tools_import_export_meta_boxes', 'gamipress_import_export_settings_tool_meta_boxes' ); | |
| 69 | + | |
| 70 | +/** | |
| 71 | + * Export Settings action | |
| 72 | + * | |
| 73 | + * @since 1.1.7 | |
| 74 | + */ | |
| 75 | +function gamipress_action_export_settings() { | |
| 76 | + // Security check, forces to die if not security passed | |
| 77 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 78 | + | |
| 79 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) | |
| 80 | + return; | |
| 81 | + | |
| 82 | + nocache_headers(); | |
| 83 | + | |
| 84 | + header( 'Content-Type: text/plain' ); | |
| 85 | + header( 'Content-Disposition: attachment; filename="gamipress-settings-export.txt"' ); | |
| 86 | + | |
| 87 | + $settings = get_option( 'gamipress_settings' ); | |
| 88 | + | |
| 89 | + echo json_encode( $settings ); | |
| 90 | + die(); | |
| 91 | + | |
| 92 | +} | |
| 93 | +add_action( 'gamipress_action_post_export_settings', 'gamipress_action_export_settings' ); | |
| 94 | + | |
| 95 | +/** | |
| 96 | + * AJAX handler for the import settings tool | |
| 97 | + * | |
| 98 | + * @since 1.1.7 | |
| 99 | + */ | |
| 100 | +function gamipress_ajax_import_settings_tool() { | |
| 101 | + // Security check, forces to die if not security passed | |
| 102 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 103 | + | |
| 104 | + // Check parameters received | |
| 105 | + if( ! isset( $_FILES['file'] ) ) | |
| 106 | + wp_send_json_error( __( 'No settings to import.', 'gamipress' ) ); | |
| 107 | + | |
| 108 | + $import_file = $_FILES['file']['tmp_name']; | |
| 109 | + | |
| 110 | + if( empty( $import_file ) ) | |
| 111 | + wp_send_json_error( __( 'Can not retrieve the file to import, check server file permissions.', 'gamipress' ) ); | |
| 112 | + | |
| 113 | + // Check user capabilities | |
| 114 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 115 | + wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + // Retrieve the settings from the file and convert the json object to an array | |
| 119 | + $settings = json_decode( file_get_contents( $import_file ), true ) ; | |
| 120 | + | |
| 121 | + if( ! is_array( $settings ) || empty( $settings ) ) { | |
| 122 | + wp_send_json_error( __( 'Empty settings, so nothing to import.', 'gamipress' ) ); | |
| 123 | + } | |
| 124 | + | |
| 125 | + update_option( 'gamipress_settings', $settings ); | |
| 126 | + | |
| 127 | + // Return a success message | |
| 128 | + wp_send_json_success( __( 'Settings has been imported successfully.', 'gamipress' ) ); | |
| 129 | +} | |
| 130 | +add_action( 'wp_ajax_gamipress_import_settings_tool', 'gamipress_ajax_import_settings_tool' ); | |