MS_Plugin

Primary Membership plugin class.

Defined (1)

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

/app/class-ms-plugin.php  
  1. class MS_Plugin { 
  2.  
  3. /** 
  4. * Singletone instance of the plugin. 
  5. * @since 1.0.0 
  6. * @var MS_Plugin 
  7. */ 
  8. private static $instance = null; 
  9.  
  10. /** 
  11. * Modifier values. Modifiers are similar to wp-config constants, but can be 
  12. * also changed via code. 
  13. * @since 1.0.0 
  14. * @var array 
  15. */ 
  16. private static $modifiers = array(); 
  17.  
  18. /** 
  19. * The WordPress internal plugin identifier. 
  20. * @since 1.0.0 
  21. * @var string 
  22. */ 
  23. private $id; 
  24.  
  25. /** 
  26. * The plugin name. 
  27. * @since 1.0.0 
  28. * @var string 
  29. */ 
  30. private $name; 
  31.  
  32. /** 
  33. * The plugin version. 
  34. * @since 1.0.0 
  35. * @var string 
  36. */ 
  37. private $version; 
  38.  
  39. /** 
  40. * The plugin file. 
  41. * @since 1.0.0 
  42. * @var string 
  43. */ 
  44. private $file; 
  45.  
  46. /** 
  47. * The plugin path. 
  48. * @since 1.0.0 
  49. * @var string 
  50. */ 
  51. private $dir; 
  52.  
  53. /** 
  54. * The plugin URL. 
  55. * @since 1.0.0 
  56. * @var string 
  57. */ 
  58. private $url; 
  59.  
  60. /** 
  61. * The plugin settings. 
  62. * @since 1.0.0 
  63. * @var MS_Model_Settings 
  64. */ 
  65. private $settings; 
  66.  
  67. /** 
  68. * The plugin add-on settings. 
  69. * @since 1.0.0 
  70. * @var MS_Model_Addon 
  71. */ 
  72. private $addon; 
  73.  
  74. /** 
  75. * The main controller of the plugin. 
  76. * @since 1.0.0 
  77. * @var MS_Controller_Plugin 
  78. */ 
  79. private $controller; 
  80.  
  81. /** 
  82. * The API controller (for convenience) 
  83. * @since 1.0.0 
  84. * @var MS_Controller_Api 
  85. */ 
  86. public static $api = null; 
  87.  
  88. /** 
  89. * Plugin constructor. 
  90. * Set properties, registers hooks and loads the plugin. 
  91. * @since 1.0.0 
  92. */ 
  93. public function __construct() { 
  94.  
  95. /** 
  96. * Actions to execute before the plugin construction starts. 
  97. * @since 1.0.0 
  98. * @param object $this The MS_Plugin object. 
  99. */ 
  100. do_action( 'ms_plugin_init', $this ); 
  101.  
  102. /** 
  103. * Deprecated action. 
  104. * @since 1.0.0 
  105. * @deprecated since 2.0.0 
  106. */ 
  107. do_action( 'ms_plugin_construct_start', $this ); 
  108.  
  109. /** Setup plugin properties */ 
  110. $this->id = MS_PLUGIN; 
  111. $this->name = MS_PLUGIN_NAME; 
  112. $this->version = MS_PLUGIN_VERSION; 
  113. $this->file = MS_PLUGIN_FILE; 
  114. $this->dir = plugin_dir_path( MS_PLUGIN_FILE ); 
  115. $this->url = plugin_dir_url( MS_PLUGIN_FILE ); 
  116.  
  117. // Might refresh the Rewrite-Rules and reloads the page. 
  118. add_action( 
  119. 'wp_loaded',  
  120. array( $this, 'maybe_flush_rewrite_rules' ),  
  121. ); 
  122.  
  123. // Hooks init to register custom post types. 
  124. add_action( 
  125. 'init',  
  126. array( $this, 'register_custom_post_types' ),  
  127. ); 
  128.  
  129. // Hooks init to add rewrite rules and tags (both work in conjunction). 
  130. add_action( 'init', array( $this, 'add_rewrite_rules' ), 1 ); 
  131. add_action( 'init', array( $this, 'add_rewrite_tags' ), 1 ); 
  132.  
  133. // Plugin activation Hook. 
  134. register_activation_hook( 
  135. MS_PLUGIN_FILE,  
  136. array( $this, 'plugin_activation' ) 
  137. ); 
  138.  
  139. /** 
  140. * Hooks init to create the primary plugin controller. 
  141. * We use the setup_theme hook because plugins_loaded is too early: 
  142. * wp_redirect (used by the update model) is initialized after 
  143. * plugins_loaded but before setup_theme. 
  144. */ 
  145. add_action( 
  146. 'setup_theme',  
  147. array( $this, 'ms_plugin_constructing' ) 
  148. ); 
  149.  
  150. /** 
  151. * Creates and Filters the Settings Model. 
  152. * @since 1.0.0 
  153. * @param object $this The MS_Plugin object. 
  154. */ 
  155. $this->settings = MS_Factory::load( 'MS_Model_Settings' ); 
  156.  
  157. /** 
  158. * Creates and Filters the Addon Model. 
  159. * @since 1.0.0 
  160. * @param object $this The MS_Plugin object. 
  161. */ 
  162. $this->addon = MS_Factory::load( 'MS_Model_Addon' ); 
  163.  
  164. add_filter( 
  165. 'plugin_action_links_' . MS_PLUGIN,  
  166. array( $this, 'plugin_settings_link' ) 
  167. ); 
  168.  
  169. add_filter( 
  170. 'network_admin_plugin_action_links_' . MS_PLUGIN,  
  171. array( $this, 'plugin_settings_link' ) 
  172. ); 
  173.  
  174. // Grab instance of self. 
  175. self::$instance = $this; 
  176.  
  177. /** 
  178. * Actions to execute when the Plugin object has successfully constructed. 
  179. * @since 1.0.0 
  180. * @param object $this The MS_Plugin object. 
  181. */ 
  182. do_action( 'ms_plugin_construct_end', $this ); 
  183.  
  184. /** 
  185. * Loads primary plugin controllers. 
  186. * Related Action Hooks: 
  187. * - setup_theme 
  188. * @since 1.0.0 
  189. */ 
  190. public function ms_plugin_constructing() { 
  191. /** 
  192. * Creates and Filters the Plugin Controller. 
  193. * ---> MAIN ENTRY POINT CONTROLLER FOR PLUGIN <--- 
  194. * @uses MS_Controller_Plugin 
  195. * @since 1.0.0 
  196. */ 
  197. $this->controller = MS_Factory::create( 'MS_Controller_Plugin' ); 
  198.  
  199. /** 
  200. * Register plugin custom post types. 
  201. * @since 1.0.0 
  202. */ 
  203. public function register_custom_post_types() { 
  204. do_action( 'ms_plugin_register_custom_post_types_before', $this ); 
  205.  
  206. $cpts = apply_filters( 
  207. 'ms_plugin_register_custom_post_types',  
  208. array( 
  209. MS_Model_Membership::get_post_type() => MS_Model_Membership::get_register_post_type_args(),  
  210. MS_Model_Relationship::get_post_type() => MS_Model_Relationship::get_register_post_type_args(),  
  211. MS_Model_Invoice::get_post_type() => MS_Model_Invoice::get_register_post_type_args(),  
  212. MS_Model_Communication::get_post_type() => MS_Model_Communication::get_register_post_type_args(),  
  213. MS_Model_Event::get_post_type() => MS_Model_Event::get_register_post_type_args(),  
  214. ); 
  215.  
  216. foreach ( $cpts as $cpt => $args ) { 
  217. MS_Helper_Utility::register_post_type( $cpt, $args ); 
  218.  
  219. /** 
  220. * Add rewrite rules. 
  221. * @since 1.0.0 
  222. */ 
  223. public function add_rewrite_rules() { 
  224. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  225.  
  226. // Gateway return - IPN. 
  227. add_rewrite_rule( 
  228. 'ms-payment-return/(.+)/?',  
  229. 'index.php?paymentgateway=$matches[1]',  
  230. 'top' 
  231. ); 
  232.  
  233. // Alternative payment return URL: Old Membership plugin. 
  234. $use_old_ipn = apply_filters( 'ms_legacy_paymentreturn_url', true ); 
  235. if ( $use_old_ipn && ! class_exists( 'M_Membership' ) ) { 
  236. add_rewrite_rule( 
  237. 'paymentreturn/(.+)/?',  
  238. 'index.php?paymentgateway=$matches[1]',  
  239. 'top' 
  240. ); 
  241.  
  242. /** Media / download ----- */ 
  243. $mmask = $settings->downloads['masked_url']; 
  244. $mtype = $settings->downloads['protection_type']; 
  245.  
  246. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEDIA ) && $mmask ) { 
  247. if ( MS_Rule_Media_Model::PROTECTION_TYPE_HYBRID == $mtype ) { 
  248. add_rewrite_rule( 
  249. sprintf( '^%1$s/?$', $mmask ),  
  250. 'index.php?protectedfile=0',  
  251. 'top' 
  252. ); 
  253. } else { 
  254. add_rewrite_rule( 
  255. sprintf( '^%1$s/([^/]+)', $mmask ),  
  256. 'index.php?protectedfile=$matches[1]',  
  257. 'top' 
  258. ); 
  259. /** End: Media / download ----- */ 
  260.  
  261. do_action( 'ms_plugin_add_rewrite_rules', $this ); 
  262.  
  263. /** 
  264. * Add rewrite tags. 
  265. * @since 1.0.0 
  266. */ 
  267. public function add_rewrite_tags() { 
  268. // Membership site pages. 
  269. add_rewrite_tag( '%ms_page%', '(.+)' ); 
  270.  
  271. // Gateway return - IPN. 
  272. add_rewrite_tag( '%paymentgateway%', '(.+)' ); 
  273.  
  274. // Media / download. 
  275. add_rewrite_tag( '%protectedfile%', '(.+)' ); 
  276.  
  277. do_action( 'ms_plugin_add_rewrite_tags', $this ); 
  278.  
  279. /** 
  280. * Actions executed in plugin activation. 
  281. * @since 1.0.0 
  282. */ 
  283. public function plugin_activation() { 
  284. // Prevent recursion during plugin activation. 
  285. $refresh = lib3()->session->get( 'refresh_url_rules' ); 
  286. if ( $refresh ) { return; } 
  287.  
  288. // Update the Membership2 database entries after activation. 
  289. MS_Model_Upgrade::update( true ); 
  290.  
  291. do_action( 'ms_plugin_activation', $this ); 
  292.  
  293. /** 
  294. * Redirect page and request plugin to flush the WordPress rewrite rules 
  295. * on next request. 
  296. * @since 1.0.0 
  297. * @param string $url The URL to load after flushing the rewrite rules. 
  298. */ 
  299. static public function flush_rewrite_rules( $url = false ) { 
  300. if ( isset( $_GET['ms_flushed'] ) && 'yes' == $_GET['ms_flushed'] ) { 
  301. $refresh = true; 
  302. } else { 
  303. $refresh = lib3()->session->get( 'refresh_url_rules' ); 
  304.  
  305. if ( $refresh ) { return; } 
  306.  
  307. lib3()->session->add( 'refresh_url_rules', true ); 
  308.  
  309. // The URL param is only to avoid cache. 
  310. $url = esc_url_raw( 
  311. add_query_arg( 'ms_ts', time(), $url ) 
  312. ); 
  313. wp_safe_redirect( $url ); 
  314. exit; 
  315.  
  316. /** 
  317. * Flush the WordPress rewrite rules. 
  318. * @since 1.0.0 
  319. */ 
  320. public function maybe_flush_rewrite_rules() { 
  321. $refresh = lib3()->session->get_clear( 'refresh_url_rules' ); 
  322. if ( ! $refresh ) { return; } 
  323.  
  324. // Set up the plugin specific rewrite rules again. 
  325. $this->add_rewrite_rules(); 
  326. $this->add_rewrite_tags(); 
  327.  
  328. do_action( 'ms_plugin_flush_rewrite_rules', $this ); 
  329.  
  330. $url = remove_query_arg( 'ms_ts' ); 
  331. $url = esc_url_raw( add_query_arg( 'ms_flushed', 'yes', $url ) ); 
  332. wp_safe_redirect( $url ); 
  333. exit; 
  334.  
  335. /** 
  336. * Add link to settings page in plugins page. 
  337. * @since 1.0.0 
  338. * @param array $links WordPress default array of links. 
  339. * @return array Array of links with settings page links added. 
  340. */ 
  341. public function plugin_settings_link( $links ) { 
  342. if ( ! is_network_admin() ) { 
  343. $text = __( 'Settings', 'membership2' ); 
  344. $url = MS_Controller_Plugin::get_admin_url( 'settings' ); 
  345.  
  346. if ( $this->settings->initial_setup ) { 
  347. $url = MS_Controller_Plugin::get_admin_url(); 
  348.  
  349. /** 
  350. * Filter the plugin settings link. 
  351. * @since 1.0.0 
  352. * @param object $this The MS_Plugin object. 
  353. */ 
  354. $settings_link = apply_filters( 
  355. 'ms_plugin_settings_link',  
  356. sprintf( '<a href="%s">%s</a>', $url, $text ),  
  357. $this 
  358. ); 
  359. array_unshift( $links, $settings_link ); 
  360.  
  361. return $links; 
  362.  
  363. /** 
  364. * Returns singleton instance of the plugin. 
  365. * @since 1.0.0 
  366. * @static 
  367. * @access public 
  368. * @return MS_Plugin 
  369. */ 
  370. public static function instance() { 
  371. if ( ! self::$instance ) { 
  372. self::$instance = new MS_Plugin(); 
  373.  
  374. self::$instance = apply_filters( 
  375. 'ms_plugin_instance',  
  376. self::$instance 
  377. ); 
  378.  
  379. return self::$instance; 
  380.  
  381. /** 
  382. * Returns plugin enabled status. 
  383. * @since 1.0.0 
  384. * @access public 
  385. * @static 
  386. * @return bool The status. 
  387. */ 
  388. public static function is_enabled() { 
  389. return self::instance()->settings->plugin_enabled; 
  390.  
  391. /** 
  392. * Returns plugin wizard status. 
  393. * @since 1.0.0 
  394. * @access public 
  395. * @static 
  396. * @return bool The status. 
  397. */ 
  398. public static function is_wizard() { 
  399. return ! ! self::instance()->settings->initial_setup; 
  400.  
  401. /** 
  402. * Returns the network-wide protection status. 
  403. * This flag can be changed by setting the MS_PROTECT_NETWORK flag to true 
  404. * in wp-config.php 
  405. * @since 1.0.0 
  406. * @return bool False means that only the current site is protected. 
  407. * True means that memberships are shared among all network sites. 
  408. */ 
  409. public static function is_network_wide() { 
  410.  
  411.  
  412. // Free plugin always returns false (this is a pro feature). 
  413. return false; 
  414.  
  415. /** 
  416. * Returns a modifier option. 
  417. * This is similar to a setting but more "advanced" in a way that there is 
  418. * no UI for it. A modifier can be set by the plugin (e.g. during Import 
  419. * the "no_messages" modifier is enabled) or via a const in wp-config.php 
  420. * A modifier is never saved in the database. 
  421. * It can be defined ONLY via MS_Plugin::set_modifier() or via wp-config.php 
  422. * The set_modifier() value will always take precedence over wp-config.php 
  423. * definitions. 
  424. * @since 1.0.0 
  425. * @api 
  426. * @param string $key Name of the modifier. 
  427. * @return mixed The modifier value or null. 
  428. */ 
  429. public static function get_modifier( $key ) { 
  430. $res = null; 
  431.  
  432. if ( isset( self::$modifiers[ $key ] ) ) { 
  433. $res = self::$modifiers[ $key ]; 
  434. } elseif ( defined( $key ) ) { 
  435. $res = constant( $key ); 
  436.  
  437. return $res; 
  438.  
  439. /** 
  440. * Changes a modifier option. 
  441. * @see get_modifier() for more details. 
  442. * @since 1.0.0 
  443. * @api 
  444. * @param string $key Name of the modifier. 
  445. * @param mixed $value Value of the modifier. `null` unsets the modifier. 
  446. */ 
  447. public static function set_modifier( $key, $value = null ) { 
  448. if ( null === $value ) { 
  449. unset( self::$modifiers[ $key ] ); 
  450. } else { 
  451. self::$modifiers[ $key ] = $value; 
  452.  
  453. /** 
  454. * This funciton initializes the api property for easy access to the plugin 
  455. * API. This function is *only* called by MS_Controller_Api::__construct()! 
  456. * @since 1.0.0 
  457. * @internal 
  458. * @param MS_Controller_Api $controller The initialized API controller. 
  459. */ 
  460. public static function set_api( $controller ) { 
  461. self::$api = $controller; 
  462.  
  463. /** 
  464. * Returns property associated with the plugin. 
  465. * @since 1.0.0 
  466. * @access public 
  467. * @param string $property The name of a property. 
  468. * @return mixed Returns mixed value of a property or NULL if a property doesn't exist. 
  469. */ 
  470. public function __get( $property ) { 
  471. if ( property_exists( $this, $property ) ) { 
  472. return $this->$property; 
  473.  
  474. /** 
  475. * Check if property isset. 
  476. * @since 1.0.0 
  477. * @internal 
  478. * @param string $property The name of a property. 
  479. * @return mixed Returns true/false. 
  480. */ 
  481. public function __isset( $property ) { 
  482. return isset($this->$property); 
  483. }  
  484. };