You are here

blogapi_metaweblog.module in Blog API 8

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

Module file for blogapi_blogger.

File

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

/**
 * @file
 * Module file for blogapi_blogger.
 */
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;

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

/**
 * Callback for metaWeblog.getRecentPosts.
 *
 * @param $ct
 *   Content type machine name.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param int $number_of_posts
 *   The number of recent posts to return.
 *
 * @return array|object
 *   BlogAPI response.
 */
function blogapi_metaweblog_get_recent_posts($ct, $username, $pass, $number_of_posts = 10) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  return $communicator
    ->getRecentPosts($ct, $username, $pass, $number_of_posts, TRUE);
}

/**
 * Callback for metaWeblog.editPost.
 *
 * @param $nid
 *   The node ID.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param $data
 *   The node contents.
 *
 * @param $publish
 *   Boolean Drupal publish status.
 *
 * @return object|string
 *   BlogAPI response.
 */
function blogapi_metaweblog_edit_post($nid, $username, $pass, $data, $publish) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  $formatted = [
    'title' => $data['title'],
    'body' => $data['description'],
    'format' => $data['mt_convert_breaks'],
    'publish' => $publish,
    'comments' => blogapi_metaweblog_decode_comment_code($data['mt_allow_comments']),
  ];
  return $communicator
    ->editPost($nid, $username, $pass, $formatted);
}

/**
 * Callback for metaWeblog.newPost.
 *
 * @param $ct
 *   Content type machine name.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param $data
 *   The node contents.
 *
 * @param $publish
 *   Boolean Drupal publish status.
 *
 * @return bool|object|string
 *   BlogAPI response.
 */
function blogapi_metaweblog_new_post($ct, $username, $pass, $data, $publish) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  $formatted = [
    'title' => $data['title'],
    'body' => $data['description'],
    'format' => $data['mt_convert_breaks'],
    'publish' => $publish,
    'comments' => blogapi_metaweblog_decode_comment_code($data['mt_allow_comments']),
  ];
  return $communicator
    ->newPost($ct, $username, $pass, $formatted);
}

/**
 * Callback for metaWeblog.newMediaObject.
 *
 * @param $ct
 *   Content type machine name.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @param $data
 *   The image contents.
 *
 * @return array|object
 *   BlogAPI response.
 */
function blogapi_metaweblog_new_media($ct, $username, $pass, $data) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  return $communicator
    ->newMedia($ct, $username, $pass, $data);
}

/**
 * Helper function to convert metaWeblog comment codes to Drupal comment codes.
 *
 * @param $comment
 *   The comment code.
 *
 * @return int
 *   BlogAPI response.
 */
function blogapi_metaweblog_decode_comment_code($comment) {
  switch ($comment) {
    case 0:
      return CommentItemInterface::HIDDEN;
    case 1:
      return CommentItemInterface::OPEN;
    case 2:
      return CommentItemInterface::CLOSED;
    default:
      return CommentItemInterface::CLOSED;
  }
}

/**
 * Callback for metaWeblog.getPost.
 *
 * @param $nid
 *   The node ID.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @return mixed
 *   BlogAPI response.
 */
function blogapi_metaweblog_get_post($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 metaWeblog.getCategories.
 *
 * @param $nid
 *   The node ID.
 *
 * @param $username
 *   Drupal username.
 *
 * @param $pass
 *   Drupal password.
 *
 * @return mixed
 *   BlogAPI response.
 */
function blogapi_metaweblog_get_categories($nid, $username, $pass) {
  $communicator = \Drupal::service('service.communicator.blogapi');
  return $communicator
    ->getNodeCategories($nid, $username, $pass);
}

Functions

Namesort descending Description
blogapi_metaweblog_decode_comment_code Helper function to convert metaWeblog comment codes to Drupal comment codes.
blogapi_metaweblog_edit_post Callback for metaWeblog.editPost.
blogapi_metaweblog_get_categories Callback for metaWeblog.getCategories.
blogapi_metaweblog_get_post Callback for metaWeblog.getPost.
blogapi_metaweblog_get_recent_posts Callback for metaWeblog.getRecentPosts.
blogapi_metaweblog_new_media Callback for metaWeblog.newMediaObject.
blogapi_metaweblog_new_post Callback for metaWeblog.newPost.
blogapi_metaweblog_xmlrpc Returns implemented methods.