class-wpel-admin-fields.php
1 year ago
class-wpel-exceptions-fields.php
1 year ago
class-wpel-excluded-link-fields.php
3 years ago
class-wpel-exit-confirmation-fields.php
1 year ago
class-wpel-external-link-fields.php
3 years ago
class-wpel-internal-link-fields.php
3 years ago
class-wpel-link-fields-base.php
1 year ago
class-wpel-external-link-fields.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_External_Link_Fields |
| 4 | * |
| 5 | * @package WPEL |
| 6 | * @category WordPress Plugin |
| 7 | * @version 2.3 |
| 8 | * @link https://www.webfactoryltd.com/ |
| 9 | * @license Dual licensed under the MIT and GPLv2+ licenses |
| 10 | */ |
| 11 | final class WPEL_External_Link_Fields extends WPEL_Link_Fields_Base |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * Initialize |
| 16 | */ |
| 17 | protected function init() |
| 18 | { |
| 19 | $option_name = 'wpel-external-link-settings'; |
| 20 | $fields = $this->get_general_fields( $option_name ); |
| 21 | |
| 22 | // specific field settings |
| 23 | $fields[ 'apply_settings' ][ 'label' ] = __( 'Settings for external links:', 'wp-external-links' ); |
| 24 | $fields[ 'apply_settings' ][ 'default_value' ] = '1'; |
| 25 | $fields[ 'target' ][ 'label' ] = __( 'Open external links:', 'wp-external-links' ); |
| 26 | |
| 27 | // |
| 28 | $index_prev = array_search( 'rel_noreferrer', array_keys( $fields ) ); |
| 29 | $index_insert = $index_prev + 1; |
| 30 | |
| 31 | $additional_fields = array( |
| 32 | 'rel_external' => array( |
| 33 | 'label' => '', |
| 34 | 'class' => 'wpel-no-label wpel-hidden', |
| 35 | 'default_value' => '1', |
| 36 | ), |
| 37 | 'rel_sponsored' => array( |
| 38 | 'label' => '', |
| 39 | 'class' => 'wpel-no-label wpel-hidden', |
| 40 | 'default_value' => '0', |
| 41 | ), |
| 42 | 'rel_ugc' => array( |
| 43 | 'label' => '', |
| 44 | 'class' => 'wpel-no-label wpel-hidden', |
| 45 | 'default_value' => '0', |
| 46 | ) |
| 47 | ); |
| 48 | |
| 49 | $fields = array_merge( |
| 50 | array_slice( $fields, 0, $index_insert ) |
| 51 | , $additional_fields |
| 52 | , array_slice( $fields, $index_insert ) |
| 53 | ); |
| 54 | |
| 55 | $this->set_settings( array( |
| 56 | 'section_id' => 'wpel-external-link-fields', |
| 57 | 'page_id' => 'wpel-external-link-fields', |
| 58 | 'option_name' => $option_name, |
| 59 | 'option_group' => $option_name, |
| 60 | 'title' => __( 'External Links', 'wp-external-links' ), |
| 61 | 'fields' => $fields, |
| 62 | ) ); |
| 63 | |
| 64 | parent::init(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Show field methods |
| 69 | */ |
| 70 | |
| 71 | protected function show_rel_external( array $args ) |
| 72 | { |
| 73 | $this->get_html_fields()->check_with_label( |
| 74 | $args[ 'key' ] |
| 75 | , __( 'Add <code>"external"</code>', 'wp-external-links' ) |
| 76 | , '1' |
| 77 | , '' |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Validate and sanitize user input before saving to database |
| 83 | * @param array $new_values |
| 84 | * @param array $old_values |
| 85 | * @return array |
| 86 | */ |
| 87 | protected function before_update( array $new_values, array $old_values ) |
| 88 | { |
| 89 | $is_valid = true; |
| 90 | |
| 91 | $is_valid = $is_valid && in_array( $new_values[ 'rel_external' ], array( '', '1' ) ); |
| 92 | |
| 93 | if ( false === $is_valid ) { |
| 94 | // error when user input is not valid conform the UI, probably tried to "hack" |
| 95 | $this->add_error( __( 'Something went wrong. One or more values were invalid.', 'wp-external-links' ) ); |
| 96 | return $old_values; |
| 97 | } |
| 98 | |
| 99 | $update_values = parent::before_update( $new_values, $old_values ); |
| 100 | return $update_values; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 |