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

Icon_Field::add_fontawesome_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 Icon_Field extends Predefined_Options_Field {
16
	private static $fontawesome_options_cache = array();
17
18
	private static $dashicons_options_cache = array();
19
20
	public $none_label = '';
21
22
	public $button_label = '';
23
24
	/**
25
	 * Admin initialization actions
26
	 */
27
	public function admin_init() {
28
		$this->none_label = __( 'None', 'carbon-fields' );
29
		$this->button_label = __( 'Select Icon', 'carbon-fields' );
30
	}
31
32
	/**
33
	 * Hook administration scripts and styles.
34
	 */
35
	public static function admin_enqueue_scripts() {
36
		wp_enqueue_style( 'fontawesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', array(), '4.7.0' );
37
	}
38
39
	public function get_default_options() {
40
		$options = array(
41
			''=>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...
42
				'name' => $this->none_label,
43
				'id' => '',
44
				'categories' => array(),
45
				'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...
46
				'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...
47
			),
48
		);
49
		return $options;
50
	}
51
52
	public static function get_fontawesome_options() {
53
		if ( empty( static::$fontawesome_options_cache ) ) {
0 ignored issues
show
Bug introduced by
Since $fontawesome_options_cache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $fontawesome_options_cache 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...
54
			$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...
55
			foreach ( $data['icons'] as $icon ) {
56
				static::$fontawesome_options_cache[ $icon['id'] ] = array(
0 ignored issues
show
Bug introduced by
Since $fontawesome_options_cache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $fontawesome_options_cache 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...
57
					'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...
58
					'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...
59
					'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...
60
					'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...
61
					'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...
62
				);
63
			}
64
		}
65
		return static::$fontawesome_options_cache;
0 ignored issues
show
Bug introduced by
Since $fontawesome_options_cache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $fontawesome_options_cache 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...
66
	}
67
68
	public function add_fontawesome_options() {
69
		return $this->add_options( static::get_fontawesome_options() );
70
	}
71
72
	public static function get_dashicons_options() {
73
		if ( empty( static::$dashicons_options_cache ) ) {
0 ignored issues
show
Bug introduced by
Since $dashicons_options_cache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $dashicons_options_cache 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...
74
			$data = include( \Carbon_Fields\DIR . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'dashicons' . DIRECTORY_SEPARATOR . 'dashicons.php' );
75
			foreach ( $data as $icon ) {
76
				static::$dashicons_options_cache[ $icon ] = array(
0 ignored issues
show
Bug introduced by
Since $dashicons_options_cache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $dashicons_options_cache 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...
77
					'name'=>$icon,
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
78
					'id'=>$icon,
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
79
					'categories'=>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...
80
					'class'=>'dashicons-before ' . $icon,
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
81
					'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...
82
				);
83
			}
84
		}
85
		return static::$dashicons_options_cache;
0 ignored issues
show
Bug introduced by
Since $dashicons_options_cache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $dashicons_options_cache 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...
86
	}
87
88
	public function add_dashicons_options() {
89
		return $this->add_options( static::get_dashicons_options() );
90
	}
91
92
	/**
93
	 * Check if there are callbacks and populate the options
94
	 */
95 View Code Duplication
	protected function load_options() {
0 ignored issues
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...
96
		if ( empty( $this->options ) ) {
97
			return false;
98
		}
99
100
		if ( is_callable( $this->options ) ) {
101
			$options = call_user_func( $this->options );
102
			if ( ! is_array( $options ) ) {
103
				$options = array();
104
			}
105
		} else {
106
			$options = $this->options;
107
		}
108
109
		$this->options = $options;
110
	}
111
112
	/**
113
	 * Returns an array that holds the field data, suitable for JSON representation.
114
	 * This data will be available in the Underscore template and the Backbone Model.
115
	 *
116
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
117
	 * @return array
118
	 */
119
	public function to_json( $load ) {
120
		$field_data = parent::to_json( $load );
121
		$this->load_options();
122
123
		$options = $this->options;
124
		if ( empty( $options ) ) {
125
			$options = static::get_fontawesome_options();
126
		}
127
		$options = $this->get_default_options() + $options;
128
		$options = apply_filters( 'carbon_icon_options', $options, $this->get_name() );
129
130
		$field_data = array_merge( $field_data, array(
131
			'options' => $options,
132
			'button_label' => $this->button_label,
133
		) );
134
135
		return $field_data;
136
	}
137
138
	/**
139
	 * The main Underscore template of this field.
140
	 */
141
	public function template() {
142
		?>
143
		<div class="carbon-icon-container">
144
			<input type="hidden" name="{{{ name }}}" value="{{{ value }}}" class="carbon-icon-value" />
145
			<a href="#" class="carbon-icon-preview">
146
				<i class="{{{ (value && typeof options[value] !== 'undefined') ? options[value].class : 'hidden' }}}"></i>
147
				<span class="button">{{{ button_label }}}</span>
148
			</a>
149
150
			<div class="carbon-icon-popup hidden">
151
				<div class="carbon-icon-search dashicons-before dashicons-search">
152
					<input type="text" value="" placeholder="<?php esc_attr_e( 'Search...', 'carbon-fields' ); ?>" />
153
				</div>
154
				<div class="carbon-icon-scroll">
155
					<ul class="carbon-icon-list">
156
						<# if (options) { #>
157
							<# _.each(options, function(item) { #>
158
								<li class="carbon-icon-icon-container carbon-icon-icon-container-{{{ item.id }}}">
159
									<a href="#" class="carbon-icon-icon-trigger {{{ value == item.id ? 'active' : '' }}}" data-value="{{{ item.id }}}">
160
										<i class="{{{ item.class }}}">{{{ item.contents }}}</i>
161
										<span>{{{ item.name }}}</span>
162
									</a>
163
								</li>
164
							<# }); #>
165
						<# } #>
166
					</ul>
167
				</div>
168
			</div>
169
		</div>
170
		<?php
171
	}
172
}
173