MS_Rule_Shortcode_Model

Membership Shortcode Rule class.

Defined (1)

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

/app/rule/shortcode/class-ms-rule-shortcode-model.php  
  1. class MS_Rule_Shortcode_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_Shortcode::RULE_ID; 
  9.  
  10. /** 
  11. * Holds the membership-IDs of all memberships of the user. 
  12. * @since 1.0.0 
  13. * @var array 
  14. */ 
  15. static protected $membership_ids = array(); 
  16.  
  17. /** 
  18. * Protect content shortcode. 
  19. * @since 1.0.0 
  20. * @var string 
  21. */ 
  22. const PROTECT_CONTENT_SHORTCODE = 'ms-protect-content'; 
  23.  
  24. /** 
  25. * Returns the active flag for a specific rule. 
  26. * State depends on Add-on 
  27. * @since 1.0.0 
  28. * @return bool 
  29. */ 
  30. static public function is_active() { 
  31. return MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SHORTCODE ); 
  32.  
  33. /** 
  34. * Verify access to the current content. 
  35. * This rule will return NULL (not relevant), because shortcodes are 
  36. * replaced inside the page content instead of protecting the whole page. 
  37. * @since 1.0.0 
  38. * @param string $id The content id to verify access. 
  39. * @return bool|null True if has access, false otherwise. 
  40. * Null means: Rule not relevant for current page. 
  41. */ 
  42. public function has_access( $id, $admin_has_access = true ) { 
  43. return null; 
  44.  
  45. /** 
  46. * Set initial protection. 
  47. * Add [ms-protect-content] shortcode to protect membership content inside post. 
  48. * @since 1.0.0 
  49. */ 
  50. public function protect_content() { 
  51. parent::protect_content(); 
  52.  
  53. self::$membership_ids[] = $this->membership_id; 
  54.  
  55. add_shortcode( 
  56. self::PROTECT_CONTENT_SHORTCODE,  
  57. array( __CLASS__, 'protect_content_shortcode' ) 
  58. ); 
  59.  
  60. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SHORTCODE ) ) { 
  61. global $shortcode_tags; 
  62. $exclude = MS_Helper_Shortcode::get_membership_shortcodes(); 
  63.  
  64. foreach ( $shortcode_tags as $shortcode => $callback_funciton ) { 
  65. if ( in_array( $shortcode, $exclude ) ) { 
  66. continue; 
  67. if ( ! parent::has_access( $shortcode ) ) { 
  68. $shortcode_tags[ $shortcode ] = array( 
  69. &$this,  
  70. 'do_protected_shortcode',  
  71. ); 
  72.  
  73. /** 
  74. * Do protected shortcode [do_protected_shortcode]. 
  75. * This shortcode is executed to replace a protected shortcode. 
  76. * @since 1.0.0 
  77. */ 
  78. public function do_protected_shortcode() { 
  79. $content = null; 
  80. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  81. $msg = $settings->get_protection_message( 
  82. MS_Model_Settings::PROTECTION_MSG_SHORTCODE,  
  83. $this->membership_id 
  84. ); 
  85.  
  86. if ( $msg ) { 
  87. $content = $msg; 
  88. } else { 
  89. $content = __( 'Shortcode content protected.', 'membership2' ); 
  90.  
  91. return apply_filters( 
  92. 'ms_model_shortcode_do_protected_shortcode_content',  
  93. $content,  
  94. $this 
  95. ); 
  96.  
  97. /** 
  98. * Do membership content protection shortcode. 
  99. * self::PROTECT_CONTENT_SHORTCODE 
  100. * Verify if content is protected comparing to self::$membership_ids. 
  101. * @since 1.0.0 
  102. * @param array $atts The shortcode attributes. 
  103. * @param string $content The content inside the shortcode. 
  104. * @param string $code The shortcode code. 
  105. * @return string The shortcode output 
  106. */ 
  107. static public function protect_content_shortcode( $atts, $content = null, $code = '' ) { 
  108. $atts = apply_filters( 
  109. 'ms_model_shortcode_protect_content_shortcode_atts',  
  110. shortcode_atts( 
  111. array( 
  112. 'id' => '',  
  113. 'access' => true,  
  114. 'silent' => false,  
  115. 'msg' => false,  
  116. ),  
  117. $atts 
  118. ); 
  119. extract( $atts ); 
  120.  
  121. $membership_ids = explode( ', ', $id ); 
  122.  
  123. if ( $silent ) { 
  124. $msg = ''; 
  125. } else { 
  126. if ( ! is_string( $msg ) || ! strlen( $msg ) ) { 
  127. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  128. $membership_id = apply_filters( 'ms_detect_membership_id', 0 ); 
  129. $msg = $settings->get_protection_message( 
  130. MS_Model_Settings::PROTECTION_MSG_SHORTCODE,  
  131. $membership_id 
  132. ); 
  133.  
  134. $access = lib3()->is_true( $access ); 
  135.  
  136. if ( ! $access ) { 
  137. // No access to member of membership_ids 
  138.  
  139. if ( self::is_member_of( $membership_ids ) ) { 
  140. // User belongs to these memberships and therefore cannot see 
  141. // this content... 
  142.  
  143. if ( $silent ) { 
  144. // Silent protection: Do not show a message, simply hide it 
  145. $content = ''; 
  146. } else { 
  147. $content = '<div class="ms-protection-msg">'; 
  148. if ( ! empty( $msg ) ) { 
  149. $content .= $msg; 
  150. } else { 
  151. $membership_names = MS_Model_Membership::get_membership_names( 
  152. array( 'post__in' => $membership_ids ) 
  153. ); 
  154. $content .= __( 'No access to members of: ', 'membership2' ); 
  155. $content .= implode( ', ', $membership_names ); 
  156. $content .= '</div>'; 
  157. } else { 
  158. // Give access to member of membership_ids 
  159.  
  160. if ( ! self::is_member_of( $membership_ids ) ) { 
  161. // User does not belong to these memberships and therefore 
  162. // cannot see this content... 
  163.  
  164. if ( $silent ) { 
  165. // Silent protection: Do not show a message, simply hide it 
  166. $content = ''; 
  167. } else { 
  168. $content = '<div class="ms-protection-msg">'; 
  169. if ( ! empty( $msg ) ) { 
  170. $content .= $msg; 
  171. } else { 
  172. $membership_names = MS_Model_Membership::get_membership_names( 
  173. array( 'post__in' => $membership_ids ) 
  174. ); 
  175. $content .= __( 'Content protected to members of: ', 'membership2' ); 
  176. $content .= implode( ', ', $membership_names ); 
  177. $content .= '</div>'; 
  178.  
  179. return apply_filters( 
  180. 'ms_rule_shortcode_model_protect_content_shortcode_content',  
  181. do_shortcode( $content ),  
  182. $atts,  
  183. $content,  
  184. $code 
  185. ); 
  186.  
  187. /** 
  188. * For Admin-Users only: 
  189. * The [ms-protected-content] shortcode is replaced with some debug values 
  190. * for better understanding of the page structure. 
  191. * @since 1.0.0 
  192. * @param array $atts The shortcode attributes. 
  193. * @param string $content The content inside the shortcode. 
  194. * @return string The shortcode output 
  195. */ 
  196. public function debug_protect_content_shortcode( $atts, $content = '' ) { 
  197. $do_debug = true; 
  198.  
  199. /** 
  200. * This wp-config setting defines the default state of the shortcode 
  201. * debugging flag. 
  202. */ 
  203. if ( defined( 'MS_NO_SHORTCODE_PREVIEW' ) && MS_NO_SHORTCODE_PREVIEW ) { 
  204. $do_debug = false; 
  205.  
  206. /** 
  207. * Use this filter to disable the protected-content debugging 
  208. * information for admin users. 
  209. * The content will always be displayed for admin users. 
  210. */ 
  211. $do_debug = apply_filters( 
  212. 'ms_model_shortcode_debug_protected_content',  
  213. $do_debug,  
  214. $atts,  
  215. $content 
  216. ); 
  217.  
  218. $content = do_shortcode( $content ); 
  219.  
  220. if ( ! $do_debug ) { 
  221. return $content; 
  222.  
  223. $atts = apply_filters( 
  224. 'ms_model_shortcode_protect_content_shortcode_atts',  
  225. shortcode_atts( 
  226. array( 
  227. 'id' => '',  
  228. 'access' => true,  
  229. 'silent' => false,  
  230. 'msg' => false,  
  231. ),  
  232. $atts 
  233. ); 
  234. extract( $atts ); 
  235.  
  236. if ( lib3()->is_true( $access ) ) { 
  237. $msg_access = __( 'Visible for members of', 'membership2' ); 
  238. $alt_msg1 = __( 'Other users will see', 'membership2' ); 
  239. $alt_msg2 = __( 'Other uses will see nothing', 'membership2' ); 
  240. } else { 
  241. $msg_access = __( 'Hidden from members of', 'membership2' ); 
  242. $alt_msg1 = __( 'Those users will see', 'membership2' ); 
  243. $alt_msg2 = __( 'Those uses will see nothing', 'membership2' ); 
  244.  
  245. if ( $msg ) { 
  246. $msg_alt = sprintf( '%s: %s', $alt_msg1, $msg ); 
  247. } else { 
  248. $msg_alt = $alt_msg2; 
  249.  
  250. $membership_ids = explode( ', ', $id ); 
  251. $memberships = array(); 
  252. foreach ( $membership_ids as $membership_id ) { 
  253. $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id ); 
  254. $memberships[] = $membership->get_name_tag(); 
  255.  
  256. $css = '<style> 
  257. .ms-membership { 
  258. display: inline-block; 
  259. border-radius: 3px; 
  260. color: #FFF; 
  261. background: #888888; 
  262. padding: 1px 5px; 
  263. font-size: 12px; 
  264. height: 20px; 
  265. line-height: 20px; 
  266. .ms-protected-info { 
  267. border: 1px solid rgba(0, 0, 0, 0.07); 
  268. .ms-protected-info:hover { 
  269. border: 1px solid rgba(0, 0, 0, 0.3); 
  270. .ms-protected-info .ms-details,  
  271. .ms-protected-info .ms-alternate-msg { 
  272. background: #EEE; 
  273. padding: 4px; 
  274. font-size: 12px; 
  275. color: #666; 
  276. opacity: 0.25; 
  277. .ms-protected-info:hover .ms-details,  
  278. .ms-protected-info:hover .ms-alternate-msg { 
  279. opacity: 1; 
  280. .ms-protected-info .ms-contents { 
  281. padding: 4px 8px; 
  282. </style>'; 
  283.  
  284. $code = sprintf( 
  285. '<div class="ms-protected-info" title="%5$s"><div class="ms-details">%3$s: %4$s</div><div class="ms-contents">%1$s</div><div class="ms-alternate-msg">%2$s</div></div>',  
  286. $content,  
  287. $msg_alt,  
  288. $msg_access,  
  289. implode( ' ', $memberships ),  
  290. __( 'This information is only displayed for admin users', 'membership2' ) 
  291. ); 
  292.  
  293. return $css . $code; 
  294.  
  295. /** 
  296. * Returns true when the current user is a member of one of the specified 
  297. * memberships. 
  298. * @since 1.0.0 
  299. * @return bool 
  300. */ 
  301. static protected function is_member_of( $ids ) { 
  302. $result = false; 
  303.  
  304. if ( empty( $ids ) ) { 
  305. $result = true; 
  306. } else { 
  307. if ( ! is_array( $ids ) ) { 
  308. $ids = array( $ids ); 
  309.  
  310. foreach ( self::$membership_ids as $the_id ) { 
  311. if ( in_array( $the_id, $ids ) ) { 
  312. $result = true; 
  313. break; 
  314.  
  315. return $result; 
  316.  
  317. /** 
  318. * Get the total content count. 
  319. * @since 1.0.0 
  320. * @param $args The query post args. Not used. 
  321. * @return int The total content count. 
  322. */ 
  323. public function get_content_count( $args = null ) { 
  324. $args['posts_per_page'] = 0; 
  325. $args['offset'] = false; 
  326. $items = $this->get_contents( $args ); 
  327. $count = count( $items ); 
  328.  
  329. return apply_filters( 
  330. 'ms_rule_shortcode_model_get_content_count',  
  331. $count,  
  332. $this 
  333. ); 
  334.  
  335. /** 
  336. * Get content to protect. 
  337. * @since 1.0.0 
  338. * @param $args The filter args 
  339. * @return array The contents array. 
  340. */ 
  341. public function get_contents( $args = null ) { 
  342. global $shortcode_tags; 
  343.  
  344. $exclude = MS_Helper_Shortcode::get_membership_shortcodes(); 
  345. $contents = array(); 
  346.  
  347. foreach ( $shortcode_tags as $key => $function ) { 
  348. if ( in_array( $key, $exclude ) ) { 
  349. continue; 
  350.  
  351. // Search the shortcode-tag... 
  352. if ( ! empty( $args['s'] ) ) { 
  353. if ( false === stripos( $key, $args['s'] ) ) { 
  354. continue; 
  355.  
  356. $contents[ $key ] = new StdClass(); 
  357. $contents[ $key ]->id = $key; 
  358. $contents[ $key ]->name = "[$key]"; 
  359. $contents[ $key ]->type = MS_Rule_Shortcode::RULE_ID; 
  360. $contents[ $key ]->access = $this->get_rule_value( $key ); 
  361.  
  362. $filter = $this->get_exclude_include( $args ); 
  363. if ( is_array( $filter->include ) ) { 
  364. $contents = array_intersect_key( $contents, array_flip( $filter->include ) ); 
  365. } elseif ( is_array( $filter->exclude ) ) { 
  366. $contents = array_diff_key( $contents, array_flip( $filter->exclude ) ); 
  367.  
  368. if ( ! empty( $args['posts_per_page'] ) ) { 
  369. $total = $args['posts_per_page']; 
  370. $offset = ! empty( $args['offset'] ) ? $args['offset'] : 0; 
  371. $contents = array_slice( $contents, $offset, $total ); 
  372.  
  373. return apply_filters( 
  374. 'ms_rule_shortcode_model_get_contents',  
  375. $contents 
  376. ); 
  377.