Skip to content

Instantly share code, notes, and snippets.

@jkhaui
Created December 5, 2018 04:27
Show Gist options
  • Save jkhaui/c9c29f3f6a5b16d327e6c777dfebab29 to your computer and use it in GitHub Desktop.
Save jkhaui/c9c29f3f6a5b16d327e6c777dfebab29 to your computer and use it in GitHub Desktop.
<?php
class SubscriptionType extends \WPGraphQL\Type\WPObjectType {
private static $fields;
public function __construct() {
$config = [
'name' => 'SubscriptionType',
'fields' => self::fields(),
'description' => __( 'A list of users\' transaction history and whether or not they have an active subscription.', 'meprSub' ),
];
parent::__construct( $config );
}
protected static function fields() {
if ( null === self::$fields ) {
self::$fields = function() {
$fields = [
'membershipStatus' => [
'type' => 'string',
'description' => __( 'Shows whether or not the user has an active member subscription.', 'meprSub' ),
],
];
return self::prepare_fields( $fields, 'SubscriptionType' );
};
}
return ! empty( self::$fields ) ? self::$fields : null;
}
}
// Custom class to resolve data from MemberPress subcription table.
class MeprGraphQLClass {
private $wpdb, $meprsubstable;
public function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
/* Enter the custom MemberPress subscriptions table || Define here or in get_subs function?
$this->meprsubstable = $wpdb->prefix . "mepr_subscriptions";
*/
}
public static function get_subs() {
$meprsubstable = $wpdb->prefix . "mepr_subscriptions";
// Select all columns in the table
$user = $wpdb->get_results( "SELECT * FROM $table_name" );
// Select the row for each user
foreach ($user as $row) {
$sub_stat = $row->status;
// Return the membership status of each user
return $sub_stat;
}
}
}
add_action( 'graphql_root_query', 'wp_graphql_custom_table_root_query' );
function wp_graphql_custom_table_root_query( $fields ) {
$fields['userActions'] = [
'type' => \WPGraphQL\Types::string(),
'description' => __( 'Returns a random Dad joke', 'meprSub' ),
'resolve' => function() {
return MeprGraphQLCLass::get_subs();
},
];
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment