PluginProbe
Name Directory / 1.9.5
Name Directory v1.9.5
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.9.5, at admin.php

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