Completed
Pull Request — 2.x (#4782)
by Scott Kingsley
46:15 queued 38:43
created

PodsField_Icon::display()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 5
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package Pods\Fields
4
 */
5
class PodsField_Icon extends PodsField {
6
7
	/**
8
	 * Field Type Identifier
9
	 *
10
	 * @var string
11
	 * @since 2.0
12
	 */
13
	public static $type = 'icon';
14
15
	/**
16
	 * Field Type Label
17
	 *
18
	 * @var string
19
	 * @since 2.0
20
	 */
21
	public static $label = 'Icon Picker';
22
23
	/**
24
	 * Field Type Preparation
25
	 *
26
	 * @var string
27
	 * @since 2.0
28
	 */
29
	public static $prepare = '%s';
30
31
	/**
32
	 * Do things like register/enqueue scripts and stylesheets
33
	 *
34
	 * @since 2.0
35
	 */
36
	public function __construct () {
37
		self::$label = __( 'Icon Picker', 'pods' );
38
39
		// Load the icon picker library
40
		if ( ! class_exists( 'Icon_Picker' ) ) {
41
			require_once PODS_DIR . '/vendor/kucrut/icon-picker/icon-picker.php';
42
		}
43
		$icon_picker = Icon_Picker::instance();
44
	}
45
46
	/**
47
	 * Add options and set defaults to
48
	 *
49
	 *
50
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
51
	 * @since 2.0
52
	 */
53
	public function options () {
54
		$options = array(
55
			self::$type . '_repeatable' => array(
56
				'label' => __( 'Repeatable Field', 'pods' ),
57
				'default' => 0,
58
				'type' => 'boolean',
59
				'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
60
				'boolean_yes_label' => '',
61
				'dependency' => true,
62
				'developer_mode' => true
63
			),
64
			'output_options' => array(
65
				'label' => __( 'Icon libraries', 'pods' ),
66
				'group' => array(
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
67
				)
68
			),
69
		);
70
71
		return $options;
72
	}
73
74
	/**
75
	 * Define the current field's schema for DB table storage
76
	 *
77
	 * @param array $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
78
	 *
79
	 * @return string
80
	 * @since 2.0
81
	 */
82
	public function schema ( $options = null ) {
83
		$schema = 'LONGTEXT';
84
		return $schema;
85
	}
86
87
	/**
88
	 * Change the way the value of the field is displayed with Pods::get
89
	 *
90
	 * @param mixed $value
91
	 * @param string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
92
	 * @param array $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
93
	 * @param array $pod
0 ignored issues
show
Documentation introduced by
Should the type for parameter $pod not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
94
	 * @param int $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
95
	 *
96
	 * @return mixed|null|string
97
	 * @since 2.0
98
	 */
99
	public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
100
		//$value = $this->strip_html( $value, $options );
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
		//$value = icon_picker_get_icon_url( $value );
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
103
		return $value;
104
	}
105
106
	/**
107
	 * Customize output of the form field
108
	 *
109
	 * @param string $name
110
	 * @param mixed $value
111
	 * @param array $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
112
	 * @param array $pod
0 ignored issues
show
Documentation introduced by
Should the type for parameter $pod not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
113
	 * @param int $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
114
	 *
115
	 * @since 2.0
116
	 */
117
	public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
118
		$options = (array) $options;
119
		$form_field_type = PodsForm::$field_type;
120
121
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
122
			if ( pods_var( 'read_only', $options, false ) ) {
123
				$options[ 'readonly' ] = true;
124
			} else {
125
				return;
126
			}
127
		} elseif ( ! pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
128
			$options[ 'readonly' ] = true;
129
		}
130
131
		pods_view( PODS_DIR . 'ui/fields/icon.php', compact( array_keys( get_defined_vars() ) ) );
132
	}
133
134
	/**
135
	 * Validate a value before it's saved
136
	 *
137
	 * @param mixed $value
138
	 * @param string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
139
	 * @param array $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
140
	 * @param array $fields
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fields not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
141
	 * @param array $pod
0 ignored issues
show
Documentation introduced by
Should the type for parameter $pod not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
142
	 * @param int $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
143
	 *
144
	 * @param null $params
145
	 * @return array|bool
146
	 * @since 2.0
147
	 */
148
	public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
149
		$errors = array();
150
151
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
152
153
		if ( empty( $check ) ) {
154
			if ( pods_var( 'required', $options ) ) {
155
				$errors[] = __( 'This field is required.', 'pods' );
156
			}
157
		}
158
159
		if ( ! empty( $errors ) ) {
160
			return $errors;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $errors; (array) is incompatible with the return type of the parent method PodsField::validate of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
161
		}
162
163
		return true;
164
	}
165
166
	/**
167
	 * Change the value or perform actions after validation but before saving to the DB
168
	 *
169
	 * @param mixed $value
170
	 * @param int $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
171
	 * @param string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
172
	 * @param array $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
173
	 * @param array $fields
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fields not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
174
	 * @param array $pod
0 ignored issues
show
Documentation introduced by
Should the type for parameter $pod not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
175
	 * @param object $params
0 ignored issues
show
Documentation introduced by
Should the type for parameter $params not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
176
	 *
177
	 * @return mixed|string
178
	 * @since 2.0
179
	 */
180
	public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
181
182
		$value = array_intersect_key( $value, array( 'type' => '', 'icon' => '' ) );
183
184
		if ( empty( $value['type'] ) || empty( $value['icon'] ) ) {
185
			$value = '';
186
		}
187
188
		$value = array_map( 'strip_tags', $value );
189
190
		return $value;
191
	}
192
193
	/**
194
	 * Customize the Pods UI manage table column output
195
	 *
196
	 * @param int $id
197
	 * @param mixed $value
198
	 * @param string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
199
	 * @param array $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
200
	 * @param array $fields
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fields not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
201
	 * @param array $pod
0 ignored issues
show
Documentation introduced by
Should the type for parameter $pod not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
202
	 *
203
	 * @return mixed|string
204
	 * @since 2.0
205
	 */
206
	public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
207
		/*$value = $this->strip_html( $value, $options );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
208
209
		if ( 0 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) )
210
			$value = wp_trim_words( $value );*/
211
212
		return $value;
213
	}
214
}
215