| @@ -2,10 +2,16 @@ | ||
| 2 | 2 | |
| 3 | 3 | defined( 'ABSPATH' ) || exit; |
| 4 | 4 | |
| 5 | 5 | class WPCoder_Lite_Enabled_Snippets { |
| 6 | + /** | |
| 7 | + * @var false|mixed|null | |
| 8 | + */ | |
| 9 | + private $options; | |
| 10 | + | |
| 6 | 11 | public function __construct() { |
| 7 | - $options = get_option( '_wp_coder_snippets', [] ); | |
| 12 | + $options = get_option( '_wp_coder_snippets', [] ); | |
| 13 | + $this->options = $options; | |
| 8 | 14 | |
| 9 | 15 | if ( array_key_exists( 'enable_duplication', $options ) ) { |
| 10 | 16 | add_filter( 'post_row_actions', [ $this, 'add_duplicate_link' ], 10, 2 ); |
| 11 | 17 | add_filter( 'page_row_actions', [ $this, 'add_duplicate_link' ], 10, 2 ); |
| @@ -44,12 +50,98 @@ | ||
| 44 | 50 | if ( array_key_exists( 'enable_default_alt_to_avatar', $options ) ) { |
| 45 | 51 | add_filter( 'pre_get_avatar_data', [ $this, 'default_alt_to_avatar' ] ); |
| 46 | 52 | } |
| 47 | 53 | |
| 54 | + if ( array_key_exists( 'enable_force_external_links_new_tab', $options ) ) { | |
| 55 | + add_filter( 'the_content', [ $this, 'force_external_links_new_tab' ] ); | |
| 56 | + } | |
| 48 | 57 | |
| 58 | + if ( array_key_exists( 'enable_redirect_404_to_home', $options ) ) { | |
| 59 | + add_action( 'template_redirect', static function () { | |
| 60 | + if ( is_404() ) { | |
| 61 | + wp_redirect( home_url(), 301 ); | |
| 62 | + exit; | |
| 63 | + } | |
| 64 | + } ); | |
| 65 | + } | |
| 49 | 66 | |
| 67 | + if ( array_key_exists( 'enable_limit_comment_length', $options ) ) { | |
| 68 | + add_filter( 'preprocess_comment', [ $this, 'limit_comment_length' ] ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + if ( array_key_exists( 'enable_image_lazy_load', $options ) ) { | |
| 72 | + add_filter( 'wp_content_img_tag', static function( $html, $context, $id ) { | |
| 73 | + $has_loading = strpos( $html, ' loading=' ) !== false; | |
| 74 | + $has_decoding = strpos( $html, ' decoding=' ) !== false; | |
| 75 | + $has_high_priority = strpos( $html, 'fetchpriority="high"' ) !== false | |
| 76 | + || strpos( $html, "fetchpriority='high'" ) !== false; | |
| 77 | + | |
| 78 | + // If both attributes already exist, return unchanged | |
| 79 | + if ( $has_loading && $has_decoding ) { | |
| 80 | + return $html; | |
| 81 | + } | |
| 82 | + | |
| 83 | + // Build attributes to add | |
| 84 | + $attributes = ''; | |
| 85 | + // Don't add lazy loading to high priority images (LCP images) | |
| 86 | + if ( ! $has_loading && ! $has_high_priority ) { | |
| 87 | + $attributes .= ' loading="lazy"'; | |
| 88 | + } | |
| 89 | + if ( ! $has_decoding ) { | |
| 90 | + $attributes .= ' decoding="async"'; | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ( ! empty( $attributes ) ) { | |
| 94 | + $html = preg_replace( '/<img\b/i', '<img' . $attributes, $html, 1 ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + return $html; | |
| 98 | + }, 10, 3 ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + remove_filter( 'comment_text', 'make_clickable', 9 ); | |
| 102 | + add_filter( 'pre_comment_content', 'wp_strip_all_tags' ); | |
| 103 | + | |
| 50 | 104 | } |
| 51 | 105 | |
| 106 | + public function limit_comment_length( $commentdata ) { | |
| 107 | + $max_length = ! empty( $this->options['limit_comment_length'] ) ? absint( $this->options['limit_comment_length'] ) : 300; | |
| 108 | + $mess = ! empty( $this->options['limit_comment_mess'] ) ? esc_attr( $this->options['limit_comment_mess'] ) : 'Your comment is too long. Max 300 characters.'; | |
| 109 | + if ( strlen( $commentdata['comment_content'] ) > $max_length ) { | |
| 110 | + wp_die( esc_attr( $mess ) ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + return $commentdata; | |
| 114 | + } | |
| 115 | + | |
| 116 | + public function force_external_links_new_tab( $content ) { | |
| 117 | + $site_host = wp_parse_url( home_url(), PHP_URL_HOST ); | |
| 118 | + | |
| 119 | + return preg_replace_callback( | |
| 120 | + '#<a\s[^>]*href=["\'](https?:\/\/[^"\']+)["\'][^>]*>#i', | |
| 121 | + function ( $matches ) use ( $site_host ) { | |
| 122 | + $href = $matches[1]; | |
| 123 | + $link_host = wp_parse_url( $href, PHP_URL_HOST ); | |
| 124 | + | |
| 125 | + if ( $link_host && $link_host !== $site_host ) { | |
| 126 | + $tag = $matches[0]; | |
| 127 | + | |
| 128 | + if ( stripos( $tag, 'target=' ) === false ) { | |
| 129 | + $tag = str_ireplace( '<a', '<a target="_blank"', $tag ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + if ( stripos( $tag, 'rel=' ) === false ) { | |
| 133 | + $tag = str_ireplace( '<a', '<a rel="noopener noreferrer nofollow"', $tag ); | |
| 134 | + } | |
| 135 | + | |
| 136 | + return $tag; | |
| 137 | + } | |
| 138 | + | |
| 139 | + return $matches[0]; | |
| 140 | + }, | |
| 141 | + $content | |
| 142 | + ); | |
| 143 | + } | |
| 52 | 144 | |
| 53 | 145 | public function default_alt_to_avatar( $atts ) { |
| 54 | 146 | if ( empty( $atts['alt'] ) ) { |
| 55 | 147 | if ( have_comments() ) { |