/** * @param array $data * @return bool */ public function updateMediaFields($data = null) { $id = isset($data['id']) ? $data['id'] : $this->id; if (isset($data['id'])) { unset($data['id']); } $errors = false; $post = $_post = get_post($id, ARRAY_A); if (isset($data['post_content'])) { $post['post_content'] = $data['post_content']; } if (isset($data['post_title'])) { $post['post_title'] = $data['post_title']; } if (isset($data['post_excerpt'])) { $post['post_excerpt'] = $data['post_excerpt']; } if (isset($data['menu_order'])) { $post['menu_order'] = $data['menu_order']; } $post = apply_filters('attachment_fields_to_save', $post, $data); if (isset($data['image_alt'])) { $image_alt = get_post_meta($id, '_wp_attachment_image_alt', true); if ($image_alt != stripslashes($data['image_alt'])) { $image_alt = wp_strip_all_tags(stripslashes($data['image_alt']), true); // update_meta expects slashed update_post_meta($id, '_wp_attachment_image_alt', addslashes($image_alt)); } } if (isset($data['url'])) { $image_url = get_post_meta($id, '_gumm_attachment_image_link', true); if (!is_array($image_url) || is_array($image_url && !isset($image_url['url']))) { $image_url = array('url' => '', 'button' => ''); } if (isset($data['link_button'])) { $image_url['button'] = stripslashes($data['link_button']); } if ($image_url['url'] != stripslashes($data['url'])) { $image_url['url'] = addslashes(wp_strip_all_tags(stripslashes($data['url']), true)); $image_url['button'] = addslashes(stripslashes($image_url['button'])); // update_meta expects slashed update_post_meta($id, '_gumm_attachment_image_link', $image_url); } } if (isset($post['errors'])) { $errors[$attachment_id] = $post['errors']; unset($post['errors']); } if ($post != $_post) { wp_update_post($post); } foreach (get_attachment_taxonomies($post) as $t) { if (isset($data[$t])) { wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $data[$t])), $t, false); } } return $errors ? false : true; }
/** * @ticket 37368 */ public function test_should_respect_output_objects() { register_taxonomy('wptests_tax2', 'attachment:image'); $a = self::factory()->attachment->create_object('image.jpg', 0, array('post_mime_type' => 'image/jpeg', 'post_type' => 'attachment')); $attachment = get_post($a); $found = get_attachment_taxonomies($attachment, 'objects'); $this->assertSame(array('wptests_tax2'), array_keys($found)); $this->assertInternalType('object', $found['wptests_tax2']); $this->assertSame('wptests_tax2', $found['wptests_tax2']->name); }
public static function remove_media_fields($form_fields, $attachment) { if (!isset($_GET['post_id'])) { return $form_fields; } $product_id = $_GET['post_id']; if (isset($_GET['tab']) && $_GET['tab'] == 'gallery' && get_post_type($product_id) == 'product' && wp_attachment_is_image($attachment->ID)) { foreach (get_attachment_taxonomies($attachment) as $taxonomy) { if (isset($form_fields[$taxonomy])) { unset($form_fields[$taxonomy]); } } } return $form_fields; }
/** * Return all of the taxonomy names that are of $object_type. * * It appears that this function can be used to find all of the names inside of * $wp_taxonomies global variable. * * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should * result in <code>Array('category', 'post_tag')</code> * * @package WordPress * @subpackage Taxonomy * @since 2.3.0 * * @uses $wp_taxonomies * * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts) * @return array The names of all taxonomy of $object_type. */ function get_object_taxonomies($object) { global $wp_taxonomies; if (is_object($object)) { if ($object->post_type == 'attachment') { return get_attachment_taxonomies($object); } $object = $object->post_type; } $object = (array) $object; $taxonomies = array(); foreach ((array) $wp_taxonomies as $taxonomy) { if (array_intersect($object, (array) $taxonomy->object_type)) { $taxonomies[] = $taxonomy->name; } } return $taxonomies; }
static function attachment_fields_to_save($post, $attachment_data) { if (defined('DOING_AJAX') && DOING_AJAX && isset($_REQUEST['action']) && $_REQUEST['action'] == 'save-attachment-compat' && isset($_REQUEST['id']) && isset($_REQUEST['media_category_nonce']) && wp_verify_nonce($_REQUEST['media_category_nonce'], 'save_attachment_media_categories')) { $attachment_id = intval($_REQUEST['id']); if (empty($attachment_id)) { wp_send_json_error(); } foreach (get_attachment_taxonomies($post) as $taxonomy) { if (isset($_REQUEST['tax_input'][$taxonomy])) { $terms = array_filter(array_map('intval', $_REQUEST['tax_input'][$taxonomy])); wp_set_object_terms($attachment_id, $terms, $taxonomy, false); } else { wp_set_object_terms($attachment_id, array(), $taxonomy, false); } } } return $post; }
/** * Return all of the taxonomy names that are of $object_type. * * It appears that this function can be used to find all of the names inside of * $wp_taxonomies global variable. * * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should * result in <code>Array('category', 'post_tag')</code> * * @package WordPress * @subpackage Taxonomy * @since 2.3.0 * * @uses $wp_taxonomies * * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts) * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default. * @return array The names of all taxonomy of $object_type. */ function get_object_taxonomies($object, $output = 'names') { global $wp_taxonomies; if (is_object($object)) { if ($object->post_type == 'attachment') { return get_attachment_taxonomies($object); } $object = $object->post_type; } $object = (array) $object; $taxonomies = array(); foreach ((array) $wp_taxonomies as $tax_name => $tax_obj) { if (array_intersect($object, (array) $tax_obj->object_type)) { if ('names' == $output) { $taxonomies[] = $tax_name; } else { $taxonomies[$tax_name] = $tax_obj; } } } return $taxonomies; }
/** * Ajax handler for saving backwards compatible attachment attributes. * * @since 3.5.0 */ function wp_ajax_save_attachment_compat() { if (!isset($_REQUEST['id'])) { wp_send_json_error(); } if (!($id = absint($_REQUEST['id']))) { wp_send_json_error(); } if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) { wp_send_json_error(); } $attachment_data = $_REQUEST['attachments'][$id]; check_ajax_referer('update-post_' . $id, 'nonce'); if (!current_user_can('edit_post', $id)) { wp_send_json_error(); } $post = get_post($id, ARRAY_A); if ('attachment' != $post['post_type']) { wp_send_json_error(); } /** This filter is documented in wp-admin/includes/media.php */ $post = apply_filters('attachment_fields_to_save', $post, $attachment_data); if (isset($post['errors'])) { $errors = $post['errors']; // @todo return me and display me! unset($post['errors']); } wp_update_post($post); foreach (get_attachment_taxonomies($post) as $taxonomy) { if (isset($attachment_data[$taxonomy])) { wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false); } } if (!($attachment = wp_prepare_attachment_for_js($id))) { wp_send_json_error(); } wp_send_json_success($attachment); }
function get_compat_media_markup($attachment_id, $args = null) { $post = get_post($attachment_id); $default_args = array('errors' => null, 'in_modal' => false); $user_can_edit = current_user_can('edit_post', $attachment_id); $args = wp_parse_args($args, $default_args); /** This filter is documented in wp-admin/includes/media.php */ $args = apply_filters('get_media_item_args', $args); $form_fields = array(); if ($args['in_modal']) { foreach (get_attachment_taxonomies($post) as $taxonomy) { $t = (array) get_taxonomy($taxonomy); if (!$t['public'] || !$t['show_ui']) { continue; } if (empty($t['label'])) { $t['label'] = $taxonomy; } if (empty($t['args'])) { $t['args'] = array(); } $terms = get_object_term_cache($post->ID, $taxonomy); if (false === $terms) { $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); } $values = array(); foreach ($terms as $term) { $values[] = $term->slug; } $t['value'] = join(', ', $values); $t['taxonomy'] = true; $form_fields[$taxonomy] = $t; } } // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing ) $form_fields = array_merge_recursive($form_fields, (array) $args['errors']); /** This filter is documented in wp-admin/includes/media.php */ $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post); unset($form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'], $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'], $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url']); /** This filter is documented in wp-admin/includes/media.php */ $media_meta = apply_filters('media_meta', '', $post); $defaults = array('input' => 'text', 'required' => false, 'value' => '', 'extra_rows' => array(), 'show_in_edit' => true, 'show_in_modal' => true); $hidden_fields = array(); $item = ''; foreach ($form_fields as $id => $field) { if ($id[0] == '_') { continue; } $name = "attachments[{$attachment_id}][{$id}]"; $id_attr = "attachments-{$attachment_id}-{$id}"; if (!empty($field['tr'])) { $item .= $field['tr']; continue; } $field = array_merge($defaults, $field); if (!$field['show_in_edit'] && !$args['in_modal'] || !$field['show_in_modal'] && $args['in_modal']) { continue; } if ($field['input'] == 'hidden') { $hidden_fields[$name] = $field['value']; continue; } $readonly = !$user_can_edit && !empty($field['taxonomy']) ? " readonly='readonly' " : ''; $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : ''; $aria_required = $field['required'] ? " aria-required='true' " : ''; $class = 'compat-field-' . $id; $class .= $field['required'] ? ' form-required' : ''; $item .= "\t\t<tr class='{$class}'>"; $item .= "\t\t\t<th scope='row' class='label'><label for='{$id_attr}'><span class='alignleft'>{$field['label']}</span>{$required}<br class='clear' /></label>"; $item .= "</th>\n\t\t\t<td class='field'>"; if (!empty($field[$field['input']])) { $item .= $field[$field['input']]; } elseif ($field['input'] == 'textarea') { if ('post_content' == $id && user_can_richedit()) { // sanitize_post() skips the post_content when user_can_richedit. $field['value'] = htmlspecialchars($field['value'], ENT_QUOTES); } $item .= "<textarea id='{$id_attr}' name='{$name}' {$aria_required}>" . $field['value'] . '</textarea>'; } else { $item .= "<input type='text' class='text' id='{$id_attr}' name='{$name}' value='" . esc_attr($field['value']) . "' {$readonly} {$aria_required} />"; } if (!empty($field['helps'])) { $item .= "<p class='help'>" . join("</p>\n<p class='help'>", array_unique((array) $field['helps'])) . '</p>'; } $item .= "</td>\n\t\t</tr>\n"; $extra_rows = array(); if (!empty($field['errors'])) { foreach (array_unique((array) $field['errors']) as $error) { $extra_rows['error'][] = $error; } } if (!empty($field['extra_rows'])) { foreach ($field['extra_rows'] as $class => $rows) { foreach ((array) $rows as $html) { $extra_rows[$class][] = $html; } } } foreach ($extra_rows as $class => $rows) { foreach ($rows as $html) { $item .= "\t\t<tr><td></td><td class='{$class}'>{$html}</td></tr>\n"; } } } if (!empty($form_fields['_final'])) { $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n"; } if ($item) { $item = '<table class="compat-attachment-fields">' . $item . '</table>'; } foreach ($hidden_fields as $hidden_field => $value) { $item .= '<input type="hidden" name="' . esc_attr($hidden_field) . '" value="' . esc_attr($value) . '" />' . "\n"; } if ($item) { $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr($post->menu_order) . '" />' . $item; } return array('item' => $item, 'meta' => $media_meta); }
/** * {@internal Missing Short Description}} * * @since unknown * * @param unknown_type $post * @param unknown_type $errors * @return unknown */ function get_attachment_fields_to_edit($post, $errors = null) { if (is_int($post)) { $post =& get_post($post); } if (is_array($post)) { $post = (object) $post; } $image_url = wp_get_attachment_url($post->ID); $edit_post = sanitize_post($post, 'edit'); $form_fields = array('post_title' => array('label' => __('Title'), 'value' => $edit_post->post_title), 'post_excerpt' => array('label' => __('Caption'), 'value' => $edit_post->post_excerpt), 'post_content' => array('label' => __('Description'), 'value' => $edit_post->post_content, 'input' => 'textarea'), 'url' => array('label' => __('Link URL'), 'input' => 'html', 'html' => image_link_input_fields($post, get_option('image_default_link_type')), 'helps' => __('Enter a link URL or click above for presets.')), 'menu_order' => array('label' => __('Order'), 'value' => $edit_post->menu_order), 'image_url' => array('label' => __('File URL'), 'input' => 'html', 'html' => "<input type='text' class='urlfield' readonly='readonly' name='attachments[{$post->ID}][url]' value='" . esc_attr($image_url) . "' /><br />", 'value' => isset($edit_post->post_url) ? $edit_post->post_url : '', 'helps' => __('Location of the uploaded file.'))); foreach (get_attachment_taxonomies($post) as $taxonomy) { $t = (array) get_taxonomy($taxonomy); if (empty($t['label'])) { $t['label'] = $taxonomy; } if (empty($t['args'])) { $t['args'] = array(); } $terms = get_object_term_cache($post->ID, $taxonomy); if (empty($terms)) { $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); } $values = array(); foreach ($terms as $term) { $values[] = $term->name; } $t['value'] = join(', ', $values); $form_fields[$taxonomy] = $t; } // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing ) $form_fields = array_merge_recursive($form_fields, (array) $errors); $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post); return $form_fields; }
function get_attachment_fields_to_edit($post, $errors = null) { if (is_int($post)) { $post =& get_post($post); } if (is_array($post)) { $post = (object) $post; } $edit_post = sanitize_post($post, 'edit'); $file = wp_get_attachment_url($post->ID); $link = get_attachment_link($post->ID); $form_fields = array('post_title' => array('label' => __('Title'), 'value' => $edit_post->post_title), 'post_excerpt' => array('label' => __('Caption'), 'value' => $edit_post->post_excerpt), 'post_content' => array('label' => __('Description'), 'value' => $edit_post->post_content, 'input' => 'textarea'), 'url' => array('label' => __('Link URL'), 'input' => 'html', 'html' => "\n\t\t\t\t<input type='text' name='attachments[{$post->ID}][url]' value='" . attribute_escape($file) . "' /><br />\n\t\t\t\t<button type='button' class='button url-{$post->ID}' value=''>" . __('None') . "</button>\n\t\t\t\t<button type='button' class='button url-{$post->ID}' value='" . attribute_escape($file) . "'>" . __('File URL') . "</button>\n\t\t\t\t<button type='button' class='button url-{$post->ID}' value='" . attribute_escape($link) . "'>" . __('Post URL') . "</button>\n\t\t\t\t<script type='text/javascript'>\n\t\t\t\tjQuery('button.url-{$post->ID}').bind('click', function(){jQuery(this).siblings('input').val(this.value);});\n\t\t\t\t</script>\n", 'helps' => __('Enter a link URL or click above for presets.'))); foreach (get_attachment_taxonomies($post) as $taxonomy) { $t = (array) get_taxonomy($taxonomy); if (empty($t['label'])) { $t['label'] = $taxonomy; } if (empty($t['args'])) { $t['args'] = array(); } $terms = get_object_term_cache($post->ID, $taxonomy); if (empty($terms)) { $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); } $values = array(); foreach ($terms as $term) { $values[] = $term->name; } $t['value'] = join(', ', $values); $form_fields[$taxonomy] = $t; } // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing ) $form_fields = array_merge_recursive($form_fields, (array) $errors); $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post); return $form_fields; }
/** Add category checkboxes to attachment details on insert media popup */ function wpmediacategory_attachment_fields_to_edit($form_fields, $post) { foreach (get_attachment_taxonomies($post->ID) as $taxonomy) { $terms = get_object_term_cache($post->ID, $taxonomy); $t = (array) get_taxonomy($taxonomy); if (!$t['public'] || !$t['show_ui']) { continue; } if (empty($t['label'])) { $t['label'] = $taxonomy; } if (empty($t['args'])) { $t['args'] = array(); } if (false === $terms) { $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); } $values = array(); foreach ($terms as $term) { $values[] = $term->slug; } $t['value'] = join(', ', $values); $t['show_in_edit'] = false; if ($t['hierarchical']) { ob_start(); wp_terms_checklist($post->ID, array('taxonomy' => $taxonomy, 'checked_ontop' => false, 'walker' => new wpmediacategory_walker_media_taxonomy_checklist())); if (ob_get_contents() != false) { $html = '<ul class="term-list">' . ob_get_contents() . '</ul>'; } else { $html = '<ul class="term-list"><li>No ' . $t['label'] . '</li></ul>'; } ob_end_clean(); $t['input'] = 'html'; $t['html'] = $html; } $form_fields[$taxonomy] = $t; } return $form_fields; }