{"id":14293170,"date":"2021-04-08T12:11:05","date_gmt":"2021-04-08T12:11:05","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/optimize-output-buffer-handler\/"},"modified":"2021-04-08T12:11:05","modified_gmt":"2021-04-08T12:11:05","slug":"optimize-output-buffer-handler","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/optimize-output-buffer-handler\/","title":{"rendered":"Optimize output buffer handler"},"content":{"rendered":"<p>Hi,<br \/>\nIn the class named <code>Cachify<\/code>, you use ob_start with custom output buffer handler: <code>ob_start( &#039;Cachify::set_cache&#039; );<\/code><\/p>\n<p>I suggest you replace the <code>set_cache<\/code> method with this one:<\/p>\n<pre><code>\t\/**\n\t * Assign the cache\n\t *\n\t * @since   0.1\n\t * @change  2.0\n\t *\n\t * @param   string $data  Content of the page.\n     * @param   int $phase    Bitmask of PHP_OUTPUT_HANDLER_* constants.\n     * @return  string        Content of the page.\n\t *\/\n\tpublic static function set_cache( $data, $phase ) {\n\n        if ($phase &amp; PHP_OUTPUT_HANDLER_FINAL || $phase &amp; PHP_OUTPUT_HANDLER_END) {\n            \/* Empty? *\/\n            if (empty($data)) {\n                return &#039;&#039;;\n            }\n\n            \/**\n             * Filters whether the buffered data should actually be cached\n             *\n             * @param bool   $should_cache  Whether the data should be cached.\n             * @param string $data          The actual data.\n             * @param object $method        Instance of the selected caching method.\n             * @param string $cache_hash    The cache hash.\n             * @param int    $cache_expires Cache validity period.\n             *\n             * @since 2.3\n             *\n             *\/\n            $should_cache = apply_filters(&#039;cachify_store_item&#039;, true, $data, self::$method, self::_cache_hash(), self::_cache_expires());\n\n            \/* Save? *\/\n            if ($should_cache) {\n                call_user_func(array(\n                    self::$method,\n                    &#039;store_item&#039;,\n                ), self::_cache_hash(), self::_minify_cache($data), self::_cache_expires(), self::_signature_details());\n            }\n        }\n\n\t\treturn $data;\n\t}<\/code><\/pre>\n<p>Checking the phase parameter helps to leverage processing as the output callback function called multiple times when <code>ob_flush()<\/code> involved.<br \/>\nExample:<br \/>\n<a href=\"http:\/\/sandbox.onlinephpfunctions.com\/code\/4dc11a69b0da536768f211199dd8c31d1ca50609\" rel=\"noopener\" target=\"_blank\">http:\/\/sandbox.onlinephpfunctions.com\/code\/4dc11a69b0da536768f211199dd8c31d1ca50609<\/a><\/p>\n","protected":false},"template":"","class_list":["post-14293170","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/14293170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/14293170\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=14293170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}