generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/develop' into feature/pre-publish-checks
# Conflicts: # composer.json
- Loading branch information
Showing
27 changed files
with
692 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Core button block modifications. | ||
* | ||
* @package wp-newsletter-builder | ||
*/ | ||
|
||
namespace WP_Newsletter_Builder; | ||
|
||
/** | ||
* Registers assets so that they can be enqueued through Gutenberg in | ||
* the corresponding context. | ||
*/ | ||
function register_button_scripts(): void { | ||
wp_register_script( | ||
'plugin-newsletter-button', | ||
get_entry_asset_url( 'wp-newsletter-builder-button' ), | ||
get_asset_dependency_array( 'wp-newsletter-builder-button' ), | ||
get_asset_version( 'wp-newsletter-builder-button' ), | ||
true | ||
); | ||
wp_set_script_translations( 'plugin-newsletter-button' ); | ||
} | ||
add_action( 'init', __NAMESPACE__ . '\register_button_scripts' ); | ||
|
||
/** | ||
* Enqueue block editor assets for button. | ||
*/ | ||
function action_enqueue_button_assets(): void { | ||
$post_type = get_edit_post_type(); | ||
if ( ( 'nb_newsletter' !== $post_type ) && ( 'nb_template' !== $post_type ) ) { | ||
return; | ||
} | ||
wp_enqueue_script( 'plugin-newsletter-button' ); | ||
} | ||
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\action_enqueue_button_assets' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import domReady from '@wordpress/dom-ready'; | ||
import { unregisterBlockStyle } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Modifies supports for Button and Buttons blocks. | ||
* | ||
* @param {Object} settings - The original block settings. | ||
* @param {string} name - The name of the block. | ||
* | ||
* @returns {Object} The modified block settings. | ||
*/ | ||
// @ts-ignore | ||
function modifyButtonSupports(settings, name) { | ||
// Bail early if the block does not have supports. | ||
if (!settings?.supports) { | ||
return settings; | ||
} | ||
// Only apply to Button blocks. | ||
if ( | ||
(name === 'core/button') || (name === 'core/buttons') | ||
) { | ||
return { | ||
...settings, | ||
supports: Object.assign(settings.supports, { | ||
anchor: false, | ||
color: { | ||
background: false, | ||
text: false, | ||
}, | ||
customClassName: false, | ||
inserter: false, | ||
layout: false, | ||
shadow: false, | ||
spacing: false, | ||
styles: [], | ||
typography: { | ||
__experimentalFontSize: false, | ||
__experimentalLineHeight: false, | ||
__experimentalLetterSpacing: true, | ||
__experimentalFontFamily: false, | ||
__experimentalFontWeight: false, | ||
__experimentalFontStyle: false, | ||
__experimentalTextTransform: true, | ||
}, | ||
__experimentalBorder: { | ||
color: false, | ||
radius: true, | ||
style: true, | ||
width: false, | ||
__experimentalSkipSerialization: true, | ||
__experimentalDefaultControls: { | ||
color: false, | ||
radius: true, | ||
style: true, | ||
width: false, | ||
}, | ||
}, | ||
}), | ||
}; | ||
} | ||
return settings; | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'wp-newsletter-builder/button', | ||
modifyButtonSupports, | ||
); | ||
|
||
// @ts-ignore | ||
domReady(() => { unregisterBlockStyle('core/button', ['fill', 'outline']); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Core image block modifications. | ||
* | ||
* @package wp-newsletter-builder | ||
*/ | ||
|
||
namespace WP_Newsletter_Builder; | ||
|
||
/** | ||
* Registers assets so that they can be enqueued through Gutenberg in | ||
* the corresponding context. | ||
*/ | ||
function register_image_scripts(): void { | ||
wp_register_script( | ||
'plugin-newsletter-image', | ||
get_entry_asset_url( 'wp-newsletter-builder-image' ), | ||
get_asset_dependency_array( 'wp-newsletter-builder-image' ), | ||
get_asset_version( 'wp-newsletter-builder-image' ), | ||
true | ||
); | ||
wp_set_script_translations( 'plugin-newsletter-image' ); | ||
} | ||
add_action( 'init', __NAMESPACE__ . '\register_image_scripts' ); | ||
|
||
/** | ||
* Enqueue block editor assets for image. | ||
*/ | ||
function action_enqueue_image_assets(): void { | ||
$post_type = get_edit_post_type(); | ||
if ( ( 'nb_newsletter' !== $post_type ) && ( 'nb_template' !== $post_type ) ) { | ||
return; | ||
} | ||
wp_enqueue_script( 'plugin-newsletter-image' ); | ||
} | ||
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\action_enqueue_image_assets' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import domReady from '@wordpress/dom-ready'; | ||
import { unregisterBlockStyle } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Modifies supports for Image block. | ||
* | ||
* @param {Object} settings - The original block settings. | ||
* @param {string} name - The name of the block. | ||
* | ||
* @returns {Object} The modified block settings. | ||
*/ | ||
// @ts-ignore | ||
function modifyImageSupports(settings, name) { | ||
// Bail early if the block does not have supports. | ||
if (!settings?.supports) { | ||
return settings; | ||
} | ||
// Only apply to Image blocks. | ||
if ( | ||
name === 'core/image' | ||
) { | ||
return { | ||
...settings, | ||
attributes: Object.assign(settings.attributes, { | ||
allowResize: { type: 'boolean', default: false }, | ||
}), | ||
supports: Object.assign(settings.supports, { | ||
anchor: false, | ||
align: false, | ||
alignWide: false, | ||
customClassName: false, | ||
filter: { | ||
duotone: false, | ||
}, | ||
__experimentalBorder: { | ||
color: false, | ||
radius: true, | ||
width: false, | ||
__experimentalSkipSerialization: true, | ||
__experimentalDefaultControls: { | ||
color: false, | ||
radius: true, | ||
width: false, | ||
}, | ||
}, | ||
shadow: false, | ||
}), | ||
}; | ||
} | ||
return settings; | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'wp-newsletter-builder/image', | ||
modifyImageSupports, | ||
); | ||
|
||
// @ts-ignore | ||
domReady(() => { unregisterBlockStyle('core/image', ['default', 'rounded']); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "wp-newsletter-builder/button", | ||
"version": "0.1.0", | ||
"title": "Newsletter Button", | ||
"allowedBlocks": ["core/button"], | ||
"category": "design", | ||
"icon": "button", | ||
"description": "Button wrapper that provides email-friendly enhancements", | ||
"textdomain": "button", | ||
"editorScript": "file:index.ts", | ||
"editorStyle": "file:index.css", | ||
"style": [ | ||
"file:style-index.css" | ||
], | ||
"render": "file:render.php", | ||
"attributes": { | ||
"textColor": { | ||
"type": "string", | ||
"default": "#fff" | ||
}, | ||
"bgColor": { | ||
"type": "string", | ||
"default": "#0279af" | ||
}, | ||
"radius": { | ||
"type": "string", | ||
"default": "0" | ||
}, | ||
"btnWidth": { | ||
"type": "string", | ||
"default": "max-content" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { useEffect } from 'react'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useBlockProps, InspectorControls, InnerBlocks } from '@wordpress/block-editor'; | ||
import { ColorPicker, PanelBody } from '@wordpress/components'; | ||
import useInnerBlockAttributes from '@/hooks/useInnerBlocksAttributes'; | ||
|
||
/** | ||
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files. | ||
* Those files can contain any CSS code that gets applied to the editor. | ||
* | ||
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css | ||
*/ | ||
// Uncomment this line if you want to import a CSS file for this block. | ||
// import './index.scss'; | ||
|
||
/** | ||
* The edit function describes the structure of your block in the context of the | ||
* editor. This represents what the editor will render when the block is used. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit | ||
* | ||
* @return {WPElement} Element to render. | ||
*/ | ||
interface EditProps { | ||
attributes: { | ||
bgColor?: string; | ||
btnWidth?: string, | ||
radius?: string, | ||
textColor?: string, | ||
}; | ||
setAttributes: (attributes: {}) => void; | ||
clientId: string, | ||
} | ||
|
||
export default function Edit({ | ||
attributes: { | ||
bgColor = '#0279af', | ||
btnWidth = 'max-content', | ||
radius = '0', | ||
textColor = '#fff', | ||
}, | ||
setAttributes, | ||
clientId, | ||
}: EditProps) { | ||
const TEMPLATE = [['core/button']]; | ||
const innerBlockAttributes = useInnerBlockAttributes(clientId); | ||
const innerBorderRadius = innerBlockAttributes[0]?.style?.border?.radius || '0'; | ||
const innerWidth = innerBlockAttributes[0]?.width; | ||
const buttonStyles = { | ||
backgroundColor: bgColor, | ||
borderRadius: radius, | ||
color: textColor, | ||
margin: '0 auto', | ||
width: btnWidth, | ||
}; | ||
|
||
useEffect(() => { | ||
setAttributes({ | ||
radius: innerBorderRadius, | ||
btnWidth: innerWidth !== undefined ? `${innerWidth}%` : 'max-content', | ||
}); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [innerBorderRadius, innerWidth]); | ||
|
||
return ( | ||
<> | ||
<InspectorControls> | ||
<PanelBody title="Button Color"> | ||
<h3>{__('Background color', 'wp-newsletter-builder')}</h3> | ||
{/* Using ColorPicker instead of ColorPalette to ensure email-friendly values. */} | ||
<ColorPicker | ||
color={bgColor} | ||
onChange={(color) => setAttributes({ bgColor: color })} | ||
/> | ||
<h3>{__('Text color', 'wp-newsletter-builder')}</h3> | ||
<ColorPicker | ||
color={textColor} | ||
onChange={(color) => setAttributes({ textColor: color })} | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<div {...useBlockProps({ style: buttonStyles })}> | ||
<InnerBlocks | ||
// @ts-ignore | ||
template={TEMPLATE} | ||
templateLock="all" | ||
/> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* Block Name: Newsletter Button. | ||
* | ||
* @package wp-newsletter-builder | ||
*/ | ||
|
||
/** | ||
* 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 wp_newsletter_builder_button_block_init() { | ||
// Register the block by passing the location of block.json. | ||
register_block_type( | ||
__DIR__ | ||
); | ||
} | ||
add_action( 'init', 'wp_newsletter_builder_button_block_init' ); |
Oops, something went wrong.