Sample .htaccess for Laravel with PHP FPM

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

#    RewriteEngine On
#    RewriteCond %{HTTP:Authorization} ^(Bearer\ )(.*)$ [NC]
#    RewriteRule ^(.*) $1?access_token=%2 [QSA]
#Header set Access-Control-Allow-Origin "*"
#Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE"
#Header set Access-Control-Allow-Headers "Content-Type, Authorization"
<IfModule mod_rewrite.c>

<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://kprep.api.ozandac.com/$1 [R,L]
RewriteEngine On
# Prevent direct access to the "public" folder - redirect to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /public/
RewriteRule ^public/(.*) /$1 [R=302,L]

# Redirect Trailing Slashes If Not A Folder...
# - but look for the file in the "public" folder
# (ensure we are not already in the "public" folder)
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{DOCUMENT_ROOT}/public/$1 !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]

# Rewrite "everything" to the "public" subdirectory if not already
# This ignores existing files/dirs in the document root
RewriteCond %{REQUEST_URI} ^/(.*)
RewriteRule !^public/ public/%1

# Handle Front Controller... (as before)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Gravityforms limit submission – once for user, unlimited for guest

	public function context() {

		// rule targets specific user, if this user is different we don't need to test the rule
		if ( $this->user_id != 'all' ) {
			if ( $this->user_id != get_current_user_id() ) {
				return false;
			}
			
		}
		if($this->user_id == "all")
		{
		    if(get_current_user_id()===0)
			{
			    return false;
			}
		}

		return true;
	}

File: gp-limit-submissions/includes/GPLS_Rule_User.php

Plugin: https://gravitywiz.com/documentation/gravity-forms-limit-submissions

How to add Buy Now button on Product page – WordPress/woocommerce?

Change &amp;&amp; to &&

add_filter('woocommerce_add_to_cart_redirect', 'malpani_redirect_checkout_add_cart');

        function malpani_redirect_checkout_add_cart()
        {
            if (isset($_POST["buynow"]) &amp;&amp; $_POST["buynow"] === "2") {
                return wc_get_checkout_url();
            }

        }

        add_filter('woocommerce_after_add_to_cart_button', 'malpani_add_buy_button');
        function malpani_add_buy_button()
        {
        echo '<input id="buynowinput" name="buynow" value="1" hidden>
            <button type="button" id="buy_now" class="buy_now button">Buy Now</button>
            <script>
                jQuery(document).ready(function ($) {
                    $("#buy_now").click(function (e) {
                        $("#buynowinput").val(2);
                        $(".single_add_to_cart_button").click();
                        e.stopPropagation();
                        e.preventDefault();
                    })
                });
            </script>';

How to get Gravity forms entry value by label?

function harsh_get_value_by_label( $form, $entry, $label ) {
	foreach ( $form['fields'] as $field ) {
		$lead_key = $field->label;
		
		if ( strtolower( $lead_key ) == strtolower( $label ) ) {
			return $entry[ $field->id ];
		}
		if($field->inputs != null and count($field->inputs))
		{
			foreach($field->inputs as $children)
			{
				$subLeadKey = $children["label"];
				if ( strtolower( $subLeadKey ) == strtolower( $label ) ) 
				{
					return $entry[ $children["id"] ];
				}
			}
		}
	}
	return false;
}

How to edit wordpress woocommerce order api responses (legacy + latest)

<?php
//// FOR LEGACY API ONLY
add_filter('woocommerce_api_order_response', 'harsh_add_shipping_phone_legacy', 99, 1);
function harsh_add_shipping_phone($order_data)
{
$order_data["billing_address"]["email"] = "XXXX";
$shipping_phone = get_post_meta($order_data["id"],"_shipping_phone",1);
if($shipping_phone!==false &amp;&amp; strlen($shipping_phone))
{
$order_data["billing_address"]["biller_phone"] = $order_data["billing_address"]["phone"];
$order_data["shipping_address"]["phone"] = $shipping_phone;
$order_data["billing_address"]["phone"] = $shipping_phone;
}
foreach($order_data["line_items"] as $k=>$v)
{
unset($order_data["line_items"][$k]["meta"]);
}
return $order_data;
}
//// FOR latest api
function wc_add_rest_order_meta( $response, $post, $request ) {
$order_data = $response->get_data();
$order_data["billing"]["email"] = "XXXX";
$sp = array_search('_shipping_phone', array_column($order_data["meta_data"], 'key'));
if($sp!==false)
{
$order_data["billing"]["biller_phone"] = $order_data["billing"]["phone"];
if(strlen($order_data["meta_data"][$sp]->value))
{
$order_data["shipping"]["phone"] = $order_data["meta_data"][$sp]->value;
$order_data["billing"]["phone"] = $order_data["meta_data"][$sp]->value;
}
}
foreach($order_data["line_items"] as $k=>$v)
{
unset($order_data["line_items"][$k]["meta_data"]);
}
$response->data = $order_data;
return $response;	
}
function wc_add_rest_order_meta2( $response, $post, $request ) {
foreach($response as $oneorder)
{
$order_data = $oneorder->get_data();
$order_data["billing"]["email"] = "XXXX";
$sp = array_search('_shipping_phone', array_column($order_data["meta_data"], 'key'));
if($sp!==false)
{
$order_data["billing"]["biller_phone"] = $order_data["billing"]["phone"];
if(strlen($order_data["meta_data"][$sp]->value))
{
$order_data["shipping"]["phone"] = $order_data["meta_data"][$sp]->value;
$order_data["billing"]["phone"] = $order_data["meta_data"][$sp]->value;
}
}
foreach($order_data["line_items"] as $k=>$v)
{
unset($order_data["line_items"][$k]["meta_data"]);
}
$oneorder->data = $order_data;
}
return $response;	
}
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'wc_add_rest_order_meta', 10, 3 );
add_filter( 'woocommerce_rest_prepare_shop_orders_object', 'wc_add_rest_order_meta2', 10, 3 );

Standalone script to print all published post IDs in WordPress

<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
define( 'SHORTINIT', true );
require( 'wp-load.php' );
if(isset($wpdb))
{
$qr = 'SELECT * FROM `'.$wpdb->prefix . 'posts` where `post_type`="post" and `post_status`="publish"';
$posts = $wpdb->get_results( $qr);
//$pr = implode(",",array_column($posts,"ID"));
$pr = json_encode(array_column($posts,"ID"));
echo $pr;
}

PHP caveats – int/float type conversion

The following code checks various scenarios in which inbuilt functions like intval, floatval, is_numeric, is_float and filter_var would result for several different types of values. Feel free to run in your local, leave comments if I can make the results more interesting with some more functions.

https://gist.github.com/harshvardhanmalpani/8dd0222e4821d0e495532abe9007fef1

Output / Results

Test Value 1: (string)true
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 2: (boolean)1
Integer converted Value: 1
Float converted Value: 1
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 3: (boolean)
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 4: (string)false
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 5: (integer)0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 6: (double)-0
Integer converted Value: 0
Float converted Value: -0
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 7: (integer)0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 8: (string)-0
Integer converted Value: 0
Float converted Value: -0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 9: (string)+0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 10: (string)-0.0
Integer converted Value: 0
Float converted Value: -0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 11: (string)+0.0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 12: (string)0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 13: (string)0w
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 14: (string)0.0t
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 15: (string)0.1
Integer converted Value: 0
Float converted Value: 0.1
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 16: (double)0.1
Integer converted Value: 0
Float converted Value: 0.1
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 17: (integer)1
Integer converted Value: 1
Float converted Value: 1
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 18: (string)1
Integer converted Value: 1
Float converted Value: 1
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 19: (double)10000
Integer converted Value: 10000
Float converted Value: 10000
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 20: (string)2e
Integer converted Value: 2
Float converted Value: 2
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 21: (double)20000
Integer converted Value: 20000
Float converted Value: 20000
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 22: (string)2e4
Integer converted Value: 20000
Float converted Value: 20000
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 23: (double)1337
Integer converted Value: 1337
Float converted Value: 1337
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 24: (string)1e3
Integer converted Value: 1000
Float converted Value: 1000
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 25: (double)10
Integer converted Value: 10
Float converted Value: 10
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 26: (double)1.3
Integer converted Value: 1
Float converted Value: 1.3
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 27: (string)1.4
Integer converted Value: 1
Float converted Value: 1.4
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 28: (string)1t
Integer converted Value: 1
Float converted Value: 1
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 29: (string)1.5n
Integer converted Value: 1
Float converted Value: 1.5
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 30: (integer)26
Integer converted Value: 26
Float converted Value: 26
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 31: (string)0x539
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 32: (integer)34
Integer converted Value: 34
Float converted Value: 34
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 33: (string)042
Integer converted Value: 42
Float converted Value: 42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 34: (integer)42
Integer converted Value: 42
Float converted Value: 42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 35: (integer)-42
Integer converted Value: -42
Float converted Value: -42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 36: (string)+42
Integer converted Value: 42
Float converted Value: 42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 37: (string)-42
Integer converted Value: -42
Float converted Value: -42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----

Jenkins auto deploying flask in test env with last build process kill and pipenv

#!/bin/sh
cd $WORKSPACE
export PY=/usr/local/bin/python3
export FLASK_RUN_PORT=4005
$PY -m pip install pipenv
$PY -m pipenv install --dev
export VENV_HOME_DIR=$($PY -m pipenv --venv)
source $VENV_HOME_DIR/bin/activate
flask db upgrade
flask populate-initial-data
export BUILD_ID=XYZapi
$WORKSPACE/killlastbuild.sh
flask run &amp;
deactivate

killlastbuild.sh can have anything inside it depending upon your project requirements. I put 2 process killers to kill background-ed daemon processes from previous build command

ps -Af | grep "xyzapi" | grep -v grep | awk '{print$2}' | head -1 | xargs kill -9
ps -Af | grep "xyzapi" | grep -v grep | awk '{print$2}' | head -1 | xargs kill -9

Total inode count for a particular user in linux

Inode usage for current working directory

echo "Detailed Inode usage for: $(pwd)" ; for d in `find -maxdepth 1 -type d |cut -d\/ -f2 |grep -xv . |sort`; do c=$(find $d |wc -l) ; printf "$c\t\t- $d\n" ; done ; printf "Total: \t\t$(find $(pwd) | wc -l)\n"

Use cd ~ to change pwd to current user home

cd ~

Reference:

Magesh from 2daygeek : https://www.2daygeek.com/linux-check-count-inode-usage/