wpml-tf-settings-read.php
1.58 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
<?php
/**
* Class WPML_TF_Settings_Read
*
* @author OnTheGoSystems
*/
class WPML_TF_Settings_Read extends WPML_TF_Settings_Handler {
/**
* @param string $settings_class
*
* @return IWPML_TF_Settings
*
* @throws InvalidArgumentException
*/
public function get( $settings_class ) {
if ( ! class_exists( $settings_class ) ) {
throw new InvalidArgumentException( $settings_class . ' does not exist.' );
}
if ( ! in_array( 'IWPML_TF_Settings', class_implements( $settings_class ) ) ) {
throw new InvalidArgumentException( $settings_class . ' should implement IWPML_TF_Settings.' );
}
$settings_properties = get_option( $this->get_option_name( $settings_class ) );
/** @var IWPML_TF_Settings $settings */
$settings = new $settings_class();
if ( is_array( $settings_properties ) ) {
$this->set_properties( $settings, $settings_properties );
}
return $settings;
}
/**
* @param IWPML_TF_Settings $settings
* @param array $settings_properties
*
* @throws BadMethodCallException
*/
private function set_properties( IWPML_TF_Settings $settings, array $settings_properties ) {
foreach ( $settings->get_properties() as $property_name => $property_value ) {
if ( method_exists( $settings, 'set_' . $property_name ) ) {
if ( isset( $settings_properties[ $property_name ] ) ) {
call_user_func( array( $settings, 'set_' . $property_name ), $settings_properties[ $property_name ] );
}
} else {
throw new BadMethodCallException( 'The method set_' . $property_name . ' is required in ' . get_class( $settings ) . '.' );
}
}
}
}