class-fl-builder-template-data-exporter.php
7.0 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Template data exporter for the builder.
*
* @since 1.8
*/
final class FLBuilderTemplateDataExporter {
/**
* Initializes the exporter.
*
* @since 1.8
* @return void
*/
static public function init()
{
add_action( 'plugins_loaded', __CLASS__ . '::init_hooks' );
add_action( 'fl_builder_admin_settings_templates_form', __CLASS__ . '::render_admin_settings', 9 );
add_action( 'fl_builder_admin_settings_save', __CLASS__ . '::save_admin_settings' );
}
/**
* Init actions and filters.
*
* @since 1.8
* @return void
*/
static public function init_hooks()
{
if ( ! is_admin() ) {
return;
}
add_action( 'admin_menu', __CLASS__ . '::menu' );
if ( isset( $_REQUEST['page'] ) && 'fl-builder-template-data-exporter' == $_REQUEST['page'] ) {
add_action( 'admin_enqueue_scripts', __CLASS__ . '::styles_scripts' );
add_action( 'init', __CLASS__ . '::export' );
}
}
/**
* Renders the admin settings.
*
* @since 1.8
* @return void
*/
static public function render_admin_settings()
{
include FL_BUILDER_TEMPLATE_DATA_EXPORTER_DIR . 'includes/admin-settings-templates.php';
}
/**
* Saves the admin settings.
*
* @since 1.8
* @return void
*/
static public function save_admin_settings()
{
if ( isset( $_POST['fl-templates-nonce'] ) && wp_verify_nonce( $_POST['fl-templates-nonce'], 'templates' ) ) {
$admin_ui_enabled = isset( $_POST['fl-template-data-exporter'] ) ? 1 : 0;
FLBuilderModel::update_admin_settings_option( '_fl_builder_template_data_exporter', $admin_ui_enabled, true );
}
}
/**
* Checks to see whether the exporter is enabled or not.
*
* @since 1.8
* @return void
*/
static public function is_enabled()
{
return FLBuilderModel::get_admin_settings_option( '_fl_builder_template_data_exporter', true );
}
/**
* Enqueues scripts and styles for the exporter.
*
* @since 1.8
* @return void
*/
static public function styles_scripts()
{
wp_enqueue_script(
'fl-builder-template-data-exporter',
FL_BUILDER_TEMPLATE_DATA_EXPORTER_URL . 'js/fl-builder-template-data-exporter.js',
array(),
FL_BUILDER_VERSION
);
}
/**
* Renders the admin settings menu.
*
* @since 1.8
* @return void
*/
static public function menu()
{
if ( self::is_enabled() && current_user_can( 'delete_users' ) ) {
$title = __( 'Template Exporter', 'fl-builder' );
$cap = 'delete_users';
$slug = 'fl-builder-template-data-exporter';
$func = __CLASS__ . '::render';
add_submenu_page( 'tools.php', $title, $title, $cap, $slug, $func );
}
}
/**
* Renders the exporter UI.
*
* @since 1.8
* @return void
*/
static public function render()
{
$layouts = self::get_ui_data();
$rows = self::get_ui_data( 'row' );
$modules = self::get_ui_data( 'module' );
include FL_BUILDER_TEMPLATE_DATA_EXPORTER_DIR . 'includes/template-data-exporter.php';
}
/**
* Run the exporter.
*
* @since 1.8
* @return void
*/
static public function export()
{
if ( ! current_user_can( 'delete_users' ) ) {
return;
}
if ( ! isset( $_POST['fl-builder-template-data-exporter-nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['fl-builder-template-data-exporter-nonce'], 'fl-builder-template-data-exporter' ) ) {
return;
}
$templates = array(
'layout' => array(),
'row' => array(),
'module' => array()
);
if ( isset( $_POST['fl-builder-export-layout'] ) && is_array( $_POST['fl-builder-export-layout'] ) ) {
$templates['layout'] = self::get_export_data( $_POST['fl-builder-export-layout'] );
}
if ( isset( $_POST['fl-builder-export-row'] ) && is_array( $_POST['fl-builder-export-row'] ) ) {
$templates['row'] = self::get_export_data( $_POST['fl-builder-export-row'] );
}
if ( isset( $_POST['fl-builder-export-module'] ) && is_array( $_POST['fl-builder-export-module'] ) ) {
$templates['module'] = self::get_export_data( $_POST['fl-builder-export-module'] );
}
header( 'X-Robots-Tag: noindex, nofollow', true );
header( 'Content-Type: application/octet-stream' );
header( 'Content-Description: File Transfer' );
header( 'Content-Disposition: attachment; filename="templates.dat";' );
header( 'Content-Transfer-Encoding: binary' );
echo serialize( $templates );
die();
}
/**
* Returns user template data of a certain type for the UI.
*
* @since 1.8
* @access private
* @param string $type
* @return array
*/
static private function get_ui_data( $type = 'layout' )
{
$templates = array();
foreach( get_posts( array(
'post_type' => 'fl-builder-template',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'fl-builder-template-type',
'field' => 'slug',
'terms' => $type
)
)
) ) as $post ) {
$templates[] = array(
'id' => $post->ID,
'name' => $post->post_title
);
}
return $templates;
}
/**
* Returns user template data for the specified posts.
*
* @since 1.8
* @access private
* @param array $post_ids
* @return array
*/
static private function get_export_data( $post_ids = array() )
{
if ( empty( $post_ids ) ) {
return array();
}
$templates = array();
$index = 0;
foreach( get_posts( array(
'post_type' => 'fl-builder-template',
'orderby' => 'menu_order title',
'order' => 'ASC',
'posts_per_page' => '-1',
'post__in' => $post_ids
) ) as $post ) {
// Build the template object.
$template = new StdClass();
$template->name = $post->post_title;
$template->index = $index++;
$template->type = FLBuilderModel::get_user_template_type( $post->ID );
$template->global = false;
$template->image = '';
$template->categories = array();
$template->nodes = FLBuilderModel::generate_new_node_ids( FLBuilderModel::get_layout_data( 'published', $post->ID ) );
$template->settings = FLBuilderModel::get_layout_settings( 'published', $post->ID );
// Get the template thumbnail.
if ( has_post_thumbnail( $post->ID ) ) {
$attachment_image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
$image_src = apply_filters( 'fl_builder_exporter_template_thumb_src', $attachment_image_src[0] );
$template->image = $image_src;
}
// Get the template categories.
$categories = wp_get_post_terms( $post->ID, 'fl-builder-template-category' );
if ( 0 === count( $categories ) || is_wp_error( $categories ) ) {
$template->categories['uncategorized'] = 'Uncategorized';
}
else {
foreach ( $categories as $category ) {
$template->categories[ $category->slug ] = $category->name;
}
}
// Add the template to the templates array.
$templates[] = apply_filters( 'fl_builder_exporter_template', $template, $post );
}
return $templates;
}
}
FLBuilderTemplateDataExporter::init();