e configuration data. * * @param string|array $key The key or array of keys to set. * @param mixed $value The value to set. * * @return mixed The value that was set. */ public function set( $key, $value ) { if ( ! is_array( $key ) ) { $this->_data[ $key ] = $value; } else { // set extension's key. $key0 = $key[0]; $key1 = $key[1]; if ( ! isset( $this->_data[ $key0 ] ) || ! is_array( $this->_data[ $key0 ] ) ) { $this->_data[ $key0 ] = array(); } $this->_data[ $key0 ][ $key1 ] = $value; } return $value; } /** * Checks if the current configuration is a preview. * * @return bool True if preview mode is enabled, false otherwise. */ public function is_preview() { return $this->_preview; } /** * Checks if the current configuration is the master configuration. * * @return bool True if the configuration is the master, false otherwise. */ public function is_master() { return $this->_is_master; } /** * Checks if the configuration is compiled. * * @return bool True if the configuration is compiled, false otherwise. */ public function is_compiled() { return $this->_compiled; } /** * Sets the default configuration values. * * @return void */ public function set_defaults() { $c = new ConfigCompiler( $this->_blog_id, $this->_preview ); $this->_data = $c->get_data(); } /** * Saves the current configuration. * * @return void */ public function save() { if ( function_exists( 'do_action' ) ) { do_action( 'w3tc_config_save', $this ); } $c = new ConfigCompiler( $this->_blog_id, $this->_preview ); $c->apply_data( $this->_data ); $c->save(); } /** * Checks if a configuration key is sealed (immutable). * * @param string $key The configuration key to check. * * @return bool True if the key is sealed, false otherwise. */ public function is_sealed( $key ) { if ( $this->is_master() ) { return false; } // better to use master config data here, but its faster and preciese enough for UI. return ConfigCompiler::child_key_sealed( $key, $this->_data, $this->_data ); } /** * Exports the current configuration as a JSON string. * * @return string The configuration data as a JSON string. */ public function export() { if ( defined( 'JSON_PRETTY_PRINT' ) ) { $content = wp_json_encode( $this->_data, JSON_PRETTY_PRINT ); } else { $content = wp_json_encode( $this->_data ); } return $content; } /** * Imports a configuration from a file. * * @global $wp_filesystem * @see get_filesystem_method() * * @param string $filename The path to the file to import. * * @return bool True if the import was successful, false otherwise. */ public function import( string $filename ): bool { if ( 'direct' !== \get_filesystem_method() ) { return false; } // Initialize WP_Filesystem. global $wp_filesystem; WP_Filesystem(); if ( $wp_filesystem->exists( $filename ) && $wp_filesystem->is_readable( $filename ) ) { $content = $wp_filesystem->get_contents( $filename ); if ( \substr( $content, 0, 14 ) === '