You are here

blogapi_blogger.module in Blog API 8

Same filename and directory in other branches
  1. 7.2 modules/blogapi_blogger/blogapi_blogger.module

File

modules/blogapi_blogger/blogapi_blogger.module
View source
<?php

/**
 * @file
 */
use Drupal\Core\Url;

/**
 * @file
 * Module file for blogapi_blogger.
 */

/**
 * Returns implemented methods.
 *
 * Implements hook_xmlrpc().
 */
function blogapi_blogger_xmlrpc() {
  $manager = \Drupal::service('plugin.manager.blogapi');
  $plugin = $manager
    ->createInstance('BloggerProvider');
  return $plugin
    ->getMethods();
}

/**
 * Callback for blogger.deletePost.
 *
 * @param $hash
 *   Unused value.
 *
 * @param $nid
 *   The node ID.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param $publish
 *   Boolean Drupal publish status.
 *
 * @return bool|object
 *   BlogAPI response.
 */
function blogapi_blogger_delete_post($hash, $nid, $username, $pass, $publish) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  return $communicator
    ->deletePost($nid, $username, $pass);
}

/**
 * Callback for blogger.getUsersBlogs.
 *
 * @param $hash
 *   Unused value.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @return array
 *   Return formatted array of available content types.
 */
function blogapi_blogger_get_users_blogs($hash, $username, $pass) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  $user = $communicator
    ->authenticate($username, $pass, TRUE);
  if (!$user) {
    return $communicator
      ->returnXmlError($communicator::BLOGAPI_XML_ERROR_AUTH);
  }
  $content_type = $communicator->blogapiConfig
    ->get('content_types');
  if (empty($content_type)) {
    return [];
  }
  $return = [];
  foreach ($content_type as $type) {
    if ($type) {
      $url = Url::fromUri('internal:/user/' . $user
        ->id())
        ->setAbsolute();
      $return[] = [
        'url' => $url
          ->toString(),
        'blogid' => $type,
        'blogName' => $user
          ->getUsername() . ': ' . $type,
      ];
    }
  }
  return $return;
}

/**
 * Callback for blogger.getUserInfo.
 *
 * @param $hash
 *   Unused value.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @return array
 *   Return formatted array of user info.
 */
function blogapi_blogger_get_user_info($hash, $username, $pass) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  $user = $communicator
    ->authenticate($username, $pass, TRUE);
  if (!$user) {
    return $communicator
      ->returnXmlError($communicator::BLOGAPI_XML_ERROR_AUTH);
  }
  $uid = $user
    ->id();
  $name = $user
    ->getDisplayName();
  $email = $user
    ->getEmail();
  $url = Url::fromUri('internal:/user/' . $uid)
    ->setAbsolute();
  $strings = [];
  if (is_string($name)) {
    $strings = explode(' ', $name);
  }
  return [
    'userid' => $uid,
    'firstname' => isset($strings[0]) ? $strings[0] : $username,
    'lastname' => isset($strings[1]) ? $strings[1] : '',
    'nickname' => $username,
    'email' => $email,
    'url' => $url
      ->toString(),
  ];
}

/**
 * Callback for blogger.newPost.
 *
 * @param $hash
 *   Unused variable.
 *
 * @param $ct
 *   Content type machine name.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param $content
 *   Node contents.
 *
 * @param $publish
 *   Bool variable with publish status.
 *
 * @return mixed
 *   Return a response.
 */
function blogapi_blogger_new_post($hash, $ct, $username, $pass, $content, $publish) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  $data = [
    'type' => $ct,
    'publish' => $publish,
    'title' => blogapi_blogger_get_title($content),
    'body' => preg_replace('/<title>.*?<\\/title>/i', '', $content),
  ];
  return $communicator
    ->newPost($ct, $username, $pass, $data);
}

/**
 * Callback for blogger.editPost.
 *
 * @param $hash
 *   Unused variable.
 *
 * @param $nid
 *   Drupal node ID.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param $content
 *   Node contents.
 *
 * @param $publish
 *   Bool variable with publish status.
 *
 * @return mixed
 *   Return a response.
 */
function blogapi_blogger_edit_post($hash, $nid, $username, $pass, $content, $publish) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  $formatted = [
    'title' => blogapi_blogger_get_title($content),
    'body' => preg_replace('/<title>.*?<\\/title>/i', '', $content),
    'publish' => $publish,
  ];
  return $communicator
    ->editPost($nid, $username, $pass, $formatted);
}

/**
 * Callback for blogger.editPost.
 *
 * @param $hash
 *   Unused variable.
 *
 * @param $nid
 *   Drupal node ID.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @return mixed
 *   Return a response.
 */
function blogapi_blogger_get_post($hash, $nid, $username, $pass) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  $node = $communicator
    ->getPost($nid, $username, $pass);
  if ($communicator
    ->responseIsError($node)) {
    return $node;
  }
  return $communicator
    ->formatXml($node, TRUE);
}

/**
 * Callback for blogger.getRecentPosts.
 *
 * @param $hash
 *   Unused variable.
 *
 * @param $ct
 *   Drupal content type.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param $number_of_posts
 *   Number of posts to return.
 *
 * @param bool $bodies
 *   Bool to determine if it should return the node body.
 *
 * @return mixed
 *   Returns a response.
 */
function blogapi_blogger_get_recent_posts($hash, $ct, $username, $pass, $number_of_posts, $bodies = TRUE) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  return $communicator
    ->getRecentPosts($ct, $username, $pass, $number_of_posts, $bodies);
}

/**
 * Helper function to extract title.
 *
 * @param $string
 *   The input string.
 *
 * @return string
 *   The returned title.
 */
function blogapi_blogger_get_title($string) {
  if (preg_match('/<title>(.*?)<\\/title>/i', $string, $title)) {
    return strip_tags($title[0]);
  }
  return '';
}

Functions

Namesort descending Description
blogapi_blogger_delete_post Callback for blogger.deletePost.
blogapi_blogger_edit_post Callback for blogger.editPost.
blogapi_blogger_get_post Callback for blogger.editPost.
blogapi_blogger_get_recent_posts Callback for blogger.getRecentPosts.
blogapi_blogger_get_title Helper function to extract title.
blogapi_blogger_get_users_blogs Callback for blogger.getUsersBlogs.
blogapi_blogger_get_user_info Callback for blogger.getUserInfo.
blogapi_blogger_new_post Callback for blogger.newPost.
blogapi_blogger_xmlrpc Returns implemented methods.