[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  /******/ (() => { // webpackBootstrap
   2  /******/     "use strict";
   3  /******/     // The require scope
   4  /******/     var __webpack_require__ = {};
   5  /******/     
   6  /************************************************************************/
   7  /******/     /* webpack/runtime/compat get default export */
   8  /******/     (() => {
   9  /******/         // getDefaultExport function for compatibility with non-harmony modules
  10  /******/         __webpack_require__.n = (module) => {
  11  /******/             var getter = module && module.__esModule ?
  12  /******/                 () => (module['default']) :
  13  /******/                 () => (module);
  14  /******/             __webpack_require__.d(getter, { a: getter });
  15  /******/             return getter;
  16  /******/         };
  17  /******/     })();
  18  /******/     
  19  /******/     /* webpack/runtime/define property getters */
  20  /******/     (() => {
  21  /******/         // define getter functions for harmony exports
  22  /******/         __webpack_require__.d = (exports, definition) => {
  23  /******/             for(var key in definition) {
  24  /******/                 if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  25  /******/                     Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  26  /******/                 }
  27  /******/             }
  28  /******/         };
  29  /******/     })();
  30  /******/     
  31  /******/     /* webpack/runtime/hasOwnProperty shorthand */
  32  /******/     (() => {
  33  /******/         __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
  34  /******/     })();
  35  /******/     
  36  /************************************************************************/
  37  var __webpack_exports__ = {};
  38  
  39  // EXPORTS
  40  __webpack_require__.d(__webpack_exports__, {
  41    "default": () => (/* binding */ index_default)
  42  });
  43  
  44  // UNUSED EXPORTS: ServerSideRender, useServerSideRender
  45  
  46  ;// external "ReactJSXRuntime"
  47  const external_ReactJSXRuntime_namespaceObject = window["ReactJSXRuntime"];
  48  ;// external ["wp","element"]
  49  const external_wp_element_namespaceObject = window["wp"]["element"];
  50  ;// external ["wp","i18n"]
  51  const external_wp_i18n_namespaceObject = window["wp"]["i18n"];
  52  ;// external ["wp","components"]
  53  const external_wp_components_namespaceObject = window["wp"]["components"];
  54  ;// external ["wp","data"]
  55  const external_wp_data_namespaceObject = window["wp"]["data"];
  56  ;// external ["wp","compose"]
  57  const external_wp_compose_namespaceObject = window["wp"]["compose"];
  58  ;// external ["wp","apiFetch"]
  59  const external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
  60  var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
  61  ;// external ["wp","url"]
  62  const external_wp_url_namespaceObject = window["wp"]["url"];
  63  ;// external ["wp","blocks"]
  64  const external_wp_blocks_namespaceObject = window["wp"]["blocks"];
  65  ;// ./node_modules/@wordpress/server-side-render/build-module/hook.js
  66  
  67  
  68  
  69  
  70  
  71  function rendererPath(block, attributes = null, urlQueryArgs = {}) {
  72    return (0,external_wp_url_namespaceObject.addQueryArgs)(`/wp/v2/block-renderer/$block}`, {
  73      context: "edit",
  74      ...null !== attributes ? { attributes } : {},
  75      ...urlQueryArgs
  76    });
  77  }
  78  function removeBlockSupportAttributes(attributes) {
  79    const {
  80      backgroundColor,
  81      borderColor,
  82      fontFamily,
  83      fontSize,
  84      gradient,
  85      textColor,
  86      className,
  87      ...restAttributes
  88    } = attributes;
  89    const {
  90      border,
  91      color,
  92      elements,
  93      shadow,
  94      spacing,
  95      typography,
  96      ...restStyles
  97    } = attributes?.style || {};
  98    return {
  99      ...restAttributes,
 100      style: restStyles
 101    };
 102  }
 103  function useServerSideRender(args) {
 104    const [response, setResponse] = (0,external_wp_element_namespaceObject.useState)({ status: "idle" });
 105    const shouldDebounceRef = (0,external_wp_element_namespaceObject.useRef)(false);
 106    const {
 107      attributes,
 108      block,
 109      skipBlockSupportAttributes = false,
 110      httpMethod = "GET",
 111      urlQueryArgs
 112    } = args;
 113    let sanitizedAttributes = attributes && (0,external_wp_blocks_namespaceObject.__experimentalSanitizeBlockAttributes)(block, attributes);
 114    if (skipBlockSupportAttributes) {
 115      sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
 116    }
 117    const isPostRequest = "POST" === httpMethod;
 118    const urlAttributes = isPostRequest ? null : sanitizedAttributes;
 119    const path = rendererPath(block, urlAttributes, urlQueryArgs);
 120    const body = isPostRequest ? JSON.stringify({ attributes: sanitizedAttributes ?? null }) : void 0;
 121    (0,external_wp_element_namespaceObject.useEffect)(() => {
 122      const controller = new AbortController();
 123      const debouncedFetch = (0,external_wp_compose_namespaceObject.debounce)(
 124        function() {
 125          {
 126            setResponse({ status: "loading" });
 127            external_wp_apiFetch_default()({
 128              path,
 129              method: isPostRequest ? "POST" : "GET",
 130              body,
 131              headers: isPostRequest ? {
 132                "Content-Type": "application/json"
 133              } : {},
 134              signal: controller.signal
 135            }).then((res) => {
 136              setResponse({
 137                status: "success",
 138                content: res ? res.rendered : ""
 139              });
 140            }).catch((error) => {
 141              if (error.name === "AbortError") {
 142                return;
 143              }
 144              setResponse({
 145                status: "error",
 146                error: error.message
 147              });
 148            }).finally(() => {
 149              shouldDebounceRef.current = true;
 150            });
 151          }
 152        },
 153        shouldDebounceRef.current ? 500 : 0
 154      );
 155      debouncedFetch();
 156      return () => {
 157        controller.abort();
 158        debouncedFetch.cancel();
 159      };
 160    }, [path, isPostRequest, body]);
 161    return response;
 162  }
 163  
 164  
 165  ;// ./node_modules/@wordpress/server-side-render/build-module/server-side-render.js
 166  
 167  
 168  
 169  
 170  
 171  
 172  const EMPTY_OBJECT = {};
 173  function DefaultEmptyResponsePlaceholder({ className }) {
 174    return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Placeholder, { className, children: (0,external_wp_i18n_namespaceObject.__)("Block rendered as empty.") });
 175  }
 176  function DefaultErrorResponsePlaceholder({ message, className }) {
 177    const errorMessage = (0,external_wp_i18n_namespaceObject.sprintf)(
 178      // translators: %s: error message describing the problem
 179      (0,external_wp_i18n_namespaceObject.__)("Error loading block: %s"),
 180      message
 181    );
 182    return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Placeholder, { className, children: errorMessage });
 183  }
 184  function DefaultLoadingResponsePlaceholder({ children }) {
 185    const [showLoader, setShowLoader] = (0,external_wp_element_namespaceObject.useState)(false);
 186    (0,external_wp_element_namespaceObject.useEffect)(() => {
 187      const timeout = setTimeout(() => {
 188        setShowLoader(true);
 189      }, 1e3);
 190      return () => clearTimeout(timeout);
 191    }, []);
 192    return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsxs)("div", { style: { position: "relative" }, children: [
 193      showLoader && /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(
 194        "div",
 195        {
 196          style: {
 197            position: "absolute",
 198            top: "50%",
 199            left: "50%",
 200            marginTop: "-9px",
 201            marginLeft: "-9px"
 202          },
 203          children: /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Spinner, {})
 204        }
 205      ),
 206      /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)("div", { style: { opacity: showLoader ? "0.3" : 1 }, children })
 207    ] });
 208  }
 209  function ServerSideRender(props) {
 210    const prevContentRef = (0,external_wp_element_namespaceObject.useRef)("");
 211    const {
 212      className,
 213      EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,
 214      ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,
 215      LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,
 216      ...restProps
 217    } = props;
 218    const { content, status, error } = useServerSideRender(restProps);
 219    (0,external_wp_element_namespaceObject.useEffect)(() => {
 220      if (content) {
 221        prevContentRef.current = content;
 222      }
 223    }, [content]);
 224    if (status === "loading") {
 225      return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(LoadingResponsePlaceholder, { ...props, children: !!prevContentRef.current && /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_element_namespaceObject.RawHTML, { className, children: prevContentRef.current }) });
 226    }
 227    if (status === "success" && !content) {
 228      return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(EmptyResponsePlaceholder, { ...props });
 229    }
 230    if (status === "error") {
 231      return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(ErrorResponsePlaceholder, { message: error, ...props });
 232    }
 233    return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_element_namespaceObject.RawHTML, { className, children: content });
 234  }
 235  function ServerSideRenderWithPostId({
 236    urlQueryArgs = EMPTY_OBJECT,
 237    ...props
 238  }) {
 239    const currentPostId = (0,external_wp_data_namespaceObject.useSelect)((select) => {
 240      const postId = select("core/editor")?.getCurrentPostId();
 241      return postId && typeof postId === "number" ? postId : null;
 242    }, []);
 243    const newUrlQueryArgs = (0,external_wp_element_namespaceObject.useMemo)(() => {
 244      if (!currentPostId) {
 245        return urlQueryArgs;
 246      }
 247      return {
 248        post_id: currentPostId,
 249        ...urlQueryArgs
 250      };
 251    }, [currentPostId, urlQueryArgs]);
 252    return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(ServerSideRender, { urlQueryArgs: newUrlQueryArgs, ...props });
 253  }
 254  
 255  
 256  ;// ./node_modules/@wordpress/server-side-render/build-module/index.js
 257  
 258  
 259  const ServerSideRenderCompat = ServerSideRenderWithPostId;
 260  ServerSideRenderCompat.ServerSideRender = ServerSideRenderWithPostId;
 261  ServerSideRenderCompat.useServerSideRender = useServerSideRender;
 262  var index_default = ServerSideRenderCompat;
 263  
 264  
 265  (window.wp = window.wp || {}).serverSideRender = __webpack_exports__["default"];
 266  /******/ })()
 267  ;


Generated : Sat Oct 25 08:20:05 2025 Cross-referenced by PHPXref