Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ev3rywh3re/6842eeed792e15ee65ee7036b694aca2 to your computer and use it in GitHub Desktop.
Save ev3rywh3re/6842eeed792e15ee65ee7036b694aca2 to your computer and use it in GitHub Desktop.
Mediavine WordPress Engineer Preliminary Questions

Instructions

  • Fork this gist.
  • Modify the code samples to complete the first 2 tasks.
  • Answer the remaining 2 questions.
  • Reply back to the email you were sent with the link to your completed gist by 5:00 pm EST on Wednesday, January 22.

  1. Given an API route for retrieving a specific record, do the following:
    1. Get the id param
    2. Return a 404 if the record isn't found
    3. Return a response with the record if it is found

Note: This task is conceptual and we understand you do not have the full code. Please imagine there is working code up to the point of the task. Please do not complete the imagined portion of the code, nor write a full plugin to complete this task.

Route format:

/publishers/(?P<id>\d+)

/publishers/397

Code that returns the response:

<?php

/**
 * WordPress add_action for custom REST API endpoint for testing available at: /wp-json/publishers/1
 */
function rest_get_publisher_route() {
  register_rest_route(
    'publishers',
    '(?P<id>\d+)',
    array(
      'methods' => 'GET',
      'callback' => 'rest_get_publisher'
    )
  );
}
add_action( 'rest_api_init', rest_get_publisher_route );

/**
 * Return publisher from database or return 404 if not found.
 *
 * For this example I have added an action above to manipulate the REST API enpoints.
 * this could be added to a custom plugin for testing. With add_action above can
 * be tested on a default WordPress site with an active post if included via plugin.
 * Custom REST API route is located at: /wp-json/publishers/1
 *
 * @param string $request Custom WordPress REST API endpoing.
 * @return oject|error Return REST API of publisher found by id.
 */
function get_publisher( \WP_REST_Request $request ) {
    // Task 1-i: Replace `null` with the retreived `id` parameter
    $publisher_id = null;
    // Get request parameters
    $data_id = $request->get_params();
    // Set $publisher_id as found id parameter as created in custom route
    $publisher_id = $data_id['id'];
    
    /*
     * Imagine code that gets the record from the database as `$publisher`
     *  
     * If this was a custom post type we'd use the defined custom post type. For this sample I'm using normal posts.
     */
    $the_post = get_post( $the_id );

    if ( empty( $publisher ) ) {
        // Task 1-ii: Return a 404 response
        return new WP_Error( 'empty_publisher', 'No publisher record found', array( 'status' => 404 ) );
    }

    // Task 1-iii: Return a normal REST response with the `$publisher`
    return new WP_REST_Response( $the_post, 200 );
}
  1. Do the following:
    1. Add a WordPress hook to determine the value of the visible property on some item.
    2. Then use that hook elsewhere to change the value to false;
<?php

function modify_item( $item ) {
    /*
     * `$item` is an object
     */

    // Task 2-i: Set the `visible` property of `$item` to the value of a hook `item_visibility`, with a default value of `true`
    $item = apply_filters( 'item_visibility', $item );

    return $item;
}

/*
 * Elsewhere in the code
 */

function hide_item() {
    // Task 2-ii: Add the `item_visibility` hook to change the `visible property of `$item` to `false`
   if ( $item->visible != false ) $item->visible = false;
}
// Using add_filter defaults, but I understand how multiple values and priorities are used.
add_filter( 'item_visibility', 'hide_item' );
  1. What is your favorite WordPress core function and why?

My favorite is actually a class called wp_http. This is used to make outgoing requests and can be used to interact with other servers and systems. It has several functions and methods and can be read about at: https://developer.wordpress.org/reference/classes/wp_http/

  1. What frustrates you about WordPress, and what's something that you think needs to change?

Documentation is always frustrating and sometimes you will find actions and filters not available on occasional routs. I haven't encountered a good example lately, but it would be good to know when some filters and actions are not available. It sucks to find out by digging though core code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment