WP Dev with Docker

I got stuck for a bit today on how to configure a PHP PDO connection in a Docker based WordPress dev env. Here’s the function…

/**
 * Get the database connection
 * @return \PDO database connection instance
 */
public function get_connection() {
    $dbname = \TMSC\tmsc_sync()::$tms_db_name;
    $username = \TMSC\tmsc_sync()::$tms_db_user;
    $password = \TMSC\tmsc_sync()::$tms_db_password;
    // Build the DSN string
    $host = \TMSC\tmsc_sync()::$tms_db_host;
    $port = '3306';
    if ( strpos( $host, ':' ) ) {
        list( $host, $port ) = explode( ':', $host );
    }

    $dsn = "mysql:host={$host};port={$port};dbname={$dbname}";

    $connection = new \PDO( $dsn, $username, $password, array(
        \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
    ) );

    // Since MySQL supports it, don't automatically quote parameters to allow control over data types.
    // This comes in useful for automatically manipulating the LIMIT clause.
    $connection->setAttribute( \PDO::ATTR_EMULATE_PREPARES, false );

    // Uncomment to enable connection debugging.
    $connection->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );

    $this->set_mysql_global_defaults( $connection );
    return $connection;
}

When this function connects to a Remote AWS/RDS database, we configure it with a FQD for the $host variable. But, when running the same function locally, within our Docker setup, we must use the docker container name “jetpack_mysql” (as defined in the docker-compose.yml file) and not “127.0.0.1” or “localhost”.

Gutenfied, redux

This is my first post on the site using [Gutenberg](https://github.com/WordPress/gutenberg).

My grandmother’s desk

I sit there, typing here

This is an “Image”. The one further up is a “Cover Image”. Neat, huh?

Regarding an Open Web

I just stumbled across a thing I wrote months ago at work, x-posting here for posterity:

I heard a segment recently on NPR’s Here And Now re: Facebook and Google together capturing 80-90% of online advertising. As so many web-based business models hinge on advertising (either directly or indirectly), this allows these two companies to “steer” the future of the web in so many ways.

The web I grew up on was about connecting to information (aka, content). Increasingly, today’s web is about connecting to people. Google and Facebook are coming at this intersection of content and people from opposite directions and at that intersection is the proverbial end of the rainbow, pot of gold.

I don’t have a full theory developed for this (yet), but I believe this centralization and consolidation, combined with increasingly algorithmic driven user experiences, reduces our online experience to normalized advertising units and pays no respect to the diversity of our offline/analog world. For me, the concept of an “open web” represents an alternative.

Testing GPX in WP

[lfh-gpx src=https://mark.drovdahl.com/wp-content/uploads/2018/01/Lake_Poway_Trail_Run_w_Katie.gpx title=”Lake_Poway_Trail_Run_w_Katie” color=blue width=5 ]Always good to sneak in a run while on vacation =)[/lfh-gpx]

…and two, if by sea

OUTBOUND, ~140 NM from Seattle, WA to Squamish, BC

September 11, 6:21am | Google Photos Link

IMG_20170911_062144
Departing Bainbridge Island, heading East to Shilshole Marina

September 11, 8:31am | Google Photos Link

IMG_20170911_083128
Passing Fort Flagler northbound via Admiralty Inlet

September 11, 9:12am | Google Photos Link

IMG_20170911_091231
Smith Island, halfway across Strait of Juan de Fuca

September 11, 10:54am | Google Photos Link

IMG_20170911_105450
Canadian Customs dock in Bedwell Harbour on South Pender Island

September 11, 11:52am | Google Photos Link

IMG_20170911_115205
Passing under the bridge which separates South and North Pender Islands

September 11, 2:48pm | Google Photos Link

IMG_20170911_144836
View to the South while running northbound in Howe Sound

Walk the line…endings

Have a file where you want to add a comma to the end of each line?

$ sed 's/$/,/' ip_addresses.txt | head -n2

This should work…but, wait, it didn’t. Instead, it replaced the first character of each line with a comma:

,6.127.0.0/17
,7.238.80.0/21

Ah, because the file is dos encoded, see the “\r \n”:

$ head -n1 ip_addresses.txt | od -c
0000000 9 6 . 1 2 7 . 0 . 0 / 1 7 \r \n
0000017

Ok, so dos2unix to the rescue:

$ dos2unix ip_addresses.txt
dos2unix: converting file ip_addresses.txt to Unix format...

And, finally:

$ sed 's/$/,/' ip_addresses.txt | head -n2
96.127.0.0/17,
87.238.80.0/21,

But, not so fast. We actually wanted commas on all but the last line:

$ sed 's/$/,/' ip_addresses.txt | tail -n2
103.246.150.0/23,
103.246.148.0/23,

So, use “$!” which matches lines which are NOT the last line:

$ sed '$!s/$/,/' ip_addresses.txt | tail -n2
103.246.150.0/23,
103.246.148.0/2