Blocks
2 weeks ago
Assets.php
2 weeks ago
Configuration.php
2 weeks ago
Configuration_Interface.php
2 weeks ago
Meta.php
2 weeks ago
Meta_Interface.php
2 weeks ago
Provider.php
2 weeks ago
Utils.php
2 weeks ago
Meta.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tribe__Editor__Meta |
| 5 | * |
| 6 | * @since 4.8 |
| 7 | */ |
| 8 | abstract class Tribe__Editor__Meta |
| 9 | implements Tribe__Editor__Meta_Interface { |
| 10 | |
| 11 | /** |
| 12 | * Default definition for an attribute of type text |
| 13 | * |
| 14 | * @since 4.8 |
| 15 | * |
| 16 | * @return array |
| 17 | */ |
| 18 | protected function text() { |
| 19 | return [ |
| 20 | 'auth_callback' => [ $this, 'auth_callback' ], |
| 21 | 'sanitize_callback' => 'sanitize_text_field', |
| 22 | 'type' => 'string', |
| 23 | 'single' => true, |
| 24 | 'show_in_rest' => true, |
| 25 | ]; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Add arguments to escape a text area field |
| 30 | * |
| 31 | * @since 4.8 |
| 32 | * |
| 33 | * @return array |
| 34 | */ |
| 35 | protected function textarea() { |
| 36 | return [ |
| 37 | 'auth_callback' => [ $this, 'auth_callback' ], |
| 38 | 'sanitize_callback' => 'sanitize_textarea_field', |
| 39 | 'type' => 'string', |
| 40 | 'single' => true, |
| 41 | 'show_in_rest' => true, |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Add arguments to escape a field of URL type |
| 47 | * |
| 48 | * @since 4.8 |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | protected function url() { |
| 53 | return [ |
| 54 | 'auth_callback' => [ $this, 'auth_callback' ], |
| 55 | 'sanitize_callback' => 'esc_url_raw', |
| 56 | 'type' => 'string', |
| 57 | 'single' => true, |
| 58 | 'show_in_rest' => true, |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Default definition for an attribute of type text |
| 64 | * |
| 65 | * @since 4.8 |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | protected function numeric() { |
| 70 | return [ |
| 71 | 'auth_callback' => [ $this, 'auth_callback' ], |
| 72 | 'sanitize_callback' => 'absint', |
| 73 | 'type' => 'number', |
| 74 | 'single' => true, |
| 75 | 'show_in_rest' => true, |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | /*** |
| 80 | * Default definition for an attribute of type boolean |
| 81 | * |
| 82 | * @since 4.8 |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | protected function boolean() { |
| 87 | return [ |
| 88 | 'auth_callback' => [ $this, 'auth_callback' ], |
| 89 | 'sanitize_callback' => [ $this, 'sanitize_boolean' ], |
| 90 | 'type' => 'boolean', |
| 91 | 'single' => true, |
| 92 | 'show_in_rest' => true, |
| 93 | ]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Register a numeric type of array |
| 98 | * |
| 99 | * @since 4.8 |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | protected function numeric_array() { |
| 104 | return [ |
| 105 | 'description' => __( 'Numeric Array', 'tribe-common' ), |
| 106 | 'auth_callback' => [ $this, 'auth_callback' ], |
| 107 | 'sanitize_callback' => [ $this, 'sanitize_numeric_array' ], |
| 108 | 'type' => 'number', |
| 109 | 'single' => false, |
| 110 | 'show_in_rest' => true, |
| 111 | ]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Register a text type of array |
| 116 | * |
| 117 | * @since 4.8 |
| 118 | * |
| 119 | * @return array |
| 120 | */ |
| 121 | protected function text_array() { |
| 122 | return [ |
| 123 | 'description' => __( 'Text Array', 'tribe-common' ), |
| 124 | 'auth_callback' => [ $this, 'auth_callback' ], |
| 125 | 'sanitize_callback' => [ $this, 'sanitize_text_array' ], |
| 126 | 'type' => 'string', |
| 127 | 'single' => false, |
| 128 | 'show_in_rest' => true, |
| 129 | ]; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Sanitize an array of text |
| 134 | * |
| 135 | * @since 4.8 |
| 136 | * |
| 137 | * @param $value |
| 138 | * |
| 139 | * @return array |
| 140 | */ |
| 141 | public function sanitize_text_array( $value ) { |
| 142 | if ( is_array( $value ) ) { |
| 143 | return array_map( 'sanitize_text_field', $value ); |
| 144 | } else { |
| 145 | return sanitize_text_field( $value ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Checks and sanitize a given value to a numeric array or a numeric string |
| 151 | * |
| 152 | * @since 4.8 |
| 153 | * |
| 154 | * @param mixed $value Check agains this value |
| 155 | * |
| 156 | * @return array|bool|int |
| 157 | */ |
| 158 | public function sanitize_numeric_array( $value ) { |
| 159 | if ( is_array( $value ) ) { |
| 160 | return wp_parse_id_list( $value ); |
| 161 | } elseif ( is_numeric( $value ) ) { |
| 162 | return absint( $value ); |
| 163 | } else { |
| 164 | return false; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Make sure sanitization on boolean does not triggered warnings when multiple values are passed |
| 170 | * to the function |
| 171 | * |
| 172 | * @since 4.8 |
| 173 | * |
| 174 | * @param $value |
| 175 | * |
| 176 | * @return bool |
| 177 | */ |
| 178 | public function sanitize_boolean( $value ) { |
| 179 | return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sanitize strings allowing the usage of white spaces before or after the separators, as |
| 184 | * - sanitize_text_field removes any whitespace |
| 185 | * |
| 186 | * @since 4.8 |
| 187 | * |
| 188 | * @param $value |
| 189 | * |
| 190 | * @return mixed |
| 191 | */ |
| 192 | public function sanitize_separator( $value ) { |
| 193 | return filter_var( $value, FILTER_SANITIZE_STRING ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Verify if the current user can edit or not this Post |
| 198 | * |
| 199 | * @since 4.8 |
| 200 | * |
| 201 | * @param bool $allowed Whether the user can add the post meta. Default false. |
| 202 | * @param string $meta_key The meta key. |
| 203 | * @param int $post_id Post ID. |
| 204 | * @param int $user_id User ID. |
| 205 | * @param string $cap Capability name. |
| 206 | * @param array $caps User capabilities. |
| 207 | * |
| 208 | * @return boolean |
| 209 | */ |
| 210 | public function auth_callback( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) { |
| 211 | $post = get_post( $post_id ); |
| 212 | $post_type_obj = get_post_type_object( $post->post_type ); |
| 213 | $current_user_can = current_user_can( $post_type_obj->cap->edit_post, $post_id ); |
| 214 | |
| 215 | return $current_user_can; |
| 216 | } |
| 217 | } |
| 218 |