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
----

How to migrate Customers from Magento2 to WordPress?

Lets break it into 2 parts:

a. Migrate Customer data
b. Migrate Customer Passwords

1) Migrate Customer Data from Magento2 to WordPress

This is rather easy to be honest, there are several plugins for importing data from CSV and then mapping the CSV fields.

My recommended plugin is Customer/Order/Coupon CSV Import Suite by SkyVerge

It basically does everything from customer email to customer address data. It also provides column mapping and custom attribute import for all users. Also comes with multiple configuration options.

Best Free Alternative is https://wordpress.org/plugins/import-users-from-csv-with-meta
But in this one, you can not import magento2 passwords as of v 1.15.0.1
I will update about the solution very soon. So as of now only paid option is there.

2) Migrate Customer Password from Magento2 to WordPress

This wordpress plugin checks and updates passwords of users migrated from Magento2 to WordPress
Plugin Link: https://github.com/harshvardhanmalpani/migrate-password-from-magento2-to-wordpress

Requirements:

  1. user_pass column will have this kind data from Magento2 dcbdc524f215fd054502dcad5a23a702ec029c02ff8d7051d049f76e29927f8b:C8yVqeuPfkHWvkmipx0iKLPtOUGETpLL:1
  2. usermeta table must have a meta_key “migrated_cs” for this user, meta_value can be anything positive
  3. This plugin file password-migrator.php should be in wp-content/plugins and must be an active plugin

What is does?

For new wordpress based users which dont have anything to do with magento2, it doesnt do anything

For all migrated users (those have the key “migrated_cs”), this plugin checks if input password matches old password from magento2 or not, if matched, it clears the key migrated_cs and updates the password using wordpress’ algorithm ; else results false.

How to batch edit multiple images to make square sized pictures with original image in center using php imagejpeg?

<?php
// WORKS FOR JPEG IMAGES ONLY. FOR PNG, change function names
// also create a folder named "out" in the $datadir folder
///only 4 lines to be edited
//1. dir in which your image files are present
$datadir="J:/dgcert/";
//2. Expected min. Width of the output file
$_outw=840;
//3. Expected min. Height of the output file
$_outh=840;
//4. output folder (inside datadir)
$outfolder= '/out/';
//5. force final size -- -- If set to 1/true then line 2 and 3 will become max sizes of output image
// TO BE DONE IN FUTURE . does not work as of now
$forcesize=0;
////// no more edits needed
$files=array_diff(scandir($datadir),array(".",".."));
foreach ($files as $file)
{
if(is_file($datadir.'/'.$file)){		
$outw=$_outw;
$outh=$_outh;
$mugl_im= imagecreatefromjpeg($datadir.$file);
list($thiswidth, $thisheight)= getimagesize($datadir.$file);
if($outw<$thiswidth) $outw=$thiswidth;
if($outh<$thisheight) $outh=$thisheight;
if($outw<$outh)$outw=$outh;
if($outh<$outw)$outh=$outw;
//echo $width . $height;exit;
$thumb = imagecreatetruecolor($outw, $outh);
//white color rgb
$bg = imagecolorallocate ( $thumb, 255, 255, 255 );
imagefilledrectangle($thumb,0,0,$outw,$outh,$bg);
imagecopyresized($thumb,$mugl_im,ceil(($outw-$thiswidth)/2),ceil(($outh-$thisheight)/2),0,0,$thiswidth,$thisheight,$thiswidth,$thisheight);
imagejpeg($thumb,$datadir.$outfolder.$file);
imagedestroy($thumb);
imagedestroy($mugl_im);
}
}
?>

Sort keys of an array based on order of values in another array – PHP

$a=array(56,7,6,8,5,34);
$b=array(
array(6,"34er34",44),
array(8,"34d34",44),
array(7,"343th4",44),
array(34,"343d4",44),
array(56,"3434",44),
array(5,"343gh4",44));
$d=array_column($b,0);
var_dump($d);
$g=[];
foreach($a as $c)
{
$f=array_search($c,$d);
if($f!==false)
$g[]=$b[$f];
}
var_export($g);