PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / MCP / Support / Sanitizer.php

Sanitizer.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/MCP/Support/Sanitizer.php

160 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\MCP\Support;
4
5 use LP_Helper;
6
7 defined( 'ABSPATH' ) || exit;
8
9 /**
10 * Boundary sanitization helpers for MCP write input.
11 *
12 * Reuses LearnPress sanitization conventions so MCP write tools store data the
13 * same way the Course Builder and admin flows do.
14 */
15 class Sanitizer {
16
17 /**
18 * Sanitize a plain text field (titles, names, short labels).
19 *
20 * @param mixed $value Raw value.
21 *
22 * @return string
23 */
24 public static function text( $value ): string {
25 return sanitize_text_field( (string) $value );
26 }
27
28 /**
29 * Sanitize rich/HTML content the LearnPress way.
30 *
31 * @param mixed $value Raw value.
32 *
33 * @return string
34 */
35 public static function html( $value ): string {
36 return LP_Helper::sanitize_params_submitted( (string) $value, 'html' );
37 }
38
39 /**
40 * Coerce a JSON boolean, "true"/"false", 1/0 into a strict boolean.
41 *
42 * @param mixed $value Raw value.
43 *
44 * @return bool
45 */
46 public static function boolean( $value ): bool {
47 return (bool) filter_var( $value, FILTER_VALIDATE_BOOLEAN );
48 }
49
50 /**
51 * Parse a date/time string into MySQL GMT format.
52 *
53 * Returns null for empty or unparseable input so callers can reject it
54 * instead of silently persisting the 1970-01-01 epoch.
55 *
56 * @param mixed $value Raw value.
57 *
58 * @return string|null 'Y-m-d H:i:s' (GMT) or null.
59 */
60 public static function datetime( $value ): ?string {
61 $value = trim( (string) $value );
62 if ( '' === $value ) {
63 return null;
64 }
65
66 $timestamp = strtotime( $value );
67 if ( false === $timestamp ) {
68 return null;
69 }
70
71 return gmdate( 'Y-m-d H:i:s', $timestamp );
72 }
73
74 /**
75 * Sanitize a URL for storage.
76 *
77 * @param mixed $value Raw value.
78 *
79 * @return string
80 */
81 public static function url( $value ): string {
82 return esc_url_raw( (string) $value );
83 }
84
85 /**
86 * Sanitize a list of positive integer IDs.
87 *
88 * @param mixed $value Raw value (array expected).
89 *
90 * @return int[]
91 */
92 public static function id_list( $value ): array {
93 if ( ! is_array( $value ) ) {
94 return array();
95 }
96
97 $ids = array();
98 foreach ( $value as $item ) {
99 $id = absint( $item );
100 if ( $id > 0 ) {
101 $ids[] = $id;
102 }
103 }
104
105 return array_values( array_unique( $ids ) );
106 }
107
108 /**
109 * Normalize ability input to an array.
110 *
111 * @param mixed $input Raw ability input.
112 * @param string $ability Ability name for error context.
113 *
114 * @return array|\WP_Error
115 */
116 public static function input_array( $input, string $ability ) {
117 if ( null === $input ) {
118 return array();
119 }
120 if ( is_array( $input ) ) {
121 return $input;
122 }
123
124 return Errors::invalid(
125 sprintf(
126 /* translators: %s: ability name. */
127 __( 'Invalid input for ability "%s". Input must be an object.', 'learnpress' ),
128 $ability
129 )
130 );
131 }
132
133 /**
134 * Normalize status input into a unique sanitized status list.
135 *
136 * @param mixed $input String, CSV string, array, or null.
137 *
138 * @return array
139 */
140 public static function status_list( $input ): array {
141 if ( null === $input || '' === $input ) {
142 return array();
143 }
144
145 $values = is_array( $input )
146 ? $input
147 : ( false !== strpos( (string) $input, ',' ) ? explode( ',', (string) $input ) : array( (string) $input ) );
148
149 $out = array();
150 foreach ( $values as $value ) {
151 $status = LP_Helper::sanitize_params_submitted( (string) $value, 'key' );
152 if ( '' !== $status ) {
153 $out[] = $status;
154 }
155 }
156
157 return array_values( array_unique( $out ) );
158 }
159 }
160