Bootstrapping your plugin

24
BOOTSTRAPPING YOUR PLUGIN Marko Heijnen WordCamp NYC 2014

Transcript of Bootstrapping your plugin

Page 1: Bootstrapping your plugin

BOOTSTRAPPING YOUR PLUGINMarko HeijnenWordCamp NYC 2014

Page 2: Bootstrapping your plugin

MARKO HEIJNEN

•Founder  of  CodeKitchen  

•Work  for  1&1  

•Lead  developer  of  GlotPress  

•Core  contributor  for  WordPress

WORDPRESS DEVELOPER

The Netherlands

Page 3: Bootstrapping your plugin

TODAY’STOPICS

• Features of a plugin• Setting up your plugin• Automation of your tasks

Page 4: Bootstrapping your plugin

THE START

You have a great idea and want to build it. You first start by writing what it should do and plan ahead.

Page 5: Bootstrapping your plugin

INTERNAT

IONALIZ

ATIO

N

CSS / JAVA

SCRIPT

VERSION CONTR

OL

COMPILING LE

SS/SASSCONCAT

ENATE

JSHINT PHPUNIT

/ QUNIT

COMPRESS

PLUGIN HEADERS & README

BUILD PROCESS

MINIFICAT

ION

GUI DESIG

N

USER EXPERIENCE

INTERFA

CE DESIGN

LOCALIZ

ATIO

N

KEEP UP TO DAT

E

LOGOTY

PES

REQUIREMENTS OF A PLUGIN

USER WORKFL

OWS

Page 6: Bootstrapping your plugin

SETTING UP YOUR PLUGIN

Page 7: Bootstrapping your plugin

DO IT YOUR SELF

Doing the same things again, over and over

Page 8: Bootstrapping your plugin

MAIN FILE

<?php /* Plugin Name: Tabify edit screen Plugin URI: http://rocksta.rs/plugin/tabify-edit-screen Description: Enables tabs in the edit screen and manage them from the back-end Author: Marko Heijnen Text Domain: tabify-edit-screen Version: 0.8.3 Author URI: http://markoheijnen.com */

Page 9: Bootstrapping your plugin

USE A SCAFFOLD

wp  scaffold  plugin  my-­‐cool-­‐plugin  ![--plugin_name=<title>]What to put in the ‘Plugin Name:’ header[--skip-tests]Don’t generate files for unit testing.[--activate]Activate the newly generated plugin.

Page 10: Bootstrapping your plugin

AND YOU WRITE YOUR PLUGINBut creating your plugin isn’t all about the code. It also mean maintenance of it.

Page 11: Bootstrapping your plugin

SETTING UP YOUR UNIT TESTS

wp  scaffold  plugin-­‐tests  

• phpunit.xml is the configuration file for PHPUnit• .travis.yml is the configuration file for

Travis CI• tests/bootstrap.php is the file that makes

the current plugin active when running the test suite• tests/test-sample.php is a sample file

containing the actual tests11

Page 12: Bootstrapping your plugin

SETTING UP YOUR UNIT TESTS

My  basic  tests  are:  

• test_tested_up_to• test_stable_tag• test_package_json

12

h>ps://github.com/markoheijnen/tabify-­‐edit-­‐screen/blob/master/tests/test-­‐plugin.php

Page 13: Bootstrapping your plugin

AUTOMATION OF YOUR TASKS

Page 14: Bootstrapping your plugin

YOUR NORMALLYTASKS

• Minify CSS/JS• Compress CSS/JS/Images• Concatenate CSS/JS• Validate JS• Create new pot file• Download translations• Unit test• Deploy

Page 15: Bootstrapping your plugin

GRUNT

•Running  tasks  by  using  CLI  

•Easy  to  use,  harder  to  configure  

•Extendable  with  your  own  plugins  

•Uses  npm  for  plugin  management  

THE JAVASCRIPT TASK RUNNER

http://gruntjs.com

Page 16: Bootstrapping your plugin

THE BASIC SETUP

A typical setup will involve adding two files to your project: package.json and the Gruntfile

• package.jsonThis file is used by npm to store metadata for projects published as npm modules. You will list grunt and the Grunt plugins your project needs as devDependencies in this file.• Gruntfile

Is used to configure or define tasks and load Grunt plugins.

Page 17: Bootstrapping your plugin

PACKAGE.JSON{

"name": "tabify-edit-screen",

"version": "0.8.3",

"description": "Enables tabs in the edit screen and manage them from the back-end",

"repository": { "type": “git", "url": “https://github.com/markoheijnen/tabify-edit-screen.git" },

"author": "Marko Heijnen",

"license": "GPLv2 or later",

"devDependencies": {

"grunt": "~0.4.5",

"grunt-contrib-clean": "~0.5.0",

"grunt-contrib-copy": "~0.5.0",

"grunt-contrib-cssmin": "~0.10.0",

"grunt-contrib-uglify": "~0.5.0",

"grunt-glotpress": "~0.1.0",

"grunt-wp-i18n": "~0.4.6" }

}

Page 18: Bootstrapping your plugin

INSTALLING DEPENDENCIES

• npm install• This will install all plugins inside

node_modules

Page 19: Bootstrapping your plugin

GRUNTFILE.JS

module.exports = function(grunt) { grunt.initConfig({ });};

Page 20: Bootstrapping your plugin

WHICH I’M USING

Copy  files  and  folders. Compress  CSS  files.

InternaPonalize  WordPress  themes  and  plugins

Gets  translaPons  from  a  GlotPress  installaPon

Clean  files  and  folders.

Minify  javascript  files  with  UglifyJS.

GRUNT-CONTRIB-CLEAN GRUNT-CONTRIB-COPY GRUNT-CONTRIB-CSSMIN

GRUNT-CONTRIB-UGLIFY GRUNT-WP-I18N GRUNT-GLOTPRESS

Page 21: Bootstrapping your plugin

CREATING POT FILE

grunt.initConfig({ makepot: { core: { options: { domainPath: '/languages', type: 'wp-plugin', } } },});!grunt.loadNpmTasks(‘grunt-wp-i18n’);!grunt.registerTask('default', ['makepot:core']);

Page 22: Bootstrapping your plugin

DOWNLOADING TRANSLATIONS

grunt.initConfig({ glotpress_download: { core: { options: { domainPath: 'languages', url: 'http://wp-translate.org', slug: 'tabify-edit-screen', textdomain: 'tabify-edit-screen' } } },});!grunt.registerTask('default', [‘glotpress_download:core']);

Page 23: Bootstrapping your plugin

OTHER COOL PLUGINS

Minify  GIF,  JPEG,  PNG  and  SVG  images

Run  QUnit  unit  tests  in  a  headless  PhantomJS  instance.

Compile  Sass  to  CSS Deploys  a  git  Repo  to  the  WordPress  SVN  repo

Validate  files  with  JSHint.

Run  predefined  tasks  whenever  watched  file  pa>erns  are  added,  changed  or  deleted.

GRUNT-CONTRIB-JSHINT GRUNT-CONTRIB-IMAGEMIN GRUNT-CONTRIB-QUNIT

GRUNT-CONTRIB-WATCH GRUNT-SASS GRUNT-WP-DEPLOY

Page 24: Bootstrapping your plugin

THANKSQUESTIONS? @markoheijnen - markoheijnen.com