/** * Ajax handler for Media Manager "fill compat-attachment-fields" queries * * Prepares an array of (HTML) taxonomy meta boxes with attachment-specific values. * * @since 2.20 * * @return void passes array of results to wp_send_json_success() for JSON encoding and transmission */ public static function mla_fill_compat_fields_action() { if (empty($_REQUEST['query']) || !($requested = $_REQUEST['query'])) { wp_send_json_error(); } if (empty($_REQUEST['id']) || !($post_id = absint($_REQUEST['id']))) { wp_send_json_error(); } if (NULL == ($post = get_post($post_id))) { wp_send_json_error(); } $results = apply_filters('mla_media_modal_begin_fill_compat_fields', array(), $requested, $post); if (!empty($results)) { wp_send_json_success($results); } /* * Match all supported taxonomies against the requested list */ foreach (get_taxonomies(array('show_ui' => true), 'objects') as $key => $value) { if (MLACore::mla_taxonomy_support($key)) { if (is_integer($index = array_search($key, $requested))) { $request = $requested[$index]; } else { continue; } if (!($use_checklist = $value->hierarchical)) { $use_checklist = MLACore::mla_taxonomy_support($key, 'flat-checklist'); } if ($use_checklist) { if ('checked' == MLACore::mla_get_option(MLACoreOptions::MLA_MEDIA_MODAL_DETAILS_CATEGORY_METABOX)) { unset($requested[$index]); $label = $value->label; $terms = get_object_term_cache($post_id, $key); if (false === $terms) { $terms = wp_get_object_terms($post_id, $key); wp_cache_add($post_id, $terms, $key . '_relationships'); } if (is_wp_error($terms) || empty($terms)) { $terms = array(); } $list = array(); foreach ($terms as $term) { $list[] = $term->term_id; } // foreach $term sort($list); $list = join(',', $list); /* * Simulate the 'add_meta_boxes' callback */ $box = array('id' => $key . 'div', 'title' => $label, 'callback' => 'MLACore::mla_checklist_meta_box', 'args' => array('taxonomy' => $key, 'in_modal' => true)); ob_start(); MLACore::mla_checklist_meta_box($post, $box); $row_content = ob_get_clean(); $row = "\t\t<th class='label' valign='top' scope='row' style='width: 99%;'>\n"; $row .= "\t\t<label for='mla-attachments-{$post_id}-{$key}'>\n"; $row .= "\t\t<span title='" . __('Click to toggle', 'media-library-assistant') . "' class='alignleft' style='width: 99%; text-align: left;'>{$label}</span><br class='clear'>\n"; $row .= "\t\t</label></th>\n"; $row .= "\t\t<td class='field' style='width: 99%; display: none'>\n"; $row .= "\t\t<div class='mla-taxonomy-field'>\n"; $row .= "\t\t<input name='attachments[{$post_id}][{$key}]' class='text' id='mla-attachments-{$post_id}-{$key}' type='hidden' value='{$list}'>\n"; $row .= $row_content; $row .= "\t\t</div>\n"; $row .= "\t\t</td>\n"; $results[$key] = $row; } // checked } else { // flat if ('checked' == MLACore::mla_get_option(MLACoreOptions::MLA_MEDIA_MODAL_DETAILS_TAG_METABOX)) { unset($requested[$index]); $label = $value->label; $terms = get_object_term_cache($post_id, $key); if (false === $terms) { $terms = wp_get_object_terms($post_id, $key); wp_cache_add($post_id, $terms, $key . '_relationships'); } if (is_wp_error($terms) || empty($terms)) { $terms = array(); } $list = array(); foreach ($terms as $term) { $list[] = $term->name; } // foreach $term sort($list); $hidden_list = join(',', $list); $row = "\t\t<th class='label' valign='top' scope='row' style='width: 99%;'>\n"; $row .= "\t\t<label for='mla-attachments-{$post_id}-{$key}'>\n"; $row .= "\t\t<span title='" . __('Click to toggle', 'media-library-assistant') . "' class='alignleft' style='width: 99%; text-align: left;'>{$label}</span><br class='clear'>\n"; $row .= "\t\t</label></th>\n"; $row .= "\t\t<td class='field' style='width: 99%; display: none'>\n"; $row .= "\t\t<div class='mla-taxonomy-field'>\n"; $row .= "\t\t<div class='tagsdiv' id='mla-taxonomy-{$key}'>\n"; $row .= "\t\t<div class='jaxtag'>\n"; $row .= "\t\t<div class='nojs-tags hide-if-js'>\n"; $row .= "\t\t<input name='attachments[{$post_id}][{$key}]' class='the-tags' id='mla-attachments-{$post_id}-{$key}' type='hidden' value='{$hidden_list}'>\n"; $row .= "\t\t<input name='mla_tags[{$post_id}][{$key}]' class='server-tags' id='mla-tags-{$post_id}-{$key}' type='hidden' value='{$hidden_list}'>\n"; $row .= "\t\t</div>\n"; // nojs-tags $row .= "\t\t<div class='ajaxtag'>\n"; $row .= "\t\t<label class='screen-reader-text' for='new-tag-{$key}'>" . __('Tags', 'media-library-assistant') . "</label>\n"; /* translators: %s: add new taxonomy label */ $row .= "\t\t<div class='taghint'>" . sprintf(__('Add New %1$s', 'media-library-assistant'), $label) . "</div>\n"; $row .= "\t\t<p>\n"; $row .= "\t\t<input name='newtag[{$key}]' class='newtag form-input-tip' id='new-tag-{$key}' type='text' size='16' value='' autocomplete='off'>\n"; $row .= "\t\t<input class='button tagadd' type='button' value='Add'>\n"; $row .= "\t\t</p>\n"; $row .= "\t\t</div>\n"; // ajaxtag $row .= "\t\t<p class='howto'>Separate tags with commas</p>\n"; $row .= "\t\t</div>\n"; // jaxtag $row .= "\t\t<div class='tagchecklist'>\n"; foreach ($list as $index => $term) { $row .= "\t\t<span><a class='ntdelbutton' id='post_tag-check-num-{$index}'>X</a> {$term}</span>\n"; } $row .= "\t\t</div>\n"; // tagchecklist $row .= "\t\t</div>\n"; // tagsdiv $row .= "\t\t<p><a class='tagcloud-link' id='mla-link-{$key}' href='#titlediv'>" . __('Choose from the most used tags', 'media-library-assistant') . "</a></p>\n"; $row .= "\t\t</div>\n"; // mla-taxonomy-field $row .= "\t\t</td>\n"; $results[$key] = $row; } // checked } // flat } // is supported } // foreach /* * Any left-over requests are for unsupported taxonomies */ foreach ($requested as $key) { $row = "\t\t<tr class='compat-field-{$key} mla-taxonomy-row'>\n"; $row .= "\t\t<th class='label' valign='top' scope='row'>\n"; $row .= "\t\t<label for='mla-attachments-{$post_id}-{$key}'>\n"; $row .= "\t\t<span title='" . __('Click to toggle', 'media-library-assistant') . "' class='alignleft'>{$label}</span><br class='clear'>\n"; $row .= "\t\t</label></th>\n"; $row .= "\t\t<td class='field' style='display: none'>\n"; $row .= "\t\t<div class='mla-taxonomy-field'>\n"; $row .= "\t\t<input name='attachments[{$post_id}][{$key}]' class='text' id='mla-attachments-{$post_id}-{$key}' type='hidden' value=''>\n"; $row .= "\t\t<div id='taxonomy-{$key}' class='categorydiv'>\n"; $row .= __('Not Supported', 'media-library-assistant') . ".\n"; $row .= "\t\t</div>\n"; $row .= "\t\t</div>\n"; $row .= "\t\t</td>\n"; $row .= "\t\t</tr>\n"; $results[$key] = $row; } wp_send_json_success(apply_filters('mla_media_modal_end_fill_compat_fields', $results, $_REQUEST['query'], $requested, $post)); }