[ 'prefixes' => [ 'global_', 'lock_global_' ], 'keys' => [ 'default_container_width', 'container_padding', 'container_element_gap', 'enabled_block_copy_paste_style', 'enabled_load_google_font_locally', 'enabled_only_selected_fonts', 'selected_fonts', 'font_metric_fallback', ], ], 'ablocks_manage_performance' => [ 'prefixes' => [ 'perf_' ], 'keys' => [ 'enabled_assets_file_generation', ], ], ]); } /** * The capability required to change a given settings key. * * @param string $key * * @return string */ public static function capability_for_key( $key ) { foreach ( self::key_map() as $capability => $match ) { if ( in_array( $key, $match['keys'], true ) ) { return $capability; } foreach ( $match['prefixes'] as $prefix ) { if ( 0 === strpos( $key, $prefix ) ) { return $capability; } } } return 'ablocks_manage_settings'; } /** * Replace values the current user may not change with the saved ones. * * @param array $payload * * @return array */ public static function filter_payload( array $payload ) { // Real administrators, and anyone holding all three, change everything. if ( Permissions::is_real_admin() ) { return $payload; } $saved = BaseSettings::get_saved_data(); $default = BaseSettings::get_default_data(); $allowed = []; foreach ( $payload as $key => $value ) { $capability = self::capability_for_key( $key ); if ( ! isset( $allowed[ $capability ] ) ) { $allowed[ $capability ] = current_user_can( $capability ); } if ( $allowed[ $capability ] ) { continue; } if ( array_key_exists( $key, $saved ) ) { $payload[ $key ] = $saved[ $key ]; } elseif ( array_key_exists( $key, $default ) ) { $payload[ $key ] = $default[ $key ]; } else { unset( $payload[ $key ] ); } } return $payload; } }