class-media-library-organizer-settings.php 9.79 KB
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
<?php
/**
 * Settings class.
 *
 * @package Media_Library_Organizer
 * @author WP Media Library
 */

/**
 * Handles fetching, defining defaults and saving settings in the options table.
 *
 * @since   1.0.0
 */
class Media_Library_Organizer_Settings {

	/**
	 * Holds the base class object.
	 *
	 * @since   1.0.5
	 *
	 * @var     object
	 */
	public $base;

	/**
	 * The key prefix to use for settings
	 *
	 * @since   1.0.0
	 *
	 * @var     string
	 */
	private $key_prefix = '_mlo';

	/**
	 * Constructor
	 *
	 * @since   1.0.5
	 *
	 * @param   object $base    Base Plugin Class.
	 */
	public function __construct( $base ) {

		// Store base class.
		$this->base = $base;

	}

	/**
	 * Returns a setting for the given Type
	 *
	 * @since   1.0.0
	 *
	 * @param   string $type       Type.
	 * @param   string $key        Setting Key.
	 * @return  mixed               Setting Value
	 */
	public function get_setting( $type, $key ) {

		// Get settings.
		$settings = $this->get_settings( $type );

		// Get setting.
		$setting = ( isset( $settings[ $key ] ) ? $settings[ $key ] : '' );

		/**
		 * Filters the settings data for the given setting type and key before it is returned.
		 *
		 * @since   1.0.7
		 *
		 * @param   mixed   $setting        Settings Data (string, array, object).
		 * @param   string  $type           Setting Type (option key).
		 * @param   string  $key            Setting Type Key (setting key within option value).
		 */
		$setting = apply_filters( 'media_library_organizer_settings_get_setting', $setting, $type, $key );

		// Return.
		return $setting;

	}

	/**
	 * Returns all settings for the given Type
	 *
	 * @since   1.0.0
	 *
	 * @param    string $type   Type.
	 * @return   array              Settings
	 */
	public function get_settings( $type ) {

		// Get settings.
		$settings = get_option( $this->key_prefix . '_' . $type );

		// Get default settings.
		$defaults = $this->get_default_settings( $type );

		// If we couldn't fetch any defaults, we can only return the settings.
		if ( empty( $defaults ) ) {
			return $settings;
		}

		// If no settings exists, fallback to the defaults.
		if ( ! $settings ) {
			$settings = $defaults;
		} else {
			// Iterate through the defaults, checking if the settings have the same key
			// If not, add the setting key with the default value.
			// This ensures that on a Plugin upgrade where new defaults are introduced,
			// they are immediately available for use without the user needing to save their
			// settings.
			foreach ( $defaults as $default_key => $default_value ) {
				if ( ! isset( $settings[ $default_key ] ) ) {
					$settings[ $default_key ] = $default_value;
				}
			}
		}

		/**
		 * Filters the settings data for the given setting type before it is returned.
		 *
		 * @since   1.0.7
		 *
		 * @param   mixed   $settings       Settings Data (string, array, object).
		 * @param   string  $type           Setting Type (option key).
		 */
		$settings = apply_filters( 'media_library_organizer_settings_get_settings', $settings, $type );

		// Return.
		return $settings;

	}

	/**
	 * Saves a single setting for the given Type and Key
	 *
	 * @since   1.0.0
	 *
	 * @param   string $type   Type.
	 * @param   string $key    Setting Key.
	 * @param   mixed  $value  Setting Value.
	 * @return  bool            Success
	 */
	public function update_setting( $type, $key, $value ) {

		// Get settings.
		$settings = $this->get_settings( $type );

		/**
		 * Defines the setting data just before it is saved for the given type and key.
		 *
		 * @since   1.0.7
		 *
		 * @param   mixed   $value        Settings Data (string, array, object).
		 * @param   string  $type         Setting Type (option key).
		 * @param   string  $key          Setting Type Key (setting key within option value).
		 */
		$value = apply_filters( 'media_library_organizer_settings_update_setting', $value, $type, $key );

		// Update single setting.
		$settings[ $key ] = $value;

		// Update settings.
		return $this->update_settings( $type, $settings );

	}

	/**
	 * Saves all settings for the given Type
	 *
	 * @since 1.0.0
	 *
	 * @param    string $type       Type.
	 * @param    array  $settings   Settings.
	 * @return   bool                Success
	 */
	public function update_settings( $type, $settings ) {

		/**
		 * Defines the settings data just before it is saved for the given type.
		 *
		 * @since   1.0.7
		 *
		 * @param   mixed   $settings     Settings Data (string, array, object).
		 * @param   string  $type         Setting Type (option key).
		 */
		$settings = apply_filters( 'media_library_organizer_settings_update_settings', $settings, $type );

		// Update settings.
		update_option( $this->key_prefix . '_' . $type, $settings );

		return true;

	}

	/**
	 * Deletes a single setting for the given Type and Key
	 *
	 * @since   1.0.0
	 *
	 * @param   string $type   Type.
	 * @param   string $key    Key.
	 * @return  bool            Success
	 */
	public function delete_setting( $type, $key ) {

		// Get settings.
		$settings = $this->get_settings( $type );

		// Delete single setting.
		if ( isset( $settings[ $key ] ) ) {
			unset( $settings[ $key ] );
		}

		/**
		 * Filters the setting data just before it is deleted.
		 *
		 * @since   1.0.7
		 *
		 * @param   mixed   $value        Settings Data (string, array, object).
		 * @param   string  $type         Setting Type (option key).
		 * @param   string  $key          Setting Type Key (setting key within option value).
		 */
		$settings = apply_filters( 'media_library_organizer_settings_delete_setting', $settings, $type, $key );

		// Update settings.
		return $this->update_settings( $type, $settings );

	}

	/**
	 * Deletes all settings for the given Type
	 *
	 * @since   1.0.0
	 *
	 * @param   string $type   Type.
	 * @return  bool            Success
	 */
	public function delete_settings( $type ) {

		// Delete settings.
		delete_option( $this->key_prefix . '_' . $type );

		/**
		 * Runs actions after a Settings Type is deleted.
		 *
		 * @since   1.0.7
		 *
		 * @param   string  $type         Setting Type (option key).
		 */
		do_action( 'media_library_organizer_settings_delete_settings', $type );

		return true;

	}

	/**
	 * Converts multidimensional form data, that is stored in a JSON string using JS' JSON.stringify(),
	 * into a PHP multidimensional array.
	 *
	 * Typically used on settings screens where form data is submitted using AJAX.
	 *
	 * @since   1.1.6
	 *
	 * @param   string $json   JSON String.
	 * @return  array           Multidimensional array
	 */
	public function convert_multidimensional_form_data_json_string_to_array( $json ) {

		$string = '';
		foreach ( json_decode( wp_unslash( $json ) ) as $key => $setting ) {
			$string .= $setting->name . '=' . $setting->value . '&';
		}
		parse_str( rtrim( $string, '&' ), $settings );

		// Drop some keys that aren't setting related.
		unset( $settings['media-library-organizer_nonce'], $settings['_wp_http_referer'] );

		// Return.
		return $settings;

	}

	/**
	 * Returns the default settings for the given Type
	 *
	 * @since   1.0.0
	 *
	 * @param   string $type    Type.
	 * @return  mixed           Default Settings | empty string
	 */
	private function get_default_settings( $type ) {

		// Define defaults.
		$defaults = array(
			// General.
			'general'      => array(
				'mlo-category_enabled' => 1,
				'orderby_enabled'      => 1,
				'order_enabled'        => 1,
			),

			// User Options.
			'user-options' => array(
				'orderby_enabled' => 0,
				'order_enabled'   => 0,
			),
		);

		/**
		 * Defines the default settings for the given type.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $defaults       Setting Defaults.
		 * @param   string  $type           Setting Type (option key).
		 */
		$defaults = apply_filters( 'media_library_organizer_settings_get_default_settings', $defaults, $type );

		// Return.
		return ( isset( $defaults[ $type ] ) ? $defaults[ $type ] : '' );

	}

	/**
	 * Recursive array_merge function
	 *
	 * @since   1.0.0
	 *
	 * @param   array $array1       First array.
	 * @param   array $array2       Second array.
	 * @return  array               Merged Data
	 */
	private function array_merge_assoc_recursive( $array1, $array2 ) {

		$merged = $array1;

		foreach ( $array2 as $key => $value ) {
			if ( is_array( $value ) && isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) ) {
				$merged[ $key ] = $this->array_merge_assoc_recursive( $merged[ $key ], $value );
			} elseif ( is_numeric( $key ) ) {
				if ( ! in_array( $value, $merged, true ) ) {
					$merged[] = $value;
				}
			} else {
				$merged[ $key ] = $value;
			}
		}

		return $merged;

	}

	/**
	 * Recursive array_diff function
	 *
	 * @since 1.0.0
	 *
	 * @param   array $array1       First array.
	 * @param   array $array2       Second array.
	 * @return  array               Difference
	 */
	private function array_diff_assoc_recursive( $array1, $array2 ) {

		$difference = array();

		foreach ( $array1 as $key => $value ) {
			if ( is_array( $value ) ) {
				if ( ! isset( $array2[ $key ] ) || ! is_array( $array2[ $key ] ) ) {
					$difference[ $key ] = $value;
				} else {
					$new_diff = $this->array_diff_assoc_recursive( $value, $array2[ $key ] );
					if ( ! empty( $new_diff ) ) {
						$difference[ $key ] = $new_diff;
					}
				}
			} elseif ( ! array_key_exists( $key, $array2 ) || $array2[ $key ] !== $value ) {
				$difference[ $key ] = $value;
			}
		}

		return $difference;

	}

	/**
	 * Escapes the given string value, depending on whether we're in the WordPress Administration
	 * interface or not.
	 *
	 * @since   1.0.0
	 *
	 * @param   string $string     String to escape.
	 * @return  string              Escaped string
	 */
	private function escape_string( $string ) {

		if ( is_admin() ) {
			// Escape the data, so it outputs in an input field correctly.
			$string = esc_attr( $string );
		}

		// Stripslashes.
		$string = stripslashes( $string );

		// Return.
		return $string;

	}

}