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-link-fields-base.php
344 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_Link_Fields_Base |
| 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 | abstract class WPEL_Link_Fields_Base extends FWP_Settings_Section_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * Get general fields |
| 16 | * @return array |
| 17 | */ |
| 18 | final protected function get_general_fields() |
| 19 | { |
| 20 | return array( |
| 21 | 'apply_settings' => array( |
| 22 | 'label' => __( 'Settings for links:', 'wp-external-links' ), |
| 23 | 'class' => 'js-apply-settings', |
| 24 | ), |
| 25 | 'target' => array( |
| 26 | 'label' => __( 'Open links:', 'wp-external-links' ), |
| 27 | 'class' => 'wpel-hidden', |
| 28 | 'default_value' => '', |
| 29 | ), |
| 30 | 'target_overwrite' => array( |
| 31 | 'label' => '', |
| 32 | 'class' => 'wpel-no-label wpel-hidden', |
| 33 | ), |
| 34 | 'rel_follow' => array( |
| 35 | 'label' => __( 'Set <code>follow</code> or <code>nofollow</code>:', 'wp-external-links' ), |
| 36 | 'class' => 'wpel-hidden', |
| 37 | 'default_value' => '', |
| 38 | ), |
| 39 | 'rel_follow_overwrite' => array( |
| 40 | 'label' => '', |
| 41 | 'class' => 'wpel-no-label wpel-hidden', |
| 42 | ), |
| 43 | 'rel_noopener' => array( |
| 44 | 'label' => __( 'Also add to <code>rel</code> attribute:', 'wp-external-links' ), |
| 45 | 'class' => 'wpel-hidden', |
| 46 | 'default_value' => '1', |
| 47 | ), |
| 48 | 'rel_noreferrer' => array( |
| 49 | 'label' => '', |
| 50 | 'class' => 'wpel-no-label wpel-hidden', |
| 51 | 'default_value' => '1', |
| 52 | ), |
| 53 | 'title' => array( |
| 54 | 'label' => __( 'Set <code>title</code>:', 'wp-external-links' ), |
| 55 | 'class' => 'wpel-hidden', |
| 56 | 'default_value' => '{title}', |
| 57 | ), |
| 58 | 'class' => array( |
| 59 | 'label' => __( 'Add CSS class(es):', 'wp-external-links' ), |
| 60 | 'class' => 'wpel-hidden', |
| 61 | ), |
| 62 | 'icon_type' => array( |
| 63 | 'label' => __( 'Choose icon type:', 'wp-external-links' ), |
| 64 | 'class' => 'js-icon-type wpel-hidden', |
| 65 | ), |
| 66 | 'icon_image' => array( |
| 67 | 'label' => __( 'Choose icon image:', 'wp-external-links' ), |
| 68 | 'class' => 'js-icon-type-child js-icon-type-image wpel-hidden', |
| 69 | 'default_value' => '1', |
| 70 | ), |
| 71 | 'icon_dashicon' => array( |
| 72 | 'label' => __( 'Choose dashicon:', 'wp-external-links' ), |
| 73 | 'class' => 'js-icon-type-child js-icon-type-dashicon wpel-hidden', |
| 74 | ), |
| 75 | 'icon_fontawesome' => array( |
| 76 | 'label' => __( 'Choose FA icon:', 'wp-external-links' ), |
| 77 | 'class' => 'js-icon-type-child js-icon-type-fontawesome wpel-hidden', |
| 78 | ), |
| 79 | 'icon_position' => array( |
| 80 | 'label' => __( 'Icon position:', 'wp-external-links' ), |
| 81 | 'class' => 'js-icon-type-depend wpel-hidden', |
| 82 | 'default_value' => 'right', |
| 83 | ), |
| 84 | 'no_icon_for_img' => array( |
| 85 | 'label' => __( 'Skip icon with <code><img></code>:', 'wp-external-links' ), |
| 86 | 'class' => 'js-icon-type-depend wpel-hidden', |
| 87 | 'default_value' => '1', |
| 88 | ), |
| 89 | ); |
| 90 | |
| 91 | parent::init(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Show field methods |
| 96 | */ |
| 97 | |
| 98 | protected function show_apply_settings( array $args ) |
| 99 | { |
| 100 | $this->get_html_fields()->check_with_label( |
| 101 | $args[ 'key' ] |
| 102 | , __( 'Apply these settings', 'wp-external-links' ) |
| 103 | , '1' |
| 104 | , '' |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | protected function show_target( array $args ) |
| 109 | { |
| 110 | $this->get_html_fields()->select( |
| 111 | $args[ 'key' ] |
| 112 | , array( |
| 113 | '' => __( '- keep as is -', 'wp-external-links' ), |
| 114 | '_self' => __( 'in the same window, tab or frame', 'wp-external-links' ), |
| 115 | '_blank' => __( 'each in a separate new window or tab', 'wp-external-links' ), |
| 116 | '_new' => __( 'all in the same new window or tab (NOT recommended)', 'wp-external-links' ), |
| 117 | '_top' => __( 'in the topmost frame (NOT recommended)', 'wp-external-links' ), |
| 118 | ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | protected function show_target_overwrite( array $args ) |
| 123 | { |
| 124 | $this->get_html_fields()->check_with_label( |
| 125 | $args[ 'key' ] |
| 126 | , __( 'Overwrite existing values.', 'wp-external-links' ) |
| 127 | , '1' |
| 128 | , '' |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | protected function show_rel_follow( array $args ) |
| 133 | { |
| 134 | $this->get_html_fields()->select( |
| 135 | $args[ 'key' ] |
| 136 | , array( |
| 137 | '' => __( '- keep as is -', 'wp-external-links' ), |
| 138 | 'follow' => __( 'follow', 'wp-external-links' ), |
| 139 | 'nofollow' => __( 'nofollow', 'wp-external-links' ), |
| 140 | ) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | protected function show_rel_follow_overwrite( array $args ) |
| 145 | { |
| 146 | $this->get_html_fields()->check_with_label( |
| 147 | $args[ 'key' ] |
| 148 | , __( 'Overwrite existing values.', 'wp-external-links' ) |
| 149 | , '1' |
| 150 | , '' |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | protected function show_rel_noopener( array $args ) |
| 155 | { |
| 156 | $this->get_html_fields()->check_with_label( |
| 157 | $args[ 'key' ] |
| 158 | , __( 'Add <code>"noopener"</code>', 'wp-external-links' ) |
| 159 | , '1' |
| 160 | , '' |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | protected function show_rel_noreferrer( array $args ) |
| 165 | { |
| 166 | $this->get_html_fields()->check_with_label( |
| 167 | $args[ 'key' ] |
| 168 | , __( 'Add <code>"noreferrer"</code>', 'wp-external-links' ) |
| 169 | , '1' |
| 170 | , '' |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | protected function show_rel_sponsored( array $args ) |
| 175 | { |
| 176 | $this->get_html_fields()->check_with_label( |
| 177 | $args[ 'key' ] |
| 178 | , __( 'Add <code>"sponsored"</code>', 'wp-external-links' ) |
| 179 | , '1' |
| 180 | , '' |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | protected function show_rel_ugc( array $args ) |
| 185 | { |
| 186 | $this->get_html_fields()->check_with_label( |
| 187 | $args[ 'key' ] |
| 188 | , __( 'Add <code>"ugc"</code>', 'wp-external-links' ) |
| 189 | , '1' |
| 190 | , '' |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | protected function show_title( array $args ) |
| 195 | { |
| 196 | $this->get_html_fields()->text( $args[ 'key' ], array( |
| 197 | 'class' => 'regular-text', |
| 198 | ) ); |
| 199 | |
| 200 | echo '<p class="description">' |
| 201 | . esc_html__( 'Use this <code>{title}</code> for the original title value and <code>{text}</code> for the link text (or <code>{text_clean}</code> for text stripped of HTML) as shown on the page', 'wp-external-links' ) |
| 202 | .'</p>'; |
| 203 | } |
| 204 | |
| 205 | protected function show_class( array $args ) |
| 206 | { |
| 207 | $this->get_html_fields()->text( $args[ 'key' ], array( |
| 208 | 'class' => 'regular-text', |
| 209 | ) ); |
| 210 | } |
| 211 | |
| 212 | protected function show_icon_type( array $args ) |
| 213 | { |
| 214 | $this->get_html_fields()->select( |
| 215 | $args[ 'key' ] |
| 216 | , array( |
| 217 | '' => __( '- no icon -', 'wp-external-links' ), |
| 218 | 'image' => __( 'Image', 'wp-external-links' ), |
| 219 | 'dashicon' => __( 'Dashicon', 'wp-external-links' ), |
| 220 | 'fontawesome' => __( 'Font Awesome', 'wp-external-links' ), |
| 221 | ) |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | protected function show_icon_image( array $args ) |
| 226 | { |
| 227 | echo '<fieldset>'; |
| 228 | echo '<div class="wpel-icon-type-image-column">'; |
| 229 | |
| 230 | for ( $x = 1; $x <= 20; $x++ ) { |
| 231 | echo '<label>'; |
| 232 | WPEL_Plugin::wp_kses_wf($this->get_html_fields()->radio( $args[ 'key' ], strval( $x ) )); |
| 233 | echo '<img src="'. esc_url(plugins_url( '/public/images/wpel-icons/icon-'. esc_attr( $x ) .'.png', WPEL_Plugin::get_plugin_file() )) .'">'; |
| 234 | echo '</label>'; |
| 235 | echo '<br>'; |
| 236 | |
| 237 | if ( $x % 5 === 0 ) { |
| 238 | echo '</div>'; |
| 239 | echo '<div class="wpel-icon-type-image-column">'; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | echo '</div>'; |
| 244 | echo '</fieldset>'; |
| 245 | } |
| 246 | |
| 247 | protected function show_icon_dashicon( array $args ) |
| 248 | { |
| 249 | global $wp_filesystem; |
| 250 | WPEL_Plugin::wp_init_filesystem(); |
| 251 | |
| 252 | $dashicons_str = $wp_filesystem->get_contents( WPEL_Plugin::get_plugin_dir( '/data/json/dashicons.json' ) ); |
| 253 | $dashicons_json = json_decode( $dashicons_str, true ); |
| 254 | $dashicons = $dashicons_json[ 'icons' ]; |
| 255 | |
| 256 | $options = array(); |
| 257 | foreach ( $dashicons as $icon ) { |
| 258 | $options[ $icon[ 'className' ] ] = '&#x'. $icon[ 'unicode' ] . ';'; |
| 259 | } |
| 260 | |
| 261 | $this->get_html_fields()->select( $args[ 'key' ], $options, array( |
| 262 | 'style' => 'font-family:dashicons', |
| 263 | ) ); |
| 264 | } |
| 265 | |
| 266 | protected function show_icon_fontawesome( array $args ) |
| 267 | { |
| 268 | global $wp_filesystem; |
| 269 | WPEL_Plugin::wp_init_filesystem(); |
| 270 | |
| 271 | $fa_icons_str = $wp_filesystem->get_contents( WPEL_Plugin::get_plugin_dir( '/data/json/fontawesome.json' ) ); |
| 272 | $fa_icons_json = json_decode( $fa_icons_str, true ); |
| 273 | $fa_icons = $fa_icons_json[ 'icons' ]; |
| 274 | |
| 275 | $options = array(); |
| 276 | foreach ( $fa_icons as $icon ) { |
| 277 | $options[ $icon[ 'className' ] ] = '&#x'. $icon[ 'unicode' ] . ';'; |
| 278 | } |
| 279 | |
| 280 | $this->get_html_fields()->select( $args[ 'key' ], $options, array( |
| 281 | 'style' => 'font-family:FontAwesome', |
| 282 | ) ); |
| 283 | } |
| 284 | |
| 285 | protected function show_icon_position( array $args ) |
| 286 | { |
| 287 | $this->get_html_fields()->select( |
| 288 | $args[ 'key' ] |
| 289 | , array( |
| 290 | 'left' => __( 'Left side of the link', 'wp-external-links' ), |
| 291 | 'right' => __( 'Right side of the link', 'wp-external-links' ), |
| 292 | ) |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | protected function show_no_icon_for_img( array $args ) |
| 297 | { |
| 298 | $this->get_html_fields()->check_with_label( |
| 299 | $args[ 'key' ] |
| 300 | , __( 'No icon for links already containing an <code><img></code>-tag.', 'wp-external-links' ) |
| 301 | , '1' |
| 302 | , '' |
| 303 | ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Validate and sanitize user input before saving to databse |
| 308 | * @param array $new_values |
| 309 | * @param array $old_values |
| 310 | * @return array |
| 311 | */ |
| 312 | protected function before_update( array $new_values, array $old_values ) |
| 313 | { |
| 314 | $update_values = $new_values; |
| 315 | $is_valid = true; |
| 316 | |
| 317 | $is_valid = $is_valid && in_array( $new_values[ 'apply_settings' ], array( '', '1' ) ); |
| 318 | $is_valid = $is_valid && in_array( $new_values[ 'target_overwrite' ], array( '', '1' ) ); |
| 319 | $is_valid = $is_valid && in_array( $new_values[ 'rel_follow_overwrite' ], array( '', '1' ) ); |
| 320 | $is_valid = $is_valid && in_array( $new_values[ 'rel_noopener' ], array( '', '1' ) ); |
| 321 | $is_valid = $is_valid && in_array( $new_values[ 'rel_noreferrer' ], array( '', '1' ) ); |
| 322 | $is_valid = $is_valid && in_array( $new_values[ 'no_icon_for_img' ], array( '', '1' ) ); |
| 323 | |
| 324 | if ( false === $is_valid ) { |
| 325 | // error when user input is not valid conform the UI, probably tried to "hack" |
| 326 | $this->add_error( __( 'Something went wrong. One or more values were invalid.', 'wp-external-links' ) ); |
| 327 | return $old_values; |
| 328 | } |
| 329 | |
| 330 | $update_values[ 'target' ] = sanitize_text_field( $new_values[ 'target' ] ); |
| 331 | $update_values[ 'rel_follow' ] = sanitize_text_field( $new_values[ 'rel_follow' ] ); |
| 332 | $update_values[ 'title' ] = sanitize_text_field( $new_values[ 'title' ] ); |
| 333 | $update_values[ 'class' ] = sanitize_text_field( $new_values[ 'class' ] ); |
| 334 | $update_values[ 'icon_type' ] = sanitize_text_field( $new_values[ 'icon_type' ] ); |
| 335 | $update_values[ 'icon_image' ] = sanitize_text_field( $new_values[ 'icon_image' ] ); |
| 336 | $update_values[ 'icon_dashicon' ] = sanitize_text_field( $new_values[ 'icon_dashicon' ] ); |
| 337 | $update_values[ 'icon_fontawesome' ] = sanitize_text_field( $new_values[ 'icon_fontawesome' ] ); |
| 338 | $update_values[ 'icon_position' ] = sanitize_text_field( $new_values[ 'icon_position' ] ); |
| 339 | |
| 340 | return $update_values; |
| 341 | } |
| 342 | |
| 343 | } |
| 344 |