<?php
/*
 * Author: Michael Finkenberger
 * @since V2.5.24.0
 * Last change in plugin version: V2.5.24.0
 * - V2.5.24.0: Main admin functions moved from main plugin file to this separate file
 * Date: 21.03.2026
 * Tested with the latest plugin version
*/



function foodle_define_metafields() {
  global $wpdb;
  global $wp_roles;
  global $foodle_help_tooltips;
  global $foodle_warnings_removed;
  global $foodle_no_safety_query;
  global $foodle_roles_metafields;
  global $foodle_roles_deleteun;

  if ( ( ! current_user_can('manage_options') ) && ( ! $foodle_roles_metafields ) ) {
    echo '<p style="font-size:1.5em;color:darkred;"><strong>'.__('This tab is for Foodle administrators only!','foodle-for-democracy-poll').'</strong></p>';
    return;
  }

  // Help and Warnings Tooltips
  $help_meta_own_metafield_name = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('You may define your own field names to be filled in the user\'s profiles.<br>They can be used to display categorized voter information in polls<br>or even be used independently from this plugin.<br>For ease of use, own field names are NOT case sensitive!<br>(Content is being validated during input)<br><br><strong>Whether own or existing metafield:</strong><br>The metafield name will be displayed as field title in the user profile.<br>Sorting is always done in the same order as the fields are defined in this table!','foodle-for-democracy-poll').'" ' : '';
  $help_metafield_type = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('You can select from a few HTML input types for<br>the metafield, be it an existing or an own field.<br>Type \'foodle-date\' will refuse any text input but<br>select a date by the jQuery datepicker (see<br>the Foodle settings for format defintion).<br>In the \'Metafield Defaults & Sorting\' tab, the<br>\'range\' type will be shown as \'number\' type,<br>\'foodle-date\' as plain text.','foodle-for-democracy-poll').'" ' : '';
  $help_meta_regexp_def = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('With the help of Regular Expressions, fields can be filled automatically in the user\'s profile,<br>based on future entries in the first user metafield (even when the first field is filled using<br>drop-down). Therefore, this option is only available for the second and further fields.<br>However, Regular Expressions for fields will be overruled, when the same field is defined<br>as a drop-down field for user entry!<br>(Content is being validated partially during input and the RegExp prior submit)','foodle-for-democracy-poll').'" ' : '';
  $help_meta_existing_metafield_name = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Alternatively, you may choose already existing fields (maybe brought by another plugin).<br>This plugin tries to avoid unwanted or dangerous existing user-meta content.<br>Any unused Foodle field names with still existing data are identified for recovery!<br><span style=\'color:darkred;\'>However, you must take extreme special care when using such<br>existing user metafields as the results may not be foreseeable!</span><br><br><strong>Whether own or existing metafield:</strong><br>The metafield name will be displayed as field title in the user profile.<br>Sorting is always done in the same order as the fields are defined in this table!','foodle-for-democracy-poll').'" ' : '';
  $help_meta_regexp_repl = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Once a Regular Expression match is found (if defined in the lefthand input),<br>this is the replacement string. Leave empty to delete the matched string.<br>\'---\' will be entered automatically throughout this table for empty inputs.<br>(Content is being validated during input)','foodle-for-democracy-poll').'" ' : '';
  $help_meta_link_input = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('If you determine the id or name of a related existing input field in the user profile,<br>you can herewith link this existing input with your own input field.<br>The other input field will be deactivated and a remark will be displayed.<br>To mark an id, you may (or not) use # in front.<br>For a name reference, you must use &.<br>(Content is being validated during input)','foodle-for-democracy-poll').'" ' : '';
  $help_meta_category_allow = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Determines, whether the field can be activated as category column in the<br>democracy poll edit page. If not, the field will be shown but be disabled for selection.<br>It is used there to select the left hand category colum, which is as well a sorting criterium.<br>The use underneath the poll\'s answer columns in the Foodle table is not concerned here.','foodle-for-democracy-poll').'" ' : '';
  $help_meta_poll_allow = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Determines, whether the field can be activated as category per answer column in the<br>democracy poll edit page. If not, the field will be shown but be disabled for selection.<br>It is used there to select the categories shown underneath the poll\'s answer columns.<br>The definition as category column in the Foodle table is not concerned here.','foodle-for-democracy-poll').'" ' : '';
  $help_meta_description = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('You can define a description to be displayed alongside the input field in the user profile.<br>This could be used to explain the field use or the expected input format (if not defined anyway through drop-down).<br>(Content is being validated during input)','foodle-for-democracy-poll').'" ' : '';
  $help_meta_roles = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Select the related roles for this meta field.<br>This will as well determine the meta field\'s<br>visibility in the concerned user profiles.<br>Administrators will get a visualization thereof<br>in the user profiles (default = all roles).','foodle-for-democracy-poll').'" ' : '';
  $help_meta_delete_row = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Delete this metafield row.','foodle-for-democracy-poll').'"' : '';
  $help_meta_add_row = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Add a new metafield row.','foodle-for-democracy-poll').'"' : '';
  $help_dragging_rows = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('You may drag each row to where you want in order to change the row order.<br>When moving the first row or when moving another row there,<br>a new row will take that special place <strong>after saving</strong>.','foodle-for-democracy-poll').'<br><span style=\'color:DarkGoldenRod;\'>'.__('In such case, any RegExp content will be lost in that new first row.','foodle-for-democracy-poll').'</span>"' : '';
  $warning_delete_unused_metafield_entries = ( ! $foodle_warnings_removed ) ? ' foodle_ttwarning="'.__('Warning:<br>This may delete Foodle categories used for previous Foodles, if any.<br>In this case, the Foodles will most probably no longer display as before!','foodle-for-democracy-poll').'" ' : '';
  $warning_meta_existing_metafield_name = ( ! $foodle_warnings_removed ) ? ' foodle_ttwarning="'.__('Warning:<br>Be careful when working with unknown existing fields.<br>This may harm WordPress\' or other plugins\' functionality!','foodle-for-democracy-poll').'" ' : '';
  $warning_metafield_type = ( ! $foodle_warnings_removed ) ? ' foodle_ttwarning="'.__('Warning:<br>Changing field types can \'damage\' already existing<br>user profile data upon saving the user profile again.','foodle-for-democracy-poll').'" ' : '';

  $DB_PREFIX = $wpdb->prefix;
  $users = get_users(array(
    'orderby'  => 'meta_value',
    'meta_key' => 'last_name', // just a habit ;-)
    'order'    => 'ASC'
  ));
  $all_user_meta = array();
  foreach( $users as $user ) {
    $user_metas = array_keys(get_user_meta($user->ID));
    foreach( $user_metas as $user_meta ) {
      if (
        ( ! in_array ( $user_meta, array(
          'login','first_name','last_name','nickname','description','rich_editing','comment_shortcuts','role',
          'email','action','pending','admin_color','jabber','aim','yim','default_password_nag','use_ssl','show_admin_bar_front',
          'show_welcome_panel','dismissed_wp_pointers','nav_menu_recently_edited','managenav-menuscolumnshidden','facebook',
          'instagram','linkedin','myspace','pinterest','soundcloud','tumblr','twitter','wikipedia','youtube') )
        )
        && ( ! is_array(get_user_meta($user->ID,$user_meta,true) ) ) // avoid any user meta with array content
        && ( strpos($user_meta,'closedpostboxes_') === false )
        && ( strpos($user_meta,'metaboxhidden_') === false )
        && ( strpos($user_meta,'meta-box-order_') === false )
        && ( strpos($user_meta,'puresimple_') === false )
        && ( strpos($user_meta,'editprofile_') === false )
        && ( strpos($user_meta,'avatar_') === false )
        && ( strpos($user_meta,'upload_') === false )
        && ( strpos($user_meta,'users_') === false )
        && ( strpos($user_meta,'roc_') === false )
        && ( strpos($user_meta,'i4t3_') === false )
        && ( strpos($user_meta,'enable_') === false )
        && ( strpos($user_meta,'puresimple_') === false )
        && ( strpos($user_meta,'screen_layout_') === false )
        && ( strpos($user_meta,substr($DB_PREFIX,0,5)) === false )
        && ( strpos($user_meta,'username') === false )
        && ( strpos($user_meta,'user_login') === false )
        && ( strpos($user_meta,'meta') === false )
        && ( strpos($user_meta,'session') === false )
        && ( strpos($user_meta,'locale') === false )
        && ( strpos($user_meta,'syntax') === false )
        && ( strpos($user_meta,'nocaptcha') === false )
        && ( strpos($user_meta,'_wp_http') === false )
        && ( strpos($user_meta,'essential_blocks_') === false )
        && ( strpos($user_meta,'flamingo_') === false )
        && ( strpos($user_meta,'melange_') === false )
        && ( strpos($user_meta,'wpseo_') === false )
        && ( strpos($user_meta,'_yoast_') === false )
        && ( strpos($user_meta,'wpda_') === false )
        && ( strpos($user_meta,'edit_') === false )
        && ( strpos($user_meta,'ppress') === false )
        // && ( strpos($user_meta,'dbem_') === false ) // already covered
        && ( strpos($user_meta,'_jpum_') === false )
        && ( strpos($user_meta,'_pum_') === false )
        && ( strpos($user_meta,'nonce') === false )
        && ( strpos($user_meta,'crypt') === false )
        && ( strpos($user_meta,'pass') === false )
        && ( strpos($user_meta,'wsal') === false )
        && ( strpos($user_meta,'em_') === false )
        // && ( strpos($user_meta,'dem_') === false ) // already covered
        // && ( strpos($user_meta,'foodle') === false ) // will now be shown but modified to allow recovery of STILL EXISTING own field data
        && ( strpos($user_meta,'foodle_ph_') === false ) // MF :)
        && ( strpos($user_meta,'mfdwc') === false ) // MF :)
      ) $all_user_meta[$user_meta] = true; // each user meta key exists here only once
    }
  }
  $all_user_meta = array_keys($all_user_meta);
  sort($all_user_meta); // a sorted list of all existing user meta keys except the foodle ones and except the database and WP related ones

  echo '<div style="max-width:1250px"><form action="'.admin_url( 'admin.php?page=foodle-admin-page&tab=define-metafields' ).'" method="post">';
  $foodle_reg_ex = __('Regular Expression','foodle-for-democracy-poll');
  ?>
    <figure class="foodle-block-table">
      <table unselectable="on" class="foodle-meta-table unselectable">
        <thead>
          <tr class="foodle-header-row">
            <th style="padding:20px 0px 20px 0px; text-align:center;"><div style="opacity:0.4; margin:auto; width:18px; height:16px; background-size:contain; background-image: url('<?php echo plugin_dir_url(__FILE__).'img/move_sm.png'; ?>');" <?php echo $help_dragging_rows ?>></div></th>
            <th style="padding:20px 0px 20px 0px; text-align:center;"><?php echo __('Use Own Metafields','foodle-for-democracy-poll') ?><br>&nbsp;<hr style="height:1px;border-width:0;color:grey;background-color:grey;margin: 10px 10% 10px 10%;" /><?php echo $foodle_reg_ex ?></th>
            <th style="padding:20px 0px 20px 0px; text-align:center;"><?php echo __('Use Existing Metafields<br>(filtered)','foodle-for-democracy-poll') ?><hr style="height:1px;border-width:0;color:grey;background-color:grey;margin: 10px 10% 10px 10%;" /><?php echo __('Replacement String','foodle-for-democracy-poll') ?></th>
            <th style="padding:20px 0px 20px 0px; text-align:center;"><?php echo __('Enable/Disable Category Selections','foodle-for-democracy-poll') ?></th>
            <th style="padding:20px 0px 20px 0px; text-align:center;"><?php echo __('Add Input Description','foodle-for-democracy-poll') ?><hr style="height:1px;border-width:0;color:grey;background-color:grey;margin: 10px 10% 10px 10%;" /><?php echo __('Roles concerned for the meta field','foodle-for-democracy-poll') ?></th>
            <th style="padding:20px 0px 20px 0px; text-align:center;"></th> <!-- the column with the buttons -->
          </tr>
          <tr class="foodle-meta-row-above">
            <td colspan="6" style="text-align:center;">
              <?php echo __('This first row is the basis for any other fields\' user-defined content fed by regular expressions','foodle-for-democracy-poll').'!'; ?>
            </td>
          </tr>
        </thead>
        <tbody id="foodle-meta-sortable" style="overflow-y:auto;">
  <?php
  if ( get_option('foodle_meta_fields') ) {
    $foodle_meta_fields = get_option('foodle_meta_fields');
  } else {
    $foodle_meta_fields[''] = array('', '', 'yes');
  }
  $foodle_meta_fieldslugs = array();
  foreach($foodle_meta_fields as $foodle_meta_fieldname => $foodle_meta_field_content) {
    $foodle_meta_fieldslugs[] = foodle_fieldname_to_meta_name($foodle_meta_fieldname);
  }
  //  if ( get_option('foodle_regex_main') ) {
  //    $foodle_regex_main = get_option('foodle_meta_fields');
  //  } else {
  //    $foodle_regex_main['---'] = array('','');
  //  }

  $foodle_number_of_rows = count($foodle_meta_fields);
  $foodle_meta_count = -1; // in order to have the first iteration see 0
  foreach ( $foodle_meta_fields as $foodle_meta_field_raw => $foodle_meta_data ) {
    $foodle_meta_data []= ''; // avoid an error for existing users as there's a new field (version 1.8.4.0)
    $foodle_meta_data []= ''; // avoid an error for existing users as there's a new field (version 1.8.6.0)
    $foodle_meta_data []= ''; // avoid an error for existing users as there's a new field (version 2.5.19.2)
    $foodle_meta_data []= ''; // just in case...
    $foodle_meta_count += 1;
    if ( strpos($foodle_meta_field_raw, '••') === 0 ) {
      $foodle_meta_field = '---';
      $foodle_own_field_style='opacity:0.4; cursor:not-allowed;';
      $foodle_own_field_attr='readonly="readonly"';
      $foodle_existing_field = str_replace('••', '', $foodle_meta_field_raw);
      $foodle_link_meta_field_style='opacity:1.0;';
      $foodle_link_meta_field_attr='';
    } else {
      $foodle_meta_field = $foodle_meta_field_raw;
      $foodle_own_field_style='opacity:1.0;';
      $foodle_own_field_attr='';
      $foodle_existing_field = 'own-field';
      $foodle_link_meta_field_style='opacity:0.4; cursor:not-allowed;';
      $foodle_link_meta_field_attr='readonly="readonly"';
    }
    $foodle_link_meta_field = ( $foodle_meta_data[1] == '' ) ? '---' : $foodle_meta_data[1];
    $foodle_description = ( $foodle_meta_data[0] == '' ) ? '---' : $foodle_meta_data[0];
    $poll_use_yes_checked = ( $foodle_meta_data[2] != 'no' ) ? 'checked="checked"' : ''; // avoid an undefined condition as a new field
    $poll_use_no_checked = ( $foodle_meta_data[2] == 'no' ) ? 'checked="checked"' : '';
    $category_use_yes_checked = ( $foodle_meta_data[3] != 'no' ) ? 'checked="checked"' : ''; // avoid an undefined condition as a new field
    $category_use_no_checked = ( $foodle_meta_data[3] == 'no' ) ? 'checked="checked"' : '';
    $foodle_field_type_stored = $foodle_meta_data[4];
    $foodle_roles_for_meta_field = ( $foodle_meta_data[5] == '' ) ?  array('all') : $foodle_meta_data[5] ;
    $column_category_checked = '';
    $required = ''; // no requirement at this time, maybe in a later plugin version
    if ($foodle_meta_count == 0) {
      $foodle_del_button_style = 'opacity:0.1; cursor:not-allowed;';
      $foodle_del_button_attr = 'disabled="disabled"';
      if ( $foodle_number_of_rows == 1 ) { // just one row existing
        $foodle_add_button_style = 'opacity:1.0; cursor:pointer;';
        $foodle_add_button_attr = '';
      } else {
        $foodle_add_button_style = 'opacity:0.1; cursor:not-allowed;';
        $foodle_add_button_attr = 'disabled="disabled"';
      }
      $foodle_regexp_field = '---';
      $foodle_regexp_style = 'opacity:0.2; cursor:not-allowed;';
      $foodle_regexp_attr = 'readonly="readonly"';
      $foodle_regrep_field = '---';
      $foodle_regrep_style = 'opacity:0.2; cursor:not-allowed;';
      $foodle_regrep_attr = 'readonly="readonly"';
    } else {
      $foodle_del_button_style = 'opacity:1.0; cursor:pointer;';
      $foodle_del_button_attr = '';
      if ( $foodle_meta_count + 1 == $foodle_number_of_rows ) { // the last row
        $foodle_add_button_style = 'opacity:1.0; cursor:pointer;';
        $foodle_add_button_attr = '';
      } else {
        $foodle_add_button_style = 'opacity:0.1; cursor:not-allowed;';
        $foodle_add_button_attr = 'disabled="disabled"';
      }
      $foodle_regexp_style = 'opacity:1.0;';
      $foodle_regexp_attr = '';
      $foodle_regrep_style = 'opacity:1.0;';
      $foodle_regrep_attr = '';
      if ( get_option('foodle_regex_main') ) {
        $foodle_regexp_meta_field = str_replace('', '', $foodle_meta_field_raw); // optional later str_replace
        if ( isset(get_option('foodle_regex_main')[$foodle_regexp_meta_field]) ) {
          $foodle_regexp_field = get_option('foodle_regex_main')[$foodle_regexp_meta_field][0];
          $foodle_regexp_field = ( $foodle_regexp_field == '' ) ? '---' : $foodle_regexp_field;
          $foodle_regrep_field = get_option('foodle_regex_main')[$foodle_regexp_meta_field][1];
          $foodle_regrep_field = ( $foodle_regrep_field == '' ) ? '---' : $foodle_regrep_field;
        } else {
          $foodle_regexp_field = '---';
          $foodle_regrep_field = '---';
        }
      } else {
      $foodle_regexp_field = '---';
      $foodle_regrep_field = '---';
      }
    }
    $not_used = __('not used','foodle-for-democracy-poll');

    $foodle_orphan_metafields = false; // Initialize: no orphan metafields

    $foodle_field_types = array('foodle-date','date','time','datetime-local','number','tel','email','url','range');

    if ($foodle_meta_count == 0 ): ?>
          <tr id="foodle-meta-row-<?php echo $foodle_meta_count ?>" class="foodle-meta-row-above">
    <?php
    else:
    ?>
          <tr id="foodle-meta-row-<?php echo $foodle_meta_count ?>" class="foodle-meta-row">
    <?php
    endif
    ?>
            <td style="padding: 15px 10px 15px 10px;">
              <div class="foodle-meta-handle" style="cursor:move; margin:auto; width:18px; height:16px; background-size:contain; background-image: url('<?php echo plugin_dir_url(__FILE__).'img/move_sm.png'; ?>');" <?php echo $help_dragging_rows ?>></div>
            </td>
            <td style="padding: 15px 10px 15px 10px;">
              <label <?php echo $help_meta_own_metafield_name ?>><span style="font-weight:bold;"><?php echo __('Define own field name','foodle-for-democracy-poll').':' ?></span><br>
                <input type="text" style="font-size:0.95em;<?php echo $foodle_own_field_style ?>" id="own-foodle-sel-<?php echo $foodle_meta_count ?>" name="own_meta_field[]" value="<?php echo $foodle_meta_field; ?>" placeholder="<?php echo __('Field key and name','foodle-for-democracy-poll') ?>" onkeyup="foodle_check_metafield_name(this);" <?php echo $required." ".$foodle_own_field_attr ?>>
              </label><br><br>
              <label <?php echo $warning_metafield_type.$help_metafield_type ?>><span style="font-weight:bold;"><?php echo __('Define field type','foodle-for-democracy-poll').':' ?></span><br>
              <select style="font-size:0.95em;" id="foodle-field-type-<?php echo $foodle_meta_count ?>"  name="meta_field_type[]" size="1">
                  <option style="font-size:0.95em;" value="text" selected="selected">text</option>
                  <?php foreach ( $foodle_field_types as $foodle_field_type ) { ?>
                    <option style="font-size:0.95em;" value="<?php echo $foodle_field_type;?>" <?php if ( $foodle_field_type == $foodle_field_type_stored ) echo 'selected="selected"';?>><?php echo $foodle_field_type; ?></option>
                  <?php } ?>
                </select>
              </label>
              <hr style="height:1px;border-width:0;color:grey;background-color:grey;margin: 41px 0px 20px 0px;" />
              <label <?php echo $help_meta_regexp_def ?>style="color:DarkGoldenRod;"><span style="font-weight:bold;"><?php echo __('Optional RegExp','foodle-for-democracy-poll').'...' ?></span><br>
                <input type="text" style="font-size:0.95em; border-color:DarkGoldenRod; <?php echo $foodle_regexp_style ?>" id="regexp-foodle-sel-<?php echo $foodle_meta_count ?>" name="regexp_field[]" value="<?php echo $foodle_regexp_field ?>" placeholder="<?php echo $foodle_reg_ex ?>" onkeyup="foodle_check_regexp_input(this);" onfocusin="if ( ( this.value.match(/^[/]/) == null ) && ( ! $(this).prop('readonly') ) ) this.value='';" onfocusout="foodle_check_regexp_static($(this)); foodle_check_regexp($(this));" autocomplete="off" <?php echo $foodle_regexp_attr ?>>
                <br>&nbsp;&nbsp;&nbsp;--- = <?php echo $not_used; ?>
              </label>
            </td>
            <td style="padding: 15px 10px 15px 10px;">
              <label <?php echo $warning_meta_existing_metafield_name.$help_meta_existing_metafield_name; ?>><span style="font-weight:bold;"><?php echo __('Select existing field name','foodle-for-democracy-poll').':' ?></span><br>
                <select style="font-size:0.95em;" id="exist-foodle-sel-<?php echo $foodle_meta_count ?>"  name="existing_meta_field[]" size="1" onchange="foodle_manage_meta($(this));">
                  <option style="font-size:0.95em;" value="own-field" selected="selected"><?php echo __('Use own field name','foodle-for-democracy-poll')?></option>
                  <?php foreach ( $all_user_meta as $user_meta_key ) {
                    $foodle_meta_value = $user_meta_key;
                    $foodle_meta_view = $user_meta_key;
                    $foodle_meta_selected = ( $user_meta_key == $foodle_existing_field ) ? ' selected="selected" ' : '' ;
                    $foodle_disabled = false;
                    if ( strpos($user_meta_key,'foodle-field-') !== false ) {
                        if ( ! in_array($user_meta_key,$foodle_meta_fieldslugs) ) {
                            $foodle_orphan_metafields = true; // Found orphan metafield
                            $foodle_meta_field_slug = substr($user_meta_key,13);
                            $foodle_meta_field_name = str_replace('_',' ',str_replace('€','.',$foodle_meta_field_slug));
                            $foodle_meta_view = __('RECOVER','foodle-for-democracy-poll').': \''.$foodle_meta_field_name.'\' ('.__('as there\'s still data there','foodle-for-democracy-poll').'!)';
                            $foodle_disabled = false;
                        } else continue;
                    } ?>
                    <option styl_e="font-size:0.95em;" value="<?php echo $foodle_meta_value;?>" <?php echo $foodle_meta_selected; if ( $foodle_disabled ) echo ' disabled="disabled" ';?>><?php echo $foodle_meta_view; ?></option>
                  <?php } ?>
                </select>
              </label><br><br>
              <label <?php echo $help_meta_link_input ?>><span style="font-weight:bold;"><?php echo __('Link existing id/#id/&name','foodle-for-democracy-poll').':' ?></span><br>
                <input type="text" style="font-size:0.95em; <?php echo $foodle_link_meta_field_style ?>" id="link-foodle-sel-<?php echo $foodle_meta_count ?>"   name="link_meta_field[]" value="<?php echo $foodle_link_meta_field; ?>" placeholder="<?php echo __('Original input id or name','foodle-for-democracy-poll') ?>" onkeyup="foodle_check_link_input(this);" onfocusin="if ( ( this.value.match(/^[#&a-zA-Z]/) == null ) && ( ! $(this).prop('readonly') ) ) this.value='';" onfocusout="foodle_check_link_content($(this));" <?php echo $foodle_link_meta_field_attr ?>>
                <br>&nbsp;&nbsp;&nbsp;--- = <?php echo $not_used; ?>
              </label>
              <hr style="height:1px;border-width:0;color:grey;background-color:grey;margin: 20px 0px 20px 0px;" />
              <label <?php echo $help_meta_regexp_repl ?>style="color:DarkGoldenRod;"><span style="font-weight:bold;"><?php echo __('... and replacement','foodle-for-democracy-poll').':' ?></span><br>
                <input type="text" style="font-size:0.95em;
                  border-color:DarkGoldenRod; <?php echo $foodle_regrep_style ?>" id="regrep-foodle-sel-<?php echo $foodle_meta_count ?>" name="regrep_field[]" value="<?php echo $foodle_regrep_field ?>" placeholder="<?php echo __('Match replacement','foodle-for-democracy-poll') ?>" onkeyup="foodle_check_text_input(this);" onfocusin="if ( ( this.value.match(/^[a-zA-Z]/) == null ) && ( ! $(this).prop('readonly') ) ) this.value='';" onfocusout="foodle_check_content($(this));" <?php echo $foodle_regrep_attr ?>>
                <br>&nbsp;&nbsp;&nbsp;--- = <?php echo __('eliminate match','foodle-for-democracy-poll'); ?>
              </label>
            </td>
            <td style="padding: 15px 10px 15px 10px;min-width:140px;">
              <label <?php echo $help_meta_category_allow ?>><span style="font-weight:bold;"><?php echo __('Allow selection in democracy poll edit as Foodle table left hand category column','foodle-for-democracy-poll').':' ?></span><br>
                <label><input type="radio" style="margin: 0px 4px 0px 20px; font-size:0.95em;" name="foodle_category_use<?php echo $foodle_meta_count ?>" <?php echo $category_use_yes_checked ?> value="yes" /><?php echo __('yes','foodle-for-democracy-poll') ?></label><label><input type="radio" style="margin:0px 4px 0px 20px; font-size:0.95em;" name="foodle_category_use<?php echo $foodle_meta_count ?>" <?php echo $category_use_no_checked ?> value="no" /><?php echo __('no','foodle-for-democracy-poll') ?></label>
              </label>
              <hr style="height:1px;border-width:0;color:silver;background-color:silver;margin: 15px 0px 15px 0px;" />
              <label <?php echo $help_meta_poll_allow ?>><span style="font-weight:bold;"><?php echo __('Allow selection in democracy poll edit as Foodle table category per answer column','foodle-for-democracy-poll').':' ?></span><br>
                <label><input type="radio" style="margin: 0px 4px 0px 20px; font-size:0.95em;" name="foodle_poll_use<?php echo $foodle_meta_count ?>" <?php echo $poll_use_yes_checked ?> value="yes" /><?php echo __('yes','foodle-for-democracy-poll') ?></label><label><input type="radio" style="margin:0px 4px 0px 20px; font-size:0.95em;" name="foodle_poll_use<?php echo $foodle_meta_count ?>" <?php echo $poll_use_no_checked ?> value="no" /><?php echo __('no','foodle-for-democracy-poll') ?></label>
              </label>
            </td>
            <td style="padding: 15px 10px 15px 10px;">
              <label <?php echo $help_meta_description ?>><span style="font-weight:bold;"><?php echo __('Input description','foodle-for-democracy-poll').':' ?></span><br>
                <textarea style="font-size:0.95em;" id="descr-foodle-sel-<?php echo $foodle_meta_count ?>" name="description[]" placeholder="<?php echo __('Description displayed under the metafield','foodle-for-democracy-poll') ?>" rows="4" cols="30" onkeyup="foodle_check_text_input(this);" onfocusin="if ( this.value.match(/^[a-zA-Z]/) == null ) this.value='';" onfocusout="foodle_check_content($(this));"><?php echo $foodle_description ?></textarea>
                <br>&nbsp;&nbsp;&nbsp;--- = <?php echo __('no description','foodle-for-democracy-poll'); ?>
              </label>
              <hr style="height:1px;border-width:0;color:silver;background-color:silver;margin: 15px 0px 15px 0px;" />
              <label <?php echo $help_meta_roles ?>><span style="font-weight:bold;"><?php echo __('Related roles','foodle-for-democracy-poll').':' ?></span><br>
                <select style="font-size:0.8em;" id="foodle-roles-for-meta-field-sel-<?php echo $foodle_meta_count ?>" name="foodle_roles_for_meta_field[<?php echo $foodle_meta_count ?>][]" size="9" multiple>
                <option value="all" <?php if ( in_array('all',$foodle_roles_for_meta_field) ) echo 'selected="selected"'; ?>><?php echo __('All','foodle-for-democracy-poll'); ?></option>
                  <?php foreach ( $wp_roles->role_names as $foodle_role_key=>$foodle_role_value) {
                    //if ( $foodle_role_key == 'administrator' ) continue; // This could be used to leave administrators out of the game, however, currently not!
                  $foodle_role_selected = ( in_array($foodle_role_key,$foodle_roles_for_meta_field) ) ? 'selected="selected"' : "";
                  //$foodle_role_selected = 'selected="selected"';
                  echo '<option value="'.$foodle_role_key.'" '.$foodle_role_selected.'>'._x($foodle_role_value,'User role').'</option>';
                  }
                  // Add a hidden field to identify the roles selection, even if no role was selected. A bit redundant, as no entry in the option equals an empty array entry.
                  ?>
                  <input type="hidden" name="foodle_roles_for_meta_field[]" value="*">
                </select>
              </label>
            </td>
            <td style="padding: 15px 10px 15px 10px;">
              <div style="display:block; padding: 10px;">
                <input <?php echo $help_meta_delete_row ?>type="button" style="padding:0px; border: 0px solid darkred; color:white; font-size:1em; width:25px; height:25px;<?php echo $foodle_del_button_style; ?> background-color:darkred; border-radius:25px;" id="del-foodle-sel-<?php echo $foodle_meta_count ?>" value="✘" onclick="foodle_remove_meta_row($(this));" <?php echo $foodle_del_button_attr; ?>><br><br>
                <input type="hidden" name="foodle_meta_line[]" value="<?php echo $foodle_meta_count ?>">
                <input <?php echo $help_meta_add_row ?>type="button" style="padding:5px 8px; border: 0px solid darkgreen; color:white; font-size:1em; width:25px; height:25px;<?php echo $foodle_add_button_style; ?> background-color:darkgreen; border-radius:25px;" id="add-foodle-sel-<?php echo $foodle_meta_count ?>" value="✛" onclick="foodle_add_meta_row($(this));" <?php echo $foodle_add_button_attr; ?>>
              </div>
            </td>
          </tr>
  <?php
  }
  ?>
        </tbody>
      </table>
    </figure>

    <script type="text/javascript">
      var $ = jQuery;
      var foodle_regexp_good = true;
      var foodle_regexp_error = '';
      var foodle_regexp_this;
      var foodle_scroll_store;
      var foodle_width_allow_unset = true;
      var plugin_url = '<?php echo plugin_dir_url(__FILE__); ?>';

      function foodle_activate_event_handler_to_store_meta_scrolltop() {
        $('.foodle-meta-handle').on('mousedown', function() {
          foodle_scroll_store = $('html').scrollTop();
        });
      }

      function foodle_activate_event_handler_to_set_meta_column_widths() {
        $('.foodle-meta-handle').on('mousedown', function() {
          $(".foodle-meta-table").css("width", $(".foodle-meta-table").outerWidth());
          $(".foodle-meta-table th, .foodle-meta-table td").each( function() {
            $(this).css("width", $(this).width() + 0.001);
          });
        });
        $('.foodle-meta-handle').on('mouseup', function() {
          if ( foodle_width_allow_unset )
            $(".foodle-meta-table, .foodle-meta-table th, .foodle-meta-table td").each( function() {
              $(this).css("width", '');
          });
        });
      }

      function foodle_unset_meta_column_widths() {
        $(".foodle-meta-table, .foodle-meta-table th, .foodle-meta-table td").each( function() {
          $(this).css("width", '');
        });
      }

      $('#foodle-meta-sortable').sortable({
        opacity: 0.75,
        revert: true,
        forcePlaceholderSize: true,
        forceHelperSize: true,
        handle: '.foodle-meta-handle',
        //axis: 'y',
        pull: 'clone',
        tolerance: 'pointer',
        delay: 150,
        scroll: true,
        scrollSensitivity: 10,
        scrollSpeed: 40,
        create: function(e, ui){
          foodle_activate_event_handler_to_store_meta_scrolltop();
          foodle_activate_event_handler_to_set_meta_column_widths();
        },
        start: function(e, ui){
          ui.placeholder.css('visibility', 'visible');
          ui.placeholder.children().css({'background-color':'AliceBlue','background-image':'url("' + plugin_url + 'img/foodle.png")','background-repeat':'no-repeat','background-position':'center','opacity':'0'}).fadeTo(660,0.4);
          ui.placeholder.height(ui.item.outerHeight());
          foodle_width_allow_unset = false;
        },
        activate: function(e, ui){
          $('html').scrollTop(foodle_scroll_store);
        },
        stop: function(e, ui){
          foodle_unset_meta_column_widths();
          foodle_width_allow_unset = true;
        }
      });



      function foodle_add_meta_row(foodle_this) {
        foodle_meta_last_row = foodle_this.closest('tr').prop('outerHTML');

        idtitle = 'foodle-meta-row-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        newcount = parseInt(foodle_meta_last_row.substring( posstart + idlength, posend)) + 1; // this value is valid for all further fields
        new_val = idtitle + newcount;
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'foodle-field-type-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'own-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'exist-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'link-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'descr-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'add-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'del-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'regexp-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'regrep-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        idtitle = 'foodle_category_use'; // is a name, not an ID, but variable names are unchanged for convenience
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        const regex1 = new RegExp(old_val,'g'); // replace all occurrences
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(' checked="checked"', '');
        foodle_meta_last_row = foodle_meta_last_row.replace(' checked="checked"', ''); // 'foodle_poll_use' as well
        //foodle_meta_last_row = foodle_meta_last_row.replace(' checked="checked"', '');
        foodle_meta_last_row = foodle_meta_last_row.replace(regex1, new_val); // all occurrences
        foodle_meta_last_row = foodle_meta_last_row.replace(new_val, new_val + '" checked="checked'); // just the first

        idtitle = 'foodle_poll_use'; // is a name, not an ID, but variable names are unchanged for convenience
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        const regex2 = new RegExp(old_val,'g'); // replace all occurrences
        new_val = idtitle + newcount; // newcount defined before
        // "checked" was already removed above :)
        foodle_meta_last_row = foodle_meta_last_row.replace(regex2, new_val); // all occurrences
        foodle_meta_last_row = foodle_meta_last_row.replace(new_val, new_val + '" checked="checked'); // just the first

        idtitle = 'foodle_meta_line[]" value="';
        idlength = idtitle.length; // characters
        posstart = foodle_meta_last_row.indexOf(idtitle);
        posend = foodle_meta_last_row.indexOf('"', posstart);
        old_val = foodle_meta_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_meta_last_row = foodle_meta_last_row.replace(old_val, new_val);

        $('.foodle-meta-row-above').addClass('foodle-meta-row');
        $('.foodle-meta-row').last().after(foodle_meta_last_row);
        $('.foodle-meta-row-above').removeClass('foodle-meta-row');

        oldcount = newcount - 1;
        $('#foodle-meta-row-' + newcount + ' td').css('background-color','#dddddd').css('border-top','1px solid grey');

        $('#own-foodle-sel-' + newcount).prop('readonly',false).css('cursor','').css('opacity','1.0').val('');
        $('#foodle-field-type-' + newcount).val('text');
        $('#exist-foodle-sel-' + newcount).val('own-field');
        $('#link-foodle-sel-' + newcount).prop('readonly',true).css('cursor','not-allowed').css('opacity','0.4').val('---');
        $('#descr-foodle-sel-' + newcount).val('---');
        $('#regexp-foodle-sel-' + newcount).prop('readonly',false).css('cursor','').css('opacity','1.0').val('---');
        $('#regrep-foodle-sel-' + newcount).prop('readonly',false).css('cursor','').css('opacity','1.0').val('---');
        $('#add-foodle-sel-' + oldcount).prop('readonly',true).css('cursor','not-allowed').css('opacity','0.1');
        $('#del-foodle-sel-' + newcount).prop('disabled',false).css('cursor','pointer').css('opacity','1.0');
        foodle_activate_tooltips(); // to catch the new row as well
        foodle_activate_event_handler_to_store_meta_scrolltop(); // to catch the new row as well
        foodle_activate_event_handler_to_set_meta_column_widths(); // to catch the new row as well
      }



      function foodle_remove_meta_row(foodle_this) {
        foodle_this.closest('tr').remove();
        $('.foodle-meta-row-above').addClass('foodle-meta-row');
        $('.foodle-meta-row').last().children().last().children().first().children().last().prop('disabled',false).css('opacity','1.0').css('cursor','pointer');
        $('.foodle-meta-row-above').removeClass('foodle-meta-row');
      }



      function foodle_manage_meta(foodle_this) {
        own_input_id = '#own' + foodle_this.attr('id').substring(5);
        link_input_id = '#link' + foodle_this.attr('id').substring(5);
        if ( foodle_this.val() == 'own-field' ) {
          $(own_input_id).prop('readonly',false).css('cursor','').val('').css('opacity','1.0');
          $(link_input_id).prop('readonly',true).css('cursor','not-allowed').val('---').css('opacity','0.4');
        } else {
          $(own_input_id).prop('readonly',true).css('cursor','not-allowed').val('---').css('opacity','0.4');
          $(link_input_id).prop('readonly',false).css('cursor','').val('---').css('opacity','1.0');
        }
      }



      function foodle_check_metafield_name(foodle_this) {
        if ( ( ! ( ( event.shiftKey ) && ( ( event.keyCode == 37 ) || ( event.keyCode == 38 ) || ( event.keyCode == 39 ) || ( event.keyCode == 40 ) ) ) ) && ( event.keyCode != 16 ) && ( event.keyCode != 17 ) && ( event.keyCode != 18 ) ) {
          foodle_save_curpos = foodle_this.selectionStart;
          foodle_save_selend = foodle_this.selectionEnd;
          foodle_save_value = foodle_this.value;

          // this is the very check - the rest is just there to smoothen the user input
          foodle_this.value=foodle_this.value.replace(/^[^a-zA-Z]/, '').replace(/[^a-zA-Z0-9.\-_ ]/g, '').replace(/\s+/g, ' ');

          foodle_len_diff = foodle_this.value.length - foodle_save_value.length;
          if ( foodle_this.value.length > foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos - 1;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          }
          if ( foodle_this.value.length < foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos + foodle_len_diff;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          } else {
            foodle_this.selectionStart = foodle_save_curpos - foodle_len_diff;
            foodle_this.selectionEnd = foodle_save_selend - foodle_len_diff;
          }
        }
      }



      function foodle_check_content(foodle_this) {
        $v = foodle_this.val();
        if ( $v.match(/^[a-zA-Z]/) == null ) { // must start with a letter
          foodle_this.val('---');
        }
      }



      function foodle_check_link_content(foodle_this) {
        $v = foodle_this.val();
        if ( $v.match(/^[#&a-zA-Z]/) == null ) { // must start with a letter or # or &
          foodle_this.val('---');
        }
      }



      function foodle_check_text_input(foodle_this) {
        if ( ( ! ( ( event.shiftKey ) && ( ( event.keyCode == 37 ) || ( event.keyCode == 38 ) || ( event.keyCode == 39 ) || ( event.keyCode == 40 ) ) ) ) && ( event.keyCode != 16 ) && ( event.keyCode != 17 ) && ( event.keyCode != 18 ) ) {
          foodle_save_curpos = foodle_this.selectionStart;
          foodle_save_selend = foodle_this.selectionEnd;
          foodle_save_value = foodle_this.value;

          // this is the very check - the rest is just there to smoothen the user input
          foodle_this.value=foodle_this.value.replace(/^[^a-zA-Z]/, '');

          foodle_len_diff = foodle_this.value.length - foodle_save_value.length;
          if ( foodle_this.value.length > foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos - 1;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          }
          if ( foodle_this.value.length < foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos + foodle_len_diff;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          } else {
            foodle_this.selectionStart = foodle_save_curpos - foodle_len_diff;
            foodle_this.selectionEnd = foodle_save_selend - foodle_len_diff;
          }
        }
      }



      function foodle_check_link_input(foodle_this) {
        if ( ( ! ( ( event.shiftKey ) && ( ( event.keyCode == 37 ) || ( event.keyCode == 38 ) || ( event.keyCode == 39 ) || ( event.keyCode == 40 ) ) ) ) && ( event.keyCode != 16 ) && ( event.keyCode != 17 ) && ( event.keyCode != 18 ) ) {
          foodle_save_curpos = foodle_this.selectionStart;
          foodle_save_selend = foodle_this.selectionEnd;
          foodle_save_value = foodle_this.value;

          // this is the very check - the rest is just there to smoothen the user input
          foodle_this.value=foodle_this.value.replace(/^[^#&a-zA-Z]/, '');

          foodle_len_diff = foodle_this.value.length - foodle_save_value.length;
          if ( foodle_this.value.length > foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos - 1;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          }
          if ( foodle_this.value.length < foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos + foodle_len_diff;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          } else {
            foodle_this.selectionStart = foodle_save_curpos - foodle_len_diff;
            foodle_this.selectionEnd = foodle_save_selend - foodle_len_diff;
          }
        }
      }



      function foodle_check_regexp_static(foodle_this) {
        $v = foodle_this.val();
        if ( $v.match(/^[/]/) == null ) { // must start with '/'
          foodle_this.val('---');
        }
      }



      function foodle_check_regexp_input(foodle_this) {
        if ( ( ! ( ( event.shiftKey ) && ( ( event.keyCode == 37 ) || ( event.keyCode == 38 ) || ( event.keyCode == 39 ) || ( event.keyCode == 40 ) ) ) ) && ( event.keyCode != 16 ) && ( event.keyCode != 17 ) && ( event.keyCode != 18 ) ) {
          foodle_save_curpos = foodle_this.selectionStart;
          foodle_save_selend = foodle_this.selectionEnd;
          foodle_save_value = foodle_this.value;

          // this is the very check - the rest is just there to smoothen the user input
          foodle_this.value=foodle_this.value.replace(/^[^/]/, '');

          foodle_len_diff = foodle_this.value.length - foodle_save_value.length;
          if ( foodle_this.value.length > foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos - 1;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          }
          if ( foodle_this.value.length < foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos + foodle_len_diff;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          } else {
            foodle_this.selectionStart = foodle_save_curpos - foodle_len_diff;
            foodle_this.selectionEnd = foodle_save_selend - foodle_len_diff;
          }
        }
      }



      function foodle_check_regexp(foodle_this) {
        foodle_regexp_good = true;
        foodle_regexp_error = '';
        foodle_regexp_value = foodle_this.val();
        if ( foodle_regexp_value == '---' ) return; // no Regular Expression
        foodle_regexp_first = foodle_regexp_value.indexOf('/');
        foodle_regexp_last = foodle_regexp_value.lastIndexOf('/');
        if ( foodle_regexp_first == foodle_regexp_last ) {
          foodle_regexp_this = foodle_this;
          foodle_regexp_good = false;
          foodle_regexp_error = 'SyntaxError: Missing pattern delimiter.';
        } else {
          if ( ( foodle_regexp_value != '---' ) && ( foodle_regexp_first == foodle_regexp_last ) ) {
            foodle_regexp_this = foodle_this;
            foodle_regexp_good = false;
          }
          else if ( foodle_this.attr('id').substring(0,6) == 'regexp' ) { // just to be sure
            foodle_regexp_pattern = foodle_regexp_value.substr(foodle_regexp_first + 1, foodle_regexp_last - foodle_regexp_first - 1);
            foodle_regexp_flags = foodle_regexp_value.substr(foodle_regexp_last + 1);
            try {
              var regex = new RegExp(foodle_regexp_pattern, foodle_regexp_flags);
              foodle_this.css('border-color', 'DarkGoldenRod');
              foodle_regexp_good = true;
            }
            catch (e) {
              foodle_regexp_this = foodle_this;
              foodle_regexp_good = false;
              foodle_regexp_error = e;
            }
          }
        }
      }



      function foodle_false_regexp_prevent_submit() {

        if ( foodle_regexp_good ) {
            <?php if ( ! $foodle_no_safety_query ): ?> return confirm('<?php echo __('Are you sure to save the metafield definition?','foodle-for-democracy-poll') ?>'); <?php endif ?>
        } else {
          regexp_content = foodle_regexp_this.val();
          foodle_regexp_this.css('border-color', 'red');

          foodle_regexp_this.focus();
          alert('<?php echo __('Invalid Regular Expression','foodle-for-democracy-poll') ?>!\n\n' + foodle_regexp_error);
          return false;
        }
      }



      $(document).ready(function() {
      $(window).keydown(function(event){
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
        }
        });
      });
    </script>
  <?php

  $foodle_delete_button_text = ( $foodle_orphan_metafields ) ? __('Delete all orphaned metafield entries in the database','foodle-for-democracy-poll') : __('No orphaned metafields to delete in the database','foodle-for-democracy-poll');
  $foodle_delete_button_inhibit = ( $foodle_orphan_metafields ) ? '' : ' disabled';
  $foodle_delete_warning = __('Are you sure to delete the orphaned metafield database entries?\n \nIf not sure, you might want to look into the following:\n&bull; Check the drop-down of existing field names for RECOVER: entries\n&bull; Recover the field name(s) found there by selecting accordingly\n&bull; Then, consult the Metafield Defaults & Sorting tab and...\n&bull; ...in there, check the data still stored and decide!\n&bull; You may fine-tune the recovered own field names (not case sensitive)\n \nSo, do you want to proceed deleting?','foodle-for-democracy-poll');
  $foodle_delete_button = ( ( ! current_user_can('manage_options') ) && ( ! $foodle_roles_deleteun ) ) ? '' : '<div style="float:right;margin:30px 0px 0px 5px;"><input type="submit" class="button-primary"'.$warning_delete_unused_metafield_entries.' style="background-color:DarkGoldenrod !important;" name="delete_no_longer_used_metafields" value="'.$foodle_delete_button_text.'..."'.$foodle_delete_button_inhibit.' onclick="return confirm(\''.$foodle_delete_warning.'\');"/></div>';
  echo '<div style="float:left;margin:30px 5px 0px 0px;"><input type="submit" class="button-primary" name="save_metafields_definition" value="'.__('Save metafields definition','foodle-for-democracy-poll').'..." onclick="return foodle_false_regexp_prevent_submit();"/></div>'.$foodle_delete_button.'</form></div>';
}



function foodle_metafield_defaults_sorting() {
  global $foodle_sorting;
  global $foodle_help_tooltips;
  global $foodle_warnings_removed;
  global $foodle_no_safety_query;
  global $foodle_roles_sorting;

  if ( ( ! current_user_can('manage_options') ) && ( ! $foodle_roles_sorting ) ) {
    echo '<p style="font-size:1.5em;color:darkred;"><strong>'.__('This tab is for Foodle administrators only!','foodle-for-democracy-poll').'</strong></p>';
    return;
  }

  // Help and Warnings Tooltips
  $help_sort_metafield_name = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('The metafield name.<br><br><strong>Whether own or existing metafield:</strong><br>The metafield name will be displayed as field title in the user profile.<br>Sorting is always done in the same order as the fields are defined!','foodle-for-democracy-poll').'" ' : '';
  $help_sort_metafield_input = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('One metafield content found (number of occurrences).<br>Click to list the related users.','foodle-for-democracy-poll').'" ' : '';
  $help_sort_metafield_input_replace = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Replace the existing input found (and all its occurrences) by a new input (= sub-category for the polls).<br>This may be used to normalize user inputs if not done through (or for to prepare) drop-down.<br><span style=\'color:darkred;\'>However, you must take extreme special care when modifying content of<br>such existing user metafields as the results may not be foreseeable!</span>','foodle-for-democracy-poll').'" ' : '';
  $help_sort_metafield_not_used_yet = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('This metafield is currently not used.','foodle-for-democracy-poll').'" ' : '';
  $help_sort_metafield_sort = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('If selected:<br>The below list is used for metafield content (sub-categories) sorting only.<br>In this case, you may as well use substrings for sorting.','foodle-for-democracy-poll').'" ' : '';
  $help_sort_metafield_sort_drop_down = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('If selected:<br>The below list is used for metafield content (sub-categories) sorting and drop-down entry.<br>Users can only select data found in this list!<br>A Drop-Down will override Regular Expressions!','foodle-for-democracy-poll').'" ' : '';
  $help_sort_metafield_sort_list = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('This list defines the sorting for the sub-categories (metafield input).<br>One line per element is required.<br>For Drop-Down, leaving the first line empty will allow to leave the field empty<br>when a user wants to save his profile without such content.','foodle-for-democracy-poll').'" ' : '';
  $help_generate_sortlist = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Click to auto-generate the above list<br>based on the basic meta field and<br>the regular expression.<br>(<strong>Disabled upon any input!</strong>)','foodle-for-democracy-poll').'" ' : '';
  $warning_sort_existing_metafield = ( ! $foodle_warnings_removed ) ? ' foodle_ttwarning="'.__('Warning:<br>Be careful when modifying content of unknown existing fields.<br>This may have unwanted effects and may harm WordPress or other plugins!','foodle-for-democracy-poll').'" ' : '';
  $warning_sort_metafield_error_dont_use = ( ! $foodle_warnings_removed ) ? ' foodle_ttwarning_dy="-63" foodle_ttwarning="'.__('This metafield is obviously not to be used!<br>Array content has been detected!','foodle-for-democracy-poll').'" ' : '';
  $warning_sort_metafield_error_disabled = ( ! $foodle_warnings_removed ) ? ' foodle_ttwarning_dy="-63"  foodle_ttwarning="'.__('This metafield is obviously not to be used!<br>It was disabled in the user profile!','foodle-for-democracy-poll').'" ' : '';
  $warning_clean_meta_fields = ( ! $foodle_warnings_removed ) ? ' foodle_ttwarning_dy="-110"  foodle_ttwarning="'.__('The clean-up of meta fields works for non-empty drop down lists only.<br><strong>Be aware that it may have unwanted effects:</strong><br>It will make sure that all users of related user roles will get an entry (protected space),<br>if they are not part of the drop-down list.<br>This could replace previous user entries and will delete entries from not related user roles!','foodle-for-democracy-poll').'" ' : '';

  echo '<p><form action="'.admin_url( 'admin.php?page=foodle-admin-page&tab=metafield-default-sorting' ).'" method="post">';
  ?>
    <figure class="foodle-block-table">
      <table class="foodle-sort-table">
        <thead>
          <tr class="foodle-header-row">
            <th style="padding:0px 10px; text-align:center;"><br><?php echo __('Metafield Name','foodle-for-democracy-poll') ?><br><br></th>
            <th style="padding:0px 10px; text-align:center;" colspan="2"><br><?php echo __('Current Values<br>& Replacements','foodle-for-democracy-poll') ?><br><br></th>
            <th style="padding:0px 10px; text-align:center;"><br><?php echo $foodle_sorting ?><br><br></th>
          </tr>
        </thead>
        <tbody>
  <?php
  $users = get_users(array(
    'orderby'  => 'meta_value',
    'meta_key' => 'last_name', // just a habit ;-)
    'order'    => 'ASC'
  ));
  $foodle_user_metas = array();
  $foodle_user_metas_types = array();
  foreach((array)get_option('foodle_meta_fields') as $foodle_meta_field => $foodle_meta_field_data ) {
    $foodle_user_metas_types[$foodle_meta_field] = "text";
    if ( isset($foodle_meta_field_data[4]) ) $foodle_user_metas_types[$foodle_meta_field] = ( $foodle_meta_field_data[4] != "range" ) ? $foodle_meta_field_data[4] : "number" ;
    foreach($users as $user) {
      update_meta_cache( 'user', array($user->ID) ); // get new data in case it was updated
      $foodle_user_meta = get_user_meta($user->ID, foodle_fieldname_to_meta_name($foodle_meta_field), true );
      if ( ( $foodle_user_meta != '' ) &&  ( ! is_array($foodle_user_meta) ) )
        $foodle_user_metas[$foodle_meta_field][$foodle_user_meta] = ( isset($foodle_user_metas[$foodle_meta_field][$foodle_user_meta]) ) ? $foodle_user_metas[$foodle_meta_field][$foodle_user_meta] + 1 : 1 ;
      else
        if ( is_array($foodle_user_meta) )
          $foodle_user_metas[$foodle_meta_field]['Error: don\'t use!'] = ( isset ($foodle_user_metas[$foodle_meta_field]['Error: don\'t use!']) ) ? $foodle_user_metas[$foodle_meta_field]['Error: don\'t use!'] + 1 : 1 ;
    }
    if ( isset($foodle_user_metas[$foodle_meta_field]) )
      ksort($foodle_user_metas[$foodle_meta_field]);
    else
      $foodle_user_metas[$foodle_meta_field] = array('•••not•used•yet•••' => 0);
  }
  $foodle_meta_defaults_sorting = array();
  $is_foodle_meta_defaults_sorting = true;
  if ( get_option('foodle_meta_defaults_sorting') ) {
    $foodle_meta_defaults_sorting = get_option('foodle_meta_defaults_sorting');
  } else {
    $is_foodle_meta_defaults_sorting = false;
  }
  $foodle_new_sub_category = __('Modify sub-category','foodle-for-democracy-poll');
  $foodle_one_line_per_item = __('One line per item (sorted)','foodle-for-democracy-poll');
  if ( count($foodle_user_metas) == 0 ) echo '<tr><td style="text-align:center;padding:10px; color:SteelBlue;" colspan="4">'.__('No categories to define sorting for, yet','foodle-for-democracy-poll').'!</td></tr>';
  $foodle_base_sortlist = false; // Whether the basic metafield carries a sortlist
  $foodle_field_count = -1; // to get 0 as first iteration
  foreach($foodle_user_metas as $foodle_user_meta => $foodle_user_meta_data ) {
    $foodle_field_count += 1;
    $foodle_user_meta_clean = str_replace('.','-',$foodle_user_meta);
    $foodle_number_of_lines = count($foodle_user_meta_data);
    $foodle_row_count = -1; // to get 0 as first iteration
    foreach ( $foodle_user_meta_data as $foodle_user_meta_input => $foodle_user_meta_count ) {
      $foodle_row_count += 1; // just count the rows
      $foodle_row_class = ( $foodle_field_count == 0 ) ? 'foodle-sorting-row-above' : 'foodle-sorting-row';
      $foodle_checked_list = 'checked="checked"';
      $foodle_checked_drop_down = '';
      $foodle_sortlist = '';
      $foodle_clean_up_enabled = false;
      if ( ( $is_foodle_meta_defaults_sorting) && ( isset($foodle_meta_defaults_sorting[$foodle_user_meta]['sorttype']) ) ) {
        if ( $foodle_meta_defaults_sorting[$foodle_user_meta]['sorttype'] == 'drop-down') {
          $foodle_checked_list = '';
          $foodle_checked_drop_down = 'checked="checked"';
          if ( current_user_can('manage_options') ) $foodle_clean_up_enabled = true; // Clean-up is for administrators only
        }
        $foodle_sortlist = str_replace('<br>','&#13;&#10;',$foodle_meta_defaults_sorting[$foodle_user_meta]['sortlist']);
        if ( ( $foodle_field_count == 0 ) && ( strlen($foodle_sortlist) != 0 ) ) $foodle_base_sortlist = true;
        $foodle_number_of_sortitems = count(explode('<br>',$foodle_meta_defaults_sorting[$foodle_user_meta]['sortlist']));
      }
      if ( strlen($foodle_sortlist) == 0 ) $foodle_clean_up_enabled = false; // No clean-up if drop down list is empty
    ?>
          <tr class="<?php echo $foodle_row_class; ?>">
    <?php
      if ( $foodle_row_count == 0 ):
    ?>
            <td <?php echo $help_sort_metafield_name ?>rowspan="<?php echo $foodle_number_of_lines ?>" style="padding:10px 10px; border-right: 1px solid #888888;">
              <?php echo '<span style="font-size:1.1em;font-weight:bold;">'.str_replace('••', '', $foodle_user_meta).'</span>' ?>
              <?php if ( $foodle_clean_up_enabled ): ?>
                <br>
                <div <?php echo $warning_clean_meta_fields; ?>  style="margin:20px 8px 0px 8px;"><button type="submit" class="button-primary" style="background-color:Green!important;border:1px solid DarkRed !important;" name="meta_field_clean_up" value="<?php echo $foodle_user_meta; ?>" onclick="return confirm('<?php echo __('Are you sure to start the clean-up for','foodle-for-democracy-poll').' <'.str_replace('••','',$foodle_user_meta).'>?'; ?>');"><?php echo __('Clean-up','foodle-for-democracy-poll'); ?>...</button></div>
              <?php endif ?>
            </td>
    <?php
      endif;
      if ( ( $foodle_user_meta_input != '•••not•used•yet•••' ) && ( $foodle_user_meta_input != 'Error: don\'t use!' ) ):
    ?>
            <td style="padding:10px 10px;"><?php echo '<span'.$help_sort_metafield_input.'><button type="submit" class="button-primary" style="background-color:#80abb9!important;" name="foodle_list_meta_sub_members" value="'.$foodle_user_meta.'|'.$foodle_user_meta_input.'">'.str_replace(' ', '&nbsp;', $foodle_user_meta_input.'  ('.$foodle_user_meta_count.')').'</button></span>' ?></td>
            <td style="padding:10px 10px;"><label <?php echo $warning_sort_existing_metafield.$help_sort_metafield_input_replace ?>><?php echo sprintf(_n('Replace this entry by:','Replace all %d entries by:',$foodle_user_meta_count,'foodle-for-democracy-poll'), $foodle_user_meta_count) ?><br><input type="<?php echo $foodle_user_metas_types[$foodle_user_meta] ?>" name="subreplace[<?php echo $foodle_user_meta.']['.$foodle_user_meta_input ?>]" placeholder="<?php echo $foodle_new_sub_category ?>"></label></td>
    <?php
      elseif (  $foodle_user_meta_input == 'Error: don\'t use!' ):
    ?>
            <td colspan="2" style="padding:10px 10px; color:darkred;"><?php echo '<span'.$warning_sort_metafield_error_dont_use.'>'.__('Error: don\'t use (array content found)','foodle-for-democracy-poll').'!!!'.'</span>' ?></td>
    <?php
      else:
    ?>
            <td colspan="2" style="padding:10px 10px;"><?php echo '<span'.$help_sort_metafield_not_used_yet.'>'.__('Not used, yet','foodle-for-democracy-poll').'!'.'</span>' ?></td>
    <?php
      endif;
      if ( ( $foodle_row_count == 0 ) && ( $foodle_user_meta_input != 'Error: don\'t use!' ) ):
    ?>
            <td rowspan="<?php echo $foodle_number_of_lines ?>" style="padding:10px 10px; border-left: 1px solid #888888">
              <label <?php echo $help_sort_metafield_sort ?>><input type="radio" name="sorttype[<?php echo $foodle_user_meta ?>]" value="list" <?php echo $foodle_checked_list ?>><?php echo __('(Sub-)String List','foodle-for-democracy-poll')?></label>&nbsp;&nbsp;&nbsp;
              <label <?php echo $help_sort_metafield_sort_drop_down ?>><input type="radio" name="sorttype[<?php echo $foodle_user_meta ?>]" value="drop-down" <?php echo $foodle_checked_drop_down ?>><?php echo __('Drop-Down','foodle-for-democracy-poll'); ?></label><?php if ( ( isset(get_option('foodle_regex_main')[$foodle_user_meta]) ) && ( get_option('foodle_regex_main')[$foodle_user_meta] != '---' ) ): ?><br><span style="font-size:0.8em; color:darkred;"><?php echo str_replace(' ','&nbsp;', '('.__('Warning: A Regular Expression is set for this field','foodle-for-democracy-poll').')'); ?></span><?php endif ?><br><br>
              <label <?php echo $help_sort_metafield_sort_list ?>><span  style="float:left;color:SteelBlue;font-weight:bold;" class="auto-sortlist-label" id="auto_sortlist_label_<?php echo $foodle_user_meta_clean ?>"></span><?php echo __('Definition per sorted list (empty = not used)','foodle-for-democracy-poll') ?><br>
              <div class="mf_foodle_auto_sortlist_response" id="mf_foodle_auto_sortlist_response_<?php echo $foodle_user_meta_clean ?>"><div class="mf_foodle_auto_sortlist_overlay" id="mf_foodle_auto_sortlist_overlay_<?php echo $foodle_user_meta_clean ?>"></div>
                <textarea id="foodle_sortlist_<?php echo $foodle_user_meta_clean ?>" name="sortlist[<?php echo $foodle_user_meta ?>]" rows="<?php echo ($foodle_number_of_sortitems + 1) ?>" cols="30" wrap="hard" placeholder="<?php echo $foodle_one_line_per_item ?>"><?php echo $foodle_sortlist ?></textarea>
              </div>
              </label>
              <?php if ( ( isset(get_option('foodle_regex_main')[$foodle_user_meta]) ) && ( get_option('foodle_regex_main')[$foodle_user_meta] != '---' ) && ( $foodle_base_sortlist ) && ( $foodle_checked_drop_down == '' ) ): ?>
                <br><br>
                <button <?php echo $help_generate_sortlist; ?>onclick="foodle_auto_generate_sortlist_js('<?php echo $foodle_user_meta; ?>'); return false;" class="button-primary auto-sortlist-button" id="auto_sortlist_button_" style="background-color:#80abb9!important;" value="<?php echo $foodle_user_meta; ?>"><?php echo __('Generate list from regular expression','foodle-for-democracy-poll'); ?></button><br>
              <?php endif ?>
            </td>
    <?php
      elseif ( $foodle_user_meta_input == 'Error: don\'t use!' ):
    ?>
            <td rowspan="<?php echo $foodle_number_of_lines ?>" style="padding:10px 10px; border-left: 1px solid #888888; color:darkred;">
              <?php echo '<span'.$warning_sort_metafield_error_disabled.'>'.__('Error: Use has been disabled','foodle-for-democracy-poll').'!!!'.'</span>' ?>
            </td>
    <?php
      endif
    ?>
          </tr>
    <?php
    }
  }
  ?>
        </tbody>
      </table>
    </figure>
  <?php
  echo '<script type="text/javascript"> var $=jQuery; $("[type=foodle-date]").addClass("foodle-date").attr("type","text"); </script>';
  echo '<p>&nbsp;</p>';
  echo '<input type="submit" class="button-primary" name="save_meta_defaults_sorting" value="'.__('Save defaults & sorting','foodle-for-democracy-poll').'..."'; if ( ! $foodle_no_safety_query ) echo ' onclick="return confirm(\''.__('Are you sure to save the Replacements\nand the Defaults & Sorting data?','foodle-for-democracy-poll').'\');"'; echo '/></form></p>';
}



function foodle_set_special_roles_users() {
  global $wp_roles;
  global $foodle_help_tooltips;
  global $foodle_no_safety_query;
  global $foodle_roles_sproles;

  if ( ( ! current_user_can('manage_options') ) && ( ! $foodle_roles_sproles ) ) {
    echo '<p style="font-size:1.5em;color:darkred;"><strong>'.__('This tab is for Foodle administrators only!','foodle-for-democracy-poll').'</strong></p>';
    return;
  }

  $help_special_role_user = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Selection of roles and users.<br> A \'No selection\' entry will delete the row upon saving.','foodle-for-democracy-poll').'" ' : '';
  $help_special_capabilities = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('<strong><u>Definition of capabilities:</u></strong><br><strong>Mark:</strong> This role or user and his votes will be marked when found in a poll results list. It will,<br>however, not be considered in the category statistics per answer, nor for \'maxcount\'.<br><strong>Special view:</strong> This role or user can view (and execute) hidden content (like reminder activity).<br><strong>No voter:</strong> This user will not be considered a voter, regardless his role.<br><strong>No reminders:</strong> This role or user will receive no email reminders for poll votes.<br>(The latter may be typical for the administrator and a few other registered users.<br>&nbsp;Reason: On command, this plugin would remind all users with a role determined for a certain poll<br> &nbsp;in case they didn\'t vote so far for this poll, except those referred to herein!)','foodle-for-democracy-poll').'" ' : '';
  $help_special_delete_row = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Delete this Foodle capabilities row.','foodle-for-democracy-poll').'" ' : '';
  $help_special_add_row = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Add a new Foodle capabilities row.','foodle-for-democracy-poll').'" ' : '';
  $help_dragging_su_rows = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('You may drag each row to where you want in order to change the row order.<br>This will become active <strong>upon saving</strong>.<br>However, this is a rather cosmetic, not a functional option.','foodle-for-democracy-poll').'"' : '';

  $users = get_users(array(
    'orderby'  => 'meta_value',
    'meta_key' => 'last_name', // just a habit ;-)
    'order'    => 'ASC'
  ));

  echo '<p><form action="'.admin_url( 'admin.php?page=foodle-admin-page&tab=special-roles-users' ).'" method="post">';
  ?>
    <figure class="foodle-block-table">
      <table unselectable="on" class="foodle-special-roles-table unselectable">
        <thead>
          <tr class="foodle-header-row">
            <th style="padding:0px 10px; text-align:center;"><div style="opacity:0.4; margin:auto; width:18px; height:16px; background-size:contain; background-image: url('<?php echo plugin_dir_url(__FILE__).'img/move_sm.png'; ?>');" <?php echo $help_dragging_su_rows ?>></div></th>
            <th style="padding:0px 10px; text-align:center;"><br><?php echo __('Select Roles & Users','foodle-for-democracy-poll') ?><br><br></th>
            <th style="padding:0px 10px; text-align:center;"><br><?php echo __('Set Foodle Capabilities','foodle-for-democracy-poll') ?><br><br></th>
            <th style="padding:0px 10px; text-align:center;">&nbsp;</th> <!-- the column with the buttons -->
          </tr>
        </thead>
        <tbody id="foodle-special-roles-sortable">
  <?php
  if ( ( get_option('foodle_special_functions') ) && ( count(get_option('foodle_special_functions')) != 0 ) ) {
    $foodle_special_functions = get_option('foodle_special_functions');
  } else {
    $foodle_special_functions['no_selection'] = array('');
  }

  $foodle_number_of_rows = count($foodle_special_functions);
  $foodle_meta_count = -1; // in order to have the first iteration see 0
  foreach ( $foodle_special_functions as $selected_user_role => $foodle_capabilities ) {
    $foodle_meta_count += 1;
    if ($foodle_meta_count == 0) {
      $foodle_del_button_style = 'opacity:0.1; cursor:not-allowed;';
      $foodle_del_button_attr = 'disabled="disabled"';
      if ( $foodle_number_of_rows == 1 ) { // just one row existing
        $foodle_add_button_style = 'opacity:1.0; cursor:pointer;';
        $foodle_add_button_attr = '';
      } else {
        $foodle_add_button_style = 'opacity:0.1; cursor:not-allowed;';
        $foodle_add_button_attr = 'disabled="disabled"';
      }
    } else {
      $foodle_del_button_style = 'opacity:1.0; cursor:pointer;';
      $foodle_del_button_attr = '';
      if ( $foodle_meta_count + 1 == $foodle_number_of_rows ) { // the last row
        $foodle_add_button_style = 'opacity:1.0; cursor:pointer;';
        $foodle_add_button_attr = '';
      } else {
        $foodle_add_button_style = 'opacity:0.1; cursor:not-allowed;';
        $foodle_add_button_attr = 'disabled="disabled"';
      }
    }
  ?>
          <tr class="foodle-functions-row" id="foodle-functions-row-<?php echo $foodle_meta_count ?>">
            <td style="padding: 10px 10px;">
              <div class="foodle-special-roles-handle" style="cursor:move; margin:auto; width:18px; height:16px; background-size:contain; background-image: url('<?php echo plugin_dir_url(__FILE__).'img/move_sm.png'; ?>');" <?php echo $help_dragging_su_rows ?>></div>
            </td>
            <td style="padding:10px 10px;">
              <label <?php echo $help_special_role_user ?>><span style="font-weight:bold;"><?php echo __('Role / User','foodle-for-democracy-poll').':' ?></span><br>
                <select style="font-size:0.95em;" class="roles-users-sel" id="roles-users-sel-<?php echo $foodle_meta_count ?>"  name="roles_users_field[]" size="1">
                  <optgroup label ="<?php echo __('Nothing','foodle-for-democracy-poll') ?>">
                    <option style="font-size:0.95em;" value="no_selection" <?php if ( $selected_user_role == 'no_selection' ) echo 'selected="selected"';?>><?php echo __('No selection','foodle-for-democracy-poll') ?></option>
                  </optgroup>
                  <optgroup label ="<?php echo __('Roles','foodle-for-democracy-poll') ?>">
                    <?php   foreach ( $wp_roles->role_names as $foodle_role_slug => $foodle_role_name) { ?>
                      <option style="font-size:0.95em;" value="<?php echo $foodle_role_slug;?>" <?php if ( $selected_user_role == $foodle_role_slug ) echo 'selected="selected"';?>><?php echo _ex($foodle_role_name,'User role'); ?></option>
                    <?php } ?>
                  </optgroup>
                  <optgroup label ="<?php echo __('Users','foodle-for-democracy-poll') ?>">
                    <?php foreach ( $users as $user ) {
                            $foodle_user_id = $user->ID;
                            $foodle_user_info = get_userdata($foodle_user_id);
                            $foodle_user = $foodle_user_info->user_login;
                           ?>
                      <option style="font-size:0.95em;" value="<?php echo $foodle_user_id;?>" <?php if ( $selected_user_role == $foodle_user_id ) echo 'selected="selected"';?>><?php echo $foodle_user; ?></option>
                    <?php } ?>
                  </optgroup>
                </select>
              </label>
            </td>
            <td style="padding:10px 10px;">
              <span  <?php echo $help_special_capabilities ?>>
                <span style="font-weight:bold;"><?php echo __('Capabilities','foodle-for-democracy-poll').':' ?></span><br>
                <label>
                  <input type ="checkbox" style="font-size:0.95em;" id="check-mark-<?php echo $foodle_meta_count ?>" name="capabilities<?php echo $foodle_meta_count ?>[]" value="mark" <?php if ( in_array('mark', $foodle_capabilities ) ) echo 'checked="checked"';?>/><?php echo __('Mark','foodle-for-democracy-poll') ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </label>
                <label>
                  <input type ="checkbox" style="font-size:0.95em;" id="check-view-<?php echo $foodle_meta_count ?>" name="capabilities<?php echo $foodle_meta_count ?>[]" value="view" <?php if ( in_array('view', $foodle_capabilities ) ) echo 'checked="checked"';?>/><?php echo __('Special view','foodle-for-democracy-poll') ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </label>
                <label>
                  <input type ="checkbox" style="font-size:0.95em;" class='check-no-voter' id="check-no-voter-<?php echo $foodle_meta_count ?>" name="capabilities<?php echo $foodle_meta_count ?>[]" value="no-voter" <?php if ( ! is_numeric($selected_user_role) ) echo 'disabled="disabled"'; if ( in_array('no-voter', $foodle_capabilities ) ) echo 'checked="checked"';?>/><?php echo __('No voter','foodle-for-democracy-poll') ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </label>
                <label>
                  <input type ="checkbox" style="font-size:0.95em;" id="check-no-remind-<?php echo $foodle_meta_count ?>" name="capabilities<?php echo $foodle_meta_count ?>[]" value="no-remind" <?php if ( in_array('no-remind', $foodle_capabilities ) ) echo 'checked="checked"';?>/><?php echo __('No reminders','foodle-for-democracy-poll') ?>
                </label>
              </span>
            </td>
            <td style="padding:0px 10px;">
              <div style="display:block; padding:10px;">
                <input <?php echo $help_special_delete_row ?>type="button" style="padding:0px; border: 0px solid darkred; color:white; font-size:1em; width:25px; height:25px;<?php echo $foodle_del_button_style; ?> background-color:darkred; border-radius:25px;" id="del-foodle-sel-<?php echo $foodle_meta_count ?>" value="✘" onclick="foodle_remove_functions_row($(this));" <?php echo $foodle_del_button_attr; ?>><br><br>
                <input type="hidden" name="foodle_functions_line[]" value="<?php echo $foodle_meta_count ?>">
                <input <?php echo $help_special_add_row ?>type="button" style="padding:5px 8px; border: 0px solid darkgreen; color:white; font-size:1em; width:25px; height:25px;<?php echo $foodle_add_button_style; ?> background-color:darkgreen; border-radius:25px;" id="add-foodle-sel-<?php echo $foodle_meta_count ?>" value="✛" onclick="foodle_add_functions_row($(this));" <?php echo $foodle_add_button_attr; ?>>
              </div>
            </td>
          </tr>
  <?php
  }
  ?>
        </tbody>
      </table>
    </figure>

    <script type="text/javascript">
      var $ = jQuery;
      var foodle_scroll_store;
      var foodle_width_allow_unset = true;
      var plugin_url = '<?php echo plugin_dir_url(__FILE__); ?>';

      function foodle_activate_event_handler_to_store_special_roles_scrolltop() {
        $('.foodle-special-roles-handle').on('mousedown', function() {
          foodle_scroll_store = $('html').scrollTop();
        });
      }

      function foodle_activate_event_handler_to_set_special_roles_column_widths() {
        $('.foodle-special-roles-handle').on('mousedown', function() {
          $(".foodle-special-roles-table").css("width", $(".foodle-special-roles-table").outerWidth());
          $(".foodle-special-roles-table th, .foodle-special-roles-table td").each( function() {
            $(this).css("width", $(this).width() + 0.001);
          });
        });
        $('.foodle-special-roles-handle').on('mouseup', function() {
          if ( foodle_width_allow_unset )
            $(".foodle-special-roles-table, .foodle-special-roles-table th, .foodle-special-roles-table td").each( function() {
              $(this).css("width", '');
          });
        });
      }

      function foodle_unset_special_roles_column_widths() {
        $(".foodle-special-roles-table, .foodle-special-roles-table th, .foodle-special-roles-table td").each( function() {
          $(this).css("width", '');
        });
      }

      $('#foodle-special-roles-sortable').sortable({
        opacity: 0.66,
        revert: true,
        forcePlaceholderSize: true,
        forceHelperSize: true,
        handle: '.foodle-special-roles-handle',
        //axis: 'y',
        pull: 'clone',
        tolerance: 'pointer',
        delay: 150,
        scroll: true,
        scrollSensitivity: 10,
        scrollSpeed: 40,
        create: function(e, ui){
          foodle_activate_event_handler_to_store_special_roles_scrolltop();
          foodle_activate_event_handler_to_set_special_roles_column_widths();
        },
        start: function(e, ui){
          ui.placeholder.css('visibility', 'visible');
          ui.placeholder.children().css({'background-color':'AliceBlue','background-image':'url("' + plugin_url + 'img/foodle.png")','background-repeat':'no-repeat','background-position':'center','opacity':'0'}).fadeTo(660,0.4);
          ui.placeholder.height(ui.item.outerHeight());
          foodle_width_allow_unset = false;
        },
        activate: function(e, ui){
          $('html').scrollTop(foodle_scroll_store);
        },
        stop: function(e, ui){
          foodle_unset_special_roles_column_widths();
          foodle_width_allow_unset = true;
        }
      });

      function check_no_voter_change(){
        $('.roles-users-sel').change(function(){
          if ( isNaN($(this).val()) )
            $(this).parents('.foodle-functions-row').find('.check-no-voter').prop('disabled',true);
          else
            $(this).parents('.foodle-functions-row').find('.check-no-voter').prop('disabled',false);
        });
      }
      check_no_voter_change();

      function foodle_add_functions_row(foodle_this) {
        foodle_functions_last_row = foodle_this.closest('tr').prop('outerHTML');

        idtitle = 'roles-users-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        newcount = (foodle_functions_last_row.substring( posstart + idlength, posend)) * 1 + 1; // this value is valid for all further fields
        new_val = idtitle + newcount;
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        idtitle = 'capabilities';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('[', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        const regex = new RegExp(old_val,'g'); // replace all occurrences
        new_val = idtitle + newcount;
        foodle_functions_last_row = foodle_functions_last_row.replace(regex, new_val); // all occurrences

        idtitle = 'check-mark-';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        new_val = idtitle + newcount;
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        idtitle = 'check-view-';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        idtitle = 'no-voter-';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        idtitle = 'check-no-remind-';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        idtitle = 'add-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        idtitle = 'del-foodle-sel-';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        idtitle = 'foodle_functions_line[]" value="';
        idlength = idtitle.length; // characters
        posstart = foodle_functions_last_row.indexOf(idtitle);
        posend = foodle_functions_last_row.indexOf('"', posstart);
        old_val = foodle_functions_last_row.substring(posstart, posend);
        new_val = idtitle + newcount; // newcount defined before
        foodle_functions_last_row = foodle_functions_last_row.replace(old_val, new_val);

        $('.foodle-functions-row').last().after(foodle_functions_last_row);

        oldcount = newcount - 1;

        $('#roles-users-sel-' + newcount).children().children().prop('selected',false);
        $('#roles-users-sel-' + newcount).children().first().children().first().prop('selected',true);

        $('#check-mark-' + newcount).prop('checked',false);
        $('#check-view-' + newcount).prop('checked',false);
        $('#check-no-voter-' + newcount).prop('checked',false);
        $('#check-no-remind-' + newcount).prop('checked',false);

        $('#add-foodle-sel-' + oldcount).prop('disabled',true).css('cursor','not-allowed').css('opacity','0.1');
        $('#del-foodle-sel-' + newcount).prop('disabled',false).css('cursor','pointer').css('opacity','1.0');
        foodle_activate_tooltips(); // to catch the new row as well
        foodle_activate_event_handler_to_store_special_roles_scrolltop(); // to catch the new row as well
        foodle_activate_event_handler_to_set_special_roles_column_widths(); // to catch the new row as well

        check_no_voter_change();
      }



      function foodle_remove_functions_row(foodle_this) {
        foodle_this.closest('tr').remove();
        $('.foodle-functions-row').last().children().last().children().first().children().last().prop('disabled',false).css('opacity','1.0').css('cursor','pointer');
      }



      $(document).ready(function() {
        $(window).keydown(function(event) {
          if(event.keyCode == 13) {
            event.preventDefault();
            return false;
          }
        });
      });
    </script>
  <?php

  echo '<p>&nbsp;</p>';
  echo '<input type="submit" class="button-primary" name="save_special_roles_users" value="'.__('Save special users setting','foodle-for-democracy-poll').'..."'; if ( ! $foodle_no_safety_query ) echo ' onclick="return confirm(\''.__('Are you sure to save the special rights\nfor roles and users?','foodle-for-democracy-poll').'\');"'; echo '/></form></p>';
}



function foodle_edit_email() {
  global $foodle_no_safety_query;
  global $foodle_roles_email;

  if ( ( ! current_user_can('manage_options') ) && ( ! $foodle_roles_email ) ) {
    echo '<p style="font-size:1.5em;color:darkred;"><strong>'.__('This tab is for Foodle administrators only!','foodle-for-democracy-poll').'</strong></p>';
    return;
  }

  if (!get_option('foodle_email_content') )
    echo '<div style="color:#500404;">('.__('The eMail content has not been set, yet. You will not be able to send reminder emails to late poll users!','foodle-for-democracy-poll').')</div><br />';
  else
    echo '<div style="color:#045004;">('.__('The eMail content is ready to use. You may initiate reminder emails to late poll users!','foodle-for-democracy-poll').')</div><br />';

  echo '<p><table><tr><th>'.__('eMail Content','foodle-for-democracy-poll').'</th></tr></table></p>';

  $settings = array(
    'wpautop' => false,
    'media_buttons' => true,
    'default_editor' => '',
    'drag_drop_upload' => true,
    'textarea_rows' => 30,
    'tabindex' => 5,
    'editor_css' => '',
    'editor_class' => 'foodle-email-editor',
    'teeny' => false,
    'tinymce' => true,
    'quicktags' => true
  );
  $foodle_email_content = '';
  if ( get_option('foodle_email_content') ) $foodle_email_content = html_entity_decode(get_option('foodle_email_content'));
  echo '<form name="foodle_email_editor" action="'.admin_url( 'admin.php?page=foodle-admin-page&tab=edit-email' ).'" method="post">';
  echo '<div style="width:90%;">';
  wp_editor($foodle_email_content, 'foodle_email_content', $settings);
  echo '</div>';
  echo '<br><strong>'.__('Available placeholders','foodle-for-democracy-poll').':</strong><br>
  '.__('Poll','foodle-for-democracy-poll').':&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{foodle-title}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{poll-ID}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{poll-title}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{poll-start}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{poll-end}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{users-voted}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{poll-url}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{poll-link}</span><br>
  '.__('User','foodle-for-democracy-poll').':&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{user-login}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{first-name}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{last-name}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{display-name}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{nickname}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{user-nicename}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{user-profile-url}</span><br>
  '.__('Admin','foodle-for-democracy-poll').':&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{firstname-trigger}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{lastname-trigger}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{password-forgot-url}</span>&nbsp;&nbsp;
        <span class="foodle-copy-inline" role="button" tabindex="0" data-copied="false">{password-forgot-link}</span>';
  echo '<p>&nbsp;</p>';
  echo '<input type="submit" class="button-primary" value="'.__('Update email content...','foodle-for-democracy-poll').'"'; if ( ! $foodle_no_safety_query ) echo ' onclick="return confirm(\''.__('Are you sure to save the email content?','foodle-for-democracy-poll').'\');"'; echo '/></form></p>';
}



function foodle_settings() {
  global $wp_roles;
  global $foodle_title;
  global $foodle_admin_menu_color;
  global $foodle_help_tooltips;
  global $foodle_frontend_tooltips;
  global $foodle_warnings_removed;
  global $foodle_no_safety_query;
  global $foodle_results_text_default;
  global $foodle_results_text;
  global $foodle_bar_graph_text_default;
  global $foodle_bar_graph_text;
  global $foodle_roles_metafields;
  global $foodle_roles_sorting;
  global $foodle_roles_sproles;
  global $foodle_roles_email;
  global $foodle_roles_usage;
  global $foodle_roles_settings;
  global $foodle_roles_tips;
  global $foodle_roles_deleteun;
  global $foodle_email_link_admins;
  global $foodle_email_link_non_admins;
  global $foodle_show_vote_date_admins;
  global $foodle_show_vote_time_admins;
  global $foodle_show_vote_date_specview;
  global $foodle_show_vote_time_specview;
  global $foodle_scroll_up_button_visible;

  if ( ( ! current_user_can('manage_options') ) && ( ! $foodle_roles_settings ) ) {
    echo '<p style="font-size:1.5em;color:darkred;"><strong>'.__('This tab is for Foodle administrators only!','foodle-for-democracy-poll').'</strong></p>';
    return;
  }

  $help_settings_foodle_title = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('The \'Foodle Title\' will be used and displayed throughout this plugin<br>in several places including the user profile area.<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span>','foodle-for-democracy-poll').'" ' : '';
  $help_settings_results_text = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Define the text to be displayed above the Foodle table.<br>Three dashes (\'---\') means: no headline.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_bar_graph_text = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Define the text to be displayed inside the bar graph.<br>Three dashes (\'---\') means: no text.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_show_email_links = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, voters\' names will contain a mailto link to their email<br>address, visible for administrators and/or non-administrators.<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span>','foodle-for-democracy-poll').'" ' : '';
  $help_settings_show_vote_date_time = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, the date and time of each vote will be diplayed for admins<br>and/or special viewers (see tab \'Special Roles & Users\'), even when the<br>poll\'s date display is switched off.<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span>','foodle-for-democracy-poll').'" ' : '';
  $help_settings_admin_menu_color = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('In order to identify this plugin and Democracy Poll easier in the admin menu<br> and/or admin toolbar, you may chose any convenient highlighting color.<br>An invalid color name/value will retain the original color setting.<br>Default: #8CBD5A.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_page_post_exclusion = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('With this comma-separated list of IDs, you may determine all pages and/or posts where<br>the Foodle table\'s interactive AJAX shall be switched off. This could become necessary<br>to avoid conflicts, e.g. when more than one Foodle shortcode is present.<br>(Content is being validated during input)<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span>','foodle-for-democracy-poll').'" ' : '';
  $help_settings_date_format = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Define the date format to be used for the metafield input type \'foodle-date\'.<br>The jQuery datepicker documentation should be consulted for details.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_roles = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Define the additional roles which will have the capability to manage Foodle<br>(allowed functions can be tailored).<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span><br>Democracy also provides roles delegation in the Democracy settings.','foodle-for-democracy-poll').'<br>'.__('Remark','foodle-for-democracy-poll').': \''._x("Subscriber",'User role').'\' '.__('is not included for safety reasons.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_roles_allowance = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Define the accessible Foodle functions for such roles carefully.<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span>','foodle-for-democracy-poll').'" ' : '';
  $help_democracy_textarea_template = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Can be used to define a default content, e.g. inserting certain Foodle shortcodes,<br>text or basic HTML code (limited like in WordPress posts).<br>The content can be copied into each Democracy poll\'s textarea during poll edit<br>by the click of a button.<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!','foodle-for-democracy-poll').'" ' : '';
  $help_settings_show_in_admin_bar = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, the Foodle menu will be shown in the admin menu toolbar.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_vote_expiry = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, a regular task will update the poll logs database to avoid vote expiry.<br>If not, Democracy Poll will not use any votes older than one year for re-voting.<br>In such case, the user can vote again in polls still active and his former vote<br>will remain visible in additon.<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span>','foodle-for-democracy-poll').'" ' : '';
  $help_settings_show_extra_metafields = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, the Foodle metafields will be accessible in the user profile.<br>If not, the administrator will still see them, marked by a red dashed border.<br>There\'s as well a shortcode to access them in the front end.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_enable_help = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, Help-Tooltips (like this one) will be activated.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_disable_warnings = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, warning tooltips will be disabled.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_no_safety_query = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, the safety query to avoid inadvertent saving<br>will be disabled in the Foodle admin tabs.<br>This will not affect the button to clean-up the meta fields or<br>to delete the orphaned metafield entries.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_enable_frontend_tooltips = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('When checked, Tooltips (like this one) will be activated in the Foodle frontend.<br>This does not influence the administrator\'s individual use<br>of the tooltip function provided by Foodle.','foodle-for-democracy-poll').'" ' : '';
  $help_settings_scroll_up_button = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('Select the locations where to display the Foodle scroll up button.<br>You can thus also avoid conflicts with the theme or other plugins.<br><span style=\'color:DarkGoldenrod;\'>This settings line will remain an administrator privilege!</span>','foodle-for-democracy-poll').'" ' : '';

  $foodle_democracy_textarea_template = ( ( get_option('foodle_settings') ) && ( isset(get_option('foodle_settings')['foodle-democracy-textarea-template']) ) ) ? get_option('foodle_settings')['foodle-democracy-textarea-template'] : "";
  $foodle_democracy_textarea_template = str_replace("\'","'",str_replace('\"','"',$foodle_democracy_textarea_template));

  $foodle_vote_expiry_checked = '';
  if ( ( get_option('foodle_settings') ) && ( isset(get_option('foodle_settings')['remove-vote-expiry']) ) ) {
    $foodle_vote_expiry_checked = ( get_option('foodle_settings')['remove-vote-expiry'] ) ? 'checked="checked"' : '';
  }

  $foodle_page_post_exclusion = '';
  if ( ( get_option('foodle_settings') ) && ( isset(get_option('foodle_settings')['foodle_page_post_exclusion']) ) )
    if ( get_option('foodle_settings')['foodle_page_post_exclusion'] != '' ) $foodle_page_post_exclusion = implode(',', get_option('foodle_settings')['foodle_page_post_exclusion']);

  $foodle_date_format = 'mm/dd/yy';
  if ( ( get_option('foodle_settings') ) && ( isset(get_option('foodle_settings')['foodle_date_format']) ) )
    $foodle_date_format = get_option('foodle_settings')['foodle_date_format'];

  $foodle_timestamp = ( wp_next_scheduled( 'foodle_cron_hook' ) ) ?  date_i18n('j. F Y', wp_next_scheduled( 'foodle_cron_hook' )) : __('Not scheduled','foodle-for-democracy-poll');

  $foodle_selected_roles = array();
  if ( ( get_option('foodle_settings') ) && ( isset(get_option('foodle_settings')['foodle_selected_roles']) ) )
    $foodle_selected_roles = get_option('foodle_settings')['foodle_selected_roles'];

  $foodle_show_in_admin_menu_bar = false;
  if ( ( get_option('foodle_settings') ) && ( isset(get_option('foodle_settings')['show-in-admin-menu-bar']) ) )
    $foodle_show_in_admin_menu_bar = get_option('foodle_settings')['show-in-admin-menu-bar'];
  $foodle_show_in_admin_menu_bar_checked = ( $foodle_show_in_admin_menu_bar ) ? 'checked="checked"' : '';

  $foodle_metafields_user_profile = true;
  if ( ( get_option('foodle_settings') ) && ( isset(get_option('foodle_settings')['metafields-user-profile']) ) )
    $foodle_metafields_user_profile = get_option('foodle_settings')['metafields-user-profile'];
  $foodle_metafields_user_profile_checked = ( $foodle_metafields_user_profile ) ? 'checked="checked"' : '';

  $foodle_help_tooltips_checked = ( $foodle_help_tooltips ) ? 'checked="checked"' : '';
  $foodle_frontend_tooltips_checked = ( $foodle_frontend_tooltips ) ? 'checked="checked"' : '';
  $foodle_warnings_removed_checked = ( $foodle_warnings_removed ) ? 'checked="checked"' : '';
  $foodle_no_safety_query_checked = ( $foodle_no_safety_query ) ? 'checked="checked"' : '';

  $foodle_roles_metafields_checked = ( $foodle_roles_metafields ) ? 'checked="checked"' : '';
  $foodle_roles_sorting_checked = ( $foodle_roles_sorting ) ? 'checked="checked"' : '';
  $foodle_roles_sproles_checked = ( $foodle_roles_sproles ) ? 'checked="checked"' : '';
  $foodle_roles_email_checked = ( $foodle_roles_email ) ? 'checked="checked"' : '';
  $foodle_roles_usage_checked = ( $foodle_roles_usage ) ? 'checked="checked"' : '';
  $foodle_roles_settings_checked = ( $foodle_roles_settings ) ? 'checked="checked"' : '';
  $foodle_roles_tips_checked = ( $foodle_roles_tips ) ? 'checked="checked"' : '';
  $foodle_roles_deleteun_checked = ( $foodle_roles_deleteun ) ? 'checked="checked"' : '';
  $foodle_email_link_admins_checked = ( $foodle_email_link_admins ) ? 'checked="checked"' : '';
  $foodle_email_link_non_admins_checked = ( $foodle_email_link_non_admins ) ? 'checked="checked"' : '';
  $foodle_show_vote_date_admins_checked = ( $foodle_show_vote_date_admins ) ? 'checked="checked"' : '';
  $foodle_show_vote_time_admins_checked = ( $foodle_show_vote_time_admins ) ? 'checked="checked"' : '';
  $foodle_show_vote_date_specview_checked = ( $foodle_show_vote_date_specview ) ? 'checked="checked"' : '';
  $foodle_show_vote_time_specview_checked = ( $foodle_show_vote_time_specview ) ? 'checked="checked"' : '';

  $foodle_scroll_up_button_frontend_visible_checked = ( $foodle_scroll_up_button_visible['frontend'] ) ? 'checked="checked"' : '';
  $foodle_scroll_up_button_democracy_visible_checked = ( $foodle_scroll_up_button_visible['democracy'] ) ? 'checked="checked"' : '';
  $foodle_scroll_up_button_foodle_visible_checked = ( $foodle_scroll_up_button_visible['foodle'] ) ? 'checked="checked"' : '';
  $foodle_scroll_up_button_comments_visible_checked = ( $foodle_scroll_up_button_visible['comments'] ) ? 'checked="checked"' : '';
  $foodle_scroll_up_button_bar_graph_visible_checked = ( $foodle_scroll_up_button_visible['bar_graph'] ) ? 'checked="checked"' : '';
  $foodle_scroll_up_button_backend_visible_checked = ( $foodle_scroll_up_button_visible['backend'] ) ? 'checked="checked"' : '';
  $foodle_scroll_up_button_democracy_admin_visible_checked = ( $foodle_scroll_up_button_visible['democracy_admin'] ) ? 'checked="checked"' : '';
  $foodle_scroll_up_button_foodle_admin_visible_checked = ( $foodle_scroll_up_button_visible['foodle_admin'] ) ? 'checked="checked"' : '';
  
  $foodle_row_hidden = ( current_user_can('manage_options') ) ? "" : ' hidden ';

    echo '<p><form action="'.admin_url( 'admin.php?page=foodle-admin-page&tab=foodle-settings' ).'" method="post">';
  ?>
    <figure class="foodle-block-table">
      <table class="foodle-settings-table">
        <thead>
          <tr class="foodle-header-row">
            <th style="padding:0px 10px; text-align:center;"><br><?php echo __('Subject','foodle-for-democracy-poll') ?><br><br></th>
            <th colspan="3" style="padding:0px 10px; text-align:center;"><br><?php echo __('Setting','foodle-for-democracy-poll') ?><br><br></th>
          </tr>
        </thead>
        <tbody>
          <tr <?php echo $foodle_row_hidden ?> <?php echo $help_settings_foodle_title ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Foodle Title','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('This title is used throughout the plugin incl. the profile pages.','foodle-for-democracy-poll') ?>
            </td>
            <td colspan="3" class="foodle-settings-row-hidden" style="padding:10px 10px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
                <input type="text" style="font-size:0.95em;" id="foodle-title" name="foodle_title" value="<?php echo $foodle_title; ?>" placeholder="Foodle"/>
            </td>
          </tr>
          <tr <?php echo $help_settings_results_text ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Foodle Table Headline','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('This text will be displayed above the Foodle table, \'---\' = no headline.','foodle-for-democracy-poll') ?>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <input type="text" style="font-size:0.95em;" id="foodle-results-text" name="foodle_results_text" value="<?php echo $foodle_results_text; ?>" size="50" placeholder="<? echo $foodle_results_text_default ?>"/>
            </td>
          </tr>
          <tr <?php echo $help_settings_bar_graph_text ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Bar Graph Text','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('This text will be displayed in each bar graph, \'---\' = no text.','foodle-for-democracy-poll') ?>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <input type="text" style="font-size:0.95em;" id="foodle-bar-graph-text" name="foodle_bar_graph_text" value="<?php echo $foodle_bar_graph_text; ?>" size="50" placeholder="<? echo $foodle_bar_graph_text_default ?>"/>
            </td>
          </tr>
          <tr <?php echo $foodle_row_hidden ?> <?php echo $help_settings_show_email_links ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Add a mailto link to the voters\' names in the Foodle table','foodle-for-democracy-poll').':' ?>
              </span>
            </td>
            <td class="foodle-settings-row-hidden" colspan="3" style="padding:10px 10px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
                <label><input type="checkbox" style="font-size:0.95em;" id="voter-email-link-for-admins" name="voter-email-link-for-admins" <?php echo $foodle_email_link_admins_checked; ?> /> <?php echo __('Link visible for admins','foodle-for-democracy-poll') ?></label>&nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="checkbox" style="font-size:0.95em;" id="voter-email-link-for-non-admins" name="voter-email-link-for-non-admins" <?php echo $foodle_email_link_non_admins_checked; ?> /> <?php echo __('Link visible for non-admins','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $foodle_row_hidden ?> <?php echo $help_settings_show_vote_date_time ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Show any vote date and vote time to administrators and special viewers anyway','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('Special viewers can be selected in the tab \'Special Roles & Users\'. Time will only display if the date is being displayed!','foodle-for-democracy-poll') ?>
            </td>
            <td class="foodle-settings-row-hidden" colspan="3" style="padding:10px 10px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
                <label><input type="checkbox" style="font-size:0.95em;" id="vote-date-for-admins" name="vote-date-for-admins" <?php echo $foodle_show_vote_date_admins_checked; ?> /> <?php echo __('Vote date for administrators','foodle-for-democracy-poll') ?></label>&nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="checkbox" style="font-size:0.95em;" id="vote-time-for-admins" name="vote-time-for-admins" <?php echo $foodle_show_vote_time_admins_checked; ?> /> <?php echo __('Vote time for administrators','foodle-for-democracy-poll') ?></label><br>
                <label><input type="checkbox" style="font-size:0.95em;" id="vote-date-for-specview" name="vote-date-for-specview" <?php echo $foodle_show_vote_date_specview_checked; ?> /> <?php echo __('Vote date for special viewers','foodle-for-democracy-poll') ?></label>&nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="checkbox" style="font-size:0.95em;" id="vote-time-for-specview" name="vote-time-for-specview" <?php echo $foodle_show_vote_time_specview_checked; ?> /> <?php echo __('Vote time for special viewers','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $help_settings_admin_menu_color ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Democracy Poll & Foodle Admin Menu Colors','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('Just meant as a smooth highlighting of these two plugin links.','foodle-for-democracy-poll') ?>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <input type="text" id="foodle-admin-color-select" style="font-size:0.95em;" id="foodle-admin-menu-color" name="foodle_admin_menu_color" value="<?php echo $foodle_admin_menu_color; ?>" placeholder="#8CBD5A" onkeyup="foodle_check_hex_color(this);"/><input type="text" id="foodle-admin-color-view" disabled="disabled" style="width:30px;border:0px; background-color:<?php echo $foodle_admin_menu_color; ?>;"/>
            </td>
          </tr>
          <tr <?php echo $foodle_row_hidden ?> <?php echo $help_settings_page_post_exclusion ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Page and Post IDs with Foodle\'s interactive AJAX switched off','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('May be necessary to avoid conflicts in certain cases.','foodle-for-democracy-poll') ?>
            </td>
            <td class="foodle-settings-row-hidden" colspan="3" style="padding:10px 10px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
                <input type="text" style="font-size:0.95em;" id="foodle-page-post-exclusion" name="foodle_page_post_exclusion" value="<?php echo $foodle_page_post_exclusion; ?>" size="50" placeholder="<?php echo __('Comma-separated list','foodle-for-democracy-poll') ?>" onkeyup="foodle_check_post_list(this);"/>
            </td>
          </tr>
          <tr <?php echo $help_settings_date_format ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('The datepicker format to be used for input type \'foodle-date\'','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('To make sure that date inputs are formatted - see <a href=\'https://api.jqueryui.com/datepicker/#utility-formatDate\'>jQuery datepicker API</a>.','foodle-for-democracy-poll') ?>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <input type="text" style="font-size:0.95em;" id="foodle-date-format" name="foodle_date_format" value="<?php echo $foodle_date_format; ?>" size="50" placeholder="<?php echo __('Date format acc. jQuery datepicker API','foodle-for-democracy-poll') ?>"/>
            </td>
          </tr>
          <tr <?php echo $foodle_row_hidden ?> class="foodle-standard-row">
            <td <?php echo $help_settings_roles ?>style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('The roles, besides \'Administrator\', which will have access to manage Foodle','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('Asigns the capability \'manage_foodle\' to the selected roles and tailors their functional allowances.','foodle-for-democracy-poll') ?>
            </td>
            <td <?php echo $help_settings_roles ?>style="padding:10px 10px;">
            <select class="foodle-roles-select" style="font-size:0.85em;" id="foodle-selected-roles" name="foodle_selected_roles[]" size="6" multiple>
          <?php foreach ( $wp_roles->role_names as $foodle_wp_role_slug=>$foodle_wp_role_name ) {
                  if ( ( $foodle_wp_role_slug == 'administrator' ) || ( $foodle_wp_role_slug == 'subscriber' ) ) continue;
                  $foodle_role_selected = ( in_array($foodle_wp_role_slug,$foodle_selected_roles) ) ? 'selected="selected"' : ''; ?>
            <option value="<?php echo $foodle_wp_role_slug.'" '.$foodle_role_selected ?>><?php _ex($foodle_wp_role_name,'User role') ?></option>
          <?php } ?>
            </select>
            </td>
            <td <?php echo $help_settings_roles_allowance ?>style="padding:10px 5px 10px 10px;">
            <label style="float:left; white-space:nowrap;"><input type="checkbox" style="font-size:0.95em;" id="foodle-roles-metafields" name="foodle-roles-metafields" <?php echo $foodle_roles_metafields_checked; ?> /><?php echo __('Metafield definiton','foodle-for-democracy-poll') ?></label><br>
            <label style="float:left; white-space:nowrap;"><input type="checkbox" style="font-size:0.95em;" id="foodle-roles-sproles" name="foodle-roles-sproles" <?php echo $foodle_roles_sproles_checked; ?> /><?php echo __('Special roles/users','foodle-for-democracy-poll') ?></label><br>
            <label style="float:left; white-space:nowrap;"><input type="checkbox" style="font-size:0.95em;" id="foodle-roles-settings" name="foodle-roles-settings" <?php echo $foodle_roles_settings_checked; ?> /><?php echo __('Foodle Settings','foodle-for-democracy-poll') ?></label><br>
            <label style="float:left; white-space:nowrap;"><input type="checkbox" style="font-size:0.95em;" id="foodle-roles-deleteun" name="foodle-roles-deleteun" <?php echo $foodle_roles_deleteun_checked; ?> /><?php echo __('Delete unused data','foodle-for-democracy-poll') ?></label>
            </td>
            <td class="foodle-settings-row-hidden" <?php echo $help_settings_roles_allowance ?>style="padding:10px 10px 10px 5px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
            <label style="float:right; white-space:nowrap;"><?php echo __('Defaults & sorting','foodle-for-democracy-poll') ?> <input type="checkbox" style="font-size:0.95em;" id="foodle-roles-sorting" name="foodle-roles-sorting" <?php echo $foodle_roles_sorting_checked; ?> /></label><br>
            <label style="float:right; white-space:nowrap;"><?php echo __('Email Definition','foodle-for-democracy-poll') ?> <input type="checkbox" style="font-size:0.95em;" id="foodle-roles-email" name="foodle-roles-email" <?php echo $foodle_roles_email_checked; ?> /></label><br>
            <label style="float:right; white-space:nowrap;"><?php echo __('Shortcode Use','foodle-for-democracy-poll') ?> <input type="checkbox" style="font-size:0.95em;" id="foodle-roles-usage" name="foodle-roles-usage" <?php echo $foodle_roles_usage_checked; ?> /></label><br>
            <label style="float:right; white-space:nowrap;"><?php echo __('Widespread Tips','foodle-for-democracy-poll') ?> <input type="checkbox" style="font-size:0.95em;" id="foodle-roles-tips" name="foodle-roles-tips" <?php echo $foodle_roles_tips_checked; ?> /></label>
            </td>
          </tr>
          <tr <?php echo $foodle_row_hidden ?> <?php echo $help_democracy_textarea_template ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Pre-defined user template for use in Democracy\'s textarea of each poll','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('Can be inserted during poll edit by the click of a button.','foodle-for-democracy-poll') ?>
            </td>
            <td class="foodle-settings-row-hidden" colspan="3" style="padding:10px 10px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
                <textarea type="text" style="font-size:0.95em;" id="foodle-democracy-textarea-template" name="foodle_democracy_textarea_template" rows="8" cols="50" placeholder="<?php echo __('Democracy textarea template','foodle-for-democracy-poll') ?>" ><?php echo $foodle_democracy_textarea_template; ?></textarea>
            </td>
          </tr>
          <tr <?php echo $help_settings_show_in_admin_bar ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Show the Foodle menu in the admin menu toolbar','foodle-for-democracy-poll').':' ?>
              </span>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <label><input type="checkbox" style="font-size:0.95em;" id="show-in-admin-menu-bar" name="show-in-admin-menu-bar" <?php echo $foodle_show_in_admin_menu_bar_checked; ?>  /> <?php echo __('Show in admin menu toolbar','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $foodle_row_hidden ?> <?php echo $help_settings_vote_expiry ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Remove Democracy Poll votes\' expiring regularly','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('Typically one year from each individual vote.','foodle-for-democracy-poll') ?>
            </td>
            <td class="foodle-settings-row-hidden" colspan="3" style="padding:10px 10px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
                <label><input type="checkbox" style="font-size:0.95em;" id="remove-vote-expiry" name="remove-vote-expiry" <?php echo $foodle_vote_expiry_checked; ?>  /> <?php echo str_replace(' ', '&nbsp;', __('Next check','foodle-for-democracy-poll').': '.$foodle_timestamp) ?></label>
            </td>
          </tr>
          <tr <?php echo $help_settings_show_extra_metafields ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Make the Foodle metafields acccessible in the user profile','foodle-for-democracy-poll').':' ?>
              </span>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <label><input type="checkbox" style="font-size:0.95em;" id="metafields-user-profile" name="metafields-user-profile" <?php echo $foodle_metafields_user_profile_checked; ?>  /> <?php echo __('Show Foodle metafields in user profile','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $help_settings_enable_help ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Switch on help tooltips','foodle-for-democracy-poll').':' ?>
              </span>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <label><input type="checkbox" style="font-size:0.95em;" id="help-tooltips" name="help-tooltips" <?php echo $foodle_help_tooltips_checked; ?>  /> <?php echo __('Help tooltips','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $help_settings_disable_warnings ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Don\'t display the warnings any more','foodle-for-democracy-poll').':' ?>
              </span>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <label><input type="checkbox" style="font-size:0.95em;" id="remove-warnings" name="remove-warnings" <?php echo $foodle_warnings_removed_checked; ?>  /> <?php echo __('No warnings','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $help_settings_no_safety_query ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Don\'t display a safety query before saving','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('Except for clean-up or deletion of metafield entries.','foodle-for-democracy-poll') ?>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <label><input type="checkbox" style="font-size:0.95em;" id="no-safety-query" name="no-safety-query" <?php echo $foodle_no_safety_query_checked; ?>  /> <?php echo __('No safety query','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $help_settings_enable_frontend_tooltips ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Switch on Foodle front end tooltips','foodle-for-democracy-poll').':' ?>
              </span>
            </td>
            <td colspan="3" style="padding:10px 10px;">
                <label><input type="checkbox" style="font-size:0.95em;" id="frontend-tooltips" name="frontend-tooltips" <?php echo $foodle_frontend_tooltips_checked; ?>  /> <?php echo __('Foodle front end tooltips','foodle-for-democracy-poll') ?></label>
            </td>
          </tr>
          <tr <?php echo $foodle_row_hidden ?> <?php echo $help_settings_scroll_up_button ?>class="foodle-standard-row">
            <td style="padding:10px 10px;">
              <span style="font-weight:bold;">
              <?php echo __('Locations where the Foodle scroll up button shall be active','foodle-for-democracy-poll').':' ?>
              </span><br><?php echo __('Can be tailored as per your needs and also in order to avoid any conflict. Selecting back end or front end includes the respective subsections!','foodle-for-democracy-poll') ?>
            </td>
            <td class="foodle-settings-row-hidden" colspan="3" style="padding:10px 10px; background-image:url('<?php echo plugin_dir_url( __FILE__ ); ?>img/lock-open-red-22x30.png');">
              <table style="border-collapse:collapse;border:none;margin:0px;padding:0px;"><tbody>
                <tr><td><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-frontend" name="scroll-up-visible-frontend" <?php echo $foodle_scroll_up_button_frontend_visible_checked; ?> /> <?php echo __('Complete front end','foodle-for-democracy-poll') ?></label></td>
                <td style="padding-left:60px"><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-backend" name="scroll-up-visible-backend" <?php echo $foodle_scroll_up_button_backend_visible_checked; ?> /> <?php echo __('Complete back end','foodle-for-democracy-poll') ?></label></td></tr>
                <tr><td><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-democracy" name="scroll-up-visible-democracy" <?php echo $foodle_scroll_up_button_democracy_visible_checked; ?> /> <?php echo __('Democracy','foodle-for-democracy-poll') ?></label></td>
                <td style="padding-left:60px"><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-democracy-admin" name="scroll-up-visible-democracy-admin" <?php echo $foodle_scroll_up_button_democracy_admin_visible_checked; ?> /> <?php echo __('Democracy admin','foodle-for-democracy-poll') ?></label></td></tr>
                <tr><td><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-foodle" name="scroll-up-visible-foodle" <?php echo $foodle_scroll_up_button_foodle_visible_checked; ?> /> <?php echo __('Foodle','foodle-for-democracy-poll') ?></label></td>
                <td style="padding-left:60px"><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-foodle-admin" name="scroll-up-visible-foodle-admin" <?php echo $foodle_scroll_up_button_foodle_admin_visible_checked; ?> /> <?php echo __('Foodle admin','foodle-for-democracy-poll') ?></label></td></tr>
                <tr><td><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-comments" name="scroll-up-visible-comments" <?php echo $foodle_scroll_up_button_comments_visible_checked; ?> /> <?php echo __('Comments','foodle-for-democracy-poll') ?></label></td><td style="padding-left:60px"></td></tr>
                <tr><td><label><input type="checkbox" style="font-size:0.95em;" id="scroll-up-visible-bar-graph" name="scroll-up-visible-bar-graph" <?php echo $foodle_scroll_up_button_bar_graph_visible_checked; ?> /> <?php echo __('Bar graph','foodle-for-democracy-poll') ?></label></td><td style="padding-left:60px"></td></tr>
              </tbody></table>
            </td>
          </tr>
        </tbody>
      </table>
    </figure>

   <script type="text/javascript">
      var $ = jQuery;

      $(document).ready(() => {
        // Vote date visibility
        if ( ! $('#vote-date-for-admins').prop('checked') ) $('#vote-time-for-admins').prop('disabled',true);
        if ( ! $('#vote-date-for-specview').prop('checked') ) $('#vote-time-for-specview').prop('disabled',true);
        $($('#vote-date-for-admins')).change(function(){
          if ( $(this).prop('checked') )
            $('#vote-time-for-admins').prop('disabled',false);
          else
            $('#vote-time-for-admins').prop('disabled',true);
        });
        $($('#vote-date-for-specview')).change(function(){
          if ( $(this).prop('checked') )
            $('#vote-time-for-specview').prop('disabled',false);
          else
            $('#vote-time-for-specview').prop('disabled',true);
        });
        // Scroll up button visibility
        if ( $('#scroll-up-visible-frontend').prop('checked') ) {
          $('#scroll-up-visible-democracy').prop('disabled',true);
          $('#scroll-up-visible-foodle').prop('disabled',true);
          $('#scroll-up-visible-comments').prop('disabled',true);
          $('#scroll-up-visible-bar-graph').prop('disabled',true);
        }
        if ( $('#scroll-up-visible-backend').prop('checked') ) {
          $('#scroll-up-visible-democracy-admin').prop('disabled',true);
          $('#scroll-up-visible-foodle-admin').prop('disabled',true);
        }
        $($('#scroll-up-visible-frontend')).change(function(){
          if ( $(this).prop('checked') ) {
            $('#scroll-up-visible-democracy').prop('disabled',true);
            $('#scroll-up-visible-foodle').prop('disabled',true);
            $('#scroll-up-visible-comments').prop('disabled',true);
            $('#scroll-up-visible-bar-graph').prop('disabled',true);
          } else {
            $('#scroll-up-visible-democracy').prop('disabled',false);
            $('#scroll-up-visible-foodle').prop('disabled',false);
            $('#scroll-up-visible-comments').prop('disabled',false);
            $('#scroll-up-visible-bar-graph').prop('disabled',false);
          }
        });
        $($('#scroll-up-visible-backend')).change(function(){
          if ( $(this).prop('checked') ) {
            $('#scroll-up-visible-democracy-admin').prop('disabled',true);
            $('#scroll-up-visible-foodle-admin').prop('disabled',true);
          } else {
            $('#scroll-up-visible-democracy-admin').prop('disabled',false);
            $('#scroll-up-visible-foodle-admin').prop('disabled',false);
          }
        });
      });

      $(document).ready(() => {
        foodle_check_admin_color();
      });

      $("#foodle-admin-color-select").on("input",function(){
        setTimeout(() => {
          foodle_check_admin_color();
        }, 250);
      });

      function foodle_isHexColor (hex) {
        return ( hex.substr(0,1) == "#" ) && ( ( hex.length == 4 ) || ( hex.length == 5 ) || ( hex.length == 7 ) || ( hex.length == 9 ) || ( hex.length == 7 ) ) && ( ! isNaN(Number('0x' + hex.substr(1))) );
      }

      function foodle_check_admin_color() {
        $foodle_admin_color = $("#foodle-admin-color-select").val();
        if ( foodle_isHexColor($foodle_admin_color) ) 
          $("#foodle-admin-color-view").css("opacity","1").css("background-color",$foodle_admin_color);
        else
          $("#foodle-admin-color-view").css("opacity","0");
      }

      function foodle_check_hex_color(foodle_this) {
        if ( ( ! ( ( event.shiftKey ) && ( ( event.keyCode == 37 ) || ( event.keyCode == 38 ) || ( event.keyCode == 39 ) || ( event.keyCode == 40 ) ) ) ) && ( event.keyCode != 16 ) && ( event.keyCode != 17 ) && ( event.keyCode != 18 ) ) {
          foodle_save_curpos = foodle_this.selectionStart;
          foodle_save_selend = foodle_this.selectionEnd;
          foodle_save_value = foodle_this.value;

          // this is the very check - the rest is just there to smoothen the user input
          $foodle_save_first = foodle_this.value.substr(0,1);
          foodle_this.value=foodle_this.value.replace(/[^a-fA-F0-9]/g, '');
          if ( $foodle_save_first == "#" ) foodle_this.value = "#" + foodle_this.value;

          foodle_len_diff = foodle_this.value.length - foodle_save_value.length;
          if ( foodle_this.value.length > foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos - 1;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          }
          if ( foodle_this.value.length < foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos + foodle_len_diff;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          } else {
            foodle_this.selectionStart = foodle_save_curpos - foodle_len_diff;
            foodle_this.selectionEnd = foodle_save_selend - foodle_len_diff;
          }
        }
      }



      function foodle_check_post_list(foodle_this) {
        if ( ( ! ( ( event.shiftKey ) && ( ( event.keyCode == 37 ) || ( event.keyCode == 38 ) || ( event.keyCode == 39 ) || ( event.keyCode == 40 ) ) ) ) && ( event.keyCode != 16 ) && ( event.keyCode != 17 ) && ( event.keyCode != 18 ) ) {
          foodle_save_curpos = foodle_this.selectionStart;
          foodle_save_selend = foodle_this.selectionEnd;
          foodle_save_value = foodle_this.value;

          // this is the very check - the rest is just there to smoothen the user input
          foodle_this.value=foodle_this.value.replace(/[^0-9,]/g, '');

          foodle_len_diff = foodle_this.value.length - foodle_save_value.length;
          if ( foodle_this.value.length > foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos - 1;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          }
          if ( foodle_this.value.length < foodle_save_value.length ) {
            foodle_this.selectionStart = foodle_save_curpos + foodle_len_diff;
            foodle_this.selectionEnd = foodle_this.selectionStart;
          } else {
            foodle_this.selectionStart = foodle_save_curpos - foodle_len_diff;
            foodle_this.selectionEnd = foodle_save_selend - foodle_len_diff;
          }
        }
      }
    </script>
  <?php

  echo '<p>&nbsp;</p>';
  echo '<input type="submit" class="button-primary" name="save_foodle_settings" value="'.__('Save Foodle settings','foodle-for-democracy-poll').'..."'; if ( ! $foodle_no_safety_query ) echo ' onclick="return confirm(\''.__('Are you sure to save the foodle settings?','foodle-for-democracy-poll').'\');"'; echo '/></form></p>';
}



function foodle_usage() {
  global $wpdb;
  global $foodle_help_tooltips;
  global $foodle_warnings_removed;
  global $foodle_no_safety_query;
  global $foodle_roles_usage;

  if ( ( ! current_user_can('manage_options') ) && ( ! $foodle_roles_usage ) ) {
      echo '<p style="font-size:1.5em;color:darkred;"><strong>'.__('This tab is for Foodle administrators only!','foodle-for-democracy-poll').'</strong></p>';
      return;
  }

  // Help and Warnings Tooltips
  $help_usage_raw = __('This column is an overview about the %s<br>shortcode uses in polls, pages and posts.<br>You may follow the related links for further examination.<br>This list and the Democracy database will be updated<br>upon each page/post and poll save.<br>&#x2606; marks the initial (\'original\') use.<br><span style=\'color:#2271b1;\'>Blue</span> = link, <span style=\'color:DarkGreen;\'>green</span> = edit, <span style=\'color:DarkRed;\'>red</span> = poll id error,<br><span style=\'color:DarkGoldenrod;\'>gold</span> = shortcode id does not match poll id in textarea.','foodle-for-democracy-poll');
  $help_usage_democracy = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.sprintf($help_usage_raw,'Democracy').'" ' : '';
  $help_usage_foodle = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.sprintf($help_usage_raw,'Foodle').'" ' : '';
  $help_usage_comments = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.sprintf($help_usage_raw,'Comments').'" ' : '';
  $help_trigger = ( $foodle_help_tooltips ) ? ' foodle_tooltip="'.__('As an administrator, you can initiate the update for pages/posts manually by this.<br>The same will be accomplished automatically upon each page/post and poll save.','foodle-for-democracy-poll').'" ' : '';

  $poll_textarea_use = __('Poll\'s textarea','foodle-for-democracy-poll');

  echo '<figure class="foodle-block-table">';
  echo '<table class="foodle-usage-table">';
  echo '<thead><tr class="foodle-header-row"><th style="padding:14px;">'.__('Poll ID','foodle-for-democracy-poll').'</th><th style="padding:14px;">'.__('Poll Name','foodle-for-democracy-poll').'</th><th'.$help_usage_democracy.' style="padding:14px;">'.__('Democracy Shortcode Use<br>(Page/Post ID, Status)','foodle-for-democracy-poll').'</th><th'.$help_usage_foodle.' style="padding:14px;">'.__('Foodle Shortcode Use<br>(Page/Post ID, Status)','foodle-for-democracy-poll').'</th><th'.$help_usage_comments.' style="padding:14px;">'.__('Comments Shortcode Use<br>(Page/Post ID, Status)','foodle-for-democracy-poll').'</th></tr></thead><tbody>';
  $post_has_pw = ' &#60;'.__('password','foodle-for-democracy-poll').'&#62;';


  $foodle_shortcode_use_errors_in_democracy_textarea = array();

  // All related democracy and foodle shortcodes to be recorded at this time
  $shortcodes_search = array('democracy','foodle-democracy-poll-list-log','foodle-comments','foodle-poll-bar-graph','foodle-link-democracy-poll');
  $pattern = get_shortcode_regex($shortcodes_search);

  foreach($wpdb->get_results("SELECT * FROM $wpdb->democracy_q ORDER BY id DESC") as $poll_list ) { // get the relevant poll data
    $in_posts = explode(',', $poll_list->in_posts);
    $in_foodles = explode(',', $poll_list->in_foodles);
    $in_comments = explode(',', $poll_list->in_comments);
    $poll_note = $poll_list->note;
    $in_posts_lines = array();
    $in_comments_lines = array();
    $in_foodles_lines = array();

    if ( preg_match_all( '/'. $pattern .'/s', $poll_note, $matches )
    && array_key_exists( 2, $matches ) ) {
      $shortcode_count  = count( $matches[2] );
      $shortcodes_array = $matches[2];
      $atts_array       = $matches[3];
      for ( $i = 0; $i < $shortcode_count; $i++ ) {
        $shortcode = $shortcodes_array[ $i ];
        $atts      = shortcode_parse_atts( $atts_array[ $i ] );
        $use_color = 'darkgreen';
        if ( ! in_array('id',array_keys($atts))  || ( $atts['id'] == '' ) ) {
          $use_color = 'darkred';
          $foodle_shortcode_use_errors_in_democracy_textarea[] = sprintf(__('Missing or erroneous poll id for [%s] in polls','foodle-for-democracy-poll'),$shortcode).': <a style="color:darkgreen;" href="'.admin_url('options-general.php?page=democracy-poll&edit_poll='.$poll_list->id).'#dmc-note-anchor">'.$poll_list->question.' ('.$poll_list->id.', id: '.$shortcode_id.')</a>';
        } else {
          $shortcode_id = strtolower($atts['id']);
          $poll_question = '';
          if ( is_numeric($shortcode_id) ) {
            $sql = "SELECT * FROM $wpdb->democracy_q where id={$shortcode_id}";
            $poll_question = $wpdb->get_row($sql)->question; // Check whether id exists
          }
          if ( ( $shortcode_id != 'self') && ( strlen($poll_question) == 0 ) ) {
            $use_color = 'darkred';
            $foodle_shortcode_use_errors_in_democracy_textarea[] = sprintf(__('Not existing poll id for [%s] in polls','foodle-for-democracy-poll'),$shortcode).': <a style="color:darkgreen;" href="'.admin_url('options-general.php?page=democracy-poll&edit_poll='.$poll_list->id).'#dmc-note-anchor">'.$poll_list->question.' ('.$poll_list->id.', id: '.$shortcode_id.')</a>';
          }
        }
        if ( ( $use_color == 'darkgreen' ) && ( $shortcode_id != 'self' ) && ( $shortcode_id != $poll_list->id ) ) {
          $use_color = 'DarkGoldenrod';
          $foodle_shortcode_use_errors_in_democracy_textarea[] = sprintf(__('[%s] shortcode id does not match poll id in textarea','foodle-for-democracy-poll'),$shortcode).': <a style="color:darkgreen;" href="'.admin_url('options-general.php?page=democracy-poll&edit_poll='.$poll_list->id).'#dmc-note-anchor">'.$poll_list->question.' ('.$shortcode_id.' ⇾ '.$poll_list->id.'/self)</a>';
        }
        if ( $shortcode == 'democracy' ) $in_posts_lines[] = '<a style="color:'.$use_color.';" href="'.admin_url('options-general.php?page=democracy-poll&edit_poll='.$poll_list->id).'#dmc-note-anchor">'.$poll_textarea_use.' (id: '.$atts['id'].')</a>';
        if ( $shortcode == 'foodle-democracy-poll-list-log' ) $in_foodles_lines[] = '<a style="color:'.$use_color.';" href="'.admin_url('options-general.php?page=democracy-poll&edit_poll='.$poll_list->id).'#dmc-note-anchor">'.$poll_textarea_use.' (id: '.$atts['id'].')</a>';
        if ( $shortcode == 'foodle-comments' ) $in_comments_lines[] = '<a style="color:'.$use_color.';" href="'.admin_url('options-general.php?page=democracy-poll&edit_poll='.$poll_list->id).'#dmc-note-anchor">'.$poll_textarea_use.' (id: '.$atts['id'].')</a>';
      }
    }

    echo '<tr class="foodle-standard-row">';
    echo '<td style="border-right: 1px solid #888888; padding:14px; text-align:center;">'.$poll_list->id.'</td>';
    echo '<td style="border-right: 1px solid #888888;padding:14px;"><a style="color:darkgreen;" href="'.admin_url('options-general.php?page=democracy-poll&edit_poll='.$poll_list->id).'#dmc-note-anchor">'.esc_html($poll_list->question).'</a></td>';
    echo '<td style="border-right: 1px solid #888888;padding:14px;">';
    $foodle_first_use = 0;
    $foodle_democracy_list = array();
    foreach($in_posts as $in_post) {
      $foodle_first_use += 1;
      $foodle_origin = ( $foodle_first_use == 1 ) ? "&#x2606;" : "" ;
      $democracy_post_has_pw = ( get_post($in_post)->post_password !== "" ) ? $post_has_pw : '';
      if ( $in_post != '' ) $in_posts_lines[] = '<a style="color:#2271b1;" href="'.get_permalink($in_post).'">'.$foodle_origin.'&nbsp;'.get_the_title($in_post).'&nbsp;&nbsp;'.$foodle_origin.'</a>&nbsp;&nbsp;<a style="color:darkgreen;text-decoration:none!important;font-size:1.4em;" href="'.admin_url('post.php?post='.$in_post).'&action=edit">✎</a><br>('.$in_post.', '.get_post_statuses()[get_post_status($in_post)].$democracy_post_has_pw.')';
    }
    echo implode('<br><br>', $in_posts_lines);
    echo '</td>';
    echo'<td style="border-right: 1px solid #888888;padding:14px;">';
    $foodle_first_use = 0;
    $foodle_foodle_list = array();
    foreach($in_foodles as $in_foodle) {
      $foodle_first_use += 1;
      $foodle_origin = ( $foodle_first_use == 1 ) ? "&#x2606;" : "" ;
      $foodle_post_has_pw = ( get_post($in_foodle)->post_password !== "" ) ? $post_has_pw : '';
      if ( $in_foodle != '' ) $in_foodles_lines[] = '<a style="xclor:#2271b1;" href="'.get_permalink($in_foodle).'">'.$foodle_origin.'&nbsp;'.get_the_title($in_foodle).'&nbsp;&nbsp;'.$foodle_origin.'</a>&nbsp;&nbsp;<a style="color:darkgreen;text-decoration:none!important;font-size:1.4em;" href="'.admin_url('post.php?post='.$in_foodle).'&action=edit">✎</a><br>('.$in_foodle.', '.get_post_statuses()[get_post_status($in_foodle)].$foodle_post_has_pw.')';
    }
    echo implode('<br><br>', $in_foodles_lines);
    echo '</td>';
    echo'<td style="padding:14px;">';
    $foodle_first_use = 0;
    $foodle_comments_list = array();
    foreach($in_comments as $in_comment) {
      $foodle_first_use += 1;
      $foodle_origin = ( $foodle_first_use == 1 ) ? "&#x2606;" : "" ;
      $foodle_post_has_pw = ( get_post($in_foodle)->post_password !== "" ) ? $post_has_pw : '';
      if ( $in_comment != '' ) $in_comments_lines[] = '<a style="color:#2271b1;" href="'.get_permalink($in_comment).'">'.$foodle_origin.'&nbsp;'.get_the_title($in_comment).'&nbsp;&nbsp;'.$foodle_origin.'</a>&nbsp;&nbsp;<a style="color:darkgreen;text-decoration:none!important;font-size:1.4em;" href="'.admin_url('post.php?post='.$in_comment).'&action=edit">✎</a><br>('.$in_comment.', '.get_post_statuses()[get_post_status($in_comment)].$foodle_post_has_pw.')';
    }
    echo implode('<br><br>', $in_comments_lines);
    echo '</td>';
    echo '</tr>';
  }

  echo '</tbody></table></figure>'; 

  if ( current_user_can('manage_options') ) {
    echo '<p>&nbsp;</p>';
    echo '<p><form action="'.admin_url( 'admin.php?page=foodle-admin-page&tab=foodle-usage' ).'" method="post">';
    echo '<input'.$help_trigger.' type="submit" class="button-primary" name="update_shortcode_usage" value="'.__('Update the shortcode use data','foodle-for-democracy-poll').'..."'; if ( ! $foodle_no_safety_query ) echo ' onclick="return confirm(\''.__('Are you sure to update the shortcode use data?\n(This will automatically be accomplished with\neach page/post and poll save)','foodle-for-democracy-poll').'\');"'; echo '/></form></p>';
  }
  if ( count($foodle_shortcode_use_errors_in_democracy_textarea) > 0 ) {
    $foodle_shortcode_use_errors_in_democracy_textarea_html = '<ul style="list-style-type:disc;margin-left:20px;"><li>'.implode('</li><li>',$foodle_shortcode_use_errors_in_democracy_textarea).'</li></ul>';
    $foodle_admin_notice = __('The following errors occured during the last shortcode use update in polls','foodle-for-democracy-poll').':<br>'.$foodle_shortcode_use_errors_in_democracy_textarea_html;
    foodle_pwx_admin_notice__warning('<p>'.$foodle_admin_notice.'</p>');
  }
  if ( get_option('foodle_shortcode_usage_error_pages_posts') ) {
    $foodle_admin_notice = __('The following errors occured during the last shortcode use update in pages/posts','foodle-for-democracy-poll').':<br>'.get_option('foodle_shortcode_usage_error_pages_posts');
    foodle_pwx_admin_notice__warning('<p>'.$foodle_admin_notice.'</p>');
  }
}
