| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WPCoder_Lite_Enabled_Snippets { |
| 6 |
/** |
| 7 |
* @var false|mixed|null |
| 8 |
*/ |
| 9 |
private $options; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$options = get_option( '_wp_coder_snippets', [] ); |
| 13 |
$this->options = $options; |
| 14 |
|
| 15 |
if ( array_key_exists( 'enable_duplication', $options ) ) { |
| 16 |
add_filter( 'post_row_actions', [ $this, 'add_duplicate_link' ], 10, 2 ); |
| 17 |
add_filter( 'page_row_actions', [ $this, 'add_duplicate_link' ], 10, 2 ); |
| 18 |
add_action( 'admin_action_wpcoder_duplicate_post', [ $this, 'duplicate_post' ] ); |
| 19 |
add_action( 'admin_notices', [ $this, 'duplication_admin_notice' ] ); |
| 20 |
} |
| 21 |
|
| 22 |
if ( array_key_exists( 'enable_svg_upload', $options ) ) { |
| 23 |
add_filter( 'upload_mimes', [ $this, 'add_svg_mime' ] ); |
| 24 |
add_filter( 'wp_check_filetype_and_ext', [ $this, 'confirm_file_type_is_svg' ], 10, 5 ); |
| 25 |
} |
| 26 |
|
| 27 |
if ( array_key_exists( 'enable_pages_excerpt', $options ) ) { |
| 28 |
add_action( 'after_setup_theme', static function () { |
| 29 |
add_post_type_support( 'page', 'excerpt' ); |
| 30 |
} ); |
| 31 |
} |
| 32 |
|
| 33 |
if ( array_key_exists( 'enable_shortcode_in_text_widgets', $options ) ) { |
| 34 |
add_filter( 'widget_text', 'do_shortcode' ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( array_key_exists( 'enable_featured_img_rss_feeds', $options ) ) { |
| 38 |
add_filter( 'the_excerpt_rss', [ $this, 'rss_post_thumbnail' ] ); |
| 39 |
add_filter( 'the_content_feed', [ $this, 'rss_post_thumbnail' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( array_key_exists( 'enable_page_slug_to_body_class', $options ) ) { |
| 43 |
add_filter( 'body_class', [ $this, 'add_slug_body_class' ] ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( array_key_exists( 'enable_lowercase_filenames_for_uploads', $options ) ) { |
| 47 |
add_filter( 'sanitize_file_name', 'mb_strtolower' ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( array_key_exists( 'enable_default_alt_to_avatar', $options ) ) { |
| 51 |
add_filter( 'pre_get_avatar_data', [ $this, 'default_alt_to_avatar' ] ); |
| 52 |
} |
| 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 |
} |
| 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 |
} |
| 66 |
|
| 67 |
if ( array_key_exists( 'enable_limit_comment_length', $options ) ) { |
| 68 |
add_filter( 'preprocess_comment', [ $this, 'limit_comment_length' ] ); |
| 69 |
} |
| 70 |
|
| 71 |
remove_filter( 'comment_text', 'make_clickable', 9 ); |
| 72 |
add_filter( 'pre_comment_content', 'wp_strip_all_tags' ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public function limit_comment_length( $commentdata ) { |
| 77 |
$max_length = ! empty( $this->options['limit_comment_length'] ) ? absint( $this->options['limit_comment_length'] ) : 300; |
| 78 |
$mess = ! empty( $this->options['limit_comment_mess'] ) ? esc_attr( $this->options['limit_comment_mess'] ) : 'Your comment is too long. Max 300 characters.'; |
| 79 |
if ( strlen( $commentdata['comment_content'] ) > $max_length ) { |
| 80 |
wp_die( esc_attr( $mess ) ); |
| 81 |
} |
| 82 |
|
| 83 |
return $commentdata; |
| 84 |
} |
| 85 |
|
| 86 |
public function force_external_links_new_tab( $content ) { |
| 87 |
$site_url = wp_parse_url( home_url()); |
| 88 |
|
| 89 |
return preg_replace_callback( |
| 90 |
'#<a\s[^>]*href=["\'](https?:\/\/[^"\']+)["\'][^>]*>#i', |
| 91 |
function ( $matches ) use ( $site_url ) { |
| 92 |
$href = $matches[1]; |
| 93 |
$link_host = wp_parse_url( $href ); |
| 94 |
|
| 95 |
if ( $link_host && $link_host !== $site_url ) { |
| 96 |
$tag = $matches[0]; |
| 97 |
|
| 98 |
if ( stripos( $tag, 'target=' ) === false ) { |
| 99 |
$tag = str_ireplace( '<a', '<a target="_blank"', $tag ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( stripos( $tag, 'rel=' ) === false ) { |
| 103 |
$tag = str_ireplace( '<a', '<a rel="noopener noreferrer nofollow"', $tag ); |
| 104 |
} |
| 105 |
|
| 106 |
return $tag; |
| 107 |
} |
| 108 |
|
| 109 |
return $matches[0]; |
| 110 |
}, |
| 111 |
$content |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
public function default_alt_to_avatar( $atts ) { |
| 116 |
if ( empty( $atts['alt'] ) ) { |
| 117 |
if ( have_comments() ) { |
| 118 |
$author = get_comment_author(); |
| 119 |
} else { |
| 120 |
$author = get_the_author_meta( 'display_name' ); |
| 121 |
} |
| 122 |
$alt = sprintf( 'Avatar for %s', $author ); |
| 123 |
$atts['alt'] = $alt; |
| 124 |
} |
| 125 |
|
| 126 |
return $atts; |
| 127 |
} |
| 128 |
|
| 129 |
public function add_slug_body_class( $classes ) { |
| 130 |
global $post; |
| 131 |
if ( isset( $post ) ) { |
| 132 |
$classes[] = $post->post_type . '-' . $post->post_name; |
| 133 |
} |
| 134 |
|
| 135 |
return $classes; |
| 136 |
} |
| 137 |
|
| 138 |
public function rss_post_thumbnail( $content ) { |
| 139 |
global $post; |
| 140 |
if ( has_post_thumbnail( $post->ID ) ) { |
| 141 |
$content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content; |
| 142 |
} |
| 143 |
|
| 144 |
return $content; |
| 145 |
} |
| 146 |
|
| 147 |
public function add_duplicate_link( $actions, $post ) { |
| 148 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 149 |
return $actions; |
| 150 |
} |
| 151 |
|
| 152 |
$url = wp_nonce_url( |
| 153 |
add_query_arg( |
| 154 |
array( |
| 155 |
'action' => 'wpcoder_duplicate_post', |
| 156 |
'post' => $post->ID, |
| 157 |
), |
| 158 |
'admin.php' |
| 159 |
), |
| 160 |
basename( __FILE__ ), |
| 161 |
'duplicate_nonce' |
| 162 |
); |
| 163 |
|
| 164 |
$actions['duplicate'] = '<a href="' . esc_url( $url ) . '" title="Duplicate this as draft" rel="permalink">Duplicate</a>'; |
| 165 |
|
| 166 |
return $actions; |
| 167 |
} |
| 168 |
|
| 169 |
public function duplicate_post(): void { |
| 170 |
// check if post ID has been provided and action |
| 171 |
if ( empty( $_GET['post'] ) ) { |
| 172 |
wp_die( 'No post to duplicate has been provided!' ); |
| 173 |
} |
| 174 |
|
| 175 |
// Nonce verification |
| 176 |
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], |
| 177 |
basename( __FILE__ ) ) ) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
// Get the original post id |
| 182 |
$post_id = absint( $_GET['post'] ); |
| 183 |
|
| 184 |
// And all the original post data then |
| 185 |
$post = get_post( $post_id ); |
| 186 |
|
| 187 |
/* |
| 188 |
* if you don't want current user to be the new post author, |
| 189 |
* then change next couple of lines to this: $new_post_author = $post->post_author; |
| 190 |
*/ |
| 191 |
$current_user = wp_get_current_user(); |
| 192 |
$new_post_author = $current_user->ID; |
| 193 |
|
| 194 |
// if post data exists (I am sure it is, but just in a case), create the post duplicate |
| 195 |
if ( $post ) { |
| 196 |
// new post data array |
| 197 |
$args = array( |
| 198 |
'comment_status' => $post->comment_status, |
| 199 |
'ping_status' => $post->ping_status, |
| 200 |
'post_author' => $new_post_author, |
| 201 |
'post_content' => $post->post_content, |
| 202 |
'post_excerpt' => $post->post_excerpt, |
| 203 |
'post_name' => $post->post_name, |
| 204 |
'post_parent' => $post->post_parent, |
| 205 |
'post_password' => $post->post_password, |
| 206 |
'post_status' => 'draft', |
| 207 |
'post_title' => $post->post_title, |
| 208 |
'post_type' => $post->post_type, |
| 209 |
'to_ping' => $post->to_ping, |
| 210 |
'menu_order' => $post->menu_order |
| 211 |
); |
| 212 |
|
| 213 |
// insert the post by wp_insert_post() function |
| 214 |
$new_post_id = wp_insert_post( $args ); |
| 215 |
|
| 216 |
/* |
| 217 |
* get all current post terms ad set them to the new post draft |
| 218 |
*/ |
| 219 |
$taxonomies = get_object_taxonomies( get_post_type( $post ) ); // returns array of taxonomy names for post type, ex array("category", "post_tag"); |
| 220 |
if ( $taxonomies ) { |
| 221 |
foreach ( $taxonomies as $taxonomy ) { |
| 222 |
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) ); |
| 223 |
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false ); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// duplicate all post meta |
| 228 |
$post_meta = get_post_meta( $post_id ); |
| 229 |
if ( $post_meta ) { |
| 230 |
foreach ( $post_meta as $meta_key => $meta_values ) { |
| 231 |
if ( '_wp_old_slug' == $meta_key ) { // do nothing for this meta key |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
foreach ( $meta_values as $meta_value ) { |
| 236 |
add_post_meta( $new_post_id, $meta_key, $meta_value ); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
// or we can redirect to all posts with a message |
| 242 |
wp_safe_redirect( |
| 243 |
add_query_arg( |
| 244 |
array( |
| 245 |
'post_type' => ( 'post' !== get_post_type( $post ) ? get_post_type( $post ) : false ), |
| 246 |
'saved' => 'post_duplication_created' // just a custom slug here |
| 247 |
), |
| 248 |
admin_url( 'edit.php' ) |
| 249 |
) |
| 250 |
); |
| 251 |
exit; |
| 252 |
} |
| 253 |
|
| 254 |
wp_die( 'Post creation failed, could not find original post.' ); |
| 255 |
} |
| 256 |
|
| 257 |
public function duplication_admin_notice(): void { |
| 258 |
// Get the current screen |
| 259 |
$screen = get_current_screen(); |
| 260 |
|
| 261 |
if ( 'edit' !== $screen->base ) { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
//Checks if settings updated |
| 266 |
if ( isset( $_GET['saved'] ) && 'post_duplication_created' === $_GET['saved'] ) { |
| 267 |
echo '<div class="notice notice-success is-dismissible"><p>Post copy created.</p></div>'; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
public function add_svg_mime( $upload_mimes ) { |
| 272 |
if ( ! current_user_can( 'administrator' ) ) { |
| 273 |
return $upload_mimes; |
| 274 |
} |
| 275 |
|
| 276 |
$upload_mimes['svg'] = 'image/svg+xml'; |
| 277 |
$upload_mimes['svgz'] = 'image/svg+xml'; |
| 278 |
|
| 279 |
return $upload_mimes; |
| 280 |
} |
| 281 |
|
| 282 |
public function confirm_file_type_is_svg( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) { |
| 283 |
if ( ! $wp_check_filetype_and_ext['type'] ) { |
| 284 |
$check_filetype = wp_check_filetype( $filename, $mimes ); |
| 285 |
$ext = $check_filetype['ext']; |
| 286 |
$type = $check_filetype['type']; |
| 287 |
$proper_filename = $filename; |
| 288 |
|
| 289 |
if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) { |
| 290 |
$ext = false; |
| 291 |
$type = false; |
| 292 |
} |
| 293 |
|
| 294 |
$wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' ); |
| 295 |
} |
| 296 |
|
| 297 |
return $wp_check_filetype_and_ext; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
new WPCoder_Lite_Enabled_Snippets; |