PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/includes/any-post.php

https://bitbucket.org/lgorence/quickpress
PHP | 745 lines | 430 code | 84 blank | 231 comment | 75 complexity | 626f539d6dcd5a5ab515f96bc1c75c4a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * The manager to rule all (post) managers.
  4. *
  5. * This class dynamically registers container modules for the available post types
  6. * (including custom post types) and does stuff that pertain to all of them, such
  7. * as handling save/delete hooks and (re)creating synch records.
  8. *
  9. * @package Broken Link Checker
  10. * @author Janis Elsts
  11. * @access private
  12. */
  13. class blcPostTypeOverlord {
  14. var $enabled_post_types = array(); //Post types currently selected for link checking
  15. var $enabled_post_statuses = array('publish'); //Only posts that have one of these statuses shall be checked
  16. var $plugin_conf;
  17. var $resynch_already_done = false;
  18. /**
  19. * Class "constructor". Can't use an actual constructor due to how PHP4 handles object references.
  20. *
  21. * Specifically, this class is a singleton. The function needs to pass $this to several other
  22. * functions (to set up hooks), which will store the reference for later use. However, it appears
  23. * that in PHP4 the actual value of $this is thrown away right after the constructor finishes, and
  24. * `new` returns a *copy* of $this. The result is that getInstance() won't be returning a ref.
  25. * to the same object as is used for hook callbacks. And that's horrible.
  26. *
  27. * Sets up hooks that monitor added/modified/deleted posts and registers
  28. * virtual modules for all post types.
  29. *
  30. * @return void
  31. */
  32. function init(){
  33. $this->plugin_conf = blc_get_configuration();
  34. if ( isset($this->plugin_conf->options['enabled_post_statuses']) ){
  35. $this->enabled_post_statuses = $this->plugin_conf->options['enabled_post_statuses'];
  36. }
  37. //Register a virtual container module for each enabled post type
  38. $module_manager = blcModuleManager::getInstance();
  39. $post_types = get_post_types(array(), 'objects');
  40. $exceptions = array('revision', 'nav_menu_item', 'attachment');
  41. $built_in = array('post', 'page');
  42. foreach($post_types as $data){
  43. $post_type = $data->name;
  44. if ( in_array($post_type, $exceptions) ){
  45. continue;
  46. }
  47. $module_manager->register_virtual_module(
  48. $post_type,
  49. array(
  50. 'Name' => $data->labels->name,
  51. 'ModuleCategory' => 'container',
  52. 'ModuleContext' => 'all',
  53. 'ModuleClassName' => 'blcAnyPostContainerManager',
  54. )
  55. );
  56. }
  57. //These hooks update the synch & instance records when posts are added, deleted or modified.
  58. add_action('delete_post', array(&$this,'post_deleted'));
  59. add_action('save_post', array(&$this,'post_saved'));
  60. //We also treat post trashing/untrashing as delete/save.
  61. add_action('trash_post', array(&$this,'post_deleted'));
  62. add_action('untrash_post', array(&$this,'post_saved'));
  63. //Highlight and nofollow broken links in posts & pages
  64. if ( $this->plugin_conf->options['mark_broken_links'] || $this->plugin_conf->options['nofollow_broken_links'] ){
  65. add_filter( 'the_content', array(&$this, 'hook_the_content') );
  66. if ( $this->plugin_conf->options['mark_broken_links'] && !empty( $this->plugin_conf->options['broken_link_css'] ) ){
  67. add_action( 'wp_head', array(&$this,'hook_wp_head') );
  68. }
  69. }
  70. }
  71. /**
  72. * Retrieve an instance of the overlord class.
  73. *
  74. * @return blcPostTypeOverlord
  75. */
  76. static function getInstance(){
  77. static $instance = null;
  78. if ( is_null($instance) ){
  79. $instance = new blcPostTypeOverlord;
  80. $instance->init();
  81. }
  82. return $instance;
  83. }
  84. /**
  85. * Notify the overlord that a post type is active.
  86. *
  87. * Called by individual instances of blcAnyPostContainerManager to let
  88. * the overlord know that they've been created. Since a module instance
  89. * is only created if the module is active, this event indicates that
  90. * the user has enabled the corresponding post type for link checking.
  91. *
  92. * @param string $post_type
  93. * @return void
  94. */
  95. function post_type_enabled($post_type){
  96. if ( !in_array($post_type, $this->enabled_post_types) ){
  97. $this->enabled_post_types[] = $post_type;
  98. }
  99. }
  100. /**
  101. * Remove the synch. record and link instances associated with a post when it's deleted
  102. *
  103. * @param int $post_id
  104. * @return void
  105. */
  106. function post_deleted($post_id){
  107. //Get the container type matching the type of the deleted post
  108. $post = get_post($post_id);
  109. if ( !$post ){
  110. return;
  111. }
  112. //Get the associated container object
  113. $post_container = blcContainerHelper::get_container( array($post->post_type, intval($post_id)) );
  114. if ( $post_container ){
  115. //Delete it
  116. $post_container->delete();
  117. //Clean up any dangling links
  118. blc_cleanup_links();
  119. }
  120. }
  121. /**
  122. * When a post is saved or modified, mark it as unparsed.
  123. *
  124. * @param int $post_id
  125. * @return void
  126. */
  127. function post_saved($post_id){
  128. //Get the container type matching the type of the deleted post
  129. $post = get_post($post_id);
  130. if ( !$post ){
  131. return;
  132. }
  133. //Only check links in currently enabled post types
  134. if ( !in_array($post->post_type, $this->enabled_post_types) ) return;
  135. //Only check posts that have one of the allowed statuses
  136. if ( !in_array($post->post_status, $this->enabled_post_statuses) ) return;
  137. //Get the container & mark it as unparsed
  138. $args = array($post->post_type, intval($post_id));
  139. $post_container = blcContainerHelper::get_container( $args );
  140. $post_container->mark_as_unsynched();
  141. }
  142. /**
  143. * Create or update synchronization records for all posts.
  144. *
  145. * @param bool $forced If true, assume that all synch. records are gone and will need to be recreated from scratch.
  146. * @return void
  147. */
  148. function resynch($container_type = '', $forced = false){
  149. global $wpdb;
  150. global $blclog;
  151. //Resynch is expensive in terms of DB performance. Thus we only do it once, processing
  152. //all post types in one go and ignoring any further resynch requests during this pageload.
  153. //BUG: This might be a problem if there ever is an actual need to run resynch twice or
  154. //more per pageload.
  155. if ( $this->resynch_already_done ){
  156. $blclog->log(sprintf('...... Skipping "%s" resyncyh since all post types were already synched.', $container_type));
  157. return;
  158. }
  159. if ( empty($this->enabled_post_types) ){
  160. $blclog->warn(sprintf('...... Skipping "%s" resyncyh since no post types are enabled.', $container_type));
  161. return;
  162. }
  163. $escaped_post_types = array_map(array(&$wpdb, 'escape'), $this->enabled_post_types);
  164. $escaped_post_statuses = array_map(array(&$wpdb, 'escape'), $this->enabled_post_statuses);
  165. if ( $forced ){
  166. //Create new synchronization records for all posts.
  167. $blclog->log('...... Creating synch records for these post types: '.implode(', ', $escaped_post_types) . ' that have one of these statuses: ' . implode(', ', $escaped_post_statuses));
  168. $q = "INSERT INTO {$wpdb->prefix}blc_synch(container_id, container_type, synched)
  169. SELECT posts.id, posts.post_type, 0
  170. FROM {$wpdb->posts} AS posts
  171. WHERE
  172. posts.post_status IN (%s)
  173. AND posts.post_type IN (%s)";
  174. $q = sprintf(
  175. $q,
  176. "'" . implode("', '", $escaped_post_statuses) . "'",
  177. "'" . implode("', '", $escaped_post_types) . "'"
  178. );
  179. $wpdb->query( $q );
  180. $blclog->log(sprintf('...... %d rows inserted', $wpdb->rows_affected));
  181. } else {
  182. //Delete synch records corresponding to posts that no longer exist.
  183. $blclog->log('...... Deleting synch records for removed posts');
  184. $q = "DELETE synch.*
  185. FROM
  186. {$wpdb->prefix}blc_synch AS synch LEFT JOIN {$wpdb->posts} AS posts
  187. ON posts.ID = synch.container_id
  188. WHERE
  189. synch.container_type IN (%s) AND posts.ID IS NULL";
  190. $q = sprintf(
  191. $q,
  192. "'" . implode("', '", $escaped_post_types) . "'"
  193. );
  194. $wpdb->query( $q );
  195. $blclog->log(sprintf('...... %d rows deleted', $wpdb->rows_affected));
  196. //Remove the 'synched' flag from all posts that have been updated
  197. //since the last time they were parsed/synchronized.
  198. $blclog->log('...... Marking changed posts as unsynched');
  199. $q = "UPDATE
  200. {$wpdb->prefix}blc_synch AS synch
  201. JOIN {$wpdb->posts} AS posts ON (synch.container_id = posts.ID and synch.container_type=posts.post_type)
  202. SET
  203. synched = 0
  204. WHERE
  205. synch.last_synch < posts.post_modified";
  206. $wpdb->query( $q );
  207. $blclog->log(sprintf('...... %d rows updated', $wpdb->rows_affected));
  208. //Create synch. records for posts that don't have them.
  209. $blclog->log('...... Creating synch records for new posts');
  210. $q = "INSERT INTO {$wpdb->prefix}blc_synch(container_id, container_type, synched)
  211. SELECT posts.id, posts.post_type, 0
  212. FROM
  213. {$wpdb->posts} AS posts LEFT JOIN {$wpdb->prefix}blc_synch AS synch
  214. ON (synch.container_id = posts.ID and synch.container_type=posts.post_type)
  215. WHERE
  216. posts.post_status IN (%s)
  217. AND posts.post_type IN (%s)
  218. AND synch.container_id IS NULL";
  219. $q = sprintf(
  220. $q,
  221. "'" . implode("', '", $escaped_post_statuses) . "'",
  222. "'" . implode("', '", $escaped_post_types) . "'"
  223. );
  224. $wpdb->query($q);
  225. $blclog->log(sprintf('...... %d rows inserted', $wpdb->rows_affected));
  226. }
  227. $this->resynch_already_done = true;
  228. }
  229. /**
  230. * Hook for the 'the_content' filter. Scans the current post and adds the 'broken_link'
  231. * CSS class to all links that are known to be broken. Currently works only on standard
  232. * HTML links (i.e. the '<a href=...' kind).
  233. *
  234. * @param string $content Post content
  235. * @return string Modified post content.
  236. */
  237. function hook_the_content($content){
  238. global $post, $wpdb; /** @var wpdb $wpdb */
  239. if ( empty($post) || !in_array($post->post_type, $this->enabled_post_types)) {
  240. return $content;
  241. }
  242. //Retrieve info about all occurences of broken links in the current post
  243. $q = "
  244. SELECT instances.raw_url
  245. FROM {$wpdb->prefix}blc_instances AS instances JOIN {$wpdb->prefix}blc_links AS links
  246. ON instances.link_id = links.link_id
  247. WHERE
  248. instances.container_type = %s
  249. AND instances.container_id = %d
  250. AND links.broken = 1
  251. AND parser_type = 'link'
  252. ";
  253. $q = $wpdb->prepare($q, $post->post_type, $post->ID);
  254. $links = $wpdb->get_results($q, ARRAY_A);
  255. //Return the content unmodified if there are no broken links in this post.
  256. if ( empty($links) || !is_array($links) ){
  257. return $content;
  258. }
  259. //Put the broken link URLs in an array
  260. $broken_link_urls = array();
  261. foreach($links as $link){
  262. $broken_link_urls[] = $link['raw_url'];
  263. }
  264. //Iterate over all HTML links and modify the broken ones
  265. if ( $parser = blcParserHelper::get_parser('link') ){
  266. $content = $parser->multi_edit($content, array(&$this, 'highlight_broken_link'), $broken_link_urls);
  267. }
  268. return $content;
  269. }
  270. /**
  271. * Analyse a link and add 'broken_link' CSS class if the link is broken.
  272. *
  273. * @see blcHtmlLink::multi_edit()
  274. *
  275. * @param array $link Associative array of link data.
  276. * @param array $broken_link_urls List of broken link URLs present in the current post.
  277. * @return array|string The modified link
  278. */
  279. function highlight_broken_link($link, $broken_link_urls){
  280. if ( !in_array($link['href'], $broken_link_urls) ){
  281. //Link not broken = return the original link tag
  282. return $link['#raw'];
  283. }
  284. //Add 'broken_link' to the 'class' attribute (unless already present).
  285. if ( $this->plugin_conf->options['mark_broken_links'] ){
  286. if ( isset($link['class']) ){
  287. $classes = explode(' ', $link['class']);
  288. if ( !in_array('broken_link', $classes) ){
  289. $classes[] = 'broken_link';
  290. $link['class'] = implode(' ', $classes);
  291. }
  292. } else {
  293. $link['class'] = 'broken_link';
  294. }
  295. }
  296. //Nofollow the link (unless it's already nofollow'ed)
  297. if ( $this->plugin_conf->options['nofollow_broken_links'] ){
  298. if ( isset($link['rel']) ){
  299. $relations = explode(' ', $link['rel']);
  300. if ( !in_array('nofollow', $relations) ){
  301. $relations[] = 'nofollow';
  302. $link['rel'] = implode(' ', $relations);
  303. }
  304. } else {
  305. $link['rel'] = 'nofollow';
  306. }
  307. }
  308. return $link;
  309. }
  310. /**
  311. * A hook for the 'wp_head' action. Outputs the user-defined broken link CSS.
  312. *
  313. * @return void
  314. */
  315. function hook_wp_head(){
  316. echo '<style type="text/css">',$this->plugin_conf->options['broken_link_css'],'</style>';
  317. }
  318. }
  319. //Start up the post overlord
  320. blcPostTypeOverlord::getInstance();
  321. /**
  322. * Universal container item class used for all post types.
  323. *
  324. * @package Broken Link Checker
  325. * @author Janis Elsts
  326. * @access public
  327. */
  328. class blcAnyPostContainer extends blcContainer {
  329. var $default_field = 'post_content';
  330. /**
  331. * Get action links for this post.
  332. *
  333. * @param string $container_field Ignored.
  334. * @return array of action link HTML.
  335. */
  336. function ui_get_action_links($container_field = ''){
  337. $actions = array();
  338. //Fetch the post (it should be cached already)
  339. $post = $this->get_wrapped_object();
  340. if ( !$post ){
  341. return $actions;
  342. }
  343. $post_type_object = get_post_type_object($post->post_type);
  344. //Each post type can have its own cap requirements
  345. if ( current_user_can( $post_type_object->cap->edit_post, $this->container_id ) ){
  346. $actions['edit'] = sprintf(
  347. '<span class="edit"><a href="%s" title="%s">%s</a>',
  348. $this->get_edit_url(),
  349. $post_type_object->labels->edit_item,
  350. __('Edit')
  351. );
  352. //Trash/Delete link
  353. if ( current_user_can( $post_type_object->cap->delete_post, $this->container_id ) ){
  354. if ( $this->can_be_trashed() ) {
  355. $actions['trash'] = sprintf(
  356. "<span class='trash'><a class='submitdelete' title='%s' href='%s'>%s</a>",
  357. esc_attr(__('Move this item to the Trash')),
  358. get_delete_post_link($this->container_id, '', false),
  359. __('Trash')
  360. );
  361. } else {
  362. $actions['delete'] = sprintf(
  363. "<span><a class='submitdelete' title='%s' href='%s'>%s</a>",
  364. esc_attr(__('Delete this item permanently')),
  365. get_delete_post_link($this->container_id, '', true),
  366. __('Delete')
  367. );
  368. }
  369. }
  370. }
  371. //View/Preview link
  372. $title = get_the_title($this->container_id);
  373. if ( in_array($post->post_status, array('pending', 'draft')) ) {
  374. if ( current_user_can($post_type_object->cap->edit_post, $this->container_id) ){
  375. $actions['view'] = sprintf(
  376. '<span class="view"><a href="%s" title="%s" rel="permalink">%s</a>',
  377. esc_url( add_query_arg( 'preview', 'true', get_permalink($this->container_id) ) ),
  378. esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $title)),
  379. __('Preview')
  380. );
  381. }
  382. } elseif ( 'trash' != $post->post_status ) {
  383. $actions['view'] = sprintf(
  384. '<span class="view"><a href="%s" title="%s" rel="permalink">%s</a>',
  385. esc_url( get_permalink($this->container_id) ),
  386. esc_attr(sprintf(__('View &#8220;%s&#8221;'), $title)),
  387. __('View')
  388. );
  389. }
  390. return $actions;
  391. }
  392. /**
  393. * Get the HTML for displaying the post title in the "Source" column.
  394. *
  395. * @param string $container_field Ignored.
  396. * @param string $context How to filter the output. Optional, defaults to 'display'.
  397. * @return string HTML
  398. */
  399. function ui_get_source($container_field = '', $context = 'display'){
  400. $source = '<a class="row-title" href="%s" title="%s">%s</a>';
  401. $source = sprintf(
  402. $source,
  403. $this->get_edit_url(),
  404. esc_attr(__('Edit this item')),
  405. get_the_title($this->container_id)
  406. );
  407. return $source;
  408. }
  409. /**
  410. * Get edit URL for this container. Returns the URL of the Dashboard page where the item
  411. * associated with this container can be edited.
  412. *
  413. * @access protected
  414. *
  415. * @return string
  416. */
  417. function get_edit_url(){
  418. /*
  419. The below is a near-exact copy of the get_post_edit_link() function.
  420. Unfortunately we can't just call that function because it has a hardcoded
  421. caps-check which fails when called from the email notification script
  422. executed by Cron.
  423. */
  424. if ( !$post = $this->get_wrapped_object() ){
  425. return '';
  426. }
  427. $context = 'display';
  428. $action = '&amp;action=edit';
  429. $post_type_object = get_post_type_object( $post->post_type );
  430. if ( !$post_type_object ){
  431. return '';
  432. }
  433. return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
  434. }
  435. /**
  436. * Retrieve the post associated with this container.
  437. *
  438. * @access protected
  439. *
  440. * @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).
  441. * @return object Post data.
  442. */
  443. function get_wrapped_object($ensure_consistency = false){
  444. if( $ensure_consistency || is_null($this->wrapped_object) ){
  445. $this->wrapped_object = get_post($this->container_id);
  446. }
  447. return $this->wrapped_object;
  448. }
  449. /**
  450. * Update the post associated with this container.
  451. *
  452. * @access protected
  453. *
  454. * @return bool|WP_Error True on success, an error if something went wrong.
  455. */
  456. function update_wrapped_object(){
  457. if ( is_null($this->wrapped_object) ){
  458. return new WP_Error(
  459. 'no_wrapped_object',
  460. __('Nothing to update', 'broken-link-checker')
  461. );
  462. }
  463. $id = wp_update_post($this->wrapped_object);
  464. if ( $id != 0 ){
  465. return true;
  466. } else {
  467. return new WP_Error(
  468. 'update_failed',
  469. sprintf(__('Updating post %d failed', 'broken-link-checker'), $this->container_id)
  470. );
  471. }
  472. }
  473. /**
  474. * Get the base URL of the container. For posts, the post permalink is used
  475. * as the base URL when normalizing relative links.
  476. *
  477. * @return string
  478. */
  479. function base_url(){
  480. return get_permalink($this->container_id);
  481. }
  482. /**
  483. * Delete or trash the post corresponding to this container.
  484. * Will always move to trash instead of deleting if trash is enabled.
  485. *
  486. * @return bool|WP_error
  487. */
  488. function delete_wrapped_object(){
  489. //Note that we don't need to delete the synch record and instances here -
  490. //wp_delete_post()/wp_trash_post() will run the post_delete/trash hook,
  491. //which will be caught by blcPostContainerManager, which will in turn
  492. //delete anything that needs to be deleted.
  493. if ( EMPTY_TRASH_DAYS ){
  494. return $this->trash_wrapped_object();
  495. } else {
  496. if ( wp_delete_post($this->container_id, true) ){
  497. return true;
  498. } else {
  499. return new WP_Error(
  500. 'delete_failed',
  501. sprintf(
  502. __('Failed to delete post "%s" (%d)', 'broken-link-checker'),
  503. get_the_title($this->container_id),
  504. $this->container_id
  505. )
  506. );
  507. };
  508. }
  509. }
  510. /**
  511. * Move the post corresponding to this container to the Trash.
  512. *
  513. * @return bool|WP_Error
  514. */
  515. function trash_wrapped_object(){
  516. if ( !EMPTY_TRASH_DAYS ){
  517. return new WP_Error(
  518. 'trash_disabled',
  519. sprintf(
  520. __('Can\'t move post "%s" (%d) to the trash because the trash feature is disabled', 'broken-link-checker'),
  521. get_the_title($this->container_id),
  522. $this->container_id
  523. )
  524. );
  525. }
  526. $post = get_post($this->container_id);
  527. if ( $post->post_status == 'trash' ){
  528. //Prevent conflicts between post and custom field containers trying to trash the same post.
  529. //BUG: Post and custom field containers shouldn't wrap the same object
  530. return true;
  531. }
  532. if ( wp_trash_post($this->container_id) ){
  533. return true;
  534. } else {
  535. return new WP_Error(
  536. 'trash_failed',
  537. sprintf(
  538. __('Failed to move post "%s" (%d) to the trash', 'broken-link-checker'),
  539. get_the_title($this->container_id),
  540. $this->container_id
  541. )
  542. );
  543. };
  544. }
  545. /**
  546. * Check if the current user can delete/trash this post.
  547. *
  548. * @return bool
  549. */
  550. function current_user_can_delete(){
  551. $post = $this->get_wrapped_object();
  552. $post_type_object = get_post_type_object($post->post_type);
  553. return current_user_can( $post_type_object->cap->delete_post, $this->container_id );
  554. }
  555. function can_be_trashed(){
  556. return defined('EMPTY_TRASH_DAYS') && EMPTY_TRASH_DAYS;
  557. }
  558. }
  559. /**
  560. * Universal manager usable for most post types.
  561. *
  562. * @package Broken Link Checker
  563. * @access public
  564. */
  565. class blcAnyPostContainerManager extends blcContainerManager {
  566. var $container_class_name = 'blcAnyPostContainer';
  567. var $fields = array('post_content' => 'html');
  568. function init(){
  569. parent::init();
  570. //Notify the overlord that the post/container type that this instance is
  571. //responsible for is enabled.
  572. $overlord = blcPostTypeOverlord::getInstance();
  573. $overlord->post_type_enabled($this->container_type);
  574. }
  575. /**
  576. * Instantiate multiple containers of the container type managed by this class.
  577. *
  578. * @param array $containers Array of assoc. arrays containing container data.
  579. * @param string $purpose An optional code indicating how the retrieved containers will be used.
  580. * @param bool $load_wrapped_objects Preload wrapped objects regardless of purpose.
  581. *
  582. * @return array of blcPostContainer indexed by "container_type|container_id"
  583. */
  584. function get_containers($containers, $purpose = '', $load_wrapped_objects = false){
  585. $containers = $this->make_containers($containers);
  586. //Preload post data if it is likely to be useful later
  587. $preload = $load_wrapped_objects || in_array($purpose, array(BLC_FOR_DISPLAY, BLC_FOR_PARSING));
  588. if ( $preload ){
  589. $post_ids = array();
  590. foreach($containers as $container){
  591. $post_ids[] = $container->container_id;
  592. }
  593. $args = array('include' => implode(',', $post_ids));
  594. $posts = get_posts($args);
  595. foreach($posts as $post){
  596. $key = $this->container_type . '|' . $post->ID;
  597. if ( isset($containers[$key]) ){
  598. $containers[$key]->wrapped_object = $post;
  599. }
  600. }
  601. }
  602. return $containers;
  603. }
  604. /**
  605. * Create or update synchronization records for all posts.
  606. *
  607. * @param bool $forced If true, assume that all synch. records are gone and will need to be recreated from scratch.
  608. * @return void
  609. */
  610. function resynch($forced = false){
  611. $overlord = blcPostTypeOverlord::getInstance();
  612. $overlord->resynch($this->container_type, $forced);
  613. }
  614. /**
  615. * Get the message to display after $n posts have been deleted.
  616. *
  617. * @param int $n Number of deleted posts.
  618. * @return string A delete confirmation message, e.g. "5 posts were moved deleted"
  619. */
  620. function ui_bulk_delete_message($n){
  621. //Since the "Trash" feature has been introduced, calling wp_delete_post
  622. //doesn't actually delete the post (unless you set force_delete to True),
  623. //just moves it to the trash. So we pick the message accordingly.
  624. //(If possible, BLC *always* moves to trash instead of deleting permanently.)
  625. if ( function_exists('wp_trash_post') && EMPTY_TRASH_DAYS ){
  626. return blcAnyPostContainerManager::ui_bulk_trash_message($n);
  627. } else {
  628. $post_type_object = get_post_type_object($this->container_type);
  629. $type_name = '';
  630. if ( $this->container_type == 'post' || is_null($post_type_object) ){
  631. $delete_msg = _n("%d post deleted.", "%d posts deleted.", $n, 'broken-link-checker');
  632. } elseif ( $this->container_type == 'page' ){
  633. $delete_msg = _n("%d page deleted.", "%d pages deleted.", $n, 'broken-link-checker');
  634. } else {
  635. $delete_msg = _n('%d "%s" deleted.', '%d "%s" deleted.', $n, 'broken-link-checker');
  636. $type_name = ($n == 1 ? $post_type_object->labels->singular_name : $post_type_object->labels->name);
  637. }
  638. return sprintf($delete_msg, $n, $type_name);
  639. }
  640. }
  641. /**
  642. * Get the message to display after $n posts have been trashed.
  643. *
  644. * @param int $n Number of deleted posts.
  645. * @return string A confirmation message, e.g. "5 posts were moved to trash"
  646. */
  647. function ui_bulk_trash_message($n){
  648. $post_type_object = get_post_type_object($this->container_type);
  649. $type_name = '';
  650. if ( $this->container_type == 'post' || is_null($post_type_object) ){
  651. $delete_msg = _n("%d post moved to the Trash.", "%d posts moved to the Trash.", $n, 'broken-link-checker');
  652. } elseif ( $this->container_type == 'page' ){
  653. $delete_msg = _n("%d page moved to the Trash.", "%d pages moved to the Trash.", $n, 'broken-link-checker');
  654. } else {
  655. $delete_msg = _n('%d "%s" moved to the Trash.', '%d "%s" moved to the Trash.', $n, 'broken-link-checker');
  656. $type_name = ($n == 1 ? $post_type_object->labels->singular_name : $post_type_object->labels->name);
  657. }
  658. return sprintf($delete_msg, $n, $type_name);
  659. }
  660. }
  661. ?>