assets
5 years ago
build
5 years ago
packages
5 years ago
src
5 years ago
templates
5 years ago
vendor
5 years ago
readme.txt
5 years ago
woocommerce-gutenberg-products-block.php
5 years ago
woocommerce-gutenberg-products-block.php
248 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: WooCommerce Blocks |
| 4 | * Plugin URI: https://github.com/woocommerce/woocommerce-gutenberg-products-block |
| 5 | * Description: WooCommerce blocks for the Gutenberg editor. |
| 6 | * Version: 4.9.1 |
| 7 | * Author: Automattic |
| 8 | * Author URI: https://woocommerce.com |
| 9 | * Text Domain: woo-gutenberg-products-block |
| 10 | * Requires at least: 5.5 |
| 11 | * Requires PHP: 7.0 |
| 12 | * WC requires at least: 4.9 |
| 13 | * WC tested up to: 5.2 |
| 14 | * |
| 15 | * @package WooCommerce\Blocks |
| 16 | * @internal This file is only used when running as a feature plugin. |
| 17 | */ |
| 18 | |
| 19 | defined( 'ABSPATH' ) || exit; |
| 20 | |
| 21 | $minimum_wp_version = '5.5'; |
| 22 | |
| 23 | if ( ! defined( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) ) { |
| 24 | define( 'WC_BLOCKS_IS_FEATURE_PLUGIN', true ); |
| 25 | } |
| 26 | /** |
| 27 | * Whether notices must be displayed in the current page (plugins and WooCommerce pages). |
| 28 | * |
| 29 | * @since 2.5.0 |
| 30 | */ |
| 31 | function should_display_compatibility_notices() { |
| 32 | $current_screen = get_current_screen(); |
| 33 | |
| 34 | if ( ! isset( $current_screen ) ) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | $is_plugins_page = |
| 39 | property_exists( $current_screen, 'id' ) && |
| 40 | 'plugins' === $current_screen->id; |
| 41 | $is_woocommerce_page = |
| 42 | property_exists( $current_screen, 'parent_base' ) && |
| 43 | 'woocommerce' === $current_screen->parent_base; |
| 44 | |
| 45 | return $is_plugins_page || $is_woocommerce_page; |
| 46 | } |
| 47 | |
| 48 | if ( version_compare( $GLOBALS['wp_version'], $minimum_wp_version, '<' ) ) { |
| 49 | /** |
| 50 | * Outputs for an admin notice about running WooCommerce Blocks on outdated WordPress. |
| 51 | * |
| 52 | * @since 2.5.0 |
| 53 | */ |
| 54 | function woocommerce_blocks_admin_unsupported_wp_notice() { |
| 55 | if ( should_display_compatibility_notices() ) { |
| 56 | ?> |
| 57 | <div class="notice notice-error is-dismissible"> |
| 58 | <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.', 'woocommerce' ); ?></p> |
| 59 | </div> |
| 60 | <?php |
| 61 | } |
| 62 | } |
| 63 | add_action( 'admin_notices', 'woocommerce_blocks_admin_unsupported_wp_notice' ); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns whether the current version is a development version |
| 69 | * Note this relies on composer.json version, not plugin version. |
| 70 | * Development installs of the plugin don't have a version defined in |
| 71 | * composer json. |
| 72 | * |
| 73 | * @return bool True means the current version is a development version. |
| 74 | */ |
| 75 | function woocommerce_blocks_is_development_version() { |
| 76 | $composer_file = __DIR__ . '/composer.json'; |
| 77 | if ( ! is_readable( $composer_file ) ) { |
| 78 | return false; |
| 79 | } |
| 80 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- including local file |
| 81 | $composer_config = json_decode( file_get_contents( $composer_file ), true ); |
| 82 | return ! isset( $composer_config['version'] ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * If development version is detected and the Jetpack constant is not defined, show a notice. |
| 87 | */ |
| 88 | if ( woocommerce_blocks_is_development_version() && ! defined( 'JETPACK_AUTOLOAD_DEV' ) ) { |
| 89 | add_action( |
| 90 | 'admin_notices', |
| 91 | function() { |
| 92 | echo '<div class="error"><p>'; |
| 93 | printf( |
| 94 | /* translators: %1$s is referring to a php constant name, %2$s is referring to the wp-config.php file. */ |
| 95 | 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.', 'woocommerce' ), |
| 96 | 'JETPACK_AUTOLOAD_DEV', |
| 97 | 'wp-config.php' |
| 98 | ); |
| 99 | echo '</p></div>'; |
| 100 | } |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Autoload packages. |
| 107 | * |
| 108 | * The package autoloader includes version information which prevents classes in this feature plugin |
| 109 | * conflicting with WooCommerce core. |
| 110 | * |
| 111 | * We want to fail gracefully if `composer install` has not been executed yet, so we are checking for the autoloader. |
| 112 | * If the autoloader is not present, let's log the failure and display a nice admin notice. |
| 113 | */ |
| 114 | $autoloader = __DIR__ . '/vendor/autoload_packages.php'; |
| 115 | if ( is_readable( $autoloader ) ) { |
| 116 | require $autoloader; |
| 117 | } else { |
| 118 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 119 | error_log( // phpcs:ignore |
| 120 | sprintf( |
| 121 | /* translators: 1: composer command. 2: plugin directory */ |
| 122 | esc_html__( 'Your installation of the WooCommerce Blocks feature plugin is incomplete. Please run %1$s within the %2$s directory.', 'woocommerce' ), |
| 123 | '`composer install`', |
| 124 | '`' . esc_html( str_replace( ABSPATH, '', __DIR__ ) ) . '`' |
| 125 | ) |
| 126 | ); |
| 127 | } |
| 128 | /** |
| 129 | * Outputs an admin notice if composer install has not been ran. |
| 130 | */ |
| 131 | add_action( |
| 132 | 'admin_notices', |
| 133 | function() { |
| 134 | ?> |
| 135 | <div class="notice notice-error"> |
| 136 | <p> |
| 137 | <?php |
| 138 | printf( |
| 139 | /* translators: 1: composer command. 2: plugin directory */ |
| 140 | esc_html__( 'Your installation of the WooCommerce Blocks feature plugin is incomplete. Please run %1$s within the %2$s directory.', 'woocommerce' ), |
| 141 | '<code>composer install</code>', |
| 142 | '<code>' . esc_html( str_replace( ABSPATH, '', __DIR__ ) ) . '</code>' |
| 143 | ); |
| 144 | ?> |
| 145 | </p> |
| 146 | </div> |
| 147 | <?php |
| 148 | } |
| 149 | ); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | add_action( 'plugins_loaded', array( '\Automattic\WooCommerce\Blocks\Package', 'init' ) ); |
| 154 | |
| 155 | /** |
| 156 | * Pre-filters script translations for the given file, script handle and text domain. |
| 157 | * |
| 158 | * @param string|false|null $translations JSON-encoded translation data. Default null. |
| 159 | * @param string|false $file Path to the translation file to load. False if there isn't one. |
| 160 | * @param string $handle Name of the script to register a translation domain to. |
| 161 | * @param string $domain The text domain. |
| 162 | * @return string JSON translations. |
| 163 | */ |
| 164 | function woocommerce_blocks_get_i18n_data_json( $translations, $file, $handle, $domain ) { |
| 165 | if ( 'woo-gutenberg-products-block' !== $domain ) { |
| 166 | return $translations; |
| 167 | } |
| 168 | |
| 169 | global $wp_scripts; |
| 170 | |
| 171 | if ( ! isset( $wp_scripts->registered[ $handle ], $wp_scripts->registered[ $handle ]->src ) ) { |
| 172 | return $translations; |
| 173 | } |
| 174 | |
| 175 | $handle_filename = basename( $wp_scripts->registered[ $handle ]->src ); |
| 176 | $locale = determine_locale(); |
| 177 | $lang_dir = WP_LANG_DIR . '/plugins'; |
| 178 | |
| 179 | // Translations are always based on the unminified filename. |
| 180 | if ( substr( $handle_filename, -7 ) === '.min.js' ) { |
| 181 | $handle_filename = substr( $handle_filename, 0, -7 ) . '.js'; |
| 182 | } |
| 183 | |
| 184 | // WordPress 5.0 uses md5 hashes of file paths to associate translation |
| 185 | // JSON files with the file they should be included for. This is an md5 |
| 186 | // of 'packages/woocommerce-blocks/build/FILENAME.js'. |
| 187 | $core_path_md5 = md5( 'packages/woocommerce-blocks/build/' . $handle_filename ); |
| 188 | $core_json_file = $lang_dir . '/woocommerce-' . $locale . '-' . $core_path_md5 . '.json'; |
| 189 | $json_translations = is_file( $core_json_file ) && is_readable( $core_json_file ) ? file_get_contents( $core_json_file ) : false; // phpcs:ignore |
| 190 | |
| 191 | if ( ! $json_translations ) { |
| 192 | return $translations; |
| 193 | } |
| 194 | |
| 195 | // Rather than short circuit pre_load_script_translations, we will output |
| 196 | // core translations using an inline script. This will allow us to continue |
| 197 | // to load feature-plugin translations which may exist as well. |
| 198 | $output = <<<JS |
| 199 | ( function( domain, translations ) { |
| 200 | var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; |
| 201 | localeData[""].domain = domain; |
| 202 | wp.i18n.setLocaleData( localeData, domain ); |
| 203 | } )( "{$domain}", {$json_translations} ); |
| 204 | JS; |
| 205 | |
| 206 | printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); // phpcs:ignore |
| 207 | |
| 208 | // Finally, short circuit the pre_load_script_translations hook by returning |
| 209 | // the translation JSON from the feature plugin, if it exists so this hook |
| 210 | // does not run again for the current handle. |
| 211 | $path_md5 = md5( 'build/' . $handle_filename ); |
| 212 | $json_file = $lang_dir . '/' . $domain . '-' . $locale . '-' . $path_md5 . '.json'; |
| 213 | $translations = is_file( $json_file ) && is_readable( $json_file ) ? file_get_contents( $json_file ) : false; // phpcs:ignore |
| 214 | |
| 215 | if ( $translations ) { |
| 216 | return $translations; |
| 217 | } |
| 218 | |
| 219 | // Return valid empty Jed locale. |
| 220 | return '{ "locale_data": { "messages": { "": {} } } }'; |
| 221 | } |
| 222 | |
| 223 | add_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 ); |
| 224 | |
| 225 | /** |
| 226 | * Filter translations so we can retrieve translations from Core when the original and the translated |
| 227 | * texts are the same (which happens when translations are missing). |
| 228 | * |
| 229 | * @param string $translation Translated text based on WC Blocks translations. |
| 230 | * @param string $text Text to translate. |
| 231 | * @param string $domain The text domain. |
| 232 | * @return string WC Blocks translation. In case it's the same as $text, Core translation. |
| 233 | */ |
| 234 | function woocommerce_blocks_get_php_translation_from_core( $translation, $text, $domain ) { |
| 235 | if ( 'woo-gutenberg-products-block' !== $domain ) { |
| 236 | return $translation; |
| 237 | } |
| 238 | |
| 239 | // When translation is the same, that could mean the string is not translated. |
| 240 | // In that case, load it from core. |
| 241 | if ( $translation === $text ) { |
| 242 | return translate( $text, 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction, WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.TextDomainMismatch |
| 243 | } |
| 244 | return $translation; |
| 245 | } |
| 246 | |
| 247 | add_filter( 'gettext', 'woocommerce_blocks_get_php_translation_from_core', 10, 3 ); |
| 248 |