Changeset 2684972
- Timestamp:
- 02/25/2022 12:14:03 PM (4 years ago)
- Location:
- woorule
- Files:
-
- 4 edited
- 1 copied
-
tags/2.7.3 (copied) (copied from woorule/trunk)
-
tags/2.7.3/inc/class-woorule-options.php (modified) (7 diffs)
-
tags/2.7.3/inc/partials/admin-settings.php (modified) (1 diff)
-
trunk/inc/class-woorule-options.php (modified) (7 diffs)
-
trunk/inc/partials/admin-settings.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
woorule/tags/2.7.3/inc/class-woorule-options.php
r2684556 r2684972 28 28 29 29 /** 30 * Plugin options.31 *32 * @var array33 */34 protected $options = array();35 36 /**37 * Woorule_Options constructor.38 *39 * @return void40 */41 protected function __construct() {42 $this->options = get_option( self::OPTIONS_KEY, array() );43 }44 45 /**46 30 * Prevent unserializing. 47 31 * … … 62 46 * Get class instance. 63 47 * 64 * @return objectInstance.48 * @return $this Instance. 65 49 */ 66 50 protected static function get_instance() { … … 87 71 $instance = self::get_instance(); 88 72 89 $instance->filter_options();90 91 73 if ( 0 === strpos( $name, 'get_' ) ) { 92 74 return $instance->get( substr( $name, 4 ) ); … … 106 88 * 107 89 * @return mixed 108 *109 * @throws BadMethodCallException Exception.110 90 */ 111 91 protected function get( $option_name ) { 112 //$this->options = get_option( self::OPTIONS_KEY, array());92 $options = self::load_options(); 113 93 114 94 if ( 'options' === $option_name ) { 115 return $this->options; 116 } elseif ( isset( $this->options[ 'woorule_' . $option_name ] ) ) { 117 return $this->options[ 'woorule_' . $option_name ]; 118 } else { 119 throw new BadMethodCallException( $option_name . ' is not defined in ' . __CLASS__ ); 95 return $options; 120 96 } 97 98 return isset( $options[ 'woorule_' . $option_name ] ) ? $options[ 'woorule_' . $option_name ] : null; 121 99 } 122 100 … … 128 106 * 129 107 * @return void 130 *131 * @throws BadMethodCallException Exception.132 108 */ 133 109 protected function set( $option_name, $value ) { 134 static $updated = null;110 $options = self::load_options(); 135 111 136 if ( 'options' === $option_name && is_array( $value ) ) { 137 // Merge new options with existent object's options. 138 $value = wp_parse_args( $value, $this->options ); 139 $this->filter_options( $value ); 140 } elseif ( isset( $this->options[ 'woorule_' . $option_name ] ) ) { 141 $this->options[ 'woorule_' . $option_name ] = $value; 112 if ( 'options' === $option_name ) { 113 if ( is_array( $value ) ) { 114 // Merge new options with existent object's options. 115 $value = wp_parse_args( $value, $options ); 116 117 update_option( self::OPTIONS_KEY, $value, false ); 118 } 142 119 } else { 143 throw new BadMethodCallException( $option_name . ' is not defined in ' . __CLASS__ ); 144 } 120 $options[ 'woorule_' . $option_name ] = $value; 145 121 146 if ( is_null( $updated ) ) { 147 $updated = true; 148 149 // Save options to DB on shutdown. 150 add_action( 151 'shutdown', 152 // Anonymous function is used because we do want to have public DB update function. 153 function () { 154 $this->filter_options(); 155 update_option( self::OPTIONS_KEY, $this->options, false ); 156 } 157 ); 122 update_option( self::OPTIONS_KEY, $options, false ); 158 123 } 159 124 } … … 164 129 * @return array 165 130 */ 166 pr otectedfunction get_options_defaults() {131 private static function get_options_defaults() { 167 132 return (array) apply_filters( 168 133 'woorule_options_defaults', … … 181 146 * @param array $options Options. 182 147 * 183 * @return void148 * @return array 184 149 */ 185 pr otected function filter_options( $options = null) {186 $this->options =shortcode_atts(150 private static function filter_options( $options ) { 151 return shortcode_atts( 187 152 self::get_options_defaults(), 188 is_null( $options ) ? $this->options :$options153 $options 189 154 ); 190 155 } 156 157 /** 158 * Load options. 159 * 160 * @return array 161 */ 162 private static function load_options() { 163 return self::filter_options( get_option( self::OPTIONS_KEY, array() ) ); 164 } 191 165 } -
woorule/tags/2.7.3/inc/partials/admin-settings.php
r2684385 r2684972 88 88 </th> 89 89 <td> 90 <input name="woorule_api" id="woorule_api" type="text" class="regular-text code" value=" 91 <?php echo esc_attr( $args['api_key'] ); ?>"/> 90 <input name="woorule_api" id="woorule_api" type="text" class="regular-text code" value="<?php echo esc_attr( $args['api_key'] ); ?>"/> 92 91 <span class="description"> 93 92 <?php echo wp_kses_post( -
woorule/trunk/inc/class-woorule-options.php
r2684556 r2684972 28 28 29 29 /** 30 * Plugin options.31 *32 * @var array33 */34 protected $options = array();35 36 /**37 * Woorule_Options constructor.38 *39 * @return void40 */41 protected function __construct() {42 $this->options = get_option( self::OPTIONS_KEY, array() );43 }44 45 /**46 30 * Prevent unserializing. 47 31 * … … 62 46 * Get class instance. 63 47 * 64 * @return objectInstance.48 * @return $this Instance. 65 49 */ 66 50 protected static function get_instance() { … … 87 71 $instance = self::get_instance(); 88 72 89 $instance->filter_options();90 91 73 if ( 0 === strpos( $name, 'get_' ) ) { 92 74 return $instance->get( substr( $name, 4 ) ); … … 106 88 * 107 89 * @return mixed 108 *109 * @throws BadMethodCallException Exception.110 90 */ 111 91 protected function get( $option_name ) { 112 //$this->options = get_option( self::OPTIONS_KEY, array());92 $options = self::load_options(); 113 93 114 94 if ( 'options' === $option_name ) { 115 return $this->options; 116 } elseif ( isset( $this->options[ 'woorule_' . $option_name ] ) ) { 117 return $this->options[ 'woorule_' . $option_name ]; 118 } else { 119 throw new BadMethodCallException( $option_name . ' is not defined in ' . __CLASS__ ); 95 return $options; 120 96 } 97 98 return isset( $options[ 'woorule_' . $option_name ] ) ? $options[ 'woorule_' . $option_name ] : null; 121 99 } 122 100 … … 128 106 * 129 107 * @return void 130 *131 * @throws BadMethodCallException Exception.132 108 */ 133 109 protected function set( $option_name, $value ) { 134 static $updated = null;110 $options = self::load_options(); 135 111 136 if ( 'options' === $option_name && is_array( $value ) ) { 137 // Merge new options with existent object's options. 138 $value = wp_parse_args( $value, $this->options ); 139 $this->filter_options( $value ); 140 } elseif ( isset( $this->options[ 'woorule_' . $option_name ] ) ) { 141 $this->options[ 'woorule_' . $option_name ] = $value; 112 if ( 'options' === $option_name ) { 113 if ( is_array( $value ) ) { 114 // Merge new options with existent object's options. 115 $value = wp_parse_args( $value, $options ); 116 117 update_option( self::OPTIONS_KEY, $value, false ); 118 } 142 119 } else { 143 throw new BadMethodCallException( $option_name . ' is not defined in ' . __CLASS__ ); 144 } 120 $options[ 'woorule_' . $option_name ] = $value; 145 121 146 if ( is_null( $updated ) ) { 147 $updated = true; 148 149 // Save options to DB on shutdown. 150 add_action( 151 'shutdown', 152 // Anonymous function is used because we do want to have public DB update function. 153 function () { 154 $this->filter_options(); 155 update_option( self::OPTIONS_KEY, $this->options, false ); 156 } 157 ); 122 update_option( self::OPTIONS_KEY, $options, false ); 158 123 } 159 124 } … … 164 129 * @return array 165 130 */ 166 pr otectedfunction get_options_defaults() {131 private static function get_options_defaults() { 167 132 return (array) apply_filters( 168 133 'woorule_options_defaults', … … 181 146 * @param array $options Options. 182 147 * 183 * @return void148 * @return array 184 149 */ 185 pr otected function filter_options( $options = null) {186 $this->options =shortcode_atts(150 private static function filter_options( $options ) { 151 return shortcode_atts( 187 152 self::get_options_defaults(), 188 is_null( $options ) ? $this->options :$options153 $options 189 154 ); 190 155 } 156 157 /** 158 * Load options. 159 * 160 * @return array 161 */ 162 private static function load_options() { 163 return self::filter_options( get_option( self::OPTIONS_KEY, array() ) ); 164 } 191 165 } -
woorule/trunk/inc/partials/admin-settings.php
r2684385 r2684972 88 88 </th> 89 89 <td> 90 <input name="woorule_api" id="woorule_api" type="text" class="regular-text code" value=" 91 <?php echo esc_attr( $args['api_key'] ); ?>"/> 90 <input name="woorule_api" id="woorule_api" type="text" class="regular-text code" value="<?php echo esc_attr( $args['api_key'] ); ?>"/> 92 91 <span class="description"> 93 92 <?php echo wp_kses_post(
Note: See TracChangeset
for help on using the changeset viewer.