forked from BuildTheEarth-Italia/Wordpress-Theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.php
349 lines (304 loc) · 9.7 KB
/
settings.php
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//https://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//importo WP_List_Table se non esiste
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
//creo classe personalizzata
class Photos_List_Table extends WP_List_Table
{
//costruttore
function __construct()
{
parent::__construct(array('ajax' => true));
}
//ritorno nomi delle colonne
function get_columns()
{
return array(
'cb' => '<input type="checkbox" />',
'id' => 'ID',
'path' => 'Anteprima',
'title' => 'Titolo',
'description' => 'Descrizione',
'visible' => 'Visibilità'
);
}
function get_bulk_actions()
{
$actions = array(
'delete' => 'Cancella',
'show' => 'Mostra',
'hide' => 'Nascondi'
);
return $actions;
}
function column_cb($item)
{
return sprintf(
'<input type="checkbox" name="ids[]" value="%s" />',
$item->id
);
}
function column_default($item, $column_name)
{
switch ($column_name) {
case 'path':
$actions = array(
$item->visible ? 'hide' : 'show' => sprintf('<a href="%s">%s</a>', add_query_arg(array('action' => $item->visible ? 'hide' : 'show', 'id' => $item->id)), $item->visible ? 'Nascondi' : 'Mostra'),
'delete' => sprintf('<a href="%s">Elimina</a>', add_query_arg(array('action' => 'delete', 'id' => $item->id))),
);
return sprintf('<img height="30" width="50" src="%1$s" alt="%1$s"/> %2$s', $item->$column_name, $this->row_actions($actions));
break;
case 'visible':
return '<span class="dashicons ' . ($item->visible ? 'dashicons-visibility' : 'dashicons-hidden') . '"></span>';
break;
default:
return $item->$column_name;
}
}
protected function get_views()
{
$views = array('Tutti', 'Nascosti', 'Visibili');
$current = (!empty($_REQUEST['photo_tag']) ? $_REQUEST['photo_tag'] : 'Tutti');
//ciclo per creare links
foreach ($views as &$view) {
if ($view == 'Tutti') {
$arg = remove_query_arg('photo_tag');
} else {
$arg = add_query_arg('photo_tag', $view);
}
$view = '<a href="' . $arg . '"' . ($current == $view ? ' class="current"' : '') . '>' . $view . '</a>';
}
return $views;
}
function usort_reorder($a, $b)
{
// If no sort, default to title
$orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'id';
// If no order, default to asc
$order = (!empty($_GET['order'])) ? $_GET['order'] : 'desc';
// Determine sort order
$result = strcmp($a->$orderby, $b->$orderby);
// Send final sort direction to usort
return ($order === 'asc') ? $result : -$result;
}
function get_sortable_columns()
{
$sortable_columns = array(
'id' => array('id', false),
'title' => array('title', false)
);
return $sortable_columns;
}
//ritorno items
function prepare_items()
{
global $wpdb;
$per_page = 10;
$current_page = $this->get_pagenum();
if (1 < $current_page) {
$offset = $per_page * ($current_page - 1);
} else {
$offset = 0;
}
$items = get_photos_from_bte_theme_showcase(true);
//rimuovo elementi non selezionati
foreach ($items as $item) {
if (isset($_REQUEST['photo_tag']))
if (($_REQUEST['photo_tag'] == 'Nascosti' && $item->visible == true) || ($_REQUEST['photo_tag'] == 'Visibili' && $item->visible == false)) {
continue;
}
$this->items[] = $item;
}
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
usort($items, array(&$this, 'usort_reorder'));
$count = $wpdb->get_var("SELECT COUNT(id) FROM {$wpdb->prefix}bte_theme_showcase;");
// Set the pagination
$this->set_pagination_args(array(
'total_items' => $count,
'per_page' => $per_page,
'total_pages' => ceil($count / $per_page)
));
}
}
function get_photo_list()
{
$media_query = new WP_Query(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
)
);
$list = array();
foreach ($media_query->posts as $post) {
$list[] = $post; #wp_get_attachment_url($post->ID);
}
return $list;
}
function render_bte_theme_menu_page()
{
//creo tabella
$table = new Photos_List_Table();
//controllo azioni
if (!empty($_REQUEST['action']) && !empty($_REQUEST['id'])) {
switch ($_REQUEST['action']) {
case 'delete':
//cancello la foto
delete_photo_from_bte_theme_showcase($_REQUEST['id']);
break;
case 'hide':
//cambio la visibilità della foto a false
hide_photo_of_bte_theme_showcase($_REQUEST['id']);
break;
case 'show':
//cambio la visibilità della foto a true
show_photo_of_bte_theme_showcase($_REQUEST['id']);
break;
}
} else if (!empty($table->current_action()) && !empty($_REQUEST['ids'])) {
switch ($table->current_action()) {
case 'delete':
//cancello ogni foto selezionata
foreach ($_REQUEST['ids'] as $id) {
delete_photo_from_bte_theme_showcase($id);
}
break;
case 'hide':
//cambio la visibilità delle foto selezionate
foreach ($_REQUEST['ids'] as $id) {
hide_photo_of_bte_theme_showcase($id);
}
break;
case 'show':
//cambio la visibilità delle foto selezionate
foreach ($_REQUEST['ids'] as $id) {
show_photo_of_bte_theme_showcase($id);
}
break;
}
}
?>
<div class="wrap pmc-fs">
<h1 class="wp-heading-inline">Impostazioni del tema</h1>
<button class="page-title-action bte-upload">Aggiungi foto</button>
<hr class="wp-header-end">
<?php if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'upload') { ?>
<form method="post">
<?= json_encode(get_photo_list()); ?>
<?php } else {
$table->prepare_items(); ?>
<input type="hidden" name="page" value="" />
<?php $table->views(); ?>
<form method="post">
<?php
// Stampo la tabella per le immagini
$table->display();
}
?>
</form>
<form method="post" action="options.php">
<?php
settings_fields("bte_theme_menu_page");
// Stampo le opzioni
do_settings_sections("bte_theme_menu_page");
// Ed ora il pulstane per salvare
submit_button();
?>
</form>
</div>
<?php
}
function add_bte_theme_menu_item()
{
// Aggiungo una pagina
add_submenu_page('themes.php', 'Impostazioni della galleria', 'Galleria', 'edit_theme_options', 'bte_theme_menu_page', 'render_bte_theme_menu_page');
// La sezione con i campi
add_settings_section('bte_sentences_section', 'Frasi della Homescreen', null, 'bte_theme_menu_page');
add_settings_section('bte_discord_section', 'Collegamenti con Discord', null, 'bte_theme_menu_page');
//Salvo il dato
register_setting('bte_theme_menu_page', 'bte_main_sentence');
register_setting('bte_theme_menu_page', 'bte_second_sentences', ['type' => 'array', 'description' => 'Inserisci una frase per riga']);
register_setting('bte_theme_menu_page', 'bte_discord_url');
register_setting('bte_theme_menu_page', 'bte_discord_add_utm', ['type' => 'boolean']);
register_setting('bte_theme_menu_page', 'bte_discord_widget_url');
add_settings_field('bte_main_sentence', 'Frase primaria', 'bte_fill_main_sentence', 'bte_theme_menu_page', 'bte_sentences_section');
add_settings_field('bte_second_sentences', 'Frasi secondarie', 'bte_fill_second_sentences', 'bte_theme_menu_page', 'bte_sentences_section');
add_settings_field('bte_discord_url', 'URL al server Discord', 'bte_fill_discord_url', 'bte_theme_menu_page', 'bte_discord_section');
add_settings_field('bte_discord_add_utm', 'Usare i tag UTM?', 'bte_fill_discord_add_utm', 'bte_theme_menu_page', 'bte_discord_section');
add_settings_field('bte_discord_widget_url', 'URL del widget Discord', 'bte_fill_discord_widget_url', 'bte_theme_menu_page', 'bte_discord_section');
}
add_action('admin_menu', 'add_bte_theme_menu_item');
//aggiungo supporto a script per usare galleria media
function bte_load_media_scripts($page)
{
if ($page != 'appearance_page_bte_theme_menu_page')
return;
wp_enqueue_media();
wp_enqueue_script('bte_script_admin_media');
}
add_action('admin_enqueue_scripts', 'bte_load_media_scripts');
//risposta ajax
add_action('wp_ajax_bte_add_image_via_ajax', 'bte_add_image_via_ajax');
function bte_add_image_via_ajax()
{
if (!isset($_GET['images'])) {
wp_send_json_error(
new WP_Error('i1', 'Non sono state fornite immagini.'),
500
);
return;
}
foreach ($_GET['images'] as $id) {
//ottengo dati relativi ad immagine
$src = wp_get_attachment_image_url($id, [660, 388]);
$title = get_the_title($id);
$description = get_post($id)->post_content;
//aggiungo foto al database
add_photo_to_bte_theme_showcase(
$id,
$src,
$title,
$description
);
}
//Successo!
wp_send_json_success(null, 201);
}
function bte_fill_main_sentence()
{
?>
<input type="text" name="bte_main_sentence" id="bte_main_sentence" value="<?= get_option('bte_main_sentence'); ?>" />
<?php
}
function bte_fill_second_sentences()
{
?>
<textarea name="bte_second_sentences" id="bte_second_sentences"><?= get_option('bte_second_sentences'); ?></textarea>
<?php
}
function bte_fill_discord_url()
{
?>
<input type="text" name="bte_discord_url" id="bte_discord_url" required value="<?= get_option('bte_discord_url', 'https://discord.com/invite/dMahHCH'); ?>" />
<?php
}
function bte_fill_discord_add_utm()
{
?>
<input type="checkbox" name="bte_discord_add_utm" id="bte_discord_add_utm" value="1" <?php checked(1, get_option('bte_discord_add_utm'), true); ?>" />
<?php
}
function bte_fill_discord_widget_url()
{
?>
<input type="text" name="bte_discord_widget_url" id="bte_discord_widget_url" required value="<?= get_option('bte_discord_widget_url', 'https://discordapp.com/widget?id=686910132017430538&theme=light'); ?>" />
<?php
}
?>