Skip to content

Commit

Permalink
fix #1463
Browse files Browse the repository at this point in the history
  • Loading branch information
inc2734 committed Jan 30, 2025
1 parent 895bce4 commit c55015d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Framework/Controller/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @package snow-monkey
* @author inc2734
* @license GPL-2.0+
* @version 27.5.4
* @version 27.6.0
*/

namespace Framework\Controller;
Expand Down Expand Up @@ -94,7 +94,7 @@ function () {
<?php
settings_fields( self::MENU_SLUG );
submit_button(
esc_html__( 'Retrieve patterns from the pattern library', 'snow-monkey' ),
esc_html__( 'Retrieve data from the pattern / style library', 'snow-monkey' ),
'primary'
);
?>
Expand Down Expand Up @@ -126,6 +126,7 @@ public function _init() {
function ( $option ) {
delete_transient( 'snow-monkey-remote-pattern-categories' );
delete_transient( 'snow-monkey-remote-patterns' );
delete_transient( 'snow-monkey-remote-styles' );

if ( isset( $option['clear-remote-patterns-cache'] ) && '1' === $option['clear-remote-patterns-cache'] ) {
return get_option( self::SETTINGS_NAME );
Expand Down
124 changes: 124 additions & 0 deletions app/setup/remote-block-styles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* @package snow-monkey
* @author inc2734
* @license GPL-2.0+
* @version 27.6.0
*/

use Framework\Controller\Manager;

/**
* Get remote block styles.
*
* @param string $url A URL of the remote styles API.
* @param array $args An array of request arguments.
* @return array
*/
function _snow_monkey_get_remote_block_styles( $url, array $args = array() ) {
global $wp_version;

$response = wp_remote_get(
$url,
array(
'user-agent' => 'WordPress/' . $wp_version,
'timeout' => 30,
'headers' => array_merge(
$args,
array(
'Accept-Encoding' => '',
)
),
)
);

if ( ! $response || is_wp_error( $response ) ) {
return array();
}

$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $response_code ) {
return array();
}

$styles = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $styles ) ) {
return array();
}

foreach ( $styles as $key => $style ) {
$styles[ $key ]['viewportWidth'] = 1440;
}

return $styles;
}

/**
* Register remote block styles.
* This requires a valid license key.
*/
add_action(
'init',
function () {
$request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI' );
if ( ! $request_uri ) {
$request_uri = esc_html( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ); // @phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
if ( $request_uri && false !== strpos( $request_uri, 'wp-json/snow-monkey-license-manager' ) ) {
return;
}

$transient = get_transient( 'snow-monkey-remote-styles' );

if ( false !== $transient ) {
$remote_block_styles = $transient;
} else {
$license_key = Manager::get_option( 'license-key' );
$status = Manager::get_license_status( $license_key );

$url = 'https://snow-monkey.2inc.org/wp-json/snow-monkey-license-manager/v1/styles/';

$remote_block_styles = 'true' === $status
? _snow_monkey_get_remote_block_styles(
$url,
array(
'X-Snow-Monkey-License-key' => Manager::get_option( 'license-key' ),
'X-Snow-Monkey-Locale' => get_locale(),
)
)
: false;

if ( $remote_block_styles && is_array( $remote_block_styles ) ) {
set_transient( 'snow-monkey-remote-styles', $remote_block_styles, DAY_IN_SECONDS );
}
}

if ( ! is_array( $remote_block_styles ) ) {
$remote_block_styles = array();
}

$registry = WP_Block_Styles_Registry::get_instance();

foreach ( $remote_block_styles as $style ) {
if ( ! $style['name'] || ! $style['label'] || ! $style['blockTypes'] ) {
continue;
}

$block_types = array_filter(
array_map(
function ( $block_type ) {
return str_replace( array( 'core-', 'snow-monkey-blocks-' ), array( 'core/', 'snow-monkey-blocks/' ), $block_type );
},
$style['blockTypes']
)
);

if ( in_array( 'core/button', $block_types, true ) ) {
$block_types[] = 'snow-monkey-blocks/btn';
}

register_block_style( array_unique( $block_types ), (array) $style );
}
},
9
);
2 changes: 1 addition & 1 deletion languages/ja.l10n.php

Large diffs are not rendered by default.

Binary file modified languages/ja.mo
Binary file not shown.
26 changes: 13 additions & 13 deletions languages/ja.po
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
msgid ""
msgstr ""
"Project-Id-Version: Snow Monkey\n"
"POT-Creation-Date: 2025-01-06 23:52+0900\n"
"PO-Revision-Date: 2025-01-06 23:53+0900\n"
"POT-Creation-Date: 2025-01-30 10:20+0900\n"
"PO-Revision-Date: 2025-01-30 10:20+0900\n"
"Last-Translator: inc2734 <[email protected]>\n"
"Language-Team: inc2734 <[email protected]>\n"
"Language: ja_JP\n"
Expand Down Expand Up @@ -134,50 +134,50 @@ msgid "Reset settings"
msgstr "設定をリセット"

#: Framework/Controller/Manager.php:97
msgid "Retrieve patterns from the pattern library"
msgstr "パターンライブラリーからパターンを再取得する"
msgid "Retrieve data from the pattern / style library"
msgstr "パターン / スタイルライブラリーからデータを再取得する"

#: Framework/Controller/Manager.php:165
#: Framework/Controller/Manager.php:166
msgid "Settings"
msgstr "設定"

#: Framework/Controller/Manager.php:173
#: Framework/Controller/Manager.php:174
msgid "License key"
msgstr "ライセンスキー"

#: Framework/Controller/Manager.php:179 Framework/Controller/Manager.php:221
#: Framework/Controller/Manager.php:180 Framework/Controller/Manager.php:222
msgid "Enable"
msgstr "有効"

#: Framework/Controller/Manager.php:184 Framework/Controller/Manager.php:226
#: Framework/Controller/Manager.php:185 Framework/Controller/Manager.php:227
msgid "Disable"
msgstr "無効"

#: Framework/Controller/Manager.php:204
#: Framework/Controller/Manager.php:205
msgid ""
"If the license key entered is valid, premium block patterns are available."
msgstr ""
"入力されたライセンスキーが有効な場合、プレミアムブロックパターンが利用できま"
"す。"

#: Framework/Controller/Manager.php:205
#: Framework/Controller/Manager.php:206
msgid ""
"If the license key is entered, the XServer register key setting is not saved."
msgstr "ライセンスキーを入力した場合、XServer 登録キーの設定は保存されません。"

#: Framework/Controller/Manager.php:215
#: Framework/Controller/Manager.php:216
msgid "XServer register key"
msgstr "XServer 登録キー"

#: Framework/Controller/Manager.php:246
#: Framework/Controller/Manager.php:247
msgid ""
"If the XServer register key entered is valid, premium block patterns are "
"available."
msgstr ""
"入力されたXServer 登録キーが有効な場合、プレミアムブロックパターンが使用可能"
"です。"

#: Framework/Controller/Manager.php:247
#: Framework/Controller/Manager.php:248
msgid ""
"If you are using XServer integration, enter the XServer register key in the "
"XServer register key entry field without entering anything in the license "
Expand Down

0 comments on commit c55015d

Please sign in to comment.