MS_Addon

Add-On controller.

Defined (1)

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

/app/class-ms-addon.php  
  1. abstract class MS_Addon extends MS_Controller { 
  2.  
  3. /** 
  4. * Ajax action used to update the addon settings. 
  5. * @since 1.0.1.0 
  6. * @var string 
  7. */ 
  8. const AJAX_UPDATE = 'ms_addon_update'; 
  9.  
  10. /** 
  11. * Reference to the MS_Model_Addon instance. 
  12. * @type MS_Model_Addon 
  13. */ 
  14. static protected $model = null; 
  15.  
  16. /** 
  17. * Reference to the MS_Model_Settings instance. 
  18. * @type MS_Model_Addon 
  19. */ 
  20. static protected $settings = null; 
  21.  
  22. /** 
  23. * Initialize the Add-On. 
  24. * @since 1.0.0 
  25. */ 
  26. public function __construct() { 
  27. parent::__construct(); 
  28.  
  29. self::$model = MS_Factory::load( 'MS_Model_Addon' ); 
  30. self::$settings = MS_Factory::load( 'MS_Model_Settings' ); 
  31.  
  32. $this->add_filter( 'ms_model_addon_register', 'register' ); 
  33. $this->add_action( 'ms_model_addon_initialize', 'init_addon' ); 
  34.  
  35. $this->add_ajax_action( 
  36. $this->ajax_action(),  
  37. 'ajax_update_settings' 
  38. ); 
  39.  
  40. /** 
  41. * Initializes the Add-on. 
  42. * @since 1.0.0 
  43. */ 
  44. public function init_addon() { 
  45. $this->init(); 
  46.  
  47. /** 
  48. * Returns the Add-on ID (self::ID). 
  49. * @since 1.0.1.0 
  50. * @return string 
  51. */ 
  52. public function get_id() { 
  53. trigger_error( 'get_id() not implemented by Add-on', E_USER_WARNING ); 
  54.  
  55. /** 
  56. * Initializes the Add-on. Always executed. 
  57. * @since 1.0.0 
  58. */ 
  59. abstract public function init(); 
  60.  
  61. /** 
  62. * Registers the Add-On 
  63. * @since 1.0.0 
  64. * @param array $list The Add-Ons list. 
  65. * @return array The updated Add-Ons list. 
  66. */ 
  67. abstract public function register( $addons ); 
  68.  
  69. /** 
  70. * Returns the Ajax action string used to update settings for an add-on. 
  71. * @since 1.0.1.0 
  72. * @return string 
  73. */ 
  74. protected function ajax_action() { 
  75. return self::AJAX_UPDATE . '-' . $this->get_id(); 
  76.  
  77. /** 
  78. * Returns a single Add-on setting value that was previously saved via 
  79. * the AJAX_UPDATE ajax action. 
  80. * @since 1.0.1.0 
  81. * @param stirng $key The setting key. 
  82. * @return string The value. 
  83. */ 
  84. public function get_setting( $key ) { 
  85. $value = self::$settings->get_custom_setting( 
  86. $this->get_id(),  
  87. $key 
  88. ); 
  89.  
  90. return $value; 
  91.  
  92. /** 
  93. * Ajax handler that updates the Add-on settings. 
  94. * @since 1.0.1.0 
  95. */ 
  96. public function ajax_update_settings() { 
  97. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  98. $fields = array( 'field', 'value' ); 
  99.  
  100. if ( $this->verify_nonce() 
  101. && self::validate_required( $fields, 'POST', false ) 
  102. && $this->is_admin_user() 
  103. ) { 
  104. self::$settings->set_custom_setting( 
  105. $this->get_id(),  
  106. $_POST['field'],  
  107. $_POST['value'] 
  108. ); 
  109.  
  110. self::$settings->save(); 
  111. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  112.  
  113. echo $msg; 
  114. exit; 
  115.