PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.5
WP Coder – Insert & Manage Code Snippets v4.5
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / includes / snippets / class-snippet-enabled.php

class-snippet-enabled.php in WP Coder – Insert & Manage Code Snippets 4.5, at includes/snippets/class-snippet-enabled.php

331 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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
104 }
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 }
144
145 public function default_alt_to_avatar( $atts ) {
146 if ( empty( $atts['alt'] ) ) {
147 if ( have_comments() ) {
148 $author = get_comment_author();
149 } else {
150 $author = get_the_author_meta( 'display_name' );
151 }
152 $alt = sprintf( 'Avatar for %s', $author );
153 $atts['alt'] = $alt;
154 }
155
156 return $atts;
157 }
158
159 public function add_slug_body_class( $classes ) {
160 global $post;
161 if ( isset( $post ) ) {
162 $classes[] = $post->post_type . '-' . $post->post_name;
163 }
164
165 return $classes;
166 }
167
168 public function rss_post_thumbnail( $content ) {
169 global $post;
170 if ( has_post_thumbnail( $post->ID ) ) {
171 $content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content;
172 }
173
174 return $content;
175 }
176
177 public function add_duplicate_link( $actions, $post ) {
178 if ( ! current_user_can( 'edit_posts' ) ) {
179 return $actions;
180 }
181
182 $url = wp_nonce_url(
183 add_query_arg(
184 array(
185 'action' => 'wpcoder_duplicate_post',
186 'post' => $post->ID,
187 ),
188 'admin.php'
189 ),
190 basename( __FILE__ ),
191 'duplicate_nonce'
192 );
193
194 $actions['duplicate'] = '<a href="' . esc_url( $url ) . '" title="Duplicate this as draft" rel="permalink">Duplicate</a>';
195
196 return $actions;
197 }
198
199 public function duplicate_post(): void {
200 // check if post ID has been provided and action
201 if ( empty( $_GET['post'] ) ) {
202 wp_die( 'No post to duplicate has been provided!' );
203 }
204
205 // Nonce verification
206 if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'],
207 basename( __FILE__ ) ) ) {
208 return;
209 }
210
211 // Get the original post id
212 $post_id = absint( $_GET['post'] );
213
214 // And all the original post data then
215 $post = get_post( $post_id );
216
217 /*
218 * if you don't want current user to be the new post author,
219 * then change next couple of lines to this: $new_post_author = $post->post_author;
220 */
221 $current_user = wp_get_current_user();
222 $new_post_author = $current_user->ID;
223
224 // if post data exists (I am sure it is, but just in a case), create the post duplicate
225 if ( $post ) {
226 // new post data array
227 $args = array(
228 'comment_status' => $post->comment_status,
229 'ping_status' => $post->ping_status,
230 'post_author' => $new_post_author,
231 'post_content' => $post->post_content,
232 'post_excerpt' => $post->post_excerpt,
233 'post_name' => $post->post_name,
234 'post_parent' => $post->post_parent,
235 'post_password' => $post->post_password,
236 'post_status' => 'draft',
237 'post_title' => $post->post_title,
238 'post_type' => $post->post_type,
239 'to_ping' => $post->to_ping,
240 'menu_order' => $post->menu_order
241 );
242
243 // insert the post by wp_insert_post() function
244 $new_post_id = wp_insert_post( $args );
245
246 /*
247 * get all current post terms ad set them to the new post draft
248 */
249 $taxonomies = get_object_taxonomies( get_post_type( $post ) ); // returns array of taxonomy names for post type, ex array("category", "post_tag");
250 if ( $taxonomies ) {
251 foreach ( $taxonomies as $taxonomy ) {
252 $post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
253 wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
254 }
255 }
256
257 // duplicate all post meta
258 $post_meta = get_post_meta( $post_id );
259 if ( $post_meta ) {
260 foreach ( $post_meta as $meta_key => $meta_values ) {
261 if ( '_wp_old_slug' == $meta_key ) { // do nothing for this meta key
262 continue;
263 }
264
265 foreach ( $meta_values as $meta_value ) {
266 add_post_meta( $new_post_id, $meta_key, $meta_value );
267 }
268 }
269 }
270
271 // or we can redirect to all posts with a message
272 wp_safe_redirect(
273 add_query_arg(
274 array(
275 'post_type' => ( 'post' !== get_post_type( $post ) ? get_post_type( $post ) : false ),
276 'saved' => 'post_duplication_created' // just a custom slug here
277 ),
278 admin_url( 'edit.php' )
279 )
280 );
281 exit;
282 }
283
284 wp_die( 'Post creation failed, could not find original post.' );
285 }
286
287 public function duplication_admin_notice(): void {
288 // Get the current screen
289 $screen = get_current_screen();
290
291 if ( 'edit' !== $screen->base ) {
292 return;
293 }
294
295 //Checks if settings updated
296 if ( isset( $_GET['saved'] ) && 'post_duplication_created' === $_GET['saved'] ) {
297 echo '<div class="notice notice-success is-dismissible"><p>Post copy created.</p></div>';
298 }
299 }
300
301 public function add_svg_mime( $upload_mimes ) {
302 if ( ! current_user_can( 'administrator' ) ) {
303 return $upload_mimes;
304 }
305
306 $upload_mimes['svg'] = 'image/svg+xml';
307 $upload_mimes['svgz'] = 'image/svg+xml';
308
309 return $upload_mimes;
310 }
311
312 public function confirm_file_type_is_svg( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {
313 if ( ! $wp_check_filetype_and_ext['type'] ) {
314 $check_filetype = wp_check_filetype( $filename, $mimes );
315 $ext = $check_filetype['ext'];
316 $type = $check_filetype['type'];
317 $proper_filename = $filename;
318
319 if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
320 $ext = false;
321 $type = false;
322 }
323
324 $wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
325 }
326
327 return $wp_check_filetype_and_ext;
328 }
329 }
330
331 new WPCoder_Lite_Enabled_Snippets;