Module: Wpxf::Helpers::Export

Includes:
Db::Loot
Included in:
WordPress::FileDownload
Defined in:
lib/wpxf/helpers/export.rb

Overview

Provides helper methods for common functionality used to export files.

Instance Method Summary collapse

Methods included from Db::Loot

#store_loot

Instance Method Details

#export_and_log_loot(content, description, type, extension = '') ⇒ Models::LootItem

Save content to a new file and log the loot in the database.

Parameters:

  • content (String)

    the file content to save.

  • extension (String) (defaults to: '')

    the file extension to use when creating the file.

Returns:



41
42
43
44
45
# File 'lib/wpxf/helpers/export.rb', line 41

def export_and_log_loot(content, description, type, extension = '')
  filename = generate_unique_filename(extension)
  File.write(filename, content)
  store_loot filename, description, type
end

#export_pathString

Returns the path to save the file to.

Returns:

  • (String)

    the path to save the file to.



24
25
26
27
# File 'lib/wpxf/helpers/export.rb', line 24

def export_path
  return nil if normalized_option_value('export_path').nil?
  File.expand_path normalized_option_value('export_path')
end

#generate_unique_filename(file_extension) ⇒ String

Returns the path to a unique filename in the wpxf home directory.

Returns:

  • (String)

    the path to a unique filename in the wpxf home directory.



30
31
32
33
34
35
# File 'lib/wpxf/helpers/export.rb', line 30

def generate_unique_filename(file_extension)
  storage_path = File.join(Dir.home, '.wpxf', 'loot')
  FileUtils.mkdir_p(storage_path) unless File.directory?(storage_path)
  filename = "#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}#{file_extension}"
  File.join(storage_path, filename)
end

#register_export_path_option(required) ⇒ StringOption

Register the export_path option.

Parameters:

  • required (Boolean)

    a value indicating whether the option should be required.

Returns:



12
13
14
15
16
17
18
19
20
21
# File 'lib/wpxf/helpers/export.rb', line 12

def register_export_path_option(required)
  opt = StringOption.new(
    name: 'export_path',
    desc: 'The path to save the file to',
    required: required
  )

  register_option(opt)
  opt
end