Skip to content

Commit

Permalink
wp-graphql#156 Adding initial mutation files.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughdevore committed Nov 7, 2017
1 parent f8fc877 commit b33d703
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Type/RootMutationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WPGraphQL\Type\PostObject\Mutation\PostObjectDelete;
use WPGraphQL\Type\PostObject\Mutation\PostObjectUpdate;
use WPGraphQL\Type\PostObject\Mutation\TermObjectDelete;
use WPGraphQL\Type\Setting\Mutation\SettingUpdate;
use WPGraphQL\Type\TermObject\Mutation\TermObjectCreate;
use WPGraphQL\Type\TermObject\Mutation\TermObjectUpdate;
use WPGraphQL\Type\User\Mutation\UserCreate;
Expand Down Expand Up @@ -74,6 +75,7 @@ private static function fields() {
$fields = [];
$allowed_post_types = \WPGraphQL::$allowed_post_types;
$allowed_taxonomies = \WPGraphQL::$allowed_taxonomies;
$allowed_setting_types = DataSource::get_allowed_setting_types();

if ( ! empty( $allowed_post_types ) && is_array( $allowed_post_types ) ) {
foreach ( $allowed_post_types as $post_type ) {
Expand Down Expand Up @@ -103,6 +105,22 @@ private static function fields() {
} // End foreach().
} // End if().

/**
* Create the root mutation fields for any setting type in
* the $allowed_setting_types array.
*/
if ( ! empty( $allowed_setting_types ) && is_array( $allowed_setting_types ) ) {
foreach ( $allowed_setting_types as $setting_type ) {
/**
* Sanitize the setting type field name and
* then build the mutation field
*/
$setting_type = str_replace('_', '', strtolower( $setting_type ) );
$fields[ 'update' . $setting_type . 'Settings' ] = SettingUpdate::mutate( $setting_type );

}
}

if ( ! empty( $allowed_taxonomies ) && is_array( $allowed_taxonomies ) ) {
foreach ( $allowed_taxonomies as $taxonomy ) {

Expand Down
117 changes: 117 additions & 0 deletions src/Type/Setting/Mutation/SettingMutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace WPGraphQL\Type\Setting\Mutation;

use WPGraphQL\Types;

/**
* Class SettingMutation
*
* @package WPGraphQL\Type\Setting
*/
class SettingMutation {

/**
* Holds the input fields configuration
*/
private static $input_fields;

/**
* Holds the $setting_type definition
*/
private static $setting_type;

/**
* Holds the $setting_type_array definition which contains
* all of the settings for the given setting_type
*/
private static $setting_type_array;

/**
* @param $setting_type The group setting type
*
* @return mixed|array|null $input_fields
*/
public static function input_fields( $setting_type ) {

if ( ! empty( $setting_type ) ) {

$input_fields = [];

/**
* 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 input fields
*/
self::$setting_type_array = DataSource::get_setting_type_array( $setting_type );

/**
* Loop through the $setting_type_array and build the setting with
* proper fields
*/
foreach ( $setting_type_array as $key => $setting ) {

/**
* 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['show_in_rest']['name'] ) ) {
$individual_setting_key = lcfirst( str_replace( '_', '', ucwords( $setting['show_in_rest']['name'], '_' ) ) );
} else {
$individual_setting_key = lcfirst( str_replace( '_', '', ucwords( $setting['setting'], '_' ) ) );
}

/**
* Only add the field to the root query field if show_in_graphql is true
* and show_in_rest is true or an array of REST args exists
*/
if ( is_array( $setting['show_in_rest'] ) || true === $setting['show_in_rest'] && true === $setting['show_in_graphql'] ) {
/**
* Dynamically build the individual setting and it's fields
* then add it to the fields array
*/
$input_fields[ $individual_setting_key ] = [
'type' => DataSource::resolve_setting_scalar_type( $setting['type'] ),
'description' => $setting['description'],
];

}

}

self::$input_fields = apply_filters( 'graphql_setting_mutation_input_fields', $input_fields );

}

return ( ! empty( self::$input_fields ) ) ? self::input_fields : null;

}

/**
* This prepares the media item for insertion
*
* @param array $input The input for the mutation from the GraphQL request
* @param \WP_Post_Type $post_type_object The post_type_object for the mediaItem (attachment)
* @param string $mutation_name The name of the mutation being performed (create, update, etc.)
* @param mixed $file The mediaItem (attachment) file
*
* @return array $media_item_args
*/
public static function prepare_settings( $input, $setting_type, $mutation_name, $file ) {

/**
* Filter the $insert_post_args
*
* @param array $insert_post_args The array of $input_post_args that will be passed to wp_insert_attachment
* @param array $input The data that was entered as input for the mutation
* @param \WP_Post_Type $post_type_object The post_type_object that the mutation is affecting
* @param string $mutation_type The type of mutation being performed (create, update, delete)
*/
$insert_setting_args = apply_filters( 'graphql_media_item_insert_setting_args', $insert_setting_args, $input, $mutation_name );


return $input;
}


}
70 changes: 70 additions & 0 deletions src/Type/Setting/Mutation/SettingUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace WPGraphQL\Type\Setting\Mutation;

use WPGraphQL\Types;


/**
* Class SettingUpdate
*
* @package WPGraphQL\Type\Setting\Mutation
*/
class SettingUpdate {

/**
* Stores the setting mutation field definition
*
* @var array $mutation
*/
private static $mutation = [];

/**
* Define the update mutation for various setting types
*
* @param $setting_type
*
* @return array|mixed
*/
public static function mutate( $setting_type ) {

if ( empty( self::$mutation ) ) {

/**
* Set the formatted name of the Setting type
*/
$setting_type_name = lcfirst( ucwords( $setting_type ) );

/**
* Set the name of the mutation being performed
*/
$mutation_name = 'update' . ucwords( $setting_type );

self::$mutation[$setting_type_name] = [
'name' => esc_html( $mutation_name ),
'description' => "Updates the {$setting_type_name} setting.",
'inputFields' => \SettingMutation::input_fields( $setting_type ),
'outputFields' => [
$setting_type_name => [
'type' => Types::setting( $setting_type ),
'resolve' => function( $payload ) {
return $payload;
}
],
],
'mutateAndGetPayload' => function( $input ) use ( $setting_type, $mutation_name ) {

$setting_args = \SettingMutation::prepare_settings( $input );

return "hughie";
},
];

return ! empty( self::$mutation[ $setting_type_name ] ) ? self::$mutation[ $setting_type_name ] : null;

}

}


}

0 comments on commit b33d703

Please sign in to comment.