woocommerce/plugins/woocommerce-blocks/woocommerce-gutenberg-produ...

264 lines
9.6 KiB
PHP
Raw Normal View History

<?php
/**
* Plugin Name: WooCommerce Blocks
* Plugin URI: https://github.com/woocommerce/woocommerce-gutenberg-products-block
* Description: WooCommerce blocks for the Gutenberg editor.
* Version: 7.7.0-dev
* Author: Automattic
* Author URI: https://woocommerce.com
* Text Domain: woo-gutenberg-products-block
* Requires at least: 5.9
* Requires PHP: 7.0
Release: 7.4.0 (https://github.com/woocommerce/woocommerce-blocks/pull/6225) * Empty commit for release pull request * Add Changelog to readme.txt * Update WC tested and required versions * Add testing notes * Register missing C&C inner blocks and update fallback template for older C& C versions (https://github.com/woocommerce/woocommerce-blocks/pull/6195) * Register missing C & C inner blocks and update fallback template for older C & C versions This will fix the issues with missing order summary inner blocks: Coupons (both in C & C blocks) and the Cart header. The issue was happening because, for example, for Cart the coupons were registred on the on frontend, but it just wasn't forced in the attributes. Because it also wasn't added to the PHP fallback layout, the render function didn't include it. For the Checkout block the coupons inner block wasn't registered at all. * Revert changes to Checkout.php, we don't need to test for inner blocks * Revert "Revert changes to Checkout.php, we don't need to test for inner blocks" This reverts commit fc39535f8476d02e35353374d9434a8723458cfa. * Fix the returned template for older Checkout block iterations * Fix Cart and Checkout templates to accommodate the Summary order inner blocks * Hide coupon form div from inner blocks if coubons are not enabled * Fix checkout coupon tests in checkout They have been written for logged in user * Fix Order Summary Heading inner block's default text * Update comments with better wording * Revert "Hide coupon form div from inner blocks if coubons are not enabled" This reverts commit ab09021b923fad4218b3016e47760ad07f6bc960. (cherry picked from commit 0b18ff59bd4c3fd80c46ab01d00c180bf499df4d) * Update the zip file link * Update testing instructions * Remove experimental build related PR from testing notes * Fix/order summary sidebar css (https://github.com/woocommerce/woocommerce-blocks/pull/6231) * Add box sizing to Totals item * Add some unit tests for Order summary blocks * Fix Proceed to checkout button size (cherry picked from commit d22ce7cd0be17df9475d34b469e83486595eea71) * Update the WC required/tested versions * Mini Cart Contents: Use block pattern to make the empty cart message translatable (https://github.com/woocommerce/woocommerce-blocks/pull/6248) * try: use block pattern to make empty cart message translatable * Update src/BlockTypes/MiniCart.php Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com> * rename function Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com> Co-authored-by: Luigi <gigitux@gmail.com> (cherry picked from commit 93ad72f6ffc2b832d2e8b6cf92d35897d498f8d6) * Update the release's ZIP file * Update testing notes In woocommerce/woocommerce-blocks#6065, for Cart only Order Summary Heading & Coupon form can be removed, and for Checkout only the Coupon form. * Update the testing notes Remove woocommerce/woocommerce-blocks#5870 testing notes because they can't be tested as a user * Update Testing notes Add screenshots to the woocommerce/woocommerce-blocks#5967 testing notes * Remove woocommerce/woocommerce-blocks#6166 testing instructions We reverted this PR * Revert (https://github.com/woocommerce/woocommerce-blocks/pull/6166) (https://github.com/woocommerce/woocommerce-blocks/pull/6253) Revert "Prevent Featured Product block from breaking when product is out of stock + hidden from catalog (https://github.com/woocommerce/woocommerce-blocks/pull/6166)" This reverts commit 92387e2 (cherry picked from commit c586b1a5f3d7365f6e67bbeb8d920d99444ecc6e) * Bumping version strings to new version. Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Saad Tarhi <saad.trh@gmail.com> Co-authored-by: Raluca Stan <ralucastn@gmail.com> Co-authored-by: Luigi Teschio <gigitux@gmail.com> Co-authored-by: Tung Du <dinhtungdu@gmail.com>
2022-04-14 11:55:33 +00:00
* WC requires at least: 6.3
* WC tested up to: 6.4
*
* @package WooCommerce\Blocks
* @internal This file is only used when running as a feature plugin.
*/
defined( 'ABSPATH' ) || exit;
$minimum_wp_version = '5.9';
if ( ! defined( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) ) {
define( 'WC_BLOCKS_IS_FEATURE_PLUGIN', true );
}
/**
* Whether notices must be displayed in the current page (plugins and WooCommerce pages).
*
* @since 2.5.0
*/
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.
*
* @since 2.5.0
*/
function woocommerce_blocks_admin_unsupported_wp_notice() {
if ( should_display_compatibility_notices() ) {
?>
Update relevant files due to L2->L0 policy change for minimum required versions. (https://github.com/woocommerce/woocommerce-blocks/pull/4484) * update README files with version requirements * update phpcs config * update default environment for tests locally * update workflows to only test against versions matching new policy * update plugin header for new version policy * restore older WP version support in E2E tests and PHPCS I realized that we might still want to test against L2 WP versions in our E2E tests until those tests can be moved to WC core. While the feature plugin is L0, we still need to support L2 for existing features that are already surfaced in the WooCommerce plugin package. Also with PHPCS if there are fixes to older things in the codebase, we will need to preserve the usage of L2 friendly (back-compat) code. * add detection of Woo Core version to bootstrap * update version used in phpunit tests * add links to version policy update post * restore 5.5.1 for Woo Core version Getting errors with 5.5.2 - not sure what's going on but might be the tag isn't available as expected for 5.5.2 maybe? * switch to hardcoded version header reference PHPUNIT was tripping on this for some reason. * remove older WP version e2e test workflows * fix phpunit tests for new woo test suite Woo Core now uses a container and has some mocks that are used in some legacy test helpers. * remove explicit set WOO_VERSION for phpunit tests. This should default to latest for the GH workflow runs. * Update release PR template. Add some explicit instructions around updating required WP and WC versions at time of release. * use latest WOO_VERSION in .env * some improvements to reduce the things needing updated * remove extraneous code * Add some more checklist items to release PR templates * remove unnecessary conditional * remove is-dismissible css classes * Improve clarity of error messaging * Update README.md Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com> * remove checklist items about versions for patch releases Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
2021-07-29 14:52:34 +00:00
<div class="notice notice-error">
<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>
</div>
<?php
}
}
add_action( 'admin_notices', 'woocommerce_blocks_admin_unsupported_wp_notice' );
return;
}
/**
* 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.
*/
function woocommerce_blocks_is_development_version() {
$composer_file = __DIR__ . '/composer.json';
if ( ! is_readable( $composer_file ) ) {
return false;
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- including local file
$composer_config = json_decode( file_get_contents( $composer_file ), true );
return ! isset( $composer_config['version'] );
}
/**
* If development version is detected and the Jetpack constant is not defined, show a notice.
*/
if ( woocommerce_blocks_is_development_version() && ! defined( 'JETPACK_AUTOLOAD_DEV' ) ) {
add_action(
'admin_notices',
function() {
echo '<div class="error"><p>';
printf(
/* translators: %1$s is referring to a php constant name, %2$s is referring to the wp-config.php file. */
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' ),
'JETPACK_AUTOLOAD_DEV',
'wp-config.php'
);
echo '</p></div>';
}
);
}
/**
* 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.
*/
$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
}
);
return;
}
add_action( 'plugins_loaded', array( '\Automattic\WooCommerce\Blocks\Package', 'init' ) );
Implement PHP DI container and refactor. Also implements new Asset data interface for extendable settings passed to js. (https://github.com/woocommerce/woocommerce-blocks/pull/956) * Add dependency injection container for blocks * Add new Pacakge and Bootstrap classes. - Bootstrap for bootstrapping the plugin. - Package will replace `src/Package` and added as a dependency for any classes needing package info. * Introduce AssetsDataRegistry for managing asset data * refactor existing classes to use new DIC and Asset Data Registry - this is the bare minimum needed to make this pull viable. - further refactors will be done in more atomic smaller pulls for easier review. * add new settings handling and export `@woocommerce/settings` as an alias to wc.wcSettings - the export is exposed php side on the `wc-settings` handle. * Remove unnecessary concatenation * Fix typos and improve doc blocks * fix php linting issue * Use better escaping function. * improve jsdoc spacing * improve test assertion * use fully qualified class names in bootstrap * improve comment block to account for dynamic version string replace on build * handle exceptions a bit differently * correct dependency reference in webpack config * remove blank lines * fix doc block comment alignment * Various doc/grammar/spacing fixes from code review. Co-Authored-By: Albert Juhé Lluveras <contact@albertjuhe.com> * improve naming, documentation and logic of filter callbacks While this is intended for sanitization/validation, the callback ultimately provides flexibility for filtering the value before returning or setting in state so `filter` is a better name for this.
2019-09-23 18:07:13 +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_src = explode( '/build/', $wp_scripts->registered[ $handle ]->src );
$handle_filename = $handle_src[1];
$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;
if ( empty( $wp_scripts->done ) ) {
// If we hadn't printed any script into the page, let's enqueue the translations.
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script( $handle_filename, '', array( 'wp-i18n' ), false, true );
wp_enqueue_script( $handle_filename );
wp_add_inline_script(
$handle_filename,
$output
);
} else {
// If we have already printed scripts into the page, there is a chance that
// scripts have finished being printed. That means that if we enqueued them here,
// they would never be printed. Instead of enqueuing, then, let's print directly
// the script tag.
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 );
/**
* 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 );