/**
  * Load style and markup templates to $mla_custom_templates
  *
  * @since 2.30
  *
  * @return	void
  */
 public static function mla_load_custom_templates()
 {
     if (empty(MLATemplate_Support::$mla_template_definitions)) {
         MLATemplate_Support::mla_localize_template_definitions();
     }
     MLATemplate_Support::$mla_custom_templates = NULL;
     $default_templates = MLACore::mla_load_template('mla-custom-templates.tpl');
     /* 	
      * Load the default templates
      */
     if (is_null($default_templates)) {
         MLACore::mla_debug_add('<strong>mla_debug mla_load_custom_templates()</strong> ' . __('error loading tpls/mla-custom-templates.tpl', 'media-library-assistant'), MLACore::MLA_DEBUG_CATEGORY_ANY);
         return;
     } elseif (!$default_templates) {
         MLACore::mla_debug_add('<strong>mla_debug mla_load_custom_templates()</strong> ' . __('tpls/mla-custom-templates.tpl not found', 'media-library-assistant'), MLACore::MLA_DEBUG_CATEGORY_ANY);
         return;
     }
     /*
      * Record explicit shortcode assignments
      */
     $mla_shortcode_slugs = array();
     foreach ($default_templates as $key => $value) {
         $mla_shortcode_slug = NULL;
         $match_count = preg_match('#\\<!-- mla_shortcode_slug="(.+)" --\\>[\\r\\n]*#', $value, $matches, PREG_OFFSET_CAPTURE);
         if ($match_count == 0) {
             $match_count = preg_match('#mla_shortcode_slug="(.+)"[ \\r\\n]*#', $value, $matches, PREG_OFFSET_CAPTURE);
         }
         if ($match_count > 0) {
             $mla_shortcode_slug = $matches[1][0];
             //error_log( __LINE__ . " default template {$key}, {$mla_shortcode_slug} matches = " . var_export( $matches, true ), 0 );
             $value = substr_replace($value, '', $matches[0][1], strlen($matches[0][0]));
             if (empty($value)) {
                 unset($default_templates[$key]);
                 //error_log( __LINE__ . " unset default template {$key}, {$mla_shortcode_slug}", 0 );
             } else {
                 $default_templates[$key] = $value;
                 //error_log( __LINE__ . " replace default template {$key}, {$mla_shortcode_slug} value = " . MLAData::mla_hex_dump( $value ), 0 );
             }
         }
         if (!empty($mla_shortcode_slug)) {
             $tail = strrpos($key, '-style');
             if (!(false === $tail)) {
                 $mla_shortcode_slugs['style'][substr($key, 0, $tail)] = $mla_shortcode_slug;
             } else {
                 $tail = strrpos($key, '-arguments-markup');
                 if (!(false === $tail)) {
                     $mla_shortcode_slugs['markup'][substr($key, 0, $tail)] = $mla_shortcode_slug;
                 }
             }
         }
     }
     //error_log( __LINE__ . " mla_shortcode_slugs = " . var_export( $mla_shortcode_slugs, true ), 0 );
     /*
      * Find the shortcode and template type for array indices
      */
     foreach ($default_templates as $key => $value) {
         $tail = strrpos($key, '-style');
         if (!(false === $tail)) {
             $name = substr($key, 0, $tail);
             if (isset($mla_shortcode_slugs['style'][$name])) {
                 // Assign to the declared shortcode
                 MLATemplate_Support::$mla_custom_templates['style'][$mla_shortcode_slugs['style'][$name]][$name] = $value;
                 $name = NULL;
             } else {
                 // Guess at the shortcode
                 foreach (MLATemplate_Support::$mla_template_definitions['style'] as $slug => $definition) {
                     if (isset($definition['default_names']) && in_array($name, $definition['default_names'])) {
                         MLATemplate_Support::$mla_custom_templates['style'][$definition['slug']][$name] = $value;
                         $name = NULL;
                         break;
                     }
                 }
             }
             // Can't find the shortcode; assume it's mla_gallery
             if ($name) {
                 MLATemplate_Support::$mla_custom_templates['style']['gallery'][$name] = $value;
             }
             continue;
         }
         $tail = strrpos($key, '-markup');
         if (!(false === $tail)) {
             $name = substr($key, 0, $tail);
             // Look for explicit assignment
             foreach ($mla_shortcode_slugs['markup'] as $root_name => $mla_shortcode_slug) {
                 $root = strpos($name, $root_name);
                 if (0 === $root) {
                     $section_name = substr($name, strlen($root_name) + 1);
                     //error_log( __LINE__ . " assign default template {$key}, to [{$mla_shortcode_slug}][{$root_name}][{$section_name}]", 0 );
                     // Assign to the declared shortcode
                     MLATemplate_Support::$mla_custom_templates['markup'][$mla_shortcode_slug][$root_name][$section_name] = $value;
                     $name = NULL;
                     break;
                 }
             }
             if ($name) {
                 // Guess at the shortcode
                 foreach (MLATemplate_Support::$mla_template_definitions['markup'] as $slug => $definition) {
                     if (isset($definition['default_names'])) {
                         foreach ($definition['default_names'] as $default_name) {
                             $root = strpos($name, $default_name);
                             if (0 === $root) {
                                 foreach ($definition['sections'] as $section_name => $section_value) {
                                     $tail = strrpos($name, '-' . $section_name);
                                     if (!(false === $tail)) {
                                         $name = substr($name, 0, $tail);
                                         MLATemplate_Support::$mla_custom_templates['markup'][$definition['slug']][$name][$section_name] = $value;
                                     }
                                 }
                                 $name = NULL;
                                 break;
                             }
                         }
                     }
                 }
             }
             // Can't find the shortcode; assume it's mla_gallery
             if ($name) {
                 foreach (MLATemplate_Support::$mla_template_definitions['markup']['gallery']['sections'] as $section_name => $section_value) {
                     $tail = strrpos($name, '-' . $section_name);
                     if (!(false === $tail)) {
                         $name = substr($name, 0, $tail);
                         MLATemplate_Support::$mla_custom_templates['markup']['gallery'][$name][$section_name] = $value;
                     }
                 }
             }
         }
     }
     /*
      * Add user-defined Style and Markup templates
      *
      * MLA versions before 2.40 will not have separate 'attributes' and 'content' elements,
      * but version 2.3x must "go both ways" to allow for reversions.
      */
     $templates = MLACore::mla_get_option('style_templates');
     if (is_array($templates)) {
         foreach ($templates as $name => $value) {
             // Check for 2.40+ format
             if (is_array($value)) {
                 $attributes = $value['attributes'];
                 MLATemplate_Support::$mla_custom_templates['style'][$attributes['mla_shortcode_slug']][$name] = $value['content'];
                 continue;
             }
             // Check for explicit assignment
             $match_count = preg_match('#\\<!-- mla_shortcode_slug="(.+)" --\\>[\\r\\n]*#', $value, $matches, PREG_OFFSET_CAPTURE);
             if ($match_count > 0) {
                 //error_log( __LINE__ . " default template {$name} matches = " . var_export( $matches, true ), 0 );
                 $value = substr_replace($value, '', $matches[0][1], strlen($matches[0][0]));
                 //error_log( __LINE__ . " replace default template {$name} value = " . MLAData::mla_hex_dump( $value ), 0 );
                 MLATemplate_Support::$mla_custom_templates['style'][$matches[1][0]][$name] = $value;
                 continue;
             }
             // Guess from content
             foreach (MLATemplate_Support::$mla_template_definitions['style'] as $slug => $definition) {
                 if (false !== strpos($value, '.' . $definition['slug'])) {
                     MLATemplate_Support::$mla_custom_templates['style'][$definition['slug']][$name] = $value;
                     $name = NULL;
                     break;
                 }
             }
             if ($name) {
                 MLATemplate_Support::$mla_custom_templates['style']['gallery'][$name] = $value;
             }
         }
         // foreach $templates
     }
     // is_array
     $templates = MLACore::mla_get_option('markup_templates');
     if (is_array($templates)) {
         foreach ($templates as $name => $value) {
             // Check for 2.40+ format
             if (isset($value['attributes'])) {
                 $attributes = $value['attributes'];
                 unset($value['attributes']);
                 MLATemplate_Support::$mla_custom_templates['markup'][$attributes['mla_shortcode_slug']][$name] = $value['content'];
                 continue;
             }
             // Check for explicit assignment
             if (isset($value['arguments'])) {
                 $match_count = preg_match('#mla_shortcode_slug="(.+)"[ \\r\\n]*#', $value['arguments'], $matches, PREG_OFFSET_CAPTURE);
             } else {
                 $match_count = 0;
             }
             if ($match_count > 0) {
                 //error_log( __LINE__ . " user template {$key} matches = " . var_export( $matches, true ), 0 );
                 $value['arguments'] = substr_replace($value['arguments'], '', $matches[0][1], strlen($matches[0][0]));
                 if (empty($value['arguments'])) {
                     //error_log( __LINE__ . " unset user template {$key}", 0 );
                     unset($value['arguments']);
                 } else {
                     //error_log( __LINE__ . " replace user {$key} value = " . MLAData::mla_hex_dump( $value['arguments'] ), 0 );
                 }
                 MLATemplate_Support::$mla_custom_templates['markup'][$matches[1][0]][$name] = $value;
                 continue;
             }
             // Guess from content
             $full_text = '';
             // for guessing shortcode name
             foreach ($value as $section_name => $section_value) {
                 $full_text .= $section_value;
             }
             foreach (MLATemplate_Support::$mla_template_definitions['markup'] as $slug => $definition) {
                 if (preg_match('#class=[\'\\"]*.*' . $definition['slug'] . '#', $full_text, $matches)) {
                     MLATemplate_Support::$mla_custom_templates['markup'][$definition['slug']][$name] = $value;
                     $name = NULL;
                     break;
                 }
             }
             if ($name) {
                 MLATemplate_Support::$mla_custom_templates['markup']['gallery'][$name] = $value;
             }
         }
         // foreach $templates
     }
     // is_array
     //error_log( __LINE__ . ' mla_load_custom_templates MLATemplate_Support::$mla_custom_templates = ' . var_export( MLATemplate_Support::$mla_custom_templates, true ), 0 );
 }