Completed
Push — milestone/2.0 ( 8a1186...26a446 )
by
unknown
04:33
created

Decorator::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\REST_API;
4
5
use \Carbon_Fields\Helper\Helper;
6
use \Carbon_Fields\Container\Repository as ContainerRepository;
7
8
/**
9
 * Decorate default REST routes with extra information provided by Carbon Fields
10
 */
11
class Decorator {
12
13
	/**
14
	 * ContainerRepository instance
15
	 * 
16
	 * @var ContainerRepository
17
	 */
18
	protected $container_repository;
19
	
20
	/**
21
	 * @param ContainerRepository $container_repository
22
	 */
23
	public function __construct( ContainerRepository $container_repository ) {
24
		$this->container_repository = $container_repository;
25
	}
26
27
	/**
28
	 * Boot up functionality
29
	 */
30
	public function boot() {
31
		add_action( 'rest_api_init', array( $this, 'register_fields' ) );
32
	}
33
34
	/**
35
	 * Register Carbon Fields using the register_rest_field() function
36
	 */
37
	public function register_fields() {
38
		$containers = $this->container_repository->get_containers();
39
		$containers = array_filter( $containers, function( $container ) {
40
			return ( $container->type !== 'Theme_Options' );
41
		} );
42
43
		foreach ( $containers as $container ) {
44
			$fields = $container->get_fields();
45
			$context = strtolower( $container->type );
46
			$type_callable = array( __CLASS__, "get_{$context}_container_settings" );
47
			if ( !is_callable( $type_callable ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
48
				continue; // unsupported container type
49
			}
50
			$types = call_user_func( $type_callable, $container );
51
52
			foreach ( $fields as $field ) {
53
				// TODO remove debug statement
54
				if ( false && ! $field->get_visible_in_rest_api() ) {
55
					continue;
56
				}
57
58
				$getter = function( $object, $field_name, $request ) use ( $container ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
					$object_id = self::get_object_id( $object, $container->type );
60
					return Helper::get_value( $object_id, $container->type, $field_name );
61
				};
62
63
				$setter = function( $value, $object, $field_name ) use ( $container ) {
64
					$object_id = self::get_object_id( $object, $container->type );
65
					$success = Helper::set_value( $object_id, $container->type, $field_name, $value );
66
					if ( ! $success ) {
67
						echo 'Failed to find or update field "' . $key . '".';
0 ignored issues
show
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
introduced by
Expected next thing to be a escaping function, not '$key'
Loading history...
68
						exit;
1 ignored issue
show
Coding Style Compatibility introduced by
The method register_fields() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
69
					}
70
				};
71
72
				register_rest_field( $types,
73
					$field->get_base_name(), array(
74
						'get_callback'    => $getter,
75
						'update_callback' => $setter,
76
						'schema'          => null,
77
					)
78
				);
79
			}
80
		}
81
	}
82
83
	/**
84
	 * Get Post Meta Container visibility settings
85
	 *
86
	 * @return array
87
	 */	
88
	public static function get_post_meta_container_settings( $container ) {
89
		return $container->settings['post_type'];
90
	}
91
92
	/**
93
	 * Get Term Meta Container visibility settings
94
	 *
95
	 * @return array
96
	 */	
97
	public static function get_term_meta_container_settings( $container ) {
98
		return $container->settings['taxonomy'];
99
	}
100
101
	/**
102
	 * Get User Meta Container visibility settings
103
	 *
104
	 * @return string
105
	 */	
106
	public static function get_user_meta_container_settings( $container ) {
1 ignored issue
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
		return 'user';
108
	}
109
110
	/**
111
	 * Get Comment Meta Container visibility settings
112
	 * 
113
	 * @return string
114
	 */	
115
	public static function get_comment_meta_container_settings( $container ) {
1 ignored issue
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
		return 'comment';
117
	}
118
119
	/**
120
	 * Retrieve ID from object based on $context
121
	 * 
122
	 * @param object $object
123
	 * @param string $container_type
124
	 * @return null|int
125
	 */
126
	public static function get_object_id( $object, $container_type ) {
127
		$object = is_array( $object ) ? (object) $object : $object;
128
		switch ( $container_type ) {
129
			case 'Post_Meta': // fallthrough intended
130
			case 'User_Meta': 
131
				return isset( $object->ID ) ? $object->ID : $object->id;
132
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
133
			case 'Term_Meta':
134
				return $object->term_id;
135
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
136
			case 'Comment_Meta' : 
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
137
				return $object->comment_ID;
138
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
139
		}
140
		return null;
141
	}
142
}
143