array( 'list_of' => 'String' ), 'resolve' => __NAMESPACE__ . '\\templates_resolver', ) ); } /** * Resolver for getting the templates that will be loaded on a given node * * @param array $root GraphQL Root Object. * @param array $args Args passed to query. * @param AppContext $context The context of the query to pass along. * @param ResolveInfo $info The ResolveInfo object. * * @return array|string[] */ function templates_resolver( $root, $args, AppContext $context, ResolveInfo $info ) { global $wp_query; global $faustwp_checked_templates; $faustwp_checked_templates = array(); // Loop through each of the template conditionals, and find the appropriate template file. foreach ( template_hierarchy_types() as $type ) { add_filter( "{$type}_template_hierarchy", __NAMESPACE__ . '\\log_template_hierarchy' ); } $template = false; /** * Force $wp_query to initialize as WP GraphQL does not always parse the query. This is important for getting * all of the templates for the site root. */ $wp_query->parse_query(); foreach ( get_conditional_tags() as $tag => $tag_info ) { if ( empty( $tag_info['template_getter'] ) ) { continue; } $template_getter = $tag_info['template_getter']; if ( call_user_func( $tag ) ) { $template = call_user_func( $template_getter ); } if ( $template ) { if ( 'is_attachment' === $tag ) { remove_filter( 'the_content', 'prepend_attachment' ); } break; } } foreach ( template_hierarchy_types() as $type ) { remove_filter( "{$type}_template_hierarchy", __NAMESPACE__ . '\\log_template_hierarchy' ); } /* Strip PHP extension from the checked templates */ $faustwp_checked_templates = array_map( function ( $template ) { return basename( $template, '.php' ); }, $faustwp_checked_templates ); /** * Add index as the default template. */ return array_unique( array_merge( $faustwp_checked_templates, array( 'index' ) ) ); } /** * Callback to log the templates that will be located by WordPress into a global variable. * * @param string[] $templates Templates being loaded. * * @return array */ function log_template_hierarchy( $templates ) { global $faustwp_checked_templates; $faustwp_checked_templates = array_merge( $faustwp_checked_templates, $templates ); return $faustwp_checked_templates; } add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_conditional_tags_field' ); /** * Adds ConditionalTags type and registers it as a field on the UniformResourceIdentifiable type. This type handles * evaluating the supported conditional tags and providing it over GraphQL for a given URI. * * @return void * @uses register_graphql_field * @uses WPE\FaustWP\GraphQL\get_conditional_tags * * @uses register_graphql_object_type * * @deprecated */ function register_conditional_tags_field() { register_graphql_object_type( 'ConditionalTags', array( 'description' => __( 'GraphQL representation of WordPress Conditional Tags.', 'faustwp' ), 'fields' => array_map( function ( $tag ) { return array( 'type' => 'Boolean', 'deprecationReason' => __( 'Deprecated in favor of using Next.js pages', 'faustwp' ), 'description' => $tag['description'], ); }, array_combine( array_map( '\WPE\FaustWP\Utilities\camelcase', array_keys( get_conditional_tags() ) ), array_values( get_conditional_tags() ) ) ), ) ); register_graphql_field( 'UniformResourceIdentifiable', 'conditionalTags', array( 'type' => 'ConditionalTags', 'deprecationReason' => __( 'Deprecated in favor of using Next.js pages', 'faustwp' ), 'resolve' => __NAMESPACE__ . '\\conditional_tags_resolver', ) ); } /** * Resolver for conditionalTags field. * * @param array $root GraphQL Root Object. * @param array $args Args passed to query. * @param AppContext $context The context of the query to pass along. * @param ResolveInfo $info The ResolveInfo object. * * @return array */ function conditional_tags_resolver( $root, $args, AppContext $context, ResolveInfo $info ) { $conditional_tag_values = array(); foreach ( get_conditional_tags() as $tag => $tag_info ) { $conditional_tag_values[ \WPE\FaustWP\Utilities\camelcase( $tag ) ] = $tag(); } return $conditional_tag_values; } add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_generate_ac_mutation' ); /** * Register a mutation for exchanging a username/email and password for a short-lived authorization code. * * @return void */ function register_generate_ac_mutation() { register_graphql_mutation( 'generateAuthorizationCode', array( 'inputFields' => array( 'username' => array( 'type' => 'String', 'description' => __( 'Username for WordPress user', 'faustwp' ), ), 'email' => array( 'type' => 'String', 'description' => __( 'Email for WordPress user', 'faustwp' ), ), 'password' => array( 'type' => 'String', 'description' => __( 'Password for WordPress user', 'faustwp' ), ), ), 'outputFields' => array( 'code' => array( 'type' => 'String', 'description' => __( 'Authorization code used for requesting refresh/access tokens', 'faustwp' ), ), 'error' => array( 'type' => 'String', 'description' => __( 'Error encountered during user authentication, if any', 'faustwp' ), ), ), 'mutateAndGetPayload' => function( $input, $context, $info ) { $is_email = isset( $input['email'] ) ? true : false; $username = isset( $input['username'] ) ? $input['username'] : null; $email = isset( $input['email'] ) ? $input['email'] : null; $password = isset( $input['password'] ) ? $input['password'] : null; if ( $is_email ) { $authenticate = wp_authenticate_email_password( null, $email, $password ); $user = get_user_by( 'email', $email ); } else { $authenticate = wp_authenticate_username_password( null, $username, $password ); $user = get_user_by( 'login', $username ); } if ( is_wp_error( $authenticate ) ) { return array( 'code' => null, 'error' => $authenticate->get_error_message(), ); } // Generate an authorization code that expires in 1 minute. $code = generate_authorization_code( $user, MINUTE_IN_SECONDS * 1 ); return array( 'code' => $code, 'error' => null, ); }, ) ); }