1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
<?php
namespace WPGraphQL\Type\Setting;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Type\WPObjectType;
use WPGraphQL\Types;
/**
* class SettingType
*
* This sets up the base settingType. Custom settings that are set to "show_in_graphql" automatically
* use the SettingType and inherit the fields that are defined here. The fields get passed through a
* filter unique to each type, so each setting can modify it's type schema via field filters.
*
*/
class SettingType extends WPObjectType {
/**
* Holds the $fields definition for the SettingType
*
* @var array $fields
* @access private
*/
private static $fields;
/**
* Holds the $setting_type definition
*
* @var string $setting_type
* @access private
*/
private static $setting_type;
/**
* Holds the $setting_type_array definition which contains
* all of the settings for the given setting_type
*
* @var array $setting_fields
* @access private
*/
private static $setting_fields;
/**
* SettingType constructor.
*
* @param string $setting_type The setting group name
* @access public
*/
public function __construct( $setting_type ) {
/**
* Set the setting_type so we can use it in $fields
*/
self::$setting_type = $setting_type;
/**
* Retrieve all of the settings that are categorized under the $setting_type
* and set them as the $setting_type_array for later use in building fields
*/
self::$setting_fields = DataSource::get_setting_group_fields( $setting_type );
$config = [
'name' => ucfirst( $setting_type ) . 'Settings',
'description' => sprintf( __( 'The %s setting type', 'wp-graphql' ), $setting_type ),
'fields' => self::fields( self::$setting_fields, $setting_type ),
];
parent::__construct( $config );
}
/**
* This defines the fields (various settings) for a given setting type
*
* @param $setting_fields
*
* @access private
* @return \GraphQL\Type\Definition\FieldDefinition|mixed|null
*/
private static function fields( $setting_fields, $group ) {
/**
* Set $fields to an empty array so that we aren't storing values
* from another setting_type
*/
$fields = [];
if ( ! empty( $setting_fields ) && is_array( $setting_fields ) ) {
/**
* Loop through the $setting_type_array and build the setting with
* proper fields
*/
foreach ( $setting_fields as $key => $setting_field ) {
/**
* Determine if the individual setting already has a
* REST API name, if not use the option name (setting).
* Sanitize the field name to be camelcase
*/
if ( ! empty( $setting_field['show_in_rest']['name'] ) ) {
$field_key = $setting_field['show_in_rest']['name'];
} else if ( ! empty( $setting_field['setting'] ) ) {
$field_key = $setting_field['setting'];
} else {
$field_key = $key;
}
$field_key = lcfirst( str_replace( '_', '', ucwords( $field_key, '_' ) ) );
if ( ! empty( $key ) && ! empty( $field_key ) ) {
/**
* Dynamically build the individual setting and it's fields
* then add it to the fields array
*/
$fields[ $field_key ] = [
'type' => Types::get_type( $setting_field['type'] ),
'description' => $setting_field['description'],
'resolve' => function( $root, $args, AppContext $context, ResolveInfo $info ) use ( $setting_field, $field_key, $key ) {
/**
* Check to see if the user querying the email field has the 'manage_options' capability
* All other options should be public by default
*/
if ( 'admin_email' === $setting_field['key'] ) {
if ( ! current_user_can( 'manage_options' ) ) {
throw new UserError( __( 'Sorry, you do not have permission to view this setting.', 'wp-graphql' ) );
}
}
$option = ! empty( $setting_field['key'] ) ? get_option( $setting_field['key'] ) : null;
switch ( $setting_field['type'] ) {
case 'integer':
$option = absint( $option );
break;
case 'string':
$option = (string) $option;
break;
case 'boolean':
$option = (boolean) $option;
break;
case 'float':
case 'number':
$option = (float) $option;
break;
default:
$option = (string) $option;
}
return $option;
},
];
}
}
/**
* Pass the fields through a filter to allow for hooking in and adjusting the shape
* of the type's schema
*/
self::$fields = self::prepare_fields( $fields, self::$setting_type );
}
return ! empty( self::$fields ) ? self::$fields : null;
}
}