Exemplo n.º 1
0
 /**
  * Delete or trash posts, bookmarks and other items that contain any of the specified links.
  * 
  * Will prefer moving stuff to trash to permanent deletion. If it encounters an item that 
  * can't be moved to the trash, it will skip that item by default.
  *
  * @param array $selected_links An array of link IDs
  * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false.
  * @return array Confirmation message and its CSS class.
  */
 function do_bulk_delete_sources($selected_links, $force_delete = false)
 {
     $message = '';
     $msg_class = 'updated';
     //Delete posts, blogroll entries and any other link containers that contain any of the selected links.
     //
     //Note that once all containers 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) {
             /* @var blcLink $link */
             $instances = $link->get_instances();
             foreach ($instances as $instance) {
                 /* @var blcLinkInstance $instance */
                 $key = $instance->container_type . '|' . $instance->container_id;
                 $containers[$key] = array($instance->container_type, $instance->container_id);
             }
         }
         //Instantiate the containers
         $containers = blcContainerHelper::get_containers($containers);
         //Delete/trash their associated entities
         $deleted = array();
         $skipped = array();
         foreach ($containers as $container) {
             /* @var blcContainer $container */
             if (!$container->current_user_can_delete()) {
                 continue;
             }
             if ($force_delete) {
                 $rez = $container->delete_wrapped_object();
             } else {
                 if ($container->can_be_trashed()) {
                     $rez = $container->trash_wrapped_object();
                 } else {
                     $skipped[] = $container;
                     continue;
                 }
             }
             if (is_wp_error($rez)) {
                 /* @var 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.
                 $container_type = $container->container_type;
                 if (isset($deleted[$container_type])) {
                     $deleted[$container_type]++;
                 } else {
                     $deleted[$container_type] = 1;
                 }
             }
         }
         //Generate delete confirmation messages
         foreach ($deleted as $container_type => $number) {
             if ($force_delete) {
                 $messages[] = blcContainerHelper::ui_bulk_delete_message($container_type, $number);
             } else {
                 $messages[] = blcContainerHelper::ui_bulk_trash_message($container_type, $number);
             }
         }
         //If some items couldn't be trashed, let the user know
         if (count($skipped) > 0) {
             $message = sprintf(_n("%d item was skipped because it can't be moved to the Trash. You need to delete it manually.", "%d items were skipped because they can't be moved to the Trash. You need to delete them manually.", count($skipped)), count($skipped));
             $message .= '<br><ul>';
             foreach ($skipped as $container) {
                 $message .= sprintf('<li>%s</li>', $container->ui_get_source(''));
             }
             $message .= '</ul>';
             $messages[] = $message;
         }
         if (count($messages) > 0) {
             $message = implode('<p>', $messages);
         } else {
             $message = __("Didn't find anything to delete!", 'broken-link-checker');
             $msg_class = 'error';
         }
     }
     return array($message, $msg_class);
 }
 /**
  * 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. Defaults to false.
  * @param bool $load_wrapped_objects Preload wrapped objects regardless of purpose. Defaults to false.
  * @param bool $include_invalid Include instances that refer to not-loaded containers or parsers. Defaults to false.
  * @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, $include_invalid = 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})";
     //Skip instances that reference containers or parsers that aren't currently loaded
     if (!$include_invalid) {
         $manager =& blcModuleManager::getInstance();
         $active_containers = $manager->get_escaped_ids('container');
         $active_parsers = $manager->get_escaped_ids('parser');
         $q .= " AND container_type IN ({$active_containers}) ";
         $q .= " AND parser_type IN ({$active_parsers}) ";
     }
     $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 = blcContainerHelper::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;
 }
Exemplo n.º 3
0
 /**
  * Retrieve link containers that need to be synchronized (parsed).
  *
  * @param integer $max_results The maximum number of containers to return. Defaults to returning all unsynched containers. 
  * @return blcContainer[]
  */
 static function get_unsynched_containers($max_results = 0)
 {
     global $wpdb;
     /* @var wpdb $wpdb */
     $q = "SELECT * FROM {$wpdb->prefix}blc_synch WHERE synched = 0";
     if ($max_results > 0) {
         $q .= " LIMIT {$max_results}";
     }
     $container_data = $wpdb->get_results($q, ARRAY_A);
     //FB::log($container_data, "Unsynched containers");
     if (empty($container_data)) {
         return array();
     }
     $containers = blcContainerHelper::get_containers($container_data, BLC_FOR_PARSING, 'dummy');
     return $containers;
 }