PluginProbe
Name Directory / 1.9.2
Name Directory v1.9.2
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.2, at admin.php

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