Skip to content

Instantly share code, notes, and snippets.

View adamcrampton's full-sized avatar
💪
Crushing it

Adam Crampton adamcrampton

💪
Crushing it
  • Sydney, Australia
View GitHub Profile
@adamcrampton
adamcrampton / commaStringFormatter.js
Created October 24, 2018 21:14
Takes a comma separated string and replaces the last comma with ", and"
commaStringFormatter = (speechArray) => {
formattedString = [speechArray.slice(0, -1).join(', '), speechArray.slice(-1)[0]].join(speechArray.length < 2 ? '' : ', and ');
return formattedString;
}
@adamcrampton
adamcrampton / collectionCombiner.php
Created October 28, 2018 23:13
Nested Collection each looping
collect($locales)->each(function (string $locale) {
collect($fields)->each(function (Field $field) use ($locale)
$this->doSomeWork($locale, $field);
});
});
@adamcrampton
adamcrampton / eloquentSearch.php
Created October 28, 2018 23:22
Simple Eloquent search snippet
User::query()
->where('name', 'LIKE', "%{$searchTerm}%")
->orWhere('email', 'LIKE', "%{$searchTerm}%")
->get();
@adamcrampton
adamcrampton / stripNonAlphaNumeric.js
Created October 29, 2018 02:13
Strip all non alphanumeric characters from a string
str = str.replace(/[\W_]+/g,'';
@adamcrampton
adamcrampton / momentTester.js
Created October 30, 2018 01:31
moment.js tester
// Requires moment.js
const moment = require('moment');
// Remove date parameter from moment method to get "now"
var formattedDate = moment('2018-10-29 13:57:37').format('h:mm A');
alert(formattedDate);
@adamcrampton
adamcrampton / spacetoHyphenRegex.js
Created November 5, 2018 00:34
Replace spaces with hyphens
someString = someString.replace(/\s+/g, '-');
@adamcrampton
adamcrampton / getAuthorPostCounts.sql
Created November 6, 2018 05:44
Get WordPress author post counts, within a date range, regardless of current access level
SELECT u.ID, u.display_name, COUNT(*)
FROM wp_posts p
RIGHT JOIN wp_users u ON p.post_author = u.ID
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.post_date > '2017-11-01 00:00:00'
AND p.post_date < '2018-11-01 00:00:00'
-- Add any exclusions below
-- AND p.post_author NOT IN (8962, 90013, 9256, 52277)
GROUP BY u.display_name
@adamcrampton
adamcrampton / differenceBetweenLetAndVar.js
Created November 6, 2018 20:50
Example code describing the difference between let and var in JavaScript
// They are very similar when used like this outside a function block.
let me = 'go'; // globally scoped
var i = 'able'; // globally scoped
// Global variables defined with let will not be added as properties on the global window object like those defined with var
console.log(window.me); // undefined
console.log(window.i); // 'able'
// They are identical when used like this in a function block.
function ingWithinEstablishedParameters() {
@adamcrampton
adamcrampton / responsiveIframe.html
Created November 19, 2018 23:39
Responsive iframe snippet
<style>
.resp-iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border:0;
}
</style>
@adamcrampton
adamcrampton / WPCommentQueries.sql
Last active November 21, 2018 23:53
Useful WordPress queries for comments table
-- All live comments
SELECT COUNT(*) FROM wp_comments
WHERE comment_approved = 1
AND comment_date > '2018-08-22'
-- Comment count grouped by author
SELECT COUNT(*) AS comment_count, comment_author, user_id FROM wp_comments
WHERE comment_approved = 1
AND comment_date > '2018-08-22'
GROUP BY user_id