1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class Push7_Subscribe_Button_Options {
private static $options = null;
private $appid;
private $enable_social_buzz;
private $social_buzz_mode;
private $social_buzz_message;
private $social_buzz_posttype;
public function __get( $name ) {
if ( property_exists( $this, $name ) ) {
return $this->$name;
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE );
return null;
}
private function __construct( array $options ) {
$this->appid = $options['appid'];
$this->enable_social_buzz = $options['enable_social_buzz'];
$this->social_buzz_mode = $options['social_buzz_mode'];
$this->social_buzz_message = $options['social_buzz_message'];
$this->social_buzz_posttype = $options['social_buzz_posttype'];
}
private static function merege_option() {
$default = array(
'appid' => null,
'enable_social_buzz' => false,
'social_buzz_mode' => 'simple',
'social_buzz_message' => __( 'Subscribe Latest Update with Push Notification!', 'simple-push-subscribe-button' ),
'social_buzz_posttype' => array( 'post' ),
);
$default = apply_filters( 'push7_sb_default_options', $default );
$current_options = get_option( Push7_Subscribe_Button::PLUGIN_OPTIONS, array() );
if ( $current_options instanceof self ) {
$current_options = get_object_vars( $current_options );
}
return array_merge( $default, $current_options );
}
public static function get_options() {
if ( ! self::$options ) {
self::$options = new self( self::merege_option() );
}
return self::$options;
}
public static function invalidate_options() {
self::$options = null;
}
public static function create_options( $options_in ) {
if ( ! is_array( $options_in ) ) {
if ( $options_in instanceof static ) {
$options_in = get_object_vars( $options_in );
} else {
throw new RuntimeException( 'Invalid action.' );
}
}
$options['appid'] = empty( $options_in['appid'] ) ? '' : $options_in['appid'];
$options['enable_social_buzz'] =
empty( $options_in['enable_social_buzz'] ) ? false :
( 'true' === $options_in['enable_social_buzz'] ? true :
( 'on' === $options_in['enable_social_buzz'] ? true :
( true === $options_in['enable_social_buzz'] ? true :
false ) ) );
$options['social_buzz_message'] = empty( $options_in['social_buzz_message'] ) ? '' : $options_in['social_buzz_message'];
$options['social_buzz_mode'] = ! empty( $options_in['social_buzz_mode'] ) && in_array( $options_in['social_buzz_mode'], array_keys( Push7_Subscribe_Button::get_sbztypes() ), true ) ? $options_in['social_buzz_mode'] : null;
if ( empty( $options_in['social_buzz_posttype'] ) or ! is_array( $options_in['social_buzz_posttype'] ) ) {
$options['social_buzz_posttype'] = array();
} else {
$post_types = get_post_types();
foreach ( $options_in['social_buzz_posttype'] as $post_type ) {
if ( in_array( $post_type, $post_types, true ) ) {
$options['social_buzz_posttype'][] = $post_type;
}
}
}
return new self( $options );
}
}