wp/wp-includes/js/dist/private-apis.js
changeset 21 48c4eec2b7e6
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 /******/ (() => { // webpackBootstrap
       
     2 /******/ 	"use strict";
       
     3 /******/ 	// The require scope
       
     4 /******/ 	var __webpack_require__ = {};
       
     5 /******/ 	
       
     6 /************************************************************************/
       
     7 /******/ 	/* webpack/runtime/define property getters */
       
     8 /******/ 	(() => {
       
     9 /******/ 		// define getter functions for harmony exports
       
    10 /******/ 		__webpack_require__.d = (exports, definition) => {
       
    11 /******/ 			for(var key in definition) {
       
    12 /******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
       
    13 /******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
       
    14 /******/ 				}
       
    15 /******/ 			}
       
    16 /******/ 		};
       
    17 /******/ 	})();
       
    18 /******/ 	
       
    19 /******/ 	/* webpack/runtime/hasOwnProperty shorthand */
       
    20 /******/ 	(() => {
       
    21 /******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
       
    22 /******/ 	})();
       
    23 /******/ 	
       
    24 /******/ 	/* webpack/runtime/make namespace object */
       
    25 /******/ 	(() => {
       
    26 /******/ 		// define __esModule on exports
       
    27 /******/ 		__webpack_require__.r = (exports) => {
       
    28 /******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
       
    29 /******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
       
    30 /******/ 			}
       
    31 /******/ 			Object.defineProperty(exports, '__esModule', { value: true });
       
    32 /******/ 		};
       
    33 /******/ 	})();
       
    34 /******/ 	
       
    35 /************************************************************************/
       
    36 var __webpack_exports__ = {};
       
    37 // ESM COMPAT FLAG
       
    38 __webpack_require__.r(__webpack_exports__);
       
    39 
       
    40 // EXPORTS
       
    41 __webpack_require__.d(__webpack_exports__, {
       
    42   __dangerousOptInToUnstableAPIsOnlyForCoreModules: () => (/* reexport */ __dangerousOptInToUnstableAPIsOnlyForCoreModules)
       
    43 });
       
    44 
       
    45 ;// CONCATENATED MODULE: ./node_modules/@wordpress/private-apis/build-module/implementation.js
       
    46 /**
       
    47  * wordpress/private-apis – the utilities to enable private cross-package
       
    48  * exports of private APIs.
       
    49  *
       
    50  * This "implementation.js" file is needed for the sake of the unit tests. It
       
    51  * exports more than the public API of the package to aid in testing.
       
    52  */
       
    53 
       
    54 /**
       
    55  * The list of core modules allowed to opt-in to the private APIs.
       
    56  */
       
    57 const CORE_MODULES_USING_PRIVATE_APIS = ['@wordpress/block-directory', '@wordpress/block-editor', '@wordpress/block-library', '@wordpress/blocks', '@wordpress/commands', '@wordpress/components', '@wordpress/core-commands', '@wordpress/core-data', '@wordpress/customize-widgets', '@wordpress/data', '@wordpress/edit-post', '@wordpress/edit-site', '@wordpress/edit-widgets', '@wordpress/editor', '@wordpress/format-library', '@wordpress/interface', '@wordpress/patterns', '@wordpress/preferences', '@wordpress/reusable-blocks', '@wordpress/router', '@wordpress/dataviews'];
       
    58 
       
    59 /**
       
    60  * A list of core modules that already opted-in to
       
    61  * the privateApis package.
       
    62  *
       
    63  * @type {string[]}
       
    64  */
       
    65 const registeredPrivateApis = [];
       
    66 
       
    67 /*
       
    68  * Warning for theme and plugin developers.
       
    69  *
       
    70  * The use of private developer APIs is intended for use by WordPress Core
       
    71  * and the Gutenberg plugin exclusively.
       
    72  *
       
    73  * Dangerously opting in to using these APIs is NOT RECOMMENDED. Furthermore,
       
    74  * the WordPress Core philosophy to strive to maintain backward compatibility
       
    75  * for third-party developers DOES NOT APPLY to private APIs.
       
    76  *
       
    77  * THE CONSENT STRING FOR OPTING IN TO THESE APIS MAY CHANGE AT ANY TIME AND
       
    78  * WITHOUT NOTICE. THIS CHANGE WILL BREAK EXISTING THIRD-PARTY CODE. SUCH A
       
    79  * CHANGE MAY OCCUR IN EITHER A MAJOR OR MINOR RELEASE.
       
    80  */
       
    81 const requiredConsent = 'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.';
       
    82 
       
    83 /** @type {boolean} */
       
    84 let allowReRegistration;
       
    85 // The safety measure is meant for WordPress core where IS_WORDPRESS_CORE
       
    86 // is set to true.
       
    87 // For the general use-case, the re-registration should be allowed by default
       
    88 // Let's default to true, then. Try/catch will fall back to "true" even if the
       
    89 // environment variable is not explicitly defined.
       
    90 try {
       
    91   allowReRegistration =  true ? false : 0;
       
    92 } catch (error) {
       
    93   allowReRegistration = true;
       
    94 }
       
    95 
       
    96 /**
       
    97  * Called by a @wordpress package wishing to opt-in to accessing or exposing
       
    98  * private private APIs.
       
    99  *
       
   100  * @param {string} consent    The consent string.
       
   101  * @param {string} moduleName The name of the module that is opting in.
       
   102  * @return {{lock: typeof lock, unlock: typeof unlock}} An object containing the lock and unlock functions.
       
   103  */
       
   104 const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (consent, moduleName) => {
       
   105   if (!CORE_MODULES_USING_PRIVATE_APIS.includes(moduleName)) {
       
   106     throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}". ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
       
   107   }
       
   108   if (!allowReRegistration && registeredPrivateApis.includes(moduleName)) {
       
   109     // This check doesn't play well with Story Books / Hot Module Reloading
       
   110     // and isn't included in the Gutenberg plugin. It only matters in the
       
   111     // WordPress core release.
       
   112     throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}" which is already registered. ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
       
   113   }
       
   114   if (consent !== requiredConsent) {
       
   115     throw new Error(`You tried to opt-in to unstable APIs without confirming you know the consequences. ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on the next WordPress release.');
       
   116   }
       
   117   registeredPrivateApis.push(moduleName);
       
   118   return {
       
   119     lock,
       
   120     unlock
       
   121   };
       
   122 };
       
   123 
       
   124 /**
       
   125  * Binds private data to an object.
       
   126  * It does not alter the passed object in any way, only
       
   127  * registers it in an internal map of private data.
       
   128  *
       
   129  * The private data can't be accessed by any other means
       
   130  * than the `unlock` function.
       
   131  *
       
   132  * @example
       
   133  * ```js
       
   134  * const object = {};
       
   135  * const privateData = { a: 1 };
       
   136  * lock( object, privateData );
       
   137  *
       
   138  * object
       
   139  * // {}
       
   140  *
       
   141  * unlock( object );
       
   142  * // { a: 1 }
       
   143  * ```
       
   144  *
       
   145  * @param {any} object      The object to bind the private data to.
       
   146  * @param {any} privateData The private data to bind to the object.
       
   147  */
       
   148 function lock(object, privateData) {
       
   149   if (!object) {
       
   150     throw new Error('Cannot lock an undefined object.');
       
   151   }
       
   152   if (!(__private in object)) {
       
   153     object[__private] = {};
       
   154   }
       
   155   lockedData.set(object[__private], privateData);
       
   156 }
       
   157 
       
   158 /**
       
   159  * Unlocks the private data bound to an object.
       
   160  *
       
   161  * It does not alter the passed object in any way, only
       
   162  * returns the private data paired with it using the `lock()`
       
   163  * function.
       
   164  *
       
   165  * @example
       
   166  * ```js
       
   167  * const object = {};
       
   168  * const privateData = { a: 1 };
       
   169  * lock( object, privateData );
       
   170  *
       
   171  * object
       
   172  * // {}
       
   173  *
       
   174  * unlock( object );
       
   175  * // { a: 1 }
       
   176  * ```
       
   177  *
       
   178  * @param {any} object The object to unlock the private data from.
       
   179  * @return {any} The private data bound to the object.
       
   180  */
       
   181 function unlock(object) {
       
   182   if (!object) {
       
   183     throw new Error('Cannot unlock an undefined object.');
       
   184   }
       
   185   if (!(__private in object)) {
       
   186     throw new Error('Cannot unlock an object that was not locked before. ');
       
   187   }
       
   188   return lockedData.get(object[__private]);
       
   189 }
       
   190 const lockedData = new WeakMap();
       
   191 
       
   192 /**
       
   193  * Used by lock() and unlock() to uniquely identify the private data
       
   194  * related to a containing object.
       
   195  */
       
   196 const __private = Symbol('Private API ID');
       
   197 
       
   198 // Unit tests utilities:
       
   199 
       
   200 /**
       
   201  * Private function to allow the unit tests to allow
       
   202  * a mock module to access the private APIs.
       
   203  *
       
   204  * @param {string} name The name of the module.
       
   205  */
       
   206 function allowCoreModule(name) {
       
   207   CORE_MODULES_USING_PRIVATE_APIS.push(name);
       
   208 }
       
   209 
       
   210 /**
       
   211  * Private function to allow the unit tests to set
       
   212  * a custom list of allowed modules.
       
   213  */
       
   214 function resetAllowedCoreModules() {
       
   215   while (CORE_MODULES_USING_PRIVATE_APIS.length) {
       
   216     CORE_MODULES_USING_PRIVATE_APIS.pop();
       
   217   }
       
   218 }
       
   219 /**
       
   220  * Private function to allow the unit tests to reset
       
   221  * the list of registered private apis.
       
   222  */
       
   223 function resetRegisteredPrivateApis() {
       
   224   while (registeredPrivateApis.length) {
       
   225     registeredPrivateApis.pop();
       
   226   }
       
   227 }
       
   228 
       
   229 ;// CONCATENATED MODULE: ./node_modules/@wordpress/private-apis/build-module/index.js
       
   230 
       
   231 
       
   232 (window.wp = window.wp || {}).privateApis = __webpack_exports__;
       
   233 /******/ })()
       
   234 ;