Пример #1
0
 /**
  * blcLink::edit()
  * Edit all instances of the link by changing the URL.
  *
  * Here's how this really works : create a new link with the new URL. Then edit()
  * all instances and point them to the new link record. If some instance can't be 
  * edited they will still point to the old record. The old record is deleted
  * if all instances were edited successfully.   
  *
  * @param string $new_url
  * @param string $new_text Optional.
  * @return array An associative array with these keys : 
  *   new_link_id - the database ID of the new link.
  *   new_link - the new link (an instance of blcLink).
  *   cnt_okay - the number of successfully edited link instances. 
  *   cnt_error - the number of instances that caused problems.
  *   errors - an array of WP_Error objects corresponding to the failed edits.  
  */
 function edit($new_url, $new_text = null)
 {
     if (!$this->valid()) {
         return new WP_Error('link_invalid', __("Link is not valid", 'broken-link-checker'));
     }
     //FB::info('Changing link '.$this->link_id .' to URL "'.$new_url.'"');
     $instances = $this->get_instances();
     //Fail if there are no instances
     if (empty($instances)) {
         return array('new_link_id' => $this->link_id, 'new_link' => $this, 'cnt_okay' => 0, 'cnt_error' => 0, 'errors' => array(new WP_Error('no_instances_found', __('This link can not be edited because it is not used anywhere on this site.', 'broken-link-checker'))));
     }
     //Load or create a link with the URL = $new_url
     $new_link = new blcLink($new_url);
     $was_new = $new_link->is_new;
     if ($new_link->is_new) {
         //FB::log($new_link, 'Saving a new link');
         $new_link->save();
         //so that we get a valid link_id
     }
     //FB::log("Changing link to $new_url");
     if (empty($new_link->link_id)) {
         //FB::error("Failed to create a new link record");
         return array('new_link_id' => $this->link_id, 'new_link' => $this, 'cnt_okay' => 0, 'cnt_error' => 0, 'errors' => array(new WP_Error('link_creation_failed', __('Failed to create a DB entry for the new URL.', 'broken-link-checker'))));
     }
     $cnt_okay = $cnt_error = 0;
     $errors = array();
     //Edit each instance.
     //FB::info('Editing ' . count($instances) . ' instances');
     foreach ($instances as $instance) {
         $rez = $instance->edit($new_url, $this->url, $new_text);
         if (is_wp_error($rez)) {
             $cnt_error++;
             array_push($errors, $rez);
             //FB::error($instance, 'Failed to edit instance ' . $instance->instance_id);
         } else {
             $cnt_okay++;
             $instance->link_id = $new_link->link_id;
             $instance->save();
             //FB::info($instance, 'Successfully edited instance '  . $instance->instance_id);
         }
     }
     //If all instances were edited successfully we can delete the old link record.
     //UNLESS this link is equal to the new link (which should never happen, but whatever).
     if ($cnt_error == 0 && $cnt_okay > 0 && $this->link_id != $new_link->link_id) {
         $this->forget(false);
     }
     //On the other hand, if no instances could be edited and the $new_link was really new,
     //then delete it.
     if ($cnt_okay == 0 && $was_new) {
         $new_link->forget(false);
         $new_link = $this;
     }
     return array('new_link_id' => $new_link->link_id, 'new_link' => $new_link, 'cnt_okay' => $cnt_okay, 'cnt_error' => $cnt_error, 'errors' => $errors);
 }