/** * Process bulk action for one or more attachments * * @since 2.00 * * @param string Bulk action slug: delete, edit, restore, trash, custom action * @param array Form elements, e.g., from $_REQUEST * * @return array messages and page content: ( 'message', 'body', 'unchanged', 'success', 'failure', 'item_results' ) */ public static function mla_process_bulk_action($bulk_action, $request = NULL) { $page_content = array('message' => '', 'body' => '', 'unchanged' => 0, 'success' => 0, 'failure' => 0, 'item_results' => array()); $custom_field_map = MLAOptions::mla_custom_field_support('bulk_edit'); /* * do_cleanup will remove the bulk edit elements from the $_REQUEST super array. * It is passed in the $request so it can be filtered. */ if (NULL == $request) { $request = $_REQUEST; $request['mla_bulk_action_do_cleanup'] = true; } else { $request['mla_bulk_action_do_cleanup'] = false; } $request = apply_filters('mla_list_table_bulk_action_initial_request', $request, $bulk_action, $custom_field_map); self::mla_debug_add('mla_process_bulk_action $request = ' . var_export($request, true), MLA::MLA_DEBUG_CATEGORY_AJAX); if (isset($request['cb_attachment'])) { $item_content = apply_filters('mla_list_table_begin_bulk_action', NULL, $bulk_action); if (is_null($item_content)) { $prevent_default = false; } else { $prevent_default = isset($item_content['prevent_default']) ? $item_content['prevent_default'] : false; } if ($prevent_default) { if (isset($item_content['message'])) { $page_content['message'] = $item_content['message']; } if (isset($item_content['body'])) { $page_content['body'] = $item_content['body']; } return $page_content; } if (!empty($request['bulk_custom_field_map'])) { do_action('mla_begin_mapping', 'bulk_custom', NULL); } elseif (!empty($request['bulk_map'])) { do_action('mla_begin_mapping', 'bulk_iptc_exif', NULL); } foreach ($request['cb_attachment'] as $index => $post_id) { if (!current_user_can('edit_post', $post_id)) { $page_content['message'] .= __('ERROR', 'media-library-assistant') . ': ' . __('You are not allowed to edit Attachment: ', 'media-library-assistant') . $post_id . '<br>'; continue; } $request = apply_filters('mla_list_table_bulk_action_item_request', $request, $bulk_action, $post_id, $custom_field_map); $item_content = apply_filters('mla_list_table_bulk_action', NULL, $bulk_action, $post_id); if (is_null($item_content)) { $prevent_default = false; $custom_message = ''; } else { $prevent_default = isset($item_content['prevent_default']) ? $item_content['prevent_default'] : false; $custom_message = isset($item_content['message']) ? $item_content['message'] : ''; } if (!$prevent_default) { switch ($bulk_action) { case 'delete': $item_content = self::_delete_single_item($post_id); break; case 'edit': if (!empty($request['bulk_custom_field_map'])) { $updates = MLAOptions::mla_evaluate_custom_field_mapping($post_id, 'single_attachment_mapping'); $item_content = MLAData::mla_update_single_item($post_id, $updates); break; } if (!empty($request['bulk_map'])) { $item = get_post($post_id); $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($item, 'iptc_exif_mapping'); $item_content = MLAData::mla_update_single_item($post_id, $updates); break; } $new_data = self::mla_prepare_bulk_edits($post_id, $request, $custom_field_map); self::mla_debug_add("mla_process_bulk_action( {$post_id} ) new_data = " . var_export($new_data, true), MLA::MLA_DEBUG_CATEGORY_AJAX); $tax_input = $new_data['tax_input']; $tax_action = $new_data['tax_action']; unset($new_data['tax_input']); unset($new_data['tax_action']); $item_content = MLAData::mla_update_single_item($post_id, $new_data, $tax_input, $tax_action); self::mla_debug_add("mla_process_bulk_action( {$post_id} ) item_content = " . var_export($item_content, true), MLA::MLA_DEBUG_CATEGORY_AJAX); break; case 'restore': $item_content = self::_restore_single_item($post_id); break; case 'trash': $item_content = self::_trash_single_item($post_id); break; default: $item_content = apply_filters('mla_list_table_custom_bulk_action', NULL, $bulk_action, $post_id); if (is_null($item_content)) { $prevent_default = false; /* translators: 1: ERROR tag 2: bulk action */ $custom_message = sprintf(__('%1$s: Unknown bulk action %2$s', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $bulk_action); } else { $prevent_default = isset($item_content['prevent_default']) ? $item_content['prevent_default'] : false; } } // switch $bulk_action } // ! $prevent_default // Custom action can set $prevent_default, so test again. if (!$prevent_default) { if (!empty($custom_message)) { $no_changes = sprintf(__('Item %1$d, no changes detected.', 'media-library-assistant'), $post_id); if ($no_changes == $item_content['message']) { $item_content['message'] = $custom_message; } else { $item_content['message'] = $custom_message . '<br>' . $item_content['message']; } } $page_content['item_results'][$post_id] = array('result' => 'unknown', 'message' => $item_content['message']); if (!empty($item_content['message'])) { $page_content['message'] .= $item_content['message'] . '<br>'; if (false !== strpos($item_content['message'], __('no changes detected', 'media-library-assistant'))) { $page_content['unchanged'] += 1; $page_content['item_results'][$post_id]['result'] = 'unchanged'; } elseif (false !== strpos($item_content['message'], __('ERROR', 'media-library-assistant'))) { $page_content['failure'] += 1; $page_content['item_results'][$post_id]['result'] = 'failure'; } else { $page_content['success'] += 1; $page_content['item_results'][$post_id]['result'] = 'success'; } } } // ! $prevent_default } // foreach cb_attachment if (!empty($request['bulk_custom_field_map']) || !empty($request['bulk_map'])) { do_action('mla_end_mapping'); } $item_content = apply_filters('mla_list_table_end_bulk_action', NULL, $bulk_action); if (isset($item_content['message'])) { $page_content['message'] .= $item_content['message']; } if (isset($item_content['body'])) { $page_content['body'] = $item_content['body']; } if ($request['mla_bulk_action_do_cleanup']) { unset($_REQUEST['post_title']); unset($_REQUEST['post_excerpt']); unset($_REQUEST['post_content']); unset($_REQUEST['image_alt']); unset($_REQUEST['comment_status']); unset($_REQUEST['ping_status']); unset($_REQUEST['post_parent']); unset($_REQUEST['post_author']); unset($_REQUEST['tax_input']); unset($_REQUEST['tax_action']); foreach (MLAOptions::mla_custom_field_support('bulk_edit') as $slug => $details) { unset($_REQUEST[$slug]); } unset($_REQUEST['cb_attachment']); } } else { // isset cb_attachment /* translators: 1: action name, e.g., edit */ $page_content['message'] = sprintf(__('Bulk Action %1$s - no items selected.', 'media-library-assistant'), $bulk_action); } if ($request['mla_bulk_action_do_cleanup']) { unset($_REQUEST['action']); unset($_REQUEST['bulk_custom_field_map']); unset($_REQUEST['bulk_map']); unset($_REQUEST['bulk_edit']); unset($_REQUEST['action2']); } return $page_content; }
/** * Perform IPTC/EXIF and Custom Field mapping on just-inserted attachment * * This filter tests the $add_attachment_id variable set by the mla_add_attachment_action * to ensure that mapping is only performed for new additions, not metadata updates. * * @since 1.10 * * @param array Attachment metadata for just-inserted attachment * @param integer ID of just-inserted attachment * * @return array Updated attachment metadata */ public static function mla_update_attachment_metadata_filter($data, $post_id) { $options = array(); $options['is_upload'] = MLAOptions::$add_attachment_id == $post_id; MLAOptions::$add_attachment_id = 0; $options['enable_iptc_exif_mapping'] = 'checked' == MLACore::mla_get_option('enable_iptc_exif_mapping'); $options['enable_custom_field_mapping'] = 'checked' == MLACore::mla_get_option('enable_custom_field_mapping'); $options['enable_iptc_exif_update'] = 'checked' == MLACore::mla_get_option('enable_iptc_exif_update'); $options['enable_custom_field_update'] = 'checked' == MLACore::mla_get_option('enable_custom_field_update'); $options = apply_filters('mla_update_attachment_metadata_options', $options, $data, $post_id); $data = apply_filters('mla_update_attachment_metadata_prefilter', $data, $post_id, $options); if ($options['is_upload']) { if ($options['enable_iptc_exif_mapping'] || $options['enable_custom_field_mapping']) { do_action('mla_begin_mapping', 'create_metadata', $post_id); } if ($options['enable_iptc_exif_mapping']) { $item = get_post($post_id); $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($item, 'iptc_exif_mapping', NULL, $data, true); $updates = MLAOptions::_update_attachment_metadata($updates, $data); if (!empty($updates)) { $item_content = MLAData::mla_update_single_item($post_id, $updates); } } if ($options['enable_custom_field_mapping']) { $updates = MLAOptions::mla_evaluate_custom_field_mapping($post_id, 'single_attachment_mapping', NULL, $data); $updates = MLAOptions::_update_attachment_metadata($updates, $data); if (!empty($updates)) { $item_content = MLAData::mla_update_single_item($post_id, $updates); } } if ($options['enable_iptc_exif_mapping'] || $options['enable_custom_field_mapping']) { do_action('mla_end_mapping'); } } else { if ($options['enable_iptc_exif_update'] || $options['enable_custom_field_update']) { do_action('mla_begin_mapping', 'update_metadata', $post_id); } if ($options['enable_iptc_exif_update']) { $item = get_post($post_id); $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($item, 'iptc_exif_mapping', NULL, $data); $updates = MLAOptions::_update_attachment_metadata($updates, $data); if (!empty($updates)) { $item_content = MLAData::mla_update_single_item($post_id, $updates); } } if ($options['enable_custom_field_update']) { $updates = MLAOptions::mla_evaluate_custom_field_mapping($post_id, 'single_attachment_mapping', NULL, $data); $updates = MLAOptions::_update_attachment_metadata($updates, $data); if (!empty($updates)) { $item_content = MLAData::mla_update_single_item($post_id, $updates); } } if ($options['enable_iptc_exif_update'] || $options['enable_custom_field_update']) { do_action('mla_end_mapping'); } } $data = apply_filters('mla_update_attachment_metadata_postfilter', $data, $post_id, $options); return $data; }
/** * Render the "Assistant" subpage in the Media section, using the list_table package * * @since 0.1 * * @return void */ public static function mla_render_admin_page() { /* * WordPress class-wp-list-table.php doesn't look in hidden fields to set * the month filter dropdown or sorting parameters */ if (isset($_REQUEST['m'])) { $_GET['m'] = $_REQUEST['m']; } if (isset($_REQUEST['order'])) { $_GET['order'] = $_REQUEST['order']; } if (isset($_REQUEST['orderby'])) { $_GET['orderby'] = $_REQUEST['orderby']; } $bulk_action = self::_current_bulk_action(); $page_title = MLAOptions::mla_get_option(MLAOptions::MLA_SCREEN_PAGE_TITLE); echo "<div class=\"wrap\">\r\n"; echo "<div id=\"icon-upload\" class=\"icon32\"><br/></div>\r\n"; echo "<h2>{$page_title}"; // trailing </h2> is action-specific if (!current_user_can('upload_files')) { echo " - Error</h2>\r\n"; wp_die(__('You do not have permission to manage attachments.')); } $page_content = array('message' => '', 'body' => ''); /* * The category taxonomy (edit screens) is a special case because * post_categories_meta_box() changes the input name */ if (!isset($_REQUEST['tax_input'])) { $_REQUEST['tax_input'] = array(); } if (isset($_REQUEST['post_category'])) { $_REQUEST['tax_input']['category'] = $_REQUEST['post_category']; unset($_REQUEST['post_category']); } /* * Process bulk actions that affect an array of items */ if ($bulk_action && $bulk_action != 'none') { if (isset($_REQUEST['cb_attachment'])) { foreach ($_REQUEST['cb_attachment'] as $index => $post_id) { switch ($bulk_action) { case 'delete': $item_content = self::_delete_single_item($post_id); break; case 'edit': if (!empty($_REQUEST['bulk_custom_field_map'])) { $updates = MLAOptions::mla_evaluate_custom_field_mapping($post_id, 'single_attachment_mapping'); $item_content = MLAData::mla_update_single_item($post_id, $updates); break; } if (!empty($_REQUEST['bulk_map'])) { $item = get_post($post_id); $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($item, 'iptc_exif_mapping'); $item_content = MLAData::mla_update_single_item($post_id, $updates); break; } /* * Copy the edit form contents to $new_data */ $new_data = array(); if (isset($_REQUEST['post_parent'])) { if (is_numeric($_REQUEST['post_parent'])) { $new_data['post_parent'] = $_REQUEST['post_parent']; } } if (isset($_REQUEST['post_author'])) { if (-1 != $_REQUEST['post_author']) { $new_data['post_author'] = $_REQUEST['post_author']; } } /* * Custom field support */ $custom_fields = array(); foreach (MLAOptions::mla_custom_field_support('bulk_edit') as $slug => $label) { if (isset($_REQUEST[$slug])) { if (!empty($_REQUEST[$slug])) { $custom_fields[$label] = $_REQUEST[$slug]; } } } // foreach if (!empty($custom_fields)) { $new_data['custom_updates'] = $custom_fields; } $item_content = MLAData::mla_update_single_item($post_id, $new_data, $_REQUEST['tax_input'], $_REQUEST['tax_action']); break; case 'restore': $item_content = self::_restore_single_item($post_id); break; //case 'tag': //case 'tag': case 'trash': $item_content = self::_trash_single_item($post_id); break; default: $item_content = array('message' => sprintf('Unknown bulk action %s', $bulk_action), 'body' => ''); } // switch $bulk_action $page_content['message'] .= $item_content['message'] . '<br>'; } // foreach cb_attachment unset($_REQUEST['post_parent']); unset($_REQUEST['post_author']); unset($_REQUEST['tax_input']); unset($_REQUEST['tax_action']); foreach (MLAOptions::mla_custom_field_support('bulk_edit') as $slug => $label) { unset($_REQUEST[$slug]); } unset($_REQUEST['cb_attachment']); } else { $page_content['message'] = 'Bulk Action ' . $bulk_action . ' - no items selected.'; } unset($_REQUEST['action']); unset($_REQUEST['bulk_edit']); unset($_REQUEST['action2']); } // $bulk_action if (isset($_REQUEST['clear_filter_by'])) { unset($_REQUEST['heading_suffix']); unset($_REQUEST['parent']); unset($_REQUEST['author']); unset($_REQUEST['mla-tax']); unset($_REQUEST['mla-term']); unset($_REQUEST['mla-metakey']); unset($_REQUEST['mla-metavalue']); } if (isset($_REQUEST['delete_all'])) { global $wpdb; $ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type=%s AND post_status = %s", 'attachment', 'trash')); $delete_count = 0; foreach ($ids as $post_id) { $item_content = self::_delete_single_item($post_id); if (false !== strpos($item_content['message'], 'ERROR:')) { $page_content['message'] .= $item_content['message'] . '<br>'; } else { $delete_count++; } } if ($delete_count) { $page_content['message'] .= sprintf(_nx('%s item deleted.', '%s items deleted.', $delete_count, 'deleted items'), number_format_i18n($delete_count)); } else { $page_content['message'] .= 'No items deleted.'; } } /* * Process row-level actions that affect a single item */ if (!empty($_REQUEST['mla_admin_action'])) { check_admin_referer(self::MLA_ADMIN_NONCE); switch ($_REQUEST['mla_admin_action']) { case self::MLA_ADMIN_SINGLE_DELETE: $page_content = self::_delete_single_item($_REQUEST['mla_item_ID']); break; case self::MLA_ADMIN_SINGLE_EDIT_DISPLAY: echo " - Edit single item</h2>"; $page_content = self::_display_single_item($_REQUEST['mla_item_ID']); break; case self::MLA_ADMIN_SINGLE_EDIT_UPDATE: if (!empty($_REQUEST['update'])) { $page_content = MLAData::mla_update_single_item($_REQUEST['mla_item_ID'], $_REQUEST['attachments'][$_REQUEST['mla_item_ID']], $_REQUEST['tax_input']); } elseif (!empty($_REQUEST['map-iptc-exif'])) { $item = get_post($_REQUEST['mla_item_ID']); $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($item, 'iptc_exif_mapping'); $page_content = MLAData::mla_update_single_item($_REQUEST['mla_item_ID'], $updates); } else { $page_content = array('message' => 'Item: ' . $_REQUEST['mla_item_ID'] . ' cancelled.', 'body' => ''); } break; case self::MLA_ADMIN_SINGLE_RESTORE: $page_content = self::_restore_single_item($_REQUEST['mla_item_ID']); break; case self::MLA_ADMIN_SINGLE_TRASH: $page_content = self::_trash_single_item($_REQUEST['mla_item_ID']); break; default: $page_content = array('message' => sprintf('Unknown mla_admin_action - "%1$s"', $_REQUEST['mla_admin_action']), 'body' => ''); break; } // switch ($_REQUEST['mla_admin_action']) } // (!empty($_REQUEST['mla_admin_action']) if (!empty($page_content['body'])) { if (!empty($page_content['message'])) { if (false !== strpos($page_content['message'], 'ERROR:')) { $messages_class = 'mla_errors'; } else { $messages_class = 'mla_messages'; } echo " <div class=\"{$messages_class}\"><p>\r\n"; echo ' ' . $page_content['message'] . "\r\n"; echo " </p></div>\r\n"; // id="message" } echo $page_content['body']; } else { /* * Display Attachments list */ if (!empty($_REQUEST['heading_suffix'])) { echo ' - ' . esc_html($_REQUEST['heading_suffix']) . "</h2>\r\n"; } elseif (!empty($_REQUEST['s']) && !empty($_REQUEST['mla_search_fields'])) { echo ' - search results for "' . esc_html(stripslashes(trim($_REQUEST['s']))) . "\"</h2>\r\n"; } else { echo "</h2>\r\n"; } if (!empty($page_content['message'])) { if (false !== strpos($page_content['message'], 'ERROR:')) { $messages_class = 'mla_errors'; } else { $messages_class = 'mla_messages'; } echo " <div class=\"{$messages_class}\"><p>\r\n"; echo ' ' . $page_content['message'] . "\r\n"; echo " </p></div>\r\n"; // id="message" } /* * Optional - limit width of the views list */ $view_width = MLAOptions::mla_get_option(MLAOptions::MLA_TABLE_VIEWS_WIDTH); if (!empty($view_width)) { if (is_numeric($view_width)) { $view_width .= 'px'; } echo " <style type='text/css'>\r\n"; echo " ul.subsubsub {\r\n"; echo " width: {$view_width};\r\n"; echo " max-width: {$view_width};\r\n"; echo " }\r\n"; echo " </style>\r\n"; } // Create an instance of our package class... $MLAListTable = new MLA_List_Table(); // Fetch, prepare, sort, and filter our data... $MLAListTable->prepare_items(); $MLAListTable->views(); // Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions // echo '<form action="' . admin_url( 'upload.php' ) . '" method="get" id="mla-filter">' . "\r\n"; echo '<form action="' . admin_url('upload.php?page=' . self::ADMIN_PAGE_SLUG) . '" method="post" id="mla-filter">' . "\r\n"; // echo '<form action="' . admin_url( 'upload.php?page=' . self::ADMIN_PAGE_SLUG ) . '" method="get" id="mla-filter">' . "\r\n"; /* * Compose the Search Media box */ if (!empty($_REQUEST['s']) && !empty($_REQUEST['mla_search_fields'])) { $search_value = esc_attr(stripslashes(trim($_REQUEST['s']))); $search_fields = $_REQUEST['mla_search_fields']; $search_connector = $_REQUEST['mla_search_connector']; } else { $search_value = ''; $search_fields = array('title', 'content'); $search_connector = 'AND'; } echo '<p class="search-box">' . "\r\n"; echo '<label class="screen-reader-text" for="media-search-input">Search Media:</label>' . "\r\n"; echo '<input type="text" size="45" id="media-search-input" name="s" value="' . $search_value . '" />' . "\r\n"; echo '<input type="submit" name="mla-search-submit" id="search-submit" class="button" value="Search Media" /><br>' . "\r\n"; if ('OR' == $search_connector) { echo '<input type="radio" name="mla_search_connector" value="AND" /> and ' . "\r\n"; echo '<input type="radio" name="mla_search_connector" checked="checked" value="OR" /> or ' . "\r\n"; } else { echo '<input type="radio" name="mla_search_connector" checked="checked" value="AND" /> and ' . "\r\n"; echo '<input type="radio" name="mla_search_connector" value="OR" /> or ' . "\r\n"; } if (in_array('title', $search_fields)) { echo '<input type="checkbox" name="mla_search_fields[]" id="search-title" checked="checked" value="title" /> title ' . "\r\n"; } else { echo '<input type="checkbox" name="mla_search_fields[]" id="search-title" value="title" /> title ' . "\r\n"; } if (in_array('name', $search_fields)) { echo '<input type="checkbox" name="mla_search_fields[]" id="search-name" checked="checked" value="name" /> name ' . "\r\n"; } else { echo '<input type="checkbox" name="mla_search_fields[]" id="search-name" value="name" /> name ' . "\r\n"; } if (in_array('alt-text', $search_fields)) { echo '<input type="checkbox" name="mla_search_fields[]" id="search-alt-text" checked="checked" value="alt-text" /> ALT text ' . "\r\n"; } else { echo '<input type="checkbox" name="mla_search_fields[]" id="search-alt-text" value="alt-text" /> ALT text ' . "\r\n"; } if (in_array('excerpt', $search_fields)) { echo '<input type="checkbox" name="mla_search_fields[]" id="search-excerpt" checked="checked" value="excerpt" /> caption ' . "\r\n"; } else { echo '<input type="checkbox" name="mla_search_fields[]" id="search-excerpt" value="excerpt" /> caption ' . "\r\n"; } if (in_array('content', $search_fields)) { echo '<input type="checkbox" name="mla_search_fields[]" id="search-content" checked="checked" value="content" /> description ' . "\r\n"; } else { echo '<input type="checkbox" name="mla_search_fields[]" id="search-content" value="content" /> description ' . "\r\n"; } echo '</p>' . "\r\n"; /* * We also need to ensure that the form posts back to our current page and remember all the view arguments */ echo sprintf('<input type="hidden" name="page" value="%1$s" />', $_REQUEST['page']) . "\r\n"; $view_arguments = MLA_List_Table::mla_submenu_arguments(); foreach ($view_arguments as $key => $value) { if ('meta_query' == $key) { $value = stripslashes($_REQUEST['meta_query']); } /* * Search box elements are already set up in the above "search-box" */ if (in_array($key, array('s', 'mla_search_connector', 'mla_search_fields'))) { continue; } if (is_array($value)) { foreach ($value as $element_key => $element_value) { echo sprintf('<input type="hidden" name="%1$s[%2$s]" value="%3$s" />', $key, $element_key, esc_attr($element_value)) . "\r\n"; } } else { echo sprintf('<input type="hidden" name="%1$s" value="%2$s" />', $key, esc_attr($value)) . "\r\n"; } } // Now we can render the completed list table $MLAListTable->display(); echo "</form><!-- id=mla-filter -->\r\n"; /* * Insert the hidden form and table for inline edits (quick & bulk) */ echo self::_build_inline_edit_form($MLAListTable); echo "<div id=\"ajax-response\"></div>\r\n"; echo "<br class=\"clear\" />\r\n"; echo "</div><!-- class=wrap -->\r\n"; } // display attachments list }
/** * Process IPTC/EXIF custom field settings against all image attachments * without saving the settings to the mla_option * * @since 1.00 * * @uses $_REQUEST if passed a NULL parameter * * @param array | NULL specific iptc_exif_custom_mapping values * @param integer offset for chunk mapping * @param integer length for chunk mapping * * @return array Message(s) reflecting the results of the operation */ private static function _process_iptc_exif_custom($settings = NULL, $offset = 0, $length = 0) { if (NULL == $settings) { $source = 'iptc_exif_custom'; $settings = isset($_REQUEST['iptc_exif_mapping']) ? stripslashes_deep($_REQUEST['iptc_exif_mapping']) : array(); if (isset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_FIELD])) { unset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_FIELD]); } if (isset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_RULE])) { unset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_RULE]); } } else { $source = 'iptc_exif_custom_rule'; $settings = stripslashes_deep($settings); } if (empty($settings['custom'])) { return array('message' => sprintf(__('%1$s: No %2$s settings to process.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), __('Custom field', 'media-library-assistant')), 'body' => ''); } $examine_count = 0; $update_count = 0; $query = array('orderby' => 'none', 'post_parent' => 'all', 'post_mime_type' => 'image,application/*pdf*'); if ($length > 0) { $query['numberposts'] = $length; $query['offset'] = $offset; } $posts = MLAShortcodes::mla_get_shortcode_attachments(0, $query); if (is_string($posts)) { return array('message' => $posts, 'body' => ''); } do_action('mla_begin_mapping', $source, NULL); foreach ($posts as $key => $post) { $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($post, 'iptc_exif_custom_mapping', $settings); $examine_count += 1; if (!empty($updates)) { $results = MLAData::mla_update_single_item($post->ID, $updates); if (stripos($results['message'], __('updated.', 'media-library-assistant'))) { $update_count += 1; } } } // foreach post do_action('mla_end_mapping'); if ($update_count) { /* translators: 1: field type 2: examined count 3: updated count */ $message = sprintf(__('%1$s mapping completed; %2$d attachment(s) examined, %3$d updated.'), 'IPTC/EXIF ' . __('Custom field', 'media-library-assistant'), $examine_count, $update_count) . "\r\n"; } else { /* translators: 1: field type 2: examined count */ $message = sprintf(__('%1$s mapping completed; %2$d attachment(s) examined, no changes detected.'), 'IPTC/EXIF ' . __('Custom field', 'media-library-assistant'), $examine_count) . "\r\n"; } return array('message' => $message, 'body' => '', 'processed' => $examine_count, 'unchanged' => $examine_count - $update_count, 'success' => $update_count); }
/** * Process IPTC/EXIF custom field settings against all image attachments * without saving the settings to the mla_option * * @since 1.00 * * @uses $_REQUEST if passed a NULL parameter * * @param array | NULL specific iptc_exif_custom_mapping values * * @return array Message(s) reflecting the results of the operation */ private static function _process_iptc_exif_custom($settings = NULL) { if (NULL == $settings) { $settings = isset($_REQUEST['iptc_exif_mapping']) ? $_REQUEST['iptc_exif_mapping'] : array(); if (isset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_FIELD])) { unset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_FIELD]); } if (isset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_RULE])) { unset($settings['custom'][MLAOptions::MLA_NEW_CUSTOM_RULE]); } } if (empty($settings['custom'])) { return array('message' => 'ERROR: No custom field settings to process.', 'body' => ''); } $examine_count = 0; $update_count = 0; // $query = array( 'orderby' => 'none', 'post_parent' => 'all' ); // , 'post_mime_type' => 'image' ); // $query = array( 'orderby' => 'none', 'post_parent' => 'all', 'post_mime_type' => 'application/*pdf*' ); $query = array('orderby' => 'none', 'post_parent' => 'all', 'post_mime_type' => 'image,application/*pdf*'); $posts = MLAShortcodes::mla_get_shortcode_attachments(0, $query); if (is_string($posts)) { return array('message' => $posts, 'body' => ''); } foreach ($posts as $key => $post) { $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($post, 'iptc_exif_custom_mapping', $settings); $examine_count += 1; if (!empty($updates)) { $results = MLAData::mla_update_single_item($post->ID, $updates); if (stripos($results['message'], 'updated.')) { $update_count += 1; } } } // foreach post if ($update_count) { $message = "IPTC/EXIF custom field mapping completed; {$examine_count} attachment(s) examined, {$update_count} updated.\r\n"; } else { $message = "IPTC/EXIF custom field mapping completed; {$examine_count} attachment(s) examined, no changes detected.\r\n"; } return array('message' => $message, 'body' => ''); }
/** * Perform ITC/EXIF mapping on just-inserted attachment * * @since 1.00 * * @param integer ID of just-inserted attachment * * @return void */ public static function mla_add_attachment_action($post_id) { if ('checked' == MLAOptions::mla_get_option('enable_iptc_exif_mapping')) { $item = get_post($post_id); $updates = MLAOptions::mla_evaluate_iptc_exif_mapping($item, 'iptc_exif_mapping'); if (!empty($updates)) { $item_content = MLAData::mla_update_single_item($post_id, $updates); } } }