2018-02-13 19:03:53 +00:00
< ? php
/**
2018-12-04 20:54:14 +00:00
* Plugin Name : WooCommerce Blocks
2018-02-13 19:03:53 +00:00
* Plugin URI : https :// github . com / woocommerce / woocommerce - gutenberg - products - block
2018-12-04 20:54:14 +00:00
* Description : WooCommerce blocks for the Gutenberg editor .
2019-11-19 19:08:55 +00:00
* Version : 2.6 . 0 - dev
2018-02-13 19:03:53 +00:00
* Author : Automattic
* Author URI : https :// woocommerce . com
2018-12-04 14:51:21 +00:00
* Text Domain : woo - gutenberg - products - block
2020-05-12 20:31:49 +00:00
* Requires at least : 5.2
2019-11-11 17:06:03 +00:00
* Requires PHP : 5.6
2020-04-08 10:23:25 +00:00
* WC requires at least : 4.0
2020-02-18 13:51:25 +00:00
* WC tested up to : 4.0
2018-12-11 17:14:02 +00:00
*
* @ package WooCommerce\Blocks
2019-07-01 12:52:44 +00:00
* @ internal This file is only used when running the REST API as a feature plugin .
2018-02-13 19:03:53 +00:00
*/
2019-07-01 12:52:44 +00:00
defined ( 'ABSPATH' ) || exit ;
2018-02-13 19:03:53 +00:00
2020-05-12 20:31:49 +00:00
$minimum_wp_version = '5.2' ;
2019-11-15 12:33:43 +00:00
/**
* Whether notices must be displayed in the current page ( plugins and WooCommerce pages ) .
*
2019-11-19 17:20:22 +00:00
* @ since 2.5 . 0
2019-11-15 12:33:43 +00:00
*/
function should_display_compatibility_notices () {
$current_screen = get_current_screen ();
if ( ! isset ( $current_screen ) ) {
return false ;
}
$is_plugins_page =
property_exists ( $current_screen , 'id' ) &&
'plugins' === $current_screen -> id ;
$is_woocommerce_page =
property_exists ( $current_screen , 'parent_base' ) &&
'woocommerce' === $current_screen -> parent_base ;
return $is_plugins_page || $is_woocommerce_page ;
}
if ( version_compare ( $GLOBALS [ 'wp_version' ], $minimum_wp_version , '<' ) ) {
/**
* Outputs for an admin notice about running WooCommerce Blocks on outdated WordPress .
*
2019-11-19 17:20:22 +00:00
* @ since 2.5 . 0
2019-11-15 12:33:43 +00:00
*/
function woocommerce_blocks_admin_unsupported_wp_notice () {
if ( should_display_compatibility_notices () ) {
?>
< div class = " notice notice-error is-dismissible " >
< p >< ? php esc_html_e ( 'WooCommerce Blocks requires a more recent version of WordPress and has been paused. Please update WordPress to continue enjoying WooCommerce Blocks.' , 'woo-gutenberg-products-block' ); ?> </p>
</ div >
< ? php
}
}
add_action ( 'admin_notices' , 'woocommerce_blocks_admin_unsupported_wp_notice' );
return ;
}
2018-11-13 19:12:32 +00:00
/**
2019-07-01 12:52:44 +00:00
* Autoload packages .
*
* The package autoloader includes version information which prevents classes in this feature plugin
* conflicting with WooCommerce core .
*
* We want to fail gracefully if `composer install` has not been executed yet , so we are checking for the autoloader .
* If the autoloader is not present , let ' s log the failure and display a nice admin notice .
2018-11-13 19:12:32 +00:00
*/
2019-07-01 12:52:44 +00:00
$autoloader = __DIR__ . '/vendor/autoload_packages.php' ;
if ( is_readable ( $autoloader ) ) {
require $autoloader ;
} else {
if ( defined ( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log ( // phpcs:ignore
sprintf (
/* translators: 1: composer command. 2: plugin directory */
esc_html__ ( 'Your installation of the WooCommerce Blocks feature plugin is incomplete. Please run %1$s within the %2$s directory.' , 'woo-gutenberg-products-block' ),
'`composer install`' ,
'`' . esc_html ( str_replace ( ABSPATH , '' , __DIR__ ) ) . '`'
)
);
}
/**
* Outputs an admin notice if composer install has not been ran .
*/
add_action (
'admin_notices' ,
function () {
?>
< div class = " notice notice-error " >
< p >
< ? php
printf (
/* translators: 1: composer command. 2: plugin directory */
esc_html__ ( 'Your installation of the WooCommerce Blocks feature plugin is incomplete. Please run %1$s within the %2$s directory.' , 'woo-gutenberg-products-block' ),
'<code>composer install</code>' ,
'<code>' . esc_html ( str_replace ( ABSPATH , '' , __DIR__ ) ) . '</code>'
);
?>
</ p >
</ div >
< ? php
}
2018-12-14 14:57:11 +00:00
);
2019-07-01 12:52:44 +00:00
return ;
2018-11-13 19:12:32 +00:00
}
2019-07-01 12:52:44 +00:00
2019-11-14 17:19:22 +00:00
add_action ( 'plugins_loaded' , array ( '\Automattic\WooCommerce\Blocks\Package' , 'init' ) );
2019-09-23 18:07:13 +00:00
2019-11-23 16:28:54 +00:00
/**
* Pre - filters script translations for the given file , script handle and text domain .
*
* @ param string | false | null $translations JSON - encoded translation data . Default null .
* @ param string | false $file Path to the translation file to load . False if there isn ' t one .
* @ param string $handle Name of the script to register a translation domain to .
* @ param string $domain The text domain .
* @ return string JSON translations .
*/
function woocommerce_blocks_get_i18n_data_json ( $translations , $file , $handle , $domain ) {
if ( 'woo-gutenberg-products-block' !== $domain ) {
return $translations ;
}
global $wp_scripts ;
if ( ! isset ( $wp_scripts -> registered [ $handle ], $wp_scripts -> registered [ $handle ] -> src ) ) {
return $translations ;
}
$handle_filename = basename ( $wp_scripts -> registered [ $handle ] -> src );
$locale = determine_locale ();
$lang_dir = WP_LANG_DIR . '/plugins' ;
// Translations are always based on the unminified filename.
if ( substr ( $handle_filename , - 7 ) === '.min.js' ) {
$handle_filename = substr ( $handle_filename , 0 , - 7 ) . '.js' ;
}
// WordPress 5.0 uses md5 hashes of file paths to associate translation
// JSON files with the file they should be included for. This is an md5
// of 'packages/woocommerce-blocks/build/FILENAME.js'.
$core_path_md5 = md5 ( 'packages/woocommerce-blocks/build/' . $handle_filename );
$core_json_file = $lang_dir . '/woocommerce-' . $locale . '-' . $core_path_md5 . '.json' ;
$json_translations = is_file ( $core_json_file ) && is_readable ( $core_json_file ) ? file_get_contents ( $core_json_file ) : false ; // phpcs:ignore
if ( ! $json_translations ) {
return $translations ;
}
// Rather than short circuit pre_load_script_translations, we will output
// core translations using an inline script. This will allow us to continue
// to load feature-plugin translations which may exist as well.
$output = <<< JS
( function ( domain , translations ) {
var localeData = translations . locale_data [ domain ] || translations . locale_data . messages ;
localeData [ " " ] . domain = domain ;
wp . i18n . setLocaleData ( localeData , domain );
} )( " { $domain } " , { $json_translations } );
JS ;
printf ( " <script type='text/javascript'> \n %s \n </script> \n " , $output ); // phpcs:ignore
// Finally, short circuit the pre_load_script_translations hook by returning
// the translation JSON from the feature plugin, if it exists so this hook
// does not run again for the current handle.
$path_md5 = md5 ( 'build/' . $handle_filename );
$json_file = $lang_dir . '/' . $domain . '-' . $locale . '-' . $path_md5 . '.json' ;
$translations = is_file ( $json_file ) && is_readable ( $json_file ) ? file_get_contents ( $json_file ) : false ; // phpcs:ignore
if ( $translations ) {
return $translations ;
}
// Return valid empty Jed locale.
return '{ "locale_data": { "messages": { "": {} } } }' ;
}
add_filter ( 'pre_load_script_translations' , 'woocommerce_blocks_get_i18n_data_json' , 10 , 4 );
2020-01-31 14:28:18 +00:00
/**
* Filter translations so we can retrieve translations from Core when the original and the translated
* texts are the same ( which happens when translations are missing ) .
*
* @ param string $translation Translated text based on WC Blocks translations .
* @ param string $text Text to translate .
* @ param string $domain The text domain .
* @ return string WC Blocks translation . In case it ' s the same as $text , Core translation .
*/
function woocommerce_blocks_get_php_translation_from_core ( $translation , $text , $domain ) {
if ( 'woo-gutenberg-products-block' !== $domain ) {
return $translation ;
}
// When translation is the same, that could mean the string is not translated.
// In that case, load it from core.
if ( $translation === $text ) {
return translate ( $text , 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction, WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.TextDomainMismatch
}
return $translation ;
}
add_filter ( 'gettext' , 'woocommerce_blocks_get_php_translation_from_core' , 10 , 3 );