Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Schinkel mikeschinkel

View GitHub Profile
@mikeschinkel
mikeschinkel / example.php
Last active Jul 27, 2021
How we might be able to move to using parameterized queries in WordPress core
View example.php
<?php
// Put this in wp-config.php
define('PREPARED_SQL_REQUIRED',true);
// A site builder who wants to use prepared statements and parameterized queries
// could run these in various hooks before $wpdb->query() below is run.
$question_fragment = $wpdb->prepare( '`question_id` = %d', $question_id );
$answer_fragment = $wpdb->prepare( '`answer_name` = %s', $new_answer );
$wpdb->compose('UPDATE %s polls SET vote = vote+1 WHERE %s AND %s',
@mikeschinkel
mikeschinkel / make_literal.php
Created Jun 26, 2021
A make_literal() function to get around functions that use is_literal() over-zealously
View make_literal.php
<?php
$safe_var = 'all your base they belong to us';
file_put_contents('/tmp/exploit.txt',$safe_var );
// imagine lots of stuff going on here...
$safe_var = file_get_contents('/tmp/exploit.txt');
function make_literal(string $non_literal):string {
$literal = '';
@mikeschinkel
mikeschinkel / try-break.php
Last active Mar 30, 2021
Illustrating the try-break pattern in action for PHP 8.x — hopefully to see PHP > 8.0 allow replacing `do{...}while(false};` with a naked `try{}`
View try-break.php
<?php
/**
* This shows our try-break example in action.
*/
function main() {
do {
$result = example(0,"hello");
if ($result->is_error() ) {
$result->print_error();
@mikeschinkel
mikeschinkel / _main.php
Last active Mar 16, 2021
Strawman psuedo-code for set of potential autoloader related classes for PHP core for proposing to internals
View _main.php
<?php
use PHP\Autoload;
use PHP\ClassmapLoader;
use PHP\CallableLoader;
Autoload::set_loader(new ClassmapLoader([
"Foo" => "path/to/Foo.php",
"Bar" => "another/path/to/Bar.php",
"Baz" => "yet/another/path/to/Baz.php",
@mikeschinkel
mikeschinkel / go.mod
Last active Feb 25, 2021
Simple GoLang App to demonstrate Mountebank's inability to support HTTP CONNECT method.
View go.mod
module proxy-client
go 1.15
require (
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93
)
@mikeschinkel
mikeschinkel / 1.attributes-with-use.php
Last active Aug 18, 2020
Hypothetical example of using use statement for attributes
View 1.attributes-with-use.php
<?php
namespace MyNamespace;
// namespace use
use WpAttributes\PrimaryKey;
use WpAttributes\PostType;
use WpAttributes\TemplateVariable;
use PHP\VirtualReadonly;
@mikeschinkel
mikeschinkel / 0.readme.md
Last active May 17, 2020
Refactoring Pantheon_Sessions::initialize_session_override() using do{...}while(false);
View 0.readme.md

Example use of do {...} while(false)

See this gist for background.

This example comes from my work here when looking for a solution for this issue related to this WordPress plugin.

I recognized I could make it clearer using the do {...} while(false) pattern so I wanted to document here.

Note:

@mikeschinkel
mikeschinkel / Apis.php
Last active May 14, 2020
PHP Examples that could benefit from Try...Break (method excerpts from real production code)
View Apis.php
<?php
class Apis {
static function JsonGET( string $api_url, array $args = array() ) {
do {
$wp_error = null;
$args = wp_parse_args( $args, array(
'response_type' => ARRAY_A,
) );
@mikeschinkel
mikeschinkel / form1.php
Last active May 6, 2020
Examples showing a huge array literal vs. many small array literals
View form1.php
<?php
class MembershipForm {
static function form() {
return array(
array(
'type' => 'textfield',
'heading' => __( 'License Type ID' ),
'param_name' => 'license_type',
'description' => esc_attr__( 'Enter License Type ID', 'wds-vc-elements' ),
@mikeschinkel
mikeschinkel / module.audio.mp3.php
Created Mar 16, 2020
module.audio.mp3.php from WordPress where decodeMPEGaudioHeader() is partially refactored from over 600 lines to ~200 lines.
View module.audio.mp3.php
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// see readme.txt for more details //
/////////////////////////////////////////////////////////////////
// //