PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/system/plugins/undelete/undelete.plugin.php

https://github.com/HabariMag/habarimag-old
PHP | 274 lines | 206 code | 27 blank | 41 comment | 35 complexity | 428e6dba0e7f9b879d73fa15b17e543c MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Undelete Class
  4. *
  5. * This class provides undelete functionality for posts and comments, and
  6. * provides a trashcan interface for restoring items.
  7. *
  8. * @todo Document methods
  9. * @todo Provide an undo popup link like in gmail.
  10. **/
  11. class Undelete extends Plugin
  12. {
  13. /**
  14. * function action_plugin_activation
  15. * adds the "deleted" status type to the poststatus table
  16. * when this plugin is activated.
  17. **/
  18. public function action_plugin_activation( $file )
  19. {
  20. if ( realpath( $file ) == __FILE__ ) {
  21. Post::add_new_status( 'deleted', true );
  22. Options::set( 'undelete__style', '#primarycontent .deleted { background-color: #933; text-decoration: line-through; }' );
  23. }
  24. }
  25. /**
  26. * This function is executed when the filter "before_post_delete" is
  27. * called just before a post is to be deleted.
  28. * This filter should return a boolean value to indicate whether
  29. * the post should be deleted or not.
  30. * @param Boolean Whether to delete the post or not
  31. * @param Post The post object to potentially delete
  32. * @return Boolean Whether to delete the post or not
  33. **/
  34. function filter_post_delete_allow( $result, $post )
  35. {
  36. // all we need to do is set the post status to "deleted"
  37. // and then return false. The Post::delete() method will
  38. // see the false return value, and simply return, leaving
  39. // the post in the database.
  40. if ( $post->status != Post::status( 'deleted' ) && ACL::access_check( $post->get_access(), 'delete' ) ) {
  41. $post->info->prior_status = $post->status;
  42. $post->status = Post::status( 'deleted' );
  43. $post->update();
  44. return false;
  45. }
  46. else {
  47. return true;
  48. }
  49. }
  50. public function filter_post_actions($actions, $post)
  51. {
  52. if ( $post->status == Post::status('deleted') && ACL::access_check( $post->get_access(), 'delete' ) ) {
  53. $actions['remove']['label']= _t('Delete forever');
  54. $actions['remove']['title']= _t('Permanently delete this post');
  55. $remove = array_pop($actions);
  56. $restore = array(
  57. 'url' => 'javascript:unDelete.post('. $post->id . ');',
  58. 'title' => _t('Restore this item'),
  59. 'label' => _t('Restore')
  60. );
  61. array_push($actions, $restore, $remove);
  62. }
  63. return $actions;
  64. }
  65. public function filter_posts_manage_actions( $actions )
  66. {
  67. // get all the post types
  68. $require_any = array( 'own_posts' => 'delete' );
  69. $types = Post::list_active_post_types();
  70. foreach ($types as $key => $value ) {
  71. $require_any['post_' . $key] = 'delete';
  72. }
  73. if ( User::identify()->can_any( $require_any ) ) {
  74. $actions[] = array( 'action' => 'itemManage.update(\'restore\');return false;', 'title' => _t( 'Restore Selected Entries' ), 'label' => _t( 'Restore Selected' ) );
  75. }
  76. return $actions;
  77. }
  78. public function filter_admin_entries_action( $status_msg, $action, $posts )
  79. {
  80. $num = 0;
  81. switch ( $action ) {
  82. case 'restore':
  83. foreach( $posts as $post ) {
  84. $result = $this->undelete_post( $post->id );
  85. if ( $result ) {
  86. $num++;
  87. }
  88. }
  89. if ( $num == count( $posts ) ) {
  90. $status_msg = sprintf( _n('Restored %d post', 'Restored %d posts', $num ), $num );
  91. }
  92. else {
  93. $status_msg = _t( 'You did not have permission to restore some entries.' );
  94. }
  95. break;
  96. }
  97. return $status_msg;
  98. }
  99. /**
  100. * function undelete_post
  101. * This function reverts a post's status from 'deleted' to whatever
  102. * it previously was.
  103. **/
  104. private function undelete_post( $post_id )
  105. {
  106. $post = Post::get( array( 'id' => $post_id, 'status' => Post::status('any') ) );
  107. if ( $post->status == Post::status('deleted') && ACL::access_check( $post->get_access(), 'delete' ) ) {
  108. $post->status = $post->info->prior_status ? $post->info->prior_status : Post::status( 'draft' );
  109. unset( $post->info->prior_status );
  110. $post->update();
  111. EventLog::log(
  112. sprintf(_t('Post %1$s (%2$s) restored.'), $post->id, $post->slug),
  113. 'info', 'content', 'habari'
  114. );
  115. //scheduled post
  116. if ( $post->status == Post::status( 'scheduled' ) ) {
  117. Posts::update_scheduled_posts_cronjob();
  118. }
  119. return true;
  120. }
  121. else {
  122. return false;
  123. }
  124. }
  125. public function action_auth_ajax_undelete()
  126. {
  127. if ( $this->undelete_post($_POST['id']) ) {
  128. _e('Restored post %d', array($_POST['id']) );
  129. }
  130. else {
  131. _e( 'Could not restore post %d', array($_POST['id']) );
  132. }
  133. }
  134. private function get_perms()
  135. {
  136. $type_perms = array();
  137. $types = Post::list_active_post_types();
  138. foreach( $types as $key => $value ) {
  139. $perm = array( 'post_' . $key => ACL::get_bitmask( 'delete' ) );
  140. $types_perms = array_merge( $type_perms, $perm );
  141. }
  142. $perms = array( 'own_posts' => ACL::get_bitmask( 'delete' ), 'post_any' => ACL::get_bitmask( 'delete' ) );
  143. $perms = array_merge( $perms, $type_perms );
  144. return $perms;
  145. }
  146. public function filter_plugin_config( $actions, $plugin_id )
  147. {
  148. if ( $plugin_id == $this->plugin_id() ) {
  149. $actions[]= _t( 'Configure' );
  150. if ( User::identify()->can_any( $this->get_perms() ) ) {
  151. $actions[]= _t( 'Empty Trash' );
  152. }
  153. }
  154. return $actions;
  155. }
  156. public function action_plugin_ui( $plugin_id, $action )
  157. {
  158. if ( $plugin_id == $this->plugin_id() ) {
  159. switch ( $action ) {
  160. case _t( 'Configure' ):
  161. $ui = new FormUI( strtolower( get_class( $this ) ) );
  162. $ui->append( 'textarea', 'style', 'option:undelete__style', _t( 'Style declaration for deleted content:' ) );
  163. $ui->append( 'submit', 'save', _t( 'Save' ) );
  164. $ui->on_success( array( $this, 'updated_config' ) );
  165. $ui->out();
  166. break;
  167. case _t( 'Empty Trash' ):
  168. $ui = new FormUI( strtolower( get_class( $this ) ) );
  169. $ui->append( 'static', 'explanation', _t('Pressing this button will permanently delete all posts from the virtual trash can. You cannot undo this.') );
  170. $ui->append( 'submit', 'delete', _t( 'Delete All' ) );
  171. $ui->on_success( array( $this, 'deleted_all' ) );
  172. $ui->out();
  173. break;
  174. }
  175. }
  176. }
  177. public function updated_config( $ui )
  178. {
  179. $ui->save();
  180. return false;
  181. }
  182. public function deleted_all( $ui )
  183. {
  184. $count = self::delete_all();
  185. Session::notice(sprintf( _t('Permanently deleted %d posts'), $count));
  186. return false;
  187. }
  188. // This method will permanently delete all posts stored in the trash can
  189. private function delete_all()
  190. {
  191. $posts = Posts::get(array('status' => Post::status('deleted'), 'nolimit' => true));
  192. $count = 0;
  193. foreach($posts as $post) {
  194. if ( ACL::access_check( $post->get_access(), 'delete' ) ) {
  195. $post->delete();
  196. $count++;
  197. }
  198. }
  199. return $count;
  200. }
  201. /**
  202. * this method will inject some CSS into the <head>
  203. * of the public facing theme so that deleted posts
  204. * will show up differently
  205. */
  206. public function action_template_header()
  207. {
  208. // only show the style to logged in users
  209. if ( User::identify()->loggedin ) {
  210. echo '<style type="text/css">';
  211. Options::out( 'undelete__style' );
  212. echo '</style>';
  213. }
  214. }
  215. public function action_admin_header( $theme )
  216. {
  217. if ( $theme->page == 'posts' ) {
  218. Stack::add( 'admin_stylesheet', array($this->get_url() . '/undelete.css', 'screen') );
  219. $url = URL::get( 'auth_ajax', array('context' => 'undelete') );
  220. $script = <<<JS
  221. var unDelete = {
  222. post : function(id) {
  223. spinner.start();
  224. \$.post(
  225. '$url',
  226. {'id':id},
  227. function( result ) {
  228. spinner.stop();
  229. human_msg.display_msg( result );
  230. if ( $('.timeline').length ) {
  231. var loupeInfo = timeline.getLoupeInfo();
  232. itemManage.fetch( 0, loupeInfo.limit, true );
  233. timeline.updateLoupeInfo();
  234. }
  235. else {
  236. itemManage.fetch( 0, 20, false );
  237. }
  238. }
  239. );
  240. }
  241. }
  242. JS;
  243. Stack::add( 'admin_header_javascript', $script, 'undelete', 'admin' );
  244. }
  245. }
  246. }
  247. ?>