Skip to content

Instantly share code, notes, and snippets.

Mike Schinkel mikeschinkel

View GitHub Profile
@mikeschinkel
mikeschinkel / dowhilefalse-example.php
Created Apr 25, 2019
do {...} while (false) example assuming WordPress/PHP
View dowhilefalse-example.php
function some_function($value) {
do {
if ( some_condition ) {
break;
}
if ( some_other_condition ) {
break;
}
if ( some_further_condition ) {
break;
@mikeschinkel
mikeschinkel / benchmark-for-single-wordpress-action-hook
Created Apr 25, 2019
Benchmark for single WordPress action hook
View benchmark-for-single-wordpress-action-hook
<?php
define( 'ITERATIONS', 1000000 );
require( __DIR__ . '/wp-load.php' );
add_action('my_action', '__return_null' );
header('Content-type: text/plain');
time_it( 'hook an action with no parameters: ', function($condition) { do_action('my_action'); });
@mikeschinkel
mikeschinkel / earlyreturns-vs-dowhilefalse-benchmark.php
Created Apr 25, 2019
Benchmarks for PHP's Early Returns vs. Do While False
View earlyreturns-vs-dowhilefalse-benchmark.php
<?php
define( 'ITERATIONS', 10000000 );
$earlyreturn = time_it( 'early returns (w/value): ', function($condition) { earlyreturn1($condition); });
$dowhilefalse = time_it( 'do while false (w/value):', function($condition) { dowhilefalse1($condition); });
printf( "\nPercent increase (w/value): %.3f", ($dowhilefalse-$earlyreturn)/$dowhilefalse );
printf( "\nPercent difference (w/value): %.3f", 1-($dowhilefalse-$earlyreturn)/$dowhilefalse );
$earlyreturn = time_it( "early returns (no value): ", function($condition) { earlyreturn2($condition); });
@mikeschinkel
mikeschinkel / wp-config.php
Last active Feb 19, 2019
Update candidate for wp-config.php in WPLib Box per https://github.com/wplib/wplib-box/issues/579
View wp-config.php
<?php
if ( getcwd()=='/var/www' && isset( $_SERVER[ 'HTTP_HOST' ] ) ) {
$hostname = $_SERVER[ 'HTTP_HOST' ];
} else if ( is_file( __DIR__ . '/../HOSTNAME' ) ) {
$hostname = file_get_contents( __DIR__ . '/../HOSTNAME' );
} else {
$hostname = 'wplib.box';
}
@mikeschinkel
mikeschinkel / _about.md
Last active Jan 9, 2019
PHP module for use in WordPress to display Bibblio v3.9 related posts element.
View _about.md

The code in feature-bibblio-related-posts.php does not work and I think there is a bug in Bibblio's JS file.

prepareModuleOptions() calls getCustomUniqueIdentifierFromUrl() but does not pass it the options argument.

Note: feature-bibblio-related-posts.php is using a very thin framework called RPLib that I wrote. It auto-loads "modules" where different type modules have different traits available.

@mikeschinkel
mikeschinkel / reading-time.php
Last active Nov 9, 2018
Number of seconds to read an image, per Medium's algorithm: https://blog.medium.com/read-time-and-you-bc2048ab620c
View reading-time.php
<?php
function image_reading_seconds( $count ) {
// Every image start with 3 seconds
$reading_seconds = $count * 3;
// The first 9 images also add the cumulative
// amount of seconds calculated by 10 seconds
// minus the image count for each image.
$reading_seconds += 9 >= $count
@mikeschinkel
mikeschinkel / forms_model.php
Created Oct 27, 2018
Proposal for GravityForms to add a 'gform_custom_field_name_clauses' filter.
View forms_model.php
<?php
class GFFormsModel {
public static function get_custom_field_names() {
global $wpdb;
$clauses = apply_filters( 'gform_custom_field_name_clauses', array(
'found_rows' => '',
'distinct' => 'DISTINCT',
'fields' => 'meta_key',
@mikeschinkel
mikeschinkel / fix-gravity-forms.php
Created Oct 27, 2018
Fix GravityForms GFFormsModel::get_custom_field_names() for large numbers of posts and post meta by limiting to post_type='page'
View fix-gravity-forms.php
<?php
/**
* Class Fix_Gravity_Forms
*/
class Fix_Gravity_Forms {
const POST_TYPE = 'page';
const SQL_MARKER = "WHERE meta_key NOT BETWEEN '_' AND '_z'";
@mikeschinkel
mikeschinkel / pimple-example.php
Created Oct 18, 2018
The Problems with Pimple - What's wrong with this picture?
View pimple-example.php
<?php
$container = new Pimple();
$container['auth'] = function($c) {
return new OAuth();
};
$container['db'] = function($c) {
return new DB();
};
$container['tweet_service'] = function($c) {
$twService = new TwitterService();
@mikeschinkel
mikeschinkel / args-example.php
Created Oct 17, 2018
Example passing optional $args
View args-example.php
<?php
/**
* @param array $args
*
* @return string
*/
function get_banner_image_html( $size = 'medium', $args = array() ){
$args = array_merge( array(
'before' => '<figure class="show__banner-image">',
'after' => '</figure>',
You can’t perform that action at this time.