[ 'tab' => tab, 'field' => field ]. * * @return array */ private static function index(): array { $index = array(); foreach ( Settings::fields() as $tab => $data ) { foreach ( (array) ( $data['sections'] ?? array() ) as $section ) { foreach ( (array) ( $section['fields'] ?? array() ) as $key => $field ) { $index[ (string) $key ] = array( 'tab' => (string) $tab, 'field' => (array) $field, ); } } } return $index; } /** * The export: the saved settings of every known field, secrets left out, images as addresses. * * @return array */ public static function data(): array { $stored = Settings::stored(); $settings = array(); foreach ( self::index() as $key => $entry ) { if ( ! array_key_exists( $key, $stored ) || self::is_secret( $key, $entry['field'] ) ) { continue; } $value = $stored[ $key ]; if ( 'image' === ( $entry['field']['type'] ?? '' ) ) { $url = absint( $value ) ? wp_get_attachment_url( absint( $value ) ) : ''; $value = $url ? $url : ''; } $settings[ $key ] = $value; } ksort( $settings ); return array( 'plugin' => 'aiolos-checkout', 'format' => self::FORMAT, 'version' => AIOLOS_CHECKOUT_VERSION, 'pro_version' => defined( 'ACWP_VERSION' ) ? (string) ACWP_VERSION : null, 'exported_at' => gmdate( 'c' ), 'tabs' => array_keys( Settings::fields() ), 'settings' => $settings, ); } /** * Sends the export as a file. */ public static function export(): void { self::guard( 'aiolos_export_settings' ); $json = wp_json_encode( self::data(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( ! is_string( $json ) ) { self::back( array( 'aiolos-tools' => 'export-failed' ) ); } $host = wp_parse_url( home_url(), PHP_URL_HOST ); $name = 'aiolos-checkout-settings-' . sanitize_file_name( is_string( $host ) ? $host : 'site' ) . '-' . gmdate( 'Y-m-d' ) . '.json'; Log::info( 'Settings exported.' ); nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $name . '"' ); header( 'X-Content-Type-Options: nosniff' ); echo $json; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- a JSON file download, not HTML. exit; } /** * Reads an uploaded export and applies it. */ public static function import(): void { self::guard( 'aiolos_import_settings' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- guard() checked the nonce. $file = isset( $_FILES['aiolos_import'] ) && is_array( $_FILES['aiolos_import'] ) ? $_FILES['aiolos_import'] : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only the temporary path and numbers are read, each checked. // phpcs:enable $tmp = isset( $file['tmp_name'] ) && is_string( $file['tmp_name'] ) ? $file['tmp_name'] : ''; if ( ! $tmp || UPLOAD_ERR_OK !== (int) ( $file['error'] ?? UPLOAD_ERR_NO_FILE ) || ! is_uploaded_file( $tmp ) ) { self::back( array( 'aiolos-tools' => 'no-file' ) ); } if ( (int) ( $file['size'] ?? 0 ) > self::MAX_SIZE || filesize( $tmp ) > self::MAX_SIZE ) { self::back( array( 'aiolos-tools' => 'too-big' ) ); } $raw = file_get_contents( $tmp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a local uploaded file. $data = is_string( $raw ) ? json_decode( $raw, true ) : null; if ( ! is_array( $data ) || 'aiolos-checkout' !== ( $data['plugin'] ?? '' ) || ! isset( $data['settings'] ) || ! is_array( $data['settings'] ) ) { self::back( array( 'aiolos-tools' => 'invalid' ) ); } $result = self::apply( $data ); Log::info( 'Settings imported.', $result ); self::back( array( 'aiolos-tools' => 'imported', // Prefixed: WordPress strips "skipped" and other bulk-action names from admin addresses. 'aiolos-imported' => $result['imported'], 'aiolos-skipped' => $result['skipped'], 'aiolos-images' => $result['images_failed'], ) ); } /** * Applies an export to this site. Tabs in the file are replaced (a setting missing from the * file goes back to its default, as on the exporting site); other tabs are not touched. * * @param array $data Decoded export. * @return array{imported:int,skipped:int,images_failed:int} */ public static function apply( array $data ): array { $index = self::index(); $incoming = (array) $data['settings']; $tabs = array(); if ( isset( $data['tabs'] ) && is_array( $data['tabs'] ) ) { $tabs = array_values( array_filter( $data['tabs'], 'is_string' ) ); } if ( ! $tabs ) { foreach ( array_keys( $incoming ) as $key ) { if ( isset( $index[ (string) $key ] ) ) { $tabs[] = $index[ (string) $key ]['tab']; } } } $tabs = array_values( array_unique( $tabs ) ); $stored = Settings::stored(); $counts = array( 'imported' => 0, 'skipped' => 0, 'images_failed' => 0, ); foreach ( $incoming as $key => $value ) { $key = (string) $key; if ( ! isset( $index[ $key ] ) || self::is_secret( $key, $index[ $key ]['field'] ) ) { ++$counts['skipped']; } } foreach ( $index as $key => $entry ) { $field = $entry['field']; if ( ! in_array( $entry['tab'], $tabs, true ) || self::is_secret( $key, $field ) ) { continue; } if ( ! array_key_exists( $key, $incoming ) ) { unset( $stored[ $key ] ); continue; } $value = $incoming[ $key ]; if ( 'image' === ( $field['type'] ?? '' ) ) { $value = self::image_id( $value ); if ( null === $value ) { ++$counts['images_failed']; continue; } } $value = Settings::sanitize_value( $field, $value ); if ( self::is_default_text( $field, $value ) ) { unset( $stored[ $key ] ); } else { $stored[ $key ] = $value; } ++$counts['imported']; } update_option( Settings::OPTION, $stored, true ); Settings::flush(); foreach ( $tabs as $tab ) { /** This action is documented in src/Admin/Admin.php */ do_action( 'aiolos_settings_saved', $tab ); } return $counts; } /** * A text equal to its default is not stored (it keeps following the site's language), as on * the settings screen. * * @param array $field Field. * @param mixed $value Sanitized value. */ private static function is_default_text( array $field, $value ): bool { if ( ! in_array( $field['type'] ?? 'text', array( 'text', 'lines', 'textarea' ), true ) || ! is_string( $field['default'] ?? null ) || '' === $field['default'] || ! is_scalar( $value ) ) { return false; } $normalize = static fn( $text ) => trim( str_replace( array( "\r\n", "\r" ), "\n", (string) $text ) ); return $normalize( $value ) === $normalize( $field['default'] ); } /** * An image setting from an export: an attachment of this site found by its address, or the * image downloaded into the media library. 0 for none; null when it could not be brought in. * * @param mixed $value Address (or an id, from an export of this same site). */ private static function image_id( $value ): ?int { if ( is_int( $value ) || ( is_string( $value ) && ctype_digit( $value ) ) ) { $id = absint( $value ); return ( $id && wp_attachment_is_image( $id ) ) ? $id : 0; } if ( ! is_string( $value ) || '' === trim( $value ) ) { return 0; } $url = esc_url_raw( trim( $value ), array( 'http', 'https' ) ); if ( '' === $url ) { return null; } $id = attachment_url_to_postid( $url ); if ( $id && wp_attachment_is_image( $id ) ) { return $id; } // From another site: downloaded like an image added by address. wp_http_validate_url() // refuses local and private addresses. if ( ! current_user_can( 'upload_files' ) || ! wp_http_validate_url( $url ) ) { return null; } require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $id = media_sideload_image( $url, 0, null, 'id' ); if ( is_wp_error( $id ) || ! wp_attachment_is_image( (int) $id ) ) { Log::error( 'Logo from the import could not be downloaded.', array( 'url' => $url ) ); return null; } return (int) $id; } /** * Puts one tab back to its defaults (secrets are kept). */ public static function reset(): void { self::guard( 'aiolos_reset_tab' ); $tab = isset( $_POST['tab'] ) && is_string( $_POST['tab'] ) ? sanitize_key( wp_unslash( $_POST['tab'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- guard() checked the nonce. $schema = Settings::fields(); if ( '' === $tab || empty( $schema[ $tab ]['sections'] ) ) { self::back( array( 'aiolos-tools' => 'invalid-tab' ) ); } $stored = Settings::stored(); foreach ( (array) $schema[ $tab ]['sections'] as $section ) { foreach ( (array) ( $section['fields'] ?? array() ) as $key => $field ) { if ( ! self::is_secret( (string) $key, (array) $field ) ) { unset( $stored[ $key ] ); } } } update_option( Settings::OPTION, $stored, true ); Settings::flush(); /** This action is documented in src/Admin/Admin.php */ do_action( 'aiolos_settings_saved', $tab ); Log::info( 'Tab reset to defaults.', array( 'tab' => $tab ) ); $back = isset( $_POST['back'] ) && is_string( $_POST['back'] ) ? sanitize_key( wp_unslash( $_POST['back'] ) ) : 'tools'; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- guard() checked the nonce. wp_safe_redirect( add_query_arg( array( 'aiolos-tools' => 'reset', 'reset-tab' => $tab, ), Admin::url( 'tab' === $back ? $tab : 'tools' ) ) ); exit; } /** * Capability and nonce. * * @param string $action Nonce action. */ private static function guard( string $action ): void { if ( ! current_user_can( Admin::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to change these settings.', 'aiolos-checkout-for-woocommerce' ), 403 ); } check_admin_referer( $action ); } /** * Back to the Tools tab with a result. * * @param array $args Query arguments. */ private static function back( array $args ): void { wp_safe_redirect( add_query_arg( $args, Admin::url( 'tools' ) ) ); exit; } }