|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Page Framework |
|
4
|
|
|
* |
|
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
|
6
|
|
|
* Copyright (c) 2013-2015 Michael Uno; Licensed MIT |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Provides utility methods which do not use WordPress functions. |
|
12
|
|
|
* |
|
13
|
|
|
* @since DEVVER |
|
14
|
|
|
* @subpackage Utility |
|
15
|
|
|
* @internal |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AdminPageFramework_Utility_HTMLAttribute extends AdminPageFramework_Utility_SystemInformation { |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Generates inline CSS rules from the given array. |
|
21
|
|
|
* |
|
22
|
|
|
* For example, |
|
23
|
|
|
* <code> |
|
24
|
|
|
* array( |
|
25
|
|
|
* 'width' => '32px', |
|
26
|
|
|
* 'height' => '32px', |
|
27
|
|
|
* ) |
|
28
|
|
|
* </code> |
|
29
|
|
|
* will be |
|
30
|
|
|
* <code> |
|
31
|
|
|
* 'width: 32px; height: 32px;' |
|
32
|
|
|
* </code> |
|
33
|
|
|
* |
|
34
|
|
|
* @since 3.6.0 |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
static public function getInlineCSS( array $aCSSRules ) { |
|
|
|
|
|
|
38
|
|
|
$_aOutput = array(); |
|
39
|
|
|
foreach( $aCSSRules as $_sProperty => $_sValue ) { |
|
40
|
|
|
$_aOutput[] = $_sProperty . ': ' . $_sValue; |
|
41
|
|
|
} |
|
42
|
|
|
return implode( '; ', $_aOutput ); |
|
43
|
|
|
} |
|
44
|
|
|
/** |
|
45
|
|
|
* @since 3.2.0 |
|
46
|
|
|
* @deprecated 3.6.0 Use `getInlineCSS()` instead. |
|
47
|
|
|
*/ |
|
48
|
|
|
static public function generateInlineCSS( array $aCSSRules ) { |
|
|
|
|
|
|
49
|
|
|
return self::getInlineCSS( $aCSSRules ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Generates a string of inline styles for the style attribute value from multiple arguments. |
|
54
|
|
|
* |
|
55
|
|
|
* Duplicated items will be merged. |
|
56
|
|
|
* |
|
57
|
|
|
* For example, |
|
58
|
|
|
* ` |
|
59
|
|
|
* getStyleAttribute( array( 'margin-top' => '10px', 'display: inline-block' ), 'float:right; display: none;' ) |
|
60
|
|
|
* ` |
|
61
|
|
|
* will generate |
|
62
|
|
|
* ` |
|
63
|
|
|
* margin-top: 10px; display: inline-block; float:right; |
|
64
|
|
|
* ` |
|
65
|
|
|
* @since 3.6.0 |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
static public function getStyleAttribute( $asInlineCSSes ) { |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$_aCSSRules = array(); |
|
71
|
|
|
foreach( array_reverse( func_get_args() ) as $_asCSSRules ) { |
|
72
|
|
|
|
|
73
|
|
|
// For array, store in the container. |
|
74
|
|
|
if ( is_array( $_asCSSRules ) ) { |
|
75
|
|
|
$_aCSSRules = array_merge( $_asCSSRules, $_aCSSRules ); |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// At this point, it is a string. Break them down to array elements. |
|
80
|
|
|
$__aCSSRules = explode( ';', $_asCSSRules ); |
|
81
|
|
|
foreach( $__aCSSRules as $_sPair ) { |
|
82
|
|
|
$_aCSSPair = explode( ':', $_sPair ); |
|
83
|
|
|
if ( ! isset( $_aCSSPair[ 0 ], $_aCSSPair[ 1 ] ) ) { |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
$_aCSSRules[ $_aCSSPair[ 0 ] ] = $_aCSSPair[ 1 ]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
return self::getInlineCSS( array_unique( $_aCSSRules ) ); |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
/** |
|
94
|
|
|
* @since 3.3.1 |
|
95
|
|
|
* @deprecated 3.6.0 Use `getStyleAttribute()` instead. |
|
96
|
|
|
*/ |
|
97
|
|
|
static public function generateStyleAttribute( $asInlineCSSes ) { |
|
|
|
|
|
|
98
|
|
|
self::getStyleAttribute( $asInlineCSSes ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Generates a string of class selectors from multiple arguments. |
|
103
|
|
|
* |
|
104
|
|
|
* For example, |
|
105
|
|
|
* <code> |
|
106
|
|
|
* $sClasses = getClassAttribute( array( 'button, button-primary' ), 'remove_button button' ); |
|
107
|
|
|
* </code> |
|
108
|
|
|
* Will generates |
|
109
|
|
|
* <code> |
|
110
|
|
|
* button button-primary remove_button |
|
111
|
|
|
* </code> |
|
112
|
|
|
* |
|
113
|
|
|
* @remark Duplicated items will be merged. |
|
114
|
|
|
* @since 3.6.0 |
|
115
|
|
|
* @todo Fix an issue that when a multidimensional array is passed, it causes a warning: Notice: Array to string conversion. |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
static public function getClassAttribute( /* $asClassSelectors1, $asClassSelectors12, ... */ ) { |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$_aClasses = array(); |
|
121
|
|
|
foreach( func_get_args() as $_asClasses ) { |
|
122
|
|
|
if ( ! in_array( gettype( $_asClasses ), array( 'array', 'string' ) ) ) { |
|
123
|
|
|
continue; |
|
124
|
|
|
} |
|
125
|
|
|
$_aClasses = array_merge( |
|
126
|
|
|
$_aClasses, |
|
127
|
|
|
is_array( $_asClasses ) |
|
128
|
|
|
? $_asClasses |
|
129
|
|
|
: explode( ' ', $_asClasses ) |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
$_aClasses = array_unique( array_filter( $_aClasses ) ); |
|
133
|
|
|
|
|
134
|
|
|
// @todo examine if it is okay to remove the trim() function below. |
|
135
|
|
|
return trim( implode( ' ', $_aClasses ) ); |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
/** |
|
139
|
|
|
* Generates a string of class selectors from multiple arguments. |
|
140
|
|
|
* |
|
141
|
|
|
* @since 3.2.0 |
|
142
|
|
|
* @return string |
|
143
|
|
|
* @deprecated 3.6.0 |
|
144
|
|
|
*/ |
|
145
|
|
|
static public function generateClassAttribute( /* $asClassSelectors1, $asClassSelectors12 ... */ ) { |
|
|
|
|
|
|
146
|
|
|
$_aParams = func_get_args(); |
|
147
|
|
|
return call_user_func_array( |
|
148
|
|
|
array( __CLASS__ , 'getClassAttribute' ), |
|
149
|
|
|
$_aParams |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns an array for generating a data attribute from the given associative array. |
|
155
|
|
|
* |
|
156
|
|
|
* @since 3.4.0 |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
|
|
static public function getDataAttributeArray( array $aArray ) { |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
$_aNewArray = array(); |
|
162
|
|
|
foreach( $aArray as $sKey => $v ) { |
|
163
|
|
|
if ( in_array( gettype( $v ), array( 'array', 'object' ) ) ) { |
|
164
|
|
|
continue; |
|
165
|
|
|
} |
|
166
|
|
|
$_aNewArray[ "data-{$sKey}" ] = $v ? $v : '0'; |
|
167
|
|
|
} |
|
168
|
|
|
return $_aNewArray; |
|
169
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.