From 19f6416d192d180c1d10e811ea5fe31b315bb15c Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 25 Mar 2019 23:12:34 -0400 Subject: [PATCH] add settings page, add export option --- inc/admin/class-roeadmin.php | 32 +++++++- inc/admin/template.php | 30 +++++++ inc/class-roeintegration.php | 151 +++++++++++++++++++++-------------- inc/roe-pressbooks.php | 10 --- roe-pressbooks.php | 47 ++++++++++- 5 files changed, 198 insertions(+), 72 deletions(-) create mode 100644 inc/admin/template.php delete mode 100644 inc/roe-pressbooks.php diff --git a/inc/admin/class-roeadmin.php b/inc/admin/class-roeadmin.php index 4522817..30e7663 100644 --- a/inc/admin/class-roeadmin.php +++ b/inc/admin/class-roeadmin.php @@ -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; + } +} + diff --git a/inc/admin/template.php b/inc/admin/template.php new file mode 100644 index 0000000..1c756e1 --- /dev/null +++ b/inc/admin/template.php @@ -0,0 +1,30 @@ +
+

River of Ebooks API credentials

+ +

+ Register an application here: /keys. +
+ Once it has been whitelisted, your users will be able to use the AppID and secret to publish ebooks to the River of Ebooks. +

+ +
+ + + + + + + + + + +
+ +

The AppID of the application you registered.

+
+ +

The secret of the application you registered.

+
+ +
+
diff --git a/inc/class-roeintegration.php b/inc/class-roeintegration.php index f9d0506..7aa623c 100644 --- a/inc/class-roeintegration.php +++ b/inc/class-roeintegration.php @@ -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; } } diff --git a/inc/roe-pressbooks.php b/inc/roe-pressbooks.php deleted file mode 100644 index 512c93b..0000000 --- a/inc/roe-pressbooks.php +++ /dev/null @@ -1,10 +0,0 @@ -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 '

'; + _e('Please configure your site\'s publisher id and secret. It is required to publish to the River of Ebooks.', 'roe-pressbooks'); + echo '

'; + _e('Settings', 'roe-pressbooks'); + echo '
'; +} + +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 ) ); +}