MS_Model_Option

Abstract Option model.

Defined (1)

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

/app/model/class-ms-model-option.php  
  1. class MS_Model_Option extends MS_Model { 
  2.  
  3. /** 
  4. * Singleton instance. 
  5. * @since 1.0.0 
  6. * @staticvar MS_Model_Option 
  7. */ 
  8. public static $instance; 
  9.  
  10. /** 
  11. * Settings data for extensions/integrations. 
  12. * @since 1.0.0 
  13. * @var array 
  14. */ 
  15. protected $custom = array(); 
  16.  
  17. /** 
  18. * Save content in wp_option table. 
  19. * Update WP cache and instance singleton. 
  20. * @since 1.0.0 
  21. */ 
  22. public function save() { 
  23. $this->before_save(); 
  24.  
  25. $option_key = $this->option_key(); 
  26.  
  27. $settings = MS_Factory::serialize_model( $this ); 
  28. MS_Factory::update_option( $option_key, $settings ); 
  29.  
  30. $this->instance = $this; 
  31. $this->after_save(); 
  32.  
  33. wp_cache_set( $option_key, $this, 'MS_Model_Option' ); 
  34.  
  35. /** 
  36. * Reads the options from options table 
  37. * @since 1.0.0 
  38. */ 
  39. public function refresh() { 
  40. $option_key = $this->option_key(); 
  41.  
  42. $settings = MS_Factory::get_option( $option_key ); 
  43. MS_Factory::populate_model( $this, $settings ); 
  44.  
  45. wp_cache_set( $option_key, $this, 'MS_Model_Option' ); 
  46.  
  47. /** 
  48. * Delete from wp option table 
  49. * @since 1.0.0 
  50. */ 
  51. public function delete() { 
  52. do_action( 'ms_model_option_delete_before', $this ); 
  53.  
  54. $option_key = $this->option_key(); 
  55.  
  56. MS_Factory::delete_option( $option_key ); 
  57. wp_cache_delete( $option_key, 'MS_Model_Option' ); 
  58.  
  59. do_action( 'ms_model_option_delete_after', $this ); 
  60.  
  61. /** 
  62. * validates and prepares the option key before it is used to read/write 
  63. * a value in the database. 
  64. * @since 1.0.0 
  65. * @api Used by MS_Factory 
  66. * @return string 
  67. */ 
  68. public function option_key() { 
  69. // Option key should be all lowercase. 
  70. $key = strtolower( get_class( $this ) ); 
  71.  
  72. // Network-wide IS PRO ONLY! 
  73.  
  74. return substr( $key, 0, 64 ); 
  75.  
  76. /** 
  77. * Set custom setting. 
  78. * @since 1.0.0 
  79. * @param string $group_name The custom setting group. 
  80. * @param string $field_name The custom setting field. 
  81. * @param mixed $value The custom setting value. 
  82. */ 
  83. public function set_custom_setting( $group_name, $field_name, $value ) { 
  84. if ( isset( $this->custom[ $group_name ] ) ) { 
  85. $group = $this->custom[ $group_name ]; 
  86. } else { 
  87. $group = array(); 
  88.  
  89. $field_value = apply_filters( 
  90. 'ms_model_settings_set_custom_setting',  
  91. $value,  
  92. $group_name,  
  93. $field_name,  
  94. $this 
  95. ); 
  96.  
  97. $key = false; 
  98.  
  99. // Very basic support for array updates. 
  100. // We only support updating 1-dimensional arrays with a 
  101. // specified key value. 
  102. if ( strpos( $field_name, '[' ) ) { 
  103. $field_name = str_replace( ']', '', $field_name ); 
  104. list( $field_name, $key ) = explode( '[', $field_name, 2 ); 
  105.  
  106. if ( $key ) { 
  107. if ( empty( $group[ $field_name ] ) ) { 
  108. $group[ $field_name ] = array(); 
  109. if ( is_array( $group[ $field_name ] ) ) { 
  110. $group[ $field_name ][ $key ] = $field_value; 
  111. } else { 
  112. $group[ $field_name ] = $field_value; 
  113.  
  114. $this->custom[ $group_name ] = $group; 
  115.  
  116. /** 
  117. * Get custom setting. 
  118. * @since 1.0.0 
  119. * @param string $group_name The custom setting group. 
  120. * @param string $field_name Optional. The custom setting field. 
  121. * @return mixed $value The custom setting value. 
  122. */ 
  123. public function get_custom_setting( $group_name, $field_name = null ) { 
  124. $value = ''; 
  125.  
  126. if ( isset( $this->custom[ $group_name ] ) ) { 
  127. $group = $this->custom[ $group_name ]; 
  128. } else { 
  129. $group = array(); 
  130.  
  131. if ( $field_name ) { 
  132. // Very basic support for array updates. 
  133. // We only support updating 1-dimensional arrays with a 
  134. // specified key value. 
  135. if ( strpos( $field_name, '[' ) ) { 
  136. $field_name = str_replace( ']', '', $field_name ); 
  137. list( $key, $field_name ) = explode( '[', $field_name, 2 ); 
  138.  
  139. if ( isset( $group[ $key ] ) ) { 
  140. $group = $group[ $key ]; 
  141. } else { 
  142. $group = array(); 
  143.  
  144. if ( isset( $group[ $field_name ] ) ) { 
  145. $value = $group[ $field_name ]; 
  146. } else { 
  147. $value = $group; 
  148.  
  149. return apply_filters( 
  150. 'ms_model_settings_get_custom_setting',  
  151. $value,  
  152. $group_name,  
  153. $field_name,  
  154. $this 
  155. );