コード例 #1
0
 /**
  * Change all links with the specified URL to a new URL.
  *
  * @param string $field_name
  * @param blcParser $parser
  * @param string $new_url
  * @param string $old_url
  * @param string $old_raw_url
  * @param string $new_text Optional.
  *
  * @return array|WP_Error The new value of raw_url on success, or an error object if something went wrong.
  */
 function edit_link($field_name, $parser, $new_url, $old_url = '', $old_raw_url = '', $new_text = null)
 {
     //Ensure we're operating on a consistent copy of the wrapped object.
     /* 
     Explanation 
     
     Consider this scenario where the container object wraps a blog post : 
     	1) The container object gets created and loads the post data. 
     	2) Someone modifies the DB data corresponding to the post.
     	3) The container tries to edit a link present in the post. However, the post
     	has changed since the time it was first cached, so when the container updates
     	the post with it's changes, it will overwrite whatever modifications were made
     	in step 2.
     	
     This would not be a problem if WP entities like posts and comments were 
     actually real objects, not just bags of key=>value pairs, but oh well.
     	
     Therefore, it is necessary to re-load the wrapped object before editing it.   
     */
     $this->get_wrapped_object(true);
     //Get the current value of the field that needs to be edited.
     $old_value = $this->get_field($field_name);
     //Have the parser modify the specified link. If successful, the parser will
     //return an associative array with two keys - 'content' and 'raw_url'.
     //Otherwise we'll get an instance of WP_Error.
     if ($parser->is_link_text_editable()) {
         $edit_result = $parser->edit($old_value, $new_url, $old_url, $old_raw_url, $new_text);
     } else {
         $edit_result = $parser->edit($old_value, $new_url, $old_url, $old_raw_url);
     }
     if (is_wp_error($edit_result)) {
         return $edit_result;
     }
     //Update the field with the new value returned by the parser.
     $update_result = $this->update_field($field_name, $edit_result['content'], $old_value);
     if (is_wp_error($update_result)) {
         return $update_result;
     }
     //Return the new values to the instance.
     unset($edit_result['content']);
     //(Except content, which it doesn't need.)
     return $edit_result;
 }