add settings page, add export option

master
unknown 2019-03-25 23:12:34 -04:00
parent 064ec833ff
commit 19f6416d19
5 changed files with 198 additions and 72 deletions

View File

@ -2,4 +2,34 @@
namespace ROE\Admin;
use ROE
class ROEAdmin {
function __construct () {
add_action('network_admin_menu', [$this, '_roe_admin_menu']);
add_action('network_admin_edit_roepressbooksaction', [$this, '_roe_save_settings']);
}
function _roe_admin_menu () {
add_submenu_page('settings.php', 'RoE Options', 'River of Ebooks', 'manage_network_options', 'roe-pressbooks', [$this, '_roe_plugin_options']);
}
function _roe_plugin_options () {
if (!current_user_can('manage_network_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
require_once(__DIR__ . '/template.php');
}
function _roe_save_settings () {
check_admin_referer('roe-validate'); // nonce security check
update_site_option('roe_pressbooks_key', $_POST['roe_pressbooks_key']);
update_site_option('roe_pressbooks_secret', $_POST['roe_pressbooks_secret']);
wp_redirect(add_query_arg(array(
'page' => 'roe-pressbooks',
'updated' => true), network_admin_url('settings.php')
));
exit;
}
}

30
inc/admin/template.php Normal file
View File

@ -0,0 +1,30 @@
<div class="wrap">
<h1>River of Ebooks API credentials</h1>
<p>
Register an application here: <a href="http://ec2-18-219-223-27.us-east-2.compute.amazonaws.com/keys"><?php echo ROE_BASE_URL; ?>/keys</a>.
<br />
Once it has been whitelisted, your users will be able to use the AppID and secret to publish ebooks to the River of Ebooks.
</p>
<form method="post" action="edit.php?action=roepressbooksaction">
<?php wp_nonce_field( 'roe-validate' ); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="some_field">AppID</label></th>
<td>
<input name="roe_pressbooks_key" class="regular-text" type="text" id="roe-key" value="<?php echo get_site_option('roe_pressbooks_key') ?>" />
<p class="description">The AppID of the application you registered.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="some_field">Application secret</label></th>
<td>
<input name="roe_pressbooks_secret" class="regular-text" type="password" id="roe-secret" value="<?php echo get_site_option('roe_pressbooks_secret') ?>" />
<p class="description">The secret of the application you registered.</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>

View File

@ -2,79 +2,110 @@
namespace ROE;
class ROEIntegration {
use Pressbooks\Modules\Export\Export;
class ROEIntegration extends Export {
const VERSION = '1.0.0';
protected $plugin_slug = 'roe-pressbooks';
/**
* Instance of this class.
*
* @since 1.0.0
* @var object
*/
protected static $instance = null;
private function __construct () {
function __construct ( array $args ) {
$this->bookTitle = get_bloginfo( 'name' );
$this->bookMeta = \Pressbooks\Book::getBookInformation();
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
* Return whether the plugin is usable.
* @since 1.0.0
* @return presence of both key and secret.
*/
public static function get_instance () {
// If the single instance hasn't been set, set it now.
if (null == self::$instance) {
self::$instance = new self;
public static function is_active () {
return get_site_option('roe_pressbooks_key') && get_site_option('roe_pressbooks_secret');
}
/**
* Create $output
* @return bool
*/
function convert () {
/*$output = $this->transform(true);
$this->output = $output;
error_log(print_r($output, true));*/
$siteurl = get_site_url(get_current_blog_id());
$identifier = isset($this->bookMeta['pb_print_isbn']) ? $this->bookMeta['pb_print_isbn'] : "url:md5:".md5($siteurl);
$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
],
"links" => [],
"images" => [
[
"href" => $this->bookMeta['pb_cover_image']
]
]
];
$this->output = $output;
return $this->send();
}
/**
* Check the sanity of $this->output
*
* @return bool
*/
function validate () {
return true;
}
function transform ($return = false) {
if ( ! current_user_can( 'edit_posts' ) ) {
wp_die( __( 'Invalid permission error', 'pressbooks' ) );
}
return self::$instance;
}
/**
* Returns merged array of all ROE user options
* @since 1.0.2
* @return array
*/
private function getUserOptions () {
$other = get_option('roe_other_settings', []);
$result = @array_merge($other);
return $result;
}
/**
* Fired when the plugin is activated.
* @since 1.0.0
*/
function activate () {
if (!current_user_can('activate_plugins')) {
return;
static $buffer;
if ( ! function_exists( 'wxr_cdata' ) ) {
ob_start();
require_once( ABSPATH . 'wp-admin/includes/export.php' );
@export_wp(); // @codingStandardsIgnoreLine
$buffer = ob_get_clean();
}
add_site_option('roe-pressbooks-activated', true);
}
/**
* Fired when the plugin is deactivated.
* @since 1.0.0
*/
function deactivate () {
if (!current_user_can('activate_plugins')) {
return;
if ( $return ) {
return $buffer;
} else {
echo $buffer;
return null;
}
delete_site_option('roe-pressbooks-activated');
}
/**
* Return the plugin slug.
* @since 1.0.0
* @return Plugin slug variable.
*/
function getPluginSlug () {
return $this->plugin_slug;
function send () {
$url = ROE_BASE_URL . "/api/publish";
$content = json_encode($this->output);
$headers = [
"roe-key: " . get_site_option('roe_pressbooks_key'),
"roe-secret: " . get_site_option('roe_pressbooks_secret'),
"Content-Type: application/json"
];
// error_log(print_r($headers, true));
$response = wp_remote_post($url, [
"headers" => $headers,
"body" => $content
]);
// error_log(print_r($response, true));
if ( is_wp_error($response) || $response['response']['code'] !== 200 ) {
return false;
}
return true;
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace ROEPressbooks;
class Something {
/**
* Constructor.
*/
function __construct() {}
}

View File

@ -3,7 +3,7 @@
Plugin Name: River of Ebooks for Pressbooks
Plugin URI: https://pressbooks.org
Description: RoE integration with Pressbooks
Version: 0.0.1
Version: 1.0.0
Author: Pressbooks (Book Oven Inc.)
Author URI: https://pressbooks.org
Requires PHP: 7.0
@ -13,6 +13,8 @@ License: GPL v3 or later
Network: True
*/
define ('ROE_BASE_URL', 'http://ec2-18-219-223-27.us-east-2.compute.amazonaws.com');
// -------------------------------------------------------------------------------------------------------------------
// Check requirements
// -------------------------------------------------------------------------------------------------------------------
@ -55,3 +57,46 @@ if ( ! \Pressbooks\Book::isBook() ) {
);
$updater->setBranch( 'master' );
}
function roe_check_compatibility () {
if ( is_plugin_active('roe-pressbooks/roe-pressbooks.php') && is_network_admin() ) {
if ( ! \ROE\ROEIntegration::is_active() ) {
add_action( 'network_admin_notices', '_roe_show_set_config' );
}
}
}
function _roe_show_set_config () {
echo '<div class="notice notice-warning"><p style="display:inline-block;height:40px;line-height:40px">';
_e('Please configure your site\'s publisher id and secret. It is required to publish to the River of Ebooks.', 'roe-pressbooks');
echo '</p><a class="button" style="float:right;height:30px;margin:15px 0;" href="#">';
_e('Settings', 'roe-pressbooks');
echo '</a></div>';
}
roe_check_compatibility();
add_filter( 'pb_active_export_modules', function ( $modules ) {
if ( isset( $_POST['export_formats']['roe'] ) && \ROE\ROEIntegration::is_active() ) {
$modules[] = '\ROE\ROEIntegration';
}
return $modules;
} );
add_filter( 'pb_export_formats', function ( $formats ) {
if (\ROE\ROEIntegration::is_active()) {
$formats['exotic']['roe'] = __( 'River of Ebooks', 'pressbooks' );
}
return $formats;
} );
if (is_network_admin()) {
new \ROE\Admin\ROEAdmin;
}
add_action( 'http_api_debug', 'viper_http_api_debug', 10, 5 );
function viper_http_api_debug( $response, $type, $class, $args, $url ) {
error_log( 'Request URL: ' . var_export( $url, true ) );
error_log( 'Request Args: ' . var_export( $args, true ) );
error_log( 'Request Response : ' . var_export( $response, true ) );
}