Skip to content

Instantly share code, notes, and snippets.

View hsimah's full-sized avatar

hsimah

View GitHub Profile
function Enter-Session {
[CmdletBinding()]
Param()
DynamicParam {
$ParameterName = 'ComputerName'
$Attribute = New-Object System.Management.Automation.ParameterAttribute
$Attributes = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$Parameters = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
<?php
use WPGraphQL\Data\DataSource;
class UserQuizType {
private static $entity = null;
/**
* Register UserQuiz fields
@hsimah
hsimah / wpgraphql-metabox.php
Last active December 8, 2018 04:12
WPGraphQL > Meta Box Custom Fields
<?
// Meta Box managed postmeta fields
// CPT
register_graphql_fields( 'Quiz', [
'quizType' => [
'type' => 'String',
'description' => __( 'Quiz type', 'wpgraphql-metabox' ),
'resolve' => function( $quiz_post ) {
@hsimah
hsimah / custom-join.php
Last active January 21, 2019 10:19
Use GraphQL query arguments where data is in custom table
<?php
add_filter( 'posts_clauses', function( $clauses, $query_object ) {
global $wpdb;
$user_id = get_current_user_id();
if ( ! array_key_exists('graphql_args', $query_object->query ) ) {
return $clauses;
}
switch ( $query_object->query['post_type'] ) {
case 'edgeuserquiz':
<?php
/*
* This is an upsert mutation. It will create or update a cpt and a custom table holding additional data
*/
register_graphql_mutation( 'upsertPost', [
'inputFields' => array_merge(
PostObjectCreate::get_input_fields( $post_type ), // use default mutation inputs for this post type
[
'tutorialId' => [
add_filter( 'posts_clauses', function( $clauses, $query_object ) {
global $wpdb;
$user_id = get_current_user_id();
if ( ! array_key_exists('graphql_args', $query_object->query ) ) {
return $clauses;
}
switch ( $query_object->query['post_type'] ) {
case 'tutorial':
$joined = false;
if ( ! array_key_exists( 'where', $query_object->query['graphql_args'] ) ) {
CREATE TABLE `ps_edgeusertut` (
`ID` int(11) unsigned NOT NULL,
`ut_userid` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
`ut_tutorial` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
`ut_faved` tinyint(1) DEFAULT NULL,
`ut_datefaved` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
`ut_unlocked` tinyint(1) DEFAULT NULL,
`ut_dateunlocked` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
`ut_watched` tinyint(1) DEFAULT NULL,
`ut_watch_history` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
@hsimah
hsimah / wpgraphql-rcp-check.php
Last active June 22, 2019 00:44
A hook handler to check whether the current user has appropriate access in Restrict Content Pro to view the post (a tutorial type)
<?php
add_filter( 'graphql_data_is_private', function( $is_private, $model_name, $data ) {
if ( $model_name === 'PostObject' ) {
if ( $data->post_type === 'tutorial' ) {
$user_id = get_current_user_id();
if ( ! rcp_user_can_access( $user_id, $data->ID ) ) return true;
if ( ! rcp_is_active( $user_id ) ) return true;
// TODO update RCP and stop using deprecated rcp_is_active
// if ( ! rcp_user_has_active_membership( $user_id ) ) return true;
# Clean a filename
$Path.Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
# Clean a directory path
$Path.Split([IO.Path]::GetInvalidPathChars()) -join '_'
# Resolves a relative path
# throws an exception if path doesn't exist
$Path | Resolve-Path | Select-Object -ExpandProperty Path