PageRenderTime 71ms CodeModel.GetById 30ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 1ms

/wp-content/plugins/broken-link-checker/modules/containers/blogroll.php

https://bitbucket.org/lgorence/quickpress
PHP | 309 lines | 164 code | 42 blank | 103 comment | 17 complexity | 1e9ae67d5fa30ea5bdc7243d4e3c91cf MD5 | raw file
  1<?php
  2/*
  3Plugin Name: Blogroll items
  4Description: 
  5Version: 1.0
  6Author: Janis Elsts
  7
  8ModuleID: blogroll
  9ModuleCategory: container
 10ModuleClassName: blcBookmarkManager
 11*/
 12
 13class blcBookmark extends blcContainer{
 14	
 15	function ui_get_source($container_field = '', $context = 'display'){
 16		$bookmark = $this->get_wrapped_object();
 17		
 18		$image = sprintf(
 19			'<img src="%s/broken-link-checker/images/link.png" class="blc-small-image" title="%2$s" alt="%2$s">',
 20			WP_PLUGIN_URL,
 21			__('Bookmark', 'broken-link-checker')						
 22		);
 23		
 24		$link_name = sprintf(
 25			'<a class="row-title" href="%s" title="%s">%s</a>',
 26			$this->get_edit_url(),
 27			__('Edit this bookmark', 'broken-link-checker'),
 28			sanitize_bookmark_field('link_name', $bookmark->link_name, $this->container_id, 'display')
 29		);
 30		
 31		if ( $context != 'email' ){
 32			return "$image $link_name";
 33		} else {
 34			return $link_name;
 35		}
 36	}
 37	
 38	function ui_get_action_links($container_field){
 39		//Inline action links for bookmarks     
 40		$bookmark = &$this->get_wrapped_object();
 41		
 42		$delete_url = admin_url( wp_nonce_url("link.php?action=delete&link_id={$this->container_id}", 'delete-bookmark_' . $this->container_id) ); 
 43		
 44      	$actions = array();
 45		if ( current_user_can('manage_links') ) {
 46			$actions['edit'] = '<span class="edit"><a href="' . $this->get_edit_url() . '" title="' . esc_attr(__('Edit this bookmark', 'broken-link-checker')) . '">' . __('Edit') . '</a>';
 47			$actions['delete'] = "<span class='delete'><a class='submitdelete' href='" . esc_url($delete_url) . "' onclick=\"if ( confirm('" . esc_js(sprintf( __("You are about to delete this link '%s'\n  'Cancel' to stop, 'OK' to delete."), $bookmark->link_name)) . "') ) { return true;}return false;\">" . __('Delete') . "</a>";
 48		}
 49	
 50		return $actions;
 51	}
 52	
 53	function get_edit_url(){
 54		return esc_url(admin_url("link.php?action=edit&link_id={$this->container_id}"));
 55	}
 56	
 57  /**
 58   * Retrieve the bookmark associated with this container. 
 59   *
 60   * @access protected
 61   *
 62   * @param bool $ensure_consistency Set this to true to ignore the cached $wrapped_object value and retrieve an up-to-date copy of the wrapped object from the DB (or WP's internal cache).
 63   * @return object Bookmark data.
 64   */
 65	function get_wrapped_object($ensure_consistency = false){
 66		if( $ensure_consistency || is_null($this->wrapped_object) ){
 67			$this->wrapped_object = &get_bookmark($this->container_id);
 68		}		
 69		return $this->wrapped_object;
 70	}
 71
 72  /**
 73   * Update the bookmark associated with this container.
 74   *
 75   * @access protected
 76   *
 77   * @return bool|WP_Error True on success, an error if something went wrong.
 78   */
 79	function update_wrapped_object(){
 80		if ( is_null($this->wrapped_object) ){
 81			return new WP_Error(
 82				'no_wrapped_object',
 83				__('Nothing to update', 'broken-link-checker')
 84			);
 85		}
 86		
 87		//wp_update_link() expects it's argument to be an array.
 88		$data = (array)$this->wrapped_object;
 89		//Update the bookmark
 90		$rez = wp_update_link($data);
 91		
 92		if ( !empty($rez) ){
 93			return true;
 94		} else {
 95			return new WP_Error(
 96				'update_failed',
 97				sprintf(__('Updating bookmark %d failed', 'broken-link-checker'), $this->container_id)
 98			);
 99		}
100	}
101	
102  /**
103   * Delete the bookmark corresponding to this container.
104   * Also removes the synch. record of the container and removes all associated instances.
105   *
106   * @return bool|WP_error
107   */
108	function delete_wrapped_object(){
109		if ( wp_delete_link($this->container_id) ){
110			//Note that there is no need to explicitly delete the synch. record and instances 
111			//associated with this link - wp_delete_link() will execute the 'delete_link' action, 
112			//which will be noticed by blcBookmarkManager, which will then delete anything that needs
113			//to be deleted.
114			
115			//But in case the (undocumented) behaviour of wp_delete_link() changes in a later WP version,
116			//add a call to $this->delete() here.
117			return true;
118		} else {
119			$bookmark = $this->get_wrapped_object();
120			
121			if ( is_null($bookmark) ){
122				$link_name = "[nonexistent]";
123			} else {
124				$link_name = sanitize_bookmark_field('link_name', $bookmark->link_name, $this->container_id, 'display');
125			}
126			
127			$msg = sprintf(
128				__('Failed to delete blogroll link "%s" (%d)', 'broken-link-checker'),
129				$link_name,
130				$this->container_id
131			);
132			
133			return new WP_Error( 'delete_failed', $msg );
134		};
135	}
136	
137	function current_user_can_delete(){
138		return current_user_can('manage_links');
139	}
140	
141	function can_be_trashed(){
142		return false;
143	}
144	
145  /**
146   * Get the default link text. For bookmarks, this is the bookmark name.
147   *
148   * @param string $field
149   * @return string
150   */
151	function default_link_text($field = ''){
152		$bookmark = $this->get_wrapped_object();
153		return sanitize_bookmark_field('link_name', $bookmark->link_name, $this->container_id, 'display');
154	}
155	
156  /**
157   * For bookmarks, calling unlink() simply removes the bookmark.
158   *
159   * @return bool|WP_Error True on success, or an error object if something went wrong.
160   */
161	function unlink($field_name, $parser, $url, $raw_url =''){
162		return $this->delete_wrapped_object(); 
163	}
164	
165}
166
167class blcBookmarkManager extends blcContainerManager{
168	var $container_class_name = 'blcBookmark';
169	var $fields = array('link_url' => 'url_field');
170	
171  /**
172   * Set up hooks that monitor added/modified/deleted bookmarks.
173   *
174   * @return void
175   */
176	function init(){
177		parent::init();
178		
179        add_action('add_link', array(&$this,'hook_add_link'));
180        add_action('edit_link', array(&$this,'hook_edit_link'));
181        add_action('delete_link', array(&$this,'hook_delete_link'));
182	}
183	
184  /**
185   * Instantiate multiple containers of the container type managed by this class.
186   *
187   * @param array $containers Array of assoc. arrays containing container data.
188   * @param string $purpose An optional code indicating how the retrieved containers will be used.
189   * @param bool $load_wrapped_objects Preload wrapped objects regardless of purpose. 
190   * 
191   * @return array of blcBookmark indexed by "container_type|container_id"
192   */
193	function get_containers($containers, $purpose = '', $load_wrapped_objects = false){
194		$containers = $this->make_containers($containers);
195		
196		//Preload bookmark data if it is likely to be useful later
197		$preload = $load_wrapped_objects || in_array($purpose, array(BLC_FOR_DISPLAY, BLC_FOR_PARSING));
198		if ( $preload ){
199			$bookmark_ids = array();
200			foreach($containers as $container){
201				$bookmark_ids[] = $container->container_id;
202			}
203			
204			$args = array('include' => implode(',', $bookmark_ids));
205			$bookmarks = get_bookmarks($args);
206			
207			foreach($bookmarks as $bookmark){
208				$key = $this->container_type . '|' . $bookmark->link_id;
209				if ( isset($containers[$key]) ){
210					$containers[$key]->wrapped_object = $bookmark;
211				}
212			}
213		}
214		
215		return $containers;
216	}
217	
218  /**
219   * Create or update synchronization records for all posts.
220   *
221   * @param bool $forced If true, assume that all synch. records are gone and will need to be recreated from scratch. 
222   * @return void
223   */
224	function resynch($forced = false){
225		global $wpdb;
226		
227		if ( !$forced ){
228			//Usually the number of bookmarks is rather small, so it's cheap enough to always 
229			//drop the entire list of bookmark-related synch records and recreate it from scratch.
230			$q = $wpdb->prepare(
231				"DELETE FROM {$wpdb->prefix}blc_synch WHERE container_type = %s",
232				$this->container_type
233			);
234			$wpdb->query( $q );	 
235		}
236		
237		//Create new synchronization records for all bookmarks (AKA the blogroll).
238 		$q = "INSERT INTO {$wpdb->prefix}blc_synch(container_id, container_type, synched)
239			  SELECT link_id, %s, 0
240			  FROM {$wpdb->links}
241			  WHERE 1";
242		$q = $wpdb->prepare($q, $this->container_type);
243 		$wpdb->query( $q );
244	}
245	
246  /**
247   * When a bookmark is added mark it as unsynched.
248   *
249   * @param int $link_id
250   * @return void
251   */
252	function hook_add_link( $link_id ){
253		$container = blcContainerHelper::get_container( array($this->container_type, $link_id) );
254		$container->mark_as_unsynched();
255	}
256	
257  /**
258   * Ditto for modified bookmarks.
259   *
260   * @param int $link_id
261   * @return void
262   */
263	function hook_edit_link( $link_id ){
264		$this->hook_add_link( $link_id );
265	}
266	
267  /**
268   * When a bookmark is deleted, remove the related DB records.
269   *
270   * @param int $link_id
271   * @return void
272   */
273	function hook_delete_link( $link_id ){
274		//Get the container object.
275		$container = blcContainerHelper::get_container( array($this->container_type, $link_id) );
276		//Get the link(s) associated with it.
277		$links = $container->get_links(); 
278		
279		//Remove synch. record & instance records.
280		$container->delete();
281		
282		//Clean up links associated with this bookmark (there's probably only one)
283		$link_ids = array();
284		foreach($links as $link){
285			$link_ids[] = $link->link_id;
286		}
287		blc_cleanup_links($link_ids);
288	}
289	
290  /**
291   * Get the message to display after $n bookmarks have been deleted.
292   *
293   * @param int $n Number of deleted bookmarks.
294   * @return string The delete confirmation message.
295   */
296	function ui_bulk_delete_message($n){
297		return sprintf(
298			_n(
299				"%d blogroll link deleted.", 
300				"%d blogroll links deleted.", 
301				$n, 
302				'broken-link-checker'
303			),
304			$n
305		);
306	}
307}
308
309?>