Skip to content

Instantly share code, notes, and snippets.

View nydame's full-sized avatar

Felicia Betancourt nydame

View GitHub Profile
<?php
function stop_modified_date_update( $new, $old ) {
$new['post_modified'] = $old['post_modified'];
$new['post_modified_gmt'] = $old['post_modified_gmt'];
return $new;
}
add_filter( 'wp_insert_post_data', 'stop_modified_date_update', 10, 2 );
// do stuff that updates post(s) here
@nydame
nydame / viz-diffing.js
Created January 10, 2019 19:37
Visual diffing script
/*
### How to use [Puppeteer](https://github.com/GoogleChrome/puppeteer) for visual diffing
#### Install the following NPM packages: puppeteer, pixemmatch, fs-extra and pngjs (first time only) in the directory where you want to keep your tests
```npm install --save-dev puppeteer pixelmatch fs-extra pngjs```
#### Create your testing script (first time only); what follows is a simplified example that tests only 2 pages
```
*/
const puppeteer = require('puppeteer');
const pixelmatch = require('pixelmatch');
const fs = require('fs-extra');
# Your init script
#
# Atom will evaluate this file each time a new window is opened. It is run
# after packages are loaded/activated and after the previous editor state
# has been restored.
#
# An example hack to log to the console when each text editor is saved.
#
# atom.workspace.observeTextEditors (editor) ->
# editor.onDidSave ->
@nydame
nydame / CookieMaster.js
Last active October 20, 2018 14:34
This JavaScript module lets you store information on the client with localStorage or sessionStorage with a fallback to cookies.
// import CookieMaster
// create an instance, e.g., ```cm = new CookieMaster("localStorage");```
// run ```cm.checkStoreKey(yourKey, yourValue);``` to store your information, providing that the key doesn't already exist
class CookieMaster {
constructor(type) {
// type = "sessionStorage" or "localStorage"; latter is default
this.type = type || "localStorage";
}
@nydame
nydame / conditional-cache-buster.php
Created October 4, 2018 22:28
Conditional cache-busting for WordPress stylesheets
// Tired of making changes to your WordPress site's stylesheet, only to have the client tell you "It looks the same to me!"?
// Force WordPress to send a fresh copy to logged-in users with this simple code snippet.
// (In practice, put this code in your WordPress theme (e.g., in functions.php) or in a custom plugin.)
$now = new DateTime();
$now_version = $now->format('YmdHis');
if (is_user_logged_in()) {
wp_enqueue_style( 'pacificbiosciences-style', get_stylesheet_uri(), array(), $now_version );
} else {
wp_enqueue_style( 'pacificbiosciences-style', get_stylesheet_uri() );
}
@nydame
nydame / Integration of Visual Diffing into Google Puppeteer for Easy End-to-end Testing
Last active August 9, 2020 10:20
How to incorporate PixelMatch into a Node.js script that runs Google Puppeteer
const puppeteer = require('puppeteer');
const pixelmatch = require('pixelmatch');
const fs = require('fs-extra');
const PNG = require('pngjs').PNG;
const testNum = 0; // EDIT before each individual test
const dPath = 'apple.com-20180704'; // EDIT before each set of tests
const filenameBase = 'apple-test';
const pathBase = dPath + testNum + '/' + filenameBase;
(async () => {
[6] pry(#<Post>):1> nesting
Nesting status:
--
0. main (Pry top level)
1. #<Post>
[7] pry(#<Post>):1> self.to_s
=> "#<Post:0x00007fdad6ce57a8>"
[8] pry(#<Post>):1> self.title = "This is a Title Made with Pry"
=> "This is a Title Made with Pry"
[9] pry(#<Post>):1> self.title
@nydame
nydame / restrict_media_permissions.php
Last active May 20, 2018 16:25
Control who can delete Media Library files on a WordPress site on the basis of a capability
</php
// In this example, only logged-in users with the 'activate_plugins' capability will be able to delete files shown in the Media Library.
// (By default, only administrators have this capability.)
// This code should be put in functions.php or in a plugin.
add_action('delete_attachment', array($this, 'restrict_media_deletion_permissions'), 1);
public function restrict_media_deletion_permissions($post_id) {
if (! current_user_can('activate_plugins')) {
?>
<script type="text/javascript">alert("You do not have permission to delete media files.");</script>
<?php
@nydame
nydame / array_to_list.md
Last active May 20, 2018 16:17
Convert a 1-dimensional array to a parameter list (JavaScript)

#Array-to-parameter-list conversion

##During a recent mock technical interview, I was asked to find the largest of a series of numbers stored in an array. The first solution I hit upon was to feed all the array members to Math.max(), but I just couldn't figure out how to do it, and I ended up taking another approach. When the interview was over, the interviewer gave me two ways I could've made my original idea work.

###(1) Use apply()

const numberArray = [12, 45, 0, 7];
return Math.max.apply( null, numberArray ); // 45

###(2) Use the new spread operator

@nydame
nydame / add_async_to_script.php
Last active February 19, 2019 23:01
WordPress filter that will add "async" to an enqueued script
#Assuming the script has already been enqueued with 'my-script' as a handle, add this to functions.php or to a plugin
add_filter( 'script_loader_tag', 'add_async_to_my_script', 10, 3 );
function add_async_to_my_script( $tag, $handle, $src ) {
if ( $handle === 'my_script') {
$tag = '<script type="text/javascript" scr="' . esc_url($src) . '" async></script>';
}
return $tag;
}