Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/ wp-ops Public archive

WordPress Operations Library with semantics similar to WP CLI

License

Notifications You must be signed in to change notification settings

wplib/wp-ops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WP_Ops

WordPress Operations Library similar semantics to WP CLI

This library is for the creation of simple PHP scripts that can be used to initialize WordPress databases for:

  1. Setup for automated testing, and
  2. New and/or updated website deployments

Rationale

Since WP CLI will never be a library and it is a pain to setup and call WP CLI from a PHPUnit script or to trigger WP CLI via web host we decided to create an easy to use library for doing similar tasks.

Status

We have only implement the functionality we have needed to date, but pull requests for additional features will be considered. Or you can hire us to add more functionality.

Setup for Automated Testing

In lieu of more detailed docs awaiting time to better document, here is an example with detailed comments. This code is being used in the setUp() method for a PHPUnit test:

/**
 * Set the directory where post content can be found.
 *
 * Post content file names should be in this form:
 *
 *      {slug}.html
 *
 * Where {slug} is one of the columns for data passed in
 * as the first parameter to create_from, that uniquely
 * identifies a post, and is used to refer to the post
 * in other data sets defined on peer with the posts()
 * data set.
 *
 * Post content should include the syntax {{image_url}}
 * where you want to replace with an actual image URL.
 */
WP_Ops::set_assets_dir( __DIR__ . '/assets/html' );

/**
 * Delete all posts of type 'post' using the WordPress
 * API, regardless of status.
 *
 * After deletion, reset auto-increment for ID to
 * either 1 or the next lowest number of any remaining
 * posts give other post types might exist.
 */
WP_Ops::post( 'post' )->delete_all( 'reset=1' );

/**
 * Delete all post meta using the WordPress API,
 * regardless of post.
 *
 * After deletion, reset auto-increment for ID to
 * either 1 or the next lowest number of any remaining
 * posts give other post types might exist.
 *
 * Will not delete meta for posts that exist. To
 * delete meta for existing posts pass `ignore_parents=1'
 *
 */
WP_Ops::meta( 'post' )->delete_all( 'reset=1' );

/**
 * Delete all images in uploads directory except ones
 * that are referenced in a $post->guid field.
 *
 * To delete all regardless of $post->guid pass
 * ignore_guid=1
 */
WP_Ops::media()->delete_images( 'ignore_guid=' );

/**
 * Create image files from columnar data.
 *
 * This does NOT create attachments.
 *
 * The columnar data should be an array of arrays
 * where the sub-arrays represent rows in the database
 * with the first row being a header row, with these
 * values:
 *
 *      ['path','type','background','size','count','filename'],
 *
 * And an example row might look like this:
 *
 *        ['2018/08','png','#000000','100x100',2,'black'],
 *
 */
$images = WP_Ops::media()->create_images_from( $this->images() );

/**
 * Create posts from columnar data.
 *
 * The columnar data should be an array of arrays
 * where the sub-arrays represent rows in the database
 * with the first row being a header row, with these
 * values:
 *
 *      ['slug','status','type','title' ],
 *
 * And an example row might look like this:
 *
 *        ['google-atl','publish','post','Google Kicks Off Global Science Fair In Atlanta'],
 *
 */
$posts = WP_Ops::post()->create_from( $this->posts() );

/**
 * Associates media with posts from columnar data.
 *
 * The columnar data should be an array of arrays
 * where the sub-arrays represent rows in the database
 * with the first row being a header row, with these
 * values:
 *
 *      ['post_slug','media_path','type','meta_key'],
 *
 * And here are example rows for the four (4) different
 * potential types:
 *
 *        ['google-atl','2018/08/grey.jpg','embedded', ''],
 *        ['google-atl','2018/08/grey.jpg','attached', '' ],
 *        ['google-atl','2018/08/grey.jpg','thumbnail','' ],
 *        ['google-atl','2018/08/grey.jpg','custom',   '_image_url'],
 *
 * The next two (2) paramaters are an array of WP_Ops\Post
 * objects and an array of WP_Ops\Media objects, typically
 * generated by, respectively, WP_Ops::post()->create_from()
 * and WP_Ops::media()->create_images_from().
 */
WP_Ops::post()->associate_media_from( $this->post_images(), $posts, $images );

Example Data

The prior mentioned PHPUnit test also contains these following methods to provide the data used by the prior code:

/** 
 * @return array[]
 */
protected function posts() {
    return array(
        [ 'slug',       'status',   'type',  'title' ],
        [ 'atl-mayor',  'publish',  'post',  'Atlanta Mayor Pitches Plan For The Gulch In Tense Public Meeting' ],
        [ 'buford',     'publish',  'post',  'Empty dirt lot off Buford Highway in Brookhaven' ],
        [ 'exhibit',    'publish',  'post',  'Exhibit - With Drawn Arms: Glenn Kaino and Tommie Smith.' ],
        [ 'google-atl', 'publish',  'post',  'Google Kicks Off Global Science Fair In Atlanta' ],
        [ 'kemp',       'publish',  'post',  'Kemp’s $90M School Safety Plan Focuses On Mental Health, Local Control' ],
        [ 'marta',      'publish',  'post',  'MARTA Shares New Vision For Transit Expansion Around Atlanta' ],
        [ 'plains',     'publish',  'post',  'Carter, Abrams: Georgians From 2 Eras Chase Rural Votes' ],
        [ 'racism',     'publish',  'post',  'Early African-American Schools Refuted White Supremacists’ View' ],
        [ 'ryangravel', 'publish',  'post',  'Ryan Gravel\'s Next Big Idea: Eat, Drink, Save The World' ],
        [ 'ti-map',     'publish',  'post',  'MAP: T.I.’s Vision To Revive His Childhood Neighborhood' ],
        [ 'transit',    'publish',  'post',  'Growing Georgia Community Finds New Residents Pose Challenge For Transit' ],
    );
}

/**
 * @return array[]
 */
protected function images() {
    return array(
        [  'path',  'type', 'background', 'size',   'count', 'filename', ],
        [ '2018/08', 'png', '#000000',   '100x100',    2,     'black',   ],
        [ '2018/08', 'gif', '#ffffff',   '50x50',      5,     'white',   ],
        [ '2018/08', 'jpg', '#ff0000',   '250x250',    9,     'red',     ],
        [ '2018/08', 'png', '#00ff00',   '500x500',    3,     'lime',    ],
        [ '2018/08', 'gif', '#ffff00',   '1024x768',   4,     'yellow',  ],
        [ '2018/08', 'jpg', '#00ffff',   '640x480',    7,     'aqua',    ],
        [ '2018/08', 'png', '#0000ff',   '240x320',    2,     'blue',    ],
        [ '2018/08', 'gif', '#ff00ff',   '175x300',    8,     'fuschia', ],
        [ '2018/08', 'jpg', '#888888',   '100x100',    1,     'grey',    ],
        [ '2018/08', 'png', '#880000',   '50x50',      3,     'maroon',  ],
        [ '2018/08', 'gif', '#008800',   '250x250',    7,     'green',   ],

        [ '2018/09', 'jpg', '#888800',   '500x500',    4,     'olive',   ],
        [ '2018/09', 'png', '#008888',   '1024x768',   2,     'teal',    ],
        [ '2018/09', 'gif', '#000088',   '640x480',    3,     'navy',    ],
        [ '2018/09', 'jpg', '#880088',   '240x320',    6,     'purple',  ],
        [ '2018/09', 'png', '#000000',   '100x100',    2,     'black',   ],
        [ '2018/09', 'gif', '#ffffff',   '50x50',      5,     'white',   ],
        [ '2018/09', 'jpg', '#ff0000',   '250x250',    9,     'red',     ],
        [ '2018/09', 'png', '#00ff00',   '500x500',    3,     'lime',    ],
        [ '2018/09', 'gif', '#ffff00',   '1024x768',   4,     'yellow',  ],
        [ '2018/09', 'jpg', '#00ffff',   '640x480',    7,     'aqua',    ],

        [ '2018/10', 'png', '#0000ff',   '240x320',    2,     'blue',    ],
        [ '2018/10', 'gif', '#ff00ff',   '175x300',    8,     'fuschia', ],
        [ '2018/10', 'gif', '#888888',   '100x100',    1,     'grey',    ],
        [ '2018/10', 'png', '#880000',   '50x50',      3,     'maroon',  ],
        [ '2018/10', 'gif', '#008800',   '250x250',    7,     'green',   ],
        [ '2018/10', 'jpg', '#888800',   '500x500',    4,     'olive',   ],
        [ '2018/10', 'png', '#008888',   '1024x768',   2,     'teal',    ],
        [ '2018/10', 'gif', '#000088',   '640x480',    3,     'navy',    ],
        [ '2018/10', 'jpg', '#880088',   '240x320',    6,     'purple',  ],
    );
}

/**
 * @return array[]
 */
protected function post_images() {
    return array(
        [ 'post_slug',   'media_path',                      'type',      'meta_key' ],

        [ 'atl-mayor',   '2018/08/white.gif',               'embedded',    '' ],
        [ 'atl-mayor',   '2018/08/white-1.gif',             'embedded',    '' ],
        [ 'buford',      '2018/10/grey.gif',                'embedded',    '' ],
        [ 'buford',      '2018/10/grey.gif',                'embedded',    '' ],
        [ 'exhibit',     '2018/08/fuschia-1.gif',           'embedded',    '' ],
        [ 'exhibit',     '2018/10/fuschia-7-175x300.gif',   'embedded',    '' ],
        [ 'google-atl',  '2018/08/red-7.jpg',               'embedded',    '' ],
        [ 'google-atl',  '2018/08/grey.jpg',                'embedded',    '' ],
        [ 'kemp',        '2018/09/yellow-300x225.gif',      'embedded',    '' ],
        [ 'marta',       '2018/08/red-6.jpg',               'embedded',    '' ],
        [ 'plains',      '2018/08/blue-1.png',              'embedded',    '' ],
        [ 'racism',      '2018/08/black.png',               'embedded',    '' ],
        [ 'racism',      '2018/08/black-1.png',             'embedded',    '' ],
        [ 'racism',      '2018/08/maroon.png',              'embedded',    '' ],
        [ 'racism',      '2018/10/green-5.gif',             'embedded',    '' ],
        [ 'ryangravel',  '2018/09/lime-2.png',              'embedded',    '' ],
        [ 'ryangravel',  '2018/09/lime-2.png',              'embedded',    '' ],
        [ 'transit',     '2018/08/fuschia.gif',             'embedded',    '' ],
        [ 'transit',     '2018/08/fuschia-6.gif',           'embedded',    '' ],

        [ 'atl-mayor',   '2018/08/white.gif',               'attached',    '' ],
        [ 'buford',      '2018/08/grey.gif',                'attached',    '' ],
        [ 'exhibit',     '2018/08/white-1.gif',             'attached',    '' ],
        [ 'google-atl',  '2018/08/grey.jpg',                'attached',    '' ],
        [ 'kemp',        '2018/08/yellow.gif',              'attached',    '' ],
        [ 'marta',       '2018/08/red.jpg',                 'attached',    '' ],
        [ 'plains',      '2018/08/blue.png',                'attached',    '' ],
        [ 'racism',      '2018/08/black.png',               'attached',    '' ],
        [ 'ryangravel',  '2018/08/lime.png',                'attached',    '' ],
        [ 'ti-map',      '2018/08/aqua.jpg',                'attached',    '' ],
        [ 'transit',     '2018/08/fuschia.gif',             'attached',    '' ],

        [ 'atl-mayor',   '2018/08/white-3.gif',             'thumbnail',    '' ],
        [ 'buford',      '2018/08/grey.gif',                'thumbnail',    '' ],
        [ 'exhibit',     '2018/08/white-2.gif',             'thumbnail',    '' ],
        [ 'google-atl',  '2018/10/grey.gif',                'thumbnail',    '' ],
        [ 'kemp',        '2018/08/yellow-1.gif',            'thumbnail',    '' ],
        [ 'marta',       '2018/08/red-1.jpg',               'thumbnail',    '' ],
        [ 'plains',      '2018/08/blue-1.png',              'thumbnail',    '' ],
        [ 'racism',      '2018/08/black-1.png',             'thumbnail',    '' ],
        [ 'ryangravel',  '2018/08/lime-1.png',              'thumbnail',    '' ],
        [ 'ti-map',      '2018/08/aqua-1.jpg',              'thumbnail',    '' ],
        [ 'transit',     '2018/08/fuschia-1.gif',           'thumbnail',    '' ],

        [ 'atl-mayor',  '2018/08/lime.png',                 'custom',      '_image_url' ],
        [ 'ti-map',     '2018/08/fuschia.gif',              'custom',      '_image_url' ],
        [ 'racism',     '2018/09/olive.jpg',                'custom',      '_image_url' ],
        [ 'marta',      '2018/09/black-1.png',              'custom',      '_image_url' ],
        [ 'plains',     '2018/09/yellow-2.gif',             'custom',      '_image_url' ],
        [ 'exhibit',    '2018/08/grey.jpg',                 'custom',      '_image_url' ],
        [ 'ryangravel', '2018/10/teal-1.png',               'custom',      '_image_url' ],
        [ 'transit',    '2018/10/navy-2.gif',               'custom',      '_image_url' ],
        [ 'buford',     '2018/08/aqua-2.jpg',               'custom',      '_image_url' ],
        [ 'kemp',       '2018/10/maroon-2.png',             'custom',      '_image_url' ],
        [ 'google-atl', '2018/10/purple-2.jpg',             'custom',      '_image_url' ],

    );
}

New and/or updated website deployments

More to come here...

About

WordPress Operations Library with semantics similar to WP CLI

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages