### Install WP Rocket with Composer Source: https://github.com/wp-media/wp-rocket/blob/develop/README.md This command installs the WP Rocket plugin as a Composer dependency. After installation, users must manually define `WP_ROCKET_EMAIL` and `WP_ROCKET_KEY` constants in their `wp-config.php` file for license validation and full plugin functionality. ```bash composer require wp-media/wp-rocket ``` -------------------------------- ### PHP Cron Expression Parser Usage Examples Source: https://github.com/wp-media/wp-rocket/blob/develop/inc/Dependencies/ActionScheduler/lib/cron-expression/README.md Demonstrates how to use the `CronExpression` class in PHP. Examples include working with predefined scheduling definitions, complex expressions, calculating future/past run dates by skipping iterations, and calculating dates relative to a specific time. ```PHP isDue(); echo $cron->getNextRunDate()->format('Y-m-d H:i:s'); echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s'); // Works with complex expressions $cron = Cron\CronExpression::factory('3-59/15 2,6-12 */15 1 2-5'); echo $cron->getNextRunDate()->format('Y-m-d H:i:s'); // Calculate a run date two iterations into the future $cron = Cron\CronExpression::factory('@daily'); echo $cron->getNextRunDate(null, 2)->format('Y-m-d H:i:s'); // Calculate a run date relative to a specific time $cron = Cron\CronExpression::factory('@monthly'); echo $cron->getNextRunDate('2010-01-12 00:00:00')->format('Y-m-d H:i:s'); ``` -------------------------------- ### Install PHP Cron Expression Parser with Composer Source: https://github.com/wp-media/wp-rocket/blob/develop/inc/Dependencies/ActionScheduler/lib/cron-expression/README.md To add the PHP Cron Expression Parser library to your project, include the specified dependency in your project's `composer.json` file. This allows Composer to download and manage the library. ```JavaScript { "require": { "mtdowling/cron-expression": "1.0.*" } } ``` -------------------------------- ### WP Rocket Gulp Development Tasks Reference Source: https://github.com/wp-media/wp-rocket/blob/develop/README.md This section provides a reference for various Gulp commands used in WP Rocket development. These tasks automate the building, minification, and watching of CSS and JavaScript assets, streamlining the development workflow. ```APIDOC Gulp Tasks: CSS Tasks: `gulp build:saas:unmin`: Builds Full admin CSS, the unminified version (wpr-admin.css) `gulp build:saas:min`: Builds Full admin CSS, the minified version (wpr-admin.min.css) `gulp build:sass:all`: Builds all admin CSS files (wpr-admin.css, wpr-admin.min.css, wpr-admin-rtl.css, wpr-admin-rtl.min.css) `gulp sass:watch`: Watches all admin CSS files mentioned above and builds them again with any change. JS Tasks: `gulp build:js:app:unmin`: Builds admin app js file, the unminified version (wpr-admin.js) `gulp build:js:app:min`: Builds admin app js file, the minified version (wpr-admin.min.js) `gulp build:js:lazyloadcss:min`: Builds lazyload CSS js file, the minified version (lazyload-css.min.js) `gulp build:js:all`: Builds all js files mentioned above (wpr-admin.js, wpr-admin.min.js, lazyload-css.min.js, wpr-beacon) `gulp build:js:beacon`: Builds beacon script (wpr-beacon.min.js, source file, and map file) `gulp js:watch`: Watches all js files changes and build them again with any change. ``` -------------------------------- ### Apply Background Styles to Elements with Class Starting 'top' Source: https://github.com/wp-media/wp-rocket/blob/develop/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/inline.html This CSS rule applies specific background properties to HTML elements whose 'class' attribute starts with 'top'. It sets a solid background color, and then overlays two background images with different positions and repeat behaviors. A commented-out line shows an alternative background image path. ```CSS [class|="top"] { background: #3dd83d; background: url(https://fastly.picsum.photos/id/976/200/300.jpg?hmac=s1Uz9fgJv32r8elfaIYn7pLpQXps7X9oYNwC5XirhO8) right bottom no-repeat, url(https://rocketlabsqa.ovh/wp-content/rocket-test-data/images/fixtheissue.jpg) left top repeat; /* background : url("/wp-content/rocket-test-data/images/wp-rocket.svg") no-repeat; */ } ``` -------------------------------- ### Center and Open New Popup Window Source: https://github.com/wp-media/wp-rocket/blob/develop/tests/Fixtures/inc/Engine/CDN/CDN/HTML/siteURL/original.html Defines the NSLPopupCenter function to open a new browser window or popup centered on the screen. It adjusts dimensions for mobile devices and ensures the new window gains focus. Parameters include the URL, title, width, and height of the desired window. ```JavaScript (function (undefined) {var _targetWindow ="prefer-popup"; window.NSLPopupCenter = function (url, title, w, h) { var userAgent = navigator.userAgent, mobile = function () { return /\b(iPhone|iP[ao]d)/.test(userAgent) || /\b(iP[ao]d)/.test(userAgent) || /Android/i.test(userAgent) || /Mobile/i.test(userAgent); }, screenX = window.screenX !== undefined ? window.screenX : window.screenLeft, screenY = window.screenY !== undefined ? window.screenY : window.screenTop, outerWidth = window.outerWidth !== undefined ? window.outerWidth : document.documentElement.clientWidth, outerHeight = window.outerHeight !== undefined ? window.outerHeight : document.documentElement.clientHeight - 22, targetWidth = mobile() ? null : w, targetHeight = mobile() ? null : h, V = screenX < 0 ? window.screen.width + screenX : screenX, left = parseInt(V + (outerWidth - targetWidth) / 2, 10), right = parseInt(screenY + (outerHeight - targetHeight) / 2.5, 10), features = []; if (targetWidth !== null) { features.push('width=' + targetWidth); } if (targetHeight !== null) { features.push('height=' + targetHeight); } features.push('left=' + left); features.push('top=' + right); features.push('scrollbars=1'); var newWindow = window.open(url, title, features.join(',')); if (window.focus) { newWindow.focus(); } return newWindow; }; ``` -------------------------------- ### Initialize WordPress Emoji Support Source: https://github.com/wp-media/wp-rocket/blob/develop/tests/Fixtures/inc/Engine/CDN/CDN/HTML/siteURL/rewrite.html This JavaScript snippet initializes WordPress emoji support by setting up base URLs for emoji assets and defining a function to test browser capabilities for rendering emojis (flags and standard emojis). It then loads necessary emoji scripts based on these checks. ```javascript window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0- ``` -------------------------------- ### Apply Basic CSS Styles to a Test Element Source: https://github.com/wp-media/wp-rocket/blob/develop/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/input_w_bg_image_lcp.html This CSS snippet defines a class named '.test' that sets the element's width to 100%, height to 400px, and assigns a background image from a specified URL. This is a common pattern for styling sections or components on a webpage. ```CSS .test{ width: 100%; height: 400px; background-image: url("http://example.org/wp-content/uploads/image.jpg"); } ``` -------------------------------- ### NSL Popup Center and WebView Detection Utility Source: https://github.com/wp-media/wp-rocket/blob/develop/tests/Fixtures/inc/Engine/CDN/CDN/HTML/siteURL/rewrite.html This comprehensive JavaScript utility provides two main functionalities: `NSLPopupCenter` for opening and centering new popup windows, and `checkWebView` for robustly detecting if the current environment is a web view. The web view detection includes detailed logic for parsing user agents to identify OS, browser, and browser version, and then applies specific heuristics for iOS and Android platforms. It also includes helper functions for version parsing and semantic version normalization. ```JavaScript (function (undefined) {var _targetWindow ="prefer-popup"; window.NSLPopupCenter = function (url, title, w, h) { var userAgent = navigator.userAgent, mobile = function () { return /\b(iPhone|iP[ao]d)/.test(userAgent) || /\b(iP[ao]d)/.test(userAgent) || /Android/i.test(userAgent) || /Mobile/i.test(userAgent); }, screenX = window.screenX !== undefined ? window.screenX : window.screenLeft, screenY = window.screenY !== undefined ? window.screenY : window.screenTop, outerWidth = window.outerWidth !== undefined ? window.outerWidth : document.documentElement.clientWidth, outerHeight = window.outerHeight !== undefined ? window.outerHeight : document.documentElement.clientHeight - 22, targetWidth = mobile() ? null : w, targetHeight = mobile() ? null : h, V = screenX < 0 ? window.screen.width + screenX : screenX, left = parseInt(V + (outerWidth - targetWidth) / 2, 10), right = parseInt(screenY + (outerHeight - targetHeight) / 2.5, 10), features = []; if (targetWidth !== null) { features.push('width=' + targetWidth); } if (targetHeight !== null) { features.push('height=' + targetHeight); } features.push('left=' + left); features.push('top=' + right); features.push('scrollbars=1'); var newWindow = window.open(url, title, features.join(',')); if (window.focus) { newWindow.focus(); } return newWindow; }; var isWebView = null; function checkWebView() { if (isWebView === null) { function _detectOS(ua) { switch (true) { case /Android/.test(ua): return "Android"; case /iPhone|iPad|iPod/.test(ua): return "iOS"; case /Windows/.test(ua): return "Windows"; case /Mac OS X/.test(ua): return "Mac"; case /CrOS/.test(ua): return "Chrome OS"; case /Firefox/.test(ua): return "Firefox OS"; } return ""; } function _detectBrowser(ua) { var android = /Android/.test(ua); switch (true) { case /CriOS/.test(ua): return "Chrome for iOS"; case /Edge/.test(ua): return "Edge"; case android && /Silk\//.test(ua): return "Silk"; case /Chrome/.test(ua): return "Chrome"; case /Firefox/.test(ua): return "Firefox"; case android: return "AOSP"; case /MSIE|Trident/.test(ua): return "IE"; case /Safari\//.test(ua): return "Safari"; case /AppleWebKit/.test(ua): return "WebKit"; } return ""; } function _detectBrowserVersion(ua, browser) { switch (browser) { case "Chrome for iOS": return _getVersion(ua, "CriOS/"); case "Edge": return _getVersion(ua, "Edge/"); case "Chrome": return _getVersion(ua, "Chrome/"); case "Firefox": return _getVersion(ua, "Firefox/"); case "Silk": return _getVersion(ua, "Silk/"); case "AOSP": return _getVersion(ua, "Version/"); case "IE": return /IEMobile/.test(ua) ? _getVersion(ua, "IEMobile/") : /MSIE/.test(ua) ? _getVersion(ua, "MSIE ") : _getVersion(ua, "rv:"); case "Safari": return _getVersion(ua, "Version/"); case "WebKit": return _getVersion(ua, "WebKit/"); } return "0.0.0"; } function _getVersion(ua, token) { try { return _normalizeSemverString(ua.split(token)[1].trim().split(/[^\w\.]/)[0]); } catch (o_O) { } return "0.0.0"; } function _normalizeSemverString(version) { var ary = version.split(/[\._]/); return (parseInt(ary[0], 10) || 0) + "." + (parseInt(ary[1], 10) || 0) + "." + (parseInt(ary[2], 10) || 0); } function _isWebView(ua, os, browser, version, options) { switch (os + browser) { case "iOSSafari": return false; case "iOSWebKit": return _isWebView_iOS(options); case "AndroidAOSP": return false; case "AndroidChrome": return parseFloat(version) >= 42 ? /; wv/.test(ua) : /\d{2}\.0\.0/.test(version) ? true : _isWebView_Android(options); } return false; } function _isWebView_iOS(options) { var document = (window["document"] || {}); if ("WEB_VIEW" in options) { return options["WEB_VIEW"]; } return !("fullscreenEnabled" in document || "webkitFullscreenEnabled" in document || false); } function _isWebView_Android(options) { if ("WEB_VIEW" in options) { return options["WEB_VIEW"]; } return !("requestFileSystem" in window || "webkitRequestFileSystem" in window || false); } var options = {}; var nav = window.navigator || {}; var ua = nav.userAgent || ""; var os = _detectOS(ua); var browser = _detectBrowser(ua); var browserVersion = _detectBrowserVersion(ua, browser); isWebView = _isWebView(ua, os, browser, browserVersion, options); } return isWebView; } window._nsl.push(function ($) { var targetWindow = _targetWindow || 'prefer-popup'; $('a[data-plugin="nsl"][data-action="connect"],a[data-plugin="nsl"][data-action="link"]').on('click', function (e) { var $target = $(this), href = $target.att ``` -------------------------------- ### Load Missive Chat Widget Script Source: https://github.com/wp-media/wp-rocket/blob/develop/tests/Fixtures/inc/Engine/CDN/CDN/HTML/siteURL/rewrite.html This JavaScript line loads the Missive chat widget script. It constructs the script URL using a global configuration object `w.MissiveChatConfig.id` and assigns it to the `src` property of an existing script element `s`. It also includes conditional checks for URL filters and mobile user agents, though these are empty in the provided snippet. ```JavaScript s.src = 'https://webchat.missiveapp.com/' + w.MissiveChatConfig.id + '/missive.js'; if (/filter/.test(window.location.href)) {}; if ( /Mobi|Android/i.test(navigator.userAgent)) {}; ``` -------------------------------- ### Initialize WordPress Emoji Detection and Settings Source: https://github.com/wp-media/wp-rocket/blob/develop/tests/Fixtures/inc/Engine/CDN/CDN/HTML/siteURL/original.html This JavaScript code initializes WordPress emoji settings, defines a function to test emoji rendering capabilities using a canvas, and loads necessary emoji scripts. It checks for browser support for various emoji types (flag, standard emoji) and sets global support flags. ```JavaScript window.\_wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/","svgExt":".svg","source":{"concatemoji":"https:\/\/example.org\/wp-includes\/js\/wp-emoji-release.min.js?ver=5.2.2"}}; !function(a,b,c){function d(a,b){var c=String.fromCharCode;l.clearRect(0,0,k.width,k.height),l.fillText(c.apply(this,a),0,0);var d=k.toDataURL();l.clearRect(0,0,k.width,k.height),l.fillText(c.apply(this,b),0,0);var e=k.toDataURL();return d===e}function e(a){var b;if(!l||!l.fillText)return!1;switch(l.textBaseline="top",l.font="600 32px Arial",a){case"flag":return!(b=d(\[55356,56826,55356,56819\],\[55356,56826,8203,55356,56819\]))&&(b=d(\[55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447\],\[55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447\]),!b);case"emoji":return b=d(\[55357,56424,55356,57342,8205,55358,56605,8205,55357,56424,55356,57340\],\[55357,56424,55356,57342,8203,55358,56605,8203,55357,56424,55356,57340\]),!b}return!1}function f(a){var c=b.createElement("script");c.src=a,c.defer=c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var g,h,i,j,k=b.createElement("canvas"),l=k.getContext&&k.getContext("2d");for(j=Array("flag","emoji"),c.supports={everything:!0,everythingExceptFlag:!0},i=0;i