Your Code Can be Poetry Too

Your Code Can be Poetry Too

Presented on December 3rd 2019 at the Leiden WP meetup, Leiden, The Netherlands.
https://www.meetup.com/Leiden-WordPress-Meetup/events/266101021/
---------------------------------------------------------------
Ever heard of the WordPress Coding standards ? Ever wondered why you - and your development team - should use them ?

In contrast to most coding standards, using the WordPress Coding Standards is about so much more than just code style. It is about best practices, modern code, preventing conflicts with other themes and plugins and can even help safeguard you against some common security vulnerabilities.

No matter whether you are a developer or you can't tell divs from eval's, the WordPress Coding Standards can help you. Let me tell you how...

Relevant link: https://github.com/jrfnl/QA-WP-Projects

2776198ea9584b6c0d4b494293b8d635?s=128

Juliette Reinders Folmer

December 03, 2019
Tweet

Transcript

  1. Your Code Can be Poetry Too Introduction to WordPressCS

  2. Hello! Juliette Reinders Folmer @jrf_nl @jrfnl @jrf

  3. WordPressCS !== Code Style

  4. WordPressCS === Coding Standards

  5. WordPressCS === Quality Control

  6. hotblack

  7. Marnhe du Plooy

  8. Reviewing Code [1] function my_prefix_print_link( $url, $link_text ) { printf(

    '<a href="%1$s">%2$s</a>', $url, esc_html( $link_text ), ); }
  9. Reviewing Code [1] function my_prefix_print_link( $url, $link_text ) { printf(

    '<a href="%1$s">%2$s</a>', $url, esc_html( $link_text ), ); }
  10. Reviewing Code [2] function my_prefix_verify_answer() { if ( strpos( $_POST['phrase'],

    "It's a brand new day" ) != false ) { echo esc_html( 'Congratulations, you got it right!', 'domain' ); } else { printf( esc_html( 'Sorry, %s was the wrong answer', 'domain' ), $_POST['phrase'] ); } }
  11. Reviewing Code [2] function my_prefix_verify_answer() { if ( strpos( $_POST['phrase'],

    "It's a brand new day" ) != false ) { echo esc_html( 'Congratulations, you got it right!', 'domain' ); } else { printf( esc_html( 'Sorry, %s was the wrong answer', 'domain' ), $_POST['phrase'] ); } }
  12. Reviewing Code [3] function admin_page_header() { ?> <div class="wrap"> <?php

    screen_icon(); ?> <h2>My Admin Page</h2> <p><?= _e( 'Don\'t forget to save after changing the settings', TEXT_DOMAIN ); ?></p> <?php }
  13. Reviewing Code [3] function admin_page_header() { ?> <div class="wrap"> <?php

    screen_icon(); ?> <h2>My Admin Page</h2> <p><?= _e( 'Don\'t forget to save after changing the settings', TEXT_DOMAIN ); ?></p> <?php }
  14. Reviewing Code [4] function my_prefix_find_post( $search ) { global $wpdb,

    $id; $result = $wpdb->get_col( "SELECT post_id FROM $wpdb->posts WHERE post_title LIKE '$search';" ); foreach ( $result as $id ) { // Do something. } }
  15. Reviewing Code [4] function my_prefix_find_post( $search ) { global $wpdb,

    $id; $result = $wpdb->get_col( "SELECT post_id FROM $wpdb->posts WHERE post_title LIKE '$search';" ); foreach ( $result as $id ) { // Do something. } }
  16. How WordPressCS Can Help  Security  Interoperability  PHP

    Compatibility  WP Compatibility  Internationalization  Best Practices  Consistency  Readability
  17. QA WP Projects PHP Compatibility Standard WordPress Coding Standards PHP

    Codesniffer
  18. PHP • http://php.net/download Compo ser • https://getcomposer.org/download/ Install • Install

    via Composer: composer global require --dev jrfnl/qawpprojects Update • Update via Composer: composer global update jrfnl/qawpprojects --with-dependencies Install / Update
  19. What's Included in QA WP Projects ? PHP_CodeSniffer PEAR, PSR1,

    PSR2, PSR12, Squiz, Zend WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra PHPCompatibilityWP PHPCompatibility PHPCompatibilityParagonie WP-QA-Basic WP-QA-Strict
  20. Minimal Ruleset – phpcs.xml.dist <?xml version="1.0"?> <ruleset name="My Project Ruleset">

    <description>Settings for Project X</description> <file>.</file> <exclude-pattern>*/vendor/*</exclude-pattern> <arg value="sp"/> <arg name="extensions" value="php"/> <arg name="basepath" value="./"/> <rule ref="WordPress"/> </ruleset>
  21. Adding Configuration [1] <ruleset> ... <!-- Also check for PHP

    cross-version compatibility. --> <config name="testVersion" value="5.6-"/> <rule ref="PHPCompatibilityWP"/> <!-- Set the minimum supported WP version. This is used by several sniffs. --> <config name="minimum_supported_wp_version" value="5.2"/> </ruleset>
  22. Adding Configuration [2] <ruleset> ... <!-- Verify that the correct

    text domain is used. --> <rule ref="WordPress.WP.I18n"> <properties> <property name="text_domain" type="array"> <element value="my-textdomain"/> </property> </properties> </rule> </ruleset>
  23. Adding Configuration [3] <ruleset> ... <!-- Verify that everything in

    the global namespace is prefixed with a plugin specific prefix. --> <rule ref="WordPress.NamingConventions.PrefixAllGlobals"> <properties> <property name="prefixes" type="array"> <element value="my_prefix"/> </property> </properties> </rule> </ruleset>
  24. Thanks! Any questions ? Slides: https://speakerdeck.com/jrf Code: https://github.com/jrfnl/ qa-wp-projects @jrf_nl

    @jrfnl @jrf