/** * Get all link instances associated with one or more links. * * @param array $link_ids Array of link IDs. * @param string $purpose An optional code indicating how the instances will be used. Available predefined constants : BLC_FOR_DISPLAY, BLC_FOR_EDITING * @param bool $load_containers Preload containers regardless of purpose. * @param bool $load_wrapped_objects Preload wrapped objects regardless of purpose. * @return array An array indexed by link ID. Each item of the array will be an array of blcLinkInstance objects. */ function blc_get_instances($link_ids, $purpose = '', $load_containers = false, $load_wrapped_objects = false) { global $wpdb; if (empty($link_ids)) { return array(); } $link_ids_in = implode(', ', $link_ids); $q = "SELECT * FROM {$wpdb->prefix}blc_instances WHERE link_id IN ({$link_ids_in})"; $results = $wpdb->get_results($q, ARRAY_A); if (empty($results)) { return array(); } //Also retrieve the containers, if it could be useful. $load_containers = $load_containers || in_array($purpose, array(BLC_FOR_DISPLAY, BLC_FOR_EDITING)); if ($load_containers) { //Collect a list of (container_type, container_id) pairs $container_ids = array(); foreach ($results as $result) { array_push($container_ids, array($result['container_type'], intval($result['container_id']))); } $containers = blc_get_containers($container_ids, $purpose, $load_wrapped_objects); } //Create an object for each instance and group them by link ID $instances = array(); foreach ($results as $result) { $instance = new blcLinkInstance($result); //Assign a container to the link instance, if available if ($load_containers && !empty($containers)) { $key = $instance->container_type . '|' . $instance->container_id; if (isset($containers[$key])) { $instance->_container = $containers[$key]; } } if (isset($instances[$instance->link_id])) { array_push($instances[$instance->link_id], $instance); } else { $instances[$instance->link_id] = array($instance); } } return $instances; }
/** * Delete posts, bookmarks and other items that contain any of the specified links. * * @param array $selected_links An array of link IDs * @return array Confirmation message and its CSS class. */ function do_bulk_delete_sources($selected_links) { global $blc_container_registry; $message = ''; $msg_class = 'updated'; //Delete posts, blogroll entries and any other link containers that contain any of the selected links. // //Note that once all cotnainers containing a particular link have been deleted, //there is no need to explicitly delete the link record itself. The hooks attached to //the actions that execute when something is deleted (e.g. "post_deleted") will //take care of that. check_admin_referer('bulk-action'); if (count($selected_links) > 0) { $messages = array(); //Fetch all the selected links $links = blc_get_links(array('link_ids' => $selected_links, 'load_instances' => true)); //Make a list of all containers associated with these links, with each container //listed only once. $containers = array(); foreach ($links as $link) { $instances = $link->get_instances(); foreach ($instances as $instance) { $key = $instance->container_type . '|' . $instance->container_id; $containers[$key] = array($instance->container_type, $instance->container_id); } } //Instantiate the containers $containers = blc_get_containers($containers); //Delete their associated entities $deleted = array(); foreach ($containers as $container) { $container_type = $container->container_type; $rez = $container->delete_wrapped_object(); if (is_wp_error($rez)) { //Record error messages for later display $messages[] = $rez->get_error_message(); $msg_class = 'error'; } else { //Keep track of how many of each type were deleted. if (isset($deleted[$container_type])) { $deleted[$container_type]++; } else { $deleted[$container_type] = 1; } } } //Generate delete confirmation messages foreach ($deleted as $container_type => $number) { $messages[] = $blc_container_registry->ui_bulk_delete_message($container_type, $number); } if (count($messages) > 0) { $message = implode('<br>', $messages); } else { $message = __("Didn't find anything to delete!", 'broken-link-checker'); $msg_class = 'error'; } } return array($message, $msg_class); }