Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/model/class-field-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,45 @@ public function __construct( \NF_Database_Models_Field $field_model ) {
parent::__construct();
}

/**
* Returns a single raw field setting value formatted for GraphQL String fields.
*
* @param string $name Setting name.
*
* @return string|null
*/
public function get_setting_as_string( $name ) {
if ( ! is_string( $name ) || '' === $name ) {
return null;
}

return self::normalize_setting_value( $this->data->get_setting( $name ) );
}

/**
* Normalizes a setting value to a GraphQL String-compatible value.
*
* @param mixed $value Setting value.
*
* @return string|null
*/
private static function normalize_setting_value( $value ) {
if ( null === $value ) {
return null;
}

if ( is_bool( $value ) ) {
return $value ? 'true' : 'false';
}

if ( is_scalar( $value ) ) {
return (string) $value;
}

$json = wp_json_encode( $value );
return false === $json ? null : $json;
}

/**
* Initializes the field resolvers
*
Expand All @@ -61,6 +100,33 @@ protected function init() {
? Relay::toGlobalId( 'FormField', $this->data->get_id() )
: null;
},
'placeholder' => function () {
$placeholder = $this->data->get_setting( 'placeholder' );
return is_scalar( $placeholder ) ? (string) $placeholder : null;
},
'descriptionText' => function () {
$description = $this->data->get_setting( 'desc_text' );

if ( is_scalar( $description ) ) {
return (string) $description;
}

$description = $this->data->get_setting( 'description' );

if ( is_scalar( $description ) ) {
return (string) $description;
}

if ( is_array( $description ) && isset( $description['desc_text'] ) && is_scalar( $description['desc_text'] ) ) {
return (string) $description['desc_text'];
}

return null;
},
'settingsJson' => function () {
$json = wp_json_encode( $this->data->get_settings() );
return false === $json ? null : $json;
},
];

$all_settings = $this->data->get_settings();
Expand Down
27 changes: 27 additions & 0 deletions src/type/interface/class-form-field-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function register( &$type_registry ) {
register_graphql_interface_type(
'FormField',
[
'interfaces' => [ 'Node' ],
'description' => __( 'Action object', 'wp-graphql-ninja-forms' ),
'fields' => [
'id' => [
Expand All @@ -46,6 +47,32 @@ public static function register( &$type_registry ) {
'type' => 'String',
'description' => __( 'Label of the field', 'wp-graphql-ninja-forms' ),
],
'placeholder' => [
'type' => 'String',
'description' => __( 'Placeholder text of the field', 'wp-graphql-ninja-forms' ),
],
'descriptionText' => [
'type' => 'String',
'description' => __( 'Description/help text of the field', 'wp-graphql-ninja-forms' ),
],
'settingsJson' => [
'type' => 'String',
'description' => __( 'All raw field settings encoded as JSON', 'wp-graphql-ninja-forms' ),
],
'setting' => [
'type' => 'String',
'description' => __( 'Returns a raw field setting by key', 'wp-graphql-ninja-forms' ),
'args' => [
'name' => [
'type' => 'String',
'description' => __( 'Raw Ninja Forms setting key (for example: placeholder, desc_text, default)', 'wp-graphql-ninja-forms' ),
],
],
'resolve' => function ( Field_Model $model, array $args ) {
$name = isset( $args['name'] ) ? $args['name'] : '';
return $model->get_setting_as_string( $name );
},
],
'key' => [
'type' => 'String',
'description' => __( 'Key of the field', 'wp-graphql-ninja-forms' ),
Expand Down
33 changes: 30 additions & 3 deletions src/type/object/class-field-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use GraphQL\Error\UserError;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\NinjaForms\Model\Field_Model;

use WPGraphQL\NinjaForms\Utils\NF_Mapper;

Expand All @@ -33,8 +34,8 @@ public static function register() {
$type_name = ucfirst( $field->get_name() ) . 'Field';

$fields = array_merge(
self::get_common_fields(),
NF_Mapper::get_fields( $field->get_settings(), $type_name )
NF_Mapper::get_fields( $field->get_settings(), $type_name ),
self::get_common_fields()
);

register_graphql_object_type(
Expand Down Expand Up @@ -145,6 +146,32 @@ public static function get_common_fields() {
'type' => 'String',
'description' => __( 'Label of the field', 'wp-graphql-ninja-forms' ),
],
'placeholder' => [
'type' => 'String',
'description' => __( 'Placeholder text of the field', 'wp-graphql-ninja-forms' ),
],
'descriptionText' => [
'type' => 'String',
'description' => __( 'Description/help text of the field', 'wp-graphql-ninja-forms' ),
],
'settingsJson' => [
'type' => 'String',
'description' => __( 'All raw field settings encoded as JSON', 'wp-graphql-ninja-forms' ),
],
'setting' => [
'type' => 'String',
'description' => __( 'Returns a raw field setting by key', 'wp-graphql-ninja-forms' ),
'args' => [
'name' => [
'type' => 'String',
'description' => __( 'Raw Ninja Forms setting key (for example: placeholder, desc_text, default)', 'wp-graphql-ninja-forms' ),
],
],
'resolve' => function ( Field_Model $model, array $args ) {
$name = isset( $args['name'] ) ? $args['name'] : '';
return $model->get_setting_as_string( $name );
},
],
'key' => [
'type' => 'String',
'description' => __( 'Key of the field', 'wp-graphql-ninja-forms' ),
Expand Down Expand Up @@ -182,7 +209,7 @@ public static function get_common_fields() {
'description' => __( 'The field is required?', 'wp-graphql-ninja-forms' ),
],
'labelPos' => [
'type' => 'String',
'type' => 'FieldLabelPosEnum',
'description' => __( 'Position of the label', 'wp-graphql-ninja-forms' ),
],
'personallyIdentifiable' => [
Expand Down
29 changes: 25 additions & 4 deletions src/utils/class-nf-mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@
* Class NF_Mapper
*/
class NF_Mapper {
/**
* Normalize a schema description so it won't be interpreted as a callable string by WPGraphQL.
*
* @param mixed $description Raw description value.
*
* @return string
*/
private static function normalize_description( $description ) {
$description = is_scalar( $description ) ? (string) $description : '';

if ( '' === $description ) {
return '';
}

if ( is_callable( $description ) ) {
return 'Ninja Forms setting: ' . $description;
}

return $description;
}
/**
* Return fields from the ninja form settings
*
Expand All @@ -27,6 +47,7 @@ public static function get_fields( array $data, $base_type ) {

foreach ( $data as $setting ) {
$field_name = graphql_format_field_name( $setting['name'] );
$description = self::normalize_description( isset( $setting['label'] ) ? $setting['label'] : $setting['name'] );

if ( empty( $field_name ) ) {
continue;
Expand All @@ -35,7 +56,7 @@ public static function get_fields( array $data, $base_type ) {
if ( 'option-repeater' === $setting['type'] ) {
$fields[ $field_name ] = [
'type' => [ 'list_of' => 'FieldOption' ],
'description' => $setting['name'],
'description' => $description,
];
} elseif ( 'fieldset' === $setting['type'] ) {
$type = $base_type . ucfirst( $field_name );
Expand All @@ -45,15 +66,15 @@ public static function get_fields( array $data, $base_type ) {
register_graphql_object_type(
$type,
[
'description' => $setting['label'],
'description' => $description,
'fields' => self::get_fields( $setting['settings'], $type ),
]
);
}

$fields[ $field_name ] = [
'type' => $type,
'description' => $setting['label'],
'description' => $description,
];
} else {
switch ( $setting['type'] ) {
Expand All @@ -69,7 +90,7 @@ public static function get_fields( array $data, $base_type ) {

$fields[ $field_name ] = [
'type' => $type,
'description' => isset( $setting['label'] ) ? $setting['label'] : $setting['name'],
'description' => $description,
];
}
}
Expand Down
15 changes: 15 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

// autoload.php @generated by Composer

if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
throw new RuntimeException($err);
}

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit24abdfdfdef450ca1376b6314df711a6::getLoader();
Loading