1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Interface for WordPress core |
5
|
|
|
* |
6
|
|
|
* @package uix |
7
|
|
|
* @author David Cramer |
8
|
|
|
* @license GPL-2.0+ |
9
|
|
|
* @link |
10
|
|
|
* @copyright 2016 David Cramer |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace uix\ui\core; |
14
|
|
|
|
15
|
|
|
class core { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Set this object type assets |
19
|
|
|
* |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
* @access public |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
public $assets = [ |
25
|
|
|
'script' => [], |
26
|
|
|
'style' => [], |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* setup actions and hooks - override to add specific hooks. use |
31
|
|
|
* parent::actions() to keep admin head |
32
|
|
|
* |
33
|
|
|
* @since 1.0.0 |
34
|
|
|
* @access protected |
35
|
|
|
*/ |
36
|
|
|
protected function actions() { |
37
|
|
|
|
38
|
|
|
// init uix after loaded. |
39
|
|
|
add_action( 'init', [ $this, 'init' ] ); |
40
|
|
|
// set location. |
41
|
|
|
$location = 'wp_print_styles'; |
42
|
|
|
if ( is_admin() ) { |
43
|
|
|
$location = 'admin_enqueue_scripts'; |
44
|
|
|
} |
45
|
|
|
// init UIX headers. |
46
|
|
|
add_action( $location, [ $this, 'enqueue_core' ] ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Define core UIX styles - override to register core ( common styles for |
52
|
|
|
* uix type ) |
53
|
|
|
* |
54
|
|
|
* @since 1.0.0 |
55
|
|
|
* @access public |
56
|
|
|
*/ |
57
|
|
|
public function set_assets() { |
58
|
|
|
if ( ! empty( $this->struct['style'] ) ) { |
59
|
|
|
$this->assets['style'] = array_merge( $this->assets['style'], $this->struct['style'] ); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
if ( ! empty( $this->struct['script'] ) ) { |
62
|
|
|
$this->assets['script'] = array_merge( $this->assets['script'], $this->struct['script'] ); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* enqueue core assets |
68
|
|
|
* |
69
|
|
|
* @since 1.0.0 |
70
|
|
|
* @access public |
71
|
|
|
*/ |
72
|
|
|
public function enqueue_core() { |
73
|
|
|
|
74
|
|
|
// Register uix core asset. |
75
|
|
|
$this->core_assets(); |
76
|
|
|
$this->enqueue_actions(); |
77
|
|
|
// push assets to ui manager. |
78
|
|
|
uix()->set_assets( $this->assets ); |
79
|
|
|
// done enqueuing - dpo inline or manual enqueue. |
80
|
|
|
$this->set_active_styles(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Do core enqueue actions. |
85
|
|
|
* |
86
|
|
|
* @since 3.0.0 |
87
|
|
|
* @access public |
88
|
|
|
*/ |
89
|
|
|
private function enqueue_actions() { |
90
|
|
|
/** |
91
|
|
|
* Do object initilisation. |
92
|
|
|
* |
93
|
|
|
* @param object current uix instance |
94
|
|
|
*/ |
95
|
|
|
do_action( 'uix_admin_enqueue_scripts_' . $this->type, $this ); |
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Do object initilisation for specific slug. |
99
|
|
|
* |
100
|
|
|
* @param object current uix instance |
101
|
|
|
*/ |
102
|
|
|
do_action( 'uix_admin_enqueue_scripts_' . $this->type . '_' . $this->slug, $this ); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Register UIX depend js and css and call set assets |
107
|
|
|
* |
108
|
|
|
* @since 1.0.0 |
109
|
|
|
* @access protected |
110
|
|
|
*/ |
111
|
|
|
protected function core_assets() { |
112
|
|
|
wp_register_script( 'uix', $this->url . 'assets/js/core' . UIX_ASSET_DEBUG . '.js' ); |
|
|
|
|
113
|
|
|
wp_register_style( 'uix', $this->url . 'assets/css/core' . UIX_ASSET_DEBUG . '.css', [ 'dashicons' ] ); |
114
|
|
|
wp_localize_script( 'uix', 'uixApi', [ |
115
|
|
|
'root' => esc_url_raw( rest_url() ), |
116
|
|
|
'nonce' => wp_create_nonce( 'wp_rest' ), |
117
|
|
|
] ); |
118
|
|
|
|
119
|
|
|
// set assets . methods at before this point can set assets, after this not so much. |
120
|
|
|
$this->set_assets(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* runs after assets have been enqueued |
125
|
|
|
* |
126
|
|
|
* @since 1.0.0 |
127
|
|
|
* @access protected |
128
|
|
|
*/ |
129
|
|
|
protected function set_active_styles() { |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets ajax assets. |
134
|
|
|
* |
135
|
|
|
* @since 3.0.0 |
136
|
|
|
* @access protected |
137
|
|
|
*/ |
138
|
|
|
protected function set_ajax_assets() { |
139
|
|
|
$this->assets = array( |
140
|
|
|
'script' => array( |
141
|
|
|
'baldrick' => array( |
142
|
|
|
'src' => $this->url . 'assets/js/jquery.baldrick' . UIX_ASSET_DEBUG . '.js', |
143
|
|
|
'deps' => array( 'jquery' ), |
144
|
|
|
), |
145
|
|
|
'uix-ajax' => array( |
146
|
|
|
'src' => $this->url . 'assets/js/ajax' . UIX_ASSET_DEBUG . '.js', |
147
|
|
|
'deps' => array( 'baldrick' ), |
148
|
|
|
), |
149
|
|
|
), |
150
|
|
|
'style' => array( |
151
|
|
|
'uix-ajax' => $this->url . 'assets/css/ajax' . UIX_ASSET_DEBUG . '.css', |
152
|
|
|
), |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Base color helper |
158
|
|
|
* |
159
|
|
|
* @since 1.0.0 |
160
|
|
|
* @access public |
161
|
|
|
*/ |
162
|
|
|
protected function base_color() { |
163
|
|
|
$color = '#D84315'; |
164
|
|
|
if ( empty( $this->struct['base_color'] ) ) { |
165
|
|
|
if ( ! empty( $this->parent ) ) { |
166
|
|
|
$color = $this->parent->base_color(); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} else { |
169
|
|
|
$color = $this->struct['base_color']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* do object initilisation for specific slug |
174
|
|
|
* |
175
|
|
|
* @param object current uix instance |
176
|
|
|
*/ |
177
|
|
|
return apply_filters( 'uix_base_color_' . $this->type . '_' . $this->slug, $color ); |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: