MS_Model_Addon

Manage Add-ons.

Defined (1)

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

/app/model/class-ms-model-addon.php  
  1. class MS_Model_Addon extends MS_Model_Option { 
  2.  
  3. /** 
  4. * Add-on name constants. 
  5. * @deprecated Since 1.1.0 the Add-On constants are deprecated. 
  6. * Use the appropriate hooks to register new addons! 
  7. * Example: See the "Taxamo" addon 
  8. * @since 1.0.0 
  9. * @var string 
  10. */ 
  11. const ADDON_MULTI_MEMBERSHIPS = 'multi_memberships'; 
  12. const ADDON_POST_BY_POST = 'post_by_post'; 
  13. const ADDON_URL_GROUPS = 'url_groups'; 
  14. const ADDON_CPT_POST_BY_POST = 'cpt_post_by_post'; 
  15. const ADDON_TRIAL = 'trial'; 
  16. const ADDON_MEDIA = 'media'; 
  17. const ADDON_SHORTCODE = 'shortcode'; 
  18. const ADDON_AUTO_MSGS_PLUS = 'auto_msgs_plus'; 
  19. const ADDON_SPECIAL_PAGES = 'special_pages'; 
  20. const ADDON_ADV_MENUS = 'adv_menus'; 
  21. const ADDON_ADMINSIDE = 'adminside'; 
  22. const ADDON_MEMBERCAPS = 'membercaps'; 
  23. const ADDON_MEMBERCAPS_ADV = 'membercaps_advanced'; 
  24.  
  25. /** 
  26. * List of all registered Add-ons 
  27. * Related hook: ms_model_addon_register 
  28. * @var array { 
  29. * @key <string> The add-on ID. 
  30. * @value object { 
  31. * The add-on data. 
  32. * $name <string> Display name 
  33. * $parent <string> Empty/The Add-on ID of the parent 
  34. * $description <string> Description 
  35. * $footer <string> For the Add-ons list 
  36. * $icon <string> For the Add-ons list 
  37. * $class <string> For the Add-ons list 
  38. * $details <array of HTML elements> For the Add-ons list 
  39. * } 
  40. * } 
  41. */ 
  42. static private $_registered = array(); 
  43.  
  44. /** 
  45. * Used by function `flush_list` 
  46. * @since 1.0.0 
  47. * @var bool 
  48. */ 
  49. static private $_reload_files = false; 
  50.  
  51. /** 
  52. * List of add-on files to load when plugin is initialized. 
  53. * @since 1.0.0 
  54. * @var array of file-paths 
  55. */ 
  56. protected $addon_files = array(); 
  57.  
  58. /** 
  59. * Add-ons array. 
  60. * @since 1.0.0 
  61. * @var array { 
  62. * @key <string> The add-on ID. 
  63. * @value <boolean> The add-on enbled status (always true). 
  64. * } 
  65. */ 
  66. protected $active = array(); 
  67.  
  68. /** 
  69. * Initalize Object Hooks 
  70. * @since 1.0.0 
  71. */ 
  72. public function __construct() { 
  73. parent::__construct(); 
  74.  
  75. $this->add_action( 'ms_model_addon_flush', 'flush_list' ); 
  76.  
  77. /** 
  78. * Returns a list of all registered Add-Ons 
  79. * @since 1.0.0 
  80. * @return array Add-on lisl 
  81. */ 
  82. static public function get_addons() { 
  83. static $Done = false; 
  84. $res = null; 
  85.  
  86. if ( ! $Done || self::$_reload_files ) { 
  87. self::$_registered = array(); 
  88. $addons = array(); 
  89. $Done = true; 
  90. self::load_core_addons(); 
  91.  
  92. // Register core add-ons 
  93. $addons = self::get_core_list(); 
  94.  
  95. /** 
  96. * Register new addons. 
  97. * @since 1.0.0 
  98. */ 
  99. $addons = apply_filters( 
  100. 'ms_model_addon_register',  
  101. $addons 
  102. ); 
  103.  
  104. // Sanitation and populate default fields. 
  105. foreach ( $addons as $key => $data ) { 
  106. self::$_registered[$key] = $data->name; 
  107.  
  108. $addons[$key]->id = $key; 
  109. $addons[$key]->active = self::is_enabled( $key ); 
  110. $addons[$key]->title = $data->name; 
  111.  
  112. if ( isset( $addons[$key]->icon ) ) { 
  113. $addons[$key]->icon = '<i class="' . $addons[$key]->icon . '"></i>'; 
  114. } else { 
  115. $addons[$key]->icon = '<i class="wpmui-fa wpmui-fa-puzzle-piece"></i>'; 
  116.  
  117. if ( empty( $addons[$key]->action ) ) { 
  118. $addons[$key]->action = array(); 
  119. $addons[$key]->action[] = array( 
  120. 'id' => 'ms-toggle-' . $key,  
  121. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER,  
  122. 'value' => self::is_enabled( $key ),  
  123. 'class' => 'toggle-plugin',  
  124. 'ajax_data' => array( 
  125. 'action' => MS_Controller_Addon::AJAX_ACTION_TOGGLE_ADDON,  
  126. 'field' => 'active',  
  127. 'addon' => $key,  
  128. ),  
  129. ); 
  130. $addons[$key]->action[] = MS_Helper_Html::save_text( null, false, true ); 
  131.  
  132. /** 
  133. * Add custom Actions or remove default actions 
  134. * @since 1.0.0 
  135. */ 
  136. $addons[$key]->action = apply_filters( 
  137. 'ms_model_addon_action-' . $key,  
  138. $addons[$key]->action,  
  139. $addons[$key] 
  140. ); 
  141.  
  142. natcasesort( self::$_registered ); 
  143. foreach ( self::$_registered as $key => $dummy ) { 
  144. self::$_registered[$key] = $addons[$key]; 
  145.  
  146. /** 
  147. * The Add-on list is prepared. Initialize the addons now. 
  148. * @since 1.0.0 
  149. */ 
  150. do_action( 'ms_model_addon_initialize' ); 
  151.  
  152. return self::$_registered; 
  153.  
  154. /** 
  155. * Force to reload the add-on list 
  156. * Related action hooks: 
  157. * - ms_model_addon_flush 
  158. * @since 1.0.0 
  159. */ 
  160. public function flush_list() { 
  161. self::$_reload_files = true; 
  162. self::get_addons(); 
  163.  
  164. /** 
  165. * Checks the /app/addon directory for a list of all addons and loads these 
  166. * files. 
  167. * @since 1.0.0 
  168. */ 
  169. static protected function load_core_addons() { 
  170. $model = MS_Factory::load( 'MS_Model_Addon' ); 
  171. $root_path = trailingslashit( dirname( dirname( MS_Plugin::instance()->dir ) ) ); 
  172. $plugin_dir = substr( MS_Plugin::instance()->dir, strlen( $root_path ) ); 
  173. $addon_dir = $plugin_dir . 'app/addon/'; 
  174.  
  175. if ( empty( $model->addon_files ) || self::$_reload_files ) { 
  176. // In Admin dashboard we always refresh the addon-list... 
  177. self::$_reload_files = false; 
  178.  
  179. $mask = $root_path . $addon_dir . '*/class-ms-addon-*.php'; 
  180. $addons = glob( $mask ); 
  181.  
  182. $model->addon_files = array(); 
  183. foreach ( $addons as $file ) { 
  184. $model->addon_files[] = substr( $file, strlen( $root_path ) ); 
  185.  
  186. /** 
  187. * Allow other plugins/themes to register custom addons 
  188. * @since 1.0.0 
  189. * @var array 
  190. */ 
  191. $model->addon_files = apply_filters( 
  192. 'ms_model_addon_files',  
  193. $model->addon_files 
  194. ); 
  195.  
  196. $model->save(); 
  197.  
  198. // Loop all recignized Add-ons and initialize them. 
  199. foreach ( $model->addon_files as $file ) { 
  200. $addon = $root_path . $file; 
  201.  
  202. // Get class-name from file-name 
  203. $class = basename( $file ); 
  204. $class = str_replace( '.php', '', $class ); 
  205. $class = implode( '_', array_map( 'ucfirst', explode( '-', $class ) ) ); 
  206. $class = substr( $class, 6 ); // remove 'Class_' prefix 
  207.  
  208. if ( file_exists( $addon ) ) { 
  209. if ( ! class_exists( $class ) ) { 
  210. try { 
  211. include_once $addon; 
  212. } catch ( Exception $ex ) { 
  213.  
  214. if ( class_exists( $class ) ) { 
  215. MS_Factory::load( $class ); 
  216.  
  217. /** 
  218. * Allow custom addon-initialization code to run 
  219. * @since 1.0.0 
  220. */ 
  221. do_action( 'ms_model_addon_load' ); 
  222.  
  223. /** 
  224. * Verify if an add-on is enabled 
  225. * @since 1.0.0 
  226. * @var string $addon The add-on type. 
  227. * @return boolean True if enabled. 
  228. */ 
  229. static public function is_enabled( $addon ) { 
  230. $model = MS_Factory::load( 'MS_Model_Addon' ); 
  231. $enabled = ! empty( $model->active[ $addon ] ); 
  232.  
  233. if ( $enabled ) { 
  234. // Sub-addons are considered enabled only when the parent add-on is enabled also. 
  235. switch ( $addon ) { 
  236. case self::ADDON_MEMBERCAPS_ADV: 
  237. $enabled = self::is_enabled( self::ADDON_MEMBERCAPS ); 
  238. break; 
  239.  
  240. return apply_filters( 
  241. 'ms_model_addon_is_enabled_' . $addon,  
  242. $enabled 
  243. ); 
  244.  
  245. /** 
  246. * Enable an add-on type in the plugin. 
  247. * @since 1.0.0 
  248. * @var string $addon The add-on type. 
  249. */ 
  250. static public function enable( $addon ) { 
  251. $model = MS_Factory::load( 'MS_Model_Addon' ); 
  252. $model->refresh(); 
  253. $model->active[ $addon ] = true; 
  254. $model->save(); 
  255.  
  256. do_action( 'ms_model_addon_enable', $addon, $model ); 
  257.  
  258. /** 
  259. * Disable an add-on type in the plugin. 
  260. * @since 1.0.0 
  261. * @var string $addon The add-on type. 
  262. */ 
  263. static public function disable( $addon ) { 
  264. $model = MS_Factory::load( 'MS_Model_Addon' ); 
  265. $model->refresh(); 
  266. unset( $model->active[ $addon ] ); 
  267. $model->save(); 
  268.  
  269. do_action( 'ms_model_addon_disable', $addon, $model ); 
  270.  
  271. /** 
  272. * Toggle add-on type status in the plugin. 
  273. * @since 1.0.0 
  274. * @var string $addon The add-on type. 
  275. */ 
  276. static public function toggle_activation( $addon, $value = null ) { 
  277. $model = MS_Factory::load( 'MS_Model_Addon' ); 
  278. if ( null === $value ) { 
  279. $value = self::is_enabled( $addon ); 
  280.  
  281. if ( $value ) { 
  282. $model->disable( $addon ); 
  283. } else { 
  284. $model->enable( $addon ); 
  285.  
  286. do_action( 'ms_model_addon_toggle_activation', $addon, $model ); 
  287.  
  288. /** 
  289. * Enable add-on necessary to membership. 
  290. * @since 1.0.0 
  291. * @var string $addon The add-on type. 
  292. */ 
  293. public function auto_config( $membership ) { 
  294. if ( $membership->trial_period_enabled ) { 
  295. $this->enable( self::ADDON_TRIAL ); 
  296.  
  297. do_action( 'ms_model_addon_auto_config', $membership, $this ); 
  298.  
  299. /** 
  300. * Returns a list of all registered Add-Ons. 
  301. * Alias for the `get_addons()` function. 
  302. * @since 1.0.0 
  303. * @return array List of all registered Add-ons. 
  304. */ 
  305. public function get_addon_list() { 
  306. return self::get_addons(); 
  307.  
  308. /** 
  309. * Returns Add-On details for the core add-ons in legacy format. 
  310. * New Add-ons are stored in the /app/addon folder and use the 
  311. * ms_model_addon_register hook to provide these informations. 
  312. * ** This function should not be extended ** 
  313. * ** Create new Add-ons in the app/addon/ directory ** 
  314. * @since 1.0.0 
  315. * @return array List of Add-ons 
  316. */ 
  317. static private function get_core_list() { 
  318. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  319.  
  320. $options_text = sprintf( 
  321. '<i class="dashicons dashicons dashicons-admin-settings"></i> %s',  
  322. __( 'Options available', 'membership2' ) 
  323. ); 
  324.  
  325. $list[self::ADDON_MULTI_MEMBERSHIPS] = (object) array( 
  326. 'name' => __( 'Multiple Memberships', 'membership2' ),  
  327. 'description' => __( 'Your members can join more than one membership at the same time.', 'membership2' ),  
  328. 'icon' => 'dashicons dashicons-forms',  
  329. ); 
  330.  
  331. $list[self::ADDON_TRIAL] = (object) array( 
  332. 'name' => __( 'Trial Period', 'membership2' ),  
  333. 'description' => __( 'Allow your members to sign up for a free membership trial. Trial details can be configured separately for each membership.', 'membership2' ),  
  334. ); 
  335.  
  336. $list[self::ADDON_POST_BY_POST] = (object) array( 
  337. 'name' => __( 'Individual Posts', 'membership2' ),  
  338. 'description' => __( 'Protect individual Posts instead of Categories.', 'membership2' ),  
  339. ); 
  340.  
  341. $list[self::ADDON_CPT_POST_BY_POST] = (object) array( 
  342. 'name' => __( 'Individual Custom Posts', 'membership2' ),  
  343. 'description' => __( 'Protect individual Posts of a Custom Post Type.', 'membership2' ),  
  344. 'action' => array( __( 'Pro Version', 'membership2' ) ),  
  345. ); 
  346.  
  347. $list[self::ADDON_MEDIA] = (object) array( 
  348. 'name' => __( 'Media Protection', 'membership2' ),  
  349. 'description' => __( 'Protect Images and other Media-Library content.', 'membership2' ),  
  350. 'footer' => $options_text,  
  351. 'icon' => 'dashicons dashicons-admin-media',  
  352. 'class' => 'ms-options',  
  353. 'details' => array( 
  354. array( 
  355. 'id' => 'masked_url',  
  356. 'before' => esc_html( trailingslashit( get_option( 'home' ) ) ),  
  357. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  358. 'title' => __( 'Mask download URL:', 'membership2' ),  
  359. 'value' => $settings->downloads['masked_url'],  
  360. 'data_ms' => array( 
  361. 'field' => 'masked_url',  
  362. 'action' => MS_Controller_Settings::AJAX_ACTION_UPDATE_SETTING,  
  363. '_wpnonce' => true, // Nonce will be generated from 'action' 
  364. ),  
  365. ),  
  366. array( 
  367. 'id' => 'protection_type',  
  368. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO,  
  369. 'title' => __( 'Protection method', 'membership2' ),  
  370. 'desc' => __( 'You can change the way that Membership2 changes the default URL to your WordPress media library files.<br>This is done for increased protection by hiding the real filename and path.', 'membership2' ),  
  371. 'value' => $settings->downloads['protection_type'],  
  372. 'field_options' => MS_Rule_Media_Model::get_protection_types(),  
  373. 'data_ms' => array( 
  374. 'field' => 'protection_type',  
  375. 'action' => MS_Controller_Settings::AJAX_ACTION_UPDATE_SETTING,  
  376. '_wpnonce' => true, // Nonce will be generated from 'action' 
  377. ),  
  378. ),  
  379. array( 
  380. 'id' => 'advanced',  
  381. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER,  
  382. 'title' => __( 'Protect Individual Media files', 'membership2' ) . 
  383. ' - ' . __( 'Pro Version', 'membership2' ),  
  384. 'wrapper_class' => 'disabled',  
  385. 'read_only' => true,  
  386. 'desc' => __( 'Enable this to display a new tab in "Membership2" where you can manually modify access to each media library item.<br>Default: When this option is disabled then the parent-post controls the access to the media file.', 'membership2' ),  
  387. 'value' => false,  
  388. ),  
  389. ),  
  390. ); 
  391.  
  392. $list[self::ADDON_SHORTCODE] = (object) array( 
  393. 'name' => __( 'Shortcode Protection', 'membership2' ),  
  394. 'description' => __( 'Protect Shortcode-Output via Memberships.', 'membership2' ),  
  395. 'icon' => 'dashicons dashicons-editor-code',  
  396. ); 
  397.  
  398. $list[self::ADDON_URL_GROUPS] = (object) array( 
  399. 'name' => __( 'URL Protection', 'membership2' ),  
  400. 'description' => __( 'URL Protection will protect pages by the URL. This rule overrides all other rules, so use it carefully.', 'membership2' ),  
  401. 'icon' => 'dashicons dashicons-admin-links',  
  402. ); 
  403.  
  404. $list[self::ADDON_AUTO_MSGS_PLUS] = (object) array( 
  405. 'name' => __( 'Additional Automated Messages', 'membership2' ),  
  406. 'description' => __( 'Send your members automated Email responses for various additional events.', 'membership2' ),  
  407. 'icon' => 'dashicons dashicons-email',  
  408. ); 
  409.  
  410. $list[self::ADDON_SPECIAL_PAGES] = (object) array( 
  411. 'name' => __( 'Protect Special Pages', 'membership2' ),  
  412. 'description' => __( 'Change protection of special pages such as the search results.', 'membership2' ),  
  413. 'icon' => 'dashicons dashicons-admin-home',  
  414. ); 
  415.  
  416. $list[self::ADDON_ADV_MENUS] = (object) array( 
  417. 'name' => __( 'Advanced menu protection', 'membership2' ),  
  418. 'description' => __( 'Adds a new option to the General Settings that controls how WordPress menus are protected.<br />Protect individual Menu-Items, replace the contents of WordPress Menu-Locations or replace each Menu individually.', 'membership2' ),  
  419. 'footer' => $options_text,  
  420. 'class' => 'ms-options',  
  421. 'details' => array( 
  422. array( 
  423. 'id' => 'menu_protection',  
  424. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  425. 'title' => __( 'Choose how you want to protect your WordPress menus.', 'membership2' ),  
  426. 'value' => $settings->menu_protection,  
  427. 'field_options' => array( 
  428. 'item' => __( 'Protect single Menu Items (Default)', 'membership2' ),  
  429. 'menu' => __( 'Replace individual Menus', 'membership2' ),  
  430. 'location' => __( 'Overwrite contents of Menu Locations', 'membership2' ),  
  431. ),  
  432. 'data_ms' => array( 
  433. 'action' => MS_Controller_Settings::AJAX_ACTION_UPDATE_SETTING,  
  434. 'field' => 'menu_protection',  
  435. ),  
  436. ),  
  437. ),  
  438. ); 
  439.  
  440. // New since 1.1 
  441. $list[self::ADDON_ADMINSIDE] = (object) array( 
  442. 'name' => __( 'Admin Side Protection', 'membership2' ),  
  443. 'description' => __( 'Control the pages and even Meta boxes that members can access on the admin side.', 'membership2' ),  
  444. 'icon' => 'dashicons dashicons-admin-network',  
  445. 'action' => array( __( 'Pro Version', 'membership2' ) ),  
  446. ); 
  447.  
  448. $list[self::ADDON_MEMBERCAPS] = (object) array( 
  449. 'name' => __( 'Member Capabilities', 'membership2' ),  
  450. 'description' => __( 'Manage user-capabilities on membership level.', 'membership2' ),  
  451. 'footer' => $options_text,  
  452. 'class' => 'ms-options',  
  453. 'icon' => 'dashicons dashicons-admin-users',  
  454. 'details' => array( 
  455. array( 
  456. 'id' => 'ms-toggle-' . self::ADDON_MEMBERCAPS_ADV,  
  457. 'title' => __( 'Advanced Capability protection', 'membership2' ),  
  458. 'desc' => __( 'Allows you to protect individual WordPress Capabilities. When activated then the "User Roles" tab is replaced by a "Member Capabilities" tab where you can protect and assign individual WordPress Capabilities instead of roles.', 'membership2' ),  
  459. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER,  
  460. 'value' => self::is_enabled( self::ADDON_MEMBERCAPS_ADV ),  
  461. 'class' => 'toggle-plugin',  
  462. 'ajax_data' => array( 
  463. 'action' => MS_Controller_Addon::AJAX_ACTION_TOGGLE_ADDON,  
  464. 'field' => 'active',  
  465. 'addon' => self::ADDON_MEMBERCAPS_ADV,  
  466. ),  
  467. ),  
  468. ),  
  469. ); 
  470.  
  471. return $list; 
  472.