MS_Model_Settings

Settings model.

Defined (1)

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

/app/model/class-ms-model-settings.php  
  1. class MS_Model_Settings extends MS_Model_Option { 
  2.  
  3. /** 
  4. * Singleton instance. 
  5. * @since 1.0.0 
  6. * @staticvar MS_Model_Settings 
  7. */ 
  8. public static $instance; 
  9.  
  10. /** 
  11. * Protection Message Type constants. 
  12. * User can set 3 different protection message defaults: 
  13. * - Whole page is protected 
  14. * - Shortcode content is protected 
  15. * - Read-more content is protected 
  16. * @since 1.0.0 
  17. */ 
  18. const PROTECTION_MSG_CONTENT = 'content'; 
  19. const PROTECTION_MSG_SHORTCODE = 'shortcode'; 
  20. const PROTECTION_MSG_MORE_TAG = 'more_tag'; 
  21.  
  22. /** 
  23. * ID of the model object. 
  24. * @since 1.0.0 
  25. * @var int 
  26. */ 
  27. protected $id = 'ms_plugin_settings'; 
  28.  
  29. /** 
  30. * Model name. 
  31. * @since 1.0.0 
  32. * @var string 
  33. */ 
  34. protected $name = 'Plugin settings'; 
  35.  
  36. /** 
  37. * Current db version. 
  38. * @since 1.0.0 
  39. * @var string 
  40. */ 
  41. protected $version = ''; 
  42.  
  43. /** 
  44. * Plugin enabled status indicator. 
  45. * @since 1.0.0 
  46. * @var boolean 
  47. */ 
  48. protected $plugin_enabled = false; 
  49.  
  50. /** 
  51. * Initial setup status indicator. 
  52. * Wizard mode. 
  53. * @since 1.0.0 
  54. * @var boolean 
  55. */ 
  56. protected $initial_setup = true; 
  57.  
  58. /** 
  59. * Is set to false when the first membership was created. 
  60. * @since 1.0.0 
  61. * @var boolean 
  62. */ 
  63. protected $is_first_membership = true; 
  64.  
  65. /** 
  66. * Is set to false when the first paid membership was created. 
  67. * @since 1.0.0 
  68. * @var boolean 
  69. */ 
  70. protected $is_first_paid_membership = true; 
  71.  
  72. /** 
  73. * Wizard step tracker. 
  74. * Indicate which step of the wizard. 
  75. * @since 1.0.0 
  76. * @var boolean 
  77. */ 
  78. protected $wizard_step = ''; 
  79.  
  80. /** 
  81. * Hide Membership2 Menu pointer indicator. 
  82. * Wizard mode. 
  83. * @since 1.0.0 
  84. * @var boolean 
  85. */ 
  86. protected $hide_wizard_pointer = false; 
  87.  
  88. /** 
  89. * Hide Toolbar for non admin users indicator. 
  90. * Wizard mode. 
  91. * @since 1.0.0 
  92. * @var boolean 
  93. */ 
  94. protected $hide_admin_bar = true; 
  95.  
  96. /** 
  97. * The currency used in the plugin. 
  98. * @since 1.0.0 
  99. * @var string 
  100. */ 
  101. protected $currency = 'USD'; 
  102.  
  103. /** 
  104. * The name used in the invoices. 
  105. * @since 1.0.0 
  106. * @var string 
  107. */ 
  108. protected $invoice_sender_name = ''; 
  109.  
  110. /** 
  111. * Global payments already set indicator. 
  112. * @since 1.0.0 
  113. * @var boolean 
  114. */ 
  115. protected $is_global_payments_set = false; 
  116.  
  117. /** 
  118. * Protection Messages. 
  119. * @since 1.0.0 
  120. * @var array 
  121. */ 
  122. protected $protection_messages = array(); 
  123.  
  124. /** 
  125. * How menu items are protected. 
  126. * @since 1.0.0 
  127. * @var string 
  128. */ 
  129. protected $menu_protection = 'item'; 
  130.  
  131. /** 
  132. * Media / Downloads settings. 
  133. * @since 1.0.0 
  134. * @var array 
  135. */ 
  136. protected $downloads = array( 
  137. 'protection_type' => MS_Rule_Media_Model::PROTECTION_TYPE_COMPLETE,  
  138. 'masked_url' => 'downloads',  
  139. ); 
  140.  
  141. /** 
  142. * Import flags 
  143. * When data was imported a flag can be set here to remember that some 
  144. * members come from there. 
  145. * @since 1.0.0 
  146. * @var array 
  147. */ 
  148. protected $import = array(); 
  149.  
  150. /** 
  151. * Special view. 
  152. * This defines a special view that is displayed when the plugin is loaded 
  153. * instead of the default plugin page that would be displayed. 
  154. * @since 1.0.0 
  155. * @var string 
  156. */ 
  157. protected $special_view = false; 
  158.  
  159. /** 
  160. * Get protection message types. 
  161. * @since 1.0.0 
  162. * @return string[] The available protection message types. 
  163. */ 
  164. public static function get_protection_msg_types() { 
  165. $types = array( 
  166. self::PROTECTION_MSG_CONTENT,  
  167. self::PROTECTION_MSG_SHORTCODE,  
  168. self::PROTECTION_MSG_MORE_TAG,  
  169. ); 
  170.  
  171. return apply_filters( 'ms_model_settings_get_protection_msg_types', $types ); 
  172.  
  173. /** 
  174. * Validate protection message type. 
  175. * @since 1.0.0 
  176. * @param string $type The protection message type to validate. 
  177. * @return boolean True if valid. 
  178. */ 
  179. public static function is_valid_protection_msg_type( $type ) { 
  180. $types = self::get_protection_msg_types(); 
  181.  
  182. return apply_filters( 
  183. 'ms_model_settings_is_valid_protection_msg_type',  
  184. in_array( $type, $types ) 
  185. ); 
  186.  
  187. /** 
  188. * Set protection message type. 
  189. * @since 1.0.0 
  190. * @param string $type The protection message type. 
  191. * @param string $msg The protection message. 
  192. * @param MS_Model_Membership $membership Optional. If defined the 
  193. * protection message specific for this membership will be set. 
  194. */ 
  195. public function set_protection_message( $type, $msg, $membership = null ) { 
  196. if ( self::is_valid_protection_msg_type( $type ) ) { 
  197. $key = $type; 
  198.  
  199. if ( $membership ) { 
  200. if ( $membership instanceof MS_Model_Membership ) { 
  201. $key .= '_' . $membership->id; 
  202. } elseif ( is_scalar( $membership ) ) { 
  203. $key .= '_' . $membership; 
  204.  
  205. if ( null === $msg ) { 
  206. unset( $this->protection_messages[ $key ] ); 
  207. } else { 
  208. $this->protection_messages[ $key ] = stripslashes( wp_kses_post( $msg ) ); 
  209.  
  210. do_action( 
  211. 'ms_model_settings_set_protection_message',  
  212. $type,  
  213. $msg,  
  214. $membership,  
  215. $this 
  216. ); 
  217.  
  218. /** 
  219. * Get protection message type. 
  220. * @since 1.0.0 
  221. * @param string $type The protection message type. 
  222. * @param MS_Model_Membership $membership Optional. If defined the 
  223. * protection message specific for this membership will be returned. 
  224. * @param bool $found This is set to true if the specified membership did 
  225. * override this message. 
  226. * @return string $msg The protection message. 
  227. */ 
  228. public function get_protection_message( $type, $membership = null, &$found = null ) { 
  229. $msg = ''; 
  230. $found = false; 
  231. if ( self::is_valid_protection_msg_type( $type ) ) { 
  232. $key = $type; 
  233.  
  234. if ( $membership ) { 
  235. if ( $membership instanceof MS_Model_Membership ) { 
  236. $key_override = $key . '_' . $membership->id; 
  237. } elseif ( is_scalar( $membership ) ) { 
  238. $key_override = $key . '_' . $membership; 
  239. } else { 
  240. $key_override = $key; 
  241. if ( isset( $this->protection_messages[ $key_override ] ) ) { 
  242. $key = $key_override; 
  243. $found = true; 
  244.  
  245. if ( isset( $this->protection_messages[ $key ] ) ) { 
  246. $msg = $this->protection_messages[ $key ]; 
  247. } else { 
  248. $msg = __( 'The content you are trying to access is only available to members. Sorry.', 'membership2' ); 
  249.  
  250. return apply_filters( 
  251. 'ms_model_settings_get_protection_message',  
  252. $msg,  
  253. $type,  
  254. $this 
  255. ); 
  256.  
  257. /** 
  258. * Activates a special view. 
  259. * Next time the plugin is loaded this special view is displayed. 
  260. * This should be set in MS_Model_Upgrade (or earlier) to ensure the special 
  261. * view is displayed on the current page request. 
  262. * @since 1.0.0 
  263. * @param string $name Name of the view to display. 
  264. */ 
  265. static public function set_special_view( $name ) { 
  266. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  267. $settings->special_view = $name; 
  268. $settings->save(); 
  269.  
  270. /** 
  271. * Returns the currently set special view. 
  272. * @since 1.0.0 
  273. * @return string Name of the view to display. 
  274. */ 
  275. static public function get_special_view() { 
  276. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  277. $view = $settings->special_view; 
  278. return $view; 
  279.  
  280. /** 
  281. * Deactivates the special view. 
  282. * @since 1.0.0 
  283. */ 
  284. static public function reset_special_view() { 
  285. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  286. $settings->special_view = false; 
  287. $settings->save(); 
  288.  
  289. /** 
  290. * Get available currencies. 
  291. * @since 1.0.0 
  292. * @return array { 
  293. * @type string $currency The currency. 
  294. * @type string $title The currency title. 
  295. * } 
  296. */ 
  297. public static function get_currencies() { 
  298. static $Currencies = null; 
  299.  
  300. if ( null === $Currencies ) { 
  301. $Currencies = apply_filters( 
  302. 'ms_model_settings_get_currencies',  
  303. array( 
  304. 'AUD' => __( 'AUD - Australian Dollar', 'membership2' ),  
  305. 'BRL' => __( 'BRL - Brazilian Real', 'membership2' ),  
  306. 'CAD' => __( 'CAD - Canadian Dollar', 'membership2' ),  
  307. 'CHF' => __( 'CHF - Swiss Franc', 'membership2' ),  
  308. 'CZK' => __( 'CZK - Czech Koruna', 'membership2' ),  
  309. 'DKK' => __( 'DKK - Danish Krone', 'membership2' ),  
  310. 'EUR' => __( 'EUR - Euro', 'membership2' ),  
  311. 'GBP' => __( 'GBP - Pound Sterling', 'membership2' ),  
  312. 'HKD' => __( 'HKD - Hong Kong Dollar', 'membership2' ),  
  313. 'HUF' => __( 'HUF - Hungarian Forint', 'membership2' ),  
  314. 'ILS' => __( 'ILS - Israeli Shekel', 'membership2' ),  
  315. 'JPY' => __( 'JPY - Japanese Yen', 'membership2' ),  
  316. 'MYR' => __( 'MYR - Malaysian Ringgits', 'membership2' ),  
  317. 'MXN' => __( 'MXN - Mexican Peso', 'membership2' ),  
  318. 'NOK' => __( 'NOK - Norwegian Krone', 'membership2' ),  
  319. 'NZD' => __( 'NZD - New Zealand Dollar', 'membership2' ),  
  320. 'PHP' => __( 'PHP - Philippine Pesos', 'membership2' ),  
  321. 'PLN' => __( 'PLN - Polish Zloty', 'membership2' ),  
  322. 'RUB' => __( 'RUB - Russian Ruble', 'membership2' ),  
  323. 'SEK' => __( 'SEK - Swedish Krona', 'membership2' ),  
  324. 'SGD' => __( 'SGD - Singapore Dollar', 'membership2' ),  
  325. 'TWD' => __( 'TWD - Taiwan New Dollars', 'membership2' ),  
  326. 'THB' => __( 'THB - Thai Baht', 'membership2' ),  
  327. 'USD' => __( 'USD - U.S. Dollar', 'membership2' ),  
  328. 'ZAR' => __( 'ZAR - South African Rand', 'membership2' ),  
  329. ); 
  330.  
  331. return $Currencies; 
  332.  
  333. /** 
  334. * Set specific property. 
  335. * @since 1.0.0 
  336. * @param string $property The name of a property to associate. 
  337. * @param mixed $value The value of a property. 
  338. */ 
  339. public function __set( $property, $value ) { 
  340. if ( property_exists( $this, $property ) ) { 
  341. switch ( $property ) { 
  342. case 'currency': 
  343. if ( array_key_exists( $value, self::get_currencies() ) ) { 
  344. $this->$property = $value; 
  345. break; 
  346.  
  347. case 'invoice_sender_name': 
  348. $this->$property = sanitize_text_field( $value ); 
  349. break; 
  350.  
  351. case 'plugin_enabled': 
  352. case 'initial_setup': 
  353. case 'is_first_membership': 
  354. case 'hide_admin_bar': 
  355. $this->$property = lib3()->is_true( $value ); 
  356. break; 
  357.  
  358. default: 
  359. $this->$property = $value; 
  360. break; 
  361. } else { 
  362. switch ( $property ) { 
  363. case 'protection_type': 
  364. if ( MS_Rule_Media_Model::is_valid_protection_type( $value ) ) { 
  365. $this->downloads['protection_type'] = $value; 
  366. break; 
  367.  
  368. case 'masked_url': 
  369. $this->downloads['masked_url'] = sanitize_text_field( $value ); 
  370. break; 
  371.  
  372. /** 
  373. * Returns a specific property. 
  374. * @since 1.0.0 
  375. * @param string $property The name of a property. 
  376. * @return mixed $value The value of a property. 
  377. */ 
  378. public function __get( $property ) { 
  379. $value = null; 
  380.  
  381. switch ( $property ) { 
  382. case 'menu_protection': 
  383. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_ADV_MENUS ) ) { 
  384. $value = 'item'; 
  385. } else { 
  386. $value = $this->menu_protection; 
  387. break; 
  388.  
  389. default: 
  390. if ( property_exists( $this, $property ) ) { 
  391. $value = $this->$property; 
  392. } else { 
  393. switch ( $property ) { 
  394. case 'currency_symbol': 
  395. // Same translation table in: 
  396. // -> ms-view-membership-setup-payment.js 
  397. $symbol = $this->currency; 
  398. switch ( $symbol ) { 
  399. case 'USD': $symbol = '$'; break; 
  400. case 'EUR': $symbol = '*'; break; 
  401. case 'JPY': $symbol = ''; break; 
  402. $value = $symbol; 
  403.  
  404. return apply_filters( 'ms_model_settings__get', $value, $property, $this );