$30 off During Our Annual Pro Sale. View Details »

Your Code Can be Poetry Too

Your Code Can be Poetry Too

Presented on September 8th 2023 at WordCamp The Netherlands, Arnhem, The Netherlands.
https://netherlands.wordcamp.org/2023/session/your-code-can-be-poetry-too/
---------------------------------------------------------------
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…

Juliette Reinders Folmer

September 08, 2023
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. Your Code Can be Poetry Too
    Introduction to WordPressCS

    View Slide

  2. Hello!
    Juliette
    Reinders Folmer
    @jrfnl @jrf
    @[email protected]

    View Slide

  3. WordPressCS !== Code Style

    View Slide

  4. WordPressCS !== Code Style

    View Slide

  5. WordPressCS === Coding Standards

    View Slide

  6. WordPressCS === Quality Control

    View Slide

  7. “ Only half of
    programming is coding.
    The other 90% is
    debugging.

    View Slide

  8. hotblack

    View Slide

  9. Marnhe du Plooy

    View Slide

  10. Reviewing Code [1]
    function my_prefix_print_link( $url, $link_text ) {
    printf(
    '%2$s',
    $url,
    esc_html( $link_text ),
    );
    }

    View Slide

  11. Reviewing Code [1]
    function my_prefix_print_link( $url, $link_text ) {
    printf(
    '%2$s',
    $url,
    esc_html( $link_text ),
    );
    }

    View Slide

  12. 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']
    );
    }
    }

    View Slide

  13. 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']
    );
    }
    }

    View Slide

  14. Reviewing Code [3]
    function admin_page_header() {
    ?>


    My Admin Page
    = _e( 'Don\'t forget to save after changing the
    settings', TEXT_DOMAIN ); ?>
    }

    View Slide

  15. Reviewing Code [3]
    function admin_page_header() {
    ?>


    My Admin Page
    = _e( 'Don\'t forget to save after changing the
    settings', TEXT_DOMAIN ); ?>
    }

    View Slide

  16. 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.
    }
    }

    View Slide

  17. 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.
    }
    }

    View Slide

  18. How WordPressCS Can Help
    ✓ Security
    ✓ Interoperability
    ✓ WP Compatibility
    ✓ Internationalization
    ✓ Best Practices
    ✓ Documentation
    ✓ Consistency
    ✓ Readability
    ✓ PHP Compatibility

    View Slide

  19. PHP
    • http://php.net/download
    Comp
    oser
    • https://getcomposer.org/download/
    Install
    • Install via Composer:
    composer global require –dev
    wp-coding-standards/wpcs
    phpcompatibility/phpcompatibility-wp
    Update
    • Update via Composer:
    composer global update wp-coding-standards/wpcs
    --with-dependencies
    Run
    • vendor/bin/phpcs -ps .
    --standard=WordPress,PHPCompatibilityWP
    Install / Update

    View Slide

  20. What Was Included in WordPressCS 2.x ?
    PHP_CodeSniffer
    Generic
    PEAR
    PSR1, PSR2, PSR12
    Squiz
    Zend
    WordPress
    WordPress
    WordPress-Core
    WordPress-Docs
    WordPress-Extra
    PHPCompatibilityWP
    PHPCompatibility
    PHPCompatibilityWP
    Composer PHPCS Plugin

    View Slide

  21. WordPressCS
    2.x vs 3.x
    Why did it take so long ??????
    • Re-architecture
    • Rules for modern PHP
    • PHP 7.4
    • PHP 8.0
    • PHP 8.1
    • PHP 8.2

    View Slide

  22. What Is Included in WordPressCS 3.x ?
    PHP_CodeSniffer
    Generic
    PEAR
    PSR1, PSR2, PSR12
    Squiz
    Zend
    PHPCSUtils
    WordPress
    WordPress
    WordPress-Core
    WordPress-Docs
    WordPress-Extra
    PHPCSExtra
    Universal
    NormalizedArrays
    Modernizer
    PHPCompatibility
    WP
    PHPCompatibility
    PHPCompatibility
    WP
    Composer PHPCS Plugin

    View Slide

  23. Recommended Reading for Upgrading:
    • Changelog
    • Upgrade Guide for end-users/ruleset maintainers
    • Upgrade Guide for devs of external standards

    View Slide

  24. Minimal Ruleset – phpcs.xml.dist


    Settings for Project X
    .
    /vendor/





    View Slide

  25. Adding Configuration [1]

    ...






    View Slide

  26. Adding Configuration [2]

    ...









    View Slide

  27. Adding Configuration [3]

    ...









    View Slide

  28. elvis santana
    PHP_CodeSniffer
    Pro-tips

    View Slide

  29. Explain

    View Slide

  30. phpcs --standard=WordPress --generator=Text (Markdown|HTML)

    View Slide

  31. --filter=GitModified
    --filter=GitStaged

    View Slide

  32. “ Open Source Software is not Free.
    There is a HUGE cost.
    You are generally just not the
    one paying the price…

    View Slide

  33. This Project Needs Your Help
    https://opencollective.com/thewpcc/contribute/wp-php-63406
    sfluehnsdorf

    View Slide

  34. Thanks!
    Any
    questions ?
    Slides: https://speakerdeck.com/jrf
    @jrfnl @jrf
    @[email protected]

    View Slide