[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/dist/ -> server-side-render.js (source)

   1  this["wp"] = this["wp"] || {}; this["wp"]["serverSideRender"] =
   2  /******/ (function(modules) { // webpackBootstrap
   3  /******/     // The module cache
   4  /******/     var installedModules = {};
   5  /******/
   6  /******/     // The require function
   7  /******/ 	function __webpack_require__(moduleId) {
   8  /******/
   9  /******/         // Check if module is in cache
  10  /******/         if(installedModules[moduleId]) {
  11  /******/             return installedModules[moduleId].exports;
  12  /******/         }
  13  /******/         // Create a new module (and put it into the cache)
  14  /******/         var module = installedModules[moduleId] = {
  15  /******/             i: moduleId,
  16  /******/             l: false,
  17  /******/             exports: {}
  18  /******/         };
  19  /******/
  20  /******/         // Execute the module function
  21  /******/         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22  /******/
  23  /******/         // Flag the module as loaded
  24  /******/         module.l = true;
  25  /******/
  26  /******/         // Return the exports of the module
  27  /******/         return module.exports;
  28  /******/     }
  29  /******/
  30  /******/
  31  /******/     // expose the modules object (__webpack_modules__)
  32  /******/     __webpack_require__.m = modules;
  33  /******/
  34  /******/     // expose the module cache
  35  /******/     __webpack_require__.c = installedModules;
  36  /******/
  37  /******/     // define getter function for harmony exports
  38  /******/     __webpack_require__.d = function(exports, name, getter) {
  39  /******/         if(!__webpack_require__.o(exports, name)) {
  40  /******/             Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41  /******/         }
  42  /******/     };
  43  /******/
  44  /******/     // define __esModule on exports
  45  /******/     __webpack_require__.r = function(exports) {
  46  /******/         if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47  /******/             Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48  /******/         }
  49  /******/         Object.defineProperty(exports, '__esModule', { value: true });
  50  /******/     };
  51  /******/
  52  /******/     // create a fake namespace object
  53  /******/     // mode & 1: value is a module id, require it
  54  /******/     // mode & 2: merge all properties of value into the ns
  55  /******/     // mode & 4: return value when already ns object
  56  /******/     // mode & 8|1: behave like require
  57  /******/     __webpack_require__.t = function(value, mode) {
  58  /******/         if(mode & 1) value = __webpack_require__(value);
  59  /******/         if(mode & 8) return value;
  60  /******/         if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61  /******/         var ns = Object.create(null);
  62  /******/         __webpack_require__.r(ns);
  63  /******/         Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64  /******/         if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65  /******/         return ns;
  66  /******/     };
  67  /******/
  68  /******/     // getDefaultExport function for compatibility with non-harmony modules
  69  /******/     __webpack_require__.n = function(module) {
  70  /******/         var getter = module && module.__esModule ?
  71  /******/ 			function getDefault() { return module['default']; } :
  72  /******/ 			function getModuleExports() { return module; };
  73  /******/         __webpack_require__.d(getter, 'a', getter);
  74  /******/         return getter;
  75  /******/     };
  76  /******/
  77  /******/     // Object.prototype.hasOwnProperty.call
  78  /******/     __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79  /******/
  80  /******/     // __webpack_public_path__
  81  /******/     __webpack_require__.p = "";
  82  /******/
  83  /******/
  84  /******/     // Load entry module and return exports
  85  /******/     return __webpack_require__(__webpack_require__.s = "4dqW");
  86  /******/ })
  87  /************************************************************************/
  88  /******/ ({
  89  
  90  /***/ "1ZqX":
  91  /***/ (function(module, exports) {
  92  
  93  (function() { module.exports = window["wp"]["data"]; }());
  94  
  95  /***/ }),
  96  
  97  /***/ "4dqW":
  98  /***/ (function(module, __webpack_exports__, __webpack_require__) {
  99  
 100  "use strict";
 101  // ESM COMPAT FLAG
 102  __webpack_require__.r(__webpack_exports__);
 103  
 104  // EXTERNAL MODULE: ./node_modules/@babel/runtime/helpers/esm/extends.js
 105  var esm_extends = __webpack_require__("wx14");
 106  
 107  // EXTERNAL MODULE: external ["wp","element"]
 108  var external_wp_element_ = __webpack_require__("GRId");
 109  
 110  // EXTERNAL MODULE: external ["wp","data"]
 111  var external_wp_data_ = __webpack_require__("1ZqX");
 112  
 113  // EXTERNAL MODULE: external ["wp","deprecated"]
 114  var external_wp_deprecated_ = __webpack_require__("NMb1");
 115  var external_wp_deprecated_default = /*#__PURE__*/__webpack_require__.n(external_wp_deprecated_);
 116  
 117  // EXTERNAL MODULE: external "lodash"
 118  var external_lodash_ = __webpack_require__("YLtl");
 119  
 120  // EXTERNAL MODULE: external ["wp","compose"]
 121  var external_wp_compose_ = __webpack_require__("K9lf");
 122  
 123  // EXTERNAL MODULE: external ["wp","i18n"]
 124  var external_wp_i18n_ = __webpack_require__("l3Sj");
 125  
 126  // EXTERNAL MODULE: external ["wp","apiFetch"]
 127  var external_wp_apiFetch_ = __webpack_require__("ywyh");
 128  var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_);
 129  
 130  // EXTERNAL MODULE: external ["wp","url"]
 131  var external_wp_url_ = __webpack_require__("Mmq9");
 132  
 133  // EXTERNAL MODULE: external ["wp","components"]
 134  var external_wp_components_ = __webpack_require__("tI+e");
 135  
 136  // EXTERNAL MODULE: external ["wp","blocks"]
 137  var external_wp_blocks_ = __webpack_require__("HSyU");
 138  
 139  // CONCATENATED MODULE: ./node_modules/@wordpress/server-side-render/build-module/server-side-render.js
 140  
 141  
 142  
 143  /**
 144   * External dependencies
 145   */
 146  
 147  /**
 148   * WordPress dependencies
 149   */
 150  
 151  
 152  
 153  
 154  
 155  
 156  
 157  
 158  function rendererPath(block) {
 159    let attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
 160    let urlQueryArgs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
 161    return Object(external_wp_url_["addQueryArgs"])(`/wp/v2/block-renderer/$block}`, {
 162      context: 'edit',
 163      ...(null !== attributes ? {
 164        attributes
 165      } : {}),
 166      ...urlQueryArgs
 167    });
 168  }
 169  
 170  function DefaultEmptyResponsePlaceholder(_ref) {
 171    let {
 172      className
 173    } = _ref;
 174    return Object(external_wp_element_["createElement"])(external_wp_components_["Placeholder"], {
 175      className: className
 176    }, Object(external_wp_i18n_["__"])('Block rendered as empty.'));
 177  }
 178  
 179  function DefaultErrorResponsePlaceholder(_ref2) {
 180    let {
 181      response,
 182      className
 183    } = _ref2;
 184    const errorMessage = Object(external_wp_i18n_["sprintf"])( // translators: %s: error message describing the problem
 185    Object(external_wp_i18n_["__"])('Error loading block: %s'), response.errorMsg);
 186    return Object(external_wp_element_["createElement"])(external_wp_components_["Placeholder"], {
 187      className: className
 188    }, errorMessage);
 189  }
 190  
 191  function DefaultLoadingResponsePlaceholder(_ref3) {
 192    let {
 193      children,
 194      showLoader
 195    } = _ref3;
 196    return Object(external_wp_element_["createElement"])("div", {
 197      style: {
 198        position: 'relative'
 199      }
 200    }, showLoader && Object(external_wp_element_["createElement"])("div", {
 201      style: {
 202        position: 'absolute',
 203        top: '50%',
 204        left: '50%',
 205        marginTop: '-9px',
 206        marginLeft: '-9px'
 207      }
 208    }, Object(external_wp_element_["createElement"])(external_wp_components_["Spinner"], null)), Object(external_wp_element_["createElement"])("div", {
 209      style: {
 210        opacity: showLoader ? '0.3' : 1
 211      }
 212    }, children));
 213  }
 214  
 215  function ServerSideRender(props) {
 216    const {
 217      attributes,
 218      block,
 219      className,
 220      httpMethod = 'GET',
 221      urlQueryArgs,
 222      EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,
 223      ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,
 224      LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder
 225    } = props;
 226    const isMountedRef = Object(external_wp_element_["useRef"])(true);
 227    const [showLoader, setShowLoader] = Object(external_wp_element_["useState"])(false);
 228    const fetchRequestRef = Object(external_wp_element_["useRef"])();
 229    const [response, setResponse] = Object(external_wp_element_["useState"])(null);
 230    const prevProps = Object(external_wp_compose_["usePrevious"])(props);
 231    const [isLoading, setIsLoading] = Object(external_wp_element_["useState"])(false);
 232  
 233    function fetchData() {
 234      if (!isMountedRef.current) {
 235        return;
 236      }
 237  
 238      setIsLoading(true);
 239  
 240      const sanitizedAttributes = attributes && Object(external_wp_blocks_["__experimentalSanitizeBlockAttributes"])(block, attributes); // If httpMethod is 'POST', send the attributes in the request body instead of the URL.
 241      // This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
 242  
 243  
 244      const isPostRequest = 'POST' === httpMethod;
 245      const urlAttributes = isPostRequest ? null : sanitizedAttributes !== null && sanitizedAttributes !== void 0 ? sanitizedAttributes : null;
 246      const path = rendererPath(block, urlAttributes, urlQueryArgs);
 247      const data = isPostRequest ? {
 248        attributes: sanitizedAttributes !== null && sanitizedAttributes !== void 0 ? sanitizedAttributes : null
 249      } : null; // Store the latest fetch request so that when we process it, we can
 250      // check if it is the current request, to avoid race conditions on slow networks.
 251  
 252      const fetchRequest = fetchRequestRef.current = external_wp_apiFetch_default()({
 253        path,
 254        data,
 255        method: isPostRequest ? 'POST' : 'GET'
 256      }).then(fetchResponse => {
 257        if (isMountedRef.current && fetchRequest === fetchRequestRef.current && fetchResponse) {
 258          setResponse(fetchResponse.rendered);
 259        }
 260      }).catch(error => {
 261        if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
 262          setResponse({
 263            error: true,
 264            errorMsg: error.message
 265          });
 266        }
 267      }).finally(() => {
 268        if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
 269          setIsLoading(false);
 270        }
 271      });
 272      return fetchRequest;
 273    }
 274  
 275    const debouncedFetchData = Object(external_wp_compose_["useDebounce"])(fetchData, 500); // When the component unmounts, set isMountedRef to false. This will
 276    // let the async fetch callbacks know when to stop.
 277  
 278    Object(external_wp_element_["useEffect"])(() => () => {
 279      isMountedRef.current = false;
 280    }, []);
 281    Object(external_wp_element_["useEffect"])(() => {
 282      // Don't debounce the first fetch. This ensures that the first render
 283      // shows data as soon as possible
 284      if (prevProps === undefined) {
 285        fetchData();
 286      } else if (!Object(external_lodash_["isEqual"])(prevProps, props)) {
 287        debouncedFetchData();
 288      }
 289    });
 290    /**
 291     * Effect to handle showing the loading placeholder.
 292     * Show it only if there is no previous response or
 293     * the request takes more than one second.
 294     */
 295  
 296    Object(external_wp_element_["useEffect"])(() => {
 297      if (!isLoading) {
 298        return;
 299      }
 300  
 301      const timeout = setTimeout(() => {
 302        setShowLoader(true);
 303      }, 1000);
 304      return () => clearTimeout(timeout);
 305    }, [isLoading]);
 306    const hasResponse = !!response;
 307    const hasEmptyResponse = response === '';
 308    const hasError = response === null || response === void 0 ? void 0 : response.error;
 309  
 310    if (hasEmptyResponse || !hasResponse) {
 311      return Object(external_wp_element_["createElement"])(EmptyResponsePlaceholder, props);
 312    }
 313  
 314    if (hasError) {
 315      return Object(external_wp_element_["createElement"])(ErrorResponsePlaceholder, Object(esm_extends["a" /* default */])({
 316        response: response
 317      }, props));
 318    }
 319  
 320    if (isLoading) {
 321      return Object(external_wp_element_["createElement"])(LoadingResponsePlaceholder, Object(esm_extends["a" /* default */])({}, props, {
 322        showLoader: showLoader
 323      }), hasResponse && Object(external_wp_element_["createElement"])(external_wp_element_["RawHTML"], {
 324        className: className
 325      }, response));
 326    }
 327  
 328    return Object(external_wp_element_["createElement"])(external_wp_element_["RawHTML"], {
 329      className: className
 330    }, response);
 331  }
 332  
 333  // CONCATENATED MODULE: ./node_modules/@wordpress/server-side-render/build-module/index.js
 334  
 335  
 336  
 337  /**
 338   * WordPress dependencies
 339   */
 340  
 341  
 342  
 343  /**
 344   * Internal dependencies
 345   */
 346  
 347  
 348  /**
 349   * Constants
 350   */
 351  
 352  const EMPTY_OBJECT = {};
 353  const ExportedServerSideRender = Object(external_wp_data_["withSelect"])(select => {
 354    // FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
 355    // It is used by blocks that can be loaded into a *non-post* block editor.
 356    // eslint-disable-next-line @wordpress/data-no-store-string-literals
 357    const coreEditorSelect = select('core/editor');
 358  
 359    if (coreEditorSelect) {
 360      const currentPostId = coreEditorSelect.getCurrentPostId(); // For templates and template parts we use a custom ID format.
 361      // Since they aren't real posts, we don't want to use their ID
 362      // for server-side rendering. Since they use a string based ID,
 363      // we can assume real post IDs are numbers.
 364  
 365      if (currentPostId && typeof currentPostId === 'number') {
 366        return {
 367          currentPostId
 368        };
 369      }
 370    }
 371  
 372    return EMPTY_OBJECT;
 373  })(_ref => {
 374    let {
 375      urlQueryArgs = EMPTY_OBJECT,
 376      currentPostId,
 377      ...props
 378    } = _ref;
 379    const newUrlQueryArgs = Object(external_wp_element_["useMemo"])(() => {
 380      if (!currentPostId) {
 381        return urlQueryArgs;
 382      }
 383  
 384      return {
 385        post_id: currentPostId,
 386        ...urlQueryArgs
 387      };
 388    }, [currentPostId, urlQueryArgs]);
 389    return Object(external_wp_element_["createElement"])(ServerSideRender, Object(esm_extends["a" /* default */])({
 390      urlQueryArgs: newUrlQueryArgs
 391    }, props));
 392  });
 393  
 394  if (window && window.wp && window.wp.components) {
 395    window.wp.components.ServerSideRender = Object(external_wp_element_["forwardRef"])((props, ref) => {
 396      external_wp_deprecated_default()('wp.components.ServerSideRender', {
 397        since: '5.3',
 398        alternative: 'wp.serverSideRender'
 399      });
 400      return Object(external_wp_element_["createElement"])(ExportedServerSideRender, Object(esm_extends["a" /* default */])({}, props, {
 401        ref: ref
 402      }));
 403    });
 404  }
 405  
 406  /* harmony default export */ var build_module = __webpack_exports__["default"] = (ExportedServerSideRender);
 407  
 408  
 409  /***/ }),
 410  
 411  /***/ "GRId":
 412  /***/ (function(module, exports) {
 413  
 414  (function() { module.exports = window["wp"]["element"]; }());
 415  
 416  /***/ }),
 417  
 418  /***/ "HSyU":
 419  /***/ (function(module, exports) {
 420  
 421  (function() { module.exports = window["wp"]["blocks"]; }());
 422  
 423  /***/ }),
 424  
 425  /***/ "K9lf":
 426  /***/ (function(module, exports) {
 427  
 428  (function() { module.exports = window["wp"]["compose"]; }());
 429  
 430  /***/ }),
 431  
 432  /***/ "Mmq9":
 433  /***/ (function(module, exports) {
 434  
 435  (function() { module.exports = window["wp"]["url"]; }());
 436  
 437  /***/ }),
 438  
 439  /***/ "NMb1":
 440  /***/ (function(module, exports) {
 441  
 442  (function() { module.exports = window["wp"]["deprecated"]; }());
 443  
 444  /***/ }),
 445  
 446  /***/ "YLtl":
 447  /***/ (function(module, exports) {
 448  
 449  (function() { module.exports = window["lodash"]; }());
 450  
 451  /***/ }),
 452  
 453  /***/ "l3Sj":
 454  /***/ (function(module, exports) {
 455  
 456  (function() { module.exports = window["wp"]["i18n"]; }());
 457  
 458  /***/ }),
 459  
 460  /***/ "tI+e":
 461  /***/ (function(module, exports) {
 462  
 463  (function() { module.exports = window["wp"]["components"]; }());
 464  
 465  /***/ }),
 466  
 467  /***/ "wx14":
 468  /***/ (function(module, __webpack_exports__, __webpack_require__) {
 469  
 470  "use strict";
 471  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _extends; });
 472  function _extends() {
 473    _extends = Object.assign || function (target) {
 474      for (var i = 1; i < arguments.length; i++) {
 475        var source = arguments[i];
 476  
 477        for (var key in source) {
 478          if (Object.prototype.hasOwnProperty.call(source, key)) {
 479            target[key] = source[key];
 480          }
 481        }
 482      }
 483  
 484      return target;
 485    };
 486  
 487    return _extends.apply(this, arguments);
 488  }
 489  
 490  /***/ }),
 491  
 492  /***/ "ywyh":
 493  /***/ (function(module, exports) {
 494  
 495  (function() { module.exports = window["wp"]["apiFetch"]; }());
 496  
 497  /***/ })
 498  
 499  /******/ })["default"];


Generated: Wed Dec 1 01:00:03 2021 Cross-referenced by PHPXref 0.7.1