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