Commit ce1d286a authored by Kevin Cristiano's avatar Kevin Cristiano 🌎
Browse files

Merge branch 'master' into 'master'

CiviCRM Content Block

See merge request !1
parents d7b5b38b 9c84f121
{
"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"]
]
}
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-server-side-render'), 'version' => '2977fd9b98028bc2e469775fb450c857');
\ No newline at end of file
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=16)}([function(e,t){!function(){e.exports=this.wp.element}()},function(e,t){!function(){e.exports=this.wp.i18n}()},function(e,t){!function(){e.exports=this.wp.components}()},function(e,t,n){var r=n(13),i=n(14),c=n(15);e.exports=function(e){return r(e)||i(e)||c()}},function(e,t,n){var r=n(10),i=n(11),c=n(12);e.exports=function(e,t){return r(e)||i(e,t)||c()}},function(e,t){e.exports=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}},function(e,t){!function(){e.exports=this.wp.blocks}()},function(e,t){!function(){e.exports=this.wp.blockEditor}()},function(e,t){!function(){e.exports=this.wp.serverSideRender}()},function(e,t){function n(){return e.exports=n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},n.apply(this,arguments)}e.exports=n},function(e,t){e.exports=function(e){if(Array.isArray(e))return e}},function(e,t){e.exports=function(e,t){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)){var n=[],r=!0,i=!1,c=void 0;try{for(var o,a=e[Symbol.iterator]();!(r=(o=a.next()).done)&&(n.push(o.value),!t||n.length!==t);r=!0);}catch(e){i=!0,c=e}finally{try{r||null==a.return||a.return()}finally{if(i)throw c}}return n}}},function(e,t){e.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}},function(e,t){e.exports=function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}},function(e,t){e.exports=function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}},function(e,t){e.exports=function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}},function(e,t,n){"use strict";n.r(t);var r=n(0),i=n(1),c=n(6),o=n(7),a=n(8),l=n.n(a),u=n(4),b=n.n(u),s=n(2),f=function(e){var t=e.component,n=e.setAttributes,c=Object(r.useState)([{label:Object(i.__)("User Dashboard","civicrm"),value:"user-dashboard"},{label:Object(i.__)("Profile","civicrm"),value:"profile"},{label:Object(i.__)("Contribution Page","civicrm"),value:"contribution",component:"CiviContribute"},{label:Object(i.__)("Event Page","civicrm"),value:"event",component:"CiviEvent"},{label:Object(i.__)("Petition","civicrm"),value:"petition",component:"CiviCampaign"}]),o=b()(c,2),a=o[0],l=o[1];return Object(r.useEffect)((function(){CRM.api4("Setting","get",{select:["enable_components"]}).then((function(e){var t=e[0];if(t.value&&t.value.length){var n=a.filter((function(e){var n=t.value.includes(e.component);return!e.component||n}));l(n)}}))}),[]),Object(r.createElement)(s.SelectControl,{label:Object(i.__)("Component","civicrm"),value:t,options:a,onChange:function(e){return n({component:e})}})},v=function(e){var t=e.hijack,n=e.setAttributes;return Object(r.createElement)(s.ToggleControl,{label:Object(i.__)("Override page content"),help:Object(i.__)("If you only insert one shortcode, you can choose to override all page content with the content of the shortcode.","civicrm"),checked:t,onChange:function(){return n({hijack:!t})}})},m=n(3),p=n.n(m),O=function(e){var t=e.gid,n=e.setAttributes,c=Object(r.useState)([{label:Object(i.__)("Select a Profile","civicrm"),value:0,disabled:!0}]),o=b()(c,2),a=o[0],l=o[1];return Object(r.useEffect)((function(){CRM.api4("UFGroup","get",{select:["title","group_type"],where:[["is_active","=","1"]]}).then((function(e){if(e||e.length){var t=e.map((function(e){return{label:e.title,value:e.id}}));l([].concat(p()(a),p()(t)))}}))}),[]),Object(r.createElement)(s.SelectControl,{label:Object(i.__)("Profile","civicrm"),value:t,options:a,onChange:function(e){return n({gid:e})}})},j=function(e){var t=e.id,n=e.setAttributes,c=Object(r.useState)([{label:Object(i.__)("Select an Event","civicrm"),value:0,disabled:!0}]),o=b()(c,2),a=o[0],l=o[1];return Object(r.useEffect)((function(){CRM.api4("Event","get",{select:["title","summary","event_type.label","start_date","end_date"],where:[["is_active","=","1"],["is_template","=","0"]]}).then((function(e){if(e||e.length){var t=e.map((function(e){return{label:e.title,value:e.id}}));l([].concat(p()(a),p()(t)))}}))}),[]),Object(r.createElement)(s.SelectControl,{label:Object(i.__)("Event Page","civicrm"),value:t,options:a,onChange:function(e){return n({id:e})}})},d=function(e){var t=e.id,n=e.setAttributes,c=Object(r.useState)([{label:Object(i.__)("Select a Contribution page","civicrm"),value:"0",disabled:!0}]),o=b()(c,2),a=o[0],l=o[1];return Object(r.useEffect)((function(){CRM.api4("ContributionPage","get",{select:["title","intro_text"],where:[["is_active","=","1"]]}).then((function(e){if(e||e.length){var t=e.map((function(e){return{label:e.title,value:e.id}}));l([].concat(p()(a),p()(t)))}}))}),[]),Object(r.createElement)(s.SelectControl,{label:Object(i.__)("Contribution Page","civicrm"),value:t,options:a,onChange:function(e){return n({id:e})}})},_=function(e){var t=e.mode,n=e.setAttributes,c=[{label:Object(i.__)("Create","civicrm"),value:"create"},{label:Object(i.__)("Edit","civicrm"),value:"edit"},{label:Object(i.__)("View","civicrm"),value:"view"},{label:Object(i.__)("Search/Public Directory","civicrm"),value:"search"}];return Object(r.createElement)(s.RadioControl,{label:Object(i.__)("Mode","civicrm"),value:t,options:c,selected:t||"create",onChange:function(e){return n({mode:e})}})},g=function(e){var t=e.mode,n=e.setAttributes,c=[{label:Object(i.__)("Live Mode","civicrm"),value:"live"},{label:Object(i.__)("Test Mode","civicrm"),value:"test"}];return Object(r.createElement)(s.RadioControl,{label:Object(i.__)("Mode","civicrm"),value:t,options:c,selected:t||"live",onChange:function(e){return n({mode:e})}})},y=function(e){var t=e.action,n=e.setAttributes,c=[{label:Object(i.__)("Event Info Page","civicrm"),value:"info"},{label:Object(i.__)("Event Registration Page","civicrm"),value:"register"}];return Object(r.createElement)(s.RadioControl,{label:Object(i.__)("Action","civicrm"),value:t,options:c,selected:t||"info",onChange:function(e){return n({action:e})}})},h=n(9),E=n.n(h),C=n(5),x=n.n(C);function A(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}var w=function(e){var t=e.attribute,n=e.index,c=e.extra_attributes,o=e.setAttributes,a=function(e){var r=p()(c);r[n]=function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?A(Object(n),!0).forEach((function(t){x()(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):A(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}({},t,x()({},e.target.name,e.target.value)),o({extra_attributes:p()(r)})};return Object(r.createElement)(r.Fragment,null,Object(r.createElement)("strong",null,Object(i.__)("Attribute","civicrm")," ",n+1),Object(r.createElement)(s.BaseControl,{label:Object(i.__)("Attribute name","civicrm")},Object(r.createElement)("input",{className:"components-text-control__input",type:"text",name:"name",value:t.name,onChange:a})),Object(r.createElement)(s.BaseControl,{label:Object(i.__)("Attribute value","civicrm")},Object(r.createElement)("input",{className:"components-text-control__input",type:"text",name:"value",value:t.value,onChange:a})),Object(r.createElement)(s.IconButton,{icon:"trash",onClick:function(){return(e=p()(c)).splice(n,1),void o({extra_attributes:p()(e)});var e}},Object(i.__)("Delete attribute","civicrm")," ",n+1),Object(r.createElement)("hr",null))},P=function(e){var t=e.extra_attributes,n=e.setAttributes;return Object(r.createElement)(s.PanelBody,{title:Object(i.__)("Extra attributes","civicrm"),initialOpen:!1},Object(r.createElement)(s.PanelRow,null,Object(r.createElement)(s.IconButton,{icon:"insert",isDefault:!0,isLarge:!0,onClick:function(){n({extra_attributes:[].concat(p()(t),[{name:"",value:""}])})}},Object(i.__)("Add attribute","civicrm"))),t.length?t.map((function(e,i){return Object(r.createElement)(w,E()({key:i},{attribute:e,index:i,extra_attributes:t,setAttributes:n}))})):"")},S=function(e){var t=e.attributes,n=e.setAttributes,c=t.component,a=t.gid,u=t.id,b=t.mode,s=t.action,m=t.hijack,p=t.extra_attributes,h=["live","test"];switch(c){case"profile":E=Object(r.createElement)(O,{gid:a,setAttributes:n}),["create","edit","view","search"].includes(b)||n({mode:"create"});break;case"event":E=Object(r.createElement)(j,{id:u,setAttributes:n}),h.includes(b)||n({mode:"live"});break;case"contribution":E=Object(r.createElement)(d,{id:u,setAttributes:n}),h.includes(b)||n({mode:"live"})}var E,C=Object(r.createElement)(_,{mode:b,setAttributes:n}),x=Object(r.createElement)(g,{mode:b,setAttributes:n}),A=Object(r.createElement)(y,{action:s,setAttributes:n});return Object(r.createElement)(r.Fragment,null,Object(r.createElement)(o.InspectorControls,{key:"inspector"},Object(r.createElement)(f,{component:c,setAttributes:n}),E,"profile"==c?C:"","event"==c?A:"",["event","contribution"].includes(c)?x:"",Object(r.createElement)(v,{hijack:m,setAttributes:n}),Object(r.createElement)(P,{extra_attributes:p,setAttributes:n})),Object(r.createElement)("p",null,Object(i.__)("CiviCRM content block","civicrm")),Object(r.createElement)(l.a,{block:"civicrm/civicrm-content-block",attributes:t}))};Object(c.registerBlockType)("civicrm/civicrm-content-block",{title:Object(i.__)("CiviCRM Content","civicrm"),icon:"megaphone",category:"layout",attributes:{component:{type:"string",default:"profile"},gid:{type:"string",default:"1"},id:{type:"string"},mode:{type:"string",default:"create"},action:{type:"string",default:"info"},hijack:{type:"boolean",default:!1},extra_attributes:{type:"array",default:[]}},edit:function(e){var t=e.attributes,n=e.setAttributes;return Object(r.createElement)(S,{attributes:t,setAttributes:n})},save:function(e){return null}})}]);
\ No newline at end of file
This diff is collapsed.
...@@ -55,71 +55,14 @@ class CiviCRM_Gutenberg_Blocks { ...@@ -55,71 +55,14 @@ class CiviCRM_Gutenberg_Blocks {
// Init block and shortcode. // Init block and shortcode.
add_action( 'init', array( $this, 'block_init' ) ); add_action( 'init', array( $this, 'block_init' ) );
// Check content. // render block preview
//add_filter( 'the_content', array( $this, 'the_content' ), 1 ); add_filter( 'rest_post_dispatch', [ $this, 'render_block_preview' ], 10, 3);
// Add API endpoint. // convert serialised civicrm content blocks to shortcode
add_action( 'rest_api_init', array( $this, 'user_roles' ) ); add_action( 'wp', [ $this, 'prerender' ], 5 );
} // add civicrm core resources
add_action( 'admin_head', [ $this, 'maybe_add_core_resources' ], 20 );
/**
* 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;
} }
...@@ -151,61 +94,60 @@ class CiviCRM_Gutenberg_Blocks { ...@@ -151,61 +94,60 @@ class CiviCRM_Gutenberg_Blocks {
// Register our block editor script. // Register our block editor script.
wp_register_script( wp_register_script(
'civicrm-profile-block', 'civicrm-gutenberg-blocks',
plugins_url( 'assets/js/civicrm-profile-block.build.js', CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE ), plugins_url( 'build/index.js', CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE ),
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-i18n' ) [ 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-i18n' ],
filemtime( CIVICRM_GUTENBERG_BLOCKS_PLUGIN_PATH . 'build/index.js' )
); );
// Register our block, and explicitly define the attributes we accept. // Register our block, and explicitly define the attributes we accept.
register_block_type( 'civicrm/civicrm-profile-block', array( register_block_type( 'civicrm/civicrm-content-block', [
'attributes' => array( 'attributes' => [
'component' => array( 'component' => [
'type' => 'string', 'type' => 'string',
'default' => 'profile', 'default' => 'profile',
), ],
'gid' => array( 'gid' => [
'type' => 'string',
'default' => '1',
],
'id' => [
'type' => 'string', 'type' => 'string',
'default' => '1', 'default' => '1',
), ],
'mode' => array( 'mode' => [
'type' => 'string', 'type' => 'string',
'enum' => array( 'test', 'live' ), 'enum' => [ 'test', 'live', 'create', 'edit', 'view', 'search' ],
'default' => 'test', 'default' => 'live',
), ],
'hijack' => array( 'action' => [
'type' => 'string',
'enum' => [ 'info', 'register' ],
'default' => 'info',
],
'hijack' => [
'type' => 'boolean', 'type' => 'boolean',
'default' => false, 'default' => false,
), ],
), 'extra_attributes' => [
'editor_script' => 'civicrm-profile-block', // The script name we gave in the wp_register_script() call. 'type' => 'array',
'render_callback' => array( $this, 'block_render' ), 'default' => [],
) ); ],
],
/* 'editor_script' => 'civicrm-gutenberg-blocks', // The script name we gave in the wp_register_script() call.
// Pass translations to our JavaScript. 'render_callback' => [ $this, 'block_render' ],
] );
/**
* May be extended to wp_set_script_translations( 'my-handle', 'my-domain',
* plugin_dir_path( MY_PLUGIN ) . 'languages' ) ). For details see
* https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
*/
wp_set_script_translations( wp_set_script_translations(
'civicrm-profile-block', 'civicrm-gutenberg-blocks',
'civicrm-gutenberg-blocks', 'civicrm-gutenberg-blocks',
plugin_dir_path( CIVICRM_GUTENBERG_BLOCKS_PLUGIN_FILE ) . 'languages' 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' ) );
} }
...@@ -221,9 +163,21 @@ class CiviCRM_Gutenberg_Blocks { ...@@ -221,9 +163,21 @@ class CiviCRM_Gutenberg_Blocks {
* *
* @param array $attributes The attributes that were set on the block or shortcode. * @param array $attributes The attributes that were set on the block or shortcode.
*/ */
public function block_render( $attributes = array() ) { public function block_render( $attributes = [] ) {
$attributes = $this->clenup_and_set_default_attributes( $attributes );
$pairs = array(); // add extra attributes
if ( ! empty( $attributes['extra_attributes'] ) ) {
foreach ( $attributes['extra_attributes'] AS $attr ) {
if ( empty( $attr['value'] ) ) continue;
$attributes[$attr['name']] = $attr['value'];
}
}
unset( $attributes['extra_attributes'] );
$pairs = [];
foreach( $attributes AS $key => $value ) { foreach( $attributes AS $key => $value ) {
$pairs[] = esc_attr( $key ) . '="' . esc_attr( $value ) . '"'; $pairs[] = esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
} }
...@@ -237,24 +191,358 @@ class CiviCRM_Gutenberg_Blocks { ...@@ -237,24 +191,358 @@ class CiviCRM_Gutenberg_Blocks {
/** /**
* Check content for rendering. * Filters the block-renderer route response
* and renders the civicrm content block preview.
*
* @uses 'rest_post_dispatch' filter
*
* @since 1.0
*
* @param WP_REST_Response $response
* @param WP_REST_Server $server
* @param WP_REST_Request $request
* @return WP_REST_Response $response
*/
public function render_block_preview( $response, WP_REST_Server $server, WP_REST_Request $request ) {
if ( ! strpos( $request->get_route(), 'civicrm/civicrm-content-block' ) ) {
return $response;
}
$response->set_data( [
'rendered' => $this->get_block_preview( $request->get_param( 'attributes' ) )
] );
return $response;
}
/**
* Retreives CiviCRM's content block html preview.
*
* @since 1.0
*
* @param array $atts The block attributes
* @return string $html
*/
public function get_block_preview( array $atts ) {
switch ( $atts['component'] ) {
case 'profile':
return $this->render_profile( $atts );
break;
case 'contribution':
return $this->render_contribution_page( $atts );
break;
case 'event':
return $this->render_event_page( $atts );
break;
case 'user-dashboard':
return $this->render_user_dashboard( $atts );
break;
}
}
/**
* Renders a profile's html.
*
* @since 0.1
*
* @param array $atts The block attributes
* @return string $html
*/
public function render_profile( array $atts ) {
if ( empty( $atts['gid'] ) ) return '';
$args = [
'civicrm',
'profile',
$atts['mode']
];
$_REQUEST['gid'] = $atts['gid'];
ob_start();
( new CRM_Profile_Page_Router )->run( $args );
return ob_get_clean();
}
/**
* Renders a contribution's page html.
*
* @since 0.1
*
* @param array $atts The block attributes
* @return string $html
*/
public function render_contribution_page( array $atts ) {
if ( empty( $atts['id'] ) ) return '';
$_REQUEST['id'] = $atts['id'];
ob_start();
( new CRM_Contribute_Controller_Contribution(
'CiviContribute',
CRM_Core_Action::ADD
) )->run();
return ob_get_clean();
}
/**
* Renders an event's page html.
* *
* @since 0.1 * @since 0.1
* *
* @param str $content The content. * @param array $atts The block attributes
* @return str $content The content. * @return string $content
*/ */
public function the_content( $content ) { public function render_event_page( array $atts ) {
$e = new Exception; if ( empty( $atts['id'] ) ) return '';
$trace = $e->getTraceAsString();
error_log( print_r( array(
'method' => __METHOD__,
'content' => $content,
//'backtrace' => $trace,
), true ) );
return $content; $_REQUEST['id'] = $atts['id'];
ob_start();
if ( $atts['action'] == 'register' ) {
( new CRM_Event_Controller_Registration() )->run();
} else {
( new CRM_Event_Page_EventInfo )->run();
}
return ob_get_clean();
}
/**
* Renders the user dashboard html.
*
* @since 0.1
*
* @param array $atts The block attributes
* @return string $html
*/
public function render_user_dashboard( array $atts ) {
ob_start();
( new CRM_Contact_Page_View_UserDashBoard() )->run();
return ob_get_clean();
}
/**
* Determine if a CiviCRM block is present
* in any of the posts about to be displayed
* and replace the serialised block string
* with it's shortcode version.
*
* Based on CiviCRM_For_WordPress_Shortcodes::prerender().
*
* @uses 'wp' action
*
* @since 0.1
*
* @return void
*/
public function prerender() {
if ( is_404() ) return;
if ( have_posts() ) {
while ( have_posts() ) : the_post();
global $post;
$post = $this->convert_serialised_block_to_shortcode( $post );
endwhile;
}
// reset loop
rewind_posts();
}
/**
* Replaces all serslised content block
* strings with their shorcode string
* in post_content.
*
* @since 0.1
*
* @param WP_Post $post
* @return WP_Post $post
*/
public function convert_serialised_block_to_shortcode( $post ) {
if ( is_null( $post ) || ! $post instanceof WP_Post ) return $post;
if ( ! has_block( 'civicrm/civicrm-content-block', $post ) ) return $post;
// get blocks
$blocks = parse_blocks( $post->post_content );
// get all civicrm content blocks
$civicrm_content_blocks = array_filter(
$blocks,
function ( $block ) {
return $block['blockName'] == 'civicrm/civicrm-content-block';
}
);
// reset keys
$civicrm_content_blocks = array_reduce(
$civicrm_content_blocks,
function( $list, $block ) {
$list[] = $block;
return $list;
},
[]
);
// get all serialised content block strings
preg_match_all(
'/<!-- wp:civicrm\/civicrm-content-block.*\/-->/',
$post->post_content,
$serialised_content_blocks
);
// bail if no matches
if ( empty( $serialised_content_blocks[0] ) ) return $post;
// replace serialised content block with shortcode
array_map(
function( $key, $block ) use ( &$post, $serialised_content_blocks ) {
$post->post_content = str_replace(
$serialised_content_blocks[0][$key],
$this->block_render( $block['attrs'] ), // render with defaults
$post->post_content
);
},
array_keys( $civicrm_content_blocks ),
$civicrm_content_blocks
);
return $post;
}
/**
* Cleans up the attributes
* and sets default values.
*
* We need to set defaults because
* default values for attributes aren't
* converted into html comments.
*
* @see https://github.com/WordPress/gutenberg/issues/7342
*
* @since 0.1
*
* @param array $atts The block attributes
* @param array $attributes The block attributes
*/
public function clenup_and_set_default_attributes( array $atts = [] ) {
$attributes = array_merge(
[
'component' => 'profile',