| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Tag Groups |
| 5 |
* @author Christoph Amthor |
| 6 |
* @copyright 2020 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 7 |
* @license GPL-3.0+ |
| 8 |
*/ |
| 9 |
|
| 10 |
if (!class_exists('TagGroups_Export')) { |
| 11 |
/** |
| 12 |
* |
| 13 |
* @since 1.38.0 |
| 14 |
*/ |
| 15 |
// phpcs:ignore PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps -- Legacy class structure |
| 16 |
class TagGroups_Export |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Options to be exported |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private $options ; |
| 24 |
/** |
| 25 |
* Terms to be exported |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private $terms ; |
| 30 |
/** |
| 31 |
* pseudo-random hash to cloak the exported files |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $hash ; |
| 36 |
/** |
| 37 |
* whether an error occured |
| 38 |
* |
| 39 |
* @var integer |
| 40 |
*/ |
| 41 |
private $error ; |
| 42 |
// phpcs:ignore Squiz.Scope.MethodScope.Missing -- Legacy code |
| 43 |
function __construct() |
| 44 |
{ |
| 45 |
$this->error = false; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create an array of all options that should be exported |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps, Squiz.Scope.MethodScope.Missing -- Legacy method naming |
| 54 |
function process_options_for_export() |
| 55 |
{ |
| 56 |
$this->options = array( |
| 57 |
'name' => 'tag_groups_options', |
| 58 |
'version' => TAG_GROUPS_VERSION, |
| 59 |
'date' => current_time('mysql'), |
| 60 |
); |
| 61 |
$available_options = TagGroups_Options::get_available_options(); |
| 62 |
foreach ($available_options as $key => $value) { |
| 63 |
if ($available_options[$key]['export']) { |
| 64 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 65 |
$this->options[$key] = TagGroups_Options::get_option($key); |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
if (TagGroups_Options::TAG_GROUPS_PLUGIN == $available_options[$key]['origin']) { |
| 70 |
$this->options[$key] = TagGroups_Options::get_option($key); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Create an array of all terms that should be exported |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps, Squiz.Scope.MethodScope.Missing -- Legacy method naming |
| 82 |
function process_terms_for_export() |
| 83 |
{ |
| 84 |
// generate array of all terms |
| 85 |
$wp_terms = get_terms(array( |
| 86 |
'hide_empty' => false, |
| 87 |
'taxonomy' => TagGroups_Taxonomy::get_enabled_taxonomies(), |
| 88 |
)); |
| 89 |
$this->terms = array( |
| 90 |
'name' => 'tag_groups_terms', |
| 91 |
'version' => TAG_GROUPS_VERSION, |
| 92 |
'date' => current_time('mysql'), |
| 93 |
); |
| 94 |
$this->terms['terms'] = array(); |
| 95 |
foreach ($wp_terms as $term) { |
| 96 |
$tg_term = new TagGroups_Term($term->term_id); |
| 97 |
// We export only fields that later can be updated with wp_update_term() |
| 98 |
$this->terms['terms'][] = array( |
| 99 |
'term_id' => $term->term_id, |
| 100 |
'name' => $term->name, |
| 101 |
'slug' => $term->slug, |
| 102 |
'term_group' => $tg_term->get_groups(), |
| 103 |
'taxonomy' => $term->taxonomy, |
| 104 |
'description' => $term->description, |
| 105 |
'parent' => $term->parent, |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Writes options and terms into files |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps, Squiz.Scope.MethodScope.Missing -- Legacy method naming |
| 116 |
function write_files() |
| 117 |
{ |
| 118 |
try { |
| 119 |
// misusing the password generator to get a hash |
| 120 |
$this->hash = wp_generate_password(10, false); |
| 121 |
$export_data = array( |
| 122 |
'settings' => wp_json_encode($this->options), |
| 123 |
'terms' => wp_json_encode($this->terms), |
| 124 |
); |
| 125 |
|
| 126 |
if (!set_transient('tag_groups_export_' . $this->hash, $export_data, 15 * MINUTE_IN_SECONDS)) { |
| 127 |
$this->error = true; |
| 128 |
} |
| 129 |
} catch (Exception $e) { |
| 130 |
$this->error = true; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Displays the links to download the exported files, or an error message |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps, Squiz.Scope.MethodScope.Missing -- Legacy method naming |
| 140 |
function show_download_links() |
| 141 |
{ |
| 142 |
|
| 143 |
if (!$this->error) { |
| 144 |
$settings_url = self::get_download_url($this->hash, 'settings'); |
| 145 |
$terms_url = self::get_download_url($this->hash, 'terms'); |
| 146 |
TagGroups_Admin_Notice::add('success', __('Your settings/groups and your terms have been exported. Please download the resulting files with right-click or ctrl-click:', 'tag-groups') . ' <p> |
| 147 |
<a href="' . esc_url($settings_url) . '" target="_blank">tag_groups_settings-' . esc_html($this->hash) . '.json</a> |
| 148 |
</p>' . ' <p> |
| 149 |
<a href="' . esc_url($terms_url) . '" target="_blank">tag_groups_terms-' . esc_html($this->hash) . '.json</a> |
| 150 |
</p>'); |
| 151 |
} else { |
| 152 |
TagGroups_Error::log('[Tag Groups] Error writing files'); |
| 153 |
TagGroups_Admin_Notice::add('error', __('Writing of the exported settings failed.', 'tag-groups')); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Builds an authenticated URL for an export download. |
| 159 |
* |
| 160 |
* @param string $hash |
| 161 |
* @param string $type |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- Legacy method naming |
| 165 |
private static function get_download_url($hash, $type) |
| 166 |
{ |
| 167 |
return add_query_arg( |
| 168 |
array( |
| 169 |
'action' => 'tag_groups_download_export', |
| 170 |
'tag_groups_export' => $hash, |
| 171 |
'tag_groups_export_type' => $type, |
| 172 |
'_wpnonce' => wp_create_nonce('tag-groups-download-export-' . $hash . '-' . $type), |
| 173 |
), |
| 174 |
admin_url('admin-post.php') |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Serves an exported JSON file to authorized administrators. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- Legacy method naming |
| 184 |
public static function download_file() |
| 185 |
{ |
| 186 |
if (!current_user_can('manage_options')) { |
| 187 |
wp_die(esc_html__('You do not have permission to download this export file.', 'tag-groups')); |
| 188 |
} |
| 189 |
|
| 190 |
$hash = isset($_GET['tag_groups_export']) ? sanitize_text_field(wp_unslash($_GET['tag_groups_export'])) : ''; |
| 191 |
$type = isset($_GET['tag_groups_export_type']) ? sanitize_key(wp_unslash($_GET['tag_groups_export_type'])) : ''; |
| 192 |
$nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; |
| 193 |
|
| 194 |
if (empty($hash) || !in_array($type, array( 'settings', 'terms' ), true) || !wp_verify_nonce($nonce, 'tag-groups-download-export-' . $hash . '-' . $type)) { |
| 195 |
wp_die(esc_html__('The export download link is invalid or has expired. Please run the export again.', 'tag-groups')); |
| 196 |
} |
| 197 |
|
| 198 |
$export_data = get_transient('tag_groups_export_' . $hash); |
| 199 |
|
| 200 |
if (empty($export_data[$type])) { |
| 201 |
wp_die(esc_html__('The requested export file has expired. Please run the export again.', 'tag-groups')); |
| 202 |
} |
| 203 |
|
| 204 |
$filename = sanitize_file_name('tag_groups_' . $type . '-' . $hash . '.json'); |
| 205 |
|
| 206 |
nocache_headers(); |
| 207 |
header('Content-Type: application/json; charset=' . get_option('blog_charset')); |
| 208 |
header('Content-Disposition: attachment; filename="' . $filename . '"'); |
| 209 |
header('X-Content-Type-Options: nosniff'); |
| 210 |
|
| 211 |
echo $export_data[$type]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON file download. |
| 212 |
exit; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|