Skip to content

Instantly share code, notes, and snippets.

@sumitpore
sumitpore / process.md
Created February 20, 2020 15:11
How deliciousbrains's WordPress Backgrounnd Processing class work behind the scene

Usually, we first create a object of a class extending WP_Background_Processing class. Then we create a queue, save queue & dispatch queue.

Calling dispatch method does two things

1. Schedule Healthcheck cron to run every 5 minutes. @see `schedule_cron_healthcheck`
2. Call Ajax request that processes queue.

Ajax Request's callback is maybe_handle. @see WP_Background_Processing::maybe_handle(); This maybe_handle terminates itself if any batch of respective background class is already in process. So essentially, only one batch is allowed to execute at any point.

@sumitpore
sumitpore / git merge using CLI.txt
Last active March 17, 2020 09:27
Merging Through CLI
git fetch origin
git checkout -b "source-branch" "origin/source-branch"
git checkout "origin/target-branch"
git merge --no-ff "source-branch"
git commit -m "Commit Message"
git push origin HEAD:target-branch
@sumitpore
sumitpore / replace-tags.php
Created March 21, 2020 06:05
Example of Replacing Tags using PHP
<?php
/**
* Replaces <i> and <b> tags with <em> and <strong> tags.
*
* @param string $string The input string.
* @return string The filtered text.
*/
function replaceTagsIB($string) {
$pattern = array('`(<b)([^\w])`i', '`(<i)([^\w])`i');
$replacement = array("<strong$2", "<em$2");
@sumitpore
sumitpore / clone-object-excluding-keys.js
Created March 22, 2020 06:46
Copy/Clone Object excluding specific keys
function _objectWithoutProperties(obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) continue;
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
target[i] = obj[i];
}
return target;
}
@sumitpore
sumitpore / download-object-as-json-file-from-developer-tools.js
Created March 22, 2020 06:52
Downloading Object as a JSON file from Developer Tools
// Copy below code in console tab of developer tools
// Define JSON.decycle function
if (typeof JSON.decycle !== "function") {
JSON.decycle = function decycle(object, replacer) {
"use strict";
var objects = new WeakMap(); // object to path mappings
return (function derez(value, path) {
@sumitpore
sumitpore / Commands.txt
Created April 4, 2020 09:09
Installing pcov extension on MacOS to be used with Local by Flywheel
git clone https://github.com/krakjoe/pcov.git
cd pcov
/usr/local/bin/phpize
./configure
export INCLUDE_PATH=/Applications/Local.app/Contents/Resources/extraResources/lightning-services/php-7.3.5+3/bin/darwin/include
make CPPFLAGS="-I${INCLUDE_PATH}/php -I${INCLUDE_PATH}/php/main -I${INCLUDE_PATH}/php/TSRM -I${INCLUDE_PATH}/php/Zend -I${INCLUDE_PATH}/php/ext -I${INCLUDE_PATH}/php/ext/date/lib"
sudo make install
sudo cp $(php-config --extension-dir)/pcov.so /usr/local/php/extensions
@sumitpore
sumitpore / escape_array.php
Created April 20, 2020 09:28
Array Escape in WordPress. To be used with 'IN' keyword
/**
* Format Array to make useful in SQL Queries
*
* @param array $array Array to be escaped.
* @return string
*/
function escape_array( $array = array() ) {
global $wpdb;
$escaped = array();
foreach ( $array as $k => $v ) {
@sumitpore
sumitpore / chrome-local-storage-api.js
Last active October 7, 2024 11:02
Chrome's Local StorageArea API in Synchronous way for use in Chrome extensions. Replace 'chrome.storage.local' by 'chrome.storage.sync' if you want to use Sync StorageArea
/**
* Retrieve object from Chrome's Local StorageArea
* @param {string} key
*/
const getObjectFromLocalStorage = async function(key) {
return new Promise((resolve, reject) => {
try {
chrome.storage.local.get(key, function(value) {
resolve(value[key]);
});
@sumitpore
sumitpore / sort-object-by-keys.js
Last active April 28, 2020 08:56
Sort Object by Keys
function sortObjectByKeys(obj) {
var keys = Object.keys(obj).sort(function keyOrder(k1, k2) {
if (k1 < k2) return -1;
else if (k1 > k2) return +1;
else return 0;
});
var i, after = {};
for (i = 0; i < keys.length; i++) {
@sumitpore
sumitpore / closest.js
Created May 3, 2020 07:28
jQuery closest's in Pure JavaScript
if (window.Element && !Element.prototype.closest) {
Element.prototype.closest =
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i,
el = this;
do {
i = matches.length;
while (--i >= 0 && matches.item(i) !== el) {};
} while ((i < 0) && (el = el.parentElement));