class-learndash-admin-bulk-edit-field.php
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* LearnDash Admin field class.
*
* @since 4.2.0
*
* @package LearnDash\Bulk_Edit
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Learndash_Admin_Bulk_Edit_Field' ) ) {
/**
* Learndash admin field abstract class.
*
* @since 4.2.0
*/
class Learndash_Admin_Bulk_Edit_Field {
const MINIMUM_REQUIRED_ATTRIBUTES = array( 'id', 'label', 'name', 'display_callback' );
/**
* Field attributes.
*
* @since 4.2.0
*
* @var array
*/
protected $attributes;
/**
* Construct.
*
* @since 4.2.0
*
* @param array $attributes Field attributes.
*/
public function __construct( array $attributes ) {
// check required attributes.
if ( count( array_intersect_key( $attributes, array_flip( self::MINIMUM_REQUIRED_ATTRIBUTES ) ) ) !== count( self::MINIMUM_REQUIRED_ATTRIBUTES ) ) {
wp_die(
sprintf(
// translators: placeholder: required fields.
esc_html__( 'Missing required attributes for field: %s', 'learndash' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
implode( ', ', self::MINIMUM_REQUIRED_ATTRIBUTES ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
)
);
}
$this->attributes = array_replace(
$attributes,
array(
'use_raw_name' => true,
)
);
$this->add_raw_name_for_child_fields_recursively( $this->attributes );
}
/**
* Look for child fields to add the use_raw_name key.
*
* @since 4.2.0
*
* @param array $attributes Field attributes array by reference.
*
* @return void
*/
private function add_raw_name_for_child_fields_recursively( array &$attributes ) {
foreach ( $attributes as $key => &$value ) {
if ( 'inline_fields' === $key && is_array( $value ) ) {
$this->add_raw_name_for_inline_fields_recursively( $value );
} elseif ( is_array( $value ) ) {
$this->add_raw_name_for_child_fields_recursively( $value );
}
}
}
/**
* Adds the use_raw_name key to child fields in the args array.
*
* @since 4.2.0
*
* @param array $attributes Field attributes array by reference.
*
* @return void
*/
private function add_raw_name_for_inline_fields_recursively( array &$attributes ): void {
foreach ( $attributes as $key => &$value ) {
if ( 'args' === $key && is_array( $value ) ) {
$value['use_raw_name'] = true;
}
if ( is_array( $value ) ) {
$this->add_raw_name_for_inline_fields_recursively( $value );
}
}
}
/**
* Returns the field id.
*
* @since 4.2.0
*
* @return string
*/
public function get_id(): string {
return $this->attributes['id'];
}
/**
* Returns the field label.
*
* @since 4.2.0
*
* @return string
*/
public function get_label(): string {
return $this->attributes['label'];
}
/**
* Returns the field name.
*
* @since 4.2.0
*
* @return string
*/
public function get_name(): string {
return $this->attributes['name'];
}
/**
* Echoes the input HTML.
*
* @since 4.2.0
*
* @return void
*/
public function display(): void {
call_user_func( $this->attributes['display_callback'], $this->attributes );
}
}
}