PluginProbe
Name Directory / 1.11
Name Directory v1.11
1.34.1 1.34.0 trunk 1.10 1.11 1.11.1 1.11.2 1.11.3 1.11.4 1.11.5 1.11.6 1.12 1.13 1.13.1 1.13.2 1.13.3 1.13.4 1.13.5 1.13.6 1.13.7 1.14 1.14.1 1.14.2 1.15 1.15.1 All 91 releases
name-directory / admin.php

admin.php in Name Directory 1.11, at admin.php

1,143 lines 49.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 add_action('admin_menu', 'name_directory_register_menu_entry');
4 add_action('wp_ajax_name_directory_ajax_names', 'name_directory_names');
5 add_action('wp_ajax_name_directory_switch_name_published_status', 'name_directory_ajax_switch_name_published_status');
6
7
8 /**
9 * Add a menu entry on options
10 */
11 function name_directory_register_menu_entry()
12 {
13 add_menu_page(
14 __('Name Directory', 'name-directory'),
15 __('Name Directory', 'name-directory'),
16 'manage_options',
17 'name-directory',
18 'name_directory_options',
19 'dashicons-index-card',
20 120);
21
22 add_submenu_page(
23 'name-directory',
24 __('Add directory', 'name-directory'),
25 __('Add directory', 'name-directory'),
26 'manage_options',
27 'admin.php?page=name-directory&sub=new-directory');
28 }
29
30
31 /**
32 * This is a little router for the
33 * name-directory plugin
34 */
35 function name_directory_options()
36 {
37 if (!current_user_can('manage_options'))
38 {
39 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
40 }
41
42 $sub_page = '';
43 if(! empty($_GET['sub']))
44 {
45 $sub_page = $_GET['sub'];
46 }
47 switch($sub_page)
48 {
49 case 'manage-directory':
50 name_directory_names();
51 break;
52 case 'edit-directory':
53 name_directory_edit();
54 break;
55 case 'new-directory':
56 name_directory_edit('new');
57 break;
58 case 'import':
59 name_directory_import();
60 break;
61 case 'export':
62 name_directory_export();
63 break;
64 default:
65 name_directory_show_list();
66 break;
67 }
68
69 }
70
71
72 /**
73 * Show the list of directories and all of the
74 * links to manage the directories
75 */
76 function name_directory_show_list()
77 {
78 global $wpdb;
79 global $name_directory_table_directory;
80 global $name_directory_table_directory_name;
81
82 if(! empty($_GET['delete_dir']) && is_numeric($_GET['delete_dir']))
83 {
84 $name = $wpdb->get_var(sprintf("SELECT `name` FROM %s WHERE id=%d", $name_directory_table_directory, $_GET['delete_dir']));
85 $wpdb->delete($name_directory_table_directory, array('id' => $_GET['delete_dir']), array('%d'));
86 $wpdb->delete($name_directory_table_directory_name, array('directory' => $_GET['delete_dir']), array('%d'));
87 echo "<div class='updated'><p><strong>"
88 . sprintf(__('Name directory %s and all entries deleted', 'name-directory'), "<i>" . $name . "</i>")
89 . "</strong></p></div>";
90 }
91
92 $wp_file = admin_url('admin.php');
93 $wp_page = $_GET['page'];
94 $wp_url_path = sprintf("%s?page=%s", $wp_file, $wp_page);
95 $wp_new_url = sprintf("%s&sub=%s", $wp_url_path, 'new-directory');
96
97
98 echo '<div class="wrap">';
99 echo "<h2>"
100 . __('Name Directory management', 'name-directory')
101 . " <a href='" . $wp_new_url . "' class='add-new-h2'>" . __('Add directory', 'name-directory') . "</a>"
102 . "</h2>";
103
104 if(! empty($_POST['mode']) && ! empty($_POST['dir_id']))
105 {
106 $wpdb->update(
107 $name_directory_table_directory,
108 array(
109 'name' => $_POST['name'],
110 'description' => $_POST['description'],
111 'show_title' => $_POST['show_title'],
112 'show_description' => $_POST['show_description'],
113 'show_submit_form' => $_POST['show_submit_form'],
114 'show_search_form' => $_POST['show_search_form'],
115 'show_submitter_name' => $_POST['show_submitter_name'],
116 'show_line_between_names' => $_POST['show_line_between_names'],
117 'show_all_names_on_index' => $_POST['show_all_names_on_index'],
118 'show_all_index_letters' => $_POST['show_all_index_letters'],
119 'jump_to_search_results' => $_POST['jump_to_search_results'],
120 'nr_columns' => $_POST['nr_columns'],
121 'nr_most_recent' => intval($_POST['nr_most_recent']),
122 'nr_words_description' => intval($_POST['nr_words_description']),
123 'name_term' => $_POST['name_term'],
124 ),
125 array('id' => intval($_POST['dir_id']))
126 );
127
128 echo "<div class='updated'><p>"
129 . sprintf(__('Directory %s updated.', 'name-directory'), "<i>" . esc_sql($_POST['name']) . "</i>")
130 . "</p></div>";
131
132 unset($_GET['dir_id']);
133 }
134 elseif(! empty($_POST['mode']) && $_POST['mode'] == "new")
135 {
136 $wpdb->insert(
137 $name_directory_table_directory,
138 array(
139 'name' => $_POST['name'],
140 'description' => $_POST['description'],
141 'show_title' => $_POST['show_title'],
142 'show_description' => $_POST['show_description'],
143 'show_submit_form' => $_POST['show_submit_form'],
144 'show_search_form' => $_POST['show_search_form'],
145 'show_submitter_name' => $_POST['show_submitter_name'],
146 'show_line_between_names' => $_POST['show_line_between_names'],
147 'show_all_names_on_index' => $_POST['show_all_names_on_index'],
148 'show_all_index_letters' => $_POST['show_all_index_letters'],
149 'jump_to_search_results' => $_POST['jump_to_search_results'],
150 'nr_columns' => $_POST['nr_columns'],
151 'nr_most_recent' => $_POST['nr_most_recent'],
152 'nr_words_description' => $_POST['nr_words_description'],
153 'name_term' => $_POST['name_term'],
154 ),
155 array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d')
156 );
157
158 echo "<div class='updated'><p>"
159 . sprintf(__('Directory %s created.', 'name-directory'), "<i>" . esc_sql($_POST['name']) . "</i>")
160 . "</p></div>";
161 }
162
163 $directories = $wpdb->get_results("SELECT * FROM $name_directory_table_directory");
164 $num_directories = $wpdb->num_rows;
165 $plural = ($num_directories==1)?__('name directory', 'name-directory'):__('name directories', 'name-directory');
166
167 echo "<p>"
168 . sprintf(__('You currently have %d %s.', 'name-directory'), $num_directories, $plural)
169 . "</p>";
170 ?>
171
172 <table class="wp-list-table widefat fixed name-directory" cellspacing="0">
173 <thead><?php name_directory_render_admin_overview_table_headerfooter(); ?></thead>
174
175 <tbody>
176 <?php
177 foreach ( $directories as $directory )
178 {
179 $entries = $wpdb->get_var(sprintf("SELECT COUNT(`id`) FROM %s WHERE directory=%d", $name_directory_table_directory_name, $directory->id));
180 $unpublished = $wpdb->get_var(sprintf("SELECT COUNT(`id`) FROM %s WHERE directory=%d AND `published` = 0", $name_directory_table_directory_name, $directory->id));
181 echo sprintf("
182 <tr class='type-page status-publish hentry alternate iedit author-self' valign='top'>
183 <th scope='col'>&nbsp;</th>
184 <td class='post-title page-title column-title' style='padding-left: 0;'>
185 <strong><a class='row-title' href='" . $wp_url_path . "&sub=manage-directory&dir=%d' title='%s'>%s</a>
186 <span style='font-weight: normal;'>&nbsp;%s</span></strong>
187 <div class='locked-info'>&nbsp;</div>
188 <div class='row-actions'>
189 <span class='manage'><a href='" . $wp_url_path . "&sub=manage-directory&dir=%d' title='%s'>%s</a>
190 | </span><span><a href='" . $wp_url_path . "&sub=manage-directory&dir=%d#anchor_add_name' title='%s'>%s</a>
191 | </span><span><a href='" . $wp_url_path . "&sub=edit-directory&dir=%d' title='%s'>%s</a>
192 | </span><span><a href='" . $wp_url_path . "&sub=import&dir=%d' title='%s'>%s</a>
193 | </span><span><a href='" . $wp_url_path . "&sub=export&dir=%d' title='%s'>%s</a>
194 | </span><span class='view'><a class='toggle-info' data-id='%s' href='" . $wp_url_path . "&sub=manage-directory&dir=%d#shortcode' title='%s'>%s</a></span>
195 | </span><span class='trash'><a class='namedirectory_confirmdelete submitdelete' href='" . $wp_url_path . "&delete_dir=%d' title=%s'>%s</a>
196 </div>
197 </td>
198 <td>
199 &nbsp; <strong title='%s'>%d</strong>
200 <br /><br />&nbsp;
201 </td>
202 <td>%d</td>
203 <td>%d</td>
204 </tr>",
205
206 $directory->id, $directory->name, $directory->name,
207 substr($directory->description, 0, 70),
208 $directory->id, __('Add, edit and remove names', 'name-directory'), __('Manage names', 'name-directory'),
209 $directory->id, __('Go to the add-name-form on the Manage page', 'name-directory'), __('Add name', 'name-directory'),
210 $directory->id, __('Edit name, description and appearance settings', 'name-directory'), __('Settings', 'name-directory'),
211 $directory->id, __('Import entries for this directory by uploading a .csv file', 'name-directory'), __('Import', 'name-directory'),
212 $directory->id, __('Download the contents of this directory as a .csv file', 'name-directory'), __('Export', 'name-directory'),
213 $directory->id, $directory->id, __('Show the copy-paste shortcode for this directory', 'name-directory'), __('Shortcode', 'name-directory'),
214 $directory->id, __('Permanently remove this name directory', 'name-directory'), __('Delete', 'name-directory'),
215
216 __('Number of names in this directory', 'name-directory'),
217 $entries,
218 ($entries - $unpublished),
219 $unpublished
220 );
221 echo sprintf("
222 <tr id='embed_code_%s' style='display: none;'>
223 <td>&nbsp;</td>
224 <td align='right'>%s</td>
225 <td colspan='5'>
226 <input value='[namedirectory dir=\"%s\"]' type='text' size='25' id='title'
227 style='text-align: center; padding: 8px 5px;' />
228 </td>
229 </tr>
230 <tr style='display: none;'><td colspan='7'>&nbsp;</td></tr>",
231 $directory->id,
232 __('To show your directory on your website, use the shortcode on the right.', 'name-directory') . '<br />' .
233 __('Copy the code and paste it in a post or in a page.', 'name-directory') . '<br /><small>' .
234 __('If you want to start with a specific character, like "J", use [namedirectory dir="X" start_with="j"].', 'name-directory') . '</small>',
235 $directory->id);
236 }
237 ?>
238 </tbody>
239
240 <tfoot><?php name_directory_render_admin_overview_table_headerfooter(); ?></tfoot>
241 </table>
242
243 <script type='text/javascript'>
244 jQuery(document).ready(function()
245 {
246 jQuery('.toggle-info').on('click', function(event)
247 {
248 event.preventDefault();
249 var toggle_id = jQuery(this).attr('data-id');
250 jQuery('#embed_code_' + toggle_id).toggle();
251 return false;
252 });
253
254 jQuery('.namedirectory_confirmdelete').click(function(){
255 delete_directory = confirm('<?php echo __('Are you sure you want to delete this name directory?', 'name-directory') ?>');
256 if(delete_directory == false)
257 {
258 return false;
259 }
260 });
261
262 });
263 </script>
264 <?php
265 }
266
267
268 /**
269 * A double purpose function for editing a name-directory and
270 * creating a new directory.
271 * @param string $mode
272 */
273 function name_directory_edit($mode = 'edit')
274 {
275 if (!current_user_can('manage_options'))
276 {
277 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
278 }
279
280 global $wpdb;
281 global $name_directory_table_directory;
282
283 $wp_file = admin_url('admin.php');
284 $wp_page = $_GET['page'];
285 $wp_sub = $_GET['sub'];
286 $overview_url = sprintf("%s?page=%s", $wp_file, $wp_page, $wp_sub);
287 $wp_url_path = sprintf("%s?page=%s", $wp_file, $wp_page);
288
289 $directory_id = 0;
290 if(! empty($_GET['dir']))
291 {
292 $directory_id = intval($_GET['dir']);
293 }
294 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . $directory_id, ARRAY_A);
295
296 echo '<div class="wrap">';
297 if($mode == "new")
298 {
299 $table_heading = __('Create new name directory', 'name-directory');
300 $button_text = __('Create', 'name-directory');
301 echo "<h2>" . __('Create new name directory', 'name-directory') . "</h2>";
302 echo "<p>" . __('Complete the form below to create a new name directory.', 'name-directory');
303 }
304 else
305 {
306 $table_heading = __('Edit this directory', 'name-directory');
307 $button_text = __('Save Changes', 'name-directory');
308 echo "<h2>" . __('Edit name directory', 'name-directory') . "</h2>";
309 echo "<p>"
310 . sprintf(__('You are editing the name, description and settings of directory %s', 'name-directory'),
311 $directory['name']);
312 }
313 echo " <a style='float: right;' href='" . $overview_url . "'>" . __('Back to the directory overview', 'name-directory') . "</a></p>";
314 ?>
315
316 <form name="add_name" method="post" action="<?php echo $wp_url_path; ?>">
317 <table class="wp-list-table widefat" cellpadding="0">
318 <thead>
319 <tr>
320 <th colspan="2">
321 <?php echo $table_heading; ?>
322 <input type="hidden" name="dir_id" value="<?php echo $directory_id; ?>">
323 <input type="hidden" name="mode" value="<?php echo $mode; ?>">
324 </th>
325 </tr>
326 </thead>
327 <tbody>
328 <tr>
329 <td width="29%"><?php echo __('Title', 'name-directory'); ?></td>
330 <td width="70%"><input type="text" name="name" value="<?php echo $directory['name']; ?>" size="20" style="width: 100%;"></td>
331 </tr>
332 <tr>
333 <td><?php echo __('Description', 'name-directory'); ?></td>
334 <td><textarea name="description" rows="5" style="width: 100%;"><?php echo $directory['description']; ?></textarea></td>
335 </tr>
336
337 <?php
338 $dir_boolean_settings = array(
339 'show_title' => array(
340 'friendly_name' => __('Show title', 'name-directory'),
341 'description' => false,
342 ),
343 'show_description' => array(
344 'friendly_name' => __('Show description', 'name-directory'),
345 'description' => false,
346 ),
347 'show_submit_form' => array(
348 'friendly_name' => __('Submit form', 'name-directory'),
349 'description' => __('Visitors can submit suggestions', 'name-directory'),
350 ),
351 'show_submitter_name' => array(
352 'friendly_name' => __('Submitter name', 'name-directory'),
353 'description' => __('Show the name of the submitter', 'name-directory'),
354 ),
355 'show_search_form' => array(
356 'friendly_name' => __('Show search form', 'name-directory'),
357 'description' => false,
358 ),
359 'show_line_between_names' => array(
360 'friendly_name' => __('Show line between names', 'name-directory'),
361 'description' => false,
362 ),
363 'show_all_names_on_index' => array(
364 'friendly_name' => __('Show all names by default', 'name-directory'),
365 'description' => __('If no, user HAS to use the index before entries are shown', 'name-directory'),
366 ),
367 'show_all_index_letters' => array(
368 'friendly_name' => __('Show all letters on index', 'name-directory'),
369 'description' => __('If no, just A B D E are shown if there are no entries starting with C', 'name-directory'),
370 ),
371 'jump_to_search_results' => array(
372 'friendly_name' => __('Jump to Name Directory when searching', 'name-directory'),
373 'description' => __('On the front-end, jump to the Name Directory search box. Particularly useful if you have Name Directory on a long page or onepage websites', 'name-directory'),
374 ),
375 );
376
377 $dir_options_settings = array(
378 'nr_most_recent' => array(
379 'friendly_name' => __('Show most recent names', 'name-directory'),
380 'description' => __('If No, frontend will not show \'Latest\' option.', 'name-directory'),
381 'options' => array(0 => __('No', 'name-directory'), 3 => 3, 5 => 5, 10 => 10, 25 => 25, 50 => 50, 100 => 100)
382 ),
383 'nr_words_description' => array(
384 'friendly_name' => __('Limit amount of words in description', 'name-directory'),
385 'description' => __('Display a "read-more" link on the website if the description exceeds X characters.', 'name-directory'),
386 'options' => array(0 => __('No', 'name-directory'), 10 => 10, 20 => 20, 25 => 25, 50 => 50, 100 => 100)
387 ),
388 'nr_columns' => array(
389 'friendly_name' => __('Number of columns', 'name-directory'),
390 'description' => __('The number of (vertical) columns to display the names in', 'name-directory'),
391 'options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4)
392 ),
393 );
394
395
396 foreach($dir_boolean_settings as $setting_name => $setting_props)
397 {
398 name_directory_render_admin_setting_boolean($directory, $setting_name, $setting_props['friendly_name'], $setting_props['description']);
399 }
400
401 foreach($dir_options_settings as $setting_name => $setting_props)
402 {
403 name_directory_render_admin_setting_options($directory, $setting_name, $setting_props['friendly_name'], $setting_props['description'], $setting_props['options']);
404 }
405 ?>
406 <tr>
407 <td width="29%"><?php echo __('Name term', 'name-directory'); ?><br>
408 <small><?php echo __('Alternative (plural) term for "names", i.e. movies', 'name-directory'); ?></small></td>
409 <td width="70%"><input type="text" name="name_term" value="<?php echo $directory['name_term']; ?>" size="20" style="width: 100%;">
410 <br>
411 <small><?php echo __('If you provide an alternative term for name, your website will display:', 'name-directory'); ?>
412 "<i><?php echo sprintf(__('There are currently %d %s in this directory', 'name-directory'), 1337, "movies"); ?></i>"<br>
413 <?php echo __('When left blank, the word "names" will be displayed.', 'name-directory'); ?></small></td>
414 </tr>
415 <tr>
416 <td>&nbsp;</td>
417 <td>
418 <input type="submit" name="submit" class="button button-primary button-large"
419 value="<?php echo $button_text; ?>" />
420
421 <a class='button button-large' href='<?php echo $overview_url; ?>'>
422 <?php echo __('Cancel', 'name-directory'); ?>
423 </a>
424 </td>
425 </tr>
426 </tbody>
427 </table>
428 </form>
429
430 <?php
431
432 }
433
434
435 /**
436 * Handle the names in the name directory
437 * - Display all names
438 * - Edit names (ajax and 'oldskool' view)
439 * - Create new names
440 */
441 function name_directory_names()
442 {
443 if (!current_user_can('manage_options'))
444 {
445 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
446 }
447
448 global $wpdb;
449 global $name_directory_table_directory;
450 global $name_directory_table_directory_name;
451
452 if(! empty($_GET['delete_name']) && is_numeric($_GET['delete_name']))
453 {
454 $name = $wpdb->get_var(sprintf("SELECT `name` FROM %s WHERE id=%d", $name_directory_table_directory_name, $_GET['delete_name']));
455 $wpdb->delete($name_directory_table_directory_name, array('id' => $_GET['delete_name']), array('%d'));
456 echo "<div class='updated'><p>"
457 . sprintf(__('Name %s deleted', 'name-directory'), "<i>" . $name . "</i>")
458 . "</p></div>";
459 }
460 else if(! empty($_POST['name_id']))
461 {
462 $wpdb->update(
463 $name_directory_table_directory_name,
464 array(
465 'name' => stripslashes_deep($_POST['name']),
466 'letter' => name_directory_get_first_char($_POST['name']),
467 'description' => stripslashes_deep($_POST['description']),
468 'published' => $_POST['published'],
469 'submitted_by' => $_POST['submitted_by'],
470 ),
471 array('id' => intval($_POST['name_id']))
472 );
473
474 if($_POST['action'] == "name_directory_ajax_names")
475 {
476 echo '<p>';
477 echo sprintf(__('Name %s updated', 'name-directory'), "<i>" . esc_sql($_POST['name']) . "</i>");
478 echo '. <small><i>' . __('Will be visible when the page is refreshed.', 'name-directory') . '</i> ';
479 echo ' <a href="">' . __('Refresh now', 'name-directory') . '</a></small>';
480 echo '</p>';
481 exit;
482 }
483
484 echo "<div class='updated'><p>"
485 . sprintf(__('Name %s updated', 'name-directory'), "<i>" . esc_sql($_POST['name']) . "</i>")
486 . "</p></div>";
487
488 unset($_GET['edit_name']);
489 }
490 else if(! empty($_POST['name']))
491 {
492 $name_exists = name_directory_name_exists_in_directory($_POST['name'], $_POST['directory']);
493 if($name_exists && $_POST['action'] == "name_directory_ajax_names")
494 {
495 echo '<p>';
496 echo sprintf(__('Name %s was already on the list, so it was not added', 'name-directory'),
497 '<i>' . esc_sql($_POST['name']) . '</i>');
498 echo '</p>';
499 exit;
500 }
501
502 $wpdb->insert(
503 $name_directory_table_directory_name,
504 array(
505 'directory' => $_POST['directory'],
506 'name' => stripslashes_deep($_POST['name']),
507 'letter' => name_directory_get_first_char($_POST['name']),
508 'description' => stripslashes_deep($_POST['description']),
509 'published' => $_POST['published'],
510 'submitted_by' => $_POST['submitted_by'],
511 ),
512 array('%d', '%s', '%s', '%s', '%d', '%s')
513 );
514
515 if($_POST['action'] == "name_directory_ajax_names")
516 {
517 echo '<p>';
518 printf(__('New name %s added', 'name-directory'), '<i>' . esc_sql($_POST['name']) . '</i> ');
519 echo '. <small><i>' . __('Will be visible when the page is refreshed.', 'name-directory') . '</i> ';
520 echo ' <a href="">' . __('Refresh now', 'name-directory') . '</a></small>';
521 echo '</p>';
522 exit;
523 }
524
525 echo "<div class='updated'><p><strong>"
526 . sprintf(__('New name %s added', 'name-directory'), "<i>" . esc_sql($_POST['name']) . "</i> ")
527 . "</strong></p></div>";
528 }
529 else if($_SERVER['REQUEST_METHOD'] == 'POST')
530 {
531 if($_POST['action'] == "name_directory_ajax_names")
532 {
533 echo '<p>' . __('Please fill in at least a name', 'name-directory') . '</p>';
534 exit;
535 }
536
537 echo "<div class='error'><p><strong>"
538 . __('Please fill in at least a name', 'name-directory')
539 . "</strong></p></div>";
540 }
541
542 $directory_id = intval($_GET['dir']);
543
544 $wp_file = admin_url('admin.php');
545 $wp_page = $_GET['page'];
546 $wp_sub = $_GET['sub'];
547 $overview_url = sprintf("%s?page=%s", $wp_file, $wp_page);
548 $wp_url_path = sprintf("%s?page=%s&sub=%s&dir=%d", $wp_file, $wp_page, $wp_sub, $directory_id);
549
550 $published_status = '0,1';
551 $emphasis_class = 's_all';
552 if(! empty($_GET['status']) && $_GET['status'] == 'published')
553 {
554 $published_status = '1';
555 $emphasis_class = 's_published';
556 }
557 else if(! empty($_GET['status']) && $_GET['status'] == 'unpublished')
558 {
559 $published_status = '0';
560 $emphasis_class = 's_unpublished';
561 }
562
563 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . $directory_id, ARRAY_A);
564 $names = $wpdb->get_results(sprintf("SELECT * FROM %s WHERE `directory` = %d AND `published` IN (%s) ORDER BY `name` ASC",
565 $name_directory_table_directory_name, $directory_id, $published_status));
566
567 echo '<div class="wrap">';
568 echo "<h2>" . sprintf(__('Manage names for %s', 'name-directory'), $directory['name']) . "</h2>";
569 ?>
570
571 <p>
572 View:
573 <a class='s_all' href='<?php echo $wp_url_path; ?>&status=all'><?php _e('all', 'name-directory'); ?></a> |
574 <a class='s_published' href='<?php echo $wp_url_path; ?>&status=published'><?php _e('published', 'name-directory'); ?></a> |
575 <a class='s_unpublished' href='<?php echo $wp_url_path; ?>&status=unpublished'><?php _e('unpublished', 'name-directory'); ?></a>
576
577 <span style='float: right';>
578 <a href='<?php echo $overview_url; ?>'><?php _e('Back to the directory overview', 'name-directory'); ?></a>
579 </span>
580 </p>
581
582 <table class="wp-list-table widefat name_directory_names fixed" cellpadding="0">
583 <thead>
584 <tr>
585 <th width="18%"><?php echo __('Name', 'name-directory'); ?></th>
586 <th width="54%"><?php echo __('Description', 'name-directory'); ?></th>
587 <th width="12%"><?php echo __('Submitter', 'name-directory'); ?></th>
588 <th width="9%"><?php echo __('Published', 'name-directory'); ?></th>
589 <th width="15%"><?php echo __('Manage', 'name-directory'); ?></th>
590 </tr>
591 </thead>
592 <tbody>
593 <?php
594 if(empty($names))
595 {
596 echo sprintf("<tr class='empty-directory'><td colspan='5'>%s</td></tr>",
597 __('Currently, there are no names in this directory..', 'name-directory'));
598 }
599 foreach($names as $name)
600 {
601 echo sprintf("
602 <tr>
603 <td>%s</td><td>%s</td><td>%s</td><td><span title='%s' class='toggle_published' id='nid_%d' data-nameid='%d'>%s</span></td>
604 <td><a class='button button-primary button-small' href='" . $wp_url_path . "&edit_name=%d#anchor_add_form'>%s</a>
605 <a class='button button-small' href='" . $wp_url_path . "&delete_name=%d'>%s</a>
606 </td>
607 </tr>",
608 $name->name, html_entity_decode(stripslashes($name->description)), $name->submitted_by,
609 __('Toggle published status', 'name-directory'), $name->id,
610 $name->id, name_directory_yesno($name->published),
611 $name->id, __('Edit', 'name-directory'),
612 $name->id, __('Delete', 'name-directory'));
613 }
614 ?>
615 </tbody>
616 </table>
617
618 <p>&nbsp;</p>
619
620 <?php
621 if(! empty($_GET['edit_name']))
622 {
623 $name = $wpdb->get_row(sprintf("SELECT * FROM `%s` WHERE `id` = %d",
624 $name_directory_table_directory_name, $_GET['edit_name']), ARRAY_A);
625 $table_heading = __('Edit a name', 'name-directory');
626 $save_button_txt = __('Save name', 'name-directory');
627 }
628 else
629 {
630 $table_heading = __('Add a new name', 'name-directory');
631 $save_button_txt = __('Add name', 'name-directory');
632 $name = array('name' => null, 'description' => null, 'submitted_by' => null);
633 }
634
635 ?>
636 <span style='float: right';>
637 <a href='<?php echo $overview_url; ?>'><?php _e('Back to the directory overview', 'name-directory'); ?></a>
638 </span>
639
640 <p>&nbsp;</p>
641
642 <div class="hidden" id="add_result"></div>
643
644 <a name="anchor_add_form"></a>
645 <form name="add_name" id="add_name_ajax" method="post" action="<?php echo $wp_url_path; ?>">
646 <table class="wp-list-table widefat" cellpadding="0">
647 <thead>
648 <tr>
649 <th width="18%"><?php echo $table_heading; ?>
650 <input type="hidden" name="directory" value="<?php echo $directory_id; ?>">
651 <?php
652 if(! empty($_GET['edit_name']))
653 {
654 echo '<input type="hidden" name="name_id" id="edit_name_id" value="' . intval($_GET['edit_name']) . '">';
655 }
656 ?>
657 <input type="hidden" name="action" value="0" id="add_form_ajax_submit" />
658 </th>
659 <th align="right">
660
661 <label id="input_compact" title="<?php echo __('Show the compact form, showing only the name, always published)', 'name-directory'); ?>">
662 <input type="radio" name="input_mode" />
663 <?php echo __('Quick add view', 'name-directory'); ?>
664 </label>
665 <label id="input_extensive" title="<?php echo __('Show the full form, which allows you to enter a description and submitter', 'name-directory'); ?>">
666 <input type="radio" name="input_mode" />
667 <?php echo __('Full add view', 'name-directory'); ?>
668 </label>
669
670 </th>
671 </tr>
672 </thead>
673 <tbody>
674 <tr id="add_name">
675 <td width="18%"><?php echo __('Name', 'name-directory'); ?></td>
676 <td width="82%"><input type="text" name="name" value="<?php echo $name['name']; ?>" size="20" style="width: 100%;"></td>
677 </tr>
678 <tr id="add_description">
679 <td><?php echo __('Description', 'name-directory'); ?></td>
680 <td><textarea name="description" rows="5" style="width: 100%;"><?php echo stripslashes($name['description']); ?></textarea>
681 <small><strong><?php echo __('Please be careful!', 'name-directory'); ?></strong>
682 <?php echo __('HTML markup is allowed and will we printed on your website and in the WordPress admin.', 'name-directory'); ?></small></td>
683 </tr>
684 <tr id="add_published">
685 <td><?php echo __('Published', 'name-directory'); ?></td>
686 <td>
687 <input type="radio" name="published" id="published_yes" value="1" checked="checked">
688 <label for="published_yes"><?php echo __('Yes', 'name-directory') ?></label>
689
690 <input type="radio" name="published" id="published_no" value="0"
691 <?php
692 if(isset($name['published']) && empty($name['published']))
693 {
694 echo 'checked="checked"';
695 }?>>
696 <label for="published_no"><?php echo __('No', 'name-directory') ?></label>
697 </td>
698 </tr>
699 <tr id="add_submitter">
700 <td><?php echo __('Submitted by', 'name-directory'); ?></td>
701 <td><input type="text" name="submitted_by" value="<?php echo $name['submitted_by']; ?>" size="20" style="width: 100%;"></td>
702 </tr>
703 <tr>
704 <td>&nbsp;</td>
705 <td>
706 <input type="submit" id="add_button" name="Submit" class="button button-primary button-large"
707 value="<?php echo $save_button_txt; ?>" />
708 </td>
709 </tr>
710 </tbody>
711 </table>
712 </form>
713
714 <?php
715 name_directory_print_javascript($emphasis_class);
716 name_directory_print_style();
717 }
718
719 /**
720 * Import names from a csv file into directory
721 */
722 function name_directory_import()
723 {
724 if (!current_user_can('manage_options'))
725 {
726 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
727 }
728
729 global $wpdb;
730 global $name_directory_table_directory;
731 global $name_directory_table_directory_name;
732
733 $directory_id = intval($_GET['dir']);
734 $import_success = false;
735
736 if($_SERVER['REQUEST_METHOD'] == 'POST')
737 {
738 $file = wp_import_handle_upload();
739
740 if( isset($file['error']))
741 {
742 echo $file['error'];
743 return;
744 }
745
746 $csv = array_map('str_getcsv', file($file['file']));
747
748 wp_import_cleanup($file['id']);
749 array_shift($csv);
750
751 $names_error = 0;
752 $names_imported = 0;
753 $names_duplicate = 0;
754 foreach($csv as $entry)
755 {
756 if(! $prepared_row = name_directory_prepared_import_row($entry))
757 {
758 continue;
759 }
760
761 if(name_directory_name_exists_in_directory($prepared_row['name'], $directory_id))
762 {
763 $names_duplicate++;
764 continue;
765 }
766
767 $db_res = $wpdb->insert(
768 $name_directory_table_directory_name,
769 array(
770 'directory' => $directory_id,
771 'name' => stripslashes_deep($prepared_row['name']),
772 'letter' => name_directory_get_first_char($prepared_row['name']),
773 'description' => stripslashes_deep($prepared_row['description']),
774 'published' => $prepared_row['published'],
775 'submitted_by' => !empty($prepared_row['submitted_by']) ? $prepared_row['submitted_by'] : '',
776 ),
777 array('%d', '%s', '%s', '%s', '%d', '%s')
778 );
779
780 if($db_res === false)
781 {
782 $names_error++;
783 }
784 else
785 {
786 $names_imported++;
787 }
788 }
789
790 $notice_class = 'updated';
791 $import_success = true;
792 $import_message = sprintf(__('Imported %d entries in this directory', 'name-directory'), $names_imported);
793
794 if($names_imported === 0)
795 {
796 $notice_class = 'error';
797 $import_success = false;
798 $import_message = __('Could not import any names into Name Directory', 'name-directory');
799 }
800
801 if($names_error > 0)
802 {
803 $notice_class = 'error';
804 $import_success = false;
805 if($names_imported === 0)
806 {
807 $import_message .= "! ";
808 }
809 $import_message .= sprintf(__('There were %d names that produces errors with the WordPress database on import', 'name-directory'), $names_error);
810 }
811
812 if($names_duplicate > 0)
813 {
814 $ignored = (count($csv)==$names_duplicate)?__('all', 'name-directory'):$names_duplicate;
815 echo '<div class="error" style="border-left: 4px solid #ffba00;"><p>'
816 . sprintf(__('Ignored %s names, because they were duplicate (already in the directory)', 'name-directory'), $ignored)
817 . '</p></div>';
818 }
819 elseif($names_imported === 0)
820 {
821 $import_message .= ', ' . __('please check your .csv-file', 'name-directory');
822 }
823
824 echo '<div class="' . $notice_class . '"><p>' . $import_message . '</p></div>';
825 }
826
827 $wp_file = admin_url('admin.php');
828 $wp_page = $_GET['page'];
829 $wp_sub = $_GET['sub'];
830 $overview_url = sprintf("%s?page=%s", $wp_file, $wp_page);
831 $wp_url_path = sprintf("%s?page=%s&sub=%s&dir=%d", $wp_file, $wp_page, $wp_sub, $directory_id);
832 $wp_ndir_path = sprintf("%s?page=%s&sub=%s&dir=%d", $wp_file, $wp_page, 'manage-directory', $directory_id);
833
834 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . $directory_id, ARRAY_A);
835
836 echo '<div class="wrap">';
837 echo '<h2>' . sprintf(__('Import names for %s', 'name-directory'), $directory['name']) . '</h2>';
838 echo '<div class="narrow"><p>';
839 if(! $import_success && empty($names_duplicate))
840 {
841 echo __('Use the upload form below to upload a .csv-file containing all of your names (in the first column), description and submitter are optional.', 'name-directory') . ' ';
842 echo '<h4>' . __('If you saved it from Excel or OpenOffice, please ensure that:', 'name-directory') . '</h4> ';
843 echo '<ol><li>' . __('There is a header row (this contains the column names, the first row will NOT be imported)', 'name-directory');
844 echo '</li><li>' . __('Fields are encapsulated by double quotes', 'name-directory');
845 echo '</li><li>' . __('Fields are comma-separated', 'name-directory');
846 echo '</li></ol>';
847 echo '<h4>' . __('If uploading or importing fails, these are your options', 'name-directory') . ':</h4><ol><li>';
848 echo sprintf(__('Please check out %s first and ensure your file is formatted the same.', 'name-directory'),
849 '<a href="http://plugins.svn.wordpress.org/name-directory/assets/name-directory-import-example.csv" target="_blank">' .
850 __('the example import file', 'name-directory') . '</a>') . '</li>';
851 echo '<li>
852 <a href="https://wiki.openoffice.org/wiki/Documentation/OOo3_User_Guides/Calc_Guide/Saving_spreadsheets#Saving_as_a_CSV_file">OpenOffice csv-export help</a>
853 </li>
854 <li>
855 <a href="https://support.office.com/en-us/article/Import-or-export-text-txt-or-csv-files-e8ab9ff3-be8d-43f1-9d52-b5e8a008ba5c?CorrelationId=fa46399d-2d7a-40bd-b0a5-27b99e96cf68&ui=en-US&rs=en-US&ad=US#bmexport">Excel csv-export help</a>
856 </li>
857 <li>
858 <a href="http://www.freefileconvert.com" target="_blank">' .
859 __('Use an online File Convertor', 'name-directory') . '</a>
860 </li><li>';
861 echo sprintf(__('If everything else fails, you can always ask a question at the %s.', 'name-directory'),
862 '<a href="https://wordpress.org/support/plugin/name-directory" target="_blank">' .
863 __('plugin support forums', 'name-directory') . '</a>') . ' ';
864 echo '</li></ol></p>';
865
866 if(! function_exists('str_getcsv'))
867 {
868 echo '<div class="error"><p>';
869 echo __('Name Directory Import requires PHP 5.3, you seem to have in older version. Importing names will not work for your website.', 'name-directory');
870 echo '</p></div>';
871 }
872
873 echo '<h3>' . __('Upload your .csv-file', 'name-directory') . '</h3>';
874 wp_import_upload_form($wp_url_path);
875 }
876 echo '</div></div>';
877 echo '<a href="' . $wp_ndir_path . '">' . sprintf(__('Back to %s', 'name-directory'), '<i>' . $directory['name'] . '</i>') . '</a>';
878 echo ' | ';
879 echo '<a href="' . $overview_url . '">' . __('Go to Name Directory Overview', 'name-directory') . '</a>';
880 }
881
882
883 /**
884 * Page to export names from a directory file as a .csv-file
885 */
886 function name_directory_export()
887 {
888 if (!current_user_can('manage_options'))
889 {
890 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
891 }
892
893 global $wpdb;
894 global $name_directory_table_directory;
895
896 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . intval($_GET['dir']), ARRAY_A);
897
898 $names = name_directory_get_directory_names($directory);
899
900 echo '<table id="export_names" class="hidden"><thead><tr><th>name</th><th>description</th><th>submitter</th></tr></thead><tbody>';
901 foreach($names as $entry)
902 {
903 echo '<tr><td>' . $entry['name'] . '</td><td>' . html_entity_decode(stripslashes($entry['description'])) . '</td><td>' . $entry['submitted_by'] . '</td></tr>';
904 }
905 echo '</tbody></table>';
906
907 /* Notify the user of possible not-working export functionality */
908 if(stripos($_SERVER['HTTP_USER_AGENT'], 'Chrome') === false && stripos($_SERVER['HTTP_USER_AGENT'], 'Firefox') === false)
909 {
910 echo '<div class="notice notice-warning"><p>';
911 echo __('Name Directory Export works best in Mozilla Firefox, Google Chrome and Internet Explorer 10+.', 'name-directory') . ' ';
912 echo __('If you encounter problems (or it does not export) in Internet Explorer or Microsoft Edge, please try another browser.', 'name-directory');
913 echo '</div>';
914 }
915
916 echo '<div class="wrap">';
917 echo '<h2>' . sprintf(__('Export directory %s', 'name-directory'), $directory['name']) . '</h2>';
918 echo '<div class="narrow"><p>';
919 echo __('Click the Export button to download a .csv file with the contents of your directory.', 'name-directory');
920 echo '</p><p><a href="#" id="export_name_directory_names_button" style="text-decoration:none;color:#000;background-color:#ddd;border:1px solid #ccc;padding:8px;">' . __('Export', 'name-directory') . '</a></p>';
921 echo '<a href="' . admin_url('admin.php') . '?page=name-directory">' . __('Go to Name Directory Overview', 'name-directory') . '</a>';
922
923 name_directory_print_export_javascript();
924 }
925
926
927 /**
928 * Proxy for the AJAX request to switch published-statusses
929 * No params, assumes POST
930 */
931 function name_directory_ajax_switch_name_published_status()
932 {
933 $name_id = intval($_POST['name_id']);
934 if(! empty($name_id))
935 {
936 echo name_directory_switch_name_published_status($name_id);
937 exit;
938 }
939
940 echo 'Error!';
941 exit;
942 }
943
944
945 /**
946 * Print the Style rules by this plugin
947 */
948 function name_directory_print_style()
949 {
950 $style = '
951
952 <style>
953 table.name_directory_names td
954 {
955 border-bottom: 1px solid #F0F0F0;
956 }
957 .toggle_published:hover {
958 cursor: pointer;
959 }
960 </style>';
961
962 echo $style;
963 }
964
965
966 /**
967 * Print the Javascripts needed by this plugin
968 * @param string $emphasis_class
969 */
970 function name_directory_print_javascript($emphasis_class = '')
971 {
972 $js = '
973
974 <script type="text/javascript">
975 /* Save a named preference to a cookie */
976 function savePreference(name, value)
977 {
978 var expires = "";
979 document.cookie = name+"="+value+expires+"; path=/";
980 }
981
982 /* Read the named preference from cookie */
983 function readPreference(name)
984 {
985 var nameEQ = name + "=";
986 var ca = document.cookie.split(";");
987 for(var i=0;i < ca.length;i++) {
988 var c = ca[i];
989 while (c.charAt(0)==" ") c = c.substring(1,c.length);
990 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
991 }
992 return null;
993 }
994
995 jQuery(document).ready(function()
996 {
997 jQuery("#input_compact").on("click", function(e)
998 {
999 jQuery("#published_yes").attr("checked", "checked");
1000 jQuery("#add_description, #add_published, #add_submitter").hide();
1001 savePreference("wp-plugin-nd-add_form", "compact");
1002 });
1003
1004 jQuery("#input_extensive").on("click", function(e)
1005 {
1006 jQuery("#add_description, #add_published, #add_submitter").show();
1007 savePreference("wp-plugin-nd-add_form", "extensive");
1008 });
1009
1010 var pref = readPreference("wp-plugin-nd-add_form");
1011 if(pref != null)
1012 {
1013 jQuery("#input_" + pref).trigger("click");
1014 if(! window.location.hash)
1015 {
1016 jQuery("html, body").animate({scrollTop:0}, 1);
1017 }
1018 }
1019
1020 jQuery("#add_form_ajax_submit").val("name_directory_ajax_names");
1021
1022 jQuery("#add_name_ajax").on("submit", function(e)
1023 {
1024 var form_data = jQuery(this).serialize();
1025
1026 e.preventDefault();
1027
1028 jQuery("#add_button").attr("disabled", "disabled");
1029
1030 jQuery.ajax({
1031 url: "admin-ajax.php",
1032 type: "POST",
1033 data: form_data,
1034 success: function(data)
1035 {
1036 jQuery("#add_result").addClass("updated").slideDown().html(data);
1037 jQuery("#add_name_ajax input[type=text], #add_name_ajax textarea, #edit_name_id").val("");
1038 },
1039 error: function(data)
1040 {
1041 window.location.reload();
1042 },
1043 complete: function(data)
1044 {
1045 jQuery("#add_button").removeAttr("disabled");
1046 }
1047 });
1048
1049 return false;
1050 });
1051
1052 jQuery(".toggle_published").on("click", function(e)
1053 {
1054 name_id = jQuery(this).attr("data-nameid");
1055 update_ref = jQuery(this).attr("id");
1056
1057 jQuery(this).html("<div class=\'spinner\' style=\'display:block;float:left;\'></div>");
1058
1059 jQuery.ajax({
1060 url: "admin-ajax.php",
1061 type: "POST",
1062 data: { action: "name_directory_switch_name_published_status", name_id: name_id }
1063 }).done(function(status)
1064 {
1065 jQuery("#" + update_ref).html(status);
1066 });
1067 });
1068 });
1069 </script>';
1070
1071 if(! empty($emphasis_class))
1072 {
1073 $js .= "<script>jQuery('." . $emphasis_class . "').css('font-weight', 'bold');</script>";
1074 }
1075
1076 if(! empty($_GET['edit_name']))
1077 {
1078 $js .= "<script>jQuery(document).ready(function(){
1079 jQuery('#input_extensive').trigger('click');
1080 });</script>";
1081 }
1082
1083 print $js;
1084 }
1085
1086
1087 /**
1088 * Print some Javascript which helps on exporting CSV files
1089 * From: https://jsfiddle.net/mnsinger/65hqxygo/
1090 * TODO: I really should get around to use .js files :-)
1091 */
1092 function name_directory_print_export_javascript() {
1093 echo <<<JS
1094 <script type="text/javascript">
1095 function exportTableToCSV(table, filename) {
1096
1097 var rows = table.find('tr:has(td),tr:has(th)');
1098
1099 // Temporary delimiter characters unlikely to be typed by keyboard
1100 // This is to avoid accidentally splitting the actual contents
1101 tmpColDelim = String.fromCharCode(11), // vertical tab character
1102 tmpRowDelim = String.fromCharCode(0), // null character
1103
1104 // actual delimiter characters for CSV format
1105 colDelim = '","',
1106 rowDelim = '"\\r\\n"',
1107
1108 // Grab text from table into CSV formatted string
1109 csv = '"' + rows.map(function (i, row) {
1110 var row = jQuery(row), cols = row.find('td,th');
1111
1112 return cols.map(function (j, col) {
1113 var col = jQuery(col), text = col.text();
1114
1115 return text.replace(/"/g, '""'); // escape double quotes
1116
1117 }).get().join(tmpColDelim);
1118
1119 }).get().join(tmpRowDelim).split(tmpRowDelim).join(rowDelim).split(tmpColDelim).join(colDelim) + '"';
1120
1121 // Data URI
1122 csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
1123
1124 if (window.navigator.msSaveBlob) { // IE 10+
1125 window.navigator.msSaveOrOpenBlob(new Blob([csv], {type: "text/plain;charset=utf-8;"}), "csvname.csv")
1126 }
1127 else {
1128 jQuery(this).attr({ 'download': filename, 'href': csvData, 'target': '_blank' });
1129 }
1130 }
1131
1132 jQuery(document).ready(function()
1133 {
1134 // This must be an a-element hyperlink, so it can download 'data:'-uri's
1135 jQuery("#export_name_directory_names_button").on('click', function (event)
1136 {
1137 exportTableToCSV.apply(this, [jQuery('#export_names'), 'name_directory_export.csv']);
1138 });
1139 });
1140 </script>
1141 JS;
1142
1143 }