MS_Rule_Content_Model

Membership Comment Rule class.

Defined (1)

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

/app/rule/content/class-ms-rule-content-model.php  
  1. class MS_Rule_Content_Model extends MS_Rule { 
  2.  
  3. /** 
  4. * Rule type. 
  5. * @since 1.0.0 
  6. * @var string $rule_type 
  7. */ 
  8. protected $rule_type = MS_Rule_Content::RULE_ID; 
  9.  
  10. /** 
  11. * Available special pages 
  12. * @since 1.0.0 
  13. * @var array 
  14. */ 
  15. protected $_content = null; 
  16.  
  17. /** 
  18. * Comment content ID. 
  19. * @since 1.0.0 
  20. * @var string $content_id 
  21. */ 
  22. const CONTENT_ID = 'content'; 
  23.  
  24. /** 
  25. * Rule value constants. 
  26. * @since 1.0.0 
  27. * @var int 
  28. */ 
  29. const COMMENT_NO_ACCESS = 'cmt_none'; 
  30. const COMMENT_READ = 'cmt_read'; 
  31. const COMMENT_WRITE = 'cmt_full'; 
  32. const MORE_LIMIT = 'no_more'; 
  33.  
  34. /** 
  35. * Flag of the final comment access level. 
  36. * When an user is member of multiple memberships with different 
  37. * comment-access-restrictions then the MOST GENEROUS access will be granted. 
  38. * @since 1.0.0 
  39. * @var string 
  40. */ 
  41. protected static $comment_access = self::COMMENT_NO_ACCESS; 
  42.  
  43. /** 
  44. * Set to true, if the user did not specify any comment protection. 
  45. * This means that the default logic should be used... 
  46. * @since 1.0.0 
  47. * @var bool 
  48. */ 
  49. protected static $comment_public = null; 
  50.  
  51. /** 
  52. * The message displayed below the "read more" mark. 
  53. * @since 1.0.0 
  54. * @var string 
  55. */ 
  56. protected $protection_message = ''; 
  57.  
  58. /** 
  59. * Verify access to the current content. 
  60. * This rule will return NULL (not relevant), because the comments are 
  61. * protected via WordPress hooks instead of protecting the whole page. 
  62. * @since 1.0.0 
  63. * @param string $id The content id to verify access. 
  64. * @return bool|null True if has access, false otherwise. 
  65. * Null means: Rule not relevant for current page. 
  66. */ 
  67. public function has_access( $id, $admin_has_access = true ) { 
  68. return null; 
  69.  
  70. /** 
  71. * Set initial protection. 
  72. * @since 1.0.0 
  73. */ 
  74. public function protect_content() { 
  75. parent::protect_content(); 
  76.  
  77. // ********* COMMENTS ********** 
  78.  
  79. // No comments on special pages (signup, account, ...) 
  80. $this->add_filter( 'the_content', 'check_special_page' ); 
  81.  
  82. /** 
  83. * We find the public comment access once. 
  84. * This is the access ganted to guests or memberships that do not define 
  85. * an explicit comment access rule. 
  86. */ 
  87. if ( null === self::$comment_public ) { 
  88. $base_rule = MS_Model_Membership::get_base()->get_rule( $this->rule_type ); 
  89. if ( null === $base_rule->get_rule_value( self::COMMENT_WRITE ) ) { 
  90. self::$comment_public = self::COMMENT_WRITE; 
  91. } elseif ( null === $base_rule->get_rule_value( self::COMMENT_READ ) ) { 
  92. self::$comment_public = self::COMMENT_READ; 
  93. } else { 
  94. self::$comment_public = self::COMMENT_NO_ACCESS; 
  95.  
  96. // Find the most generous comment access rule. 
  97. $has_full = $this->get_rule_value( self::COMMENT_WRITE ); 
  98. $has_read = $this->get_rule_value( self::COMMENT_READ ); 
  99. $has_none = $this->get_rule_value( self::COMMENT_NO_ACCESS ); 
  100.  
  101. if ( true === $has_full ) { 
  102. // Membership allows full comment access. 
  103. self::$comment_access = self::COMMENT_WRITE; 
  104. } elseif ( true === $has_read ) { 
  105. // Membership allows read-only access. 
  106. if ( self::$comment_access == self::COMMENT_NO_ACCESS ) { 
  107. self::$comment_access = self::COMMENT_READ; 
  108. } elseif ( true === $has_none ) { 
  109. // Membership does not allow any comment access. 
  110. // (no change, this is the default access level) 
  111. } else { 
  112. // This membership does not define a comment access: Use public access! 
  113. self::$comment_access = self::$comment_public; 
  114.  
  115. $this->add_action( 
  116. 'ms_setup_protection_done',  
  117. 'protect_comments' 
  118. ); 
  119.  
  120. // ********** READ MORE ********** 
  121.  
  122. $this->protection_message = MS_Plugin::instance()->settings->get_protection_message( 
  123. MS_Model_Settings::PROTECTION_MSG_MORE_TAG,  
  124. $this->membership_id 
  125. ); 
  126.  
  127. if ( ! parent::has_access( self::MORE_LIMIT ) ) { 
  128. $this->add_filter( 'the_content_more_link', 'show_moretag_protection', 99, 2 ); 
  129. $this->add_filter( 'the_content', 'replace_more_tag_content', 1 ); 
  130. $this->add_filter( 'the_content_feed', 'replace_more_tag_content', 1 ); 
  131.  
  132. // ********* COMMENTS ********** 
  133.  
  134. /** 
  135. * Setup the comment permissions after all membership rules were parsed. 
  136. * @since 1.0.0 
  137. */ 
  138. public function protect_comments() { 
  139. static $Done = false; 
  140.  
  141. if ( $Done ) { return; } 
  142. $Done = true; 
  143.  
  144. switch ( self::$comment_access ) { 
  145. case self::COMMENT_WRITE: 
  146. // Don't change the inherent comment status. 
  147. break; 
  148.  
  149. case self::COMMENT_READ: 
  150. $this->add_filter( 'comment_form_before', 'hide_form_start', 1 ); 
  151. $this->add_filter( 'comment_form_after', 'hide_form_end', 99 ); 
  152. add_filter( 'comment_reply_link', '__return_null', 99 ); 
  153. break; 
  154.  
  155. case self::COMMENT_NO_ACCESS: 
  156. add_filter( 'comments_open', '__return_false', 99 ); 
  157. add_filter( 'get_comments_number', '__return_zero', 99 ); 
  158. break; 
  159.  
  160. /** 
  161. * Before the comment form is output we start buffering. 
  162. * @since 1.0.0 
  163. */ 
  164. public function hide_form_start() { 
  165. ob_start(); 
  166.  
  167. /** 
  168. * At the end of the comment form we clear the buffer: The form is gone! 
  169. * @since 1.0.0 
  170. */ 
  171. public function hide_form_end() { 
  172. ob_end_clean(); 
  173.  
  174. /** 
  175. * Close comments for membership special pages. 
  176. * Related Action Hooks: 
  177. * - the_content 
  178. * @since 1.0.0 
  179. * @param string $content The content to filter. 
  180. */ 
  181. public function check_special_page( $content ) { 
  182. if ( MS_Model_Pages::is_membership_page() ) { 
  183. add_filter( 'comments_open', '__return_false', 100 ); 
  184.  
  185. return apply_filters( 
  186. 'ms_rule_content_model_check_special_page',  
  187. $content,  
  188. $this 
  189. ); 
  190.  
  191. // ********** READ MORE ********** 
  192.  
  193. /** 
  194. * Show more tag protection message. 
  195. * Related Action Hooks: 
  196. * - the_content_more_link 
  197. * @since 1.0.0 
  198. * @param string $more_tag_link the more tag link before filter. 
  199. * @param string $more_tag The more tag content before filter. 
  200. * @return string The protection message. 
  201. */ 
  202. public function show_moretag_protection( $more_tag_link, $more_tag ) { 
  203. $msg = stripslashes( $this->protection_message ); 
  204.  
  205. return apply_filters( 
  206. 'ms_rule_more_model_show_moretag_protection',  
  207. $msg,  
  208. $more_tag_link,  
  209. $more_tag,  
  210. $this 
  211. ); 
  212.  
  213. /** 
  214. * Replace more tag 
  215. * Related Action Hooks: 
  216. * - the_content 
  217. * - the_content_feed 
  218. * @since 1.0.0 
  219. * @param string $the_content The post content before filter. 
  220. * @return string The content replaced by more tag content. 
  221. */ 
  222. public function replace_more_tag_content( $the_content ) { 
  223. $more_starts_at = strpos( $the_content, '<span id="more-' ); 
  224.  
  225. if ( false !== $more_starts_at ) { 
  226. $the_content = substr( $the_content, 0, $more_starts_at ); 
  227. $the_content .= stripslashes( $this->protection_message ); 
  228.  
  229. return apply_filters( 
  230. 'ms_rule_more_model_replace_more_tag_content',  
  231. $the_content,  
  232. $this 
  233. ); 
  234.  
  235. // ********** ADMIN FUNCTIONS ********** 
  236.  
  237. /** 
  238. * Returns a list of special pages that can be configured by this rule. 
  239. * @since 1.0.0 
  240. * @param bool $flat If set to true then all pages are in the same 
  241. * hierarchy (no sub-arrays). 
  242. * @return array List of special pages. 
  243. */ 
  244. protected function get_rule_items() { 
  245. if ( ! is_array( $this->_content ) ) { 
  246. $this->_content = array(); 
  247.  
  248. $this->_content[self::COMMENT_NO_ACCESS] = (object) array( 
  249. 'label' => __( 'Comments: No Access', 'membership2' ),  
  250. ); 
  251. $this->_content[self::COMMENT_READ] = (object) array( 
  252. 'label' => __( 'Comments: Read Only Access', 'membership2' ),  
  253. ); 
  254. $this->_content[self::COMMENT_WRITE] = (object) array( 
  255. 'label' => __( 'Comments: Read and Write Access', 'membership2' ),  
  256. ); 
  257. $this->_content[self::MORE_LIMIT] = (object) array( 
  258. 'label' => __( 'Hide "read more" content', 'membership2' ),  
  259. ); 
  260.  
  261. return $this->_content; 
  262.  
  263. /** 
  264. * Count protection rules quantity. 
  265. * @since 1.0.0 
  266. * @return int $count The rule count result. 
  267. */ 
  268. public function count_rules( $args = null ) { 
  269. $count = count( $this->get_contents( $args ) ); 
  270.  
  271. return apply_filters( 
  272. 'ms_rule_content_model_count_rules',  
  273. $count,  
  274. $this 
  275. ); 
  276.  
  277. /** 
  278. * Get content to protect. 
  279. * @since 1.0.0 
  280. * @param $args Optional. Not used. 
  281. * @return array The content. 
  282. */ 
  283. public function get_contents( $args = null ) { 
  284. $items = $this->get_rule_items(); 
  285. $contents = array(); 
  286.  
  287. foreach ( $items as $key => $data ) { 
  288. $content = (object) array(); 
  289.  
  290. // Search the special page name... 
  291. if ( ! empty( $args['s'] ) ) { 
  292. if ( false === stripos( $data->label, $args['s'] ) ) { 
  293. continue; 
  294.  
  295. $content->id = $key; 
  296. $content->type = MS_Rule_Content::RULE_ID; 
  297. $content->name = $data->label; 
  298. $content->post_title = $data->label; 
  299. $content->access = $this->get_rule_value( $content->id ); 
  300.  
  301. $contents[ $content->id ] = $content; 
  302.  
  303. return apply_filters( 
  304. 'ms_rule_content_model_get_content',  
  305. $contents,  
  306. $args,  
  307. $this 
  308. ); 
  309.