Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Give_Admin_Settings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Admin_Settings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Give_Admin_Settings { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Setting pages. |
||
| 27 | * |
||
| 28 | * @since 1.8 |
||
| 29 | * @var array List of settings. |
||
| 30 | */ |
||
| 31 | private static $settings = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Setting filter and action prefix. |
||
| 35 | * |
||
| 36 | * @since 1.8 |
||
| 37 | * @var string setting fileter and action anme prefix. |
||
| 38 | */ |
||
| 39 | private static $setting_filter_prefix = ''; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Error messages. |
||
| 43 | * |
||
| 44 | * @since 1.8 |
||
| 45 | * @var array List of errors. |
||
| 46 | */ |
||
| 47 | private static $errors = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Update messages. |
||
| 51 | * |
||
| 52 | * @since 1.8 |
||
| 53 | * @var array List of messages. |
||
| 54 | */ |
||
| 55 | private static $messages = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Include the settings page classes. |
||
| 59 | * |
||
| 60 | * @since 1.8 |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public static function get_settings_pages() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Save the settings. |
||
| 82 | * |
||
| 83 | * @since 1.8 |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public static function save() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add a message. |
||
| 121 | * |
||
| 122 | * @since 1.8 |
||
| 123 | * |
||
| 124 | * @param string $code Message code (Note: This should be unique). |
||
| 125 | * @param string $message Message text. |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public static function add_message( $code, $message ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Add an error. |
||
| 135 | * |
||
| 136 | * @since 1.8 |
||
| 137 | * |
||
| 138 | * @param string $code Message code (Note: This should be unique). |
||
| 139 | * @param string $message Message text. |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public static function add_error( $code, $message ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Output messages + errors. |
||
| 149 | * |
||
| 150 | * @since 1.8 |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public static function show_messages() { |
||
| 154 | $notice_html = ''; |
||
| 155 | $classes = 'give-notice settings-error notice is-dismissible'; |
||
| 156 | |||
| 157 | self::$errors = apply_filters( self::$setting_filter_prefix . '_error_notices', self::$errors ); |
||
| 158 | self::$messages = apply_filters( self::$setting_filter_prefix . '_update_notices', self::$messages ); |
||
| 159 | |||
| 160 | View Code Duplication | if ( 0 < count( self::$errors ) ) { |
|
| 161 | foreach ( self::$errors as $code => $message ) { |
||
| 162 | $notice_html .= '<div id="setting-error-' . $code . '" class="' . $classes . ' error"><p><strong>' . $message . '</strong></p></div>'; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | View Code Duplication | if ( 0 < count( self::$messages ) ) { |
|
| 167 | foreach ( self::$messages as $code => $message ) { |
||
| 168 | $notice_html .= '<div id="setting-error-' . $code . '" class="' . $classes . ' updated"><p><strong>' . $message . '</strong></p></div>'; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | echo $notice_html; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Settings page. |
||
| 177 | * |
||
| 178 | * Handles the display of the main give settings page in admin. |
||
| 179 | * |
||
| 180 | * @since 1.8 |
||
| 181 | * @return void|bool |
||
| 182 | */ |
||
| 183 | public static function output() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get a setting from the settings API. |
||
| 231 | * |
||
| 232 | * @since 1.8 |
||
| 233 | * |
||
| 234 | * @param string $option_name |
||
| 235 | * @param string $field_id |
||
| 236 | * @param mixed $default |
||
| 237 | * |
||
| 238 | * @return string|bool |
||
| 239 | */ |
||
| 240 | public static function get_option( $option_name = '', $field_id = '', $default = false ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Output admin fields. |
||
| 266 | * |
||
| 267 | * Loops though the give options array and outputs each field. |
||
| 268 | * |
||
| 269 | * @since 1.8 |
||
| 270 | * |
||
| 271 | * @param array $options Opens array to output |
||
| 272 | * @param string $option_name Opens array to output |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | public static function output_fields( $options, $option_name = '' ) { |
||
| 277 | $current_tab = give_get_current_setting_tab(); |
||
| 278 | |||
| 279 | // Field Default values. |
||
| 280 | $defaults = array( |
||
| 281 | 'id' => '', |
||
| 282 | 'class' => '', |
||
| 283 | 'css' => '', |
||
| 284 | 'default' => '', |
||
| 285 | 'desc' => '', |
||
| 286 | 'table_html' => true, |
||
| 287 | ); |
||
| 288 | |||
| 289 | foreach ( $options as $value ) { |
||
| 290 | if ( ! isset( $value['type'] ) ) { |
||
| 291 | continue; |
||
| 292 | } |
||
| 293 | |||
| 294 | // Set title. |
||
| 295 | $defaults['title'] = isset( $value['name'] ) ? $value['name'] : ''; |
||
| 296 | |||
| 297 | // Set default setting. |
||
| 298 | $value = wp_parse_args( $value, $defaults ); |
||
| 299 | |||
| 300 | // Colorpicker field. |
||
| 301 | $value['class'] = ( 'colorpicker' === $value['type'] ? trim( $value['class'] ) . ' give-colorpicker' : $value['class'] ); |
||
| 302 | $value['type'] = ( 'colorpicker' === $value['type'] ? 'text' : $value['type'] ); |
||
| 303 | |||
| 304 | |||
| 305 | // Custom attribute handling. |
||
| 306 | $custom_attributes = array(); |
||
| 307 | |||
| 308 | View Code Duplication | if ( ! empty( $value['attributes'] ) && is_array( $value['attributes'] ) ) { |
|
| 309 | foreach ( $value['attributes'] as $attribute => $attribute_value ) { |
||
| 310 | $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | // Description handling. |
||
| 315 | $description = self::get_field_description( $value ); |
||
| 316 | |||
| 317 | // Switch based on type. |
||
| 318 | switch ( $value['type'] ) { |
||
| 319 | |||
| 320 | // Section Titles |
||
| 321 | case 'title': |
||
| 322 | if ( ! empty( $value['title'] ) ) { |
||
| 323 | echo '<div class="give-setting-tab-header give-setting-tab-header-' . $current_tab . '"><h2>' . self::get_field_title( $value ) . '</h2><hr></div>'; |
||
| 324 | } |
||
| 325 | |||
| 326 | if ( ! empty( $value['desc'] ) ) { |
||
| 327 | echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) ); |
||
| 328 | } |
||
| 329 | |||
| 330 | if ( $value['table_html'] ) { |
||
| 331 | echo '<table class="form-table give-setting-tab-body give-setting-tab-body-' . $current_tab . '">' . "\n\n"; |
||
| 332 | } |
||
| 333 | |||
| 334 | if ( ! empty( $value['id'] ) ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Trigger Action. |
||
| 338 | * |
||
| 339 | * Note: action dynamically fire on basis of field id. |
||
| 340 | * |
||
| 341 | * @since 1.8 |
||
| 342 | */ |
||
| 343 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) ); |
||
| 344 | } |
||
| 345 | |||
| 346 | break; |
||
| 347 | |||
| 348 | // Section Ends. |
||
| 349 | case 'sectionend': |
||
| 350 | View Code Duplication | if ( ! empty( $value['id'] ) ) { |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Trigger Action. |
||
| 354 | * |
||
| 355 | * Note: action dynamically fire on basis of field id. |
||
| 356 | * |
||
| 357 | * @since 1.8 |
||
| 358 | */ |
||
| 359 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' ); |
||
| 360 | } |
||
| 361 | |||
| 362 | if ( $value['table_html'] ) { |
||
| 363 | echo '</table>'; |
||
| 364 | } |
||
| 365 | |||
| 366 | View Code Duplication | if ( ! empty( $value['id'] ) ) { |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Trigger Action. |
||
| 370 | * |
||
| 371 | * Note: action dynamically fire on basis of field id. |
||
| 372 | * |
||
| 373 | * @since 1.8 |
||
| 374 | */ |
||
| 375 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' ); |
||
| 376 | } |
||
| 377 | |||
| 378 | break; |
||
| 379 | |||
| 380 | // Standard text inputs and subtypes like 'number'. |
||
| 381 | case 'colorpicker': |
||
| 382 | case 'text': |
||
| 383 | case 'email': |
||
| 384 | case 'number': |
||
| 385 | View Code Duplication | case 'password' : |
|
| 386 | |||
| 387 | $type = $value['type']; |
||
| 388 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 389 | |||
| 390 | ?> |
||
| 391 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 392 | <th scope="row" class="titledesc"> |
||
| 393 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 394 | </th> |
||
| 395 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>"> |
||
| 396 | <input |
||
| 397 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 398 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 399 | type="<?php echo esc_attr( $type ); ?>" |
||
| 400 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 401 | value="<?php echo esc_attr( $option_value ); ?>" |
||
| 402 | class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>" |
||
| 403 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 404 | /> <?php echo $description; ?> |
||
| 405 | </td> |
||
| 406 | </tr><?php |
||
| 407 | break; |
||
| 408 | |||
| 409 | // Textarea. |
||
| 410 | case 'textarea': |
||
| 411 | |||
| 412 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 413 | |||
| 414 | ?> |
||
| 415 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 416 | <th scope="row" class="titledesc"> |
||
| 417 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 418 | </th> |
||
| 419 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>"> |
||
| 420 | <textarea |
||
| 421 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 422 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 423 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 424 | class="<?php echo esc_attr( $value['class'] ); ?>" |
||
| 425 | rows="10" |
||
| 426 | cols="60" |
||
| 427 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 428 | ><?php echo esc_textarea( $option_value ); ?></textarea> |
||
| 429 | <?php echo $description; ?> |
||
| 430 | </td> |
||
| 431 | </tr><?php |
||
| 432 | break; |
||
| 433 | |||
| 434 | // Select boxes. |
||
| 435 | case 'select' : |
||
| 436 | case 'multiselect' : |
||
| 437 | |||
| 438 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 439 | |||
| 440 | ?> |
||
| 441 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 442 | <th scope="row" class="titledesc"> |
||
| 443 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 444 | </th> |
||
| 445 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>"> |
||
| 446 | <select |
||
| 447 | name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) { |
||
| 448 | echo '[]'; |
||
| 449 | } ?>" |
||
| 450 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 451 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 452 | class="<?php echo esc_attr( $value['class'] ); ?>" |
||
| 453 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 454 | <?php echo ( 'multiselect' == $value['type'] ) ? 'multiple="multiple"' : ''; ?> |
||
| 455 | > |
||
| 456 | |||
| 457 | <?php |
||
| 458 | if ( ! empty( $value['options'] ) ) { |
||
| 459 | foreach ( $value['options'] as $key => $val ) { |
||
| 460 | ?> |
||
| 461 | <option value="<?php echo esc_attr( $key ); ?>" <?php |
||
| 462 | |||
| 463 | if ( is_array( $option_value ) ) { |
||
| 464 | selected( in_array( $key, $option_value ), true ); |
||
| 465 | } else { |
||
| 466 | selected( $option_value, $key ); |
||
| 467 | } |
||
| 468 | |||
| 469 | ?>><?php echo $val ?></option> |
||
| 470 | <?php |
||
| 471 | } |
||
| 472 | } |
||
| 473 | ?> |
||
| 474 | |||
| 475 | </select> <?php echo $description; ?> |
||
| 476 | </td> |
||
| 477 | </tr><?php |
||
| 478 | break; |
||
| 479 | |||
| 480 | // Radio inputs. |
||
| 481 | case 'radio_inline' : |
||
| 482 | $value['class'] = empty( $value['class'] ) ? 'give-radio-inline' : $value['class'] . ' give-radio-inline'; |
||
| 483 | case 'radio' : |
||
| 484 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 485 | ?> |
||
| 486 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 487 | <th scope="row" class="titledesc"> |
||
| 488 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 489 | </th> |
||
| 490 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>"> |
||
| 491 | <fieldset> |
||
| 492 | <ul> |
||
| 493 | <?php |
||
| 494 | View Code Duplication | foreach ( $value['options'] as $key => $val ) { |
|
| 495 | ?> |
||
| 496 | <li> |
||
| 497 | <label><input |
||
| 498 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 499 | value="<?php echo $key; ?>" |
||
| 500 | type="radio" |
||
| 501 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 502 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 503 | <?php checked( $key, $option_value ); ?> |
||
| 504 | /> <?php echo $val ?></label> |
||
| 505 | </li> |
||
| 506 | <?php |
||
| 507 | } |
||
| 508 | ?> |
||
| 509 | <?php echo $description; ?> |
||
| 510 | </fieldset> |
||
| 511 | </td> |
||
| 512 | </tr><?php |
||
| 513 | break; |
||
| 514 | |||
| 515 | // Checkbox input. |
||
| 516 | case 'checkbox' : |
||
| 517 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 518 | ?> |
||
| 519 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 520 | <th scope="row" class="titledesc"> |
||
| 521 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 522 | </th> |
||
| 523 | <td class="give-forminp"> |
||
| 524 | <input |
||
| 525 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 526 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 527 | type="checkbox" |
||
| 528 | class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>" |
||
| 529 | value="1" |
||
| 530 | <?php checked( $option_value, 'on' ); ?> |
||
| 531 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 532 | /> |
||
| 533 | <?php echo $description; ?> |
||
| 534 | </td> |
||
| 535 | </tr> |
||
| 536 | <?php |
||
| 537 | break; |
||
| 538 | |||
| 539 | // Multi Checkbox input. |
||
| 540 | case 'multicheck' : |
||
| 541 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 542 | $option_value = is_array( $option_value ) ? $option_value : array(); |
||
| 543 | ?> |
||
| 544 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 545 | <th scope="row" class="titledesc"> |
||
| 546 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 547 | </th> |
||
| 548 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>"> |
||
| 549 | <fieldset> |
||
| 550 | <ul> |
||
| 551 | <?php |
||
| 552 | View Code Duplication | foreach ( $value['options'] as $key => $val ) { |
|
| 553 | ?> |
||
| 554 | <li> |
||
| 555 | <label> |
||
| 556 | <input |
||
| 557 | name="<?php echo esc_attr( $value['id'] ); ?>[]" |
||
| 558 | value="<?php echo $key; ?>" |
||
| 559 | type="checkbox" |
||
| 560 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 561 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 562 | <?php if ( in_array( $key, $option_value ) ) { |
||
| 563 | echo 'checked="checked"'; |
||
| 564 | } ?> |
||
| 565 | /> <?php echo $val ?> |
||
| 566 | </label> |
||
| 567 | </li> |
||
| 568 | <?php |
||
| 569 | } |
||
| 570 | ?> |
||
| 571 | <?php echo $description; ?> |
||
| 572 | </fieldset> |
||
| 573 | </td> |
||
| 574 | </tr> |
||
| 575 | <?php |
||
| 576 | break; |
||
| 577 | |||
| 578 | // File input field. |
||
| 579 | case 'file' : |
||
| 580 | case 'media' : |
||
| 581 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 582 | $button_label = esc_html__( sprintf( 'Add or Upload %s', ( 'file' === $value['type'] ? 'File' : 'Image' ) ), 'give' ); |
||
| 583 | $fvalue = empty( $value['fvalue'] ) ? 'url' : $value['fvalue']; |
||
| 584 | |||
| 585 | $allow_media_preview_tags = array( 'jpg', 'jpeg', 'png', 'gif', 'ico' ); |
||
| 586 | $preview_image_src = $option_value ? ( 'id' === $fvalue ? wp_get_attachment_url( $option_value ) : $option_value ) : '#'; |
||
| 587 | $preview_image_extension = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : ''; |
||
| 588 | $is_show_preview = in_array( $preview_image_extension, $allow_media_preview_tags ); |
||
| 589 | ?> |
||
| 590 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 591 | <th scope="row" class="titledesc"> |
||
| 592 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 593 | </th> |
||
| 594 | <td class="give-forminp"> |
||
| 595 | <div class="give-field-wrap"> |
||
| 596 | <label for="<?php echo $value['id'] ?>"> |
||
| 597 | <input |
||
| 598 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 599 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 600 | type="text" |
||
| 601 | class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>" |
||
| 602 | value="<?php echo $option_value; ?>" |
||
| 603 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 604 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 605 | /> <input class="give-upload-button button" type="button" data-fvalue="<?php echo $fvalue; ?>" data-field-type="<?php echo $value['type']; ?>" value="<?php echo $button_label; ?>"> |
||
| 606 | <?php echo $description ?> |
||
| 607 | <div class="give-image-thumb<?php echo ! $option_value || ! $is_show_preview ? ' give-hidden' : ''; ?>"> |
||
| 608 | <span class="give-delete-image-thumb dashicons dashicons-no-alt"></span> |
||
| 609 | <img src="<?php echo $preview_image_src ; ?>" alt=""> |
||
| 610 | </div> |
||
| 611 | </label> |
||
| 612 | </div> |
||
| 613 | </td> |
||
| 614 | </tr> |
||
| 615 | <?php |
||
| 616 | break; |
||
| 617 | |||
| 618 | // WordPress Editor. |
||
| 619 | case 'wysiwyg' : |
||
| 620 | // Get option value. |
||
| 621 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 622 | |||
| 623 | // Get editor settings. |
||
| 624 | $editor_settings = ! empty( $value['options'] ) ? $value['options'] : array(); |
||
| 625 | ?> |
||
| 626 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 627 | <th scope="row" class="titledesc"> |
||
| 628 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 629 | </th> |
||
| 630 | <td class="give-forminp"> |
||
| 631 | <?php wp_editor( $option_value, $value['id'], $editor_settings ); ?> |
||
| 632 | <?php echo $description; ?> |
||
| 633 | </td> |
||
| 634 | </tr><?php |
||
| 635 | break; |
||
| 636 | |||
| 637 | // Custom: System setting field. |
||
| 638 | View Code Duplication | case 'system_info' : |
|
| 639 | ?> |
||
| 640 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 641 | <th scope="row" class="titledesc"> |
||
| 642 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 643 | </th> |
||
| 644 | <td class="give-forminp"> |
||
| 645 | <?php give_system_info_callback(); ?> |
||
| 646 | <?php echo $description; ?> |
||
| 647 | </td> |
||
| 648 | </tr><?php |
||
| 649 | break; |
||
| 650 | |||
| 651 | // Custom: Default gateways setting field. |
||
| 652 | View Code Duplication | case 'default_gateway' : |
|
| 653 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 654 | ?> |
||
| 655 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 656 | <th scope="row" class="titledesc"> |
||
| 657 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 658 | </th> |
||
| 659 | <td class="give-forminp"> |
||
| 660 | <?php give_default_gateway_callback( $value, $option_value ); ?> |
||
| 661 | <?php echo $description; ?> |
||
| 662 | </td> |
||
| 663 | </tr><?php |
||
| 664 | break; |
||
| 665 | |||
| 666 | // Custom: Enable gateways setting field. |
||
| 667 | View Code Duplication | case 'enabled_gateways' : |
|
| 668 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 669 | ?> |
||
| 670 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 671 | <th scope="row" class="titledesc"> |
||
| 672 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 673 | </th> |
||
| 674 | <td class="give-forminp"> |
||
| 675 | <?php give_enabled_gateways_callback( $value, $option_value ); ?> |
||
| 676 | <?php echo $description; ?> |
||
| 677 | </td> |
||
| 678 | </tr><?php |
||
| 679 | break; |
||
| 680 | |||
| 681 | // Custom: Email preview buttons field. |
||
| 682 | View Code Duplication | case 'email_preview_buttons' : |
|
| 683 | ?> |
||
| 684 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 685 | <th scope="row" class="titledesc"> |
||
| 686 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 687 | </th> |
||
| 688 | <td class="give-forminp"> |
||
| 689 | <?php give_email_preview_buttons_callback(); ?> |
||
| 690 | <?php echo $description; ?> |
||
| 691 | </td> |
||
| 692 | </tr><?php |
||
| 693 | break; |
||
| 694 | |||
| 695 | // Custom: API field. |
||
| 696 | case 'api' : |
||
| 697 | give_api_callback(); |
||
| 698 | echo $description; |
||
| 699 | break; |
||
| 700 | |||
| 701 | // Custom: Gateway API key. |
||
| 702 | View Code Duplication | case 'api_key' : |
|
| 703 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 704 | $type = ! empty( $option_value ) ? 'password' : 'text'; |
||
| 705 | ?> |
||
| 706 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 707 | <th scope="row" class="titledesc"> |
||
| 708 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 709 | </th> |
||
| 710 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>"> |
||
| 711 | <input |
||
| 712 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 713 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 714 | type="<?php echo esc_attr( $type ); ?>" |
||
| 715 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 716 | value="<?php echo esc_attr( trim( $option_value ) ); ?>" |
||
| 717 | class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>" |
||
| 718 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 719 | /> <?php echo $description; ?> |
||
| 720 | </td> |
||
| 721 | </tr><?php |
||
| 722 | break; |
||
| 723 | |||
| 724 | // Custom: Log field. |
||
| 725 | case 'logs' : |
||
| 726 | |||
| 727 | // Get current section. |
||
| 728 | $current_section = $_GET['section'] = give_get_current_setting_section(); |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Fires for each tab of logs view. |
||
| 732 | * |
||
| 733 | * @since 1.0 |
||
| 734 | */ |
||
| 735 | do_action( "give_logs_view_{$current_section}" ); |
||
| 736 | |||
| 737 | echo $description; |
||
| 738 | break; |
||
| 739 | |||
| 740 | // Custom: Data field. |
||
| 741 | case 'data' : |
||
| 742 | |||
| 743 | include GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-data.php'; |
||
| 744 | |||
| 745 | echo $description; |
||
| 746 | break; |
||
| 747 | |||
| 748 | // Custom: Give Docs Link field type. |
||
| 749 | case 'give_docs_link' : |
||
| 750 | ?> |
||
| 751 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 752 | <td class="give-docs-link" colspan="2"> |
||
| 753 | <?php |
||
| 754 | echo '<p class="give-docs-link"><a href="' . esc_url( $value['url'] ) |
||
| 755 | . '" target="_blank">' |
||
| 756 | . sprintf( esc_html__( 'Need Help? See docs on "%s"', 'give' ), $value['title'] ) |
||
| 757 | . '<span class="dashicons dashicons-editor-help"></span></a></p>'; |
||
| 758 | ?> |
||
| 759 | </td> |
||
| 760 | </tr><?php |
||
| 761 | break; |
||
| 762 | |||
| 763 | // Default: run an action |
||
| 764 | // You can add or handle your custom field action. |
||
| 765 | default: |
||
| 766 | // Get option value. |
||
| 767 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 768 | do_action( 'give_admin_field_' . $value['type'], $value, $option_value ); |
||
| 769 | break; |
||
| 770 | } |
||
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Helper function to get the formatted description for a given form field. |
||
| 776 | * Plugins can call this when implementing their own custom settings types. |
||
| 777 | * |
||
| 778 | * @since 1.8 |
||
| 779 | * |
||
| 780 | * @param array $value The form field value array |
||
| 781 | * |
||
| 782 | * @return string The HTML description of the field. |
||
| 783 | */ |
||
| 784 | public static function get_field_description( $value ) { |
||
| 797 | |||
| 798 | |||
| 799 | /** |
||
| 800 | * Helper function to get the formated title. |
||
| 801 | * Plugins can call this when implementing their own custom settings types. |
||
| 802 | * |
||
| 803 | * @since 1.8 |
||
| 804 | * |
||
| 805 | * @param array $value The form field value array |
||
| 806 | * |
||
| 807 | * @return array The description and tip as a 2 element array |
||
| 808 | */ |
||
| 809 | public static function get_field_title( $value ) { |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Save admin fields. |
||
| 822 | * |
||
| 823 | * Loops though the give options array and outputs each field. |
||
| 824 | * |
||
| 825 | * @since 1.8 |
||
| 826 | * |
||
| 827 | * @param array $options Options array to output |
||
| 828 | * @param string $option_name Option name to save output. If empty then option will be store in there own option name i.e option id. |
||
| 829 | * |
||
| 830 | * @return bool |
||
| 831 | */ |
||
| 832 | public static function save_fields( $options, $option_name = '' ) { |
||
| 939 | } |
||
| 940 | |||
| 941 | endif; |
||
| 942 |