dharma017
5/31/2016 - 8:42 AM

Import JSON into WordPress

Import JSON into WordPress

<?php

// Published under GPL
// tutorial here: https://codeable.io/community/how-to-import-json-into-wordpress/

class Developer_Import {

  public function __construct() {

    add_action( 'wp_ajax_import_developer', array( $this, 'import_developer' ) );
    add_action( 'wp_ajax_nopriv_import_developer', array( $this, 'import_developer' ) );

  }

  public function import_developer() {

    $developer_data = json_decode( file_get_contents( 'php://input' ) );

    if ( $this->compare_keys() ) {
      $this->insert_or_update($developer_data);
    }

    wp_die();

  }

  private function insert_or_update($developer_data) {

    if ( ! $developer_data)
      return false;

    // We search by the custom field 'developer_id' which stores the id of the 
    // object that is stored in the external service
    $args = array(
      'meta_query' => array(
        array(
          'key'   => 'developer_id',
          'value' => $developer_data->id
        )
      ),
      'post_type'      => 'developer',
      'post_status'    => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
      'posts_per_page' => 1
    );

    $developer = get_posts( $args );

    $developer_id = '';

    if ( $developer )
      $developer_id = $developer[0]->ID;

    $developer_post = array(
      'ID'            => $developer_id,
      'post_title'    => $developer_data->full_name,
      'post_content'  => $developer_data->bio,
      'post_type'     => 'developer',
      // If developer exists then it reuses its post status
      'post_status'   => ( $developer ) ? $developer[0]->post_status : 'publish'
    );

    $developer_id = wp_insert_post( $developer_post );

    if ( $developer_id ) {
      update_post_meta( $developer_id, 'developer_id', $developer_data->id );
      update_post_meta( $developer_id, 'json', addslashes( file_get_contents( 'php://input' ) ) );

      // Remove if you don't need to import tags and make sure
      // the tag name is correct if you do
      wp_set_object_terms( $developer_id, $developer_data->tags, 'developer_tag' );

      $this->generate_background_images( $developer_data->full_name, $developer_data->avatar->large_url );
    }

    print_r( $developer_id );

  }

  private function compare_keys() {

    // Signature should be in a form of algorihm=hash
    // for example: X-Codeable-Signature: sha1=246d2e58593645b1f261b1bbc867fe2a9fc1a682
    if ( ! isset( $_SERVER['HTTP_X_CODEABLE_SIGNATURE'] ) ) {
      throw new \Exception( "HTTP header 'X-Codeable-Signature' is missing." );
    }

    list( $algo, $hash ) = explode( '=', $_SERVER['HTTP_X_CODEABLE_SIGNATURE'], 2 ) + array( '', '' );
    $raw_post = file_get_contents( 'php://input' );

    // Don't forget to define your key!
    if ( $hash !== hash_hmac( $algo, $raw_post, CODEABLE_KEY ) ) {
      throw new \Exception( 'Secret hash does not match.' );
    }

    return true;

  }

}

new Developer_Import();