Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Code Commentary

Tyrel Kelsey
September 23, 2017

A Code Commentary

Ever wonder how your code stacks up? Don’t have another developer to bounce ideas off of? Submit a code sample to have to have it reviewed live by a panel of Senior Engineers.

A Code Commentary is a live code review/panel where developers (that’s you!) can submit a small code sample and receive feedback from several experts on that code live. We’ll do a quick review of the code, provide helpful tips and tricks from the examples given, and talk to participants about how everyone in the room can improve their code bases. Reviews will be broken up by Q&A sessions and code samples can be anonymous if desired.

Code snippets should not be more than 200 lines and have a clear purpose. Plugins and single-purpose snippets are encouraged.

Tyrel Kelsey

September 23, 2017
Tweet

More Decks by Tyrel Kelsey

Other Decks in Programming

Transcript

  1. A CODE COMMENTARY
    MIKE SELANDER, TYREL KELSEY

    View full-size slide

  2. function save_options() {
    global $themename, $shortname, $options;
    if ( $_GET['page'] == basename(__FILE__) ) {
    if ( 'save' == $_REQUEST['action'] ) {
    foreach ($options as $value) {
    update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
    foreach ($options as $value) {
    if( isset( $_REQUEST[ $value['id'] ] ) )
    { update_option( $value['id'], $_REQUEST[ $value['id'] ] ); } else
    { delete_option( $value['id'] ); } }
    header("Location: themes.php?page=functions.php&saved=true");
    die;
    } else if( 'reset' == $_REQUEST['action'] ) {
    foreach ($options as $value) {
    delete_option( $value['id'] ); }
    header("Location: themes.php?page=functions.php&reset=true");
    die;
    }
    }
    }

    View full-size slide

  3. function save_options() {
    // Nonce check.
    if (
    ! isset( $_POST['my_nonce'] )
    || ! wp_verify_nonce( $_POST['my_nonce'], 'nonce_action' )
    ) {
    return;
    }
    // Validate that option is being pushed.
    if ( ! isset( $_POST['my_value'] ) ) {
    return;
    }
    // Only update one option at a time.
    // Sanitize and unslash before saving.
    update_option(
    'my_value',
    sanitize_text_field( wp_unslash( $_POST['my_value'] ) )
    );
    }

    View full-size slide

  4. function find_partners( $partners ) {
    $links = array();
    if ( isset( $partners ) ) {
    foreach( $partners as $partner ) {
    if( $partner['partner_name'] == 'AngelList' ) {
    for( $i = 1; $i <= 3; $i++ ) {
    if( isset( $partner['link_' . $i . '_url' ] ) && !empty(
    $partner['link_' . $i . '_url' ] )
    && isset( $partner['link_' . $i . '_name' ] ) && !
    empty( $partner['link_' . $i . '_name' ] ) ) {
    $links[] = array( 'label' => $partner['link_' . $i .
    '_name' ], 'url' => $partner['link_' . $i . '_url' ] );
    }
    }
    }
    }
    }
    }

    View full-size slide

  5. function find_partners( $partners ) {
    // Make sure that we have valid partners first.
    if ( empty( $partners ) ) {
    return [];
    }
    // We only want to look at AngelList partners.
    $angellist_partners = array_filter( $partners, function( $partner ) {
    return 'AngelList' === $partner['partner_name'];
    } );
    // Map AngelList partners to their links and append to our links array.
    $links = [];
    foreach ( $angellist_partners as $partner ) {
    $i = 1;
    while ( ! empty( $partner[ 'link_' . $i . '_url' ] ) ) {
    $links[] = [
    'label' => $partner[ 'link_' . $i . '_name' ],
    'url' => $partner[ 'link_' . $i . '_url' ],
    ];
    $i++;
    }
    }
    return $links;
    }

    View full-size slide

  6. $html .= "";
    $html .= "";
    if ( $image )
    $html .= "";
    $html .= "";
    $html .= "Boat Name: ".get_the_title()."
    span>";
    $html .= "Location: $marina_list";
    if ( current_user_can('view_boats') )
    $html .= "<$title_tag>Rate: $$price
    $title_tag>";
    $html .= "$button_string";
    $html .= "";

    View full-size slide









  7. ".get_the_title()."




    $




    View full-size slide

  8. $posts = new WP_Query( [
    'orderby' => 'title',
    'order' => 'ASC',
    'nopaging' => true,
    'posts_per_page' => '-1',
    'meta_query' => [
    [
    'key' => 'featured',
    'value' => 1,
    'compare' => '=',
    ],
    ],
    'orderby' => 'RAND',
    ] );

    View full-size slide

  9. $posts = new WP_Query( [
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => '100',
    'no_found_rows' => true,
    'update_post_term_cache' => false,
    'update_post_meta_cache' => false,
    'meta_query' => [
    [
    'key' => 'featured',
    'value' => 'bug #23268',
    'compare' => 'EXISTS',
    ],
    ],
    ] );
    shuffle( $posts->posts );

    View full-size slide

  10. function hello_dashboard_widget() {
    global $wpdb;
    echo "Hello!";
    echo "What would you like to do today?";
    $querystr = "SELECT DISTINCT post_type\n";
    $querystr .= "FROM $wpdb->posts\n";
    $types = $wpdb->get_results($querystr, ARRAY_A);
    // Declare post types we don't want to show
    $bad_types[] = 'attachment';
    $bad_types[] = 'deprecated_log';
    $bad_types[] = 'nav_menu_item';
    $bad_types[] = 'revision';
    foreach ( $types as $type ) {
    // if post type isn't one of the ones we declared
    if ( !in_array( $type['post_type'], $bad_types ) ) {
    $type_clean = ucwords($type['post_type']);
    echo "Create a new
    $type_clean";
    }
    }
    } __( 'Create a new %s', 'my-theme' ),
    $type->labels->singular
    ) ); ?>


    } );
    }

    View full-size slide

  11. function hello_dashboard_widget() {
    global $wpdb;
    ?>


    post_type_add_new_link();
    }
    function post_type_add_new_link() {
    $post_types = get_post_types( [ 'public' => true ], 'objects' );
    array_walk( $post_types, function( $type ) {
    ?>


    __( 'Create a new %s', 'my-theme' ),
    $type->labels->singular
    ) ); ?>


    } );
    }

    View full-size slide