| 1 |
<?php |
| 2 |
if ( ! class_exists( 'LP_Meta_Box_Helper' ) ) { |
| 3 |
/** |
| 4 |
* Class LP_Meta_Box_Helper |
| 5 |
*/ |
| 6 |
class LP_Meta_Box_Helper { |
| 7 |
/** |
| 8 |
* @var array |
| 9 |
*/ |
| 10 |
protected static $types = array(); |
| 11 |
|
| 12 |
/** |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected static $conditional_logic = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* @var null |
| 19 |
*/ |
| 20 |
protected static $_screen = null; |
| 21 |
|
| 22 |
public static function output_fields( $options ) { |
| 23 |
foreach ( $options as $value ) { |
| 24 |
if ( ! isset( $value['type'] ) ) { |
| 25 |
continue; |
| 26 |
} |
| 27 |
if ( ! isset( $value['id'] ) ) { |
| 28 |
$value['id'] = ''; |
| 29 |
} |
| 30 |
if ( ! isset( $value['title'] ) ) { |
| 31 |
$value['title'] = isset( $value['name'] ) ? $value['name'] : ''; |
| 32 |
} |
| 33 |
if ( ! isset( $value['class'] ) ) { |
| 34 |
$value['class'] = ''; |
| 35 |
} |
| 36 |
if ( ! isset( $value['css'] ) ) { |
| 37 |
$value['css'] = ''; |
| 38 |
} |
| 39 |
if ( ! isset( $value['default'] ) ) { |
| 40 |
$value['default'] = ''; |
| 41 |
} |
| 42 |
if ( ! isset( $value['desc'] ) ) { |
| 43 |
$value['desc'] = ''; |
| 44 |
} |
| 45 |
if ( ! isset( $value['desc_tip'] ) ) { |
| 46 |
$value['desc_tip'] = false; |
| 47 |
} |
| 48 |
if ( ! isset( $value['placeholder'] ) ) { |
| 49 |
$value['placeholder'] = ''; |
| 50 |
} |
| 51 |
if ( ! isset( $value['suffix'] ) ) { |
| 52 |
$value['suffix'] = ''; |
| 53 |
} |
| 54 |
if ( ! isset( $value['value'] ) ) { |
| 55 |
$value['value'] = self::get_option( $value['id'], $value['default'] ); |
| 56 |
} |
| 57 |
|
| 58 |
$custom_attributes = array(); |
| 59 |
|
| 60 |
if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) { |
| 61 |
foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) { |
| 62 |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$field_description = self::get_field_description( $value ); |
| 67 |
$description = $field_description['description']; |
| 68 |
$tooltip_html = $field_description['tooltip_html']; |
| 69 |
|
| 70 |
switch ( $value['type'] ) { |
| 71 |
case 'text': |
| 72 |
case 'password': |
| 73 |
case 'datetime': |
| 74 |
case 'datetime-local': |
| 75 |
case 'date': |
| 76 |
case 'month': |
| 77 |
case 'time': |
| 78 |
case 'week': |
| 79 |
case 'number': |
| 80 |
case 'email': |
| 81 |
case 'url': |
| 82 |
case 'tel': |
| 83 |
include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/text.php'; |
| 84 |
break; |
| 85 |
case 'select': |
| 86 |
case 'multiselect': |
| 87 |
include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/select.php'; |
| 88 |
break; |
| 89 |
case 'image_advanced': |
| 90 |
include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/image-advanced.php'; |
| 91 |
break; |
| 92 |
case 'checkbox': |
| 93 |
case 'yes-no': |
| 94 |
include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/checkbox.php'; |
| 95 |
break; |
| 96 |
default: |
| 97 |
$file_meta_box_custom = LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/' . $value['type'] . '.php'; |
| 98 |
$file_meta_box_custom = apply_filters( 'learnpress/meta-box/field-custom', $file_meta_box_custom ); |
| 99 |
|
| 100 |
$pattern_find_match = '/\/admin\/meta-box\/fields\//'; |
| 101 |
preg_match( $pattern_find_match, $file_meta_box_custom, $match ); |
| 102 |
|
| 103 |
if ( empty( $match ) ) { |
| 104 |
echo sprintf( '<p class="lp-error">Path file "%s" not valid. Format must is /admin/meta-box/fields</p>', $value['type'] ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( file_exists( $file_meta_box_custom ) ) { |
| 108 |
include $file_meta_box_custom; |
| 109 |
} else { |
| 110 |
echo sprintf( '<p class="lp-error">File meta box "%s" not exists</p>', $value['type'] ); |
| 111 |
} |
| 112 |
|
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public static function save_fields( $options, $data = null ) { |
| 119 |
if ( empty( $data ) ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
$update_options = array(); |
| 124 |
$autoload_options = array(); |
| 125 |
|
| 126 |
foreach ( $options as $option ) { |
| 127 |
if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) || ( isset( $option['is_option'] ) && false === $option['is_option'] ) ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
if ( strstr( $option['id'], '[' ) ) { |
| 132 |
parse_str( $option['id'], $option_name_array ); |
| 133 |
$option_name = current( array_keys( $option_name_array ) ); |
| 134 |
$setting_name = key( $option_name_array[ $option_name ] ); |
| 135 |
$raw_value = isset( $data[ $option_name ][ $setting_name ] ) ? wp_unslash( $data[ $option_name ][ $setting_name ] ) : null; |
| 136 |
} else { |
| 137 |
$option_name = $option['id']; |
| 138 |
$setting_name = ''; |
| 139 |
$raw_value = isset( $data[ $option['id'] ] ) ? wp_unslash( $data[ $option['id'] ] ) : null; |
| 140 |
} |
| 141 |
|
| 142 |
switch ( $option['type'] ) { |
| 143 |
case 'checkbox': |
| 144 |
case 'yes-no': |
| 145 |
$value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no'; |
| 146 |
break; |
| 147 |
case 'textarea': |
| 148 |
$value = trim( $raw_value ); |
| 149 |
break; |
| 150 |
case 'multiselect': |
| 151 |
case 'multi_select_countries': |
| 152 |
$value = array_filter( array_map( 'learnpress_clean', (array) $raw_value ) ); |
| 153 |
break; |
| 154 |
case 'image-dimensions': |
| 155 |
$value = array(); |
| 156 |
if ( isset( $raw_value['width'] ) ) { |
| 157 |
$value['width'] = learnpress_clean( $raw_value['width'] ); |
| 158 |
$value['height'] = learnpress_clean( $raw_value['height'] ); |
| 159 |
$value['crop'] = isset( $raw_value['crop'] ) ? 1 : 0; |
| 160 |
} else { |
| 161 |
$value['width'] = $option['default'][0]; |
| 162 |
$value['height'] = $option['default'][1]; |
| 163 |
$value['crop'] = $option['default'][2]; |
| 164 |
} |
| 165 |
break; |
| 166 |
case 'select': |
| 167 |
$allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) ); |
| 168 |
if ( empty( $option['default'] ) && empty( $allowed_values ) ) { |
| 169 |
$value = null; |
| 170 |
break; |
| 171 |
} |
| 172 |
$default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] ); |
| 173 |
$value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default; |
| 174 |
break; |
| 175 |
case 'custom-fields': |
| 176 |
$value = array(); |
| 177 |
|
| 178 |
if ( is_array( $raw_value ) && ! empty( $raw_value ) ) { |
| 179 |
foreach ( $raw_value as $feilds ) { |
| 180 |
foreach ( $option['options'] as $cfkey => $cfoption ) { |
| 181 |
if ( $cfoption['type'] === 'checkbox' ) { |
| 182 |
$feilds[ $cfkey ] = ( isset( $feilds[ $cfkey ] ) && ( $feilds[ $cfkey ] === '1' || $feilds[ $cfkey ] === 'yes' ) ) ? 'yes' : 'no'; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
$cfsort = $feilds['sort']; |
| 187 |
unset( $feilds['sort'] ); |
| 188 |
$value[ $cfsort ] = $feilds; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
//$value = LP_Helper::sanitize_params_submitted( $value ); |
| 193 |
break; |
| 194 |
|
| 195 |
case 'image_advanced': |
| 196 |
$value = ! empty( $raw_value ) ? array_filter( explode( ',', learnpress_clean( $raw_value ) ) ) : array(); |
| 197 |
break; |
| 198 |
case 'image': |
| 199 |
$value = ! empty( $raw_value ) ? absint( learnpress_clean( $raw_value ) ) : ''; |
| 200 |
break; |
| 201 |
case 'email-content': |
| 202 |
$value = ! empty( $raw_value ) ? $raw_value : array(); |
| 203 |
//$value = LP_Helper::sanitize_params_submitted( $value, 'html' ); |
| 204 |
break; |
| 205 |
case 'url': |
| 206 |
$value = ! empty( $raw_value ) ? esc_url_raw( $raw_value ) : ''; |
| 207 |
break; |
| 208 |
default: |
| 209 |
$value = $raw_value; |
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Sanitize the value of an option. |
| 215 |
* |
| 216 |
* @since 4.0.0 |
| 217 |
*/ |
| 218 |
$value = apply_filters( 'learnpress_metabox_settings_sanitize_option', $value, $option, $raw_value ); |
| 219 |
|
| 220 |
/** |
| 221 |
* Sanitize the value of an option by option name. |
| 222 |
* |
| 223 |
* @since 4.0.0 |
| 224 |
*/ |
| 225 |
$value = apply_filters( "learnpress_metabox_settings_sanitize_option_$option_name", $value, $option, $raw_value ); |
| 226 |
|
| 227 |
if ( is_null( $value ) ) { |
| 228 |
continue; |
| 229 |
} |
| 230 |
|
| 231 |
if ( $option_name && $setting_name ) { |
| 232 |
if ( ! isset( $update_options[ $option_name ] ) ) { |
| 233 |
$update_options[ $option_name ] = get_option( $option_name, array() ); |
| 234 |
} |
| 235 |
if ( ! is_array( $update_options[ $option_name ] ) ) { |
| 236 |
$update_options[ $option_name ] = array(); |
| 237 |
} |
| 238 |
$update_options[ $option_name ][ $setting_name ] = $value; |
| 239 |
} else { |
| 240 |
$update_options[ $option_name ] = $value; |
| 241 |
} |
| 242 |
|
| 243 |
$autoload_options[ $option_name ] = isset( $option['autoload'] ) ? (bool) $option['autoload'] : true; |
| 244 |
|
| 245 |
do_action( 'learnpress_update_metabox_option', $option ); |
| 246 |
|
| 247 |
// Save all options in our array. |
| 248 |
foreach ( $update_options as $name => $value ) { |
| 249 |
update_option( $name, $value, $autoload_options[ $name ] ? 'yes' : 'no' ); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public static function get_field_description( $value ) { |
| 255 |
$description = ''; |
| 256 |
$tooltip_html = ''; |
| 257 |
|
| 258 |
if ( true === $value['desc_tip'] ) { |
| 259 |
$tooltip_html = $value['desc']; |
| 260 |
} elseif ( ! empty( $value['desc_tip'] ) ) { |
| 261 |
$description = $value['desc']; |
| 262 |
$tooltip_html = $value['desc_tip']; |
| 263 |
} elseif ( ! empty( $value['desc'] ) ) { |
| 264 |
$description = $value['desc']; |
| 265 |
} |
| 266 |
|
| 267 |
if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ), true ) ) { |
| 268 |
$description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>'; |
| 269 |
} elseif ( $description && in_array( $value['type'], array( 'checkbox', 'yes-no' ), true ) ) { |
| 270 |
$description = wp_kses_post( $description ); |
| 271 |
} elseif ( $description ) { |
| 272 |
$description = '<p class="description">' . wp_kses_post( $description ) . '</p>'; |
| 273 |
} |
| 274 |
|
| 275 |
if ( $tooltip_html && in_array( $value['type'], array( 'checkbox' ), true ) ) { |
| 276 |
$tooltip_html = '<p class="description">' . $tooltip_html . '</p>'; |
| 277 |
} elseif ( $tooltip_html ) { |
| 278 |
$tooltip_html = learn_press_quick_tip( $tooltip_html, false ); |
| 279 |
} |
| 280 |
|
| 281 |
return array( |
| 282 |
'description' => $description, |
| 283 |
'tooltip_html' => $tooltip_html, |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
public static function get_option( $option_name, $default = '' ) { |
| 288 |
if ( strstr( $option_name, '[' ) ) { |
| 289 |
parse_str( $option_name, $option_array ); |
| 290 |
|
| 291 |
// Option name is first key |
| 292 |
$option_name = current( array_keys( $option_array ) ); |
| 293 |
|
| 294 |
// Get value |
| 295 |
$option_values = get_option( $option_name, '' ); |
| 296 |
|
| 297 |
$key = key( $option_array[ $option_name ] ); |
| 298 |
|
| 299 |
if ( is_array( $option_array[ $option_name ][ $key ] ) ) { |
| 300 |
$key_child = key( $option_array[ $option_name ][ $key ] ); |
| 301 |
} |
| 302 |
|
| 303 |
if ( isset( $option_values[ $key ] ) ) { |
| 304 |
$option_value = $option_values[ $key ]; |
| 305 |
|
| 306 |
if ( is_array( $option_value ) && isset( $key_child ) |
| 307 |
&& array_key_exists( $key_child, $option_value ) ) { |
| 308 |
$option_value = $option_value[ $key_child ]; |
| 309 |
} |
| 310 |
} else { |
| 311 |
$option_value = null; |
| 312 |
} |
| 313 |
} else { |
| 314 |
// Single value |
| 315 |
$option_value = LP_Settings::instance()->get( preg_replace( '!^learn_press_!', '', $option_name ), null ); |
| 316 |
} |
| 317 |
|
| 318 |
if ( ! is_array( $option_value ) && ! is_null( $option_value ) ) { |
| 319 |
$option_value = stripslashes( $option_value ); |
| 320 |
} |
| 321 |
|
| 322 |
return $option_value === null ? $default : $option_value; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|