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-exceptions-fields.php
253 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_Exceptions_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_Exceptions_Fields extends FWP_Settings_Section_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * Initialize |
| 16 | */ |
| 17 | protected function init() |
| 18 | { |
| 19 | $this->set_settings( array( |
| 20 | 'section_id' => 'wpel-exceptions-fields', |
| 21 | 'page_id' => 'wpel-exceptions-fields', |
| 22 | 'option_name' => 'wpel-exceptions-settings', |
| 23 | 'option_group' => 'wpel-exceptions-settings', |
| 24 | 'title' => __( 'Exceptions', 'wp-external-links' ), |
| 25 | 'fields' => array( |
| 26 | 'apply_all' => array( |
| 27 | 'label' => __( 'Apply settings on:', 'wp-external-links' ), |
| 28 | 'class' => 'js-wpel-apply', |
| 29 | 'default_value' => '1', |
| 30 | ), |
| 31 | 'apply_post_content' => array( |
| 32 | 'class' => 'js-wpel-apply-child wpel-hidden wpel-no-label ', |
| 33 | 'default_value' => '1', |
| 34 | ), |
| 35 | 'apply_comments' => array( |
| 36 | 'class' => 'js-wpel-apply-child wpel-hidden wpel-no-label', |
| 37 | 'default_value' => '1', |
| 38 | ), |
| 39 | 'apply_widgets' => array( |
| 40 | 'class' => 'js-wpel-apply-child wpel-hidden wpel-no-label', |
| 41 | 'default_value' => '1', |
| 42 | ), |
| 43 | 'skip_post_ids' => array( |
| 44 | 'label' => __( 'Skip pages or posts (id\'s):', 'wp-external-links' ), |
| 45 | ), |
| 46 | 'ignore_classes' => array( |
| 47 | 'label' => __( 'Ignore links by class:', 'wp-external-links' ), |
| 48 | ), |
| 49 | 'subdomains_as_internal_links' => array( |
| 50 | 'label' => __( 'Make subdomains internal:', 'wp-external-links' ), |
| 51 | ), |
| 52 | 'include_urls' => array( |
| 53 | 'label' => __( 'Include external links by URL:', 'wp-external-links' ), |
| 54 | ), |
| 55 | 'exclude_urls' => array( |
| 56 | 'label' => __( 'Exclude external links by URL:', 'wp-external-links' ), |
| 57 | ), |
| 58 | 'excludes_as_internal_links' => array( |
| 59 | 'label' => __( 'Own settings for excluded links:', 'wp-external-links' ), |
| 60 | ), |
| 61 | 'ignore_script_tags' => array( |
| 62 | 'label' => __( 'Skip <code><script></code>:', 'wp-external-links' ), |
| 63 | 'default_value' => '1', |
| 64 | ), |
| 65 | 'ignore_mailto_links' => array( |
| 66 | 'label' => __( 'Skip <code>mailto</code> links:', 'wp-external-links' ), |
| 67 | 'default_value' => '1', |
| 68 | ), |
| 69 | ), |
| 70 | ) ); |
| 71 | |
| 72 | parent::init(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Show field methods |
| 77 | */ |
| 78 | |
| 79 | protected function show_apply_all( array $args ) |
| 80 | { |
| 81 | $this->get_html_fields()->check_with_label( |
| 82 | $args[ 'key' ] |
| 83 | , __( 'All contents (the whole page)', 'wp-external-links' ) |
| 84 | , '1' |
| 85 | , '' |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | protected function show_apply_post_content( array $args ) |
| 90 | { |
| 91 | $this->get_html_fields()->check_with_label( |
| 92 | $args[ 'key' ] |
| 93 | , __( 'Post content', 'wp-external-links' ) |
| 94 | , '1' |
| 95 | , '' |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | protected function show_apply_comments( array $args ) |
| 100 | { |
| 101 | $this->get_html_fields()->check_with_label( |
| 102 | $args[ 'key' ] |
| 103 | , __( 'Comments', 'wp-external-links' ) |
| 104 | , '1' |
| 105 | , '' |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | protected function show_apply_widgets( array $args ) |
| 110 | { |
| 111 | $this->get_html_fields()->check_with_label( |
| 112 | $args[ 'key' ] |
| 113 | , __( 'All widgets', 'wp-external-links' ) |
| 114 | , '1' |
| 115 | , '' |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | protected function show_skip_post_ids( array $args ) |
| 120 | { |
| 121 | $this->get_html_fields()->text( $args[ 'key' ], array( |
| 122 | 'class' => 'regular-text', |
| 123 | ) ); |
| 124 | |
| 125 | echo '<p class="description">' |
| 126 | . esc_html__( 'Separate page- / post-id\'s by comma.', 'wp-external-links' ) |
| 127 | .'</p>'; |
| 128 | } |
| 129 | |
| 130 | protected function show_ignore_classes( array $args ) |
| 131 | { |
| 132 | $this->get_html_fields()->text( $args[ 'key' ], array( |
| 133 | 'class' => 'regular-text', |
| 134 | ) ); |
| 135 | |
| 136 | echo '<p class="description">' |
| 137 | . esc_html__( 'Separate classes by comma.', 'wp-external-links' ) |
| 138 | .'</p>'; |
| 139 | } |
| 140 | |
| 141 | protected function show_subdomains_as_internal_links( array $args ) |
| 142 | { |
| 143 | $this->get_html_fields()->check_with_label( |
| 144 | $args[ 'key' ] |
| 145 | , esc_html__( 'Treat all links to the site\'s domain and subdomains as internal links', 'wp-external-links' ) |
| 146 | , '1' |
| 147 | , '' |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | protected function show_include_urls( array $args ) |
| 152 | { |
| 153 | $this->show_urls_field( $args[ 'key' ] ); |
| 154 | } |
| 155 | |
| 156 | protected function show_exclude_urls( array $args ) |
| 157 | { |
| 158 | $this->show_urls_field( $args[ 'key' ] ); |
| 159 | } |
| 160 | |
| 161 | protected function show_urls_field( $key ) |
| 162 | { |
| 163 | $this->get_html_fields()->text_area( $key, array( |
| 164 | 'class' => 'large-text', |
| 165 | 'rows' => 4, |
| 166 | 'placeholder' => __( "For example: \n somedomain.org, sub.domain.net/some-slug \n http://sub.moredomain.net, http://www.domain.com/other-slug", 'wp-external-links' ), |
| 167 | ) ); |
| 168 | |
| 169 | echo '<p class="description">' |
| 170 | . esc_html__( "Separate url's by comma and/or a line break. \n Write the url\'s as specific as you want them to match.", 'wp-external-links' ) |
| 171 | .'</p>'; |
| 172 | } |
| 173 | |
| 174 | protected function show_excludes_as_internal_links( array $args ) |
| 175 | { |
| 176 | echo '<fieldset>'; |
| 177 | |
| 178 | $this->get_html_fields()->radio_with_label( |
| 179 | $args[ 'key' ] |
| 180 | , __( 'Treat excluded links as internal links', 'wp-external-links' ) |
| 181 | , '1' |
| 182 | ); |
| 183 | |
| 184 | echo '<br>'; |
| 185 | |
| 186 | $this->get_html_fields()->radio_with_label( |
| 187 | $args[ 'key' ] |
| 188 | , __( 'Own settings for excluded links <span class="description">(extra tab)</span>', 'wp-external-links' ) |
| 189 | , '' |
| 190 | ); |
| 191 | |
| 192 | echo '</fieldset>'; |
| 193 | } |
| 194 | |
| 195 | protected function show_ignore_script_tags( array $args ) |
| 196 | { |
| 197 | $this->get_html_fields()->check_with_label( |
| 198 | $args[ 'key' ] |
| 199 | , __( 'Ignore all links in <code><script></code> blocks', 'wp-external-links' ) |
| 200 | , '1' |
| 201 | , '' |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | protected function show_ignore_mailto_links( array $args ) |
| 206 | { |
| 207 | $this->get_html_fields()->check_with_label( |
| 208 | $args[ 'key' ] |
| 209 | , __( 'Ignore all <code>mailto</code> links', 'wp-external-links' ) |
| 210 | , '1' |
| 211 | , '' |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Validate and sanitize user input before saving to databse |
| 217 | * @param array $new_values |
| 218 | * @param array $old_values |
| 219 | * @return array |
| 220 | */ |
| 221 | protected function before_update( array $new_values, array $old_values ) |
| 222 | { |
| 223 | $update_values = $new_values; |
| 224 | $is_valid = true; |
| 225 | |
| 226 | $is_valid = $is_valid && in_array( $new_values[ 'apply_post_content' ], array( '', '1' ) ); |
| 227 | $is_valid = $is_valid && in_array( $new_values[ 'apply_comments' ], array( '', '1' ) ); |
| 228 | $is_valid = $is_valid && in_array( $new_values[ 'apply_widgets' ], array( '', '1' ) ); |
| 229 | $is_valid = $is_valid && in_array( $new_values[ 'apply_all' ], array( '', '1' ) ); |
| 230 | $is_valid = $is_valid && in_array( $new_values[ 'subdomains_as_internal_links' ], array( '', '1' ) ); |
| 231 | $is_valid = $is_valid && in_array( $new_values[ 'excludes_as_internal_links' ], array( '', '1' ) ); |
| 232 | $is_valid = $is_valid && in_array( $new_values[ 'ignore_script_tags' ], array( '', '1' ) ); |
| 233 | $is_valid = $is_valid && in_array( $new_values[ 'ignore_mailto_links' ], array( '', '1' ) ); |
| 234 | |
| 235 | if ( false === $is_valid ) { |
| 236 | // error when user input is not valid conform the UI, probably tried to "hack" |
| 237 | $this->add_error( __( 'Something went wrong. One or more values were invalid.', 'wp-external-links' ) ); |
| 238 | return $old_values; |
| 239 | } |
| 240 | |
| 241 | if ( '' !== trim( $new_values[ 'include_urls' ] ) ) { |
| 242 | $update_values[ 'include_urls' ] = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $new_values[ 'include_urls' ] ) ) ); |
| 243 | } |
| 244 | |
| 245 | if ( '' !== trim( $new_values[ 'exclude_urls' ] ) ) { |
| 246 | $update_values[ 'exclude_urls' ] = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $new_values[ 'exclude_urls' ] ) ) ); |
| 247 | } |
| 248 | |
| 249 | return $update_values; |
| 250 | } |
| 251 | |
| 252 | } |
| 253 |