| 1 |
<?php if (! defined('ABSPATH')) { |
| 2 |
die; |
| 3 |
} // Cannot access directly. |
| 4 |
|
| 5 |
/** |
| 6 |
* DRK_Fields Abstract Class |
| 7 |
* |
| 8 |
* This is a base abstract class for creating fields in a WordPress plugin. |
| 9 |
* All field types should extend this class to ensure consistency. |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
* @version 1.0.0 |
| 13 |
*/ |
| 14 |
if (! class_exists('DarkifyFields')) { |
| 15 |
abstract class DarkifyFields extends DarkifyAbstract |
| 16 |
{ |
| 17 |
|
| 18 |
/** |
| 19 |
* @var array $field Field configuration array. |
| 20 |
* @var mixed $value Field value. |
| 21 |
* @var string $unique Unique identifier for the field. |
| 22 |
* @var string $where Context or location where the field is used. |
| 23 |
* @var mixed $parent Parent field or group, if applicable. |
| 24 |
*/ |
| 25 |
protected $field; |
| 26 |
protected $value; |
| 27 |
protected $unique; |
| 28 |
protected $where; |
| 29 |
protected $parent; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor for the DRK_Fields class. |
| 33 |
* |
| 34 |
* @param array $field Field configuration array. |
| 35 |
* @param mixed $value Field value. |
| 36 |
* @param string $unique Unique identifier for the field. |
| 37 |
* @param string $where Context where the field is being used. |
| 38 |
* @param mixed $parent Parent field, if applicable. |
| 39 |
*/ |
| 40 |
public function __construct($field = array(), $value = '', $unique = '', $where = '', $parent = '') |
| 41 |
{ |
| 42 |
$this->field = $field; |
| 43 |
$this->value = $value; |
| 44 |
$this->unique = $unique; |
| 45 |
$this->where = $where; |
| 46 |
$this->parent = $parent; |
| 47 |
} |
| 48 |
|
| 49 |
public function field_name($nested_name = '') |
| 50 |
{ |
| 51 |
|
| 52 |
$field_id = (! empty($this->field['id'])) ? $this->field['id'] : ''; |
| 53 |
$unique_id = (! empty($this->unique)) ? $this->unique . '[' . $field_id . ']' : $field_id; |
| 54 |
$field_name = (! empty($this->field['name'])) ? $this->field['name'] : $unique_id; |
| 55 |
$tag_prefix = (! empty($this->field['tag_prefix'])) ? $this->field['tag_prefix'] : ''; |
| 56 |
|
| 57 |
if (! empty($tag_prefix)) { |
| 58 |
$nested_name = str_replace('[', '[' . $tag_prefix, $nested_name); |
| 59 |
} |
| 60 |
|
| 61 |
return $field_name . $nested_name; |
| 62 |
} |
| 63 |
|
| 64 |
public function field_attributes($custom_atts = array()) |
| 65 |
{ |
| 66 |
|
| 67 |
$field_id = (! empty($this->field['id'])) ? $this->field['id'] : ''; |
| 68 |
$attributes = (! empty($this->field['attributes'])) ? $this->field['attributes'] : array(); |
| 69 |
|
| 70 |
if (! empty($field_id) && empty($attributes['data-depend-id'])) { |
| 71 |
$attributes['data-depend-id'] = $field_id; |
| 72 |
} |
| 73 |
|
| 74 |
if (! empty($this->field['placeholder'])) { |
| 75 |
$attributes['placeholder'] = $this->field['placeholder']; |
| 76 |
} |
| 77 |
|
| 78 |
$attributes = wp_parse_args($attributes, $custom_atts); |
| 79 |
|
| 80 |
$atts = ''; |
| 81 |
|
| 82 |
if (! empty($attributes)) { |
| 83 |
foreach ($attributes as $key => $value) { |
| 84 |
if ($value === 'only-key') { |
| 85 |
$atts .= ' ' . esc_attr($key); |
| 86 |
} else { |
| 87 |
$atts .= ' ' . esc_attr($key) . '="' . esc_attr($value) . '"'; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $atts; |
| 93 |
} |
| 94 |
|
| 95 |
public function field_before() |
| 96 |
{ |
| 97 |
return (! empty($this->field['before'])) ? '<div class="darkify-before-text">' . $this->field['before'] . '</div>' : ''; |
| 98 |
} |
| 99 |
|
| 100 |
public function field_after() |
| 101 |
{ |
| 102 |
|
| 103 |
$output = (! empty($this->field['after'])) ? '<div class="darkify-after-text">' . $this->field['after'] . '</div>' : ''; |
| 104 |
$output .= (! empty($this->field['desc'])) ? '<div class="clear"></div><div class="darkify-desc-text">' . $this->field['desc'] . '</div>' : ''; |
| 105 |
$output .= (! empty($this->field['help'])) ? '<div class="darkify-help"><span class="darkify-help-text">' . $this->field['help'] . '</span><i class="icofont-question-circle"></i></div>' : ''; |
| 106 |
$output .= (! empty($this->field['_error'])) ? '<div class="darkify-error-text">' . $this->field['_error'] . '</div>' : ''; |
| 107 |
|
| 108 |
return $output; |
| 109 |
} |
| 110 |
|
| 111 |
public static function field_data($type = '', $term = false, $query_args = array()) |
| 112 |
{ |
| 113 |
|
| 114 |
$options = array(); |
| 115 |
$array_search = false; |
| 116 |
|
| 117 |
// sanitize type name |
| 118 |
if (in_array($type, array('page', 'pages'))) { |
| 119 |
$option = 'page'; |
| 120 |
} else if (in_array($type, array('post', 'posts'))) { |
| 121 |
$option = 'post'; |
| 122 |
} else if (in_array($type, array('category', 'categories'))) { |
| 123 |
$option = 'category'; |
| 124 |
} else if (in_array($type, array('tag', 'tags'))) { |
| 125 |
$option = 'post_tag'; |
| 126 |
} else if (in_array($type, array('menu', 'menus'))) { |
| 127 |
$option = 'nav_menu'; |
| 128 |
} else { |
| 129 |
$option = ''; |
| 130 |
} |
| 131 |
|
| 132 |
// switch type |
| 133 |
switch ($type) { |
| 134 |
|
| 135 |
case 'page': |
| 136 |
case 'pages': |
| 137 |
case 'post': |
| 138 |
case 'posts': |
| 139 |
|
| 140 |
// term query required for ajax select |
| 141 |
if (! empty($term)) { |
| 142 |
|
| 143 |
$query = new WP_Query(wp_parse_args($query_args, array( |
| 144 |
's' => $term, |
| 145 |
'post_type' => $option, |
| 146 |
'post_status' => 'publish', |
| 147 |
'posts_per_page' => 25, |
| 148 |
))); |
| 149 |
} else { |
| 150 |
|
| 151 |
$query = new WP_Query(wp_parse_args($query_args, array( |
| 152 |
'post_type' => $option, |
| 153 |
'post_status' => 'publish', |
| 154 |
))); |
| 155 |
} |
| 156 |
|
| 157 |
if (! is_wp_error($query) && ! empty($query->posts)) { |
| 158 |
foreach ($query->posts as $item) { |
| 159 |
$options[$item->ID] = $item->post_title; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
break; |
| 164 |
|
| 165 |
case 'category': |
| 166 |
case 'categories': |
| 167 |
case 'tag': |
| 168 |
case 'tags': |
| 169 |
case 'menu': |
| 170 |
case 'menus': |
| 171 |
|
| 172 |
if (! empty($term)) { |
| 173 |
|
| 174 |
$query = new WP_Term_Query(wp_parse_args($query_args, array( |
| 175 |
'search' => $term, |
| 176 |
'taxonomy' => $option, |
| 177 |
'hide_empty' => false, |
| 178 |
'number' => 25, |
| 179 |
))); |
| 180 |
} else { |
| 181 |
|
| 182 |
$query = new WP_Term_Query(wp_parse_args($query_args, array( |
| 183 |
'taxonomy' => $option, |
| 184 |
'hide_empty' => false, |
| 185 |
))); |
| 186 |
} |
| 187 |
|
| 188 |
if (! is_wp_error($query) && ! empty($query->terms)) { |
| 189 |
foreach ($query->terms as $item) { |
| 190 |
$options[$item->term_id] = $item->name; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
break; |
| 195 |
|
| 196 |
case 'user': |
| 197 |
case 'users': |
| 198 |
|
| 199 |
if (! empty($term)) { |
| 200 |
|
| 201 |
$query = new WP_User_Query(array( |
| 202 |
'search' => '*' . $term . '*', |
| 203 |
'number' => 25, |
| 204 |
'orderby' => 'title', |
| 205 |
'order' => 'ASC', |
| 206 |
'fields' => array('display_name', 'ID') |
| 207 |
)); |
| 208 |
} else { |
| 209 |
|
| 210 |
$query = new WP_User_Query(array('fields' => array('display_name', 'ID'))); |
| 211 |
} |
| 212 |
|
| 213 |
if (! is_wp_error($query) && ! empty($query->get_results())) { |
| 214 |
foreach ($query->get_results() as $item) { |
| 215 |
$options[$item->ID] = $item->display_name; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
break; |
| 220 |
|
| 221 |
case 'sidebar': |
| 222 |
case 'sidebars': |
| 223 |
|
| 224 |
global $wp_registered_sidebars; |
| 225 |
|
| 226 |
if (! empty($wp_registered_sidebars)) { |
| 227 |
foreach ($wp_registered_sidebars as $sidebar) { |
| 228 |
$options[$sidebar['id']] = $sidebar['name']; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
$array_search = true; |
| 233 |
|
| 234 |
break; |
| 235 |
|
| 236 |
case 'role': |
| 237 |
case 'roles': |
| 238 |
|
| 239 |
global $wp_roles; |
| 240 |
|
| 241 |
if (! empty($wp_roles)) { |
| 242 |
if (! empty($wp_roles->roles)) { |
| 243 |
foreach ($wp_roles->roles as $role_key => $role_value) { |
| 244 |
$options[$role_key] = $role_value['name']; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
$array_search = true; |
| 250 |
|
| 251 |
break; |
| 252 |
|
| 253 |
case 'post_type': |
| 254 |
case 'post_types': |
| 255 |
|
| 256 |
$post_types = get_post_types(array('show_in_nav_menus' => true), 'objects'); |
| 257 |
|
| 258 |
if (! is_wp_error($post_types) && ! empty($post_types)) { |
| 259 |
foreach ($post_types as $post_type) { |
| 260 |
$options[$post_type->name] = $post_type->labels->name; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
$array_search = true; |
| 265 |
|
| 266 |
break; |
| 267 |
|
| 268 |
case 'location': |
| 269 |
case 'locations': |
| 270 |
|
| 271 |
$nav_menus = get_registered_nav_menus(); |
| 272 |
|
| 273 |
if (! is_wp_error($nav_menus) && ! empty($nav_menus)) { |
| 274 |
foreach ($nav_menus as $nav_menu_key => $nav_menu_name) { |
| 275 |
$options[$nav_menu_key] = $nav_menu_name; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
$array_search = true; |
| 280 |
|
| 281 |
break; |
| 282 |
|
| 283 |
default: |
| 284 |
|
| 285 |
if (is_callable($type)) { |
| 286 |
if (! empty($term)) { |
| 287 |
$options = call_user_func($type, $query_args); |
| 288 |
} else { |
| 289 |
$options = call_user_func($type, $term, $query_args); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
break; |
| 294 |
} |
| 295 |
|
| 296 |
// Array search by "term" |
| 297 |
if (! empty($term) && ! empty($options) && ! empty($array_search)) { |
| 298 |
$options = preg_grep('/' . $term . '/i', $options); |
| 299 |
} |
| 300 |
|
| 301 |
// Make multidimensional array for ajax search |
| 302 |
if (! empty($term) && ! empty($options)) { |
| 303 |
$arr = array(); |
| 304 |
foreach ($options as $option_key => $option_value) { |
| 305 |
$arr[] = array('value' => $option_key, 'text' => $option_value); |
| 306 |
} |
| 307 |
$options = $arr; |
| 308 |
} |
| 309 |
|
| 310 |
return $options; |
| 311 |
} |
| 312 |
|
| 313 |
public function field_wp_query_data_title($type, $values) |
| 314 |
{ |
| 315 |
|
| 316 |
$options = array(); |
| 317 |
|
| 318 |
if (! empty($values) && is_array($values)) { |
| 319 |
|
| 320 |
foreach ($values as $value) { |
| 321 |
|
| 322 |
$options[$value] = ucfirst($value); |
| 323 |
|
| 324 |
switch ($type) { |
| 325 |
|
| 326 |
case 'post': |
| 327 |
case 'posts': |
| 328 |
case 'page': |
| 329 |
case 'pages': |
| 330 |
|
| 331 |
$title = get_the_title($value); |
| 332 |
|
| 333 |
if (! is_wp_error($title) && ! empty($title)) { |
| 334 |
$options[$value] = $title; |
| 335 |
} |
| 336 |
|
| 337 |
break; |
| 338 |
|
| 339 |
case 'category': |
| 340 |
case 'categories': |
| 341 |
case 'tag': |
| 342 |
case 'tags': |
| 343 |
case 'menu': |
| 344 |
case 'menus': |
| 345 |
|
| 346 |
$term = get_term($value); |
| 347 |
|
| 348 |
if (! is_wp_error($term) && ! empty($term)) { |
| 349 |
$options[$value] = $term->name; |
| 350 |
} |
| 351 |
|
| 352 |
break; |
| 353 |
|
| 354 |
case 'user': |
| 355 |
case 'users': |
| 356 |
|
| 357 |
$user = get_user_by('id', $value); |
| 358 |
|
| 359 |
if (! is_wp_error($user) && ! empty($user)) { |
| 360 |
$options[$value] = $user->display_name; |
| 361 |
} |
| 362 |
|
| 363 |
break; |
| 364 |
|
| 365 |
case 'sidebar': |
| 366 |
case 'sidebars': |
| 367 |
|
| 368 |
global $wp_registered_sidebars; |
| 369 |
|
| 370 |
if (! empty($wp_registered_sidebars[$value])) { |
| 371 |
$options[$value] = $wp_registered_sidebars[$value]['name']; |
| 372 |
} |
| 373 |
|
| 374 |
break; |
| 375 |
|
| 376 |
case 'role': |
| 377 |
case 'roles': |
| 378 |
|
| 379 |
global $wp_roles; |
| 380 |
|
| 381 |
if (! empty($wp_roles) && ! empty($wp_roles->roles) && ! empty($wp_roles->roles[$value])) { |
| 382 |
$options[$value] = $wp_roles->roles[$value]['name']; |
| 383 |
} |
| 384 |
|
| 385 |
break; |
| 386 |
|
| 387 |
case 'post_type': |
| 388 |
case 'post_types': |
| 389 |
|
| 390 |
$post_types = get_post_types(array('show_in_nav_menus' => true)); |
| 391 |
|
| 392 |
if (! is_wp_error($post_types) && ! empty($post_types) && ! empty($post_types[$value])) { |
| 393 |
$options[$value] = ucfirst($value); |
| 394 |
} |
| 395 |
|
| 396 |
break; |
| 397 |
|
| 398 |
case 'location': |
| 399 |
case 'locations': |
| 400 |
|
| 401 |
$nav_menus = get_registered_nav_menus(); |
| 402 |
|
| 403 |
if (! is_wp_error($nav_menus) && ! empty($nav_menus) && ! empty($nav_menus[$value])) { |
| 404 |
$options[$value] = $nav_menus[$value]; |
| 405 |
} |
| 406 |
|
| 407 |
break; |
| 408 |
|
| 409 |
default: |
| 410 |
|
| 411 |
if (is_callable($type . '_title')) { |
| 412 |
$options[$value] = call_user_func($type . '_title', $value); |
| 413 |
} |
| 414 |
|
| 415 |
break; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
return $options; |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|