@10up/block-components
Version:
10up Components built for the WordPress Block Editor.
336 lines (291 loc) • 15.8 kB
JavaScript
/******/ (function() { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./components/post-context/context.ts":
/*!********************************************!*\
!*** ./components/post-context/context.ts ***!
\********************************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ DEFAULT_POST_CONTEXT: function() { return /* binding */ DEFAULT_POST_CONTEXT; },
/* harmony export */ PostContext: function() { return /* binding */ PostContext; },
/* harmony export */ usePostContext: function() { return /* binding */ usePostContext; }
/* harmony export */ });
/* harmony import */ var _wordpress_element__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @wordpress/element */ "@wordpress/element");
/* harmony import */ var _wordpress_element__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_element__WEBPACK_IMPORTED_MODULE_0__);
const DEFAULT_POST_CONTEXT = {
postId: undefined,
postType: undefined,
isEditable: undefined
};
const PostContext = (0,_wordpress_element__WEBPACK_IMPORTED_MODULE_0__.createContext)(DEFAULT_POST_CONTEXT);
const usePostContext = () => {
return (0,_wordpress_element__WEBPACK_IMPORTED_MODULE_0__.useContext)(PostContext);
};
/***/ }),
/***/ "./hooks/use-is-plugin-active/index.ts":
/*!*********************************************!*\
!*** ./hooks/use-is-plugin-active/index.ts ***!
\*********************************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ useIsPluginActive: function() { return /* binding */ useIsPluginActive; }
/* harmony export */ });
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @wordpress/data */ "@wordpress/data");
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_data__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _wordpress_core_data__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @wordpress/core-data */ "@wordpress/core-data");
/* harmony import */ var _wordpress_core_data__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_core_data__WEBPACK_IMPORTED_MODULE_1__);
const ACTIVE_STATUSES = ['active', 'network-active'];
/**
* Custom hook to check if a plugin is active and whether its resolution has finished.
*
* @param {string} pluginName The name of the plugin to check.
* @returns {[boolean, boolean]} A tuple with the first value being whether the plugin is active and the second value being whether the resolution has finished.
*/
const useIsPluginActive = pluginName => {
return (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.useSelect)(select => {
const storeSelectors = select(_wordpress_core_data__WEBPACK_IMPORTED_MODULE_1__.store);
const plugin = storeSelectors.getPlugin(pluginName);
const hasResolvedPlugins = storeSelectors.hasFinishedResolution('getPlugin', [pluginName]);
// @ts-ignore-next-line - The check here is intentional to see if the plugin is active.
const isPluginActive = ACTIVE_STATUSES.includes(plugin?.status);
return [isPluginActive, hasResolvedPlugins];
}, [pluginName]);
};
/***/ }),
/***/ "./hooks/use-is-supported-taxonomy/index.ts":
/*!**************************************************!*\
!*** ./hooks/use-is-supported-taxonomy/index.ts ***!
\**************************************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ useIsSupportedTaxonomy: function() { return /* binding */ useIsSupportedTaxonomy; }
/* harmony export */ });
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @wordpress/data */ "@wordpress/data");
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_data__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _wordpress_core_data__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @wordpress/core-data */ "@wordpress/core-data");
/* harmony import */ var _wordpress_core_data__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_core_data__WEBPACK_IMPORTED_MODULE_1__);
const useIsSupportedTaxonomy = (postType, taxonomyName) => {
return (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.useSelect)(select => {
// @ts-ignore-next-line - The type definitions for the core store are incomplete.
const {
getPostType,
hasFinishedResolution
} = select(_wordpress_core_data__WEBPACK_IMPORTED_MODULE_1__.store);
const postTypeObject = getPostType(postType);
const hasResolvedPostType = hasFinishedResolution('getPostType', [postType]);
const isSupportedTaxonomy = postTypeObject?.taxonomies?.some(name => name === taxonomyName);
return [!!isSupportedTaxonomy, hasResolvedPostType];
}, [postType, taxonomyName]);
};
/***/ }),
/***/ "./hooks/use-post/index.ts":
/*!*********************************!*\
!*** ./hooks/use-post/index.ts ***!
\*********************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ usePost: function() { return /* binding */ usePost; }
/* harmony export */ });
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @wordpress/data */ "@wordpress/data");
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_data__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _wordpress_editor__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @wordpress/editor */ "@wordpress/editor");
/* harmony import */ var _wordpress_editor__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_editor__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _components_post_context_context__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../components/post-context/context */ "./components/post-context/context.ts");
// @ts-ignore-next-line - The type definitions for the editor package are incomplete.
function usePost() {
const {
postId: blockContextPostId,
postType: blockContextPostType,
isEditable: blockContextIsEditable
} = (0,_components_post_context_context__WEBPACK_IMPORTED_MODULE_2__.usePostContext)();
const {
globalPostId,
globalPostType
} = (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.useSelect)(select => ({
globalPostId: select(_wordpress_editor__WEBPACK_IMPORTED_MODULE_1__.store).getCurrentPostId(),
globalPostType: select(_wordpress_editor__WEBPACK_IMPORTED_MODULE_1__.store).getCurrentPostType()
}), []);
const hasBlockContext = !!blockContextPostId && !!blockContextPostType;
return {
postId: blockContextPostId || globalPostId,
postType: blockContextPostType || globalPostType,
isEditable: hasBlockContext ? blockContextIsEditable : true
};
}
/***/ }),
/***/ "@wordpress/core-data":
/*!***************************************!*\
!*** external "@wordpress/core-data" ***!
\***************************************/
/***/ (function(module) {
module.exports = require("@wordpress/core-data");
/***/ }),
/***/ "@wordpress/data":
/*!**********************************!*\
!*** external "@wordpress/data" ***!
\**********************************/
/***/ (function(module) {
module.exports = require("@wordpress/data");
/***/ }),
/***/ "@wordpress/editor":
/*!************************************!*\
!*** external "@wordpress/editor" ***!
\************************************/
/***/ (function(module) {
module.exports = require("@wordpress/editor");
/***/ }),
/***/ "@wordpress/element":
/*!*************************************!*\
!*** external "@wordpress/element" ***!
\*************************************/
/***/ (function(module) {
module.exports = require("@wordpress/element");
/***/ }),
/***/ "@wordpress/i18n":
/*!**********************************!*\
!*** external "@wordpress/i18n" ***!
\**********************************/
/***/ (function(module) {
module.exports = require("@wordpress/i18n");
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ !function() {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function() { return module['default']; } :
/******/ function() { return module; };
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ !function() {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = function(exports, definition) {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ !function() {
/******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
/******/ }();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ !function() {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ }();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
!function() {
/*!*****************************************!*\
!*** ./hooks/use-primary-term/index.ts ***!
\*****************************************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ usePrimaryTerm: function() { return /* binding */ usePrimaryTerm; }
/* harmony export */ });
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @wordpress/data */ "@wordpress/data");
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_data__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _wordpress_i18n__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @wordpress/i18n */ "@wordpress/i18n");
/* harmony import */ var _wordpress_i18n__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _wordpress_core_data__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @wordpress/core-data */ "@wordpress/core-data");
/* harmony import */ var _wordpress_core_data__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_wordpress_core_data__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _use_post__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../use-post */ "./hooks/use-post/index.ts");
/* harmony import */ var _use_is_plugin_active__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../use-is-plugin-active */ "./hooks/use-is-plugin-active/index.ts");
/* harmony import */ var _use_is_supported_taxonomy__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../use-is-supported-taxonomy */ "./hooks/use-is-supported-taxonomy/index.ts");
const usePrimaryTerm = taxonomyName => {
const {
postType,
isEditable
} = (0,_use_post__WEBPACK_IMPORTED_MODULE_3__.usePost)();
const [isYoastSeoActive, hasResolvedIsPluginActive] = (0,_use_is_plugin_active__WEBPACK_IMPORTED_MODULE_4__.useIsPluginActive)('wordpress-seo/wp-seo');
const [isSupportedTaxonomy, hasResolvedIsSupportedTaxonomy] = (0,_use_is_supported_taxonomy__WEBPACK_IMPORTED_MODULE_5__.useIsSupportedTaxonomy)(postType, taxonomyName);
const primaryTermId = (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.useSelect)(select => {
if (!hasResolvedIsSupportedTaxonomy || !hasResolvedIsPluginActive) {
return null;
}
if (!isYoastSeoActive && hasResolvedIsPluginActive) {
// eslint-disable-next-line no-console
console.error('Yoast SEO is not active. Please install and activate Yoast SEO to use the PostPrimaryCategory component.');
return null;
}
if (!isSupportedTaxonomy && hasResolvedIsSupportedTaxonomy) {
// eslint-disable-next-line no-console
console.error(`The taxonomy "${taxonomyName}" is not supported for the post type "${postType}". Please use a supported taxonomy.`);
return null;
}
const yoastStore = select('yoast-seo/editor');
if (!yoastStore) {
// eslint-disable-next-line no-console
console.error(`The yoast-seo/editor store does is not available.`);
return null;
}
// @ts-ignore-next-line - The type definitions for the Yoast store are incomplete.
return select('yoast-seo/editor').getPrimaryTaxonomyId(taxonomyName);
}, [taxonomyName, isYoastSeoActive, isSupportedTaxonomy, hasResolvedIsSupportedTaxonomy, hasResolvedIsPluginActive]);
const primaryTerm = (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.useSelect)(select => {
if (!primaryTermId) {
return null;
}
const {
getEntityRecord
} = select(_wordpress_core_data__WEBPACK_IMPORTED_MODULE_2__.store);
return getEntityRecord('taxonomy', taxonomyName, primaryTermId);
}, [primaryTermId]);
return [!isEditable ? {
name: (0,_wordpress_i18n__WEBPACK_IMPORTED_MODULE_1__.__)('Primary Term', 'tenup'),
link: '#'
} : primaryTerm, isSupportedTaxonomy];
};
}();
module.exports = __webpack_exports__;
/******/ })()
;
//# sourceMappingURL=index.js.map