PluginProbe
Name Directory / trunk
Name Directory vtrunk
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 trunk, at admin.php

1,338 lines 64.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 add_action('admin_menu', 'name_directory_register_menu_entry');
6 add_action('admin_enqueue_scripts', 'name_directory_admin_add_resources');
7 add_action('wp_ajax_name_directory_ajax_names', 'name_directory_names');
8 add_action('wp_ajax_name_directory_switch_name_published_status', 'name_directory_ajax_switch_name_published_status');
9
10
11 /**
12 * Add a menu entry on options
13 */
14 function name_directory_register_menu_entry()
15 {
16 foreach( name_directory_get_capabilities() as $capability)
17 {
18 if( current_user_can( $capability ) )
19 {
20 add_menu_page(
21 __('Name Directory', 'name-directory'),
22 __('Name Directory', 'name-directory'),
23 $capability,
24 'name-directory',
25 'name_directory_options',
26 'dashicons-index-card',
27 120);
28
29 add_submenu_page(
30 'name-directory',
31 __('Add directory', 'name-directory'),
32 __('Add directory', 'name-directory'),
33 $capability,
34 'admin.php?page=name-directory&sub=new-directory');
35
36 add_submenu_page(
37 'name-directory',
38 __('Quick import into new directory', 'name-directory'),
39 __('Quick import', 'name-directory'),
40 $capability,
41 'admin.php?page=name-directory&sub=quick-import');
42
43 break;
44 }
45 }
46 }
47
48
49 /**
50 * This is a little router for the
51 * name-directory plugin
52 */
53 function name_directory_options()
54 {
55 if ( ! name_directory_is_control_allowed() )
56 {
57 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
58 }
59
60 $sub_page = '';
61 if( ! empty( $_GET['sub'] ) )
62 {
63 $sub_page = sanitize_text_field($_GET['sub']);
64 }
65
66 switch( $sub_page )
67 {
68 case 'manage-directory':
69 name_directory_names();
70 break;
71 case 'edit-directory':
72 name_directory_edit();
73 break;
74 case 'new-directory':
75 name_directory_edit('new');
76 break;
77 case 'quick-import':
78 name_directory_quick_import();
79 break;
80 case 'import':
81 name_directory_import();
82 break;
83 case 'export':
84 name_directory_export();
85 break;
86 default:
87 name_directory_show_list();
88 break;
89 }
90
91 }
92
93
94 /**
95 * Show the list of directories and all of the
96 * links to manage the directories
97 */
98 function name_directory_show_list()
99 {
100 global $wpdb;
101 global $name_directory_table_directory;
102 global $name_directory_table_directory_name;
103
104 if(! empty( $_GET['delete_dir'] ) && is_numeric( $_GET['delete_dir'] ) && check_admin_referer('name-directory-action','secnonce') )
105 {
106 $delete_dir = intval($_GET['delete_dir']);
107 $name = $wpdb->get_var(sprintf("SELECT `name` FROM %s WHERE id=%d", $name_directory_table_directory, $delete_dir));
108 $wpdb->delete($name_directory_table_directory, array('id' => $delete_dir), array('%d'));
109 $wpdb->delete($name_directory_table_directory_name, array('directory' => $delete_dir), array('%d'));
110 echo "<div class='updated'><p><strong>"
111 . sprintf(__('Name directory %s and all entries deleted', 'name-directory'), "<i>" . $name . "</i>")
112 . "</strong></p></div>";
113 }
114
115 $wp_file = admin_url('admin.php');
116 $wp_page = sanitize_text_field($_GET['page']);
117 $wp_url_path = sprintf("%s?page=%s", $wp_file, $wp_page);
118 $wp_new_url = sprintf("%s&sub=%s", $wp_url_path, 'new-directory');
119 $wp_nonce = wp_create_nonce('name-directory-action');
120
121 echo '<div class="wrap">';
122 echo "<h2>"
123 . __('Name Directory management', 'name-directory')
124 . " <a href='" . $wp_new_url . "' class='add-new-h2'>" . __('Add directory', 'name-directory') . "</a>"
125 . "</h2>";
126
127 if(! empty($_POST['mode']) && ! empty($_POST['dir_id']) && check_admin_referer( 'name_directory_dirmanagement','name_directory_adminnonce' ))
128 {
129 $wpdb->update(
130 $name_directory_table_directory,
131 array(
132 'name' => sanitize_text_field($_POST['name']),
133 'description' => sanitize_text_field($_POST['description']),
134 'show_title' => (int)$_POST['show_title'],
135 'show_description' => (int)$_POST['show_description'],
136 'show_submit_form' => (int)$_POST['show_submit_form'],
137 'show_search_form' => (int)$_POST['show_search_form'],
138 'search_in_description' => (int)$_POST['search_in_description'],
139 'search_highlight' => (int)$_POST['search_highlight'],
140 'show_submitter_name' => (int)$_POST['show_submitter_name'],
141 'show_current_num_names' => (int)$_POST['show_current_num_names'],
142 'show_index_instructions' => (int)$_POST['show_index_instructions'],
143 'show_line_between_names' => (int)$_POST['show_line_between_names'],
144 'show_character_header' => (int)$_POST['show_character_header'],
145 'show_all_names_on_index' => (int)$_POST['show_all_names_on_index'],
146 'show_all_index_letters' => (int)$_POST['show_all_index_letters'],
147 'jump_to_search_results' => (int)$_POST['jump_to_search_results'],
148 'nr_columns' => (int)$_POST['nr_columns'],
149 'nr_most_recent' => intval($_POST['nr_most_recent']),
150 'nr_words_description' => intval($_POST['nr_words_description']),
151 'email_for_submission' => sanitize_email($_POST['email_for_submission']),
152 'name_term' => sanitize_text_field($_POST['name_term']),
153 'name_term_singular' => sanitize_text_field($_POST['name_term_singular']),
154 'submitted_by_term' => sanitize_text_field($_POST['submitted_by_term']),
155 'check_submitted_names_first' => intval($_POST['check_submitted_names_first']),
156 ),
157 array('id' => intval($_POST['dir_id']))
158 );
159
160 echo "<div class='updated'><p>"
161 . sprintf(__('Directory %s updated.', 'name-directory'), "<i>" . sanitize_text_field($_POST['name']) . "</i>")
162 . "</p></div>";
163
164 unset($_GET['dir_id']);
165 }
166 elseif(! empty($_POST['mode']) && $_POST['mode'] == "new" && check_admin_referer( 'name_directory_dirmanagement','name_directory_adminnonce' ))
167 {
168 $cleaned_name = sanitize_text_field($_POST['name']);
169
170 if( empty($cleaned_name))
171 {
172 echo "<div class='error'><p><strong>" . __('Please fill in at least a (valid) name to create new name directory', 'name-directory') . "</strong></p>";
173 echo "<a href='#' onclick='javascript:history.back();'>" . __('Click here to go back and try again', 'name-directory') . "</a></div>";
174 }
175 else
176 {
177 $wpdb->insert(
178 $name_directory_table_directory,
179 array(
180 'name' => $cleaned_name,
181 'description' => sanitize_text_field($_POST['description']),
182 'show_title' => (int)$_POST['show_title'],
183 'show_description' => (int)$_POST['show_description'],
184 'show_submit_form' => (int)$_POST['show_submit_form'],
185 'show_search_form' => (int)$_POST['show_search_form'],
186 'search_in_description' => (int)$_POST['search_in_description'],
187 'search_highlight' => (int)$_POST['search_highlight'],
188 'show_submitter_name' => (int)$_POST['show_submitter_name'],
189 'show_line_between_names' => (int)$_POST['show_line_between_names'],
190 'show_character_header' => (int)$_POST['show_character_header'],
191 'show_all_names_on_index' => (int)$_POST['show_all_names_on_index'],
192 'show_all_index_letters' => (int)$_POST['show_all_index_letters'],
193 'show_current_num_names' => (int)$_POST['show_current_num_names'],
194 'show_index_instructions' => (int)$_POST['show_index_instructions'],
195 'jump_to_search_results' => (int)$_POST['jump_to_search_results'],
196 'nr_columns' => (int)$_POST['nr_columns'],
197 'nr_most_recent' => intval($_POST['nr_most_recent']),
198 'nr_words_description' => intval($_POST['nr_words_description']),
199 'email_for_submission' => sanitize_text_field($_POST['email_for_submission']),
200 'name_term' => sanitize_text_field($_POST['name_term']),
201 'name_term_singular' => sanitize_text_field($_POST['name_term_singular']),
202 'submitted_by_term' => sanitize_text_field($_POST['submitted_by_term']),
203 'check_submitted_names_first' => (int)$_POST['check_submitted_names_first'],
204 ),
205 array('%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%d')
206 );
207
208 echo "<div class='updated'><p>"
209 . sprintf(__('Directory %s created.', 'name-directory'), "<i>" . $cleaned_name . "</i>")
210 . "</p></div>";
211 }
212 }
213
214 $directories = $wpdb->get_results(sprintf("
215 SELECT nd.*
216 FROM `%s` nd
217 LEFT JOIN `%s` ndn ON nd.id = ndn.directory
218 GROUP by nd.id
219 ORDER BY MAX(ndn.id) IS NULL DESC, MAX(ndn.id) DESC", $name_directory_table_directory, $name_directory_table_directory_name));
220 $num_directories = $wpdb->num_rows;
221 $plural = ($num_directories==1)?__('name directory', 'name-directory'):__('name directories', 'name-directory');
222
223 echo "<p>"
224 . sprintf(__('You currently have %d %s.', 'name-directory'), $num_directories, $plural)
225 . "</p>";
226 ?>
227
228 <table class="wp-list-table widefat fixed table-view-list name-directory" cellspacing="0">
229 <thead><?php name_directory_render_admin_overview_table_headerfooter(); ?></thead>
230
231 <tbody>
232 <?php
233
234 $alternate = "";
235 foreach ( $directories as $directory )
236 {
237 $description = substr($directory->description, 0, 70);
238 if(strlen($description) == '70')
239 {
240 $description .= "...";
241 }
242
243 $alternate = ( $alternate == "alternate" ) ? "" : "alternate";
244
245 $entries = $wpdb->get_var(sprintf("SELECT COUNT(`id`) FROM %s WHERE directory=%d", $name_directory_table_directory_name, $directory->id));
246 $unpublished = $wpdb->get_var(sprintf("SELECT COUNT(`id`) FROM %s WHERE directory=%d AND `published` = 0", $name_directory_table_directory_name, $directory->id));
247 echo sprintf("
248 <tr class='type-page status-publish hentry " . $alternate . " iedit author-self' valign='top'>
249 <th scope='row'>&nbsp;</th>
250 <td class='post-title page-title column-title title column-title has-row-actions column-primary'>
251 <strong><a class='row-title' href='" . $wp_url_path . "&sub=manage-directory&dir=%d' title='%s'>%s</a>
252 <span>&nbsp;%s</span></strong>
253 <div class='locked-info'>&nbsp;</div>
254 <div class='row-actions'>
255 <span class='manage'><a href='" . $wp_url_path . "&sub=manage-directory&dir=%d' title='%s'>%s</a>
256 | </span><span><a href='" . $wp_url_path . "&sub=manage-directory&dir=%d&display_all_names=no#anchor_add_form' title='%s'>%s</a>
257 | </span><span><a href='" . $wp_url_path . "&sub=edit-directory&dir=%d' title='%s'>%s</a>
258 | </span><span><a href='" . $wp_url_path . "&sub=import&dir=%d' title='%s'>%s</a>
259 | </span><span><a href='" . $wp_url_path . "&sub=export&dir=%d' title='%s'>%s</a>
260 | </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>
261 | </span><span class='trash'><a class='namedirectory_confirmdelete submitdelete' href='" . $wp_url_path . "&delete_dir=%d&secnonce=%s' title=%s'>%s</a>
262 </div>
263 </td>
264 <td>
265 &nbsp; <strong title='%s'>%d</strong>
266 <br /><br />&nbsp;
267 </td>
268 <td>%d</td>
269 <td>%d</td>
270 </tr>",
271
272 $directory->id, $directory->name, $directory->name,
273 $description,
274 $directory->id, __('Add, edit and remove names', 'name-directory'), __('Manage names', 'name-directory'),
275 $directory->id, __('Go to the add-name-form on the Manage page', 'name-directory'), __('Add name', 'name-directory'),
276 $directory->id, __('Edit name, description and appearance settings', 'name-directory'), __('Settings', 'name-directory'),
277 $directory->id, __('Import entries for this directory by uploading a .csv file', 'name-directory'), __('Import', 'name-directory'),
278 $directory->id, __('Download the contents of this directory as a .csv file', 'name-directory'), __('Export', 'name-directory'),
279 $directory->id, $directory->id, __('Show the copy-paste shortcode for this directory', 'name-directory'), __('Shortcode', 'name-directory'),
280 $directory->id, $wp_nonce, __('Permanently remove this name directory', 'name-directory'), __('Delete', 'name-directory'),
281
282 __('Number of names in this directory', 'name-directory'),
283 $entries,
284 ($entries - $unpublished),
285 $unpublished
286 );
287 echo sprintf("
288 <tr id='embed_code_%s' class='name_directory_embed_code'>
289 <td>&nbsp;</td>
290 <td align='right'>%s</td>
291 <td colspan='3'>
292 <input value='[namedirectory dir=\"%s\"]' type='text' size='25' />
293 </td>
294 </tr>",
295 $directory->id,
296 __('To show your directory on your website, use the shortcode on the right.', 'name-directory') . '<br />' .
297 __('Copy the code and paste it in a post or in a page.', 'name-directory') . '<br /><small>' .
298 __('If you want to start with a specific character, like "J", use [namedirectory dir="X" start_with="j"].', 'name-directory') . '</small>',
299 $directory->id);
300 }
301 ?>
302 </tbody>
303
304 <tfoot><?php name_directory_render_admin_overview_table_headerfooter(); ?></tfoot>
305 </table>
306 <?php
307 }
308
309
310 /**
311 * A double purpose function for editing a name-directory and
312 * creating a new directory.
313 * @param string $mode
314 */
315 function name_directory_edit($mode = 'edit')
316 {
317 if( ! name_directory_is_control_allowed() )
318 {
319 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
320 }
321
322 global $wpdb;
323 global $name_directory_table_directory;
324
325 $wp_file = admin_url('admin.php');
326 $wp_page = sanitize_text_field($_GET['page']);
327 $wp_sub = sanitize_text_field($_GET['sub']);
328 $overview_url = sprintf("%s?page=%s", $wp_file, $wp_page, $wp_sub);
329 $wp_url_path = sprintf("%s?page=%s", $wp_file, $wp_page);
330
331 $directory_id = 0;
332 if(! empty($_GET['dir']))
333 {
334 $directory_id = intval($_GET['dir']);
335 }
336 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . $directory_id, ARRAY_A);
337
338 echo '<div class="wrap">';
339 if($mode == "new")
340 {
341 $table_heading = __('Create new name directory', 'name-directory');
342 $button_text = __('Create', 'name-directory');
343 echo "<h2>" . __('Create new name directory', 'name-directory') . "</h2>";
344 echo "<p>" . __('Complete the form below to create a new name directory.', 'name-directory');
345 }
346 else
347 {
348 $table_heading = __('Edit this directory', 'name-directory');
349 $button_text = __('Save Changes', 'name-directory');
350 echo "<h2>" . __('Edit name directory', 'name-directory') . "</h2>";
351 echo "<p>"
352 . sprintf(__('You are editing the name, description and settings of directory %s', 'name-directory'),
353 $directory['name']);
354 }
355 echo " <a style='float: right;' href='" . $overview_url . "'>" . __('Back to the directory overview', 'name-directory') . "</a></p>";
356 ?>
357
358 <form name="add_name" method="post" action="<?php echo $wp_url_path; ?>">
359 <table class="wp-list-table striped widefat" cellpadding="0">
360 <thead>
361 <tr>
362 <th colspan="2">
363 <?php echo $table_heading; ?>
364 <input type="hidden" name="dir_id" value="<?php echo $directory_id; ?>">
365 <input type="hidden" name="mode" value="<?php echo $mode; ?>">
366 </th>
367 </tr>
368 </thead>
369 <tbody>
370 <tr>
371 <th valign="top" width="39%"><?php echo __('Title', 'name-directory'); ?></th>
372 <td width="60%"><input type="text" name="name" autocomplete="off" value="<?php echo (! empty($directory['name']) ? esc_html($directory['name']) : ''); ?>" size="20" class="name_directory_widest"></td>
373 </tr>
374 <tr>
375 <th valign="top"><?php echo __('Description', 'name-directory'); ?><br><small><?php echo __('Just for your own administration, it does not show on the front-end', 'name-directory'); ?></small></th>
376 <td><textarea name="description" rows="5" class="name_directory_widest"><?php echo (! empty($directory['description']) ? esc_textarea($directory['description']) : ''); ?></textarea></td>
377 </tr>
378
379 <?php
380 $appearance_settings = array(
381 'show_title' => array(
382 'friendly_name' => __('Show title', 'name-directory'),
383 'description' => __('Displays the title in a h3-heading', 'name-directory'),
384 ),
385 'show_description' => array(
386 'friendly_name' => __('Show description', 'name-directory'),
387 'description' => __('This is the description of the names on the front-end', 'name-directory'),
388 ),
389 'show_line_between_names' => array(
390 'friendly_name' => __('Show line between names', 'name-directory'),
391 'description' => false,
392 ),
393 'show_character_header' => array(
394 'friendly_name' => __('Show new character heading', 'name-directory'),
395 'description' => __('Show a B-heading after all words starting with A, which will that there is a new starting letter', 'name-directory'),
396 ),
397 'show_all_names_on_index' => array(
398 'friendly_name' => __('Show all names by default', 'name-directory'),
399 'description' => __('If no, user HAS to use the index before entries are shown', 'name-directory'),
400 ),
401 );
402 name_directory_render_admin_directory_settings_section($directory, __('Appearance', 'name-directory'), $appearance_settings);
403 $appearance_settings_options = array(
404 'nr_most_recent' => array(
405 'friendly_name' => __('Show most recent names', 'name-directory'),
406 'description' => __('If No, frontend will not show \'Latest\' option.', 'name-directory'),
407 'options' => array(0 => __('No', 'name-directory'), 3 => 3, 5 => 5, 10 => 10, 25 => 25, 50 => 50, 100 => 100)
408 ),
409 'nr_words_description' => array(
410 'friendly_name' => __('Limit amount of words in description', 'name-directory'),
411 'description' => __('Display a "read-more" link on the website if the description exceeds X characters.', 'name-directory'),
412 'options' => array(0 => __('No', 'name-directory'), 10 => 10, 20 => 20, 25 => 25, 50 => 50, 100 => 100)
413 ),
414 'nr_columns' => array(
415 'friendly_name' => __('Number of columns', 'name-directory'),
416 'description' => __('The number of (vertical) columns to display the names in', 'name-directory'),
417 'options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4)
418 )
419 );
420 foreach($appearance_settings_options as $setting_name => $setting_props)
421 {
422 name_directory_render_admin_setting_options($directory, $setting_name, $setting_props['friendly_name'], $setting_props['description'], $setting_props['options']);
423 }
424
425
426 $index_settings = array(
427 'show_all_index_letters' => array(
428 'friendly_name' => __('Show all letters on index', 'name-directory'),
429 'description' => __('If no, just A B D E are shown if there are no entries starting with C', 'name-directory'),
430 ),
431 'show_current_num_names' => array(
432 'friendly_name' => __('Show current amount of names', 'name-directory'),
433 'description' => '',
434 ),
435 'show_index_instructions' => array(
436 'friendly_name' => __('Show instructions', 'name-directory'),
437 'description' => __('Show this text below the index characters', 'name-directory') . ': ' .__('Please select a letter from the index (above) to see entries', 'name-directory'),
438 ),
439 );
440 name_directory_render_admin_directory_settings_section($directory, __('Index / Character bar', 'name-directory'), $index_settings);
441
442
443 $search_options = array(
444 'show_search_form' => array(
445 'friendly_name' => __('Show search form', 'name-directory'),
446 'description' => false,
447 ),
448 'search_in_description' => array(
449 'friendly_name' => __('Search in description', 'name-directory'),
450 'description' => __('If yes, searches will be performed in the name and the description. If no, it will search in the names only', 'name-directory'),
451 ),
452 'jump_to_search_results' => array(
453 'friendly_name' => __('Jump to Name Directory when searching', 'name-directory'),
454 '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'),
455 ),
456 'search_highlight' => array(
457 'friendly_name' => __('Highlight search term', 'name-directory'),
458 'description' => __('If yes, the search term will be highlighted on the page so users can spot them easier', 'name-directory'),
459 )
460 );
461 name_directory_render_admin_directory_settings_section($directory, __('Search', 'name-directory'), $search_options);
462
463
464 $submission_settings = array(
465 'show_submit_form' => array(
466 'friendly_name' => __('Submit form', 'name-directory'),
467 'description' => __('Visitors can submit suggestions', 'name-directory'),
468 ),
469 'show_submitter_name' => array(
470 'friendly_name' => __('Submitter name', 'name-directory'),
471 'description' => __('Show the name of the submitter', 'name-directory'),
472 ),
473 'check_submitted_names_first' => array(
474 'friendly_name' => __('Check submitted names', 'name-directory'),
475 'description' => __('Whether a submitted name has to be checked/approved by an admin before it will be visible', 'name-directory'),
476 ),
477 );
478 name_directory_render_admin_directory_settings_section($directory, __('Submissions', 'name-directory'), $submission_settings);
479 ?>
480
481 <tr>
482 <th valign="top"><?php echo __('Notification e-mail address', 'name-directory'); ?><br>
483 <small><?php echo __('Which e-mail address should receive notifications? If left blank, the general WordPress admin e-mail address will be used. Keep in mind this person has to have an account on the site and the proper rights to manage Name Directory.', 'name-directory'); ?></small></th>
484 <td><input type="email" autocomplete="email" name="email_for_submission" value="<?php echo (! empty($directory['email_for_submission']) ? $directory['email_for_submission'] : ''); ?>" size="20"></td>
485 </tr>
486
487 <?php
488 name_directory_render_admin_setting_heading(__('Naming', 'name-directory'));
489 ?>
490 <tr>
491 <th valign="top"><?php echo __('Name term', 'name-directory'); ?><br>
492 <small><?php echo __('Alternative (plural) term for "names", i.e. movies', 'name-directory'); ?></small></th>
493 <td><input type="text" name="name_term" value="<?php echo (! empty($directory['name_term']) ? $directory['name_term'] : ''); ?>" size="20">
494 <br>
495 <small><?php echo __('If you provide an alternative term for name, your website will display:', 'name-directory'); ?>
496 "<i><?php echo sprintf(__('There are currently %d %s in this directory', 'name-directory'), 1337, __('movies', 'name-directory')); ?></i>"<br>
497 <?php echo __('When left blank, the word "names" will be displayed.', 'name-directory'); ?></small></td>
498 </tr>
499 <tr>
500 <th valign="top"><?php echo __('Name term (singular)', 'name-directory'); ?><br>
501 <small><?php echo __('The singular term for "names", i.e. movie', 'name-directory'); ?></small></th>
502 <td><input type="text" name="name_term_singular" value="<?php echo (! empty($directory['name_term_singular']) ? $directory['name_term_singular'] : ''); ?>" size="20"></td>
503 </tr>
504 <tr>
505 <th valign="top"><?php echo __('Submitter term', 'name-directory'); ?><br>
506 <small><?php echo __('Alternative for the submitter label, i.e. Club member', 'name-directory'); ?></small></th>
507 <td><input type="text" name="submitted_by_term" value="<?php echo (! empty($directory['submitted_by_term']) ? esc_attr($directory['submitted_by_term']) : ''); ?>" size="20">
508 <br>
509 <small><?php echo __('If you provide an alternative, your website will not display "Submitted by:", but: ', 'name-directory'); ?> "<i>Club member: Henk</i>"</small></td>
510 </tr>
511 <tr>
512 <td>
513 <?php wp_nonce_field( 'name_directory_dirmanagement','name_directory_adminnonce' ); ?>
514 </td>
515 <td>
516 <input type="submit" name="submit" class="button button-primary button-large"
517 value="<?php echo $button_text; ?>" />
518 <a class='button button-large' href='<?php echo $overview_url; ?>'>
519 <?php echo __('Cancel', 'name-directory'); ?>
520 </a>
521 </td>
522 </tr>
523 </tbody>
524 </table>
525 </form>
526
527 <?php
528
529 }
530
531
532 /**
533 * Handle the names in the name directory
534 * - Display all names
535 * - Edit names (ajax and 'oldskool' view)
536 * - Create new names
537 */
538 function name_directory_names()
539 {
540 if(! name_directory_is_control_allowed() )
541 {
542 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
543 }
544
545 global $wpdb;
546 global $name_directory_table_directory;
547 global $name_directory_table_directory_name;
548 $name_directory_settings = get_option('name_directory_general_option');
549 $show_all_names = true;
550
551 if(! empty($_GET['delete_name']) && is_numeric($_GET['delete_name']) && check_admin_referer('name-directory-action','secnonce'))
552 {
553 $name = $wpdb->get_var(sprintf("SELECT `name` FROM %s WHERE id=%d", $name_directory_table_directory_name, $_GET['delete_name']));
554 $wpdb->delete($name_directory_table_directory_name, array('id' => $_GET['delete_name']), array('%d'));
555 echo "<div class='updated'><p>"
556 . sprintf(__('Name %s deleted', 'name-directory'), "<i>" . esc_html($name) . "</i>")
557 . "</p></div>";
558 }
559 else if(! empty($_POST['name_id']))
560 {
561 if(empty($_POST['name-directory-nonce']) || ! wp_verify_nonce( $_POST['name-directory-nonce'], 'name-directory-action'))
562 {
563 echo name_directory_get_csrf_error_message($_POST['name']);
564 exit;
565 }
566
567 $description = stripslashes($_POST['description']);
568 if( ! empty( $name_directory_settings['simple_wysiwyg_editor'] ) )
569 {
570 $description = nl2br($description);
571 }
572
573 $wpdb->update(
574 $name_directory_table_directory_name,
575 array(
576 'name' => wp_kses_post(stripslashes($_POST['name'])),
577 'letter' => name_directory_get_first_char($_POST['name']),
578 'description' => wp_kses_post(stripslashes($description)),
579 'published' => (int)$_POST['published'],
580 'submitted_by' => wp_kses_post($_POST['submitted_by']),
581 ),
582 array('id' => intval($_POST['name_id']))
583 );
584
585 if($_POST['action'] == "name_directory_ajax_names")
586 {
587 $refresh_url = str_replace('edit_name=', '', $_SERVER['HTTP_REFERER']);
588 echo '<p>';
589 echo sprintf(__('Name %s updated', 'name-directory'), "<i>" . esc_html($_POST['name']) . "</i>");
590 echo '. <small><i>' . __('Will be visible when the page is refreshed.', 'name-directory') . '</i> ';
591 echo ' <a href="' . $refresh_url . '">' . __('Refresh now', 'name-directory') . '</a></small>';
592 echo '</p>';
593 exit;
594 }
595
596 echo "<div class='updated'><p>"
597 . sprintf(__('Name %s updated', 'name-directory'), "<i>" . esc_html($_POST['name']) . "</i>")
598 . "</p></div>";
599
600 unset($_GET['edit_name']);
601 }
602 else if(! empty($_POST['name']))
603 {
604 if(! wp_verify_nonce( $_POST['name-directory-nonce'], 'name-directory-action'))
605 {
606 echo name_directory_get_csrf_error_message($_POST['name']);
607 exit;
608 }
609
610 $name_exists = name_directory_name_exists_in_directory($_POST['name'], $_POST['directory']);
611 if($name_exists && $_POST['action'] == "name_directory_ajax_names")
612 {
613 echo '<p>';
614 echo sprintf(__('Name %s was already on the list, so it was not added', 'name-directory'),
615 '<i>' . esc_html($_POST['name']) . '</i>');
616 echo '</p>';
617 exit;
618 }
619
620 $description = $_POST['description'];
621 if( ! empty( $name_directory_settings['simple_wysiwyg_editor'] ) )
622 {
623 $description = nl2br($description);
624 }
625
626 $wpdb->insert(
627 $name_directory_table_directory_name,
628 array(
629 'directory' => (int)$_POST['directory'],
630 'name' => wp_kses_post($_POST['name']),
631 'letter' => name_directory_get_first_char(wp_kses_post($_POST['name'])),
632 'description' => wp_kses_post($description),
633 'published' => (int)$_POST['published'],
634 'submitted_by' => wp_kses_post($_POST['submitted_by']),
635 ),
636 array('%d', '%s', '%s', '%s', '%d', '%s')
637 );
638
639 if($_POST['action'] == "name_directory_ajax_names")
640 {
641 echo '<p>';
642 printf(__('New name %s added', 'name-directory'), '<i>' . esc_html($_POST['name']) . '</i> ');
643 echo '. <small><i>' . __('Will be visible when the page is refreshed.', 'name-directory') . '</i> ';
644 echo ' <a href="">' . __('Refresh now', 'name-directory') . '</a></small>';
645 echo '</p>';
646 exit;
647 }
648
649 echo "<div class='updated'><p><strong>"
650 . sprintf(__('New name %s added', 'name-directory'), "<i>" . esc_html($_POST['name']) . "</i> ")
651 . "</strong></p></div>";
652 }
653 else if($_SERVER['REQUEST_METHOD'] == 'POST')
654 {
655 if($_POST['action'] == "name_directory_ajax_names")
656 {
657 echo '<p>' . __('Please fill in at least a name', 'name-directory') . '</p>';
658 exit;
659 }
660
661 echo "<div class='error'><p><strong>"
662 . __('Please fill in at least a name', 'name-directory')
663 . "</strong></p></div>";
664 }
665
666 $wp_file = admin_url('admin.php');
667 $wp_page = sanitize_text_field($_GET['page']);
668 $wp_sub = sanitize_text_field($_GET['sub']);
669 $overview_url = sprintf("%s?page=%s", $wp_file, $wp_page);
670
671 if(! array_key_exists('dir', $_GET)) {
672 echo "<script>window.location.href = '" . $overview_url . "';</script>";
673 exit;
674 }
675
676 $directory_id = intval($_GET['dir']);
677
678 $wp_url_path = sprintf("%s?page=%s&sub=%s&dir=%d", $wp_file, $wp_page, $wp_sub, $directory_id);
679 $wp_ndir_path = sprintf("%s?page=%s&sub=%s&dir=%d", $wp_file, $wp_page, 'manage-directory', $directory_id);
680 $name_directory_settings = get_option('name_directory_general_option');
681 $wp_nonce = wp_create_nonce('name-directory-action');
682
683 $published_status = '0,1';
684 $emphasis_class = 's_all';
685 if(! empty($_GET['status']) && $_GET['status'] == 'published')
686 {
687 $published_status = '1';
688 $emphasis_class = 's_published';
689 }
690 else if(! empty($_GET['status']) && $_GET['status'] == 'unpublished')
691 {
692 $published_status = '0';
693 $emphasis_class = 's_unpublished';
694 }
695
696 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . $directory_id, ARRAY_A);
697
698 if( ! empty( $name_directory_settings['use_namedirectory_admin_pagination'] ) ) {
699
700 $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
701
702 $page_size = 50; // number of rows in page
703 $offset = ( $pagenum - 1 ) * $page_size;
704 $total_names = $wpdb->get_var( sprintf("SELECT COUNT(`id`) FROM %s WHERE `directory` = %d AND `published` IN (%s)",
705 $name_directory_table_directory_name, $directory_id, $published_status));
706 $num_of_pages = ceil( $total_names / $page_size );
707
708 $names = $wpdb->get_results(sprintf("SELECT * FROM %s WHERE `directory` = %d AND `published` IN (%s) ORDER BY `name` ASC LIMIT %d, %d",
709 $name_directory_table_directory_name, $directory_id, $published_status, $offset, $page_size));
710 }
711 else
712 {
713 $names = $wpdb->get_results(sprintf("SELECT * FROM %s WHERE `directory` = %d AND `published` IN (%s) ORDER BY `name` ASC",
714 $name_directory_table_directory_name, $directory_id, $published_status));
715 }
716
717 echo '<div class="wrap">';
718 echo "<h2>" . sprintf(__('Manage names for %s', 'name-directory'), $directory['name']) . "</h2>";
719 ?>
720
721 <?php
722 if(! empty($_GET['edit_name']))
723 {
724 $name = $wpdb->get_row(sprintf("SELECT * FROM `%s` WHERE `id` = %d",
725 $name_directory_table_directory_name, $_GET['edit_name']), ARRAY_A);
726 $table_heading = __('Edit a name', 'name-directory');
727 $save_button_txt = __('Save name', 'name-directory');
728 $show_all_names = false;
729 }
730 else
731 {
732 $table_heading = __('Add a new name', 'name-directory');
733 $save_button_txt = __('Add name', 'name-directory');
734 $name = array('name' => null, 'description' => '', 'submitted_by' => null);
735 }
736
737 ?>
738 <span style='float: right;'>
739 <a href='<?php echo $overview_url; ?>'><?php _e('Back to the directory overview', 'name-directory'); ?></a>
740 </span>
741
742 <p>&nbsp;</p>
743
744 <div class="hidden" id="add_result"></div>
745
746 <a name="anchor_add_form"></a>
747 <form name="add_name" id="add_name_ajax" method="post" action="<?php echo $wp_url_path; ?>">
748 <table class="wp-list-table widefat" cellpadding="0">
749 <thead>
750 <tr>
751 <th width="18%"><?php echo $table_heading; ?>
752 <input type="hidden" name="directory" value="<?php echo $directory_id; ?>">
753 <input type="hidden" name="name-directory-nonce" value="<?php echo $wp_nonce; ?>">
754 <?php
755 if(! empty($_GET['edit_name']))
756 {
757 echo '<input type="hidden" name="name_id" id="edit_name_id" value="' . intval($_GET['edit_name']) . '">';
758 }
759 ?>
760 <input type="hidden" name="action" value="0" id="add_form_ajax_submit" />
761 </th>
762 <th align="right">
763
764 <label id="input_compact" title="<?php echo __('Show the compact form, showing only the name, always published)', 'name-directory'); ?>">
765 <input type="radio" name="input_mode" />
766 <?php echo __('Quick add view', 'name-directory'); ?>
767 </label>
768 <label id="input_extensive" title="<?php echo __('Show the full form, which allows you to enter a description and submitter', 'name-directory'); ?>">
769 <input type="radio" name="input_mode" />
770 <?php echo __('Full add view', 'name-directory'); ?>
771 </label>
772
773 </th>
774 </tr>
775 </thead>
776 <tbody>
777 <tr id="add_name">
778 <td width="18%"><?php echo __('Name', 'name-directory'); ?></td>
779 <td width="82%"><input type="text" name="name" value="<?php echo esc_html($name['name']); ?>" size="20" class="name_directory_widest"></td>
780 </tr>
781 <tr id="add_description">
782 <td><?php echo __('Description', 'name-directory'); ?></td>
783 <td><?php
784 /* Determine the kind of editor to use */
785 if(empty($name_directory_settings) || empty($name_directory_settings['simple_wysiwyg_editor']))
786 {
787 echo '<textarea name="description" rows="5" class="name_directory_widest">' . esc_textarea($name['description']) . '</textarea>';
788 }
789 else
790 {
791 wp_editor($name['description'], 'description', array('textarea_rows' => 5, 'textarea_name' => 'description'));
792 }
793 ?>
794 <small><strong><?php echo __('Please be careful!', 'name-directory'); ?></strong>
795 <?php echo __('HTML markup is allowed here, but please be aware it may affect the way Name Directory or a name is displayed. Also, features like "Read more" may behave differently if you use i.e. images or tables in the description of an image.', 'name-directory'); ?></small></td>
796 </tr>
797 <tr id="add_published">
798 <td><?php echo __('Published', 'name-directory'); ?></td>
799 <td>
800 <input type="radio" name="published" id="published_yes" value="1" checked="checked">
801 <label for="published_yes"><?php echo __('Yes', 'name-directory') ?></label>
802
803 <input type="radio" name="published" id="published_no" value="0"
804 <?php
805 if(isset($name['published']) && empty($name['published']))
806 {
807 echo 'checked="checked"';
808 }?>>
809 <label for="published_no"><?php echo __('No', 'name-directory') ?></label>
810 </td>
811 </tr>
812 <tr id="add_submitter">
813 <td><?php echo __('Submitted by', 'name-directory'); ?></td>
814 <td><input type="text" name="submitted_by" value="<?php echo esc_html($name['submitted_by']); ?>" size="20" class="name_directory_widest"></td>
815 </tr>
816 <tr>
817 <td>&nbsp;</td>
818 <td>
819 <input type="submit" id="add_button" name="Submit" class="button button-primary button-large"
820 value="<?php echo $save_button_txt; ?>" />
821 </td>
822 </tr>
823 </tbody>
824 </table>
825 </form>
826
827 <?php
828
829 /* Don't show all the names when the user explicitly started the adding-mode */
830 if(! empty($_GET['display_all_names']) && sanitize_text_field($_GET['display_all_names']) == "no")
831 {
832 $show_all_names = false;
833 }
834
835 if($show_all_names)
836 {
837 ?>
838 <hr class="wp-header-end">
839 <ul class="subsubsub">
840 <li><a class='s_all' href='<?php echo $wp_url_path; ?>&status=all'><?php _e('all', 'name-directory'); ?></a> |</li>
841 <li><a class='s_published' href='<?php echo $wp_url_path; ?>&status=published'><?php _e('published', 'name-directory'); ?></a> |</li>
842 <li><a class='s_unpublished' href='<?php echo $wp_url_path; ?>&status=unpublished'><?php _e('unpublished', 'name-directory'); ?></a></li>
843 </ul>
844
845 <?php
846 $name_filter = array();
847 $num_names = 0;
848
849 $search_value = '';
850 if(! empty($_GET['s']))
851 {
852 $search_value = esc_attr($_GET['s']);
853 $name_filter['containing'] = $_GET['s'];
854
855 $names = name_directory_get_directory_names($directory, $name_filter);
856 $num_names = count($names);
857 }
858
859 $parsed_url = wp_parse_url($_SERVER['REQUEST_URI']);
860 $search_get_url = array();
861 if(! empty($parsed_url['query']))
862 {
863
864 parse_str($parsed_url['query'], $search_get_url);
865 }
866 unset($search_get_url['s']);
867
868 echo '<form method="get" role="search" action="">';
869 echo '<p class="search-box">';
870 foreach($search_get_url as $key_name=>$value)
871 {
872 if($key_name == 'name_directory_startswith')
873 {
874 continue;
875 }
876 echo "<input type='hidden' name='" . esc_attr($key_name) . "' value='" . esc_attr($value) . "' />";
877 }
878 echo "<input type='search' class='tagsdiv newtag' name='s' id='name-directory-search-input-box' value='" . $search_value . "' placeholder='" . __('Search', 'name-directory') . "...' />";
879 echo "<input type='submit' id='name-directory-search-input-button' class='button' value='" . __('Search name', 'name-directory') . "' />";
880
881
882 echo '</p>';
883 if(empty($name_filter['character']) && ! empty($search_value))
884 {
885 echo '<br><br><p class="search-box">';
886 if(empty($directory['name_term']))
887 {
888 echo sprintf(__('There are %d names in this directory containing the search term %s.', 'name-directory'), $num_names, "<em><strong>" . esc_html($search_value) . "</strong></em>");
889 }
890 else
891 {
892 echo sprintf(__('There are %d %s in this directory containing the search term %s.', 'name-directory'), $num_names, $directory['name_term'], "<em><strong>" . esc_html($search_value) . "</strong></em>");
893 }
894 echo ' <a href="' . $wp_ndir_path . '"><strong><em>' . __('Clear results', 'name-directory') . '</em></strong></a>';
895 echo "</p>";
896 }
897 else
898 {
899 echo "<br><br>";
900 }
901 echo "</form>";
902
903 ?>
904
905 <table class="wp-list-table widefat name_directory_names fixed" cellpadding="0">
906 <thead>
907 <tr>
908 <th width="18%"><?php echo __('Name', 'name-directory'); ?></th>
909 <th width="52%"><?php echo __('Description', 'name-directory'); ?></th>
910 <th width="12%"><?php echo __('Submitter', 'name-directory'); ?></th>
911 <th width="9%"><?php echo __('Published', 'name-directory'); ?></th>
912 <th width="12%"><?php echo __('Manage', 'name-directory'); ?></th>
913 <th width="5%"><?php echo __('ID', 'name-directory'); ?></th>
914 </tr>
915 </thead>
916 <tbody>
917 <?php
918 if(empty($name))
919 {
920 echo sprintf("<tr class='empty-directory'><td colspan='5'>%s</td></tr>",
921 __('Currently, there are no names in this directory..', 'name-directory'));
922 }
923
924 foreach($names as $name)
925 {
926 if(is_array($name))
927 {
928 $name = (object)$name;
929 }
930
931 echo sprintf("
932 <tr>
933 <td>%s</td><td>%s</td><td>%s</td><td><label class='name_directory_switch'><input class='toggle_published' type='checkbox' id='nid_%d' data-nameid='%d' %s><span class='name_directory_slider' title='%s'></span></label></td>
934 <td><a class='button button-primary button-small' href='" . $wp_url_path . "&edit_name=%d#anchor_add_form'>%s</a>
935 <a class='button button-small' href='" . $wp_url_path . "&delete_name=%d&secnonce=%s'>%s</a>
936 </td><td>%s</td>
937 </tr>",
938 esc_html(stripslashes($name->name)),
939 esc_html(stripslashes($name->description)),
940 sanitize_text_field(esc_html($name->submitted_by)),
941 $name->id,
942 $name->id,
943 ! empty($name->published)?' checked':'',
944 __('Toggle published status', 'name-directory'),
945 $name->id, __('Edit', 'name-directory'),
946 $name->id, $wp_nonce, __('Delete', 'name-directory'),
947 $name->id);
948 }
949 ?>
950 </tbody>
951 </table>
952
953 <p>&nbsp;</p>
954
955 <?php
956 if( ! empty( $name_directory_settings['use_namedirectory_admin_pagination'] ) )
957 {
958 echo name_directory_admin_pagination($num_of_pages, $pagenum, $total_names);
959 }
960
961 } else {
962 echo "<p>";
963 echo __('You are currently in editing or adding-mode, so not all the names are shown.', 'name-directory');
964 echo " <a class='s_all' href='" . $wp_url_path . "&status=all'>" . __('View the names', 'name-directory') . "</a>";
965 echo "</p>";
966 }
967
968 wp_add_inline_script('name_directory_admin', "jQuery('." . $emphasis_class . "').css('font-weight', 'bold');");
969
970 if(! empty($_GET['edit_name']))
971 {
972 wp_add_inline_script('name_directory_admin', "jQuery('#input_extensive').trigger('click');");
973 }
974 }
975
976
977 /**
978 * Create a directory with just a name and then go to the import page
979 */
980 function name_directory_quick_import()
981 {
982 global $wpdb;
983 global $name_directory_table_directory;
984
985 echo '<div class="wrap">';
986 echo "<h2>"
987 . __('Import names into a new directory', 'name-directory')
988 . "</h2>";
989
990 if(! empty($_POST['mode']) && $_POST['mode'] == "new" && check_admin_referer('name-directory-quick','quicknonce'))
991 {
992 $cleaned_name = sanitize_text_field($_POST['name']);
993 if(empty($cleaned_name)){
994 $cleaned_name = __('Quick import', 'name-directory');
995 }
996
997 $wpdb->insert(
998 $name_directory_table_directory,
999 array(
1000 'name' => $cleaned_name,
1001 'description' => __('Quick import', 'name-directory'),
1002 'show_title' => 1,
1003 'show_description' => 1,
1004 'show_submit_form' => 0,
1005 'check_submitted_names_first' => 0,
1006 'show_search_form' => 1,
1007 'show_submitter_name' => 0,
1008 'show_line_between_names' => 0,
1009 'show_character_header' => 0,
1010 'show_all_names_on_index' => 0,
1011 'show_all_index_letters' => 1,
1012 'jump_to_search_results' => 1,
1013 'nr_columns' => 1,
1014 'nr_most_recent' => 0,
1015 'nr_words_description' => 0,
1016 'name_term' => '',
1017 'name_term_singular' => '',
1018 'email_for_submission' => '',
1019 'show_current_num_names' => 1,
1020 'show_index_instructions' => 1,
1021 ),
1022 array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d')
1023 );
1024
1025 $import_url = sprintf("%s?page=%s&sub=%s&dir=%d", admin_url('admin.php'), sanitize_text_field($_GET['page']), 'import', $wpdb->insert_id);
1026
1027 echo "<div class='updated'><p>"
1028 . sprintf(__('Directory %s created.', 'name-directory'), "<i>" . $cleaned_name . "</i>")
1029 . "</p></div>";
1030
1031 echo __('Loading the import page...', 'name-directory');
1032 echo "<script>window.location.href = '" . $import_url . "';</script>";
1033 }
1034
1035 $wp_nonce = wp_create_nonce('name-directory-quick');
1036 ?>
1037
1038 <form name="add_name" method="post" action="<?php echo sprintf("%s?page=%s&sub=quick-import&quicknonce=%s", admin_url('admin.php'), sanitize_text_field($_GET['page']), $wp_nonce); ?>">
1039 <table class="wp-list-table widefat" cellpadding="0">
1040 <thead>
1041 <tr>
1042 <th colspan="2">
1043 <?php echo __('What would be the title of your new directory to import items into?', 'name-directory'); ?>
1044 <input type="hidden" name="mode" value="new">
1045 </th>
1046 </tr>
1047 </thead>
1048 <tbody>
1049 <tr>
1050 <td width="29%"><?php echo __('Title', 'name-directory'); ?></td>
1051 <td width="70%"><input type="text" name="name" value="" size="20" class="name_directory_widest"></td>
1052 </tr>
1053 <tr>
1054 <td>&nbsp;</td>
1055 <td>
1056 <input type="submit" name="submit" class="button button-primary button-large"
1057 value="<?php echo __('Next step', 'name-directory') ?>" />
1058 </td>
1059 </tr>
1060 </tbody>
1061 </table>
1062 </form>
1063
1064 <?php
1065 }
1066
1067
1068
1069 /**
1070 * Import names from a csv file into directory
1071 */
1072 function name_directory_import()
1073 {
1074 if(! name_directory_is_control_allowed() )
1075 {
1076 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
1077 }
1078
1079 global $wpdb;
1080 global $name_directory_table_directory;
1081 global $name_directory_table_directory_name;
1082
1083 $directory_id = intval($_GET['dir']);
1084 $import_success = false;
1085 $use_utf_import = false;
1086
1087 if($_SERVER['REQUEST_METHOD'] == 'POST')
1088 {
1089 check_admin_referer('import-upload');
1090 $file = wp_import_handle_upload();
1091
1092 if( isset($file['error']))
1093 {
1094 echo $file['error'];
1095 return;
1096 }
1097
1098 if(! empty($_POST['empty_dir_on_import']))
1099 {
1100 $wpdb->delete( $name_directory_table_directory_name, array( 'directory' => $directory_id ) );
1101 }
1102
1103 if(! empty($_POST['use_utf8_import']))
1104 {
1105 $use_utf_import = true;
1106 }
1107
1108 $csv = array_map( 'str_getcsv', file($file['file']) );
1109
1110 wp_import_cleanup($file['id']);
1111 array_shift($csv);
1112
1113 $names_error = 0;
1114 $names_imported = 0;
1115 $names_duplicate = 0;
1116 foreach($csv as $entry)
1117 {
1118 if ( ! $prepared_row = name_directory_prepared_import_row($entry, 1, $use_utf_import) )
1119 {
1120 continue;
1121 }
1122
1123 if ( name_directory_name_exists_in_directory($prepared_row['name'], $directory_id) )
1124 {
1125 $names_duplicate++;
1126 continue;
1127 }
1128
1129 $db_res = $wpdb->insert(
1130 $name_directory_table_directory_name,
1131 array(
1132 'directory' => $directory_id,
1133 'name' => sanitize_text_field($prepared_row['name']),
1134 'letter' => name_directory_get_first_char($prepared_row['name']),
1135 'description' => ! empty($prepared_row['description']) ? wp_kses_post($prepared_row['description']) : '',
1136 'published' => ! empty($prepared_row['published']) ? $prepared_row['published'] : '',
1137 'submitted_by' => ! empty($prepared_row['submitted_by']) ? sanitize_text_field($prepared_row['submitted_by']) : '',
1138 ),
1139 array('%d', '%s', '%s', '%s', '%d', '%s')
1140 );
1141
1142 if($db_res === false)
1143 {
1144 $names_error++;
1145 }
1146 else
1147 {
1148 $names_imported++;
1149 }
1150 }
1151
1152 $notice_class = 'updated';
1153 $import_success = true;
1154 $import_message = sprintf(__('Imported %d entries in this directory', 'name-directory'), $names_imported);
1155
1156 if($names_imported === 0)
1157 {
1158 $notice_class = 'error';
1159 $import_success = false;
1160 $import_message = __('Could not import any names into Name Directory', 'name-directory');
1161 }
1162
1163 if($names_error > 0)
1164 {
1165 $notice_class = 'error';
1166 $import_success = false;
1167 if($names_imported === 0)
1168 {
1169 $import_message .= "! ";
1170 }
1171 $import_message .= "<br>" . sprintf(__('There were %d names that produces errors with the WordPress database on import', 'name-directory'), $names_error);
1172 }
1173
1174 if($names_duplicate > 0)
1175 {
1176 $ignored = (count($csv)==$names_duplicate)?__('all', 'name-directory'):$names_duplicate;
1177 echo '<div class="error" style="border-left: 4px solid #ffba00;"><p>'
1178 . sprintf(__('Ignored %s names, because they were duplicate (already in the directory)', 'name-directory'), $ignored)
1179 . '</p></div>';
1180 }
1181 elseif($names_imported === 0)
1182 {
1183 $import_message .= ', ' . __('please check your .csv-file', 'name-directory');
1184 }
1185
1186 echo '<div class="' . $notice_class . '"><p>' . $import_message . '</p></div>';
1187 }
1188
1189 $wp_file = admin_url('admin.php');
1190 $wp_page = sanitize_text_field($_GET['page']);
1191 $wp_sub = sanitize_text_field($_GET['sub']);
1192 $overview_url = sprintf("%s?page=%s", $wp_file, $wp_page);
1193 $wp_url_path = sprintf("%s?page=%s&sub=%s&dir=%d", $wp_file, $wp_page, $wp_sub, $directory_id);
1194 $wp_ndir_path = sprintf("%s?page=%s&sub=%s&dir=%d", $wp_file, $wp_page, 'manage-directory', $directory_id);
1195
1196 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . $directory_id, ARRAY_A);
1197
1198 echo '<div class="wrap">';
1199 echo '<h2>' . sprintf(__('Import names for %s', 'name-directory'), $directory['name']) . '</h2>';
1200 echo '<div class="narrow name_directory_import_page"><p>';
1201 if(! $import_success && empty($names_duplicate))
1202 {
1203 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') . ' ';
1204 echo '<h4>' . __('If you saved it from Excel or OpenOffice, please ensure that:', 'name-directory') . '</h4> ';
1205 echo '<ol><li>' . __('There is a header row (this contains the column names, the first row will NOT be imported)', 'name-directory');
1206 echo '</li><li>' . __('Fields are encapsulated by double quotes', 'name-directory');
1207 echo '</li><li>' . __('Fields are comma-separated', 'name-directory');
1208 echo '</li><li>' . __('If such an option presents itself, save as UTF-8 (not ANSI)', 'name-directory');
1209 echo '</li></ol>';
1210 echo '<p>' . sprintf(__('Please check out %s first and ensure your file is formatted the same.', 'name-directory'),
1211 '<a href="http://plugins.svn.wordpress.org/name-directory/assets/name-directory-import-example.csv" target="_blank" rel="noopener noreferrer">' .
1212 __('the example import file', 'name-directory') . '</a>') . '</p>';
1213 echo '<p><em>' . __('One of the best ways to verify if your file has the right format is opening it in a plain text editor, like Windows Notepad, Geany, SublimeText or Notepad++.', 'name-directory') . '</em></p>';
1214 echo '<br>';
1215 echo '<h4>' . __('If uploading or importing fails, these are your options', 'name-directory') . '</h4><ol>';
1216 echo '<li>
1217 <a href="https://www.freefileconvert.com" target="_blank" rel="noopener noreferrer">' .
1218 __('Use an online File Convertor', 'name-directory') . '</a>
1219 </li>
1220 <li>
1221 <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>
1222 </li>
1223 <li>
1224 <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>
1225 </li>
1226 <li>';
1227 echo sprintf(__('If everything else fails, you can always ask a question at the %s.', 'name-directory'),
1228 '<a href="https://wordpress.org/support/plugin/name-directory" target="_blank" rel="noopener noreferrer">' .
1229 __('plugin support forums', 'name-directory') . '</a>') . ' ' .
1230 __('Please make sure you include all the steps you did to create the file you are trying to import.', 'name-directory');
1231 echo '</li></ol></p>';
1232 echo '<p><em>' . __('When using the upload function, script-tags are being removed for security reasons.', 'name-directory') . '</em></p>';
1233 echo '<br>';
1234
1235 if(! function_exists('str_getcsv'))
1236 {
1237 echo '<div class="error"><p>';
1238 echo __('Name Directory Import requires at least PHP 5.3, you seem to have an older version. Importing names will not work for your website.', 'name-directory');
1239 echo '</p></div>';
1240 }
1241
1242 echo '<h3>' . __('Upload your .csv-file', 'name-directory') . '</h3>';
1243 wp_import_upload_form($wp_url_path);
1244 }
1245 echo '</div></div>';
1246 echo '<a href="' . $wp_ndir_path . '">' . sprintf(__('Back to %s', 'name-directory'), '<i>' . $directory['name'] . '</i>') . '</a>';
1247 echo ' | ';
1248 echo '<a href="' . $overview_url . '">' . __('Go to Name Directory Overview', 'name-directory') . '</a>';
1249 }
1250
1251
1252 /**
1253 * Page to export names from a directory file as a .csv-file
1254 */
1255 function name_directory_export()
1256 {
1257 if(! name_directory_is_control_allowed() )
1258 {
1259 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
1260 }
1261
1262 global $wpdb;
1263 global $name_directory_table_directory;
1264
1265 $directory = $wpdb->get_row("SELECT * FROM " . $name_directory_table_directory . " WHERE `id` = " . intval($_GET['dir']), ARRAY_A);
1266
1267 $names = name_directory_get_directory_names($directory);
1268
1269 echo '<table id="export_names" class="hidden"><thead><tr><th>name</th><th>description</th><th>submitter</th></tr></thead><tbody>';
1270 foreach($names as $entry)
1271 {
1272 $description = str_replace(array("\n", "\r"), '', $entry['description']);
1273 echo '<tr><td>' . esc_html($entry['name']) . '</td><td>' . esc_html($description) . '</td><td>' . esc_html($entry['submitted_by']) . '</td></tr>';
1274 }
1275 echo '</tbody></table>';
1276
1277 /* Notify the user of possible not-working export functionality */
1278 if(stripos($_SERVER['HTTP_USER_AGENT'], 'WebKit') === false && stripos($_SERVER['HTTP_USER_AGENT'], 'Firefox') === false)
1279 {
1280 echo '<div class="notice notice-warning"><p>';
1281 echo __('Name Directory Export works best in modern browsers.', 'name-directory') . ' ';
1282 echo __('If you encounter problems (or it does not export) in browsers like Internet Explorer or older, please try another browser.', 'name-directory');
1283 echo '</div>';
1284 }
1285
1286 echo '<div class="wrap">';
1287 echo '<h2>' . sprintf(__('Export directory %s', 'name-directory'), $directory['name']) . '</h2>';
1288 echo '<div class="narrow name_directory_export"><p>';
1289 echo __('Click the Export button to download a .csv file with the contents of your directory.', 'name-directory');
1290 echo '</p><p><a href="#" id="export_name_directory_names_button" class="button button-primary">' . __('Export', 'name-directory') . '</a></p>';
1291 echo '<p><a href="' . admin_url('admin.php') . '?page=name-directory">' . __('Go to Name Directory Overview', 'name-directory') . '</a></p>';
1292 }
1293
1294
1295 /**
1296 * Proxy for the AJAX request to switch published-statusses
1297 * No params, assumes POST
1298 */
1299 function name_directory_ajax_switch_name_published_status()
1300 {
1301 check_ajax_referer('name_directory_nonce', 'ndnonce', true);
1302
1303 if(! name_directory_is_control_allowed() )
1304 {
1305 wp_die( __('You do not have sufficient permissions to access this page.', 'name-directory') );
1306 }
1307
1308 $name_id = intval($_POST['name_id']);
1309 if( ! empty($name_id) )
1310 {
1311 echo name_directory_switch_name_published_status($name_id);
1312 exit;
1313 }
1314
1315 echo 'Error!';
1316 exit;
1317 }
1318
1319
1320 /**
1321 * Add the javascript and css resources to the admin
1322 */
1323 function name_directory_admin_add_resources()
1324 {
1325 $admin_vars = array(
1326 'delete_question' => __('Are you sure you want to delete this name directory?', 'name-directory'),
1327 'empty_directory_on_import' => __('Remove all the entries of this directory before starting the import', 'name-directory'),
1328 'use_utf8_import' => __('Use special import option (use only when importing has failed before and if you are using non-latin characters)', 'name-directory'),
1329 'ajaxurl' => admin_url('admin-ajax.php'),
1330 'nd_ajax_nonce' => wp_create_nonce('name_directory_nonce')
1331 );
1332
1333 wp_register_script('name_directory_admin', plugins_url('name_directory_admin.js', __FILE__), array('jquery'), '1.1', true);
1334 wp_localize_script('name_directory_admin', 'name_directory_adminjs', $admin_vars);
1335 wp_enqueue_script('name_directory_admin');
1336
1337 wp_enqueue_style('name_directory_admin', plugins_url('name_directory_admin.css', __FILE__), '', '1.0');
1338 }