Download Remote Files Using Node

Here is a simple code snippet to help you use node to download a remote file URL to your computer

const http = require('http');
const fs = require('fs');
const url = 'https://path/to/remote/data.js';
const path = 'data.js';


/**
 *
 * @param {string} url The URL location of the file you wish to download
 * @param {string} destination The path to which you wish to write the content of the remote file.
 * @param {function} success A success callback
 * @param {function} error An error callback
 */
const download = (url, destination, success, error) => {
	var file = fs.createWriteStream(destination);
	var request = http.get(url, function (response) {
		response.pipe(file);
		file.on('finish', function () {
			file.close(function () {
				typeof success == 'function' && success.apply(null, [destination]);
			}); 
		});
	}).on('error', function (err) { 
		fs.unlink(destination); // Delete file is error
		typeof error == 'function' && error.apply(null, [err.message]);
	});
};


download(url, path, function (location) {
	console.log(location);
}, function (err) {
	console.log(err);
});

Get Post By post_name

$post_name = 'my-post';

$querystr = 'SELECT * FROM "wp_posts" WHERE "post_name" = "' $post_name . '"';

$results = $wpdb->get_results($querystr, OBJECT);


if(!empty($results)) {
	foreach ($results as $result) {
		// do your magic
		break;
	}
} else {
	echo 'post '. $post_name . ' was not found.';
}

// see: https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

htaccess: set and use environment variables inside your htaccess

A while ago I learned how to set a variable in my .htaccess that can later on be user in PHP:

.htaccess

PHP:

Something that I could not figure out was how to use the defined variable inside the htaccess. While searching for CORS implementation, I ran across this post on stackoverflow.com:

There there is the answer to my question, how to set and get an environment variable in htaccess:

SetEnvIf Origin "^(.*\.example\.com)$" ORIGIN_SUB_DOMAIN=$1

    Header set Access-Control-Allow-Origin "%{ORIGIN_SUB_DOMAIN}e" env=ORIGIN_SUB_DOMAIN

htaccess: allow access control from subdomains

When having an application running on the top domain of your site you may want to allow subdomains to make ajax requests to the top domain. If that is the case, and if you do not know which subdomain will be making the request, you should consider adding the following rule to you .htaccess file in your top domain:

SetEnvIf Origin "^(.*\.(appcropolis|simplifysites|localhost.+)\.com)$" ORIGIN_SUB_DOMAIN=$1
Header set Access-Control-Allow-Origin "%{ORIGIN_SUB_DOMAIN}e" env=ORIGIN_SUB_DOMAIN