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. 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; } } }
  2. 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'] ) ) ); }
  3. 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' ] ); } } } } } }
  4. 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; }
  5. $html .= "<li class='group'>"; $html .= "<a href='".get_permalink()."'>"; if (

    $image ) $html .= "<img src='$image'>"; $html .= "</a>"; $html .= "<h3>Boat Name: <span class='small'>".get_the_title()."</ span></h3>"; $html .= "<h3>Location: <span class='small'>$marina_list</h3></span>"; if ( current_user_can('view_boats') ) $html .= "<$title_tag>Rate: <span class='small'>$$price</span></ $title_tag>"; $html .= "<a href='".get_permalink()."' class='button small'>$button_string</a>"; $html .= "</li>";
  6. <li class='group'> <?php if ( ! empty( $image ) )

    : ?> <a href='<?php echo esc_url( get_permalink() ); ?>'> <img src='<?php echo esc_url( $image ); ?>'> </a> <?php endif; ?> <h3> <?php esc_html_e( 'Boat Name:', 'my-theme' ); ?> <span class='small'>".get_the_title()."</span> </h3> <?php if ( current_user_can('view_boats') ) : ?> <h4> <?php esc_html_e( 'Rate:', 'my-theme' ); ?> <span class='small'>$<?php esc_html( $price ); ?></span> </h4> <?php endif; ?> <a href='<?php echo esc_url( get_permalink() ) ?>' class='button small'><?php echo esc_html( $permalink ); ?></a> </li>
  7. $posts = new WP_Query( [ 'orderby' => 'title', 'order' =>

    'ASC', 'nopaging' => true, 'posts_per_page' => '-1', 'meta_query' => [ [ 'key' => 'featured', 'value' => 1, 'compare' => '=', ], ], 'orderby' => 'RAND', ] );
  8. $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 );
  9. function hello_dashboard_widget() { global $wpdb; echo "<h2>Hello!</h2>"; echo "<p>What would

    you like to do today?</p>"; $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 "<li><a href='".admin_url()."post-new.php?post_type=$type[post_type]'>Create a new $type_clean</a></li>"; } } } __( 'Create a new %s', 'my-theme' ), $type->labels->singular ) ); ?> </a> </li> <?php } ); }
  10. function hello_dashboard_widget() { global $wpdb; ?> <h2><?php esc_html_e( 'Hello!', 'my-theme'

    ); ?></h2> <p><?php esc_html_e( 'What would you like to do today?', 'my-theme' ); ?></p> <?php 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 ) { ?> <li> <a href="<?php echo esc_url( admin_url( 'post-new.php?post_type=' . $type- >name ) ); ?>"> <?php echo esc_html( sprintf( __( 'Create a new %s', 'my-theme' ), $type->labels->singular ) ); ?> </a> </li> <?php } ); }