← Back
Editing: class-meta-object.php
<?php namespace Integromat; defined( 'ABSPATH' ) || die( 'No direct access allowed' ); class Meta_Object { /** * Gets all metadata related to the object type * * @param $table * @return array */ public function get_meta_items( $table ) { global $wpdb; // Validate table name against known WordPress meta tables for security $allowed_tables = array( $wpdb->postmeta, $wpdb->usermeta, $wpdb->commentmeta, $wpdb->termmeta ); if ( ! in_array( $table, $allowed_tables, true ) ) { return array(); } // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- No WordPress function exists to get distinct meta keys from meta tables, one-time admin query, table name validated against whitelist and passed through esc_sql $query = "SELECT DISTINCT(meta_key) FROM `" . esc_sql( $table ) . "` ORDER BY meta_key"; $result = $wpdb->get_col( $query ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter if ( ! is_array( $result ) ) { return array(); } $object_type = self::object_type_for_table( $table ); $exposed = array(); foreach ( $result as $meta_key ) { if ( ! self::is_blocked_meta_key( $meta_key, $object_type ) ) { $exposed[] = $meta_key; } } return $exposed; } /** * Session tokens, application password hashes, and role keys are never offered, returned, or written. * * Other names that start with an underscore stay available. A valid API key is still required to read them. * * @param mixed $meta_key * @param string $object_type post, user, comment, or term. Unused. Callers still pass it. * @return bool */ public static function is_blocked_meta_key( $meta_key, $object_type = '' ) { unset( $object_type ); if ( ! is_string( $meta_key ) || '' === $meta_key ) { return true; } $key = strtolower( $meta_key ); if ( in_array( $key, array( 'session_tokens', 'use_ssl', 'default_password_nag', '_application_passwords' ), true ) ) { return true; } return self::is_role_meta_key( $key ); } /** * Capability and user_level keys use the site's table prefix, not only wp_. * * @param string $key Lowercased meta key. * @return bool */ private static function is_role_meta_key( $key ) { $prefixes = array( 'wp_' ); global $wpdb; if ( is_object( $wpdb ) && isset( $wpdb->base_prefix ) && is_string( $wpdb->base_prefix ) && '' !== $wpdb->base_prefix ) { $prefixes[] = strtolower( $wpdb->base_prefix ); } foreach ( array_unique( $prefixes ) as $prefix ) { $pattern = '/^' . preg_quote( $prefix, '/' ) . '(\d+_)?(capabilities|user_level)$/'; if ( preg_match( $pattern, $key ) ) { return true; } } return false; } /** * Drop blocked keys from a Custom API Fields option save. * * Option keys are stored as integromat_api_field_{meta_key}. * * @param mixed $input * @param string $object_type * @return array */ public static function sanitize_exposed_meta_options( $input, $object_type = '' ) { if ( ! is_array( $input ) ) { return array(); } $sanitized = array(); $prefix = IWC_FIELD_PREFIX; foreach ( $input as $key => $value ) { $clean_key = sanitize_key( $key ); if ( '' === $clean_key ) { continue; } $meta_key = $clean_key; if ( 0 === strpos( $clean_key, $prefix ) ) { $meta_key = substr( $clean_key, strlen( $prefix ) ); } if ( self::is_blocked_meta_key( $meta_key, $object_type ) ) { continue; } $sanitized[ $clean_key ] = sanitize_text_field( $value ); } return $sanitized; } /** * @param string $table * @return string */ private static function object_type_for_table( $table ) { global $wpdb; if ( $table === $wpdb->usermeta ) { return 'user'; } if ( $table === $wpdb->commentmeta ) { return 'comment'; } if ( $table === $wpdb->termmeta ) { return 'term'; } if ( $table === $wpdb->postmeta ) { return 'post'; } return ''; } }
Save File
Cancel