Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Pattern Importer Block - Polish up UX and Add Optional Remote Image Download #25

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-i18n', 'wp-plugins'), 'version' => '9165383a9b85136eb360');
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-i18n'), 'version' => '5f9a80da447e053331f7');
7,384 changes: 7,306 additions & 78 deletions build/index.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion build/index.js.map

This file was deleted.

12 changes: 6 additions & 6 deletions build/js/blocks/pattern-importer/block.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"title": "Pattern Inserter",
"apiVersion": 2,
"name": "dlxplugins/gbhx-pattern-inserter",
"category": "generateblocks",
"apiVersion": 3,
"name": "dlxplugins/dlx-pw-pattern-inserter",
"category": "design",
"icon": "",
"description": "Paste in a pattern and it will be inserted for you.",
"keywords": [
"generateblocks",
"remote",
"pattern",
"inserter"
],
"version": "1.0.0",
"textdomain": "gb-hacks",
"textdomain": "dlx-pattern-wrangler",
"attributes": {
"preview": {
"type": "boolean",
Expand All @@ -24,5 +24,5 @@
"preview": true
}
},
"editorScript": "gb-hacks-pattern-inserter-block"
"editorScript": "dlx-pw-pattern-inserter-block"
}
8 changes: 8 additions & 0 deletions pattern-wrangler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public function plugins_loaded() {
$preview = new Preview();
$preview->run();

// Determine if blocks can run or not.
$options = Options::get_options();
$can_disable_blocks = (bool) $options['disablePatternImporterBlock'];
if ( ! $can_disable_blocks ) {
$blocks = new Blocks();
$blocks->run();
}

/**
* When PatternWrangler can be extended.
*
Expand Down
190 changes: 13 additions & 177 deletions php/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,161 +21,26 @@ public static function run() {
$self = new self();
add_action( 'init', array( $self, 'init' ) );
add_action( 'rest_api_init', array( $self, 'init_rest_api' ) );
add_filter( 'block_type_metadata', array( $self, 'add_block_metadata' ), 10, 1 );
add_filter( 'generateblocks_typography_font_family_list', array( $self, 'add_adobe_fonts' ), 10, 1 );
add_filter( 'generateblocks_typography_font_family_list', array( $self, 'add_blocksy_adobe_fonts' ), 10, 1 );
add_filter( 'generateblocks_do_content', array( $self, 'add_post_type_content' ), 10, 2 );
return $self;
}

/**
* Add post type content.
*
* @param string $content Post content.
*/
public function add_post_type_content( $content ) {
global $post;
// Get the ID.
$post_id = $post->ID ?? 0;
// Bail if no ID.
if ( ! $post_id ) {
return $content;
}
$options = Options::get_options();
$enabled_post_types = $options['enabledPostTypes'] ?? array();
$current_post_type = get_post_type( $post_id );

// If post or page, bail.
if ( in_array( $current_post_type, array( 'post', 'page' ), true ) ) {
return $content;
}

if ( in_array( $current_post_type, $enabled_post_types, true ) ) {
$block_element = get_post( $post_id );
if ( $block_element ) {
if ( 'publish' === $block_element->post_status && empty( $block_element->post_password ) ) {
$content .= $block_element->post_content;
}
}
}
return $content;
}


/**
* Add Adobe Fonts to the list of fonts.
*
* @param array $fonts List of fonts.
*/
public function add_adobe_fonts( $fonts ) {
$options = Options::get_options();
if ( ! (bool) $options['enableAdobeFonts'] ) {
return $fonts;
}

// Get the adobe fonts.
$fonts_group = array();
if ( defined( 'CUSTOM_TYPEKIT_FONTS_FILE' ) ) {
$adobe_fonts = get_option( 'custom-typekit-fonts', array() );
if ( isset( $adobe_fonts['custom-typekit-font-details'] ) ) {
foreach ( $adobe_fonts['custom-typekit-font-details'] as $font_name => $font_details ) {
$fonts_group[] = array(
'value' => $font_name,
'label' => $font_name,
);
}
}
}
if ( ! empty( $fonts_group ) ) {
$fonts[] = array(
'label' => __( 'Adobe Fonts', 'gb-hacks' ),
'options' => $fonts_group,
);
}
return $fonts;
}

/**
* Add Adobe Fonts to the list of fonts.
*
* @param array $fonts List of fonts.
*/
public function add_blocksy_adobe_fonts( $fonts ) {
$options = Options::get_options();
if ( ! (bool) $options['enableAdobeFonts'] ) {
return $fonts;
}

// Get blocksy adoobe fonts.
$options = get_option( 'blocksy_ext_adobe_typekit_settings', array() );
$font_families = $options['fonts'] ?? array();

// Add fonts to list.
if ( ! empty( $font_families ) ) {
$fonts_group = array();
foreach ( $font_families as $font_family ) {
$fonts_group[] = array(
'value' => $font_family['slug'],
'label' => $font_family['name'],
);
}
$fonts[] = array(
'label' => __( 'Adobe Fonts', 'gb-hacks' ),
'options' => $fonts_group,
);
}
return $fonts;
}

/**
* Set Paragraph defaults.
*
* @param array $metadata {
* An array of arguments.
*
* @type string $name Block name.
* @type array $attributes Block attributes.
* }
*/
public function add_block_metadata( $metadata ) {
// Check the block type.
if ( 'generateblocks/headline' !== $metadata['name'] ) {
return $metadata;
}

// Add accordion view (collapsed view).
$metadata['attributes']['element']['default'] = 'p';

// Return the metadata.
return $metadata;
}

/**
* Register the rest routes needed.
*/
public function init_rest_api() {
register_rest_route(
'dlxplugins/gb-hacks/v1',
'dlxplugins/pattern-wrangler/v1',
'/process_image',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_add_remote_image' ),
'permission_callback' => array( $this, 'rest_image_sideload_permissions' ),
)
);
register_rest_route(
'dlxplugins/gb-hacks/v1',
'/get_asset_icon_groups',
array(
'methods' => 'GET',
'callback' => array( $this, 'rest_get_gb_groups' ),
'permission_callback' => array( $this, 'rest_image_sideload_permissions' ),
)
);
}

/**
* Process a list of images for a plugin.
* Process a list of images for a pattern.
*
* @param WP_Rest $request REST request.
*/
Expand Down Expand Up @@ -206,7 +71,7 @@ public function rest_add_remote_image( $request ) {
if ( ! $extension ) {
\wp_send_json_error(
array(
'message' => __( 'File extension not found.', 'gb-hacks' ),
'message' => __( 'File extension not found.', 'dlx-pattern-wrangler' ),
),
400
);
Expand All @@ -215,7 +80,7 @@ public function rest_add_remote_image( $request ) {
if ( ! in_array( $extension, $valid_extensions, true ) ) {
\wp_send_json_error(
array(
'message' => __( 'Invalid file extension.', 'gb-hacks' ),
'message' => __( 'Invalid file extension.', 'dlx-pattern-wrangler' ),
),
400
);
Expand Down Expand Up @@ -257,39 +122,12 @@ public function rest_add_remote_image( $request ) {
}
\wp_send_json_error(
array(
'message' => __( 'Invalid image URL.', 'gb-hacks' ),
'message' => __( 'Invalid image URL.', 'dlx-pattern-wrangler' ),
),
400
);
}

/**
* Process a list of images for a plugin.
*
* @param WP_Rest $request REST request.
*/
public function rest_get_gb_groups( $request ) {

// Get existing SVGs.
$existing_svg_assets = get_option( 'generateblocks_svg_icons', array() );

// Loop through and get `group`.
$groups = array();
foreach ( $existing_svg_assets as $svg_asset ) {
$groups[] = $svg_asset['group'];
}

// If the group is empty, then on JS side, prompt for a group name.
$groups = array_unique( $groups );

// Send success.
\wp_send_json_success(
array(
'groups' => $groups,
)
);
}

/**
* Check if user has access to REST API for retrieving and sideloading images.
*/
Expand Down Expand Up @@ -317,25 +155,23 @@ public function init() {
* Register the block editor script with localized vars.
*/
public function register_block_editor_scripts() {
$options = Options::get_options();

$deps = require_once Functions::get_plugin_dir( 'build/index.asset.php' );

wp_register_script(
'gb-hacks-pattern-inserter-block',
'dlx-pw-pattern-inserter-block',
Functions::get_plugin_url( 'build/index.js' ),
array(),
Functions::get_plugin_version(),
$deps['dependencies'],
$deps['version'],
true
);

wp_localize_script(
'gb-hacks-pattern-inserter-block',
'gbHacksPatternInserter',
'dlx-pw-pattern-inserter-block',
'dlxPWPatternInserter',
array(
'restUrl' => rest_url( 'dlxplugins/gb-hacks/v1' ),
'restUrl' => rest_url( 'dlxplugins/pattern-wrangler/v1' ),
'restNonce' => wp_create_nonce( 'wp_rest' ),
'allowedGoogleFonts' => $options['allowedGoogleFonts'] ?? array(),
'defaultHeadlineBlockEnabled' => (bool) $options['enableDefaultHeadlineBlock'] ?? false,
'defaultHeadlineBlockElement' => $options['headlineBlockElement'] ?? '',
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import './js/blocks/plugins/pattern-preview';
import './js/blocks/pattern-importer/index';
Loading
Loading