settings = $settings; } /** * The groups that can be exported, imported and reset. * * @since 1.0.0 * * @return string[] */ public function groups(): array { return array_values( array_diff( $this->settings->groups(), self::LEFT_OUT ) ); } /** * Every setting as a document. * * @since 1.0.0 * * @return array */ public function export(): array { $settings = array(); foreach ( $this->groups() as $group ) { $settings[ $group ] = $this->settings->get( $group ); } return array( 'plugin' => 'elevenrank', 'version' => ELEVENRANK_VERSION, 'exported' => gmdate( 'c' ), 'settings' => $settings, ); } /** * Applies an exported document. * * Values are cleaned by the schema. The "delete all data on uninstall" switch is never taken from a * file: a file from another site should not decide what happens to this site's data. * * @since 1.0.0 * * @param array $document Decoded JSON. * @return array{imported: string[], ignored: string[]}|null Null when the document is not an export of this plugin. */ public function import( array $document ): ?array { if ( ( $document['plugin'] ?? '' ) !== 'elevenrank' || ! isset( $document['settings'] ) || ! is_array( $document['settings'] ) ) { return null; } $imported = array(); $ignored = array(); foreach ( $document['settings'] as $group => $values ) { if ( ! is_string( $group ) || ! in_array( $group, $this->groups(), true ) || ! is_array( $values ) ) { $ignored[] = is_string( $group ) ? $group : '?'; continue; } if ( 'general' === $group ) { unset( $values['delete_data_on_uninstall'] ); } $this->settings->update( $group, $values ); $imported[] = $group; } return array( 'imported' => $imported, 'ignored' => $ignored, ); } /** * Puts groups back to their defaults. * * @since 1.0.0 * * @param string[] $groups Groups to reset; unknown names are ignored. * @return string[] The groups that were reset. */ public function reset( array $groups ): array { $done = array(); foreach ( $groups as $group ) { if ( ! is_string( $group ) || ! in_array( $group, $this->groups(), true ) ) { continue; } delete_option( $this->settings->option_name( $group ) ); $done[] = $group; } // The options are created again with their defaults. $this->settings->ensure_defaults(); return $done; } }