Skip to content

Commit

Permalink
all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwelcher committed Oct 31, 2024
1 parent f872685 commit 0c24aec
Show file tree
Hide file tree
Showing 25 changed files with 20,876 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ plugins/command-palette-tools
plugins/query-monitor
plugins/wordpress-es-modules-plugin
plugins/interactive-code-block
plugins/advanced-custom-fields
node_modules
vendor
debug.log
.vscode
.DS_Store
languages/*
mu-plugins/*
Expand Down
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"[css][scss][javascript][json][markdown][yaml][typescript][typescriptreact]": {

"editor.formatOnSave": true
},
"phpcbf.enable": true,
"phpcbf.executablePath": "${workspaceFolder}/vendor/bin/phpcbf",
"phpcbf.onsave": false,
"phpcbf.standard": "./phpcs.xml",
"phpcs.enable": true,
"phpcs.executablePath": "./vendor/bin/phpcs",
}
27 changes: 11 additions & 16 deletions plugins/acf-pods-bindings/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,21 @@ function twitch_register_block_bindings() {
)
);


// Block binding for Pods
register_block_bindings_source(
'twitch/pods',
'twitch/excerpt',
array(
'label' => __( 'PODS', 'custom-bindings' ),
'get_value_callback' => 'twitch_user_data_bindings',
'uses_context' => [ 'postId', 'postType' ],
'label' => __( 'Excerpt', 'custom-bindings' ),
'get_value_callback' => 'twitch_excerpt_bindings',
'uses_context' => array( 'postId', 'postType' ),
)
);
}

register_block_bindings_source(
'twitch/acf',
array(
'label' => __( 'Advanced Custom Fields', 'custom-bindings' ),
'get_value_callback' => 'twitch_acf_data_bindings',
'uses_context' => [ 'postId', 'postType' ],
)
);
function twitch_excerpt_bindings( $source_args, $block_instance ) {
$post_id = $block_instance->context['postId'];
$post_type = $block_instance->context['postType'];
$current_post = get_post( $post_id );
return $current_post->post_excerpt;
}


Expand Down Expand Up @@ -98,13 +94,12 @@ function twitch_acf_data_bindings( $source_args, $block_instance ) {
'enqueue_block_editor_assets',
function() {
$assets_file = plugin_dir_path( __FILE__ ) . '/build/index.asset.php';

if ( file_exists( $assets_file ) ) {
$assets = include $assets_file;
wp_enqueue_script(
'script-handle',
plugin_dir_url( __FILE__ ) . '/build/index.js',
$assets['dependencies'],
array_merge( $assets['dependencies'], array( 'wp-edit-site', 'wp-edit-post' ) ),
$assets['version'],
true
);
Expand Down
36 changes: 36 additions & 0 deletions plugins/acf-pods-bindings/src/excerpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Internal dependencies
*/
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
let settings;
export default {
label: __( 'Excerpt' ),
name: 'twitch/excerpt',
getValues( { select, context, bindings } ) {
if ( settings === undefined ) {
apiFetch( { path: '/wp/v2/settings' } ).then( ( res ) => {
console.log( 'calling' );
settings = res;
} );
}

return {
content: settings?.description,
};
// return {
// content:
// select( 'core/editor' ).getEditedPostAttribute( 'excerpt' ),
// };
},

setValues( { select, dispatch, context, bindings } ) {
// dispatch( 'core/editor' ).editPost( {
// excerpt: bindings?.content?.newValue,
// } );
},

canUserEditValue( { select, context } ) {
return true;
},
};
94 changes: 66 additions & 28 deletions plugins/acf-pods-bindings/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,48 @@
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { InspectorControls } from '@wordpress/block-editor';
import { PanelBody, SelectControl } from '@wordpress/components';
import {
InspectorControls,
useBlockBindingsUtils,
} from '@wordpress/block-editor';
import { PanelBody, SelectControl, Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityProp, useEntityRecord } from '@wordpress/core-data';
import { useEntityRecord } from '@wordpress/core-data';
import { useState } from '@wordpress/element';
import { useSelect, dispatch } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';
import {
getBlockBindingsSources,
registerBlockBindingsSource,
} from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { unlock } from './unlock-lock';
import podsBinding from './pods-binding';
import acfBinding from './acf-binding';

const { registerBlockBindingsSource } = unlock( dispatch( blocksStore ) );
import excerpt from './excerpt';

// Register the bindings sources.
// registerBlockBindingsSource( podsBinding );
// registerBlockBindingsSource( acfBinding );

// registerBlockBindingsSource( excerpt );

registerBlockBindingsSource( {
label: __( 'Excerpt' ),
name: 'twitch/excerpt',
getValues( { select, context, bindings } ) {
return {
content:
select( 'core/editor' ).getEditedPostAttribute( 'excerpt' ),
};
},

setValues( { select, dispatch, context, bindings } ) {
dispatch( 'core/editor' ).editPost( {
excerpt: bindings?.content?.newValue,
} );
},

canUserEditValue( { select, context } ) {
return true;
},
} );

/**
*
Expand All @@ -33,17 +55,17 @@ const withBlockBindingsSelector = ( BlockEdit ) => ( props ) => {
context: { postId, postType },
attributes,
setAttributes,
clientId,
} = props;

const { updateBlockBindings, removeAllBlockBindings } =
useBlockBindingsUtils( clientId );

const [ selectedFramework, setSelectedFramework ] = useState(
attributes?.metadata?.bindings?.content?.source
);

const sources = useSelect( ( select ) =>
unlock( select( blocksStore ) ).getAllBlockBindingsSources()
);

console.log( sources );
const sources = getBlockBindingsSources();

const { record, isResolving } = useEntityRecord(
'postType',
Expand All @@ -52,27 +74,43 @@ const withBlockBindingsSelector = ( BlockEdit ) => ( props ) => {
);

// PODS fields.
const podsFields = window.PodsDFV.getFields( postType, postId );
// const podsFields = window.PodsDFV.getFields( postType, postId );

// ACF fields.
const [ acfMeta ] = useEntityProp( 'postType', postType, 'acf', postId );
// // ACF fields.
// const [ acfMeta ] = useEntityProp( 'postType', postType, 'acf', postId );

// Native meta
const [ nativeMeta ] = useEntityProp(
'postType',
postType,
'meta',
postId
);
// // Native meta
// const [ nativeMeta ] = useEntityProp(
// 'postType',
// postType,
// 'meta',
// postId
// );

if ( props.name !== 'core/paragraph' ) {
if ( props.name !== 'core/image' ) {
return <BlockEdit { ...props } />;
}

return (
<>
<InspectorControls>
<PanelBody title={ __( 'Binding selector', 'twitch-streams' ) }>
<Button
variant="primary"
onClick={ () => removeAllBlockBindings() }
>
Remove bindings
</Button>
<Button
variant="primary"
onClick={ () =>
updateBlockBindings( {
alt: undefined,
} )
}
>
Add native binding
</Button>
<SelectControl
label={ __( 'Binding source', 'twitch-streams' ) }
value={ selectedFramework }
Expand Down
18 changes: 18 additions & 0 deletions plugins/block-manifest/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[*.{yml,yaml}]
indent_style = space
indent_size = 2
30 changes: 30 additions & 0 deletions plugins/block-manifest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Coverage directory used by tools like istanbul
coverage

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Output of `npm pack`
*.tgz

# Output of `wp-scripts plugin-zip`
*.zip

# dotenv environment variables file
.env
30 changes: 30 additions & 0 deletions plugins/block-manifest/block-manifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Plugin Name: Block Manifest
* Description: Example block scaffolded with Create Block tool.
* Requires at least: 6.6
* Requires PHP: 7.2
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: block-manifest
*
* @package Twitch
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function twitch_block_manifest_block_init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', 'twitch_block_manifest_block_init' );
Loading

0 comments on commit 0c24aec

Please sign in to comment.