bpMod_ContentType_BlogPostExample

Example content type class for bp-moderation.

Defined (1)

The class is defined in the following location(s).

/examples/bpMod_ContentType_BlogPostExample.php  
  1. class bpMod_ContentType_BlogPostExample 
  2.  
  3. /** 
  4. * Initialize this content type 
  5. * register content type in bp-moderation (needs to be done at every pageload) 
  6. * If this content type needed to place some hooks no matter if it was active or not 
  7. * this would be the right place, instead hooks needed only when it is active 
  8. * are in bpMod_ContentType_BlogPostExample::init() 
  9. * @return bool if successfully initialized 
  10. */ 
  11. function bootstrap() 
  12. //check if bpModeration exists, especially the method to register content types that will be used just below 
  13. if (!method_exists('bpModeration', 'register_content_type')) { 
  14. return false; 
  15.  
  16. // internal slug used by bp-moderation to differentiate between content types (alfanumeric and underscore only) 
  17. $slug = 'blog_post_example'; 
  18.  
  19. // this is how content of this type get called in the backend 
  20. $label = __('Blog post example', 'your-text-domain'); 
  21.  
  22. //callbacks are used when some information/operation is needed, see documentation of each one 
  23. $callbacks = array( 
  24. 'info' => array(__CLASS__, 'info'), // mandatory 
  25. 'init' => array(__CLASS__, 'init'), // not mandatory but you'll probably need it anyway 
  26. 'edit' => array(__CLASS__, 'edit'), // not mandatory but suggested if can be edited 
  27. 'delete' => array(__CLASS__, 'delete'), // not mandatory but suggested if can be deleted 
  28. ); 
  29.  
  30. //bp-moderation can automatically put the flag button in activities generated from this content type, but needs to know what is their activity type 
  31. $activity_types = array('new_blog_post'); 
  32.  
  33. //register this content type in bp-moderation 
  34. return bpModeration::register_content_type($slug, $label, $callbacks, $activity_types); 
  35.  
  36. /** 
  37. * add hooks to place report links (init callback) 
  38. * bp-moderation calls this on pageload only if this content type has been activated 
  39. * from the site admin in the backend. Use this callback to hook around report links 
  40. * where your content types is displayed. 
  41. */ 
  42. function init() 
  43. // for blog posts we cam just append it after the content ... 
  44. add_filter('the_content', array(__CLASS__, 'append_link')); 
  45. // ... and after the excerpt 
  46. add_filter('the_excerpt', array(__CLASS__, 'append_link')); 
  47.  
  48. /** 
  49. * filter post content and excerpt adding the report link 
  50. * @param string $content 
  51. * @return string 
  52. */ 
  53. function append_link($content) 
  54. //only for posts 
  55. if ('post' != get_post_type()) { 
  56. return $content; 
  57.  
  58. //this is the method that generate the report link, read the method doc and look the defaults for details 
  59. //if this content has also activities is better to have id and id2 consistent with ids used in the activity 
  60. $link = bpModFrontend::get_link(array( 
  61. 'type' => 'blog_post_example',  
  62. 'id' => $GLOBALS['wpdb']->blogid, // the blog id 
  63. 'id2' => get_the_ID(), // the post id 
  64. 'author_id' => get_the_author_meta('id'),  
  65. 'unflagged_text' => __('Flag this post as inappropriate', 'your-text-domain') 
  66. )); 
  67.  
  68. //autop will run after this, so we space with two lines 
  69. return "$content\n\n$link"; 
  70.  
  71. /** 
  72. * get info on the content identified by id and id2 (info callback) 
  73. * this is mandatory and is probably the most important callback, if it does 
  74. * not return the requested info the content will not be flagged 
  75. * the requested info are: 
  76. * - user id of the author (if author is not a member read 'Contents generated by non members' section in integration readme.txt) 
  77. * - url of the content (the more accurate and future-proof, the better) 
  78. * - the date when the content has been posted, mysql format and utc 
  79. * in ms install this function could be called from any site, so if the contents 
  80. * are site specic (posts definitely are) you'll need to use 'switch_to_blog()' 
  81. * before using any blog specific function and 'restore_current_blog()' after. 
  82. * @param int $id item primary id (blog id) 
  83. * @param int $id2 item secondary id (post id) 
  84. * @return array|false author, url, date or false on failure 
  85. */ 
  86. function info($id, $id2) 
  87. switch_to_blog($id); 
  88.  
  89. $post = get_post($id2); 
  90.  
  91. restore_current_blog(); 
  92.  
  93. if (!$post) { 
  94. return false; 
  95.  
  96. return array( 
  97. 'author' => $post->post_author,  
  98. 'url' => home_url("?p=$id2"), //this link url don't depend on permalink structure and post slug 
  99. 'date' => $post->post_date_gmt,  
  100. ); 
  101.  
  102. /** 
  103. * get edit url for the content identified by id and id2 (edit callback) 
  104. * @see bpMod_ContentType_BlogPostExample::info() on why switch_to_blog() and 
  105. * restore_current_blog() are needed 
  106. * @param int $id item primary id (blog id) 
  107. * @param int $id2 item secondary id (post id) 
  108. * @return string edit url 
  109. */ 
  110. function edit($id, $id2) 
  111. switch_to_blog($id); 
  112.  
  113. $url = admin_url("post.php?post=$id2&action=edit"); 
  114.  
  115. restore_current_blog(); 
  116.  
  117. return $url; 
  118.  
  119. /** 
  120. * delete the content identified by id and id2 (delete callback) 
  121. * @see bpMod_ContentType_BlogPostExample::info() on why switch_to_blog() and 
  122. * restore_current_blog() are needed 
  123. * @param int $id item primary id (blog id) 
  124. * @param int $id2 item secondary id (post id) 
  125. * @return bool if the content has been delete (or positive also if it does not exist) 
  126. */ 
  127. function delete($id, $id2) 
  128. switch_to_blog($id); 
  129.  
  130. //return true also if the content does not exists 
  131. $r = !get_post($id2) || wp_delete_post($id2); 
  132.  
  133. restore_current_blog(); 
  134.  
  135. return $r;