Commit d7b5b38b authored by Christian Wach's avatar Christian Wach
Browse files

Initial commit

parent ccea2846
{
"presets": [
[ "env", {
"modules": false,
"targets": {
"browsers": [
"last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Safari versions",
"last 2 iOS versions",
"last 1 Android version",
"last 1 ChromeAndroid version",
"ie 11"
]
}
} ]
],
"plugins": [
[ "transform-react-jsx", {
"pragma": "wp.element.createElement"
} ],
["transform-object-rest-spread"]
]
}
*~
*.bak
node_modules
# CiviCRM Gutenberg Blocks
CiviCRM Gutenberg Blocks
========================
Provides Gutenberg Editor blocks for CiviCRM Entities.
\ No newline at end of file
The *CiviCRM Gutenberg Blocks* plugin provides Gutenberg Editor blocks for CiviCRM Entities.
#### Notes ####
This plugin requires at least *WordPress 5.0*. It's recommended (but not required) to use the latest release of *CiviCRM*.
It also requires:
* [Gutenberg](http://wordpress.org/plugins/gutenberg/) version 5.0 or greater.
Be aware that this plugin is in early stages of development and is a long way from functional!
#### Known Issues ####
Until the Accessible Autocomplete component is merged into Gutenberg or WordPress core, it looks complicated to create functional blocks.
#### Translation ####
To update the translation files for this plugin, `cd` to the plugin directory, then:
`wp i18n make-pot . languages/civicrm-gutenberg-blocks.pot`
Use a plugin such as [Loco Translate](https://wordpress.org/plugins/loco-translate/) to create the files for your locale, e.g.
* `languages/civicrm-gutenberg-blocks-en_GB.po`
* `languages/civicrm-gutenberg-blocks-en_GB.mo`
Then `cd` to the `languages` directory and create the JSON file with:
`po2json civicrm-gutenberg-blocks-en_GB.po civicrm-gutenberg-blocks-en_GB.json -p --format jed`
#### Installation ####
There are two ways to install:
###### ZIP Download ######
If you have downloaded *CiviCRM Gutenberg Blocks* as a ZIP file from the GitHub repository, do the following to install and activate the plugin and theme:
1. Unzip the .zip file and rename the enclosing folder so that the plugin's files are located directly inside `/wp-content/plugins/civicrm-gutenberg-blocks`
2. Activate the plugin
3. You are done!
###### git clone ######
If you have cloned the repository, it is assumed that you know what you're doing.
This diff is collapsed.
/**
* Block Dependencies.
*
* @since 0.1
*/
import Select from 'react-select';
/**
* Internal Block Libraries.
*
* @since 0.1
*/
const { apiFetch } = wp;
const { __ } = wp.i18n;
const {
//registerStore,
withSelect,
} = wp.data;
const { registerBlockType } = wp.blocks;
const { InspectorControls } = wp.editor;
const {
ServerSideRender,
TextControl,
SelectControl,
ToggleControl,
PanelBody,
} = wp.components;
console.log('here');
/**
* Dynamic actions.
*
* @since 0.1
*/
/*
const actions = {
setUserRoles( userRoles ) {
console.log( `actions userRoles:`, userRoles );
return {
type: 'SET_USER_ROLES',
userRoles,
};
},
getUserRoles( path ) {
console.log( `actions path:`, path );
return {
type: 'GET_USER_ROLES',
path,
};
},
};
*/
/**
* Dynamic data store.
*
* @since 0.1
*/
/*
const store = registerStore(
'civicrm/civicrm-profile-block', {
reducer( state = { userRoles: {} }, action ) {
console.log( `reducer action:`, action );
switch ( action.type ) {
case 'SET_USER_ROLES':
return {
...state,
userRoles: action.userRoles,
};
case 'GET_USER_ROLES':
return action.userRoles;
}
return state;
},
actions,
selectors: {
getUserRoles( state ) {
console.log( `selectors state:`, state );
const { userRoles } = state;
return userRoles;
},
},
resolvers: {
* getUserRoles( state ) {
console.log( `resolvers state:`, state );
const userRoles = apiFetch( { path: '/civicrm/civicrm-profile-block/v1/user-roles/' } )
.then( userRoles => {
return actions.setUserRoles( userRoles );
} )
yield userRoles;
},
},
}
);
*/
/**
* Register block.
*
* @since 0.1
*/
export default registerBlockType(
'civicrm/civicrm-profile-block', {
title: __( 'CiviCRM Profile', 'civicrm-gutenberg-blocks' ),
description: __( 'Demonstration of a CiviCRM Profile block.', 'civicrm-gutenberg-blocks' ),
category: 'widgets',
icon: 'megaphone',
keywords: [
__( 'CiviCRM', 'civicrm-gutenberg-blocks' ),
__( 'Profile', 'civicrm-gutenberg-blocks' ),
],
edit: /* withSelect( ( select ) => {
return {
userRoles: select( 'civicrm/civicrm-profile-block' ).getUserRoles(),
};
} )( */ props => {
const { attributes, userRoles, className, setAttributes } = props;
const { component, gid, mode, hijack } = props.attributes;
console.log( `userRoles:`, userRoles );
const handleSelectChange = ( selectedOption ) => {
setAttributes( { gid: selectedOption.value } );
};
const defaultOptions = [
{ value: '1', label: 'One' },
{ value: '2', label: 'Two' },
{ value: '3', label: 'Three' },
];
return (
<div>
<ServerSideRender
block="civicrm/civicrm-profile-block"
attributes={ attributes }
/>
<InspectorControls>
<PanelBody>
<SelectControl
label={ __( 'Mode', 'civicrm-gutenberg-blocks' ) }
value={ mode }
options={[
{ value: 'test', label: __( 'Test Drive', 'civicrm-gutenberg-blocks' ) },
{ value: 'live', label: __( 'Live Mode', 'civicrm-gutenberg-blocks' ) },
]}
onChange={ mode => setAttributes( { mode } ) }
/>
</PanelBody>
<PanelBody>
<ToggleControl
label={ __( 'Hijack', 'civicrm-gutenberg-blocks' ) }
checked={ hijack }
onChange={ hijack => setAttributes( { hijack } ) }
/>
</PanelBody>
</InspectorControls>
</div>
);
}, // ),
save: props => {
return null;
},
},
);
<?php /*
--------------------------------------------------------------------------------
Plugin Name: CiviCRM Gutenberg Blocks
Plugin URI: https://github.com/christianwach/civicrm-gutenberg-blocks
Description: CiviCRM Blocks for the Gutenberg Editor.
Author: Christian Wach
Version: 0.1
Author URI: http://haystack.co.uk
Text Domain: civicrm-gutenberg-blocks
Domain Path: /languages
--------------------------------------------------------------------------------
*/
// Version.
define( 'CIVICRM_GUTENBERG_BLOCKS_VERSION', '0.1' );
// Store reference to this file.
define( 'CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE', __FILE__ );
// Store URL to this plugin's directory.
if ( ! defined( 'CIVICRM_GUTENBERG_BLOCKS_PLUGIN_URL' ) ) {
define( 'CIVICRM_GUTENBERG_BLOCKS_PLUGIN_URL', plugin_dir_url( CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE ) );
}
// Store PATH to this plugin's directory.
if ( ! defined( 'CIVICRM_GUTENBERG_BLOCKS_PLUGIN_PATH' ) ) {
define( 'CIVICRM_GUTENBERG_BLOCKS_PLUGIN_PATH', plugin_dir_path( CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE ) );
}
/**
* CiviCRM Gutenberg Block class.
*
* A class for encapsulating plugin functionality.
*
* @since 0.1
*/
class CiviCRM_Gutenberg_Blocks {
/**
* Initialise this object.
*
* @since 0.1
*/
public function __construct() {
// Init translation.
$this->translation();
// Init block and shortcode.
add_action( 'init', array( $this, 'block_init' ) );
// Check content.
//add_filter( 'the_content', array( $this, 'the_content' ), 1 );
// Add API endpoint.
add_action( 'rest_api_init', array( $this, 'user_roles' ) );
}
/**
* Register REST API route.
*
* @since 0.1
*/
public function user_roles() {
// Register route.
register_rest_route(
'civicrm/civicrm-profile-block/v1',
'/user-roles',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_user_roles' ),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);
}
/**
* Get the user roles.
*
* @since 0.1
*
* @return array $roles JSON feed of returned objects.
*/
public function get_user_roles() {
global $wp_roles;
$roles = array();
$user_roles = $wp_roles->roles;
foreach ( $user_roles as $key => $role ) {
$roles[] = array(
'value' => $key,
'label' => $role['name'],
);
}
$e = new Exception;
$trace = $e->getTraceAsString();
error_log( print_r( array(
'method' => __METHOD__,
'roles' => $roles,
//'backtrace' => $trace,
), true ) );
// --<
return $roles;
}
/**
* Load translation.
*
* @since 0.1
*/
public function translation() {
load_plugin_textdomain(
'civicrm-gutenberg-blocks', // Unique name.
false, // Deprecated argument.
dirname( plugin_basename( __FILE__ ) ) . '/languages/' // Relative path.
);
}
/**
* Register our block and shortcode.
*
* @since 0.1
*/
public function block_init() {
// Register our block editor script.
wp_register_script(
'civicrm-profile-block',
plugins_url( 'assets/js/civicrm-profile-block.build.js', CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE ),
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-i18n' )
);
// Register our block, and explicitly define the attributes we accept.
register_block_type( 'civicrm/civicrm-profile-block', array(
'attributes' => array(
'component' => array(
'type' => 'string',
'default' => 'profile',
),
'gid' => array(
'type' => 'string',
'default' => '1',
),
'mode' => array(
'type' => 'string',
'enum' => array( 'test', 'live' ),
'default' => 'test',
),
'hijack' => array(
'type' => 'boolean',
'default' => false,
),
),
'editor_script' => 'civicrm-profile-block', // The script name we gave in the wp_register_script() call.
'render_callback' => array( $this, 'block_render' ),
) );
/*
// Pass translations to our JavaScript.
wp_set_script_translations(
'civicrm-profile-block',
'civicrm-gutenberg-blocks',
plugin_dir_path( CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE ) . 'languages'
);
*/
// Encode translations.
$translations = json_encode( gutenberg_get_jed_locale_data( 'civicrm-gutenberg-blocks' ) );
/*
* Pass already loaded translations to our JavaScript.
*
* This happens *before* our JavaScript runs, afterwards it's too late.
*/
wp_add_inline_script(
'civicrm-profile-block',
'wp.i18n.setLocaleData( ' . $translations . ', "civicrm-gutenberg-blocks" );',
'before'
);
// Define our shortcode, too, using the same render function as the block.
//add_shortcode( 'civicrm_block', array( $this, 'block_render' ) );
}
/**
* Our combined block and shortcode renderer.
*
* For more complex shortcodes, this would naturally be a much bigger function, but
* I've kept it brief for the sake of focussing on how to use it for block rendering.
*
* @since 0.1
*
* @param array $attributes The attributes that were set on the block or shortcode.
*/
public function block_render( $attributes = array() ) {
$pairs = array();
foreach( $attributes AS $key => $value ) {
$pairs[] = esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
}
$shortcode = '[civicrm ' . implode( ' ', $pairs ) . ']';
return $shortcode;
}
/**
* Check content for rendering.
*
* @since 0.1
*
* @param str $content The content.
* @return str $content The content.
*/
public function the_content( $content ) {
$e = new Exception;
$trace = $e->getTraceAsString();
error_log( print_r( array(
'method' => __METHOD__,
'content' => $content,
//'backtrace' => $trace,
), true ) );
return $content;
}
} // Class ends.
/**
* Get a reference to this plugin.
*
* @since 0.1
*
* @return CiviCRM_Gutenberg_Blocks $civicrm_gutenberg_blocks The plugin reference.
*/
function civicrm_gutenberg_blocks() {
// Hold the plugin instance in a static variable.
static $civicrm_gutenberg_blocks = false;
// Instantiate plugin if not yet instantiated.
if ( false === $civicrm_gutenberg_blocks ) {
$civicrm_gutenberg_blocks = new CiviCRM_Gutenberg_Blocks();
}
// --<
return $civicrm_gutenberg_blocks;
}
// Init plugin.
add_action( 'plugins_loaded', 'civicrm_gutenberg_blocks' );
{
"domain": "messages",
"locale_data": {
"messages": {
"": {
"domain": "messages",
"plural_forms": "nplurals=2; plural=n != 1;",
"lang": "en_GB"
},
"CiviCRM Gutenberg Blocks": [
"CiviCRM Gutenberg Blocks"
],
"https://github.com/christianwach/civicrm-gutenberg-blocks": [
"https://github.com/christianwach/civicrm-gutenberg-blocks"
],
"CiviCRM Blocks for the Gutenberg Editor.": [
"CiviCRM Blocks for the Gutenberg Editor."
],
"Christian Wach": [
"Christian Wach"
],
"http://haystack.co.uk": [
"http://haystack.co.uk"
],
"CiviCRM Profile": [
"CiviCRM Profile"
],