Skip to content

Commit

Permalink
Add gutenberg support.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed Nov 22, 2018
1 parent c014b31 commit 7b1689d
Show file tree
Hide file tree
Showing 17 changed files with 16,593 additions and 191 deletions.
6 changes: 6 additions & 0 deletions .babelrc
@@ -0,0 +1,6 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
10 changes: 10 additions & 0 deletions .eslintrc
@@ -0,0 +1,10 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"browser": true
},
"extends": "wordpress"
}
161 changes: 161 additions & 0 deletions app/Hametuha/WpHamazon/BlockEditor.php
@@ -0,0 +1,161 @@
<?php

namespace Hametuha\WpHamazon;


use Hametuha\WpHamazon\Pattern\Singleton;

/**
* Add Block Editor.
*
* @package hamazon
* @property BootStrap $hamazon
*/
class BlockEditor extends Singleton {

/**
* Constructor
*/
public function __construct() {
add_action( 'init', [ $this, 'register_blocks' ] );
}

/**
* Register blocks.
*/
public function register_blocks() {

// If no service available, skip.
if ( ! $this->hamazon->service_instances ) {
return;
}

wp_register_script( 'hamazon-block', hamazon_asset_url( 'js/editor/hamazon-block.js' ), [ 'wp-element', 'wp-blocks' ], hamazon_info( 'version' ), true );
wp_localize_script( 'hamazon-block', 'HamazonBlock', [
'title' => __( 'Affiliate', 'hamazon' ),
'description' => __( 'Insert affiliater block of WP-Hamazon.', 'hamazon' ),
'buttonLabel' => __( 'Setting', 'hamazon' ),
'closeLabel' => __( 'Cancel' ),
'descPlaceholder' => __( 'Enter additional text(e.g. your opinion) here.', 'hamazon' ),
'services' => $this->hamazon->service_data_for_script(),
'attributes' => $this->get_attributes(),
'shortCodes' => $this->get_types(),
] );
wp_register_style( 'hamazon-block', hamazon_asset_url( 'css/hamazon-block.css' ), [], hamazon_info( 'version' ) );
// Alert Block.
register_block_type( 'hamazon/single', [
'editor_style' => 'hamazon-block',
'editor_script' => 'hamazon-block',
'attributes' => $this->get_attributes(),
'render_callback' => function( $attributes, $content = '' ) {
$attributes = wp_parse_args( $attributes, [
'type' => '',
] );
try {
if ( ! in_array( $attributes['type'], $this->get_types() ) ) {
throw new \Exception( __( 'No affiliate service available.', 'hamazon' ) );
}
if ( 'amazon' === $attributes['type'] ) {
$attributes['asin'] = $attributes['id'];
}
$contents = $content ? strip_tags( $content ) : implode( ' ', $attributes['content'] );
$instance = $this->hamazon->service_instances[ $attributes['type'] ];
$key = '';
foreach ( $this->get_types() as $short_code => $type ) {
if ( $type === $attributes['type'] ) {
$key = $short_code;
}
}
if ( ! $key ) {
throw new \Exception( __( 'Failed to get proper contents.', 'hamazon' ) );
}
$result = $instance->short_code_callback( $key, $attributes, $contents );
if ( is_wp_error( $result ) ) {
throw new \Exception( $result->get_error_message() );
}
return $result;
} catch ( \Exception $e ) {
return sprintf( '<p class="hamazon-block-no-content">%s</p>', esc_html( $e->getMessage() ) );
}
},
] );
}

/**
* Get short code
*
* @param array $attributes
* @param string $content
* @return string
*/
public function render_callback( $attributes = [], $content = '' ) {
return 'string';
}

/**
* Get available short codes.
*
* @return array
*/
protected function get_types() {
$types = [];
foreach ( $this->hamazon->service_instances as $service ) {
foreach ( $service->short_code_setting() as $short_code => $setting ) {
$types[ $short_code ] = $service->name;
}
}
return $types;
}

/**
* Get attributes.
*
* @return array
*/
protected function get_attributes() {
$attributes = [
'type' => [
'type' => 'string',
'default' => '',
],
'content' => [
'type' => 'array',
],
];
foreach ( $this->hamazon->service_instances as $name => $service ) {
foreach ( $service->short_code_setting() as $short_code => $settings ) {
foreach ( $settings as $setting ) {
$key = $setting['attr'];
if ( 'asin' === $key ) {
$key = 'id';
}
if ( ! isset( $attributes[ $key ] ) ) {
$attributes[ $key ] = [
'type' => 'string',
'default' => '',
];
}
}
}
}
return $attributes;
}

/**
* Getter
*
* @param string $name
* @return mixed
*/
public function __get( $name ) {
switch ( $name ) {
case 'hamazon':
return BootStrap::get_instance();
break;
default:
return null;
break;
}

}
}
63 changes: 41 additions & 22 deletions app/Hametuha/WpHamazon/BootStrap.php
Expand Up @@ -20,6 +20,11 @@ class BootStrap extends Singleton {
*/
protected $active_services = [];

/**
* @var AbstractService[]
*/
public $service_instances = [];

/**
* Singleton constructor.
*/
Expand All @@ -45,6 +50,7 @@ protected function __construct() {
$service = $class_name::get_instance();
if ( $service->is_valid() ) {
$this->active_services[ $service->name ] = $service->title;
$this->service_instances[ $service->name ] = $service;
}
}
}
Expand All @@ -61,6 +67,9 @@ protected function __construct() {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_script' ] );
}
$this->backward_compats();

// Fire Gutenberg.
BlockEditor::get_instance();
}

/**
Expand Down Expand Up @@ -113,7 +122,9 @@ function() {
'hamazon_post_types',
__( 'Available Post Types', 'hamazon' ),
function() {
$post_types = get_post_types( [ 'public' => true ], OBJECT );
$post_types = array_filter( get_post_types( [ 'public' => true ], OBJECT ), function( $post_type ) {
return 'attachment' !== $post_type->name;
} );
/**
* hamazon_valid_post_types
*
Expand All @@ -124,7 +135,7 @@ function() {
printf(
'<label class="hamazon-inline-block"><input type="checkbox" name="hamazon_post_types[]" value="%s" %s/> %s</label>',
esc_attr( $post_type->name ),
checked( false !== array_search( $post_type->name, get_option( 'hamazon_post_types', [] ) ), true, false ),
checked( false !== array_search( $post_type->name, (array) get_option( 'hamazon_post_types', [] ) ), true, false ),
esc_html( $post_type->label )
);
}
Expand Down Expand Up @@ -169,8 +180,7 @@ function() {
public function register_assets() {
wp_register_style( 'hamazon-admin', hamazon_asset_url( '/css/hamazon-admin.css' ), [], hamazon_info( 'version' ) );
wp_register_style( 'hamazon-editor', hamazon_asset_url( '/css/hamazon-editor.css' ), [ 'dashicons' ], hamazon_info( 'version' ) );
$editor_js = 'js/editor/hamazon-editor' . ( WP_DEBUG ? '' : '.min' ) . '.js';
wp_register_script( 'hamazon-editor', hamazon_asset_url( $editor_js ), [], hamazon_info( 'version' ), true );
wp_register_script( 'hamazon-editor', hamazon_asset_url( 'js/editor/hamazon-editor.js' ), [], hamazon_info( 'version' ), true );
wp_register_script( 'hamazon-editor-helper', hamazon_asset_url( '/js/editor-helper.js' ), [ 'jquery', 'hamazon-editor' ], hamazon_info( 'version' ), true );
}

Expand Down Expand Up @@ -293,28 +303,38 @@ public function load_hamazon_buttons( $excludes = [] ) {
'previousPage' => __( 'Previous', 'hamazon' ),
'nextPage' => __( 'Next', 'hamazon' ),
'countries' => __( 'Countries', 'hamazon' ),
'services' => array_map( function ( $key, $value ) {
$service = [
'key' => $key,
'label' => $value,
];
/**
* hamazon_service_variables
*
* Add service instance passed to react.
*
* @param mixed $data
* @param string $key
*/
$data = apply_filters( 'hamazon_service_variables', null, $key );
$service[ 'data' ] = $data;
return $service;
}, array_keys( $this->active_services ), array_values( $this->active_services ) ),
'services' => $this->service_data_for_script(),
] );
$did_localized = true;
}
}

/**
* Get service array.
*
* @return array
*/
public function service_data_for_script() {
return array_map( function ( $key, $value ) {
$service = [
'key' => $key,
'label' => $value,
];
/**
* hamazon_service_variables
*
* Add service instance passed to react.
*
* @param mixed $data
* @param string $key
*/
$data = apply_filters( 'hamazon_service_variables', null, $key );
$service['data'] = $data;

return $service;
}, array_keys( $this->active_services ), array_values( $this->active_services ) );
}

/**
* Show media buttons
*
Expand Down Expand Up @@ -375,5 +395,4 @@ public function convert_option() {
}
update_option( 'hamazon_option_updated', current_time( 'timestamp' ) );
}

}
24 changes: 0 additions & 24 deletions app/Hametuha/WpHamazon/Endpoints/AmazonEndpoint.php

This file was deleted.

7 changes: 4 additions & 3 deletions app/Hametuha/WpHamazon/Pattern/AbstractService.php
Expand Up @@ -111,6 +111,7 @@ public function short_code_set_up(){
'attrs' => $setting,
] );
} );
// Add block editor.
}
}

Expand All @@ -122,14 +123,14 @@ public function short_code_set_up(){
* @param string $content
* @return string|\WP_Error
*/
abstract protected function short_code_callback( $short_code_name, array $attributes = [], $content = '' );
abstract public function short_code_callback( $short_code_name, array $attributes = [], $content = '' );

/**
* Return short code settings
*
* @return string
* @return array
*/
abstract protected function short_code_setting();
abstract public function short_code_setting();

/**
* Filter data passed to react.
Expand Down

0 comments on commit 7b1689d

Please sign in to comment.