Skip to content

Instantly share code, notes, and snippets.

@janzikmund
janzikmund / Postman-pre-request-script-csrf-token-laravel-sanctum.js
Last active October 25, 2024 17:52
Postman Pre-Request script to append CSRF token in header for POST requests in Laravel Sanctum authenticated SPA. Requires active environment with {{url}} variable defined for main app domain.
/**
* Postman Pre-Request script to append CSRF token in header for POST requests in Laravel
* Sanctum authenticated SPA. Requires active environment with {{url}} variable defined
* for main app domain.
*
* Postman Interceptor allows appending cookies from browser, but Laravel CSRF middleware
* only validates CSRF in headers or in _token form field, not in cookies. Axios automatically
* appends the CSRF from cookie to headers, but Postman cannot access intercepted cookies
* and use them, so we have to do one pre-request to get the CSRF token, store it
* in environment so it can be reused, and then append it to headers.
@janzikmund
janzikmund / Redirect Old Indexed Website Content to new URLs.md
Last active September 21, 2022 15:07
Redirect indexed content of old website to new URLs, get list of indexed URLs

Redirect Old Indexed Website Content to new URLs

1. Grab all indexed URLs

  1. Install ginfinity chrome extension to allow infinite scrolling through Google results
  2. Search for all indexed urls using query like site:example.com
  3. Scroll through all the results (hope there is not too much :)
  4. Once all results are shown, open Dev Tools JS console and run this command (document.querySelectorAll('.g .r > a')).forEach(function(el) { console.log(el.href) });

2. Setup redirects in .htaccess

@janzikmund
janzikmund / disable-XMLRPC-in-WordPress-using-htaccess
Created July 25, 2022 06:52
Disable XMLRPC in WordPress using .htaccess
# Disable XMLRPC
<Files xmlrpc.php>
Order Allow,Deny
Deny from all
</Files>
@janzikmund
janzikmund / Magento transfer storeview to default.php
Last active November 12, 2021 22:08
Move / merge all product attributes from Magento storeview level to Default Scope and erases them on storeview level so Default takes priority everywhere
<?php
/* Script to migrate all data from storeview 1 to Default Scope (storeview 0)
@author - Jan Zikmund
*/
$store_from = 1;
$store_to = 0;
$tables = array(
'catalog_product_entity_varchar',
'catalog_product_entity_text',
@janzikmund
janzikmund / .htaccess
Last active April 26, 2021 07:38
redirect-non-www-http-to-www-https.htaccess
# Redirect from non-www or http to https www
<IfModule mod_rewrite.c>
RewriteEngine On
# exclude acme challenge urls from rewriting to not mess Let's Encrypt
RewriteRule ^\.well-known\/acme-challenge\/ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.jandhqualitykitchens.com.au/$1 [R=301,L]
@janzikmund
janzikmund / SaveQuietly.php
Created December 11, 2019 03:35
Laravel eloquent trait to add method for save model without triggering observers
<?php
namespace App\Traits;
/**
* Allows saving models without triggering observers
*/
trait SaveQuietly
{
/**
* Save model without triggering observers on model
@janzikmund
janzikmund / WordPress add sitemap link to robots.txt.php
Last active May 6, 2020 22:01
WordPress add sitemap_index.xml link to robots.txt
@janzikmund
janzikmund / CustomModel.php
Created January 6, 2020 19:29
Laravel eloquent model method to enable save with composite primary key
/**
* Set the composite keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(Builder $query)
{
$query
->where('user_id', '=', $this->getAttribute('user_id'))
@janzikmund
janzikmund / wp-version-enqueued-assets-styles-scripts-by-theme-plugin-version.php
Last active September 21, 2019 07:12
WordPress version enqueued styles and scripts
// theme
add_action( 'wp_enqueue_scripts', function() {
if ( ! is_admin() ) {
$version = wp_get_theme()->get( 'Version' );
wp_enqueue_style( 'child-theme', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', [], $version, 'all' );
}
});
// plugin
add_action( 'wp_enqueue_scripts', function() {
@janzikmund
janzikmund / simple-wordpress-logging-function.php
Created July 1, 2019 05:03
Simple WordPress logging function
/**
* Logs output into wp-content/logs/asana-api-debug.log
*/
protected function log($message)
{
$line = '[' . date('Y-m-d H:i:s') . '] ' . $message . "\n";
file_put_contents($this->logFile, $line, FILE_APPEND);
}
/**