options = $options;
if ( array_key_exists( 'enable_duplication', $options ) ) {
add_filter( 'post_row_actions', [ $this, 'add_duplicate_link' ], 10, 2 );
add_filter( 'page_row_actions', [ $this, 'add_duplicate_link' ], 10, 2 );
add_action( 'admin_action_wpcoder_duplicate_post', [ $this, 'duplicate_post' ] );
add_action( 'admin_notices', [ $this, 'duplication_admin_notice' ] );
}
if ( array_key_exists( 'enable_svg_upload', $options ) ) {
add_filter( 'upload_mimes', [ $this, 'add_svg_mime' ] );
add_filter( 'wp_check_filetype_and_ext', [ $this, 'confirm_file_type_is_svg' ], 10, 5 );
}
if ( array_key_exists( 'enable_pages_excerpt', $options ) ) {
add_action( 'after_setup_theme', static function () {
add_post_type_support( 'page', 'excerpt' );
} );
}
if ( array_key_exists( 'enable_shortcode_in_text_widgets', $options ) ) {
add_filter( 'widget_text', 'do_shortcode' );
}
if ( array_key_exists( 'enable_featured_img_rss_feeds', $options ) ) {
add_filter( 'the_excerpt_rss', [ $this, 'rss_post_thumbnail' ] );
add_filter( 'the_content_feed', [ $this, 'rss_post_thumbnail' ] );
}
if ( array_key_exists( 'enable_page_slug_to_body_class', $options ) ) {
add_filter( 'body_class', [ $this, 'add_slug_body_class' ] );
}
if ( array_key_exists( 'enable_lowercase_filenames_for_uploads', $options ) ) {
add_filter( 'sanitize_file_name', 'mb_strtolower' );
}
if ( array_key_exists( 'enable_default_alt_to_avatar', $options ) ) {
add_filter( 'pre_get_avatar_data', [ $this, 'default_alt_to_avatar' ] );
}
if ( array_key_exists( 'enable_force_external_links_new_tab', $options ) ) {
add_filter( 'the_content', [ $this, 'force_external_links_new_tab' ] );
}
if ( array_key_exists( 'enable_redirect_404_to_home', $options ) ) {
add_action( 'template_redirect', static function () {
if ( is_404() ) {
wp_redirect( home_url(), 301 );
exit;
}
} );
}
if ( array_key_exists( 'enable_limit_comment_length', $options ) ) {
add_filter( 'preprocess_comment', [ $this, 'limit_comment_length' ] );
}
if ( array_key_exists( 'enable_image_lazy_load', $options ) ) {
add_filter( 'wp_content_img_tag', static function( $html, $context, $id ) {
$has_loading = strpos( $html, ' loading=' ) !== false;
$has_decoding = strpos( $html, ' decoding=' ) !== false;
$has_high_priority = strpos( $html, 'fetchpriority="high"' ) !== false
|| strpos( $html, "fetchpriority='high'" ) !== false;
// If both attributes already exist, return unchanged
if ( $has_loading && $has_decoding ) {
return $html;
}
// Build attributes to add
$attributes = '';
// Don't add lazy loading to high priority images (LCP images)
if ( ! $has_loading && ! $has_high_priority ) {
$attributes .= ' loading="lazy"';
}
if ( ! $has_decoding ) {
$attributes .= ' decoding="async"';
}
if ( ! empty( $attributes ) ) {
$html = preg_replace( '/ ' . get_the_post_thumbnail( $post->ID ) . 'options['limit_comment_length'] ) ? absint( $this->options['limit_comment_length'] ) : 300;
$mess = ! empty( $this->options['limit_comment_mess'] ) ? esc_attr( $this->options['limit_comment_mess'] ) : 'Your comment is too long. Max 300 characters.';
if ( strlen( $commentdata['comment_content'] ) > $max_length ) {
wp_die( esc_attr( $mess ) );
}
return $commentdata;
}
public function force_external_links_new_tab( $content ) {
$site_host = wp_parse_url( home_url(), PHP_URL_HOST );
return preg_replace_callback(
'#]*href=["\'](https?:\/\/[^"\']+)["\'][^>]*>#i',
function ( $matches ) use ( $site_host ) {
$href = $matches[1];
$link_host = wp_parse_url( $href, PHP_URL_HOST );
if ( $link_host && $link_host !== $site_host ) {
$tag = $matches[0];
if ( stripos( $tag, 'target=' ) === false ) {
$tag = str_ireplace( 'post_type . '-' . $post->post_name;
}
return $classes;
}
public function rss_post_thumbnail( $content ) {
global $post;
if ( has_post_thumbnail( $post->ID ) ) {
$content = '
Post copy created.