__( 'Tools', 'adminease' ), 'icon' => '', 'fields' => [ [ 'type' => 'button', 'id' => 'export-settings-button', 'name' => '', 'input_class' => 'pwp-btn pwp-btn-primary ae-ajax-btn', 'label' => __( 'Export settings', 'adminease' ), 'description' => __( 'Export writes every AdminEase setting to a JSON file you can keep as a backup or carry to another site. Import reads one of those files back and replaces all current settings with it, so take an export first if you want a way back. Settings that write to wp-config.php or .htaccess are applied to the new site as soon as the import finishes.', 'adminease' ), ], [ 'type' => 'button', 'id' => 'reset-settings-button', 'name' => '', 'input_class' => 'pwp-btn pwp-btn-danger ae-ajax-btn', 'label' => __( 'Reset all settings', 'adminease' ), 'description' => __( 'Turns off and clears every AdminEase setting, Pro settings included, and takes back what they changed in wp-config.php and .htaccess. This cannot be undone, so export your settings first if you want a way back. Your logs and sticky notes are kept.', 'adminease' ), ], ], ]; return $fields; } /** * Registers the Tools tab sidebar section. * * @param array $subgroups The existing sub-section map. * * @return array The modified sub-section map. */ public function adminease_settings_subgroups( array $subgroups ): array { $subgroups['tools'] = [ 'import-export' => [ 'title' => __( 'Import / Export', 'adminease' ), 'icon' => '', 'field_ids' => [ 'export-settings-button' ], ], 'reset' => [ 'title' => __( 'Reset', 'adminease' ), 'icon' => '', 'field_ids' => [ 'reset-settings-button' ], ], ]; return $subgroups; } /** * Enqueues the import/export script on the AdminEase dashboard. * * @param string $hook The current admin page hook suffix. * * @return void */ public function admin_enqueue_scripts( string $hook ): void { if( 'toplevel_page_adminease' !== $hook ) { return; } wp_enqueue_script( ADMINEASE_NAME . 'ImportExportSettings', ADMINEASE_PLUGIN_URL . 'assets/js/AdminEaseImportExportSettings.js', [ 'jquery', ADMINEASE_NAME ], filemtime( ADMINEASE_DIR . 'assets/js/AdminEaseImportExportSettings.js' ), true ); wp_localize_script( ADMINEASE_NAME . 'ImportExportSettings', ADMINEASE_NAME . 'ImportExportSettingsAjaxObj', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'security' => [ 'exportSettings' => wp_create_nonce( 'adminease_export_plugin_settings' ), 'importSettings' => wp_create_nonce( 'adminease_import_plugin_settings' ), 'resetSettings' => wp_create_nonce( 'adminease_reset_plugin_settings' ), ], 'i18n' => [ 'confirmReset' => esc_html__( 'This clears every AdminEase setting, Pro settings included. It cannot be undone. Export your settings first if you want a way back. Continue?', 'adminease' ), 'resetFailed' => esc_html__( 'Failed to reset settings.', 'adminease' ), 'confirmImport' => esc_html__( 'Importing replaces every current AdminEase setting with the contents of this file. Continue?', 'adminease' ), 'selectFile' => esc_html__( 'Please choose a JSON file first.', 'adminease' ), 'notJson' => esc_html__( 'Only JSON files can be imported.', 'adminease' ), 'noFileChosen' => esc_html__( 'No file chosen', 'adminease' ), 'importFailed' => esc_html__( 'Failed to import settings.', 'adminease' ), 'exportFailed' => esc_html__( 'Failed to export settings.', 'adminease' ), 'networkError' => esc_html__( 'Network error', 'adminease' ), 'btnProcessing' => esc_html__( 'Processing…', 'adminease' ), 'btnComplete' => esc_html__( 'Complete!', 'adminease' ), ], ] ); } /** * Renders the import panel underneath the export button. * * @param array $field The field information. * * @return void */ public function adminease_after_field_render( array $field ): void { if( 'export-settings-button' !== $field['id'] ) { return; } include_once ADMINEASE_DIR . 'partials/import-export-settings.php'; } /** * Streams the stored settings to the browser as a JSON download. * @return void */ public function ajax_export_settings(): void { if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_export_plugin_settings' ) ) { wp_die( esc_html__( 'An error occurred. Refresh the page and try again.', 'adminease' ) ); } if( !current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to perform this action', 'adminease' ) ); } $json = wp_json_encode( get_option( 'adminease_settings', [] ), JSON_PRETTY_PRINT ); if( false === $json ) { wp_die( esc_html__( 'Settings could not be encoded for export.', 'adminease' ) ); } $filename = sanitize_file_name( 'adminease-settings-' . gmdate( 'Y-m-d-H-i-s' ) . '.json' ); if( !preg_match( '/^[a-zA-Z0-9._-]+\.json$/', $filename ) ) { $filename = 'adminease-settings-' . time() . '.json'; } header( 'Content-Type: application/json' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); header( 'Content-Length: ' . strlen( $json ) ); header( 'Cache-Control: no-cache, must-revalidate' ); echo $json; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON file download, not HTML output. exit; } /** * Reads an uploaded JSON file and replaces the stored settings with it. * @return void */ public function ajax_import_settings(): void { if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_import_plugin_settings' ) ) { wp_send_json_error( new WP_Error( 'security_error', __( 'An error occurred. Refresh the page and try again.', 'adminease' ) ), 403 ); } if( !current_user_can( 'manage_options' ) ) { wp_send_json_error( new WP_Error( 'permissions', esc_html__( 'You do not have sufficient permissions to perform this action', 'adminease' ) ) ); } $file = [ 'name' => isset( $_FILES['data']['name'] ) ? sanitize_file_name( wp_unslash( $_FILES['data']['name'] ) ) : '', // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- PHP-generated temp path, verified by is_uploaded_file() below. 'tmp_name' => isset( $_FILES['data']['tmp_name'] ) ? (string) $_FILES['data']['tmp_name'] : '', 'error' => isset( $_FILES['data']['error'] ) ? (int) $_FILES['data']['error'] : UPLOAD_ERR_NO_FILE, 'size' => isset( $_FILES['data']['size'] ) ? (int) $_FILES['data']['size'] : 0, ]; if( UPLOAD_ERR_OK !== $file['error'] || !is_uploaded_file( $file['tmp_name'] ) ) { wp_send_json_error( new WP_Error( 'file_upload', esc_html__( 'File upload failed.', 'adminease' ) ) ); } if( $file['size'] > 1024 * 1024 ) { wp_send_json_error( new WP_Error( 'file_size', esc_html__( 'File size exceeds the maximum limit of 1MB.', 'adminease' ) ) ); } if( 'json' !== strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) ) ) { wp_send_json_error( new WP_Error( 'file_extension', esc_html__( 'Only JSON files are allowed.', 'adminease' ) ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading the PHP upload temp file, which WP_Filesystem cannot address. $file_content = file_get_contents( $file['tmp_name'] ); if( false === $file_content || '' === trim( (string) $file_content ) ) { wp_send_json_error( new WP_Error( 'file_read', esc_html__( 'Unable to read the file, or the file is empty.', 'adminease' ) ) ); } $settings = json_decode( $file_content, true ); if( JSON_ERROR_NONE !== json_last_error() || !is_array( $settings ) ) { wp_send_json_error( new WP_Error( 'invalid_content', esc_html__( 'This file does not contain valid AdminEase settings.', 'adminease' ) ) ); } $settings = array_intersect_key( $settings, Plugin::get_plugin_fields() ); if( isset( $settings['performance'] ) && is_array( $settings['performance'] ) ) { $shared = $settings['performance']['heartbeat_interval'] ?? ( $settings['performance']['heartbeat_control_frequency'] ?? 60 ); $shared = is_scalar( $shared ) && is_numeric( $shared ) ? max( 15, min( 300, (int) $shared ) ) : 60; foreach( [ 'dashboard', 'editor', 'front' ] as $place ) { if( isset( $settings['performance'][ 'heartbeat_' . $place ] ) && 'slow' === $settings['performance'][ 'heartbeat_' . $place ] ) { $settings['performance'][ 'heartbeat_' . $place ] = (string) $shared; } } unset( $settings['performance']['heartbeat_interval'] ); } if( empty( $settings ) ) { wp_send_json_error( new WP_Error( 'invalid_settings', esc_html__( 'No AdminEase settings were found in this file.', 'adminease' ) ) ); } $saved_settings = Plugin::get_instance()->write_settings( $settings, 'import' ); if( is_wp_error( $saved_settings ) ) { wp_send_json_error( $saved_settings ); } wp_send_json_success( [ 'message' => esc_html__( 'Settings imported successfully. Reloading…', 'adminease' ) ] ); } /** * Clears every stored setting. Goes through the shared writer, so the listeners * that own wp-config.php and .htaccess blocks see the empty settings and take their blocks back. * @return void */ public function ajax_reset_settings(): void { if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_reset_plugin_settings' ) ) { wp_send_json_error( new WP_Error( 'security_error', __( 'An error occurred. Refresh the page and try again.', 'adminease' ) ), 403 ); } if( !current_user_can( 'manage_options' ) ) { wp_send_json_error( new WP_Error( 'permissions', esc_html__( 'You do not have sufficient permissions to perform this action', 'adminease' ) ) ); } $saved_settings = Plugin::get_instance()->write_settings( [], 'reset' ); if( is_wp_error( $saved_settings ) ) { wp_send_json_error( $saved_settings ); } wp_send_json_success( [ 'message' => esc_html__( 'All settings were reset. Reloading…', 'adminease' ) ] ); } }