Completed
Push — master ( 0c53d4...debbec )
by David
09:08 queued 10s
created

WL_Metabox_Field_uri::html_input()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 55
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
nc 6
nop 1
dl 0
loc 55
rs 9.078
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Metaboxes: URI Field Metabox.
4
 *
5
 * @since      3.0.0
6
 * @package    Wordlift
7
 * @subpackage Wordlift/admin/WL_Metabox
8
 */
9
10
/**
11
 * Define the {@link WL_Metabox_Field_uri} class.
12
 *
13
 * @since      3.0.0
14
 * @package    Wordlift
15
 * @subpackage Wordlift/admin/WL_Metabox
16
 */
17
class WL_Metabox_Field_uri extends WL_Metabox_Field {
18
19
	/**
20
	 * Only accept URIs or local entity IDs.
21
	 * Build new entity if the user inputted a name that is not present in DB.
22
	 *
23
	 * @param mixed $value The value to sanitize.
24
	 *
25
	 * @return int|mixed|WP_Error
26
	 */
27
	public function sanitize_data_filter( $value ) {
28
29
		if ( empty( $value ) ) {
30
			return null;
31
		}
32
33
		// Check that the inserted URI, ID or name does not point to a saved
34
		// entity or when the editor types a string in the input box, we try to
35
		// find an entity with that title and, if not found, we create that entity.
36
		$absent_from_db = is_numeric( $value )
37
			? is_null( get_post( $value ) )
38
			: ! $this->exists( $value );
39
40
		// Is it an URI?
41
		$name_is_uri = strpos( $value, 'http' ) === 0;
42
43
		// We create a new entity only if the entity is not present in the DB.
44
		// In the case of an external uri, we just save the uri.
45
		if ( $absent_from_db && ! $name_is_uri ) {
46
47
			// ...we create a new entity!
48
			$new_entity_id = wp_insert_post( array(
49
				'post_status' => 'publish',
50
				'post_type'   => Wordlift_Entity_Service::TYPE_NAME,
51
				'post_title'  => $value,
52
			) );
53
54
			$type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' );
55
56
			wl_set_entity_main_type( $new_entity_id, $type );
0 ignored issues
show
Deprecated Code introduced by
The function wl_set_entity_main_type() has been deprecated with message: use Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri )

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
57
58
			// Build uri for this entity.
59
			$new_uri = wl_build_entity_uri( $new_entity_id );
60
			wl_set_entity_uri( $new_entity_id, $new_uri );
61
62
			Wordlift_Linked_Data_Service::get_instance()->push( $new_entity_id );
63
64
			// Update the value that will be saved as meta.
65
			$value = $new_entity_id;
66
		}
67
68
		return $value;
69
	}
70
71
	/**
72
	 * Check whether an entity exists given a value.
73
	 *
74
	 * @since 3.15.0
75
	 *
76
	 * @param string $value An entity URI or a title string..
77
	 *
78
	 * @return bool True if the entity exists otherwise false.
79
	 */
80
	private function exists( $value ) {
81
82
		// When the editor types a string in the input box, we try to find
83
		// an entity with that title and, if not found, we create that entity.
84
		$entity_service = Wordlift_Entity_Service::get_instance();
85
86
		// Try looking for an entity by URI.
87
		$found_by_uri = null !== $entity_service->get_entity_post_by_uri( $value );
0 ignored issues
show
Deprecated Code introduced by
The method Wordlift_Entity_Service::get_entity_post_by_uri() has been deprecated with message: in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
88
89
		// Return true if found.
90
		if ( $found_by_uri ) {
91
			$this->log->debug( "Found entity for $value." );
92
93
			return true;
94
		}
95
96
		// Try looking for an entity by title, get any potential candidate.
97
		$candidate = get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::valid_entity_post_types() );
98
99
		// If a candidate has been found and it's an entity.
100
		return null !== $candidate && $entity_service->is_entity( $candidate->ID );
101
	}
102
103
	/**
104
	 * @inheritdoc
105
	 */
106
	public function html_wrapper_open() {
107
108
		// The containing <div> contains info on cardinality and expected types.
109
		$html = "<div class='wl-field' data-cardinality='$this->cardinality'";
110
111
		if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) {
112
113
			if ( is_array( $this->expected_uri_type ) ) {
114
				$html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'";
115
			} else {
116
				$html .= " data-expected-types='$this->expected_uri_type'";
117
			}
118
		}
119
120
		$html .= '>';
121
122
		return $html;
123
	}
124
125
	/**
126
	 * @inheritdoc
127
	 */
128
	public function html_input( $default_entity_identifier ) {
129
130
		if ( empty( $default_entity_identifier ) ) {
131
			$entity = null;
132
		} elseif ( is_numeric( $default_entity_identifier ) ) {
133
			$entity = get_post( $default_entity_identifier );
134
		} else {
135
			// @todo: we cannot be so sure this is a URI.
136
			// It is an URI
137
			$entity = Wordlift_Entity_Service
0 ignored issues
show
Deprecated Code introduced by
The method Wordlift_Entity_Service::get_entity_post_by_uri() has been deprecated with message: in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
138
				::get_instance()
139
				->get_entity_post_by_uri( $default_entity_identifier );
140
		}
141
142
		if ( ! is_null( $entity ) ) {
143
			$label = $entity->post_title;
144
			$value = $entity->ID;
145
		} else {
146
			// No ID and no internal uri. Just leave as is.
147
			$label = $default_entity_identifier;
148
			$value = $default_entity_identifier;
149
		}
150
151
		// Write saved value in page
152
		// The <input> tags host the meta value.
153
		// The visible <input> has the human readable value (i.e. entity name or uri)
154
		// and is accompained by an hidden <input> tag, passed to the server,
155
		// that contains the raw value (i.e. the uri or entity id).
156
		@ob_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
157
		?>
158
			<div class="wl-input-wrapper wl-autocomplete-wrapper">
159
				<input
160
					type="text"
161
					class="<?php echo esc_attr( $this->meta_name ); ?> wl-autocomplete"
162
					value="<?php echo esc_attr( $label ); ?>"
163
					style="width:88%"
164
				/>
165
				<input
166
					type="hidden"
167
					class="<?php echo esc_attr( $this->meta_name ); ?>"
168
					name="wl_metaboxes[<?php echo $this->meta_name ?>][]"
169
					value="<?php echo esc_attr( $value ); ?>"
170
				/>
171
172
				<button class="button wl-remove-input wl-button" type="button">
173
					<?php esc_html_e( 'Remove', 'wordlift' ); ?>
174
				</button>
175
176
				<div class="wl-input-notice"></div>
177
			</div>
178
		<?php
179
		$html = ob_get_clean();
180
181
		return $html;
182
	}
183
184
}
185