Successfully reported this slideshow.
Your SlideShare is downloading. ×

Wordpress development: A Modern Approach

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 26 Ad

More Related Content

Slideshows for you (20)

Similar to Wordpress development: A Modern Approach (20)

Advertisement

Recently uploaded (20)

Wordpress development: A Modern Approach

  1. 1. Wordpress Develompent A Modern Approach
  2. 2. Who am I ? Backend Developer @Populis @whitekross on twitter Alessandro Fiore
  3. 3. The most used PHP software https://wordpress.org/download/counter/
  4. 4. 24,5%(of the Web) http://w3techs.com/technologies/overview/content_management/all
  5. 5. Innovator’s Dilemma ?
  6. 6. Wordpress, we have some problems ● Poor Core Codebase ● 3th Party Codebase even poorer ● Lack of standards
  7. 7. Use Better Tools!
  8. 8. Wordpress ❤ Composer Custom Installers At times it may be necessary for a package to require additional actions during installation, such as installing packages outside of the default vendor library. { "name": "my-vendor/my-lib-plugin", "type": "my-lib-plugin", "require": { "my-vendor/my-lib-installer": "*" } }
  9. 9. ComposerInstallerInstallerInterface interface InstallerInterface { //Decides if the installer supports the given type public function supports($packageType); //Checks that provided package is installed. public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package); //Installs specific package. public function install(InstalledRepositoryInterface $repo, PackageInterface $package); //Updates specific package. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target); //Uninstalls specific package. public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package); //Returns the installation path of a package public function getInstallPath(PackageInterface $package); }
  10. 10. A basic Installer Plugin 1. The package file: composer.json 2. The Plugin class, e.g.: MyProjectComposerPlugin.php, implements ComposerPluginPluginInterface. 3. The Installer class, e.g.: MyProjectComposerInstaller.php, implements ComposerInstallerInstallerInterface.
  11. 11. Wordpress Core itself is a dependency 2) johnpbloch/wordpress A fork of WordPress with Composer support added. Synced every 15 minutes. 1) johnpbloch/wordpress-core-installer A custom installer to handle deploying WordPress with composer.
  12. 12. Wordpress Core itself is a dependency use ComposerInstallerLibraryInstaller; use ComposerPackagePackageInterface; class WordPressCoreInstaller extends LibraryInstaller { const TYPE = 'wordpress-core'; public function getInstallPath( PackageInterface $package ) { $extra = $package->getExtra(); if ( ! $installationDir && ! empty( $extra['wordpress-install-dir'] ) ) { $installationDir = $extra['wordpress-install-dir']; } return $installationDir; } public function supports( $packageType ) { return self::TYPE === $packageType; } }
  13. 13. Wordpress Core itself is a dependency "require": { "johnpbloch/wordpress": "4.3.1" }, "extra": { "wordpress-install-dir": "wp" },
  14. 14. Manage Plugins with Composer composer/installers A Multi-Framework Composer Library Installer. It will magically install their package to the correct location based on the specified package type. ● wordpress-plugin => wp-content/plugins/{$name} ● wordpress-theme => wp-content/themes/{$name} ● wordpress-muplugin => wp-content/mu-plugins/{$name}
  15. 15. Manage Plugins with Composer "extra": { "installer-paths": { "src/mu-plugins/{$name}/": ["type:wordpress-muplugin"], "src/plugins/{$name}/": ["type:wordpress-plugin"], "src/themes/{$name}/": ["type:wordpress-theme"] }, }
  16. 16. Manage Plugins with Composer Limited to plugins that already have a composer.json. What about 3th party plugins from https://wordpress.org/plugins/ ? Wordpress Packagist Mirrors the WordPress Plugin and Theme directories as a Composer repository.
  17. 17. Manage Plugins with Composer "repositories":[{ "type":"composer", "url":"http://wpackagist.org" } ], "require": { "aws/aws-sdk-php":"*", "wpackagist-plugin/akismet":"dev", "wpackagist-plugin/captcha":"3.9", "wpackagist-theme/hueman":"*" },
  18. 18. Wordpress ❤ Composer ├── composer.json ├── composer.lock ├── index.php ├── src/ ├── vendor/ └── wp/ ├── index.php ├── wp-activate.php ├── wp-admin/ ├── wp-blog-header.php ├── wp-comments-post.php ├── wp-config.php ├── wp-content/ ├── wp-cron.php ├── wp-includes/ ├── wp-links-opml.php ├── wp-load.php ├── wp-login.php ├── wp-mail.php ├── wp-settings.php ├── wp-signup.php ├── wp-trackback.php └── xmlrpc.php
  19. 19. Change WP Directory Structure // INDEX.php (under our new webroot) /** Loads the WordPress Environment and Template */ require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' ); /** Change Content dir */ define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/../src' ); define( 'WP_CONTENT_URL', 'http://<domain>//src' ); // WP_CONFIG.php /** Change WORDPRESS url */ define( 'WP_SITEURL','http://<domain>/wp'); define( 'WP_HOME','http://<domain>/');
  20. 20. A CLI for Wordpress composer require wp-cli/wp-cli WP-CLI is a set of command-line tools for managing WordPress installations.
  21. 21. Run Commands with WP-CLI wp <command> <sub-command> <params> ● core: Download, install, update, manage WordPress. ● db: Perform basic database operations. ● plugin: Manage, install, activate, deactivate plugins. ● post: Manage, generate, publish posts. ● scaffold: Generate code for post types, plugins, etc. ● and many more... http://wp-cli.org/commands/
  22. 22. Wordpress Automated Testing Official test suite repository. https://develop.svn.wordpress.org/trunk/ Time: 4.58 minutes, Memory: 97.75Mb There was 1 failure: FAILURES! Tests: 4251, Assertions: 15938, Failures: 1, Skipped: 65. https://make.wordpress.org/core/handbook/testing/automated-testing/
  23. 23. Test Your Plugins! $ wp scaffold plugin test_plugin ├──bin/ │ └──install-wp-tests.sh ├──phpunit.xml ├──test_plugin.php └──tests/ ├──bootstrap.php └──test-sample.php └──.travis.yml // This will install WP test suite // under “/tmp/wordpress” $ bash bin/install-wp-tests.sh wordpress_test root ''
  24. 24. Test Your Plugins! class FooTest extends WP_UnitTestCase { function test_bar() { // replace this your test $this->assertTrue( true ); } } ● Object Factories: $this->factory->post->create(); ● Custom Asserts: $response = wp_remote_get( $url ); $this->assertWPError( $response ); $this->assertQueryTrue( 'is_tag', 'is_archive' ); ● Useful Methods: setUp() | tearDown() | go_to() | remove_added_uploads() ecc..
  25. 25. Grazie! Code Samples: ● https://github.com/whitekross/wp-composer ● https://github.com/whitekross/wp-badge-poser Useful Links: ● https://roots.io/bedrock/ ● http://composer.rarst.net/ ● http://codesymphony.co/writing-wordpress-plugin-unit-tests/

×