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 .
2023-04-10 11:40:52 +00:00
* Version : 10.0 . 0
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
2023-03-07 08:49:26 +00:00
* Requires at least : 6.1
2023-01-12 11:57:53 +00:00
* Requires PHP : 7.2
2023-03-14 10:53:18 +00:00
* WC requires at least : 7.4
* WC tested up to : 7.5
2018-12-11 17:14:02 +00:00
*
* @ package WooCommerce\Blocks
2020-06-20 12:21:46 +00:00
* @ internal This file is only used when running as a feature plugin .
2018-02-13 19:03:53 +00:00
*/
2022-10-12 10:03:38 +00:00
2022-09-09 11:33:15 +00:00
defined ( 'ABSPATH' ) || exit ;
2018-02-13 19:03:53 +00:00
2023-03-07 08:49:26 +00:00
$minimum_wp_version = '6.1' ;
2019-11-15 12:33:43 +00:00
2022-09-09 11:33:15 +00:00
if ( ! defined ( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) ) {
define ( 'WC_BLOCKS_IS_FEATURE_PLUGIN' , true );
2020-06-05 19:13:51 +00:00
}
2022-10-10 12:21:07 +00:00
2022-10-12 10:03:38 +00:00
// Declare compatibility with custom order tables for WooCommerce.
2022-10-13 14:49:02 +00:00
add_action (
'before_woocommerce_init' ,
function () {
if ( class_exists ( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
2022-10-12 10:03:38 +00:00
\Automattic\WooCommerce\Utilities\FeaturesUtil :: declare_compatibility ( 'custom_order_tables' , __FILE__ , true );
}
2022-10-13 14:49:02 +00:00
}
);
2022-10-10 12:21:07 +00:00
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
*/
2022-09-09 11:33:15 +00:00
function should_display_compatibility_notices () {
2019-11-15 12:33:43 +00:00
$current_screen = get_current_screen ();
2022-09-09 11:33:15 +00:00
if ( ! isset ( $current_screen ) ) {
2019-11-15 12:33:43 +00:00
return false ;
}
$is_plugins_page =
2022-09-09 11:33:15 +00:00
property_exists ( $current_screen , 'id' ) &&
2019-11-15 12:33:43 +00:00
'plugins' === $current_screen -> id ;
$is_woocommerce_page =
2022-09-09 11:33:15 +00:00
property_exists ( $current_screen , 'parent_base' ) &&
2019-11-15 12:33:43 +00:00
'woocommerce' === $current_screen -> parent_base ;
return $is_plugins_page || $is_woocommerce_page ;
}
2022-09-09 11:33:15 +00:00
if ( version_compare ( $GLOBALS [ 'wp_version' ], $minimum_wp_version , '<' ) ) {
2019-11-15 12:33:43 +00:00
/**
* 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
*/
2022-09-09 11:33:15 +00:00
function woocommerce_blocks_admin_unsupported_wp_notice () {
if ( should_display_compatibility_notices () ) {
?>
2021-07-29 14:52:34 +00:00
< div class = " notice notice-error " >
2022-09-09 11:33:15 +00:00
< p >< ? php esc_html_e ( 'The WooCommerce Blocks feature plugin requires a more recent version of WordPress and has been paused. Please update WordPress to continue enjoying WooCommerce Blocks.' , 'woo-gutenberg-products-block' ); ?> </p>
2019-11-15 12:33:43 +00:00
</ div >
2022-09-09 11:33:15 +00:00
< ? php
2019-11-15 12:33:43 +00:00
}
}
2022-09-09 11:33:15 +00:00
add_action ( 'admin_notices' , 'woocommerce_blocks_admin_unsupported_wp_notice' );
2019-11-15 12:33:43 +00:00
return ;
}
2020-08-03 10:11:46 +00:00
/**
* Returns whether the current version is a development version
* Note this relies on composer . json version , not plugin version .
* Development installs of the plugin don ' t have a version defined in
* composer json .
*
* @ return bool True means the current version is a development version .
*/
2022-09-09 11:33:15 +00:00
function woocommerce_blocks_is_development_version () {
2020-08-03 10:11:46 +00:00
$composer_file = __DIR__ . '/composer.json' ;
2022-09-09 11:33:15 +00:00
if ( ! is_readable ( $composer_file ) ) {
2020-08-03 10:11:46 +00:00
return false ;
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- including local file
2022-09-09 11:33:15 +00:00
$composer_config = json_decode ( file_get_contents ( $composer_file ), true );
return ! isset ( $composer_config [ 'version' ] );
2020-08-03 10:11:46 +00:00
}
/**
* If development version is detected and the Jetpack constant is not defined , show a notice .
*/
2023-01-27 15:52:39 +00:00
if ( woocommerce_blocks_is_development_version () && ( ! defined ( 'JETPACK_AUTOLOAD_DEV' ) || true !== JETPACK_AUTOLOAD_DEV ) ) {
2020-08-03 10:11:46 +00:00
add_action (
'admin_notices' ,
2022-09-02 17:20:01 +00:00
function () {
2020-08-03 10:11:46 +00:00
echo '<div class="error"><p>' ;
printf (
2021-02-19 11:58:44 +00:00
/* translators: %1$s is referring to a php constant name, %2$s is referring to the wp-config.php file. */
2022-09-09 11:33:15 +00:00
esc_html__ ( 'WooCommerce Blocks development mode requires the %1$s constant to be defined and true in your %2$s file. Otherwise you are loading the blocks package from WooCommerce core.' , 'woo-gutenberg-products-block' ),
2020-08-03 10:11:46 +00:00
'JETPACK_AUTOLOAD_DEV' ,
'wp-config.php'
);
echo '</p></div>' ;
}
);
}
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' ;
2022-09-09 11:33:15 +00:00
if ( is_readable ( $autoloader ) ) {
2019-07-01 12:52:44 +00:00
require $autoloader ;
} else {
2022-09-09 11:33:15 +00:00
if ( defined ( 'WP_DEBUG' ) && WP_DEBUG ) {
2019-07-01 12:52:44 +00:00
error_log ( // phpcs:ignore
sprintf (
/* translators: 1: composer command. 2: plugin directory */
2022-09-09 11:33:15 +00:00
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' ),
2019-07-01 12:52:44 +00:00
'`composer install`' ,
2022-09-09 11:33:15 +00:00
'`' . esc_html ( str_replace ( ABSPATH , '' , __DIR__ ) ) . '`'
2019-07-01 12:52:44 +00:00
)
);
}
/**
* Outputs an admin notice if composer install has not been ran .
*/
add_action (
'admin_notices' ,
2022-09-02 17:20:01 +00:00
function () {
2022-09-09 11:33:15 +00:00
?>
2022-09-02 17:20:01 +00:00
< div class = " notice notice-error " >
< p >
< ? php
printf (
/* translators: 1: composer command. 2: plugin directory */
2022-09-09 11:33:15 +00:00
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' ),
2022-09-02 17:20:01 +00:00
'<code>composer install</code>' ,
2022-09-09 11:33:15 +00:00
'<code>' . esc_html ( str_replace ( ABSPATH , '' , __DIR__ ) ) . '</code>'
2022-09-02 17:20:01 +00:00
);
?>
</ p >
</ div >
2022-09-09 11:33:15 +00:00
< ? php
2019-07-01 12:52:44 +00:00
}
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
2022-09-09 11:33:15 +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
/**
2022-05-30 16:00:40 +00:00
* WordPress will look for translation in the following order :
* - wp - content / plugins / woocommerce - blocks / languages / woo - gutenberg - products - block - { locale } - { handle } . json
* - wp - content / plugins / woocommerce - blocks / languages / woo - gutenberg - products - block - { locale } - { md5 - handle } . json
* - wp - content / languages / plugins / woo - gutenberg - products - block - { locale } - { md5 - handle } . json
2019-11-23 16:28:54 +00:00
*
2022-05-30 16:00:40 +00:00
* We check if the last one exists , and if it doesn ' t we try to load the
* corresponding JSON file from the WC Core .
*
* @ 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 | false Path to the translation file to load . False if there isn ' t one .
2019-11-23 16:28:54 +00:00
*/
2023-03-09 06:26:56 +00:00
function load_woocommerce_core_js_translation ( $file , $handle , $domain ) {
2022-09-09 11:33:15 +00:00
if ( 'woo-gutenberg-products-block' !== $domain ) {
2022-05-30 16:00:40 +00:00
return $file ;
}
$lang_dir = WP_LANG_DIR . '/plugins' ;
/**
* We only care about the translation file of the feature plugin in the
* wp - content / languages folder .
*/
2022-09-09 11:33:15 +00:00
if ( false === strpos ( $file , $lang_dir ) ) {
2022-05-30 16:00:40 +00:00
return $file ;
}
// If the translation file for feature plugin exist, use it.
2022-09-09 11:33:15 +00:00
if ( is_readable ( $file ) ) {
2022-05-30 16:00:40 +00:00
return $file ;
2019-11-23 16:28:54 +00:00
}
global $wp_scripts ;
2022-09-09 11:33:15 +00:00
if ( ! isset ( $wp_scripts -> registered [ $handle ], $wp_scripts -> registered [ $handle ] -> src ) ) {
2022-05-30 16:00:40 +00:00
return $file ;
2019-11-23 16:28:54 +00:00
}
2022-09-09 11:33:15 +00:00
$handle_src = explode ( '/build/' , $wp_scripts -> registered [ $handle ] -> src );
2022-02-24 11:37:11 +00:00
$handle_filename = $handle_src [ 1 ];
2019-11-23 16:28:54 +00:00
$locale = determine_locale ();
$lang_dir = WP_LANG_DIR . '/plugins' ;
// Translations are always based on the unminified filename.
2022-09-09 11:33:15 +00:00
if ( substr ( $handle_filename , - 7 ) === '.min.js' ) {
$handle_filename = substr ( $handle_filename , 0 , - 7 ) . '.js' ;
2019-11-23 16:28:54 +00:00
}
2022-09-09 11:33:15 +00:00
$core_path_md5 = md5 ( 'packages/woocommerce-blocks/build/' . $handle_filename );
2019-11-23 16:28:54 +00:00
2022-05-30 16:00:40 +00:00
/**
* Return file path of the corresponding translation file in the WC Core is
* enough because `load_script_translations()` will check for its existence
* before loading it .
*/
return $lang_dir . '/woocommerce-' . $locale . '-' . $core_path_md5 . '.json' ;
2019-11-23 16:28:54 +00:00
}
2023-03-09 06:26:56 +00:00
add_filter ( 'load_script_translation_file' , 'load_woocommerce_core_js_translation' , 10 , 3 );
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 .
*/
2022-09-09 11:33:15 +00:00
function woocommerce_blocks_get_php_translation_from_core ( $translation , $text , $domain ) {
if ( 'woo-gutenberg-products-block' !== $domain ) {
2020-01-31 14:28:18 +00:00
return $translation ;
}
// When translation is the same, that could mean the string is not translated.
// In that case, load it from core.
2022-09-09 11:33:15 +00:00
if ( $translation === $text ) {
return translate ( $text , 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction, WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.TextDomainMismatch
2020-01-31 14:28:18 +00:00
}
return $translation ;
}
2022-09-09 11:33:15 +00:00
add_filter ( 'gettext' , 'woocommerce_blocks_get_php_translation_from_core' , 10 , 3 );
2022-05-31 00:42:28 +00:00
/**
* Add notice to the admin dashboard if the plugin is outdated .
*
* @ see https :// github . com / woocommerce / woocommerce - blocks / issues / 5587
*/
2022-09-09 11:33:15 +00:00
function woocommerce_blocks_plugin_outdated_notice () {
2022-05-31 00:42:28 +00:00
$is_active =
2022-09-09 11:33:15 +00:00
is_plugin_active ( 'woo-gutenberg-products-block/woocommerce-gutenberg-products-block.php' ) ||
is_plugin_active ( 'woocommerce-gutenberg-products-block/woocommerce-gutenberg-products-block.php' ) ||
is_plugin_active ( 'woocommerce-blocks/woocommerce-gutenberg-products-block.php' );
2022-05-31 00:42:28 +00:00
2022-09-09 11:33:15 +00:00
if ( ! $is_active ) {
2022-05-31 00:42:28 +00:00
return ;
}
$woocommerce_blocks_path = \Automattic\WooCommerce\Blocks\Package :: get_path ();
/**
* Check the current WC Blocks path . If the WC Blocks plugin is active but
* the current path is from the WC Core , we can consider the plugin is
* outdated because Jetpack Autoloader always loads the newer package .
*/
2022-09-09 11:33:15 +00:00
if ( ! strpos ( $woocommerce_blocks_path , 'packages/woocommerce-blocks' ) ) {
2022-05-31 00:42:28 +00:00
return ;
}
2022-09-09 11:33:15 +00:00
if ( should_display_compatibility_notices () ) {
?>
2022-05-31 00:42:28 +00:00
< div class = " notice notice-warning " >
2022-09-09 11:33:15 +00:00
< p >< ? php esc_html_e ( 'You have WooCommerce Blocks installed, but the WooCommerce bundled version is running because it is more up-to-date. This may cause unexpected compatibility issues. Please update the WooCommerce Blocks plugin.' , 'woo-gutenberg-products-block' ); ?> </p>
2022-05-31 00:42:28 +00:00
</ div >
2022-09-09 11:33:15 +00:00
< ? php
2022-05-31 00:42:28 +00:00
}
}
2022-09-09 11:33:15 +00:00
add_action ( 'admin_notices' , 'woocommerce_blocks_plugin_outdated_notice' );
2023-01-26 11:39:25 +00:00
2023-03-02 12:46:27 +00:00
/**
* Disable the Interactivity API if the required `WP_HTML_Tag_Processor` class
* doesn ' t exist , regardless of whether it was enabled manually .
*
* @ param bool $enabled Current filter value .
* @ return bool True if _also_ the `WP_HTML_Tag_Processor` class was found .
*/
function woocommerce_blocks_has_wp_html_tag_processor ( $enabled ) {
return $enabled && class_exists ( 'WP_HTML_Tag_Processor' );
}
add_filter (
'woocommerce_blocks_enable_interactivity_api' ,
'woocommerce_blocks_has_wp_html_tag_processor' ,
999
);
2023-02-28 16:33:33 +00:00
2023-03-02 12:46:27 +00:00
/**
* Load and setup the Interactivity API if enabled .
*/
function woocommerce_blocks_interactivity_setup () {
2023-03-09 06:26:56 +00:00
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
2023-03-02 12:46:27 +00:00
$is_enabled = apply_filters (
'woocommerce_blocks_enable_interactivity_api' ,
false
);
if ( $is_enabled ) {
require_once __DIR__ . '/src/Interactivity/woo-directives.php' ;
}
}
add_action ( 'plugins_loaded' , 'woocommerce_blocks_interactivity_setup' );