-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulkpress-export.php
More file actions
152 lines (126 loc) · 3.47 KB
/
bulkpress-export.php
File metadata and controls
152 lines (126 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Plugin Name: BulkPress - Export
* Plugin URI: https://blog.meloniq.net/
*
* Description: Export taxonomies into formatted file compatible with BulkPress plugin.
* Tags: bulkpress, export, taxonomy, terms
*
* Requires at least: 4.9
* Requires PHP: 7.4
* Version: 0.4
*
* Author: MELONIQ.NET
* Author URI: https://meloniq.net/
*
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Text Domain: bulkpress-export
*
* @package BulkPress\Export
*/
namespace BulkPress\Export;
// If this file is accessed directly, then abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Plugin version and textdomain constants
*/
define( 'BPE_VERSION', '0.4' );
define( 'BPE_TD', 'bulkpress-export' );
/**
* Setup.
*
* @return void
*/
function bpe_init() {
global $bulkpress_export;
require_once __DIR__ . '/src/class-admin-page.php';
$bulkpress_export['admin-page'] = new Admin_Page();
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\bpe_init' );
/**
* Creates array of terms paths or slugs.
*
* @param string $taxonomy Taxonomy name.
* @param string $content Type of content to export, either 'names' or 'slugs'.
*
* @return array Array of terms paths or slugs.
*/
function bpe_get_terms_array( $taxonomy, $content ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return array();
}
$terms_args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
);
// get all terms for the taxonomy.
$terms = get_terms( $terms_args );
$paths = array();
$slugs = array();
if ( empty( $terms ) ) {
return array();
}
foreach ( $terms as $key => $term ) {
if ( 0 === $term->parent ) {
$paths[] = bpe_esc_name( $term->name );
} else {
$paths[] = bpe_walk_terms( $terms, $term, '' );
}
$slugs[] = $term->slug;
}
array_multisort( $paths, $slugs );
return ( 'names' === $content ) ? $paths : $slugs;
}
/**
* Creates term path, helper function for bpe_get_taxonomies_array().
*
* @param array $terms All terms for the taxonomy.
* @param object $current_term Current term for which the path is being created.
* @param string $path Current path, used for recursive calls, should be empty when calling function for the first time.
*
* @return string
*/
function bpe_walk_terms( $terms, $current_term, $path ) {
$path = ( empty( $path ) ) ? bpe_esc_name( $current_term->name ) : bpe_esc_name( $current_term->name ) . '/' . $path;
if ( 0 === $current_term->parent ) {
return $path;
}
foreach ( $terms as $term ) {
if ( $current_term->parent === $term->term_id ) {
$path = bpe_walk_terms( $terms, $term, $path );
break;
}
}
return $path;
}
/**
* Escapes name for use in path.
*
* @param string $name Name to escape.
*
* @return string
*/
function bpe_esc_name( $name ) {
$name = str_replace( '&', '&', $name );
$name = str_replace( '/', '\/', $name );
return $name;
}
/**
* Generate and send .txt file to user browser.
*
* @param array $content Array of lines to output in the file.
*
* @return void
*/
function bpe_export( $content = array() ) {
$sitename = sanitize_key( get_bloginfo( 'name' ) );
$filename = $sitename . '-bulkpress-export-' . gmdate( 'Y-m-d' ) . '.txt';
header( 'Content-Description: File Transfer' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Content-Type: text/plain; charset=' . get_option( 'blog_charset' ), true );
echo implode( "\r\n", $content ); // phpcs:ignore
}