Completed
Pull Request — master (#138)
by
unknown
02:24
created

Glyph_Field::template()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 31
rs 8.8571
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
/**
8
 * Association field class.
9
 * Allows selecting and manually sorting entries from various types:
10
 *  - Posts
11
 *  - Terms
12
 *  - Users
13
 *  - Comments
14
 */
15
class Glyph_Field extends Field {
16
	static $options = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $options.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
	public $none_label = '';
19
20
	public $button_label = '';
21
22
	/**
23
	 * Admin initialization actions
24
	 */
25
	public function admin_init() {
26
		$this->none_label = __( 'None', 'carbon-fields' );
27
		$this->button_label = __( 'Select Icon', 'carbon-fields' );
28
	}
29
30
	/**
31
	 * Hook administration scripts and styles.
32
	 */
33
	public static function admin_enqueue_scripts() {
34
		wp_enqueue_style( 'fontawesome', \Carbon_Fields\URL . '/assets/fontawesome/font-awesome.min.css', array(), '4.7.0' );
35
	}
36
37
	/**
38
	 * Generate the item options for the glyph field.
39
	 *
40
	 * @return array $options The selectable options of the glyph field.
41
	 */
42
	public function get_options() {
43
		if ( empty( $options ) ) {
0 ignored issues
show
Bug introduced by
The variable $options seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
44
			$data = Yaml::parse( file_get_contents( \Carbon_Fields\DIR . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'fontawesome' . DIRECTORY_SEPARATOR . 'fontawesome.yml' ) );
0 ignored issues
show
introduced by
file_get_contents is highly discouraged, please use wpcom_vip_file_get_contents() instead.
Loading history...
45
			static::$options = array(
0 ignored issues
show
Bug introduced by
Since $options is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $options to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
46
				''=>array(
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
47
					'name' => $this->none_label,
48
					'id' => '',
49
					'categories' => array(),
50
					'class'=>'fa',
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
51
					'contents'=>'&nbsp;',
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
52
				),
53
			);
54
			foreach ( $data['icons'] as $icon ) {
55
				static::$options[ $icon['id'] ] = array(
0 ignored issues
show
Bug introduced by
Since $options is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $options to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
56
					'name'=>$icon['name'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
57
					'id'=>$icon['id'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
58
					'categories'=>$icon['categories'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
59
					'class'=>'fa fa-' . $icon['id'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
60
					'contents'=>'',
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
61
				);
62
			}
63
		}
64
65
		/**
66
		 * Filter the final list of options, available to a certain glyph field.
67
		 *
68
		 * @param array $options Unfiltered options items.
69
		 * @param string $name Name of the glyph field.
70
		 */
71
		$options = apply_filters( 'carbon_glyph_options', static::$options, $this->get_name() );
0 ignored issues
show
Bug introduced by
Since $options is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $options to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
72
73
		return $options;
74
	}
75
76
	/**
77
	 * Returns an array that holds the field data, suitable for JSON representation.
78
	 * This data will be available in the Underscore template and the Backbone Model.
79
	 *
80
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
81
	 * @return array
82
	 */
83 View Code Duplication
	public function to_json( $load ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
		$field_data = parent::to_json( $load );
85
86
		$field_data = array_merge( $field_data, array(
87
			'options' => $this->get_options(),
88
			'button_label' => $this->button_label,
89
		) );
90
91
		return $field_data;
92
	}
93
94
	/**
95
	 * The main Underscore template of this field.
96
	 */
97
	public function template() {
98
		?>
99
		<div class="carbon-glyph-container">
100
			<input type="hidden" name="{{{ name }}}" value="{{{ value }}}" class="carbon-glyph-value" />
101
			<a href="#" class="carbon-glyph-preview">
102
				<i class="{{{ (value && typeof options[value] !== 'undefined') ? options[value].class : 'hidden' }}}"></i>
103
				<span class="button">{{{ button_label }}}</span>
104
			</a>
105
106
			<div class="carbon-glyph-popup hidden">
107
				<div class="carbon-glyph-search dashicons-before dashicons-search">
108
					<input type="text" value="" placeholder="<?php esc_attr_e( 'Search...', 'carbon-fields' ); ?>" />
109
				</div>
110
				<div class="carbon-glyph-scroll">
111
					<ul class="carbon-glyph-list">
112
						<# if (options) { #>
113
							<# _.each(options, function(item) { #>
114
								<li class="carbon-glyph-icon-container carbon-glyph-icon-container-{{{ item.id }}}">
115
									<a href="#" class="carbon-glyph-icon-trigger {{{ value == item.id ? 'active' : '' }}}" data-value="{{{ item.id }}}">
116
										<i class="{{{ item.class }}}">{{{ item.contents }}}</i>
117
										<span>{{{ item.name }}}</span>
118
									</a>
119
								</li>
120
							<# }); #>
121
						<# } #>
122
					</ul>
123
				</div>
124
			</div>
125
		</div>
126
		<?php
127
	}
128
}
129