roe-pressbooks/inc/class-roeintegration.php

113 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2019-03-25 22:02:16 +00:00
<?php
namespace ROE;
2019-03-26 03:12:34 +00:00
use Pressbooks\Modules\Export\Export;
class ROEIntegration extends Export {
2019-03-25 22:02:16 +00:00
const VERSION = '1.0.0';
protected $plugin_slug = 'roe-pressbooks';
2019-03-26 03:12:34 +00:00
function __construct ( array $args ) {
$this->bookTitle = get_bloginfo( 'name' );
$this->bookMeta = \Pressbooks\Book::getBookInformation();
2019-03-25 22:02:16 +00:00
}
/**
2019-03-26 03:12:34 +00:00
* Return whether the plugin is usable.
* @since 1.0.0
* @return presence of both key and secret.
2019-03-25 22:02:16 +00:00
*/
2019-03-26 03:12:34 +00:00
public static function is_active () {
return get_site_option('roe_pressbooks_key') && get_site_option('roe_pressbooks_secret');
2019-03-25 22:02:16 +00:00
}
/**
2019-03-26 03:12:34 +00:00
* Create $output
* @return bool
2019-03-25 22:02:16 +00:00
*/
2019-03-26 03:12:34 +00:00
function convert () {
/*$output = $this->transform(true);
$this->output = $output;
error_log(print_r($output, true));*/
2019-03-25 22:02:16 +00:00
2019-03-26 03:12:34 +00:00
$siteurl = get_site_url(get_current_blog_id());
$identifier = isset($this->bookMeta['pb_print_isbn']) ? ("urn:isbn:" . $this->bookMeta['pb_print_isbn']) : ("url:md5:" . md5($siteurl));
2019-03-26 03:12:34 +00:00
$timestamp = (new \DateTime())->format('c');
$output = [
"metadata" => [
"@type" => "http://schema.org/Book",
"title" => $this->bookMeta['pb_title'],
"author" => $this->bookMeta['pb_authors'],
"identifier" => $identifier,
"publisher" => $siteurl,
"language" => $this->bookMeta['pb_language'],
"modified" => $timestamp
],
2019-03-26 23:39:07 +00:00
"links" => [
[
"href" => $siteurl
]
],
2019-03-26 03:12:34 +00:00
"images" => [
[
"href" => $this->bookMeta['pb_cover_image']
]
]
];
$this->output = $output;
return $this->send();
2019-03-25 22:02:16 +00:00
}
/**
2019-03-26 03:12:34 +00:00
* Check the sanity of $this->output
*
* @return bool
2019-03-25 22:02:16 +00:00
*/
2019-03-26 03:12:34 +00:00
function validate () {
return true;
2019-03-25 22:02:16 +00:00
}
2019-03-26 03:12:34 +00:00
function transform ($return = false) {
if ( ! current_user_can( 'edit_posts' ) ) {
wp_die( __( 'Invalid permission error', 'pressbooks' ) );
}
static $buffer;
if ( ! function_exists( 'wxr_cdata' ) ) {
ob_start();
require_once( ABSPATH . 'wp-admin/includes/export.php' );
@export_wp(); // @codingStandardsIgnoreLine
$buffer = ob_get_clean();
}
if ( $return ) {
return $buffer;
} else {
echo $buffer;
return null;
2019-03-25 22:02:16 +00:00
}
}
2019-03-26 03:12:34 +00:00
function send () {
$url = ROE_BASE_URL . "/api/publish";
$content = json_encode($this->output);
2019-03-26 03:49:06 +00:00
$headers = join("\r\n", [
2019-03-26 03:12:34 +00:00
"roe-key: " . get_site_option('roe_pressbooks_key'),
"roe-secret: " . get_site_option('roe_pressbooks_secret'),
2019-03-26 03:49:06 +00:00
"Content-Type: application/json",
]);
2019-03-26 03:12:34 +00:00
$response = wp_remote_post($url, [
"headers" => $headers,
"body" => $content
]);
if ( is_wp_error($response) || $response['response']['code'] !== 200 ) {
return false;
}
return true;
2019-03-25 22:02:16 +00:00
}
}