Skip to content

Instantly share code, notes, and snippets.

View jasonbahl's full-sized avatar
:octocat:

Jason Bahl jasonbahl

:octocat:
View GitHub Profile
@jasonbahl
jasonbahl / register-redux-settings
Created January 8, 2021 09:18
Initial support for WPGraphQL for the Redux Framework
$opt_name = 'graphql_demo';
$theme = wp_get_theme(); // For use with some settings. Not necessary.
$args = array(
'display_name' => $theme->get( 'Name' ),
'display_version' => $theme->get( 'Version' ),
'menu_title' => esc_html__( 'Sample Options', 'redux-framework-demo' ),
'customizer' => true,
);
@jasonbahl
jasonbahl / disable-frontent.php
Last active January 9, 2024 13:30
Disable frontend but allow REST, CRON and GraphQL Requests
<?php
add_action( 'parse_request', 'disable_front_end', 99 );
function disable_front_end() {
global $wp;
/**
* If the request is not part of a CRON, REST Request, GraphQL Request or Admin request,
@jasonbahl
jasonbahl / wp-graphql-page-siblings-connection.php
Created July 13, 2023 14:47
Register a page siblings connection in WPGraphQL
add_action( 'graphql_register_types', function() {
register_graphql_connection( [
'fromType' => 'Page',
'toType' => 'Page',
'connectionTypeName' => 'PageSiblings',
'fromFieldName' => 'siblings',
'resolve' => function( $page, $args, $context, $info ) {
$parent = $page->parentDatabaseId ?? null;
@jasonbahl
jasonbahl / order-by-acf-like-count.php
Last active June 13, 2023 16:29
Shows how to add a custom order value to a connection to order by a custom field.
add_filter( 'graphql_PostObjectsConnectionOrderbyEnum_values', function( $values ) {
$values['LIKE_COUNT'] = [
'value' => 'like_count',
'description' => __( 'The number of likes on the post', 'wp-graphql' ),
];
return $values;
} );
@jasonbahl
jasonbahl / faust-automated-persisted-queries-plugin.js
Created October 25, 2022 15:13
Faust.js plugin for adding Automated Persisted Query support. Plays nice with https://github.com/wp-graphql/wp-graphql-smart-cache
import {createPersistedQueryLink} from '@apollo/client/link/persisted-queries';
import {HttpLink} from "@apollo/client";
import {sha256} from 'crypto-hash';
const linkChain = createPersistedQueryLink({ sha256 }).concat(
new HttpLink({ uri: process.env.WPGRAPHQL_URL }),
);
class PersistedQueriesPlugin {
apply({ addFilter }) {
@jasonbahl
jasonbahl / wp-graphql-smart-cache-custom-even-listener.php
Created June 1, 2023 16:33
Listen to custom events and call purge
add_action( 'graphql_cache_invalidation_init', static function( \WPGraphQL\SmartCache\Cache\Invalidation $invalidation ) {
add_action( 'updated_option', static function( $option, $value, $original_value ) use ( $invalidation ) {
// phpcs:ignore
if ( ! isset( $_POST['_acf_screen'] ) || 'options' !== $_POST['_acf_screen'] ) {
return;
}
// phpcs:ignore
add_filter( 'graphql_query_analyzer_graphql_keys', function( $graphql_keys, $return_keys, $skipped_keys, $return_keys_array, $skipped_keys_array ) {
$keys_array = explode( ' ', $return_keys );
if ( empty( $keys_array ) || ! in_array( 'operation:GetPostBySlug', $keys_array, true ) ) {
return $graphql_keys;
}
if ( ( $key = array_search('list:tag', $keys_array ) ) !== false ) {
unset( $keys_array[$key] );
@jasonbahl
jasonbahl / wp-graphql-acf-override-resolver.php
Last active May 11, 2023 15:33
Override a resolver for wp-graphql-acf v0.6.1 and older to prevent orphaned IDs from causing errors.
function _graphql_acf_sanitize_post_object_resolver( $value ) {
if ( ! $value instanceof \WPGraphQL\Model\Post ) {
return $value;
}
if ( 'publish' !== $value->status ) {
return $value;
}
add_action( 'graphql_return_response', function( $filtered_response, $response, $schema, $operation, $query, $variables, $request, $query_id ) {
$errors = [];
if ( ! is_array( $filtered_response ) && ! empty( $filtered_response->errors ) && is_array( $filtered_response->errors ) ) {
$errors = $filtered_response->errors;
} else if ( is_array( $filtered_response ) && ! empty( $filtered_response['errors'] ) && is_array( $filtered_response['errors'] ) ) {
$errors = $filtered_response['errors'];
}
function _graphql_acf_sanitize_flexible_content_resolver( $value, $acf_field, $context, $type_name ) {
if ( ! isset( $value['acf_fc_layout'] ) ) {
return null;
}
$type_registry = $context->type_registry;
$field_type_name = $type_name . '_' . ucfirst( \WPGraphQL\ACF\Config::camel_case( $acf_field['name'] ) );