PluginProbe
Code Embed / 2.6.2
Code Embed v2.6.2
2.6.4 2.6.3 2.6.2 2.6.1 trunk 1.0 1.1 1.2 1.3 1.4.1 1.5.1 1.6.1 2.0.2 2.1.2 2.2.2 2.3.9 2.4 2.5.1 2.5.2 2.6
simple-embed-code / includes / secure.php

secure.php in Code Embed 2.6.2, at includes/secure.php

72 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Security
4 *
5 * Functions related to sanitizing Code Embed meta values.
6 *
7 * @package simple-embed-code
8 */
9
10 // Exit if accessed directly.
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Sanitize Code Embed meta on every write
18 *
19 * Filter that fires on every call to update_metadata / add_metadata — including the
20 * wp_ajax_add_meta AJAX handler and the REST API, not just save_post.
21 *
22 * @param mixed $check Null to allow the operation, non-null to short-circuit.
23 * @param int $object_id Post ID.
24 * @param string $meta_key Meta key being written.
25 * @param mixed $meta_value Meta value being written.
26 * @return mixed Null to allow the write to proceed, or true to short-circuit it.
27 */
28 function sec_sanitize_meta_on_write( $check, $object_id, $meta_key, $meta_value ) {
29
30 // Allow admins / editors with unfiltered_html to write without restriction.
31 if ( current_user_can( 'unfiltered_html' ) ) {
32 return $check;
33 }
34
35 $options = get_option( 'artiss_code_embed' );
36
37 if ( ! is_array( $options ) || empty( $options['keyword_ident'] ) ) {
38 return $check;
39 }
40
41 $prefix = $options['keyword_ident'];
42
43 // Only act on meta keys that belong to this plugin.
44 if ( substr( $meta_key, 0, strlen( $prefix ) ) !== $prefix ) {
45 return $check;
46 }
47
48 // Strip dangerous markup while preserving safe HTML.
49 $clean = wp_kses_post( $meta_value );
50
51 if ( $clean === $meta_value ) {
52 // Value is already clean — let the normal write proceed.
53 return $check;
54 }
55
56 // The value was dirty. Remove this filter temporarily to avoid infinite recursion, write the sanitized value ourselves, then
57 // re-add the filter and short-circuit the original write.
58 remove_filter( 'update_post_metadata', 'sec_sanitize_meta_on_write', 10 );
59 remove_filter( 'add_post_metadata', 'sec_sanitize_meta_on_write', 10 );
60
61 update_post_meta( $object_id, $meta_key, $clean );
62
63 add_filter( 'update_post_metadata', 'sec_sanitize_meta_on_write', 10, 4 );
64 add_filter( 'add_post_metadata', 'sec_sanitize_meta_on_write', 10, 4 );
65
66 // Return a non-null value to short-circuit the original (unsanitized) write.
67 return true;
68 }
69
70 add_filter( 'update_post_metadata', 'sec_sanitize_meta_on_write', 10, 4 );
71 add_filter( 'add_post_metadata', 'sec_sanitize_meta_on_write', 10, 4 );
72