/**
  * The MLA Term List support function.
  *
  * This is an alternative to the WordPress wp_list_categories, wp_dropdown_categories
  * and wp_terms_checklist functions, with additional options to customize the hyperlink
  * behind each term.
  *
  * @since 2.25
  *
  * @param array $attr Attributes of the shortcode.
  *
  * @return string HTML content to display the term list, dropdown control or checklist.
  */
 public static function mla_term_list($attr)
 {
     global $post;
     /*
      * Some do_shortcode callers may not have a specific post in mind
      */
     if (!is_object($post)) {
         $post = (object) array('ID' => 0, 'post_author' => '0', 'post_date' => '', 'post_content' => '', 'post_title' => '', 'post_excerpt' => '', 'post_status' => '', 'post_name' => '', 'post_modified' => '', 'guid' => '', 'post_type' => '');
     }
     $defaults = array_merge(self::$mla_get_terms_parameters, array('echo' => false, 'mla_debug' => false, 'mla_output' => 'ulist', 'hierarchical' => 'true', 'separator' => "\n", 'single_text' => '%d item', 'multiple_text' => '%d items', 'link' => 'current', 'current_item' => '', 'current_item_class' => 'mla_current_item', 'mla_item_parameter' => 'current_item', 'show_count' => false, 'mla_style' => NULL, 'mla_markup' => NULL, 'itemtag' => 'ul', 'termtag' => 'li', 'captiontag' => '', 'mla_nolink_text' => '', 'mla_target' => '', 'hide_if_empty' => false, 'option_all_text' => '', 'option_all_value' => '0', 'option_none_text' => '', 'option_none_value' => '-1', 'depth' => 0, 'child_of' => 0, 'include_tree' => NULL, 'exclude_tree' => NULL), self::$term_list_item_specific_arguments);
     /*
      * Filter the attributes before $mla_item_parameter and "request:" prefix processing.
      */
     $attr = apply_filters('mla_term_list_raw_attributes', $attr);
     /*
      * The current_item parameter can be changed to support
      * multiple lists per page.
      */
     if (!isset($attr['mla_item_parameter'])) {
         $attr['mla_item_parameter'] = $defaults['mla_item_parameter'];
     }
     $mla_item_parameter = $attr['mla_item_parameter'];
     /*
      * Special handling of mla_item_parameter to make
      * multiple lists per page easier. Look for this parameter in $_REQUEST
      * if it's not present in the shortcode itself.
      */
     if (!isset($attr[$mla_item_parameter])) {
         if (isset($_REQUEST[$mla_item_parameter])) {
             $attr[$mla_item_parameter] = $_REQUEST[$mla_item_parameter];
         }
     }
     // $instance supports multiple lists in one page/post
     static $instance = 0;
     $instance++;
     /*
      * Some values are already known, and can be used in data selection parameters
      */
     $upload_dir = wp_upload_dir();
     $page_values = array('instance' => $instance, 'selector' => "mla_term_list-{$instance}", 'site_url' => site_url(), 'base_url' => $upload_dir['baseurl'], 'base_dir' => $upload_dir['basedir'], 'id' => $post->ID, 'page_ID' => $post->ID, 'page_author' => $post->post_author, 'page_date' => $post->post_date, 'page_content' => $post->post_content, 'page_title' => $post->post_title, 'page_excerpt' => $post->post_excerpt, 'page_status' => $post->post_status, 'page_name' => $post->post_name, 'page_modified' => $post->post_modified, 'page_guid' => $post->guid, 'page_type' => $post->post_type, 'page_url' => get_page_link());
     /*
      * Determine markup template to get default arguments
      */
     $arguments = shortcode_atts($defaults, $attr);
     /*
      * $mla_item_parameter, if non-default, doesn't make it through the shortcode_atts
      * filter, so we handle it separately
      */
     if (!isset($arguments[$mla_item_parameter])) {
         if (isset($attr[$mla_item_parameter])) {
             $arguments[$mla_item_parameter] = $attr[$mla_item_parameter];
         } else {
             $arguments[$mla_item_parameter] = $defaults['current_item'];
         }
     }
     if ($arguments['mla_markup']) {
         $template = $arguments['mla_markup'];
     } else {
         $output_parameters = array_map('strtolower', array_map('trim', explode(',', $arguments['mla_output'])));
         if (in_array($output_parameters[0], array('list', 'ulist', 'olist', 'dlist'))) {
             if ('dlist' == $output_parameters[0] || 'list' == $output_parameters[0] && 'dd' == $arguments['captiontag']) {
                 $template = 'term-list-dl';
             } else {
                 $template = 'term-list-ul';
             }
         } elseif ('dropdown' == $output_parameters[0]) {
             $template = 'term-list-dropdown';
         } elseif ('checklist' == $output_parameters[0]) {
             $template = 'term-list-checklist';
         }
     }
     /*
      * Apply default arguments set in the markup template
      */
     $arguments = MLATemplate_Support::mla_fetch_custom_template($template, 'term-list', 'markup', 'arguments');
     if (!empty($arguments)) {
         $attr = wp_parse_args($attr, self::_validate_attributes(array(), $arguments));
     }
     /*
      * Adjust data selection arguments; remove pagination-specific arguments
      */
     unset($attr['limit']);
     unset($attr['offset']);
     /*
      * Look for page-level, 'request:' and 'query:' substitution parameters,
      * which can be added to any input parameter
      */
     foreach ($attr as $attr_key => $attr_value) {
         /*
          * item-specific Display Content parameters must be evaluated
          * later, when all of the information is available.
          */
         if (array_key_exists($attr_key, self::$term_list_item_specific_arguments)) {
             continue;
         }
         $attr_value = str_replace('{+', '[+', str_replace('+}', '+]', $attr_value));
         $replacement_values = MLAData::mla_expand_field_level_parameters($attr_value, $attr, $page_values);
         $attr[$attr_key] = MLAData::mla_parse_template($attr_value, $replacement_values);
     }
     $attr = apply_filters('mla_term_list_attributes', $attr);
     $arguments = shortcode_atts($defaults, $attr);
     /*
      * $mla_item_parameter, if non-default, doesn't make it through the shortcode_atts
      * filter, so we handle it separately
      */
     if (!isset($arguments[$mla_item_parameter])) {
         if (isset($attr[$mla_item_parameter])) {
             $arguments[$mla_item_parameter] = $attr[$mla_item_parameter];
         } else {
             $arguments[$mla_item_parameter] = $defaults['current_item'];
         }
     }
     /*
      * Clean up the current_item(s) to separate term_id from slug
      */
     if (!empty($arguments[$mla_item_parameter])) {
         if (is_string($arguments[$mla_item_parameter])) {
             $arguments[$mla_item_parameter] = explode(',', $arguments[$mla_item_parameter]);
         }
         foreach ($arguments[$mla_item_parameter] as $index => $value) {
             if (ctype_digit($value)) {
                 $arguments[$mla_item_parameter][$index] = absint($value);
             }
         }
     }
     $arguments = apply_filters('mla_term_list_arguments', $arguments);
     self::$mla_debug = !empty($arguments['mla_debug']) ? trim(strtolower($arguments['mla_debug'])) : false;
     if (self::$mla_debug) {
         if ('true' == self::$mla_debug) {
             MLACore::mla_debug_mode('buffer');
         } elseif ('log' == self::$mla_debug) {
             MLACore::mla_debug_mode('log');
         } else {
             self::$mla_debug = false;
         }
     }
     if (self::$mla_debug) {
         MLACore::mla_debug_add('<strong>' . __('mla_debug attributes', 'media-library-assistant') . '</strong> = ' . var_export($attr, true));
         MLACore::mla_debug_add('<strong>' . __('mla_debug arguments', 'media-library-assistant') . '</strong> = ' . var_export($arguments, true));
     }
     /*
      * Determine output type and templates
      */
     $output_parameters = array_map('strtolower', array_map('trim', explode(',', $arguments['mla_output'])));
     if ($is_list = in_array($output_parameters[0], array('list', 'ulist', 'olist', 'dlist'))) {
         $default_style = 'none';
         if ('list' == $output_parameters[0] && 'dd' == $arguments['captiontag']) {
             $default_markup = 'term-list-dl';
             $arguments['itemtag'] = 'dl';
             $arguments['termtag'] = 'dt';
         } else {
             $default_markup = 'term-list-ul';
             $arguments['termtag'] = 'li';
             $arguments['captiontag'] = '';
             switch ($output_parameters[0]) {
                 case 'ulist':
                     $arguments['itemtag'] = 'ul';
                     break;
                 case 'olist':
                     $arguments['itemtag'] = 'ol';
                     break;
                 case 'dlist':
                     $default_markup = 'term-list-dl';
                     $arguments['itemtag'] = 'dl';
                     $arguments['termtag'] = 'dt';
                     $arguments['captiontag'] = 'dd';
                     break;
                 default:
                     $arguments['itemtag'] = 'ul';
             }
         }
         if (NULL == $arguments['mla_style']) {
             $arguments['mla_style'] = $default_style;
         }
         if (NULL == $arguments['mla_markup']) {
             $arguments['mla_markup'] = $default_markup;
         }
     }
     if ($is_dropdown = 'dropdown' == $output_parameters[0]) {
         $default_style = 'none';
         $default_markup = 'term-list-dropdown';
         $arguments['itemtag'] = 'select';
         $arguments['termtag'] = 'option';
         if (NULL == $arguments['mla_style']) {
             $arguments['mla_style'] = $default_style;
         }
         if (NULL == $arguments['mla_markup']) {
             $arguments['mla_markup'] = $default_markup;
         }
     }
     if ($is_checklist = 'checklist' == $output_parameters[0]) {
         $default_style = 'none';
         $default_markup = 'term-list-checklist';
         $arguments['termtag'] = 'li';
         if (NULL == $arguments['mla_style']) {
             $arguments['mla_style'] = $default_style;
         }
         if (NULL == $arguments['mla_markup']) {
             $arguments['mla_markup'] = $default_markup;
         }
     }
     $is_hierarchical = !empty($arguments['hierarchical']) && 'true' == strtolower($arguments['hierarchical']);
     /*
      * Convert lists to arrays
      */
     if (is_string($arguments['taxonomy'])) {
         $arguments['taxonomy'] = explode(',', $arguments['taxonomy']);
     }
     if (is_string($arguments['post_type'])) {
         $arguments['post_type'] = explode(',', $arguments['post_type']);
     }
     if (is_string($arguments['post_status'])) {
         $arguments['post_status'] = explode(',', $arguments['post_status']);
     }
     // Hierarchical exclude is done in _get_term_tree to exclude children
     if ($is_hierarchical && isset($arguments['exclude'])) {
         $exclude_later = $arguments['exclude'];
         unset($arguments['exclude']);
     } else {
         $exclude_later = NULL;
     }
     $tags = self::mla_get_terms($arguments);
     if (!empty($exclude_later)) {
         $arguments['exclude'] = $exclude_later;
     }
     /*
      * Invalid taxonomy names return WP_Error
      */
     if (is_wp_error($tags)) {
         $list = '<strong>' . __('ERROR', 'media-library-assistant') . ': ' . $tags->get_error_message() . '</strong>, ' . $tags->get_error_data($tags->get_error_code());
         if ('array' == $arguments['mla_output']) {
             return array($list);
         }
         if (empty($arguments['echo'])) {
             return $list;
         }
         echo $list;
         return;
     }
     /*
      * Fill in the item_specific link properties, calculate list parameters
      */
     if (isset($tags['found_rows'])) {
         $found_rows = $tags['found_rows'];
         unset($tags['found_rows']);
     } else {
         $found_rows = count($tags);
     }
     $show_empty = false;
     if (0 == $found_rows) {
         if (self::$mla_debug) {
             MLACore::mla_debug_add('<strong>' . __('mla_debug empty list', 'media-library-assistant') . '</strong>, query = ' . var_export($arguments, true));
             $list = MLACore::mla_debug_flush();
             if ('<p></p>' == $list) {
                 $list = '';
             }
         } else {
             $list = '';
         }
         if ('array' == $arguments['mla_output']) {
             $list .= $arguments['mla_nolink_text'];
             if (empty($list)) {
                 return array();
             } else {
                 return array($list);
             }
         }
         $show_empty = empty($arguments['hide_if_empty']) || 'true' !== strtolower($arguments['hide_if_empty']);
         if (($is_checklist || $is_dropdown) && $show_empty) {
             if (empty($arguments['option_none_text'])) {
                 $arguments['option_none_text'] = __('no-terms', 'media-library-assistant');
             }
             $tags[0] = (object) array('term_id' => $arguments['option_none_value'], 'name' => $arguments['option_none_text'], 'slug' => sanitize_title($arguments['option_none_text']), 'term_group' => '0', 'term_taxonomy_id' => $arguments['option_none_value'], 'taxonomy' => current($arguments['taxonomy']), 'description' => '', 'parent' => '0', 'count' => 0, 'level' => 0, 'edit_link' => '', 'term_link' => '', 'link' => '');
             $is_hierarchical = false;
             $found_rows = 1;
         } else {
             $list .= $arguments['mla_nolink_text'];
             if (empty($arguments['echo'])) {
                 return $list;
             }
             echo $list;
             return;
         }
     }
     if (self::$mla_debug) {
         $list = MLACore::mla_debug_flush();
     } else {
         $list = '';
     }
     if ($is_hierarchical) {
         $tags = self::_get_term_tree($tags, $arguments);
         if (is_wp_error($tags)) {
             $list = '<strong>' . __('ERROR', 'media-library-assistant') . ': ' . $tags->get_error_message() . '</strong>, ' . $tags->get_error_data($tags->get_error_code());
             if ('array' == $arguments['mla_output']) {
                 return array($list);
             }
             if (empty($arguments['echo'])) {
                 return $list;
             }
             echo $list;
             return;
         }
         if (isset($tags['found_rows'])) {
             $found_rows = $tags['found_rows'];
             unset($tags['found_rows']);
         } else {
             $found_rows = count($tags);
         }
     } else {
         if (!$show_empty) {
             foreach ($tags as $key => $tag) {
                 $tags[$key]->level = 0;
                 $link = get_edit_tag_link($tag->term_id, $tag->taxonomy);
                 if (!is_wp_error($link)) {
                     $tags[$key]->edit_link = $link;
                     $link = get_term_link(intval($tag->term_id), $tag->taxonomy);
                     $tags[$key]->term_link = $link;
                 }
                 if (is_wp_error($link)) {
                     $list = '<strong>' . __('ERROR', 'media-library-assistant') . ': ' . $link->get_error_message() . '</strong>, ' . $link->get_error_data($link->get_error_code());
                     if ('array' == $arguments['mla_output']) {
                         return array($list);
                     }
                     if (empty($arguments['echo'])) {
                         return $list;
                     }
                     echo $list;
                     return;
                 }
                 if ('edit' == $arguments['link']) {
                     $tags[$key]->link = $tags[$key]->edit_link;
                 } else {
                     $tags[$key]->link = $tags[$key]->term_link;
                 }
             }
             // foreach tag
         }
     }
     /*
      * Add the optional 'all-terms' option, if requested
      */
     if (($is_checklist || $is_dropdown) && !empty($arguments['option_all_text']) && !$show_empty) {
         $option_all = (object) array('term_id' => $arguments['option_all_value'], 'name' => $arguments['option_all_text'], 'slug' => sanitize_title($arguments['option_all_text']), 'term_group' => '0', 'term_taxonomy_id' => $arguments['option_all_value'], 'taxonomy' => current($arguments['taxonomy']), 'description' => '', 'parent' => '0', 'count' => -1, 'level' => 0, 'edit_link' => '', 'term_link' => '', 'link' => '');
         array_unshift($tags[$option_all->taxonomy], $option_all);
         $found_rows += 1;
     }
     $style_values = array_merge($page_values, array('mla_output' => $arguments['mla_output'], 'mla_style' => $arguments['mla_style'], 'mla_markup' => $arguments['mla_markup'], 'taxonomy' => implode('-', $arguments['taxonomy']), 'current_item' => $arguments['current_item'], 'itemtag' => tag_escape($arguments['itemtag']), 'termtag' => tag_escape($arguments['termtag']), 'captiontag' => tag_escape($arguments['captiontag']), 'itemtag_attributes' => '', 'itemtag_class' => 'term-list term-list-taxonomy-' . implode('-', $arguments['taxonomy']), 'itemtag_id' => $page_values['selector'], 'all_found_rows' => $found_rows, 'found_rows' => $found_rows, 'separator' => $arguments['separator'], 'single_text' => $arguments['single_text'], 'multiple_text' => $arguments['multiple_text'], 'echo' => $arguments['echo'], 'link' => $arguments['link']));
     $style_template = $gallery_style = '';
     $use_mla_term_list_style = $is_list && 'none' != strtolower($style_values['mla_style']);
     if (apply_filters('use_mla_term_list_style', $use_mla_term_list_style, $style_values['mla_style'])) {
         $style_template = MLATemplate_support::mla_fetch_custom_template($style_values['mla_style'], 'term-list', 'style');
         if (empty($style_template)) {
             $style_values['mla_style'] = $default_style;
             $style_template = MLATemplate_support::mla_fetch_custom_template($default_style, 'term-list', 'style');
         }
         if (!empty($style_template)) {
             /*
              * Look for 'query' and 'request' substitution parameters
              */
             $style_values = MLAData::mla_expand_field_level_parameters($style_template, $attr, $style_values);
             $style_values = apply_filters('mla_term_list_style_values', $style_values);
             $style_template = apply_filters('mla_term_list_style_template', $style_template);
             $gallery_style = MLAData::mla_parse_template($style_template, $style_values);
             $gallery_style = apply_filters('mla_term_list_style_parse', $gallery_style, $style_template, $style_values);
         }
         // !empty template
     }
     // use_mla_term_list_style
     $list .= $gallery_style;
     $markup_values = $style_values;
     /*
      * Accumulate links for flat and array output
      */
     $tag_links = array();
     if ($is_hierarchical) {
         foreach ($tags as $taxonomy => $root_terms) {
             if (isset($root_terms['found_rows'])) {
                 $markup_values['found_rows'] = $root_terms['found_rows'];
                 unset($root_terms['found_rows']);
             } else {
                 $markup_values['found_rows'] = count($root_terms);
             }
             self::_compose_term_list($list, $tag_links, $root_terms, $markup_values, $arguments, $attr);
         }
     } else {
         self::_compose_term_list($list, $tag_links, $tags, $markup_values, $arguments, $attr);
     }
     if ('array' == $arguments['mla_output'] || empty($arguments['echo'])) {
         return $list;
     }
     echo $list;
 }
 /**
  * Put user-defined markup templates to $mla_custom_templates and database
  *
  * @since 2.30
  *
  * @param	array	name => value for all user-defined markup templates
  * @return	boolean	true if success, false if failure
  */
 public static function mla_put_markup_templates($templates)
 {
     if (MLACore::mla_update_option('markup_templates', $templates)) {
         MLATemplate_Support::mla_load_custom_templates();
         return true;
     }
     return false;
 }
 /**
  * Save MLA Gallery settings to the options table
  *
  * @since 0.80
  *
  * @uses $_REQUEST
  *
  * @return	array	Message(s) reflecting the results of the operation
  */
 private static function _save_gallery_settings()
 {
     $settings_changed = false;
     $message_list = '';
     $error_list = '';
     /*
      * Start with any page-level options
      */
     foreach (MLACoreOptions::$mla_option_definitions as $key => $value) {
         if ('mla_gallery' == $value['tab']) {
             $this_setting_changed = false;
             $old_value = MLACore::mla_get_option($key);
             if ('select' == $value['type']) {
                 if ($old_value != $_REQUEST[MLA_OPTION_PREFIX . $key]) {
                     $this_setting_changed = true;
                 }
             } elseif ('text' == $value['type']) {
                 if ('' == $_REQUEST[MLA_OPTION_PREFIX . $key]) {
                     $_REQUEST[MLA_OPTION_PREFIX . $key] = $value['std'];
                 }
                 if ($old_value != $_REQUEST[MLA_OPTION_PREFIX . $key]) {
                     $this_setting_changed = true;
                 }
             } elseif ('checkbox' == $value['type']) {
                 if (isset($_REQUEST[MLA_OPTION_PREFIX . $key])) {
                     $this_setting_changed = "checked" != $old_value;
                 } else {
                     $this_setting_changed = "checked" == $old_value;
                 }
             }
             /*
              * Always update to scrub default settings
              */
             $message = self::mla_update_option_row($key, $value);
             if ($this_setting_changed) {
                 $settings_changed = true;
                 $message_list .= $message;
             }
         }
         // mla_gallery option
     }
     // foreach mla_options
     /*
      * Get the current style contents for comparison
      *     $new_names[ shortcode ][ template_name ]
      *     $old_templates, $new_templates [ template_name ] - omit shortcode for backwards compatibility
      */
     $old_templates = MLATemplate_Support::mla_get_style_templates();
     $new_templates = array();
     $new_names = $_REQUEST['mla_style_templates_name'];
     $new_values = stripslashes_deep($_REQUEST['mla_style_templates_value']);
     $new_deletes = isset($_REQUEST['mla_style_templates_delete']) ? $_REQUEST['mla_style_templates_delete'] : array();
     /*
      * Build new style template array, noting changes
      */
     $templates_changed = false;
     foreach ($new_names as $shortcode => $template_names) {
         $definition = MLATemplate_Support::$mla_template_definitions['style'][$shortcode];
         foreach ($template_names as $name => $new_name) {
             if (in_array($name, $definition['default_names'])) {
                 continue;
             }
             if (array_key_exists($shortcode, $new_deletes) && array_key_exists($name, $new_deletes[$shortcode])) {
                 /* translators: 1: template type 2: template name */
                 $message_list .= '<br>' . sprintf(_x('Deleting %1$s "%2$s".', 'message_list', 'media-library-assistant'), __('Style Template', 'media-library-assistant'), $name);
                 $templates_changed = true;
                 continue;
             }
             $new_slug = sanitize_title($new_name);
             if ('blank' == $name) {
                 if ('' == $new_slug) {
                     continue;
                 } elseif ('blank' == $new_slug) {
                     /* translators: 1: ERROR tag 2: template name 3: template type */
                     $error_list .= '<br>' . sprintf(__('%1$s: Reserved name "%2$s", new %3$s discarded.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $new_slug, __('Style Template', 'media-library-assistant'));
                     continue;
                 }
                 if (array_key_exists($new_slug, $old_templates)) {
                     /* translators: 1: ERROR tag 2: template name 3: template type */
                     $error_list .= '<br>' . sprintf(__('%1$s: Duplicate name "%2$s", new %3$s discarded.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $new_slug, __('Style Template', 'media-library-assistant'));
                     continue;
                 } else {
                     /* translators: 1: template type 2: template name */
                     $message_list .= '<br>' . sprintf(_x('Adding new %1$s "%2$s".', 'message_list', 'media-library-assistant'), __('Style Template', 'media-library-assistant'), $new_slug);
                     $templates_changed = true;
                 }
             }
             // 'blank' - reserved name
             /*
              * Handle name changes, check for duplicates
              */
             if ('' == $new_slug) {
                 /* translators: 1: ERROR tag 2: element name 3: old value */
                 $error_list .= '<br>' . sprintf(__('%1$s: Blank %2$s, reverting to "%3$s".', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('style template name', 'media-library-assistant'), $name);
                 $new_slug = $name;
             }
             if ($new_slug != $name) {
                 if (array_key_exists($new_slug, $old_templates)) {
                     /* translators: 1: ERROR tag 2: element name 3: new value 4: old value */
                     $error_list .= '<br>' . sprintf(__('%1$s: Duplicate new %2$s "%3$s", reverting to "%4$s".', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('style template name', 'media-library-assistant'), $new_slug, $name);
                     $new_slug = $name;
                 } elseif ('blank' == $new_slug) {
                     /* translators: 1: ERROR tag 2: element name 3: new value 4: old value */
                     $error_list .= '<br>' . sprintf(__('%1$s: Reserved %2$s "%3$s", reverting to "%4$s".', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('style template name', 'media-library-assistant'), $new_slug, $name);
                     $new_slug = $name;
                 } elseif ('blank' != $name) {
                     /* translators: 1: element name 2: old_value 3: new_value */
                     $message_list .= '<br>' . sprintf(_x('Changing %1$s from "%2$s" to "%3$s"', 'message_list', 'media-library-assistant'), __('style template name', 'media-library-assistant'), $name, $new_slug);
                     $templates_changed = true;
                 }
             }
             // name changed
             if ('blank' != $name && $new_values[$shortcode][$name] != $old_templates[$name]) {
                 /* translators: 1: template type 2: template name */
                 $message_list .= '<br>' . sprintf(_x('Updating contents of %1$s "%2$s".', 'message_list', 'media-library-assistant'), __('Style Template', 'media-library-assistant'), $new_slug);
                 $templates_changed = true;
             }
             /*
              * Encode shortcode assignment in template content
              */
             $new_templates[$new_slug] = sprintf("<!-- mla_shortcode_slug=\"%1\$s\" -->\r\n%2\$s", $definition['slug'], $new_values[$shortcode][$name]);
         }
         // foreach $name
     }
     // each shortcode
     if ($templates_changed) {
         $settings_changed = true;
         if (false == MLATemplate_Support::mla_put_style_templates($new_templates)) {
             /* translators: 1: ERROR tag 2: template type */
             $error_list .= '<br>' . sprintf(__('%1$s: Update of %2$s failed.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('Style Template', 'media-library-assistant'));
         }
     }
     /*
      * Get the current markup contents for comparison
      */
     $old_templates = MLATemplate_Support::mla_get_markup_templates();
     $new_templates = array();
     $new_names = $_REQUEST['mla_markup_templates_name'];
     $new_deletes = isset($_REQUEST['mla_markup_templates_delete']) ? $_REQUEST['mla_markup_templates_delete'] : array();
     $new_values = stripslashes_deep($_REQUEST['mla_markup_templates_sections']);
     /*
      * Build new markup template array, noting changes
      */
     $templates_changed = false;
     foreach ($new_names as $shortcode => $template_names) {
         $definition = MLATemplate_Support::$mla_template_definitions['markup'][$shortcode];
         foreach ($template_names as $name => $new_name) {
             if (in_array($name, $definition['default_names'])) {
                 continue;
             }
             if (array_key_exists($shortcode, $new_deletes) && array_key_exists($name, $new_deletes[$shortcode])) {
                 /* translators: 1: template type 2: template name */
                 $message_list .= '<br>' . sprintf(_x('Deleting %1$s "%2$s".', 'message_list', 'media-library-assistant'), __('markup template', 'media-library-assistant'), $name);
                 $templates_changed = true;
                 continue;
             }
             $new_slug = sanitize_title($new_name);
             if ('blank' == $name) {
                 if ('' == $new_slug) {
                     continue;
                 }
                 if ('blank' == $new_slug) {
                     /* translators: 1: ERROR tag 2: template name 3: template type */
                     $error_list .= '<br>' . sprintf(__('%1$s: Reserved name "%2$s", new %3$s discarded.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $new_slug, __('markup template', 'media-library-assistant'));
                     continue;
                 }
                 if (array_key_exists($new_slug, $old_templates)) {
                     /* translators: 1: ERROR tag 2: template name 3: template type */
                     $error_list .= '<br>' . sprintf(__('%1$s: Duplicate name "%2$s", new %3$s discarded.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $new_slug, __('markup template', 'media-library-assistant'));
                     continue;
                 } else {
                     /* translators: 1: template type 2: template name */
                     $message_list .= '<br>' . sprintf(_x('Adding new %1$s "%2$s".', 'message_list', 'media-library-assistant'), __('markup template', 'media-library-assistant'), $new_slug);
                     $templates_changed = true;
                 }
             }
             // 'blank' - reserved name
             /*
              * Handle name changes, check for duplicates
              */
             if ('' == $new_slug) {
                 /* translators: 1: ERROR tag 2: element name 3: old value */
                 $error_list .= '<br>' . sprintf(__('%1$s: Blank %2$s, reverting to "%3$s".', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('markup template name', 'media-library-assistant'), $name);
                 $new_slug = $name;
             }
             if ($new_slug != $name) {
                 if (array_key_exists($new_slug, $old_templates)) {
                     /* translators: 1: ERROR tag 2: element name 3: new value 4: old value */
                     $error_list .= '<br>' . sprintf(__('%1$s: Duplicate new %2$s "%3$s", reverting to "%4$s".', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('markup template name', 'media-library-assistant'), $new_slug, $name);
                     $new_slug = $name;
                 } elseif ('blank' == $new_slug) {
                     /* translators: 1: ERROR tag 2: element name 3: new value 4: old value */
                     $error_list .= '<br>' . sprintf(__('%1$s: Reserved %2$s "%3$s", reverting to "%4$s".', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('markup template name', 'media-library-assistant'), $new_slug, $name);
                     $new_slug = $name;
                 } elseif ('blank' != $name) {
                     /* translators: 1: element name 2: old_value 3: new_value */
                     $message_list .= '<br>' . sprintf(_x('Changing %1$s from "%2$s" to "%3$s"', 'message_list', 'media-library-assistant'), __('markup template name', 'media-library-assistant'), $name, $new_slug);
                     $templates_changed = true;
                 }
             }
             // name changed
             if ('blank' != $name) {
                 foreach ($new_values[$shortcode][$name] as $section_name => $section_value) {
                     $old_value = isset($old_templates[$name][$section_name]) ? $old_templates[$name][$section_name] : '';
                     if ($section_value != $old_value) {
                         /* translators: 1: section name, 2: template name */
                         $message_list .= '<br>' . sprintf(_x('Updating %1$s markup for "%2$s".', 'message_list', 'media-library-assistant'), $section_name, $new_slug);
                         $templates_changed = true;
                     }
                 }
             }
             // ! 'blank'
             /*
              * Encode shortcode assignment in template content
              */
             if (isset($new_values[$shortcode][$name]['arguments'])) {
                 $new_values[$shortcode][$name]['arguments'] = sprintf("mla_shortcode_slug=\"%1\$s\"\r\n%2\$s", $definition['slug'], $new_values[$shortcode][$name]['arguments']);
             } else {
                 $new_values[$shortcode][$name]['arguments'] = sprintf("mla_shortcode_slug=\"%1\$s\"\r\n", $definition['slug']);
             }
             foreach ($new_values[$shortcode][$name] as $section_name => $section_value) {
                 if (!empty($section_value)) {
                     $new_templates[$new_slug][$section_name] = $section_value;
                 }
             }
         }
         // foreach $name
     }
     // foreach $shortcode
     if ($templates_changed) {
         $settings_changed = true;
         if (false == MLATemplate_Support::mla_put_markup_templates($new_templates)) {
             /* translators: 1: ERROR tag 2: template type */
             $error_list .= '<br>' . sprintf(__('%1$s: Update of %2$s failed.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('markup template', 'media-library-assistant'));
         }
     }
     if ($settings_changed) {
         /* translators: 1: field type */
         $message = sprintf(__('%1$s settings saved.', 'media-library-assistant'), __('Shortcodes', 'media-library-assistant')) . "\r\n";
     } else {
         /* translators: 1: field type */
         $message = sprintf(__('%1$s no changes detected.', 'media-library-assistant'), __('Shortcodes', 'media-library-assistant')) . "\r\n";
     }
     $page_content = array('message' => $message . $error_list, 'body' => '');
     /*
      * Uncomment this for debugging.
      */
     // $page_content['message'] .= $message_list;
     return $page_content;
 }